]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/dwarf2out.c
Address Richard's review comments.
[thirdparty/gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
28
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
34
35 /* DWARF2 Abbreviation Glossary:
36
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
42
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
45
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
48
49 DIE = Debugging Information Entry
50
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
54
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "tree.h"
64 #include "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "function.h"
68 #include "emit-rtl.h"
69 #include "gimple.h"
70 #include "version.h"
71 #include "flags.h"
72 #include "hard-reg-set.h"
73 #include "regs.h"
74 #include "insn-config.h"
75 #include "reload.h"
76 #include "function.h"
77 #include "output.h"
78 #include "expr.h"
79 #include "except.h"
80 #include "dwarf2.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "ggc.h"
85 #include "md5.h"
86 #include "tm_p.h"
87 #include "diagnostic.h"
88 #include "tree-pretty-print.h"
89 #include "debug.h"
90 #include "target.h"
91 #include "common/common-target.h"
92 #include "langhooks.h"
93 #include "hash-table.h"
94 #include "cgraph.h"
95 #include "input.h"
96 #include "ira.h"
97 #include "lra.h"
98 #include "dumpfile.h"
99 #include "opts.h"
100 #include "tree-dfa.h"
101 #include "gdb/gdb-index.h"
102
103 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
104 static rtx last_var_location_insn;
105 static rtx cached_next_real_insn;
106
107 #ifdef VMS_DEBUGGING_INFO
108 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
109
110 /* Define this macro to be a nonzero value if the directory specifications
111 which are output in the debug info should end with a separator. */
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
113 /* Define this macro to evaluate to a nonzero value if GCC should refrain
114 from generating indirect strings in DWARF2 debug information, for instance
115 if your target is stuck with an old version of GDB that is unable to
116 process them properly or uses VMS Debug. */
117 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
118 #else
119 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
120 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
121 #endif
122
123 /* ??? Poison these here until it can be done generically. They've been
124 totally replaced in this file; make sure it stays that way. */
125 #undef DWARF2_UNWIND_INFO
126 #undef DWARF2_FRAME_INFO
127 #if (GCC_VERSION >= 3000)
128 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
129 #endif
130
131 /* The size of the target's pointer type. */
132 #ifndef PTR_SIZE
133 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
134 #endif
135
136 /* Array of RTXes referenced by the debugging information, which therefore
137 must be kept around forever. */
138 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
139
140 /* A pointer to the base of a list of incomplete types which might be
141 completed at some later time. incomplete_types_list needs to be a
142 vec<tree, va_gc> *because we want to tell the garbage collector about
143 it. */
144 static GTY(()) vec<tree, va_gc> *incomplete_types;
145
146 /* A pointer to the base of a table of references to declaration
147 scopes. This table is a display which tracks the nesting
148 of declaration scopes at the current scope and containing
149 scopes. This table is used to find the proper place to
150 define type declaration DIE's. */
151 static GTY(()) vec<tree, va_gc> *decl_scope_table;
152
153 /* Pointers to various DWARF2 sections. */
154 static GTY(()) section *debug_info_section;
155 static GTY(()) section *debug_skeleton_info_section;
156 static GTY(()) section *debug_abbrev_section;
157 static GTY(()) section *debug_skeleton_abbrev_section;
158 static GTY(()) section *debug_aranges_section;
159 static GTY(()) section *debug_addr_section;
160 static GTY(()) section *debug_macinfo_section;
161 static GTY(()) section *debug_line_section;
162 static GTY(()) section *debug_skeleton_line_section;
163 static GTY(()) section *debug_loc_section;
164 static GTY(()) section *debug_pubnames_section;
165 static GTY(()) section *debug_pubtypes_section;
166 static GTY(()) section *debug_str_section;
167 static GTY(()) section *debug_str_dwo_section;
168 static GTY(()) section *debug_str_offsets_section;
169 static GTY(()) section *debug_ranges_section;
170 static GTY(()) section *debug_frame_section;
171
172 /* Maximum size (in bytes) of an artificially generated label. */
173 #define MAX_ARTIFICIAL_LABEL_BYTES 30
174
175 /* According to the (draft) DWARF 3 specification, the initial length
176 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
177 bytes are 0xffffffff, followed by the length stored in the next 8
178 bytes.
179
180 However, the SGI/MIPS ABI uses an initial length which is equal to
181 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
182
183 #ifndef DWARF_INITIAL_LENGTH_SIZE
184 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
185 #endif
186
187 /* Round SIZE up to the nearest BOUNDARY. */
188 #define DWARF_ROUND(SIZE,BOUNDARY) \
189 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
190
191 /* CIE identifier. */
192 #if HOST_BITS_PER_WIDE_INT >= 64
193 #define DWARF_CIE_ID \
194 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
195 #else
196 #define DWARF_CIE_ID DW_CIE_ID
197 #endif
198
199
200 /* A vector for a table that contains frame description
201 information for each routine. */
202 #define NOT_INDEXED (-1U)
203 #define NO_INDEX_ASSIGNED (-2U)
204
205 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
206
207 struct GTY(()) indirect_string_node {
208 const char *str;
209 unsigned int refcount;
210 enum dwarf_form form;
211 char *label;
212 unsigned int index;
213 };
214
215 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
216
217 /* With split_debug_info, both the comp_dir and dwo_name go in the
218 main object file, rather than the dwo, similar to the force_direct
219 parameter elsewhere but with additional complications:
220
221 1) The string is needed in both the main object file and the dwo.
222 That is, the comp_dir and dwo_name will appear in both places.
223
224 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
225 DW_FORM_GNU_str_index.
226
227 3) GCC chooses the form to use late, depending on the size and
228 reference count.
229
230 Rather than forcing the all debug string handling functions and
231 callers to deal with these complications, simply use a separate,
232 special-cased string table for any attribute that should go in the
233 main object file. This limits the complexity to just the places
234 that need it. */
235
236 static GTY ((param_is (struct indirect_string_node)))
237 htab_t skeleton_debug_str_hash;
238
239 static GTY(()) int dw2_string_counter;
240
241 /* True if the compilation unit places functions in more than one section. */
242 static GTY(()) bool have_multiple_function_sections = false;
243
244 /* Whether the default text and cold text sections have been used at all. */
245
246 static GTY(()) bool text_section_used = false;
247 static GTY(()) bool cold_text_section_used = false;
248
249 /* The default cold text section. */
250 static GTY(()) section *cold_text_section;
251
252 /* Forward declarations for functions defined in this file. */
253
254 static char *stripattributes (const char *);
255 static void output_call_frame_info (int);
256 static void dwarf2out_note_section_used (void);
257
258 /* Personality decl of current unit. Used only when assembler does not support
259 personality CFI. */
260 static GTY(()) rtx current_unit_personality;
261
262 /* Data and reference forms for relocatable data. */
263 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
264 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
265
266 #ifndef DEBUG_FRAME_SECTION
267 #define DEBUG_FRAME_SECTION ".debug_frame"
268 #endif
269
270 #ifndef FUNC_BEGIN_LABEL
271 #define FUNC_BEGIN_LABEL "LFB"
272 #endif
273
274 #ifndef FUNC_END_LABEL
275 #define FUNC_END_LABEL "LFE"
276 #endif
277
278 #ifndef PROLOGUE_END_LABEL
279 #define PROLOGUE_END_LABEL "LPE"
280 #endif
281
282 #ifndef EPILOGUE_BEGIN_LABEL
283 #define EPILOGUE_BEGIN_LABEL "LEB"
284 #endif
285
286 #ifndef FRAME_BEGIN_LABEL
287 #define FRAME_BEGIN_LABEL "Lframe"
288 #endif
289 #define CIE_AFTER_SIZE_LABEL "LSCIE"
290 #define CIE_END_LABEL "LECIE"
291 #define FDE_LABEL "LSFDE"
292 #define FDE_AFTER_SIZE_LABEL "LASFDE"
293 #define FDE_END_LABEL "LEFDE"
294 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
295 #define LINE_NUMBER_END_LABEL "LELT"
296 #define LN_PROLOG_AS_LABEL "LASLTP"
297 #define LN_PROLOG_END_LABEL "LELTP"
298 #define DIE_LABEL_PREFIX "DW"
299 \f
300 /* Match the base name of a file to the base name of a compilation unit. */
301
302 static int
303 matches_main_base (const char *path)
304 {
305 /* Cache the last query. */
306 static const char *last_path = NULL;
307 static int last_match = 0;
308 if (path != last_path)
309 {
310 const char *base;
311 int length = base_of_path (path, &base);
312 last_path = path;
313 last_match = (length == main_input_baselength
314 && memcmp (base, main_input_basename, length) == 0);
315 }
316 return last_match;
317 }
318
319 #ifdef DEBUG_DEBUG_STRUCT
320
321 static int
322 dump_struct_debug (tree type, enum debug_info_usage usage,
323 enum debug_struct_file criterion, int generic,
324 int matches, int result)
325 {
326 /* Find the type name. */
327 tree type_decl = TYPE_STUB_DECL (type);
328 tree t = type_decl;
329 const char *name = 0;
330 if (TREE_CODE (t) == TYPE_DECL)
331 t = DECL_NAME (t);
332 if (t)
333 name = IDENTIFIER_POINTER (t);
334
335 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
336 criterion,
337 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
338 matches ? "bas" : "hdr",
339 generic ? "gen" : "ord",
340 usage == DINFO_USAGE_DFN ? ";" :
341 usage == DINFO_USAGE_DIR_USE ? "." : "*",
342 result,
343 (void*) type_decl, name);
344 return result;
345 }
346 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
347 dump_struct_debug (type, usage, criterion, generic, matches, result)
348
349 #else
350
351 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
352 (result)
353
354 #endif
355
356 /* Get the number of HOST_WIDE_INTs needed to represent the precision
357 of the number. */
358
359 static unsigned int
360 get_full_len (const wide_int &op)
361 {
362 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
363 / HOST_BITS_PER_WIDE_INT);
364 }
365
366 static bool
367 should_emit_struct_debug (tree type, enum debug_info_usage usage)
368 {
369 enum debug_struct_file criterion;
370 tree type_decl;
371 bool generic = lang_hooks.types.generic_p (type);
372
373 if (generic)
374 criterion = debug_struct_generic[usage];
375 else
376 criterion = debug_struct_ordinary[usage];
377
378 if (criterion == DINFO_STRUCT_FILE_NONE)
379 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
380 if (criterion == DINFO_STRUCT_FILE_ANY)
381 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
382
383 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
384
385 if (type_decl != NULL)
386 {
387 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
388 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
389
390 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
391 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
392 }
393
394 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
395 }
396 \f
397 /* Return a pointer to a copy of the section string name S with all
398 attributes stripped off, and an asterisk prepended (for assemble_name). */
399
400 static inline char *
401 stripattributes (const char *s)
402 {
403 char *stripped = XNEWVEC (char, strlen (s) + 2);
404 char *p = stripped;
405
406 *p++ = '*';
407
408 while (*s && *s != ',')
409 *p++ = *s++;
410
411 *p = '\0';
412 return stripped;
413 }
414
415 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
416 switch to the data section instead, and write out a synthetic start label
417 for collect2 the first time around. */
418
419 static void
420 switch_to_eh_frame_section (bool back)
421 {
422 tree label;
423
424 #ifdef EH_FRAME_SECTION_NAME
425 if (eh_frame_section == 0)
426 {
427 int flags;
428
429 if (EH_TABLES_CAN_BE_READ_ONLY)
430 {
431 int fde_encoding;
432 int per_encoding;
433 int lsda_encoding;
434
435 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
436 /*global=*/0);
437 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
438 /*global=*/1);
439 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
440 /*global=*/0);
441 flags = ((! flag_pic
442 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
443 && (fde_encoding & 0x70) != DW_EH_PE_aligned
444 && (per_encoding & 0x70) != DW_EH_PE_absptr
445 && (per_encoding & 0x70) != DW_EH_PE_aligned
446 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
447 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
448 ? 0 : SECTION_WRITE);
449 }
450 else
451 flags = SECTION_WRITE;
452 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
453 }
454 #endif /* EH_FRAME_SECTION_NAME */
455
456 if (eh_frame_section)
457 switch_to_section (eh_frame_section);
458 else
459 {
460 /* We have no special eh_frame section. Put the information in
461 the data section and emit special labels to guide collect2. */
462 switch_to_section (data_section);
463
464 if (!back)
465 {
466 label = get_file_function_name ("F");
467 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
468 targetm.asm_out.globalize_label (asm_out_file,
469 IDENTIFIER_POINTER (label));
470 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
471 }
472 }
473 }
474
475 /* Switch [BACK] to the eh or debug frame table section, depending on
476 FOR_EH. */
477
478 static void
479 switch_to_frame_table_section (int for_eh, bool back)
480 {
481 if (for_eh)
482 switch_to_eh_frame_section (back);
483 else
484 {
485 if (!debug_frame_section)
486 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
487 SECTION_DEBUG, NULL);
488 switch_to_section (debug_frame_section);
489 }
490 }
491
492 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
493
494 enum dw_cfi_oprnd_type
495 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
496 {
497 switch (cfi)
498 {
499 case DW_CFA_nop:
500 case DW_CFA_GNU_window_save:
501 case DW_CFA_remember_state:
502 case DW_CFA_restore_state:
503 return dw_cfi_oprnd_unused;
504
505 case DW_CFA_set_loc:
506 case DW_CFA_advance_loc1:
507 case DW_CFA_advance_loc2:
508 case DW_CFA_advance_loc4:
509 case DW_CFA_MIPS_advance_loc8:
510 return dw_cfi_oprnd_addr;
511
512 case DW_CFA_offset:
513 case DW_CFA_offset_extended:
514 case DW_CFA_def_cfa:
515 case DW_CFA_offset_extended_sf:
516 case DW_CFA_def_cfa_sf:
517 case DW_CFA_restore:
518 case DW_CFA_restore_extended:
519 case DW_CFA_undefined:
520 case DW_CFA_same_value:
521 case DW_CFA_def_cfa_register:
522 case DW_CFA_register:
523 case DW_CFA_expression:
524 return dw_cfi_oprnd_reg_num;
525
526 case DW_CFA_def_cfa_offset:
527 case DW_CFA_GNU_args_size:
528 case DW_CFA_def_cfa_offset_sf:
529 return dw_cfi_oprnd_offset;
530
531 case DW_CFA_def_cfa_expression:
532 return dw_cfi_oprnd_loc;
533
534 default:
535 gcc_unreachable ();
536 }
537 }
538
539 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
540
541 enum dw_cfi_oprnd_type
542 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
543 {
544 switch (cfi)
545 {
546 case DW_CFA_def_cfa:
547 case DW_CFA_def_cfa_sf:
548 case DW_CFA_offset:
549 case DW_CFA_offset_extended_sf:
550 case DW_CFA_offset_extended:
551 return dw_cfi_oprnd_offset;
552
553 case DW_CFA_register:
554 return dw_cfi_oprnd_reg_num;
555
556 case DW_CFA_expression:
557 return dw_cfi_oprnd_loc;
558
559 default:
560 return dw_cfi_oprnd_unused;
561 }
562 }
563
564 /* Output one FDE. */
565
566 static void
567 output_fde (dw_fde_ref fde, bool for_eh, bool second,
568 char *section_start_label, int fde_encoding, char *augmentation,
569 bool any_lsda_needed, int lsda_encoding)
570 {
571 const char *begin, *end;
572 static unsigned int j;
573 char l1[20], l2[20];
574
575 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
576 /* empty */ 0);
577 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
578 for_eh + j);
579 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
580 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
581 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
582 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
583 " indicating 64-bit DWARF extension");
584 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
585 "FDE Length");
586 ASM_OUTPUT_LABEL (asm_out_file, l1);
587
588 if (for_eh)
589 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
590 else
591 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
592 debug_frame_section, "FDE CIE offset");
593
594 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
595 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
596
597 if (for_eh)
598 {
599 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
600 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
601 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
602 "FDE initial location");
603 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
604 end, begin, "FDE address range");
605 }
606 else
607 {
608 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
609 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
610 }
611
612 if (augmentation[0])
613 {
614 if (any_lsda_needed)
615 {
616 int size = size_of_encoded_value (lsda_encoding);
617
618 if (lsda_encoding == DW_EH_PE_aligned)
619 {
620 int offset = ( 4 /* Length */
621 + 4 /* CIE offset */
622 + 2 * size_of_encoded_value (fde_encoding)
623 + 1 /* Augmentation size */ );
624 int pad = -offset & (PTR_SIZE - 1);
625
626 size += pad;
627 gcc_assert (size_of_uleb128 (size) == 1);
628 }
629
630 dw2_asm_output_data_uleb128 (size, "Augmentation size");
631
632 if (fde->uses_eh_lsda)
633 {
634 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
635 fde->funcdef_number);
636 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
637 gen_rtx_SYMBOL_REF (Pmode, l1),
638 false,
639 "Language Specific Data Area");
640 }
641 else
642 {
643 if (lsda_encoding == DW_EH_PE_aligned)
644 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
645 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
646 "Language Specific Data Area (none)");
647 }
648 }
649 else
650 dw2_asm_output_data_uleb128 (0, "Augmentation size");
651 }
652
653 /* Loop through the Call Frame Instructions associated with this FDE. */
654 fde->dw_fde_current_label = begin;
655 {
656 size_t from, until, i;
657
658 from = 0;
659 until = vec_safe_length (fde->dw_fde_cfi);
660
661 if (fde->dw_fde_second_begin == NULL)
662 ;
663 else if (!second)
664 until = fde->dw_fde_switch_cfi_index;
665 else
666 from = fde->dw_fde_switch_cfi_index;
667
668 for (i = from; i < until; i++)
669 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
670 }
671
672 /* If we are to emit a ref/link from function bodies to their frame tables,
673 do it now. This is typically performed to make sure that tables
674 associated with functions are dragged with them and not discarded in
675 garbage collecting links. We need to do this on a per function basis to
676 cope with -ffunction-sections. */
677
678 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
679 /* Switch to the function section, emit the ref to the tables, and
680 switch *back* into the table section. */
681 switch_to_section (function_section (fde->decl));
682 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
683 switch_to_frame_table_section (for_eh, true);
684 #endif
685
686 /* Pad the FDE out to an address sized boundary. */
687 ASM_OUTPUT_ALIGN (asm_out_file,
688 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
689 ASM_OUTPUT_LABEL (asm_out_file, l2);
690
691 j += 2;
692 }
693
694 /* Return true if frame description entry FDE is needed for EH. */
695
696 static bool
697 fde_needed_for_eh_p (dw_fde_ref fde)
698 {
699 if (flag_asynchronous_unwind_tables)
700 return true;
701
702 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
703 return true;
704
705 if (fde->uses_eh_lsda)
706 return true;
707
708 /* If exceptions are enabled, we have collected nothrow info. */
709 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
710 return false;
711
712 return true;
713 }
714
715 /* Output the call frame information used to record information
716 that relates to calculating the frame pointer, and records the
717 location of saved registers. */
718
719 static void
720 output_call_frame_info (int for_eh)
721 {
722 unsigned int i;
723 dw_fde_ref fde;
724 dw_cfi_ref cfi;
725 char l1[20], l2[20], section_start_label[20];
726 bool any_lsda_needed = false;
727 char augmentation[6];
728 int augmentation_size;
729 int fde_encoding = DW_EH_PE_absptr;
730 int per_encoding = DW_EH_PE_absptr;
731 int lsda_encoding = DW_EH_PE_absptr;
732 int return_reg;
733 rtx personality = NULL;
734 int dw_cie_version;
735
736 /* Don't emit a CIE if there won't be any FDEs. */
737 if (!fde_vec)
738 return;
739
740 /* Nothing to do if the assembler's doing it all. */
741 if (dwarf2out_do_cfi_asm ())
742 return;
743
744 /* If we don't have any functions we'll want to unwind out of, don't emit
745 any EH unwind information. If we make FDEs linkonce, we may have to
746 emit an empty label for an FDE that wouldn't otherwise be emitted. We
747 want to avoid having an FDE kept around when the function it refers to
748 is discarded. Example where this matters: a primary function template
749 in C++ requires EH information, an explicit specialization doesn't. */
750 if (for_eh)
751 {
752 bool any_eh_needed = false;
753
754 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
755 {
756 if (fde->uses_eh_lsda)
757 any_eh_needed = any_lsda_needed = true;
758 else if (fde_needed_for_eh_p (fde))
759 any_eh_needed = true;
760 else if (TARGET_USES_WEAK_UNWIND_INFO)
761 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
762 }
763
764 if (!any_eh_needed)
765 return;
766 }
767
768 /* We're going to be generating comments, so turn on app. */
769 if (flag_debug_asm)
770 app_enable ();
771
772 /* Switch to the proper frame section, first time. */
773 switch_to_frame_table_section (for_eh, false);
774
775 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
776 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
777
778 /* Output the CIE. */
779 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
780 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
781 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
782 dw2_asm_output_data (4, 0xffffffff,
783 "Initial length escape value indicating 64-bit DWARF extension");
784 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
785 "Length of Common Information Entry");
786 ASM_OUTPUT_LABEL (asm_out_file, l1);
787
788 /* Now that the CIE pointer is PC-relative for EH,
789 use 0 to identify the CIE. */
790 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
791 (for_eh ? 0 : DWARF_CIE_ID),
792 "CIE Identifier Tag");
793
794 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
795 use CIE version 1, unless that would produce incorrect results
796 due to overflowing the return register column. */
797 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
798 dw_cie_version = 1;
799 if (return_reg >= 256 || dwarf_version > 2)
800 dw_cie_version = 3;
801 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
802
803 augmentation[0] = 0;
804 augmentation_size = 0;
805
806 personality = current_unit_personality;
807 if (for_eh)
808 {
809 char *p;
810
811 /* Augmentation:
812 z Indicates that a uleb128 is present to size the
813 augmentation section.
814 L Indicates the encoding (and thus presence) of
815 an LSDA pointer in the FDE augmentation.
816 R Indicates a non-default pointer encoding for
817 FDE code pointers.
818 P Indicates the presence of an encoding + language
819 personality routine in the CIE augmentation. */
820
821 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
822 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
823 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
824
825 p = augmentation + 1;
826 if (personality)
827 {
828 *p++ = 'P';
829 augmentation_size += 1 + size_of_encoded_value (per_encoding);
830 assemble_external_libcall (personality);
831 }
832 if (any_lsda_needed)
833 {
834 *p++ = 'L';
835 augmentation_size += 1;
836 }
837 if (fde_encoding != DW_EH_PE_absptr)
838 {
839 *p++ = 'R';
840 augmentation_size += 1;
841 }
842 if (p > augmentation + 1)
843 {
844 augmentation[0] = 'z';
845 *p = '\0';
846 }
847
848 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
849 if (personality && per_encoding == DW_EH_PE_aligned)
850 {
851 int offset = ( 4 /* Length */
852 + 4 /* CIE Id */
853 + 1 /* CIE version */
854 + strlen (augmentation) + 1 /* Augmentation */
855 + size_of_uleb128 (1) /* Code alignment */
856 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
857 + 1 /* RA column */
858 + 1 /* Augmentation size */
859 + 1 /* Personality encoding */ );
860 int pad = -offset & (PTR_SIZE - 1);
861
862 augmentation_size += pad;
863
864 /* Augmentations should be small, so there's scarce need to
865 iterate for a solution. Die if we exceed one uleb128 byte. */
866 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
867 }
868 }
869
870 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
871 if (dw_cie_version >= 4)
872 {
873 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
874 dw2_asm_output_data (1, 0, "CIE Segment Size");
875 }
876 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
877 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
878 "CIE Data Alignment Factor");
879
880 if (dw_cie_version == 1)
881 dw2_asm_output_data (1, return_reg, "CIE RA Column");
882 else
883 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
884
885 if (augmentation[0])
886 {
887 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
888 if (personality)
889 {
890 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
891 eh_data_format_name (per_encoding));
892 dw2_asm_output_encoded_addr_rtx (per_encoding,
893 personality,
894 true, NULL);
895 }
896
897 if (any_lsda_needed)
898 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
899 eh_data_format_name (lsda_encoding));
900
901 if (fde_encoding != DW_EH_PE_absptr)
902 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
903 eh_data_format_name (fde_encoding));
904 }
905
906 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
907 output_cfi (cfi, NULL, for_eh);
908
909 /* Pad the CIE out to an address sized boundary. */
910 ASM_OUTPUT_ALIGN (asm_out_file,
911 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
912 ASM_OUTPUT_LABEL (asm_out_file, l2);
913
914 /* Loop through all of the FDE's. */
915 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
916 {
917 unsigned int k;
918
919 /* Don't emit EH unwind info for leaf functions that don't need it. */
920 if (for_eh && !fde_needed_for_eh_p (fde))
921 continue;
922
923 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
924 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
925 augmentation, any_lsda_needed, lsda_encoding);
926 }
927
928 if (for_eh && targetm.terminate_dw2_eh_frame_info)
929 dw2_asm_output_data (4, 0, "End of Table");
930
931 /* Turn off app to make assembly quicker. */
932 if (flag_debug_asm)
933 app_disable ();
934 }
935
936 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
937
938 static void
939 dwarf2out_do_cfi_startproc (bool second)
940 {
941 int enc;
942 rtx ref;
943 rtx personality = get_personality_function (current_function_decl);
944
945 fprintf (asm_out_file, "\t.cfi_startproc\n");
946
947 if (personality)
948 {
949 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
950 ref = personality;
951
952 /* ??? The GAS support isn't entirely consistent. We have to
953 handle indirect support ourselves, but PC-relative is done
954 in the assembler. Further, the assembler can't handle any
955 of the weirder relocation types. */
956 if (enc & DW_EH_PE_indirect)
957 ref = dw2_force_const_mem (ref, true);
958
959 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
960 output_addr_const (asm_out_file, ref);
961 fputc ('\n', asm_out_file);
962 }
963
964 if (crtl->uses_eh_lsda)
965 {
966 char lab[20];
967
968 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
969 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
970 current_function_funcdef_no);
971 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
972 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
973
974 if (enc & DW_EH_PE_indirect)
975 ref = dw2_force_const_mem (ref, true);
976
977 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
978 output_addr_const (asm_out_file, ref);
979 fputc ('\n', asm_out_file);
980 }
981 }
982
983 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
984 this allocation may be done before pass_final. */
985
986 dw_fde_ref
987 dwarf2out_alloc_current_fde (void)
988 {
989 dw_fde_ref fde;
990
991 fde = ggc_alloc_cleared_dw_fde_node ();
992 fde->decl = current_function_decl;
993 fde->funcdef_number = current_function_funcdef_no;
994 fde->fde_index = vec_safe_length (fde_vec);
995 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
996 fde->uses_eh_lsda = crtl->uses_eh_lsda;
997 fde->nothrow = crtl->nothrow;
998 fde->drap_reg = INVALID_REGNUM;
999 fde->vdrap_reg = INVALID_REGNUM;
1000
1001 /* Record the FDE associated with this function. */
1002 cfun->fde = fde;
1003 vec_safe_push (fde_vec, fde);
1004
1005 return fde;
1006 }
1007
1008 /* Output a marker (i.e. a label) for the beginning of a function, before
1009 the prologue. */
1010
1011 void
1012 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1013 const char *file ATTRIBUTE_UNUSED)
1014 {
1015 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1016 char * dup_label;
1017 dw_fde_ref fde;
1018 section *fnsec;
1019 bool do_frame;
1020
1021 current_function_func_begin_label = NULL;
1022
1023 do_frame = dwarf2out_do_frame ();
1024
1025 /* ??? current_function_func_begin_label is also used by except.c for
1026 call-site information. We must emit this label if it might be used. */
1027 if (!do_frame
1028 && (!flag_exceptions
1029 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1030 return;
1031
1032 fnsec = function_section (current_function_decl);
1033 switch_to_section (fnsec);
1034 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1035 current_function_funcdef_no);
1036 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1037 current_function_funcdef_no);
1038 dup_label = xstrdup (label);
1039 current_function_func_begin_label = dup_label;
1040
1041 /* We can elide the fde allocation if we're not emitting debug info. */
1042 if (!do_frame)
1043 return;
1044
1045 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1046 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1047 would include pass_dwarf2_frame. If we've not created the FDE yet,
1048 do so now. */
1049 fde = cfun->fde;
1050 if (fde == NULL)
1051 fde = dwarf2out_alloc_current_fde ();
1052
1053 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1054 fde->dw_fde_begin = dup_label;
1055 fde->dw_fde_current_label = dup_label;
1056 fde->in_std_section = (fnsec == text_section
1057 || (cold_text_section && fnsec == cold_text_section));
1058
1059 /* We only want to output line number information for the genuine dwarf2
1060 prologue case, not the eh frame case. */
1061 #ifdef DWARF2_DEBUGGING_INFO
1062 if (file)
1063 dwarf2out_source_line (line, file, 0, true);
1064 #endif
1065
1066 if (dwarf2out_do_cfi_asm ())
1067 dwarf2out_do_cfi_startproc (false);
1068 else
1069 {
1070 rtx personality = get_personality_function (current_function_decl);
1071 if (!current_unit_personality)
1072 current_unit_personality = personality;
1073
1074 /* We cannot keep a current personality per function as without CFI
1075 asm, at the point where we emit the CFI data, there is no current
1076 function anymore. */
1077 if (personality && current_unit_personality != personality)
1078 sorry ("multiple EH personalities are supported only with assemblers "
1079 "supporting .cfi_personality directive");
1080 }
1081 }
1082
1083 /* Output a marker (i.e. a label) for the end of the generated code
1084 for a function prologue. This gets called *after* the prologue code has
1085 been generated. */
1086
1087 void
1088 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1089 const char *file ATTRIBUTE_UNUSED)
1090 {
1091 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1092
1093 /* Output a label to mark the endpoint of the code generated for this
1094 function. */
1095 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1096 current_function_funcdef_no);
1097 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1098 current_function_funcdef_no);
1099 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1100 }
1101
1102 /* Output a marker (i.e. a label) for the beginning of the generated code
1103 for a function epilogue. This gets called *before* the prologue code has
1104 been generated. */
1105
1106 void
1107 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1108 const char *file ATTRIBUTE_UNUSED)
1109 {
1110 dw_fde_ref fde = cfun->fde;
1111 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1112
1113 if (fde->dw_fde_vms_begin_epilogue)
1114 return;
1115
1116 /* Output a label to mark the endpoint of the code generated for this
1117 function. */
1118 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1119 current_function_funcdef_no);
1120 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1121 current_function_funcdef_no);
1122 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1123 }
1124
1125 /* Output a marker (i.e. a label) for the absolute end of the generated code
1126 for a function definition. This gets called *after* the epilogue code has
1127 been generated. */
1128
1129 void
1130 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1131 const char *file ATTRIBUTE_UNUSED)
1132 {
1133 dw_fde_ref fde;
1134 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1135
1136 last_var_location_insn = NULL_RTX;
1137 cached_next_real_insn = NULL_RTX;
1138
1139 if (dwarf2out_do_cfi_asm ())
1140 fprintf (asm_out_file, "\t.cfi_endproc\n");
1141
1142 /* Output a label to mark the endpoint of the code generated for this
1143 function. */
1144 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1145 current_function_funcdef_no);
1146 ASM_OUTPUT_LABEL (asm_out_file, label);
1147 fde = cfun->fde;
1148 gcc_assert (fde != NULL);
1149 if (fde->dw_fde_second_begin == NULL)
1150 fde->dw_fde_end = xstrdup (label);
1151 }
1152
1153 void
1154 dwarf2out_frame_finish (void)
1155 {
1156 /* Output call frame information. */
1157 if (targetm.debug_unwind_info () == UI_DWARF2)
1158 output_call_frame_info (0);
1159
1160 /* Output another copy for the unwinder. */
1161 if ((flag_unwind_tables || flag_exceptions)
1162 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1163 output_call_frame_info (1);
1164 }
1165
1166 /* Note that the current function section is being used for code. */
1167
1168 static void
1169 dwarf2out_note_section_used (void)
1170 {
1171 section *sec = current_function_section ();
1172 if (sec == text_section)
1173 text_section_used = true;
1174 else if (sec == cold_text_section)
1175 cold_text_section_used = true;
1176 }
1177
1178 static void var_location_switch_text_section (void);
1179 static void set_cur_line_info_table (section *);
1180
1181 void
1182 dwarf2out_switch_text_section (void)
1183 {
1184 section *sect;
1185 dw_fde_ref fde = cfun->fde;
1186
1187 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1188
1189 if (!in_cold_section_p)
1190 {
1191 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1192 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1193 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1194 }
1195 else
1196 {
1197 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1198 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1199 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1200 }
1201 have_multiple_function_sections = true;
1202
1203 /* There is no need to mark used sections when not debugging. */
1204 if (cold_text_section != NULL)
1205 dwarf2out_note_section_used ();
1206
1207 if (dwarf2out_do_cfi_asm ())
1208 fprintf (asm_out_file, "\t.cfi_endproc\n");
1209
1210 /* Now do the real section switch. */
1211 sect = current_function_section ();
1212 switch_to_section (sect);
1213
1214 fde->second_in_std_section
1215 = (sect == text_section
1216 || (cold_text_section && sect == cold_text_section));
1217
1218 if (dwarf2out_do_cfi_asm ())
1219 dwarf2out_do_cfi_startproc (true);
1220
1221 var_location_switch_text_section ();
1222
1223 if (cold_text_section != NULL)
1224 set_cur_line_info_table (sect);
1225 }
1226 \f
1227 /* And now, the subset of the debugging information support code necessary
1228 for emitting location expressions. */
1229
1230 /* Data about a single source file. */
1231 struct GTY(()) dwarf_file_data {
1232 const char * filename;
1233 int emitted_number;
1234 };
1235
1236 typedef struct GTY(()) deferred_locations_struct
1237 {
1238 tree variable;
1239 dw_die_ref die;
1240 } deferred_locations;
1241
1242
1243 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1244
1245
1246 /* Describe an entry into the .debug_addr section. */
1247
1248 enum ate_kind {
1249 ate_kind_rtx,
1250 ate_kind_rtx_dtprel,
1251 ate_kind_label
1252 };
1253
1254 typedef struct GTY(()) addr_table_entry_struct {
1255 enum ate_kind kind;
1256 unsigned int refcount;
1257 unsigned int index;
1258 union addr_table_entry_struct_union
1259 {
1260 rtx GTY ((tag ("0"))) rtl;
1261 char * GTY ((tag ("1"))) label;
1262 }
1263 GTY ((desc ("%1.kind"))) addr;
1264 }
1265 addr_table_entry;
1266
1267 /* Location lists are ranges + location descriptions for that range,
1268 so you can track variables that are in different places over
1269 their entire life. */
1270 typedef struct GTY(()) dw_loc_list_struct {
1271 dw_loc_list_ref dw_loc_next;
1272 const char *begin; /* Label and addr_entry for start of range */
1273 addr_table_entry *begin_entry;
1274 const char *end; /* Label for end of range */
1275 char *ll_symbol; /* Label for beginning of location list.
1276 Only on head of list */
1277 const char *section; /* Section this loclist is relative to */
1278 dw_loc_descr_ref expr;
1279 hashval_t hash;
1280 /* True if all addresses in this and subsequent lists are known to be
1281 resolved. */
1282 bool resolved_addr;
1283 /* True if this list has been replaced by dw_loc_next. */
1284 bool replaced;
1285 bool emitted;
1286 /* True if the range should be emitted even if begin and end
1287 are the same. */
1288 bool force;
1289 } dw_loc_list_node;
1290
1291 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1292
1293 /* Convert a DWARF stack opcode into its string name. */
1294
1295 static const char *
1296 dwarf_stack_op_name (unsigned int op)
1297 {
1298 const char *name = get_DW_OP_name (op);
1299
1300 if (name != NULL)
1301 return name;
1302
1303 return "OP_<unknown>";
1304 }
1305
1306 /* Return a pointer to a newly allocated location description. Location
1307 descriptions are simple expression terms that can be strung
1308 together to form more complicated location (address) descriptions. */
1309
1310 static inline dw_loc_descr_ref
1311 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1312 unsigned HOST_WIDE_INT oprnd2)
1313 {
1314 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1315
1316 descr->dw_loc_opc = op;
1317 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1318 descr->dw_loc_oprnd1.val_entry = NULL;
1319 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1320 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1321 descr->dw_loc_oprnd2.val_entry = NULL;
1322 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1323
1324 return descr;
1325 }
1326
1327 /* Return a pointer to a newly allocated location description for
1328 REG and OFFSET. */
1329
1330 static inline dw_loc_descr_ref
1331 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1332 {
1333 if (reg <= 31)
1334 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1335 offset, 0);
1336 else
1337 return new_loc_descr (DW_OP_bregx, reg, offset);
1338 }
1339
1340 /* Add a location description term to a location description expression. */
1341
1342 static inline void
1343 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1344 {
1345 dw_loc_descr_ref *d;
1346
1347 /* Find the end of the chain. */
1348 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1349 ;
1350
1351 *d = descr;
1352 }
1353
1354 /* Compare two location operands for exact equality. */
1355
1356 static bool
1357 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1358 {
1359 if (a->val_class != b->val_class)
1360 return false;
1361 switch (a->val_class)
1362 {
1363 case dw_val_class_none:
1364 return true;
1365 case dw_val_class_addr:
1366 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1367
1368 case dw_val_class_offset:
1369 case dw_val_class_unsigned_const:
1370 case dw_val_class_const:
1371 case dw_val_class_range_list:
1372 case dw_val_class_lineptr:
1373 case dw_val_class_macptr:
1374 /* These are all HOST_WIDE_INT, signed or unsigned. */
1375 return a->v.val_unsigned == b->v.val_unsigned;
1376
1377 case dw_val_class_loc:
1378 return a->v.val_loc == b->v.val_loc;
1379 case dw_val_class_loc_list:
1380 return a->v.val_loc_list == b->v.val_loc_list;
1381 case dw_val_class_die_ref:
1382 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1383 case dw_val_class_fde_ref:
1384 return a->v.val_fde_index == b->v.val_fde_index;
1385 case dw_val_class_lbl_id:
1386 case dw_val_class_high_pc:
1387 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1388 case dw_val_class_str:
1389 return a->v.val_str == b->v.val_str;
1390 case dw_val_class_flag:
1391 return a->v.val_flag == b->v.val_flag;
1392 case dw_val_class_file:
1393 return a->v.val_file == b->v.val_file;
1394 case dw_val_class_decl_ref:
1395 return a->v.val_decl_ref == b->v.val_decl_ref;
1396
1397 case dw_val_class_const_double:
1398 return (a->v.val_double.high == b->v.val_double.high
1399 && a->v.val_double.low == b->v.val_double.low);
1400
1401 case dw_val_class_wide_int:
1402 return *a->v.val_wide == *b->v.val_wide;
1403
1404 case dw_val_class_vec:
1405 {
1406 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1407 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1408
1409 return (a_len == b_len
1410 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1411 }
1412
1413 case dw_val_class_data8:
1414 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1415
1416 case dw_val_class_vms_delta:
1417 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1418 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1419 }
1420 gcc_unreachable ();
1421 }
1422
1423 /* Compare two location atoms for exact equality. */
1424
1425 static bool
1426 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1427 {
1428 if (a->dw_loc_opc != b->dw_loc_opc)
1429 return false;
1430
1431 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1432 address size, but since we always allocate cleared storage it
1433 should be zero for other types of locations. */
1434 if (a->dtprel != b->dtprel)
1435 return false;
1436
1437 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1438 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1439 }
1440
1441 /* Compare two complete location expressions for exact equality. */
1442
1443 bool
1444 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1445 {
1446 while (1)
1447 {
1448 if (a == b)
1449 return true;
1450 if (a == NULL || b == NULL)
1451 return false;
1452 if (!loc_descr_equal_p_1 (a, b))
1453 return false;
1454
1455 a = a->dw_loc_next;
1456 b = b->dw_loc_next;
1457 }
1458 }
1459
1460
1461 /* Add a constant OFFSET to a location expression. */
1462
1463 static void
1464 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1465 {
1466 dw_loc_descr_ref loc;
1467 HOST_WIDE_INT *p;
1468
1469 gcc_assert (*list_head != NULL);
1470
1471 if (!offset)
1472 return;
1473
1474 /* Find the end of the chain. */
1475 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1476 ;
1477
1478 p = NULL;
1479 if (loc->dw_loc_opc == DW_OP_fbreg
1480 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1481 p = &loc->dw_loc_oprnd1.v.val_int;
1482 else if (loc->dw_loc_opc == DW_OP_bregx)
1483 p = &loc->dw_loc_oprnd2.v.val_int;
1484
1485 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1486 offset. Don't optimize if an signed integer overflow would happen. */
1487 if (p != NULL
1488 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1489 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1490 *p += offset;
1491
1492 else if (offset > 0)
1493 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1494
1495 else
1496 {
1497 loc->dw_loc_next = int_loc_descriptor (-offset);
1498 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1499 }
1500 }
1501
1502 /* Add a constant OFFSET to a location list. */
1503
1504 static void
1505 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1506 {
1507 dw_loc_list_ref d;
1508 for (d = list_head; d != NULL; d = d->dw_loc_next)
1509 loc_descr_plus_const (&d->expr, offset);
1510 }
1511
1512 #define DWARF_REF_SIZE \
1513 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1514
1515 static unsigned long int get_base_type_offset (dw_die_ref);
1516
1517 /* Return the size of a location descriptor. */
1518
1519 static unsigned long
1520 size_of_loc_descr (dw_loc_descr_ref loc)
1521 {
1522 unsigned long size = 1;
1523
1524 switch (loc->dw_loc_opc)
1525 {
1526 case DW_OP_addr:
1527 size += DWARF2_ADDR_SIZE;
1528 break;
1529 case DW_OP_GNU_addr_index:
1530 case DW_OP_GNU_const_index:
1531 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1532 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1533 break;
1534 case DW_OP_const1u:
1535 case DW_OP_const1s:
1536 size += 1;
1537 break;
1538 case DW_OP_const2u:
1539 case DW_OP_const2s:
1540 size += 2;
1541 break;
1542 case DW_OP_const4u:
1543 case DW_OP_const4s:
1544 size += 4;
1545 break;
1546 case DW_OP_const8u:
1547 case DW_OP_const8s:
1548 size += 8;
1549 break;
1550 case DW_OP_constu:
1551 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1552 break;
1553 case DW_OP_consts:
1554 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1555 break;
1556 case DW_OP_pick:
1557 size += 1;
1558 break;
1559 case DW_OP_plus_uconst:
1560 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1561 break;
1562 case DW_OP_skip:
1563 case DW_OP_bra:
1564 size += 2;
1565 break;
1566 case DW_OP_breg0:
1567 case DW_OP_breg1:
1568 case DW_OP_breg2:
1569 case DW_OP_breg3:
1570 case DW_OP_breg4:
1571 case DW_OP_breg5:
1572 case DW_OP_breg6:
1573 case DW_OP_breg7:
1574 case DW_OP_breg8:
1575 case DW_OP_breg9:
1576 case DW_OP_breg10:
1577 case DW_OP_breg11:
1578 case DW_OP_breg12:
1579 case DW_OP_breg13:
1580 case DW_OP_breg14:
1581 case DW_OP_breg15:
1582 case DW_OP_breg16:
1583 case DW_OP_breg17:
1584 case DW_OP_breg18:
1585 case DW_OP_breg19:
1586 case DW_OP_breg20:
1587 case DW_OP_breg21:
1588 case DW_OP_breg22:
1589 case DW_OP_breg23:
1590 case DW_OP_breg24:
1591 case DW_OP_breg25:
1592 case DW_OP_breg26:
1593 case DW_OP_breg27:
1594 case DW_OP_breg28:
1595 case DW_OP_breg29:
1596 case DW_OP_breg30:
1597 case DW_OP_breg31:
1598 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1599 break;
1600 case DW_OP_regx:
1601 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1602 break;
1603 case DW_OP_fbreg:
1604 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1605 break;
1606 case DW_OP_bregx:
1607 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1608 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1609 break;
1610 case DW_OP_piece:
1611 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1612 break;
1613 case DW_OP_bit_piece:
1614 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1615 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1616 break;
1617 case DW_OP_deref_size:
1618 case DW_OP_xderef_size:
1619 size += 1;
1620 break;
1621 case DW_OP_call2:
1622 size += 2;
1623 break;
1624 case DW_OP_call4:
1625 size += 4;
1626 break;
1627 case DW_OP_call_ref:
1628 size += DWARF_REF_SIZE;
1629 break;
1630 case DW_OP_implicit_value:
1631 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1632 + loc->dw_loc_oprnd1.v.val_unsigned;
1633 break;
1634 case DW_OP_GNU_implicit_pointer:
1635 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1636 break;
1637 case DW_OP_GNU_entry_value:
1638 {
1639 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1640 size += size_of_uleb128 (op_size) + op_size;
1641 break;
1642 }
1643 case DW_OP_GNU_const_type:
1644 {
1645 unsigned long o
1646 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1647 size += size_of_uleb128 (o) + 1;
1648 switch (loc->dw_loc_oprnd2.val_class)
1649 {
1650 case dw_val_class_vec:
1651 size += loc->dw_loc_oprnd2.v.val_vec.length
1652 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1653 break;
1654 case dw_val_class_const:
1655 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1656 break;
1657 case dw_val_class_const_double:
1658 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1659 break;
1660 case dw_val_class_wide_int:
1661 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1662 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1663 break;
1664 default:
1665 gcc_unreachable ();
1666 }
1667 break;
1668 }
1669 case DW_OP_GNU_regval_type:
1670 {
1671 unsigned long o
1672 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1673 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1674 + size_of_uleb128 (o);
1675 }
1676 break;
1677 case DW_OP_GNU_deref_type:
1678 {
1679 unsigned long o
1680 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1681 size += 1 + size_of_uleb128 (o);
1682 }
1683 break;
1684 case DW_OP_GNU_convert:
1685 case DW_OP_GNU_reinterpret:
1686 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1687 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1688 else
1689 {
1690 unsigned long o
1691 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1692 size += size_of_uleb128 (o);
1693 }
1694 break;
1695 case DW_OP_GNU_parameter_ref:
1696 size += 4;
1697 break;
1698 default:
1699 break;
1700 }
1701
1702 return size;
1703 }
1704
1705 /* Return the size of a series of location descriptors. */
1706
1707 unsigned long
1708 size_of_locs (dw_loc_descr_ref loc)
1709 {
1710 dw_loc_descr_ref l;
1711 unsigned long size;
1712
1713 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1714 field, to avoid writing to a PCH file. */
1715 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1716 {
1717 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1718 break;
1719 size += size_of_loc_descr (l);
1720 }
1721 if (! l)
1722 return size;
1723
1724 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1725 {
1726 l->dw_loc_addr = size;
1727 size += size_of_loc_descr (l);
1728 }
1729
1730 return size;
1731 }
1732
1733 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1734 static void get_ref_die_offset_label (char *, dw_die_ref);
1735 static unsigned long int get_ref_die_offset (dw_die_ref);
1736
1737 /* Output location description stack opcode's operands (if any).
1738 The for_eh_or_skip parameter controls whether register numbers are
1739 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1740 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1741 info). This should be suppressed for the cases that have not been converted
1742 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1743
1744 static void
1745 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1746 {
1747 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1748 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1749
1750 switch (loc->dw_loc_opc)
1751 {
1752 #ifdef DWARF2_DEBUGGING_INFO
1753 case DW_OP_const2u:
1754 case DW_OP_const2s:
1755 dw2_asm_output_data (2, val1->v.val_int, NULL);
1756 break;
1757 case DW_OP_const4u:
1758 if (loc->dtprel)
1759 {
1760 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1761 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1762 val1->v.val_addr);
1763 fputc ('\n', asm_out_file);
1764 break;
1765 }
1766 /* FALLTHRU */
1767 case DW_OP_const4s:
1768 dw2_asm_output_data (4, val1->v.val_int, NULL);
1769 break;
1770 case DW_OP_const8u:
1771 if (loc->dtprel)
1772 {
1773 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1774 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1775 val1->v.val_addr);
1776 fputc ('\n', asm_out_file);
1777 break;
1778 }
1779 /* FALLTHRU */
1780 case DW_OP_const8s:
1781 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1782 dw2_asm_output_data (8, val1->v.val_int, NULL);
1783 break;
1784 case DW_OP_skip:
1785 case DW_OP_bra:
1786 {
1787 int offset;
1788
1789 gcc_assert (val1->val_class == dw_val_class_loc);
1790 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1791
1792 dw2_asm_output_data (2, offset, NULL);
1793 }
1794 break;
1795 case DW_OP_implicit_value:
1796 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1797 switch (val2->val_class)
1798 {
1799 case dw_val_class_const:
1800 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1801 break;
1802 case dw_val_class_vec:
1803 {
1804 unsigned int elt_size = val2->v.val_vec.elt_size;
1805 unsigned int len = val2->v.val_vec.length;
1806 unsigned int i;
1807 unsigned char *p;
1808
1809 if (elt_size > sizeof (HOST_WIDE_INT))
1810 {
1811 elt_size /= 2;
1812 len *= 2;
1813 }
1814 for (i = 0, p = val2->v.val_vec.array;
1815 i < len;
1816 i++, p += elt_size)
1817 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1818 "fp or vector constant word %u", i);
1819 }
1820 break;
1821 case dw_val_class_const_double:
1822 {
1823 unsigned HOST_WIDE_INT first, second;
1824
1825 if (WORDS_BIG_ENDIAN)
1826 {
1827 first = val2->v.val_double.high;
1828 second = val2->v.val_double.low;
1829 }
1830 else
1831 {
1832 first = val2->v.val_double.low;
1833 second = val2->v.val_double.high;
1834 }
1835 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1836 first, NULL);
1837 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1838 second, NULL);
1839 }
1840 break;
1841 case dw_val_class_wide_int:
1842 {
1843 int i;
1844 int len = get_full_len (*val2->v.val_wide);
1845 if (WORDS_BIG_ENDIAN)
1846 for (i = len; i >= 0; --i)
1847 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1848 val2->v.val_wide->elt (i), NULL);
1849 else
1850 for (i = 0; i < len; ++i)
1851 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1852 val2->v.val_wide->elt (i), NULL);
1853 }
1854 break;
1855 case dw_val_class_addr:
1856 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1857 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1858 break;
1859 default:
1860 gcc_unreachable ();
1861 }
1862 break;
1863 #else
1864 case DW_OP_const2u:
1865 case DW_OP_const2s:
1866 case DW_OP_const4u:
1867 case DW_OP_const4s:
1868 case DW_OP_const8u:
1869 case DW_OP_const8s:
1870 case DW_OP_skip:
1871 case DW_OP_bra:
1872 case DW_OP_implicit_value:
1873 /* We currently don't make any attempt to make sure these are
1874 aligned properly like we do for the main unwind info, so
1875 don't support emitting things larger than a byte if we're
1876 only doing unwinding. */
1877 gcc_unreachable ();
1878 #endif
1879 case DW_OP_const1u:
1880 case DW_OP_const1s:
1881 dw2_asm_output_data (1, val1->v.val_int, NULL);
1882 break;
1883 case DW_OP_constu:
1884 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1885 break;
1886 case DW_OP_consts:
1887 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1888 break;
1889 case DW_OP_pick:
1890 dw2_asm_output_data (1, val1->v.val_int, NULL);
1891 break;
1892 case DW_OP_plus_uconst:
1893 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1894 break;
1895 case DW_OP_breg0:
1896 case DW_OP_breg1:
1897 case DW_OP_breg2:
1898 case DW_OP_breg3:
1899 case DW_OP_breg4:
1900 case DW_OP_breg5:
1901 case DW_OP_breg6:
1902 case DW_OP_breg7:
1903 case DW_OP_breg8:
1904 case DW_OP_breg9:
1905 case DW_OP_breg10:
1906 case DW_OP_breg11:
1907 case DW_OP_breg12:
1908 case DW_OP_breg13:
1909 case DW_OP_breg14:
1910 case DW_OP_breg15:
1911 case DW_OP_breg16:
1912 case DW_OP_breg17:
1913 case DW_OP_breg18:
1914 case DW_OP_breg19:
1915 case DW_OP_breg20:
1916 case DW_OP_breg21:
1917 case DW_OP_breg22:
1918 case DW_OP_breg23:
1919 case DW_OP_breg24:
1920 case DW_OP_breg25:
1921 case DW_OP_breg26:
1922 case DW_OP_breg27:
1923 case DW_OP_breg28:
1924 case DW_OP_breg29:
1925 case DW_OP_breg30:
1926 case DW_OP_breg31:
1927 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1928 break;
1929 case DW_OP_regx:
1930 {
1931 unsigned r = val1->v.val_unsigned;
1932 if (for_eh_or_skip >= 0)
1933 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1934 gcc_assert (size_of_uleb128 (r)
1935 == size_of_uleb128 (val1->v.val_unsigned));
1936 dw2_asm_output_data_uleb128 (r, NULL);
1937 }
1938 break;
1939 case DW_OP_fbreg:
1940 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1941 break;
1942 case DW_OP_bregx:
1943 {
1944 unsigned r = val1->v.val_unsigned;
1945 if (for_eh_or_skip >= 0)
1946 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1947 gcc_assert (size_of_uleb128 (r)
1948 == size_of_uleb128 (val1->v.val_unsigned));
1949 dw2_asm_output_data_uleb128 (r, NULL);
1950 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1951 }
1952 break;
1953 case DW_OP_piece:
1954 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1955 break;
1956 case DW_OP_bit_piece:
1957 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1958 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1959 break;
1960 case DW_OP_deref_size:
1961 case DW_OP_xderef_size:
1962 dw2_asm_output_data (1, val1->v.val_int, NULL);
1963 break;
1964
1965 case DW_OP_addr:
1966 if (loc->dtprel)
1967 {
1968 if (targetm.asm_out.output_dwarf_dtprel)
1969 {
1970 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1971 DWARF2_ADDR_SIZE,
1972 val1->v.val_addr);
1973 fputc ('\n', asm_out_file);
1974 }
1975 else
1976 gcc_unreachable ();
1977 }
1978 else
1979 {
1980 #ifdef DWARF2_DEBUGGING_INFO
1981 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1982 #else
1983 gcc_unreachable ();
1984 #endif
1985 }
1986 break;
1987
1988 case DW_OP_GNU_addr_index:
1989 case DW_OP_GNU_const_index:
1990 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1991 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1992 "(index into .debug_addr)");
1993 break;
1994
1995 case DW_OP_GNU_implicit_pointer:
1996 {
1997 char label[MAX_ARTIFICIAL_LABEL_BYTES
1998 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1999 gcc_assert (val1->val_class == dw_val_class_die_ref);
2000 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2001 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2002 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2003 }
2004 break;
2005
2006 case DW_OP_GNU_entry_value:
2007 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2008 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2009 break;
2010
2011 case DW_OP_GNU_const_type:
2012 {
2013 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2014 gcc_assert (o);
2015 dw2_asm_output_data_uleb128 (o, NULL);
2016 switch (val2->val_class)
2017 {
2018 case dw_val_class_const:
2019 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2020 dw2_asm_output_data (1, l, NULL);
2021 dw2_asm_output_data (l, val2->v.val_int, NULL);
2022 break;
2023 case dw_val_class_vec:
2024 {
2025 unsigned int elt_size = val2->v.val_vec.elt_size;
2026 unsigned int len = val2->v.val_vec.length;
2027 unsigned int i;
2028 unsigned char *p;
2029
2030 l = len * elt_size;
2031 dw2_asm_output_data (1, l, NULL);
2032 if (elt_size > sizeof (HOST_WIDE_INT))
2033 {
2034 elt_size /= 2;
2035 len *= 2;
2036 }
2037 for (i = 0, p = val2->v.val_vec.array;
2038 i < len;
2039 i++, p += elt_size)
2040 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2041 "fp or vector constant word %u", i);
2042 }
2043 break;
2044 case dw_val_class_const_double:
2045 {
2046 unsigned HOST_WIDE_INT first, second;
2047 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2048
2049 dw2_asm_output_data (1, 2 * l, NULL);
2050 if (WORDS_BIG_ENDIAN)
2051 {
2052 first = val2->v.val_double.high;
2053 second = val2->v.val_double.low;
2054 }
2055 else
2056 {
2057 first = val2->v.val_double.low;
2058 second = val2->v.val_double.high;
2059 }
2060 dw2_asm_output_data (l, first, NULL);
2061 dw2_asm_output_data (l, second, NULL);
2062 }
2063 break;
2064 case dw_val_class_wide_int:
2065 {
2066 int i;
2067 int len = get_full_len (*val2->v.val_wide);
2068 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2069
2070 dw2_asm_output_data (1, len * l, NULL);
2071 if (WORDS_BIG_ENDIAN)
2072 for (i = len; i >= 0; --i)
2073 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2074 else
2075 for (i = 0; i < len; ++i)
2076 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2077 }
2078 break;
2079 default:
2080 gcc_unreachable ();
2081 }
2082 }
2083 break;
2084 case DW_OP_GNU_regval_type:
2085 {
2086 unsigned r = val1->v.val_unsigned;
2087 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2088 gcc_assert (o);
2089 if (for_eh_or_skip >= 0)
2090 {
2091 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2092 gcc_assert (size_of_uleb128 (r)
2093 == size_of_uleb128 (val1->v.val_unsigned));
2094 }
2095 dw2_asm_output_data_uleb128 (r, NULL);
2096 dw2_asm_output_data_uleb128 (o, NULL);
2097 }
2098 break;
2099 case DW_OP_GNU_deref_type:
2100 {
2101 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2102 gcc_assert (o);
2103 dw2_asm_output_data (1, val1->v.val_int, NULL);
2104 dw2_asm_output_data_uleb128 (o, NULL);
2105 }
2106 break;
2107 case DW_OP_GNU_convert:
2108 case DW_OP_GNU_reinterpret:
2109 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2110 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2111 else
2112 {
2113 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2114 gcc_assert (o);
2115 dw2_asm_output_data_uleb128 (o, NULL);
2116 }
2117 break;
2118
2119 case DW_OP_GNU_parameter_ref:
2120 {
2121 unsigned long o;
2122 gcc_assert (val1->val_class == dw_val_class_die_ref);
2123 o = get_ref_die_offset (val1->v.val_die_ref.die);
2124 dw2_asm_output_data (4, o, NULL);
2125 }
2126 break;
2127
2128 default:
2129 /* Other codes have no operands. */
2130 break;
2131 }
2132 }
2133
2134 /* Output a sequence of location operations.
2135 The for_eh_or_skip parameter controls whether register numbers are
2136 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2137 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2138 info). This should be suppressed for the cases that have not been converted
2139 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2140
2141 void
2142 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2143 {
2144 for (; loc != NULL; loc = loc->dw_loc_next)
2145 {
2146 enum dwarf_location_atom opc = loc->dw_loc_opc;
2147 /* Output the opcode. */
2148 if (for_eh_or_skip >= 0
2149 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2150 {
2151 unsigned r = (opc - DW_OP_breg0);
2152 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2153 gcc_assert (r <= 31);
2154 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2155 }
2156 else if (for_eh_or_skip >= 0
2157 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2158 {
2159 unsigned r = (opc - DW_OP_reg0);
2160 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2161 gcc_assert (r <= 31);
2162 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2163 }
2164
2165 dw2_asm_output_data (1, opc,
2166 "%s", dwarf_stack_op_name (opc));
2167
2168 /* Output the operand(s) (if any). */
2169 output_loc_operands (loc, for_eh_or_skip);
2170 }
2171 }
2172
2173 /* Output location description stack opcode's operands (if any).
2174 The output is single bytes on a line, suitable for .cfi_escape. */
2175
2176 static void
2177 output_loc_operands_raw (dw_loc_descr_ref loc)
2178 {
2179 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2180 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2181
2182 switch (loc->dw_loc_opc)
2183 {
2184 case DW_OP_addr:
2185 case DW_OP_GNU_addr_index:
2186 case DW_OP_GNU_const_index:
2187 case DW_OP_implicit_value:
2188 /* We cannot output addresses in .cfi_escape, only bytes. */
2189 gcc_unreachable ();
2190
2191 case DW_OP_const1u:
2192 case DW_OP_const1s:
2193 case DW_OP_pick:
2194 case DW_OP_deref_size:
2195 case DW_OP_xderef_size:
2196 fputc (',', asm_out_file);
2197 dw2_asm_output_data_raw (1, val1->v.val_int);
2198 break;
2199
2200 case DW_OP_const2u:
2201 case DW_OP_const2s:
2202 fputc (',', asm_out_file);
2203 dw2_asm_output_data_raw (2, val1->v.val_int);
2204 break;
2205
2206 case DW_OP_const4u:
2207 case DW_OP_const4s:
2208 fputc (',', asm_out_file);
2209 dw2_asm_output_data_raw (4, val1->v.val_int);
2210 break;
2211
2212 case DW_OP_const8u:
2213 case DW_OP_const8s:
2214 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2215 fputc (',', asm_out_file);
2216 dw2_asm_output_data_raw (8, val1->v.val_int);
2217 break;
2218
2219 case DW_OP_skip:
2220 case DW_OP_bra:
2221 {
2222 int offset;
2223
2224 gcc_assert (val1->val_class == dw_val_class_loc);
2225 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2226
2227 fputc (',', asm_out_file);
2228 dw2_asm_output_data_raw (2, offset);
2229 }
2230 break;
2231
2232 case DW_OP_regx:
2233 {
2234 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2235 gcc_assert (size_of_uleb128 (r)
2236 == size_of_uleb128 (val1->v.val_unsigned));
2237 fputc (',', asm_out_file);
2238 dw2_asm_output_data_uleb128_raw (r);
2239 }
2240 break;
2241
2242 case DW_OP_constu:
2243 case DW_OP_plus_uconst:
2244 case DW_OP_piece:
2245 fputc (',', asm_out_file);
2246 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2247 break;
2248
2249 case DW_OP_bit_piece:
2250 fputc (',', asm_out_file);
2251 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2252 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2253 break;
2254
2255 case DW_OP_consts:
2256 case DW_OP_breg0:
2257 case DW_OP_breg1:
2258 case DW_OP_breg2:
2259 case DW_OP_breg3:
2260 case DW_OP_breg4:
2261 case DW_OP_breg5:
2262 case DW_OP_breg6:
2263 case DW_OP_breg7:
2264 case DW_OP_breg8:
2265 case DW_OP_breg9:
2266 case DW_OP_breg10:
2267 case DW_OP_breg11:
2268 case DW_OP_breg12:
2269 case DW_OP_breg13:
2270 case DW_OP_breg14:
2271 case DW_OP_breg15:
2272 case DW_OP_breg16:
2273 case DW_OP_breg17:
2274 case DW_OP_breg18:
2275 case DW_OP_breg19:
2276 case DW_OP_breg20:
2277 case DW_OP_breg21:
2278 case DW_OP_breg22:
2279 case DW_OP_breg23:
2280 case DW_OP_breg24:
2281 case DW_OP_breg25:
2282 case DW_OP_breg26:
2283 case DW_OP_breg27:
2284 case DW_OP_breg28:
2285 case DW_OP_breg29:
2286 case DW_OP_breg30:
2287 case DW_OP_breg31:
2288 case DW_OP_fbreg:
2289 fputc (',', asm_out_file);
2290 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2291 break;
2292
2293 case DW_OP_bregx:
2294 {
2295 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2296 gcc_assert (size_of_uleb128 (r)
2297 == size_of_uleb128 (val1->v.val_unsigned));
2298 fputc (',', asm_out_file);
2299 dw2_asm_output_data_uleb128_raw (r);
2300 fputc (',', asm_out_file);
2301 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2302 }
2303 break;
2304
2305 case DW_OP_GNU_implicit_pointer:
2306 case DW_OP_GNU_entry_value:
2307 case DW_OP_GNU_const_type:
2308 case DW_OP_GNU_regval_type:
2309 case DW_OP_GNU_deref_type:
2310 case DW_OP_GNU_convert:
2311 case DW_OP_GNU_reinterpret:
2312 case DW_OP_GNU_parameter_ref:
2313 gcc_unreachable ();
2314 break;
2315
2316 default:
2317 /* Other codes have no operands. */
2318 break;
2319 }
2320 }
2321
2322 void
2323 output_loc_sequence_raw (dw_loc_descr_ref loc)
2324 {
2325 while (1)
2326 {
2327 enum dwarf_location_atom opc = loc->dw_loc_opc;
2328 /* Output the opcode. */
2329 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2330 {
2331 unsigned r = (opc - DW_OP_breg0);
2332 r = DWARF2_FRAME_REG_OUT (r, 1);
2333 gcc_assert (r <= 31);
2334 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2335 }
2336 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2337 {
2338 unsigned r = (opc - DW_OP_reg0);
2339 r = DWARF2_FRAME_REG_OUT (r, 1);
2340 gcc_assert (r <= 31);
2341 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2342 }
2343 /* Output the opcode. */
2344 fprintf (asm_out_file, "%#x", opc);
2345 output_loc_operands_raw (loc);
2346
2347 if (!loc->dw_loc_next)
2348 break;
2349 loc = loc->dw_loc_next;
2350
2351 fputc (',', asm_out_file);
2352 }
2353 }
2354
2355 /* This function builds a dwarf location descriptor sequence from a
2356 dw_cfa_location, adding the given OFFSET to the result of the
2357 expression. */
2358
2359 struct dw_loc_descr_struct *
2360 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2361 {
2362 struct dw_loc_descr_struct *head, *tmp;
2363
2364 offset += cfa->offset;
2365
2366 if (cfa->indirect)
2367 {
2368 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2369 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2370 head->dw_loc_oprnd1.val_entry = NULL;
2371 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2372 add_loc_descr (&head, tmp);
2373 if (offset != 0)
2374 {
2375 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2376 add_loc_descr (&head, tmp);
2377 }
2378 }
2379 else
2380 head = new_reg_loc_descr (cfa->reg, offset);
2381
2382 return head;
2383 }
2384
2385 /* This function builds a dwarf location descriptor sequence for
2386 the address at OFFSET from the CFA when stack is aligned to
2387 ALIGNMENT byte. */
2388
2389 struct dw_loc_descr_struct *
2390 build_cfa_aligned_loc (dw_cfa_location *cfa,
2391 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2392 {
2393 struct dw_loc_descr_struct *head;
2394 unsigned int dwarf_fp
2395 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2396
2397 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2398 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2399 {
2400 head = new_reg_loc_descr (dwarf_fp, 0);
2401 add_loc_descr (&head, int_loc_descriptor (alignment));
2402 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2403 loc_descr_plus_const (&head, offset);
2404 }
2405 else
2406 head = new_reg_loc_descr (dwarf_fp, offset);
2407 return head;
2408 }
2409 \f
2410 /* And now, the support for symbolic debugging information. */
2411
2412 /* .debug_str support. */
2413 static int output_indirect_string (void **, void *);
2414
2415 static void dwarf2out_init (const char *);
2416 static void dwarf2out_finish (const char *);
2417 static void dwarf2out_assembly_start (void);
2418 static void dwarf2out_define (unsigned int, const char *);
2419 static void dwarf2out_undef (unsigned int, const char *);
2420 static void dwarf2out_start_source_file (unsigned, const char *);
2421 static void dwarf2out_end_source_file (unsigned);
2422 static void dwarf2out_function_decl (tree);
2423 static void dwarf2out_begin_block (unsigned, unsigned);
2424 static void dwarf2out_end_block (unsigned, unsigned);
2425 static bool dwarf2out_ignore_block (const_tree);
2426 static void dwarf2out_global_decl (tree);
2427 static void dwarf2out_type_decl (tree, int);
2428 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2429 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2430 dw_die_ref);
2431 static void dwarf2out_abstract_function (tree);
2432 static void dwarf2out_var_location (rtx);
2433 static void dwarf2out_begin_function (tree);
2434 static void dwarf2out_end_function (unsigned int);
2435 static void dwarf2out_set_name (tree, tree);
2436
2437 /* The debug hooks structure. */
2438
2439 const struct gcc_debug_hooks dwarf2_debug_hooks =
2440 {
2441 dwarf2out_init,
2442 dwarf2out_finish,
2443 dwarf2out_assembly_start,
2444 dwarf2out_define,
2445 dwarf2out_undef,
2446 dwarf2out_start_source_file,
2447 dwarf2out_end_source_file,
2448 dwarf2out_begin_block,
2449 dwarf2out_end_block,
2450 dwarf2out_ignore_block,
2451 dwarf2out_source_line,
2452 dwarf2out_begin_prologue,
2453 #if VMS_DEBUGGING_INFO
2454 dwarf2out_vms_end_prologue,
2455 dwarf2out_vms_begin_epilogue,
2456 #else
2457 debug_nothing_int_charstar,
2458 debug_nothing_int_charstar,
2459 #endif
2460 dwarf2out_end_epilogue,
2461 dwarf2out_begin_function,
2462 dwarf2out_end_function, /* end_function */
2463 dwarf2out_function_decl, /* function_decl */
2464 dwarf2out_global_decl,
2465 dwarf2out_type_decl, /* type_decl */
2466 dwarf2out_imported_module_or_decl,
2467 debug_nothing_tree, /* deferred_inline_function */
2468 /* The DWARF 2 backend tries to reduce debugging bloat by not
2469 emitting the abstract description of inline functions until
2470 something tries to reference them. */
2471 dwarf2out_abstract_function, /* outlining_inline_function */
2472 debug_nothing_rtx, /* label */
2473 debug_nothing_int, /* handle_pch */
2474 dwarf2out_var_location,
2475 dwarf2out_switch_text_section,
2476 dwarf2out_set_name,
2477 1, /* start_end_main_source_file */
2478 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2479 };
2480 \f
2481 /* NOTE: In the comments in this file, many references are made to
2482 "Debugging Information Entries". This term is abbreviated as `DIE'
2483 throughout the remainder of this file. */
2484
2485 /* An internal representation of the DWARF output is built, and then
2486 walked to generate the DWARF debugging info. The walk of the internal
2487 representation is done after the entire program has been compiled.
2488 The types below are used to describe the internal representation. */
2489
2490 /* Whether to put type DIEs into their own section .debug_types instead
2491 of making them part of the .debug_info section. Only supported for
2492 Dwarf V4 or higher and the user didn't disable them through
2493 -fno-debug-types-section. It is more efficient to put them in a
2494 separate comdat sections since the linker will then be able to
2495 remove duplicates. But not all tools support .debug_types sections
2496 yet. */
2497
2498 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2499
2500 /* Various DIE's use offsets relative to the beginning of the
2501 .debug_info section to refer to each other. */
2502
2503 typedef long int dw_offset;
2504
2505 /* Define typedefs here to avoid circular dependencies. */
2506
2507 typedef struct dw_attr_struct *dw_attr_ref;
2508 typedef struct dw_line_info_struct *dw_line_info_ref;
2509 typedef struct pubname_struct *pubname_ref;
2510 typedef struct dw_ranges_struct *dw_ranges_ref;
2511 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2512 typedef struct comdat_type_struct *comdat_type_node_ref;
2513
2514 /* The entries in the line_info table more-or-less mirror the opcodes
2515 that are used in the real dwarf line table. Arrays of these entries
2516 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2517 supported. */
2518
2519 enum dw_line_info_opcode {
2520 /* Emit DW_LNE_set_address; the operand is the label index. */
2521 LI_set_address,
2522
2523 /* Emit a row to the matrix with the given line. This may be done
2524 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2525 special opcodes. */
2526 LI_set_line,
2527
2528 /* Emit a DW_LNS_set_file. */
2529 LI_set_file,
2530
2531 /* Emit a DW_LNS_set_column. */
2532 LI_set_column,
2533
2534 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2535 LI_negate_stmt,
2536
2537 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2538 LI_set_prologue_end,
2539 LI_set_epilogue_begin,
2540
2541 /* Emit a DW_LNE_set_discriminator. */
2542 LI_set_discriminator
2543 };
2544
2545 typedef struct GTY(()) dw_line_info_struct {
2546 enum dw_line_info_opcode opcode;
2547 unsigned int val;
2548 } dw_line_info_entry;
2549
2550
2551 typedef struct GTY(()) dw_line_info_table_struct {
2552 /* The label that marks the end of this section. */
2553 const char *end_label;
2554
2555 /* The values for the last row of the matrix, as collected in the table.
2556 These are used to minimize the changes to the next row. */
2557 unsigned int file_num;
2558 unsigned int line_num;
2559 unsigned int column_num;
2560 int discrim_num;
2561 bool is_stmt;
2562 bool in_use;
2563
2564 vec<dw_line_info_entry, va_gc> *entries;
2565 } dw_line_info_table;
2566
2567 typedef dw_line_info_table *dw_line_info_table_p;
2568
2569
2570 /* Each DIE attribute has a field specifying the attribute kind,
2571 a link to the next attribute in the chain, and an attribute value.
2572 Attributes are typically linked below the DIE they modify. */
2573
2574 typedef struct GTY(()) dw_attr_struct {
2575 enum dwarf_attribute dw_attr;
2576 dw_val_node dw_attr_val;
2577 }
2578 dw_attr_node;
2579
2580
2581 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2582 The children of each node form a circular list linked by
2583 die_sib. die_child points to the node *before* the "first" child node. */
2584
2585 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2586 union die_symbol_or_type_node
2587 {
2588 const char * GTY ((tag ("0"))) die_symbol;
2589 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2590 }
2591 GTY ((desc ("%0.comdat_type_p"))) die_id;
2592 vec<dw_attr_node, va_gc> *die_attr;
2593 dw_die_ref die_parent;
2594 dw_die_ref die_child;
2595 dw_die_ref die_sib;
2596 dw_die_ref die_definition; /* ref from a specification to its definition */
2597 dw_offset die_offset;
2598 unsigned long die_abbrev;
2599 int die_mark;
2600 unsigned int decl_id;
2601 enum dwarf_tag die_tag;
2602 /* Die is used and must not be pruned as unused. */
2603 BOOL_BITFIELD die_perennial_p : 1;
2604 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2605 /* Lots of spare bits. */
2606 }
2607 die_node;
2608
2609 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2610 #define FOR_EACH_CHILD(die, c, expr) do { \
2611 c = die->die_child; \
2612 if (c) do { \
2613 c = c->die_sib; \
2614 expr; \
2615 } while (c != die->die_child); \
2616 } while (0)
2617
2618 /* The pubname structure */
2619
2620 typedef struct GTY(()) pubname_struct {
2621 dw_die_ref die;
2622 const char *name;
2623 }
2624 pubname_entry;
2625
2626
2627 struct GTY(()) dw_ranges_struct {
2628 /* If this is positive, it's a block number, otherwise it's a
2629 bitwise-negated index into dw_ranges_by_label. */
2630 int num;
2631 };
2632
2633 /* A structure to hold a macinfo entry. */
2634
2635 typedef struct GTY(()) macinfo_struct {
2636 unsigned char code;
2637 unsigned HOST_WIDE_INT lineno;
2638 const char *info;
2639 }
2640 macinfo_entry;
2641
2642
2643 struct GTY(()) dw_ranges_by_label_struct {
2644 const char *begin;
2645 const char *end;
2646 };
2647
2648 /* The comdat type node structure. */
2649 typedef struct GTY(()) comdat_type_struct
2650 {
2651 dw_die_ref root_die;
2652 dw_die_ref type_die;
2653 dw_die_ref skeleton_die;
2654 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2655 struct comdat_type_struct *next;
2656 }
2657 comdat_type_node;
2658
2659 /* The limbo die list structure. */
2660 typedef struct GTY(()) limbo_die_struct {
2661 dw_die_ref die;
2662 tree created_for;
2663 struct limbo_die_struct *next;
2664 }
2665 limbo_die_node;
2666
2667 typedef struct skeleton_chain_struct
2668 {
2669 dw_die_ref old_die;
2670 dw_die_ref new_die;
2671 struct skeleton_chain_struct *parent;
2672 }
2673 skeleton_chain_node;
2674
2675 /* Define a macro which returns nonzero for a TYPE_DECL which was
2676 implicitly generated for a type.
2677
2678 Note that, unlike the C front-end (which generates a NULL named
2679 TYPE_DECL node for each complete tagged type, each array type,
2680 and each function type node created) the C++ front-end generates
2681 a _named_ TYPE_DECL node for each tagged type node created.
2682 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2683 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2684 front-end, but for each type, tagged or not. */
2685
2686 #define TYPE_DECL_IS_STUB(decl) \
2687 (DECL_NAME (decl) == NULL_TREE \
2688 || (DECL_ARTIFICIAL (decl) \
2689 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2690 /* This is necessary for stub decls that \
2691 appear in nested inline functions. */ \
2692 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2693 && (decl_ultimate_origin (decl) \
2694 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2695
2696 /* Information concerning the compilation unit's programming
2697 language, and compiler version. */
2698
2699 /* Fixed size portion of the DWARF compilation unit header. */
2700 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2701 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2702
2703 /* Fixed size portion of the DWARF comdat type unit header. */
2704 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2705 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2706 + DWARF_OFFSET_SIZE)
2707
2708 /* Fixed size portion of public names info. */
2709 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2710
2711 /* Fixed size portion of the address range info. */
2712 #define DWARF_ARANGES_HEADER_SIZE \
2713 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2714 DWARF2_ADDR_SIZE * 2) \
2715 - DWARF_INITIAL_LENGTH_SIZE)
2716
2717 /* Size of padding portion in the address range info. It must be
2718 aligned to twice the pointer size. */
2719 #define DWARF_ARANGES_PAD_SIZE \
2720 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2721 DWARF2_ADDR_SIZE * 2) \
2722 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2723
2724 /* Use assembler line directives if available. */
2725 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2726 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2727 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2728 #else
2729 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2730 #endif
2731 #endif
2732
2733 /* Minimum line offset in a special line info. opcode.
2734 This value was chosen to give a reasonable range of values. */
2735 #define DWARF_LINE_BASE -10
2736
2737 /* First special line opcode - leave room for the standard opcodes. */
2738 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2739
2740 /* Range of line offsets in a special line info. opcode. */
2741 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2742
2743 /* Flag that indicates the initial value of the is_stmt_start flag.
2744 In the present implementation, we do not mark any lines as
2745 the beginning of a source statement, because that information
2746 is not made available by the GCC front-end. */
2747 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2748
2749 /* Maximum number of operations per instruction bundle. */
2750 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2751 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2752 #endif
2753
2754 /* This location is used by calc_die_sizes() to keep track
2755 the offset of each DIE within the .debug_info section. */
2756 static unsigned long next_die_offset;
2757
2758 /* Record the root of the DIE's built for the current compilation unit. */
2759 static GTY(()) dw_die_ref single_comp_unit_die;
2760
2761 /* A list of type DIEs that have been separated into comdat sections. */
2762 static GTY(()) comdat_type_node *comdat_type_list;
2763
2764 /* A list of DIEs with a NULL parent waiting to be relocated. */
2765 static GTY(()) limbo_die_node *limbo_die_list;
2766
2767 /* A list of DIEs for which we may have to generate
2768 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2769 static GTY(()) limbo_die_node *deferred_asm_name;
2770
2771 /* Filenames referenced by this compilation unit. */
2772 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2773
2774 /* A hash table of references to DIE's that describe declarations.
2775 The key is a DECL_UID() which is a unique number identifying each decl. */
2776 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2777
2778 /* A hash table of references to DIE's that describe COMMON blocks.
2779 The key is DECL_UID() ^ die_parent. */
2780 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2781
2782 typedef struct GTY(()) die_arg_entry_struct {
2783 dw_die_ref die;
2784 tree arg;
2785 } die_arg_entry;
2786
2787
2788 /* Node of the variable location list. */
2789 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2790 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2791 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2792 in mode of the EXPR_LIST node and first EXPR_LIST operand
2793 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2794 location or NULL for padding. For larger bitsizes,
2795 mode is 0 and first operand is a CONCAT with bitsize
2796 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2797 NULL as second operand. */
2798 rtx GTY (()) loc;
2799 const char * GTY (()) label;
2800 struct var_loc_node * GTY (()) next;
2801 };
2802
2803 /* Variable location list. */
2804 struct GTY (()) var_loc_list_def {
2805 struct var_loc_node * GTY (()) first;
2806
2807 /* Pointer to the last but one or last element of the
2808 chained list. If the list is empty, both first and
2809 last are NULL, if the list contains just one node
2810 or the last node certainly is not redundant, it points
2811 to the last node, otherwise points to the last but one.
2812 Do not mark it for GC because it is marked through the chain. */
2813 struct var_loc_node * GTY ((skip ("%h"))) last;
2814
2815 /* Pointer to the last element before section switch,
2816 if NULL, either sections weren't switched or first
2817 is after section switch. */
2818 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2819
2820 /* DECL_UID of the variable decl. */
2821 unsigned int decl_id;
2822 };
2823 typedef struct var_loc_list_def var_loc_list;
2824
2825 /* Call argument location list. */
2826 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2827 rtx GTY (()) call_arg_loc_note;
2828 const char * GTY (()) label;
2829 tree GTY (()) block;
2830 bool tail_call_p;
2831 rtx GTY (()) symbol_ref;
2832 struct call_arg_loc_node * GTY (()) next;
2833 };
2834
2835
2836 /* Table of decl location linked lists. */
2837 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2838
2839 /* Head and tail of call_arg_loc chain. */
2840 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2841 static struct call_arg_loc_node *call_arg_loc_last;
2842
2843 /* Number of call sites in the current function. */
2844 static int call_site_count = -1;
2845 /* Number of tail call sites in the current function. */
2846 static int tail_call_site_count = -1;
2847
2848 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2849 DIEs. */
2850 static vec<dw_die_ref> block_map;
2851
2852 /* A cached location list. */
2853 struct GTY (()) cached_dw_loc_list_def {
2854 /* The DECL_UID of the decl that this entry describes. */
2855 unsigned int decl_id;
2856
2857 /* The cached location list. */
2858 dw_loc_list_ref loc_list;
2859 };
2860 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2861
2862 /* Table of cached location lists. */
2863 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2864
2865 /* A pointer to the base of a list of references to DIE's that
2866 are uniquely identified by their tag, presence/absence of
2867 children DIE's, and list of attribute/value pairs. */
2868 static GTY((length ("abbrev_die_table_allocated")))
2869 dw_die_ref *abbrev_die_table;
2870
2871 /* Number of elements currently allocated for abbrev_die_table. */
2872 static GTY(()) unsigned abbrev_die_table_allocated;
2873
2874 /* Number of elements in type_die_table currently in use. */
2875 static GTY(()) unsigned abbrev_die_table_in_use;
2876
2877 /* Size (in elements) of increments by which we may expand the
2878 abbrev_die_table. */
2879 #define ABBREV_DIE_TABLE_INCREMENT 256
2880
2881 /* A global counter for generating labels for line number data. */
2882 static unsigned int line_info_label_num;
2883
2884 /* The current table to which we should emit line number information
2885 for the current function. This will be set up at the beginning of
2886 assembly for the function. */
2887 static dw_line_info_table *cur_line_info_table;
2888
2889 /* The two default tables of line number info. */
2890 static GTY(()) dw_line_info_table *text_section_line_info;
2891 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2892
2893 /* The set of all non-default tables of line number info. */
2894 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2895
2896 /* A flag to tell pubnames/types export if there is an info section to
2897 refer to. */
2898 static bool info_section_emitted;
2899
2900 /* A pointer to the base of a table that contains a list of publicly
2901 accessible names. */
2902 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2903
2904 /* A pointer to the base of a table that contains a list of publicly
2905 accessible types. */
2906 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2907
2908 /* A pointer to the base of a table that contains a list of macro
2909 defines/undefines (and file start/end markers). */
2910 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2911
2912 /* True if .debug_macinfo or .debug_macros section is going to be
2913 emitted. */
2914 #define have_macinfo \
2915 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2916 && !macinfo_table->is_empty ())
2917
2918 /* Array of dies for which we should generate .debug_ranges info. */
2919 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2920
2921 /* Number of elements currently allocated for ranges_table. */
2922 static GTY(()) unsigned ranges_table_allocated;
2923
2924 /* Number of elements in ranges_table currently in use. */
2925 static GTY(()) unsigned ranges_table_in_use;
2926
2927 /* Array of pairs of labels referenced in ranges_table. */
2928 static GTY ((length ("ranges_by_label_allocated")))
2929 dw_ranges_by_label_ref ranges_by_label;
2930
2931 /* Number of elements currently allocated for ranges_by_label. */
2932 static GTY(()) unsigned ranges_by_label_allocated;
2933
2934 /* Number of elements in ranges_by_label currently in use. */
2935 static GTY(()) unsigned ranges_by_label_in_use;
2936
2937 /* Size (in elements) of increments by which we may expand the
2938 ranges_table. */
2939 #define RANGES_TABLE_INCREMENT 64
2940
2941 /* Whether we have location lists that need outputting */
2942 static GTY(()) bool have_location_lists;
2943
2944 /* Unique label counter. */
2945 static GTY(()) unsigned int loclabel_num;
2946
2947 /* Unique label counter for point-of-call tables. */
2948 static GTY(()) unsigned int poc_label_num;
2949
2950 /* Record whether the function being analyzed contains inlined functions. */
2951 static int current_function_has_inlines;
2952
2953 /* The last file entry emitted by maybe_emit_file(). */
2954 static GTY(()) struct dwarf_file_data * last_emitted_file;
2955
2956 /* Number of internal labels generated by gen_internal_sym(). */
2957 static GTY(()) int label_num;
2958
2959 /* Cached result of previous call to lookup_filename. */
2960 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2961
2962 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2963
2964 /* Instances of generic types for which we need to generate debug
2965 info that describe their generic parameters and arguments. That
2966 generation needs to happen once all types are properly laid out so
2967 we do it at the end of compilation. */
2968 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2969
2970 /* Offset from the "steady-state frame pointer" to the frame base,
2971 within the current function. */
2972 static HOST_WIDE_INT frame_pointer_fb_offset;
2973 static bool frame_pointer_fb_offset_valid;
2974
2975 static vec<dw_die_ref> base_types;
2976
2977 /* Forward declarations for functions defined in this file. */
2978
2979 static int is_pseudo_reg (const_rtx);
2980 static tree type_main_variant (tree);
2981 static int is_tagged_type (const_tree);
2982 static const char *dwarf_tag_name (unsigned);
2983 static const char *dwarf_attr_name (unsigned);
2984 static const char *dwarf_form_name (unsigned);
2985 static tree decl_ultimate_origin (const_tree);
2986 static tree decl_class_context (tree);
2987 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2988 static inline enum dw_val_class AT_class (dw_attr_ref);
2989 static inline unsigned int AT_index (dw_attr_ref);
2990 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2991 static inline unsigned AT_flag (dw_attr_ref);
2992 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2993 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2994 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2995 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2996 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2997 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2998 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2999 unsigned int, unsigned char *);
3000 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3001 static hashval_t debug_str_do_hash (const void *);
3002 static int debug_str_eq (const void *, const void *);
3003 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3004 static inline const char *AT_string (dw_attr_ref);
3005 static enum dwarf_form AT_string_form (dw_attr_ref);
3006 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3007 static void add_AT_specification (dw_die_ref, dw_die_ref);
3008 static inline dw_die_ref AT_ref (dw_attr_ref);
3009 static inline int AT_ref_external (dw_attr_ref);
3010 static inline void set_AT_ref_external (dw_attr_ref, int);
3011 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3012 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3013 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3014 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3015 dw_loc_list_ref);
3016 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3017 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3018 static void remove_addr_table_entry (addr_table_entry *);
3019 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3020 static inline rtx AT_addr (dw_attr_ref);
3021 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3022 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3023 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3024 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3025 unsigned HOST_WIDE_INT);
3026 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3027 unsigned long, bool);
3028 static inline const char *AT_lbl (dw_attr_ref);
3029 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3030 static const char *get_AT_low_pc (dw_die_ref);
3031 static const char *get_AT_hi_pc (dw_die_ref);
3032 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3033 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3034 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3035 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3036 static bool is_cxx (void);
3037 static bool is_fortran (void);
3038 static bool is_ada (void);
3039 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3040 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3041 static void add_child_die (dw_die_ref, dw_die_ref);
3042 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3043 static dw_die_ref lookup_type_die (tree);
3044 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3045 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3046 static void equate_type_number_to_die (tree, dw_die_ref);
3047 static hashval_t decl_die_table_hash (const void *);
3048 static int decl_die_table_eq (const void *, const void *);
3049 static dw_die_ref lookup_decl_die (tree);
3050 static hashval_t common_block_die_table_hash (const void *);
3051 static int common_block_die_table_eq (const void *, const void *);
3052 static hashval_t decl_loc_table_hash (const void *);
3053 static int decl_loc_table_eq (const void *, const void *);
3054 static var_loc_list *lookup_decl_loc (const_tree);
3055 static void equate_decl_number_to_die (tree, dw_die_ref);
3056 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3057 static void print_spaces (FILE *);
3058 static void print_die (dw_die_ref, FILE *);
3059 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3060 static dw_die_ref pop_compile_unit (dw_die_ref);
3061 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3062 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3063 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3064 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3065 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3066 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3067 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3068 struct md5_ctx *, int *);
3069 struct checksum_attributes;
3070 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3071 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3072 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3073 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3074 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3075 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3076 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3077 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3078 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3079 static void compute_section_prefix (dw_die_ref);
3080 static int is_type_die (dw_die_ref);
3081 static int is_comdat_die (dw_die_ref);
3082 static int is_symbol_die (dw_die_ref);
3083 static inline bool is_template_instantiation (dw_die_ref);
3084 static void assign_symbol_names (dw_die_ref);
3085 static void break_out_includes (dw_die_ref);
3086 static int is_declaration_die (dw_die_ref);
3087 static int should_move_die_to_comdat (dw_die_ref);
3088 static dw_die_ref clone_as_declaration (dw_die_ref);
3089 static dw_die_ref clone_die (dw_die_ref);
3090 static dw_die_ref clone_tree (dw_die_ref);
3091 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3092 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3093 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3094 static dw_die_ref generate_skeleton (dw_die_ref);
3095 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3096 dw_die_ref,
3097 dw_die_ref);
3098 static void break_out_comdat_types (dw_die_ref);
3099 static void copy_decls_for_unworthy_types (dw_die_ref);
3100
3101 static void add_sibling_attributes (dw_die_ref);
3102 static void output_location_lists (dw_die_ref);
3103 static int constant_size (unsigned HOST_WIDE_INT);
3104 static unsigned long size_of_die (dw_die_ref);
3105 static void calc_die_sizes (dw_die_ref);
3106 static void calc_base_type_die_sizes (void);
3107 static void mark_dies (dw_die_ref);
3108 static void unmark_dies (dw_die_ref);
3109 static void unmark_all_dies (dw_die_ref);
3110 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3111 static unsigned long size_of_aranges (void);
3112 static enum dwarf_form value_format (dw_attr_ref);
3113 static void output_value_format (dw_attr_ref);
3114 static void output_abbrev_section (void);
3115 static void output_die_abbrevs (unsigned long, dw_die_ref);
3116 static void output_die_symbol (dw_die_ref);
3117 static void output_die (dw_die_ref);
3118 static void output_compilation_unit_header (void);
3119 static void output_comp_unit (dw_die_ref, int);
3120 static void output_comdat_type_unit (comdat_type_node *);
3121 static const char *dwarf2_name (tree, int);
3122 static void add_pubname (tree, dw_die_ref);
3123 static void add_enumerator_pubname (const char *, dw_die_ref);
3124 static void add_pubname_string (const char *, dw_die_ref);
3125 static void add_pubtype (tree, dw_die_ref);
3126 static void output_pubnames (vec<pubname_entry, va_gc> *);
3127 static void output_aranges (unsigned long);
3128 static unsigned int add_ranges_num (int);
3129 static unsigned int add_ranges (const_tree);
3130 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3131 bool *, bool);
3132 static void output_ranges (void);
3133 static dw_line_info_table *new_line_info_table (void);
3134 static void output_line_info (bool);
3135 static void output_file_names (void);
3136 static dw_die_ref base_type_die (tree);
3137 static int is_base_type (tree);
3138 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3139 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3140 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3141 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3142 static int type_is_enum (const_tree);
3143 static unsigned int dbx_reg_number (const_rtx);
3144 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3145 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3146 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3147 enum var_init_status);
3148 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3149 enum var_init_status);
3150 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3151 enum var_init_status);
3152 static int is_based_loc (const_rtx);
3153 static int resolve_one_addr (rtx *, void *);
3154 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3155 enum var_init_status);
3156 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3157 enum var_init_status);
3158 static dw_loc_list_ref loc_list_from_tree (tree, int);
3159 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3160 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3161 static tree field_type (const_tree);
3162 static unsigned int simple_type_align_in_bits (const_tree);
3163 static unsigned int simple_decl_align_in_bits (const_tree);
3164 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3165 static HOST_WIDE_INT field_byte_offset (const_tree);
3166 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3167 dw_loc_list_ref);
3168 static void add_data_member_location_attribute (dw_die_ref, tree);
3169 static bool add_const_value_attribute (dw_die_ref, rtx);
3170 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3171 static void insert_wide_int (const wide_int &, unsigned char *, int);
3172 static void insert_float (const_rtx, unsigned char *);
3173 static rtx rtl_for_decl_location (tree);
3174 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3175 enum dwarf_attribute);
3176 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3177 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3178 static void add_name_attribute (dw_die_ref, const char *);
3179 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3180 static void add_comp_dir_attribute (dw_die_ref);
3181 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3182 static void add_subscript_info (dw_die_ref, tree, bool);
3183 static void add_byte_size_attribute (dw_die_ref, tree);
3184 static void add_bit_offset_attribute (dw_die_ref, tree);
3185 static void add_bit_size_attribute (dw_die_ref, tree);
3186 static void add_prototyped_attribute (dw_die_ref, tree);
3187 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3188 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3189 static void add_src_coords_attributes (dw_die_ref, tree);
3190 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3191 static void push_decl_scope (tree);
3192 static void pop_decl_scope (void);
3193 static dw_die_ref scope_die_for (tree, dw_die_ref);
3194 static inline int local_scope_p (dw_die_ref);
3195 static inline int class_scope_p (dw_die_ref);
3196 static inline int class_or_namespace_scope_p (dw_die_ref);
3197 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3198 static void add_calling_convention_attribute (dw_die_ref, tree);
3199 static const char *type_tag (const_tree);
3200 static tree member_declared_type (const_tree);
3201 #if 0
3202 static const char *decl_start_label (tree);
3203 #endif
3204 static void gen_array_type_die (tree, dw_die_ref);
3205 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3206 #if 0
3207 static void gen_entry_point_die (tree, dw_die_ref);
3208 #endif
3209 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3210 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3211 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3212 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3213 static void gen_formal_types_die (tree, dw_die_ref);
3214 static void gen_subprogram_die (tree, dw_die_ref);
3215 static void gen_variable_die (tree, tree, dw_die_ref);
3216 static void gen_const_die (tree, dw_die_ref);
3217 static void gen_label_die (tree, dw_die_ref);
3218 static void gen_lexical_block_die (tree, dw_die_ref, int);
3219 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3220 static void gen_field_die (tree, dw_die_ref);
3221 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3222 static dw_die_ref gen_compile_unit_die (const char *);
3223 static void gen_inheritance_die (tree, tree, dw_die_ref);
3224 static void gen_member_die (tree, dw_die_ref);
3225 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3226 enum debug_info_usage);
3227 static void gen_subroutine_type_die (tree, dw_die_ref);
3228 static void gen_typedef_die (tree, dw_die_ref);
3229 static void gen_type_die (tree, dw_die_ref);
3230 static void gen_block_die (tree, dw_die_ref, int);
3231 static void decls_for_scope (tree, dw_die_ref, int);
3232 static inline int is_redundant_typedef (const_tree);
3233 static bool is_naming_typedef_decl (const_tree);
3234 static inline dw_die_ref get_context_die (tree);
3235 static void gen_namespace_die (tree, dw_die_ref);
3236 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3237 static dw_die_ref force_decl_die (tree);
3238 static dw_die_ref force_type_die (tree);
3239 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3240 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3241 static struct dwarf_file_data * lookup_filename (const char *);
3242 static void retry_incomplete_types (void);
3243 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3244 static void gen_generic_params_dies (tree);
3245 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3246 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3247 static void splice_child_die (dw_die_ref, dw_die_ref);
3248 static int file_info_cmp (const void *, const void *);
3249 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3250 const char *, const char *);
3251 static void output_loc_list (dw_loc_list_ref);
3252 static char *gen_internal_sym (const char *);
3253 static bool want_pubnames (void);
3254
3255 static void prune_unmark_dies (dw_die_ref);
3256 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3257 static void prune_unused_types_mark (dw_die_ref, int);
3258 static void prune_unused_types_walk (dw_die_ref);
3259 static void prune_unused_types_walk_attribs (dw_die_ref);
3260 static void prune_unused_types_prune (dw_die_ref);
3261 static void prune_unused_types (void);
3262 static int maybe_emit_file (struct dwarf_file_data *fd);
3263 static inline const char *AT_vms_delta1 (dw_attr_ref);
3264 static inline const char *AT_vms_delta2 (dw_attr_ref);
3265 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3266 const char *, const char *);
3267 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3268 static void gen_remaining_tmpl_value_param_die_attribute (void);
3269 static bool generic_type_p (tree);
3270 static void schedule_generic_params_dies_gen (tree t);
3271 static void gen_scheduled_generic_parms_dies (void);
3272
3273 static const char *comp_dir_string (void);
3274
3275 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3276
3277 /* enum for tracking thread-local variables whose address is really an offset
3278 relative to the TLS pointer, which will need link-time relocation, but will
3279 not need relocation by the DWARF consumer. */
3280
3281 enum dtprel_bool
3282 {
3283 dtprel_false = 0,
3284 dtprel_true = 1
3285 };
3286
3287 /* Return the operator to use for an address of a variable. For dtprel_true, we
3288 use DW_OP_const*. For regular variables, which need both link-time
3289 relocation and consumer-level relocation (e.g., to account for shared objects
3290 loaded at a random address), we use DW_OP_addr*. */
3291
3292 static inline enum dwarf_location_atom
3293 dw_addr_op (enum dtprel_bool dtprel)
3294 {
3295 if (dtprel == dtprel_true)
3296 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3297 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3298 else
3299 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3300 }
3301
3302 /* Return a pointer to a newly allocated address location description. If
3303 dwarf_split_debug_info is true, then record the address with the appropriate
3304 relocation. */
3305 static inline dw_loc_descr_ref
3306 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3307 {
3308 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3309
3310 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3311 ref->dw_loc_oprnd1.v.val_addr = addr;
3312 ref->dtprel = dtprel;
3313 if (dwarf_split_debug_info)
3314 ref->dw_loc_oprnd1.val_entry
3315 = add_addr_table_entry (addr,
3316 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3317 else
3318 ref->dw_loc_oprnd1.val_entry = NULL;
3319
3320 return ref;
3321 }
3322
3323 /* Section names used to hold DWARF debugging information. */
3324
3325 #ifndef DEBUG_INFO_SECTION
3326 #define DEBUG_INFO_SECTION ".debug_info"
3327 #endif
3328 #ifndef DEBUG_DWO_INFO_SECTION
3329 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3330 #endif
3331 #ifndef DEBUG_ABBREV_SECTION
3332 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3333 #endif
3334 #ifndef DEBUG_DWO_ABBREV_SECTION
3335 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3336 #endif
3337 #ifndef DEBUG_ARANGES_SECTION
3338 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3339 #endif
3340 #ifndef DEBUG_ADDR_SECTION
3341 #define DEBUG_ADDR_SECTION ".debug_addr"
3342 #endif
3343 #ifndef DEBUG_NORM_MACINFO_SECTION
3344 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3345 #endif
3346 #ifndef DEBUG_DWO_MACINFO_SECTION
3347 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3348 #endif
3349 #ifndef DEBUG_MACINFO_SECTION
3350 #define DEBUG_MACINFO_SECTION \
3351 (!dwarf_split_debug_info \
3352 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3353 #endif
3354 #ifndef DEBUG_NORM_MACRO_SECTION
3355 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3356 #endif
3357 #ifndef DEBUG_DWO_MACRO_SECTION
3358 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3359 #endif
3360 #ifndef DEBUG_MACRO_SECTION
3361 #define DEBUG_MACRO_SECTION \
3362 (!dwarf_split_debug_info \
3363 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3364 #endif
3365 #ifndef DEBUG_LINE_SECTION
3366 #define DEBUG_LINE_SECTION ".debug_line"
3367 #endif
3368 #ifndef DEBUG_DWO_LINE_SECTION
3369 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3370 #endif
3371 #ifndef DEBUG_LOC_SECTION
3372 #define DEBUG_LOC_SECTION ".debug_loc"
3373 #endif
3374 #ifndef DEBUG_DWO_LOC_SECTION
3375 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3376 #endif
3377 #ifndef DEBUG_PUBNAMES_SECTION
3378 #define DEBUG_PUBNAMES_SECTION \
3379 ((debug_generate_pub_sections == 2) \
3380 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3381 #endif
3382 #ifndef DEBUG_PUBTYPES_SECTION
3383 #define DEBUG_PUBTYPES_SECTION \
3384 ((debug_generate_pub_sections == 2) \
3385 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3386 #endif
3387 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3388 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3389 #ifndef DEBUG_STR_OFFSETS_SECTION
3390 #define DEBUG_STR_OFFSETS_SECTION \
3391 (!dwarf_split_debug_info \
3392 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3393 #endif
3394 #ifndef DEBUG_STR_DWO_SECTION
3395 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3396 #endif
3397 #ifndef DEBUG_STR_SECTION
3398 #define DEBUG_STR_SECTION ".debug_str"
3399 #endif
3400 #ifndef DEBUG_RANGES_SECTION
3401 #define DEBUG_RANGES_SECTION ".debug_ranges"
3402 #endif
3403
3404 /* Standard ELF section names for compiled code and data. */
3405 #ifndef TEXT_SECTION_NAME
3406 #define TEXT_SECTION_NAME ".text"
3407 #endif
3408
3409 /* Section flags for .debug_macinfo/.debug_macro section. */
3410 #define DEBUG_MACRO_SECTION_FLAGS \
3411 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3412
3413 /* Section flags for .debug_str section. */
3414 #define DEBUG_STR_SECTION_FLAGS \
3415 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3416 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3417 : SECTION_DEBUG)
3418
3419 /* Section flags for .debug_str.dwo section. */
3420 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3421
3422 /* Labels we insert at beginning sections we can reference instead of
3423 the section names themselves. */
3424
3425 #ifndef TEXT_SECTION_LABEL
3426 #define TEXT_SECTION_LABEL "Ltext"
3427 #endif
3428 #ifndef COLD_TEXT_SECTION_LABEL
3429 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3430 #endif
3431 #ifndef DEBUG_LINE_SECTION_LABEL
3432 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3433 #endif
3434 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3435 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3436 #endif
3437 #ifndef DEBUG_INFO_SECTION_LABEL
3438 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3439 #endif
3440 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3441 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3442 #endif
3443 #ifndef DEBUG_ABBREV_SECTION_LABEL
3444 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3445 #endif
3446 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3447 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3448 #endif
3449 #ifndef DEBUG_ADDR_SECTION_LABEL
3450 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3451 #endif
3452 #ifndef DEBUG_LOC_SECTION_LABEL
3453 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3454 #endif
3455 #ifndef DEBUG_RANGES_SECTION_LABEL
3456 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3457 #endif
3458 #ifndef DEBUG_MACINFO_SECTION_LABEL
3459 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3460 #endif
3461 #ifndef DEBUG_MACRO_SECTION_LABEL
3462 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3463 #endif
3464 #define SKELETON_COMP_DIE_ABBREV 1
3465 #define SKELETON_TYPE_DIE_ABBREV 2
3466
3467 /* Definitions of defaults for formats and names of various special
3468 (artificial) labels which may be generated within this file (when the -g
3469 options is used and DWARF2_DEBUGGING_INFO is in effect.
3470 If necessary, these may be overridden from within the tm.h file, but
3471 typically, overriding these defaults is unnecessary. */
3472
3473 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3474 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3475 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3476 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3477 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3478 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3479 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3480 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3481 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3482 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3483 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3484 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3485 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3486 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3487
3488 #ifndef TEXT_END_LABEL
3489 #define TEXT_END_LABEL "Letext"
3490 #endif
3491 #ifndef COLD_END_LABEL
3492 #define COLD_END_LABEL "Letext_cold"
3493 #endif
3494 #ifndef BLOCK_BEGIN_LABEL
3495 #define BLOCK_BEGIN_LABEL "LBB"
3496 #endif
3497 #ifndef BLOCK_END_LABEL
3498 #define BLOCK_END_LABEL "LBE"
3499 #endif
3500 #ifndef LINE_CODE_LABEL
3501 #define LINE_CODE_LABEL "LM"
3502 #endif
3503
3504 \f
3505 /* Return the root of the DIE's built for the current compilation unit. */
3506 static dw_die_ref
3507 comp_unit_die (void)
3508 {
3509 if (!single_comp_unit_die)
3510 single_comp_unit_die = gen_compile_unit_die (NULL);
3511 return single_comp_unit_die;
3512 }
3513
3514 /* We allow a language front-end to designate a function that is to be
3515 called to "demangle" any name before it is put into a DIE. */
3516
3517 static const char *(*demangle_name_func) (const char *);
3518
3519 void
3520 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3521 {
3522 demangle_name_func = func;
3523 }
3524
3525 /* Test if rtl node points to a pseudo register. */
3526
3527 static inline int
3528 is_pseudo_reg (const_rtx rtl)
3529 {
3530 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3531 || (GET_CODE (rtl) == SUBREG
3532 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3533 }
3534
3535 /* Return a reference to a type, with its const and volatile qualifiers
3536 removed. */
3537
3538 static inline tree
3539 type_main_variant (tree type)
3540 {
3541 type = TYPE_MAIN_VARIANT (type);
3542
3543 /* ??? There really should be only one main variant among any group of
3544 variants of a given type (and all of the MAIN_VARIANT values for all
3545 members of the group should point to that one type) but sometimes the C
3546 front-end messes this up for array types, so we work around that bug
3547 here. */
3548 if (TREE_CODE (type) == ARRAY_TYPE)
3549 while (type != TYPE_MAIN_VARIANT (type))
3550 type = TYPE_MAIN_VARIANT (type);
3551
3552 return type;
3553 }
3554
3555 /* Return nonzero if the given type node represents a tagged type. */
3556
3557 static inline int
3558 is_tagged_type (const_tree type)
3559 {
3560 enum tree_code code = TREE_CODE (type);
3561
3562 return (code == RECORD_TYPE || code == UNION_TYPE
3563 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3564 }
3565
3566 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3567
3568 static void
3569 get_ref_die_offset_label (char *label, dw_die_ref ref)
3570 {
3571 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3572 }
3573
3574 /* Return die_offset of a DIE reference to a base type. */
3575
3576 static unsigned long int
3577 get_base_type_offset (dw_die_ref ref)
3578 {
3579 if (ref->die_offset)
3580 return ref->die_offset;
3581 if (comp_unit_die ()->die_abbrev)
3582 {
3583 calc_base_type_die_sizes ();
3584 gcc_assert (ref->die_offset);
3585 }
3586 return ref->die_offset;
3587 }
3588
3589 /* Return die_offset of a DIE reference other than base type. */
3590
3591 static unsigned long int
3592 get_ref_die_offset (dw_die_ref ref)
3593 {
3594 gcc_assert (ref->die_offset);
3595 return ref->die_offset;
3596 }
3597
3598 /* Convert a DIE tag into its string name. */
3599
3600 static const char *
3601 dwarf_tag_name (unsigned int tag)
3602 {
3603 const char *name = get_DW_TAG_name (tag);
3604
3605 if (name != NULL)
3606 return name;
3607
3608 return "DW_TAG_<unknown>";
3609 }
3610
3611 /* Convert a DWARF attribute code into its string name. */
3612
3613 static const char *
3614 dwarf_attr_name (unsigned int attr)
3615 {
3616 const char *name;
3617
3618 switch (attr)
3619 {
3620 #if VMS_DEBUGGING_INFO
3621 case DW_AT_HP_prologue:
3622 return "DW_AT_HP_prologue";
3623 #else
3624 case DW_AT_MIPS_loop_unroll_factor:
3625 return "DW_AT_MIPS_loop_unroll_factor";
3626 #endif
3627
3628 #if VMS_DEBUGGING_INFO
3629 case DW_AT_HP_epilogue:
3630 return "DW_AT_HP_epilogue";
3631 #else
3632 case DW_AT_MIPS_stride:
3633 return "DW_AT_MIPS_stride";
3634 #endif
3635 }
3636
3637 name = get_DW_AT_name (attr);
3638
3639 if (name != NULL)
3640 return name;
3641
3642 return "DW_AT_<unknown>";
3643 }
3644
3645 /* Convert a DWARF value form code into its string name. */
3646
3647 static const char *
3648 dwarf_form_name (unsigned int form)
3649 {
3650 const char *name = get_DW_FORM_name (form);
3651
3652 if (name != NULL)
3653 return name;
3654
3655 return "DW_FORM_<unknown>";
3656 }
3657 \f
3658 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3659 instance of an inlined instance of a decl which is local to an inline
3660 function, so we have to trace all of the way back through the origin chain
3661 to find out what sort of node actually served as the original seed for the
3662 given block. */
3663
3664 static tree
3665 decl_ultimate_origin (const_tree decl)
3666 {
3667 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3668 return NULL_TREE;
3669
3670 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3671 nodes in the function to point to themselves; ignore that if
3672 we're trying to output the abstract instance of this function. */
3673 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3674 return NULL_TREE;
3675
3676 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3677 most distant ancestor, this should never happen. */
3678 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3679
3680 return DECL_ABSTRACT_ORIGIN (decl);
3681 }
3682
3683 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3684 of a virtual function may refer to a base class, so we check the 'this'
3685 parameter. */
3686
3687 static tree
3688 decl_class_context (tree decl)
3689 {
3690 tree context = NULL_TREE;
3691
3692 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3693 context = DECL_CONTEXT (decl);
3694 else
3695 context = TYPE_MAIN_VARIANT
3696 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3697
3698 if (context && !TYPE_P (context))
3699 context = NULL_TREE;
3700
3701 return context;
3702 }
3703 \f
3704 /* Add an attribute/value pair to a DIE. */
3705
3706 static inline void
3707 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3708 {
3709 /* Maybe this should be an assert? */
3710 if (die == NULL)
3711 return;
3712
3713 vec_safe_reserve (die->die_attr, 1);
3714 vec_safe_push (die->die_attr, *attr);
3715 }
3716
3717 static inline enum dw_val_class
3718 AT_class (dw_attr_ref a)
3719 {
3720 return a->dw_attr_val.val_class;
3721 }
3722
3723 /* Return the index for any attribute that will be referenced with a
3724 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3725 are stored in dw_attr_val.v.val_str for reference counting
3726 pruning. */
3727
3728 static inline unsigned int
3729 AT_index (dw_attr_ref a)
3730 {
3731 if (AT_class (a) == dw_val_class_str)
3732 return a->dw_attr_val.v.val_str->index;
3733 else if (a->dw_attr_val.val_entry != NULL)
3734 return a->dw_attr_val.val_entry->index;
3735 return NOT_INDEXED;
3736 }
3737
3738 /* Add a flag value attribute to a DIE. */
3739
3740 static inline void
3741 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3742 {
3743 dw_attr_node attr;
3744
3745 attr.dw_attr = attr_kind;
3746 attr.dw_attr_val.val_class = dw_val_class_flag;
3747 attr.dw_attr_val.val_entry = NULL;
3748 attr.dw_attr_val.v.val_flag = flag;
3749 add_dwarf_attr (die, &attr);
3750 }
3751
3752 static inline unsigned
3753 AT_flag (dw_attr_ref a)
3754 {
3755 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3756 return a->dw_attr_val.v.val_flag;
3757 }
3758
3759 /* Add a signed integer attribute value to a DIE. */
3760
3761 static inline void
3762 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3763 {
3764 dw_attr_node attr;
3765
3766 attr.dw_attr = attr_kind;
3767 attr.dw_attr_val.val_class = dw_val_class_const;
3768 attr.dw_attr_val.val_entry = NULL;
3769 attr.dw_attr_val.v.val_int = int_val;
3770 add_dwarf_attr (die, &attr);
3771 }
3772
3773 static inline HOST_WIDE_INT
3774 AT_int (dw_attr_ref a)
3775 {
3776 gcc_assert (a && AT_class (a) == dw_val_class_const);
3777 return a->dw_attr_val.v.val_int;
3778 }
3779
3780 /* Add an unsigned integer attribute value to a DIE. */
3781
3782 static inline void
3783 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3784 unsigned HOST_WIDE_INT unsigned_val)
3785 {
3786 dw_attr_node attr;
3787
3788 attr.dw_attr = attr_kind;
3789 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3790 attr.dw_attr_val.val_entry = NULL;
3791 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3792 add_dwarf_attr (die, &attr);
3793 }
3794
3795 static inline unsigned HOST_WIDE_INT
3796 AT_unsigned (dw_attr_ref a)
3797 {
3798 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3799 return a->dw_attr_val.v.val_unsigned;
3800 }
3801
3802 /* Add an unsigned wide integer attribute value to a DIE. */
3803
3804 static inline void
3805 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3806 const wide_int& w)
3807 {
3808 dw_attr_node attr;
3809
3810 attr.dw_attr = attr_kind;
3811 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3812 attr.dw_attr_val.v.val_wide = ggc_alloc_cleared_wide_int ();
3813 *attr.dw_attr_val.v.val_wide = w;
3814 add_dwarf_attr (die, &attr);
3815 }
3816
3817 /* Add an unsigned double integer attribute value to a DIE. */
3818
3819 static inline void
3820 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3821 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3822 {
3823 dw_attr_node attr;
3824
3825 attr.dw_attr = attr_kind;
3826 attr.dw_attr_val.val_class = dw_val_class_const_double;
3827 attr.dw_attr_val.val_entry = NULL;
3828 attr.dw_attr_val.v.val_double.high = high;
3829 attr.dw_attr_val.v.val_double.low = low;
3830 add_dwarf_attr (die, &attr);
3831 }
3832
3833 /* Add a floating point attribute value to a DIE and return it. */
3834
3835 static inline void
3836 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3837 unsigned int length, unsigned int elt_size, unsigned char *array)
3838 {
3839 dw_attr_node attr;
3840
3841 attr.dw_attr = attr_kind;
3842 attr.dw_attr_val.val_class = dw_val_class_vec;
3843 attr.dw_attr_val.val_entry = NULL;
3844 attr.dw_attr_val.v.val_vec.length = length;
3845 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3846 attr.dw_attr_val.v.val_vec.array = array;
3847 add_dwarf_attr (die, &attr);
3848 }
3849
3850 /* Add an 8-byte data attribute value to a DIE. */
3851
3852 static inline void
3853 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3854 unsigned char data8[8])
3855 {
3856 dw_attr_node attr;
3857
3858 attr.dw_attr = attr_kind;
3859 attr.dw_attr_val.val_class = dw_val_class_data8;
3860 attr.dw_attr_val.val_entry = NULL;
3861 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3862 add_dwarf_attr (die, &attr);
3863 }
3864
3865 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3866 dwarf_split_debug_info, address attributes in dies destined for the
3867 final executable have force_direct set to avoid using indexed
3868 references. */
3869
3870 static inline void
3871 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3872 bool force_direct)
3873 {
3874 dw_attr_node attr;
3875 char * lbl_id;
3876
3877 lbl_id = xstrdup (lbl_low);
3878 attr.dw_attr = DW_AT_low_pc;
3879 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3880 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3881 if (dwarf_split_debug_info && !force_direct)
3882 attr.dw_attr_val.val_entry
3883 = add_addr_table_entry (lbl_id, ate_kind_label);
3884 else
3885 attr.dw_attr_val.val_entry = NULL;
3886 add_dwarf_attr (die, &attr);
3887
3888 attr.dw_attr = DW_AT_high_pc;
3889 if (dwarf_version < 4)
3890 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3891 else
3892 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3893 lbl_id = xstrdup (lbl_high);
3894 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3895 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3896 && dwarf_split_debug_info && !force_direct)
3897 attr.dw_attr_val.val_entry
3898 = add_addr_table_entry (lbl_id, ate_kind_label);
3899 else
3900 attr.dw_attr_val.val_entry = NULL;
3901 add_dwarf_attr (die, &attr);
3902 }
3903
3904 /* Hash and equality functions for debug_str_hash. */
3905
3906 static hashval_t
3907 debug_str_do_hash (const void *x)
3908 {
3909 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3910 }
3911
3912 static int
3913 debug_str_eq (const void *x1, const void *x2)
3914 {
3915 return strcmp ((((const struct indirect_string_node *)x1)->str),
3916 (const char *)x2) == 0;
3917 }
3918
3919 /* Add STR to the given string hash table. */
3920
3921 static struct indirect_string_node *
3922 find_AT_string_in_table (const char *str, htab_t table)
3923 {
3924 struct indirect_string_node *node;
3925 void **slot;
3926
3927 slot = htab_find_slot_with_hash (table, str,
3928 htab_hash_string (str), INSERT);
3929 if (*slot == NULL)
3930 {
3931 node = ggc_alloc_cleared_indirect_string_node ();
3932 node->str = ggc_strdup (str);
3933 *slot = node;
3934 }
3935 else
3936 node = (struct indirect_string_node *) *slot;
3937
3938 node->refcount++;
3939 return node;
3940 }
3941
3942 /* Add STR to the indirect string hash table. */
3943
3944 static struct indirect_string_node *
3945 find_AT_string (const char *str)
3946 {
3947 if (! debug_str_hash)
3948 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3949 debug_str_eq, NULL);
3950
3951 return find_AT_string_in_table (str, debug_str_hash);
3952 }
3953
3954 /* Add a string attribute value to a DIE. */
3955
3956 static inline void
3957 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3958 {
3959 dw_attr_node attr;
3960 struct indirect_string_node *node;
3961
3962 node = find_AT_string (str);
3963
3964 attr.dw_attr = attr_kind;
3965 attr.dw_attr_val.val_class = dw_val_class_str;
3966 attr.dw_attr_val.val_entry = NULL;
3967 attr.dw_attr_val.v.val_str = node;
3968 add_dwarf_attr (die, &attr);
3969 }
3970
3971 static inline const char *
3972 AT_string (dw_attr_ref a)
3973 {
3974 gcc_assert (a && AT_class (a) == dw_val_class_str);
3975 return a->dw_attr_val.v.val_str->str;
3976 }
3977
3978 /* Call this function directly to bypass AT_string_form's logic to put
3979 the string inline in the die. */
3980
3981 static void
3982 set_indirect_string (struct indirect_string_node *node)
3983 {
3984 char label[32];
3985 /* Already indirect is a no op. */
3986 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3987 {
3988 gcc_assert (node->label);
3989 return;
3990 }
3991 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3992 ++dw2_string_counter;
3993 node->label = xstrdup (label);
3994
3995 if (!dwarf_split_debug_info)
3996 {
3997 node->form = DW_FORM_strp;
3998 node->index = NOT_INDEXED;
3999 }
4000 else
4001 {
4002 node->form = DW_FORM_GNU_str_index;
4003 node->index = NO_INDEX_ASSIGNED;
4004 }
4005 }
4006
4007 /* Find out whether a string should be output inline in DIE
4008 or out-of-line in .debug_str section. */
4009
4010 static enum dwarf_form
4011 find_string_form (struct indirect_string_node *node)
4012 {
4013 unsigned int len;
4014
4015 if (node->form)
4016 return node->form;
4017
4018 len = strlen (node->str) + 1;
4019
4020 /* If the string is shorter or equal to the size of the reference, it is
4021 always better to put it inline. */
4022 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4023 return node->form = DW_FORM_string;
4024
4025 /* If we cannot expect the linker to merge strings in .debug_str
4026 section, only put it into .debug_str if it is worth even in this
4027 single module. */
4028 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4029 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4030 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4031 return node->form = DW_FORM_string;
4032
4033 set_indirect_string (node);
4034
4035 return node->form;
4036 }
4037
4038 /* Find out whether the string referenced from the attribute should be
4039 output inline in DIE or out-of-line in .debug_str section. */
4040
4041 static enum dwarf_form
4042 AT_string_form (dw_attr_ref a)
4043 {
4044 gcc_assert (a && AT_class (a) == dw_val_class_str);
4045 return find_string_form (a->dw_attr_val.v.val_str);
4046 }
4047
4048 /* Add a DIE reference attribute value to a DIE. */
4049
4050 static inline void
4051 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4052 {
4053 dw_attr_node attr;
4054
4055 #ifdef ENABLE_CHECKING
4056 gcc_assert (targ_die != NULL);
4057 #else
4058 /* With LTO we can end up trying to reference something we didn't create
4059 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4060 if (targ_die == NULL)
4061 return;
4062 #endif
4063
4064 attr.dw_attr = attr_kind;
4065 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4066 attr.dw_attr_val.val_entry = NULL;
4067 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4068 attr.dw_attr_val.v.val_die_ref.external = 0;
4069 add_dwarf_attr (die, &attr);
4070 }
4071
4072 /* Change DIE reference REF to point to NEW_DIE instead. */
4073
4074 static inline void
4075 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4076 {
4077 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4078 ref->dw_attr_val.v.val_die_ref.die = new_die;
4079 ref->dw_attr_val.v.val_die_ref.external = 0;
4080 }
4081
4082 /* Add an AT_specification attribute to a DIE, and also make the back
4083 pointer from the specification to the definition. */
4084
4085 static inline void
4086 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4087 {
4088 add_AT_die_ref (die, DW_AT_specification, targ_die);
4089 gcc_assert (!targ_die->die_definition);
4090 targ_die->die_definition = die;
4091 }
4092
4093 static inline dw_die_ref
4094 AT_ref (dw_attr_ref a)
4095 {
4096 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4097 return a->dw_attr_val.v.val_die_ref.die;
4098 }
4099
4100 static inline int
4101 AT_ref_external (dw_attr_ref a)
4102 {
4103 if (a && AT_class (a) == dw_val_class_die_ref)
4104 return a->dw_attr_val.v.val_die_ref.external;
4105
4106 return 0;
4107 }
4108
4109 static inline void
4110 set_AT_ref_external (dw_attr_ref a, int i)
4111 {
4112 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4113 a->dw_attr_val.v.val_die_ref.external = i;
4114 }
4115
4116 /* Add an FDE reference attribute value to a DIE. */
4117
4118 static inline void
4119 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4120 {
4121 dw_attr_node attr;
4122
4123 attr.dw_attr = attr_kind;
4124 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4125 attr.dw_attr_val.val_entry = NULL;
4126 attr.dw_attr_val.v.val_fde_index = targ_fde;
4127 add_dwarf_attr (die, &attr);
4128 }
4129
4130 /* Add a location description attribute value to a DIE. */
4131
4132 static inline void
4133 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4134 {
4135 dw_attr_node attr;
4136
4137 attr.dw_attr = attr_kind;
4138 attr.dw_attr_val.val_class = dw_val_class_loc;
4139 attr.dw_attr_val.val_entry = NULL;
4140 attr.dw_attr_val.v.val_loc = loc;
4141 add_dwarf_attr (die, &attr);
4142 }
4143
4144 static inline dw_loc_descr_ref
4145 AT_loc (dw_attr_ref a)
4146 {
4147 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4148 return a->dw_attr_val.v.val_loc;
4149 }
4150
4151 static inline void
4152 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4153 {
4154 dw_attr_node attr;
4155
4156 attr.dw_attr = attr_kind;
4157 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4158 attr.dw_attr_val.val_entry = NULL;
4159 attr.dw_attr_val.v.val_loc_list = loc_list;
4160 add_dwarf_attr (die, &attr);
4161 have_location_lists = true;
4162 }
4163
4164 static inline dw_loc_list_ref
4165 AT_loc_list (dw_attr_ref a)
4166 {
4167 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4168 return a->dw_attr_val.v.val_loc_list;
4169 }
4170
4171 static inline dw_loc_list_ref *
4172 AT_loc_list_ptr (dw_attr_ref a)
4173 {
4174 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4175 return &a->dw_attr_val.v.val_loc_list;
4176 }
4177
4178 /* Table of entries into the .debug_addr section. */
4179
4180 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4181
4182 /* Hash an address_table_entry. */
4183
4184 static hashval_t
4185 addr_table_entry_do_hash (const void *x)
4186 {
4187 const addr_table_entry *a = (const addr_table_entry *) x;
4188 switch (a->kind)
4189 {
4190 case ate_kind_rtx:
4191 return iterative_hash_rtx (a->addr.rtl, 0);
4192 case ate_kind_rtx_dtprel:
4193 return iterative_hash_rtx (a->addr.rtl, 1);
4194 case ate_kind_label:
4195 return htab_hash_string (a->addr.label);
4196 default:
4197 gcc_unreachable ();
4198 }
4199 }
4200
4201 /* Determine equality for two address_table_entries. */
4202
4203 static int
4204 addr_table_entry_eq (const void *x1, const void *x2)
4205 {
4206 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4207 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4208
4209 if (a1->kind != a2->kind)
4210 return 0;
4211 switch (a1->kind)
4212 {
4213 case ate_kind_rtx:
4214 case ate_kind_rtx_dtprel:
4215 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4216 case ate_kind_label:
4217 return strcmp (a1->addr.label, a2->addr.label) == 0;
4218 default:
4219 gcc_unreachable ();
4220 }
4221 }
4222
4223 /* Initialize an addr_table_entry. */
4224
4225 void
4226 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4227 {
4228 e->kind = kind;
4229 switch (kind)
4230 {
4231 case ate_kind_rtx:
4232 case ate_kind_rtx_dtprel:
4233 e->addr.rtl = (rtx) addr;
4234 break;
4235 case ate_kind_label:
4236 e->addr.label = (char *) addr;
4237 break;
4238 }
4239 e->refcount = 0;
4240 e->index = NO_INDEX_ASSIGNED;
4241 }
4242
4243 /* Add attr to the address table entry to the table. Defer setting an
4244 index until output time. */
4245
4246 static addr_table_entry *
4247 add_addr_table_entry (void *addr, enum ate_kind kind)
4248 {
4249 addr_table_entry *node;
4250 addr_table_entry finder;
4251 void **slot;
4252
4253 gcc_assert (dwarf_split_debug_info);
4254 if (! addr_index_table)
4255 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4256 addr_table_entry_eq, NULL);
4257 init_addr_table_entry (&finder, kind, addr);
4258 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4259
4260 if (*slot == HTAB_EMPTY_ENTRY)
4261 {
4262 node = ggc_alloc_cleared_addr_table_entry ();
4263 init_addr_table_entry (node, kind, addr);
4264 *slot = node;
4265 }
4266 else
4267 node = (addr_table_entry *) *slot;
4268
4269 node->refcount++;
4270 return node;
4271 }
4272
4273 /* Remove an entry from the addr table by decrementing its refcount.
4274 Strictly, decrementing the refcount would be enough, but the
4275 assertion that the entry is actually in the table has found
4276 bugs. */
4277
4278 static void
4279 remove_addr_table_entry (addr_table_entry *entry)
4280 {
4281 addr_table_entry *node;
4282
4283 gcc_assert (dwarf_split_debug_info && addr_index_table);
4284 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4285 /* After an index is assigned, the table is frozen. */
4286 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4287 node->refcount--;
4288 }
4289
4290 /* Given a location list, remove all addresses it refers to from the
4291 address_table. */
4292
4293 static void
4294 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4295 {
4296 for (; descr; descr = descr->dw_loc_next)
4297 if (descr->dw_loc_oprnd1.val_entry != NULL)
4298 {
4299 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4300 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4301 }
4302 }
4303
4304 /* A helper function for dwarf2out_finish called through
4305 htab_traverse. Assign an addr_table_entry its index. All entries
4306 must be collected into the table when this function is called,
4307 because the indexing code relies on htab_traverse to traverse nodes
4308 in the same order for each run. */
4309
4310 static int
4311 index_addr_table_entry (void **h, void *v)
4312 {
4313 addr_table_entry *node = (addr_table_entry *) *h;
4314 unsigned int *index = (unsigned int *) v;
4315
4316 /* Don't index unreferenced nodes. */
4317 if (node->refcount == 0)
4318 return 1;
4319
4320 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4321 node->index = *index;
4322 *index += 1;
4323
4324 return 1;
4325 }
4326
4327 /* Add an address constant attribute value to a DIE. When using
4328 dwarf_split_debug_info, address attributes in dies destined for the
4329 final executable should be direct references--setting the parameter
4330 force_direct ensures this behavior. */
4331
4332 static inline void
4333 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4334 bool force_direct)
4335 {
4336 dw_attr_node attr;
4337
4338 attr.dw_attr = attr_kind;
4339 attr.dw_attr_val.val_class = dw_val_class_addr;
4340 attr.dw_attr_val.v.val_addr = addr;
4341 if (dwarf_split_debug_info && !force_direct)
4342 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4343 else
4344 attr.dw_attr_val.val_entry = NULL;
4345 add_dwarf_attr (die, &attr);
4346 }
4347
4348 /* Get the RTX from to an address DIE attribute. */
4349
4350 static inline rtx
4351 AT_addr (dw_attr_ref a)
4352 {
4353 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4354 return a->dw_attr_val.v.val_addr;
4355 }
4356
4357 /* Add a file attribute value to a DIE. */
4358
4359 static inline void
4360 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4361 struct dwarf_file_data *fd)
4362 {
4363 dw_attr_node attr;
4364
4365 attr.dw_attr = attr_kind;
4366 attr.dw_attr_val.val_class = dw_val_class_file;
4367 attr.dw_attr_val.val_entry = NULL;
4368 attr.dw_attr_val.v.val_file = fd;
4369 add_dwarf_attr (die, &attr);
4370 }
4371
4372 /* Get the dwarf_file_data from a file DIE attribute. */
4373
4374 static inline struct dwarf_file_data *
4375 AT_file (dw_attr_ref a)
4376 {
4377 gcc_assert (a && AT_class (a) == dw_val_class_file);
4378 return a->dw_attr_val.v.val_file;
4379 }
4380
4381 /* Add a vms delta attribute value to a DIE. */
4382
4383 static inline void
4384 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4385 const char *lbl1, const char *lbl2)
4386 {
4387 dw_attr_node attr;
4388
4389 attr.dw_attr = attr_kind;
4390 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4391 attr.dw_attr_val.val_entry = NULL;
4392 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4393 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4394 add_dwarf_attr (die, &attr);
4395 }
4396
4397 /* Add a label identifier attribute value to a DIE. */
4398
4399 static inline void
4400 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4401 const char *lbl_id)
4402 {
4403 dw_attr_node attr;
4404
4405 attr.dw_attr = attr_kind;
4406 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4407 attr.dw_attr_val.val_entry = NULL;
4408 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4409 if (dwarf_split_debug_info)
4410 attr.dw_attr_val.val_entry
4411 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4412 ate_kind_label);
4413 add_dwarf_attr (die, &attr);
4414 }
4415
4416 /* Add a section offset attribute value to a DIE, an offset into the
4417 debug_line section. */
4418
4419 static inline void
4420 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4421 const char *label)
4422 {
4423 dw_attr_node attr;
4424
4425 attr.dw_attr = attr_kind;
4426 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4427 attr.dw_attr_val.val_entry = NULL;
4428 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4429 add_dwarf_attr (die, &attr);
4430 }
4431
4432 /* Add a section offset attribute value to a DIE, an offset into the
4433 debug_macinfo section. */
4434
4435 static inline void
4436 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4437 const char *label)
4438 {
4439 dw_attr_node attr;
4440
4441 attr.dw_attr = attr_kind;
4442 attr.dw_attr_val.val_class = dw_val_class_macptr;
4443 attr.dw_attr_val.val_entry = NULL;
4444 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4445 add_dwarf_attr (die, &attr);
4446 }
4447
4448 /* Add an offset attribute value to a DIE. */
4449
4450 static inline void
4451 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4452 unsigned HOST_WIDE_INT offset)
4453 {
4454 dw_attr_node attr;
4455
4456 attr.dw_attr = attr_kind;
4457 attr.dw_attr_val.val_class = dw_val_class_offset;
4458 attr.dw_attr_val.val_entry = NULL;
4459 attr.dw_attr_val.v.val_offset = offset;
4460 add_dwarf_attr (die, &attr);
4461 }
4462
4463 /* Add a range_list attribute value to a DIE. When using
4464 dwarf_split_debug_info, address attributes in dies destined for the
4465 final executable should be direct references--setting the parameter
4466 force_direct ensures this behavior. */
4467
4468 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4469 #define RELOCATED_OFFSET (NULL)
4470
4471 static void
4472 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4473 long unsigned int offset, bool force_direct)
4474 {
4475 dw_attr_node attr;
4476
4477 attr.dw_attr = attr_kind;
4478 attr.dw_attr_val.val_class = dw_val_class_range_list;
4479 /* For the range_list attribute, use val_entry to store whether the
4480 offset should follow split-debug-info or normal semantics. This
4481 value is read in output_range_list_offset. */
4482 if (dwarf_split_debug_info && !force_direct)
4483 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4484 else
4485 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4486 attr.dw_attr_val.v.val_offset = offset;
4487 add_dwarf_attr (die, &attr);
4488 }
4489
4490 /* Return the start label of a delta attribute. */
4491
4492 static inline const char *
4493 AT_vms_delta1 (dw_attr_ref a)
4494 {
4495 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4496 return a->dw_attr_val.v.val_vms_delta.lbl1;
4497 }
4498
4499 /* Return the end label of a delta attribute. */
4500
4501 static inline const char *
4502 AT_vms_delta2 (dw_attr_ref a)
4503 {
4504 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4505 return a->dw_attr_val.v.val_vms_delta.lbl2;
4506 }
4507
4508 static inline const char *
4509 AT_lbl (dw_attr_ref a)
4510 {
4511 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4512 || AT_class (a) == dw_val_class_lineptr
4513 || AT_class (a) == dw_val_class_macptr
4514 || AT_class (a) == dw_val_class_high_pc));
4515 return a->dw_attr_val.v.val_lbl_id;
4516 }
4517
4518 /* Get the attribute of type attr_kind. */
4519
4520 static dw_attr_ref
4521 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4522 {
4523 dw_attr_ref a;
4524 unsigned ix;
4525 dw_die_ref spec = NULL;
4526
4527 if (! die)
4528 return NULL;
4529
4530 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4531 if (a->dw_attr == attr_kind)
4532 return a;
4533 else if (a->dw_attr == DW_AT_specification
4534 || a->dw_attr == DW_AT_abstract_origin)
4535 spec = AT_ref (a);
4536
4537 if (spec)
4538 return get_AT (spec, attr_kind);
4539
4540 return NULL;
4541 }
4542
4543 /* Returns the parent of the declaration of DIE. */
4544
4545 static dw_die_ref
4546 get_die_parent (dw_die_ref die)
4547 {
4548 dw_die_ref t;
4549
4550 if (!die)
4551 return NULL;
4552
4553 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4554 || (t = get_AT_ref (die, DW_AT_specification)))
4555 die = t;
4556
4557 return die->die_parent;
4558 }
4559
4560 /* Return the "low pc" attribute value, typically associated with a subprogram
4561 DIE. Return null if the "low pc" attribute is either not present, or if it
4562 cannot be represented as an assembler label identifier. */
4563
4564 static inline const char *
4565 get_AT_low_pc (dw_die_ref die)
4566 {
4567 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4568
4569 return a ? AT_lbl (a) : NULL;
4570 }
4571
4572 /* Return the "high pc" attribute value, typically associated with a subprogram
4573 DIE. Return null if the "high pc" attribute is either not present, or if it
4574 cannot be represented as an assembler label identifier. */
4575
4576 static inline const char *
4577 get_AT_hi_pc (dw_die_ref die)
4578 {
4579 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4580
4581 return a ? AT_lbl (a) : NULL;
4582 }
4583
4584 /* Return the value of the string attribute designated by ATTR_KIND, or
4585 NULL if it is not present. */
4586
4587 static inline const char *
4588 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4589 {
4590 dw_attr_ref a = get_AT (die, attr_kind);
4591
4592 return a ? AT_string (a) : NULL;
4593 }
4594
4595 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4596 if it is not present. */
4597
4598 static inline int
4599 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4600 {
4601 dw_attr_ref a = get_AT (die, attr_kind);
4602
4603 return a ? AT_flag (a) : 0;
4604 }
4605
4606 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4607 if it is not present. */
4608
4609 static inline unsigned
4610 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4611 {
4612 dw_attr_ref a = get_AT (die, attr_kind);
4613
4614 return a ? AT_unsigned (a) : 0;
4615 }
4616
4617 static inline dw_die_ref
4618 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4619 {
4620 dw_attr_ref a = get_AT (die, attr_kind);
4621
4622 return a ? AT_ref (a) : NULL;
4623 }
4624
4625 static inline struct dwarf_file_data *
4626 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4627 {
4628 dw_attr_ref a = get_AT (die, attr_kind);
4629
4630 return a ? AT_file (a) : NULL;
4631 }
4632
4633 /* Return TRUE if the language is C++. */
4634
4635 static inline bool
4636 is_cxx (void)
4637 {
4638 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4639
4640 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4641 }
4642
4643 /* Return TRUE if the language is Java. */
4644
4645 static inline bool
4646 is_java (void)
4647 {
4648 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4649
4650 return lang == DW_LANG_Java;
4651 }
4652
4653 /* Return TRUE if the language is Fortran. */
4654
4655 static inline bool
4656 is_fortran (void)
4657 {
4658 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4659
4660 return (lang == DW_LANG_Fortran77
4661 || lang == DW_LANG_Fortran90
4662 || lang == DW_LANG_Fortran95);
4663 }
4664
4665 /* Return TRUE if the language is Ada. */
4666
4667 static inline bool
4668 is_ada (void)
4669 {
4670 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4671
4672 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4673 }
4674
4675 /* Remove the specified attribute if present. */
4676
4677 static void
4678 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4679 {
4680 dw_attr_ref a;
4681 unsigned ix;
4682
4683 if (! die)
4684 return;
4685
4686 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4687 if (a->dw_attr == attr_kind)
4688 {
4689 if (AT_class (a) == dw_val_class_str)
4690 if (a->dw_attr_val.v.val_str->refcount)
4691 a->dw_attr_val.v.val_str->refcount--;
4692
4693 /* vec::ordered_remove should help reduce the number of abbrevs
4694 that are needed. */
4695 die->die_attr->ordered_remove (ix);
4696 return;
4697 }
4698 }
4699
4700 /* Remove CHILD from its parent. PREV must have the property that
4701 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4702
4703 static void
4704 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4705 {
4706 gcc_assert (child->die_parent == prev->die_parent);
4707 gcc_assert (prev->die_sib == child);
4708 if (prev == child)
4709 {
4710 gcc_assert (child->die_parent->die_child == child);
4711 prev = NULL;
4712 }
4713 else
4714 prev->die_sib = child->die_sib;
4715 if (child->die_parent->die_child == child)
4716 child->die_parent->die_child = prev;
4717 }
4718
4719 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4720 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4721
4722 static void
4723 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4724 {
4725 dw_die_ref parent = old_child->die_parent;
4726
4727 gcc_assert (parent == prev->die_parent);
4728 gcc_assert (prev->die_sib == old_child);
4729
4730 new_child->die_parent = parent;
4731 if (prev == old_child)
4732 {
4733 gcc_assert (parent->die_child == old_child);
4734 new_child->die_sib = new_child;
4735 }
4736 else
4737 {
4738 prev->die_sib = new_child;
4739 new_child->die_sib = old_child->die_sib;
4740 }
4741 if (old_child->die_parent->die_child == old_child)
4742 old_child->die_parent->die_child = new_child;
4743 }
4744
4745 /* Move all children from OLD_PARENT to NEW_PARENT. */
4746
4747 static void
4748 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4749 {
4750 dw_die_ref c;
4751 new_parent->die_child = old_parent->die_child;
4752 old_parent->die_child = NULL;
4753 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4754 }
4755
4756 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4757 matches TAG. */
4758
4759 static void
4760 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4761 {
4762 dw_die_ref c;
4763
4764 c = die->die_child;
4765 if (c) do {
4766 dw_die_ref prev = c;
4767 c = c->die_sib;
4768 while (c->die_tag == tag)
4769 {
4770 remove_child_with_prev (c, prev);
4771 /* Might have removed every child. */
4772 if (c == c->die_sib)
4773 return;
4774 c = c->die_sib;
4775 }
4776 } while (c != die->die_child);
4777 }
4778
4779 /* Add a CHILD_DIE as the last child of DIE. */
4780
4781 static void
4782 add_child_die (dw_die_ref die, dw_die_ref child_die)
4783 {
4784 /* FIXME this should probably be an assert. */
4785 if (! die || ! child_die)
4786 return;
4787 gcc_assert (die != child_die);
4788
4789 child_die->die_parent = die;
4790 if (die->die_child)
4791 {
4792 child_die->die_sib = die->die_child->die_sib;
4793 die->die_child->die_sib = child_die;
4794 }
4795 else
4796 child_die->die_sib = child_die;
4797 die->die_child = child_die;
4798 }
4799
4800 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4801 is the specification, to the end of PARENT's list of children.
4802 This is done by removing and re-adding it. */
4803
4804 static void
4805 splice_child_die (dw_die_ref parent, dw_die_ref child)
4806 {
4807 dw_die_ref p;
4808
4809 /* We want the declaration DIE from inside the class, not the
4810 specification DIE at toplevel. */
4811 if (child->die_parent != parent)
4812 {
4813 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4814
4815 if (tmp)
4816 child = tmp;
4817 }
4818
4819 gcc_assert (child->die_parent == parent
4820 || (child->die_parent
4821 == get_AT_ref (parent, DW_AT_specification)));
4822
4823 for (p = child->die_parent->die_child; ; p = p->die_sib)
4824 if (p->die_sib == child)
4825 {
4826 remove_child_with_prev (child, p);
4827 break;
4828 }
4829
4830 add_child_die (parent, child);
4831 }
4832
4833 /* Return a pointer to a newly created DIE node. */
4834
4835 static inline dw_die_ref
4836 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4837 {
4838 dw_die_ref die = ggc_alloc_cleared_die_node ();
4839
4840 die->die_tag = tag_value;
4841
4842 if (parent_die != NULL)
4843 add_child_die (parent_die, die);
4844 else
4845 {
4846 limbo_die_node *limbo_node;
4847
4848 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4849 limbo_node->die = die;
4850 limbo_node->created_for = t;
4851 limbo_node->next = limbo_die_list;
4852 limbo_die_list = limbo_node;
4853 }
4854
4855 return die;
4856 }
4857
4858 /* Return the DIE associated with the given type specifier. */
4859
4860 static inline dw_die_ref
4861 lookup_type_die (tree type)
4862 {
4863 return TYPE_SYMTAB_DIE (type);
4864 }
4865
4866 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4867 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4868 anonymous type instead the one of the naming typedef. */
4869
4870 static inline dw_die_ref
4871 strip_naming_typedef (tree type, dw_die_ref type_die)
4872 {
4873 if (type
4874 && TREE_CODE (type) == RECORD_TYPE
4875 && type_die
4876 && type_die->die_tag == DW_TAG_typedef
4877 && is_naming_typedef_decl (TYPE_NAME (type)))
4878 type_die = get_AT_ref (type_die, DW_AT_type);
4879 return type_die;
4880 }
4881
4882 /* Like lookup_type_die, but if type is an anonymous type named by a
4883 typedef[1], return the DIE of the anonymous type instead the one of
4884 the naming typedef. This is because in gen_typedef_die, we did
4885 equate the anonymous struct named by the typedef with the DIE of
4886 the naming typedef. So by default, lookup_type_die on an anonymous
4887 struct yields the DIE of the naming typedef.
4888
4889 [1]: Read the comment of is_naming_typedef_decl to learn about what
4890 a naming typedef is. */
4891
4892 static inline dw_die_ref
4893 lookup_type_die_strip_naming_typedef (tree type)
4894 {
4895 dw_die_ref die = lookup_type_die (type);
4896 return strip_naming_typedef (type, die);
4897 }
4898
4899 /* Equate a DIE to a given type specifier. */
4900
4901 static inline void
4902 equate_type_number_to_die (tree type, dw_die_ref type_die)
4903 {
4904 TYPE_SYMTAB_DIE (type) = type_die;
4905 }
4906
4907 /* Returns a hash value for X (which really is a die_struct). */
4908
4909 static hashval_t
4910 decl_die_table_hash (const void *x)
4911 {
4912 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4913 }
4914
4915 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4916
4917 static int
4918 decl_die_table_eq (const void *x, const void *y)
4919 {
4920 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4921 }
4922
4923 /* Return the DIE associated with a given declaration. */
4924
4925 static inline dw_die_ref
4926 lookup_decl_die (tree decl)
4927 {
4928 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4929 }
4930
4931 /* Returns a hash value for X (which really is a var_loc_list). */
4932
4933 static hashval_t
4934 decl_loc_table_hash (const void *x)
4935 {
4936 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4937 }
4938
4939 /* Return nonzero if decl_id of var_loc_list X is the same as
4940 UID of decl *Y. */
4941
4942 static int
4943 decl_loc_table_eq (const void *x, const void *y)
4944 {
4945 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4946 }
4947
4948 /* Return the var_loc list associated with a given declaration. */
4949
4950 static inline var_loc_list *
4951 lookup_decl_loc (const_tree decl)
4952 {
4953 if (!decl_loc_table)
4954 return NULL;
4955 return (var_loc_list *)
4956 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4957 }
4958
4959 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4960
4961 static hashval_t
4962 cached_dw_loc_list_table_hash (const void *x)
4963 {
4964 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4965 }
4966
4967 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4968 UID of decl *Y. */
4969
4970 static int
4971 cached_dw_loc_list_table_eq (const void *x, const void *y)
4972 {
4973 return (((const cached_dw_loc_list *) x)->decl_id
4974 == DECL_UID ((const_tree) y));
4975 }
4976
4977 /* Equate a DIE to a particular declaration. */
4978
4979 static void
4980 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4981 {
4982 unsigned int decl_id = DECL_UID (decl);
4983 void **slot;
4984
4985 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4986 *slot = decl_die;
4987 decl_die->decl_id = decl_id;
4988 }
4989
4990 /* Return how many bits covers PIECE EXPR_LIST. */
4991
4992 static int
4993 decl_piece_bitsize (rtx piece)
4994 {
4995 int ret = (int) GET_MODE (piece);
4996 if (ret)
4997 return ret;
4998 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4999 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5000 return INTVAL (XEXP (XEXP (piece, 0), 0));
5001 }
5002
5003 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5004
5005 static rtx *
5006 decl_piece_varloc_ptr (rtx piece)
5007 {
5008 if ((int) GET_MODE (piece))
5009 return &XEXP (piece, 0);
5010 else
5011 return &XEXP (XEXP (piece, 0), 1);
5012 }
5013
5014 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5015 Next is the chain of following piece nodes. */
5016
5017 static rtx
5018 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5019 {
5020 if (bitsize <= (int) MAX_MACHINE_MODE)
5021 return alloc_EXPR_LIST (bitsize, loc_note, next);
5022 else
5023 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5024 GEN_INT (bitsize),
5025 loc_note), next);
5026 }
5027
5028 /* Return rtx that should be stored into loc field for
5029 LOC_NOTE and BITPOS/BITSIZE. */
5030
5031 static rtx
5032 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5033 HOST_WIDE_INT bitsize)
5034 {
5035 if (bitsize != -1)
5036 {
5037 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5038 if (bitpos != 0)
5039 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5040 }
5041 return loc_note;
5042 }
5043
5044 /* This function either modifies location piece list *DEST in
5045 place (if SRC and INNER is NULL), or copies location piece list
5046 *SRC to *DEST while modifying it. Location BITPOS is modified
5047 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5048 not copied and if needed some padding around it is added.
5049 When modifying in place, DEST should point to EXPR_LIST where
5050 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5051 to the start of the whole list and INNER points to the EXPR_LIST
5052 where earlier pieces cover PIECE_BITPOS bits. */
5053
5054 static void
5055 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5056 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5057 HOST_WIDE_INT bitsize, rtx loc_note)
5058 {
5059 int diff;
5060 bool copy = inner != NULL;
5061
5062 if (copy)
5063 {
5064 /* First copy all nodes preceding the current bitpos. */
5065 while (src != inner)
5066 {
5067 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5068 decl_piece_bitsize (*src), NULL_RTX);
5069 dest = &XEXP (*dest, 1);
5070 src = &XEXP (*src, 1);
5071 }
5072 }
5073 /* Add padding if needed. */
5074 if (bitpos != piece_bitpos)
5075 {
5076 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5077 copy ? NULL_RTX : *dest);
5078 dest = &XEXP (*dest, 1);
5079 }
5080 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5081 {
5082 gcc_assert (!copy);
5083 /* A piece with correct bitpos and bitsize already exist,
5084 just update the location for it and return. */
5085 *decl_piece_varloc_ptr (*dest) = loc_note;
5086 return;
5087 }
5088 /* Add the piece that changed. */
5089 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5090 dest = &XEXP (*dest, 1);
5091 /* Skip over pieces that overlap it. */
5092 diff = bitpos - piece_bitpos + bitsize;
5093 if (!copy)
5094 src = dest;
5095 while (diff > 0 && *src)
5096 {
5097 rtx piece = *src;
5098 diff -= decl_piece_bitsize (piece);
5099 if (copy)
5100 src = &XEXP (piece, 1);
5101 else
5102 {
5103 *src = XEXP (piece, 1);
5104 free_EXPR_LIST_node (piece);
5105 }
5106 }
5107 /* Add padding if needed. */
5108 if (diff < 0 && *src)
5109 {
5110 if (!copy)
5111 dest = src;
5112 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5113 dest = &XEXP (*dest, 1);
5114 }
5115 if (!copy)
5116 return;
5117 /* Finally copy all nodes following it. */
5118 while (*src)
5119 {
5120 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5121 decl_piece_bitsize (*src), NULL_RTX);
5122 dest = &XEXP (*dest, 1);
5123 src = &XEXP (*src, 1);
5124 }
5125 }
5126
5127 /* Add a variable location node to the linked list for DECL. */
5128
5129 static struct var_loc_node *
5130 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5131 {
5132 unsigned int decl_id;
5133 var_loc_list *temp;
5134 void **slot;
5135 struct var_loc_node *loc = NULL;
5136 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5137
5138 if (TREE_CODE (decl) == VAR_DECL
5139 && DECL_HAS_DEBUG_EXPR_P (decl))
5140 {
5141 tree realdecl = DECL_DEBUG_EXPR (decl);
5142 if (handled_component_p (realdecl)
5143 || (TREE_CODE (realdecl) == MEM_REF
5144 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5145 {
5146 HOST_WIDE_INT maxsize;
5147 tree innerdecl;
5148 innerdecl
5149 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5150 if (!DECL_P (innerdecl)
5151 || DECL_IGNORED_P (innerdecl)
5152 || TREE_STATIC (innerdecl)
5153 || bitsize <= 0
5154 || bitpos + bitsize > 256
5155 || bitsize != maxsize)
5156 return NULL;
5157 decl = innerdecl;
5158 }
5159 }
5160
5161 decl_id = DECL_UID (decl);
5162 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5163 if (*slot == NULL)
5164 {
5165 temp = ggc_alloc_cleared_var_loc_list ();
5166 temp->decl_id = decl_id;
5167 *slot = temp;
5168 }
5169 else
5170 temp = (var_loc_list *) *slot;
5171
5172 /* For PARM_DECLs try to keep around the original incoming value,
5173 even if that means we'll emit a zero-range .debug_loc entry. */
5174 if (temp->last
5175 && temp->first == temp->last
5176 && TREE_CODE (decl) == PARM_DECL
5177 && NOTE_P (temp->first->loc)
5178 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5179 && DECL_INCOMING_RTL (decl)
5180 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5181 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5182 == GET_CODE (DECL_INCOMING_RTL (decl))
5183 && prev_real_insn (temp->first->loc) == NULL_RTX
5184 && (bitsize != -1
5185 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5186 NOTE_VAR_LOCATION_LOC (loc_note))
5187 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5188 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5189 {
5190 loc = ggc_alloc_cleared_var_loc_node ();
5191 temp->first->next = loc;
5192 temp->last = loc;
5193 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5194 }
5195 else if (temp->last)
5196 {
5197 struct var_loc_node *last = temp->last, *unused = NULL;
5198 rtx *piece_loc = NULL, last_loc_note;
5199 int piece_bitpos = 0;
5200 if (last->next)
5201 {
5202 last = last->next;
5203 gcc_assert (last->next == NULL);
5204 }
5205 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5206 {
5207 piece_loc = &last->loc;
5208 do
5209 {
5210 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5211 if (piece_bitpos + cur_bitsize > bitpos)
5212 break;
5213 piece_bitpos += cur_bitsize;
5214 piece_loc = &XEXP (*piece_loc, 1);
5215 }
5216 while (*piece_loc);
5217 }
5218 /* TEMP->LAST here is either pointer to the last but one or
5219 last element in the chained list, LAST is pointer to the
5220 last element. */
5221 if (label && strcmp (last->label, label) == 0)
5222 {
5223 /* For SRA optimized variables if there weren't any real
5224 insns since last note, just modify the last node. */
5225 if (piece_loc != NULL)
5226 {
5227 adjust_piece_list (piece_loc, NULL, NULL,
5228 bitpos, piece_bitpos, bitsize, loc_note);
5229 return NULL;
5230 }
5231 /* If the last note doesn't cover any instructions, remove it. */
5232 if (temp->last != last)
5233 {
5234 temp->last->next = NULL;
5235 unused = last;
5236 last = temp->last;
5237 gcc_assert (strcmp (last->label, label) != 0);
5238 }
5239 else
5240 {
5241 gcc_assert (temp->first == temp->last
5242 || (temp->first->next == temp->last
5243 && TREE_CODE (decl) == PARM_DECL));
5244 memset (temp->last, '\0', sizeof (*temp->last));
5245 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5246 return temp->last;
5247 }
5248 }
5249 if (bitsize == -1 && NOTE_P (last->loc))
5250 last_loc_note = last->loc;
5251 else if (piece_loc != NULL
5252 && *piece_loc != NULL_RTX
5253 && piece_bitpos == bitpos
5254 && decl_piece_bitsize (*piece_loc) == bitsize)
5255 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5256 else
5257 last_loc_note = NULL_RTX;
5258 /* If the current location is the same as the end of the list,
5259 and either both or neither of the locations is uninitialized,
5260 we have nothing to do. */
5261 if (last_loc_note == NULL_RTX
5262 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5263 NOTE_VAR_LOCATION_LOC (loc_note)))
5264 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5265 != NOTE_VAR_LOCATION_STATUS (loc_note))
5266 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5267 == VAR_INIT_STATUS_UNINITIALIZED)
5268 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5269 == VAR_INIT_STATUS_UNINITIALIZED))))
5270 {
5271 /* Add LOC to the end of list and update LAST. If the last
5272 element of the list has been removed above, reuse its
5273 memory for the new node, otherwise allocate a new one. */
5274 if (unused)
5275 {
5276 loc = unused;
5277 memset (loc, '\0', sizeof (*loc));
5278 }
5279 else
5280 loc = ggc_alloc_cleared_var_loc_node ();
5281 if (bitsize == -1 || piece_loc == NULL)
5282 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5283 else
5284 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5285 bitpos, piece_bitpos, bitsize, loc_note);
5286 last->next = loc;
5287 /* Ensure TEMP->LAST will point either to the new last but one
5288 element of the chain, or to the last element in it. */
5289 if (last != temp->last)
5290 temp->last = last;
5291 }
5292 else if (unused)
5293 ggc_free (unused);
5294 }
5295 else
5296 {
5297 loc = ggc_alloc_cleared_var_loc_node ();
5298 temp->first = loc;
5299 temp->last = loc;
5300 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5301 }
5302 return loc;
5303 }
5304 \f
5305 /* Keep track of the number of spaces used to indent the
5306 output of the debugging routines that print the structure of
5307 the DIE internal representation. */
5308 static int print_indent;
5309
5310 /* Indent the line the number of spaces given by print_indent. */
5311
5312 static inline void
5313 print_spaces (FILE *outfile)
5314 {
5315 fprintf (outfile, "%*s", print_indent, "");
5316 }
5317
5318 /* Print a type signature in hex. */
5319
5320 static inline void
5321 print_signature (FILE *outfile, char *sig)
5322 {
5323 int i;
5324
5325 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5326 fprintf (outfile, "%02x", sig[i] & 0xff);
5327 }
5328
5329 /* Print the information associated with a given DIE, and its children.
5330 This routine is a debugging aid only. */
5331
5332 static void
5333 print_die (dw_die_ref die, FILE *outfile)
5334 {
5335 dw_attr_ref a;
5336 dw_die_ref c;
5337 unsigned ix;
5338
5339 print_spaces (outfile);
5340 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5341 die->die_offset, dwarf_tag_name (die->die_tag),
5342 (void*) die);
5343 print_spaces (outfile);
5344 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5345 fprintf (outfile, " offset: %ld", die->die_offset);
5346 fprintf (outfile, " mark: %d\n", die->die_mark);
5347
5348 if (die->comdat_type_p)
5349 {
5350 print_spaces (outfile);
5351 fprintf (outfile, " signature: ");
5352 print_signature (outfile, die->die_id.die_type_node->signature);
5353 fprintf (outfile, "\n");
5354 }
5355
5356 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5357 {
5358 print_spaces (outfile);
5359 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5360
5361 switch (AT_class (a))
5362 {
5363 case dw_val_class_addr:
5364 fprintf (outfile, "address");
5365 break;
5366 case dw_val_class_offset:
5367 fprintf (outfile, "offset");
5368 break;
5369 case dw_val_class_loc:
5370 fprintf (outfile, "location descriptor");
5371 break;
5372 case dw_val_class_loc_list:
5373 fprintf (outfile, "location list -> label:%s",
5374 AT_loc_list (a)->ll_symbol);
5375 break;
5376 case dw_val_class_range_list:
5377 fprintf (outfile, "range list");
5378 break;
5379 case dw_val_class_const:
5380 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5381 break;
5382 case dw_val_class_unsigned_const:
5383 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5384 break;
5385 case dw_val_class_const_double:
5386 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5387 HOST_WIDE_INT_PRINT_UNSIGNED")",
5388 a->dw_attr_val.v.val_double.high,
5389 a->dw_attr_val.v.val_double.low);
5390 break;
5391 case dw_val_class_wide_int:
5392 {
5393 int i = a->dw_attr_val.v.val_wide->get_len ();
5394 fprintf (outfile, "constant (");
5395 gcc_assert (i > 0);
5396 if (a->dw_attr_val.v.val_wide->elt (i) == 0)
5397 fprintf (outfile, "0x");
5398 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5399 a->dw_attr_val.v.val_wide->elt (--i));
5400 while (-- i >= 0)
5401 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5402 a->dw_attr_val.v.val_wide->elt (i));
5403 fprintf (outfile, ")");
5404 break;
5405 }
5406 case dw_val_class_vec:
5407 fprintf (outfile, "floating-point or vector constant");
5408 break;
5409 case dw_val_class_flag:
5410 fprintf (outfile, "%u", AT_flag (a));
5411 break;
5412 case dw_val_class_die_ref:
5413 if (AT_ref (a) != NULL)
5414 {
5415 if (AT_ref (a)->comdat_type_p)
5416 {
5417 fprintf (outfile, "die -> signature: ");
5418 print_signature (outfile,
5419 AT_ref (a)->die_id.die_type_node->signature);
5420 }
5421 else if (AT_ref (a)->die_id.die_symbol)
5422 fprintf (outfile, "die -> label: %s",
5423 AT_ref (a)->die_id.die_symbol);
5424 else
5425 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5426 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5427 }
5428 else
5429 fprintf (outfile, "die -> <null>");
5430 break;
5431 case dw_val_class_vms_delta:
5432 fprintf (outfile, "delta: @slotcount(%s-%s)",
5433 AT_vms_delta2 (a), AT_vms_delta1 (a));
5434 break;
5435 case dw_val_class_lbl_id:
5436 case dw_val_class_lineptr:
5437 case dw_val_class_macptr:
5438 case dw_val_class_high_pc:
5439 fprintf (outfile, "label: %s", AT_lbl (a));
5440 break;
5441 case dw_val_class_str:
5442 if (AT_string (a) != NULL)
5443 fprintf (outfile, "\"%s\"", AT_string (a));
5444 else
5445 fprintf (outfile, "<null>");
5446 break;
5447 case dw_val_class_file:
5448 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5449 AT_file (a)->emitted_number);
5450 break;
5451 case dw_val_class_data8:
5452 {
5453 int i;
5454
5455 for (i = 0; i < 8; i++)
5456 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5457 break;
5458 }
5459 default:
5460 break;
5461 }
5462
5463 fprintf (outfile, "\n");
5464 }
5465
5466 if (die->die_child != NULL)
5467 {
5468 print_indent += 4;
5469 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5470 print_indent -= 4;
5471 }
5472 if (print_indent == 0)
5473 fprintf (outfile, "\n");
5474 }
5475
5476 /* Print the information collected for a given DIE. */
5477
5478 DEBUG_FUNCTION void
5479 debug_dwarf_die (dw_die_ref die)
5480 {
5481 print_die (die, stderr);
5482 }
5483
5484 DEBUG_FUNCTION void
5485 debug (die_struct &ref)
5486 {
5487 print_die (&ref, stderr);
5488 }
5489
5490 DEBUG_FUNCTION void
5491 debug (die_struct *ptr)
5492 {
5493 if (ptr)
5494 debug (*ptr);
5495 else
5496 fprintf (stderr, "<nil>\n");
5497 }
5498
5499
5500 /* Print all DWARF information collected for the compilation unit.
5501 This routine is a debugging aid only. */
5502
5503 DEBUG_FUNCTION void
5504 debug_dwarf (void)
5505 {
5506 print_indent = 0;
5507 print_die (comp_unit_die (), stderr);
5508 }
5509 \f
5510 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5511 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5512 DIE that marks the start of the DIEs for this include file. */
5513
5514 static dw_die_ref
5515 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5516 {
5517 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5518 dw_die_ref new_unit = gen_compile_unit_die (filename);
5519
5520 new_unit->die_sib = old_unit;
5521 return new_unit;
5522 }
5523
5524 /* Close an include-file CU and reopen the enclosing one. */
5525
5526 static dw_die_ref
5527 pop_compile_unit (dw_die_ref old_unit)
5528 {
5529 dw_die_ref new_unit = old_unit->die_sib;
5530
5531 old_unit->die_sib = NULL;
5532 return new_unit;
5533 }
5534
5535 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5536 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5537 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5538
5539 /* Calculate the checksum of a location expression. */
5540
5541 static inline void
5542 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5543 {
5544 int tem;
5545 hashval_t hash = 0;
5546
5547 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5548 CHECKSUM (tem);
5549 hash = hash_loc_operands (loc, hash);
5550 CHECKSUM (hash);
5551 }
5552
5553 /* Calculate the checksum of an attribute. */
5554
5555 static void
5556 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5557 {
5558 dw_loc_descr_ref loc;
5559 rtx r;
5560
5561 CHECKSUM (at->dw_attr);
5562
5563 /* We don't care that this was compiled with a different compiler
5564 snapshot; if the output is the same, that's what matters. */
5565 if (at->dw_attr == DW_AT_producer)
5566 return;
5567
5568 switch (AT_class (at))
5569 {
5570 case dw_val_class_const:
5571 CHECKSUM (at->dw_attr_val.v.val_int);
5572 break;
5573 case dw_val_class_unsigned_const:
5574 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5575 break;
5576 case dw_val_class_const_double:
5577 CHECKSUM (at->dw_attr_val.v.val_double);
5578 break;
5579 case dw_val_class_wide_int:
5580 CHECKSUM (*at->dw_attr_val.v.val_wide);
5581 break;
5582 case dw_val_class_vec:
5583 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5584 (at->dw_attr_val.v.val_vec.length
5585 * at->dw_attr_val.v.val_vec.elt_size));
5586 break;
5587 case dw_val_class_flag:
5588 CHECKSUM (at->dw_attr_val.v.val_flag);
5589 break;
5590 case dw_val_class_str:
5591 CHECKSUM_STRING (AT_string (at));
5592 break;
5593
5594 case dw_val_class_addr:
5595 r = AT_addr (at);
5596 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5597 CHECKSUM_STRING (XSTR (r, 0));
5598 break;
5599
5600 case dw_val_class_offset:
5601 CHECKSUM (at->dw_attr_val.v.val_offset);
5602 break;
5603
5604 case dw_val_class_loc:
5605 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5606 loc_checksum (loc, ctx);
5607 break;
5608
5609 case dw_val_class_die_ref:
5610 die_checksum (AT_ref (at), ctx, mark);
5611 break;
5612
5613 case dw_val_class_fde_ref:
5614 case dw_val_class_vms_delta:
5615 case dw_val_class_lbl_id:
5616 case dw_val_class_lineptr:
5617 case dw_val_class_macptr:
5618 case dw_val_class_high_pc:
5619 break;
5620
5621 case dw_val_class_file:
5622 CHECKSUM_STRING (AT_file (at)->filename);
5623 break;
5624
5625 case dw_val_class_data8:
5626 CHECKSUM (at->dw_attr_val.v.val_data8);
5627 break;
5628
5629 default:
5630 break;
5631 }
5632 }
5633
5634 /* Calculate the checksum of a DIE. */
5635
5636 static void
5637 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5638 {
5639 dw_die_ref c;
5640 dw_attr_ref a;
5641 unsigned ix;
5642
5643 /* To avoid infinite recursion. */
5644 if (die->die_mark)
5645 {
5646 CHECKSUM (die->die_mark);
5647 return;
5648 }
5649 die->die_mark = ++(*mark);
5650
5651 CHECKSUM (die->die_tag);
5652
5653 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5654 attr_checksum (a, ctx, mark);
5655
5656 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5657 }
5658
5659 #undef CHECKSUM
5660 #undef CHECKSUM_BLOCK
5661 #undef CHECKSUM_STRING
5662
5663 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5664 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5665 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5666 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5667 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5668 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5669 #define CHECKSUM_ATTR(FOO) \
5670 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5671
5672 /* Calculate the checksum of a number in signed LEB128 format. */
5673
5674 static void
5675 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5676 {
5677 unsigned char byte;
5678 bool more;
5679
5680 while (1)
5681 {
5682 byte = (value & 0x7f);
5683 value >>= 7;
5684 more = !((value == 0 && (byte & 0x40) == 0)
5685 || (value == -1 && (byte & 0x40) != 0));
5686 if (more)
5687 byte |= 0x80;
5688 CHECKSUM (byte);
5689 if (!more)
5690 break;
5691 }
5692 }
5693
5694 /* Calculate the checksum of a number in unsigned LEB128 format. */
5695
5696 static void
5697 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5698 {
5699 while (1)
5700 {
5701 unsigned char byte = (value & 0x7f);
5702 value >>= 7;
5703 if (value != 0)
5704 /* More bytes to follow. */
5705 byte |= 0x80;
5706 CHECKSUM (byte);
5707 if (value == 0)
5708 break;
5709 }
5710 }
5711
5712 /* Checksum the context of the DIE. This adds the names of any
5713 surrounding namespaces or structures to the checksum. */
5714
5715 static void
5716 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5717 {
5718 const char *name;
5719 dw_die_ref spec;
5720 int tag = die->die_tag;
5721
5722 if (tag != DW_TAG_namespace
5723 && tag != DW_TAG_structure_type
5724 && tag != DW_TAG_class_type)
5725 return;
5726
5727 name = get_AT_string (die, DW_AT_name);
5728
5729 spec = get_AT_ref (die, DW_AT_specification);
5730 if (spec != NULL)
5731 die = spec;
5732
5733 if (die->die_parent != NULL)
5734 checksum_die_context (die->die_parent, ctx);
5735
5736 CHECKSUM_ULEB128 ('C');
5737 CHECKSUM_ULEB128 (tag);
5738 if (name != NULL)
5739 CHECKSUM_STRING (name);
5740 }
5741
5742 /* Calculate the checksum of a location expression. */
5743
5744 static inline void
5745 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5746 {
5747 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5748 were emitted as a DW_FORM_sdata instead of a location expression. */
5749 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5750 {
5751 CHECKSUM_ULEB128 (DW_FORM_sdata);
5752 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5753 return;
5754 }
5755
5756 /* Otherwise, just checksum the raw location expression. */
5757 while (loc != NULL)
5758 {
5759 hashval_t hash = 0;
5760
5761 CHECKSUM_ULEB128 (loc->dtprel);
5762 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5763 hash = hash_loc_operands (loc, hash);
5764 CHECKSUM (hash);
5765 loc = loc->dw_loc_next;
5766 }
5767 }
5768
5769 /* Calculate the checksum of an attribute. */
5770
5771 static void
5772 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5773 struct md5_ctx *ctx, int *mark)
5774 {
5775 dw_loc_descr_ref loc;
5776 rtx r;
5777
5778 if (AT_class (at) == dw_val_class_die_ref)
5779 {
5780 dw_die_ref target_die = AT_ref (at);
5781
5782 /* For pointer and reference types, we checksum only the (qualified)
5783 name of the target type (if there is a name). For friend entries,
5784 we checksum only the (qualified) name of the target type or function.
5785 This allows the checksum to remain the same whether the target type
5786 is complete or not. */
5787 if ((at->dw_attr == DW_AT_type
5788 && (tag == DW_TAG_pointer_type
5789 || tag == DW_TAG_reference_type
5790 || tag == DW_TAG_rvalue_reference_type
5791 || tag == DW_TAG_ptr_to_member_type))
5792 || (at->dw_attr == DW_AT_friend
5793 && tag == DW_TAG_friend))
5794 {
5795 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5796
5797 if (name_attr != NULL)
5798 {
5799 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5800
5801 if (decl == NULL)
5802 decl = target_die;
5803 CHECKSUM_ULEB128 ('N');
5804 CHECKSUM_ULEB128 (at->dw_attr);
5805 if (decl->die_parent != NULL)
5806 checksum_die_context (decl->die_parent, ctx);
5807 CHECKSUM_ULEB128 ('E');
5808 CHECKSUM_STRING (AT_string (name_attr));
5809 return;
5810 }
5811 }
5812
5813 /* For all other references to another DIE, we check to see if the
5814 target DIE has already been visited. If it has, we emit a
5815 backward reference; if not, we descend recursively. */
5816 if (target_die->die_mark > 0)
5817 {
5818 CHECKSUM_ULEB128 ('R');
5819 CHECKSUM_ULEB128 (at->dw_attr);
5820 CHECKSUM_ULEB128 (target_die->die_mark);
5821 }
5822 else
5823 {
5824 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5825
5826 if (decl == NULL)
5827 decl = target_die;
5828 target_die->die_mark = ++(*mark);
5829 CHECKSUM_ULEB128 ('T');
5830 CHECKSUM_ULEB128 (at->dw_attr);
5831 if (decl->die_parent != NULL)
5832 checksum_die_context (decl->die_parent, ctx);
5833 die_checksum_ordered (target_die, ctx, mark);
5834 }
5835 return;
5836 }
5837
5838 CHECKSUM_ULEB128 ('A');
5839 CHECKSUM_ULEB128 (at->dw_attr);
5840
5841 switch (AT_class (at))
5842 {
5843 case dw_val_class_const:
5844 CHECKSUM_ULEB128 (DW_FORM_sdata);
5845 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5846 break;
5847
5848 case dw_val_class_unsigned_const:
5849 CHECKSUM_ULEB128 (DW_FORM_sdata);
5850 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5851 break;
5852
5853 case dw_val_class_const_double:
5854 CHECKSUM_ULEB128 (DW_FORM_block);
5855 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5856 CHECKSUM (at->dw_attr_val.v.val_double);
5857 break;
5858
5859 case dw_val_class_wide_int:
5860 CHECKSUM_ULEB128 (DW_FORM_block);
5861 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5862 CHECKSUM (*at->dw_attr_val.v.val_wide);
5863 break;
5864
5865 case dw_val_class_vec:
5866 CHECKSUM_ULEB128 (DW_FORM_block);
5867 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5868 * at->dw_attr_val.v.val_vec.elt_size);
5869 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5870 (at->dw_attr_val.v.val_vec.length
5871 * at->dw_attr_val.v.val_vec.elt_size));
5872 break;
5873
5874 case dw_val_class_flag:
5875 CHECKSUM_ULEB128 (DW_FORM_flag);
5876 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5877 break;
5878
5879 case dw_val_class_str:
5880 CHECKSUM_ULEB128 (DW_FORM_string);
5881 CHECKSUM_STRING (AT_string (at));
5882 break;
5883
5884 case dw_val_class_addr:
5885 r = AT_addr (at);
5886 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5887 CHECKSUM_ULEB128 (DW_FORM_string);
5888 CHECKSUM_STRING (XSTR (r, 0));
5889 break;
5890
5891 case dw_val_class_offset:
5892 CHECKSUM_ULEB128 (DW_FORM_sdata);
5893 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5894 break;
5895
5896 case dw_val_class_loc:
5897 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5898 loc_checksum_ordered (loc, ctx);
5899 break;
5900
5901 case dw_val_class_fde_ref:
5902 case dw_val_class_lbl_id:
5903 case dw_val_class_lineptr:
5904 case dw_val_class_macptr:
5905 case dw_val_class_high_pc:
5906 break;
5907
5908 case dw_val_class_file:
5909 CHECKSUM_ULEB128 (DW_FORM_string);
5910 CHECKSUM_STRING (AT_file (at)->filename);
5911 break;
5912
5913 case dw_val_class_data8:
5914 CHECKSUM (at->dw_attr_val.v.val_data8);
5915 break;
5916
5917 default:
5918 break;
5919 }
5920 }
5921
5922 struct checksum_attributes
5923 {
5924 dw_attr_ref at_name;
5925 dw_attr_ref at_type;
5926 dw_attr_ref at_friend;
5927 dw_attr_ref at_accessibility;
5928 dw_attr_ref at_address_class;
5929 dw_attr_ref at_allocated;
5930 dw_attr_ref at_artificial;
5931 dw_attr_ref at_associated;
5932 dw_attr_ref at_binary_scale;
5933 dw_attr_ref at_bit_offset;
5934 dw_attr_ref at_bit_size;
5935 dw_attr_ref at_bit_stride;
5936 dw_attr_ref at_byte_size;
5937 dw_attr_ref at_byte_stride;
5938 dw_attr_ref at_const_value;
5939 dw_attr_ref at_containing_type;
5940 dw_attr_ref at_count;
5941 dw_attr_ref at_data_location;
5942 dw_attr_ref at_data_member_location;
5943 dw_attr_ref at_decimal_scale;
5944 dw_attr_ref at_decimal_sign;
5945 dw_attr_ref at_default_value;
5946 dw_attr_ref at_digit_count;
5947 dw_attr_ref at_discr;
5948 dw_attr_ref at_discr_list;
5949 dw_attr_ref at_discr_value;
5950 dw_attr_ref at_encoding;
5951 dw_attr_ref at_endianity;
5952 dw_attr_ref at_explicit;
5953 dw_attr_ref at_is_optional;
5954 dw_attr_ref at_location;
5955 dw_attr_ref at_lower_bound;
5956 dw_attr_ref at_mutable;
5957 dw_attr_ref at_ordering;
5958 dw_attr_ref at_picture_string;
5959 dw_attr_ref at_prototyped;
5960 dw_attr_ref at_small;
5961 dw_attr_ref at_segment;
5962 dw_attr_ref at_string_length;
5963 dw_attr_ref at_threads_scaled;
5964 dw_attr_ref at_upper_bound;
5965 dw_attr_ref at_use_location;
5966 dw_attr_ref at_use_UTF8;
5967 dw_attr_ref at_variable_parameter;
5968 dw_attr_ref at_virtuality;
5969 dw_attr_ref at_visibility;
5970 dw_attr_ref at_vtable_elem_location;
5971 };
5972
5973 /* Collect the attributes that we will want to use for the checksum. */
5974
5975 static void
5976 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5977 {
5978 dw_attr_ref a;
5979 unsigned ix;
5980
5981 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5982 {
5983 switch (a->dw_attr)
5984 {
5985 case DW_AT_name:
5986 attrs->at_name = a;
5987 break;
5988 case DW_AT_type:
5989 attrs->at_type = a;
5990 break;
5991 case DW_AT_friend:
5992 attrs->at_friend = a;
5993 break;
5994 case DW_AT_accessibility:
5995 attrs->at_accessibility = a;
5996 break;
5997 case DW_AT_address_class:
5998 attrs->at_address_class = a;
5999 break;
6000 case DW_AT_allocated:
6001 attrs->at_allocated = a;
6002 break;
6003 case DW_AT_artificial:
6004 attrs->at_artificial = a;
6005 break;
6006 case DW_AT_associated:
6007 attrs->at_associated = a;
6008 break;
6009 case DW_AT_binary_scale:
6010 attrs->at_binary_scale = a;
6011 break;
6012 case DW_AT_bit_offset:
6013 attrs->at_bit_offset = a;
6014 break;
6015 case DW_AT_bit_size:
6016 attrs->at_bit_size = a;
6017 break;
6018 case DW_AT_bit_stride:
6019 attrs->at_bit_stride = a;
6020 break;
6021 case DW_AT_byte_size:
6022 attrs->at_byte_size = a;
6023 break;
6024 case DW_AT_byte_stride:
6025 attrs->at_byte_stride = a;
6026 break;
6027 case DW_AT_const_value:
6028 attrs->at_const_value = a;
6029 break;
6030 case DW_AT_containing_type:
6031 attrs->at_containing_type = a;
6032 break;
6033 case DW_AT_count:
6034 attrs->at_count = a;
6035 break;
6036 case DW_AT_data_location:
6037 attrs->at_data_location = a;
6038 break;
6039 case DW_AT_data_member_location:
6040 attrs->at_data_member_location = a;
6041 break;
6042 case DW_AT_decimal_scale:
6043 attrs->at_decimal_scale = a;
6044 break;
6045 case DW_AT_decimal_sign:
6046 attrs->at_decimal_sign = a;
6047 break;
6048 case DW_AT_default_value:
6049 attrs->at_default_value = a;
6050 break;
6051 case DW_AT_digit_count:
6052 attrs->at_digit_count = a;
6053 break;
6054 case DW_AT_discr:
6055 attrs->at_discr = a;
6056 break;
6057 case DW_AT_discr_list:
6058 attrs->at_discr_list = a;
6059 break;
6060 case DW_AT_discr_value:
6061 attrs->at_discr_value = a;
6062 break;
6063 case DW_AT_encoding:
6064 attrs->at_encoding = a;
6065 break;
6066 case DW_AT_endianity:
6067 attrs->at_endianity = a;
6068 break;
6069 case DW_AT_explicit:
6070 attrs->at_explicit = a;
6071 break;
6072 case DW_AT_is_optional:
6073 attrs->at_is_optional = a;
6074 break;
6075 case DW_AT_location:
6076 attrs->at_location = a;
6077 break;
6078 case DW_AT_lower_bound:
6079 attrs->at_lower_bound = a;
6080 break;
6081 case DW_AT_mutable:
6082 attrs->at_mutable = a;
6083 break;
6084 case DW_AT_ordering:
6085 attrs->at_ordering = a;
6086 break;
6087 case DW_AT_picture_string:
6088 attrs->at_picture_string = a;
6089 break;
6090 case DW_AT_prototyped:
6091 attrs->at_prototyped = a;
6092 break;
6093 case DW_AT_small:
6094 attrs->at_small = a;
6095 break;
6096 case DW_AT_segment:
6097 attrs->at_segment = a;
6098 break;
6099 case DW_AT_string_length:
6100 attrs->at_string_length = a;
6101 break;
6102 case DW_AT_threads_scaled:
6103 attrs->at_threads_scaled = a;
6104 break;
6105 case DW_AT_upper_bound:
6106 attrs->at_upper_bound = a;
6107 break;
6108 case DW_AT_use_location:
6109 attrs->at_use_location = a;
6110 break;
6111 case DW_AT_use_UTF8:
6112 attrs->at_use_UTF8 = a;
6113 break;
6114 case DW_AT_variable_parameter:
6115 attrs->at_variable_parameter = a;
6116 break;
6117 case DW_AT_virtuality:
6118 attrs->at_virtuality = a;
6119 break;
6120 case DW_AT_visibility:
6121 attrs->at_visibility = a;
6122 break;
6123 case DW_AT_vtable_elem_location:
6124 attrs->at_vtable_elem_location = a;
6125 break;
6126 default:
6127 break;
6128 }
6129 }
6130 }
6131
6132 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6133
6134 static void
6135 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6136 {
6137 dw_die_ref c;
6138 dw_die_ref decl;
6139 struct checksum_attributes attrs;
6140
6141 CHECKSUM_ULEB128 ('D');
6142 CHECKSUM_ULEB128 (die->die_tag);
6143
6144 memset (&attrs, 0, sizeof (attrs));
6145
6146 decl = get_AT_ref (die, DW_AT_specification);
6147 if (decl != NULL)
6148 collect_checksum_attributes (&attrs, decl);
6149 collect_checksum_attributes (&attrs, die);
6150
6151 CHECKSUM_ATTR (attrs.at_name);
6152 CHECKSUM_ATTR (attrs.at_accessibility);
6153 CHECKSUM_ATTR (attrs.at_address_class);
6154 CHECKSUM_ATTR (attrs.at_allocated);
6155 CHECKSUM_ATTR (attrs.at_artificial);
6156 CHECKSUM_ATTR (attrs.at_associated);
6157 CHECKSUM_ATTR (attrs.at_binary_scale);
6158 CHECKSUM_ATTR (attrs.at_bit_offset);
6159 CHECKSUM_ATTR (attrs.at_bit_size);
6160 CHECKSUM_ATTR (attrs.at_bit_stride);
6161 CHECKSUM_ATTR (attrs.at_byte_size);
6162 CHECKSUM_ATTR (attrs.at_byte_stride);
6163 CHECKSUM_ATTR (attrs.at_const_value);
6164 CHECKSUM_ATTR (attrs.at_containing_type);
6165 CHECKSUM_ATTR (attrs.at_count);
6166 CHECKSUM_ATTR (attrs.at_data_location);
6167 CHECKSUM_ATTR (attrs.at_data_member_location);
6168 CHECKSUM_ATTR (attrs.at_decimal_scale);
6169 CHECKSUM_ATTR (attrs.at_decimal_sign);
6170 CHECKSUM_ATTR (attrs.at_default_value);
6171 CHECKSUM_ATTR (attrs.at_digit_count);
6172 CHECKSUM_ATTR (attrs.at_discr);
6173 CHECKSUM_ATTR (attrs.at_discr_list);
6174 CHECKSUM_ATTR (attrs.at_discr_value);
6175 CHECKSUM_ATTR (attrs.at_encoding);
6176 CHECKSUM_ATTR (attrs.at_endianity);
6177 CHECKSUM_ATTR (attrs.at_explicit);
6178 CHECKSUM_ATTR (attrs.at_is_optional);
6179 CHECKSUM_ATTR (attrs.at_location);
6180 CHECKSUM_ATTR (attrs.at_lower_bound);
6181 CHECKSUM_ATTR (attrs.at_mutable);
6182 CHECKSUM_ATTR (attrs.at_ordering);
6183 CHECKSUM_ATTR (attrs.at_picture_string);
6184 CHECKSUM_ATTR (attrs.at_prototyped);
6185 CHECKSUM_ATTR (attrs.at_small);
6186 CHECKSUM_ATTR (attrs.at_segment);
6187 CHECKSUM_ATTR (attrs.at_string_length);
6188 CHECKSUM_ATTR (attrs.at_threads_scaled);
6189 CHECKSUM_ATTR (attrs.at_upper_bound);
6190 CHECKSUM_ATTR (attrs.at_use_location);
6191 CHECKSUM_ATTR (attrs.at_use_UTF8);
6192 CHECKSUM_ATTR (attrs.at_variable_parameter);
6193 CHECKSUM_ATTR (attrs.at_virtuality);
6194 CHECKSUM_ATTR (attrs.at_visibility);
6195 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6196 CHECKSUM_ATTR (attrs.at_type);
6197 CHECKSUM_ATTR (attrs.at_friend);
6198
6199 /* Checksum the child DIEs. */
6200 c = die->die_child;
6201 if (c) do {
6202 dw_attr_ref name_attr;
6203
6204 c = c->die_sib;
6205 name_attr = get_AT (c, DW_AT_name);
6206 if (is_template_instantiation (c))
6207 {
6208 /* Ignore instantiations of member type and function templates. */
6209 }
6210 else if (name_attr != NULL
6211 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6212 {
6213 /* Use a shallow checksum for named nested types and member
6214 functions. */
6215 CHECKSUM_ULEB128 ('S');
6216 CHECKSUM_ULEB128 (c->die_tag);
6217 CHECKSUM_STRING (AT_string (name_attr));
6218 }
6219 else
6220 {
6221 /* Use a deep checksum for other children. */
6222 /* Mark this DIE so it gets processed when unmarking. */
6223 if (c->die_mark == 0)
6224 c->die_mark = -1;
6225 die_checksum_ordered (c, ctx, mark);
6226 }
6227 } while (c != die->die_child);
6228
6229 CHECKSUM_ULEB128 (0);
6230 }
6231
6232 /* Add a type name and tag to a hash. */
6233 static void
6234 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6235 {
6236 CHECKSUM_ULEB128 (tag);
6237 CHECKSUM_STRING (name);
6238 }
6239
6240 #undef CHECKSUM
6241 #undef CHECKSUM_STRING
6242 #undef CHECKSUM_ATTR
6243 #undef CHECKSUM_LEB128
6244 #undef CHECKSUM_ULEB128
6245
6246 /* Generate the type signature for DIE. This is computed by generating an
6247 MD5 checksum over the DIE's tag, its relevant attributes, and its
6248 children. Attributes that are references to other DIEs are processed
6249 by recursion, using the MARK field to prevent infinite recursion.
6250 If the DIE is nested inside a namespace or another type, we also
6251 need to include that context in the signature. The lower 64 bits
6252 of the resulting MD5 checksum comprise the signature. */
6253
6254 static void
6255 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6256 {
6257 int mark;
6258 const char *name;
6259 unsigned char checksum[16];
6260 struct md5_ctx ctx;
6261 dw_die_ref decl;
6262 dw_die_ref parent;
6263
6264 name = get_AT_string (die, DW_AT_name);
6265 decl = get_AT_ref (die, DW_AT_specification);
6266 parent = get_die_parent (die);
6267
6268 /* First, compute a signature for just the type name (and its surrounding
6269 context, if any. This is stored in the type unit DIE for link-time
6270 ODR (one-definition rule) checking. */
6271
6272 if (is_cxx () && name != NULL)
6273 {
6274 md5_init_ctx (&ctx);
6275
6276 /* Checksum the names of surrounding namespaces and structures. */
6277 if (parent != NULL)
6278 checksum_die_context (parent, &ctx);
6279
6280 /* Checksum the current DIE. */
6281 die_odr_checksum (die->die_tag, name, &ctx);
6282 md5_finish_ctx (&ctx, checksum);
6283
6284 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6285 }
6286
6287 /* Next, compute the complete type signature. */
6288
6289 md5_init_ctx (&ctx);
6290 mark = 1;
6291 die->die_mark = mark;
6292
6293 /* Checksum the names of surrounding namespaces and structures. */
6294 if (parent != NULL)
6295 checksum_die_context (parent, &ctx);
6296
6297 /* Checksum the DIE and its children. */
6298 die_checksum_ordered (die, &ctx, &mark);
6299 unmark_all_dies (die);
6300 md5_finish_ctx (&ctx, checksum);
6301
6302 /* Store the signature in the type node and link the type DIE and the
6303 type node together. */
6304 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6305 DWARF_TYPE_SIGNATURE_SIZE);
6306 die->comdat_type_p = true;
6307 die->die_id.die_type_node = type_node;
6308 type_node->type_die = die;
6309
6310 /* If the DIE is a specification, link its declaration to the type node
6311 as well. */
6312 if (decl != NULL)
6313 {
6314 decl->comdat_type_p = true;
6315 decl->die_id.die_type_node = type_node;
6316 }
6317 }
6318
6319 /* Do the location expressions look same? */
6320 static inline int
6321 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6322 {
6323 return loc1->dw_loc_opc == loc2->dw_loc_opc
6324 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6325 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6326 }
6327
6328 /* Do the values look the same? */
6329 static int
6330 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6331 {
6332 dw_loc_descr_ref loc1, loc2;
6333 rtx r1, r2;
6334
6335 if (v1->val_class != v2->val_class)
6336 return 0;
6337
6338 switch (v1->val_class)
6339 {
6340 case dw_val_class_const:
6341 return v1->v.val_int == v2->v.val_int;
6342 case dw_val_class_unsigned_const:
6343 return v1->v.val_unsigned == v2->v.val_unsigned;
6344 case dw_val_class_const_double:
6345 return v1->v.val_double.high == v2->v.val_double.high
6346 && v1->v.val_double.low == v2->v.val_double.low;
6347 case dw_val_class_wide_int:
6348 return *v1->v.val_wide == *v2->v.val_wide;
6349 case dw_val_class_vec:
6350 if (v1->v.val_vec.length != v2->v.val_vec.length
6351 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6352 return 0;
6353 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6354 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6355 return 0;
6356 return 1;
6357 case dw_val_class_flag:
6358 return v1->v.val_flag == v2->v.val_flag;
6359 case dw_val_class_str:
6360 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6361
6362 case dw_val_class_addr:
6363 r1 = v1->v.val_addr;
6364 r2 = v2->v.val_addr;
6365 if (GET_CODE (r1) != GET_CODE (r2))
6366 return 0;
6367 return !rtx_equal_p (r1, r2);
6368
6369 case dw_val_class_offset:
6370 return v1->v.val_offset == v2->v.val_offset;
6371
6372 case dw_val_class_loc:
6373 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6374 loc1 && loc2;
6375 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6376 if (!same_loc_p (loc1, loc2, mark))
6377 return 0;
6378 return !loc1 && !loc2;
6379
6380 case dw_val_class_die_ref:
6381 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6382
6383 case dw_val_class_fde_ref:
6384 case dw_val_class_vms_delta:
6385 case dw_val_class_lbl_id:
6386 case dw_val_class_lineptr:
6387 case dw_val_class_macptr:
6388 case dw_val_class_high_pc:
6389 return 1;
6390
6391 case dw_val_class_file:
6392 return v1->v.val_file == v2->v.val_file;
6393
6394 case dw_val_class_data8:
6395 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6396
6397 default:
6398 return 1;
6399 }
6400 }
6401
6402 /* Do the attributes look the same? */
6403
6404 static int
6405 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6406 {
6407 if (at1->dw_attr != at2->dw_attr)
6408 return 0;
6409
6410 /* We don't care that this was compiled with a different compiler
6411 snapshot; if the output is the same, that's what matters. */
6412 if (at1->dw_attr == DW_AT_producer)
6413 return 1;
6414
6415 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6416 }
6417
6418 /* Do the dies look the same? */
6419
6420 static int
6421 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6422 {
6423 dw_die_ref c1, c2;
6424 dw_attr_ref a1;
6425 unsigned ix;
6426
6427 /* To avoid infinite recursion. */
6428 if (die1->die_mark)
6429 return die1->die_mark == die2->die_mark;
6430 die1->die_mark = die2->die_mark = ++(*mark);
6431
6432 if (die1->die_tag != die2->die_tag)
6433 return 0;
6434
6435 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6436 return 0;
6437
6438 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6439 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6440 return 0;
6441
6442 c1 = die1->die_child;
6443 c2 = die2->die_child;
6444 if (! c1)
6445 {
6446 if (c2)
6447 return 0;
6448 }
6449 else
6450 for (;;)
6451 {
6452 if (!same_die_p (c1, c2, mark))
6453 return 0;
6454 c1 = c1->die_sib;
6455 c2 = c2->die_sib;
6456 if (c1 == die1->die_child)
6457 {
6458 if (c2 == die2->die_child)
6459 break;
6460 else
6461 return 0;
6462 }
6463 }
6464
6465 return 1;
6466 }
6467
6468 /* Do the dies look the same? Wrapper around same_die_p. */
6469
6470 static int
6471 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6472 {
6473 int mark = 0;
6474 int ret = same_die_p (die1, die2, &mark);
6475
6476 unmark_all_dies (die1);
6477 unmark_all_dies (die2);
6478
6479 return ret;
6480 }
6481
6482 /* The prefix to attach to symbols on DIEs in the current comdat debug
6483 info section. */
6484 static const char *comdat_symbol_id;
6485
6486 /* The index of the current symbol within the current comdat CU. */
6487 static unsigned int comdat_symbol_number;
6488
6489 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6490 children, and set comdat_symbol_id accordingly. */
6491
6492 static void
6493 compute_section_prefix (dw_die_ref unit_die)
6494 {
6495 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6496 const char *base = die_name ? lbasename (die_name) : "anonymous";
6497 char *name = XALLOCAVEC (char, strlen (base) + 64);
6498 char *p;
6499 int i, mark;
6500 unsigned char checksum[16];
6501 struct md5_ctx ctx;
6502
6503 /* Compute the checksum of the DIE, then append part of it as hex digits to
6504 the name filename of the unit. */
6505
6506 md5_init_ctx (&ctx);
6507 mark = 0;
6508 die_checksum (unit_die, &ctx, &mark);
6509 unmark_all_dies (unit_die);
6510 md5_finish_ctx (&ctx, checksum);
6511
6512 sprintf (name, "%s.", base);
6513 clean_symbol_name (name);
6514
6515 p = name + strlen (name);
6516 for (i = 0; i < 4; i++)
6517 {
6518 sprintf (p, "%.2x", checksum[i]);
6519 p += 2;
6520 }
6521
6522 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6523 comdat_symbol_number = 0;
6524 }
6525
6526 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6527
6528 static int
6529 is_type_die (dw_die_ref die)
6530 {
6531 switch (die->die_tag)
6532 {
6533 case DW_TAG_array_type:
6534 case DW_TAG_class_type:
6535 case DW_TAG_interface_type:
6536 case DW_TAG_enumeration_type:
6537 case DW_TAG_pointer_type:
6538 case DW_TAG_reference_type:
6539 case DW_TAG_rvalue_reference_type:
6540 case DW_TAG_string_type:
6541 case DW_TAG_structure_type:
6542 case DW_TAG_subroutine_type:
6543 case DW_TAG_union_type:
6544 case DW_TAG_ptr_to_member_type:
6545 case DW_TAG_set_type:
6546 case DW_TAG_subrange_type:
6547 case DW_TAG_base_type:
6548 case DW_TAG_const_type:
6549 case DW_TAG_file_type:
6550 case DW_TAG_packed_type:
6551 case DW_TAG_volatile_type:
6552 case DW_TAG_typedef:
6553 return 1;
6554 default:
6555 return 0;
6556 }
6557 }
6558
6559 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6560 Basically, we want to choose the bits that are likely to be shared between
6561 compilations (types) and leave out the bits that are specific to individual
6562 compilations (functions). */
6563
6564 static int
6565 is_comdat_die (dw_die_ref c)
6566 {
6567 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6568 we do for stabs. The advantage is a greater likelihood of sharing between
6569 objects that don't include headers in the same order (and therefore would
6570 put the base types in a different comdat). jason 8/28/00 */
6571
6572 if (c->die_tag == DW_TAG_base_type)
6573 return 0;
6574
6575 if (c->die_tag == DW_TAG_pointer_type
6576 || c->die_tag == DW_TAG_reference_type
6577 || c->die_tag == DW_TAG_rvalue_reference_type
6578 || c->die_tag == DW_TAG_const_type
6579 || c->die_tag == DW_TAG_volatile_type)
6580 {
6581 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6582
6583 return t ? is_comdat_die (t) : 0;
6584 }
6585
6586 return is_type_die (c);
6587 }
6588
6589 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6590 compilation unit. */
6591
6592 static int
6593 is_symbol_die (dw_die_ref c)
6594 {
6595 return (is_type_die (c)
6596 || is_declaration_die (c)
6597 || c->die_tag == DW_TAG_namespace
6598 || c->die_tag == DW_TAG_module);
6599 }
6600
6601 /* Returns true iff C is a compile-unit DIE. */
6602
6603 static inline bool
6604 is_cu_die (dw_die_ref c)
6605 {
6606 return c && c->die_tag == DW_TAG_compile_unit;
6607 }
6608
6609 /* Returns true iff C is a unit DIE of some sort. */
6610
6611 static inline bool
6612 is_unit_die (dw_die_ref c)
6613 {
6614 return c && (c->die_tag == DW_TAG_compile_unit
6615 || c->die_tag == DW_TAG_partial_unit
6616 || c->die_tag == DW_TAG_type_unit);
6617 }
6618
6619 /* Returns true iff C is a namespace DIE. */
6620
6621 static inline bool
6622 is_namespace_die (dw_die_ref c)
6623 {
6624 return c && c->die_tag == DW_TAG_namespace;
6625 }
6626
6627 /* Returns true iff C is a class or structure DIE. */
6628
6629 static inline bool
6630 is_class_die (dw_die_ref c)
6631 {
6632 return c && (c->die_tag == DW_TAG_class_type
6633 || c->die_tag == DW_TAG_structure_type);
6634 }
6635
6636 /* Return non-zero if this DIE is a template parameter. */
6637
6638 static inline bool
6639 is_template_parameter (dw_die_ref die)
6640 {
6641 switch (die->die_tag)
6642 {
6643 case DW_TAG_template_type_param:
6644 case DW_TAG_template_value_param:
6645 case DW_TAG_GNU_template_template_param:
6646 case DW_TAG_GNU_template_parameter_pack:
6647 return true;
6648 default:
6649 return false;
6650 }
6651 }
6652
6653 /* Return non-zero if this DIE represents a template instantiation. */
6654
6655 static inline bool
6656 is_template_instantiation (dw_die_ref die)
6657 {
6658 dw_die_ref c;
6659
6660 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6661 return false;
6662 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6663 return false;
6664 }
6665
6666 static char *
6667 gen_internal_sym (const char *prefix)
6668 {
6669 char buf[256];
6670
6671 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6672 return xstrdup (buf);
6673 }
6674
6675 /* Assign symbols to all worthy DIEs under DIE. */
6676
6677 static void
6678 assign_symbol_names (dw_die_ref die)
6679 {
6680 dw_die_ref c;
6681
6682 if (is_symbol_die (die) && !die->comdat_type_p)
6683 {
6684 if (comdat_symbol_id)
6685 {
6686 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6687
6688 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6689 comdat_symbol_id, comdat_symbol_number++);
6690 die->die_id.die_symbol = xstrdup (p);
6691 }
6692 else
6693 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6694 }
6695
6696 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6697 }
6698
6699 struct cu_hash_table_entry
6700 {
6701 dw_die_ref cu;
6702 unsigned min_comdat_num, max_comdat_num;
6703 struct cu_hash_table_entry *next;
6704 };
6705
6706 /* Helpers to manipulate hash table of CUs. */
6707
6708 struct cu_hash_table_entry_hasher
6709 {
6710 typedef cu_hash_table_entry value_type;
6711 typedef die_struct compare_type;
6712 static inline hashval_t hash (const value_type *);
6713 static inline bool equal (const value_type *, const compare_type *);
6714 static inline void remove (value_type *);
6715 };
6716
6717 inline hashval_t
6718 cu_hash_table_entry_hasher::hash (const value_type *entry)
6719 {
6720 return htab_hash_string (entry->cu->die_id.die_symbol);
6721 }
6722
6723 inline bool
6724 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6725 const compare_type *entry2)
6726 {
6727 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6728 }
6729
6730 inline void
6731 cu_hash_table_entry_hasher::remove (value_type *entry)
6732 {
6733 struct cu_hash_table_entry *next;
6734
6735 while (entry)
6736 {
6737 next = entry->next;
6738 free (entry);
6739 entry = next;
6740 }
6741 }
6742
6743 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6744
6745 /* Check whether we have already seen this CU and set up SYM_NUM
6746 accordingly. */
6747 static int
6748 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6749 {
6750 struct cu_hash_table_entry dummy;
6751 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6752
6753 dummy.max_comdat_num = 0;
6754
6755 slot = htable.find_slot_with_hash (cu,
6756 htab_hash_string (cu->die_id.die_symbol),
6757 INSERT);
6758 entry = *slot;
6759
6760 for (; entry; last = entry, entry = entry->next)
6761 {
6762 if (same_die_p_wrap (cu, entry->cu))
6763 break;
6764 }
6765
6766 if (entry)
6767 {
6768 *sym_num = entry->min_comdat_num;
6769 return 1;
6770 }
6771
6772 entry = XCNEW (struct cu_hash_table_entry);
6773 entry->cu = cu;
6774 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6775 entry->next = *slot;
6776 *slot = entry;
6777
6778 return 0;
6779 }
6780
6781 /* Record SYM_NUM to record of CU in HTABLE. */
6782 static void
6783 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6784 unsigned int sym_num)
6785 {
6786 struct cu_hash_table_entry **slot, *entry;
6787
6788 slot = htable.find_slot_with_hash (cu,
6789 htab_hash_string (cu->die_id.die_symbol),
6790 NO_INSERT);
6791 entry = *slot;
6792
6793 entry->max_comdat_num = sym_num;
6794 }
6795
6796 /* Traverse the DIE (which is always comp_unit_die), and set up
6797 additional compilation units for each of the include files we see
6798 bracketed by BINCL/EINCL. */
6799
6800 static void
6801 break_out_includes (dw_die_ref die)
6802 {
6803 dw_die_ref c;
6804 dw_die_ref unit = NULL;
6805 limbo_die_node *node, **pnode;
6806 cu_hash_type cu_hash_table;
6807
6808 c = die->die_child;
6809 if (c) do {
6810 dw_die_ref prev = c;
6811 c = c->die_sib;
6812 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6813 || (unit && is_comdat_die (c)))
6814 {
6815 dw_die_ref next = c->die_sib;
6816
6817 /* This DIE is for a secondary CU; remove it from the main one. */
6818 remove_child_with_prev (c, prev);
6819
6820 if (c->die_tag == DW_TAG_GNU_BINCL)
6821 unit = push_new_compile_unit (unit, c);
6822 else if (c->die_tag == DW_TAG_GNU_EINCL)
6823 unit = pop_compile_unit (unit);
6824 else
6825 add_child_die (unit, c);
6826 c = next;
6827 if (c == die->die_child)
6828 break;
6829 }
6830 } while (c != die->die_child);
6831
6832 #if 0
6833 /* We can only use this in debugging, since the frontend doesn't check
6834 to make sure that we leave every include file we enter. */
6835 gcc_assert (!unit);
6836 #endif
6837
6838 assign_symbol_names (die);
6839 cu_hash_table.create (10);
6840 for (node = limbo_die_list, pnode = &limbo_die_list;
6841 node;
6842 node = node->next)
6843 {
6844 int is_dupl;
6845
6846 compute_section_prefix (node->die);
6847 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6848 &comdat_symbol_number);
6849 assign_symbol_names (node->die);
6850 if (is_dupl)
6851 *pnode = node->next;
6852 else
6853 {
6854 pnode = &node->next;
6855 record_comdat_symbol_number (node->die, cu_hash_table,
6856 comdat_symbol_number);
6857 }
6858 }
6859 cu_hash_table.dispose ();
6860 }
6861
6862 /* Return non-zero if this DIE is a declaration. */
6863
6864 static int
6865 is_declaration_die (dw_die_ref die)
6866 {
6867 dw_attr_ref a;
6868 unsigned ix;
6869
6870 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6871 if (a->dw_attr == DW_AT_declaration)
6872 return 1;
6873
6874 return 0;
6875 }
6876
6877 /* Return non-zero if this DIE is nested inside a subprogram. */
6878
6879 static int
6880 is_nested_in_subprogram (dw_die_ref die)
6881 {
6882 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6883
6884 if (decl == NULL)
6885 decl = die;
6886 return local_scope_p (decl);
6887 }
6888
6889 /* Return non-zero if this DIE contains a defining declaration of a
6890 subprogram. */
6891
6892 static int
6893 contains_subprogram_definition (dw_die_ref die)
6894 {
6895 dw_die_ref c;
6896
6897 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6898 return 1;
6899 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6900 return 0;
6901 }
6902
6903 /* Return non-zero if this is a type DIE that should be moved to a
6904 COMDAT .debug_types section. */
6905
6906 static int
6907 should_move_die_to_comdat (dw_die_ref die)
6908 {
6909 switch (die->die_tag)
6910 {
6911 case DW_TAG_class_type:
6912 case DW_TAG_structure_type:
6913 case DW_TAG_enumeration_type:
6914 case DW_TAG_union_type:
6915 /* Don't move declarations, inlined instances, or types nested in a
6916 subprogram. */
6917 if (is_declaration_die (die)
6918 || get_AT (die, DW_AT_abstract_origin)
6919 || is_nested_in_subprogram (die))
6920 return 0;
6921 /* A type definition should never contain a subprogram definition. */
6922 gcc_assert (!contains_subprogram_definition (die));
6923 return 1;
6924 case DW_TAG_array_type:
6925 case DW_TAG_interface_type:
6926 case DW_TAG_pointer_type:
6927 case DW_TAG_reference_type:
6928 case DW_TAG_rvalue_reference_type:
6929 case DW_TAG_string_type:
6930 case DW_TAG_subroutine_type:
6931 case DW_TAG_ptr_to_member_type:
6932 case DW_TAG_set_type:
6933 case DW_TAG_subrange_type:
6934 case DW_TAG_base_type:
6935 case DW_TAG_const_type:
6936 case DW_TAG_file_type:
6937 case DW_TAG_packed_type:
6938 case DW_TAG_volatile_type:
6939 case DW_TAG_typedef:
6940 default:
6941 return 0;
6942 }
6943 }
6944
6945 /* Make a clone of DIE. */
6946
6947 static dw_die_ref
6948 clone_die (dw_die_ref die)
6949 {
6950 dw_die_ref clone;
6951 dw_attr_ref a;
6952 unsigned ix;
6953
6954 clone = ggc_alloc_cleared_die_node ();
6955 clone->die_tag = die->die_tag;
6956
6957 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6958 add_dwarf_attr (clone, a);
6959
6960 return clone;
6961 }
6962
6963 /* Make a clone of the tree rooted at DIE. */
6964
6965 static dw_die_ref
6966 clone_tree (dw_die_ref die)
6967 {
6968 dw_die_ref c;
6969 dw_die_ref clone = clone_die (die);
6970
6971 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6972
6973 return clone;
6974 }
6975
6976 /* Make a clone of DIE as a declaration. */
6977
6978 static dw_die_ref
6979 clone_as_declaration (dw_die_ref die)
6980 {
6981 dw_die_ref clone;
6982 dw_die_ref decl;
6983 dw_attr_ref a;
6984 unsigned ix;
6985
6986 /* If the DIE is already a declaration, just clone it. */
6987 if (is_declaration_die (die))
6988 return clone_die (die);
6989
6990 /* If the DIE is a specification, just clone its declaration DIE. */
6991 decl = get_AT_ref (die, DW_AT_specification);
6992 if (decl != NULL)
6993 {
6994 clone = clone_die (decl);
6995 if (die->comdat_type_p)
6996 add_AT_die_ref (clone, DW_AT_signature, die);
6997 return clone;
6998 }
6999
7000 clone = ggc_alloc_cleared_die_node ();
7001 clone->die_tag = die->die_tag;
7002
7003 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7004 {
7005 /* We don't want to copy over all attributes.
7006 For example we don't want DW_AT_byte_size because otherwise we will no
7007 longer have a declaration and GDB will treat it as a definition. */
7008
7009 switch (a->dw_attr)
7010 {
7011 case DW_AT_artificial:
7012 case DW_AT_containing_type:
7013 case DW_AT_external:
7014 case DW_AT_name:
7015 case DW_AT_type:
7016 case DW_AT_virtuality:
7017 case DW_AT_linkage_name:
7018 case DW_AT_MIPS_linkage_name:
7019 add_dwarf_attr (clone, a);
7020 break;
7021 case DW_AT_byte_size:
7022 default:
7023 break;
7024 }
7025 }
7026
7027 if (die->comdat_type_p)
7028 add_AT_die_ref (clone, DW_AT_signature, die);
7029
7030 add_AT_flag (clone, DW_AT_declaration, 1);
7031 return clone;
7032 }
7033
7034
7035 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7036
7037 struct decl_table_entry
7038 {
7039 dw_die_ref orig;
7040 dw_die_ref copy;
7041 };
7042
7043 /* Helpers to manipulate hash table of copied declarations. */
7044
7045 /* Hashtable helpers. */
7046
7047 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7048 {
7049 typedef decl_table_entry value_type;
7050 typedef die_struct compare_type;
7051 static inline hashval_t hash (const value_type *);
7052 static inline bool equal (const value_type *, const compare_type *);
7053 };
7054
7055 inline hashval_t
7056 decl_table_entry_hasher::hash (const value_type *entry)
7057 {
7058 return htab_hash_pointer (entry->orig);
7059 }
7060
7061 inline bool
7062 decl_table_entry_hasher::equal (const value_type *entry1,
7063 const compare_type *entry2)
7064 {
7065 return entry1->orig == entry2;
7066 }
7067
7068 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
7069
7070 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7071 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7072 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7073 to check if the ancestor has already been copied into UNIT. */
7074
7075 static dw_die_ref
7076 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7077 {
7078 dw_die_ref parent = die->die_parent;
7079 dw_die_ref new_parent = unit;
7080 dw_die_ref copy;
7081 decl_table_entry **slot = NULL;
7082 struct decl_table_entry *entry = NULL;
7083
7084 if (decl_table.is_created ())
7085 {
7086 /* Check if the entry has already been copied to UNIT. */
7087 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
7088 INSERT);
7089 if (*slot != HTAB_EMPTY_ENTRY)
7090 {
7091 entry = *slot;
7092 return entry->copy;
7093 }
7094
7095 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7096 entry = XCNEW (struct decl_table_entry);
7097 entry->orig = die;
7098 entry->copy = NULL;
7099 *slot = entry;
7100 }
7101
7102 if (parent != NULL)
7103 {
7104 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7105 if (spec != NULL)
7106 parent = spec;
7107 if (!is_unit_die (parent))
7108 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7109 }
7110
7111 copy = clone_as_declaration (die);
7112 add_child_die (new_parent, copy);
7113
7114 if (decl_table.is_created ())
7115 {
7116 /* Record the pointer to the copy. */
7117 entry->copy = copy;
7118 }
7119
7120 return copy;
7121 }
7122 /* Copy the declaration context to the new type unit DIE. This includes
7123 any surrounding namespace or type declarations. If the DIE has an
7124 AT_specification attribute, it also includes attributes and children
7125 attached to the specification, and returns a pointer to the original
7126 parent of the declaration DIE. Returns NULL otherwise. */
7127
7128 static dw_die_ref
7129 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7130 {
7131 dw_die_ref decl;
7132 dw_die_ref new_decl;
7133 dw_die_ref orig_parent = NULL;
7134
7135 decl = get_AT_ref (die, DW_AT_specification);
7136 if (decl == NULL)
7137 decl = die;
7138 else
7139 {
7140 unsigned ix;
7141 dw_die_ref c;
7142 dw_attr_ref a;
7143
7144 /* The original DIE will be changed to a declaration, and must
7145 be moved to be a child of the original declaration DIE. */
7146 orig_parent = decl->die_parent;
7147
7148 /* Copy the type node pointer from the new DIE to the original
7149 declaration DIE so we can forward references later. */
7150 decl->comdat_type_p = true;
7151 decl->die_id.die_type_node = die->die_id.die_type_node;
7152
7153 remove_AT (die, DW_AT_specification);
7154
7155 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7156 {
7157 if (a->dw_attr != DW_AT_name
7158 && a->dw_attr != DW_AT_declaration
7159 && a->dw_attr != DW_AT_external)
7160 add_dwarf_attr (die, a);
7161 }
7162
7163 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7164 }
7165
7166 if (decl->die_parent != NULL
7167 && !is_unit_die (decl->die_parent))
7168 {
7169 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7170 if (new_decl != NULL)
7171 {
7172 remove_AT (new_decl, DW_AT_signature);
7173 add_AT_specification (die, new_decl);
7174 }
7175 }
7176
7177 return orig_parent;
7178 }
7179
7180 /* Generate the skeleton ancestor tree for the given NODE, then clone
7181 the DIE and add the clone into the tree. */
7182
7183 static void
7184 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7185 {
7186 if (node->new_die != NULL)
7187 return;
7188
7189 node->new_die = clone_as_declaration (node->old_die);
7190
7191 if (node->parent != NULL)
7192 {
7193 generate_skeleton_ancestor_tree (node->parent);
7194 add_child_die (node->parent->new_die, node->new_die);
7195 }
7196 }
7197
7198 /* Generate a skeleton tree of DIEs containing any declarations that are
7199 found in the original tree. We traverse the tree looking for declaration
7200 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7201
7202 static void
7203 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7204 {
7205 skeleton_chain_node node;
7206 dw_die_ref c;
7207 dw_die_ref first;
7208 dw_die_ref prev = NULL;
7209 dw_die_ref next = NULL;
7210
7211 node.parent = parent;
7212
7213 first = c = parent->old_die->die_child;
7214 if (c)
7215 next = c->die_sib;
7216 if (c) do {
7217 if (prev == NULL || prev->die_sib == c)
7218 prev = c;
7219 c = next;
7220 next = (c == first ? NULL : c->die_sib);
7221 node.old_die = c;
7222 node.new_die = NULL;
7223 if (is_declaration_die (c))
7224 {
7225 if (is_template_instantiation (c))
7226 {
7227 /* Instantiated templates do not need to be cloned into the
7228 type unit. Just move the DIE and its children back to
7229 the skeleton tree (in the main CU). */
7230 remove_child_with_prev (c, prev);
7231 add_child_die (parent->new_die, c);
7232 c = prev;
7233 }
7234 else
7235 {
7236 /* Clone the existing DIE, move the original to the skeleton
7237 tree (which is in the main CU), and put the clone, with
7238 all the original's children, where the original came from
7239 (which is about to be moved to the type unit). */
7240 dw_die_ref clone = clone_die (c);
7241 move_all_children (c, clone);
7242
7243 replace_child (c, clone, prev);
7244 generate_skeleton_ancestor_tree (parent);
7245 add_child_die (parent->new_die, c);
7246 node.new_die = c;
7247 c = clone;
7248 }
7249 }
7250 generate_skeleton_bottom_up (&node);
7251 } while (next != NULL);
7252 }
7253
7254 /* Wrapper function for generate_skeleton_bottom_up. */
7255
7256 static dw_die_ref
7257 generate_skeleton (dw_die_ref die)
7258 {
7259 skeleton_chain_node node;
7260
7261 node.old_die = die;
7262 node.new_die = NULL;
7263 node.parent = NULL;
7264
7265 /* If this type definition is nested inside another type,
7266 and is not an instantiation of a template, always leave
7267 at least a declaration in its place. */
7268 if (die->die_parent != NULL
7269 && is_type_die (die->die_parent)
7270 && !is_template_instantiation (die))
7271 node.new_die = clone_as_declaration (die);
7272
7273 generate_skeleton_bottom_up (&node);
7274 return node.new_die;
7275 }
7276
7277 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7278 declaration. The original DIE is moved to a new compile unit so that
7279 existing references to it follow it to the new location. If any of the
7280 original DIE's descendants is a declaration, we need to replace the
7281 original DIE with a skeleton tree and move the declarations back into the
7282 skeleton tree. */
7283
7284 static dw_die_ref
7285 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7286 dw_die_ref prev)
7287 {
7288 dw_die_ref skeleton, orig_parent;
7289
7290 /* Copy the declaration context to the type unit DIE. If the returned
7291 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7292 that DIE. */
7293 orig_parent = copy_declaration_context (unit, child);
7294
7295 skeleton = generate_skeleton (child);
7296 if (skeleton == NULL)
7297 remove_child_with_prev (child, prev);
7298 else
7299 {
7300 skeleton->comdat_type_p = true;
7301 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7302
7303 /* If the original DIE was a specification, we need to put
7304 the skeleton under the parent DIE of the declaration.
7305 This leaves the original declaration in the tree, but
7306 it will be pruned later since there are no longer any
7307 references to it. */
7308 if (orig_parent != NULL)
7309 {
7310 remove_child_with_prev (child, prev);
7311 add_child_die (orig_parent, skeleton);
7312 }
7313 else
7314 replace_child (child, skeleton, prev);
7315 }
7316
7317 return skeleton;
7318 }
7319
7320 /* Traverse the DIE and set up additional .debug_types sections for each
7321 type worthy of being placed in a COMDAT section. */
7322
7323 static void
7324 break_out_comdat_types (dw_die_ref die)
7325 {
7326 dw_die_ref c;
7327 dw_die_ref first;
7328 dw_die_ref prev = NULL;
7329 dw_die_ref next = NULL;
7330 dw_die_ref unit = NULL;
7331
7332 first = c = die->die_child;
7333 if (c)
7334 next = c->die_sib;
7335 if (c) do {
7336 if (prev == NULL || prev->die_sib == c)
7337 prev = c;
7338 c = next;
7339 next = (c == first ? NULL : c->die_sib);
7340 if (should_move_die_to_comdat (c))
7341 {
7342 dw_die_ref replacement;
7343 comdat_type_node_ref type_node;
7344
7345 /* Break out nested types into their own type units. */
7346 break_out_comdat_types (c);
7347
7348 /* Create a new type unit DIE as the root for the new tree, and
7349 add it to the list of comdat types. */
7350 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7351 add_AT_unsigned (unit, DW_AT_language,
7352 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7353 type_node = ggc_alloc_cleared_comdat_type_node ();
7354 type_node->root_die = unit;
7355 type_node->next = comdat_type_list;
7356 comdat_type_list = type_node;
7357
7358 /* Generate the type signature. */
7359 generate_type_signature (c, type_node);
7360
7361 /* Copy the declaration context, attributes, and children of the
7362 declaration into the new type unit DIE, then remove this DIE
7363 from the main CU (or replace it with a skeleton if necessary). */
7364 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7365 type_node->skeleton_die = replacement;
7366
7367 /* Add the DIE to the new compunit. */
7368 add_child_die (unit, c);
7369
7370 if (replacement != NULL)
7371 c = replacement;
7372 }
7373 else if (c->die_tag == DW_TAG_namespace
7374 || c->die_tag == DW_TAG_class_type
7375 || c->die_tag == DW_TAG_structure_type
7376 || c->die_tag == DW_TAG_union_type)
7377 {
7378 /* Look for nested types that can be broken out. */
7379 break_out_comdat_types (c);
7380 }
7381 } while (next != NULL);
7382 }
7383
7384 /* Like clone_tree, but additionally enter all the children into
7385 the hash table decl_table. */
7386
7387 static dw_die_ref
7388 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7389 {
7390 dw_die_ref c;
7391 dw_die_ref clone = clone_die (die);
7392 struct decl_table_entry *entry;
7393 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7394 htab_hash_pointer (die), INSERT);
7395 /* Assert that DIE isn't in the hash table yet. If it would be there
7396 before, the ancestors would be necessarily there as well, therefore
7397 clone_tree_hash wouldn't be called. */
7398 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7399 entry = XCNEW (struct decl_table_entry);
7400 entry->orig = die;
7401 entry->copy = clone;
7402 *slot = entry;
7403
7404 FOR_EACH_CHILD (die, c,
7405 add_child_die (clone, clone_tree_hash (c, decl_table)));
7406
7407 return clone;
7408 }
7409
7410 /* Walk the DIE and its children, looking for references to incomplete
7411 or trivial types that are unmarked (i.e., that are not in the current
7412 type_unit). */
7413
7414 static void
7415 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7416 {
7417 dw_die_ref c;
7418 dw_attr_ref a;
7419 unsigned ix;
7420
7421 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7422 {
7423 if (AT_class (a) == dw_val_class_die_ref)
7424 {
7425 dw_die_ref targ = AT_ref (a);
7426 decl_table_entry **slot;
7427 struct decl_table_entry *entry;
7428
7429 if (targ->die_mark != 0 || targ->comdat_type_p)
7430 continue;
7431
7432 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7433 INSERT);
7434
7435 if (*slot != HTAB_EMPTY_ENTRY)
7436 {
7437 /* TARG has already been copied, so we just need to
7438 modify the reference to point to the copy. */
7439 entry = *slot;
7440 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7441 }
7442 else
7443 {
7444 dw_die_ref parent = unit;
7445 dw_die_ref copy = clone_die (targ);
7446
7447 /* Record in DECL_TABLE that TARG has been copied.
7448 Need to do this now, before the recursive call,
7449 because DECL_TABLE may be expanded and SLOT
7450 would no longer be a valid pointer. */
7451 entry = XCNEW (struct decl_table_entry);
7452 entry->orig = targ;
7453 entry->copy = copy;
7454 *slot = entry;
7455
7456 FOR_EACH_CHILD (targ, c,
7457 add_child_die (copy,
7458 clone_tree_hash (c, decl_table)));
7459
7460 /* Make sure the cloned tree is marked as part of the
7461 type unit. */
7462 mark_dies (copy);
7463
7464 /* If TARG has surrounding context, copy its ancestor tree
7465 into the new type unit. */
7466 if (targ->die_parent != NULL
7467 && !is_unit_die (targ->die_parent))
7468 parent = copy_ancestor_tree (unit, targ->die_parent,
7469 decl_table);
7470
7471 add_child_die (parent, copy);
7472 a->dw_attr_val.v.val_die_ref.die = copy;
7473
7474 /* Make sure the newly-copied DIE is walked. If it was
7475 installed in a previously-added context, it won't
7476 get visited otherwise. */
7477 if (parent != unit)
7478 {
7479 /* Find the highest point of the newly-added tree,
7480 mark each node along the way, and walk from there. */
7481 parent->die_mark = 1;
7482 while (parent->die_parent
7483 && parent->die_parent->die_mark == 0)
7484 {
7485 parent = parent->die_parent;
7486 parent->die_mark = 1;
7487 }
7488 copy_decls_walk (unit, parent, decl_table);
7489 }
7490 }
7491 }
7492 }
7493
7494 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7495 }
7496
7497 /* Copy declarations for "unworthy" types into the new comdat section.
7498 Incomplete types, modified types, and certain other types aren't broken
7499 out into comdat sections of their own, so they don't have a signature,
7500 and we need to copy the declaration into the same section so that we
7501 don't have an external reference. */
7502
7503 static void
7504 copy_decls_for_unworthy_types (dw_die_ref unit)
7505 {
7506 decl_hash_type decl_table;
7507
7508 mark_dies (unit);
7509 decl_table.create (10);
7510 copy_decls_walk (unit, unit, decl_table);
7511 decl_table.dispose ();
7512 unmark_dies (unit);
7513 }
7514
7515 /* Traverse the DIE and add a sibling attribute if it may have the
7516 effect of speeding up access to siblings. To save some space,
7517 avoid generating sibling attributes for DIE's without children. */
7518
7519 static void
7520 add_sibling_attributes (dw_die_ref die)
7521 {
7522 dw_die_ref c;
7523
7524 if (! die->die_child)
7525 return;
7526
7527 if (die->die_parent && die != die->die_parent->die_child)
7528 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7529
7530 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7531 }
7532
7533 /* Output all location lists for the DIE and its children. */
7534
7535 static void
7536 output_location_lists (dw_die_ref die)
7537 {
7538 dw_die_ref c;
7539 dw_attr_ref a;
7540 unsigned ix;
7541
7542 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7543 if (AT_class (a) == dw_val_class_loc_list)
7544 output_loc_list (AT_loc_list (a));
7545
7546 FOR_EACH_CHILD (die, c, output_location_lists (c));
7547 }
7548
7549 /* We want to limit the number of external references, because they are
7550 larger than local references: a relocation takes multiple words, and
7551 even a sig8 reference is always eight bytes, whereas a local reference
7552 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7553 So if we encounter multiple external references to the same type DIE, we
7554 make a local typedef stub for it and redirect all references there.
7555
7556 This is the element of the hash table for keeping track of these
7557 references. */
7558
7559 struct external_ref
7560 {
7561 dw_die_ref type;
7562 dw_die_ref stub;
7563 unsigned n_refs;
7564 };
7565
7566 /* Hashtable helpers. */
7567
7568 struct external_ref_hasher : typed_free_remove <external_ref>
7569 {
7570 typedef external_ref value_type;
7571 typedef external_ref compare_type;
7572 static inline hashval_t hash (const value_type *);
7573 static inline bool equal (const value_type *, const compare_type *);
7574 };
7575
7576 inline hashval_t
7577 external_ref_hasher::hash (const value_type *r)
7578 {
7579 dw_die_ref die = r->type;
7580 hashval_t h = 0;
7581
7582 /* We can't use the address of the DIE for hashing, because
7583 that will make the order of the stub DIEs non-deterministic. */
7584 if (! die->comdat_type_p)
7585 /* We have a symbol; use it to compute a hash. */
7586 h = htab_hash_string (die->die_id.die_symbol);
7587 else
7588 {
7589 /* We have a type signature; use a subset of the bits as the hash.
7590 The 8-byte signature is at least as large as hashval_t. */
7591 comdat_type_node_ref type_node = die->die_id.die_type_node;
7592 memcpy (&h, type_node->signature, sizeof (h));
7593 }
7594 return h;
7595 }
7596
7597 inline bool
7598 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7599 {
7600 return r1->type == r2->type;
7601 }
7602
7603 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7604
7605 /* Return a pointer to the external_ref for references to DIE. */
7606
7607 static struct external_ref *
7608 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7609 {
7610 struct external_ref ref, *ref_p;
7611 external_ref **slot;
7612
7613 ref.type = die;
7614 slot = map.find_slot (&ref, INSERT);
7615 if (*slot != HTAB_EMPTY_ENTRY)
7616 return *slot;
7617
7618 ref_p = XCNEW (struct external_ref);
7619 ref_p->type = die;
7620 *slot = ref_p;
7621 return ref_p;
7622 }
7623
7624 /* Subroutine of optimize_external_refs, below.
7625
7626 If we see a type skeleton, record it as our stub. If we see external
7627 references, remember how many we've seen. */
7628
7629 static void
7630 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7631 {
7632 dw_die_ref c;
7633 dw_attr_ref a;
7634 unsigned ix;
7635 struct external_ref *ref_p;
7636
7637 if (is_type_die (die)
7638 && (c = get_AT_ref (die, DW_AT_signature)))
7639 {
7640 /* This is a local skeleton; use it for local references. */
7641 ref_p = lookup_external_ref (map, c);
7642 ref_p->stub = die;
7643 }
7644
7645 /* Scan the DIE references, and remember any that refer to DIEs from
7646 other CUs (i.e. those which are not marked). */
7647 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7648 if (AT_class (a) == dw_val_class_die_ref
7649 && (c = AT_ref (a))->die_mark == 0
7650 && is_type_die (c))
7651 {
7652 ref_p = lookup_external_ref (map, c);
7653 ref_p->n_refs++;
7654 }
7655
7656 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7657 }
7658
7659 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7660 points to an external_ref, DATA is the CU we're processing. If we don't
7661 already have a local stub, and we have multiple refs, build a stub. */
7662
7663 int
7664 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7665 {
7666 struct external_ref *ref_p = *slot;
7667
7668 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7669 {
7670 /* We have multiple references to this type, so build a small stub.
7671 Both of these forms are a bit dodgy from the perspective of the
7672 DWARF standard, since technically they should have names. */
7673 dw_die_ref cu = data;
7674 dw_die_ref type = ref_p->type;
7675 dw_die_ref stub = NULL;
7676
7677 if (type->comdat_type_p)
7678 {
7679 /* If we refer to this type via sig8, use AT_signature. */
7680 stub = new_die (type->die_tag, cu, NULL_TREE);
7681 add_AT_die_ref (stub, DW_AT_signature, type);
7682 }
7683 else
7684 {
7685 /* Otherwise, use a typedef with no name. */
7686 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7687 add_AT_die_ref (stub, DW_AT_type, type);
7688 }
7689
7690 stub->die_mark++;
7691 ref_p->stub = stub;
7692 }
7693 return 1;
7694 }
7695
7696 /* DIE is a unit; look through all the DIE references to see if there are
7697 any external references to types, and if so, create local stubs for
7698 them which will be applied in build_abbrev_table. This is useful because
7699 references to local DIEs are smaller. */
7700
7701 static external_ref_hash_type
7702 optimize_external_refs (dw_die_ref die)
7703 {
7704 external_ref_hash_type map;
7705 map.create (10);
7706 optimize_external_refs_1 (die, map);
7707 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7708 return map;
7709 }
7710
7711 /* The format of each DIE (and its attribute value pairs) is encoded in an
7712 abbreviation table. This routine builds the abbreviation table and assigns
7713 a unique abbreviation id for each abbreviation entry. The children of each
7714 die are visited recursively. */
7715
7716 static void
7717 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7718 {
7719 unsigned long abbrev_id;
7720 unsigned int n_alloc;
7721 dw_die_ref c;
7722 dw_attr_ref a;
7723 unsigned ix;
7724
7725 /* Scan the DIE references, and replace any that refer to
7726 DIEs from other CUs (i.e. those which are not marked) with
7727 the local stubs we built in optimize_external_refs. */
7728 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7729 if (AT_class (a) == dw_val_class_die_ref
7730 && (c = AT_ref (a))->die_mark == 0)
7731 {
7732 struct external_ref *ref_p;
7733 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7734
7735 ref_p = lookup_external_ref (extern_map, c);
7736 if (ref_p->stub && ref_p->stub != die)
7737 change_AT_die_ref (a, ref_p->stub);
7738 else
7739 /* We aren't changing this reference, so mark it external. */
7740 set_AT_ref_external (a, 1);
7741 }
7742
7743 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7744 {
7745 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7746 dw_attr_ref die_a, abbrev_a;
7747 unsigned ix;
7748 bool ok = true;
7749
7750 if (abbrev->die_tag != die->die_tag)
7751 continue;
7752 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7753 continue;
7754
7755 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7756 continue;
7757
7758 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7759 {
7760 abbrev_a = &(*abbrev->die_attr)[ix];
7761 if ((abbrev_a->dw_attr != die_a->dw_attr)
7762 || (value_format (abbrev_a) != value_format (die_a)))
7763 {
7764 ok = false;
7765 break;
7766 }
7767 }
7768 if (ok)
7769 break;
7770 }
7771
7772 if (abbrev_id >= abbrev_die_table_in_use)
7773 {
7774 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7775 {
7776 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7777 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7778 n_alloc);
7779
7780 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7781 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7782 abbrev_die_table_allocated = n_alloc;
7783 }
7784
7785 ++abbrev_die_table_in_use;
7786 abbrev_die_table[abbrev_id] = die;
7787 }
7788
7789 die->die_abbrev = abbrev_id;
7790 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7791 }
7792 \f
7793 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7794
7795 static int
7796 constant_size (unsigned HOST_WIDE_INT value)
7797 {
7798 int log;
7799
7800 if (value == 0)
7801 log = 0;
7802 else
7803 log = floor_log2 (value);
7804
7805 log = log / 8;
7806 log = 1 << (floor_log2 (log) + 1);
7807
7808 return log;
7809 }
7810
7811 /* Return the size of a DIE as it is represented in the
7812 .debug_info section. */
7813
7814 static unsigned long
7815 size_of_die (dw_die_ref die)
7816 {
7817 unsigned long size = 0;
7818 dw_attr_ref a;
7819 unsigned ix;
7820 enum dwarf_form form;
7821
7822 size += size_of_uleb128 (die->die_abbrev);
7823 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7824 {
7825 switch (AT_class (a))
7826 {
7827 case dw_val_class_addr:
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_offset:
7837 size += DWARF_OFFSET_SIZE;
7838 break;
7839 case dw_val_class_loc:
7840 {
7841 unsigned long lsize = size_of_locs (AT_loc (a));
7842
7843 /* Block length. */
7844 if (dwarf_version >= 4)
7845 size += size_of_uleb128 (lsize);
7846 else
7847 size += constant_size (lsize);
7848 size += lsize;
7849 }
7850 break;
7851 case dw_val_class_loc_list:
7852 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7853 {
7854 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7855 size += size_of_uleb128 (AT_index (a));
7856 }
7857 else
7858 size += DWARF_OFFSET_SIZE;
7859 break;
7860 case dw_val_class_range_list:
7861 size += DWARF_OFFSET_SIZE;
7862 break;
7863 case dw_val_class_const:
7864 size += size_of_sleb128 (AT_int (a));
7865 break;
7866 case dw_val_class_unsigned_const:
7867 {
7868 int csize = constant_size (AT_unsigned (a));
7869 if (dwarf_version == 3
7870 && a->dw_attr == DW_AT_data_member_location
7871 && csize >= 4)
7872 size += size_of_uleb128 (AT_unsigned (a));
7873 else
7874 size += csize;
7875 }
7876 break;
7877 case dw_val_class_const_double:
7878 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7879 if (HOST_BITS_PER_WIDE_INT >= 64)
7880 size++; /* block */
7881 break;
7882 case dw_val_class_wide_int:
7883 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7884 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7885 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7886 > 64)
7887 size++; /* block */
7888 break;
7889 case dw_val_class_vec:
7890 size += constant_size (a->dw_attr_val.v.val_vec.length
7891 * a->dw_attr_val.v.val_vec.elt_size)
7892 + a->dw_attr_val.v.val_vec.length
7893 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7894 break;
7895 case dw_val_class_flag:
7896 if (dwarf_version >= 4)
7897 /* Currently all add_AT_flag calls pass in 1 as last argument,
7898 so DW_FORM_flag_present can be used. If that ever changes,
7899 we'll need to use DW_FORM_flag and have some optimization
7900 in build_abbrev_table that will change those to
7901 DW_FORM_flag_present if it is set to 1 in all DIEs using
7902 the same abbrev entry. */
7903 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7904 else
7905 size += 1;
7906 break;
7907 case dw_val_class_die_ref:
7908 if (AT_ref_external (a))
7909 {
7910 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7911 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7912 is sized by target address length, whereas in DWARF3
7913 it's always sized as an offset. */
7914 if (use_debug_types)
7915 size += DWARF_TYPE_SIGNATURE_SIZE;
7916 else if (dwarf_version == 2)
7917 size += DWARF2_ADDR_SIZE;
7918 else
7919 size += DWARF_OFFSET_SIZE;
7920 }
7921 else
7922 size += DWARF_OFFSET_SIZE;
7923 break;
7924 case dw_val_class_fde_ref:
7925 size += DWARF_OFFSET_SIZE;
7926 break;
7927 case dw_val_class_lbl_id:
7928 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7929 {
7930 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7931 size += size_of_uleb128 (AT_index (a));
7932 }
7933 else
7934 size += DWARF2_ADDR_SIZE;
7935 break;
7936 case dw_val_class_lineptr:
7937 case dw_val_class_macptr:
7938 size += DWARF_OFFSET_SIZE;
7939 break;
7940 case dw_val_class_str:
7941 form = AT_string_form (a);
7942 if (form == DW_FORM_strp)
7943 size += DWARF_OFFSET_SIZE;
7944 else if (form == DW_FORM_GNU_str_index)
7945 size += size_of_uleb128 (AT_index (a));
7946 else
7947 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7948 break;
7949 case dw_val_class_file:
7950 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7951 break;
7952 case dw_val_class_data8:
7953 size += 8;
7954 break;
7955 case dw_val_class_vms_delta:
7956 size += DWARF_OFFSET_SIZE;
7957 break;
7958 case dw_val_class_high_pc:
7959 size += DWARF2_ADDR_SIZE;
7960 break;
7961 default:
7962 gcc_unreachable ();
7963 }
7964 }
7965
7966 return size;
7967 }
7968
7969 /* Size the debugging information associated with a given DIE. Visits the
7970 DIE's children recursively. Updates the global variable next_die_offset, on
7971 each time through. Uses the current value of next_die_offset to update the
7972 die_offset field in each DIE. */
7973
7974 static void
7975 calc_die_sizes (dw_die_ref die)
7976 {
7977 dw_die_ref c;
7978
7979 gcc_assert (die->die_offset == 0
7980 || (unsigned long int) die->die_offset == next_die_offset);
7981 die->die_offset = next_die_offset;
7982 next_die_offset += size_of_die (die);
7983
7984 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7985
7986 if (die->die_child != NULL)
7987 /* Count the null byte used to terminate sibling lists. */
7988 next_die_offset += 1;
7989 }
7990
7991 /* Size just the base type children at the start of the CU.
7992 This is needed because build_abbrev needs to size locs
7993 and sizing of type based stack ops needs to know die_offset
7994 values for the base types. */
7995
7996 static void
7997 calc_base_type_die_sizes (void)
7998 {
7999 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8000 unsigned int i;
8001 dw_die_ref base_type;
8002 #if ENABLE_ASSERT_CHECKING
8003 dw_die_ref prev = comp_unit_die ()->die_child;
8004 #endif
8005
8006 die_offset += size_of_die (comp_unit_die ());
8007 for (i = 0; base_types.iterate (i, &base_type); i++)
8008 {
8009 #if ENABLE_ASSERT_CHECKING
8010 gcc_assert (base_type->die_offset == 0
8011 && prev->die_sib == base_type
8012 && base_type->die_child == NULL
8013 && base_type->die_abbrev);
8014 prev = base_type;
8015 #endif
8016 base_type->die_offset = die_offset;
8017 die_offset += size_of_die (base_type);
8018 }
8019 }
8020
8021 /* Set the marks for a die and its children. We do this so
8022 that we know whether or not a reference needs to use FORM_ref_addr; only
8023 DIEs in the same CU will be marked. We used to clear out the offset
8024 and use that as the flag, but ran into ordering problems. */
8025
8026 static void
8027 mark_dies (dw_die_ref die)
8028 {
8029 dw_die_ref c;
8030
8031 gcc_assert (!die->die_mark);
8032
8033 die->die_mark = 1;
8034 FOR_EACH_CHILD (die, c, mark_dies (c));
8035 }
8036
8037 /* Clear the marks for a die and its children. */
8038
8039 static void
8040 unmark_dies (dw_die_ref die)
8041 {
8042 dw_die_ref c;
8043
8044 if (! use_debug_types)
8045 gcc_assert (die->die_mark);
8046
8047 die->die_mark = 0;
8048 FOR_EACH_CHILD (die, c, unmark_dies (c));
8049 }
8050
8051 /* Clear the marks for a die, its children and referred dies. */
8052
8053 static void
8054 unmark_all_dies (dw_die_ref die)
8055 {
8056 dw_die_ref c;
8057 dw_attr_ref a;
8058 unsigned ix;
8059
8060 if (!die->die_mark)
8061 return;
8062 die->die_mark = 0;
8063
8064 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8065
8066 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8067 if (AT_class (a) == dw_val_class_die_ref)
8068 unmark_all_dies (AT_ref (a));
8069 }
8070
8071 /* Calculate if the entry should appear in the final output file. It may be
8072 from a pruned a type. */
8073
8074 static bool
8075 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8076 {
8077 /* By limiting gnu pubnames to definitions only, gold can generate a
8078 gdb index without entries for declarations, which don't include
8079 enough information to be useful. */
8080 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8081 return false;
8082
8083 if (table == pubname_table)
8084 {
8085 /* Enumerator names are part of the pubname table, but the
8086 parent DW_TAG_enumeration_type die may have been pruned.
8087 Don't output them if that is the case. */
8088 if (p->die->die_tag == DW_TAG_enumerator &&
8089 (p->die->die_parent == NULL
8090 || !p->die->die_parent->die_perennial_p))
8091 return false;
8092
8093 /* Everything else in the pubname table is included. */
8094 return true;
8095 }
8096
8097 /* The pubtypes table shouldn't include types that have been
8098 pruned. */
8099 return (p->die->die_offset != 0
8100 || !flag_eliminate_unused_debug_types);
8101 }
8102
8103 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8104 generated for the compilation unit. */
8105
8106 static unsigned long
8107 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8108 {
8109 unsigned long size;
8110 unsigned i;
8111 pubname_ref p;
8112 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8113
8114 size = DWARF_PUBNAMES_HEADER_SIZE;
8115 FOR_EACH_VEC_ELT (*names, i, p)
8116 if (include_pubname_in_output (names, p))
8117 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8118
8119 size += DWARF_OFFSET_SIZE;
8120 return size;
8121 }
8122
8123 /* Return the size of the information in the .debug_aranges section. */
8124
8125 static unsigned long
8126 size_of_aranges (void)
8127 {
8128 unsigned long size;
8129
8130 size = DWARF_ARANGES_HEADER_SIZE;
8131
8132 /* Count the address/length pair for this compilation unit. */
8133 if (text_section_used)
8134 size += 2 * DWARF2_ADDR_SIZE;
8135 if (cold_text_section_used)
8136 size += 2 * DWARF2_ADDR_SIZE;
8137 if (have_multiple_function_sections)
8138 {
8139 unsigned fde_idx;
8140 dw_fde_ref fde;
8141
8142 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8143 {
8144 if (DECL_IGNORED_P (fde->decl))
8145 continue;
8146 if (!fde->in_std_section)
8147 size += 2 * DWARF2_ADDR_SIZE;
8148 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8149 size += 2 * DWARF2_ADDR_SIZE;
8150 }
8151 }
8152
8153 /* Count the two zero words used to terminated the address range table. */
8154 size += 2 * DWARF2_ADDR_SIZE;
8155 return size;
8156 }
8157 \f
8158 /* Select the encoding of an attribute value. */
8159
8160 static enum dwarf_form
8161 value_format (dw_attr_ref a)
8162 {
8163 switch (AT_class (a))
8164 {
8165 case dw_val_class_addr:
8166 /* Only very few attributes allow DW_FORM_addr. */
8167 switch (a->dw_attr)
8168 {
8169 case DW_AT_low_pc:
8170 case DW_AT_high_pc:
8171 case DW_AT_entry_pc:
8172 case DW_AT_trampoline:
8173 return (AT_index (a) == NOT_INDEXED
8174 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8175 default:
8176 break;
8177 }
8178 switch (DWARF2_ADDR_SIZE)
8179 {
8180 case 1:
8181 return DW_FORM_data1;
8182 case 2:
8183 return DW_FORM_data2;
8184 case 4:
8185 return DW_FORM_data4;
8186 case 8:
8187 return DW_FORM_data8;
8188 default:
8189 gcc_unreachable ();
8190 }
8191 case dw_val_class_range_list:
8192 case dw_val_class_loc_list:
8193 if (dwarf_version >= 4)
8194 return DW_FORM_sec_offset;
8195 /* FALLTHRU */
8196 case dw_val_class_vms_delta:
8197 case dw_val_class_offset:
8198 switch (DWARF_OFFSET_SIZE)
8199 {
8200 case 4:
8201 return DW_FORM_data4;
8202 case 8:
8203 return DW_FORM_data8;
8204 default:
8205 gcc_unreachable ();
8206 }
8207 case dw_val_class_loc:
8208 if (dwarf_version >= 4)
8209 return DW_FORM_exprloc;
8210 switch (constant_size (size_of_locs (AT_loc (a))))
8211 {
8212 case 1:
8213 return DW_FORM_block1;
8214 case 2:
8215 return DW_FORM_block2;
8216 case 4:
8217 return DW_FORM_block4;
8218 default:
8219 gcc_unreachable ();
8220 }
8221 case dw_val_class_const:
8222 return DW_FORM_sdata;
8223 case dw_val_class_unsigned_const:
8224 switch (constant_size (AT_unsigned (a)))
8225 {
8226 case 1:
8227 return DW_FORM_data1;
8228 case 2:
8229 return DW_FORM_data2;
8230 case 4:
8231 /* In DWARF3 DW_AT_data_member_location with
8232 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8233 constant, so we need to use DW_FORM_udata if we need
8234 a large constant. */
8235 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8236 return DW_FORM_udata;
8237 return DW_FORM_data4;
8238 case 8:
8239 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8240 return DW_FORM_udata;
8241 return DW_FORM_data8;
8242 default:
8243 gcc_unreachable ();
8244 }
8245 case dw_val_class_const_double:
8246 switch (HOST_BITS_PER_WIDE_INT)
8247 {
8248 case 8:
8249 return DW_FORM_data2;
8250 case 16:
8251 return DW_FORM_data4;
8252 case 32:
8253 return DW_FORM_data8;
8254 case 64:
8255 default:
8256 return DW_FORM_block1;
8257 }
8258 case dw_val_class_wide_int:
8259 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8260 {
8261 case 8:
8262 return DW_FORM_data1;
8263 case 16:
8264 return DW_FORM_data2;
8265 case 32:
8266 return DW_FORM_data4;
8267 case 64:
8268 return DW_FORM_data8;
8269 default:
8270 return DW_FORM_block1;
8271 }
8272 case dw_val_class_vec:
8273 switch (constant_size (a->dw_attr_val.v.val_vec.length
8274 * a->dw_attr_val.v.val_vec.elt_size))
8275 {
8276 case 1:
8277 return DW_FORM_block1;
8278 case 2:
8279 return DW_FORM_block2;
8280 case 4:
8281 return DW_FORM_block4;
8282 default:
8283 gcc_unreachable ();
8284 }
8285 case dw_val_class_flag:
8286 if (dwarf_version >= 4)
8287 {
8288 /* Currently all add_AT_flag calls pass in 1 as last argument,
8289 so DW_FORM_flag_present can be used. If that ever changes,
8290 we'll need to use DW_FORM_flag and have some optimization
8291 in build_abbrev_table that will change those to
8292 DW_FORM_flag_present if it is set to 1 in all DIEs using
8293 the same abbrev entry. */
8294 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8295 return DW_FORM_flag_present;
8296 }
8297 return DW_FORM_flag;
8298 case dw_val_class_die_ref:
8299 if (AT_ref_external (a))
8300 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8301 else
8302 return DW_FORM_ref;
8303 case dw_val_class_fde_ref:
8304 return DW_FORM_data;
8305 case dw_val_class_lbl_id:
8306 return (AT_index (a) == NOT_INDEXED
8307 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8308 case dw_val_class_lineptr:
8309 case dw_val_class_macptr:
8310 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8311 case dw_val_class_str:
8312 return AT_string_form (a);
8313 case dw_val_class_file:
8314 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8315 {
8316 case 1:
8317 return DW_FORM_data1;
8318 case 2:
8319 return DW_FORM_data2;
8320 case 4:
8321 return DW_FORM_data4;
8322 default:
8323 gcc_unreachable ();
8324 }
8325
8326 case dw_val_class_data8:
8327 return DW_FORM_data8;
8328
8329 case dw_val_class_high_pc:
8330 switch (DWARF2_ADDR_SIZE)
8331 {
8332 case 1:
8333 return DW_FORM_data1;
8334 case 2:
8335 return DW_FORM_data2;
8336 case 4:
8337 return DW_FORM_data4;
8338 case 8:
8339 return DW_FORM_data8;
8340 default:
8341 gcc_unreachable ();
8342 }
8343
8344 default:
8345 gcc_unreachable ();
8346 }
8347 }
8348
8349 /* Output the encoding of an attribute value. */
8350
8351 static void
8352 output_value_format (dw_attr_ref a)
8353 {
8354 enum dwarf_form form = value_format (a);
8355
8356 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8357 }
8358
8359 /* Given a die and id, produce the appropriate abbreviations. */
8360
8361 static void
8362 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8363 {
8364 unsigned ix;
8365 dw_attr_ref a_attr;
8366
8367 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8368 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8369 dwarf_tag_name (abbrev->die_tag));
8370
8371 if (abbrev->die_child != NULL)
8372 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8373 else
8374 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8375
8376 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8377 {
8378 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8379 dwarf_attr_name (a_attr->dw_attr));
8380 output_value_format (a_attr);
8381 }
8382
8383 dw2_asm_output_data (1, 0, NULL);
8384 dw2_asm_output_data (1, 0, NULL);
8385 }
8386
8387
8388 /* Output the .debug_abbrev section which defines the DIE abbreviation
8389 table. */
8390
8391 static void
8392 output_abbrev_section (void)
8393 {
8394 unsigned long abbrev_id;
8395
8396 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8397 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8398
8399 /* Terminate the table. */
8400 dw2_asm_output_data (1, 0, NULL);
8401 }
8402
8403 /* Output a symbol we can use to refer to this DIE from another CU. */
8404
8405 static inline void
8406 output_die_symbol (dw_die_ref die)
8407 {
8408 const char *sym = die->die_id.die_symbol;
8409
8410 gcc_assert (!die->comdat_type_p);
8411
8412 if (sym == 0)
8413 return;
8414
8415 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8416 /* We make these global, not weak; if the target doesn't support
8417 .linkonce, it doesn't support combining the sections, so debugging
8418 will break. */
8419 targetm.asm_out.globalize_label (asm_out_file, sym);
8420
8421 ASM_OUTPUT_LABEL (asm_out_file, sym);
8422 }
8423
8424 /* Return a new location list, given the begin and end range, and the
8425 expression. */
8426
8427 static inline dw_loc_list_ref
8428 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8429 const char *section)
8430 {
8431 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8432
8433 retlist->begin = begin;
8434 retlist->begin_entry = NULL;
8435 retlist->end = end;
8436 retlist->expr = expr;
8437 retlist->section = section;
8438
8439 return retlist;
8440 }
8441
8442 /* Generate a new internal symbol for this location list node, if it
8443 hasn't got one yet. */
8444
8445 static inline void
8446 gen_llsym (dw_loc_list_ref list)
8447 {
8448 gcc_assert (!list->ll_symbol);
8449 list->ll_symbol = gen_internal_sym ("LLST");
8450 }
8451
8452 /* Output the location list given to us. */
8453
8454 static void
8455 output_loc_list (dw_loc_list_ref list_head)
8456 {
8457 dw_loc_list_ref curr = list_head;
8458
8459 if (list_head->emitted)
8460 return;
8461 list_head->emitted = true;
8462
8463 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8464
8465 /* Walk the location list, and output each range + expression. */
8466 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8467 {
8468 unsigned long size;
8469 /* Don't output an entry that starts and ends at the same address. */
8470 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8471 continue;
8472 size = size_of_locs (curr->expr);
8473 /* If the expression is too large, drop it on the floor. We could
8474 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8475 in the expression, but >= 64KB expressions for a single value
8476 in a single range are unlikely very useful. */
8477 if (size > 0xffff)
8478 continue;
8479 if (dwarf_split_debug_info)
8480 {
8481 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8482 "Location list start/length entry (%s)",
8483 list_head->ll_symbol);
8484 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8485 "Location list range start index (%s)",
8486 curr->begin);
8487 /* The length field is 4 bytes. If we ever need to support
8488 an 8-byte length, we can add a new DW_LLE code or fall back
8489 to DW_LLE_GNU_start_end_entry. */
8490 dw2_asm_output_delta (4, curr->end, curr->begin,
8491 "Location list range length (%s)",
8492 list_head->ll_symbol);
8493 }
8494 else if (!have_multiple_function_sections)
8495 {
8496 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8497 "Location list begin address (%s)",
8498 list_head->ll_symbol);
8499 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8500 "Location list end address (%s)",
8501 list_head->ll_symbol);
8502 }
8503 else
8504 {
8505 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8506 "Location list begin address (%s)",
8507 list_head->ll_symbol);
8508 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8509 "Location list end address (%s)",
8510 list_head->ll_symbol);
8511 }
8512
8513 /* Output the block length for this list of location operations. */
8514 gcc_assert (size <= 0xffff);
8515 dw2_asm_output_data (2, size, "%s", "Location expression size");
8516
8517 output_loc_sequence (curr->expr, -1);
8518 }
8519
8520 if (dwarf_split_debug_info)
8521 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8522 "Location list terminator (%s)",
8523 list_head->ll_symbol);
8524 else
8525 {
8526 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8527 "Location list terminator begin (%s)",
8528 list_head->ll_symbol);
8529 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8530 "Location list terminator end (%s)",
8531 list_head->ll_symbol);
8532 }
8533 }
8534
8535 /* Output a range_list offset into the debug_range section. Emit a
8536 relocated reference if val_entry is NULL, otherwise, emit an
8537 indirect reference. */
8538
8539 static void
8540 output_range_list_offset (dw_attr_ref a)
8541 {
8542 const char *name = dwarf_attr_name (a->dw_attr);
8543
8544 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8545 {
8546 char *p = strchr (ranges_section_label, '\0');
8547 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8548 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8549 debug_ranges_section, "%s", name);
8550 *p = '\0';
8551 }
8552 else
8553 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8554 "%s (offset from %s)", name, ranges_section_label);
8555 }
8556
8557 /* Output the offset into the debug_loc section. */
8558
8559 static void
8560 output_loc_list_offset (dw_attr_ref a)
8561 {
8562 char *sym = AT_loc_list (a)->ll_symbol;
8563
8564 gcc_assert (sym);
8565 if (dwarf_split_debug_info)
8566 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8567 "%s", dwarf_attr_name (a->dw_attr));
8568 else
8569 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8570 "%s", dwarf_attr_name (a->dw_attr));
8571 }
8572
8573 /* Output an attribute's index or value appropriately. */
8574
8575 static void
8576 output_attr_index_or_value (dw_attr_ref a)
8577 {
8578 const char *name = dwarf_attr_name (a->dw_attr);
8579
8580 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8581 {
8582 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8583 return;
8584 }
8585 switch (AT_class (a))
8586 {
8587 case dw_val_class_addr:
8588 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8589 break;
8590 case dw_val_class_high_pc:
8591 case dw_val_class_lbl_id:
8592 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8593 break;
8594 case dw_val_class_loc_list:
8595 output_loc_list_offset (a);
8596 break;
8597 default:
8598 gcc_unreachable ();
8599 }
8600 }
8601
8602 /* Output a type signature. */
8603
8604 static inline void
8605 output_signature (const char *sig, const char *name)
8606 {
8607 int i;
8608
8609 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8610 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8611 }
8612
8613 /* Output the DIE and its attributes. Called recursively to generate
8614 the definitions of each child DIE. */
8615
8616 static void
8617 output_die (dw_die_ref die)
8618 {
8619 dw_attr_ref a;
8620 dw_die_ref c;
8621 unsigned long size;
8622 unsigned ix;
8623
8624 /* If someone in another CU might refer to us, set up a symbol for
8625 them to point to. */
8626 if (! die->comdat_type_p && die->die_id.die_symbol)
8627 output_die_symbol (die);
8628
8629 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8630 (unsigned long)die->die_offset,
8631 dwarf_tag_name (die->die_tag));
8632
8633 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8634 {
8635 const char *name = dwarf_attr_name (a->dw_attr);
8636
8637 switch (AT_class (a))
8638 {
8639 case dw_val_class_addr:
8640 output_attr_index_or_value (a);
8641 break;
8642
8643 case dw_val_class_offset:
8644 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8645 "%s", name);
8646 break;
8647
8648 case dw_val_class_range_list:
8649 output_range_list_offset (a);
8650 break;
8651
8652 case dw_val_class_loc:
8653 size = size_of_locs (AT_loc (a));
8654
8655 /* Output the block length for this list of location operations. */
8656 if (dwarf_version >= 4)
8657 dw2_asm_output_data_uleb128 (size, "%s", name);
8658 else
8659 dw2_asm_output_data (constant_size (size), size, "%s", name);
8660
8661 output_loc_sequence (AT_loc (a), -1);
8662 break;
8663
8664 case dw_val_class_const:
8665 /* ??? It would be slightly more efficient to use a scheme like is
8666 used for unsigned constants below, but gdb 4.x does not sign
8667 extend. Gdb 5.x does sign extend. */
8668 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8669 break;
8670
8671 case dw_val_class_unsigned_const:
8672 {
8673 int csize = constant_size (AT_unsigned (a));
8674 if (dwarf_version == 3
8675 && a->dw_attr == DW_AT_data_member_location
8676 && csize >= 4)
8677 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8678 else
8679 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8680 }
8681 break;
8682
8683 case dw_val_class_const_double:
8684 {
8685 unsigned HOST_WIDE_INT first, second;
8686
8687 if (HOST_BITS_PER_WIDE_INT >= 64)
8688 dw2_asm_output_data (1,
8689 HOST_BITS_PER_DOUBLE_INT
8690 / HOST_BITS_PER_CHAR,
8691 NULL);
8692
8693 if (WORDS_BIG_ENDIAN)
8694 {
8695 first = a->dw_attr_val.v.val_double.high;
8696 second = a->dw_attr_val.v.val_double.low;
8697 }
8698 else
8699 {
8700 first = a->dw_attr_val.v.val_double.low;
8701 second = a->dw_attr_val.v.val_double.high;
8702 }
8703
8704 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8705 first, "%s", name);
8706 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8707 second, NULL);
8708 }
8709 break;
8710
8711 case dw_val_class_wide_int:
8712 {
8713 int i;
8714 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8715 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8716 if (len * HOST_BITS_PER_WIDE_INT > 64)
8717 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8718 NULL);
8719
8720 if (WORDS_BIG_ENDIAN)
8721 for (i = len; i >= 0; --i)
8722 {
8723 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8724 name);
8725 name = NULL;
8726 }
8727 else
8728 for (i = 0; i < len; ++i)
8729 {
8730 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8731 name);
8732 name = NULL;
8733 }
8734 }
8735 break;
8736
8737 case dw_val_class_vec:
8738 {
8739 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8740 unsigned int len = a->dw_attr_val.v.val_vec.length;
8741 unsigned int i;
8742 unsigned char *p;
8743
8744 dw2_asm_output_data (constant_size (len * elt_size),
8745 len * elt_size, "%s", name);
8746 if (elt_size > sizeof (HOST_WIDE_INT))
8747 {
8748 elt_size /= 2;
8749 len *= 2;
8750 }
8751 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8752 i < len;
8753 i++, p += elt_size)
8754 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8755 "fp or vector constant word %u", i);
8756 break;
8757 }
8758
8759 case dw_val_class_flag:
8760 if (dwarf_version >= 4)
8761 {
8762 /* Currently all add_AT_flag calls pass in 1 as last argument,
8763 so DW_FORM_flag_present can be used. If that ever changes,
8764 we'll need to use DW_FORM_flag and have some optimization
8765 in build_abbrev_table that will change those to
8766 DW_FORM_flag_present if it is set to 1 in all DIEs using
8767 the same abbrev entry. */
8768 gcc_assert (AT_flag (a) == 1);
8769 if (flag_debug_asm)
8770 fprintf (asm_out_file, "\t\t\t%s %s\n",
8771 ASM_COMMENT_START, name);
8772 break;
8773 }
8774 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8775 break;
8776
8777 case dw_val_class_loc_list:
8778 output_attr_index_or_value (a);
8779 break;
8780
8781 case dw_val_class_die_ref:
8782 if (AT_ref_external (a))
8783 {
8784 if (AT_ref (a)->comdat_type_p)
8785 {
8786 comdat_type_node_ref type_node =
8787 AT_ref (a)->die_id.die_type_node;
8788
8789 gcc_assert (type_node);
8790 output_signature (type_node->signature, name);
8791 }
8792 else
8793 {
8794 const char *sym = AT_ref (a)->die_id.die_symbol;
8795 int size;
8796
8797 gcc_assert (sym);
8798 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8799 length, whereas in DWARF3 it's always sized as an
8800 offset. */
8801 if (dwarf_version == 2)
8802 size = DWARF2_ADDR_SIZE;
8803 else
8804 size = DWARF_OFFSET_SIZE;
8805 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8806 name);
8807 }
8808 }
8809 else
8810 {
8811 gcc_assert (AT_ref (a)->die_offset);
8812 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8813 "%s", name);
8814 }
8815 break;
8816
8817 case dw_val_class_fde_ref:
8818 {
8819 char l1[20];
8820
8821 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8822 a->dw_attr_val.v.val_fde_index * 2);
8823 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8824 "%s", name);
8825 }
8826 break;
8827
8828 case dw_val_class_vms_delta:
8829 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8830 AT_vms_delta2 (a), AT_vms_delta1 (a),
8831 "%s", name);
8832 break;
8833
8834 case dw_val_class_lbl_id:
8835 output_attr_index_or_value (a);
8836 break;
8837
8838 case dw_val_class_lineptr:
8839 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8840 debug_line_section, "%s", name);
8841 break;
8842
8843 case dw_val_class_macptr:
8844 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8845 debug_macinfo_section, "%s", name);
8846 break;
8847
8848 case dw_val_class_str:
8849 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8850 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8851 a->dw_attr_val.v.val_str->label,
8852 debug_str_section,
8853 "%s: \"%s\"", name, AT_string (a));
8854 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8855 dw2_asm_output_data_uleb128 (AT_index (a),
8856 "%s: \"%s\"", name, AT_string (a));
8857 else
8858 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8859 break;
8860
8861 case dw_val_class_file:
8862 {
8863 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8864
8865 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8866 a->dw_attr_val.v.val_file->filename);
8867 break;
8868 }
8869
8870 case dw_val_class_data8:
8871 {
8872 int i;
8873
8874 for (i = 0; i < 8; i++)
8875 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8876 i == 0 ? "%s" : NULL, name);
8877 break;
8878 }
8879
8880 case dw_val_class_high_pc:
8881 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8882 get_AT_low_pc (die), "DW_AT_high_pc");
8883 break;
8884
8885 default:
8886 gcc_unreachable ();
8887 }
8888 }
8889
8890 FOR_EACH_CHILD (die, c, output_die (c));
8891
8892 /* Add null byte to terminate sibling list. */
8893 if (die->die_child != NULL)
8894 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8895 (unsigned long) die->die_offset);
8896 }
8897
8898 /* Output the compilation unit that appears at the beginning of the
8899 .debug_info section, and precedes the DIE descriptions. */
8900
8901 static void
8902 output_compilation_unit_header (void)
8903 {
8904 int ver = dwarf_version;
8905
8906 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8907 dw2_asm_output_data (4, 0xffffffff,
8908 "Initial length escape value indicating 64-bit DWARF extension");
8909 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8910 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8911 "Length of Compilation Unit Info");
8912 dw2_asm_output_data (2, ver, "DWARF version number");
8913 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8914 debug_abbrev_section,
8915 "Offset Into Abbrev. Section");
8916 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8917 }
8918
8919 /* Output the compilation unit DIE and its children. */
8920
8921 static void
8922 output_comp_unit (dw_die_ref die, int output_if_empty)
8923 {
8924 const char *secname, *oldsym;
8925 char *tmp;
8926 external_ref_hash_type extern_map;
8927
8928 /* Unless we are outputting main CU, we may throw away empty ones. */
8929 if (!output_if_empty && die->die_child == NULL)
8930 return;
8931
8932 /* Even if there are no children of this DIE, we must output the information
8933 about the compilation unit. Otherwise, on an empty translation unit, we
8934 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8935 will then complain when examining the file. First mark all the DIEs in
8936 this CU so we know which get local refs. */
8937 mark_dies (die);
8938
8939 extern_map = optimize_external_refs (die);
8940
8941 build_abbrev_table (die, extern_map);
8942
8943 extern_map.dispose ();
8944
8945 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8946 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8947 calc_die_sizes (die);
8948
8949 oldsym = die->die_id.die_symbol;
8950 if (oldsym)
8951 {
8952 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8953
8954 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8955 secname = tmp;
8956 die->die_id.die_symbol = NULL;
8957 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8958 }
8959 else
8960 {
8961 switch_to_section (debug_info_section);
8962 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8963 info_section_emitted = true;
8964 }
8965
8966 /* Output debugging information. */
8967 output_compilation_unit_header ();
8968 output_die (die);
8969
8970 /* Leave the marks on the main CU, so we can check them in
8971 output_pubnames. */
8972 if (oldsym)
8973 {
8974 unmark_dies (die);
8975 die->die_id.die_symbol = oldsym;
8976 }
8977 }
8978
8979 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8980 and .debug_pubtypes. This is configured per-target, but can be
8981 overridden by the -gpubnames or -gno-pubnames options. */
8982
8983 static inline bool
8984 want_pubnames (void)
8985 {
8986 if (debug_info_level <= DINFO_LEVEL_TERSE)
8987 return false;
8988 if (debug_generate_pub_sections != -1)
8989 return debug_generate_pub_sections;
8990 return targetm.want_debug_pub_sections;
8991 }
8992
8993 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8994
8995 static void
8996 add_AT_pubnames (dw_die_ref die)
8997 {
8998 if (want_pubnames ())
8999 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9000 }
9001
9002 /* Add a string attribute value to a skeleton DIE. */
9003
9004 static inline void
9005 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9006 const char *str)
9007 {
9008 dw_attr_node attr;
9009 struct indirect_string_node *node;
9010
9011 if (! skeleton_debug_str_hash)
9012 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
9013 debug_str_eq, NULL);
9014
9015 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9016 find_string_form (node);
9017 if (node->form == DW_FORM_GNU_str_index)
9018 node->form = DW_FORM_strp;
9019
9020 attr.dw_attr = attr_kind;
9021 attr.dw_attr_val.val_class = dw_val_class_str;
9022 attr.dw_attr_val.val_entry = NULL;
9023 attr.dw_attr_val.v.val_str = node;
9024 add_dwarf_attr (die, &attr);
9025 }
9026
9027 /* Helper function to generate top-level dies for skeleton debug_info and
9028 debug_types. */
9029
9030 static void
9031 add_top_level_skeleton_die_attrs (dw_die_ref die)
9032 {
9033 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9034 const char *comp_dir = comp_dir_string ();
9035
9036 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9037 if (comp_dir != NULL)
9038 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9039 add_AT_pubnames (die);
9040 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9041 }
9042
9043 /* Return the single type-unit die for skeleton type units. */
9044
9045 static dw_die_ref
9046 get_skeleton_type_unit (void)
9047 {
9048 /* For dwarf_split_debug_sections with use_type info, all type units in the
9049 skeleton sections have identical dies (but different headers). This
9050 single die will be output many times. */
9051
9052 static dw_die_ref skeleton_type_unit = NULL;
9053
9054 if (skeleton_type_unit == NULL)
9055 {
9056 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
9057 add_top_level_skeleton_die_attrs (skeleton_type_unit);
9058 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
9059 }
9060 return skeleton_type_unit;
9061 }
9062
9063 /* Output skeleton debug sections that point to the dwo file. */
9064
9065 static void
9066 output_skeleton_debug_sections (dw_die_ref comp_unit)
9067 {
9068 /* These attributes will be found in the full debug_info section. */
9069 remove_AT (comp_unit, DW_AT_producer);
9070 remove_AT (comp_unit, DW_AT_language);
9071
9072 switch_to_section (debug_skeleton_info_section);
9073 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9074
9075 /* Produce the skeleton compilation-unit header. This one differs enough from
9076 a normal CU header that it's better not to call output_compilation_unit
9077 header. */
9078 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9079 dw2_asm_output_data (4, 0xffffffff,
9080 "Initial length escape value indicating 64-bit DWARF extension");
9081
9082 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9083 DWARF_COMPILE_UNIT_HEADER_SIZE
9084 - DWARF_INITIAL_LENGTH_SIZE
9085 + size_of_die (comp_unit),
9086 "Length of Compilation Unit Info");
9087 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9088 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9089 debug_abbrev_section,
9090 "Offset Into Abbrev. Section");
9091 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9092
9093 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9094 output_die (comp_unit);
9095
9096 /* Build the skeleton debug_abbrev section. */
9097 switch_to_section (debug_skeleton_abbrev_section);
9098 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9099
9100 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9101 if (use_debug_types)
9102 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
9103
9104 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9105 }
9106
9107 /* Output a comdat type unit DIE and its children. */
9108
9109 static void
9110 output_comdat_type_unit (comdat_type_node *node)
9111 {
9112 const char *secname;
9113 char *tmp;
9114 int i;
9115 #if defined (OBJECT_FORMAT_ELF)
9116 tree comdat_key;
9117 #endif
9118 external_ref_hash_type extern_map;
9119
9120 /* First mark all the DIEs in this CU so we know which get local refs. */
9121 mark_dies (node->root_die);
9122
9123 extern_map = optimize_external_refs (node->root_die);
9124
9125 build_abbrev_table (node->root_die, extern_map);
9126
9127 extern_map.dispose ();
9128
9129 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9130 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9131 calc_die_sizes (node->root_die);
9132
9133 #if defined (OBJECT_FORMAT_ELF)
9134 if (!dwarf_split_debug_info)
9135 secname = ".debug_types";
9136 else
9137 secname = ".debug_types.dwo";
9138
9139 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9140 sprintf (tmp, "wt.");
9141 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9142 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9143 comdat_key = get_identifier (tmp);
9144 targetm.asm_out.named_section (secname,
9145 SECTION_DEBUG | SECTION_LINKONCE,
9146 comdat_key);
9147 #else
9148 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9149 sprintf (tmp, ".gnu.linkonce.wt.");
9150 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9151 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9152 secname = tmp;
9153 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9154 #endif
9155
9156 /* Output debugging information. */
9157 output_compilation_unit_header ();
9158 output_signature (node->signature, "Type Signature");
9159 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9160 "Offset to Type DIE");
9161 output_die (node->root_die);
9162
9163 unmark_dies (node->root_die);
9164
9165 #if defined (OBJECT_FORMAT_ELF)
9166 if (dwarf_split_debug_info)
9167 {
9168 /* Produce the skeleton type-unit header. */
9169 const char *secname = ".debug_types";
9170
9171 targetm.asm_out.named_section (secname,
9172 SECTION_DEBUG | SECTION_LINKONCE,
9173 comdat_key);
9174 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9175 dw2_asm_output_data (4, 0xffffffff,
9176 "Initial length escape value indicating 64-bit DWARF extension");
9177
9178 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9179 DWARF_COMPILE_UNIT_HEADER_SIZE
9180 - DWARF_INITIAL_LENGTH_SIZE
9181 + size_of_die (get_skeleton_type_unit ())
9182 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9183 "Length of Type Unit Info");
9184 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9185 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9186 debug_skeleton_abbrev_section_label,
9187 debug_abbrev_section,
9188 "Offset Into Abbrev. Section");
9189 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9190 output_signature (node->signature, "Type Signature");
9191 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9192
9193 output_die (get_skeleton_type_unit ());
9194 }
9195 #endif
9196 }
9197
9198 /* Return the DWARF2/3 pubname associated with a decl. */
9199
9200 static const char *
9201 dwarf2_name (tree decl, int scope)
9202 {
9203 if (DECL_NAMELESS (decl))
9204 return NULL;
9205 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9206 }
9207
9208 /* Add a new entry to .debug_pubnames if appropriate. */
9209
9210 static void
9211 add_pubname_string (const char *str, dw_die_ref die)
9212 {
9213 pubname_entry e;
9214
9215 e.die = die;
9216 e.name = xstrdup (str);
9217 vec_safe_push (pubname_table, e);
9218 }
9219
9220 static void
9221 add_pubname (tree decl, dw_die_ref die)
9222 {
9223 if (!want_pubnames ())
9224 return;
9225
9226 /* Don't add items to the table when we expect that the consumer will have
9227 just read the enclosing die. For example, if the consumer is looking at a
9228 class_member, it will either be inside the class already, or will have just
9229 looked up the class to find the member. Either way, searching the class is
9230 faster than searching the index. */
9231 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9232 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9233 {
9234 const char *name = dwarf2_name (decl, 1);
9235
9236 if (name)
9237 add_pubname_string (name, die);
9238 }
9239 }
9240
9241 /* Add an enumerator to the pubnames section. */
9242
9243 static void
9244 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9245 {
9246 pubname_entry e;
9247
9248 gcc_assert (scope_name);
9249 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9250 e.die = die;
9251 vec_safe_push (pubname_table, e);
9252 }
9253
9254 /* Add a new entry to .debug_pubtypes if appropriate. */
9255
9256 static void
9257 add_pubtype (tree decl, dw_die_ref die)
9258 {
9259 pubname_entry e;
9260
9261 if (!want_pubnames ())
9262 return;
9263
9264 if ((TREE_PUBLIC (decl)
9265 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9266 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9267 {
9268 tree scope = NULL;
9269 const char *scope_name = "";
9270 const char *sep = is_cxx () ? "::" : ".";
9271 const char *name;
9272
9273 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9274 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9275 {
9276 scope_name = lang_hooks.dwarf_name (scope, 1);
9277 if (scope_name != NULL && scope_name[0] != '\0')
9278 scope_name = concat (scope_name, sep, NULL);
9279 else
9280 scope_name = "";
9281 }
9282
9283 if (TYPE_P (decl))
9284 name = type_tag (decl);
9285 else
9286 name = lang_hooks.dwarf_name (decl, 1);
9287
9288 /* If we don't have a name for the type, there's no point in adding
9289 it to the table. */
9290 if (name != NULL && name[0] != '\0')
9291 {
9292 e.die = die;
9293 e.name = concat (scope_name, name, NULL);
9294 vec_safe_push (pubtype_table, e);
9295 }
9296
9297 /* Although it might be more consistent to add the pubinfo for the
9298 enumerators as their dies are created, they should only be added if the
9299 enum type meets the criteria above. So rather than re-check the parent
9300 enum type whenever an enumerator die is created, just output them all
9301 here. This isn't protected by the name conditional because anonymous
9302 enums don't have names. */
9303 if (die->die_tag == DW_TAG_enumeration_type)
9304 {
9305 dw_die_ref c;
9306
9307 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9308 }
9309 }
9310 }
9311
9312 /* Output a single entry in the pubnames table. */
9313
9314 static void
9315 output_pubname (dw_offset die_offset, pubname_entry *entry)
9316 {
9317 dw_die_ref die = entry->die;
9318 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9319
9320 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9321
9322 if (debug_generate_pub_sections == 2)
9323 {
9324 /* This logic follows gdb's method for determining the value of the flag
9325 byte. */
9326 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9327 switch (die->die_tag)
9328 {
9329 case DW_TAG_typedef:
9330 case DW_TAG_base_type:
9331 case DW_TAG_subrange_type:
9332 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9333 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9334 break;
9335 case DW_TAG_enumerator:
9336 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9337 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9338 if (!is_cxx () && !is_java ())
9339 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9340 break;
9341 case DW_TAG_subprogram:
9342 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9343 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9344 if (!is_ada ())
9345 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9346 break;
9347 case DW_TAG_constant:
9348 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9349 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9350 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9351 break;
9352 case DW_TAG_variable:
9353 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9354 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9355 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9356 break;
9357 case DW_TAG_namespace:
9358 case DW_TAG_imported_declaration:
9359 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9360 break;
9361 case DW_TAG_class_type:
9362 case DW_TAG_interface_type:
9363 case DW_TAG_structure_type:
9364 case DW_TAG_union_type:
9365 case DW_TAG_enumeration_type:
9366 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9367 if (!is_cxx () && !is_java ())
9368 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9369 break;
9370 default:
9371 /* An unusual tag. Leave the flag-byte empty. */
9372 break;
9373 }
9374 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9375 "GDB-index flags");
9376 }
9377
9378 dw2_asm_output_nstring (entry->name, -1, "external name");
9379 }
9380
9381
9382 /* Output the public names table used to speed up access to externally
9383 visible names; or the public types table used to find type definitions. */
9384
9385 static void
9386 output_pubnames (vec<pubname_entry, va_gc> *names)
9387 {
9388 unsigned i;
9389 unsigned long pubnames_length = size_of_pubnames (names);
9390 pubname_ref pub;
9391
9392 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9393 dw2_asm_output_data (4, 0xffffffff,
9394 "Initial length escape value indicating 64-bit DWARF extension");
9395 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9396
9397 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9398 dw2_asm_output_data (2, 2, "DWARF Version");
9399
9400 if (dwarf_split_debug_info)
9401 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9402 debug_skeleton_info_section,
9403 "Offset of Compilation Unit Info");
9404 else
9405 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9406 debug_info_section,
9407 "Offset of Compilation Unit Info");
9408 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9409 "Compilation Unit Length");
9410
9411 FOR_EACH_VEC_ELT (*names, i, pub)
9412 {
9413 if (include_pubname_in_output (names, pub))
9414 {
9415 dw_offset die_offset = pub->die->die_offset;
9416
9417 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9418 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9419 gcc_assert (pub->die->die_mark);
9420
9421 /* If we're putting types in their own .debug_types sections,
9422 the .debug_pubtypes table will still point to the compile
9423 unit (not the type unit), so we want to use the offset of
9424 the skeleton DIE (if there is one). */
9425 if (pub->die->comdat_type_p && names == pubtype_table)
9426 {
9427 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9428
9429 if (type_node != NULL)
9430 die_offset = (type_node->skeleton_die != NULL
9431 ? type_node->skeleton_die->die_offset
9432 : 0);
9433 }
9434
9435 output_pubname (die_offset, pub);
9436 }
9437 }
9438
9439 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9440 }
9441
9442 /* Output public names and types tables if necessary. */
9443
9444 static void
9445 output_pubtables (void)
9446 {
9447 if (!want_pubnames () || !info_section_emitted)
9448 return;
9449
9450 switch_to_section (debug_pubnames_section);
9451 output_pubnames (pubname_table);
9452 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9453 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9454 simply won't look for the section. */
9455 switch_to_section (debug_pubtypes_section);
9456 output_pubnames (pubtype_table);
9457 }
9458
9459
9460 /* Output the information that goes into the .debug_aranges table.
9461 Namely, define the beginning and ending address range of the
9462 text section generated for this compilation unit. */
9463
9464 static void
9465 output_aranges (unsigned long aranges_length)
9466 {
9467 unsigned i;
9468
9469 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9470 dw2_asm_output_data (4, 0xffffffff,
9471 "Initial length escape value indicating 64-bit DWARF extension");
9472 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9473 "Length of Address Ranges Info");
9474 /* Version number for aranges is still 2, even in DWARF3. */
9475 dw2_asm_output_data (2, 2, "DWARF Version");
9476 if (dwarf_split_debug_info)
9477 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9478 debug_skeleton_info_section,
9479 "Offset of Compilation Unit Info");
9480 else
9481 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9482 debug_info_section,
9483 "Offset of Compilation Unit Info");
9484 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9485 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9486
9487 /* We need to align to twice the pointer size here. */
9488 if (DWARF_ARANGES_PAD_SIZE)
9489 {
9490 /* Pad using a 2 byte words so that padding is correct for any
9491 pointer size. */
9492 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9493 2 * DWARF2_ADDR_SIZE);
9494 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9495 dw2_asm_output_data (2, 0, NULL);
9496 }
9497
9498 /* It is necessary not to output these entries if the sections were
9499 not used; if the sections were not used, the length will be 0 and
9500 the address may end up as 0 if the section is discarded by ld
9501 --gc-sections, leaving an invalid (0, 0) entry that can be
9502 confused with the terminator. */
9503 if (text_section_used)
9504 {
9505 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9506 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9507 text_section_label, "Length");
9508 }
9509 if (cold_text_section_used)
9510 {
9511 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9512 "Address");
9513 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9514 cold_text_section_label, "Length");
9515 }
9516
9517 if (have_multiple_function_sections)
9518 {
9519 unsigned fde_idx;
9520 dw_fde_ref fde;
9521
9522 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9523 {
9524 if (DECL_IGNORED_P (fde->decl))
9525 continue;
9526 if (!fde->in_std_section)
9527 {
9528 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9529 "Address");
9530 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9531 fde->dw_fde_begin, "Length");
9532 }
9533 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9534 {
9535 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9536 "Address");
9537 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9538 fde->dw_fde_second_begin, "Length");
9539 }
9540 }
9541 }
9542
9543 /* Output the terminator words. */
9544 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9545 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9546 }
9547
9548 /* Add a new entry to .debug_ranges. Return the offset at which it
9549 was placed. */
9550
9551 static unsigned int
9552 add_ranges_num (int num)
9553 {
9554 unsigned int in_use = ranges_table_in_use;
9555
9556 if (in_use == ranges_table_allocated)
9557 {
9558 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9559 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9560 ranges_table_allocated);
9561 memset (ranges_table + ranges_table_in_use, 0,
9562 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9563 }
9564
9565 ranges_table[in_use].num = num;
9566 ranges_table_in_use = in_use + 1;
9567
9568 return in_use * 2 * DWARF2_ADDR_SIZE;
9569 }
9570
9571 /* Add a new entry to .debug_ranges corresponding to a block, or a
9572 range terminator if BLOCK is NULL. */
9573
9574 static unsigned int
9575 add_ranges (const_tree block)
9576 {
9577 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9578 }
9579
9580 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9581 When using dwarf_split_debug_info, address attributes in dies destined
9582 for the final executable should be direct references--setting the
9583 parameter force_direct ensures this behavior. */
9584
9585 static void
9586 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9587 bool *added, bool force_direct)
9588 {
9589 unsigned int in_use = ranges_by_label_in_use;
9590 unsigned int offset;
9591
9592 if (in_use == ranges_by_label_allocated)
9593 {
9594 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9595 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9596 ranges_by_label,
9597 ranges_by_label_allocated);
9598 memset (ranges_by_label + ranges_by_label_in_use, 0,
9599 RANGES_TABLE_INCREMENT
9600 * sizeof (struct dw_ranges_by_label_struct));
9601 }
9602
9603 ranges_by_label[in_use].begin = begin;
9604 ranges_by_label[in_use].end = end;
9605 ranges_by_label_in_use = in_use + 1;
9606
9607 offset = add_ranges_num (-(int)in_use - 1);
9608 if (!*added)
9609 {
9610 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9611 *added = true;
9612 }
9613 }
9614
9615 static void
9616 output_ranges (void)
9617 {
9618 unsigned i;
9619 static const char *const start_fmt = "Offset %#x";
9620 const char *fmt = start_fmt;
9621
9622 for (i = 0; i < ranges_table_in_use; i++)
9623 {
9624 int block_num = ranges_table[i].num;
9625
9626 if (block_num > 0)
9627 {
9628 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9629 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9630
9631 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9632 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9633
9634 /* If all code is in the text section, then the compilation
9635 unit base address defaults to DW_AT_low_pc, which is the
9636 base of the text section. */
9637 if (!have_multiple_function_sections)
9638 {
9639 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9640 text_section_label,
9641 fmt, i * 2 * DWARF2_ADDR_SIZE);
9642 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9643 text_section_label, NULL);
9644 }
9645
9646 /* Otherwise, the compilation unit base address is zero,
9647 which allows us to use absolute addresses, and not worry
9648 about whether the target supports cross-section
9649 arithmetic. */
9650 else
9651 {
9652 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9653 fmt, i * 2 * DWARF2_ADDR_SIZE);
9654 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9655 }
9656
9657 fmt = NULL;
9658 }
9659
9660 /* Negative block_num stands for an index into ranges_by_label. */
9661 else if (block_num < 0)
9662 {
9663 int lab_idx = - block_num - 1;
9664
9665 if (!have_multiple_function_sections)
9666 {
9667 gcc_unreachable ();
9668 #if 0
9669 /* If we ever use add_ranges_by_labels () for a single
9670 function section, all we have to do is to take out
9671 the #if 0 above. */
9672 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9673 ranges_by_label[lab_idx].begin,
9674 text_section_label,
9675 fmt, i * 2 * DWARF2_ADDR_SIZE);
9676 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9677 ranges_by_label[lab_idx].end,
9678 text_section_label, NULL);
9679 #endif
9680 }
9681 else
9682 {
9683 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9684 ranges_by_label[lab_idx].begin,
9685 fmt, i * 2 * DWARF2_ADDR_SIZE);
9686 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9687 ranges_by_label[lab_idx].end,
9688 NULL);
9689 }
9690 }
9691 else
9692 {
9693 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9694 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9695 fmt = start_fmt;
9696 }
9697 }
9698 }
9699
9700 /* Data structure containing information about input files. */
9701 struct file_info
9702 {
9703 const char *path; /* Complete file name. */
9704 const char *fname; /* File name part. */
9705 int length; /* Length of entire string. */
9706 struct dwarf_file_data * file_idx; /* Index in input file table. */
9707 int dir_idx; /* Index in directory table. */
9708 };
9709
9710 /* Data structure containing information about directories with source
9711 files. */
9712 struct dir_info
9713 {
9714 const char *path; /* Path including directory name. */
9715 int length; /* Path length. */
9716 int prefix; /* Index of directory entry which is a prefix. */
9717 int count; /* Number of files in this directory. */
9718 int dir_idx; /* Index of directory used as base. */
9719 };
9720
9721 /* Callback function for file_info comparison. We sort by looking at
9722 the directories in the path. */
9723
9724 static int
9725 file_info_cmp (const void *p1, const void *p2)
9726 {
9727 const struct file_info *const s1 = (const struct file_info *) p1;
9728 const struct file_info *const s2 = (const struct file_info *) p2;
9729 const unsigned char *cp1;
9730 const unsigned char *cp2;
9731
9732 /* Take care of file names without directories. We need to make sure that
9733 we return consistent values to qsort since some will get confused if
9734 we return the same value when identical operands are passed in opposite
9735 orders. So if neither has a directory, return 0 and otherwise return
9736 1 or -1 depending on which one has the directory. */
9737 if ((s1->path == s1->fname || s2->path == s2->fname))
9738 return (s2->path == s2->fname) - (s1->path == s1->fname);
9739
9740 cp1 = (const unsigned char *) s1->path;
9741 cp2 = (const unsigned char *) s2->path;
9742
9743 while (1)
9744 {
9745 ++cp1;
9746 ++cp2;
9747 /* Reached the end of the first path? If so, handle like above. */
9748 if ((cp1 == (const unsigned char *) s1->fname)
9749 || (cp2 == (const unsigned char *) s2->fname))
9750 return ((cp2 == (const unsigned char *) s2->fname)
9751 - (cp1 == (const unsigned char *) s1->fname));
9752
9753 /* Character of current path component the same? */
9754 else if (*cp1 != *cp2)
9755 return *cp1 - *cp2;
9756 }
9757 }
9758
9759 struct file_name_acquire_data
9760 {
9761 struct file_info *files;
9762 int used_files;
9763 int max_files;
9764 };
9765
9766 /* Traversal function for the hash table. */
9767
9768 static int
9769 file_name_acquire (void ** slot, void *data)
9770 {
9771 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9772 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9773 struct file_info *fi;
9774 const char *f;
9775
9776 gcc_assert (fnad->max_files >= d->emitted_number);
9777
9778 if (! d->emitted_number)
9779 return 1;
9780
9781 gcc_assert (fnad->max_files != fnad->used_files);
9782
9783 fi = fnad->files + fnad->used_files++;
9784
9785 /* Skip all leading "./". */
9786 f = d->filename;
9787 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9788 f += 2;
9789
9790 /* Create a new array entry. */
9791 fi->path = f;
9792 fi->length = strlen (f);
9793 fi->file_idx = d;
9794
9795 /* Search for the file name part. */
9796 f = strrchr (f, DIR_SEPARATOR);
9797 #if defined (DIR_SEPARATOR_2)
9798 {
9799 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9800
9801 if (g != NULL)
9802 {
9803 if (f == NULL || f < g)
9804 f = g;
9805 }
9806 }
9807 #endif
9808
9809 fi->fname = f == NULL ? fi->path : f + 1;
9810 return 1;
9811 }
9812
9813 /* Output the directory table and the file name table. We try to minimize
9814 the total amount of memory needed. A heuristic is used to avoid large
9815 slowdowns with many input files. */
9816
9817 static void
9818 output_file_names (void)
9819 {
9820 struct file_name_acquire_data fnad;
9821 int numfiles;
9822 struct file_info *files;
9823 struct dir_info *dirs;
9824 int *saved;
9825 int *savehere;
9826 int *backmap;
9827 int ndirs;
9828 int idx_offset;
9829 int i;
9830
9831 if (!last_emitted_file)
9832 {
9833 dw2_asm_output_data (1, 0, "End directory table");
9834 dw2_asm_output_data (1, 0, "End file name table");
9835 return;
9836 }
9837
9838 numfiles = last_emitted_file->emitted_number;
9839
9840 /* Allocate the various arrays we need. */
9841 files = XALLOCAVEC (struct file_info, numfiles);
9842 dirs = XALLOCAVEC (struct dir_info, numfiles);
9843
9844 fnad.files = files;
9845 fnad.used_files = 0;
9846 fnad.max_files = numfiles;
9847 htab_traverse (file_table, file_name_acquire, &fnad);
9848 gcc_assert (fnad.used_files == fnad.max_files);
9849
9850 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9851
9852 /* Find all the different directories used. */
9853 dirs[0].path = files[0].path;
9854 dirs[0].length = files[0].fname - files[0].path;
9855 dirs[0].prefix = -1;
9856 dirs[0].count = 1;
9857 dirs[0].dir_idx = 0;
9858 files[0].dir_idx = 0;
9859 ndirs = 1;
9860
9861 for (i = 1; i < numfiles; i++)
9862 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9863 && memcmp (dirs[ndirs - 1].path, files[i].path,
9864 dirs[ndirs - 1].length) == 0)
9865 {
9866 /* Same directory as last entry. */
9867 files[i].dir_idx = ndirs - 1;
9868 ++dirs[ndirs - 1].count;
9869 }
9870 else
9871 {
9872 int j;
9873
9874 /* This is a new directory. */
9875 dirs[ndirs].path = files[i].path;
9876 dirs[ndirs].length = files[i].fname - files[i].path;
9877 dirs[ndirs].count = 1;
9878 dirs[ndirs].dir_idx = ndirs;
9879 files[i].dir_idx = ndirs;
9880
9881 /* Search for a prefix. */
9882 dirs[ndirs].prefix = -1;
9883 for (j = 0; j < ndirs; j++)
9884 if (dirs[j].length < dirs[ndirs].length
9885 && dirs[j].length > 1
9886 && (dirs[ndirs].prefix == -1
9887 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9888 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9889 dirs[ndirs].prefix = j;
9890
9891 ++ndirs;
9892 }
9893
9894 /* Now to the actual work. We have to find a subset of the directories which
9895 allow expressing the file name using references to the directory table
9896 with the least amount of characters. We do not do an exhaustive search
9897 where we would have to check out every combination of every single
9898 possible prefix. Instead we use a heuristic which provides nearly optimal
9899 results in most cases and never is much off. */
9900 saved = XALLOCAVEC (int, ndirs);
9901 savehere = XALLOCAVEC (int, ndirs);
9902
9903 memset (saved, '\0', ndirs * sizeof (saved[0]));
9904 for (i = 0; i < ndirs; i++)
9905 {
9906 int j;
9907 int total;
9908
9909 /* We can always save some space for the current directory. But this
9910 does not mean it will be enough to justify adding the directory. */
9911 savehere[i] = dirs[i].length;
9912 total = (savehere[i] - saved[i]) * dirs[i].count;
9913
9914 for (j = i + 1; j < ndirs; j++)
9915 {
9916 savehere[j] = 0;
9917 if (saved[j] < dirs[i].length)
9918 {
9919 /* Determine whether the dirs[i] path is a prefix of the
9920 dirs[j] path. */
9921 int k;
9922
9923 k = dirs[j].prefix;
9924 while (k != -1 && k != (int) i)
9925 k = dirs[k].prefix;
9926
9927 if (k == (int) i)
9928 {
9929 /* Yes it is. We can possibly save some memory by
9930 writing the filenames in dirs[j] relative to
9931 dirs[i]. */
9932 savehere[j] = dirs[i].length;
9933 total += (savehere[j] - saved[j]) * dirs[j].count;
9934 }
9935 }
9936 }
9937
9938 /* Check whether we can save enough to justify adding the dirs[i]
9939 directory. */
9940 if (total > dirs[i].length + 1)
9941 {
9942 /* It's worthwhile adding. */
9943 for (j = i; j < ndirs; j++)
9944 if (savehere[j] > 0)
9945 {
9946 /* Remember how much we saved for this directory so far. */
9947 saved[j] = savehere[j];
9948
9949 /* Remember the prefix directory. */
9950 dirs[j].dir_idx = i;
9951 }
9952 }
9953 }
9954
9955 /* Emit the directory name table. */
9956 idx_offset = dirs[0].length > 0 ? 1 : 0;
9957 for (i = 1 - idx_offset; i < ndirs; i++)
9958 dw2_asm_output_nstring (dirs[i].path,
9959 dirs[i].length
9960 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9961 "Directory Entry: %#x", i + idx_offset);
9962
9963 dw2_asm_output_data (1, 0, "End directory table");
9964
9965 /* We have to emit them in the order of emitted_number since that's
9966 used in the debug info generation. To do this efficiently we
9967 generate a back-mapping of the indices first. */
9968 backmap = XALLOCAVEC (int, numfiles);
9969 for (i = 0; i < numfiles; i++)
9970 backmap[files[i].file_idx->emitted_number - 1] = i;
9971
9972 /* Now write all the file names. */
9973 for (i = 0; i < numfiles; i++)
9974 {
9975 int file_idx = backmap[i];
9976 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9977
9978 #ifdef VMS_DEBUGGING_INFO
9979 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9980
9981 /* Setting these fields can lead to debugger miscomparisons,
9982 but VMS Debug requires them to be set correctly. */
9983
9984 int ver;
9985 long long cdt;
9986 long siz;
9987 int maxfilelen = strlen (files[file_idx].path)
9988 + dirs[dir_idx].length
9989 + MAX_VMS_VERSION_LEN + 1;
9990 char *filebuf = XALLOCAVEC (char, maxfilelen);
9991
9992 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9993 snprintf (filebuf, maxfilelen, "%s;%d",
9994 files[file_idx].path + dirs[dir_idx].length, ver);
9995
9996 dw2_asm_output_nstring
9997 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9998
9999 /* Include directory index. */
10000 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10001
10002 /* Modification time. */
10003 dw2_asm_output_data_uleb128
10004 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10005 ? cdt : 0,
10006 NULL);
10007
10008 /* File length in bytes. */
10009 dw2_asm_output_data_uleb128
10010 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10011 ? siz : 0,
10012 NULL);
10013 #else
10014 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10015 "File Entry: %#x", (unsigned) i + 1);
10016
10017 /* Include directory index. */
10018 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10019
10020 /* Modification time. */
10021 dw2_asm_output_data_uleb128 (0, NULL);
10022
10023 /* File length in bytes. */
10024 dw2_asm_output_data_uleb128 (0, NULL);
10025 #endif /* VMS_DEBUGGING_INFO */
10026 }
10027
10028 dw2_asm_output_data (1, 0, "End file name table");
10029 }
10030
10031
10032 /* Output one line number table into the .debug_line section. */
10033
10034 static void
10035 output_one_line_info_table (dw_line_info_table *table)
10036 {
10037 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10038 unsigned int current_line = 1;
10039 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10040 dw_line_info_entry *ent;
10041 size_t i;
10042
10043 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10044 {
10045 switch (ent->opcode)
10046 {
10047 case LI_set_address:
10048 /* ??? Unfortunately, we have little choice here currently, and
10049 must always use the most general form. GCC does not know the
10050 address delta itself, so we can't use DW_LNS_advance_pc. Many
10051 ports do have length attributes which will give an upper bound
10052 on the address range. We could perhaps use length attributes
10053 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10054 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10055
10056 /* This can handle any delta. This takes
10057 4+DWARF2_ADDR_SIZE bytes. */
10058 dw2_asm_output_data (1, 0, "set address %s", line_label);
10059 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10060 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10061 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10062 break;
10063
10064 case LI_set_line:
10065 if (ent->val == current_line)
10066 {
10067 /* We still need to start a new row, so output a copy insn. */
10068 dw2_asm_output_data (1, DW_LNS_copy,
10069 "copy line %u", current_line);
10070 }
10071 else
10072 {
10073 int line_offset = ent->val - current_line;
10074 int line_delta = line_offset - DWARF_LINE_BASE;
10075
10076 current_line = ent->val;
10077 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10078 {
10079 /* This can handle deltas from -10 to 234, using the current
10080 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10081 This takes 1 byte. */
10082 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10083 "line %u", current_line);
10084 }
10085 else
10086 {
10087 /* This can handle any delta. This takes at least 4 bytes,
10088 depending on the value being encoded. */
10089 dw2_asm_output_data (1, DW_LNS_advance_line,
10090 "advance to line %u", current_line);
10091 dw2_asm_output_data_sleb128 (line_offset, NULL);
10092 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10093 }
10094 }
10095 break;
10096
10097 case LI_set_file:
10098 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10099 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10100 break;
10101
10102 case LI_set_column:
10103 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10104 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10105 break;
10106
10107 case LI_negate_stmt:
10108 current_is_stmt = !current_is_stmt;
10109 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10110 "is_stmt %d", current_is_stmt);
10111 break;
10112
10113 case LI_set_prologue_end:
10114 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10115 "set prologue end");
10116 break;
10117
10118 case LI_set_epilogue_begin:
10119 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10120 "set epilogue begin");
10121 break;
10122
10123 case LI_set_discriminator:
10124 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10125 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10126 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10127 dw2_asm_output_data_uleb128 (ent->val, NULL);
10128 break;
10129 }
10130 }
10131
10132 /* Emit debug info for the address of the end of the table. */
10133 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10134 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10135 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10136 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10137
10138 dw2_asm_output_data (1, 0, "end sequence");
10139 dw2_asm_output_data_uleb128 (1, NULL);
10140 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10141 }
10142
10143 /* Output the source line number correspondence information. This
10144 information goes into the .debug_line section. */
10145
10146 static void
10147 output_line_info (bool prologue_only)
10148 {
10149 char l1[20], l2[20], p1[20], p2[20];
10150 int ver = dwarf_version;
10151 bool saw_one = false;
10152 int opc;
10153
10154 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10155 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10156 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10157 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10158
10159 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10160 dw2_asm_output_data (4, 0xffffffff,
10161 "Initial length escape value indicating 64-bit DWARF extension");
10162 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10163 "Length of Source Line Info");
10164 ASM_OUTPUT_LABEL (asm_out_file, l1);
10165
10166 dw2_asm_output_data (2, ver, "DWARF Version");
10167 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10168 ASM_OUTPUT_LABEL (asm_out_file, p1);
10169
10170 /* Define the architecture-dependent minimum instruction length (in bytes).
10171 In this implementation of DWARF, this field is used for information
10172 purposes only. Since GCC generates assembly language, we have no
10173 a priori knowledge of how many instruction bytes are generated for each
10174 source line, and therefore can use only the DW_LNE_set_address and
10175 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10176 this as '1', which is "correct enough" for all architectures,
10177 and don't let the target override. */
10178 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10179
10180 if (ver >= 4)
10181 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10182 "Maximum Operations Per Instruction");
10183 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10184 "Default is_stmt_start flag");
10185 dw2_asm_output_data (1, DWARF_LINE_BASE,
10186 "Line Base Value (Special Opcodes)");
10187 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10188 "Line Range Value (Special Opcodes)");
10189 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10190 "Special Opcode Base");
10191
10192 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10193 {
10194 int n_op_args;
10195 switch (opc)
10196 {
10197 case DW_LNS_advance_pc:
10198 case DW_LNS_advance_line:
10199 case DW_LNS_set_file:
10200 case DW_LNS_set_column:
10201 case DW_LNS_fixed_advance_pc:
10202 case DW_LNS_set_isa:
10203 n_op_args = 1;
10204 break;
10205 default:
10206 n_op_args = 0;
10207 break;
10208 }
10209
10210 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10211 opc, n_op_args);
10212 }
10213
10214 /* Write out the information about the files we use. */
10215 output_file_names ();
10216 ASM_OUTPUT_LABEL (asm_out_file, p2);
10217 if (prologue_only)
10218 {
10219 /* Output the marker for the end of the line number info. */
10220 ASM_OUTPUT_LABEL (asm_out_file, l2);
10221 return;
10222 }
10223
10224 if (separate_line_info)
10225 {
10226 dw_line_info_table *table;
10227 size_t i;
10228
10229 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10230 if (table->in_use)
10231 {
10232 output_one_line_info_table (table);
10233 saw_one = true;
10234 }
10235 }
10236 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10237 {
10238 output_one_line_info_table (cold_text_section_line_info);
10239 saw_one = true;
10240 }
10241
10242 /* ??? Some Darwin linkers crash on a .debug_line section with no
10243 sequences. Further, merely a DW_LNE_end_sequence entry is not
10244 sufficient -- the address column must also be initialized.
10245 Make sure to output at least one set_address/end_sequence pair,
10246 choosing .text since that section is always present. */
10247 if (text_section_line_info->in_use || !saw_one)
10248 output_one_line_info_table (text_section_line_info);
10249
10250 /* Output the marker for the end of the line number info. */
10251 ASM_OUTPUT_LABEL (asm_out_file, l2);
10252 }
10253 \f
10254 /* Given a pointer to a tree node for some base type, return a pointer to
10255 a DIE that describes the given type.
10256
10257 This routine must only be called for GCC type nodes that correspond to
10258 Dwarf base (fundamental) types. */
10259
10260 static dw_die_ref
10261 base_type_die (tree type)
10262 {
10263 dw_die_ref base_type_result;
10264 enum dwarf_type encoding;
10265
10266 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10267 return 0;
10268
10269 /* If this is a subtype that should not be emitted as a subrange type,
10270 use the base type. See subrange_type_for_debug_p. */
10271 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10272 type = TREE_TYPE (type);
10273
10274 switch (TREE_CODE (type))
10275 {
10276 case INTEGER_TYPE:
10277 if ((dwarf_version >= 4 || !dwarf_strict)
10278 && TYPE_NAME (type)
10279 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10280 && DECL_IS_BUILTIN (TYPE_NAME (type))
10281 && DECL_NAME (TYPE_NAME (type)))
10282 {
10283 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10284 if (strcmp (name, "char16_t") == 0
10285 || strcmp (name, "char32_t") == 0)
10286 {
10287 encoding = DW_ATE_UTF;
10288 break;
10289 }
10290 }
10291 if (TYPE_STRING_FLAG (type))
10292 {
10293 if (TYPE_UNSIGNED (type))
10294 encoding = DW_ATE_unsigned_char;
10295 else
10296 encoding = DW_ATE_signed_char;
10297 }
10298 else if (TYPE_UNSIGNED (type))
10299 encoding = DW_ATE_unsigned;
10300 else
10301 encoding = DW_ATE_signed;
10302 break;
10303
10304 case REAL_TYPE:
10305 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10306 {
10307 if (dwarf_version >= 3 || !dwarf_strict)
10308 encoding = DW_ATE_decimal_float;
10309 else
10310 encoding = DW_ATE_lo_user;
10311 }
10312 else
10313 encoding = DW_ATE_float;
10314 break;
10315
10316 case FIXED_POINT_TYPE:
10317 if (!(dwarf_version >= 3 || !dwarf_strict))
10318 encoding = DW_ATE_lo_user;
10319 else if (TYPE_UNSIGNED (type))
10320 encoding = DW_ATE_unsigned_fixed;
10321 else
10322 encoding = DW_ATE_signed_fixed;
10323 break;
10324
10325 /* Dwarf2 doesn't know anything about complex ints, so use
10326 a user defined type for it. */
10327 case COMPLEX_TYPE:
10328 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10329 encoding = DW_ATE_complex_float;
10330 else
10331 encoding = DW_ATE_lo_user;
10332 break;
10333
10334 case BOOLEAN_TYPE:
10335 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10336 encoding = DW_ATE_boolean;
10337 break;
10338
10339 default:
10340 /* No other TREE_CODEs are Dwarf fundamental types. */
10341 gcc_unreachable ();
10342 }
10343
10344 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10345
10346 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10347 int_size_in_bytes (type));
10348 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10349 add_pubtype (type, base_type_result);
10350
10351 return base_type_result;
10352 }
10353
10354 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10355 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10356
10357 static inline int
10358 is_base_type (tree type)
10359 {
10360 switch (TREE_CODE (type))
10361 {
10362 case ERROR_MARK:
10363 case VOID_TYPE:
10364 case INTEGER_TYPE:
10365 case REAL_TYPE:
10366 case FIXED_POINT_TYPE:
10367 case COMPLEX_TYPE:
10368 case BOOLEAN_TYPE:
10369 return 1;
10370
10371 case ARRAY_TYPE:
10372 case RECORD_TYPE:
10373 case UNION_TYPE:
10374 case QUAL_UNION_TYPE:
10375 case ENUMERAL_TYPE:
10376 case FUNCTION_TYPE:
10377 case METHOD_TYPE:
10378 case POINTER_TYPE:
10379 case REFERENCE_TYPE:
10380 case NULLPTR_TYPE:
10381 case OFFSET_TYPE:
10382 case LANG_TYPE:
10383 case VECTOR_TYPE:
10384 return 0;
10385
10386 default:
10387 gcc_unreachable ();
10388 }
10389
10390 return 0;
10391 }
10392
10393 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10394 node, return the size in bits for the type if it is a constant, or else
10395 return the alignment for the type if the type's size is not constant, or
10396 else return BITS_PER_WORD if the type actually turns out to be an
10397 ERROR_MARK node. */
10398
10399 static inline unsigned HOST_WIDE_INT
10400 simple_type_size_in_bits (const_tree type)
10401 {
10402 if (TREE_CODE (type) == ERROR_MARK)
10403 return BITS_PER_WORD;
10404 else if (TYPE_SIZE (type) == NULL_TREE)
10405 return 0;
10406 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10407 return tree_to_uhwi (TYPE_SIZE (type));
10408 else
10409 return TYPE_ALIGN (type);
10410 }
10411
10412 /* Similarly, but return an offset_int instead of UHWI. */
10413
10414 static inline offset_int
10415 offset_int_type_size_in_bits (const_tree type)
10416 {
10417 if (TREE_CODE (type) == ERROR_MARK)
10418 return BITS_PER_WORD;
10419 else if (TYPE_SIZE (type) == NULL_TREE)
10420 return 0;
10421 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10422 return wi::to_offset (TYPE_SIZE (type));
10423 else
10424 return TYPE_ALIGN (type);
10425 }
10426
10427 /* Given a pointer to a tree node for a subrange type, return a pointer
10428 to a DIE that describes the given type. */
10429
10430 static dw_die_ref
10431 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10432 {
10433 dw_die_ref subrange_die;
10434 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10435
10436 if (context_die == NULL)
10437 context_die = comp_unit_die ();
10438
10439 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10440
10441 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10442 {
10443 /* The size of the subrange type and its base type do not match,
10444 so we need to generate a size attribute for the subrange type. */
10445 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10446 }
10447
10448 if (low)
10449 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10450 if (high)
10451 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10452
10453 return subrange_die;
10454 }
10455
10456 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10457 entry that chains various modifiers in front of the given type. */
10458
10459 static dw_die_ref
10460 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10461 dw_die_ref context_die)
10462 {
10463 enum tree_code code = TREE_CODE (type);
10464 dw_die_ref mod_type_die;
10465 dw_die_ref sub_die = NULL;
10466 tree item_type = NULL;
10467 tree qualified_type;
10468 tree name, low, high;
10469 dw_die_ref mod_scope;
10470
10471 if (code == ERROR_MARK)
10472 return NULL;
10473
10474 /* See if we already have the appropriately qualified variant of
10475 this type. */
10476 qualified_type
10477 = get_qualified_type (type,
10478 ((is_const_type ? TYPE_QUAL_CONST : 0)
10479 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10480
10481 if (qualified_type == sizetype
10482 && TYPE_NAME (qualified_type)
10483 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10484 {
10485 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10486
10487 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10488 && TYPE_PRECISION (t)
10489 == TYPE_PRECISION (qualified_type)
10490 && TYPE_UNSIGNED (t)
10491 == TYPE_UNSIGNED (qualified_type));
10492 qualified_type = t;
10493 }
10494
10495 /* If we do, then we can just use its DIE, if it exists. */
10496 if (qualified_type)
10497 {
10498 mod_type_die = lookup_type_die (qualified_type);
10499 if (mod_type_die)
10500 return mod_type_die;
10501 }
10502
10503 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10504
10505 /* Handle C typedef types. */
10506 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10507 && !DECL_ARTIFICIAL (name))
10508 {
10509 tree dtype = TREE_TYPE (name);
10510
10511 if (qualified_type == dtype)
10512 {
10513 /* For a named type, use the typedef. */
10514 gen_type_die (qualified_type, context_die);
10515 return lookup_type_die (qualified_type);
10516 }
10517 else if (is_const_type < TYPE_READONLY (dtype)
10518 || is_volatile_type < TYPE_VOLATILE (dtype)
10519 || (is_const_type <= TYPE_READONLY (dtype)
10520 && is_volatile_type <= TYPE_VOLATILE (dtype)
10521 && DECL_ORIGINAL_TYPE (name) != type))
10522 /* cv-unqualified version of named type. Just use the unnamed
10523 type to which it refers. */
10524 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10525 is_const_type, is_volatile_type,
10526 context_die);
10527 /* Else cv-qualified version of named type; fall through. */
10528 }
10529
10530 mod_scope = scope_die_for (type, context_die);
10531
10532 if (is_const_type
10533 /* If both is_const_type and is_volatile_type, prefer the path
10534 which leads to a qualified type. */
10535 && (!is_volatile_type
10536 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10537 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10538 {
10539 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10540 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10541 }
10542 else if (is_volatile_type)
10543 {
10544 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10545 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10546 }
10547 else if (code == POINTER_TYPE)
10548 {
10549 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10550 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10551 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10552 item_type = TREE_TYPE (type);
10553 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10554 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10555 TYPE_ADDR_SPACE (item_type));
10556 }
10557 else if (code == REFERENCE_TYPE)
10558 {
10559 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10560 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10561 type);
10562 else
10563 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10564 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10565 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10566 item_type = TREE_TYPE (type);
10567 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10568 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10569 TYPE_ADDR_SPACE (item_type));
10570 }
10571 else if (code == INTEGER_TYPE
10572 && TREE_TYPE (type) != NULL_TREE
10573 && subrange_type_for_debug_p (type, &low, &high))
10574 {
10575 mod_type_die = subrange_type_die (type, low, high, context_die);
10576 item_type = TREE_TYPE (type);
10577 }
10578 else if (is_base_type (type))
10579 mod_type_die = base_type_die (type);
10580 else
10581 {
10582 gen_type_die (type, context_die);
10583
10584 /* We have to get the type_main_variant here (and pass that to the
10585 `lookup_type_die' routine) because the ..._TYPE node we have
10586 might simply be a *copy* of some original type node (where the
10587 copy was created to help us keep track of typedef names) and
10588 that copy might have a different TYPE_UID from the original
10589 ..._TYPE node. */
10590 if (TREE_CODE (type) != VECTOR_TYPE)
10591 return lookup_type_die (type_main_variant (type));
10592 else
10593 /* Vectors have the debugging information in the type,
10594 not the main variant. */
10595 return lookup_type_die (type);
10596 }
10597
10598 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10599 don't output a DW_TAG_typedef, since there isn't one in the
10600 user's program; just attach a DW_AT_name to the type.
10601 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10602 if the base type already has the same name. */
10603 if (name
10604 && ((TREE_CODE (name) != TYPE_DECL
10605 && (qualified_type == TYPE_MAIN_VARIANT (type)
10606 || (!is_const_type && !is_volatile_type)))
10607 || (TREE_CODE (name) == TYPE_DECL
10608 && TREE_TYPE (name) == qualified_type
10609 && DECL_NAME (name))))
10610 {
10611 if (TREE_CODE (name) == TYPE_DECL)
10612 /* Could just call add_name_and_src_coords_attributes here,
10613 but since this is a builtin type it doesn't have any
10614 useful source coordinates anyway. */
10615 name = DECL_NAME (name);
10616 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10617 }
10618 /* This probably indicates a bug. */
10619 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10620 {
10621 name = TYPE_NAME (type);
10622 if (name
10623 && TREE_CODE (name) == TYPE_DECL)
10624 name = DECL_NAME (name);
10625 add_name_attribute (mod_type_die,
10626 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10627 }
10628
10629 if (qualified_type)
10630 equate_type_number_to_die (qualified_type, mod_type_die);
10631
10632 if (item_type)
10633 /* We must do this after the equate_type_number_to_die call, in case
10634 this is a recursive type. This ensures that the modified_type_die
10635 recursion will terminate even if the type is recursive. Recursive
10636 types are possible in Ada. */
10637 sub_die = modified_type_die (item_type,
10638 TYPE_READONLY (item_type),
10639 TYPE_VOLATILE (item_type),
10640 context_die);
10641
10642 if (sub_die != NULL)
10643 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10644
10645 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10646 if (TYPE_ARTIFICIAL (type))
10647 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10648
10649 return mod_type_die;
10650 }
10651
10652 /* Generate DIEs for the generic parameters of T.
10653 T must be either a generic type or a generic function.
10654 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10655
10656 static void
10657 gen_generic_params_dies (tree t)
10658 {
10659 tree parms, args;
10660 int parms_num, i;
10661 dw_die_ref die = NULL;
10662 int non_default;
10663
10664 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10665 return;
10666
10667 if (TYPE_P (t))
10668 die = lookup_type_die (t);
10669 else if (DECL_P (t))
10670 die = lookup_decl_die (t);
10671
10672 gcc_assert (die);
10673
10674 parms = lang_hooks.get_innermost_generic_parms (t);
10675 if (!parms)
10676 /* T has no generic parameter. It means T is neither a generic type
10677 or function. End of story. */
10678 return;
10679
10680 parms_num = TREE_VEC_LENGTH (parms);
10681 args = lang_hooks.get_innermost_generic_args (t);
10682 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10683 non_default = int_cst_value (TREE_CHAIN (args));
10684 else
10685 non_default = TREE_VEC_LENGTH (args);
10686 for (i = 0; i < parms_num; i++)
10687 {
10688 tree parm, arg, arg_pack_elems;
10689 dw_die_ref parm_die;
10690
10691 parm = TREE_VEC_ELT (parms, i);
10692 arg = TREE_VEC_ELT (args, i);
10693 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10694 gcc_assert (parm && TREE_VALUE (parm) && arg);
10695
10696 if (parm && TREE_VALUE (parm) && arg)
10697 {
10698 /* If PARM represents a template parameter pack,
10699 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10700 by DW_TAG_template_*_parameter DIEs for the argument
10701 pack elements of ARG. Note that ARG would then be
10702 an argument pack. */
10703 if (arg_pack_elems)
10704 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10705 arg_pack_elems,
10706 die);
10707 else
10708 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10709 true /* emit name */, die);
10710 if (i >= non_default)
10711 add_AT_flag (parm_die, DW_AT_default_value, 1);
10712 }
10713 }
10714 }
10715
10716 /* Create and return a DIE for PARM which should be
10717 the representation of a generic type parameter.
10718 For instance, in the C++ front end, PARM would be a template parameter.
10719 ARG is the argument to PARM.
10720 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10721 name of the PARM.
10722 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10723 as a child node. */
10724
10725 static dw_die_ref
10726 generic_parameter_die (tree parm, tree arg,
10727 bool emit_name_p,
10728 dw_die_ref parent_die)
10729 {
10730 dw_die_ref tmpl_die = NULL;
10731 const char *name = NULL;
10732
10733 if (!parm || !DECL_NAME (parm) || !arg)
10734 return NULL;
10735
10736 /* We support non-type generic parameters and arguments,
10737 type generic parameters and arguments, as well as
10738 generic generic parameters (a.k.a. template template parameters in C++)
10739 and arguments. */
10740 if (TREE_CODE (parm) == PARM_DECL)
10741 /* PARM is a nontype generic parameter */
10742 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10743 else if (TREE_CODE (parm) == TYPE_DECL)
10744 /* PARM is a type generic parameter. */
10745 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10746 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10747 /* PARM is a generic generic parameter.
10748 Its DIE is a GNU extension. It shall have a
10749 DW_AT_name attribute to represent the name of the template template
10750 parameter, and a DW_AT_GNU_template_name attribute to represent the
10751 name of the template template argument. */
10752 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10753 parent_die, parm);
10754 else
10755 gcc_unreachable ();
10756
10757 if (tmpl_die)
10758 {
10759 tree tmpl_type;
10760
10761 /* If PARM is a generic parameter pack, it means we are
10762 emitting debug info for a template argument pack element.
10763 In other terms, ARG is a template argument pack element.
10764 In that case, we don't emit any DW_AT_name attribute for
10765 the die. */
10766 if (emit_name_p)
10767 {
10768 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10769 gcc_assert (name);
10770 add_AT_string (tmpl_die, DW_AT_name, name);
10771 }
10772
10773 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10774 {
10775 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10776 TMPL_DIE should have a child DW_AT_type attribute that is set
10777 to the type of the argument to PARM, which is ARG.
10778 If PARM is a type generic parameter, TMPL_DIE should have a
10779 child DW_AT_type that is set to ARG. */
10780 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10781 add_type_attribute (tmpl_die, tmpl_type, 0,
10782 TREE_THIS_VOLATILE (tmpl_type),
10783 parent_die);
10784 }
10785 else
10786 {
10787 /* So TMPL_DIE is a DIE representing a
10788 a generic generic template parameter, a.k.a template template
10789 parameter in C++ and arg is a template. */
10790
10791 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10792 to the name of the argument. */
10793 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10794 if (name)
10795 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10796 }
10797
10798 if (TREE_CODE (parm) == PARM_DECL)
10799 /* So PARM is a non-type generic parameter.
10800 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10801 attribute of TMPL_DIE which value represents the value
10802 of ARG.
10803 We must be careful here:
10804 The value of ARG might reference some function decls.
10805 We might currently be emitting debug info for a generic
10806 type and types are emitted before function decls, we don't
10807 know if the function decls referenced by ARG will actually be
10808 emitted after cgraph computations.
10809 So must defer the generation of the DW_AT_const_value to
10810 after cgraph is ready. */
10811 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10812 }
10813
10814 return tmpl_die;
10815 }
10816
10817 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10818 PARM_PACK must be a template parameter pack. The returned DIE
10819 will be child DIE of PARENT_DIE. */
10820
10821 static dw_die_ref
10822 template_parameter_pack_die (tree parm_pack,
10823 tree parm_pack_args,
10824 dw_die_ref parent_die)
10825 {
10826 dw_die_ref die;
10827 int j;
10828
10829 gcc_assert (parent_die && parm_pack);
10830
10831 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10832 add_name_and_src_coords_attributes (die, parm_pack);
10833 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10834 generic_parameter_die (parm_pack,
10835 TREE_VEC_ELT (parm_pack_args, j),
10836 false /* Don't emit DW_AT_name */,
10837 die);
10838 return die;
10839 }
10840
10841 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10842 an enumerated type. */
10843
10844 static inline int
10845 type_is_enum (const_tree type)
10846 {
10847 return TREE_CODE (type) == ENUMERAL_TYPE;
10848 }
10849
10850 /* Return the DBX register number described by a given RTL node. */
10851
10852 static unsigned int
10853 dbx_reg_number (const_rtx rtl)
10854 {
10855 unsigned regno = REGNO (rtl);
10856
10857 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10858
10859 #ifdef LEAF_REG_REMAP
10860 if (crtl->uses_only_leaf_regs)
10861 {
10862 int leaf_reg = LEAF_REG_REMAP (regno);
10863 if (leaf_reg != -1)
10864 regno = (unsigned) leaf_reg;
10865 }
10866 #endif
10867
10868 regno = DBX_REGISTER_NUMBER (regno);
10869 gcc_assert (regno != INVALID_REGNUM);
10870 return regno;
10871 }
10872
10873 /* Optionally add a DW_OP_piece term to a location description expression.
10874 DW_OP_piece is only added if the location description expression already
10875 doesn't end with DW_OP_piece. */
10876
10877 static void
10878 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10879 {
10880 dw_loc_descr_ref loc;
10881
10882 if (*list_head != NULL)
10883 {
10884 /* Find the end of the chain. */
10885 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10886 ;
10887
10888 if (loc->dw_loc_opc != DW_OP_piece)
10889 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10890 }
10891 }
10892
10893 /* Return a location descriptor that designates a machine register or
10894 zero if there is none. */
10895
10896 static dw_loc_descr_ref
10897 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10898 {
10899 rtx regs;
10900
10901 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10902 return 0;
10903
10904 /* We only use "frame base" when we're sure we're talking about the
10905 post-prologue local stack frame. We do this by *not* running
10906 register elimination until this point, and recognizing the special
10907 argument pointer and soft frame pointer rtx's.
10908 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10909 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10910 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10911 {
10912 dw_loc_descr_ref result = NULL;
10913
10914 if (dwarf_version >= 4 || !dwarf_strict)
10915 {
10916 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10917 initialized);
10918 if (result)
10919 add_loc_descr (&result,
10920 new_loc_descr (DW_OP_stack_value, 0, 0));
10921 }
10922 return result;
10923 }
10924
10925 regs = targetm.dwarf_register_span (rtl);
10926
10927 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10928 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10929 else
10930 {
10931 unsigned int dbx_regnum = dbx_reg_number (rtl);
10932 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10933 return 0;
10934 return one_reg_loc_descriptor (dbx_regnum, initialized);
10935 }
10936 }
10937
10938 /* Return a location descriptor that designates a machine register for
10939 a given hard register number. */
10940
10941 static dw_loc_descr_ref
10942 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10943 {
10944 dw_loc_descr_ref reg_loc_descr;
10945
10946 if (regno <= 31)
10947 reg_loc_descr
10948 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10949 else
10950 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10951
10952 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10953 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10954
10955 return reg_loc_descr;
10956 }
10957
10958 /* Given an RTL of a register, return a location descriptor that
10959 designates a value that spans more than one register. */
10960
10961 static dw_loc_descr_ref
10962 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10963 enum var_init_status initialized)
10964 {
10965 int size, i;
10966 dw_loc_descr_ref loc_result = NULL;
10967
10968 /* Simple, contiguous registers. */
10969 if (regs == NULL_RTX)
10970 {
10971 unsigned reg = REGNO (rtl);
10972 int nregs;
10973
10974 #ifdef LEAF_REG_REMAP
10975 if (crtl->uses_only_leaf_regs)
10976 {
10977 int leaf_reg = LEAF_REG_REMAP (reg);
10978 if (leaf_reg != -1)
10979 reg = (unsigned) leaf_reg;
10980 }
10981 #endif
10982
10983 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10984 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10985
10986 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10987
10988 loc_result = NULL;
10989 while (nregs--)
10990 {
10991 dw_loc_descr_ref t;
10992
10993 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10994 VAR_INIT_STATUS_INITIALIZED);
10995 add_loc_descr (&loc_result, t);
10996 add_loc_descr_op_piece (&loc_result, size);
10997 ++reg;
10998 }
10999 return loc_result;
11000 }
11001
11002 /* Now onto stupid register sets in non contiguous locations. */
11003
11004 gcc_assert (GET_CODE (regs) == PARALLEL);
11005
11006 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11007 loc_result = NULL;
11008
11009 for (i = 0; i < XVECLEN (regs, 0); ++i)
11010 {
11011 dw_loc_descr_ref t;
11012
11013 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11014 VAR_INIT_STATUS_INITIALIZED);
11015 add_loc_descr (&loc_result, t);
11016 add_loc_descr_op_piece (&loc_result, size);
11017 }
11018
11019 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11020 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11021 return loc_result;
11022 }
11023
11024 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11025
11026 /* Return a location descriptor that designates a constant i,
11027 as a compound operation from constant (i >> shift), constant shift
11028 and DW_OP_shl. */
11029
11030 static dw_loc_descr_ref
11031 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11032 {
11033 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11034 add_loc_descr (&ret, int_loc_descriptor (shift));
11035 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11036 return ret;
11037 }
11038
11039 /* Return a location descriptor that designates a constant. */
11040
11041 static dw_loc_descr_ref
11042 int_loc_descriptor (HOST_WIDE_INT i)
11043 {
11044 enum dwarf_location_atom op;
11045
11046 /* Pick the smallest representation of a constant, rather than just
11047 defaulting to the LEB encoding. */
11048 if (i >= 0)
11049 {
11050 int clz = clz_hwi (i);
11051 int ctz = ctz_hwi (i);
11052 if (i <= 31)
11053 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11054 else if (i <= 0xff)
11055 op = DW_OP_const1u;
11056 else if (i <= 0xffff)
11057 op = DW_OP_const2u;
11058 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11059 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11060 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11061 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11062 while DW_OP_const4u is 5 bytes. */
11063 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11064 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11065 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11066 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11067 while DW_OP_const4u is 5 bytes. */
11068 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11069 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11070 op = DW_OP_const4u;
11071 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11072 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11073 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11074 while DW_OP_constu of constant >= 0x100000000 takes at least
11075 6 bytes. */
11076 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11077 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11078 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11079 >= HOST_BITS_PER_WIDE_INT)
11080 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11081 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11082 while DW_OP_constu takes in this case at least 6 bytes. */
11083 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11084 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11085 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11086 && size_of_uleb128 (i) > 6)
11087 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11088 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11089 else
11090 op = DW_OP_constu;
11091 }
11092 else
11093 {
11094 if (i >= -0x80)
11095 op = DW_OP_const1s;
11096 else if (i >= -0x8000)
11097 op = DW_OP_const2s;
11098 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11099 {
11100 if (size_of_int_loc_descriptor (i) < 5)
11101 {
11102 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11103 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11104 return ret;
11105 }
11106 op = DW_OP_const4s;
11107 }
11108 else
11109 {
11110 if (size_of_int_loc_descriptor (i)
11111 < (unsigned long) 1 + size_of_sleb128 (i))
11112 {
11113 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11114 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11115 return ret;
11116 }
11117 op = DW_OP_consts;
11118 }
11119 }
11120
11121 return new_loc_descr (op, i, 0);
11122 }
11123
11124 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11125 without actually allocating it. */
11126
11127 static unsigned long
11128 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11129 {
11130 return size_of_int_loc_descriptor (i >> shift)
11131 + size_of_int_loc_descriptor (shift)
11132 + 1;
11133 }
11134
11135 /* Return size_of_locs (int_loc_descriptor (i)) without
11136 actually allocating it. */
11137
11138 static unsigned long
11139 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11140 {
11141 unsigned long s;
11142
11143 if (i >= 0)
11144 {
11145 int clz, ctz;
11146 if (i <= 31)
11147 return 1;
11148 else if (i <= 0xff)
11149 return 2;
11150 else if (i <= 0xffff)
11151 return 3;
11152 clz = clz_hwi (i);
11153 ctz = ctz_hwi (i);
11154 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11155 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11156 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11157 - clz - 5);
11158 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11159 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11160 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11161 - clz - 8);
11162 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11163 return 5;
11164 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11165 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11166 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11167 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11168 - clz - 8);
11169 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11170 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11171 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11172 - clz - 16);
11173 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11174 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11175 && s > 6)
11176 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11177 - clz - 32);
11178 else
11179 return 1 + s;
11180 }
11181 else
11182 {
11183 if (i >= -0x80)
11184 return 2;
11185 else if (i >= -0x8000)
11186 return 3;
11187 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11188 {
11189 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11190 {
11191 s = size_of_int_loc_descriptor (-i) + 1;
11192 if (s < 5)
11193 return s;
11194 }
11195 return 5;
11196 }
11197 else
11198 {
11199 unsigned long r = 1 + size_of_sleb128 (i);
11200 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11201 {
11202 s = size_of_int_loc_descriptor (-i) + 1;
11203 if (s < r)
11204 return s;
11205 }
11206 return r;
11207 }
11208 }
11209 }
11210
11211 /* Return loc description representing "address" of integer value.
11212 This can appear only as toplevel expression. */
11213
11214 static dw_loc_descr_ref
11215 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11216 {
11217 int litsize;
11218 dw_loc_descr_ref loc_result = NULL;
11219
11220 if (!(dwarf_version >= 4 || !dwarf_strict))
11221 return NULL;
11222
11223 litsize = size_of_int_loc_descriptor (i);
11224 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11225 is more compact. For DW_OP_stack_value we need:
11226 litsize + 1 (DW_OP_stack_value)
11227 and for DW_OP_implicit_value:
11228 1 (DW_OP_implicit_value) + 1 (length) + size. */
11229 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11230 {
11231 loc_result = int_loc_descriptor (i);
11232 add_loc_descr (&loc_result,
11233 new_loc_descr (DW_OP_stack_value, 0, 0));
11234 return loc_result;
11235 }
11236
11237 loc_result = new_loc_descr (DW_OP_implicit_value,
11238 size, 0);
11239 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11240 loc_result->dw_loc_oprnd2.v.val_int = i;
11241 return loc_result;
11242 }
11243
11244 /* Return a location descriptor that designates a base+offset location. */
11245
11246 static dw_loc_descr_ref
11247 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11248 enum var_init_status initialized)
11249 {
11250 unsigned int regno;
11251 dw_loc_descr_ref result;
11252 dw_fde_ref fde = cfun->fde;
11253
11254 /* We only use "frame base" when we're sure we're talking about the
11255 post-prologue local stack frame. We do this by *not* running
11256 register elimination until this point, and recognizing the special
11257 argument pointer and soft frame pointer rtx's. */
11258 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11259 {
11260 rtx elim = (ira_use_lra_p
11261 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11262 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11263
11264 if (elim != reg)
11265 {
11266 if (GET_CODE (elim) == PLUS)
11267 {
11268 offset += INTVAL (XEXP (elim, 1));
11269 elim = XEXP (elim, 0);
11270 }
11271 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11272 && (elim == hard_frame_pointer_rtx
11273 || elim == stack_pointer_rtx))
11274 || elim == (frame_pointer_needed
11275 ? hard_frame_pointer_rtx
11276 : stack_pointer_rtx));
11277
11278 /* If drap register is used to align stack, use frame
11279 pointer + offset to access stack variables. If stack
11280 is aligned without drap, use stack pointer + offset to
11281 access stack variables. */
11282 if (crtl->stack_realign_tried
11283 && reg == frame_pointer_rtx)
11284 {
11285 int base_reg
11286 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11287 ? HARD_FRAME_POINTER_REGNUM
11288 : REGNO (elim));
11289 return new_reg_loc_descr (base_reg, offset);
11290 }
11291
11292 gcc_assert (frame_pointer_fb_offset_valid);
11293 offset += frame_pointer_fb_offset;
11294 return new_loc_descr (DW_OP_fbreg, offset, 0);
11295 }
11296 }
11297
11298 regno = REGNO (reg);
11299 #ifdef LEAF_REG_REMAP
11300 if (crtl->uses_only_leaf_regs)
11301 {
11302 int leaf_reg = LEAF_REG_REMAP (regno);
11303 if (leaf_reg != -1)
11304 regno = (unsigned) leaf_reg;
11305 }
11306 #endif
11307 regno = DWARF_FRAME_REGNUM (regno);
11308
11309 if (!optimize && fde
11310 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11311 {
11312 /* Use cfa+offset to represent the location of arguments passed
11313 on the stack when drap is used to align stack.
11314 Only do this when not optimizing, for optimized code var-tracking
11315 is supposed to track where the arguments live and the register
11316 used as vdrap or drap in some spot might be used for something
11317 else in other part of the routine. */
11318 return new_loc_descr (DW_OP_fbreg, offset, 0);
11319 }
11320
11321 if (regno <= 31)
11322 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11323 offset, 0);
11324 else
11325 result = new_loc_descr (DW_OP_bregx, regno, offset);
11326
11327 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11328 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11329
11330 return result;
11331 }
11332
11333 /* Return true if this RTL expression describes a base+offset calculation. */
11334
11335 static inline int
11336 is_based_loc (const_rtx rtl)
11337 {
11338 return (GET_CODE (rtl) == PLUS
11339 && ((REG_P (XEXP (rtl, 0))
11340 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11341 && CONST_INT_P (XEXP (rtl, 1)))));
11342 }
11343
11344 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11345 failed. */
11346
11347 static dw_loc_descr_ref
11348 tls_mem_loc_descriptor (rtx mem)
11349 {
11350 tree base;
11351 dw_loc_descr_ref loc_result;
11352
11353 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11354 return NULL;
11355
11356 base = get_base_address (MEM_EXPR (mem));
11357 if (base == NULL
11358 || TREE_CODE (base) != VAR_DECL
11359 || !DECL_THREAD_LOCAL_P (base))
11360 return NULL;
11361
11362 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11363 if (loc_result == NULL)
11364 return NULL;
11365
11366 if (MEM_OFFSET (mem))
11367 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11368
11369 return loc_result;
11370 }
11371
11372 /* Output debug info about reason why we failed to expand expression as dwarf
11373 expression. */
11374
11375 static void
11376 expansion_failed (tree expr, rtx rtl, char const *reason)
11377 {
11378 if (dump_file && (dump_flags & TDF_DETAILS))
11379 {
11380 fprintf (dump_file, "Failed to expand as dwarf: ");
11381 if (expr)
11382 print_generic_expr (dump_file, expr, dump_flags);
11383 if (rtl)
11384 {
11385 fprintf (dump_file, "\n");
11386 print_rtl (dump_file, rtl);
11387 }
11388 fprintf (dump_file, "\nReason: %s\n", reason);
11389 }
11390 }
11391
11392 /* Helper function for const_ok_for_output, called either directly
11393 or via for_each_rtx. */
11394
11395 static int
11396 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11397 {
11398 rtx rtl = *rtlp;
11399
11400 if (GET_CODE (rtl) == UNSPEC)
11401 {
11402 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11403 we can't express it in the debug info. */
11404 #ifdef ENABLE_CHECKING
11405 /* Don't complain about TLS UNSPECs, those are just too hard to
11406 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11407 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11408 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11409 if (XVECLEN (rtl, 0) == 0
11410 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11411 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11412 inform (current_function_decl
11413 ? DECL_SOURCE_LOCATION (current_function_decl)
11414 : UNKNOWN_LOCATION,
11415 #if NUM_UNSPEC_VALUES > 0
11416 "non-delegitimized UNSPEC %s (%d) found in variable location",
11417 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11418 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11419 XINT (rtl, 1));
11420 #else
11421 "non-delegitimized UNSPEC %d found in variable location",
11422 XINT (rtl, 1));
11423 #endif
11424 #endif
11425 expansion_failed (NULL_TREE, rtl,
11426 "UNSPEC hasn't been delegitimized.\n");
11427 return 1;
11428 }
11429
11430 if (targetm.const_not_ok_for_debug_p (rtl))
11431 {
11432 expansion_failed (NULL_TREE, rtl,
11433 "Expression rejected for debug by the backend.\n");
11434 return 1;
11435 }
11436
11437 if (GET_CODE (rtl) != SYMBOL_REF)
11438 return 0;
11439
11440 if (CONSTANT_POOL_ADDRESS_P (rtl))
11441 {
11442 bool marked;
11443 get_pool_constant_mark (rtl, &marked);
11444 /* If all references to this pool constant were optimized away,
11445 it was not output and thus we can't represent it. */
11446 if (!marked)
11447 {
11448 expansion_failed (NULL_TREE, rtl,
11449 "Constant was removed from constant pool.\n");
11450 return 1;
11451 }
11452 }
11453
11454 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11455 return 1;
11456
11457 /* Avoid references to external symbols in debug info, on several targets
11458 the linker might even refuse to link when linking a shared library,
11459 and in many other cases the relocations for .debug_info/.debug_loc are
11460 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11461 to be defined within the same shared library or executable are fine. */
11462 if (SYMBOL_REF_EXTERNAL_P (rtl))
11463 {
11464 tree decl = SYMBOL_REF_DECL (rtl);
11465
11466 if (decl == NULL || !targetm.binds_local_p (decl))
11467 {
11468 expansion_failed (NULL_TREE, rtl,
11469 "Symbol not defined in current TU.\n");
11470 return 1;
11471 }
11472 }
11473
11474 return 0;
11475 }
11476
11477 /* Return true if constant RTL can be emitted in DW_OP_addr or
11478 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11479 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11480
11481 static bool
11482 const_ok_for_output (rtx rtl)
11483 {
11484 if (GET_CODE (rtl) == SYMBOL_REF)
11485 return const_ok_for_output_1 (&rtl, NULL) == 0;
11486
11487 if (GET_CODE (rtl) == CONST)
11488 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11489
11490 return true;
11491 }
11492
11493 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11494 if possible, NULL otherwise. */
11495
11496 static dw_die_ref
11497 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11498 {
11499 dw_die_ref type_die;
11500 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11501
11502 if (type == NULL)
11503 return NULL;
11504 switch (TREE_CODE (type))
11505 {
11506 case INTEGER_TYPE:
11507 case REAL_TYPE:
11508 break;
11509 default:
11510 return NULL;
11511 }
11512 type_die = lookup_type_die (type);
11513 if (!type_die)
11514 type_die = modified_type_die (type, false, false, comp_unit_die ());
11515 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11516 return NULL;
11517 return type_die;
11518 }
11519
11520 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11521 type matching MODE, or, if MODE is narrower than or as wide as
11522 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11523 possible. */
11524
11525 static dw_loc_descr_ref
11526 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11527 {
11528 enum machine_mode outer_mode = mode;
11529 dw_die_ref type_die;
11530 dw_loc_descr_ref cvt;
11531
11532 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11533 {
11534 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11535 return op;
11536 }
11537 type_die = base_type_for_mode (outer_mode, 1);
11538 if (type_die == NULL)
11539 return NULL;
11540 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11541 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11542 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11543 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11544 add_loc_descr (&op, cvt);
11545 return op;
11546 }
11547
11548 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11549
11550 static dw_loc_descr_ref
11551 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11552 dw_loc_descr_ref op1)
11553 {
11554 dw_loc_descr_ref ret = op0;
11555 add_loc_descr (&ret, op1);
11556 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11557 if (STORE_FLAG_VALUE != 1)
11558 {
11559 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11560 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11561 }
11562 return ret;
11563 }
11564
11565 /* Return location descriptor for signed comparison OP RTL. */
11566
11567 static dw_loc_descr_ref
11568 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11569 enum machine_mode mem_mode)
11570 {
11571 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11572 dw_loc_descr_ref op0, op1;
11573 int shift;
11574
11575 if (op_mode == VOIDmode)
11576 op_mode = GET_MODE (XEXP (rtl, 1));
11577 if (op_mode == VOIDmode)
11578 return NULL;
11579
11580 if (dwarf_strict
11581 && (GET_MODE_CLASS (op_mode) != MODE_INT
11582 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11583 return NULL;
11584
11585 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11586 VAR_INIT_STATUS_INITIALIZED);
11587 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11588 VAR_INIT_STATUS_INITIALIZED);
11589
11590 if (op0 == NULL || op1 == NULL)
11591 return NULL;
11592
11593 if (GET_MODE_CLASS (op_mode) != MODE_INT
11594 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11595 return compare_loc_descriptor (op, op0, op1);
11596
11597 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11598 {
11599 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11600 dw_loc_descr_ref cvt;
11601
11602 if (type_die == NULL)
11603 return NULL;
11604 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11605 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11606 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11607 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11608 add_loc_descr (&op0, cvt);
11609 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11610 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11611 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11612 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11613 add_loc_descr (&op1, cvt);
11614 return compare_loc_descriptor (op, op0, op1);
11615 }
11616
11617 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11618 /* For eq/ne, if the operands are known to be zero-extended,
11619 there is no need to do the fancy shifting up. */
11620 if (op == DW_OP_eq || op == DW_OP_ne)
11621 {
11622 dw_loc_descr_ref last0, last1;
11623 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11624 ;
11625 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11626 ;
11627 /* deref_size zero extends, and for constants we can check
11628 whether they are zero extended or not. */
11629 if (((last0->dw_loc_opc == DW_OP_deref_size
11630 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11631 || (CONST_INT_P (XEXP (rtl, 0))
11632 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11633 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11634 && ((last1->dw_loc_opc == DW_OP_deref_size
11635 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11636 || (CONST_INT_P (XEXP (rtl, 1))
11637 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11638 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11639 return compare_loc_descriptor (op, op0, op1);
11640
11641 /* EQ/NE comparison against constant in narrower type than
11642 DWARF2_ADDR_SIZE can be performed either as
11643 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11644 DW_OP_{eq,ne}
11645 or
11646 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11647 DW_OP_{eq,ne}. Pick whatever is shorter. */
11648 if (CONST_INT_P (XEXP (rtl, 1))
11649 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11650 && (size_of_int_loc_descriptor (shift) + 1
11651 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11652 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11653 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11654 & GET_MODE_MASK (op_mode))))
11655 {
11656 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11657 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11658 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11659 & GET_MODE_MASK (op_mode));
11660 return compare_loc_descriptor (op, op0, op1);
11661 }
11662 }
11663 add_loc_descr (&op0, int_loc_descriptor (shift));
11664 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11665 if (CONST_INT_P (XEXP (rtl, 1)))
11666 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11667 else
11668 {
11669 add_loc_descr (&op1, int_loc_descriptor (shift));
11670 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11671 }
11672 return compare_loc_descriptor (op, op0, op1);
11673 }
11674
11675 /* Return location descriptor for unsigned comparison OP RTL. */
11676
11677 static dw_loc_descr_ref
11678 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11679 enum machine_mode mem_mode)
11680 {
11681 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11682 dw_loc_descr_ref op0, op1;
11683
11684 if (op_mode == VOIDmode)
11685 op_mode = GET_MODE (XEXP (rtl, 1));
11686 if (op_mode == VOIDmode)
11687 return NULL;
11688 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11689 return NULL;
11690
11691 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11692 return NULL;
11693
11694 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11695 VAR_INIT_STATUS_INITIALIZED);
11696 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11697 VAR_INIT_STATUS_INITIALIZED);
11698
11699 if (op0 == NULL || op1 == NULL)
11700 return NULL;
11701
11702 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11703 {
11704 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11705 dw_loc_descr_ref last0, last1;
11706 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11707 ;
11708 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11709 ;
11710 if (CONST_INT_P (XEXP (rtl, 0)))
11711 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11712 /* deref_size zero extends, so no need to mask it again. */
11713 else if (last0->dw_loc_opc != DW_OP_deref_size
11714 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11715 {
11716 add_loc_descr (&op0, int_loc_descriptor (mask));
11717 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11718 }
11719 if (CONST_INT_P (XEXP (rtl, 1)))
11720 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11721 /* deref_size zero extends, so no need to mask it again. */
11722 else if (last1->dw_loc_opc != DW_OP_deref_size
11723 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11724 {
11725 add_loc_descr (&op1, int_loc_descriptor (mask));
11726 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11727 }
11728 }
11729 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11730 {
11731 HOST_WIDE_INT bias = 1;
11732 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11733 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11734 if (CONST_INT_P (XEXP (rtl, 1)))
11735 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11736 + INTVAL (XEXP (rtl, 1)));
11737 else
11738 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11739 bias, 0));
11740 }
11741 return compare_loc_descriptor (op, op0, op1);
11742 }
11743
11744 /* Return location descriptor for {U,S}{MIN,MAX}. */
11745
11746 static dw_loc_descr_ref
11747 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11748 enum machine_mode mem_mode)
11749 {
11750 enum dwarf_location_atom op;
11751 dw_loc_descr_ref op0, op1, ret;
11752 dw_loc_descr_ref bra_node, drop_node;
11753
11754 if (dwarf_strict
11755 && (GET_MODE_CLASS (mode) != MODE_INT
11756 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11757 return NULL;
11758
11759 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11760 VAR_INIT_STATUS_INITIALIZED);
11761 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11762 VAR_INIT_STATUS_INITIALIZED);
11763
11764 if (op0 == NULL || op1 == NULL)
11765 return NULL;
11766
11767 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11768 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11769 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11770 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11771 {
11772 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11773 {
11774 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11775 add_loc_descr (&op0, int_loc_descriptor (mask));
11776 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11777 add_loc_descr (&op1, int_loc_descriptor (mask));
11778 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11779 }
11780 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11781 {
11782 HOST_WIDE_INT bias = 1;
11783 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11784 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11785 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11786 }
11787 }
11788 else if (GET_MODE_CLASS (mode) == MODE_INT
11789 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11790 {
11791 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11792 add_loc_descr (&op0, int_loc_descriptor (shift));
11793 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11794 add_loc_descr (&op1, int_loc_descriptor (shift));
11795 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11796 }
11797 else if (GET_MODE_CLASS (mode) == MODE_INT
11798 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11799 {
11800 dw_die_ref type_die = base_type_for_mode (mode, 0);
11801 dw_loc_descr_ref cvt;
11802 if (type_die == NULL)
11803 return NULL;
11804 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11805 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11806 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11807 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11808 add_loc_descr (&op0, cvt);
11809 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11810 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11811 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11812 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11813 add_loc_descr (&op1, cvt);
11814 }
11815
11816 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11817 op = DW_OP_lt;
11818 else
11819 op = DW_OP_gt;
11820 ret = op0;
11821 add_loc_descr (&ret, op1);
11822 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11823 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11824 add_loc_descr (&ret, bra_node);
11825 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11826 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11827 add_loc_descr (&ret, drop_node);
11828 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11829 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11830 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11831 && GET_MODE_CLASS (mode) == MODE_INT
11832 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11833 ret = convert_descriptor_to_mode (mode, ret);
11834 return ret;
11835 }
11836
11837 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11838 but after converting arguments to type_die, afterwards
11839 convert back to unsigned. */
11840
11841 static dw_loc_descr_ref
11842 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11843 enum machine_mode mode, enum machine_mode mem_mode)
11844 {
11845 dw_loc_descr_ref cvt, op0, op1;
11846
11847 if (type_die == NULL)
11848 return NULL;
11849 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11850 VAR_INIT_STATUS_INITIALIZED);
11851 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11852 VAR_INIT_STATUS_INITIALIZED);
11853 if (op0 == NULL || op1 == NULL)
11854 return NULL;
11855 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11856 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11857 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11858 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11859 add_loc_descr (&op0, cvt);
11860 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11861 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11862 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11863 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11864 add_loc_descr (&op1, cvt);
11865 add_loc_descr (&op0, op1);
11866 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11867 return convert_descriptor_to_mode (mode, op0);
11868 }
11869
11870 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11871 const0 is DW_OP_lit0 or corresponding typed constant,
11872 const1 is DW_OP_lit1 or corresponding typed constant
11873 and constMSB is constant with just the MSB bit set
11874 for the mode):
11875 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11876 L1: const0 DW_OP_swap
11877 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11878 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11879 L3: DW_OP_drop
11880 L4: DW_OP_nop
11881
11882 CTZ is similar:
11883 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11884 L1: const0 DW_OP_swap
11885 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11886 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11887 L3: DW_OP_drop
11888 L4: DW_OP_nop
11889
11890 FFS is similar:
11891 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11892 L1: const1 DW_OP_swap
11893 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11894 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11895 L3: DW_OP_drop
11896 L4: DW_OP_nop */
11897
11898 static dw_loc_descr_ref
11899 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11900 enum machine_mode mem_mode)
11901 {
11902 dw_loc_descr_ref op0, ret, tmp;
11903 HOST_WIDE_INT valv;
11904 dw_loc_descr_ref l1jump, l1label;
11905 dw_loc_descr_ref l2jump, l2label;
11906 dw_loc_descr_ref l3jump, l3label;
11907 dw_loc_descr_ref l4jump, l4label;
11908 rtx msb;
11909
11910 if (GET_MODE_CLASS (mode) != MODE_INT
11911 || GET_MODE (XEXP (rtl, 0)) != mode)
11912 return NULL;
11913
11914 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11915 VAR_INIT_STATUS_INITIALIZED);
11916 if (op0 == NULL)
11917 return NULL;
11918 ret = op0;
11919 if (GET_CODE (rtl) == CLZ)
11920 {
11921 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11922 valv = GET_MODE_BITSIZE (mode);
11923 }
11924 else if (GET_CODE (rtl) == FFS)
11925 valv = 0;
11926 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11927 valv = GET_MODE_BITSIZE (mode);
11928 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11929 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11930 add_loc_descr (&ret, l1jump);
11931 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11932 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11933 VAR_INIT_STATUS_INITIALIZED);
11934 if (tmp == NULL)
11935 return NULL;
11936 add_loc_descr (&ret, tmp);
11937 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11938 add_loc_descr (&ret, l4jump);
11939 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11940 ? const1_rtx : const0_rtx,
11941 mode, mem_mode,
11942 VAR_INIT_STATUS_INITIALIZED);
11943 if (l1label == NULL)
11944 return NULL;
11945 add_loc_descr (&ret, l1label);
11946 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11947 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11948 add_loc_descr (&ret, l2label);
11949 if (GET_CODE (rtl) != CLZ)
11950 msb = const1_rtx;
11951 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11952 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11953 << (GET_MODE_BITSIZE (mode) - 1));
11954 else
11955 msb = immed_wide_int_const
11956 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
11957 GET_MODE_PRECISION (mode)), mode);
11958 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11959 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11960 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11961 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11962 else
11963 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11964 VAR_INIT_STATUS_INITIALIZED);
11965 if (tmp == NULL)
11966 return NULL;
11967 add_loc_descr (&ret, tmp);
11968 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11969 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11970 add_loc_descr (&ret, l3jump);
11971 tmp = mem_loc_descriptor (const1_rtx, 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 (GET_CODE (rtl) == CLZ
11977 ? DW_OP_shl : DW_OP_shr, 0, 0));
11978 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11979 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11980 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11981 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11982 add_loc_descr (&ret, l2jump);
11983 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11984 add_loc_descr (&ret, l3label);
11985 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11986 add_loc_descr (&ret, l4label);
11987 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11988 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11989 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11990 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11991 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11992 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11993 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11994 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11995 return ret;
11996 }
11997
11998 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11999 const1 is DW_OP_lit1 or corresponding typed constant):
12000 const0 DW_OP_swap
12001 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12002 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12003 L2: DW_OP_drop
12004
12005 PARITY is similar:
12006 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12007 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12008 L2: DW_OP_drop */
12009
12010 static dw_loc_descr_ref
12011 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
12012 enum machine_mode mem_mode)
12013 {
12014 dw_loc_descr_ref op0, ret, tmp;
12015 dw_loc_descr_ref l1jump, l1label;
12016 dw_loc_descr_ref l2jump, l2label;
12017
12018 if (GET_MODE_CLASS (mode) != MODE_INT
12019 || GET_MODE (XEXP (rtl, 0)) != mode)
12020 return NULL;
12021
12022 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12023 VAR_INIT_STATUS_INITIALIZED);
12024 if (op0 == NULL)
12025 return NULL;
12026 ret = op0;
12027 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12028 VAR_INIT_STATUS_INITIALIZED);
12029 if (tmp == NULL)
12030 return NULL;
12031 add_loc_descr (&ret, tmp);
12032 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12033 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12034 add_loc_descr (&ret, l1label);
12035 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12036 add_loc_descr (&ret, l2jump);
12037 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12038 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12039 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12040 VAR_INIT_STATUS_INITIALIZED);
12041 if (tmp == NULL)
12042 return NULL;
12043 add_loc_descr (&ret, tmp);
12044 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12045 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12046 ? DW_OP_plus : DW_OP_xor, 0, 0));
12047 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12048 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12049 VAR_INIT_STATUS_INITIALIZED);
12050 add_loc_descr (&ret, tmp);
12051 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12052 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12053 add_loc_descr (&ret, l1jump);
12054 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12055 add_loc_descr (&ret, l2label);
12056 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12057 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12058 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12059 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12060 return ret;
12061 }
12062
12063 /* BSWAP (constS is initial shift count, either 56 or 24):
12064 constS const0
12065 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12066 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12067 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12068 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12069 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12070
12071 static dw_loc_descr_ref
12072 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
12073 enum machine_mode mem_mode)
12074 {
12075 dw_loc_descr_ref op0, ret, tmp;
12076 dw_loc_descr_ref l1jump, l1label;
12077 dw_loc_descr_ref l2jump, l2label;
12078
12079 if (GET_MODE_CLASS (mode) != MODE_INT
12080 || BITS_PER_UNIT != 8
12081 || (GET_MODE_BITSIZE (mode) != 32
12082 && GET_MODE_BITSIZE (mode) != 64))
12083 return NULL;
12084
12085 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12086 VAR_INIT_STATUS_INITIALIZED);
12087 if (op0 == NULL)
12088 return NULL;
12089
12090 ret = op0;
12091 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12092 mode, mem_mode,
12093 VAR_INIT_STATUS_INITIALIZED);
12094 if (tmp == NULL)
12095 return NULL;
12096 add_loc_descr (&ret, tmp);
12097 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12098 VAR_INIT_STATUS_INITIALIZED);
12099 if (tmp == NULL)
12100 return NULL;
12101 add_loc_descr (&ret, tmp);
12102 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12103 add_loc_descr (&ret, l1label);
12104 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12105 mode, mem_mode,
12106 VAR_INIT_STATUS_INITIALIZED);
12107 add_loc_descr (&ret, tmp);
12108 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12109 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12110 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12111 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12112 VAR_INIT_STATUS_INITIALIZED);
12113 if (tmp == NULL)
12114 return NULL;
12115 add_loc_descr (&ret, tmp);
12116 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12117 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12118 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12119 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12120 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12121 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12122 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12123 VAR_INIT_STATUS_INITIALIZED);
12124 add_loc_descr (&ret, tmp);
12125 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12126 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12127 add_loc_descr (&ret, l2jump);
12128 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12129 VAR_INIT_STATUS_INITIALIZED);
12130 add_loc_descr (&ret, tmp);
12131 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12132 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12133 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12134 add_loc_descr (&ret, l1jump);
12135 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12136 add_loc_descr (&ret, l2label);
12137 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12138 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12139 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12140 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12141 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12142 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12143 return ret;
12144 }
12145
12146 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12147 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12148 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12149 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12150
12151 ROTATERT is similar:
12152 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12153 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12154 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12155
12156 static dw_loc_descr_ref
12157 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12158 enum machine_mode mem_mode)
12159 {
12160 rtx rtlop1 = XEXP (rtl, 1);
12161 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12162 int i;
12163
12164 if (GET_MODE_CLASS (mode) != MODE_INT)
12165 return NULL;
12166
12167 if (GET_MODE (rtlop1) != VOIDmode
12168 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12169 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12170 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12171 VAR_INIT_STATUS_INITIALIZED);
12172 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12173 VAR_INIT_STATUS_INITIALIZED);
12174 if (op0 == NULL || op1 == NULL)
12175 return NULL;
12176 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12177 for (i = 0; i < 2; i++)
12178 {
12179 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12180 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12181 mode, mem_mode,
12182 VAR_INIT_STATUS_INITIALIZED);
12183 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12184 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12185 ? DW_OP_const4u
12186 : HOST_BITS_PER_WIDE_INT == 64
12187 ? DW_OP_const8u : DW_OP_constu,
12188 GET_MODE_MASK (mode), 0);
12189 else
12190 mask[i] = NULL;
12191 if (mask[i] == NULL)
12192 return NULL;
12193 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12194 }
12195 ret = op0;
12196 add_loc_descr (&ret, op1);
12197 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12198 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12199 if (GET_CODE (rtl) == ROTATERT)
12200 {
12201 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12202 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12203 GET_MODE_BITSIZE (mode), 0));
12204 }
12205 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12206 if (mask[0] != NULL)
12207 add_loc_descr (&ret, mask[0]);
12208 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12209 if (mask[1] != NULL)
12210 {
12211 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12212 add_loc_descr (&ret, mask[1]);
12213 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12214 }
12215 if (GET_CODE (rtl) == ROTATE)
12216 {
12217 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12218 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12219 GET_MODE_BITSIZE (mode), 0));
12220 }
12221 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12222 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12223 return ret;
12224 }
12225
12226 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12227 for DEBUG_PARAMETER_REF RTL. */
12228
12229 static dw_loc_descr_ref
12230 parameter_ref_descriptor (rtx rtl)
12231 {
12232 dw_loc_descr_ref ret;
12233 dw_die_ref ref;
12234
12235 if (dwarf_strict)
12236 return NULL;
12237 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12238 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12239 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12240 if (ref)
12241 {
12242 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12243 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12244 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12245 }
12246 else
12247 {
12248 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12249 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12250 }
12251 return ret;
12252 }
12253
12254 /* The following routine converts the RTL for a variable or parameter
12255 (resident in memory) into an equivalent Dwarf representation of a
12256 mechanism for getting the address of that same variable onto the top of a
12257 hypothetical "address evaluation" stack.
12258
12259 When creating memory location descriptors, we are effectively transforming
12260 the RTL for a memory-resident object into its Dwarf postfix expression
12261 equivalent. This routine recursively descends an RTL tree, turning
12262 it into Dwarf postfix code as it goes.
12263
12264 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12265
12266 MEM_MODE is the mode of the memory reference, needed to handle some
12267 autoincrement addressing modes.
12268
12269 Return 0 if we can't represent the location. */
12270
12271 dw_loc_descr_ref
12272 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12273 enum machine_mode mem_mode,
12274 enum var_init_status initialized)
12275 {
12276 dw_loc_descr_ref mem_loc_result = NULL;
12277 enum dwarf_location_atom op;
12278 dw_loc_descr_ref op0, op1;
12279 rtx inner = NULL_RTX;
12280
12281 if (mode == VOIDmode)
12282 mode = GET_MODE (rtl);
12283
12284 /* Note that for a dynamically sized array, the location we will generate a
12285 description of here will be the lowest numbered location which is
12286 actually within the array. That's *not* necessarily the same as the
12287 zeroth element of the array. */
12288
12289 rtl = targetm.delegitimize_address (rtl);
12290
12291 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12292 return NULL;
12293
12294 switch (GET_CODE (rtl))
12295 {
12296 case POST_INC:
12297 case POST_DEC:
12298 case POST_MODIFY:
12299 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12300
12301 case SUBREG:
12302 /* The case of a subreg may arise when we have a local (register)
12303 variable or a formal (register) parameter which doesn't quite fill
12304 up an entire register. For now, just assume that it is
12305 legitimate to make the Dwarf info refer to the whole register which
12306 contains the given subreg. */
12307 if (!subreg_lowpart_p (rtl))
12308 break;
12309 inner = SUBREG_REG (rtl);
12310 case TRUNCATE:
12311 if (inner == NULL_RTX)
12312 inner = XEXP (rtl, 0);
12313 if (GET_MODE_CLASS (mode) == MODE_INT
12314 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12315 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12316 #ifdef POINTERS_EXTEND_UNSIGNED
12317 || (mode == Pmode && mem_mode != VOIDmode)
12318 #endif
12319 )
12320 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12321 {
12322 mem_loc_result = mem_loc_descriptor (inner,
12323 GET_MODE (inner),
12324 mem_mode, initialized);
12325 break;
12326 }
12327 if (dwarf_strict)
12328 break;
12329 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12330 break;
12331 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12332 && (GET_MODE_CLASS (mode) != MODE_INT
12333 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12334 break;
12335 else
12336 {
12337 dw_die_ref type_die;
12338 dw_loc_descr_ref cvt;
12339
12340 mem_loc_result = mem_loc_descriptor (inner,
12341 GET_MODE (inner),
12342 mem_mode, initialized);
12343 if (mem_loc_result == NULL)
12344 break;
12345 type_die = base_type_for_mode (mode,
12346 GET_MODE_CLASS (mode) == MODE_INT);
12347 if (type_die == NULL)
12348 {
12349 mem_loc_result = NULL;
12350 break;
12351 }
12352 if (GET_MODE_SIZE (mode)
12353 != GET_MODE_SIZE (GET_MODE (inner)))
12354 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12355 else
12356 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12357 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12358 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12359 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12360 add_loc_descr (&mem_loc_result, cvt);
12361 }
12362 break;
12363
12364 case REG:
12365 if (GET_MODE_CLASS (mode) != MODE_INT
12366 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12367 && rtl != arg_pointer_rtx
12368 && rtl != frame_pointer_rtx
12369 #ifdef POINTERS_EXTEND_UNSIGNED
12370 && (mode != Pmode || mem_mode == VOIDmode)
12371 #endif
12372 ))
12373 {
12374 dw_die_ref type_die;
12375 unsigned int dbx_regnum;
12376
12377 if (dwarf_strict)
12378 break;
12379 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12380 break;
12381 type_die = base_type_for_mode (mode,
12382 GET_MODE_CLASS (mode) == MODE_INT);
12383 if (type_die == NULL)
12384 break;
12385
12386 dbx_regnum = dbx_reg_number (rtl);
12387 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12388 break;
12389 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12390 dbx_regnum, 0);
12391 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12392 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12393 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12394 break;
12395 }
12396 /* Whenever a register number forms a part of the description of the
12397 method for calculating the (dynamic) address of a memory resident
12398 object, DWARF rules require the register number be referred to as
12399 a "base register". This distinction is not based in any way upon
12400 what category of register the hardware believes the given register
12401 belongs to. This is strictly DWARF terminology we're dealing with
12402 here. Note that in cases where the location of a memory-resident
12403 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12404 OP_CONST (0)) the actual DWARF location descriptor that we generate
12405 may just be OP_BASEREG (basereg). This may look deceptively like
12406 the object in question was allocated to a register (rather than in
12407 memory) so DWARF consumers need to be aware of the subtle
12408 distinction between OP_REG and OP_BASEREG. */
12409 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12410 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12411 else if (stack_realign_drap
12412 && crtl->drap_reg
12413 && crtl->args.internal_arg_pointer == rtl
12414 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12415 {
12416 /* If RTL is internal_arg_pointer, which has been optimized
12417 out, use DRAP instead. */
12418 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12419 VAR_INIT_STATUS_INITIALIZED);
12420 }
12421 break;
12422
12423 case SIGN_EXTEND:
12424 case ZERO_EXTEND:
12425 if (GET_MODE_CLASS (mode) != MODE_INT)
12426 break;
12427 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12428 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12429 if (op0 == 0)
12430 break;
12431 else if (GET_CODE (rtl) == ZERO_EXTEND
12432 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12433 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12434 < HOST_BITS_PER_WIDE_INT
12435 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12436 to expand zero extend as two shifts instead of
12437 masking. */
12438 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12439 {
12440 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12441 mem_loc_result = op0;
12442 add_loc_descr (&mem_loc_result,
12443 int_loc_descriptor (GET_MODE_MASK (imode)));
12444 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12445 }
12446 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12447 {
12448 int shift = DWARF2_ADDR_SIZE
12449 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12450 shift *= BITS_PER_UNIT;
12451 if (GET_CODE (rtl) == SIGN_EXTEND)
12452 op = DW_OP_shra;
12453 else
12454 op = DW_OP_shr;
12455 mem_loc_result = op0;
12456 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12457 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12458 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12459 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12460 }
12461 else if (!dwarf_strict)
12462 {
12463 dw_die_ref type_die1, type_die2;
12464 dw_loc_descr_ref cvt;
12465
12466 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12467 GET_CODE (rtl) == ZERO_EXTEND);
12468 if (type_die1 == NULL)
12469 break;
12470 type_die2 = base_type_for_mode (mode, 1);
12471 if (type_die2 == NULL)
12472 break;
12473 mem_loc_result = op0;
12474 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12475 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12476 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12477 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12478 add_loc_descr (&mem_loc_result, cvt);
12479 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12480 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12481 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12482 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12483 add_loc_descr (&mem_loc_result, cvt);
12484 }
12485 break;
12486
12487 case MEM:
12488 {
12489 rtx new_rtl = avoid_constant_pool_reference (rtl);
12490 if (new_rtl != rtl)
12491 {
12492 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12493 initialized);
12494 if (mem_loc_result != NULL)
12495 return mem_loc_result;
12496 }
12497 }
12498 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12499 get_address_mode (rtl), mode,
12500 VAR_INIT_STATUS_INITIALIZED);
12501 if (mem_loc_result == NULL)
12502 mem_loc_result = tls_mem_loc_descriptor (rtl);
12503 if (mem_loc_result != NULL)
12504 {
12505 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12506 || GET_MODE_CLASS (mode) != MODE_INT)
12507 {
12508 dw_die_ref type_die;
12509 dw_loc_descr_ref deref;
12510
12511 if (dwarf_strict)
12512 return NULL;
12513 type_die
12514 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12515 if (type_die == NULL)
12516 return NULL;
12517 deref = new_loc_descr (DW_OP_GNU_deref_type,
12518 GET_MODE_SIZE (mode), 0);
12519 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12520 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12521 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12522 add_loc_descr (&mem_loc_result, deref);
12523 }
12524 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12525 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12526 else
12527 add_loc_descr (&mem_loc_result,
12528 new_loc_descr (DW_OP_deref_size,
12529 GET_MODE_SIZE (mode), 0));
12530 }
12531 break;
12532
12533 case LO_SUM:
12534 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12535
12536 case LABEL_REF:
12537 /* Some ports can transform a symbol ref into a label ref, because
12538 the symbol ref is too far away and has to be dumped into a constant
12539 pool. */
12540 case CONST:
12541 case SYMBOL_REF:
12542 if (GET_MODE_CLASS (mode) != MODE_INT
12543 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12544 #ifdef POINTERS_EXTEND_UNSIGNED
12545 && (mode != Pmode || mem_mode == VOIDmode)
12546 #endif
12547 ))
12548 break;
12549 if (GET_CODE (rtl) == SYMBOL_REF
12550 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12551 {
12552 dw_loc_descr_ref temp;
12553
12554 /* If this is not defined, we have no way to emit the data. */
12555 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12556 break;
12557
12558 temp = new_addr_loc_descr (rtl, dtprel_true);
12559
12560 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12561 add_loc_descr (&mem_loc_result, temp);
12562
12563 break;
12564 }
12565
12566 if (!const_ok_for_output (rtl))
12567 break;
12568
12569 symref:
12570 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12571 vec_safe_push (used_rtx_array, rtl);
12572 break;
12573
12574 case CONCAT:
12575 case CONCATN:
12576 case VAR_LOCATION:
12577 case DEBUG_IMPLICIT_PTR:
12578 expansion_failed (NULL_TREE, rtl,
12579 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12580 return 0;
12581
12582 case ENTRY_VALUE:
12583 if (dwarf_strict)
12584 return NULL;
12585 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12586 {
12587 if (GET_MODE_CLASS (mode) != MODE_INT
12588 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12589 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12590 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12591 else
12592 {
12593 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12594 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12595 return NULL;
12596 op0 = one_reg_loc_descriptor (dbx_regnum,
12597 VAR_INIT_STATUS_INITIALIZED);
12598 }
12599 }
12600 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12601 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12602 {
12603 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12604 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12605 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12606 return NULL;
12607 }
12608 else
12609 gcc_unreachable ();
12610 if (op0 == NULL)
12611 return NULL;
12612 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12613 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12614 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12615 break;
12616
12617 case DEBUG_PARAMETER_REF:
12618 mem_loc_result = parameter_ref_descriptor (rtl);
12619 break;
12620
12621 case PRE_MODIFY:
12622 /* Extract the PLUS expression nested inside and fall into
12623 PLUS code below. */
12624 rtl = XEXP (rtl, 1);
12625 goto plus;
12626
12627 case PRE_INC:
12628 case PRE_DEC:
12629 /* Turn these into a PLUS expression and fall into the PLUS code
12630 below. */
12631 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12632 gen_int_mode (GET_CODE (rtl) == PRE_INC
12633 ? GET_MODE_UNIT_SIZE (mem_mode)
12634 : -GET_MODE_UNIT_SIZE (mem_mode),
12635 mode));
12636
12637 /* ... fall through ... */
12638
12639 case PLUS:
12640 plus:
12641 if (is_based_loc (rtl)
12642 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12643 || XEXP (rtl, 0) == arg_pointer_rtx
12644 || XEXP (rtl, 0) == frame_pointer_rtx)
12645 && GET_MODE_CLASS (mode) == MODE_INT)
12646 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12647 INTVAL (XEXP (rtl, 1)),
12648 VAR_INIT_STATUS_INITIALIZED);
12649 else
12650 {
12651 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12652 VAR_INIT_STATUS_INITIALIZED);
12653 if (mem_loc_result == 0)
12654 break;
12655
12656 if (CONST_INT_P (XEXP (rtl, 1))
12657 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12658 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12659 else
12660 {
12661 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12662 VAR_INIT_STATUS_INITIALIZED);
12663 if (op1 == 0)
12664 break;
12665 add_loc_descr (&mem_loc_result, op1);
12666 add_loc_descr (&mem_loc_result,
12667 new_loc_descr (DW_OP_plus, 0, 0));
12668 }
12669 }
12670 break;
12671
12672 /* If a pseudo-reg is optimized away, it is possible for it to
12673 be replaced with a MEM containing a multiply or shift. */
12674 case MINUS:
12675 op = DW_OP_minus;
12676 goto do_binop;
12677
12678 case MULT:
12679 op = DW_OP_mul;
12680 goto do_binop;
12681
12682 case DIV:
12683 if (!dwarf_strict
12684 && GET_MODE_CLASS (mode) == MODE_INT
12685 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12686 {
12687 mem_loc_result = typed_binop (DW_OP_div, rtl,
12688 base_type_for_mode (mode, 0),
12689 mode, mem_mode);
12690 break;
12691 }
12692 op = DW_OP_div;
12693 goto do_binop;
12694
12695 case UMOD:
12696 op = DW_OP_mod;
12697 goto do_binop;
12698
12699 case ASHIFT:
12700 op = DW_OP_shl;
12701 goto do_shift;
12702
12703 case ASHIFTRT:
12704 op = DW_OP_shra;
12705 goto do_shift;
12706
12707 case LSHIFTRT:
12708 op = DW_OP_shr;
12709 goto do_shift;
12710
12711 do_shift:
12712 if (GET_MODE_CLASS (mode) != MODE_INT)
12713 break;
12714 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12715 VAR_INIT_STATUS_INITIALIZED);
12716 {
12717 rtx rtlop1 = XEXP (rtl, 1);
12718 if (GET_MODE (rtlop1) != VOIDmode
12719 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12720 < GET_MODE_BITSIZE (mode))
12721 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12722 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12723 VAR_INIT_STATUS_INITIALIZED);
12724 }
12725
12726 if (op0 == 0 || op1 == 0)
12727 break;
12728
12729 mem_loc_result = op0;
12730 add_loc_descr (&mem_loc_result, op1);
12731 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12732 break;
12733
12734 case AND:
12735 op = DW_OP_and;
12736 goto do_binop;
12737
12738 case IOR:
12739 op = DW_OP_or;
12740 goto do_binop;
12741
12742 case XOR:
12743 op = DW_OP_xor;
12744 goto do_binop;
12745
12746 do_binop:
12747 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12748 VAR_INIT_STATUS_INITIALIZED);
12749 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12750 VAR_INIT_STATUS_INITIALIZED);
12751
12752 if (op0 == 0 || op1 == 0)
12753 break;
12754
12755 mem_loc_result = op0;
12756 add_loc_descr (&mem_loc_result, op1);
12757 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12758 break;
12759
12760 case MOD:
12761 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12762 {
12763 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12764 base_type_for_mode (mode, 0),
12765 mode, mem_mode);
12766 break;
12767 }
12768
12769 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12770 VAR_INIT_STATUS_INITIALIZED);
12771 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12772 VAR_INIT_STATUS_INITIALIZED);
12773
12774 if (op0 == 0 || op1 == 0)
12775 break;
12776
12777 mem_loc_result = op0;
12778 add_loc_descr (&mem_loc_result, op1);
12779 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12780 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12781 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12782 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12783 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12784 break;
12785
12786 case UDIV:
12787 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12788 {
12789 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12790 {
12791 op = DW_OP_div;
12792 goto do_binop;
12793 }
12794 mem_loc_result = typed_binop (DW_OP_div, rtl,
12795 base_type_for_mode (mode, 1),
12796 mode, mem_mode);
12797 }
12798 break;
12799
12800 case NOT:
12801 op = DW_OP_not;
12802 goto do_unop;
12803
12804 case ABS:
12805 op = DW_OP_abs;
12806 goto do_unop;
12807
12808 case NEG:
12809 op = DW_OP_neg;
12810 goto do_unop;
12811
12812 do_unop:
12813 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12814 VAR_INIT_STATUS_INITIALIZED);
12815
12816 if (op0 == 0)
12817 break;
12818
12819 mem_loc_result = op0;
12820 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12821 break;
12822
12823 case CONST_INT:
12824 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12825 #ifdef POINTERS_EXTEND_UNSIGNED
12826 || (mode == Pmode
12827 && mem_mode != VOIDmode
12828 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12829 #endif
12830 )
12831 {
12832 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12833 break;
12834 }
12835 if (!dwarf_strict
12836 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12837 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12838 {
12839 dw_die_ref type_die = base_type_for_mode (mode, 1);
12840 enum machine_mode amode;
12841 if (type_die == NULL)
12842 return NULL;
12843 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12844 MODE_INT, 0);
12845 if (INTVAL (rtl) >= 0
12846 && amode != BLKmode
12847 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12848 /* const DW_OP_GNU_convert <XXX> vs.
12849 DW_OP_GNU_const_type <XXX, 1, const>. */
12850 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12851 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12852 {
12853 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12854 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12855 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12856 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12857 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12858 add_loc_descr (&mem_loc_result, op0);
12859 return mem_loc_result;
12860 }
12861 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12862 INTVAL (rtl));
12863 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12864 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12865 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12866 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12867 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12868 else
12869 {
12870 mem_loc_result->dw_loc_oprnd2.val_class
12871 = dw_val_class_const_double;
12872 mem_loc_result->dw_loc_oprnd2.v.val_double
12873 = double_int::from_shwi (INTVAL (rtl));
12874 }
12875 }
12876 break;
12877
12878 case CONST_DOUBLE:
12879 if (!dwarf_strict)
12880 {
12881 dw_die_ref type_die;
12882
12883 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
12884 CONST_DOUBLE rtx could represent either an large integer
12885 or a floating-point constant. If
12886 TARGET_SUPPORTS_WIDE_INT != 0, the value is always a
12887 floating point constant.
12888
12889 When it is an integer, a CONST_DOUBLE is used whenever
12890 the constant requires 2 HWIs to be adequately
12891 represented. We output CONST_DOUBLEs as blocks. */
12892 if (mode == VOIDmode
12893 || (GET_MODE (rtl) == VOIDmode
12894 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12895 break;
12896 type_die = base_type_for_mode (mode,
12897 GET_MODE_CLASS (mode) == MODE_INT);
12898 if (type_die == NULL)
12899 return NULL;
12900 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12901 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12902 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12903 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12904 #if TARGET_SUPPORTS_WIDE_INT == 0
12905 if (!SCALAR_FLOAT_MODE_P (mode))
12906 {
12907 mem_loc_result->dw_loc_oprnd2.val_class
12908 = dw_val_class_const_double;
12909 mem_loc_result->dw_loc_oprnd2.v.val_double
12910 = rtx_to_double_int (rtl);
12911 }
12912 else
12913 #endif
12914 {
12915 unsigned int length = GET_MODE_SIZE (mode);
12916 unsigned char *array
12917 = (unsigned char*) ggc_alloc_atomic (length);
12918
12919 insert_float (rtl, array);
12920 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12921 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12922 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12923 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12924 }
12925 }
12926 break;
12927
12928 case CONST_WIDE_INT:
12929 if (!dwarf_strict)
12930 {
12931 dw_die_ref type_die;
12932
12933 type_die = base_type_for_mode (mode,
12934 GET_MODE_CLASS (mode) == MODE_INT);
12935 if (type_die == NULL)
12936 return NULL;
12937 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12938 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12939 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12940 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12941 mem_loc_result->dw_loc_oprnd2.val_class
12942 = dw_val_class_wide_int;
12943 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc_cleared_wide_int ();
12944 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
12945 }
12946 break;
12947
12948 case EQ:
12949 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12950 break;
12951
12952 case GE:
12953 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12954 break;
12955
12956 case GT:
12957 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12958 break;
12959
12960 case LE:
12961 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12962 break;
12963
12964 case LT:
12965 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12966 break;
12967
12968 case NE:
12969 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12970 break;
12971
12972 case GEU:
12973 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12974 break;
12975
12976 case GTU:
12977 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12978 break;
12979
12980 case LEU:
12981 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12982 break;
12983
12984 case LTU:
12985 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12986 break;
12987
12988 case UMIN:
12989 case UMAX:
12990 if (GET_MODE_CLASS (mode) != MODE_INT)
12991 break;
12992 /* FALLTHRU */
12993 case SMIN:
12994 case SMAX:
12995 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12996 break;
12997
12998 case ZERO_EXTRACT:
12999 case SIGN_EXTRACT:
13000 if (CONST_INT_P (XEXP (rtl, 1))
13001 && CONST_INT_P (XEXP (rtl, 2))
13002 && ((unsigned) INTVAL (XEXP (rtl, 1))
13003 + (unsigned) INTVAL (XEXP (rtl, 2))
13004 <= GET_MODE_BITSIZE (mode))
13005 && GET_MODE_CLASS (mode) == MODE_INT
13006 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13007 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13008 {
13009 int shift, size;
13010 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13011 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13012 if (op0 == 0)
13013 break;
13014 if (GET_CODE (rtl) == SIGN_EXTRACT)
13015 op = DW_OP_shra;
13016 else
13017 op = DW_OP_shr;
13018 mem_loc_result = op0;
13019 size = INTVAL (XEXP (rtl, 1));
13020 shift = INTVAL (XEXP (rtl, 2));
13021 if (BITS_BIG_ENDIAN)
13022 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13023 - shift - size;
13024 if (shift + size != (int) DWARF2_ADDR_SIZE)
13025 {
13026 add_loc_descr (&mem_loc_result,
13027 int_loc_descriptor (DWARF2_ADDR_SIZE
13028 - shift - size));
13029 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13030 }
13031 if (size != (int) DWARF2_ADDR_SIZE)
13032 {
13033 add_loc_descr (&mem_loc_result,
13034 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13035 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13036 }
13037 }
13038 break;
13039
13040 case IF_THEN_ELSE:
13041 {
13042 dw_loc_descr_ref op2, bra_node, drop_node;
13043 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13044 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13045 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13046 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13047 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13048 VAR_INIT_STATUS_INITIALIZED);
13049 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13050 VAR_INIT_STATUS_INITIALIZED);
13051 if (op0 == NULL || op1 == NULL || op2 == NULL)
13052 break;
13053
13054 mem_loc_result = op1;
13055 add_loc_descr (&mem_loc_result, op2);
13056 add_loc_descr (&mem_loc_result, op0);
13057 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13058 add_loc_descr (&mem_loc_result, bra_node);
13059 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13060 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13061 add_loc_descr (&mem_loc_result, drop_node);
13062 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13063 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13064 }
13065 break;
13066
13067 case FLOAT_EXTEND:
13068 case FLOAT_TRUNCATE:
13069 case FLOAT:
13070 case UNSIGNED_FLOAT:
13071 case FIX:
13072 case UNSIGNED_FIX:
13073 if (!dwarf_strict)
13074 {
13075 dw_die_ref type_die;
13076 dw_loc_descr_ref cvt;
13077
13078 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13079 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13080 if (op0 == NULL)
13081 break;
13082 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13083 && (GET_CODE (rtl) == FLOAT
13084 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13085 <= DWARF2_ADDR_SIZE))
13086 {
13087 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13088 GET_CODE (rtl) == UNSIGNED_FLOAT);
13089 if (type_die == NULL)
13090 break;
13091 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13092 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13093 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13094 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13095 add_loc_descr (&op0, cvt);
13096 }
13097 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13098 if (type_die == NULL)
13099 break;
13100 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13101 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13102 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13103 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13104 add_loc_descr (&op0, cvt);
13105 if (GET_MODE_CLASS (mode) == MODE_INT
13106 && (GET_CODE (rtl) == FIX
13107 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13108 {
13109 op0 = convert_descriptor_to_mode (mode, op0);
13110 if (op0 == NULL)
13111 break;
13112 }
13113 mem_loc_result = op0;
13114 }
13115 break;
13116
13117 case CLZ:
13118 case CTZ:
13119 case FFS:
13120 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13121 break;
13122
13123 case POPCOUNT:
13124 case PARITY:
13125 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13126 break;
13127
13128 case BSWAP:
13129 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13130 break;
13131
13132 case ROTATE:
13133 case ROTATERT:
13134 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13135 break;
13136
13137 case COMPARE:
13138 /* In theory, we could implement the above. */
13139 /* DWARF cannot represent the unsigned compare operations
13140 natively. */
13141 case SS_MULT:
13142 case US_MULT:
13143 case SS_DIV:
13144 case US_DIV:
13145 case SS_PLUS:
13146 case US_PLUS:
13147 case SS_MINUS:
13148 case US_MINUS:
13149 case SS_NEG:
13150 case US_NEG:
13151 case SS_ABS:
13152 case SS_ASHIFT:
13153 case US_ASHIFT:
13154 case SS_TRUNCATE:
13155 case US_TRUNCATE:
13156 case UNORDERED:
13157 case ORDERED:
13158 case UNEQ:
13159 case UNGE:
13160 case UNGT:
13161 case UNLE:
13162 case UNLT:
13163 case LTGT:
13164 case FRACT_CONVERT:
13165 case UNSIGNED_FRACT_CONVERT:
13166 case SAT_FRACT:
13167 case UNSIGNED_SAT_FRACT:
13168 case SQRT:
13169 case ASM_OPERANDS:
13170 case VEC_MERGE:
13171 case VEC_SELECT:
13172 case VEC_CONCAT:
13173 case VEC_DUPLICATE:
13174 case UNSPEC:
13175 case HIGH:
13176 case FMA:
13177 case STRICT_LOW_PART:
13178 case CONST_VECTOR:
13179 case CONST_FIXED:
13180 case CLRSB:
13181 case CLOBBER:
13182 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13183 can't express it in the debug info. This can happen e.g. with some
13184 TLS UNSPECs. */
13185 break;
13186
13187 case CONST_STRING:
13188 resolve_one_addr (&rtl, NULL);
13189 goto symref;
13190
13191 default:
13192 #ifdef ENABLE_CHECKING
13193 print_rtl (stderr, rtl);
13194 gcc_unreachable ();
13195 #else
13196 break;
13197 #endif
13198 }
13199
13200 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13201 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13202
13203 return mem_loc_result;
13204 }
13205
13206 /* Return a descriptor that describes the concatenation of two locations.
13207 This is typically a complex variable. */
13208
13209 static dw_loc_descr_ref
13210 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13211 {
13212 dw_loc_descr_ref cc_loc_result = NULL;
13213 dw_loc_descr_ref x0_ref
13214 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13215 dw_loc_descr_ref x1_ref
13216 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13217
13218 if (x0_ref == 0 || x1_ref == 0)
13219 return 0;
13220
13221 cc_loc_result = x0_ref;
13222 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13223
13224 add_loc_descr (&cc_loc_result, x1_ref);
13225 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13226
13227 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13228 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13229
13230 return cc_loc_result;
13231 }
13232
13233 /* Return a descriptor that describes the concatenation of N
13234 locations. */
13235
13236 static dw_loc_descr_ref
13237 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13238 {
13239 unsigned int i;
13240 dw_loc_descr_ref cc_loc_result = NULL;
13241 unsigned int n = XVECLEN (concatn, 0);
13242
13243 for (i = 0; i < n; ++i)
13244 {
13245 dw_loc_descr_ref ref;
13246 rtx x = XVECEXP (concatn, 0, i);
13247
13248 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13249 if (ref == NULL)
13250 return NULL;
13251
13252 add_loc_descr (&cc_loc_result, ref);
13253 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13254 }
13255
13256 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13257 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13258
13259 return cc_loc_result;
13260 }
13261
13262 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13263 for DEBUG_IMPLICIT_PTR RTL. */
13264
13265 static dw_loc_descr_ref
13266 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13267 {
13268 dw_loc_descr_ref ret;
13269 dw_die_ref ref;
13270
13271 if (dwarf_strict)
13272 return NULL;
13273 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13274 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13275 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13276 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13277 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13278 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13279 if (ref)
13280 {
13281 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13282 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13283 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13284 }
13285 else
13286 {
13287 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13288 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13289 }
13290 return ret;
13291 }
13292
13293 /* Output a proper Dwarf location descriptor for a variable or parameter
13294 which is either allocated in a register or in a memory location. For a
13295 register, we just generate an OP_REG and the register number. For a
13296 memory location we provide a Dwarf postfix expression describing how to
13297 generate the (dynamic) address of the object onto the address stack.
13298
13299 MODE is mode of the decl if this loc_descriptor is going to be used in
13300 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13301 allowed, VOIDmode otherwise.
13302
13303 If we don't know how to describe it, return 0. */
13304
13305 static dw_loc_descr_ref
13306 loc_descriptor (rtx rtl, enum machine_mode mode,
13307 enum var_init_status initialized)
13308 {
13309 dw_loc_descr_ref loc_result = NULL;
13310
13311 switch (GET_CODE (rtl))
13312 {
13313 case SUBREG:
13314 /* The case of a subreg may arise when we have a local (register)
13315 variable or a formal (register) parameter which doesn't quite fill
13316 up an entire register. For now, just assume that it is
13317 legitimate to make the Dwarf info refer to the whole register which
13318 contains the given subreg. */
13319 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13320 loc_result = loc_descriptor (SUBREG_REG (rtl),
13321 GET_MODE (SUBREG_REG (rtl)), initialized);
13322 else
13323 goto do_default;
13324 break;
13325
13326 case REG:
13327 loc_result = reg_loc_descriptor (rtl, initialized);
13328 break;
13329
13330 case MEM:
13331 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13332 GET_MODE (rtl), initialized);
13333 if (loc_result == NULL)
13334 loc_result = tls_mem_loc_descriptor (rtl);
13335 if (loc_result == NULL)
13336 {
13337 rtx new_rtl = avoid_constant_pool_reference (rtl);
13338 if (new_rtl != rtl)
13339 loc_result = loc_descriptor (new_rtl, mode, initialized);
13340 }
13341 break;
13342
13343 case CONCAT:
13344 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13345 initialized);
13346 break;
13347
13348 case CONCATN:
13349 loc_result = concatn_loc_descriptor (rtl, initialized);
13350 break;
13351
13352 case VAR_LOCATION:
13353 /* Single part. */
13354 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13355 {
13356 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13357 if (GET_CODE (loc) == EXPR_LIST)
13358 loc = XEXP (loc, 0);
13359 loc_result = loc_descriptor (loc, mode, initialized);
13360 break;
13361 }
13362
13363 rtl = XEXP (rtl, 1);
13364 /* FALLTHRU */
13365
13366 case PARALLEL:
13367 {
13368 rtvec par_elems = XVEC (rtl, 0);
13369 int num_elem = GET_NUM_ELEM (par_elems);
13370 enum machine_mode mode;
13371 int i;
13372
13373 /* Create the first one, so we have something to add to. */
13374 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13375 VOIDmode, initialized);
13376 if (loc_result == NULL)
13377 return NULL;
13378 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13379 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13380 for (i = 1; i < num_elem; i++)
13381 {
13382 dw_loc_descr_ref temp;
13383
13384 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13385 VOIDmode, initialized);
13386 if (temp == NULL)
13387 return NULL;
13388 add_loc_descr (&loc_result, temp);
13389 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13390 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13391 }
13392 }
13393 break;
13394
13395 case CONST_INT:
13396 if (mode != VOIDmode && mode != BLKmode)
13397 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13398 INTVAL (rtl));
13399 break;
13400
13401 case CONST_DOUBLE:
13402 if (mode == VOIDmode)
13403 mode = GET_MODE (rtl);
13404
13405 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13406 {
13407 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13408
13409 /* Note that a CONST_DOUBLE rtx could represent either an integer
13410 or a floating-point constant. A CONST_DOUBLE is used whenever
13411 the constant requires more than one word in order to be
13412 adequately represented. We output CONST_DOUBLEs as blocks. */
13413 loc_result = new_loc_descr (DW_OP_implicit_value,
13414 GET_MODE_SIZE (mode), 0);
13415 #if TARGET_SUPPORTS_WIDE_INT == 0
13416 if (!SCALAR_FLOAT_MODE_P (mode))
13417 {
13418 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13419 loc_result->dw_loc_oprnd2.v.val_double
13420 = rtx_to_double_int (rtl);
13421 }
13422 else
13423 #endif
13424 {
13425 unsigned int length = GET_MODE_SIZE (mode);
13426 unsigned char *array
13427 = (unsigned char*) ggc_alloc_atomic (length);
13428
13429 insert_float (rtl, array);
13430 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13431 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13432 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13433 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13434 }
13435 }
13436 break;
13437
13438 case CONST_WIDE_INT:
13439 if (mode == VOIDmode)
13440 mode = GET_MODE (rtl);
13441
13442 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13443 {
13444 loc_result = new_loc_descr (DW_OP_implicit_value,
13445 GET_MODE_SIZE (mode), 0);
13446 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13447 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc_cleared_wide_int ();
13448 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13449 }
13450 break;
13451
13452 case CONST_VECTOR:
13453 if (mode == VOIDmode)
13454 mode = GET_MODE (rtl);
13455
13456 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13457 {
13458 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13459 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13460 unsigned char *array = (unsigned char *)
13461 ggc_alloc_atomic (length * elt_size);
13462 unsigned int i;
13463 unsigned char *p;
13464 enum machine_mode imode = GET_MODE_INNER (mode);
13465
13466 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13467 switch (GET_MODE_CLASS (mode))
13468 {
13469 case MODE_VECTOR_INT:
13470 for (i = 0, p = array; i < length; i++, p += elt_size)
13471 {
13472 rtx elt = CONST_VECTOR_ELT (rtl, i);
13473 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13474 }
13475 break;
13476
13477 case MODE_VECTOR_FLOAT:
13478 for (i = 0, p = array; i < length; i++, p += elt_size)
13479 {
13480 rtx elt = CONST_VECTOR_ELT (rtl, i);
13481 insert_float (elt, p);
13482 }
13483 break;
13484
13485 default:
13486 gcc_unreachable ();
13487 }
13488
13489 loc_result = new_loc_descr (DW_OP_implicit_value,
13490 length * elt_size, 0);
13491 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13492 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13493 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13494 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13495 }
13496 break;
13497
13498 case CONST:
13499 if (mode == VOIDmode
13500 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13501 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13502 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13503 {
13504 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13505 break;
13506 }
13507 /* FALLTHROUGH */
13508 case SYMBOL_REF:
13509 if (!const_ok_for_output (rtl))
13510 break;
13511 case LABEL_REF:
13512 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13513 && (dwarf_version >= 4 || !dwarf_strict))
13514 {
13515 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13516 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13517 vec_safe_push (used_rtx_array, rtl);
13518 }
13519 break;
13520
13521 case DEBUG_IMPLICIT_PTR:
13522 loc_result = implicit_ptr_descriptor (rtl, 0);
13523 break;
13524
13525 case PLUS:
13526 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13527 && CONST_INT_P (XEXP (rtl, 1)))
13528 {
13529 loc_result
13530 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13531 break;
13532 }
13533 /* FALLTHRU */
13534 do_default:
13535 default:
13536 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13537 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13538 && dwarf_version >= 4)
13539 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13540 {
13541 /* Value expression. */
13542 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13543 if (loc_result)
13544 add_loc_descr (&loc_result,
13545 new_loc_descr (DW_OP_stack_value, 0, 0));
13546 }
13547 break;
13548 }
13549
13550 return loc_result;
13551 }
13552
13553 /* We need to figure out what section we should use as the base for the
13554 address ranges where a given location is valid.
13555 1. If this particular DECL has a section associated with it, use that.
13556 2. If this function has a section associated with it, use that.
13557 3. Otherwise, use the text section.
13558 XXX: If you split a variable across multiple sections, we won't notice. */
13559
13560 static const char *
13561 secname_for_decl (const_tree decl)
13562 {
13563 const char *secname;
13564
13565 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13566 {
13567 tree sectree = DECL_SECTION_NAME (decl);
13568 secname = TREE_STRING_POINTER (sectree);
13569 }
13570 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13571 {
13572 tree sectree = DECL_SECTION_NAME (current_function_decl);
13573 secname = TREE_STRING_POINTER (sectree);
13574 }
13575 else if (cfun && in_cold_section_p)
13576 secname = crtl->subsections.cold_section_label;
13577 else
13578 secname = text_section_label;
13579
13580 return secname;
13581 }
13582
13583 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13584
13585 static bool
13586 decl_by_reference_p (tree decl)
13587 {
13588 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13589 || TREE_CODE (decl) == VAR_DECL)
13590 && DECL_BY_REFERENCE (decl));
13591 }
13592
13593 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13594 for VARLOC. */
13595
13596 static dw_loc_descr_ref
13597 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13598 enum var_init_status initialized)
13599 {
13600 int have_address = 0;
13601 dw_loc_descr_ref descr;
13602 enum machine_mode mode;
13603
13604 if (want_address != 2)
13605 {
13606 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13607 /* Single part. */
13608 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13609 {
13610 varloc = PAT_VAR_LOCATION_LOC (varloc);
13611 if (GET_CODE (varloc) == EXPR_LIST)
13612 varloc = XEXP (varloc, 0);
13613 mode = GET_MODE (varloc);
13614 if (MEM_P (varloc))
13615 {
13616 rtx addr = XEXP (varloc, 0);
13617 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13618 mode, initialized);
13619 if (descr)
13620 have_address = 1;
13621 else
13622 {
13623 rtx x = avoid_constant_pool_reference (varloc);
13624 if (x != varloc)
13625 descr = mem_loc_descriptor (x, mode, VOIDmode,
13626 initialized);
13627 }
13628 }
13629 else
13630 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13631 }
13632 else
13633 return 0;
13634 }
13635 else
13636 {
13637 if (GET_CODE (varloc) == VAR_LOCATION)
13638 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13639 else
13640 mode = DECL_MODE (loc);
13641 descr = loc_descriptor (varloc, mode, initialized);
13642 have_address = 1;
13643 }
13644
13645 if (!descr)
13646 return 0;
13647
13648 if (want_address == 2 && !have_address
13649 && (dwarf_version >= 4 || !dwarf_strict))
13650 {
13651 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13652 {
13653 expansion_failed (loc, NULL_RTX,
13654 "DWARF address size mismatch");
13655 return 0;
13656 }
13657 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13658 have_address = 1;
13659 }
13660 /* Show if we can't fill the request for an address. */
13661 if (want_address && !have_address)
13662 {
13663 expansion_failed (loc, NULL_RTX,
13664 "Want address and only have value");
13665 return 0;
13666 }
13667
13668 /* If we've got an address and don't want one, dereference. */
13669 if (!want_address && have_address)
13670 {
13671 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13672 enum dwarf_location_atom op;
13673
13674 if (size > DWARF2_ADDR_SIZE || size == -1)
13675 {
13676 expansion_failed (loc, NULL_RTX,
13677 "DWARF address size mismatch");
13678 return 0;
13679 }
13680 else if (size == DWARF2_ADDR_SIZE)
13681 op = DW_OP_deref;
13682 else
13683 op = DW_OP_deref_size;
13684
13685 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13686 }
13687
13688 return descr;
13689 }
13690
13691 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13692 if it is not possible. */
13693
13694 static dw_loc_descr_ref
13695 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13696 {
13697 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13698 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13699 else if (dwarf_version >= 3 || !dwarf_strict)
13700 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13701 else
13702 return NULL;
13703 }
13704
13705 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13706 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13707
13708 static dw_loc_descr_ref
13709 dw_sra_loc_expr (tree decl, rtx loc)
13710 {
13711 rtx p;
13712 unsigned int padsize = 0;
13713 dw_loc_descr_ref descr, *descr_tail;
13714 unsigned HOST_WIDE_INT decl_size;
13715 rtx varloc;
13716 enum var_init_status initialized;
13717
13718 if (DECL_SIZE (decl) == NULL
13719 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13720 return NULL;
13721
13722 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13723 descr = NULL;
13724 descr_tail = &descr;
13725
13726 for (p = loc; p; p = XEXP (p, 1))
13727 {
13728 unsigned int bitsize = decl_piece_bitsize (p);
13729 rtx loc_note = *decl_piece_varloc_ptr (p);
13730 dw_loc_descr_ref cur_descr;
13731 dw_loc_descr_ref *tail, last = NULL;
13732 unsigned int opsize = 0;
13733
13734 if (loc_note == NULL_RTX
13735 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13736 {
13737 padsize += bitsize;
13738 continue;
13739 }
13740 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13741 varloc = NOTE_VAR_LOCATION (loc_note);
13742 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13743 if (cur_descr == NULL)
13744 {
13745 padsize += bitsize;
13746 continue;
13747 }
13748
13749 /* Check that cur_descr either doesn't use
13750 DW_OP_*piece operations, or their sum is equal
13751 to bitsize. Otherwise we can't embed it. */
13752 for (tail = &cur_descr; *tail != NULL;
13753 tail = &(*tail)->dw_loc_next)
13754 if ((*tail)->dw_loc_opc == DW_OP_piece)
13755 {
13756 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13757 * BITS_PER_UNIT;
13758 last = *tail;
13759 }
13760 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13761 {
13762 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13763 last = *tail;
13764 }
13765
13766 if (last != NULL && opsize != bitsize)
13767 {
13768 padsize += bitsize;
13769 /* Discard the current piece of the descriptor and release any
13770 addr_table entries it uses. */
13771 remove_loc_list_addr_table_entries (cur_descr);
13772 continue;
13773 }
13774
13775 /* If there is a hole, add DW_OP_*piece after empty DWARF
13776 expression, which means that those bits are optimized out. */
13777 if (padsize)
13778 {
13779 if (padsize > decl_size)
13780 {
13781 remove_loc_list_addr_table_entries (cur_descr);
13782 goto discard_descr;
13783 }
13784 decl_size -= padsize;
13785 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13786 if (*descr_tail == NULL)
13787 {
13788 remove_loc_list_addr_table_entries (cur_descr);
13789 goto discard_descr;
13790 }
13791 descr_tail = &(*descr_tail)->dw_loc_next;
13792 padsize = 0;
13793 }
13794 *descr_tail = cur_descr;
13795 descr_tail = tail;
13796 if (bitsize > decl_size)
13797 goto discard_descr;
13798 decl_size -= bitsize;
13799 if (last == NULL)
13800 {
13801 HOST_WIDE_INT offset = 0;
13802 if (GET_CODE (varloc) == VAR_LOCATION
13803 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13804 {
13805 varloc = PAT_VAR_LOCATION_LOC (varloc);
13806 if (GET_CODE (varloc) == EXPR_LIST)
13807 varloc = XEXP (varloc, 0);
13808 }
13809 do
13810 {
13811 if (GET_CODE (varloc) == CONST
13812 || GET_CODE (varloc) == SIGN_EXTEND
13813 || GET_CODE (varloc) == ZERO_EXTEND)
13814 varloc = XEXP (varloc, 0);
13815 else if (GET_CODE (varloc) == SUBREG)
13816 varloc = SUBREG_REG (varloc);
13817 else
13818 break;
13819 }
13820 while (1);
13821 /* DW_OP_bit_size offset should be zero for register
13822 or implicit location descriptions and empty location
13823 descriptions, but for memory addresses needs big endian
13824 adjustment. */
13825 if (MEM_P (varloc))
13826 {
13827 unsigned HOST_WIDE_INT memsize
13828 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13829 if (memsize != bitsize)
13830 {
13831 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13832 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13833 goto discard_descr;
13834 if (memsize < bitsize)
13835 goto discard_descr;
13836 if (BITS_BIG_ENDIAN)
13837 offset = memsize - bitsize;
13838 }
13839 }
13840
13841 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13842 if (*descr_tail == NULL)
13843 goto discard_descr;
13844 descr_tail = &(*descr_tail)->dw_loc_next;
13845 }
13846 }
13847
13848 /* If there were any non-empty expressions, add padding till the end of
13849 the decl. */
13850 if (descr != NULL && decl_size != 0)
13851 {
13852 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13853 if (*descr_tail == NULL)
13854 goto discard_descr;
13855 }
13856 return descr;
13857
13858 discard_descr:
13859 /* Discard the descriptor and release any addr_table entries it uses. */
13860 remove_loc_list_addr_table_entries (descr);
13861 return NULL;
13862 }
13863
13864 /* Return the dwarf representation of the location list LOC_LIST of
13865 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13866 function. */
13867
13868 static dw_loc_list_ref
13869 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13870 {
13871 const char *endname, *secname;
13872 rtx varloc;
13873 enum var_init_status initialized;
13874 struct var_loc_node *node;
13875 dw_loc_descr_ref descr;
13876 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13877 dw_loc_list_ref list = NULL;
13878 dw_loc_list_ref *listp = &list;
13879
13880 /* Now that we know what section we are using for a base,
13881 actually construct the list of locations.
13882 The first location information is what is passed to the
13883 function that creates the location list, and the remaining
13884 locations just get added on to that list.
13885 Note that we only know the start address for a location
13886 (IE location changes), so to build the range, we use
13887 the range [current location start, next location start].
13888 This means we have to special case the last node, and generate
13889 a range of [last location start, end of function label]. */
13890
13891 secname = secname_for_decl (decl);
13892
13893 for (node = loc_list->first; node; node = node->next)
13894 if (GET_CODE (node->loc) == EXPR_LIST
13895 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13896 {
13897 if (GET_CODE (node->loc) == EXPR_LIST)
13898 {
13899 /* This requires DW_OP_{,bit_}piece, which is not usable
13900 inside DWARF expressions. */
13901 if (want_address != 2)
13902 continue;
13903 descr = dw_sra_loc_expr (decl, node->loc);
13904 if (descr == NULL)
13905 continue;
13906 }
13907 else
13908 {
13909 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13910 varloc = NOTE_VAR_LOCATION (node->loc);
13911 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13912 }
13913 if (descr)
13914 {
13915 bool range_across_switch = false;
13916 /* If section switch happens in between node->label
13917 and node->next->label (or end of function) and
13918 we can't emit it as a single entry list,
13919 emit two ranges, first one ending at the end
13920 of first partition and second one starting at the
13921 beginning of second partition. */
13922 if (node == loc_list->last_before_switch
13923 && (node != loc_list->first || loc_list->first->next)
13924 && current_function_decl)
13925 {
13926 endname = cfun->fde->dw_fde_end;
13927 range_across_switch = true;
13928 }
13929 /* The variable has a location between NODE->LABEL and
13930 NODE->NEXT->LABEL. */
13931 else if (node->next)
13932 endname = node->next->label;
13933 /* If the variable has a location at the last label
13934 it keeps its location until the end of function. */
13935 else if (!current_function_decl)
13936 endname = text_end_label;
13937 else
13938 {
13939 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13940 current_function_funcdef_no);
13941 endname = ggc_strdup (label_id);
13942 }
13943
13944 *listp = new_loc_list (descr, node->label, endname, secname);
13945 if (TREE_CODE (decl) == PARM_DECL
13946 && node == loc_list->first
13947 && NOTE_P (node->loc)
13948 && strcmp (node->label, endname) == 0)
13949 (*listp)->force = true;
13950 listp = &(*listp)->dw_loc_next;
13951
13952 if (range_across_switch)
13953 {
13954 if (GET_CODE (node->loc) == EXPR_LIST)
13955 descr = dw_sra_loc_expr (decl, node->loc);
13956 else
13957 {
13958 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13959 varloc = NOTE_VAR_LOCATION (node->loc);
13960 descr = dw_loc_list_1 (decl, varloc, want_address,
13961 initialized);
13962 }
13963 gcc_assert (descr);
13964 /* The variable has a location between NODE->LABEL and
13965 NODE->NEXT->LABEL. */
13966 if (node->next)
13967 endname = node->next->label;
13968 else
13969 endname = cfun->fde->dw_fde_second_end;
13970 *listp = new_loc_list (descr,
13971 cfun->fde->dw_fde_second_begin,
13972 endname, secname);
13973 listp = &(*listp)->dw_loc_next;
13974 }
13975 }
13976 }
13977
13978 /* Try to avoid the overhead of a location list emitting a location
13979 expression instead, but only if we didn't have more than one
13980 location entry in the first place. If some entries were not
13981 representable, we don't want to pretend a single entry that was
13982 applies to the entire scope in which the variable is
13983 available. */
13984 if (list && loc_list->first->next)
13985 gen_llsym (list);
13986
13987 return list;
13988 }
13989
13990 /* Return if the loc_list has only single element and thus can be represented
13991 as location description. */
13992
13993 static bool
13994 single_element_loc_list_p (dw_loc_list_ref list)
13995 {
13996 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13997 return !list->ll_symbol;
13998 }
13999
14000 /* To each location in list LIST add loc descr REF. */
14001
14002 static void
14003 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14004 {
14005 dw_loc_descr_ref copy;
14006 add_loc_descr (&list->expr, ref);
14007 list = list->dw_loc_next;
14008 while (list)
14009 {
14010 copy = ggc_alloc_dw_loc_descr_node ();
14011 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14012 add_loc_descr (&list->expr, copy);
14013 while (copy->dw_loc_next)
14014 {
14015 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
14016 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14017 copy->dw_loc_next = new_copy;
14018 copy = new_copy;
14019 }
14020 list = list->dw_loc_next;
14021 }
14022 }
14023
14024 /* Given two lists RET and LIST
14025 produce location list that is result of adding expression in LIST
14026 to expression in RET on each position in program.
14027 Might be destructive on both RET and LIST.
14028
14029 TODO: We handle only simple cases of RET or LIST having at most one
14030 element. General case would inolve sorting the lists in program order
14031 and merging them that will need some additional work.
14032 Adding that will improve quality of debug info especially for SRA-ed
14033 structures. */
14034
14035 static void
14036 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14037 {
14038 if (!list)
14039 return;
14040 if (!*ret)
14041 {
14042 *ret = list;
14043 return;
14044 }
14045 if (!list->dw_loc_next)
14046 {
14047 add_loc_descr_to_each (*ret, list->expr);
14048 return;
14049 }
14050 if (!(*ret)->dw_loc_next)
14051 {
14052 add_loc_descr_to_each (list, (*ret)->expr);
14053 *ret = list;
14054 return;
14055 }
14056 expansion_failed (NULL_TREE, NULL_RTX,
14057 "Don't know how to merge two non-trivial"
14058 " location lists.\n");
14059 *ret = NULL;
14060 return;
14061 }
14062
14063 /* LOC is constant expression. Try a luck, look it up in constant
14064 pool and return its loc_descr of its address. */
14065
14066 static dw_loc_descr_ref
14067 cst_pool_loc_descr (tree loc)
14068 {
14069 /* Get an RTL for this, if something has been emitted. */
14070 rtx rtl = lookup_constant_def (loc);
14071
14072 if (!rtl || !MEM_P (rtl))
14073 {
14074 gcc_assert (!rtl);
14075 return 0;
14076 }
14077 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14078
14079 /* TODO: We might get more coverage if we was actually delaying expansion
14080 of all expressions till end of compilation when constant pools are fully
14081 populated. */
14082 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14083 {
14084 expansion_failed (loc, NULL_RTX,
14085 "CST value in contant pool but not marked.");
14086 return 0;
14087 }
14088 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14089 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14090 }
14091
14092 /* Return dw_loc_list representing address of addr_expr LOC
14093 by looking for inner INDIRECT_REF expression and turning
14094 it into simple arithmetics. */
14095
14096 static dw_loc_list_ref
14097 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14098 {
14099 tree obj, offset;
14100 HOST_WIDE_INT bitsize, bitpos, bytepos;
14101 enum machine_mode mode;
14102 int unsignedp, volatilep = 0;
14103 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14104
14105 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14106 &bitsize, &bitpos, &offset, &mode,
14107 &unsignedp, &volatilep, false);
14108 STRIP_NOPS (obj);
14109 if (bitpos % BITS_PER_UNIT)
14110 {
14111 expansion_failed (loc, NULL_RTX, "bitfield access");
14112 return 0;
14113 }
14114 if (!INDIRECT_REF_P (obj))
14115 {
14116 expansion_failed (obj,
14117 NULL_RTX, "no indirect ref in inner refrence");
14118 return 0;
14119 }
14120 if (!offset && !bitpos)
14121 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14122 else if (toplev
14123 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14124 && (dwarf_version >= 4 || !dwarf_strict))
14125 {
14126 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14127 if (!list_ret)
14128 return 0;
14129 if (offset)
14130 {
14131 /* Variable offset. */
14132 list_ret1 = loc_list_from_tree (offset, 0);
14133 if (list_ret1 == 0)
14134 return 0;
14135 add_loc_list (&list_ret, list_ret1);
14136 if (!list_ret)
14137 return 0;
14138 add_loc_descr_to_each (list_ret,
14139 new_loc_descr (DW_OP_plus, 0, 0));
14140 }
14141 bytepos = bitpos / BITS_PER_UNIT;
14142 if (bytepos > 0)
14143 add_loc_descr_to_each (list_ret,
14144 new_loc_descr (DW_OP_plus_uconst,
14145 bytepos, 0));
14146 else if (bytepos < 0)
14147 loc_list_plus_const (list_ret, bytepos);
14148 add_loc_descr_to_each (list_ret,
14149 new_loc_descr (DW_OP_stack_value, 0, 0));
14150 }
14151 return list_ret;
14152 }
14153
14154
14155 /* Generate Dwarf location list representing LOC.
14156 If WANT_ADDRESS is false, expression computing LOC will be computed
14157 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14158 if WANT_ADDRESS is 2, expression computing address useable in location
14159 will be returned (i.e. DW_OP_reg can be used
14160 to refer to register values). */
14161
14162 static dw_loc_list_ref
14163 loc_list_from_tree (tree loc, int want_address)
14164 {
14165 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14166 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14167 int have_address = 0;
14168 enum dwarf_location_atom op;
14169
14170 /* ??? Most of the time we do not take proper care for sign/zero
14171 extending the values properly. Hopefully this won't be a real
14172 problem... */
14173
14174 switch (TREE_CODE (loc))
14175 {
14176 case ERROR_MARK:
14177 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14178 return 0;
14179
14180 case PLACEHOLDER_EXPR:
14181 /* This case involves extracting fields from an object to determine the
14182 position of other fields. We don't try to encode this here. The
14183 only user of this is Ada, which encodes the needed information using
14184 the names of types. */
14185 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14186 return 0;
14187
14188 case CALL_EXPR:
14189 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14190 /* There are no opcodes for these operations. */
14191 return 0;
14192
14193 case PREINCREMENT_EXPR:
14194 case PREDECREMENT_EXPR:
14195 case POSTINCREMENT_EXPR:
14196 case POSTDECREMENT_EXPR:
14197 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14198 /* There are no opcodes for these operations. */
14199 return 0;
14200
14201 case ADDR_EXPR:
14202 /* If we already want an address, see if there is INDIRECT_REF inside
14203 e.g. for &this->field. */
14204 if (want_address)
14205 {
14206 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14207 (loc, want_address == 2);
14208 if (list_ret)
14209 have_address = 1;
14210 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14211 && (ret = cst_pool_loc_descr (loc)))
14212 have_address = 1;
14213 }
14214 /* Otherwise, process the argument and look for the address. */
14215 if (!list_ret && !ret)
14216 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14217 else
14218 {
14219 if (want_address)
14220 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14221 return NULL;
14222 }
14223 break;
14224
14225 case VAR_DECL:
14226 if (DECL_THREAD_LOCAL_P (loc))
14227 {
14228 rtx rtl;
14229 enum dwarf_location_atom tls_op;
14230 enum dtprel_bool dtprel = dtprel_false;
14231
14232 if (targetm.have_tls)
14233 {
14234 /* If this is not defined, we have no way to emit the
14235 data. */
14236 if (!targetm.asm_out.output_dwarf_dtprel)
14237 return 0;
14238
14239 /* The way DW_OP_GNU_push_tls_address is specified, we
14240 can only look up addresses of objects in the current
14241 module. We used DW_OP_addr as first op, but that's
14242 wrong, because DW_OP_addr is relocated by the debug
14243 info consumer, while DW_OP_GNU_push_tls_address
14244 operand shouldn't be. */
14245 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14246 return 0;
14247 dtprel = dtprel_true;
14248 tls_op = DW_OP_GNU_push_tls_address;
14249 }
14250 else
14251 {
14252 if (!targetm.emutls.debug_form_tls_address
14253 || !(dwarf_version >= 3 || !dwarf_strict))
14254 return 0;
14255 /* We stuffed the control variable into the DECL_VALUE_EXPR
14256 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14257 no longer appear in gimple code. We used the control
14258 variable in specific so that we could pick it up here. */
14259 loc = DECL_VALUE_EXPR (loc);
14260 tls_op = DW_OP_form_tls_address;
14261 }
14262
14263 rtl = rtl_for_decl_location (loc);
14264 if (rtl == NULL_RTX)
14265 return 0;
14266
14267 if (!MEM_P (rtl))
14268 return 0;
14269 rtl = XEXP (rtl, 0);
14270 if (! CONSTANT_P (rtl))
14271 return 0;
14272
14273 ret = new_addr_loc_descr (rtl, dtprel);
14274 ret1 = new_loc_descr (tls_op, 0, 0);
14275 add_loc_descr (&ret, ret1);
14276
14277 have_address = 1;
14278 break;
14279 }
14280 /* FALLTHRU */
14281
14282 case PARM_DECL:
14283 case RESULT_DECL:
14284 if (DECL_HAS_VALUE_EXPR_P (loc))
14285 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14286 want_address);
14287 /* FALLTHRU */
14288
14289 case FUNCTION_DECL:
14290 {
14291 rtx rtl;
14292 var_loc_list *loc_list = lookup_decl_loc (loc);
14293
14294 if (loc_list && loc_list->first)
14295 {
14296 list_ret = dw_loc_list (loc_list, loc, want_address);
14297 have_address = want_address != 0;
14298 break;
14299 }
14300 rtl = rtl_for_decl_location (loc);
14301 if (rtl == NULL_RTX)
14302 {
14303 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14304 return 0;
14305 }
14306 else if (CONST_INT_P (rtl))
14307 {
14308 HOST_WIDE_INT val = INTVAL (rtl);
14309 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14310 val &= GET_MODE_MASK (DECL_MODE (loc));
14311 ret = int_loc_descriptor (val);
14312 }
14313 else if (GET_CODE (rtl) == CONST_STRING)
14314 {
14315 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14316 return 0;
14317 }
14318 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14319 ret = new_addr_loc_descr (rtl, dtprel_false);
14320 else
14321 {
14322 enum machine_mode mode, mem_mode;
14323
14324 /* Certain constructs can only be represented at top-level. */
14325 if (want_address == 2)
14326 {
14327 ret = loc_descriptor (rtl, VOIDmode,
14328 VAR_INIT_STATUS_INITIALIZED);
14329 have_address = 1;
14330 }
14331 else
14332 {
14333 mode = GET_MODE (rtl);
14334 mem_mode = VOIDmode;
14335 if (MEM_P (rtl))
14336 {
14337 mem_mode = mode;
14338 mode = get_address_mode (rtl);
14339 rtl = XEXP (rtl, 0);
14340 have_address = 1;
14341 }
14342 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14343 VAR_INIT_STATUS_INITIALIZED);
14344 }
14345 if (!ret)
14346 expansion_failed (loc, rtl,
14347 "failed to produce loc descriptor for rtl");
14348 }
14349 }
14350 break;
14351
14352 case MEM_REF:
14353 /* ??? FIXME. */
14354 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14355 return 0;
14356 /* Fallthru. */
14357 case INDIRECT_REF:
14358 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14359 have_address = 1;
14360 break;
14361
14362 case COMPOUND_EXPR:
14363 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14364
14365 CASE_CONVERT:
14366 case VIEW_CONVERT_EXPR:
14367 case SAVE_EXPR:
14368 case MODIFY_EXPR:
14369 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14370
14371 case COMPONENT_REF:
14372 case BIT_FIELD_REF:
14373 case ARRAY_REF:
14374 case ARRAY_RANGE_REF:
14375 case REALPART_EXPR:
14376 case IMAGPART_EXPR:
14377 {
14378 tree obj, offset;
14379 HOST_WIDE_INT bitsize, bitpos, bytepos;
14380 enum machine_mode mode;
14381 int unsignedp, volatilep = 0;
14382
14383 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14384 &unsignedp, &volatilep, false);
14385
14386 gcc_assert (obj != loc);
14387
14388 list_ret = loc_list_from_tree (obj,
14389 want_address == 2
14390 && !bitpos && !offset ? 2 : 1);
14391 /* TODO: We can extract value of the small expression via shifting even
14392 for nonzero bitpos. */
14393 if (list_ret == 0)
14394 return 0;
14395 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14396 {
14397 expansion_failed (loc, NULL_RTX,
14398 "bitfield access");
14399 return 0;
14400 }
14401
14402 if (offset != NULL_TREE)
14403 {
14404 /* Variable offset. */
14405 list_ret1 = loc_list_from_tree (offset, 0);
14406 if (list_ret1 == 0)
14407 return 0;
14408 add_loc_list (&list_ret, list_ret1);
14409 if (!list_ret)
14410 return 0;
14411 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14412 }
14413
14414 bytepos = bitpos / BITS_PER_UNIT;
14415 if (bytepos > 0)
14416 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14417 else if (bytepos < 0)
14418 loc_list_plus_const (list_ret, bytepos);
14419
14420 have_address = 1;
14421 break;
14422 }
14423
14424 case INTEGER_CST:
14425 if ((want_address || !tree_fits_shwi_p (loc))
14426 && (ret = cst_pool_loc_descr (loc)))
14427 have_address = 1;
14428 else if (want_address == 2
14429 && tree_fits_shwi_p (loc)
14430 && (ret = address_of_int_loc_descriptor
14431 (int_size_in_bytes (TREE_TYPE (loc)),
14432 tree_to_shwi (loc))))
14433 have_address = 1;
14434 else if (tree_fits_shwi_p (loc))
14435 ret = int_loc_descriptor (tree_to_shwi (loc));
14436 else
14437 {
14438 expansion_failed (loc, NULL_RTX,
14439 "Integer operand is not host integer");
14440 return 0;
14441 }
14442 break;
14443
14444 case CONSTRUCTOR:
14445 case REAL_CST:
14446 case STRING_CST:
14447 case COMPLEX_CST:
14448 if ((ret = cst_pool_loc_descr (loc)))
14449 have_address = 1;
14450 else
14451 /* We can construct small constants here using int_loc_descriptor. */
14452 expansion_failed (loc, NULL_RTX,
14453 "constructor or constant not in constant pool");
14454 break;
14455
14456 case TRUTH_AND_EXPR:
14457 case TRUTH_ANDIF_EXPR:
14458 case BIT_AND_EXPR:
14459 op = DW_OP_and;
14460 goto do_binop;
14461
14462 case TRUTH_XOR_EXPR:
14463 case BIT_XOR_EXPR:
14464 op = DW_OP_xor;
14465 goto do_binop;
14466
14467 case TRUTH_OR_EXPR:
14468 case TRUTH_ORIF_EXPR:
14469 case BIT_IOR_EXPR:
14470 op = DW_OP_or;
14471 goto do_binop;
14472
14473 case FLOOR_DIV_EXPR:
14474 case CEIL_DIV_EXPR:
14475 case ROUND_DIV_EXPR:
14476 case TRUNC_DIV_EXPR:
14477 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14478 return 0;
14479 op = DW_OP_div;
14480 goto do_binop;
14481
14482 case MINUS_EXPR:
14483 op = DW_OP_minus;
14484 goto do_binop;
14485
14486 case FLOOR_MOD_EXPR:
14487 case CEIL_MOD_EXPR:
14488 case ROUND_MOD_EXPR:
14489 case TRUNC_MOD_EXPR:
14490 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14491 {
14492 op = DW_OP_mod;
14493 goto do_binop;
14494 }
14495 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14496 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14497 if (list_ret == 0 || list_ret1 == 0)
14498 return 0;
14499
14500 add_loc_list (&list_ret, list_ret1);
14501 if (list_ret == 0)
14502 return 0;
14503 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14504 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14505 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14506 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14507 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14508 break;
14509
14510 case MULT_EXPR:
14511 op = DW_OP_mul;
14512 goto do_binop;
14513
14514 case LSHIFT_EXPR:
14515 op = DW_OP_shl;
14516 goto do_binop;
14517
14518 case RSHIFT_EXPR:
14519 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14520 goto do_binop;
14521
14522 case POINTER_PLUS_EXPR:
14523 case PLUS_EXPR:
14524 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14525 {
14526 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14527 if (list_ret == 0)
14528 return 0;
14529
14530 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14531 break;
14532 }
14533
14534 op = DW_OP_plus;
14535 goto do_binop;
14536
14537 case LE_EXPR:
14538 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14539 return 0;
14540
14541 op = DW_OP_le;
14542 goto do_binop;
14543
14544 case GE_EXPR:
14545 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14546 return 0;
14547
14548 op = DW_OP_ge;
14549 goto do_binop;
14550
14551 case LT_EXPR:
14552 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14553 return 0;
14554
14555 op = DW_OP_lt;
14556 goto do_binop;
14557
14558 case GT_EXPR:
14559 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14560 return 0;
14561
14562 op = DW_OP_gt;
14563 goto do_binop;
14564
14565 case EQ_EXPR:
14566 op = DW_OP_eq;
14567 goto do_binop;
14568
14569 case NE_EXPR:
14570 op = DW_OP_ne;
14571 goto do_binop;
14572
14573 do_binop:
14574 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14575 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14576 if (list_ret == 0 || list_ret1 == 0)
14577 return 0;
14578
14579 add_loc_list (&list_ret, list_ret1);
14580 if (list_ret == 0)
14581 return 0;
14582 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14583 break;
14584
14585 case TRUTH_NOT_EXPR:
14586 case BIT_NOT_EXPR:
14587 op = DW_OP_not;
14588 goto do_unop;
14589
14590 case ABS_EXPR:
14591 op = DW_OP_abs;
14592 goto do_unop;
14593
14594 case NEGATE_EXPR:
14595 op = DW_OP_neg;
14596 goto do_unop;
14597
14598 do_unop:
14599 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14600 if (list_ret == 0)
14601 return 0;
14602
14603 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14604 break;
14605
14606 case MIN_EXPR:
14607 case MAX_EXPR:
14608 {
14609 const enum tree_code code =
14610 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14611
14612 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14613 build2 (code, integer_type_node,
14614 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14615 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14616 }
14617
14618 /* ... fall through ... */
14619
14620 case COND_EXPR:
14621 {
14622 dw_loc_descr_ref lhs
14623 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14624 dw_loc_list_ref rhs
14625 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14626 dw_loc_descr_ref bra_node, jump_node, tmp;
14627
14628 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14629 if (list_ret == 0 || lhs == 0 || rhs == 0)
14630 return 0;
14631
14632 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14633 add_loc_descr_to_each (list_ret, bra_node);
14634
14635 add_loc_list (&list_ret, rhs);
14636 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14637 add_loc_descr_to_each (list_ret, jump_node);
14638
14639 add_loc_descr_to_each (list_ret, lhs);
14640 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14641 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14642
14643 /* ??? Need a node to point the skip at. Use a nop. */
14644 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14645 add_loc_descr_to_each (list_ret, tmp);
14646 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14647 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14648 }
14649 break;
14650
14651 case FIX_TRUNC_EXPR:
14652 return 0;
14653
14654 default:
14655 /* Leave front-end specific codes as simply unknown. This comes
14656 up, for instance, with the C STMT_EXPR. */
14657 if ((unsigned int) TREE_CODE (loc)
14658 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14659 {
14660 expansion_failed (loc, NULL_RTX,
14661 "language specific tree node");
14662 return 0;
14663 }
14664
14665 #ifdef ENABLE_CHECKING
14666 /* Otherwise this is a generic code; we should just lists all of
14667 these explicitly. We forgot one. */
14668 gcc_unreachable ();
14669 #else
14670 /* In a release build, we want to degrade gracefully: better to
14671 generate incomplete debugging information than to crash. */
14672 return NULL;
14673 #endif
14674 }
14675
14676 if (!ret && !list_ret)
14677 return 0;
14678
14679 if (want_address == 2 && !have_address
14680 && (dwarf_version >= 4 || !dwarf_strict))
14681 {
14682 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14683 {
14684 expansion_failed (loc, NULL_RTX,
14685 "DWARF address size mismatch");
14686 return 0;
14687 }
14688 if (ret)
14689 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14690 else
14691 add_loc_descr_to_each (list_ret,
14692 new_loc_descr (DW_OP_stack_value, 0, 0));
14693 have_address = 1;
14694 }
14695 /* Show if we can't fill the request for an address. */
14696 if (want_address && !have_address)
14697 {
14698 expansion_failed (loc, NULL_RTX,
14699 "Want address and only have value");
14700 return 0;
14701 }
14702
14703 gcc_assert (!ret || !list_ret);
14704
14705 /* If we've got an address and don't want one, dereference. */
14706 if (!want_address && have_address)
14707 {
14708 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14709
14710 if (size > DWARF2_ADDR_SIZE || size == -1)
14711 {
14712 expansion_failed (loc, NULL_RTX,
14713 "DWARF address size mismatch");
14714 return 0;
14715 }
14716 else if (size == DWARF2_ADDR_SIZE)
14717 op = DW_OP_deref;
14718 else
14719 op = DW_OP_deref_size;
14720
14721 if (ret)
14722 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14723 else
14724 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14725 }
14726 if (ret)
14727 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14728
14729 return list_ret;
14730 }
14731
14732 /* Same as above but return only single location expression. */
14733 static dw_loc_descr_ref
14734 loc_descriptor_from_tree (tree loc, int want_address)
14735 {
14736 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14737 if (!ret)
14738 return NULL;
14739 if (ret->dw_loc_next)
14740 {
14741 expansion_failed (loc, NULL_RTX,
14742 "Location list where only loc descriptor needed");
14743 return NULL;
14744 }
14745 return ret->expr;
14746 }
14747
14748 /* Given a value, round it up to the lowest multiple of `boundary'
14749 which is not less than the value itself. */
14750
14751 static inline HOST_WIDE_INT
14752 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14753 {
14754 return (((value + boundary - 1) / boundary) * boundary);
14755 }
14756
14757 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14758 pointer to the declared type for the relevant field variable, or return
14759 `integer_type_node' if the given node turns out to be an
14760 ERROR_MARK node. */
14761
14762 static inline tree
14763 field_type (const_tree decl)
14764 {
14765 tree type;
14766
14767 if (TREE_CODE (decl) == ERROR_MARK)
14768 return integer_type_node;
14769
14770 type = DECL_BIT_FIELD_TYPE (decl);
14771 if (type == NULL_TREE)
14772 type = TREE_TYPE (decl);
14773
14774 return type;
14775 }
14776
14777 /* Given a pointer to a tree node, return the alignment in bits for
14778 it, or else return BITS_PER_WORD if the node actually turns out to
14779 be an ERROR_MARK node. */
14780
14781 static inline unsigned
14782 simple_type_align_in_bits (const_tree type)
14783 {
14784 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14785 }
14786
14787 static inline unsigned
14788 simple_decl_align_in_bits (const_tree decl)
14789 {
14790 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14791 }
14792
14793 /* Return the result of rounding T up to ALIGN. */
14794
14795 static inline offset_int
14796 round_up_to_align (const offset_int &t, unsigned int align)
14797 {
14798 return wi::udiv_trunc (t + align - 1, align) * align;
14799 }
14800
14801 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14802 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14803 or return 0 if we are unable to determine what that offset is, either
14804 because the argument turns out to be a pointer to an ERROR_MARK node, or
14805 because the offset is actually variable. (We can't handle the latter case
14806 just yet). */
14807
14808 static HOST_WIDE_INT
14809 field_byte_offset (const_tree decl)
14810 {
14811 offset_int object_offset_in_bits;
14812 offset_int object_offset_in_bytes;
14813 offset_int bitpos_int;
14814
14815 if (TREE_CODE (decl) == ERROR_MARK)
14816 return 0;
14817
14818 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14819
14820 /* We cannot yet cope with fields whose positions are variable, so
14821 for now, when we see such things, we simply return 0. Someday, we may
14822 be able to handle such cases, but it will be damn difficult. */
14823 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14824 return 0;
14825
14826 bitpos_int = wi::to_offset (bit_position (decl));
14827
14828 #ifdef PCC_BITFIELD_TYPE_MATTERS
14829 if (PCC_BITFIELD_TYPE_MATTERS)
14830 {
14831 tree type;
14832 tree field_size_tree;
14833 offset_int deepest_bitpos;
14834 offset_int field_size_in_bits;
14835 unsigned int type_align_in_bits;
14836 unsigned int decl_align_in_bits;
14837 offset_int type_size_in_bits;
14838
14839 type = field_type (decl);
14840 type_size_in_bits = offset_int_type_size_in_bits (type);
14841 type_align_in_bits = simple_type_align_in_bits (type);
14842
14843 field_size_tree = DECL_SIZE (decl);
14844
14845 /* The size could be unspecified if there was an error, or for
14846 a flexible array member. */
14847 if (!field_size_tree)
14848 field_size_tree = bitsize_zero_node;
14849
14850 /* If the size of the field is not constant, use the type size. */
14851 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14852 field_size_in_bits = wi::to_offset (field_size_tree);
14853 else
14854 field_size_in_bits = type_size_in_bits;
14855
14856 decl_align_in_bits = simple_decl_align_in_bits (decl);
14857
14858 /* The GCC front-end doesn't make any attempt to keep track of the
14859 starting bit offset (relative to the start of the containing
14860 structure type) of the hypothetical "containing object" for a
14861 bit-field. Thus, when computing the byte offset value for the
14862 start of the "containing object" of a bit-field, we must deduce
14863 this information on our own. This can be rather tricky to do in
14864 some cases. For example, handling the following structure type
14865 definition when compiling for an i386/i486 target (which only
14866 aligns long long's to 32-bit boundaries) can be very tricky:
14867
14868 struct S { int field1; long long field2:31; };
14869
14870 Fortunately, there is a simple rule-of-thumb which can be used
14871 in such cases. When compiling for an i386/i486, GCC will
14872 allocate 8 bytes for the structure shown above. It decides to
14873 do this based upon one simple rule for bit-field allocation.
14874 GCC allocates each "containing object" for each bit-field at
14875 the first (i.e. lowest addressed) legitimate alignment boundary
14876 (based upon the required minimum alignment for the declared
14877 type of the field) which it can possibly use, subject to the
14878 condition that there is still enough available space remaining
14879 in the containing object (when allocated at the selected point)
14880 to fully accommodate all of the bits of the bit-field itself.
14881
14882 This simple rule makes it obvious why GCC allocates 8 bytes for
14883 each object of the structure type shown above. When looking
14884 for a place to allocate the "containing object" for `field2',
14885 the compiler simply tries to allocate a 64-bit "containing
14886 object" at each successive 32-bit boundary (starting at zero)
14887 until it finds a place to allocate that 64- bit field such that
14888 at least 31 contiguous (and previously unallocated) bits remain
14889 within that selected 64 bit field. (As it turns out, for the
14890 example above, the compiler finds it is OK to allocate the
14891 "containing object" 64-bit field at bit-offset zero within the
14892 structure type.)
14893
14894 Here we attempt to work backwards from the limited set of facts
14895 we're given, and we try to deduce from those facts, where GCC
14896 must have believed that the containing object started (within
14897 the structure type). The value we deduce is then used (by the
14898 callers of this routine) to generate DW_AT_location and
14899 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14900 the case of DW_AT_location, regular fields as well). */
14901
14902 /* Figure out the bit-distance from the start of the structure to
14903 the "deepest" bit of the bit-field. */
14904 deepest_bitpos = bitpos_int + field_size_in_bits;
14905
14906 /* This is the tricky part. Use some fancy footwork to deduce
14907 where the lowest addressed bit of the containing object must
14908 be. */
14909 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14910
14911 /* Round up to type_align by default. This works best for
14912 bitfields. */
14913 object_offset_in_bits
14914 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14915
14916 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
14917 {
14918 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14919
14920 /* Round up to decl_align instead. */
14921 object_offset_in_bits
14922 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14923 }
14924 }
14925 else
14926 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14927 object_offset_in_bits = bitpos_int;
14928
14929 object_offset_in_bytes
14930 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
14931 return object_offset_in_bytes.to_shwi ();
14932 }
14933 \f
14934 /* The following routines define various Dwarf attributes and any data
14935 associated with them. */
14936
14937 /* Add a location description attribute value to a DIE.
14938
14939 This emits location attributes suitable for whole variables and
14940 whole parameters. Note that the location attributes for struct fields are
14941 generated by the routine `data_member_location_attribute' below. */
14942
14943 static inline void
14944 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14945 dw_loc_list_ref descr)
14946 {
14947 if (descr == 0)
14948 return;
14949 if (single_element_loc_list_p (descr))
14950 add_AT_loc (die, attr_kind, descr->expr);
14951 else
14952 add_AT_loc_list (die, attr_kind, descr);
14953 }
14954
14955 /* Add DW_AT_accessibility attribute to DIE if needed. */
14956
14957 static void
14958 add_accessibility_attribute (dw_die_ref die, tree decl)
14959 {
14960 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14961 children, otherwise the default is DW_ACCESS_public. In DWARF2
14962 the default has always been DW_ACCESS_public. */
14963 if (TREE_PROTECTED (decl))
14964 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14965 else if (TREE_PRIVATE (decl))
14966 {
14967 if (dwarf_version == 2
14968 || die->die_parent == NULL
14969 || die->die_parent->die_tag != DW_TAG_class_type)
14970 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14971 }
14972 else if (dwarf_version > 2
14973 && die->die_parent
14974 && die->die_parent->die_tag == DW_TAG_class_type)
14975 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14976 }
14977
14978 /* Attach the specialized form of location attribute used for data members of
14979 struct and union types. In the special case of a FIELD_DECL node which
14980 represents a bit-field, the "offset" part of this special location
14981 descriptor must indicate the distance in bytes from the lowest-addressed
14982 byte of the containing struct or union type to the lowest-addressed byte of
14983 the "containing object" for the bit-field. (See the `field_byte_offset'
14984 function above).
14985
14986 For any given bit-field, the "containing object" is a hypothetical object
14987 (of some integral or enum type) within which the given bit-field lives. The
14988 type of this hypothetical "containing object" is always the same as the
14989 declared type of the individual bit-field itself (for GCC anyway... the
14990 DWARF spec doesn't actually mandate this). Note that it is the size (in
14991 bytes) of the hypothetical "containing object" which will be given in the
14992 DW_AT_byte_size attribute for this bit-field. (See the
14993 `byte_size_attribute' function below.) It is also used when calculating the
14994 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14995 function below.) */
14996
14997 static void
14998 add_data_member_location_attribute (dw_die_ref die, tree decl)
14999 {
15000 HOST_WIDE_INT offset;
15001 dw_loc_descr_ref loc_descr = 0;
15002
15003 if (TREE_CODE (decl) == TREE_BINFO)
15004 {
15005 /* We're working on the TAG_inheritance for a base class. */
15006 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15007 {
15008 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15009 aren't at a fixed offset from all (sub)objects of the same
15010 type. We need to extract the appropriate offset from our
15011 vtable. The following dwarf expression means
15012
15013 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15014
15015 This is specific to the V3 ABI, of course. */
15016
15017 dw_loc_descr_ref tmp;
15018
15019 /* Make a copy of the object address. */
15020 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15021 add_loc_descr (&loc_descr, tmp);
15022
15023 /* Extract the vtable address. */
15024 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15025 add_loc_descr (&loc_descr, tmp);
15026
15027 /* Calculate the address of the offset. */
15028 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15029 gcc_assert (offset < 0);
15030
15031 tmp = int_loc_descriptor (-offset);
15032 add_loc_descr (&loc_descr, tmp);
15033 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15034 add_loc_descr (&loc_descr, tmp);
15035
15036 /* Extract the offset. */
15037 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15038 add_loc_descr (&loc_descr, tmp);
15039
15040 /* Add it to the object address. */
15041 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15042 add_loc_descr (&loc_descr, tmp);
15043 }
15044 else
15045 offset = tree_to_shwi (BINFO_OFFSET (decl));
15046 }
15047 else
15048 offset = field_byte_offset (decl);
15049
15050 if (! loc_descr)
15051 {
15052 if (dwarf_version > 2)
15053 {
15054 /* Don't need to output a location expression, just the constant. */
15055 if (offset < 0)
15056 add_AT_int (die, DW_AT_data_member_location, offset);
15057 else
15058 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15059 return;
15060 }
15061 else
15062 {
15063 enum dwarf_location_atom op;
15064
15065 /* The DWARF2 standard says that we should assume that the structure
15066 address is already on the stack, so we can specify a structure
15067 field address by using DW_OP_plus_uconst. */
15068 op = DW_OP_plus_uconst;
15069 loc_descr = new_loc_descr (op, offset, 0);
15070 }
15071 }
15072
15073 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15074 }
15075
15076 /* Writes integer values to dw_vec_const array. */
15077
15078 static void
15079 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15080 {
15081 while (size != 0)
15082 {
15083 *dest++ = val & 0xff;
15084 val >>= 8;
15085 --size;
15086 }
15087 }
15088
15089 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15090
15091 static HOST_WIDE_INT
15092 extract_int (const unsigned char *src, unsigned int size)
15093 {
15094 HOST_WIDE_INT val = 0;
15095
15096 src += size;
15097 while (size != 0)
15098 {
15099 val <<= 8;
15100 val |= *--src & 0xff;
15101 --size;
15102 }
15103 return val;
15104 }
15105
15106 /* Writes wide_int values to dw_vec_const array. */
15107
15108 static void
15109 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15110 {
15111 int i;
15112
15113 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15114 {
15115 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15116 return;
15117 }
15118
15119 /* We'd have to extend this code to support odd sizes. */
15120 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT) == 0);
15121
15122 int n = elt_size / (HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT);
15123
15124 if (WORDS_BIG_ENDIAN)
15125 for (i = n - 1; i >= 0; i--)
15126 {
15127 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15128 dest += sizeof (HOST_WIDE_INT);
15129 }
15130 else
15131 for (i = 0; i < n; i++)
15132 {
15133 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15134 dest += sizeof (HOST_WIDE_INT);
15135 }
15136 }
15137
15138 /* Writes floating point values to dw_vec_const array. */
15139
15140 static void
15141 insert_float (const_rtx rtl, unsigned char *array)
15142 {
15143 REAL_VALUE_TYPE rv;
15144 long val[4];
15145 int i;
15146
15147 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15148 real_to_target (val, &rv, GET_MODE (rtl));
15149
15150 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15151 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15152 {
15153 insert_int (val[i], 4, array);
15154 array += 4;
15155 }
15156 }
15157
15158 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15159 does not have a "location" either in memory or in a register. These
15160 things can arise in GNU C when a constant is passed as an actual parameter
15161 to an inlined function. They can also arise in C++ where declared
15162 constants do not necessarily get memory "homes". */
15163
15164 static bool
15165 add_const_value_attribute (dw_die_ref die, rtx rtl)
15166 {
15167 switch (GET_CODE (rtl))
15168 {
15169 case CONST_INT:
15170 {
15171 HOST_WIDE_INT val = INTVAL (rtl);
15172
15173 if (val < 0)
15174 add_AT_int (die, DW_AT_const_value, val);
15175 else
15176 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15177 }
15178 return true;
15179
15180 case CONST_WIDE_INT:
15181 add_AT_wide (die, DW_AT_const_value,
15182 std::make_pair (rtl, GET_MODE (rtl)));
15183 return true;
15184
15185 case CONST_DOUBLE:
15186 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15187 floating-point constant. A CONST_DOUBLE is used whenever the
15188 constant requires more than one word in order to be adequately
15189 represented. */
15190 {
15191 enum machine_mode mode = GET_MODE (rtl);
15192
15193 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15194 add_AT_double (die, DW_AT_const_value,
15195 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15196 else
15197 {
15198 unsigned int length = GET_MODE_SIZE (mode);
15199 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
15200
15201 insert_float (rtl, array);
15202 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15203 }
15204 }
15205 return true;
15206
15207 case CONST_VECTOR:
15208 {
15209 enum machine_mode mode = GET_MODE (rtl);
15210 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15211 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15212 unsigned char *array = (unsigned char *) ggc_alloc_atomic
15213 (length * elt_size);
15214 unsigned int i;
15215 unsigned char *p;
15216 enum machine_mode imode = GET_MODE_INNER (mode);
15217
15218 switch (GET_MODE_CLASS (mode))
15219 {
15220 case MODE_VECTOR_INT:
15221 for (i = 0, p = array; i < length; i++, p += elt_size)
15222 {
15223 rtx elt = CONST_VECTOR_ELT (rtl, i);
15224 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15225 }
15226 break;
15227
15228 case MODE_VECTOR_FLOAT:
15229 for (i = 0, p = array; i < length; i++, p += elt_size)
15230 {
15231 rtx elt = CONST_VECTOR_ELT (rtl, i);
15232 insert_float (elt, p);
15233 }
15234 break;
15235
15236 default:
15237 gcc_unreachable ();
15238 }
15239
15240 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15241 }
15242 return true;
15243
15244 case CONST_STRING:
15245 if (dwarf_version >= 4 || !dwarf_strict)
15246 {
15247 dw_loc_descr_ref loc_result;
15248 resolve_one_addr (&rtl, NULL);
15249 rtl_addr:
15250 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15251 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15252 add_AT_loc (die, DW_AT_location, loc_result);
15253 vec_safe_push (used_rtx_array, rtl);
15254 return true;
15255 }
15256 return false;
15257
15258 case CONST:
15259 if (CONSTANT_P (XEXP (rtl, 0)))
15260 return add_const_value_attribute (die, XEXP (rtl, 0));
15261 /* FALLTHROUGH */
15262 case SYMBOL_REF:
15263 if (!const_ok_for_output (rtl))
15264 return false;
15265 case LABEL_REF:
15266 if (dwarf_version >= 4 || !dwarf_strict)
15267 goto rtl_addr;
15268 return false;
15269
15270 case PLUS:
15271 /* In cases where an inlined instance of an inline function is passed
15272 the address of an `auto' variable (which is local to the caller) we
15273 can get a situation where the DECL_RTL of the artificial local
15274 variable (for the inlining) which acts as a stand-in for the
15275 corresponding formal parameter (of the inline function) will look
15276 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15277 exactly a compile-time constant expression, but it isn't the address
15278 of the (artificial) local variable either. Rather, it represents the
15279 *value* which the artificial local variable always has during its
15280 lifetime. We currently have no way to represent such quasi-constant
15281 values in Dwarf, so for now we just punt and generate nothing. */
15282 return false;
15283
15284 case HIGH:
15285 case CONST_FIXED:
15286 return false;
15287
15288 case MEM:
15289 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15290 && MEM_READONLY_P (rtl)
15291 && GET_MODE (rtl) == BLKmode)
15292 {
15293 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15294 return true;
15295 }
15296 return false;
15297
15298 default:
15299 /* No other kinds of rtx should be possible here. */
15300 gcc_unreachable ();
15301 }
15302 return false;
15303 }
15304
15305 /* Determine whether the evaluation of EXPR references any variables
15306 or functions which aren't otherwise used (and therefore may not be
15307 output). */
15308 static tree
15309 reference_to_unused (tree * tp, int * walk_subtrees,
15310 void * data ATTRIBUTE_UNUSED)
15311 {
15312 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15313 *walk_subtrees = 0;
15314
15315 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15316 && ! TREE_ASM_WRITTEN (*tp))
15317 return *tp;
15318 /* ??? The C++ FE emits debug information for using decls, so
15319 putting gcc_unreachable here falls over. See PR31899. For now
15320 be conservative. */
15321 else if (!cgraph_global_info_ready
15322 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15323 return *tp;
15324 else if (TREE_CODE (*tp) == VAR_DECL)
15325 {
15326 struct varpool_node *node = varpool_get_node (*tp);
15327 if (!node || !node->definition)
15328 return *tp;
15329 }
15330 else if (TREE_CODE (*tp) == FUNCTION_DECL
15331 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15332 {
15333 /* The call graph machinery must have finished analyzing,
15334 optimizing and gimplifying the CU by now.
15335 So if *TP has no call graph node associated
15336 to it, it means *TP will not be emitted. */
15337 if (!cgraph_get_node (*tp))
15338 return *tp;
15339 }
15340 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15341 return *tp;
15342
15343 return NULL_TREE;
15344 }
15345
15346 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15347 for use in a later add_const_value_attribute call. */
15348
15349 static rtx
15350 rtl_for_decl_init (tree init, tree type)
15351 {
15352 rtx rtl = NULL_RTX;
15353
15354 STRIP_NOPS (init);
15355
15356 /* If a variable is initialized with a string constant without embedded
15357 zeros, build CONST_STRING. */
15358 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15359 {
15360 tree enttype = TREE_TYPE (type);
15361 tree domain = TYPE_DOMAIN (type);
15362 enum machine_mode mode = TYPE_MODE (enttype);
15363
15364 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15365 && domain
15366 && integer_zerop (TYPE_MIN_VALUE (domain))
15367 && compare_tree_int (TYPE_MAX_VALUE (domain),
15368 TREE_STRING_LENGTH (init) - 1) == 0
15369 && ((size_t) TREE_STRING_LENGTH (init)
15370 == strlen (TREE_STRING_POINTER (init)) + 1))
15371 {
15372 rtl = gen_rtx_CONST_STRING (VOIDmode,
15373 ggc_strdup (TREE_STRING_POINTER (init)));
15374 rtl = gen_rtx_MEM (BLKmode, rtl);
15375 MEM_READONLY_P (rtl) = 1;
15376 }
15377 }
15378 /* Other aggregates, and complex values, could be represented using
15379 CONCAT: FIXME! */
15380 else if (AGGREGATE_TYPE_P (type)
15381 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15382 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15383 || TREE_CODE (type) == COMPLEX_TYPE)
15384 ;
15385 /* Vectors only work if their mode is supported by the target.
15386 FIXME: generic vectors ought to work too. */
15387 else if (TREE_CODE (type) == VECTOR_TYPE
15388 && !VECTOR_MODE_P (TYPE_MODE (type)))
15389 ;
15390 /* If the initializer is something that we know will expand into an
15391 immediate RTL constant, expand it now. We must be careful not to
15392 reference variables which won't be output. */
15393 else if (initializer_constant_valid_p (init, type)
15394 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15395 {
15396 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15397 possible. */
15398 if (TREE_CODE (type) == VECTOR_TYPE)
15399 switch (TREE_CODE (init))
15400 {
15401 case VECTOR_CST:
15402 break;
15403 case CONSTRUCTOR:
15404 if (TREE_CONSTANT (init))
15405 {
15406 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15407 bool constant_p = true;
15408 tree value;
15409 unsigned HOST_WIDE_INT ix;
15410
15411 /* Even when ctor is constant, it might contain non-*_CST
15412 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15413 belong into VECTOR_CST nodes. */
15414 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15415 if (!CONSTANT_CLASS_P (value))
15416 {
15417 constant_p = false;
15418 break;
15419 }
15420
15421 if (constant_p)
15422 {
15423 init = build_vector_from_ctor (type, elts);
15424 break;
15425 }
15426 }
15427 /* FALLTHRU */
15428
15429 default:
15430 return NULL;
15431 }
15432
15433 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15434
15435 /* If expand_expr returns a MEM, it wasn't immediate. */
15436 gcc_assert (!rtl || !MEM_P (rtl));
15437 }
15438
15439 return rtl;
15440 }
15441
15442 /* Generate RTL for the variable DECL to represent its location. */
15443
15444 static rtx
15445 rtl_for_decl_location (tree decl)
15446 {
15447 rtx rtl;
15448
15449 /* Here we have to decide where we are going to say the parameter "lives"
15450 (as far as the debugger is concerned). We only have a couple of
15451 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15452
15453 DECL_RTL normally indicates where the parameter lives during most of the
15454 activation of the function. If optimization is enabled however, this
15455 could be either NULL or else a pseudo-reg. Both of those cases indicate
15456 that the parameter doesn't really live anywhere (as far as the code
15457 generation parts of GCC are concerned) during most of the function's
15458 activation. That will happen (for example) if the parameter is never
15459 referenced within the function.
15460
15461 We could just generate a location descriptor here for all non-NULL
15462 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15463 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15464 where DECL_RTL is NULL or is a pseudo-reg.
15465
15466 Note however that we can only get away with using DECL_INCOMING_RTL as
15467 a backup substitute for DECL_RTL in certain limited cases. In cases
15468 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15469 we can be sure that the parameter was passed using the same type as it is
15470 declared to have within the function, and that its DECL_INCOMING_RTL
15471 points us to a place where a value of that type is passed.
15472
15473 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15474 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15475 because in these cases DECL_INCOMING_RTL points us to a value of some
15476 type which is *different* from the type of the parameter itself. Thus,
15477 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15478 such cases, the debugger would end up (for example) trying to fetch a
15479 `float' from a place which actually contains the first part of a
15480 `double'. That would lead to really incorrect and confusing
15481 output at debug-time.
15482
15483 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15484 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15485 are a couple of exceptions however. On little-endian machines we can
15486 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15487 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15488 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15489 when (on a little-endian machine) a non-prototyped function has a
15490 parameter declared to be of type `short' or `char'. In such cases,
15491 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15492 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15493 passed `int' value. If the debugger then uses that address to fetch
15494 a `short' or a `char' (on a little-endian machine) the result will be
15495 the correct data, so we allow for such exceptional cases below.
15496
15497 Note that our goal here is to describe the place where the given formal
15498 parameter lives during most of the function's activation (i.e. between the
15499 end of the prologue and the start of the epilogue). We'll do that as best
15500 as we can. Note however that if the given formal parameter is modified
15501 sometime during the execution of the function, then a stack backtrace (at
15502 debug-time) will show the function as having been called with the *new*
15503 value rather than the value which was originally passed in. This happens
15504 rarely enough that it is not a major problem, but it *is* a problem, and
15505 I'd like to fix it.
15506
15507 A future version of dwarf2out.c may generate two additional attributes for
15508 any given DW_TAG_formal_parameter DIE which will describe the "passed
15509 type" and the "passed location" for the given formal parameter in addition
15510 to the attributes we now generate to indicate the "declared type" and the
15511 "active location" for each parameter. This additional set of attributes
15512 could be used by debuggers for stack backtraces. Separately, note that
15513 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15514 This happens (for example) for inlined-instances of inline function formal
15515 parameters which are never referenced. This really shouldn't be
15516 happening. All PARM_DECL nodes should get valid non-NULL
15517 DECL_INCOMING_RTL values. FIXME. */
15518
15519 /* Use DECL_RTL as the "location" unless we find something better. */
15520 rtl = DECL_RTL_IF_SET (decl);
15521
15522 /* When generating abstract instances, ignore everything except
15523 constants, symbols living in memory, and symbols living in
15524 fixed registers. */
15525 if (! reload_completed)
15526 {
15527 if (rtl
15528 && (CONSTANT_P (rtl)
15529 || (MEM_P (rtl)
15530 && CONSTANT_P (XEXP (rtl, 0)))
15531 || (REG_P (rtl)
15532 && TREE_CODE (decl) == VAR_DECL
15533 && TREE_STATIC (decl))))
15534 {
15535 rtl = targetm.delegitimize_address (rtl);
15536 return rtl;
15537 }
15538 rtl = NULL_RTX;
15539 }
15540 else if (TREE_CODE (decl) == PARM_DECL)
15541 {
15542 if (rtl == NULL_RTX
15543 || is_pseudo_reg (rtl)
15544 || (MEM_P (rtl)
15545 && is_pseudo_reg (XEXP (rtl, 0))
15546 && DECL_INCOMING_RTL (decl)
15547 && MEM_P (DECL_INCOMING_RTL (decl))
15548 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15549 {
15550 tree declared_type = TREE_TYPE (decl);
15551 tree passed_type = DECL_ARG_TYPE (decl);
15552 enum machine_mode dmode = TYPE_MODE (declared_type);
15553 enum machine_mode pmode = TYPE_MODE (passed_type);
15554
15555 /* This decl represents a formal parameter which was optimized out.
15556 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15557 all cases where (rtl == NULL_RTX) just below. */
15558 if (dmode == pmode)
15559 rtl = DECL_INCOMING_RTL (decl);
15560 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15561 && SCALAR_INT_MODE_P (dmode)
15562 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15563 && DECL_INCOMING_RTL (decl))
15564 {
15565 rtx inc = DECL_INCOMING_RTL (decl);
15566 if (REG_P (inc))
15567 rtl = inc;
15568 else if (MEM_P (inc))
15569 {
15570 if (BYTES_BIG_ENDIAN)
15571 rtl = adjust_address_nv (inc, dmode,
15572 GET_MODE_SIZE (pmode)
15573 - GET_MODE_SIZE (dmode));
15574 else
15575 rtl = inc;
15576 }
15577 }
15578 }
15579
15580 /* If the parm was passed in registers, but lives on the stack, then
15581 make a big endian correction if the mode of the type of the
15582 parameter is not the same as the mode of the rtl. */
15583 /* ??? This is the same series of checks that are made in dbxout.c before
15584 we reach the big endian correction code there. It isn't clear if all
15585 of these checks are necessary here, but keeping them all is the safe
15586 thing to do. */
15587 else if (MEM_P (rtl)
15588 && XEXP (rtl, 0) != const0_rtx
15589 && ! CONSTANT_P (XEXP (rtl, 0))
15590 /* Not passed in memory. */
15591 && !MEM_P (DECL_INCOMING_RTL (decl))
15592 /* Not passed by invisible reference. */
15593 && (!REG_P (XEXP (rtl, 0))
15594 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15595 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15596 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15597 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15598 #endif
15599 )
15600 /* Big endian correction check. */
15601 && BYTES_BIG_ENDIAN
15602 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15603 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15604 < UNITS_PER_WORD))
15605 {
15606 enum machine_mode addr_mode = get_address_mode (rtl);
15607 int offset = (UNITS_PER_WORD
15608 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15609
15610 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15611 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15612 }
15613 }
15614 else if (TREE_CODE (decl) == VAR_DECL
15615 && rtl
15616 && MEM_P (rtl)
15617 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15618 && BYTES_BIG_ENDIAN)
15619 {
15620 enum machine_mode addr_mode = get_address_mode (rtl);
15621 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15622 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15623
15624 /* If a variable is declared "register" yet is smaller than
15625 a register, then if we store the variable to memory, it
15626 looks like we're storing a register-sized value, when in
15627 fact we are not. We need to adjust the offset of the
15628 storage location to reflect the actual value's bytes,
15629 else gdb will not be able to display it. */
15630 if (rsize > dsize)
15631 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15632 plus_constant (addr_mode, XEXP (rtl, 0),
15633 rsize - dsize));
15634 }
15635
15636 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15637 and will have been substituted directly into all expressions that use it.
15638 C does not have such a concept, but C++ and other languages do. */
15639 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15640 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15641
15642 if (rtl)
15643 rtl = targetm.delegitimize_address (rtl);
15644
15645 /* If we don't look past the constant pool, we risk emitting a
15646 reference to a constant pool entry that isn't referenced from
15647 code, and thus is not emitted. */
15648 if (rtl)
15649 rtl = avoid_constant_pool_reference (rtl);
15650
15651 /* Try harder to get a rtl. If this symbol ends up not being emitted
15652 in the current CU, resolve_addr will remove the expression referencing
15653 it. */
15654 if (rtl == NULL_RTX
15655 && TREE_CODE (decl) == VAR_DECL
15656 && !DECL_EXTERNAL (decl)
15657 && TREE_STATIC (decl)
15658 && DECL_NAME (decl)
15659 && !DECL_HARD_REGISTER (decl)
15660 && DECL_MODE (decl) != VOIDmode)
15661 {
15662 rtl = make_decl_rtl_for_debug (decl);
15663 if (!MEM_P (rtl)
15664 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15665 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15666 rtl = NULL_RTX;
15667 }
15668
15669 return rtl;
15670 }
15671
15672 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15673 returned. If so, the decl for the COMMON block is returned, and the
15674 value is the offset into the common block for the symbol. */
15675
15676 static tree
15677 fortran_common (tree decl, HOST_WIDE_INT *value)
15678 {
15679 tree val_expr, cvar;
15680 enum machine_mode mode;
15681 HOST_WIDE_INT bitsize, bitpos;
15682 tree offset;
15683 int unsignedp, volatilep = 0;
15684
15685 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15686 it does not have a value (the offset into the common area), or if it
15687 is thread local (as opposed to global) then it isn't common, and shouldn't
15688 be handled as such. */
15689 if (TREE_CODE (decl) != VAR_DECL
15690 || !TREE_STATIC (decl)
15691 || !DECL_HAS_VALUE_EXPR_P (decl)
15692 || !is_fortran ())
15693 return NULL_TREE;
15694
15695 val_expr = DECL_VALUE_EXPR (decl);
15696 if (TREE_CODE (val_expr) != COMPONENT_REF)
15697 return NULL_TREE;
15698
15699 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15700 &mode, &unsignedp, &volatilep, true);
15701
15702 if (cvar == NULL_TREE
15703 || TREE_CODE (cvar) != VAR_DECL
15704 || DECL_ARTIFICIAL (cvar)
15705 || !TREE_PUBLIC (cvar))
15706 return NULL_TREE;
15707
15708 *value = 0;
15709 if (offset != NULL)
15710 {
15711 if (!tree_fits_shwi_p (offset))
15712 return NULL_TREE;
15713 *value = tree_to_shwi (offset);
15714 }
15715 if (bitpos != 0)
15716 *value += bitpos / BITS_PER_UNIT;
15717
15718 return cvar;
15719 }
15720
15721 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15722 data attribute for a variable or a parameter. We generate the
15723 DW_AT_const_value attribute only in those cases where the given variable
15724 or parameter does not have a true "location" either in memory or in a
15725 register. This can happen (for example) when a constant is passed as an
15726 actual argument in a call to an inline function. (It's possible that
15727 these things can crop up in other ways also.) Note that one type of
15728 constant value which can be passed into an inlined function is a constant
15729 pointer. This can happen for example if an actual argument in an inlined
15730 function call evaluates to a compile-time constant address.
15731
15732 CACHE_P is true if it is worth caching the location list for DECL,
15733 so that future calls can reuse it rather than regenerate it from scratch.
15734 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15735 since we will need to refer to them each time the function is inlined. */
15736
15737 static bool
15738 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15739 enum dwarf_attribute attr)
15740 {
15741 rtx rtl;
15742 dw_loc_list_ref list;
15743 var_loc_list *loc_list;
15744 cached_dw_loc_list *cache;
15745 void **slot;
15746
15747 if (TREE_CODE (decl) == ERROR_MARK)
15748 return false;
15749
15750 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15751 || TREE_CODE (decl) == RESULT_DECL);
15752
15753 /* Try to get some constant RTL for this decl, and use that as the value of
15754 the location. */
15755
15756 rtl = rtl_for_decl_location (decl);
15757 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15758 && add_const_value_attribute (die, rtl))
15759 return true;
15760
15761 /* See if we have single element location list that is equivalent to
15762 a constant value. That way we are better to use add_const_value_attribute
15763 rather than expanding constant value equivalent. */
15764 loc_list = lookup_decl_loc (decl);
15765 if (loc_list
15766 && loc_list->first
15767 && loc_list->first->next == NULL
15768 && NOTE_P (loc_list->first->loc)
15769 && NOTE_VAR_LOCATION (loc_list->first->loc)
15770 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15771 {
15772 struct var_loc_node *node;
15773
15774 node = loc_list->first;
15775 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15776 if (GET_CODE (rtl) == EXPR_LIST)
15777 rtl = XEXP (rtl, 0);
15778 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15779 && add_const_value_attribute (die, rtl))
15780 return true;
15781 }
15782 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15783 list several times. See if we've already cached the contents. */
15784 list = NULL;
15785 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15786 cache_p = false;
15787 if (cache_p)
15788 {
15789 cache = (cached_dw_loc_list *)
15790 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15791 if (cache)
15792 list = cache->loc_list;
15793 }
15794 if (list == NULL)
15795 {
15796 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15797 /* It is usually worth caching this result if the decl is from
15798 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15799 if (cache_p && list && list->dw_loc_next)
15800 {
15801 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15802 DECL_UID (decl), INSERT);
15803 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15804 cache->decl_id = DECL_UID (decl);
15805 cache->loc_list = list;
15806 *slot = cache;
15807 }
15808 }
15809 if (list)
15810 {
15811 add_AT_location_description (die, attr, list);
15812 return true;
15813 }
15814 /* None of that worked, so it must not really have a location;
15815 try adding a constant value attribute from the DECL_INITIAL. */
15816 return tree_add_const_value_attribute_for_decl (die, decl);
15817 }
15818
15819 /* Add VARIABLE and DIE into deferred locations list. */
15820
15821 static void
15822 defer_location (tree variable, dw_die_ref die)
15823 {
15824 deferred_locations entry;
15825 entry.variable = variable;
15826 entry.die = die;
15827 vec_safe_push (deferred_locations_list, entry);
15828 }
15829
15830 /* Helper function for tree_add_const_value_attribute. Natively encode
15831 initializer INIT into an array. Return true if successful. */
15832
15833 static bool
15834 native_encode_initializer (tree init, unsigned char *array, int size)
15835 {
15836 tree type;
15837
15838 if (init == NULL_TREE)
15839 return false;
15840
15841 STRIP_NOPS (init);
15842 switch (TREE_CODE (init))
15843 {
15844 case STRING_CST:
15845 type = TREE_TYPE (init);
15846 if (TREE_CODE (type) == ARRAY_TYPE)
15847 {
15848 tree enttype = TREE_TYPE (type);
15849 enum machine_mode mode = TYPE_MODE (enttype);
15850
15851 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15852 return false;
15853 if (int_size_in_bytes (type) != size)
15854 return false;
15855 if (size > TREE_STRING_LENGTH (init))
15856 {
15857 memcpy (array, TREE_STRING_POINTER (init),
15858 TREE_STRING_LENGTH (init));
15859 memset (array + TREE_STRING_LENGTH (init),
15860 '\0', size - TREE_STRING_LENGTH (init));
15861 }
15862 else
15863 memcpy (array, TREE_STRING_POINTER (init), size);
15864 return true;
15865 }
15866 return false;
15867 case CONSTRUCTOR:
15868 type = TREE_TYPE (init);
15869 if (int_size_in_bytes (type) != size)
15870 return false;
15871 if (TREE_CODE (type) == ARRAY_TYPE)
15872 {
15873 HOST_WIDE_INT min_index;
15874 unsigned HOST_WIDE_INT cnt;
15875 int curpos = 0, fieldsize;
15876 constructor_elt *ce;
15877
15878 if (TYPE_DOMAIN (type) == NULL_TREE
15879 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15880 return false;
15881
15882 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15883 if (fieldsize <= 0)
15884 return false;
15885
15886 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15887 memset (array, '\0', size);
15888 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15889 {
15890 tree val = ce->value;
15891 tree index = ce->index;
15892 int pos = curpos;
15893 if (index && TREE_CODE (index) == RANGE_EXPR)
15894 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15895 * fieldsize;
15896 else if (index)
15897 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15898
15899 if (val)
15900 {
15901 STRIP_NOPS (val);
15902 if (!native_encode_initializer (val, array + pos, fieldsize))
15903 return false;
15904 }
15905 curpos = pos + fieldsize;
15906 if (index && TREE_CODE (index) == RANGE_EXPR)
15907 {
15908 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15909 - tree_to_shwi (TREE_OPERAND (index, 0));
15910 while (count-- > 0)
15911 {
15912 if (val)
15913 memcpy (array + curpos, array + pos, fieldsize);
15914 curpos += fieldsize;
15915 }
15916 }
15917 gcc_assert (curpos <= size);
15918 }
15919 return true;
15920 }
15921 else if (TREE_CODE (type) == RECORD_TYPE
15922 || TREE_CODE (type) == UNION_TYPE)
15923 {
15924 tree field = NULL_TREE;
15925 unsigned HOST_WIDE_INT cnt;
15926 constructor_elt *ce;
15927
15928 if (int_size_in_bytes (type) != size)
15929 return false;
15930
15931 if (TREE_CODE (type) == RECORD_TYPE)
15932 field = TYPE_FIELDS (type);
15933
15934 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15935 {
15936 tree val = ce->value;
15937 int pos, fieldsize;
15938
15939 if (ce->index != 0)
15940 field = ce->index;
15941
15942 if (val)
15943 STRIP_NOPS (val);
15944
15945 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15946 return false;
15947
15948 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15949 && TYPE_DOMAIN (TREE_TYPE (field))
15950 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15951 return false;
15952 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15953 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
15954 return false;
15955 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
15956 pos = int_byte_position (field);
15957 gcc_assert (pos + fieldsize <= size);
15958 if (val
15959 && !native_encode_initializer (val, array + pos, fieldsize))
15960 return false;
15961 }
15962 return true;
15963 }
15964 return false;
15965 case VIEW_CONVERT_EXPR:
15966 case NON_LVALUE_EXPR:
15967 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15968 default:
15969 return native_encode_expr (init, array, size) == size;
15970 }
15971 }
15972
15973 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15974 attribute is the const value T. */
15975
15976 static bool
15977 tree_add_const_value_attribute (dw_die_ref die, tree t)
15978 {
15979 tree init;
15980 tree type = TREE_TYPE (t);
15981 rtx rtl;
15982
15983 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15984 return false;
15985
15986 init = t;
15987 gcc_assert (!DECL_P (init));
15988
15989 rtl = rtl_for_decl_init (init, type);
15990 if (rtl)
15991 return add_const_value_attribute (die, rtl);
15992 /* If the host and target are sane, try harder. */
15993 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15994 && initializer_constant_valid_p (init, type))
15995 {
15996 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15997 if (size > 0 && (int) size == size)
15998 {
15999 unsigned char *array = (unsigned char *)
16000 ggc_alloc_cleared_atomic (size);
16001
16002 if (native_encode_initializer (init, array, size))
16003 {
16004 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16005 return true;
16006 }
16007 ggc_free (array);
16008 }
16009 }
16010 return false;
16011 }
16012
16013 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16014 attribute is the const value of T, where T is an integral constant
16015 variable with static storage duration
16016 (so it can't be a PARM_DECL or a RESULT_DECL). */
16017
16018 static bool
16019 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16020 {
16021
16022 if (!decl
16023 || (TREE_CODE (decl) != VAR_DECL
16024 && TREE_CODE (decl) != CONST_DECL)
16025 || (TREE_CODE (decl) == VAR_DECL
16026 && !TREE_STATIC (decl)))
16027 return false;
16028
16029 if (TREE_READONLY (decl)
16030 && ! TREE_THIS_VOLATILE (decl)
16031 && DECL_INITIAL (decl))
16032 /* OK */;
16033 else
16034 return false;
16035
16036 /* Don't add DW_AT_const_value if abstract origin already has one. */
16037 if (get_AT (var_die, DW_AT_const_value))
16038 return false;
16039
16040 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16041 }
16042
16043 /* Convert the CFI instructions for the current function into a
16044 location list. This is used for DW_AT_frame_base when we targeting
16045 a dwarf2 consumer that does not support the dwarf3
16046 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16047 expressions. */
16048
16049 static dw_loc_list_ref
16050 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16051 {
16052 int ix;
16053 dw_fde_ref fde;
16054 dw_loc_list_ref list, *list_tail;
16055 dw_cfi_ref cfi;
16056 dw_cfa_location last_cfa, next_cfa;
16057 const char *start_label, *last_label, *section;
16058 dw_cfa_location remember;
16059
16060 fde = cfun->fde;
16061 gcc_assert (fde != NULL);
16062
16063 section = secname_for_decl (current_function_decl);
16064 list_tail = &list;
16065 list = NULL;
16066
16067 memset (&next_cfa, 0, sizeof (next_cfa));
16068 next_cfa.reg = INVALID_REGNUM;
16069 remember = next_cfa;
16070
16071 start_label = fde->dw_fde_begin;
16072
16073 /* ??? Bald assumption that the CIE opcode list does not contain
16074 advance opcodes. */
16075 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16076 lookup_cfa_1 (cfi, &next_cfa, &remember);
16077
16078 last_cfa = next_cfa;
16079 last_label = start_label;
16080
16081 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16082 {
16083 /* If the first partition contained no CFI adjustments, the
16084 CIE opcodes apply to the whole first partition. */
16085 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16086 fde->dw_fde_begin, fde->dw_fde_end, section);
16087 list_tail =&(*list_tail)->dw_loc_next;
16088 start_label = last_label = fde->dw_fde_second_begin;
16089 }
16090
16091 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16092 {
16093 switch (cfi->dw_cfi_opc)
16094 {
16095 case DW_CFA_set_loc:
16096 case DW_CFA_advance_loc1:
16097 case DW_CFA_advance_loc2:
16098 case DW_CFA_advance_loc4:
16099 if (!cfa_equal_p (&last_cfa, &next_cfa))
16100 {
16101 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16102 start_label, last_label, section);
16103
16104 list_tail = &(*list_tail)->dw_loc_next;
16105 last_cfa = next_cfa;
16106 start_label = last_label;
16107 }
16108 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16109 break;
16110
16111 case DW_CFA_advance_loc:
16112 /* The encoding is complex enough that we should never emit this. */
16113 gcc_unreachable ();
16114
16115 default:
16116 lookup_cfa_1 (cfi, &next_cfa, &remember);
16117 break;
16118 }
16119 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16120 {
16121 if (!cfa_equal_p (&last_cfa, &next_cfa))
16122 {
16123 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16124 start_label, last_label, section);
16125
16126 list_tail = &(*list_tail)->dw_loc_next;
16127 last_cfa = next_cfa;
16128 start_label = last_label;
16129 }
16130 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16131 start_label, fde->dw_fde_end, section);
16132 list_tail = &(*list_tail)->dw_loc_next;
16133 start_label = last_label = fde->dw_fde_second_begin;
16134 }
16135 }
16136
16137 if (!cfa_equal_p (&last_cfa, &next_cfa))
16138 {
16139 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16140 start_label, last_label, section);
16141 list_tail = &(*list_tail)->dw_loc_next;
16142 start_label = last_label;
16143 }
16144
16145 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16146 start_label,
16147 fde->dw_fde_second_begin
16148 ? fde->dw_fde_second_end : fde->dw_fde_end,
16149 section);
16150
16151 if (list && list->dw_loc_next)
16152 gen_llsym (list);
16153
16154 return list;
16155 }
16156
16157 /* Compute a displacement from the "steady-state frame pointer" to the
16158 frame base (often the same as the CFA), and store it in
16159 frame_pointer_fb_offset. OFFSET is added to the displacement
16160 before the latter is negated. */
16161
16162 static void
16163 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16164 {
16165 rtx reg, elim;
16166
16167 #ifdef FRAME_POINTER_CFA_OFFSET
16168 reg = frame_pointer_rtx;
16169 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16170 #else
16171 reg = arg_pointer_rtx;
16172 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16173 #endif
16174
16175 elim = (ira_use_lra_p
16176 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16177 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16178 if (GET_CODE (elim) == PLUS)
16179 {
16180 offset += INTVAL (XEXP (elim, 1));
16181 elim = XEXP (elim, 0);
16182 }
16183
16184 frame_pointer_fb_offset = -offset;
16185
16186 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16187 in which to eliminate. This is because it's stack pointer isn't
16188 directly accessible as a register within the ISA. To work around
16189 this, assume that while we cannot provide a proper value for
16190 frame_pointer_fb_offset, we won't need one either. */
16191 frame_pointer_fb_offset_valid
16192 = ((SUPPORTS_STACK_ALIGNMENT
16193 && (elim == hard_frame_pointer_rtx
16194 || elim == stack_pointer_rtx))
16195 || elim == (frame_pointer_needed
16196 ? hard_frame_pointer_rtx
16197 : stack_pointer_rtx));
16198 }
16199
16200 /* Generate a DW_AT_name attribute given some string value to be included as
16201 the value of the attribute. */
16202
16203 static void
16204 add_name_attribute (dw_die_ref die, const char *name_string)
16205 {
16206 if (name_string != NULL && *name_string != 0)
16207 {
16208 if (demangle_name_func)
16209 name_string = (*demangle_name_func) (name_string);
16210
16211 add_AT_string (die, DW_AT_name, name_string);
16212 }
16213 }
16214
16215 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16216 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16217 of TYPE accordingly.
16218
16219 ??? This is a temporary measure until after we're able to generate
16220 regular DWARF for the complex Ada type system. */
16221
16222 static void
16223 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16224 dw_die_ref context_die)
16225 {
16226 tree dtype;
16227 dw_die_ref dtype_die;
16228
16229 if (!lang_hooks.types.descriptive_type)
16230 return;
16231
16232 dtype = lang_hooks.types.descriptive_type (type);
16233 if (!dtype)
16234 return;
16235
16236 dtype_die = lookup_type_die (dtype);
16237 if (!dtype_die)
16238 {
16239 gen_type_die (dtype, context_die);
16240 dtype_die = lookup_type_die (dtype);
16241 gcc_assert (dtype_die);
16242 }
16243
16244 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16245 }
16246
16247 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16248
16249 static const char *
16250 comp_dir_string (void)
16251 {
16252 const char *wd;
16253 char *wd1;
16254 static const char *cached_wd = NULL;
16255
16256 if (cached_wd != NULL)
16257 return cached_wd;
16258
16259 wd = get_src_pwd ();
16260 if (wd == NULL)
16261 return NULL;
16262
16263 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16264 {
16265 int wdlen;
16266
16267 wdlen = strlen (wd);
16268 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16269 strcpy (wd1, wd);
16270 wd1 [wdlen] = DIR_SEPARATOR;
16271 wd1 [wdlen + 1] = 0;
16272 wd = wd1;
16273 }
16274
16275 cached_wd = remap_debug_filename (wd);
16276 return cached_wd;
16277 }
16278
16279 /* Generate a DW_AT_comp_dir attribute for DIE. */
16280
16281 static void
16282 add_comp_dir_attribute (dw_die_ref die)
16283 {
16284 const char * wd = comp_dir_string ();
16285 if (wd != NULL)
16286 add_AT_string (die, DW_AT_comp_dir, wd);
16287 }
16288
16289 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16290 default. */
16291
16292 static int
16293 lower_bound_default (void)
16294 {
16295 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16296 {
16297 case DW_LANG_C:
16298 case DW_LANG_C89:
16299 case DW_LANG_C99:
16300 case DW_LANG_C_plus_plus:
16301 case DW_LANG_ObjC:
16302 case DW_LANG_ObjC_plus_plus:
16303 case DW_LANG_Java:
16304 return 0;
16305 case DW_LANG_Fortran77:
16306 case DW_LANG_Fortran90:
16307 case DW_LANG_Fortran95:
16308 return 1;
16309 case DW_LANG_UPC:
16310 case DW_LANG_D:
16311 case DW_LANG_Python:
16312 return dwarf_version >= 4 ? 0 : -1;
16313 case DW_LANG_Ada95:
16314 case DW_LANG_Ada83:
16315 case DW_LANG_Cobol74:
16316 case DW_LANG_Cobol85:
16317 case DW_LANG_Pascal83:
16318 case DW_LANG_Modula2:
16319 case DW_LANG_PLI:
16320 return dwarf_version >= 4 ? 1 : -1;
16321 default:
16322 return -1;
16323 }
16324 }
16325
16326 /* Given a tree node describing an array bound (either lower or upper) output
16327 a representation for that bound. */
16328
16329 static void
16330 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16331 {
16332 switch (TREE_CODE (bound))
16333 {
16334 case ERROR_MARK:
16335 return;
16336
16337 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16338 case INTEGER_CST:
16339 {
16340 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16341 int dflt;
16342
16343 /* Use the default if possible. */
16344 if (bound_attr == DW_AT_lower_bound
16345 && tree_fits_shwi_p (bound)
16346 && (dflt = lower_bound_default ()) != -1
16347 && tree_to_shwi (bound) == dflt)
16348 ;
16349
16350 /* Otherwise represent the bound as an unsigned value with the
16351 precision of its type. The precision and signedness of the
16352 type will be necessary to re-interpret it unambiguously. */
16353 else if (prec < HOST_BITS_PER_WIDE_INT)
16354 {
16355 unsigned HOST_WIDE_INT mask
16356 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16357 add_AT_unsigned (subrange_die, bound_attr,
16358 TREE_INT_CST_LOW (bound) & mask);
16359 }
16360 else if (prec == HOST_BITS_PER_WIDE_INT || tree_fits_uhwi_p (bound))
16361 add_AT_unsigned (subrange_die, bound_attr,
16362 TREE_INT_CST_LOW (bound));
16363 else
16364 add_AT_wide (subrange_die, bound_attr, bound);
16365 }
16366 break;
16367
16368 CASE_CONVERT:
16369 case VIEW_CONVERT_EXPR:
16370 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16371 break;
16372
16373 case SAVE_EXPR:
16374 break;
16375
16376 case VAR_DECL:
16377 case PARM_DECL:
16378 case RESULT_DECL:
16379 {
16380 dw_die_ref decl_die = lookup_decl_die (bound);
16381
16382 /* ??? Can this happen, or should the variable have been bound
16383 first? Probably it can, since I imagine that we try to create
16384 the types of parameters in the order in which they exist in
16385 the list, and won't have created a forward reference to a
16386 later parameter. */
16387 if (decl_die != NULL)
16388 {
16389 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16390 break;
16391 }
16392 }
16393 /* FALLTHRU */
16394
16395 default:
16396 {
16397 /* Otherwise try to create a stack operation procedure to
16398 evaluate the value of the array bound. */
16399
16400 dw_die_ref ctx, decl_die;
16401 dw_loc_list_ref list;
16402
16403 list = loc_list_from_tree (bound, 2);
16404 if (list == NULL || single_element_loc_list_p (list))
16405 {
16406 /* If DW_AT_*bound is not a reference nor constant, it is
16407 a DWARF expression rather than location description.
16408 For that loc_list_from_tree (bound, 0) is needed.
16409 If that fails to give a single element list,
16410 fall back to outputting this as a reference anyway. */
16411 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16412 if (list2 && single_element_loc_list_p (list2))
16413 {
16414 add_AT_loc (subrange_die, bound_attr, list2->expr);
16415 break;
16416 }
16417 }
16418 if (list == NULL)
16419 break;
16420
16421 if (current_function_decl == 0)
16422 ctx = comp_unit_die ();
16423 else
16424 ctx = lookup_decl_die (current_function_decl);
16425
16426 decl_die = new_die (DW_TAG_variable, ctx, bound);
16427 add_AT_flag (decl_die, DW_AT_artificial, 1);
16428 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16429 add_AT_location_description (decl_die, DW_AT_location, list);
16430 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16431 break;
16432 }
16433 }
16434 }
16435
16436 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16437 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16438 Note that the block of subscript information for an array type also
16439 includes information about the element type of the given array type. */
16440
16441 static void
16442 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16443 {
16444 unsigned dimension_number;
16445 tree lower, upper;
16446 dw_die_ref subrange_die;
16447
16448 for (dimension_number = 0;
16449 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16450 type = TREE_TYPE (type), dimension_number++)
16451 {
16452 tree domain = TYPE_DOMAIN (type);
16453
16454 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16455 break;
16456
16457 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16458 and (in GNU C only) variable bounds. Handle all three forms
16459 here. */
16460 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16461 if (domain)
16462 {
16463 /* We have an array type with specified bounds. */
16464 lower = TYPE_MIN_VALUE (domain);
16465 upper = TYPE_MAX_VALUE (domain);
16466
16467 /* Define the index type. */
16468 if (TREE_TYPE (domain))
16469 {
16470 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16471 TREE_TYPE field. We can't emit debug info for this
16472 because it is an unnamed integral type. */
16473 if (TREE_CODE (domain) == INTEGER_TYPE
16474 && TYPE_NAME (domain) == NULL_TREE
16475 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16476 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16477 ;
16478 else
16479 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16480 type_die);
16481 }
16482
16483 /* ??? If upper is NULL, the array has unspecified length,
16484 but it does have a lower bound. This happens with Fortran
16485 dimension arr(N:*)
16486 Since the debugger is definitely going to need to know N
16487 to produce useful results, go ahead and output the lower
16488 bound solo, and hope the debugger can cope. */
16489
16490 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16491 if (upper)
16492 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16493 }
16494
16495 /* Otherwise we have an array type with an unspecified length. The
16496 DWARF-2 spec does not say how to handle this; let's just leave out the
16497 bounds. */
16498 }
16499 }
16500
16501 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16502
16503 static void
16504 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16505 {
16506 dw_die_ref decl_die;
16507 HOST_WIDE_INT size;
16508
16509 switch (TREE_CODE (tree_node))
16510 {
16511 case ERROR_MARK:
16512 size = 0;
16513 break;
16514 case ENUMERAL_TYPE:
16515 case RECORD_TYPE:
16516 case UNION_TYPE:
16517 case QUAL_UNION_TYPE:
16518 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16519 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16520 {
16521 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16522 return;
16523 }
16524 size = int_size_in_bytes (tree_node);
16525 break;
16526 case FIELD_DECL:
16527 /* For a data member of a struct or union, the DW_AT_byte_size is
16528 generally given as the number of bytes normally allocated for an
16529 object of the *declared* type of the member itself. This is true
16530 even for bit-fields. */
16531 size = int_size_in_bytes (field_type (tree_node));
16532 break;
16533 default:
16534 gcc_unreachable ();
16535 }
16536
16537 /* Note that `size' might be -1 when we get to this point. If it is, that
16538 indicates that the byte size of the entity in question is variable. We
16539 have no good way of expressing this fact in Dwarf at the present time,
16540 when location description was not used by the caller code instead. */
16541 if (size >= 0)
16542 add_AT_unsigned (die, DW_AT_byte_size, size);
16543 }
16544
16545 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16546 which specifies the distance in bits from the highest order bit of the
16547 "containing object" for the bit-field to the highest order bit of the
16548 bit-field itself.
16549
16550 For any given bit-field, the "containing object" is a hypothetical object
16551 (of some integral or enum type) within which the given bit-field lives. The
16552 type of this hypothetical "containing object" is always the same as the
16553 declared type of the individual bit-field itself. The determination of the
16554 exact location of the "containing object" for a bit-field is rather
16555 complicated. It's handled by the `field_byte_offset' function (above).
16556
16557 Note that it is the size (in bytes) of the hypothetical "containing object"
16558 which will be given in the DW_AT_byte_size attribute for this bit-field.
16559 (See `byte_size_attribute' above). */
16560
16561 static inline void
16562 add_bit_offset_attribute (dw_die_ref die, tree decl)
16563 {
16564 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16565 tree type = DECL_BIT_FIELD_TYPE (decl);
16566 HOST_WIDE_INT bitpos_int;
16567 HOST_WIDE_INT highest_order_object_bit_offset;
16568 HOST_WIDE_INT highest_order_field_bit_offset;
16569 HOST_WIDE_INT bit_offset;
16570
16571 /* Must be a field and a bit field. */
16572 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16573
16574 /* We can't yet handle bit-fields whose offsets are variable, so if we
16575 encounter such things, just return without generating any attribute
16576 whatsoever. Likewise for variable or too large size. */
16577 if (! tree_fits_shwi_p (bit_position (decl))
16578 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16579 return;
16580
16581 bitpos_int = int_bit_position (decl);
16582
16583 /* Note that the bit offset is always the distance (in bits) from the
16584 highest-order bit of the "containing object" to the highest-order bit of
16585 the bit-field itself. Since the "high-order end" of any object or field
16586 is different on big-endian and little-endian machines, the computation
16587 below must take account of these differences. */
16588 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16589 highest_order_field_bit_offset = bitpos_int;
16590
16591 if (! BYTES_BIG_ENDIAN)
16592 {
16593 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16594 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16595 }
16596
16597 bit_offset
16598 = (! BYTES_BIG_ENDIAN
16599 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16600 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16601
16602 if (bit_offset < 0)
16603 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16604 else
16605 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16606 }
16607
16608 /* For a FIELD_DECL node which represents a bit field, output an attribute
16609 which specifies the length in bits of the given field. */
16610
16611 static inline void
16612 add_bit_size_attribute (dw_die_ref die, tree decl)
16613 {
16614 /* Must be a field and a bit field. */
16615 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16616 && DECL_BIT_FIELD_TYPE (decl));
16617
16618 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16619 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16620 }
16621
16622 /* If the compiled language is ANSI C, then add a 'prototyped'
16623 attribute, if arg types are given for the parameters of a function. */
16624
16625 static inline void
16626 add_prototyped_attribute (dw_die_ref die, tree func_type)
16627 {
16628 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16629 && prototype_p (func_type))
16630 add_AT_flag (die, DW_AT_prototyped, 1);
16631 }
16632
16633 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16634 by looking in either the type declaration or object declaration
16635 equate table. */
16636
16637 static inline dw_die_ref
16638 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16639 {
16640 dw_die_ref origin_die = NULL;
16641
16642 if (TREE_CODE (origin) != FUNCTION_DECL)
16643 {
16644 /* We may have gotten separated from the block for the inlined
16645 function, if we're in an exception handler or some such; make
16646 sure that the abstract function has been written out.
16647
16648 Doing this for nested functions is wrong, however; functions are
16649 distinct units, and our context might not even be inline. */
16650 tree fn = origin;
16651
16652 if (TYPE_P (fn))
16653 fn = TYPE_STUB_DECL (fn);
16654
16655 fn = decl_function_context (fn);
16656 if (fn)
16657 dwarf2out_abstract_function (fn);
16658 }
16659
16660 if (DECL_P (origin))
16661 origin_die = lookup_decl_die (origin);
16662 else if (TYPE_P (origin))
16663 origin_die = lookup_type_die (origin);
16664
16665 /* XXX: Functions that are never lowered don't always have correct block
16666 trees (in the case of java, they simply have no block tree, in some other
16667 languages). For these functions, there is nothing we can really do to
16668 output correct debug info for inlined functions in all cases. Rather
16669 than die, we'll just produce deficient debug info now, in that we will
16670 have variables without a proper abstract origin. In the future, when all
16671 functions are lowered, we should re-add a gcc_assert (origin_die)
16672 here. */
16673
16674 if (origin_die)
16675 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16676 return origin_die;
16677 }
16678
16679 /* We do not currently support the pure_virtual attribute. */
16680
16681 static inline void
16682 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16683 {
16684 if (DECL_VINDEX (func_decl))
16685 {
16686 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16687
16688 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16689 add_AT_loc (die, DW_AT_vtable_elem_location,
16690 new_loc_descr (DW_OP_constu,
16691 tree_to_shwi (DECL_VINDEX (func_decl)),
16692 0));
16693
16694 /* GNU extension: Record what type this method came from originally. */
16695 if (debug_info_level > DINFO_LEVEL_TERSE
16696 && DECL_CONTEXT (func_decl))
16697 add_AT_die_ref (die, DW_AT_containing_type,
16698 lookup_type_die (DECL_CONTEXT (func_decl)));
16699 }
16700 }
16701 \f
16702 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16703 given decl. This used to be a vendor extension until after DWARF 4
16704 standardized it. */
16705
16706 static void
16707 add_linkage_attr (dw_die_ref die, tree decl)
16708 {
16709 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16710
16711 /* Mimic what assemble_name_raw does with a leading '*'. */
16712 if (name[0] == '*')
16713 name = &name[1];
16714
16715 if (dwarf_version >= 4)
16716 add_AT_string (die, DW_AT_linkage_name, name);
16717 else
16718 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16719 }
16720
16721 /* Add source coordinate attributes for the given decl. */
16722
16723 static void
16724 add_src_coords_attributes (dw_die_ref die, tree decl)
16725 {
16726 expanded_location s;
16727
16728 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16729 return;
16730 s = expand_location (DECL_SOURCE_LOCATION (decl));
16731 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16732 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16733 }
16734
16735 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16736
16737 static void
16738 add_linkage_name (dw_die_ref die, tree decl)
16739 {
16740 if (debug_info_level > DINFO_LEVEL_TERSE
16741 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16742 && TREE_PUBLIC (decl)
16743 && !DECL_ABSTRACT (decl)
16744 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16745 && die->die_tag != DW_TAG_member)
16746 {
16747 /* Defer until we have an assembler name set. */
16748 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16749 {
16750 limbo_die_node *asm_name;
16751
16752 asm_name = ggc_alloc_cleared_limbo_die_node ();
16753 asm_name->die = die;
16754 asm_name->created_for = decl;
16755 asm_name->next = deferred_asm_name;
16756 deferred_asm_name = asm_name;
16757 }
16758 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16759 add_linkage_attr (die, decl);
16760 }
16761 }
16762
16763 /* Add a DW_AT_name attribute and source coordinate attribute for the
16764 given decl, but only if it actually has a name. */
16765
16766 static void
16767 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16768 {
16769 tree decl_name;
16770
16771 decl_name = DECL_NAME (decl);
16772 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16773 {
16774 const char *name = dwarf2_name (decl, 0);
16775 if (name)
16776 add_name_attribute (die, name);
16777 if (! DECL_ARTIFICIAL (decl))
16778 add_src_coords_attributes (die, decl);
16779
16780 add_linkage_name (die, decl);
16781 }
16782
16783 #ifdef VMS_DEBUGGING_INFO
16784 /* Get the function's name, as described by its RTL. This may be different
16785 from the DECL_NAME name used in the source file. */
16786 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16787 {
16788 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16789 XEXP (DECL_RTL (decl), 0), false);
16790 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16791 }
16792 #endif /* VMS_DEBUGGING_INFO */
16793 }
16794
16795 #ifdef VMS_DEBUGGING_INFO
16796 /* Output the debug main pointer die for VMS */
16797
16798 void
16799 dwarf2out_vms_debug_main_pointer (void)
16800 {
16801 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16802 dw_die_ref die;
16803
16804 /* Allocate the VMS debug main subprogram die. */
16805 die = ggc_alloc_cleared_die_node ();
16806 die->die_tag = DW_TAG_subprogram;
16807 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16808 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16809 current_function_funcdef_no);
16810 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16811
16812 /* Make it the first child of comp_unit_die (). */
16813 die->die_parent = comp_unit_die ();
16814 if (comp_unit_die ()->die_child)
16815 {
16816 die->die_sib = comp_unit_die ()->die_child->die_sib;
16817 comp_unit_die ()->die_child->die_sib = die;
16818 }
16819 else
16820 {
16821 die->die_sib = die;
16822 comp_unit_die ()->die_child = die;
16823 }
16824 }
16825 #endif /* VMS_DEBUGGING_INFO */
16826
16827 /* Push a new declaration scope. */
16828
16829 static void
16830 push_decl_scope (tree scope)
16831 {
16832 vec_safe_push (decl_scope_table, scope);
16833 }
16834
16835 /* Pop a declaration scope. */
16836
16837 static inline void
16838 pop_decl_scope (void)
16839 {
16840 decl_scope_table->pop ();
16841 }
16842
16843 /* walk_tree helper function for uses_local_type, below. */
16844
16845 static tree
16846 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16847 {
16848 if (!TYPE_P (*tp))
16849 *walk_subtrees = 0;
16850 else
16851 {
16852 tree name = TYPE_NAME (*tp);
16853 if (name && DECL_P (name) && decl_function_context (name))
16854 return *tp;
16855 }
16856 return NULL_TREE;
16857 }
16858
16859 /* If TYPE involves a function-local type (including a local typedef to a
16860 non-local type), returns that type; otherwise returns NULL_TREE. */
16861
16862 static tree
16863 uses_local_type (tree type)
16864 {
16865 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16866 return used;
16867 }
16868
16869 /* Return the DIE for the scope that immediately contains this type.
16870 Non-named types that do not involve a function-local type get global
16871 scope. Named types nested in namespaces or other types get their
16872 containing scope. All other types (i.e. function-local named types) get
16873 the current active scope. */
16874
16875 static dw_die_ref
16876 scope_die_for (tree t, dw_die_ref context_die)
16877 {
16878 dw_die_ref scope_die = NULL;
16879 tree containing_scope;
16880
16881 /* Non-types always go in the current scope. */
16882 gcc_assert (TYPE_P (t));
16883
16884 /* Use the scope of the typedef, rather than the scope of the type
16885 it refers to. */
16886 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16887 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16888 else
16889 containing_scope = TYPE_CONTEXT (t);
16890
16891 /* Use the containing namespace if there is one. */
16892 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16893 {
16894 if (context_die == lookup_decl_die (containing_scope))
16895 /* OK */;
16896 else if (debug_info_level > DINFO_LEVEL_TERSE)
16897 context_die = get_context_die (containing_scope);
16898 else
16899 containing_scope = NULL_TREE;
16900 }
16901
16902 /* Ignore function type "scopes" from the C frontend. They mean that
16903 a tagged type is local to a parmlist of a function declarator, but
16904 that isn't useful to DWARF. */
16905 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16906 containing_scope = NULL_TREE;
16907
16908 if (SCOPE_FILE_SCOPE_P (containing_scope))
16909 {
16910 /* If T uses a local type keep it local as well, to avoid references
16911 to function-local DIEs from outside the function. */
16912 if (current_function_decl && uses_local_type (t))
16913 scope_die = context_die;
16914 else
16915 scope_die = comp_unit_die ();
16916 }
16917 else if (TYPE_P (containing_scope))
16918 {
16919 /* For types, we can just look up the appropriate DIE. */
16920 if (debug_info_level > DINFO_LEVEL_TERSE)
16921 scope_die = get_context_die (containing_scope);
16922 else
16923 {
16924 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16925 if (scope_die == NULL)
16926 scope_die = comp_unit_die ();
16927 }
16928 }
16929 else
16930 scope_die = context_die;
16931
16932 return scope_die;
16933 }
16934
16935 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16936
16937 static inline int
16938 local_scope_p (dw_die_ref context_die)
16939 {
16940 for (; context_die; context_die = context_die->die_parent)
16941 if (context_die->die_tag == DW_TAG_inlined_subroutine
16942 || context_die->die_tag == DW_TAG_subprogram)
16943 return 1;
16944
16945 return 0;
16946 }
16947
16948 /* Returns nonzero if CONTEXT_DIE is a class. */
16949
16950 static inline int
16951 class_scope_p (dw_die_ref context_die)
16952 {
16953 return (context_die
16954 && (context_die->die_tag == DW_TAG_structure_type
16955 || context_die->die_tag == DW_TAG_class_type
16956 || context_die->die_tag == DW_TAG_interface_type
16957 || context_die->die_tag == DW_TAG_union_type));
16958 }
16959
16960 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16961 whether or not to treat a DIE in this context as a declaration. */
16962
16963 static inline int
16964 class_or_namespace_scope_p (dw_die_ref context_die)
16965 {
16966 return (class_scope_p (context_die)
16967 || (context_die && context_die->die_tag == DW_TAG_namespace));
16968 }
16969
16970 /* Many forms of DIEs require a "type description" attribute. This
16971 routine locates the proper "type descriptor" die for the type given
16972 by 'type', and adds a DW_AT_type attribute below the given die. */
16973
16974 static void
16975 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16976 int decl_volatile, dw_die_ref context_die)
16977 {
16978 enum tree_code code = TREE_CODE (type);
16979 dw_die_ref type_die = NULL;
16980
16981 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16982 or fixed-point type, use the inner type. This is because we have no
16983 support for unnamed types in base_type_die. This can happen if this is
16984 an Ada subrange type. Correct solution is emit a subrange type die. */
16985 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16986 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16987 type = TREE_TYPE (type), code = TREE_CODE (type);
16988
16989 if (code == ERROR_MARK
16990 /* Handle a special case. For functions whose return type is void, we
16991 generate *no* type attribute. (Note that no object may have type
16992 `void', so this only applies to function return types). */
16993 || code == VOID_TYPE)
16994 return;
16995
16996 type_die = modified_type_die (type,
16997 decl_const || TYPE_READONLY (type),
16998 decl_volatile || TYPE_VOLATILE (type),
16999 context_die);
17000
17001 if (type_die != NULL)
17002 add_AT_die_ref (object_die, DW_AT_type, type_die);
17003 }
17004
17005 /* Given an object die, add the calling convention attribute for the
17006 function call type. */
17007 static void
17008 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17009 {
17010 enum dwarf_calling_convention value = DW_CC_normal;
17011
17012 value = ((enum dwarf_calling_convention)
17013 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17014
17015 if (is_fortran ()
17016 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17017 {
17018 /* DWARF 2 doesn't provide a way to identify a program's source-level
17019 entry point. DW_AT_calling_convention attributes are only meant
17020 to describe functions' calling conventions. However, lacking a
17021 better way to signal the Fortran main program, we used this for
17022 a long time, following existing custom. Now, DWARF 4 has
17023 DW_AT_main_subprogram, which we add below, but some tools still
17024 rely on the old way, which we thus keep. */
17025 value = DW_CC_program;
17026
17027 if (dwarf_version >= 4 || !dwarf_strict)
17028 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17029 }
17030
17031 /* Only add the attribute if the backend requests it, and
17032 is not DW_CC_normal. */
17033 if (value && (value != DW_CC_normal))
17034 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17035 }
17036
17037 /* Given a tree pointer to a struct, class, union, or enum type node, return
17038 a pointer to the (string) tag name for the given type, or zero if the type
17039 was declared without a tag. */
17040
17041 static const char *
17042 type_tag (const_tree type)
17043 {
17044 const char *name = 0;
17045
17046 if (TYPE_NAME (type) != 0)
17047 {
17048 tree t = 0;
17049
17050 /* Find the IDENTIFIER_NODE for the type name. */
17051 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17052 && !TYPE_NAMELESS (type))
17053 t = TYPE_NAME (type);
17054
17055 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17056 a TYPE_DECL node, regardless of whether or not a `typedef' was
17057 involved. */
17058 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17059 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17060 {
17061 /* We want to be extra verbose. Don't call dwarf_name if
17062 DECL_NAME isn't set. The default hook for decl_printable_name
17063 doesn't like that, and in this context it's correct to return
17064 0, instead of "<anonymous>" or the like. */
17065 if (DECL_NAME (TYPE_NAME (type))
17066 && !DECL_NAMELESS (TYPE_NAME (type)))
17067 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17068 }
17069
17070 /* Now get the name as a string, or invent one. */
17071 if (!name && t != 0)
17072 name = IDENTIFIER_POINTER (t);
17073 }
17074
17075 return (name == 0 || *name == '\0') ? 0 : name;
17076 }
17077
17078 /* Return the type associated with a data member, make a special check
17079 for bit field types. */
17080
17081 static inline tree
17082 member_declared_type (const_tree member)
17083 {
17084 return (DECL_BIT_FIELD_TYPE (member)
17085 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17086 }
17087
17088 /* Get the decl's label, as described by its RTL. This may be different
17089 from the DECL_NAME name used in the source file. */
17090
17091 #if 0
17092 static const char *
17093 decl_start_label (tree decl)
17094 {
17095 rtx x;
17096 const char *fnname;
17097
17098 x = DECL_RTL (decl);
17099 gcc_assert (MEM_P (x));
17100
17101 x = XEXP (x, 0);
17102 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17103
17104 fnname = XSTR (x, 0);
17105 return fnname;
17106 }
17107 #endif
17108 \f
17109 /* These routines generate the internal representation of the DIE's for
17110 the compilation unit. Debugging information is collected by walking
17111 the declaration trees passed in from dwarf2out_decl(). */
17112
17113 static void
17114 gen_array_type_die (tree type, dw_die_ref context_die)
17115 {
17116 dw_die_ref scope_die = scope_die_for (type, context_die);
17117 dw_die_ref array_die;
17118
17119 /* GNU compilers represent multidimensional array types as sequences of one
17120 dimensional array types whose element types are themselves array types.
17121 We sometimes squish that down to a single array_type DIE with multiple
17122 subscripts in the Dwarf debugging info. The draft Dwarf specification
17123 say that we are allowed to do this kind of compression in C, because
17124 there is no difference between an array of arrays and a multidimensional
17125 array. We don't do this for Ada to remain as close as possible to the
17126 actual representation, which is especially important against the language
17127 flexibilty wrt arrays of variable size. */
17128
17129 bool collapse_nested_arrays = !is_ada ();
17130 tree element_type;
17131
17132 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17133 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17134 if (TYPE_STRING_FLAG (type)
17135 && TREE_CODE (type) == ARRAY_TYPE
17136 && is_fortran ()
17137 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17138 {
17139 HOST_WIDE_INT size;
17140
17141 array_die = new_die (DW_TAG_string_type, scope_die, type);
17142 add_name_attribute (array_die, type_tag (type));
17143 equate_type_number_to_die (type, array_die);
17144 size = int_size_in_bytes (type);
17145 if (size >= 0)
17146 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17147 else if (TYPE_DOMAIN (type) != NULL_TREE
17148 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17149 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17150 {
17151 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17152 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17153
17154 size = int_size_in_bytes (TREE_TYPE (szdecl));
17155 if (loc && size > 0)
17156 {
17157 add_AT_location_description (array_die, DW_AT_string_length, loc);
17158 if (size != DWARF2_ADDR_SIZE)
17159 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17160 }
17161 }
17162 return;
17163 }
17164
17165 array_die = new_die (DW_TAG_array_type, scope_die, type);
17166 add_name_attribute (array_die, type_tag (type));
17167 equate_type_number_to_die (type, array_die);
17168
17169 if (TREE_CODE (type) == VECTOR_TYPE)
17170 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17171
17172 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17173 if (is_fortran ()
17174 && TREE_CODE (type) == ARRAY_TYPE
17175 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17176 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17177 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17178
17179 #if 0
17180 /* We default the array ordering. SDB will probably do
17181 the right things even if DW_AT_ordering is not present. It's not even
17182 an issue until we start to get into multidimensional arrays anyway. If
17183 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17184 then we'll have to put the DW_AT_ordering attribute back in. (But if
17185 and when we find out that we need to put these in, we will only do so
17186 for multidimensional arrays. */
17187 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17188 #endif
17189
17190 if (TREE_CODE (type) == VECTOR_TYPE)
17191 {
17192 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17193 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17194 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17195 add_bound_info (subrange_die, DW_AT_upper_bound,
17196 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17197 }
17198 else
17199 add_subscript_info (array_die, type, collapse_nested_arrays);
17200
17201 /* Add representation of the type of the elements of this array type and
17202 emit the corresponding DIE if we haven't done it already. */
17203 element_type = TREE_TYPE (type);
17204 if (collapse_nested_arrays)
17205 while (TREE_CODE (element_type) == ARRAY_TYPE)
17206 {
17207 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17208 break;
17209 element_type = TREE_TYPE (element_type);
17210 }
17211
17212 add_type_attribute (array_die, element_type, 0, 0, context_die);
17213
17214 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17215 if (TYPE_ARTIFICIAL (type))
17216 add_AT_flag (array_die, DW_AT_artificial, 1);
17217
17218 if (get_AT (array_die, DW_AT_name))
17219 add_pubtype (type, array_die);
17220 }
17221
17222 static dw_loc_descr_ref
17223 descr_info_loc (tree val, tree base_decl)
17224 {
17225 HOST_WIDE_INT size;
17226 dw_loc_descr_ref loc, loc2;
17227 enum dwarf_location_atom op;
17228
17229 if (val == base_decl)
17230 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17231
17232 switch (TREE_CODE (val))
17233 {
17234 CASE_CONVERT:
17235 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17236 case VAR_DECL:
17237 return loc_descriptor_from_tree (val, 0);
17238 case INTEGER_CST:
17239 if (tree_fits_shwi_p (val))
17240 return int_loc_descriptor (tree_to_shwi (val));
17241 break;
17242 case INDIRECT_REF:
17243 size = int_size_in_bytes (TREE_TYPE (val));
17244 if (size < 0)
17245 break;
17246 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17247 if (!loc)
17248 break;
17249 if (size == DWARF2_ADDR_SIZE)
17250 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17251 else
17252 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17253 return loc;
17254 case POINTER_PLUS_EXPR:
17255 case PLUS_EXPR:
17256 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17257 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17258 {
17259 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17260 if (!loc)
17261 break;
17262 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17263 }
17264 else
17265 {
17266 op = DW_OP_plus;
17267 do_binop:
17268 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17269 if (!loc)
17270 break;
17271 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17272 if (!loc2)
17273 break;
17274 add_loc_descr (&loc, loc2);
17275 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17276 }
17277 return loc;
17278 case MINUS_EXPR:
17279 op = DW_OP_minus;
17280 goto do_binop;
17281 case MULT_EXPR:
17282 op = DW_OP_mul;
17283 goto do_binop;
17284 case EQ_EXPR:
17285 op = DW_OP_eq;
17286 goto do_binop;
17287 case NE_EXPR:
17288 op = DW_OP_ne;
17289 goto do_binop;
17290 default:
17291 break;
17292 }
17293 return NULL;
17294 }
17295
17296 static void
17297 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17298 tree val, tree base_decl)
17299 {
17300 dw_loc_descr_ref loc;
17301
17302 if (tree_fits_shwi_p (val))
17303 {
17304 add_AT_unsigned (die, attr, tree_to_shwi (val));
17305 return;
17306 }
17307
17308 loc = descr_info_loc (val, base_decl);
17309 if (!loc)
17310 return;
17311
17312 add_AT_loc (die, attr, loc);
17313 }
17314
17315 /* This routine generates DIE for array with hidden descriptor, details
17316 are filled into *info by a langhook. */
17317
17318 static void
17319 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17320 dw_die_ref context_die)
17321 {
17322 dw_die_ref scope_die = scope_die_for (type, context_die);
17323 dw_die_ref array_die;
17324 int dim;
17325
17326 array_die = new_die (DW_TAG_array_type, scope_die, type);
17327 add_name_attribute (array_die, type_tag (type));
17328 equate_type_number_to_die (type, array_die);
17329
17330 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17331 if (is_fortran ()
17332 && info->ndimensions >= 2)
17333 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17334
17335 if (info->data_location)
17336 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17337 info->base_decl);
17338 if (info->associated)
17339 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17340 info->base_decl);
17341 if (info->allocated)
17342 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17343 info->base_decl);
17344
17345 for (dim = 0; dim < info->ndimensions; dim++)
17346 {
17347 dw_die_ref subrange_die
17348 = new_die (DW_TAG_subrange_type, array_die, NULL);
17349
17350 if (info->dimen[dim].lower_bound)
17351 {
17352 /* If it is the default value, omit it. */
17353 int dflt;
17354
17355 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17356 && (dflt = lower_bound_default ()) != -1
17357 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17358 ;
17359 else
17360 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17361 info->dimen[dim].lower_bound,
17362 info->base_decl);
17363 }
17364 if (info->dimen[dim].upper_bound)
17365 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17366 info->dimen[dim].upper_bound,
17367 info->base_decl);
17368 if (info->dimen[dim].stride)
17369 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17370 info->dimen[dim].stride,
17371 info->base_decl);
17372 }
17373
17374 gen_type_die (info->element_type, context_die);
17375 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17376
17377 if (get_AT (array_die, DW_AT_name))
17378 add_pubtype (type, array_die);
17379 }
17380
17381 #if 0
17382 static void
17383 gen_entry_point_die (tree decl, dw_die_ref context_die)
17384 {
17385 tree origin = decl_ultimate_origin (decl);
17386 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17387
17388 if (origin != NULL)
17389 add_abstract_origin_attribute (decl_die, origin);
17390 else
17391 {
17392 add_name_and_src_coords_attributes (decl_die, decl);
17393 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17394 0, 0, context_die);
17395 }
17396
17397 if (DECL_ABSTRACT (decl))
17398 equate_decl_number_to_die (decl, decl_die);
17399 else
17400 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17401 }
17402 #endif
17403
17404 /* Walk through the list of incomplete types again, trying once more to
17405 emit full debugging info for them. */
17406
17407 static void
17408 retry_incomplete_types (void)
17409 {
17410 int i;
17411
17412 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17413 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17414 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17415 }
17416
17417 /* Determine what tag to use for a record type. */
17418
17419 static enum dwarf_tag
17420 record_type_tag (tree type)
17421 {
17422 if (! lang_hooks.types.classify_record)
17423 return DW_TAG_structure_type;
17424
17425 switch (lang_hooks.types.classify_record (type))
17426 {
17427 case RECORD_IS_STRUCT:
17428 return DW_TAG_structure_type;
17429
17430 case RECORD_IS_CLASS:
17431 return DW_TAG_class_type;
17432
17433 case RECORD_IS_INTERFACE:
17434 if (dwarf_version >= 3 || !dwarf_strict)
17435 return DW_TAG_interface_type;
17436 return DW_TAG_structure_type;
17437
17438 default:
17439 gcc_unreachable ();
17440 }
17441 }
17442
17443 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17444 include all of the information about the enumeration values also. Each
17445 enumerated type name/value is listed as a child of the enumerated type
17446 DIE. */
17447
17448 static dw_die_ref
17449 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17450 {
17451 dw_die_ref type_die = lookup_type_die (type);
17452
17453 if (type_die == NULL)
17454 {
17455 type_die = new_die (DW_TAG_enumeration_type,
17456 scope_die_for (type, context_die), type);
17457 equate_type_number_to_die (type, type_die);
17458 add_name_attribute (type_die, type_tag (type));
17459 if (dwarf_version >= 4 || !dwarf_strict)
17460 {
17461 if (ENUM_IS_SCOPED (type))
17462 add_AT_flag (type_die, DW_AT_enum_class, 1);
17463 if (ENUM_IS_OPAQUE (type))
17464 add_AT_flag (type_die, DW_AT_declaration, 1);
17465 }
17466 }
17467 else if (! TYPE_SIZE (type))
17468 return type_die;
17469 else
17470 remove_AT (type_die, DW_AT_declaration);
17471
17472 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17473 given enum type is incomplete, do not generate the DW_AT_byte_size
17474 attribute or the DW_AT_element_list attribute. */
17475 if (TYPE_SIZE (type))
17476 {
17477 tree link;
17478
17479 TREE_ASM_WRITTEN (type) = 1;
17480 add_byte_size_attribute (type_die, type);
17481 if (TYPE_STUB_DECL (type) != NULL_TREE)
17482 {
17483 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17484 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17485 }
17486
17487 /* If the first reference to this type was as the return type of an
17488 inline function, then it may not have a parent. Fix this now. */
17489 if (type_die->die_parent == NULL)
17490 add_child_die (scope_die_for (type, context_die), type_die);
17491
17492 for (link = TYPE_VALUES (type);
17493 link != NULL; link = TREE_CHAIN (link))
17494 {
17495 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17496 tree value = TREE_VALUE (link);
17497
17498 add_name_attribute (enum_die,
17499 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17500
17501 if (TREE_CODE (value) == CONST_DECL)
17502 value = DECL_INITIAL (value);
17503
17504 if (simple_type_size_in_bits (TREE_TYPE (value))
17505 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17506 /* DWARF2 does not provide a way of indicating whether or
17507 not enumeration constants are signed or unsigned. GDB
17508 always assumes the values are signed, so we output all
17509 values as if they were signed. That means that
17510 enumeration constants with very large unsigned values
17511 will appear to have negative values in the debugger.
17512
17513 TODO: the above comment is wrong, DWARF2 does provide
17514 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17515 This should be re-worked to use correct signed/unsigned
17516 int/double tags for all cases, instead of always treating as
17517 signed. */
17518 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17519 else
17520 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17521 that here. */
17522 add_AT_wide (enum_die, DW_AT_const_value, value);
17523 }
17524
17525 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17526 if (TYPE_ARTIFICIAL (type))
17527 add_AT_flag (type_die, DW_AT_artificial, 1);
17528 }
17529 else
17530 add_AT_flag (type_die, DW_AT_declaration, 1);
17531
17532 add_pubtype (type, type_die);
17533
17534 return type_die;
17535 }
17536
17537 /* Generate a DIE to represent either a real live formal parameter decl or to
17538 represent just the type of some formal parameter position in some function
17539 type.
17540
17541 Note that this routine is a bit unusual because its argument may be a
17542 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17543 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17544 node. If it's the former then this function is being called to output a
17545 DIE to represent a formal parameter object (or some inlining thereof). If
17546 it's the latter, then this function is only being called to output a
17547 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17548 argument type of some subprogram type.
17549 If EMIT_NAME_P is true, name and source coordinate attributes
17550 are emitted. */
17551
17552 static dw_die_ref
17553 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17554 dw_die_ref context_die)
17555 {
17556 tree node_or_origin = node ? node : origin;
17557 tree ultimate_origin;
17558 dw_die_ref parm_die
17559 = new_die (DW_TAG_formal_parameter, context_die, node);
17560
17561 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17562 {
17563 case tcc_declaration:
17564 ultimate_origin = decl_ultimate_origin (node_or_origin);
17565 if (node || ultimate_origin)
17566 origin = ultimate_origin;
17567 if (origin != NULL)
17568 add_abstract_origin_attribute (parm_die, origin);
17569 else if (emit_name_p)
17570 add_name_and_src_coords_attributes (parm_die, node);
17571 if (origin == NULL
17572 || (! DECL_ABSTRACT (node_or_origin)
17573 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17574 decl_function_context
17575 (node_or_origin))))
17576 {
17577 tree type = TREE_TYPE (node_or_origin);
17578 if (decl_by_reference_p (node_or_origin))
17579 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17580 context_die);
17581 else
17582 add_type_attribute (parm_die, type,
17583 TREE_READONLY (node_or_origin),
17584 TREE_THIS_VOLATILE (node_or_origin),
17585 context_die);
17586 }
17587 if (origin == NULL && DECL_ARTIFICIAL (node))
17588 add_AT_flag (parm_die, DW_AT_artificial, 1);
17589
17590 if (node && node != origin)
17591 equate_decl_number_to_die (node, parm_die);
17592 if (! DECL_ABSTRACT (node_or_origin))
17593 add_location_or_const_value_attribute (parm_die, node_or_origin,
17594 node == NULL, DW_AT_location);
17595
17596 break;
17597
17598 case tcc_type:
17599 /* We were called with some kind of a ..._TYPE node. */
17600 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17601 break;
17602
17603 default:
17604 gcc_unreachable ();
17605 }
17606
17607 return parm_die;
17608 }
17609
17610 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17611 children DW_TAG_formal_parameter DIEs representing the arguments of the
17612 parameter pack.
17613
17614 PARM_PACK must be a function parameter pack.
17615 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17616 must point to the subsequent arguments of the function PACK_ARG belongs to.
17617 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17618 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17619 following the last one for which a DIE was generated. */
17620
17621 static dw_die_ref
17622 gen_formal_parameter_pack_die (tree parm_pack,
17623 tree pack_arg,
17624 dw_die_ref subr_die,
17625 tree *next_arg)
17626 {
17627 tree arg;
17628 dw_die_ref parm_pack_die;
17629
17630 gcc_assert (parm_pack
17631 && lang_hooks.function_parameter_pack_p (parm_pack)
17632 && subr_die);
17633
17634 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17635 add_src_coords_attributes (parm_pack_die, parm_pack);
17636
17637 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17638 {
17639 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17640 parm_pack))
17641 break;
17642 gen_formal_parameter_die (arg, NULL,
17643 false /* Don't emit name attribute. */,
17644 parm_pack_die);
17645 }
17646 if (next_arg)
17647 *next_arg = arg;
17648 return parm_pack_die;
17649 }
17650
17651 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17652 at the end of an (ANSI prototyped) formal parameters list. */
17653
17654 static void
17655 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17656 {
17657 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17658 }
17659
17660 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17661 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17662 parameters as specified in some function type specification (except for
17663 those which appear as part of a function *definition*). */
17664
17665 static void
17666 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17667 {
17668 tree link;
17669 tree formal_type = NULL;
17670 tree first_parm_type;
17671 tree arg;
17672
17673 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17674 {
17675 arg = DECL_ARGUMENTS (function_or_method_type);
17676 function_or_method_type = TREE_TYPE (function_or_method_type);
17677 }
17678 else
17679 arg = NULL_TREE;
17680
17681 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17682
17683 /* Make our first pass over the list of formal parameter types and output a
17684 DW_TAG_formal_parameter DIE for each one. */
17685 for (link = first_parm_type; link; )
17686 {
17687 dw_die_ref parm_die;
17688
17689 formal_type = TREE_VALUE (link);
17690 if (formal_type == void_type_node)
17691 break;
17692
17693 /* Output a (nameless) DIE to represent the formal parameter itself. */
17694 parm_die = gen_formal_parameter_die (formal_type, NULL,
17695 true /* Emit name attribute. */,
17696 context_die);
17697 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17698 && link == first_parm_type)
17699 {
17700 add_AT_flag (parm_die, DW_AT_artificial, 1);
17701 if (dwarf_version >= 3 || !dwarf_strict)
17702 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17703 }
17704 else if (arg && DECL_ARTIFICIAL (arg))
17705 add_AT_flag (parm_die, DW_AT_artificial, 1);
17706
17707 link = TREE_CHAIN (link);
17708 if (arg)
17709 arg = DECL_CHAIN (arg);
17710 }
17711
17712 /* If this function type has an ellipsis, add a
17713 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17714 if (formal_type != void_type_node)
17715 gen_unspecified_parameters_die (function_or_method_type, context_die);
17716
17717 /* Make our second (and final) pass over the list of formal parameter types
17718 and output DIEs to represent those types (as necessary). */
17719 for (link = TYPE_ARG_TYPES (function_or_method_type);
17720 link && TREE_VALUE (link);
17721 link = TREE_CHAIN (link))
17722 gen_type_die (TREE_VALUE (link), context_die);
17723 }
17724
17725 /* We want to generate the DIE for TYPE so that we can generate the
17726 die for MEMBER, which has been defined; we will need to refer back
17727 to the member declaration nested within TYPE. If we're trying to
17728 generate minimal debug info for TYPE, processing TYPE won't do the
17729 trick; we need to attach the member declaration by hand. */
17730
17731 static void
17732 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17733 {
17734 gen_type_die (type, context_die);
17735
17736 /* If we're trying to avoid duplicate debug info, we may not have
17737 emitted the member decl for this function. Emit it now. */
17738 if (TYPE_STUB_DECL (type)
17739 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17740 && ! lookup_decl_die (member))
17741 {
17742 dw_die_ref type_die;
17743 gcc_assert (!decl_ultimate_origin (member));
17744
17745 push_decl_scope (type);
17746 type_die = lookup_type_die_strip_naming_typedef (type);
17747 if (TREE_CODE (member) == FUNCTION_DECL)
17748 gen_subprogram_die (member, type_die);
17749 else if (TREE_CODE (member) == FIELD_DECL)
17750 {
17751 /* Ignore the nameless fields that are used to skip bits but handle
17752 C++ anonymous unions and structs. */
17753 if (DECL_NAME (member) != NULL_TREE
17754 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17755 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17756 {
17757 gen_type_die (member_declared_type (member), type_die);
17758 gen_field_die (member, type_die);
17759 }
17760 }
17761 else
17762 gen_variable_die (member, NULL_TREE, type_die);
17763
17764 pop_decl_scope ();
17765 }
17766 }
17767 \f
17768 /* Forward declare these functions, because they are mutually recursive
17769 with their set_block_* pairing functions. */
17770 static void set_decl_origin_self (tree);
17771 static void set_decl_abstract_flags (tree, int);
17772
17773 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17774 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17775 that it points to the node itself, thus indicating that the node is its
17776 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17777 the given node is NULL, recursively descend the decl/block tree which
17778 it is the root of, and for each other ..._DECL or BLOCK node contained
17779 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17780 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17781 values to point to themselves. */
17782
17783 static void
17784 set_block_origin_self (tree stmt)
17785 {
17786 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17787 {
17788 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17789
17790 {
17791 tree local_decl;
17792
17793 for (local_decl = BLOCK_VARS (stmt);
17794 local_decl != NULL_TREE;
17795 local_decl = DECL_CHAIN (local_decl))
17796 if (! DECL_EXTERNAL (local_decl))
17797 set_decl_origin_self (local_decl); /* Potential recursion. */
17798 }
17799
17800 {
17801 tree subblock;
17802
17803 for (subblock = BLOCK_SUBBLOCKS (stmt);
17804 subblock != NULL_TREE;
17805 subblock = BLOCK_CHAIN (subblock))
17806 set_block_origin_self (subblock); /* Recurse. */
17807 }
17808 }
17809 }
17810
17811 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17812 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17813 node to so that it points to the node itself, thus indicating that the
17814 node represents its own (abstract) origin. Additionally, if the
17815 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17816 the decl/block tree of which the given node is the root of, and for
17817 each other ..._DECL or BLOCK node contained therein whose
17818 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17819 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17820 point to themselves. */
17821
17822 static void
17823 set_decl_origin_self (tree decl)
17824 {
17825 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17826 {
17827 DECL_ABSTRACT_ORIGIN (decl) = decl;
17828 if (TREE_CODE (decl) == FUNCTION_DECL)
17829 {
17830 tree arg;
17831
17832 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17833 DECL_ABSTRACT_ORIGIN (arg) = arg;
17834 if (DECL_INITIAL (decl) != NULL_TREE
17835 && DECL_INITIAL (decl) != error_mark_node)
17836 set_block_origin_self (DECL_INITIAL (decl));
17837 }
17838 }
17839 }
17840 \f
17841 /* Given a pointer to some BLOCK node, and a boolean value to set the
17842 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17843 the given block, and for all local decls and all local sub-blocks
17844 (recursively) which are contained therein. */
17845
17846 static void
17847 set_block_abstract_flags (tree stmt, int setting)
17848 {
17849 tree local_decl;
17850 tree subblock;
17851 unsigned int i;
17852
17853 BLOCK_ABSTRACT (stmt) = setting;
17854
17855 for (local_decl = BLOCK_VARS (stmt);
17856 local_decl != NULL_TREE;
17857 local_decl = DECL_CHAIN (local_decl))
17858 if (! DECL_EXTERNAL (local_decl))
17859 set_decl_abstract_flags (local_decl, setting);
17860
17861 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17862 {
17863 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17864 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17865 || TREE_CODE (local_decl) == PARM_DECL)
17866 set_decl_abstract_flags (local_decl, setting);
17867 }
17868
17869 for (subblock = BLOCK_SUBBLOCKS (stmt);
17870 subblock != NULL_TREE;
17871 subblock = BLOCK_CHAIN (subblock))
17872 set_block_abstract_flags (subblock, setting);
17873 }
17874
17875 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17876 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17877 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17878 set the abstract flags for all of the parameters, local vars, local
17879 blocks and sub-blocks (recursively) to the same setting. */
17880
17881 static void
17882 set_decl_abstract_flags (tree decl, int setting)
17883 {
17884 DECL_ABSTRACT (decl) = setting;
17885 if (TREE_CODE (decl) == FUNCTION_DECL)
17886 {
17887 tree arg;
17888
17889 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17890 DECL_ABSTRACT (arg) = setting;
17891 if (DECL_INITIAL (decl) != NULL_TREE
17892 && DECL_INITIAL (decl) != error_mark_node)
17893 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17894 }
17895 }
17896
17897 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17898 may later generate inlined and/or out-of-line instances of. */
17899
17900 static void
17901 dwarf2out_abstract_function (tree decl)
17902 {
17903 dw_die_ref old_die;
17904 tree save_fn;
17905 tree context;
17906 int was_abstract;
17907 htab_t old_decl_loc_table;
17908 htab_t old_cached_dw_loc_list_table;
17909 int old_call_site_count, old_tail_call_site_count;
17910 struct call_arg_loc_node *old_call_arg_locations;
17911
17912 /* Make sure we have the actual abstract inline, not a clone. */
17913 decl = DECL_ORIGIN (decl);
17914
17915 old_die = lookup_decl_die (decl);
17916 if (old_die && get_AT (old_die, DW_AT_inline))
17917 /* We've already generated the abstract instance. */
17918 return;
17919
17920 /* We can be called while recursively when seeing block defining inlined subroutine
17921 DIE. Be sure to not clobber the outer location table nor use it or we would
17922 get locations in abstract instantces. */
17923 old_decl_loc_table = decl_loc_table;
17924 decl_loc_table = NULL;
17925 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17926 cached_dw_loc_list_table = NULL;
17927 old_call_arg_locations = call_arg_locations;
17928 call_arg_locations = NULL;
17929 old_call_site_count = call_site_count;
17930 call_site_count = -1;
17931 old_tail_call_site_count = tail_call_site_count;
17932 tail_call_site_count = -1;
17933
17934 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17935 we don't get confused by DECL_ABSTRACT. */
17936 if (debug_info_level > DINFO_LEVEL_TERSE)
17937 {
17938 context = decl_class_context (decl);
17939 if (context)
17940 gen_type_die_for_member
17941 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17942 }
17943
17944 /* Pretend we've just finished compiling this function. */
17945 save_fn = current_function_decl;
17946 current_function_decl = decl;
17947
17948 was_abstract = DECL_ABSTRACT (decl);
17949 set_decl_abstract_flags (decl, 1);
17950 dwarf2out_decl (decl);
17951 if (! was_abstract)
17952 set_decl_abstract_flags (decl, 0);
17953
17954 current_function_decl = save_fn;
17955 decl_loc_table = old_decl_loc_table;
17956 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17957 call_arg_locations = old_call_arg_locations;
17958 call_site_count = old_call_site_count;
17959 tail_call_site_count = old_tail_call_site_count;
17960 }
17961
17962 /* Helper function of premark_used_types() which gets called through
17963 htab_traverse.
17964
17965 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17966 marked as unused by prune_unused_types. */
17967
17968 static int
17969 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17970 {
17971 tree type;
17972 dw_die_ref die;
17973
17974 type = (tree) *slot;
17975 die = lookup_type_die (type);
17976 if (die != NULL)
17977 die->die_perennial_p = 1;
17978 return 1;
17979 }
17980
17981 /* Helper function of premark_types_used_by_global_vars which gets called
17982 through htab_traverse.
17983
17984 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17985 marked as unused by prune_unused_types. The DIE of the type is marked
17986 only if the global variable using the type will actually be emitted. */
17987
17988 static int
17989 premark_types_used_by_global_vars_helper (void **slot,
17990 void *data ATTRIBUTE_UNUSED)
17991 {
17992 struct types_used_by_vars_entry *entry;
17993 dw_die_ref die;
17994
17995 entry = (struct types_used_by_vars_entry *) *slot;
17996 gcc_assert (entry->type != NULL
17997 && entry->var_decl != NULL);
17998 die = lookup_type_die (entry->type);
17999 if (die)
18000 {
18001 /* Ask cgraph if the global variable really is to be emitted.
18002 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18003 struct varpool_node *node = varpool_get_node (entry->var_decl);
18004 if (node && node->definition)
18005 {
18006 die->die_perennial_p = 1;
18007 /* Keep the parent DIEs as well. */
18008 while ((die = die->die_parent) && die->die_perennial_p == 0)
18009 die->die_perennial_p = 1;
18010 }
18011 }
18012 return 1;
18013 }
18014
18015 /* Mark all members of used_types_hash as perennial. */
18016
18017 static void
18018 premark_used_types (struct function *fun)
18019 {
18020 if (fun && fun->used_types_hash)
18021 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
18022 }
18023
18024 /* Mark all members of types_used_by_vars_entry as perennial. */
18025
18026 static void
18027 premark_types_used_by_global_vars (void)
18028 {
18029 if (types_used_by_vars_hash)
18030 htab_traverse (types_used_by_vars_hash,
18031 premark_types_used_by_global_vars_helper, NULL);
18032 }
18033
18034 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18035 for CA_LOC call arg loc node. */
18036
18037 static dw_die_ref
18038 gen_call_site_die (tree decl, dw_die_ref subr_die,
18039 struct call_arg_loc_node *ca_loc)
18040 {
18041 dw_die_ref stmt_die = NULL, die;
18042 tree block = ca_loc->block;
18043
18044 while (block
18045 && block != DECL_INITIAL (decl)
18046 && TREE_CODE (block) == BLOCK)
18047 {
18048 if (block_map.length () > BLOCK_NUMBER (block))
18049 stmt_die = block_map[BLOCK_NUMBER (block)];
18050 if (stmt_die)
18051 break;
18052 block = BLOCK_SUPERCONTEXT (block);
18053 }
18054 if (stmt_die == NULL)
18055 stmt_die = subr_die;
18056 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18057 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18058 if (ca_loc->tail_call_p)
18059 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18060 if (ca_loc->symbol_ref)
18061 {
18062 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18063 if (tdie)
18064 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18065 else
18066 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18067 }
18068 return die;
18069 }
18070
18071 /* Generate a DIE to represent a declared function (either file-scope or
18072 block-local). */
18073
18074 static void
18075 gen_subprogram_die (tree decl, dw_die_ref context_die)
18076 {
18077 tree origin = decl_ultimate_origin (decl);
18078 dw_die_ref subr_die;
18079 tree outer_scope;
18080 dw_die_ref old_die = lookup_decl_die (decl);
18081 int declaration = (current_function_decl != decl
18082 || class_or_namespace_scope_p (context_die));
18083
18084 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18085
18086 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18087 started to generate the abstract instance of an inline, decided to output
18088 its containing class, and proceeded to emit the declaration of the inline
18089 from the member list for the class. If so, DECLARATION takes priority;
18090 we'll get back to the abstract instance when done with the class. */
18091
18092 /* The class-scope declaration DIE must be the primary DIE. */
18093 if (origin && declaration && class_or_namespace_scope_p (context_die))
18094 {
18095 origin = NULL;
18096 gcc_assert (!old_die);
18097 }
18098
18099 /* Now that the C++ front end lazily declares artificial member fns, we
18100 might need to retrofit the declaration into its class. */
18101 if (!declaration && !origin && !old_die
18102 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18103 && !class_or_namespace_scope_p (context_die)
18104 && debug_info_level > DINFO_LEVEL_TERSE)
18105 old_die = force_decl_die (decl);
18106
18107 if (origin != NULL)
18108 {
18109 gcc_assert (!declaration || local_scope_p (context_die));
18110
18111 /* Fixup die_parent for the abstract instance of a nested
18112 inline function. */
18113 if (old_die && old_die->die_parent == NULL)
18114 add_child_die (context_die, old_die);
18115
18116 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18117 add_abstract_origin_attribute (subr_die, origin);
18118 /* This is where the actual code for a cloned function is.
18119 Let's emit linkage name attribute for it. This helps
18120 debuggers to e.g, set breakpoints into
18121 constructors/destructors when the user asks "break
18122 K::K". */
18123 add_linkage_name (subr_die, decl);
18124 }
18125 else if (old_die)
18126 {
18127 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18128 struct dwarf_file_data * file_index = lookup_filename (s.file);
18129
18130 if (!get_AT_flag (old_die, DW_AT_declaration)
18131 /* We can have a normal definition following an inline one in the
18132 case of redefinition of GNU C extern inlines.
18133 It seems reasonable to use AT_specification in this case. */
18134 && !get_AT (old_die, DW_AT_inline))
18135 {
18136 /* Detect and ignore this case, where we are trying to output
18137 something we have already output. */
18138 return;
18139 }
18140
18141 /* If the definition comes from the same place as the declaration,
18142 maybe use the old DIE. We always want the DIE for this function
18143 that has the *_pc attributes to be under comp_unit_die so the
18144 debugger can find it. We also need to do this for abstract
18145 instances of inlines, since the spec requires the out-of-line copy
18146 to have the same parent. For local class methods, this doesn't
18147 apply; we just use the old DIE. */
18148 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18149 && (DECL_ARTIFICIAL (decl)
18150 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18151 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18152 == (unsigned) s.line))))
18153 {
18154 subr_die = old_die;
18155
18156 /* Clear out the declaration attribute and the formal parameters.
18157 Do not remove all children, because it is possible that this
18158 declaration die was forced using force_decl_die(). In such
18159 cases die that forced declaration die (e.g. TAG_imported_module)
18160 is one of the children that we do not want to remove. */
18161 remove_AT (subr_die, DW_AT_declaration);
18162 remove_AT (subr_die, DW_AT_object_pointer);
18163 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18164 }
18165 else
18166 {
18167 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18168 add_AT_specification (subr_die, old_die);
18169 add_pubname (decl, subr_die);
18170 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18171 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18172 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18173 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18174 }
18175 }
18176 else
18177 {
18178 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18179
18180 if (TREE_PUBLIC (decl))
18181 add_AT_flag (subr_die, DW_AT_external, 1);
18182
18183 add_name_and_src_coords_attributes (subr_die, decl);
18184 add_pubname (decl, subr_die);
18185 if (debug_info_level > DINFO_LEVEL_TERSE)
18186 {
18187 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18188 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18189 0, 0, context_die);
18190 }
18191
18192 add_pure_or_virtual_attribute (subr_die, decl);
18193 if (DECL_ARTIFICIAL (decl))
18194 add_AT_flag (subr_die, DW_AT_artificial, 1);
18195
18196 add_accessibility_attribute (subr_die, decl);
18197 }
18198
18199 if (declaration)
18200 {
18201 if (!old_die || !get_AT (old_die, DW_AT_inline))
18202 {
18203 add_AT_flag (subr_die, DW_AT_declaration, 1);
18204
18205 /* If this is an explicit function declaration then generate
18206 a DW_AT_explicit attribute. */
18207 if (lang_hooks.decls.function_decl_explicit_p (decl)
18208 && (dwarf_version >= 3 || !dwarf_strict))
18209 add_AT_flag (subr_die, DW_AT_explicit, 1);
18210
18211 /* The first time we see a member function, it is in the context of
18212 the class to which it belongs. We make sure of this by emitting
18213 the class first. The next time is the definition, which is
18214 handled above. The two may come from the same source text.
18215
18216 Note that force_decl_die() forces function declaration die. It is
18217 later reused to represent definition. */
18218 equate_decl_number_to_die (decl, subr_die);
18219 }
18220 }
18221 else if (DECL_ABSTRACT (decl))
18222 {
18223 if (DECL_DECLARED_INLINE_P (decl))
18224 {
18225 if (cgraph_function_possibly_inlined_p (decl))
18226 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18227 else
18228 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18229 }
18230 else
18231 {
18232 if (cgraph_function_possibly_inlined_p (decl))
18233 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18234 else
18235 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18236 }
18237
18238 if (DECL_DECLARED_INLINE_P (decl)
18239 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18240 add_AT_flag (subr_die, DW_AT_artificial, 1);
18241
18242 equate_decl_number_to_die (decl, subr_die);
18243 }
18244 else if (!DECL_EXTERNAL (decl))
18245 {
18246 HOST_WIDE_INT cfa_fb_offset;
18247 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18248
18249 if (!old_die || !get_AT (old_die, DW_AT_inline))
18250 equate_decl_number_to_die (decl, subr_die);
18251
18252 gcc_checking_assert (fun);
18253 if (!flag_reorder_blocks_and_partition)
18254 {
18255 dw_fde_ref fde = fun->fde;
18256 if (fde->dw_fde_begin)
18257 {
18258 /* We have already generated the labels. */
18259 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18260 fde->dw_fde_end, false);
18261 }
18262 else
18263 {
18264 /* Create start/end labels and add the range. */
18265 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18266 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18267 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18268 current_function_funcdef_no);
18269 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18270 current_function_funcdef_no);
18271 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18272 false);
18273 }
18274
18275 #if VMS_DEBUGGING_INFO
18276 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18277 Section 2.3 Prologue and Epilogue Attributes:
18278 When a breakpoint is set on entry to a function, it is generally
18279 desirable for execution to be suspended, not on the very first
18280 instruction of the function, but rather at a point after the
18281 function's frame has been set up, after any language defined local
18282 declaration processing has been completed, and before execution of
18283 the first statement of the function begins. Debuggers generally
18284 cannot properly determine where this point is. Similarly for a
18285 breakpoint set on exit from a function. The prologue and epilogue
18286 attributes allow a compiler to communicate the location(s) to use. */
18287
18288 {
18289 if (fde->dw_fde_vms_end_prologue)
18290 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18291 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18292
18293 if (fde->dw_fde_vms_begin_epilogue)
18294 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18295 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18296 }
18297 #endif
18298
18299 }
18300 else
18301 {
18302 /* Generate pubnames entries for the split function code ranges. */
18303 dw_fde_ref fde = fun->fde;
18304
18305 if (fde->dw_fde_second_begin)
18306 {
18307 if (dwarf_version >= 3 || !dwarf_strict)
18308 {
18309 /* We should use ranges for non-contiguous code section
18310 addresses. Use the actual code range for the initial
18311 section, since the HOT/COLD labels might precede an
18312 alignment offset. */
18313 bool range_list_added = false;
18314 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18315 fde->dw_fde_end, &range_list_added,
18316 false);
18317 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18318 fde->dw_fde_second_end,
18319 &range_list_added, false);
18320 if (range_list_added)
18321 add_ranges (NULL);
18322 }
18323 else
18324 {
18325 /* There is no real support in DW2 for this .. so we make
18326 a work-around. First, emit the pub name for the segment
18327 containing the function label. Then make and emit a
18328 simplified subprogram DIE for the second segment with the
18329 name pre-fixed by __hot/cold_sect_of_. We use the same
18330 linkage name for the second die so that gdb will find both
18331 sections when given "b foo". */
18332 const char *name = NULL;
18333 tree decl_name = DECL_NAME (decl);
18334 dw_die_ref seg_die;
18335
18336 /* Do the 'primary' section. */
18337 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18338 fde->dw_fde_end, false);
18339
18340 /* Build a minimal DIE for the secondary section. */
18341 seg_die = new_die (DW_TAG_subprogram,
18342 subr_die->die_parent, decl);
18343
18344 if (TREE_PUBLIC (decl))
18345 add_AT_flag (seg_die, DW_AT_external, 1);
18346
18347 if (decl_name != NULL
18348 && IDENTIFIER_POINTER (decl_name) != NULL)
18349 {
18350 name = dwarf2_name (decl, 1);
18351 if (! DECL_ARTIFICIAL (decl))
18352 add_src_coords_attributes (seg_die, decl);
18353
18354 add_linkage_name (seg_die, decl);
18355 }
18356 gcc_assert (name != NULL);
18357 add_pure_or_virtual_attribute (seg_die, decl);
18358 if (DECL_ARTIFICIAL (decl))
18359 add_AT_flag (seg_die, DW_AT_artificial, 1);
18360
18361 name = concat ("__second_sect_of_", name, NULL);
18362 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18363 fde->dw_fde_second_end, false);
18364 add_name_attribute (seg_die, name);
18365 if (want_pubnames ())
18366 add_pubname_string (name, seg_die);
18367 }
18368 }
18369 else
18370 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18371 false);
18372 }
18373
18374 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18375
18376 /* We define the "frame base" as the function's CFA. This is more
18377 convenient for several reasons: (1) It's stable across the prologue
18378 and epilogue, which makes it better than just a frame pointer,
18379 (2) With dwarf3, there exists a one-byte encoding that allows us
18380 to reference the .debug_frame data by proxy, but failing that,
18381 (3) We can at least reuse the code inspection and interpretation
18382 code that determines the CFA position at various points in the
18383 function. */
18384 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18385 {
18386 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18387 add_AT_loc (subr_die, DW_AT_frame_base, op);
18388 }
18389 else
18390 {
18391 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18392 if (list->dw_loc_next)
18393 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18394 else
18395 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18396 }
18397
18398 /* Compute a displacement from the "steady-state frame pointer" to
18399 the CFA. The former is what all stack slots and argument slots
18400 will reference in the rtl; the latter is what we've told the
18401 debugger about. We'll need to adjust all frame_base references
18402 by this displacement. */
18403 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18404
18405 if (fun->static_chain_decl)
18406 add_AT_location_description (subr_die, DW_AT_static_link,
18407 loc_list_from_tree (fun->static_chain_decl, 2));
18408 }
18409
18410 /* Generate child dies for template paramaters. */
18411 if (debug_info_level > DINFO_LEVEL_TERSE)
18412 gen_generic_params_dies (decl);
18413
18414 /* Now output descriptions of the arguments for this function. This gets
18415 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18416 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18417 `...' at the end of the formal parameter list. In order to find out if
18418 there was a trailing ellipsis or not, we must instead look at the type
18419 associated with the FUNCTION_DECL. This will be a node of type
18420 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18421 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18422 an ellipsis at the end. */
18423
18424 /* In the case where we are describing a mere function declaration, all we
18425 need to do here (and all we *can* do here) is to describe the *types* of
18426 its formal parameters. */
18427 if (debug_info_level <= DINFO_LEVEL_TERSE)
18428 ;
18429 else if (declaration)
18430 gen_formal_types_die (decl, subr_die);
18431 else
18432 {
18433 /* Generate DIEs to represent all known formal parameters. */
18434 tree parm = DECL_ARGUMENTS (decl);
18435 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18436 tree generic_decl_parm = generic_decl
18437 ? DECL_ARGUMENTS (generic_decl)
18438 : NULL;
18439
18440 /* Now we want to walk the list of parameters of the function and
18441 emit their relevant DIEs.
18442
18443 We consider the case of DECL being an instance of a generic function
18444 as well as it being a normal function.
18445
18446 If DECL is an instance of a generic function we walk the
18447 parameters of the generic function declaration _and_ the parameters of
18448 DECL itself. This is useful because we want to emit specific DIEs for
18449 function parameter packs and those are declared as part of the
18450 generic function declaration. In that particular case,
18451 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18452 That DIE has children DIEs representing the set of arguments
18453 of the pack. Note that the set of pack arguments can be empty.
18454 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18455 children DIE.
18456
18457 Otherwise, we just consider the parameters of DECL. */
18458 while (generic_decl_parm || parm)
18459 {
18460 if (generic_decl_parm
18461 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18462 gen_formal_parameter_pack_die (generic_decl_parm,
18463 parm, subr_die,
18464 &parm);
18465 else if (parm && !POINTER_BOUNDS_P (parm))
18466 {
18467 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18468
18469 if (parm == DECL_ARGUMENTS (decl)
18470 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18471 && parm_die
18472 && (dwarf_version >= 3 || !dwarf_strict))
18473 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18474
18475 parm = DECL_CHAIN (parm);
18476 }
18477 else if (parm)
18478 parm = DECL_CHAIN (parm);
18479
18480 if (generic_decl_parm)
18481 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18482 }
18483
18484 /* Decide whether we need an unspecified_parameters DIE at the end.
18485 There are 2 more cases to do this for: 1) the ansi ... declaration -
18486 this is detectable when the end of the arg list is not a
18487 void_type_node 2) an unprototyped function declaration (not a
18488 definition). This just means that we have no info about the
18489 parameters at all. */
18490 if (prototype_p (TREE_TYPE (decl)))
18491 {
18492 /* This is the prototyped case, check for.... */
18493 if (stdarg_p (TREE_TYPE (decl)))
18494 gen_unspecified_parameters_die (decl, subr_die);
18495 }
18496 else if (DECL_INITIAL (decl) == NULL_TREE)
18497 gen_unspecified_parameters_die (decl, subr_die);
18498 }
18499
18500 /* Output Dwarf info for all of the stuff within the body of the function
18501 (if it has one - it may be just a declaration). */
18502 outer_scope = DECL_INITIAL (decl);
18503
18504 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18505 a function. This BLOCK actually represents the outermost binding contour
18506 for the function, i.e. the contour in which the function's formal
18507 parameters and labels get declared. Curiously, it appears that the front
18508 end doesn't actually put the PARM_DECL nodes for the current function onto
18509 the BLOCK_VARS list for this outer scope, but are strung off of the
18510 DECL_ARGUMENTS list for the function instead.
18511
18512 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18513 the LABEL_DECL nodes for the function however, and we output DWARF info
18514 for those in decls_for_scope. Just within the `outer_scope' there will be
18515 a BLOCK node representing the function's outermost pair of curly braces,
18516 and any blocks used for the base and member initializers of a C++
18517 constructor function. */
18518 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18519 {
18520 int call_site_note_count = 0;
18521 int tail_call_site_note_count = 0;
18522
18523 /* Emit a DW_TAG_variable DIE for a named return value. */
18524 if (DECL_NAME (DECL_RESULT (decl)))
18525 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18526
18527 current_function_has_inlines = 0;
18528 decls_for_scope (outer_scope, subr_die, 0);
18529
18530 if (call_arg_locations && !dwarf_strict)
18531 {
18532 struct call_arg_loc_node *ca_loc;
18533 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18534 {
18535 dw_die_ref die = NULL;
18536 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18537 rtx arg, next_arg;
18538
18539 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18540 arg; arg = next_arg)
18541 {
18542 dw_loc_descr_ref reg, val;
18543 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18544 dw_die_ref cdie, tdie = NULL;
18545
18546 next_arg = XEXP (arg, 1);
18547 if (REG_P (XEXP (XEXP (arg, 0), 0))
18548 && next_arg
18549 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18550 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18551 && REGNO (XEXP (XEXP (arg, 0), 0))
18552 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18553 next_arg = XEXP (next_arg, 1);
18554 if (mode == VOIDmode)
18555 {
18556 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18557 if (mode == VOIDmode)
18558 mode = GET_MODE (XEXP (arg, 0));
18559 }
18560 if (mode == VOIDmode || mode == BLKmode)
18561 continue;
18562 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18563 {
18564 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18565 tloc = XEXP (XEXP (arg, 0), 1);
18566 continue;
18567 }
18568 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18569 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18570 {
18571 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18572 tlocc = XEXP (XEXP (arg, 0), 1);
18573 continue;
18574 }
18575 reg = NULL;
18576 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18577 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18578 VAR_INIT_STATUS_INITIALIZED);
18579 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18580 {
18581 rtx mem = XEXP (XEXP (arg, 0), 0);
18582 reg = mem_loc_descriptor (XEXP (mem, 0),
18583 get_address_mode (mem),
18584 GET_MODE (mem),
18585 VAR_INIT_STATUS_INITIALIZED);
18586 }
18587 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18588 == DEBUG_PARAMETER_REF)
18589 {
18590 tree tdecl
18591 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18592 tdie = lookup_decl_die (tdecl);
18593 if (tdie == NULL)
18594 continue;
18595 }
18596 else
18597 continue;
18598 if (reg == NULL
18599 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18600 != DEBUG_PARAMETER_REF)
18601 continue;
18602 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18603 VOIDmode,
18604 VAR_INIT_STATUS_INITIALIZED);
18605 if (val == NULL)
18606 continue;
18607 if (die == NULL)
18608 die = gen_call_site_die (decl, subr_die, ca_loc);
18609 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18610 NULL_TREE);
18611 if (reg != NULL)
18612 add_AT_loc (cdie, DW_AT_location, reg);
18613 else if (tdie != NULL)
18614 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18615 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18616 if (next_arg != XEXP (arg, 1))
18617 {
18618 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18619 if (mode == VOIDmode)
18620 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18621 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18622 0), 1),
18623 mode, VOIDmode,
18624 VAR_INIT_STATUS_INITIALIZED);
18625 if (val != NULL)
18626 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18627 }
18628 }
18629 if (die == NULL
18630 && (ca_loc->symbol_ref || tloc))
18631 die = gen_call_site_die (decl, subr_die, ca_loc);
18632 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18633 {
18634 dw_loc_descr_ref tval = NULL;
18635
18636 if (tloc != NULL_RTX)
18637 tval = mem_loc_descriptor (tloc,
18638 GET_MODE (tloc) == VOIDmode
18639 ? Pmode : GET_MODE (tloc),
18640 VOIDmode,
18641 VAR_INIT_STATUS_INITIALIZED);
18642 if (tval)
18643 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18644 else if (tlocc != NULL_RTX)
18645 {
18646 tval = mem_loc_descriptor (tlocc,
18647 GET_MODE (tlocc) == VOIDmode
18648 ? Pmode : GET_MODE (tlocc),
18649 VOIDmode,
18650 VAR_INIT_STATUS_INITIALIZED);
18651 if (tval)
18652 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18653 tval);
18654 }
18655 }
18656 if (die != NULL)
18657 {
18658 call_site_note_count++;
18659 if (ca_loc->tail_call_p)
18660 tail_call_site_note_count++;
18661 }
18662 }
18663 }
18664 call_arg_locations = NULL;
18665 call_arg_loc_last = NULL;
18666 if (tail_call_site_count >= 0
18667 && tail_call_site_count == tail_call_site_note_count
18668 && !dwarf_strict)
18669 {
18670 if (call_site_count >= 0
18671 && call_site_count == call_site_note_count)
18672 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18673 else
18674 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18675 }
18676 call_site_count = -1;
18677 tail_call_site_count = -1;
18678 }
18679 /* Add the calling convention attribute if requested. */
18680 add_calling_convention_attribute (subr_die, decl);
18681
18682 }
18683
18684 /* Returns a hash value for X (which really is a die_struct). */
18685
18686 static hashval_t
18687 common_block_die_table_hash (const void *x)
18688 {
18689 const_dw_die_ref d = (const_dw_die_ref) x;
18690 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18691 }
18692
18693 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18694 as decl_id and die_parent of die_struct Y. */
18695
18696 static int
18697 common_block_die_table_eq (const void *x, const void *y)
18698 {
18699 const_dw_die_ref d = (const_dw_die_ref) x;
18700 const_dw_die_ref e = (const_dw_die_ref) y;
18701 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18702 }
18703
18704 /* Generate a DIE to represent a declared data object.
18705 Either DECL or ORIGIN must be non-null. */
18706
18707 static void
18708 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18709 {
18710 HOST_WIDE_INT off = 0;
18711 tree com_decl;
18712 tree decl_or_origin = decl ? decl : origin;
18713 tree ultimate_origin;
18714 dw_die_ref var_die;
18715 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18716 dw_die_ref origin_die;
18717 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18718 || class_or_namespace_scope_p (context_die));
18719 bool specialization_p = false;
18720
18721 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18722 if (decl || ultimate_origin)
18723 origin = ultimate_origin;
18724 com_decl = fortran_common (decl_or_origin, &off);
18725
18726 /* Symbol in common gets emitted as a child of the common block, in the form
18727 of a data member. */
18728 if (com_decl)
18729 {
18730 dw_die_ref com_die;
18731 dw_loc_list_ref loc;
18732 die_node com_die_arg;
18733
18734 var_die = lookup_decl_die (decl_or_origin);
18735 if (var_die)
18736 {
18737 if (get_AT (var_die, DW_AT_location) == NULL)
18738 {
18739 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18740 if (loc)
18741 {
18742 if (off)
18743 {
18744 /* Optimize the common case. */
18745 if (single_element_loc_list_p (loc)
18746 && loc->expr->dw_loc_opc == DW_OP_addr
18747 && loc->expr->dw_loc_next == NULL
18748 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18749 == SYMBOL_REF)
18750 {
18751 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18752 loc->expr->dw_loc_oprnd1.v.val_addr
18753 = plus_constant (GET_MODE (x), x , off);
18754 }
18755 else
18756 loc_list_plus_const (loc, off);
18757 }
18758 add_AT_location_description (var_die, DW_AT_location, loc);
18759 remove_AT (var_die, DW_AT_declaration);
18760 }
18761 }
18762 return;
18763 }
18764
18765 if (common_block_die_table == NULL)
18766 common_block_die_table
18767 = htab_create_ggc (10, common_block_die_table_hash,
18768 common_block_die_table_eq, NULL);
18769
18770 com_die_arg.decl_id = DECL_UID (com_decl);
18771 com_die_arg.die_parent = context_die;
18772 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18773 loc = loc_list_from_tree (com_decl, 2);
18774 if (com_die == NULL)
18775 {
18776 const char *cnam
18777 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18778 void **slot;
18779
18780 com_die = new_die (DW_TAG_common_block, context_die, decl);
18781 add_name_and_src_coords_attributes (com_die, com_decl);
18782 if (loc)
18783 {
18784 add_AT_location_description (com_die, DW_AT_location, loc);
18785 /* Avoid sharing the same loc descriptor between
18786 DW_TAG_common_block and DW_TAG_variable. */
18787 loc = loc_list_from_tree (com_decl, 2);
18788 }
18789 else if (DECL_EXTERNAL (decl))
18790 add_AT_flag (com_die, DW_AT_declaration, 1);
18791 if (want_pubnames ())
18792 add_pubname_string (cnam, com_die); /* ??? needed? */
18793 com_die->decl_id = DECL_UID (com_decl);
18794 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18795 *slot = (void *) com_die;
18796 }
18797 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18798 {
18799 add_AT_location_description (com_die, DW_AT_location, loc);
18800 loc = loc_list_from_tree (com_decl, 2);
18801 remove_AT (com_die, DW_AT_declaration);
18802 }
18803 var_die = new_die (DW_TAG_variable, com_die, decl);
18804 add_name_and_src_coords_attributes (var_die, decl);
18805 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18806 TREE_THIS_VOLATILE (decl), context_die);
18807 add_AT_flag (var_die, DW_AT_external, 1);
18808 if (loc)
18809 {
18810 if (off)
18811 {
18812 /* Optimize the common case. */
18813 if (single_element_loc_list_p (loc)
18814 && loc->expr->dw_loc_opc == DW_OP_addr
18815 && loc->expr->dw_loc_next == NULL
18816 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18817 {
18818 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18819 loc->expr->dw_loc_oprnd1.v.val_addr
18820 = plus_constant (GET_MODE (x), x, off);
18821 }
18822 else
18823 loc_list_plus_const (loc, off);
18824 }
18825 add_AT_location_description (var_die, DW_AT_location, loc);
18826 }
18827 else if (DECL_EXTERNAL (decl))
18828 add_AT_flag (var_die, DW_AT_declaration, 1);
18829 equate_decl_number_to_die (decl, var_die);
18830 return;
18831 }
18832
18833 /* If the compiler emitted a definition for the DECL declaration
18834 and if we already emitted a DIE for it, don't emit a second
18835 DIE for it again. Allow re-declarations of DECLs that are
18836 inside functions, though. */
18837 if (old_die && declaration && !local_scope_p (context_die))
18838 return;
18839
18840 /* For static data members, the declaration in the class is supposed
18841 to have DW_TAG_member tag; the specification should still be
18842 DW_TAG_variable referencing the DW_TAG_member DIE. */
18843 if (declaration && class_scope_p (context_die))
18844 var_die = new_die (DW_TAG_member, context_die, decl);
18845 else
18846 var_die = new_die (DW_TAG_variable, context_die, decl);
18847
18848 origin_die = NULL;
18849 if (origin != NULL)
18850 origin_die = add_abstract_origin_attribute (var_die, origin);
18851
18852 /* Loop unrolling can create multiple blocks that refer to the same
18853 static variable, so we must test for the DW_AT_declaration flag.
18854
18855 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18856 copy decls and set the DECL_ABSTRACT flag on them instead of
18857 sharing them.
18858
18859 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18860
18861 ??? The declare_in_namespace support causes us to get two DIEs for one
18862 variable, both of which are declarations. We want to avoid considering
18863 one to be a specification, so we must test that this DIE is not a
18864 declaration. */
18865 else if (old_die && TREE_STATIC (decl) && ! declaration
18866 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18867 {
18868 /* This is a definition of a C++ class level static. */
18869 add_AT_specification (var_die, old_die);
18870 specialization_p = true;
18871 if (DECL_NAME (decl))
18872 {
18873 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18874 struct dwarf_file_data * file_index = lookup_filename (s.file);
18875
18876 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18877 add_AT_file (var_die, DW_AT_decl_file, file_index);
18878
18879 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18880 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18881
18882 if (old_die->die_tag == DW_TAG_member)
18883 add_linkage_name (var_die, decl);
18884 }
18885 }
18886 else
18887 add_name_and_src_coords_attributes (var_die, decl);
18888
18889 if ((origin == NULL && !specialization_p)
18890 || (origin != NULL
18891 && !DECL_ABSTRACT (decl_or_origin)
18892 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18893 decl_function_context
18894 (decl_or_origin))))
18895 {
18896 tree type = TREE_TYPE (decl_or_origin);
18897
18898 if (decl_by_reference_p (decl_or_origin))
18899 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18900 else
18901 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18902 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18903 }
18904
18905 if (origin == NULL && !specialization_p)
18906 {
18907 if (TREE_PUBLIC (decl))
18908 add_AT_flag (var_die, DW_AT_external, 1);
18909
18910 if (DECL_ARTIFICIAL (decl))
18911 add_AT_flag (var_die, DW_AT_artificial, 1);
18912
18913 add_accessibility_attribute (var_die, decl);
18914 }
18915
18916 if (declaration)
18917 add_AT_flag (var_die, DW_AT_declaration, 1);
18918
18919 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18920 equate_decl_number_to_die (decl, var_die);
18921
18922 if (! declaration
18923 && (! DECL_ABSTRACT (decl_or_origin)
18924 /* Local static vars are shared between all clones/inlines,
18925 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18926 already set. */
18927 || (TREE_CODE (decl_or_origin) == VAR_DECL
18928 && TREE_STATIC (decl_or_origin)
18929 && DECL_RTL_SET_P (decl_or_origin)))
18930 /* When abstract origin already has DW_AT_location attribute, no need
18931 to add it again. */
18932 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18933 {
18934 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18935 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18936 defer_location (decl_or_origin, var_die);
18937 else
18938 add_location_or_const_value_attribute (var_die, decl_or_origin,
18939 decl == NULL, DW_AT_location);
18940 add_pubname (decl_or_origin, var_die);
18941 }
18942 else
18943 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18944 }
18945
18946 /* Generate a DIE to represent a named constant. */
18947
18948 static void
18949 gen_const_die (tree decl, dw_die_ref context_die)
18950 {
18951 dw_die_ref const_die;
18952 tree type = TREE_TYPE (decl);
18953
18954 const_die = new_die (DW_TAG_constant, context_die, decl);
18955 add_name_and_src_coords_attributes (const_die, decl);
18956 add_type_attribute (const_die, type, 1, 0, context_die);
18957 if (TREE_PUBLIC (decl))
18958 add_AT_flag (const_die, DW_AT_external, 1);
18959 if (DECL_ARTIFICIAL (decl))
18960 add_AT_flag (const_die, DW_AT_artificial, 1);
18961 tree_add_const_value_attribute_for_decl (const_die, decl);
18962 }
18963
18964 /* Generate a DIE to represent a label identifier. */
18965
18966 static void
18967 gen_label_die (tree decl, dw_die_ref context_die)
18968 {
18969 tree origin = decl_ultimate_origin (decl);
18970 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18971 rtx insn;
18972 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18973
18974 if (origin != NULL)
18975 add_abstract_origin_attribute (lbl_die, origin);
18976 else
18977 add_name_and_src_coords_attributes (lbl_die, decl);
18978
18979 if (DECL_ABSTRACT (decl))
18980 equate_decl_number_to_die (decl, lbl_die);
18981 else
18982 {
18983 insn = DECL_RTL_IF_SET (decl);
18984
18985 /* Deleted labels are programmer specified labels which have been
18986 eliminated because of various optimizations. We still emit them
18987 here so that it is possible to put breakpoints on them. */
18988 if (insn
18989 && (LABEL_P (insn)
18990 || ((NOTE_P (insn)
18991 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18992 {
18993 /* When optimization is enabled (via -O) some parts of the compiler
18994 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18995 represent source-level labels which were explicitly declared by
18996 the user. This really shouldn't be happening though, so catch
18997 it if it ever does happen. */
18998 gcc_assert (!INSN_DELETED_P (insn));
18999
19000 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19001 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19002 }
19003 else if (insn
19004 && NOTE_P (insn)
19005 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19006 && CODE_LABEL_NUMBER (insn) != -1)
19007 {
19008 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19009 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19010 }
19011 }
19012 }
19013
19014 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19015 attributes to the DIE for a block STMT, to describe where the inlined
19016 function was called from. This is similar to add_src_coords_attributes. */
19017
19018 static inline void
19019 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19020 {
19021 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19022
19023 if (dwarf_version >= 3 || !dwarf_strict)
19024 {
19025 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19026 add_AT_unsigned (die, DW_AT_call_line, s.line);
19027 }
19028 }
19029
19030
19031 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19032 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19033
19034 static inline void
19035 add_high_low_attributes (tree stmt, dw_die_ref die)
19036 {
19037 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19038
19039 if (BLOCK_FRAGMENT_CHAIN (stmt)
19040 && (dwarf_version >= 3 || !dwarf_strict))
19041 {
19042 tree chain, superblock = NULL_TREE;
19043 dw_die_ref pdie;
19044 dw_attr_ref attr = NULL;
19045
19046 if (inlined_function_outer_scope_p (stmt))
19047 {
19048 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19049 BLOCK_NUMBER (stmt));
19050 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19051 }
19052
19053 /* Optimize duplicate .debug_ranges lists or even tails of
19054 lists. If this BLOCK has same ranges as its supercontext,
19055 lookup DW_AT_ranges attribute in the supercontext (and
19056 recursively so), verify that the ranges_table contains the
19057 right values and use it instead of adding a new .debug_range. */
19058 for (chain = stmt, pdie = die;
19059 BLOCK_SAME_RANGE (chain);
19060 chain = BLOCK_SUPERCONTEXT (chain))
19061 {
19062 dw_attr_ref new_attr;
19063
19064 pdie = pdie->die_parent;
19065 if (pdie == NULL)
19066 break;
19067 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19068 break;
19069 new_attr = get_AT (pdie, DW_AT_ranges);
19070 if (new_attr == NULL
19071 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19072 break;
19073 attr = new_attr;
19074 superblock = BLOCK_SUPERCONTEXT (chain);
19075 }
19076 if (attr != NULL
19077 && (ranges_table[attr->dw_attr_val.v.val_offset
19078 / 2 / DWARF2_ADDR_SIZE].num
19079 == BLOCK_NUMBER (superblock))
19080 && BLOCK_FRAGMENT_CHAIN (superblock))
19081 {
19082 unsigned long off = attr->dw_attr_val.v.val_offset
19083 / 2 / DWARF2_ADDR_SIZE;
19084 unsigned long supercnt = 0, thiscnt = 0;
19085 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19086 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19087 {
19088 ++supercnt;
19089 gcc_checking_assert (ranges_table[off + supercnt].num
19090 == BLOCK_NUMBER (chain));
19091 }
19092 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19093 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19094 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19095 ++thiscnt;
19096 gcc_assert (supercnt >= thiscnt);
19097 add_AT_range_list (die, DW_AT_ranges,
19098 ((off + supercnt - thiscnt)
19099 * 2 * DWARF2_ADDR_SIZE),
19100 false);
19101 return;
19102 }
19103
19104 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19105
19106 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19107 do
19108 {
19109 add_ranges (chain);
19110 chain = BLOCK_FRAGMENT_CHAIN (chain);
19111 }
19112 while (chain);
19113 add_ranges (NULL);
19114 }
19115 else
19116 {
19117 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19118 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19119 BLOCK_NUMBER (stmt));
19120 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19121 BLOCK_NUMBER (stmt));
19122 add_AT_low_high_pc (die, label, label_high, false);
19123 }
19124 }
19125
19126 /* Generate a DIE for a lexical block. */
19127
19128 static void
19129 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19130 {
19131 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19132
19133 if (call_arg_locations)
19134 {
19135 if (block_map.length () <= BLOCK_NUMBER (stmt))
19136 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19137 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19138 }
19139
19140 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19141 add_high_low_attributes (stmt, stmt_die);
19142
19143 decls_for_scope (stmt, stmt_die, depth);
19144 }
19145
19146 /* Generate a DIE for an inlined subprogram. */
19147
19148 static void
19149 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19150 {
19151 tree decl;
19152
19153 /* The instance of function that is effectively being inlined shall not
19154 be abstract. */
19155 gcc_assert (! BLOCK_ABSTRACT (stmt));
19156
19157 decl = block_ultimate_origin (stmt);
19158
19159 /* Emit info for the abstract instance first, if we haven't yet. We
19160 must emit this even if the block is abstract, otherwise when we
19161 emit the block below (or elsewhere), we may end up trying to emit
19162 a die whose origin die hasn't been emitted, and crashing. */
19163 dwarf2out_abstract_function (decl);
19164
19165 if (! BLOCK_ABSTRACT (stmt))
19166 {
19167 dw_die_ref subr_die
19168 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19169
19170 if (call_arg_locations)
19171 {
19172 if (block_map.length () <= BLOCK_NUMBER (stmt))
19173 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19174 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19175 }
19176 add_abstract_origin_attribute (subr_die, decl);
19177 if (TREE_ASM_WRITTEN (stmt))
19178 add_high_low_attributes (stmt, subr_die);
19179 add_call_src_coords_attributes (stmt, subr_die);
19180
19181 decls_for_scope (stmt, subr_die, depth);
19182 current_function_has_inlines = 1;
19183 }
19184 }
19185
19186 /* Generate a DIE for a field in a record, or structure. */
19187
19188 static void
19189 gen_field_die (tree decl, dw_die_ref context_die)
19190 {
19191 dw_die_ref decl_die;
19192
19193 if (TREE_TYPE (decl) == error_mark_node)
19194 return;
19195
19196 decl_die = new_die (DW_TAG_member, context_die, decl);
19197 add_name_and_src_coords_attributes (decl_die, decl);
19198 add_type_attribute (decl_die, member_declared_type (decl),
19199 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19200 context_die);
19201
19202 if (DECL_BIT_FIELD_TYPE (decl))
19203 {
19204 add_byte_size_attribute (decl_die, decl);
19205 add_bit_size_attribute (decl_die, decl);
19206 add_bit_offset_attribute (decl_die, decl);
19207 }
19208
19209 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19210 add_data_member_location_attribute (decl_die, decl);
19211
19212 if (DECL_ARTIFICIAL (decl))
19213 add_AT_flag (decl_die, DW_AT_artificial, 1);
19214
19215 add_accessibility_attribute (decl_die, decl);
19216
19217 /* Equate decl number to die, so that we can look up this decl later on. */
19218 equate_decl_number_to_die (decl, decl_die);
19219 }
19220
19221 #if 0
19222 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19223 Use modified_type_die instead.
19224 We keep this code here just in case these types of DIEs may be needed to
19225 represent certain things in other languages (e.g. Pascal) someday. */
19226
19227 static void
19228 gen_pointer_type_die (tree type, dw_die_ref context_die)
19229 {
19230 dw_die_ref ptr_die
19231 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19232
19233 equate_type_number_to_die (type, ptr_die);
19234 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19235 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19236 }
19237
19238 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19239 Use modified_type_die instead.
19240 We keep this code here just in case these types of DIEs may be needed to
19241 represent certain things in other languages (e.g. Pascal) someday. */
19242
19243 static void
19244 gen_reference_type_die (tree type, dw_die_ref context_die)
19245 {
19246 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19247
19248 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19249 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19250 else
19251 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19252
19253 equate_type_number_to_die (type, ref_die);
19254 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19255 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19256 }
19257 #endif
19258
19259 /* Generate a DIE for a pointer to a member type. */
19260
19261 static void
19262 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19263 {
19264 dw_die_ref ptr_die
19265 = new_die (DW_TAG_ptr_to_member_type,
19266 scope_die_for (type, context_die), type);
19267
19268 equate_type_number_to_die (type, ptr_die);
19269 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19270 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19271 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19272 }
19273
19274 typedef const char *dchar_p; /* For DEF_VEC_P. */
19275
19276 static char *producer_string;
19277
19278 /* Return a heap allocated producer string including command line options
19279 if -grecord-gcc-switches. */
19280
19281 static char *
19282 gen_producer_string (void)
19283 {
19284 size_t j;
19285 auto_vec<dchar_p> switches;
19286 const char *language_string = lang_hooks.name;
19287 char *producer, *tail;
19288 const char *p;
19289 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19290 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19291
19292 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19293 switch (save_decoded_options[j].opt_index)
19294 {
19295 case OPT_o:
19296 case OPT_d:
19297 case OPT_dumpbase:
19298 case OPT_dumpdir:
19299 case OPT_auxbase:
19300 case OPT_auxbase_strip:
19301 case OPT_quiet:
19302 case OPT_version:
19303 case OPT_v:
19304 case OPT_w:
19305 case OPT_L:
19306 case OPT_D:
19307 case OPT_I:
19308 case OPT_U:
19309 case OPT_SPECIAL_unknown:
19310 case OPT_SPECIAL_ignore:
19311 case OPT_SPECIAL_program_name:
19312 case OPT_SPECIAL_input_file:
19313 case OPT_grecord_gcc_switches:
19314 case OPT_gno_record_gcc_switches:
19315 case OPT__output_pch_:
19316 case OPT_fdiagnostics_show_location_:
19317 case OPT_fdiagnostics_show_option:
19318 case OPT_fdiagnostics_show_caret:
19319 case OPT_fdiagnostics_color_:
19320 case OPT_fverbose_asm:
19321 case OPT____:
19322 case OPT__sysroot_:
19323 case OPT_nostdinc:
19324 case OPT_nostdinc__:
19325 /* Ignore these. */
19326 continue;
19327 default:
19328 if (cl_options[save_decoded_options[j].opt_index].flags
19329 & CL_NO_DWARF_RECORD)
19330 continue;
19331 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19332 == '-');
19333 switch (save_decoded_options[j].canonical_option[0][1])
19334 {
19335 case 'M':
19336 case 'i':
19337 case 'W':
19338 continue;
19339 case 'f':
19340 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19341 "dump", 4) == 0)
19342 continue;
19343 break;
19344 default:
19345 break;
19346 }
19347 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19348 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19349 break;
19350 }
19351
19352 producer = XNEWVEC (char, plen + 1 + len + 1);
19353 tail = producer;
19354 sprintf (tail, "%s %s", language_string, version_string);
19355 tail += plen;
19356
19357 FOR_EACH_VEC_ELT (switches, j, p)
19358 {
19359 len = strlen (p);
19360 *tail = ' ';
19361 memcpy (tail + 1, p, len);
19362 tail += len + 1;
19363 }
19364
19365 *tail = '\0';
19366 return producer;
19367 }
19368
19369 /* Generate the DIE for the compilation unit. */
19370
19371 static dw_die_ref
19372 gen_compile_unit_die (const char *filename)
19373 {
19374 dw_die_ref die;
19375 const char *language_string = lang_hooks.name;
19376 int language;
19377
19378 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19379
19380 if (filename)
19381 {
19382 add_name_attribute (die, filename);
19383 /* Don't add cwd for <built-in>. */
19384 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19385 add_comp_dir_attribute (die);
19386 }
19387
19388 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19389
19390 /* If our producer is LTO try to figure out a common language to use
19391 from the global list of translation units. */
19392 if (strcmp (language_string, "GNU GIMPLE") == 0)
19393 {
19394 unsigned i;
19395 tree t;
19396 const char *common_lang = NULL;
19397
19398 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19399 {
19400 if (!TRANSLATION_UNIT_LANGUAGE (t))
19401 continue;
19402 if (!common_lang)
19403 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19404 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19405 ;
19406 else if (strncmp (common_lang, "GNU C", 5) == 0
19407 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19408 /* Mixing C and C++ is ok, use C++ in that case. */
19409 common_lang = "GNU C++";
19410 else
19411 {
19412 /* Fall back to C. */
19413 common_lang = NULL;
19414 break;
19415 }
19416 }
19417
19418 if (common_lang)
19419 language_string = common_lang;
19420 }
19421
19422 language = DW_LANG_C89;
19423 if (strcmp (language_string, "GNU C++") == 0)
19424 language = DW_LANG_C_plus_plus;
19425 else if (strcmp (language_string, "GNU F77") == 0)
19426 language = DW_LANG_Fortran77;
19427 else if (strcmp (language_string, "GNU Pascal") == 0)
19428 language = DW_LANG_Pascal83;
19429 else if (dwarf_version >= 3 || !dwarf_strict)
19430 {
19431 if (strcmp (language_string, "GNU Ada") == 0)
19432 language = DW_LANG_Ada95;
19433 else if (strcmp (language_string, "GNU Fortran") == 0)
19434 language = DW_LANG_Fortran95;
19435 else if (strcmp (language_string, "GNU Java") == 0)
19436 language = DW_LANG_Java;
19437 else if (strcmp (language_string, "GNU Objective-C") == 0)
19438 language = DW_LANG_ObjC;
19439 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19440 language = DW_LANG_ObjC_plus_plus;
19441 else if (dwarf_version >= 5 || !dwarf_strict)
19442 {
19443 if (strcmp (language_string, "GNU Go") == 0)
19444 language = DW_LANG_Go;
19445 }
19446 }
19447 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19448 else if (strcmp (language_string, "GNU Fortran") == 0)
19449 language = DW_LANG_Fortran90;
19450
19451 add_AT_unsigned (die, DW_AT_language, language);
19452
19453 switch (language)
19454 {
19455 case DW_LANG_Fortran77:
19456 case DW_LANG_Fortran90:
19457 case DW_LANG_Fortran95:
19458 /* Fortran has case insensitive identifiers and the front-end
19459 lowercases everything. */
19460 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19461 break;
19462 default:
19463 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19464 break;
19465 }
19466 return die;
19467 }
19468
19469 /* Generate the DIE for a base class. */
19470
19471 static void
19472 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19473 {
19474 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19475
19476 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19477 add_data_member_location_attribute (die, binfo);
19478
19479 if (BINFO_VIRTUAL_P (binfo))
19480 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19481
19482 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19483 children, otherwise the default is DW_ACCESS_public. In DWARF2
19484 the default has always been DW_ACCESS_private. */
19485 if (access == access_public_node)
19486 {
19487 if (dwarf_version == 2
19488 || context_die->die_tag == DW_TAG_class_type)
19489 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19490 }
19491 else if (access == access_protected_node)
19492 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19493 else if (dwarf_version > 2
19494 && context_die->die_tag != DW_TAG_class_type)
19495 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19496 }
19497
19498 /* Generate a DIE for a class member. */
19499
19500 static void
19501 gen_member_die (tree type, dw_die_ref context_die)
19502 {
19503 tree member;
19504 tree binfo = TYPE_BINFO (type);
19505 dw_die_ref child;
19506
19507 /* If this is not an incomplete type, output descriptions of each of its
19508 members. Note that as we output the DIEs necessary to represent the
19509 members of this record or union type, we will also be trying to output
19510 DIEs to represent the *types* of those members. However the `type'
19511 function (above) will specifically avoid generating type DIEs for member
19512 types *within* the list of member DIEs for this (containing) type except
19513 for those types (of members) which are explicitly marked as also being
19514 members of this (containing) type themselves. The g++ front- end can
19515 force any given type to be treated as a member of some other (containing)
19516 type by setting the TYPE_CONTEXT of the given (member) type to point to
19517 the TREE node representing the appropriate (containing) type. */
19518
19519 /* First output info about the base classes. */
19520 if (binfo)
19521 {
19522 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19523 int i;
19524 tree base;
19525
19526 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19527 gen_inheritance_die (base,
19528 (accesses ? (*accesses)[i] : access_public_node),
19529 context_die);
19530 }
19531
19532 /* Now output info about the data members and type members. */
19533 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19534 {
19535 /* If we thought we were generating minimal debug info for TYPE
19536 and then changed our minds, some of the member declarations
19537 may have already been defined. Don't define them again, but
19538 do put them in the right order. */
19539
19540 child = lookup_decl_die (member);
19541 if (child)
19542 splice_child_die (context_die, child);
19543 else
19544 gen_decl_die (member, NULL, context_die);
19545 }
19546
19547 /* Now output info about the function members (if any). */
19548 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19549 {
19550 /* Don't include clones in the member list. */
19551 if (DECL_ABSTRACT_ORIGIN (member))
19552 continue;
19553
19554 child = lookup_decl_die (member);
19555 if (child)
19556 splice_child_die (context_die, child);
19557 else
19558 gen_decl_die (member, NULL, context_die);
19559 }
19560 }
19561
19562 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19563 is set, we pretend that the type was never defined, so we only get the
19564 member DIEs needed by later specification DIEs. */
19565
19566 static void
19567 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19568 enum debug_info_usage usage)
19569 {
19570 dw_die_ref type_die = lookup_type_die (type);
19571 dw_die_ref scope_die = 0;
19572 int nested = 0;
19573 int complete = (TYPE_SIZE (type)
19574 && (! TYPE_STUB_DECL (type)
19575 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19576 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19577 complete = complete && should_emit_struct_debug (type, usage);
19578
19579 if (type_die && ! complete)
19580 return;
19581
19582 if (TYPE_CONTEXT (type) != NULL_TREE
19583 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19584 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19585 nested = 1;
19586
19587 scope_die = scope_die_for (type, context_die);
19588
19589 /* Generate child dies for template paramaters. */
19590 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19591 schedule_generic_params_dies_gen (type);
19592
19593 if (! type_die || (nested && is_cu_die (scope_die)))
19594 /* First occurrence of type or toplevel definition of nested class. */
19595 {
19596 dw_die_ref old_die = type_die;
19597
19598 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19599 ? record_type_tag (type) : DW_TAG_union_type,
19600 scope_die, type);
19601 equate_type_number_to_die (type, type_die);
19602 if (old_die)
19603 add_AT_specification (type_die, old_die);
19604 else
19605 add_name_attribute (type_die, type_tag (type));
19606 }
19607 else
19608 remove_AT (type_die, DW_AT_declaration);
19609
19610 /* If this type has been completed, then give it a byte_size attribute and
19611 then give a list of members. */
19612 if (complete && !ns_decl)
19613 {
19614 /* Prevent infinite recursion in cases where the type of some member of
19615 this type is expressed in terms of this type itself. */
19616 TREE_ASM_WRITTEN (type) = 1;
19617 add_byte_size_attribute (type_die, type);
19618 if (TYPE_STUB_DECL (type) != NULL_TREE)
19619 {
19620 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19621 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19622 }
19623
19624 /* If the first reference to this type was as the return type of an
19625 inline function, then it may not have a parent. Fix this now. */
19626 if (type_die->die_parent == NULL)
19627 add_child_die (scope_die, type_die);
19628
19629 push_decl_scope (type);
19630 gen_member_die (type, type_die);
19631 pop_decl_scope ();
19632
19633 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19634 if (TYPE_ARTIFICIAL (type))
19635 add_AT_flag (type_die, DW_AT_artificial, 1);
19636
19637 /* GNU extension: Record what type our vtable lives in. */
19638 if (TYPE_VFIELD (type))
19639 {
19640 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19641
19642 gen_type_die (vtype, context_die);
19643 add_AT_die_ref (type_die, DW_AT_containing_type,
19644 lookup_type_die (vtype));
19645 }
19646 }
19647 else
19648 {
19649 add_AT_flag (type_die, DW_AT_declaration, 1);
19650
19651 /* We don't need to do this for function-local types. */
19652 if (TYPE_STUB_DECL (type)
19653 && ! decl_function_context (TYPE_STUB_DECL (type)))
19654 vec_safe_push (incomplete_types, type);
19655 }
19656
19657 if (get_AT (type_die, DW_AT_name))
19658 add_pubtype (type, type_die);
19659 }
19660
19661 /* Generate a DIE for a subroutine _type_. */
19662
19663 static void
19664 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19665 {
19666 tree return_type = TREE_TYPE (type);
19667 dw_die_ref subr_die
19668 = new_die (DW_TAG_subroutine_type,
19669 scope_die_for (type, context_die), type);
19670
19671 equate_type_number_to_die (type, subr_die);
19672 add_prototyped_attribute (subr_die, type);
19673 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19674 gen_formal_types_die (type, subr_die);
19675
19676 if (get_AT (subr_die, DW_AT_name))
19677 add_pubtype (type, subr_die);
19678 }
19679
19680 /* Generate a DIE for a type definition. */
19681
19682 static void
19683 gen_typedef_die (tree decl, dw_die_ref context_die)
19684 {
19685 dw_die_ref type_die;
19686 tree origin;
19687
19688 if (TREE_ASM_WRITTEN (decl))
19689 return;
19690
19691 TREE_ASM_WRITTEN (decl) = 1;
19692 type_die = new_die (DW_TAG_typedef, context_die, decl);
19693 origin = decl_ultimate_origin (decl);
19694 if (origin != NULL)
19695 add_abstract_origin_attribute (type_die, origin);
19696 else
19697 {
19698 tree type;
19699
19700 add_name_and_src_coords_attributes (type_die, decl);
19701 if (DECL_ORIGINAL_TYPE (decl))
19702 {
19703 type = DECL_ORIGINAL_TYPE (decl);
19704
19705 gcc_assert (type != TREE_TYPE (decl));
19706 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19707 }
19708 else
19709 {
19710 type = TREE_TYPE (decl);
19711
19712 if (is_naming_typedef_decl (TYPE_NAME (type)))
19713 {
19714 /* Here, we are in the case of decl being a typedef naming
19715 an anonymous type, e.g:
19716 typedef struct {...} foo;
19717 In that case TREE_TYPE (decl) is not a typedef variant
19718 type and TYPE_NAME of the anonymous type is set to the
19719 TYPE_DECL of the typedef. This construct is emitted by
19720 the C++ FE.
19721
19722 TYPE is the anonymous struct named by the typedef
19723 DECL. As we need the DW_AT_type attribute of the
19724 DW_TAG_typedef to point to the DIE of TYPE, let's
19725 generate that DIE right away. add_type_attribute
19726 called below will then pick (via lookup_type_die) that
19727 anonymous struct DIE. */
19728 if (!TREE_ASM_WRITTEN (type))
19729 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19730
19731 /* This is a GNU Extension. We are adding a
19732 DW_AT_linkage_name attribute to the DIE of the
19733 anonymous struct TYPE. The value of that attribute
19734 is the name of the typedef decl naming the anonymous
19735 struct. This greatly eases the work of consumers of
19736 this debug info. */
19737 add_linkage_attr (lookup_type_die (type), decl);
19738 }
19739 }
19740
19741 add_type_attribute (type_die, type, TREE_READONLY (decl),
19742 TREE_THIS_VOLATILE (decl), context_die);
19743
19744 if (is_naming_typedef_decl (decl))
19745 /* We want that all subsequent calls to lookup_type_die with
19746 TYPE in argument yield the DW_TAG_typedef we have just
19747 created. */
19748 equate_type_number_to_die (type, type_die);
19749
19750 add_accessibility_attribute (type_die, decl);
19751 }
19752
19753 if (DECL_ABSTRACT (decl))
19754 equate_decl_number_to_die (decl, type_die);
19755
19756 if (get_AT (type_die, DW_AT_name))
19757 add_pubtype (decl, type_die);
19758 }
19759
19760 /* Generate a DIE for a struct, class, enum or union type. */
19761
19762 static void
19763 gen_tagged_type_die (tree type,
19764 dw_die_ref context_die,
19765 enum debug_info_usage usage)
19766 {
19767 int need_pop;
19768
19769 if (type == NULL_TREE
19770 || !is_tagged_type (type))
19771 return;
19772
19773 /* If this is a nested type whose containing class hasn't been written
19774 out yet, writing it out will cover this one, too. This does not apply
19775 to instantiations of member class templates; they need to be added to
19776 the containing class as they are generated. FIXME: This hurts the
19777 idea of combining type decls from multiple TUs, since we can't predict
19778 what set of template instantiations we'll get. */
19779 if (TYPE_CONTEXT (type)
19780 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19781 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19782 {
19783 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19784
19785 if (TREE_ASM_WRITTEN (type))
19786 return;
19787
19788 /* If that failed, attach ourselves to the stub. */
19789 push_decl_scope (TYPE_CONTEXT (type));
19790 context_die = lookup_type_die (TYPE_CONTEXT (type));
19791 need_pop = 1;
19792 }
19793 else if (TYPE_CONTEXT (type) != NULL_TREE
19794 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19795 {
19796 /* If this type is local to a function that hasn't been written
19797 out yet, use a NULL context for now; it will be fixed up in
19798 decls_for_scope. */
19799 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19800 /* A declaration DIE doesn't count; nested types need to go in the
19801 specification. */
19802 if (context_die && is_declaration_die (context_die))
19803 context_die = NULL;
19804 need_pop = 0;
19805 }
19806 else
19807 {
19808 context_die = declare_in_namespace (type, context_die);
19809 need_pop = 0;
19810 }
19811
19812 if (TREE_CODE (type) == ENUMERAL_TYPE)
19813 {
19814 /* This might have been written out by the call to
19815 declare_in_namespace. */
19816 if (!TREE_ASM_WRITTEN (type))
19817 gen_enumeration_type_die (type, context_die);
19818 }
19819 else
19820 gen_struct_or_union_type_die (type, context_die, usage);
19821
19822 if (need_pop)
19823 pop_decl_scope ();
19824
19825 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19826 it up if it is ever completed. gen_*_type_die will set it for us
19827 when appropriate. */
19828 }
19829
19830 /* Generate a type description DIE. */
19831
19832 static void
19833 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19834 enum debug_info_usage usage)
19835 {
19836 struct array_descr_info info;
19837
19838 if (type == NULL_TREE || type == error_mark_node)
19839 return;
19840
19841 if (TYPE_NAME (type) != NULL_TREE
19842 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19843 && is_redundant_typedef (TYPE_NAME (type))
19844 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19845 /* The DECL of this type is a typedef we don't want to emit debug
19846 info for but we want debug info for its underlying typedef.
19847 This can happen for e.g, the injected-class-name of a C++
19848 type. */
19849 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19850
19851 /* If TYPE is a typedef type variant, let's generate debug info
19852 for the parent typedef which TYPE is a type of. */
19853 if (typedef_variant_p (type))
19854 {
19855 if (TREE_ASM_WRITTEN (type))
19856 return;
19857
19858 /* Prevent broken recursion; we can't hand off to the same type. */
19859 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19860
19861 /* Give typedefs the right scope. */
19862 context_die = scope_die_for (type, context_die);
19863
19864 TREE_ASM_WRITTEN (type) = 1;
19865
19866 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19867 return;
19868 }
19869
19870 /* If type is an anonymous tagged type named by a typedef, let's
19871 generate debug info for the typedef. */
19872 if (is_naming_typedef_decl (TYPE_NAME (type)))
19873 {
19874 /* Use the DIE of the containing namespace as the parent DIE of
19875 the type description DIE we want to generate. */
19876 if (DECL_CONTEXT (TYPE_NAME (type))
19877 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19878 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19879
19880 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19881 return;
19882 }
19883
19884 /* If this is an array type with hidden descriptor, handle it first. */
19885 if (!TREE_ASM_WRITTEN (type)
19886 && lang_hooks.types.get_array_descr_info
19887 && lang_hooks.types.get_array_descr_info (type, &info)
19888 && (dwarf_version >= 3 || !dwarf_strict))
19889 {
19890 gen_descr_array_type_die (type, &info, context_die);
19891 TREE_ASM_WRITTEN (type) = 1;
19892 return;
19893 }
19894
19895 /* We are going to output a DIE to represent the unqualified version
19896 of this type (i.e. without any const or volatile qualifiers) so
19897 get the main variant (i.e. the unqualified version) of this type
19898 now. (Vectors are special because the debugging info is in the
19899 cloned type itself). */
19900 if (TREE_CODE (type) != VECTOR_TYPE)
19901 type = type_main_variant (type);
19902
19903 if (TREE_ASM_WRITTEN (type))
19904 return;
19905
19906 switch (TREE_CODE (type))
19907 {
19908 case ERROR_MARK:
19909 break;
19910
19911 case POINTER_TYPE:
19912 case REFERENCE_TYPE:
19913 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19914 ensures that the gen_type_die recursion will terminate even if the
19915 type is recursive. Recursive types are possible in Ada. */
19916 /* ??? We could perhaps do this for all types before the switch
19917 statement. */
19918 TREE_ASM_WRITTEN (type) = 1;
19919
19920 /* For these types, all that is required is that we output a DIE (or a
19921 set of DIEs) to represent the "basis" type. */
19922 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19923 DINFO_USAGE_IND_USE);
19924 break;
19925
19926 case OFFSET_TYPE:
19927 /* This code is used for C++ pointer-to-data-member types.
19928 Output a description of the relevant class type. */
19929 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19930 DINFO_USAGE_IND_USE);
19931
19932 /* Output a description of the type of the object pointed to. */
19933 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19934 DINFO_USAGE_IND_USE);
19935
19936 /* Now output a DIE to represent this pointer-to-data-member type
19937 itself. */
19938 gen_ptr_to_mbr_type_die (type, context_die);
19939 break;
19940
19941 case FUNCTION_TYPE:
19942 /* Force out return type (in case it wasn't forced out already). */
19943 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19944 DINFO_USAGE_DIR_USE);
19945 gen_subroutine_type_die (type, context_die);
19946 break;
19947
19948 case METHOD_TYPE:
19949 /* Force out return type (in case it wasn't forced out already). */
19950 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19951 DINFO_USAGE_DIR_USE);
19952 gen_subroutine_type_die (type, context_die);
19953 break;
19954
19955 case ARRAY_TYPE:
19956 gen_array_type_die (type, context_die);
19957 break;
19958
19959 case VECTOR_TYPE:
19960 gen_array_type_die (type, context_die);
19961 break;
19962
19963 case ENUMERAL_TYPE:
19964 case RECORD_TYPE:
19965 case UNION_TYPE:
19966 case QUAL_UNION_TYPE:
19967 gen_tagged_type_die (type, context_die, usage);
19968 return;
19969
19970 case VOID_TYPE:
19971 case INTEGER_TYPE:
19972 case REAL_TYPE:
19973 case FIXED_POINT_TYPE:
19974 case COMPLEX_TYPE:
19975 case BOOLEAN_TYPE:
19976 case POINTER_BOUNDS_TYPE:
19977 /* No DIEs needed for fundamental types. */
19978 break;
19979
19980 case NULLPTR_TYPE:
19981 case LANG_TYPE:
19982 /* Just use DW_TAG_unspecified_type. */
19983 {
19984 dw_die_ref type_die = lookup_type_die (type);
19985 if (type_die == NULL)
19986 {
19987 tree name = TYPE_NAME (type);
19988 if (TREE_CODE (name) == TYPE_DECL)
19989 name = DECL_NAME (name);
19990 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19991 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19992 equate_type_number_to_die (type, type_die);
19993 }
19994 }
19995 break;
19996
19997 default:
19998 gcc_unreachable ();
19999 }
20000
20001 TREE_ASM_WRITTEN (type) = 1;
20002 }
20003
20004 static void
20005 gen_type_die (tree type, dw_die_ref context_die)
20006 {
20007 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20008 }
20009
20010 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20011 things which are local to the given block. */
20012
20013 static void
20014 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20015 {
20016 int must_output_die = 0;
20017 bool inlined_func;
20018
20019 /* Ignore blocks that are NULL. */
20020 if (stmt == NULL_TREE)
20021 return;
20022
20023 inlined_func = inlined_function_outer_scope_p (stmt);
20024
20025 /* If the block is one fragment of a non-contiguous block, do not
20026 process the variables, since they will have been done by the
20027 origin block. Do process subblocks. */
20028 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20029 {
20030 tree sub;
20031
20032 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20033 gen_block_die (sub, context_die, depth + 1);
20034
20035 return;
20036 }
20037
20038 /* Determine if we need to output any Dwarf DIEs at all to represent this
20039 block. */
20040 if (inlined_func)
20041 /* The outer scopes for inlinings *must* always be represented. We
20042 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20043 must_output_die = 1;
20044 else
20045 {
20046 /* Determine if this block directly contains any "significant"
20047 local declarations which we will need to output DIEs for. */
20048 if (debug_info_level > DINFO_LEVEL_TERSE)
20049 /* We are not in terse mode so *any* local declaration counts
20050 as being a "significant" one. */
20051 must_output_die = ((BLOCK_VARS (stmt) != NULL
20052 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20053 && (TREE_USED (stmt)
20054 || TREE_ASM_WRITTEN (stmt)
20055 || BLOCK_ABSTRACT (stmt)));
20056 else if ((TREE_USED (stmt)
20057 || TREE_ASM_WRITTEN (stmt)
20058 || BLOCK_ABSTRACT (stmt))
20059 && !dwarf2out_ignore_block (stmt))
20060 must_output_die = 1;
20061 }
20062
20063 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20064 DIE for any block which contains no significant local declarations at
20065 all. Rather, in such cases we just call `decls_for_scope' so that any
20066 needed Dwarf info for any sub-blocks will get properly generated. Note
20067 that in terse mode, our definition of what constitutes a "significant"
20068 local declaration gets restricted to include only inlined function
20069 instances and local (nested) function definitions. */
20070 if (must_output_die)
20071 {
20072 if (inlined_func)
20073 {
20074 /* If STMT block is abstract, that means we have been called
20075 indirectly from dwarf2out_abstract_function.
20076 That function rightfully marks the descendent blocks (of
20077 the abstract function it is dealing with) as being abstract,
20078 precisely to prevent us from emitting any
20079 DW_TAG_inlined_subroutine DIE as a descendent
20080 of an abstract function instance. So in that case, we should
20081 not call gen_inlined_subroutine_die.
20082
20083 Later though, when cgraph asks dwarf2out to emit info
20084 for the concrete instance of the function decl into which
20085 the concrete instance of STMT got inlined, the later will lead
20086 to the generation of a DW_TAG_inlined_subroutine DIE. */
20087 if (! BLOCK_ABSTRACT (stmt))
20088 gen_inlined_subroutine_die (stmt, context_die, depth);
20089 }
20090 else
20091 gen_lexical_block_die (stmt, context_die, depth);
20092 }
20093 else
20094 decls_for_scope (stmt, context_die, depth);
20095 }
20096
20097 /* Process variable DECL (or variable with origin ORIGIN) within
20098 block STMT and add it to CONTEXT_DIE. */
20099 static void
20100 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20101 {
20102 dw_die_ref die;
20103 tree decl_or_origin = decl ? decl : origin;
20104
20105 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20106 die = lookup_decl_die (decl_or_origin);
20107 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20108 && TYPE_DECL_IS_STUB (decl_or_origin))
20109 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20110 else
20111 die = NULL;
20112
20113 if (die != NULL && die->die_parent == NULL)
20114 add_child_die (context_die, die);
20115 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20116 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20117 stmt, context_die);
20118 else
20119 gen_decl_die (decl, origin, context_die);
20120 }
20121
20122 /* Generate all of the decls declared within a given scope and (recursively)
20123 all of its sub-blocks. */
20124
20125 static void
20126 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20127 {
20128 tree decl;
20129 unsigned int i;
20130 tree subblocks;
20131
20132 /* Ignore NULL blocks. */
20133 if (stmt == NULL_TREE)
20134 return;
20135
20136 /* Output the DIEs to represent all of the data objects and typedefs
20137 declared directly within this block but not within any nested
20138 sub-blocks. Also, nested function and tag DIEs have been
20139 generated with a parent of NULL; fix that up now. We don't
20140 have to do this if we're at -g1. */
20141 if (debug_info_level > DINFO_LEVEL_TERSE)
20142 {
20143 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20144 process_scope_var (stmt, decl, NULL_TREE, context_die);
20145 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20146 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20147 context_die);
20148 }
20149
20150 /* Even if we're at -g1, we need to process the subblocks in order to get
20151 inlined call information. */
20152
20153 /* Output the DIEs to represent all sub-blocks (and the items declared
20154 therein) of this block. */
20155 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20156 subblocks != NULL;
20157 subblocks = BLOCK_CHAIN (subblocks))
20158 gen_block_die (subblocks, context_die, depth + 1);
20159 }
20160
20161 /* Is this a typedef we can avoid emitting? */
20162
20163 static inline int
20164 is_redundant_typedef (const_tree decl)
20165 {
20166 if (TYPE_DECL_IS_STUB (decl))
20167 return 1;
20168
20169 if (DECL_ARTIFICIAL (decl)
20170 && DECL_CONTEXT (decl)
20171 && is_tagged_type (DECL_CONTEXT (decl))
20172 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20173 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20174 /* Also ignore the artificial member typedef for the class name. */
20175 return 1;
20176
20177 return 0;
20178 }
20179
20180 /* Return TRUE if TYPE is a typedef that names a type for linkage
20181 purposes. This kind of typedefs is produced by the C++ FE for
20182 constructs like:
20183
20184 typedef struct {...} foo;
20185
20186 In that case, there is no typedef variant type produced for foo.
20187 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20188 struct type. */
20189
20190 static bool
20191 is_naming_typedef_decl (const_tree decl)
20192 {
20193 if (decl == NULL_TREE
20194 || TREE_CODE (decl) != TYPE_DECL
20195 || !is_tagged_type (TREE_TYPE (decl))
20196 || DECL_IS_BUILTIN (decl)
20197 || is_redundant_typedef (decl)
20198 /* It looks like Ada produces TYPE_DECLs that are very similar
20199 to C++ naming typedefs but that have different
20200 semantics. Let's be specific to c++ for now. */
20201 || !is_cxx ())
20202 return FALSE;
20203
20204 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20205 && TYPE_NAME (TREE_TYPE (decl)) == decl
20206 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20207 != TYPE_NAME (TREE_TYPE (decl))));
20208 }
20209
20210 /* Returns the DIE for a context. */
20211
20212 static inline dw_die_ref
20213 get_context_die (tree context)
20214 {
20215 if (context)
20216 {
20217 /* Find die that represents this context. */
20218 if (TYPE_P (context))
20219 {
20220 context = TYPE_MAIN_VARIANT (context);
20221 return strip_naming_typedef (context, force_type_die (context));
20222 }
20223 else
20224 return force_decl_die (context);
20225 }
20226 return comp_unit_die ();
20227 }
20228
20229 /* Returns the DIE for decl. A DIE will always be returned. */
20230
20231 static dw_die_ref
20232 force_decl_die (tree decl)
20233 {
20234 dw_die_ref decl_die;
20235 unsigned saved_external_flag;
20236 tree save_fn = NULL_TREE;
20237 decl_die = lookup_decl_die (decl);
20238 if (!decl_die)
20239 {
20240 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20241
20242 decl_die = lookup_decl_die (decl);
20243 if (decl_die)
20244 return decl_die;
20245
20246 switch (TREE_CODE (decl))
20247 {
20248 case FUNCTION_DECL:
20249 /* Clear current_function_decl, so that gen_subprogram_die thinks
20250 that this is a declaration. At this point, we just want to force
20251 declaration die. */
20252 save_fn = current_function_decl;
20253 current_function_decl = NULL_TREE;
20254 gen_subprogram_die (decl, context_die);
20255 current_function_decl = save_fn;
20256 break;
20257
20258 case VAR_DECL:
20259 /* Set external flag to force declaration die. Restore it after
20260 gen_decl_die() call. */
20261 saved_external_flag = DECL_EXTERNAL (decl);
20262 DECL_EXTERNAL (decl) = 1;
20263 gen_decl_die (decl, NULL, context_die);
20264 DECL_EXTERNAL (decl) = saved_external_flag;
20265 break;
20266
20267 case NAMESPACE_DECL:
20268 if (dwarf_version >= 3 || !dwarf_strict)
20269 dwarf2out_decl (decl);
20270 else
20271 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20272 decl_die = comp_unit_die ();
20273 break;
20274
20275 case TRANSLATION_UNIT_DECL:
20276 decl_die = comp_unit_die ();
20277 break;
20278
20279 default:
20280 gcc_unreachable ();
20281 }
20282
20283 /* We should be able to find the DIE now. */
20284 if (!decl_die)
20285 decl_die = lookup_decl_die (decl);
20286 gcc_assert (decl_die);
20287 }
20288
20289 return decl_die;
20290 }
20291
20292 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20293 always returned. */
20294
20295 static dw_die_ref
20296 force_type_die (tree type)
20297 {
20298 dw_die_ref type_die;
20299
20300 type_die = lookup_type_die (type);
20301 if (!type_die)
20302 {
20303 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20304
20305 type_die = modified_type_die (type, TYPE_READONLY (type),
20306 TYPE_VOLATILE (type), context_die);
20307 gcc_assert (type_die);
20308 }
20309 return type_die;
20310 }
20311
20312 /* Force out any required namespaces to be able to output DECL,
20313 and return the new context_die for it, if it's changed. */
20314
20315 static dw_die_ref
20316 setup_namespace_context (tree thing, dw_die_ref context_die)
20317 {
20318 tree context = (DECL_P (thing)
20319 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20320 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20321 /* Force out the namespace. */
20322 context_die = force_decl_die (context);
20323
20324 return context_die;
20325 }
20326
20327 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20328 type) within its namespace, if appropriate.
20329
20330 For compatibility with older debuggers, namespace DIEs only contain
20331 declarations; all definitions are emitted at CU scope. */
20332
20333 static dw_die_ref
20334 declare_in_namespace (tree thing, dw_die_ref context_die)
20335 {
20336 dw_die_ref ns_context;
20337
20338 if (debug_info_level <= DINFO_LEVEL_TERSE)
20339 return context_die;
20340
20341 /* If this decl is from an inlined function, then don't try to emit it in its
20342 namespace, as we will get confused. It would have already been emitted
20343 when the abstract instance of the inline function was emitted anyways. */
20344 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20345 return context_die;
20346
20347 ns_context = setup_namespace_context (thing, context_die);
20348
20349 if (ns_context != context_die)
20350 {
20351 if (is_fortran ())
20352 return ns_context;
20353 if (DECL_P (thing))
20354 gen_decl_die (thing, NULL, ns_context);
20355 else
20356 gen_type_die (thing, ns_context);
20357 }
20358 return context_die;
20359 }
20360
20361 /* Generate a DIE for a namespace or namespace alias. */
20362
20363 static void
20364 gen_namespace_die (tree decl, dw_die_ref context_die)
20365 {
20366 dw_die_ref namespace_die;
20367
20368 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20369 they are an alias of. */
20370 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20371 {
20372 /* Output a real namespace or module. */
20373 context_die = setup_namespace_context (decl, comp_unit_die ());
20374 namespace_die = new_die (is_fortran ()
20375 ? DW_TAG_module : DW_TAG_namespace,
20376 context_die, decl);
20377 /* For Fortran modules defined in different CU don't add src coords. */
20378 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20379 {
20380 const char *name = dwarf2_name (decl, 0);
20381 if (name)
20382 add_name_attribute (namespace_die, name);
20383 }
20384 else
20385 add_name_and_src_coords_attributes (namespace_die, decl);
20386 if (DECL_EXTERNAL (decl))
20387 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20388 equate_decl_number_to_die (decl, namespace_die);
20389 }
20390 else
20391 {
20392 /* Output a namespace alias. */
20393
20394 /* Force out the namespace we are an alias of, if necessary. */
20395 dw_die_ref origin_die
20396 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20397
20398 if (DECL_FILE_SCOPE_P (decl)
20399 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20400 context_die = setup_namespace_context (decl, comp_unit_die ());
20401 /* Now create the namespace alias DIE. */
20402 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20403 add_name_and_src_coords_attributes (namespace_die, decl);
20404 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20405 equate_decl_number_to_die (decl, namespace_die);
20406 }
20407 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20408 if (want_pubnames ())
20409 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20410 }
20411
20412 /* Generate Dwarf debug information for a decl described by DECL.
20413 The return value is currently only meaningful for PARM_DECLs,
20414 for all other decls it returns NULL. */
20415
20416 static dw_die_ref
20417 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20418 {
20419 tree decl_or_origin = decl ? decl : origin;
20420 tree class_origin = NULL, ultimate_origin;
20421
20422 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20423 return NULL;
20424
20425 switch (TREE_CODE (decl_or_origin))
20426 {
20427 case ERROR_MARK:
20428 break;
20429
20430 case CONST_DECL:
20431 if (!is_fortran () && !is_ada ())
20432 {
20433 /* The individual enumerators of an enum type get output when we output
20434 the Dwarf representation of the relevant enum type itself. */
20435 break;
20436 }
20437
20438 /* Emit its type. */
20439 gen_type_die (TREE_TYPE (decl), context_die);
20440
20441 /* And its containing namespace. */
20442 context_die = declare_in_namespace (decl, context_die);
20443
20444 gen_const_die (decl, context_die);
20445 break;
20446
20447 case FUNCTION_DECL:
20448 /* Don't output any DIEs to represent mere function declarations,
20449 unless they are class members or explicit block externs. */
20450 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20451 && DECL_FILE_SCOPE_P (decl_or_origin)
20452 && (current_function_decl == NULL_TREE
20453 || DECL_ARTIFICIAL (decl_or_origin)))
20454 break;
20455
20456 #if 0
20457 /* FIXME */
20458 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20459 on local redeclarations of global functions. That seems broken. */
20460 if (current_function_decl != decl)
20461 /* This is only a declaration. */;
20462 #endif
20463
20464 /* If we're emitting a clone, emit info for the abstract instance. */
20465 if (origin || DECL_ORIGIN (decl) != decl)
20466 dwarf2out_abstract_function (origin
20467 ? DECL_ORIGIN (origin)
20468 : DECL_ABSTRACT_ORIGIN (decl));
20469
20470 /* If we're emitting an out-of-line copy of an inline function,
20471 emit info for the abstract instance and set up to refer to it. */
20472 else if (cgraph_function_possibly_inlined_p (decl)
20473 && ! DECL_ABSTRACT (decl)
20474 && ! class_or_namespace_scope_p (context_die)
20475 /* dwarf2out_abstract_function won't emit a die if this is just
20476 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20477 that case, because that works only if we have a die. */
20478 && DECL_INITIAL (decl) != NULL_TREE)
20479 {
20480 dwarf2out_abstract_function (decl);
20481 set_decl_origin_self (decl);
20482 }
20483
20484 /* Otherwise we're emitting the primary DIE for this decl. */
20485 else if (debug_info_level > DINFO_LEVEL_TERSE)
20486 {
20487 /* Before we describe the FUNCTION_DECL itself, make sure that we
20488 have its containing type. */
20489 if (!origin)
20490 origin = decl_class_context (decl);
20491 if (origin != NULL_TREE)
20492 gen_type_die (origin, context_die);
20493
20494 /* And its return type. */
20495 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20496
20497 /* And its virtual context. */
20498 if (DECL_VINDEX (decl) != NULL_TREE)
20499 gen_type_die (DECL_CONTEXT (decl), context_die);
20500
20501 /* Make sure we have a member DIE for decl. */
20502 if (origin != NULL_TREE)
20503 gen_type_die_for_member (origin, decl, context_die);
20504
20505 /* And its containing namespace. */
20506 context_die = declare_in_namespace (decl, context_die);
20507 }
20508
20509 /* Now output a DIE to represent the function itself. */
20510 if (decl)
20511 gen_subprogram_die (decl, context_die);
20512 break;
20513
20514 case TYPE_DECL:
20515 /* If we are in terse mode, don't generate any DIEs to represent any
20516 actual typedefs. */
20517 if (debug_info_level <= DINFO_LEVEL_TERSE)
20518 break;
20519
20520 /* In the special case of a TYPE_DECL node representing the declaration
20521 of some type tag, if the given TYPE_DECL is marked as having been
20522 instantiated from some other (original) TYPE_DECL node (e.g. one which
20523 was generated within the original definition of an inline function) we
20524 used to generate a special (abbreviated) DW_TAG_structure_type,
20525 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20526 should be actually referencing those DIEs, as variable DIEs with that
20527 type would be emitted already in the abstract origin, so it was always
20528 removed during unused type prunning. Don't add anything in this
20529 case. */
20530 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20531 break;
20532
20533 if (is_redundant_typedef (decl))
20534 gen_type_die (TREE_TYPE (decl), context_die);
20535 else
20536 /* Output a DIE to represent the typedef itself. */
20537 gen_typedef_die (decl, context_die);
20538 break;
20539
20540 case LABEL_DECL:
20541 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20542 gen_label_die (decl, context_die);
20543 break;
20544
20545 case VAR_DECL:
20546 case RESULT_DECL:
20547 /* If we are in terse mode, don't generate any DIEs to represent any
20548 variable declarations or definitions. */
20549 if (debug_info_level <= DINFO_LEVEL_TERSE)
20550 break;
20551
20552 /* Output any DIEs that are needed to specify the type of this data
20553 object. */
20554 if (decl_by_reference_p (decl_or_origin))
20555 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20556 else
20557 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20558
20559 /* And its containing type. */
20560 class_origin = decl_class_context (decl_or_origin);
20561 if (class_origin != NULL_TREE)
20562 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20563
20564 /* And its containing namespace. */
20565 context_die = declare_in_namespace (decl_or_origin, context_die);
20566
20567 /* Now output the DIE to represent the data object itself. This gets
20568 complicated because of the possibility that the VAR_DECL really
20569 represents an inlined instance of a formal parameter for an inline
20570 function. */
20571 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20572 if (ultimate_origin != NULL_TREE
20573 && TREE_CODE (ultimate_origin) == PARM_DECL)
20574 gen_formal_parameter_die (decl, origin,
20575 true /* Emit name attribute. */,
20576 context_die);
20577 else
20578 gen_variable_die (decl, origin, context_die);
20579 break;
20580
20581 case FIELD_DECL:
20582 /* Ignore the nameless fields that are used to skip bits but handle C++
20583 anonymous unions and structs. */
20584 if (DECL_NAME (decl) != NULL_TREE
20585 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20586 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20587 {
20588 gen_type_die (member_declared_type (decl), context_die);
20589 gen_field_die (decl, context_die);
20590 }
20591 break;
20592
20593 case PARM_DECL:
20594 if (DECL_BY_REFERENCE (decl_or_origin))
20595 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20596 else
20597 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20598 return gen_formal_parameter_die (decl, origin,
20599 true /* Emit name attribute. */,
20600 context_die);
20601
20602 case NAMESPACE_DECL:
20603 case IMPORTED_DECL:
20604 if (dwarf_version >= 3 || !dwarf_strict)
20605 gen_namespace_die (decl, context_die);
20606 break;
20607
20608 default:
20609 /* Probably some frontend-internal decl. Assume we don't care. */
20610 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20611 break;
20612 }
20613
20614 return NULL;
20615 }
20616 \f
20617 /* Output debug information for global decl DECL. Called from toplev.c after
20618 compilation proper has finished. */
20619
20620 static void
20621 dwarf2out_global_decl (tree decl)
20622 {
20623 /* Output DWARF2 information for file-scope tentative data object
20624 declarations, file-scope (extern) function declarations (which
20625 had no corresponding body) and file-scope tagged type declarations
20626 and definitions which have not yet been forced out. */
20627 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20628 && !POINTER_BOUNDS_P (decl))
20629 dwarf2out_decl (decl);
20630 }
20631
20632 /* Output debug information for type decl DECL. Called from toplev.c
20633 and from language front ends (to record built-in types). */
20634 static void
20635 dwarf2out_type_decl (tree decl, int local)
20636 {
20637 if (!local)
20638 dwarf2out_decl (decl);
20639 }
20640
20641 /* Output debug information for imported module or decl DECL.
20642 NAME is non-NULL name in the lexical block if the decl has been renamed.
20643 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20644 that DECL belongs to.
20645 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20646 static void
20647 dwarf2out_imported_module_or_decl_1 (tree decl,
20648 tree name,
20649 tree lexical_block,
20650 dw_die_ref lexical_block_die)
20651 {
20652 expanded_location xloc;
20653 dw_die_ref imported_die = NULL;
20654 dw_die_ref at_import_die;
20655
20656 if (TREE_CODE (decl) == IMPORTED_DECL)
20657 {
20658 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20659 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20660 gcc_assert (decl);
20661 }
20662 else
20663 xloc = expand_location (input_location);
20664
20665 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20666 {
20667 at_import_die = force_type_die (TREE_TYPE (decl));
20668 /* For namespace N { typedef void T; } using N::T; base_type_die
20669 returns NULL, but DW_TAG_imported_declaration requires
20670 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20671 if (!at_import_die)
20672 {
20673 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20674 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20675 at_import_die = lookup_type_die (TREE_TYPE (decl));
20676 gcc_assert (at_import_die);
20677 }
20678 }
20679 else
20680 {
20681 at_import_die = lookup_decl_die (decl);
20682 if (!at_import_die)
20683 {
20684 /* If we're trying to avoid duplicate debug info, we may not have
20685 emitted the member decl for this field. Emit it now. */
20686 if (TREE_CODE (decl) == FIELD_DECL)
20687 {
20688 tree type = DECL_CONTEXT (decl);
20689
20690 if (TYPE_CONTEXT (type)
20691 && TYPE_P (TYPE_CONTEXT (type))
20692 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20693 DINFO_USAGE_DIR_USE))
20694 return;
20695 gen_type_die_for_member (type, decl,
20696 get_context_die (TYPE_CONTEXT (type)));
20697 }
20698 at_import_die = force_decl_die (decl);
20699 }
20700 }
20701
20702 if (TREE_CODE (decl) == NAMESPACE_DECL)
20703 {
20704 if (dwarf_version >= 3 || !dwarf_strict)
20705 imported_die = new_die (DW_TAG_imported_module,
20706 lexical_block_die,
20707 lexical_block);
20708 else
20709 return;
20710 }
20711 else
20712 imported_die = new_die (DW_TAG_imported_declaration,
20713 lexical_block_die,
20714 lexical_block);
20715
20716 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20717 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20718 if (name)
20719 add_AT_string (imported_die, DW_AT_name,
20720 IDENTIFIER_POINTER (name));
20721 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20722 }
20723
20724 /* Output debug information for imported module or decl DECL.
20725 NAME is non-NULL name in context if the decl has been renamed.
20726 CHILD is true if decl is one of the renamed decls as part of
20727 importing whole module. */
20728
20729 static void
20730 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20731 bool child)
20732 {
20733 /* dw_die_ref at_import_die; */
20734 dw_die_ref scope_die;
20735
20736 if (debug_info_level <= DINFO_LEVEL_TERSE)
20737 return;
20738
20739 gcc_assert (decl);
20740
20741 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20742 We need decl DIE for reference and scope die. First, get DIE for the decl
20743 itself. */
20744
20745 /* Get the scope die for decl context. Use comp_unit_die for global module
20746 or decl. If die is not found for non globals, force new die. */
20747 if (context
20748 && TYPE_P (context)
20749 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20750 return;
20751
20752 if (!(dwarf_version >= 3 || !dwarf_strict))
20753 return;
20754
20755 scope_die = get_context_die (context);
20756
20757 if (child)
20758 {
20759 gcc_assert (scope_die->die_child);
20760 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20761 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20762 scope_die = scope_die->die_child;
20763 }
20764
20765 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20766 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20767
20768 }
20769
20770 /* Write the debugging output for DECL. */
20771
20772 void
20773 dwarf2out_decl (tree decl)
20774 {
20775 dw_die_ref context_die = comp_unit_die ();
20776
20777 switch (TREE_CODE (decl))
20778 {
20779 case ERROR_MARK:
20780 return;
20781
20782 case FUNCTION_DECL:
20783 /* What we would really like to do here is to filter out all mere
20784 file-scope declarations of file-scope functions which are never
20785 referenced later within this translation unit (and keep all of ones
20786 that *are* referenced later on) but we aren't clairvoyant, so we have
20787 no idea which functions will be referenced in the future (i.e. later
20788 on within the current translation unit). So here we just ignore all
20789 file-scope function declarations which are not also definitions. If
20790 and when the debugger needs to know something about these functions,
20791 it will have to hunt around and find the DWARF information associated
20792 with the definition of the function.
20793
20794 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20795 nodes represent definitions and which ones represent mere
20796 declarations. We have to check DECL_INITIAL instead. That's because
20797 the C front-end supports some weird semantics for "extern inline"
20798 function definitions. These can get inlined within the current
20799 translation unit (and thus, we need to generate Dwarf info for their
20800 abstract instances so that the Dwarf info for the concrete inlined
20801 instances can have something to refer to) but the compiler never
20802 generates any out-of-lines instances of such things (despite the fact
20803 that they *are* definitions).
20804
20805 The important point is that the C front-end marks these "extern
20806 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20807 them anyway. Note that the C++ front-end also plays some similar games
20808 for inline function definitions appearing within include files which
20809 also contain `#pragma interface' pragmas.
20810
20811 If we are called from dwarf2out_abstract_function output a DIE
20812 anyway. We can end up here this way with early inlining and LTO
20813 where the inlined function is output in a different LTRANS unit
20814 or not at all. */
20815 if (DECL_INITIAL (decl) == NULL_TREE
20816 && ! DECL_ABSTRACT (decl))
20817 return;
20818
20819 /* If we're a nested function, initially use a parent of NULL; if we're
20820 a plain function, this will be fixed up in decls_for_scope. If
20821 we're a method, it will be ignored, since we already have a DIE. */
20822 if (decl_function_context (decl)
20823 /* But if we're in terse mode, we don't care about scope. */
20824 && debug_info_level > DINFO_LEVEL_TERSE)
20825 context_die = NULL;
20826 break;
20827
20828 case VAR_DECL:
20829 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20830 declaration and if the declaration was never even referenced from
20831 within this entire compilation unit. We suppress these DIEs in
20832 order to save space in the .debug section (by eliminating entries
20833 which are probably useless). Note that we must not suppress
20834 block-local extern declarations (whether used or not) because that
20835 would screw-up the debugger's name lookup mechanism and cause it to
20836 miss things which really ought to be in scope at a given point. */
20837 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20838 return;
20839
20840 /* For local statics lookup proper context die. */
20841 if (TREE_STATIC (decl)
20842 && DECL_CONTEXT (decl)
20843 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20844 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20845
20846 /* If we are in terse mode, don't generate any DIEs to represent any
20847 variable declarations or definitions. */
20848 if (debug_info_level <= DINFO_LEVEL_TERSE)
20849 return;
20850 break;
20851
20852 case CONST_DECL:
20853 if (debug_info_level <= DINFO_LEVEL_TERSE)
20854 return;
20855 if (!is_fortran () && !is_ada ())
20856 return;
20857 if (TREE_STATIC (decl) && decl_function_context (decl))
20858 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20859 break;
20860
20861 case NAMESPACE_DECL:
20862 case IMPORTED_DECL:
20863 if (debug_info_level <= DINFO_LEVEL_TERSE)
20864 return;
20865 if (lookup_decl_die (decl) != NULL)
20866 return;
20867 break;
20868
20869 case TYPE_DECL:
20870 /* Don't emit stubs for types unless they are needed by other DIEs. */
20871 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20872 return;
20873
20874 /* Don't bother trying to generate any DIEs to represent any of the
20875 normal built-in types for the language we are compiling. */
20876 if (DECL_IS_BUILTIN (decl))
20877 return;
20878
20879 /* If we are in terse mode, don't generate any DIEs for types. */
20880 if (debug_info_level <= DINFO_LEVEL_TERSE)
20881 return;
20882
20883 /* If we're a function-scope tag, initially use a parent of NULL;
20884 this will be fixed up in decls_for_scope. */
20885 if (decl_function_context (decl))
20886 context_die = NULL;
20887
20888 break;
20889
20890 default:
20891 return;
20892 }
20893
20894 gen_decl_die (decl, NULL, context_die);
20895 }
20896
20897 /* Write the debugging output for DECL. */
20898
20899 static void
20900 dwarf2out_function_decl (tree decl)
20901 {
20902 dwarf2out_decl (decl);
20903 call_arg_locations = NULL;
20904 call_arg_loc_last = NULL;
20905 call_site_count = -1;
20906 tail_call_site_count = -1;
20907 block_map.release ();
20908 htab_empty (decl_loc_table);
20909 htab_empty (cached_dw_loc_list_table);
20910 }
20911
20912 /* Output a marker (i.e. a label) for the beginning of the generated code for
20913 a lexical block. */
20914
20915 static void
20916 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20917 unsigned int blocknum)
20918 {
20919 switch_to_section (current_function_section ());
20920 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20921 }
20922
20923 /* Output a marker (i.e. a label) for the end of the generated code for a
20924 lexical block. */
20925
20926 static void
20927 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20928 {
20929 switch_to_section (current_function_section ());
20930 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20931 }
20932
20933 /* Returns nonzero if it is appropriate not to emit any debugging
20934 information for BLOCK, because it doesn't contain any instructions.
20935
20936 Don't allow this for blocks with nested functions or local classes
20937 as we would end up with orphans, and in the presence of scheduling
20938 we may end up calling them anyway. */
20939
20940 static bool
20941 dwarf2out_ignore_block (const_tree block)
20942 {
20943 tree decl;
20944 unsigned int i;
20945
20946 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20947 if (TREE_CODE (decl) == FUNCTION_DECL
20948 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20949 return 0;
20950 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20951 {
20952 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20953 if (TREE_CODE (decl) == FUNCTION_DECL
20954 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20955 return 0;
20956 }
20957
20958 return 1;
20959 }
20960
20961 /* Hash table routines for file_hash. */
20962
20963 static int
20964 file_table_eq (const void *p1_p, const void *p2_p)
20965 {
20966 const struct dwarf_file_data *const p1 =
20967 (const struct dwarf_file_data *) p1_p;
20968 const char *const p2 = (const char *) p2_p;
20969 return filename_cmp (p1->filename, p2) == 0;
20970 }
20971
20972 static hashval_t
20973 file_table_hash (const void *p_p)
20974 {
20975 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20976 return htab_hash_string (p->filename);
20977 }
20978
20979 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20980 dwarf2out.c) and return its "index". The index of each (known) filename is
20981 just a unique number which is associated with only that one filename. We
20982 need such numbers for the sake of generating labels (in the .debug_sfnames
20983 section) and references to those files numbers (in the .debug_srcinfo
20984 and.debug_macinfo sections). If the filename given as an argument is not
20985 found in our current list, add it to the list and assign it the next
20986 available unique index number. In order to speed up searches, we remember
20987 the index of the filename was looked up last. This handles the majority of
20988 all searches. */
20989
20990 static struct dwarf_file_data *
20991 lookup_filename (const char *file_name)
20992 {
20993 void ** slot;
20994 struct dwarf_file_data * created;
20995
20996 /* Check to see if the file name that was searched on the previous
20997 call matches this file name. If so, return the index. */
20998 if (file_table_last_lookup
20999 && (file_name == file_table_last_lookup->filename
21000 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21001 return file_table_last_lookup;
21002
21003 /* Didn't match the previous lookup, search the table. */
21004 slot = htab_find_slot_with_hash (file_table, file_name,
21005 htab_hash_string (file_name), INSERT);
21006 if (*slot)
21007 return (struct dwarf_file_data *) *slot;
21008
21009 created = ggc_alloc_dwarf_file_data ();
21010 created->filename = file_name;
21011 created->emitted_number = 0;
21012 *slot = created;
21013 return created;
21014 }
21015
21016 /* If the assembler will construct the file table, then translate the compiler
21017 internal file table number into the assembler file table number, and emit
21018 a .file directive if we haven't already emitted one yet. The file table
21019 numbers are different because we prune debug info for unused variables and
21020 types, which may include filenames. */
21021
21022 static int
21023 maybe_emit_file (struct dwarf_file_data * fd)
21024 {
21025 if (! fd->emitted_number)
21026 {
21027 if (last_emitted_file)
21028 fd->emitted_number = last_emitted_file->emitted_number + 1;
21029 else
21030 fd->emitted_number = 1;
21031 last_emitted_file = fd;
21032
21033 if (DWARF2_ASM_LINE_DEBUG_INFO)
21034 {
21035 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21036 output_quoted_string (asm_out_file,
21037 remap_debug_filename (fd->filename));
21038 fputc ('\n', asm_out_file);
21039 }
21040 }
21041
21042 return fd->emitted_number;
21043 }
21044
21045 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21046 That generation should happen after function debug info has been
21047 generated. The value of the attribute is the constant value of ARG. */
21048
21049 static void
21050 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21051 {
21052 die_arg_entry entry;
21053
21054 if (!die || !arg)
21055 return;
21056
21057 if (!tmpl_value_parm_die_table)
21058 vec_alloc (tmpl_value_parm_die_table, 32);
21059
21060 entry.die = die;
21061 entry.arg = arg;
21062 vec_safe_push (tmpl_value_parm_die_table, entry);
21063 }
21064
21065 /* Return TRUE if T is an instance of generic type, FALSE
21066 otherwise. */
21067
21068 static bool
21069 generic_type_p (tree t)
21070 {
21071 if (t == NULL_TREE || !TYPE_P (t))
21072 return false;
21073 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21074 }
21075
21076 /* Schedule the generation of the generic parameter dies for the
21077 instance of generic type T. The proper generation itself is later
21078 done by gen_scheduled_generic_parms_dies. */
21079
21080 static void
21081 schedule_generic_params_dies_gen (tree t)
21082 {
21083 if (!generic_type_p (t))
21084 return;
21085
21086 if (!generic_type_instances)
21087 vec_alloc (generic_type_instances, 256);
21088
21089 vec_safe_push (generic_type_instances, t);
21090 }
21091
21092 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21093 by append_entry_to_tmpl_value_parm_die_table. This function must
21094 be called after function DIEs have been generated. */
21095
21096 static void
21097 gen_remaining_tmpl_value_param_die_attribute (void)
21098 {
21099 if (tmpl_value_parm_die_table)
21100 {
21101 unsigned i;
21102 die_arg_entry *e;
21103
21104 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21105 tree_add_const_value_attribute (e->die, e->arg);
21106 }
21107 }
21108
21109 /* Generate generic parameters DIEs for instances of generic types
21110 that have been previously scheduled by
21111 schedule_generic_params_dies_gen. This function must be called
21112 after all the types of the CU have been laid out. */
21113
21114 static void
21115 gen_scheduled_generic_parms_dies (void)
21116 {
21117 unsigned i;
21118 tree t;
21119
21120 if (!generic_type_instances)
21121 return;
21122
21123 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21124 if (COMPLETE_TYPE_P (t))
21125 gen_generic_params_dies (t);
21126 }
21127
21128
21129 /* Replace DW_AT_name for the decl with name. */
21130
21131 static void
21132 dwarf2out_set_name (tree decl, tree name)
21133 {
21134 dw_die_ref die;
21135 dw_attr_ref attr;
21136 const char *dname;
21137
21138 die = TYPE_SYMTAB_DIE (decl);
21139 if (!die)
21140 return;
21141
21142 dname = dwarf2_name (name, 0);
21143 if (!dname)
21144 return;
21145
21146 attr = get_AT (die, DW_AT_name);
21147 if (attr)
21148 {
21149 struct indirect_string_node *node;
21150
21151 node = find_AT_string (dname);
21152 /* replace the string. */
21153 attr->dw_attr_val.v.val_str = node;
21154 }
21155
21156 else
21157 add_name_attribute (die, dname);
21158 }
21159
21160 /* True if before or during processing of the first function being emitted. */
21161 static bool in_first_function_p = true;
21162 /* True if loc_note during dwarf2out_var_location call might still be
21163 before first real instruction at address equal to .Ltext0. */
21164 static bool maybe_at_text_label_p = true;
21165 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21166 static unsigned int first_loclabel_num_not_at_text_label;
21167
21168 /* Called by the final INSN scan whenever we see a var location. We
21169 use it to drop labels in the right places, and throw the location in
21170 our lookup table. */
21171
21172 static void
21173 dwarf2out_var_location (rtx loc_note)
21174 {
21175 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21176 struct var_loc_node *newloc;
21177 rtx next_real, next_note;
21178 static const char *last_label;
21179 static const char *last_postcall_label;
21180 static bool last_in_cold_section_p;
21181 static rtx expected_next_loc_note;
21182 tree decl;
21183 bool var_loc_p;
21184
21185 if (!NOTE_P (loc_note))
21186 {
21187 if (CALL_P (loc_note))
21188 {
21189 call_site_count++;
21190 if (SIBLING_CALL_P (loc_note))
21191 tail_call_site_count++;
21192 }
21193 return;
21194 }
21195
21196 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21197 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21198 return;
21199
21200 /* Optimize processing a large consecutive sequence of location
21201 notes so we don't spend too much time in next_real_insn. If the
21202 next insn is another location note, remember the next_real_insn
21203 calculation for next time. */
21204 next_real = cached_next_real_insn;
21205 if (next_real)
21206 {
21207 if (expected_next_loc_note != loc_note)
21208 next_real = NULL_RTX;
21209 }
21210
21211 next_note = NEXT_INSN (loc_note);
21212 if (! next_note
21213 || INSN_DELETED_P (next_note)
21214 || ! NOTE_P (next_note)
21215 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21216 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21217 next_note = NULL_RTX;
21218
21219 if (! next_real)
21220 next_real = next_real_insn (loc_note);
21221
21222 if (next_note)
21223 {
21224 expected_next_loc_note = next_note;
21225 cached_next_real_insn = next_real;
21226 }
21227 else
21228 cached_next_real_insn = NULL_RTX;
21229
21230 /* If there are no instructions which would be affected by this note,
21231 don't do anything. */
21232 if (var_loc_p
21233 && next_real == NULL_RTX
21234 && !NOTE_DURING_CALL_P (loc_note))
21235 return;
21236
21237 if (next_real == NULL_RTX)
21238 next_real = get_last_insn ();
21239
21240 /* If there were any real insns between note we processed last time
21241 and this note (or if it is the first note), clear
21242 last_{,postcall_}label so that they are not reused this time. */
21243 if (last_var_location_insn == NULL_RTX
21244 || last_var_location_insn != next_real
21245 || last_in_cold_section_p != in_cold_section_p)
21246 {
21247 last_label = NULL;
21248 last_postcall_label = NULL;
21249 }
21250
21251 if (var_loc_p)
21252 {
21253 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21254 newloc = add_var_loc_to_decl (decl, loc_note,
21255 NOTE_DURING_CALL_P (loc_note)
21256 ? last_postcall_label : last_label);
21257 if (newloc == NULL)
21258 return;
21259 }
21260 else
21261 {
21262 decl = NULL_TREE;
21263 newloc = NULL;
21264 }
21265
21266 /* If there were no real insns between note we processed last time
21267 and this note, use the label we emitted last time. Otherwise
21268 create a new label and emit it. */
21269 if (last_label == NULL)
21270 {
21271 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21272 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21273 loclabel_num++;
21274 last_label = ggc_strdup (loclabel);
21275 /* See if loclabel might be equal to .Ltext0. If yes,
21276 bump first_loclabel_num_not_at_text_label. */
21277 if (!have_multiple_function_sections
21278 && in_first_function_p
21279 && maybe_at_text_label_p)
21280 {
21281 static rtx last_start;
21282 rtx insn;
21283 for (insn = loc_note; insn; insn = previous_insn (insn))
21284 if (insn == last_start)
21285 break;
21286 else if (!NONDEBUG_INSN_P (insn))
21287 continue;
21288 else
21289 {
21290 rtx body = PATTERN (insn);
21291 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21292 continue;
21293 /* Inline asm could occupy zero bytes. */
21294 else if (GET_CODE (body) == ASM_INPUT
21295 || asm_noperands (body) >= 0)
21296 continue;
21297 #ifdef HAVE_attr_length
21298 else if (get_attr_min_length (insn) == 0)
21299 continue;
21300 #endif
21301 else
21302 {
21303 /* Assume insn has non-zero length. */
21304 maybe_at_text_label_p = false;
21305 break;
21306 }
21307 }
21308 if (maybe_at_text_label_p)
21309 {
21310 last_start = loc_note;
21311 first_loclabel_num_not_at_text_label = loclabel_num;
21312 }
21313 }
21314 }
21315
21316 if (!var_loc_p)
21317 {
21318 struct call_arg_loc_node *ca_loc
21319 = ggc_alloc_cleared_call_arg_loc_node ();
21320 rtx prev = prev_real_insn (loc_note), x;
21321 ca_loc->call_arg_loc_note = loc_note;
21322 ca_loc->next = NULL;
21323 ca_loc->label = last_label;
21324 gcc_assert (prev
21325 && (CALL_P (prev)
21326 || (NONJUMP_INSN_P (prev)
21327 && GET_CODE (PATTERN (prev)) == SEQUENCE
21328 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21329 if (!CALL_P (prev))
21330 prev = XVECEXP (PATTERN (prev), 0, 0);
21331 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21332 x = get_call_rtx_from (PATTERN (prev));
21333 if (x)
21334 {
21335 x = XEXP (XEXP (x, 0), 0);
21336 if (GET_CODE (x) == SYMBOL_REF
21337 && SYMBOL_REF_DECL (x)
21338 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21339 ca_loc->symbol_ref = x;
21340 }
21341 ca_loc->block = insn_scope (prev);
21342 if (call_arg_locations)
21343 call_arg_loc_last->next = ca_loc;
21344 else
21345 call_arg_locations = ca_loc;
21346 call_arg_loc_last = ca_loc;
21347 }
21348 else if (!NOTE_DURING_CALL_P (loc_note))
21349 newloc->label = last_label;
21350 else
21351 {
21352 if (!last_postcall_label)
21353 {
21354 sprintf (loclabel, "%s-1", last_label);
21355 last_postcall_label = ggc_strdup (loclabel);
21356 }
21357 newloc->label = last_postcall_label;
21358 }
21359
21360 last_var_location_insn = next_real;
21361 last_in_cold_section_p = in_cold_section_p;
21362 }
21363
21364 /* Note in one location list that text section has changed. */
21365
21366 static int
21367 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21368 {
21369 var_loc_list *list = (var_loc_list *) *slot;
21370 if (list->first)
21371 list->last_before_switch
21372 = list->last->next ? list->last->next : list->last;
21373 return 1;
21374 }
21375
21376 /* Note in all location lists that text section has changed. */
21377
21378 static void
21379 var_location_switch_text_section (void)
21380 {
21381 if (decl_loc_table == NULL)
21382 return;
21383
21384 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21385 }
21386
21387 /* Create a new line number table. */
21388
21389 static dw_line_info_table *
21390 new_line_info_table (void)
21391 {
21392 dw_line_info_table *table;
21393
21394 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21395 table->file_num = 1;
21396 table->line_num = 1;
21397 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21398
21399 return table;
21400 }
21401
21402 /* Lookup the "current" table into which we emit line info, so
21403 that we don't have to do it for every source line. */
21404
21405 static void
21406 set_cur_line_info_table (section *sec)
21407 {
21408 dw_line_info_table *table;
21409
21410 if (sec == text_section)
21411 table = text_section_line_info;
21412 else if (sec == cold_text_section)
21413 {
21414 table = cold_text_section_line_info;
21415 if (!table)
21416 {
21417 cold_text_section_line_info = table = new_line_info_table ();
21418 table->end_label = cold_end_label;
21419 }
21420 }
21421 else
21422 {
21423 const char *end_label;
21424
21425 if (flag_reorder_blocks_and_partition)
21426 {
21427 if (in_cold_section_p)
21428 end_label = crtl->subsections.cold_section_end_label;
21429 else
21430 end_label = crtl->subsections.hot_section_end_label;
21431 }
21432 else
21433 {
21434 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21435 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21436 current_function_funcdef_no);
21437 end_label = ggc_strdup (label);
21438 }
21439
21440 table = new_line_info_table ();
21441 table->end_label = end_label;
21442
21443 vec_safe_push (separate_line_info, table);
21444 }
21445
21446 if (DWARF2_ASM_LINE_DEBUG_INFO)
21447 table->is_stmt = (cur_line_info_table
21448 ? cur_line_info_table->is_stmt
21449 : DWARF_LINE_DEFAULT_IS_STMT_START);
21450 cur_line_info_table = table;
21451 }
21452
21453
21454 /* We need to reset the locations at the beginning of each
21455 function. We can't do this in the end_function hook, because the
21456 declarations that use the locations won't have been output when
21457 that hook is called. Also compute have_multiple_function_sections here. */
21458
21459 static void
21460 dwarf2out_begin_function (tree fun)
21461 {
21462 section *sec = function_section (fun);
21463
21464 if (sec != text_section)
21465 have_multiple_function_sections = true;
21466
21467 if (flag_reorder_blocks_and_partition && !cold_text_section)
21468 {
21469 gcc_assert (current_function_decl == fun);
21470 cold_text_section = unlikely_text_section ();
21471 switch_to_section (cold_text_section);
21472 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21473 switch_to_section (sec);
21474 }
21475
21476 dwarf2out_note_section_used ();
21477 call_site_count = 0;
21478 tail_call_site_count = 0;
21479
21480 set_cur_line_info_table (sec);
21481 }
21482
21483 /* Helper function of dwarf2out_end_function, called only after emitting
21484 the very first function into assembly. Check if some .debug_loc range
21485 might end with a .LVL* label that could be equal to .Ltext0.
21486 In that case we must force using absolute addresses in .debug_loc ranges,
21487 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21488 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21489 list terminator.
21490 Set have_multiple_function_sections to true in that case and
21491 terminate htab traversal. */
21492
21493 static int
21494 find_empty_loc_ranges_at_text_label (void **slot, void *)
21495 {
21496 var_loc_list *entry;
21497 struct var_loc_node *node;
21498
21499 entry = (var_loc_list *) *slot;
21500 node = entry->first;
21501 if (node && node->next && node->next->label)
21502 {
21503 unsigned int i;
21504 const char *label = node->next->label;
21505 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21506
21507 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21508 {
21509 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21510 if (strcmp (label, loclabel) == 0)
21511 {
21512 have_multiple_function_sections = true;
21513 return 0;
21514 }
21515 }
21516 }
21517 return 1;
21518 }
21519
21520 /* Hook called after emitting a function into assembly.
21521 This does something only for the very first function emitted. */
21522
21523 static void
21524 dwarf2out_end_function (unsigned int)
21525 {
21526 if (in_first_function_p
21527 && !have_multiple_function_sections
21528 && first_loclabel_num_not_at_text_label
21529 && decl_loc_table)
21530 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21531 NULL);
21532 in_first_function_p = false;
21533 maybe_at_text_label_p = false;
21534 }
21535
21536 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21537
21538 static void
21539 push_dw_line_info_entry (dw_line_info_table *table,
21540 enum dw_line_info_opcode opcode, unsigned int val)
21541 {
21542 dw_line_info_entry e;
21543 e.opcode = opcode;
21544 e.val = val;
21545 vec_safe_push (table->entries, e);
21546 }
21547
21548 /* Output a label to mark the beginning of a source code line entry
21549 and record information relating to this source line, in
21550 'line_info_table' for later output of the .debug_line section. */
21551 /* ??? The discriminator parameter ought to be unsigned. */
21552
21553 static void
21554 dwarf2out_source_line (unsigned int line, const char *filename,
21555 int discriminator, bool is_stmt)
21556 {
21557 unsigned int file_num;
21558 dw_line_info_table *table;
21559
21560 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21561 return;
21562
21563 /* The discriminator column was added in dwarf4. Simplify the below
21564 by simply removing it if we're not supposed to output it. */
21565 if (dwarf_version < 4 && dwarf_strict)
21566 discriminator = 0;
21567
21568 table = cur_line_info_table;
21569 file_num = maybe_emit_file (lookup_filename (filename));
21570
21571 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21572 the debugger has used the second (possibly duplicate) line number
21573 at the beginning of the function to mark the end of the prologue.
21574 We could eliminate any other duplicates within the function. For
21575 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21576 that second line number entry. */
21577 /* Recall that this end-of-prologue indication is *not* the same thing
21578 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21579 to which the hook corresponds, follows the last insn that was
21580 emitted by gen_prologue. What we need is to precede the first insn
21581 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21582 insn that corresponds to something the user wrote. These may be
21583 very different locations once scheduling is enabled. */
21584
21585 if (0 && file_num == table->file_num
21586 && line == table->line_num
21587 && discriminator == table->discrim_num
21588 && is_stmt == table->is_stmt)
21589 return;
21590
21591 switch_to_section (current_function_section ());
21592
21593 /* If requested, emit something human-readable. */
21594 if (flag_debug_asm)
21595 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21596
21597 if (DWARF2_ASM_LINE_DEBUG_INFO)
21598 {
21599 /* Emit the .loc directive understood by GNU as. */
21600 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21601 file_num, line, is_stmt, discriminator */
21602 fputs ("\t.loc ", asm_out_file);
21603 fprint_ul (asm_out_file, file_num);
21604 putc (' ', asm_out_file);
21605 fprint_ul (asm_out_file, line);
21606 putc (' ', asm_out_file);
21607 putc ('0', asm_out_file);
21608
21609 if (is_stmt != table->is_stmt)
21610 {
21611 fputs (" is_stmt ", asm_out_file);
21612 putc (is_stmt ? '1' : '0', asm_out_file);
21613 }
21614 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21615 {
21616 gcc_assert (discriminator > 0);
21617 fputs (" discriminator ", asm_out_file);
21618 fprint_ul (asm_out_file, (unsigned long) discriminator);
21619 }
21620 putc ('\n', asm_out_file);
21621 }
21622 else
21623 {
21624 unsigned int label_num = ++line_info_label_num;
21625
21626 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21627
21628 push_dw_line_info_entry (table, LI_set_address, label_num);
21629 if (file_num != table->file_num)
21630 push_dw_line_info_entry (table, LI_set_file, file_num);
21631 if (discriminator != table->discrim_num)
21632 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21633 if (is_stmt != table->is_stmt)
21634 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21635 push_dw_line_info_entry (table, LI_set_line, line);
21636 }
21637
21638 table->file_num = file_num;
21639 table->line_num = line;
21640 table->discrim_num = discriminator;
21641 table->is_stmt = is_stmt;
21642 table->in_use = true;
21643 }
21644
21645 /* Record the beginning of a new source file. */
21646
21647 static void
21648 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21649 {
21650 if (flag_eliminate_dwarf2_dups)
21651 {
21652 /* Record the beginning of the file for break_out_includes. */
21653 dw_die_ref bincl_die;
21654
21655 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21656 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21657 }
21658
21659 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21660 {
21661 macinfo_entry e;
21662 e.code = DW_MACINFO_start_file;
21663 e.lineno = lineno;
21664 e.info = ggc_strdup (filename);
21665 vec_safe_push (macinfo_table, e);
21666 }
21667 }
21668
21669 /* Record the end of a source file. */
21670
21671 static void
21672 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21673 {
21674 if (flag_eliminate_dwarf2_dups)
21675 /* Record the end of the file for break_out_includes. */
21676 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21677
21678 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21679 {
21680 macinfo_entry e;
21681 e.code = DW_MACINFO_end_file;
21682 e.lineno = lineno;
21683 e.info = NULL;
21684 vec_safe_push (macinfo_table, e);
21685 }
21686 }
21687
21688 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21689 the tail part of the directive line, i.e. the part which is past the
21690 initial whitespace, #, whitespace, directive-name, whitespace part. */
21691
21692 static void
21693 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21694 const char *buffer ATTRIBUTE_UNUSED)
21695 {
21696 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21697 {
21698 macinfo_entry e;
21699 /* Insert a dummy first entry to be able to optimize the whole
21700 predefined macro block using DW_MACRO_GNU_transparent_include. */
21701 if (macinfo_table->is_empty () && lineno <= 1)
21702 {
21703 e.code = 0;
21704 e.lineno = 0;
21705 e.info = NULL;
21706 vec_safe_push (macinfo_table, e);
21707 }
21708 e.code = DW_MACINFO_define;
21709 e.lineno = lineno;
21710 e.info = ggc_strdup (buffer);
21711 vec_safe_push (macinfo_table, e);
21712 }
21713 }
21714
21715 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21716 the tail part of the directive line, i.e. the part which is past the
21717 initial whitespace, #, whitespace, directive-name, whitespace part. */
21718
21719 static void
21720 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21721 const char *buffer ATTRIBUTE_UNUSED)
21722 {
21723 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21724 {
21725 macinfo_entry e;
21726 /* Insert a dummy first entry to be able to optimize the whole
21727 predefined macro block using DW_MACRO_GNU_transparent_include. */
21728 if (macinfo_table->is_empty () && lineno <= 1)
21729 {
21730 e.code = 0;
21731 e.lineno = 0;
21732 e.info = NULL;
21733 vec_safe_push (macinfo_table, e);
21734 }
21735 e.code = DW_MACINFO_undef;
21736 e.lineno = lineno;
21737 e.info = ggc_strdup (buffer);
21738 vec_safe_push (macinfo_table, e);
21739 }
21740 }
21741
21742 /* Helpers to manipulate hash table of CUs. */
21743
21744 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21745 {
21746 typedef macinfo_entry value_type;
21747 typedef macinfo_entry compare_type;
21748 static inline hashval_t hash (const value_type *);
21749 static inline bool equal (const value_type *, const compare_type *);
21750 };
21751
21752 inline hashval_t
21753 macinfo_entry_hasher::hash (const value_type *entry)
21754 {
21755 return htab_hash_string (entry->info);
21756 }
21757
21758 inline bool
21759 macinfo_entry_hasher::equal (const value_type *entry1,
21760 const compare_type *entry2)
21761 {
21762 return !strcmp (entry1->info, entry2->info);
21763 }
21764
21765 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21766
21767 /* Output a single .debug_macinfo entry. */
21768
21769 static void
21770 output_macinfo_op (macinfo_entry *ref)
21771 {
21772 int file_num;
21773 size_t len;
21774 struct indirect_string_node *node;
21775 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21776 struct dwarf_file_data *fd;
21777
21778 switch (ref->code)
21779 {
21780 case DW_MACINFO_start_file:
21781 fd = lookup_filename (ref->info);
21782 file_num = maybe_emit_file (fd);
21783 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21784 dw2_asm_output_data_uleb128 (ref->lineno,
21785 "Included from line number %lu",
21786 (unsigned long) ref->lineno);
21787 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21788 break;
21789 case DW_MACINFO_end_file:
21790 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21791 break;
21792 case DW_MACINFO_define:
21793 case DW_MACINFO_undef:
21794 len = strlen (ref->info) + 1;
21795 if (!dwarf_strict
21796 && len > DWARF_OFFSET_SIZE
21797 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21798 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21799 {
21800 ref->code = ref->code == DW_MACINFO_define
21801 ? DW_MACRO_GNU_define_indirect
21802 : DW_MACRO_GNU_undef_indirect;
21803 output_macinfo_op (ref);
21804 return;
21805 }
21806 dw2_asm_output_data (1, ref->code,
21807 ref->code == DW_MACINFO_define
21808 ? "Define macro" : "Undefine macro");
21809 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21810 (unsigned long) ref->lineno);
21811 dw2_asm_output_nstring (ref->info, -1, "The macro");
21812 break;
21813 case DW_MACRO_GNU_define_indirect:
21814 case DW_MACRO_GNU_undef_indirect:
21815 node = find_AT_string (ref->info);
21816 gcc_assert (node
21817 && ((node->form == DW_FORM_strp)
21818 || (node->form == DW_FORM_GNU_str_index)));
21819 dw2_asm_output_data (1, ref->code,
21820 ref->code == DW_MACRO_GNU_define_indirect
21821 ? "Define macro indirect"
21822 : "Undefine macro indirect");
21823 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21824 (unsigned long) ref->lineno);
21825 if (node->form == DW_FORM_strp)
21826 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21827 debug_str_section, "The macro: \"%s\"",
21828 ref->info);
21829 else
21830 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21831 ref->info);
21832 break;
21833 case DW_MACRO_GNU_transparent_include:
21834 dw2_asm_output_data (1, ref->code, "Transparent include");
21835 ASM_GENERATE_INTERNAL_LABEL (label,
21836 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21837 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21838 break;
21839 default:
21840 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21841 ASM_COMMENT_START, (unsigned long) ref->code);
21842 break;
21843 }
21844 }
21845
21846 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21847 other compilation unit .debug_macinfo sections. IDX is the first
21848 index of a define/undef, return the number of ops that should be
21849 emitted in a comdat .debug_macinfo section and emit
21850 a DW_MACRO_GNU_transparent_include entry referencing it.
21851 If the define/undef entry should be emitted normally, return 0. */
21852
21853 static unsigned
21854 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21855 macinfo_hash_type *macinfo_htab)
21856 {
21857 macinfo_entry *first, *second, *cur, *inc;
21858 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21859 unsigned char checksum[16];
21860 struct md5_ctx ctx;
21861 char *grp_name, *tail;
21862 const char *base;
21863 unsigned int i, count, encoded_filename_len, linebuf_len;
21864 macinfo_entry **slot;
21865
21866 first = &(*macinfo_table)[idx];
21867 second = &(*macinfo_table)[idx + 1];
21868
21869 /* Optimize only if there are at least two consecutive define/undef ops,
21870 and either all of them are before first DW_MACINFO_start_file
21871 with lineno {0,1} (i.e. predefined macro block), or all of them are
21872 in some included header file. */
21873 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21874 return 0;
21875 if (vec_safe_is_empty (files))
21876 {
21877 if (first->lineno > 1 || second->lineno > 1)
21878 return 0;
21879 }
21880 else if (first->lineno == 0)
21881 return 0;
21882
21883 /* Find the last define/undef entry that can be grouped together
21884 with first and at the same time compute md5 checksum of their
21885 codes, linenumbers and strings. */
21886 md5_init_ctx (&ctx);
21887 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21888 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21889 break;
21890 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21891 break;
21892 else
21893 {
21894 unsigned char code = cur->code;
21895 md5_process_bytes (&code, 1, &ctx);
21896 checksum_uleb128 (cur->lineno, &ctx);
21897 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21898 }
21899 md5_finish_ctx (&ctx, checksum);
21900 count = i - idx;
21901
21902 /* From the containing include filename (if any) pick up just
21903 usable characters from its basename. */
21904 if (vec_safe_is_empty (files))
21905 base = "";
21906 else
21907 base = lbasename (files->last ().info);
21908 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21909 if (ISIDNUM (base[i]) || base[i] == '.')
21910 encoded_filename_len++;
21911 /* Count . at the end. */
21912 if (encoded_filename_len)
21913 encoded_filename_len++;
21914
21915 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21916 linebuf_len = strlen (linebuf);
21917
21918 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21919 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21920 + 16 * 2 + 1);
21921 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21922 tail = grp_name + 4;
21923 if (encoded_filename_len)
21924 {
21925 for (i = 0; base[i]; i++)
21926 if (ISIDNUM (base[i]) || base[i] == '.')
21927 *tail++ = base[i];
21928 *tail++ = '.';
21929 }
21930 memcpy (tail, linebuf, linebuf_len);
21931 tail += linebuf_len;
21932 *tail++ = '.';
21933 for (i = 0; i < 16; i++)
21934 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21935
21936 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21937 in the empty vector entry before the first define/undef. */
21938 inc = &(*macinfo_table)[idx - 1];
21939 inc->code = DW_MACRO_GNU_transparent_include;
21940 inc->lineno = 0;
21941 inc->info = ggc_strdup (grp_name);
21942 if (!macinfo_htab->is_created ())
21943 macinfo_htab->create (10);
21944 /* Avoid emitting duplicates. */
21945 slot = macinfo_htab->find_slot (inc, INSERT);
21946 if (*slot != NULL)
21947 {
21948 inc->code = 0;
21949 inc->info = NULL;
21950 /* If such an entry has been used before, just emit
21951 a DW_MACRO_GNU_transparent_include op. */
21952 inc = *slot;
21953 output_macinfo_op (inc);
21954 /* And clear all macinfo_entry in the range to avoid emitting them
21955 in the second pass. */
21956 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21957 {
21958 cur->code = 0;
21959 cur->info = NULL;
21960 }
21961 }
21962 else
21963 {
21964 *slot = inc;
21965 inc->lineno = macinfo_htab->elements ();
21966 output_macinfo_op (inc);
21967 }
21968 return count;
21969 }
21970
21971 /* Save any strings needed by the macinfo table in the debug str
21972 table. All strings must be collected into the table by the time
21973 index_string is called. */
21974
21975 static void
21976 save_macinfo_strings (void)
21977 {
21978 unsigned len;
21979 unsigned i;
21980 macinfo_entry *ref;
21981
21982 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21983 {
21984 switch (ref->code)
21985 {
21986 /* Match the logic in output_macinfo_op to decide on
21987 indirect strings. */
21988 case DW_MACINFO_define:
21989 case DW_MACINFO_undef:
21990 len = strlen (ref->info) + 1;
21991 if (!dwarf_strict
21992 && len > DWARF_OFFSET_SIZE
21993 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21994 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21995 set_indirect_string (find_AT_string (ref->info));
21996 break;
21997 case DW_MACRO_GNU_define_indirect:
21998 case DW_MACRO_GNU_undef_indirect:
21999 set_indirect_string (find_AT_string (ref->info));
22000 break;
22001 default:
22002 break;
22003 }
22004 }
22005 }
22006
22007 /* Output macinfo section(s). */
22008
22009 static void
22010 output_macinfo (void)
22011 {
22012 unsigned i;
22013 unsigned long length = vec_safe_length (macinfo_table);
22014 macinfo_entry *ref;
22015 vec<macinfo_entry, va_gc> *files = NULL;
22016 macinfo_hash_type macinfo_htab;
22017
22018 if (! length)
22019 return;
22020
22021 /* output_macinfo* uses these interchangeably. */
22022 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22023 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22024 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22025 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22026
22027 /* For .debug_macro emit the section header. */
22028 if (!dwarf_strict)
22029 {
22030 dw2_asm_output_data (2, 4, "DWARF macro version number");
22031 if (DWARF_OFFSET_SIZE == 8)
22032 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22033 else
22034 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22035 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22036 (!dwarf_split_debug_info ? debug_line_section_label
22037 : debug_skeleton_line_section_label),
22038 debug_line_section, NULL);
22039 }
22040
22041 /* In the first loop, it emits the primary .debug_macinfo section
22042 and after each emitted op the macinfo_entry is cleared.
22043 If a longer range of define/undef ops can be optimized using
22044 DW_MACRO_GNU_transparent_include, the
22045 DW_MACRO_GNU_transparent_include op is emitted and kept in
22046 the vector before the first define/undef in the range and the
22047 whole range of define/undef ops is not emitted and kept. */
22048 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22049 {
22050 switch (ref->code)
22051 {
22052 case DW_MACINFO_start_file:
22053 vec_safe_push (files, *ref);
22054 break;
22055 case DW_MACINFO_end_file:
22056 if (!vec_safe_is_empty (files))
22057 files->pop ();
22058 break;
22059 case DW_MACINFO_define:
22060 case DW_MACINFO_undef:
22061 if (!dwarf_strict
22062 && HAVE_COMDAT_GROUP
22063 && vec_safe_length (files) != 1
22064 && i > 0
22065 && i + 1 < length
22066 && (*macinfo_table)[i - 1].code == 0)
22067 {
22068 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22069 if (count)
22070 {
22071 i += count - 1;
22072 continue;
22073 }
22074 }
22075 break;
22076 case 0:
22077 /* A dummy entry may be inserted at the beginning to be able
22078 to optimize the whole block of predefined macros. */
22079 if (i == 0)
22080 continue;
22081 default:
22082 break;
22083 }
22084 output_macinfo_op (ref);
22085 ref->info = NULL;
22086 ref->code = 0;
22087 }
22088
22089 if (!macinfo_htab.is_created ())
22090 return;
22091
22092 macinfo_htab.dispose ();
22093
22094 /* If any DW_MACRO_GNU_transparent_include were used, on those
22095 DW_MACRO_GNU_transparent_include entries terminate the
22096 current chain and switch to a new comdat .debug_macinfo
22097 section and emit the define/undef entries within it. */
22098 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22099 switch (ref->code)
22100 {
22101 case 0:
22102 continue;
22103 case DW_MACRO_GNU_transparent_include:
22104 {
22105 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22106 tree comdat_key = get_identifier (ref->info);
22107 /* Terminate the previous .debug_macinfo section. */
22108 dw2_asm_output_data (1, 0, "End compilation unit");
22109 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22110 SECTION_DEBUG
22111 | SECTION_LINKONCE,
22112 comdat_key);
22113 ASM_GENERATE_INTERNAL_LABEL (label,
22114 DEBUG_MACRO_SECTION_LABEL,
22115 ref->lineno);
22116 ASM_OUTPUT_LABEL (asm_out_file, label);
22117 ref->code = 0;
22118 ref->info = NULL;
22119 dw2_asm_output_data (2, 4, "DWARF macro version number");
22120 if (DWARF_OFFSET_SIZE == 8)
22121 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22122 else
22123 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22124 }
22125 break;
22126 case DW_MACINFO_define:
22127 case DW_MACINFO_undef:
22128 output_macinfo_op (ref);
22129 ref->code = 0;
22130 ref->info = NULL;
22131 break;
22132 default:
22133 gcc_unreachable ();
22134 }
22135 }
22136
22137 /* Set up for Dwarf output at the start of compilation. */
22138
22139 static void
22140 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22141 {
22142 /* Allocate the file_table. */
22143 file_table = htab_create_ggc (50, file_table_hash,
22144 file_table_eq, NULL);
22145
22146 /* Allocate the decl_die_table. */
22147 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22148 decl_die_table_eq, NULL);
22149
22150 /* Allocate the decl_loc_table. */
22151 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22152 decl_loc_table_eq, NULL);
22153
22154 /* Allocate the cached_dw_loc_list_table. */
22155 cached_dw_loc_list_table
22156 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22157 cached_dw_loc_list_table_eq, NULL);
22158
22159 /* Allocate the initial hunk of the decl_scope_table. */
22160 vec_alloc (decl_scope_table, 256);
22161
22162 /* Allocate the initial hunk of the abbrev_die_table. */
22163 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22164 (ABBREV_DIE_TABLE_INCREMENT);
22165 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22166 /* Zero-th entry is allocated, but unused. */
22167 abbrev_die_table_in_use = 1;
22168
22169 /* Allocate the pubtypes and pubnames vectors. */
22170 vec_alloc (pubname_table, 32);
22171 vec_alloc (pubtype_table, 32);
22172
22173 vec_alloc (incomplete_types, 64);
22174
22175 vec_alloc (used_rtx_array, 32);
22176
22177 if (!dwarf_split_debug_info)
22178 {
22179 debug_info_section = get_section (DEBUG_INFO_SECTION,
22180 SECTION_DEBUG, NULL);
22181 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22182 SECTION_DEBUG, NULL);
22183 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22184 SECTION_DEBUG, NULL);
22185 }
22186 else
22187 {
22188 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22189 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22190 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22191 SECTION_DEBUG | SECTION_EXCLUDE,
22192 NULL);
22193 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22194 SECTION_DEBUG, NULL);
22195 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22196 SECTION_DEBUG, NULL);
22197 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22198 SECTION_DEBUG, NULL);
22199 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22200 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22201
22202 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22203 the main .o, but the skeleton_line goes into the split off dwo. */
22204 debug_skeleton_line_section
22205 = get_section (DEBUG_DWO_LINE_SECTION,
22206 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22207 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22208 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22209 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22210 SECTION_DEBUG | SECTION_EXCLUDE,
22211 NULL);
22212 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22213 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22214 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22215 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22216 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22217 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22218 }
22219 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22220 SECTION_DEBUG, NULL);
22221 debug_macinfo_section = get_section (dwarf_strict
22222 ? DEBUG_MACINFO_SECTION
22223 : DEBUG_MACRO_SECTION,
22224 DEBUG_MACRO_SECTION_FLAGS, NULL);
22225 debug_line_section = get_section (DEBUG_LINE_SECTION,
22226 SECTION_DEBUG, NULL);
22227 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22228 SECTION_DEBUG, NULL);
22229 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22230 SECTION_DEBUG, NULL);
22231 debug_str_section = get_section (DEBUG_STR_SECTION,
22232 DEBUG_STR_SECTION_FLAGS, NULL);
22233 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22234 SECTION_DEBUG, NULL);
22235 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22236 SECTION_DEBUG, NULL);
22237
22238 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22239 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22240 DEBUG_ABBREV_SECTION_LABEL, 0);
22241 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22242 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22243 COLD_TEXT_SECTION_LABEL, 0);
22244 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22245
22246 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22247 DEBUG_INFO_SECTION_LABEL, 0);
22248 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22249 DEBUG_LINE_SECTION_LABEL, 0);
22250 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22251 DEBUG_RANGES_SECTION_LABEL, 0);
22252 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22253 DEBUG_ADDR_SECTION_LABEL, 0);
22254 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22255 dwarf_strict
22256 ? DEBUG_MACINFO_SECTION_LABEL
22257 : DEBUG_MACRO_SECTION_LABEL, 0);
22258 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22259
22260 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22261 vec_alloc (macinfo_table, 64);
22262
22263 switch_to_section (text_section);
22264 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22265
22266 /* Make sure the line number table for .text always exists. */
22267 text_section_line_info = new_line_info_table ();
22268 text_section_line_info->end_label = text_end_label;
22269 }
22270
22271 /* Called before compile () starts outputtting functions, variables
22272 and toplevel asms into assembly. */
22273
22274 static void
22275 dwarf2out_assembly_start (void)
22276 {
22277 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22278 && dwarf2out_do_cfi_asm ()
22279 && (!(flag_unwind_tables || flag_exceptions)
22280 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22281 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22282 }
22283
22284 /* A helper function for dwarf2out_finish called through
22285 htab_traverse. Assign a string its index. All strings must be
22286 collected into the table by the time index_string is called,
22287 because the indexing code relies on htab_traverse to traverse nodes
22288 in the same order for each run. */
22289
22290 static int
22291 index_string (void **h, void *v)
22292 {
22293 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22294 unsigned int *index = (unsigned int *) v;
22295
22296 find_string_form (node);
22297 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22298 {
22299 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22300 node->index = *index;
22301 *index += 1;
22302 }
22303 return 1;
22304 }
22305
22306 /* A helper function for output_indirect_strings called through
22307 htab_traverse. Output the offset to a string and update the
22308 current offset. */
22309
22310 static int
22311 output_index_string_offset (void **h, void *v)
22312 {
22313 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22314 unsigned int *offset = (unsigned int *) v;
22315
22316 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22317 {
22318 /* Assert that this node has been assigned an index. */
22319 gcc_assert (node->index != NO_INDEX_ASSIGNED
22320 && node->index != NOT_INDEXED);
22321 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22322 "indexed string 0x%x: %s", node->index, node->str);
22323 *offset += strlen (node->str) + 1;
22324 }
22325 return 1;
22326 }
22327
22328 /* A helper function for dwarf2out_finish called through
22329 htab_traverse. Output the indexed string. */
22330
22331 static int
22332 output_index_string (void **h, void *v)
22333 {
22334 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22335 unsigned int *cur_idx = (unsigned int *) v;
22336
22337 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22338 {
22339 /* Assert that the strings are output in the same order as their
22340 indexes were assigned. */
22341 gcc_assert (*cur_idx == node->index);
22342 assemble_string (node->str, strlen (node->str) + 1);
22343 *cur_idx += 1;
22344 }
22345 return 1;
22346 }
22347
22348 /* A helper function for dwarf2out_finish called through
22349 htab_traverse. Emit one queued .debug_str string. */
22350
22351 static int
22352 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22353 {
22354 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22355
22356 node->form = find_string_form (node);
22357 if (node->form == DW_FORM_strp && node->refcount > 0)
22358 {
22359 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22360 assemble_string (node->str, strlen (node->str) + 1);
22361 }
22362
22363 return 1;
22364 }
22365
22366 /* Output the indexed string table. */
22367
22368 static void
22369 output_indirect_strings (void)
22370 {
22371 switch_to_section (debug_str_section);
22372 if (!dwarf_split_debug_info)
22373 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22374 else
22375 {
22376 unsigned int offset = 0;
22377 unsigned int cur_idx = 0;
22378
22379 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22380
22381 switch_to_section (debug_str_offsets_section);
22382 htab_traverse_noresize (debug_str_hash,
22383 output_index_string_offset,
22384 &offset);
22385 switch_to_section (debug_str_dwo_section);
22386 htab_traverse_noresize (debug_str_hash,
22387 output_index_string,
22388 &cur_idx);
22389 }
22390 }
22391
22392 /* Callback for htab_traverse to assign an index to an entry in the
22393 table, and to write that entry to the .debug_addr section. */
22394
22395 static int
22396 output_addr_table_entry (void **slot, void *data)
22397 {
22398 addr_table_entry *entry = (addr_table_entry *) *slot;
22399 unsigned int *cur_index = (unsigned int *)data;
22400
22401 if (entry->refcount == 0)
22402 {
22403 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22404 || entry->index == NOT_INDEXED);
22405 return 1;
22406 }
22407
22408 gcc_assert (entry->index == *cur_index);
22409 (*cur_index)++;
22410
22411 switch (entry->kind)
22412 {
22413 case ate_kind_rtx:
22414 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22415 "0x%x", entry->index);
22416 break;
22417 case ate_kind_rtx_dtprel:
22418 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22419 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22420 DWARF2_ADDR_SIZE,
22421 entry->addr.rtl);
22422 fputc ('\n', asm_out_file);
22423 break;
22424 case ate_kind_label:
22425 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22426 "0x%x", entry->index);
22427 break;
22428 default:
22429 gcc_unreachable ();
22430 }
22431 return 1;
22432 }
22433
22434 /* Produce the .debug_addr section. */
22435
22436 static void
22437 output_addr_table (void)
22438 {
22439 unsigned int index = 0;
22440 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22441 return;
22442
22443 switch_to_section (debug_addr_section);
22444 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22445 }
22446
22447 #if ENABLE_ASSERT_CHECKING
22448 /* Verify that all marks are clear. */
22449
22450 static void
22451 verify_marks_clear (dw_die_ref die)
22452 {
22453 dw_die_ref c;
22454
22455 gcc_assert (! die->die_mark);
22456 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22457 }
22458 #endif /* ENABLE_ASSERT_CHECKING */
22459
22460 /* Clear the marks for a die and its children.
22461 Be cool if the mark isn't set. */
22462
22463 static void
22464 prune_unmark_dies (dw_die_ref die)
22465 {
22466 dw_die_ref c;
22467
22468 if (die->die_mark)
22469 die->die_mark = 0;
22470 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22471 }
22472
22473 /* Given DIE that we're marking as used, find any other dies
22474 it references as attributes and mark them as used. */
22475
22476 static void
22477 prune_unused_types_walk_attribs (dw_die_ref die)
22478 {
22479 dw_attr_ref a;
22480 unsigned ix;
22481
22482 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22483 {
22484 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22485 {
22486 /* A reference to another DIE.
22487 Make sure that it will get emitted.
22488 If it was broken out into a comdat group, don't follow it. */
22489 if (! AT_ref (a)->comdat_type_p
22490 || a->dw_attr == DW_AT_specification)
22491 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22492 }
22493 /* Set the string's refcount to 0 so that prune_unused_types_mark
22494 accounts properly for it. */
22495 if (AT_class (a) == dw_val_class_str)
22496 a->dw_attr_val.v.val_str->refcount = 0;
22497 }
22498 }
22499
22500 /* Mark the generic parameters and arguments children DIEs of DIE. */
22501
22502 static void
22503 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22504 {
22505 dw_die_ref c;
22506
22507 if (die == NULL || die->die_child == NULL)
22508 return;
22509 c = die->die_child;
22510 do
22511 {
22512 if (is_template_parameter (c))
22513 prune_unused_types_mark (c, 1);
22514 c = c->die_sib;
22515 } while (c && c != die->die_child);
22516 }
22517
22518 /* Mark DIE as being used. If DOKIDS is true, then walk down
22519 to DIE's children. */
22520
22521 static void
22522 prune_unused_types_mark (dw_die_ref die, int dokids)
22523 {
22524 dw_die_ref c;
22525
22526 if (die->die_mark == 0)
22527 {
22528 /* We haven't done this node yet. Mark it as used. */
22529 die->die_mark = 1;
22530 /* If this is the DIE of a generic type instantiation,
22531 mark the children DIEs that describe its generic parms and
22532 args. */
22533 prune_unused_types_mark_generic_parms_dies (die);
22534
22535 /* We also have to mark its parents as used.
22536 (But we don't want to mark our parent's kids due to this,
22537 unless it is a class.) */
22538 if (die->die_parent)
22539 prune_unused_types_mark (die->die_parent,
22540 class_scope_p (die->die_parent));
22541
22542 /* Mark any referenced nodes. */
22543 prune_unused_types_walk_attribs (die);
22544
22545 /* If this node is a specification,
22546 also mark the definition, if it exists. */
22547 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22548 prune_unused_types_mark (die->die_definition, 1);
22549 }
22550
22551 if (dokids && die->die_mark != 2)
22552 {
22553 /* We need to walk the children, but haven't done so yet.
22554 Remember that we've walked the kids. */
22555 die->die_mark = 2;
22556
22557 /* If this is an array type, we need to make sure our
22558 kids get marked, even if they're types. If we're
22559 breaking out types into comdat sections, do this
22560 for all type definitions. */
22561 if (die->die_tag == DW_TAG_array_type
22562 || (use_debug_types
22563 && is_type_die (die) && ! is_declaration_die (die)))
22564 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22565 else
22566 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22567 }
22568 }
22569
22570 /* For local classes, look if any static member functions were emitted
22571 and if so, mark them. */
22572
22573 static void
22574 prune_unused_types_walk_local_classes (dw_die_ref die)
22575 {
22576 dw_die_ref c;
22577
22578 if (die->die_mark == 2)
22579 return;
22580
22581 switch (die->die_tag)
22582 {
22583 case DW_TAG_structure_type:
22584 case DW_TAG_union_type:
22585 case DW_TAG_class_type:
22586 break;
22587
22588 case DW_TAG_subprogram:
22589 if (!get_AT_flag (die, DW_AT_declaration)
22590 || die->die_definition != NULL)
22591 prune_unused_types_mark (die, 1);
22592 return;
22593
22594 default:
22595 return;
22596 }
22597
22598 /* Mark children. */
22599 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22600 }
22601
22602 /* Walk the tree DIE and mark types that we actually use. */
22603
22604 static void
22605 prune_unused_types_walk (dw_die_ref die)
22606 {
22607 dw_die_ref c;
22608
22609 /* Don't do anything if this node is already marked and
22610 children have been marked as well. */
22611 if (die->die_mark == 2)
22612 return;
22613
22614 switch (die->die_tag)
22615 {
22616 case DW_TAG_structure_type:
22617 case DW_TAG_union_type:
22618 case DW_TAG_class_type:
22619 if (die->die_perennial_p)
22620 break;
22621
22622 for (c = die->die_parent; c; c = c->die_parent)
22623 if (c->die_tag == DW_TAG_subprogram)
22624 break;
22625
22626 /* Finding used static member functions inside of classes
22627 is needed just for local classes, because for other classes
22628 static member function DIEs with DW_AT_specification
22629 are emitted outside of the DW_TAG_*_type. If we ever change
22630 it, we'd need to call this even for non-local classes. */
22631 if (c)
22632 prune_unused_types_walk_local_classes (die);
22633
22634 /* It's a type node --- don't mark it. */
22635 return;
22636
22637 case DW_TAG_const_type:
22638 case DW_TAG_packed_type:
22639 case DW_TAG_pointer_type:
22640 case DW_TAG_reference_type:
22641 case DW_TAG_rvalue_reference_type:
22642 case DW_TAG_volatile_type:
22643 case DW_TAG_typedef:
22644 case DW_TAG_array_type:
22645 case DW_TAG_interface_type:
22646 case DW_TAG_friend:
22647 case DW_TAG_variant_part:
22648 case DW_TAG_enumeration_type:
22649 case DW_TAG_subroutine_type:
22650 case DW_TAG_string_type:
22651 case DW_TAG_set_type:
22652 case DW_TAG_subrange_type:
22653 case DW_TAG_ptr_to_member_type:
22654 case DW_TAG_file_type:
22655 if (die->die_perennial_p)
22656 break;
22657
22658 /* It's a type node --- don't mark it. */
22659 return;
22660
22661 default:
22662 /* Mark everything else. */
22663 break;
22664 }
22665
22666 if (die->die_mark == 0)
22667 {
22668 die->die_mark = 1;
22669
22670 /* Now, mark any dies referenced from here. */
22671 prune_unused_types_walk_attribs (die);
22672 }
22673
22674 die->die_mark = 2;
22675
22676 /* Mark children. */
22677 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22678 }
22679
22680 /* Increment the string counts on strings referred to from DIE's
22681 attributes. */
22682
22683 static void
22684 prune_unused_types_update_strings (dw_die_ref die)
22685 {
22686 dw_attr_ref a;
22687 unsigned ix;
22688
22689 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22690 if (AT_class (a) == dw_val_class_str)
22691 {
22692 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22693 s->refcount++;
22694 /* Avoid unnecessarily putting strings that are used less than
22695 twice in the hash table. */
22696 if (s->refcount
22697 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22698 {
22699 void ** slot;
22700 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22701 htab_hash_string (s->str),
22702 INSERT);
22703 gcc_assert (*slot == NULL);
22704 *slot = s;
22705 }
22706 }
22707 }
22708
22709 /* Remove from the tree DIE any dies that aren't marked. */
22710
22711 static void
22712 prune_unused_types_prune (dw_die_ref die)
22713 {
22714 dw_die_ref c;
22715
22716 gcc_assert (die->die_mark);
22717 prune_unused_types_update_strings (die);
22718
22719 if (! die->die_child)
22720 return;
22721
22722 c = die->die_child;
22723 do {
22724 dw_die_ref prev = c;
22725 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22726 if (c == die->die_child)
22727 {
22728 /* No marked children between 'prev' and the end of the list. */
22729 if (prev == c)
22730 /* No marked children at all. */
22731 die->die_child = NULL;
22732 else
22733 {
22734 prev->die_sib = c->die_sib;
22735 die->die_child = prev;
22736 }
22737 return;
22738 }
22739
22740 if (c != prev->die_sib)
22741 prev->die_sib = c;
22742 prune_unused_types_prune (c);
22743 } while (c != die->die_child);
22744 }
22745
22746 /* Remove dies representing declarations that we never use. */
22747
22748 static void
22749 prune_unused_types (void)
22750 {
22751 unsigned int i;
22752 limbo_die_node *node;
22753 comdat_type_node *ctnode;
22754 pubname_ref pub;
22755 dw_die_ref base_type;
22756
22757 #if ENABLE_ASSERT_CHECKING
22758 /* All the marks should already be clear. */
22759 verify_marks_clear (comp_unit_die ());
22760 for (node = limbo_die_list; node; node = node->next)
22761 verify_marks_clear (node->die);
22762 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22763 verify_marks_clear (ctnode->root_die);
22764 #endif /* ENABLE_ASSERT_CHECKING */
22765
22766 /* Mark types that are used in global variables. */
22767 premark_types_used_by_global_vars ();
22768
22769 /* Set the mark on nodes that are actually used. */
22770 prune_unused_types_walk (comp_unit_die ());
22771 for (node = limbo_die_list; node; node = node->next)
22772 prune_unused_types_walk (node->die);
22773 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22774 {
22775 prune_unused_types_walk (ctnode->root_die);
22776 prune_unused_types_mark (ctnode->type_die, 1);
22777 }
22778
22779 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22780 are unusual in that they are pubnames that are the children of pubtypes.
22781 They should only be marked via their parent DW_TAG_enumeration_type die,
22782 not as roots in themselves. */
22783 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22784 if (pub->die->die_tag != DW_TAG_enumerator)
22785 prune_unused_types_mark (pub->die, 1);
22786 for (i = 0; base_types.iterate (i, &base_type); i++)
22787 prune_unused_types_mark (base_type, 1);
22788
22789 if (debug_str_hash)
22790 htab_empty (debug_str_hash);
22791 if (skeleton_debug_str_hash)
22792 htab_empty (skeleton_debug_str_hash);
22793 prune_unused_types_prune (comp_unit_die ());
22794 for (node = limbo_die_list; node; node = node->next)
22795 prune_unused_types_prune (node->die);
22796 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22797 prune_unused_types_prune (ctnode->root_die);
22798
22799 /* Leave the marks clear. */
22800 prune_unmark_dies (comp_unit_die ());
22801 for (node = limbo_die_list; node; node = node->next)
22802 prune_unmark_dies (node->die);
22803 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22804 prune_unmark_dies (ctnode->root_die);
22805 }
22806
22807 /* Set the parameter to true if there are any relative pathnames in
22808 the file table. */
22809 static int
22810 file_table_relative_p (void ** slot, void *param)
22811 {
22812 bool *p = (bool *) param;
22813 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22814 if (!IS_ABSOLUTE_PATH (d->filename))
22815 {
22816 *p = true;
22817 return 0;
22818 }
22819 return 1;
22820 }
22821
22822 /* Helpers to manipulate hash table of comdat type units. */
22823
22824 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22825 {
22826 typedef comdat_type_node value_type;
22827 typedef comdat_type_node compare_type;
22828 static inline hashval_t hash (const value_type *);
22829 static inline bool equal (const value_type *, const compare_type *);
22830 };
22831
22832 inline hashval_t
22833 comdat_type_hasher::hash (const value_type *type_node)
22834 {
22835 hashval_t h;
22836 memcpy (&h, type_node->signature, sizeof (h));
22837 return h;
22838 }
22839
22840 inline bool
22841 comdat_type_hasher::equal (const value_type *type_node_1,
22842 const compare_type *type_node_2)
22843 {
22844 return (! memcmp (type_node_1->signature, type_node_2->signature,
22845 DWARF_TYPE_SIGNATURE_SIZE));
22846 }
22847
22848 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22849 to the location it would have been added, should we know its
22850 DECL_ASSEMBLER_NAME when we added other attributes. This will
22851 probably improve compactness of debug info, removing equivalent
22852 abbrevs, and hide any differences caused by deferring the
22853 computation of the assembler name, triggered by e.g. PCH. */
22854
22855 static inline void
22856 move_linkage_attr (dw_die_ref die)
22857 {
22858 unsigned ix = vec_safe_length (die->die_attr);
22859 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22860
22861 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22862 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22863
22864 while (--ix > 0)
22865 {
22866 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22867
22868 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22869 break;
22870 }
22871
22872 if (ix != vec_safe_length (die->die_attr) - 1)
22873 {
22874 die->die_attr->pop ();
22875 die->die_attr->quick_insert (ix, linkage);
22876 }
22877 }
22878
22879 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22880 referenced from typed stack ops and count how often they are used. */
22881
22882 static void
22883 mark_base_types (dw_loc_descr_ref loc)
22884 {
22885 dw_die_ref base_type = NULL;
22886
22887 for (; loc; loc = loc->dw_loc_next)
22888 {
22889 switch (loc->dw_loc_opc)
22890 {
22891 case DW_OP_GNU_regval_type:
22892 case DW_OP_GNU_deref_type:
22893 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22894 break;
22895 case DW_OP_GNU_convert:
22896 case DW_OP_GNU_reinterpret:
22897 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22898 continue;
22899 /* FALLTHRU */
22900 case DW_OP_GNU_const_type:
22901 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22902 break;
22903 case DW_OP_GNU_entry_value:
22904 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22905 continue;
22906 default:
22907 continue;
22908 }
22909 gcc_assert (base_type->die_parent == comp_unit_die ());
22910 if (base_type->die_mark)
22911 base_type->die_mark++;
22912 else
22913 {
22914 base_types.safe_push (base_type);
22915 base_type->die_mark = 1;
22916 }
22917 }
22918 }
22919
22920 /* Comparison function for sorting marked base types. */
22921
22922 static int
22923 base_type_cmp (const void *x, const void *y)
22924 {
22925 dw_die_ref dx = *(const dw_die_ref *) x;
22926 dw_die_ref dy = *(const dw_die_ref *) y;
22927 unsigned int byte_size1, byte_size2;
22928 unsigned int encoding1, encoding2;
22929 if (dx->die_mark > dy->die_mark)
22930 return -1;
22931 if (dx->die_mark < dy->die_mark)
22932 return 1;
22933 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22934 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22935 if (byte_size1 < byte_size2)
22936 return 1;
22937 if (byte_size1 > byte_size2)
22938 return -1;
22939 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22940 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22941 if (encoding1 < encoding2)
22942 return 1;
22943 if (encoding1 > encoding2)
22944 return -1;
22945 return 0;
22946 }
22947
22948 /* Move base types marked by mark_base_types as early as possible
22949 in the CU, sorted by decreasing usage count both to make the
22950 uleb128 references as small as possible and to make sure they
22951 will have die_offset already computed by calc_die_sizes when
22952 sizes of typed stack loc ops is computed. */
22953
22954 static void
22955 move_marked_base_types (void)
22956 {
22957 unsigned int i;
22958 dw_die_ref base_type, die, c;
22959
22960 if (base_types.is_empty ())
22961 return;
22962
22963 /* Sort by decreasing usage count, they will be added again in that
22964 order later on. */
22965 base_types.qsort (base_type_cmp);
22966 die = comp_unit_die ();
22967 c = die->die_child;
22968 do
22969 {
22970 dw_die_ref prev = c;
22971 c = c->die_sib;
22972 while (c->die_mark)
22973 {
22974 remove_child_with_prev (c, prev);
22975 /* As base types got marked, there must be at least
22976 one node other than DW_TAG_base_type. */
22977 gcc_assert (c != c->die_sib);
22978 c = c->die_sib;
22979 }
22980 }
22981 while (c != die->die_child);
22982 gcc_assert (die->die_child);
22983 c = die->die_child;
22984 for (i = 0; base_types.iterate (i, &base_type); i++)
22985 {
22986 base_type->die_mark = 0;
22987 base_type->die_sib = c->die_sib;
22988 c->die_sib = base_type;
22989 c = base_type;
22990 }
22991 }
22992
22993 /* Helper function for resolve_addr, attempt to resolve
22994 one CONST_STRING, return non-zero if not successful. Similarly verify that
22995 SYMBOL_REFs refer to variables emitted in the current CU. */
22996
22997 static int
22998 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22999 {
23000 rtx rtl = *addr;
23001
23002 if (GET_CODE (rtl) == CONST_STRING)
23003 {
23004 size_t len = strlen (XSTR (rtl, 0)) + 1;
23005 tree t = build_string (len, XSTR (rtl, 0));
23006 tree tlen = size_int (len - 1);
23007 TREE_TYPE (t)
23008 = build_array_type (char_type_node, build_index_type (tlen));
23009 rtl = lookup_constant_def (t);
23010 if (!rtl || !MEM_P (rtl))
23011 return 1;
23012 rtl = XEXP (rtl, 0);
23013 if (GET_CODE (rtl) == SYMBOL_REF
23014 && SYMBOL_REF_DECL (rtl)
23015 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23016 return 1;
23017 vec_safe_push (used_rtx_array, rtl);
23018 *addr = rtl;
23019 return 0;
23020 }
23021
23022 if (GET_CODE (rtl) == SYMBOL_REF
23023 && SYMBOL_REF_DECL (rtl))
23024 {
23025 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23026 {
23027 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23028 return 1;
23029 }
23030 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23031 return 1;
23032 }
23033
23034 if (GET_CODE (rtl) == CONST
23035 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
23036 return 1;
23037
23038 return 0;
23039 }
23040
23041 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23042 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23043 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23044
23045 static rtx
23046 string_cst_pool_decl (tree t)
23047 {
23048 rtx rtl = output_constant_def (t, 1);
23049 unsigned char *array;
23050 dw_loc_descr_ref l;
23051 tree decl;
23052 size_t len;
23053 dw_die_ref ref;
23054
23055 if (!rtl || !MEM_P (rtl))
23056 return NULL_RTX;
23057 rtl = XEXP (rtl, 0);
23058 if (GET_CODE (rtl) != SYMBOL_REF
23059 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23060 return NULL_RTX;
23061
23062 decl = SYMBOL_REF_DECL (rtl);
23063 if (!lookup_decl_die (decl))
23064 {
23065 len = TREE_STRING_LENGTH (t);
23066 vec_safe_push (used_rtx_array, rtl);
23067 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23068 array = (unsigned char *) ggc_alloc_atomic (len);
23069 memcpy (array, TREE_STRING_POINTER (t), len);
23070 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23071 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23072 l->dw_loc_oprnd2.v.val_vec.length = len;
23073 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23074 l->dw_loc_oprnd2.v.val_vec.array = array;
23075 add_AT_loc (ref, DW_AT_location, l);
23076 equate_decl_number_to_die (decl, ref);
23077 }
23078 return rtl;
23079 }
23080
23081 /* Helper function of resolve_addr_in_expr. LOC is
23082 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23083 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23084 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23085 with DW_OP_GNU_implicit_pointer if possible
23086 and return true, if unsuccessful, return false. */
23087
23088 static bool
23089 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23090 {
23091 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23092 HOST_WIDE_INT offset = 0;
23093 dw_die_ref ref = NULL;
23094 tree decl;
23095
23096 if (GET_CODE (rtl) == CONST
23097 && GET_CODE (XEXP (rtl, 0)) == PLUS
23098 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23099 {
23100 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23101 rtl = XEXP (XEXP (rtl, 0), 0);
23102 }
23103 if (GET_CODE (rtl) == CONST_STRING)
23104 {
23105 size_t len = strlen (XSTR (rtl, 0)) + 1;
23106 tree t = build_string (len, XSTR (rtl, 0));
23107 tree tlen = size_int (len - 1);
23108
23109 TREE_TYPE (t)
23110 = build_array_type (char_type_node, build_index_type (tlen));
23111 rtl = string_cst_pool_decl (t);
23112 if (!rtl)
23113 return false;
23114 }
23115 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23116 {
23117 decl = SYMBOL_REF_DECL (rtl);
23118 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23119 {
23120 ref = lookup_decl_die (decl);
23121 if (ref && (get_AT (ref, DW_AT_location)
23122 || get_AT (ref, DW_AT_const_value)))
23123 {
23124 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23125 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23126 loc->dw_loc_oprnd1.val_entry = NULL;
23127 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23128 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23129 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23130 loc->dw_loc_oprnd2.v.val_int = offset;
23131 return true;
23132 }
23133 }
23134 }
23135 return false;
23136 }
23137
23138 /* Helper function for resolve_addr, handle one location
23139 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23140 the location list couldn't be resolved. */
23141
23142 static bool
23143 resolve_addr_in_expr (dw_loc_descr_ref loc)
23144 {
23145 dw_loc_descr_ref keep = NULL;
23146 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23147 switch (loc->dw_loc_opc)
23148 {
23149 case DW_OP_addr:
23150 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23151 {
23152 if ((prev == NULL
23153 || prev->dw_loc_opc == DW_OP_piece
23154 || prev->dw_loc_opc == DW_OP_bit_piece)
23155 && loc->dw_loc_next
23156 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23157 && !dwarf_strict
23158 && optimize_one_addr_into_implicit_ptr (loc))
23159 break;
23160 return false;
23161 }
23162 break;
23163 case DW_OP_GNU_addr_index:
23164 case DW_OP_GNU_const_index:
23165 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
23166 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23167 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
23168 NULL))
23169 return false;
23170 break;
23171 case DW_OP_const4u:
23172 case DW_OP_const8u:
23173 if (loc->dtprel
23174 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23175 return false;
23176 break;
23177 case DW_OP_plus_uconst:
23178 if (size_of_loc_descr (loc)
23179 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23180 + 1
23181 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23182 {
23183 dw_loc_descr_ref repl
23184 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23185 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23186 add_loc_descr (&repl, loc->dw_loc_next);
23187 *loc = *repl;
23188 }
23189 break;
23190 case DW_OP_implicit_value:
23191 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23192 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23193 return false;
23194 break;
23195 case DW_OP_GNU_implicit_pointer:
23196 case DW_OP_GNU_parameter_ref:
23197 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23198 {
23199 dw_die_ref ref
23200 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23201 if (ref == NULL)
23202 return false;
23203 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23204 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23205 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23206 }
23207 break;
23208 case DW_OP_GNU_const_type:
23209 case DW_OP_GNU_regval_type:
23210 case DW_OP_GNU_deref_type:
23211 case DW_OP_GNU_convert:
23212 case DW_OP_GNU_reinterpret:
23213 while (loc->dw_loc_next
23214 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23215 {
23216 dw_die_ref base1, base2;
23217 unsigned enc1, enc2, size1, size2;
23218 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23219 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23220 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23221 else if (loc->dw_loc_oprnd1.val_class
23222 == dw_val_class_unsigned_const)
23223 break;
23224 else
23225 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23226 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23227 == dw_val_class_unsigned_const)
23228 break;
23229 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23230 gcc_assert (base1->die_tag == DW_TAG_base_type
23231 && base2->die_tag == DW_TAG_base_type);
23232 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23233 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23234 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23235 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23236 if (size1 == size2
23237 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23238 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23239 && loc != keep)
23240 || enc1 == enc2))
23241 {
23242 /* Optimize away next DW_OP_GNU_convert after
23243 adjusting LOC's base type die reference. */
23244 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23245 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23246 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23247 else
23248 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23249 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23250 continue;
23251 }
23252 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23253 point typed stack entry. */
23254 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23255 keep = loc->dw_loc_next;
23256 break;
23257 }
23258 break;
23259 default:
23260 break;
23261 }
23262 return true;
23263 }
23264
23265 /* Helper function of resolve_addr. DIE had DW_AT_location of
23266 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23267 and DW_OP_addr couldn't be resolved. resolve_addr has already
23268 removed the DW_AT_location attribute. This function attempts to
23269 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23270 to it or DW_AT_const_value attribute, if possible. */
23271
23272 static void
23273 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23274 {
23275 if (TREE_CODE (decl) != VAR_DECL
23276 || lookup_decl_die (decl) != die
23277 || DECL_EXTERNAL (decl)
23278 || !TREE_STATIC (decl)
23279 || DECL_INITIAL (decl) == NULL_TREE
23280 || DECL_P (DECL_INITIAL (decl))
23281 || get_AT (die, DW_AT_const_value))
23282 return;
23283
23284 tree init = DECL_INITIAL (decl);
23285 HOST_WIDE_INT offset = 0;
23286 /* For variables that have been optimized away and thus
23287 don't have a memory location, see if we can emit
23288 DW_AT_const_value instead. */
23289 if (tree_add_const_value_attribute (die, init))
23290 return;
23291 if (dwarf_strict)
23292 return;
23293 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23294 and ADDR_EXPR refers to a decl that has DW_AT_location or
23295 DW_AT_const_value (but isn't addressable, otherwise
23296 resolving the original DW_OP_addr wouldn't fail), see if
23297 we can add DW_OP_GNU_implicit_pointer. */
23298 STRIP_NOPS (init);
23299 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23300 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23301 {
23302 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23303 init = TREE_OPERAND (init, 0);
23304 STRIP_NOPS (init);
23305 }
23306 if (TREE_CODE (init) != ADDR_EXPR)
23307 return;
23308 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23309 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23310 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23311 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23312 && TREE_OPERAND (init, 0) != decl))
23313 {
23314 dw_die_ref ref;
23315 dw_loc_descr_ref l;
23316
23317 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23318 {
23319 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23320 if (!rtl)
23321 return;
23322 decl = SYMBOL_REF_DECL (rtl);
23323 }
23324 else
23325 decl = TREE_OPERAND (init, 0);
23326 ref = lookup_decl_die (decl);
23327 if (ref == NULL
23328 || (!get_AT (ref, DW_AT_location)
23329 && !get_AT (ref, DW_AT_const_value)))
23330 return;
23331 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23332 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23333 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23334 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23335 add_AT_loc (die, DW_AT_location, l);
23336 }
23337 }
23338
23339 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23340 an address in .rodata section if the string literal is emitted there,
23341 or remove the containing location list or replace DW_AT_const_value
23342 with DW_AT_location and empty location expression, if it isn't found
23343 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23344 to something that has been emitted in the current CU. */
23345
23346 static void
23347 resolve_addr (dw_die_ref die)
23348 {
23349 dw_die_ref c;
23350 dw_attr_ref a;
23351 dw_loc_list_ref *curr, *start, loc;
23352 unsigned ix;
23353
23354 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23355 switch (AT_class (a))
23356 {
23357 case dw_val_class_loc_list:
23358 start = curr = AT_loc_list_ptr (a);
23359 loc = *curr;
23360 gcc_assert (loc);
23361 /* The same list can be referenced more than once. See if we have
23362 already recorded the result from a previous pass. */
23363 if (loc->replaced)
23364 *curr = loc->dw_loc_next;
23365 else if (!loc->resolved_addr)
23366 {
23367 /* As things stand, we do not expect or allow one die to
23368 reference a suffix of another die's location list chain.
23369 References must be identical or completely separate.
23370 There is therefore no need to cache the result of this
23371 pass on any list other than the first; doing so
23372 would lead to unnecessary writes. */
23373 while (*curr)
23374 {
23375 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23376 if (!resolve_addr_in_expr ((*curr)->expr))
23377 {
23378 dw_loc_list_ref next = (*curr)->dw_loc_next;
23379 dw_loc_descr_ref l = (*curr)->expr;
23380
23381 if (next && (*curr)->ll_symbol)
23382 {
23383 gcc_assert (!next->ll_symbol);
23384 next->ll_symbol = (*curr)->ll_symbol;
23385 }
23386 if (dwarf_split_debug_info)
23387 remove_loc_list_addr_table_entries (l);
23388 *curr = next;
23389 }
23390 else
23391 {
23392 mark_base_types ((*curr)->expr);
23393 curr = &(*curr)->dw_loc_next;
23394 }
23395 }
23396 if (loc == *start)
23397 loc->resolved_addr = 1;
23398 else
23399 {
23400 loc->replaced = 1;
23401 loc->dw_loc_next = *start;
23402 }
23403 }
23404 if (!*start)
23405 {
23406 remove_AT (die, a->dw_attr);
23407 ix--;
23408 }
23409 break;
23410 case dw_val_class_loc:
23411 {
23412 dw_loc_descr_ref l = AT_loc (a);
23413 /* For -gdwarf-2 don't attempt to optimize
23414 DW_AT_data_member_location containing
23415 DW_OP_plus_uconst - older consumers might
23416 rely on it being that op instead of a more complex,
23417 but shorter, location description. */
23418 if ((dwarf_version > 2
23419 || a->dw_attr != DW_AT_data_member_location
23420 || l == NULL
23421 || l->dw_loc_opc != DW_OP_plus_uconst
23422 || l->dw_loc_next != NULL)
23423 && !resolve_addr_in_expr (l))
23424 {
23425 if (dwarf_split_debug_info)
23426 remove_loc_list_addr_table_entries (l);
23427 if (l != NULL
23428 && l->dw_loc_next == NULL
23429 && l->dw_loc_opc == DW_OP_addr
23430 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23431 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23432 && a->dw_attr == DW_AT_location)
23433 {
23434 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23435 remove_AT (die, a->dw_attr);
23436 ix--;
23437 optimize_location_into_implicit_ptr (die, decl);
23438 break;
23439 }
23440 remove_AT (die, a->dw_attr);
23441 ix--;
23442 }
23443 else
23444 mark_base_types (l);
23445 }
23446 break;
23447 case dw_val_class_addr:
23448 if (a->dw_attr == DW_AT_const_value
23449 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23450 {
23451 if (AT_index (a) != NOT_INDEXED)
23452 remove_addr_table_entry (a->dw_attr_val.val_entry);
23453 remove_AT (die, a->dw_attr);
23454 ix--;
23455 }
23456 if (die->die_tag == DW_TAG_GNU_call_site
23457 && a->dw_attr == DW_AT_abstract_origin)
23458 {
23459 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23460 dw_die_ref tdie = lookup_decl_die (tdecl);
23461 if (tdie == NULL
23462 && DECL_EXTERNAL (tdecl)
23463 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23464 {
23465 force_decl_die (tdecl);
23466 tdie = lookup_decl_die (tdecl);
23467 }
23468 if (tdie)
23469 {
23470 a->dw_attr_val.val_class = dw_val_class_die_ref;
23471 a->dw_attr_val.v.val_die_ref.die = tdie;
23472 a->dw_attr_val.v.val_die_ref.external = 0;
23473 }
23474 else
23475 {
23476 if (AT_index (a) != NOT_INDEXED)
23477 remove_addr_table_entry (a->dw_attr_val.val_entry);
23478 remove_AT (die, a->dw_attr);
23479 ix--;
23480 }
23481 }
23482 break;
23483 default:
23484 break;
23485 }
23486
23487 FOR_EACH_CHILD (die, c, resolve_addr (c));
23488 }
23489 \f
23490 /* Helper routines for optimize_location_lists.
23491 This pass tries to share identical local lists in .debug_loc
23492 section. */
23493
23494 /* Iteratively hash operands of LOC opcode. */
23495
23496 static hashval_t
23497 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23498 {
23499 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23500 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23501
23502 switch (loc->dw_loc_opc)
23503 {
23504 case DW_OP_const4u:
23505 case DW_OP_const8u:
23506 if (loc->dtprel)
23507 goto hash_addr;
23508 /* FALLTHRU */
23509 case DW_OP_const1u:
23510 case DW_OP_const1s:
23511 case DW_OP_const2u:
23512 case DW_OP_const2s:
23513 case DW_OP_const4s:
23514 case DW_OP_const8s:
23515 case DW_OP_constu:
23516 case DW_OP_consts:
23517 case DW_OP_pick:
23518 case DW_OP_plus_uconst:
23519 case DW_OP_breg0:
23520 case DW_OP_breg1:
23521 case DW_OP_breg2:
23522 case DW_OP_breg3:
23523 case DW_OP_breg4:
23524 case DW_OP_breg5:
23525 case DW_OP_breg6:
23526 case DW_OP_breg7:
23527 case DW_OP_breg8:
23528 case DW_OP_breg9:
23529 case DW_OP_breg10:
23530 case DW_OP_breg11:
23531 case DW_OP_breg12:
23532 case DW_OP_breg13:
23533 case DW_OP_breg14:
23534 case DW_OP_breg15:
23535 case DW_OP_breg16:
23536 case DW_OP_breg17:
23537 case DW_OP_breg18:
23538 case DW_OP_breg19:
23539 case DW_OP_breg20:
23540 case DW_OP_breg21:
23541 case DW_OP_breg22:
23542 case DW_OP_breg23:
23543 case DW_OP_breg24:
23544 case DW_OP_breg25:
23545 case DW_OP_breg26:
23546 case DW_OP_breg27:
23547 case DW_OP_breg28:
23548 case DW_OP_breg29:
23549 case DW_OP_breg30:
23550 case DW_OP_breg31:
23551 case DW_OP_regx:
23552 case DW_OP_fbreg:
23553 case DW_OP_piece:
23554 case DW_OP_deref_size:
23555 case DW_OP_xderef_size:
23556 hash = iterative_hash_object (val1->v.val_int, hash);
23557 break;
23558 case DW_OP_skip:
23559 case DW_OP_bra:
23560 {
23561 int offset;
23562
23563 gcc_assert (val1->val_class == dw_val_class_loc);
23564 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23565 hash = iterative_hash_object (offset, hash);
23566 }
23567 break;
23568 case DW_OP_implicit_value:
23569 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23570 switch (val2->val_class)
23571 {
23572 case dw_val_class_const:
23573 hash = iterative_hash_object (val2->v.val_int, hash);
23574 break;
23575 case dw_val_class_vec:
23576 {
23577 unsigned int elt_size = val2->v.val_vec.elt_size;
23578 unsigned int len = val2->v.val_vec.length;
23579
23580 hash = iterative_hash_object (elt_size, hash);
23581 hash = iterative_hash_object (len, hash);
23582 hash = iterative_hash (val2->v.val_vec.array,
23583 len * elt_size, hash);
23584 }
23585 break;
23586 case dw_val_class_const_double:
23587 hash = iterative_hash_object (val2->v.val_double.low, hash);
23588 hash = iterative_hash_object (val2->v.val_double.high, hash);
23589 break;
23590 case dw_val_class_wide_int:
23591 hash = iterative_hash_object (*val2->v.val_wide, hash);
23592 break;
23593 case dw_val_class_addr:
23594 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23595 break;
23596 default:
23597 gcc_unreachable ();
23598 }
23599 break;
23600 case DW_OP_bregx:
23601 case DW_OP_bit_piece:
23602 hash = iterative_hash_object (val1->v.val_int, hash);
23603 hash = iterative_hash_object (val2->v.val_int, hash);
23604 break;
23605 case DW_OP_addr:
23606 hash_addr:
23607 if (loc->dtprel)
23608 {
23609 unsigned char dtprel = 0xd1;
23610 hash = iterative_hash_object (dtprel, hash);
23611 }
23612 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23613 break;
23614 case DW_OP_GNU_addr_index:
23615 case DW_OP_GNU_const_index:
23616 {
23617 if (loc->dtprel)
23618 {
23619 unsigned char dtprel = 0xd1;
23620 hash = iterative_hash_object (dtprel, hash);
23621 }
23622 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23623 }
23624 break;
23625 case DW_OP_GNU_implicit_pointer:
23626 hash = iterative_hash_object (val2->v.val_int, hash);
23627 break;
23628 case DW_OP_GNU_entry_value:
23629 hash = hash_loc_operands (val1->v.val_loc, hash);
23630 break;
23631 case DW_OP_GNU_regval_type:
23632 case DW_OP_GNU_deref_type:
23633 {
23634 unsigned int byte_size
23635 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23636 unsigned int encoding
23637 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23638 hash = iterative_hash_object (val1->v.val_int, hash);
23639 hash = iterative_hash_object (byte_size, hash);
23640 hash = iterative_hash_object (encoding, hash);
23641 }
23642 break;
23643 case DW_OP_GNU_convert:
23644 case DW_OP_GNU_reinterpret:
23645 if (val1->val_class == dw_val_class_unsigned_const)
23646 {
23647 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23648 break;
23649 }
23650 /* FALLTHRU */
23651 case DW_OP_GNU_const_type:
23652 {
23653 unsigned int byte_size
23654 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23655 unsigned int encoding
23656 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23657 hash = iterative_hash_object (byte_size, hash);
23658 hash = iterative_hash_object (encoding, hash);
23659 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23660 break;
23661 hash = iterative_hash_object (val2->val_class, hash);
23662 switch (val2->val_class)
23663 {
23664 case dw_val_class_const:
23665 hash = iterative_hash_object (val2->v.val_int, hash);
23666 break;
23667 case dw_val_class_vec:
23668 {
23669 unsigned int elt_size = val2->v.val_vec.elt_size;
23670 unsigned int len = val2->v.val_vec.length;
23671
23672 hash = iterative_hash_object (elt_size, hash);
23673 hash = iterative_hash_object (len, hash);
23674 hash = iterative_hash (val2->v.val_vec.array,
23675 len * elt_size, hash);
23676 }
23677 break;
23678 case dw_val_class_const_double:
23679 hash = iterative_hash_object (val2->v.val_double.low, hash);
23680 hash = iterative_hash_object (val2->v.val_double.high, hash);
23681 break;
23682 case dw_val_class_wide_int:
23683 hash = iterative_hash_object (*val2->v.val_wide, hash);
23684 break;
23685 default:
23686 gcc_unreachable ();
23687 }
23688 }
23689 break;
23690
23691 default:
23692 /* Other codes have no operands. */
23693 break;
23694 }
23695 return hash;
23696 }
23697
23698 /* Iteratively hash the whole DWARF location expression LOC. */
23699
23700 static inline hashval_t
23701 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23702 {
23703 dw_loc_descr_ref l;
23704 bool sizes_computed = false;
23705 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23706 size_of_locs (loc);
23707
23708 for (l = loc; l != NULL; l = l->dw_loc_next)
23709 {
23710 enum dwarf_location_atom opc = l->dw_loc_opc;
23711 hash = iterative_hash_object (opc, hash);
23712 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23713 {
23714 size_of_locs (loc);
23715 sizes_computed = true;
23716 }
23717 hash = hash_loc_operands (l, hash);
23718 }
23719 return hash;
23720 }
23721
23722 /* Compute hash of the whole location list LIST_HEAD. */
23723
23724 static inline void
23725 hash_loc_list (dw_loc_list_ref list_head)
23726 {
23727 dw_loc_list_ref curr = list_head;
23728 hashval_t hash = 0;
23729
23730 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23731 {
23732 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23733 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23734 if (curr->section)
23735 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23736 hash);
23737 hash = hash_locs (curr->expr, hash);
23738 }
23739 list_head->hash = hash;
23740 }
23741
23742 /* Return true if X and Y opcodes have the same operands. */
23743
23744 static inline bool
23745 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23746 {
23747 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23748 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23749 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23750 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23751
23752 switch (x->dw_loc_opc)
23753 {
23754 case DW_OP_const4u:
23755 case DW_OP_const8u:
23756 if (x->dtprel)
23757 goto hash_addr;
23758 /* FALLTHRU */
23759 case DW_OP_const1u:
23760 case DW_OP_const1s:
23761 case DW_OP_const2u:
23762 case DW_OP_const2s:
23763 case DW_OP_const4s:
23764 case DW_OP_const8s:
23765 case DW_OP_constu:
23766 case DW_OP_consts:
23767 case DW_OP_pick:
23768 case DW_OP_plus_uconst:
23769 case DW_OP_breg0:
23770 case DW_OP_breg1:
23771 case DW_OP_breg2:
23772 case DW_OP_breg3:
23773 case DW_OP_breg4:
23774 case DW_OP_breg5:
23775 case DW_OP_breg6:
23776 case DW_OP_breg7:
23777 case DW_OP_breg8:
23778 case DW_OP_breg9:
23779 case DW_OP_breg10:
23780 case DW_OP_breg11:
23781 case DW_OP_breg12:
23782 case DW_OP_breg13:
23783 case DW_OP_breg14:
23784 case DW_OP_breg15:
23785 case DW_OP_breg16:
23786 case DW_OP_breg17:
23787 case DW_OP_breg18:
23788 case DW_OP_breg19:
23789 case DW_OP_breg20:
23790 case DW_OP_breg21:
23791 case DW_OP_breg22:
23792 case DW_OP_breg23:
23793 case DW_OP_breg24:
23794 case DW_OP_breg25:
23795 case DW_OP_breg26:
23796 case DW_OP_breg27:
23797 case DW_OP_breg28:
23798 case DW_OP_breg29:
23799 case DW_OP_breg30:
23800 case DW_OP_breg31:
23801 case DW_OP_regx:
23802 case DW_OP_fbreg:
23803 case DW_OP_piece:
23804 case DW_OP_deref_size:
23805 case DW_OP_xderef_size:
23806 return valx1->v.val_int == valy1->v.val_int;
23807 case DW_OP_skip:
23808 case DW_OP_bra:
23809 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23810 can cause irrelevant differences in dw_loc_addr. */
23811 gcc_assert (valx1->val_class == dw_val_class_loc
23812 && valy1->val_class == dw_val_class_loc
23813 && (dwarf_split_debug_info
23814 || x->dw_loc_addr == y->dw_loc_addr));
23815 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23816 case DW_OP_implicit_value:
23817 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23818 || valx2->val_class != valy2->val_class)
23819 return false;
23820 switch (valx2->val_class)
23821 {
23822 case dw_val_class_const:
23823 return valx2->v.val_int == valy2->v.val_int;
23824 case dw_val_class_vec:
23825 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23826 && valx2->v.val_vec.length == valy2->v.val_vec.length
23827 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23828 valx2->v.val_vec.elt_size
23829 * valx2->v.val_vec.length) == 0;
23830 case dw_val_class_const_double:
23831 return valx2->v.val_double.low == valy2->v.val_double.low
23832 && valx2->v.val_double.high == valy2->v.val_double.high;
23833 case dw_val_class_wide_int:
23834 return *valx2->v.val_wide == *valy2->v.val_wide;
23835 case dw_val_class_addr:
23836 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23837 default:
23838 gcc_unreachable ();
23839 }
23840 case DW_OP_bregx:
23841 case DW_OP_bit_piece:
23842 return valx1->v.val_int == valy1->v.val_int
23843 && valx2->v.val_int == valy2->v.val_int;
23844 case DW_OP_addr:
23845 hash_addr:
23846 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23847 case DW_OP_GNU_addr_index:
23848 case DW_OP_GNU_const_index:
23849 {
23850 rtx ax1 = valx1->val_entry->addr.rtl;
23851 rtx ay1 = valy1->val_entry->addr.rtl;
23852 return rtx_equal_p (ax1, ay1);
23853 }
23854 case DW_OP_GNU_implicit_pointer:
23855 return valx1->val_class == dw_val_class_die_ref
23856 && valx1->val_class == valy1->val_class
23857 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23858 && valx2->v.val_int == valy2->v.val_int;
23859 case DW_OP_GNU_entry_value:
23860 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23861 case DW_OP_GNU_const_type:
23862 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23863 || valx2->val_class != valy2->val_class)
23864 return false;
23865 switch (valx2->val_class)
23866 {
23867 case dw_val_class_const:
23868 return valx2->v.val_int == valy2->v.val_int;
23869 case dw_val_class_vec:
23870 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23871 && valx2->v.val_vec.length == valy2->v.val_vec.length
23872 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23873 valx2->v.val_vec.elt_size
23874 * valx2->v.val_vec.length) == 0;
23875 case dw_val_class_const_double:
23876 return valx2->v.val_double.low == valy2->v.val_double.low
23877 && valx2->v.val_double.high == valy2->v.val_double.high;
23878 case dw_val_class_wide_int:
23879 return *valx2->v.val_wide == *valy2->v.val_wide;
23880 default:
23881 gcc_unreachable ();
23882 }
23883 case DW_OP_GNU_regval_type:
23884 case DW_OP_GNU_deref_type:
23885 return valx1->v.val_int == valy1->v.val_int
23886 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23887 case DW_OP_GNU_convert:
23888 case DW_OP_GNU_reinterpret:
23889 if (valx1->val_class != valy1->val_class)
23890 return false;
23891 if (valx1->val_class == dw_val_class_unsigned_const)
23892 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23893 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23894 case DW_OP_GNU_parameter_ref:
23895 return valx1->val_class == dw_val_class_die_ref
23896 && valx1->val_class == valy1->val_class
23897 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23898 default:
23899 /* Other codes have no operands. */
23900 return true;
23901 }
23902 }
23903
23904 /* Return true if DWARF location expressions X and Y are the same. */
23905
23906 static inline bool
23907 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23908 {
23909 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23910 if (x->dw_loc_opc != y->dw_loc_opc
23911 || x->dtprel != y->dtprel
23912 || !compare_loc_operands (x, y))
23913 break;
23914 return x == NULL && y == NULL;
23915 }
23916
23917 /* Hashtable helpers. */
23918
23919 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23920 {
23921 typedef dw_loc_list_struct value_type;
23922 typedef dw_loc_list_struct compare_type;
23923 static inline hashval_t hash (const value_type *);
23924 static inline bool equal (const value_type *, const compare_type *);
23925 };
23926
23927 /* Return precomputed hash of location list X. */
23928
23929 inline hashval_t
23930 loc_list_hasher::hash (const value_type *x)
23931 {
23932 return x->hash;
23933 }
23934
23935 /* Return true if location lists A and B are the same. */
23936
23937 inline bool
23938 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23939 {
23940 if (a == b)
23941 return 1;
23942 if (a->hash != b->hash)
23943 return 0;
23944 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23945 if (strcmp (a->begin, b->begin) != 0
23946 || strcmp (a->end, b->end) != 0
23947 || (a->section == NULL) != (b->section == NULL)
23948 || (a->section && strcmp (a->section, b->section) != 0)
23949 || !compare_locs (a->expr, b->expr))
23950 break;
23951 return a == NULL && b == NULL;
23952 }
23953
23954 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23955
23956
23957 /* Recursively optimize location lists referenced from DIE
23958 children and share them whenever possible. */
23959
23960 static void
23961 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23962 {
23963 dw_die_ref c;
23964 dw_attr_ref a;
23965 unsigned ix;
23966 dw_loc_list_struct **slot;
23967
23968 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23969 if (AT_class (a) == dw_val_class_loc_list)
23970 {
23971 dw_loc_list_ref list = AT_loc_list (a);
23972 /* TODO: perform some optimizations here, before hashing
23973 it and storing into the hash table. */
23974 hash_loc_list (list);
23975 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23976 if (*slot == NULL)
23977 *slot = list;
23978 else
23979 a->dw_attr_val.v.val_loc_list = *slot;
23980 }
23981
23982 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23983 }
23984
23985
23986 /* Recursively assign each location list a unique index into the debug_addr
23987 section. */
23988
23989 static void
23990 index_location_lists (dw_die_ref die)
23991 {
23992 dw_die_ref c;
23993 dw_attr_ref a;
23994 unsigned ix;
23995
23996 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23997 if (AT_class (a) == dw_val_class_loc_list)
23998 {
23999 dw_loc_list_ref list = AT_loc_list (a);
24000 dw_loc_list_ref curr;
24001 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24002 {
24003 /* Don't index an entry that has already been indexed
24004 or won't be output. */
24005 if (curr->begin_entry != NULL
24006 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24007 continue;
24008
24009 curr->begin_entry
24010 = add_addr_table_entry (xstrdup (curr->begin),
24011 ate_kind_label);
24012 }
24013 }
24014
24015 FOR_EACH_CHILD (die, c, index_location_lists (c));
24016 }
24017
24018 /* Optimize location lists referenced from DIE
24019 children and share them whenever possible. */
24020
24021 static void
24022 optimize_location_lists (dw_die_ref die)
24023 {
24024 loc_list_hash_type htab;
24025 htab.create (500);
24026 optimize_location_lists_1 (die, htab);
24027 htab.dispose ();
24028 }
24029 \f
24030 /* Output stuff that dwarf requires at the end of every file,
24031 and generate the DWARF-2 debugging info. */
24032
24033 static void
24034 dwarf2out_finish (const char *filename)
24035 {
24036 limbo_die_node *node, *next_node;
24037 comdat_type_node *ctnode;
24038 hash_table <comdat_type_hasher> comdat_type_table;
24039 unsigned int i;
24040 dw_die_ref main_comp_unit_die;
24041
24042 /* PCH might result in DW_AT_producer string being restored from the
24043 header compilation, so always fill it with empty string initially
24044 and overwrite only here. */
24045 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24046 producer_string = gen_producer_string ();
24047 producer->dw_attr_val.v.val_str->refcount--;
24048 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24049
24050 gen_scheduled_generic_parms_dies ();
24051 gen_remaining_tmpl_value_param_die_attribute ();
24052
24053 /* Add the name for the main input file now. We delayed this from
24054 dwarf2out_init to avoid complications with PCH. */
24055 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24056 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24057 add_comp_dir_attribute (comp_unit_die ());
24058 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24059 {
24060 bool p = false;
24061 htab_traverse (file_table, file_table_relative_p, &p);
24062 if (p)
24063 add_comp_dir_attribute (comp_unit_die ());
24064 }
24065
24066 if (deferred_locations_list)
24067 for (i = 0; i < deferred_locations_list->length (); i++)
24068 {
24069 add_location_or_const_value_attribute (
24070 (*deferred_locations_list)[i].die,
24071 (*deferred_locations_list)[i].variable,
24072 false,
24073 DW_AT_location);
24074 }
24075
24076 /* Traverse the limbo die list, and add parent/child links. The only
24077 dies without parents that should be here are concrete instances of
24078 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24079 For concrete instances, we can get the parent die from the abstract
24080 instance. */
24081 for (node = limbo_die_list; node; node = next_node)
24082 {
24083 dw_die_ref die = node->die;
24084 next_node = node->next;
24085
24086 if (die->die_parent == NULL)
24087 {
24088 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24089
24090 if (origin && origin->die_parent)
24091 add_child_die (origin->die_parent, die);
24092 else if (is_cu_die (die))
24093 ;
24094 else if (seen_error ())
24095 /* It's OK to be confused by errors in the input. */
24096 add_child_die (comp_unit_die (), die);
24097 else
24098 {
24099 /* In certain situations, the lexical block containing a
24100 nested function can be optimized away, which results
24101 in the nested function die being orphaned. Likewise
24102 with the return type of that nested function. Force
24103 this to be a child of the containing function.
24104
24105 It may happen that even the containing function got fully
24106 inlined and optimized out. In that case we are lost and
24107 assign the empty child. This should not be big issue as
24108 the function is likely unreachable too. */
24109 gcc_assert (node->created_for);
24110
24111 if (DECL_P (node->created_for))
24112 origin = get_context_die (DECL_CONTEXT (node->created_for));
24113 else if (TYPE_P (node->created_for))
24114 origin = scope_die_for (node->created_for, comp_unit_die ());
24115 else
24116 origin = comp_unit_die ();
24117
24118 add_child_die (origin, die);
24119 }
24120 }
24121 }
24122
24123 limbo_die_list = NULL;
24124
24125 #if ENABLE_ASSERT_CHECKING
24126 {
24127 dw_die_ref die = comp_unit_die (), c;
24128 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24129 }
24130 #endif
24131 resolve_addr (comp_unit_die ());
24132 move_marked_base_types ();
24133
24134 for (node = deferred_asm_name; node; node = node->next)
24135 {
24136 tree decl = node->created_for;
24137 /* When generating LTO bytecode we can not generate new assembler
24138 names at this point and all important decls got theirs via
24139 free-lang-data. */
24140 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24141 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24142 {
24143 add_linkage_attr (node->die, decl);
24144 move_linkage_attr (node->die);
24145 }
24146 }
24147
24148 deferred_asm_name = NULL;
24149
24150 /* Walk through the list of incomplete types again, trying once more to
24151 emit full debugging info for them. */
24152 retry_incomplete_types ();
24153
24154 if (flag_eliminate_unused_debug_types)
24155 prune_unused_types ();
24156
24157 /* Generate separate COMDAT sections for type DIEs. */
24158 if (use_debug_types)
24159 {
24160 break_out_comdat_types (comp_unit_die ());
24161
24162 /* Each new type_unit DIE was added to the limbo die list when created.
24163 Since these have all been added to comdat_type_list, clear the
24164 limbo die list. */
24165 limbo_die_list = NULL;
24166
24167 /* For each new comdat type unit, copy declarations for incomplete
24168 types to make the new unit self-contained (i.e., no direct
24169 references to the main compile unit). */
24170 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24171 copy_decls_for_unworthy_types (ctnode->root_die);
24172 copy_decls_for_unworthy_types (comp_unit_die ());
24173
24174 /* In the process of copying declarations from one unit to another,
24175 we may have left some declarations behind that are no longer
24176 referenced. Prune them. */
24177 prune_unused_types ();
24178 }
24179
24180 /* Generate separate CUs for each of the include files we've seen.
24181 They will go into limbo_die_list. */
24182 if (flag_eliminate_dwarf2_dups)
24183 break_out_includes (comp_unit_die ());
24184
24185 /* Traverse the DIE's and add add sibling attributes to those DIE's
24186 that have children. */
24187 add_sibling_attributes (comp_unit_die ());
24188 for (node = limbo_die_list; node; node = node->next)
24189 add_sibling_attributes (node->die);
24190 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24191 add_sibling_attributes (ctnode->root_die);
24192
24193 /* When splitting DWARF info, we put some attributes in the
24194 skeleton compile_unit DIE that remains in the .o, while
24195 most attributes go in the DWO compile_unit_die. */
24196 if (dwarf_split_debug_info)
24197 main_comp_unit_die = gen_compile_unit_die (NULL);
24198 else
24199 main_comp_unit_die = comp_unit_die ();
24200
24201 /* Output a terminator label for the .text section. */
24202 switch_to_section (text_section);
24203 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24204 if (cold_text_section)
24205 {
24206 switch_to_section (cold_text_section);
24207 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24208 }
24209
24210 /* We can only use the low/high_pc attributes if all of the code was
24211 in .text. */
24212 if (!have_multiple_function_sections
24213 || (dwarf_version < 3 && dwarf_strict))
24214 {
24215 /* Don't add if the CU has no associated code. */
24216 if (text_section_used)
24217 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24218 text_end_label, true);
24219 }
24220 else
24221 {
24222 unsigned fde_idx;
24223 dw_fde_ref fde;
24224 bool range_list_added = false;
24225
24226 if (text_section_used)
24227 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24228 text_end_label, &range_list_added, true);
24229 if (cold_text_section_used)
24230 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24231 cold_end_label, &range_list_added, true);
24232
24233 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24234 {
24235 if (DECL_IGNORED_P (fde->decl))
24236 continue;
24237 if (!fde->in_std_section)
24238 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24239 fde->dw_fde_end, &range_list_added,
24240 true);
24241 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24242 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24243 fde->dw_fde_second_end, &range_list_added,
24244 true);
24245 }
24246
24247 if (range_list_added)
24248 {
24249 /* We need to give .debug_loc and .debug_ranges an appropriate
24250 "base address". Use zero so that these addresses become
24251 absolute. Historically, we've emitted the unexpected
24252 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24253 Emit both to give time for other tools to adapt. */
24254 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24255 if (! dwarf_strict && dwarf_version < 4)
24256 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24257
24258 add_ranges (NULL);
24259 }
24260 }
24261
24262 if (debug_info_level >= DINFO_LEVEL_TERSE)
24263 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24264 debug_line_section_label);
24265
24266 if (have_macinfo)
24267 add_AT_macptr (comp_unit_die (),
24268 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24269 macinfo_section_label);
24270
24271 if (dwarf_split_debug_info && addr_index_table != NULL)
24272 {
24273 /* optimize_location_lists calculates the size of the lists,
24274 so index them first, and assign indices to the entries.
24275 Although optimize_location_lists will remove entries from
24276 the table, it only does so for duplicates, and therefore
24277 only reduces ref_counts to 1. */
24278 unsigned int index = 0;
24279 index_location_lists (comp_unit_die ());
24280 htab_traverse_noresize (addr_index_table,
24281 index_addr_table_entry, &index);
24282 }
24283 if (have_location_lists)
24284 optimize_location_lists (comp_unit_die ());
24285
24286 save_macinfo_strings ();
24287
24288 if (dwarf_split_debug_info)
24289 {
24290 unsigned int index = 0;
24291
24292 /* Add attributes common to skeleton compile_units and
24293 type_units. Because these attributes include strings, it
24294 must be done before freezing the string table. Top-level
24295 skeleton die attrs are added when the skeleton type unit is
24296 created, so ensure it is created by this point. */
24297 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24298 (void) get_skeleton_type_unit ();
24299 htab_traverse_noresize (debug_str_hash, index_string, &index);
24300 }
24301
24302 /* Output all of the compilation units. We put the main one last so that
24303 the offsets are available to output_pubnames. */
24304 for (node = limbo_die_list; node; node = node->next)
24305 output_comp_unit (node->die, 0);
24306
24307 comdat_type_table.create (100);
24308 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24309 {
24310 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24311
24312 /* Don't output duplicate types. */
24313 if (*slot != HTAB_EMPTY_ENTRY)
24314 continue;
24315
24316 /* Add a pointer to the line table for the main compilation unit
24317 so that the debugger can make sense of DW_AT_decl_file
24318 attributes. */
24319 if (debug_info_level >= DINFO_LEVEL_TERSE)
24320 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24321 (!dwarf_split_debug_info
24322 ? debug_line_section_label
24323 : debug_skeleton_line_section_label));
24324
24325 output_comdat_type_unit (ctnode);
24326 *slot = ctnode;
24327 }
24328 comdat_type_table.dispose ();
24329
24330 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24331 both the main_cu and all skeleton TUs. Making this call unconditional
24332 would end up either adding a second copy of the AT_pubnames attribute, or
24333 requiring a special case in add_top_level_skeleton_die_attrs. */
24334 if (!dwarf_split_debug_info)
24335 add_AT_pubnames (comp_unit_die ());
24336
24337 if (dwarf_split_debug_info)
24338 {
24339 int mark;
24340 unsigned char checksum[16];
24341 struct md5_ctx ctx;
24342
24343 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24344 md5_init_ctx (&ctx);
24345 mark = 0;
24346 die_checksum (comp_unit_die (), &ctx, &mark);
24347 unmark_all_dies (comp_unit_die ());
24348 md5_finish_ctx (&ctx, checksum);
24349
24350 /* Use the first 8 bytes of the checksum as the dwo_id,
24351 and add it to both comp-unit DIEs. */
24352 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24353 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24354
24355 /* Add the base offset of the ranges table to the skeleton
24356 comp-unit DIE. */
24357 if (ranges_table_in_use)
24358 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24359 ranges_section_label);
24360
24361 switch_to_section (debug_addr_section);
24362 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24363 output_addr_table ();
24364 }
24365
24366 /* Output the main compilation unit if non-empty or if .debug_macinfo
24367 or .debug_macro will be emitted. */
24368 output_comp_unit (comp_unit_die (), have_macinfo);
24369
24370 if (dwarf_split_debug_info && info_section_emitted)
24371 output_skeleton_debug_sections (main_comp_unit_die);
24372
24373 /* Output the abbreviation table. */
24374 if (abbrev_die_table_in_use != 1)
24375 {
24376 switch_to_section (debug_abbrev_section);
24377 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24378 output_abbrev_section ();
24379 }
24380
24381 /* Output location list section if necessary. */
24382 if (have_location_lists)
24383 {
24384 /* Output the location lists info. */
24385 switch_to_section (debug_loc_section);
24386 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24387 output_location_lists (comp_unit_die ());
24388 }
24389
24390 output_pubtables ();
24391
24392 /* Output the address range information if a CU (.debug_info section)
24393 was emitted. We output an empty table even if we had no functions
24394 to put in it. This because the consumer has no way to tell the
24395 difference between an empty table that we omitted and failure to
24396 generate a table that would have contained data. */
24397 if (info_section_emitted)
24398 {
24399 unsigned long aranges_length = size_of_aranges ();
24400
24401 switch_to_section (debug_aranges_section);
24402 output_aranges (aranges_length);
24403 }
24404
24405 /* Output ranges section if necessary. */
24406 if (ranges_table_in_use)
24407 {
24408 switch_to_section (debug_ranges_section);
24409 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24410 output_ranges ();
24411 }
24412
24413 /* Have to end the macro section. */
24414 if (have_macinfo)
24415 {
24416 switch_to_section (debug_macinfo_section);
24417 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24418 output_macinfo ();
24419 dw2_asm_output_data (1, 0, "End compilation unit");
24420 }
24421
24422 /* Output the source line correspondence table. We must do this
24423 even if there is no line information. Otherwise, on an empty
24424 translation unit, we will generate a present, but empty,
24425 .debug_info section. IRIX 6.5 `nm' will then complain when
24426 examining the file. This is done late so that any filenames
24427 used by the debug_info section are marked as 'used'. */
24428 switch_to_section (debug_line_section);
24429 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24430 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24431 output_line_info (false);
24432
24433 if (dwarf_split_debug_info && info_section_emitted)
24434 {
24435 switch_to_section (debug_skeleton_line_section);
24436 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24437 output_line_info (true);
24438 }
24439
24440 /* If we emitted any indirect strings, output the string table too. */
24441 if (debug_str_hash || skeleton_debug_str_hash)
24442 output_indirect_strings ();
24443 }
24444
24445 #include "gt-dwarf2out.h"