]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/dwarf2out.c
Use rtx_expr_list in various places
[thirdparty/gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
28
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
34
35 /* DWARF2 Abbreviation Glossary:
36
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
42
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
45
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
48
49 DIE = Debugging Information Entry
50
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
54
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "tree.h"
64 #include "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "function.h"
68 #include "emit-rtl.h"
69 #include "hash-table.h"
70 #include "version.h"
71 #include "flags.h"
72 #include "hard-reg-set.h"
73 #include "regs.h"
74 #include "rtlhash.h"
75 #include "insn-config.h"
76 #include "reload.h"
77 #include "function.h"
78 #include "output.h"
79 #include "expr.h"
80 #include "except.h"
81 #include "dwarf2.h"
82 #include "dwarf2out.h"
83 #include "dwarf2asm.h"
84 #include "toplev.h"
85 #include "md5.h"
86 #include "tm_p.h"
87 #include "diagnostic.h"
88 #include "tree-pretty-print.h"
89 #include "debug.h"
90 #include "target.h"
91 #include "common/common-target.h"
92 #include "langhooks.h"
93 #include "cgraph.h"
94 #include "input.h"
95 #include "ira.h"
96 #include "lra.h"
97 #include "dumpfile.h"
98 #include "opts.h"
99 #include "tree-dfa.h"
100 #include "gdb/gdb-index.h"
101
102 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
103 static rtx_insn *last_var_location_insn;
104 static rtx_insn *cached_next_real_insn;
105 static void dwarf2out_decl (tree);
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 /* The DIE for C++14 'auto' in a function return type. */
253 static GTY(()) dw_die_ref auto_die;
254
255 /* The DIE for C++14 'decltype(auto)' in a function return type. */
256 static GTY(()) dw_die_ref decltype_auto_die;
257
258 /* Forward declarations for functions defined in this file. */
259
260 static char *stripattributes (const char *);
261 static void output_call_frame_info (int);
262 static void dwarf2out_note_section_used (void);
263
264 /* Personality decl of current unit. Used only when assembler does not support
265 personality CFI. */
266 static GTY(()) rtx current_unit_personality;
267
268 /* Data and reference forms for relocatable data. */
269 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
270 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
271
272 #ifndef DEBUG_FRAME_SECTION
273 #define DEBUG_FRAME_SECTION ".debug_frame"
274 #endif
275
276 #ifndef FUNC_BEGIN_LABEL
277 #define FUNC_BEGIN_LABEL "LFB"
278 #endif
279
280 #ifndef FUNC_END_LABEL
281 #define FUNC_END_LABEL "LFE"
282 #endif
283
284 #ifndef PROLOGUE_END_LABEL
285 #define PROLOGUE_END_LABEL "LPE"
286 #endif
287
288 #ifndef EPILOGUE_BEGIN_LABEL
289 #define EPILOGUE_BEGIN_LABEL "LEB"
290 #endif
291
292 #ifndef FRAME_BEGIN_LABEL
293 #define FRAME_BEGIN_LABEL "Lframe"
294 #endif
295 #define CIE_AFTER_SIZE_LABEL "LSCIE"
296 #define CIE_END_LABEL "LECIE"
297 #define FDE_LABEL "LSFDE"
298 #define FDE_AFTER_SIZE_LABEL "LASFDE"
299 #define FDE_END_LABEL "LEFDE"
300 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
301 #define LINE_NUMBER_END_LABEL "LELT"
302 #define LN_PROLOG_AS_LABEL "LASLTP"
303 #define LN_PROLOG_END_LABEL "LELTP"
304 #define DIE_LABEL_PREFIX "DW"
305 \f
306 /* Match the base name of a file to the base name of a compilation unit. */
307
308 static int
309 matches_main_base (const char *path)
310 {
311 /* Cache the last query. */
312 static const char *last_path = NULL;
313 static int last_match = 0;
314 if (path != last_path)
315 {
316 const char *base;
317 int length = base_of_path (path, &base);
318 last_path = path;
319 last_match = (length == main_input_baselength
320 && memcmp (base, main_input_basename, length) == 0);
321 }
322 return last_match;
323 }
324
325 #ifdef DEBUG_DEBUG_STRUCT
326
327 static int
328 dump_struct_debug (tree type, enum debug_info_usage usage,
329 enum debug_struct_file criterion, int generic,
330 int matches, int result)
331 {
332 /* Find the type name. */
333 tree type_decl = TYPE_STUB_DECL (type);
334 tree t = type_decl;
335 const char *name = 0;
336 if (TREE_CODE (t) == TYPE_DECL)
337 t = DECL_NAME (t);
338 if (t)
339 name = IDENTIFIER_POINTER (t);
340
341 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
342 criterion,
343 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
344 matches ? "bas" : "hdr",
345 generic ? "gen" : "ord",
346 usage == DINFO_USAGE_DFN ? ";" :
347 usage == DINFO_USAGE_DIR_USE ? "." : "*",
348 result,
349 (void*) type_decl, name);
350 return result;
351 }
352 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
353 dump_struct_debug (type, usage, criterion, generic, matches, result)
354
355 #else
356
357 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
358 (result)
359
360 #endif
361
362 /* Get the number of HOST_WIDE_INTs needed to represent the precision
363 of the number. */
364
365 static unsigned int
366 get_full_len (const wide_int &op)
367 {
368 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
369 / HOST_BITS_PER_WIDE_INT);
370 }
371
372 static bool
373 should_emit_struct_debug (tree type, enum debug_info_usage usage)
374 {
375 enum debug_struct_file criterion;
376 tree type_decl;
377 bool generic = lang_hooks.types.generic_p (type);
378
379 if (generic)
380 criterion = debug_struct_generic[usage];
381 else
382 criterion = debug_struct_ordinary[usage];
383
384 if (criterion == DINFO_STRUCT_FILE_NONE)
385 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
386 if (criterion == DINFO_STRUCT_FILE_ANY)
387 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
388
389 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
390
391 if (type_decl != NULL)
392 {
393 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
394 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
395
396 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
397 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
398 }
399
400 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
401 }
402 \f
403 /* Return a pointer to a copy of the section string name S with all
404 attributes stripped off, and an asterisk prepended (for assemble_name). */
405
406 static inline char *
407 stripattributes (const char *s)
408 {
409 char *stripped = XNEWVEC (char, strlen (s) + 2);
410 char *p = stripped;
411
412 *p++ = '*';
413
414 while (*s && *s != ',')
415 *p++ = *s++;
416
417 *p = '\0';
418 return stripped;
419 }
420
421 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
422 switch to the data section instead, and write out a synthetic start label
423 for collect2 the first time around. */
424
425 static void
426 switch_to_eh_frame_section (bool back)
427 {
428 tree label;
429
430 #ifdef EH_FRAME_SECTION_NAME
431 if (eh_frame_section == 0)
432 {
433 int flags;
434
435 if (EH_TABLES_CAN_BE_READ_ONLY)
436 {
437 int fde_encoding;
438 int per_encoding;
439 int lsda_encoding;
440
441 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
442 /*global=*/0);
443 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
444 /*global=*/1);
445 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
446 /*global=*/0);
447 flags = ((! flag_pic
448 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
449 && (fde_encoding & 0x70) != DW_EH_PE_aligned
450 && (per_encoding & 0x70) != DW_EH_PE_absptr
451 && (per_encoding & 0x70) != DW_EH_PE_aligned
452 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
453 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
454 ? 0 : SECTION_WRITE);
455 }
456 else
457 flags = SECTION_WRITE;
458 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
459 }
460 #endif /* EH_FRAME_SECTION_NAME */
461
462 if (eh_frame_section)
463 switch_to_section (eh_frame_section);
464 else
465 {
466 /* We have no special eh_frame section. Put the information in
467 the data section and emit special labels to guide collect2. */
468 switch_to_section (data_section);
469
470 if (!back)
471 {
472 label = get_file_function_name ("F");
473 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
474 targetm.asm_out.globalize_label (asm_out_file,
475 IDENTIFIER_POINTER (label));
476 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
477 }
478 }
479 }
480
481 /* Switch [BACK] to the eh or debug frame table section, depending on
482 FOR_EH. */
483
484 static void
485 switch_to_frame_table_section (int for_eh, bool back)
486 {
487 if (for_eh)
488 switch_to_eh_frame_section (back);
489 else
490 {
491 if (!debug_frame_section)
492 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
493 SECTION_DEBUG, NULL);
494 switch_to_section (debug_frame_section);
495 }
496 }
497
498 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
499
500 enum dw_cfi_oprnd_type
501 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
502 {
503 switch (cfi)
504 {
505 case DW_CFA_nop:
506 case DW_CFA_GNU_window_save:
507 case DW_CFA_remember_state:
508 case DW_CFA_restore_state:
509 return dw_cfi_oprnd_unused;
510
511 case DW_CFA_set_loc:
512 case DW_CFA_advance_loc1:
513 case DW_CFA_advance_loc2:
514 case DW_CFA_advance_loc4:
515 case DW_CFA_MIPS_advance_loc8:
516 return dw_cfi_oprnd_addr;
517
518 case DW_CFA_offset:
519 case DW_CFA_offset_extended:
520 case DW_CFA_def_cfa:
521 case DW_CFA_offset_extended_sf:
522 case DW_CFA_def_cfa_sf:
523 case DW_CFA_restore:
524 case DW_CFA_restore_extended:
525 case DW_CFA_undefined:
526 case DW_CFA_same_value:
527 case DW_CFA_def_cfa_register:
528 case DW_CFA_register:
529 case DW_CFA_expression:
530 return dw_cfi_oprnd_reg_num;
531
532 case DW_CFA_def_cfa_offset:
533 case DW_CFA_GNU_args_size:
534 case DW_CFA_def_cfa_offset_sf:
535 return dw_cfi_oprnd_offset;
536
537 case DW_CFA_def_cfa_expression:
538 return dw_cfi_oprnd_loc;
539
540 default:
541 gcc_unreachable ();
542 }
543 }
544
545 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
546
547 enum dw_cfi_oprnd_type
548 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
549 {
550 switch (cfi)
551 {
552 case DW_CFA_def_cfa:
553 case DW_CFA_def_cfa_sf:
554 case DW_CFA_offset:
555 case DW_CFA_offset_extended_sf:
556 case DW_CFA_offset_extended:
557 return dw_cfi_oprnd_offset;
558
559 case DW_CFA_register:
560 return dw_cfi_oprnd_reg_num;
561
562 case DW_CFA_expression:
563 return dw_cfi_oprnd_loc;
564
565 default:
566 return dw_cfi_oprnd_unused;
567 }
568 }
569
570 /* Output one FDE. */
571
572 static void
573 output_fde (dw_fde_ref fde, bool for_eh, bool second,
574 char *section_start_label, int fde_encoding, char *augmentation,
575 bool any_lsda_needed, int lsda_encoding)
576 {
577 const char *begin, *end;
578 static unsigned int j;
579 char l1[20], l2[20];
580
581 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
582 /* empty */ 0);
583 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
584 for_eh + j);
585 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
586 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
587 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
588 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
589 " indicating 64-bit DWARF extension");
590 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
591 "FDE Length");
592 ASM_OUTPUT_LABEL (asm_out_file, l1);
593
594 if (for_eh)
595 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
596 else
597 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
598 debug_frame_section, "FDE CIE offset");
599
600 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
601 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
602
603 if (for_eh)
604 {
605 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
606 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
607 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
608 "FDE initial location");
609 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
610 end, begin, "FDE address range");
611 }
612 else
613 {
614 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
615 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
616 }
617
618 if (augmentation[0])
619 {
620 if (any_lsda_needed)
621 {
622 int size = size_of_encoded_value (lsda_encoding);
623
624 if (lsda_encoding == DW_EH_PE_aligned)
625 {
626 int offset = ( 4 /* Length */
627 + 4 /* CIE offset */
628 + 2 * size_of_encoded_value (fde_encoding)
629 + 1 /* Augmentation size */ );
630 int pad = -offset & (PTR_SIZE - 1);
631
632 size += pad;
633 gcc_assert (size_of_uleb128 (size) == 1);
634 }
635
636 dw2_asm_output_data_uleb128 (size, "Augmentation size");
637
638 if (fde->uses_eh_lsda)
639 {
640 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
641 fde->funcdef_number);
642 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
643 gen_rtx_SYMBOL_REF (Pmode, l1),
644 false,
645 "Language Specific Data Area");
646 }
647 else
648 {
649 if (lsda_encoding == DW_EH_PE_aligned)
650 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
651 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
652 "Language Specific Data Area (none)");
653 }
654 }
655 else
656 dw2_asm_output_data_uleb128 (0, "Augmentation size");
657 }
658
659 /* Loop through the Call Frame Instructions associated with this FDE. */
660 fde->dw_fde_current_label = begin;
661 {
662 size_t from, until, i;
663
664 from = 0;
665 until = vec_safe_length (fde->dw_fde_cfi);
666
667 if (fde->dw_fde_second_begin == NULL)
668 ;
669 else if (!second)
670 until = fde->dw_fde_switch_cfi_index;
671 else
672 from = fde->dw_fde_switch_cfi_index;
673
674 for (i = from; i < until; i++)
675 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
676 }
677
678 /* If we are to emit a ref/link from function bodies to their frame tables,
679 do it now. This is typically performed to make sure that tables
680 associated with functions are dragged with them and not discarded in
681 garbage collecting links. We need to do this on a per function basis to
682 cope with -ffunction-sections. */
683
684 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
685 /* Switch to the function section, emit the ref to the tables, and
686 switch *back* into the table section. */
687 switch_to_section (function_section (fde->decl));
688 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
689 switch_to_frame_table_section (for_eh, true);
690 #endif
691
692 /* Pad the FDE out to an address sized boundary. */
693 ASM_OUTPUT_ALIGN (asm_out_file,
694 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
695 ASM_OUTPUT_LABEL (asm_out_file, l2);
696
697 j += 2;
698 }
699
700 /* Return true if frame description entry FDE is needed for EH. */
701
702 static bool
703 fde_needed_for_eh_p (dw_fde_ref fde)
704 {
705 if (flag_asynchronous_unwind_tables)
706 return true;
707
708 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
709 return true;
710
711 if (fde->uses_eh_lsda)
712 return true;
713
714 /* If exceptions are enabled, we have collected nothrow info. */
715 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
716 return false;
717
718 return true;
719 }
720
721 /* Output the call frame information used to record information
722 that relates to calculating the frame pointer, and records the
723 location of saved registers. */
724
725 static void
726 output_call_frame_info (int for_eh)
727 {
728 unsigned int i;
729 dw_fde_ref fde;
730 dw_cfi_ref cfi;
731 char l1[20], l2[20], section_start_label[20];
732 bool any_lsda_needed = false;
733 char augmentation[6];
734 int augmentation_size;
735 int fde_encoding = DW_EH_PE_absptr;
736 int per_encoding = DW_EH_PE_absptr;
737 int lsda_encoding = DW_EH_PE_absptr;
738 int return_reg;
739 rtx personality = NULL;
740 int dw_cie_version;
741
742 /* Don't emit a CIE if there won't be any FDEs. */
743 if (!fde_vec)
744 return;
745
746 /* Nothing to do if the assembler's doing it all. */
747 if (dwarf2out_do_cfi_asm ())
748 return;
749
750 /* If we don't have any functions we'll want to unwind out of, don't emit
751 any EH unwind information. If we make FDEs linkonce, we may have to
752 emit an empty label for an FDE that wouldn't otherwise be emitted. We
753 want to avoid having an FDE kept around when the function it refers to
754 is discarded. Example where this matters: a primary function template
755 in C++ requires EH information, an explicit specialization doesn't. */
756 if (for_eh)
757 {
758 bool any_eh_needed = false;
759
760 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
761 {
762 if (fde->uses_eh_lsda)
763 any_eh_needed = any_lsda_needed = true;
764 else if (fde_needed_for_eh_p (fde))
765 any_eh_needed = true;
766 else if (TARGET_USES_WEAK_UNWIND_INFO)
767 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
768 }
769
770 if (!any_eh_needed)
771 return;
772 }
773
774 /* We're going to be generating comments, so turn on app. */
775 if (flag_debug_asm)
776 app_enable ();
777
778 /* Switch to the proper frame section, first time. */
779 switch_to_frame_table_section (for_eh, false);
780
781 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
782 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
783
784 /* Output the CIE. */
785 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
786 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
787 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
788 dw2_asm_output_data (4, 0xffffffff,
789 "Initial length escape value indicating 64-bit DWARF extension");
790 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
791 "Length of Common Information Entry");
792 ASM_OUTPUT_LABEL (asm_out_file, l1);
793
794 /* Now that the CIE pointer is PC-relative for EH,
795 use 0 to identify the CIE. */
796 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
797 (for_eh ? 0 : DWARF_CIE_ID),
798 "CIE Identifier Tag");
799
800 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
801 use CIE version 1, unless that would produce incorrect results
802 due to overflowing the return register column. */
803 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
804 dw_cie_version = 1;
805 if (return_reg >= 256 || dwarf_version > 2)
806 dw_cie_version = 3;
807 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
808
809 augmentation[0] = 0;
810 augmentation_size = 0;
811
812 personality = current_unit_personality;
813 if (for_eh)
814 {
815 char *p;
816
817 /* Augmentation:
818 z Indicates that a uleb128 is present to size the
819 augmentation section.
820 L Indicates the encoding (and thus presence) of
821 an LSDA pointer in the FDE augmentation.
822 R Indicates a non-default pointer encoding for
823 FDE code pointers.
824 P Indicates the presence of an encoding + language
825 personality routine in the CIE augmentation. */
826
827 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
828 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
829 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
830
831 p = augmentation + 1;
832 if (personality)
833 {
834 *p++ = 'P';
835 augmentation_size += 1 + size_of_encoded_value (per_encoding);
836 assemble_external_libcall (personality);
837 }
838 if (any_lsda_needed)
839 {
840 *p++ = 'L';
841 augmentation_size += 1;
842 }
843 if (fde_encoding != DW_EH_PE_absptr)
844 {
845 *p++ = 'R';
846 augmentation_size += 1;
847 }
848 if (p > augmentation + 1)
849 {
850 augmentation[0] = 'z';
851 *p = '\0';
852 }
853
854 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
855 if (personality && per_encoding == DW_EH_PE_aligned)
856 {
857 int offset = ( 4 /* Length */
858 + 4 /* CIE Id */
859 + 1 /* CIE version */
860 + strlen (augmentation) + 1 /* Augmentation */
861 + size_of_uleb128 (1) /* Code alignment */
862 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
863 + 1 /* RA column */
864 + 1 /* Augmentation size */
865 + 1 /* Personality encoding */ );
866 int pad = -offset & (PTR_SIZE - 1);
867
868 augmentation_size += pad;
869
870 /* Augmentations should be small, so there's scarce need to
871 iterate for a solution. Die if we exceed one uleb128 byte. */
872 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
873 }
874 }
875
876 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
877 if (dw_cie_version >= 4)
878 {
879 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
880 dw2_asm_output_data (1, 0, "CIE Segment Size");
881 }
882 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
883 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
884 "CIE Data Alignment Factor");
885
886 if (dw_cie_version == 1)
887 dw2_asm_output_data (1, return_reg, "CIE RA Column");
888 else
889 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
890
891 if (augmentation[0])
892 {
893 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
894 if (personality)
895 {
896 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
897 eh_data_format_name (per_encoding));
898 dw2_asm_output_encoded_addr_rtx (per_encoding,
899 personality,
900 true, NULL);
901 }
902
903 if (any_lsda_needed)
904 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
905 eh_data_format_name (lsda_encoding));
906
907 if (fde_encoding != DW_EH_PE_absptr)
908 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
909 eh_data_format_name (fde_encoding));
910 }
911
912 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
913 output_cfi (cfi, NULL, for_eh);
914
915 /* Pad the CIE out to an address sized boundary. */
916 ASM_OUTPUT_ALIGN (asm_out_file,
917 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
918 ASM_OUTPUT_LABEL (asm_out_file, l2);
919
920 /* Loop through all of the FDE's. */
921 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
922 {
923 unsigned int k;
924
925 /* Don't emit EH unwind info for leaf functions that don't need it. */
926 if (for_eh && !fde_needed_for_eh_p (fde))
927 continue;
928
929 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
930 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
931 augmentation, any_lsda_needed, lsda_encoding);
932 }
933
934 if (for_eh && targetm.terminate_dw2_eh_frame_info)
935 dw2_asm_output_data (4, 0, "End of Table");
936
937 /* Turn off app to make assembly quicker. */
938 if (flag_debug_asm)
939 app_disable ();
940 }
941
942 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
943
944 static void
945 dwarf2out_do_cfi_startproc (bool second)
946 {
947 int enc;
948 rtx ref;
949 rtx personality = get_personality_function (current_function_decl);
950
951 fprintf (asm_out_file, "\t.cfi_startproc\n");
952
953 if (personality)
954 {
955 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
956 ref = personality;
957
958 /* ??? The GAS support isn't entirely consistent. We have to
959 handle indirect support ourselves, but PC-relative is done
960 in the assembler. Further, the assembler can't handle any
961 of the weirder relocation types. */
962 if (enc & DW_EH_PE_indirect)
963 ref = dw2_force_const_mem (ref, true);
964
965 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
966 output_addr_const (asm_out_file, ref);
967 fputc ('\n', asm_out_file);
968 }
969
970 if (crtl->uses_eh_lsda)
971 {
972 char lab[20];
973
974 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
975 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
976 current_function_funcdef_no);
977 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
978 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
979
980 if (enc & DW_EH_PE_indirect)
981 ref = dw2_force_const_mem (ref, true);
982
983 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
984 output_addr_const (asm_out_file, ref);
985 fputc ('\n', asm_out_file);
986 }
987 }
988
989 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
990 this allocation may be done before pass_final. */
991
992 dw_fde_ref
993 dwarf2out_alloc_current_fde (void)
994 {
995 dw_fde_ref fde;
996
997 fde = ggc_cleared_alloc<dw_fde_node> ();
998 fde->decl = current_function_decl;
999 fde->funcdef_number = current_function_funcdef_no;
1000 fde->fde_index = vec_safe_length (fde_vec);
1001 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1002 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1003 fde->nothrow = crtl->nothrow;
1004 fde->drap_reg = INVALID_REGNUM;
1005 fde->vdrap_reg = INVALID_REGNUM;
1006
1007 /* Record the FDE associated with this function. */
1008 cfun->fde = fde;
1009 vec_safe_push (fde_vec, fde);
1010
1011 return fde;
1012 }
1013
1014 /* Output a marker (i.e. a label) for the beginning of a function, before
1015 the prologue. */
1016
1017 void
1018 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1019 const char *file ATTRIBUTE_UNUSED)
1020 {
1021 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1022 char * dup_label;
1023 dw_fde_ref fde;
1024 section *fnsec;
1025 bool do_frame;
1026
1027 current_function_func_begin_label = NULL;
1028
1029 do_frame = dwarf2out_do_frame ();
1030
1031 /* ??? current_function_func_begin_label is also used by except.c for
1032 call-site information. We must emit this label if it might be used. */
1033 if (!do_frame
1034 && (!flag_exceptions
1035 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1036 return;
1037
1038 fnsec = function_section (current_function_decl);
1039 switch_to_section (fnsec);
1040 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1041 current_function_funcdef_no);
1042 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1043 current_function_funcdef_no);
1044 dup_label = xstrdup (label);
1045 current_function_func_begin_label = dup_label;
1046
1047 /* We can elide the fde allocation if we're not emitting debug info. */
1048 if (!do_frame)
1049 return;
1050
1051 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1052 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1053 would include pass_dwarf2_frame. If we've not created the FDE yet,
1054 do so now. */
1055 fde = cfun->fde;
1056 if (fde == NULL)
1057 fde = dwarf2out_alloc_current_fde ();
1058
1059 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1060 fde->dw_fde_begin = dup_label;
1061 fde->dw_fde_current_label = dup_label;
1062 fde->in_std_section = (fnsec == text_section
1063 || (cold_text_section && fnsec == cold_text_section));
1064
1065 /* We only want to output line number information for the genuine dwarf2
1066 prologue case, not the eh frame case. */
1067 #ifdef DWARF2_DEBUGGING_INFO
1068 if (file)
1069 dwarf2out_source_line (line, file, 0, true);
1070 #endif
1071
1072 if (dwarf2out_do_cfi_asm ())
1073 dwarf2out_do_cfi_startproc (false);
1074 else
1075 {
1076 rtx personality = get_personality_function (current_function_decl);
1077 if (!current_unit_personality)
1078 current_unit_personality = personality;
1079
1080 /* We cannot keep a current personality per function as without CFI
1081 asm, at the point where we emit the CFI data, there is no current
1082 function anymore. */
1083 if (personality && current_unit_personality != personality)
1084 sorry ("multiple EH personalities are supported only with assemblers "
1085 "supporting .cfi_personality directive");
1086 }
1087 }
1088
1089 /* Output a marker (i.e. a label) for the end of the generated code
1090 for a function prologue. This gets called *after* the prologue code has
1091 been generated. */
1092
1093 void
1094 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1095 const char *file ATTRIBUTE_UNUSED)
1096 {
1097 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1098
1099 /* Output a label to mark the endpoint of the code generated for this
1100 function. */
1101 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1102 current_function_funcdef_no);
1103 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1104 current_function_funcdef_no);
1105 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1106 }
1107
1108 /* Output a marker (i.e. a label) for the beginning of the generated code
1109 for a function epilogue. This gets called *before* the prologue code has
1110 been generated. */
1111
1112 void
1113 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1114 const char *file ATTRIBUTE_UNUSED)
1115 {
1116 dw_fde_ref fde = cfun->fde;
1117 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1118
1119 if (fde->dw_fde_vms_begin_epilogue)
1120 return;
1121
1122 /* Output a label to mark the endpoint of the code generated for this
1123 function. */
1124 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1125 current_function_funcdef_no);
1126 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1127 current_function_funcdef_no);
1128 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1129 }
1130
1131 /* Output a marker (i.e. a label) for the absolute end of the generated code
1132 for a function definition. This gets called *after* the epilogue code has
1133 been generated. */
1134
1135 void
1136 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1137 const char *file ATTRIBUTE_UNUSED)
1138 {
1139 dw_fde_ref fde;
1140 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1141
1142 last_var_location_insn = NULL;
1143 cached_next_real_insn = NULL;
1144
1145 if (dwarf2out_do_cfi_asm ())
1146 fprintf (asm_out_file, "\t.cfi_endproc\n");
1147
1148 /* Output a label to mark the endpoint of the code generated for this
1149 function. */
1150 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1151 current_function_funcdef_no);
1152 ASM_OUTPUT_LABEL (asm_out_file, label);
1153 fde = cfun->fde;
1154 gcc_assert (fde != NULL);
1155 if (fde->dw_fde_second_begin == NULL)
1156 fde->dw_fde_end = xstrdup (label);
1157 }
1158
1159 void
1160 dwarf2out_frame_finish (void)
1161 {
1162 /* Output call frame information. */
1163 if (targetm.debug_unwind_info () == UI_DWARF2)
1164 output_call_frame_info (0);
1165
1166 /* Output another copy for the unwinder. */
1167 if ((flag_unwind_tables || flag_exceptions)
1168 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1169 output_call_frame_info (1);
1170 }
1171
1172 /* Note that the current function section is being used for code. */
1173
1174 static void
1175 dwarf2out_note_section_used (void)
1176 {
1177 section *sec = current_function_section ();
1178 if (sec == text_section)
1179 text_section_used = true;
1180 else if (sec == cold_text_section)
1181 cold_text_section_used = true;
1182 }
1183
1184 static void var_location_switch_text_section (void);
1185 static void set_cur_line_info_table (section *);
1186
1187 void
1188 dwarf2out_switch_text_section (void)
1189 {
1190 section *sect;
1191 dw_fde_ref fde = cfun->fde;
1192
1193 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1194
1195 if (!in_cold_section_p)
1196 {
1197 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1198 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1199 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1200 }
1201 else
1202 {
1203 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1204 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1205 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1206 }
1207 have_multiple_function_sections = true;
1208
1209 /* There is no need to mark used sections when not debugging. */
1210 if (cold_text_section != NULL)
1211 dwarf2out_note_section_used ();
1212
1213 if (dwarf2out_do_cfi_asm ())
1214 fprintf (asm_out_file, "\t.cfi_endproc\n");
1215
1216 /* Now do the real section switch. */
1217 sect = current_function_section ();
1218 switch_to_section (sect);
1219
1220 fde->second_in_std_section
1221 = (sect == text_section
1222 || (cold_text_section && sect == cold_text_section));
1223
1224 if (dwarf2out_do_cfi_asm ())
1225 dwarf2out_do_cfi_startproc (true);
1226
1227 var_location_switch_text_section ();
1228
1229 if (cold_text_section != NULL)
1230 set_cur_line_info_table (sect);
1231 }
1232 \f
1233 /* And now, the subset of the debugging information support code necessary
1234 for emitting location expressions. */
1235
1236 /* Data about a single source file. */
1237 struct GTY(()) dwarf_file_data {
1238 const char * filename;
1239 int emitted_number;
1240 };
1241
1242 typedef struct GTY(()) deferred_locations_struct
1243 {
1244 tree variable;
1245 dw_die_ref die;
1246 } deferred_locations;
1247
1248
1249 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1250
1251
1252 /* Describe an entry into the .debug_addr section. */
1253
1254 enum ate_kind {
1255 ate_kind_rtx,
1256 ate_kind_rtx_dtprel,
1257 ate_kind_label
1258 };
1259
1260 typedef struct GTY(()) addr_table_entry_struct {
1261 enum ate_kind kind;
1262 unsigned int refcount;
1263 unsigned int index;
1264 union addr_table_entry_struct_union
1265 {
1266 rtx GTY ((tag ("0"))) rtl;
1267 char * GTY ((tag ("1"))) label;
1268 }
1269 GTY ((desc ("%1.kind"))) addr;
1270 }
1271 addr_table_entry;
1272
1273 /* Location lists are ranges + location descriptions for that range,
1274 so you can track variables that are in different places over
1275 their entire life. */
1276 typedef struct GTY(()) dw_loc_list_struct {
1277 dw_loc_list_ref dw_loc_next;
1278 const char *begin; /* Label and addr_entry for start of range */
1279 addr_table_entry *begin_entry;
1280 const char *end; /* Label for end of range */
1281 char *ll_symbol; /* Label for beginning of location list.
1282 Only on head of list */
1283 const char *section; /* Section this loclist is relative to */
1284 dw_loc_descr_ref expr;
1285 hashval_t hash;
1286 /* True if all addresses in this and subsequent lists are known to be
1287 resolved. */
1288 bool resolved_addr;
1289 /* True if this list has been replaced by dw_loc_next. */
1290 bool replaced;
1291 bool emitted;
1292 /* True if the range should be emitted even if begin and end
1293 are the same. */
1294 bool force;
1295 } dw_loc_list_node;
1296
1297 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1298
1299 /* Convert a DWARF stack opcode into its string name. */
1300
1301 static const char *
1302 dwarf_stack_op_name (unsigned int op)
1303 {
1304 const char *name = get_DW_OP_name (op);
1305
1306 if (name != NULL)
1307 return name;
1308
1309 return "OP_<unknown>";
1310 }
1311
1312 /* Return a pointer to a newly allocated location description. Location
1313 descriptions are simple expression terms that can be strung
1314 together to form more complicated location (address) descriptions. */
1315
1316 static inline dw_loc_descr_ref
1317 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1318 unsigned HOST_WIDE_INT oprnd2)
1319 {
1320 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1321
1322 descr->dw_loc_opc = op;
1323 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1324 descr->dw_loc_oprnd1.val_entry = NULL;
1325 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1326 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1327 descr->dw_loc_oprnd2.val_entry = NULL;
1328 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1329
1330 return descr;
1331 }
1332
1333 /* Return a pointer to a newly allocated location description for
1334 REG and OFFSET. */
1335
1336 static inline dw_loc_descr_ref
1337 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1338 {
1339 if (reg <= 31)
1340 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1341 offset, 0);
1342 else
1343 return new_loc_descr (DW_OP_bregx, reg, offset);
1344 }
1345
1346 /* Add a location description term to a location description expression. */
1347
1348 static inline void
1349 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1350 {
1351 dw_loc_descr_ref *d;
1352
1353 /* Find the end of the chain. */
1354 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1355 ;
1356
1357 *d = descr;
1358 }
1359
1360 /* Compare two location operands for exact equality. */
1361
1362 static bool
1363 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1364 {
1365 if (a->val_class != b->val_class)
1366 return false;
1367 switch (a->val_class)
1368 {
1369 case dw_val_class_none:
1370 return true;
1371 case dw_val_class_addr:
1372 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1373
1374 case dw_val_class_offset:
1375 case dw_val_class_unsigned_const:
1376 case dw_val_class_const:
1377 case dw_val_class_range_list:
1378 case dw_val_class_lineptr:
1379 case dw_val_class_macptr:
1380 /* These are all HOST_WIDE_INT, signed or unsigned. */
1381 return a->v.val_unsigned == b->v.val_unsigned;
1382
1383 case dw_val_class_loc:
1384 return a->v.val_loc == b->v.val_loc;
1385 case dw_val_class_loc_list:
1386 return a->v.val_loc_list == b->v.val_loc_list;
1387 case dw_val_class_die_ref:
1388 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1389 case dw_val_class_fde_ref:
1390 return a->v.val_fde_index == b->v.val_fde_index;
1391 case dw_val_class_lbl_id:
1392 case dw_val_class_high_pc:
1393 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1394 case dw_val_class_str:
1395 return a->v.val_str == b->v.val_str;
1396 case dw_val_class_flag:
1397 return a->v.val_flag == b->v.val_flag;
1398 case dw_val_class_file:
1399 return a->v.val_file == b->v.val_file;
1400 case dw_val_class_decl_ref:
1401 return a->v.val_decl_ref == b->v.val_decl_ref;
1402
1403 case dw_val_class_const_double:
1404 return (a->v.val_double.high == b->v.val_double.high
1405 && a->v.val_double.low == b->v.val_double.low);
1406
1407 case dw_val_class_wide_int:
1408 return *a->v.val_wide == *b->v.val_wide;
1409
1410 case dw_val_class_vec:
1411 {
1412 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1413 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1414
1415 return (a_len == b_len
1416 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1417 }
1418
1419 case dw_val_class_data8:
1420 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1421
1422 case dw_val_class_vms_delta:
1423 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1424 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1425 }
1426 gcc_unreachable ();
1427 }
1428
1429 /* Compare two location atoms for exact equality. */
1430
1431 static bool
1432 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1433 {
1434 if (a->dw_loc_opc != b->dw_loc_opc)
1435 return false;
1436
1437 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1438 address size, but since we always allocate cleared storage it
1439 should be zero for other types of locations. */
1440 if (a->dtprel != b->dtprel)
1441 return false;
1442
1443 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1444 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1445 }
1446
1447 /* Compare two complete location expressions for exact equality. */
1448
1449 bool
1450 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1451 {
1452 while (1)
1453 {
1454 if (a == b)
1455 return true;
1456 if (a == NULL || b == NULL)
1457 return false;
1458 if (!loc_descr_equal_p_1 (a, b))
1459 return false;
1460
1461 a = a->dw_loc_next;
1462 b = b->dw_loc_next;
1463 }
1464 }
1465
1466
1467 /* Add a constant OFFSET to a location expression. */
1468
1469 static void
1470 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1471 {
1472 dw_loc_descr_ref loc;
1473 HOST_WIDE_INT *p;
1474
1475 gcc_assert (*list_head != NULL);
1476
1477 if (!offset)
1478 return;
1479
1480 /* Find the end of the chain. */
1481 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1482 ;
1483
1484 p = NULL;
1485 if (loc->dw_loc_opc == DW_OP_fbreg
1486 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1487 p = &loc->dw_loc_oprnd1.v.val_int;
1488 else if (loc->dw_loc_opc == DW_OP_bregx)
1489 p = &loc->dw_loc_oprnd2.v.val_int;
1490
1491 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1492 offset. Don't optimize if an signed integer overflow would happen. */
1493 if (p != NULL
1494 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1495 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1496 *p += offset;
1497
1498 else if (offset > 0)
1499 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1500
1501 else
1502 {
1503 loc->dw_loc_next = int_loc_descriptor (-offset);
1504 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1505 }
1506 }
1507
1508 /* Add a constant OFFSET to a location list. */
1509
1510 static void
1511 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1512 {
1513 dw_loc_list_ref d;
1514 for (d = list_head; d != NULL; d = d->dw_loc_next)
1515 loc_descr_plus_const (&d->expr, offset);
1516 }
1517
1518 #define DWARF_REF_SIZE \
1519 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1520
1521 static unsigned long int get_base_type_offset (dw_die_ref);
1522
1523 /* Return the size of a location descriptor. */
1524
1525 static unsigned long
1526 size_of_loc_descr (dw_loc_descr_ref loc)
1527 {
1528 unsigned long size = 1;
1529
1530 switch (loc->dw_loc_opc)
1531 {
1532 case DW_OP_addr:
1533 size += DWARF2_ADDR_SIZE;
1534 break;
1535 case DW_OP_GNU_addr_index:
1536 case DW_OP_GNU_const_index:
1537 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1538 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1539 break;
1540 case DW_OP_const1u:
1541 case DW_OP_const1s:
1542 size += 1;
1543 break;
1544 case DW_OP_const2u:
1545 case DW_OP_const2s:
1546 size += 2;
1547 break;
1548 case DW_OP_const4u:
1549 case DW_OP_const4s:
1550 size += 4;
1551 break;
1552 case DW_OP_const8u:
1553 case DW_OP_const8s:
1554 size += 8;
1555 break;
1556 case DW_OP_constu:
1557 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1558 break;
1559 case DW_OP_consts:
1560 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1561 break;
1562 case DW_OP_pick:
1563 size += 1;
1564 break;
1565 case DW_OP_plus_uconst:
1566 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1567 break;
1568 case DW_OP_skip:
1569 case DW_OP_bra:
1570 size += 2;
1571 break;
1572 case DW_OP_breg0:
1573 case DW_OP_breg1:
1574 case DW_OP_breg2:
1575 case DW_OP_breg3:
1576 case DW_OP_breg4:
1577 case DW_OP_breg5:
1578 case DW_OP_breg6:
1579 case DW_OP_breg7:
1580 case DW_OP_breg8:
1581 case DW_OP_breg9:
1582 case DW_OP_breg10:
1583 case DW_OP_breg11:
1584 case DW_OP_breg12:
1585 case DW_OP_breg13:
1586 case DW_OP_breg14:
1587 case DW_OP_breg15:
1588 case DW_OP_breg16:
1589 case DW_OP_breg17:
1590 case DW_OP_breg18:
1591 case DW_OP_breg19:
1592 case DW_OP_breg20:
1593 case DW_OP_breg21:
1594 case DW_OP_breg22:
1595 case DW_OP_breg23:
1596 case DW_OP_breg24:
1597 case DW_OP_breg25:
1598 case DW_OP_breg26:
1599 case DW_OP_breg27:
1600 case DW_OP_breg28:
1601 case DW_OP_breg29:
1602 case DW_OP_breg30:
1603 case DW_OP_breg31:
1604 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1605 break;
1606 case DW_OP_regx:
1607 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1608 break;
1609 case DW_OP_fbreg:
1610 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1611 break;
1612 case DW_OP_bregx:
1613 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1614 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1615 break;
1616 case DW_OP_piece:
1617 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1618 break;
1619 case DW_OP_bit_piece:
1620 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1621 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1622 break;
1623 case DW_OP_deref_size:
1624 case DW_OP_xderef_size:
1625 size += 1;
1626 break;
1627 case DW_OP_call2:
1628 size += 2;
1629 break;
1630 case DW_OP_call4:
1631 size += 4;
1632 break;
1633 case DW_OP_call_ref:
1634 size += DWARF_REF_SIZE;
1635 break;
1636 case DW_OP_implicit_value:
1637 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1638 + loc->dw_loc_oprnd1.v.val_unsigned;
1639 break;
1640 case DW_OP_GNU_implicit_pointer:
1641 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1642 break;
1643 case DW_OP_GNU_entry_value:
1644 {
1645 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1646 size += size_of_uleb128 (op_size) + op_size;
1647 break;
1648 }
1649 case DW_OP_GNU_const_type:
1650 {
1651 unsigned long o
1652 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1653 size += size_of_uleb128 (o) + 1;
1654 switch (loc->dw_loc_oprnd2.val_class)
1655 {
1656 case dw_val_class_vec:
1657 size += loc->dw_loc_oprnd2.v.val_vec.length
1658 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1659 break;
1660 case dw_val_class_const:
1661 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1662 break;
1663 case dw_val_class_const_double:
1664 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1665 break;
1666 case dw_val_class_wide_int:
1667 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1668 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1669 break;
1670 default:
1671 gcc_unreachable ();
1672 }
1673 break;
1674 }
1675 case DW_OP_GNU_regval_type:
1676 {
1677 unsigned long o
1678 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1679 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1680 + size_of_uleb128 (o);
1681 }
1682 break;
1683 case DW_OP_GNU_deref_type:
1684 {
1685 unsigned long o
1686 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1687 size += 1 + size_of_uleb128 (o);
1688 }
1689 break;
1690 case DW_OP_GNU_convert:
1691 case DW_OP_GNU_reinterpret:
1692 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1693 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1694 else
1695 {
1696 unsigned long o
1697 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1698 size += size_of_uleb128 (o);
1699 }
1700 break;
1701 case DW_OP_GNU_parameter_ref:
1702 size += 4;
1703 break;
1704 default:
1705 break;
1706 }
1707
1708 return size;
1709 }
1710
1711 /* Return the size of a series of location descriptors. */
1712
1713 unsigned long
1714 size_of_locs (dw_loc_descr_ref loc)
1715 {
1716 dw_loc_descr_ref l;
1717 unsigned long size;
1718
1719 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1720 field, to avoid writing to a PCH file. */
1721 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1722 {
1723 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1724 break;
1725 size += size_of_loc_descr (l);
1726 }
1727 if (! l)
1728 return size;
1729
1730 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1731 {
1732 l->dw_loc_addr = size;
1733 size += size_of_loc_descr (l);
1734 }
1735
1736 return size;
1737 }
1738
1739 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1740 static void get_ref_die_offset_label (char *, dw_die_ref);
1741 static unsigned long int get_ref_die_offset (dw_die_ref);
1742
1743 /* Output location description stack opcode's operands (if any).
1744 The for_eh_or_skip parameter controls whether register numbers are
1745 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1746 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1747 info). This should be suppressed for the cases that have not been converted
1748 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1749
1750 static void
1751 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1752 {
1753 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1754 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1755
1756 switch (loc->dw_loc_opc)
1757 {
1758 #ifdef DWARF2_DEBUGGING_INFO
1759 case DW_OP_const2u:
1760 case DW_OP_const2s:
1761 dw2_asm_output_data (2, val1->v.val_int, NULL);
1762 break;
1763 case DW_OP_const4u:
1764 if (loc->dtprel)
1765 {
1766 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1767 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1768 val1->v.val_addr);
1769 fputc ('\n', asm_out_file);
1770 break;
1771 }
1772 /* FALLTHRU */
1773 case DW_OP_const4s:
1774 dw2_asm_output_data (4, val1->v.val_int, NULL);
1775 break;
1776 case DW_OP_const8u:
1777 if (loc->dtprel)
1778 {
1779 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1780 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1781 val1->v.val_addr);
1782 fputc ('\n', asm_out_file);
1783 break;
1784 }
1785 /* FALLTHRU */
1786 case DW_OP_const8s:
1787 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1788 dw2_asm_output_data (8, val1->v.val_int, NULL);
1789 break;
1790 case DW_OP_skip:
1791 case DW_OP_bra:
1792 {
1793 int offset;
1794
1795 gcc_assert (val1->val_class == dw_val_class_loc);
1796 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1797
1798 dw2_asm_output_data (2, offset, NULL);
1799 }
1800 break;
1801 case DW_OP_implicit_value:
1802 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1803 switch (val2->val_class)
1804 {
1805 case dw_val_class_const:
1806 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1807 break;
1808 case dw_val_class_vec:
1809 {
1810 unsigned int elt_size = val2->v.val_vec.elt_size;
1811 unsigned int len = val2->v.val_vec.length;
1812 unsigned int i;
1813 unsigned char *p;
1814
1815 if (elt_size > sizeof (HOST_WIDE_INT))
1816 {
1817 elt_size /= 2;
1818 len *= 2;
1819 }
1820 for (i = 0, p = val2->v.val_vec.array;
1821 i < len;
1822 i++, p += elt_size)
1823 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1824 "fp or vector constant word %u", i);
1825 }
1826 break;
1827 case dw_val_class_const_double:
1828 {
1829 unsigned HOST_WIDE_INT first, second;
1830
1831 if (WORDS_BIG_ENDIAN)
1832 {
1833 first = val2->v.val_double.high;
1834 second = val2->v.val_double.low;
1835 }
1836 else
1837 {
1838 first = val2->v.val_double.low;
1839 second = val2->v.val_double.high;
1840 }
1841 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1842 first, NULL);
1843 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1844 second, NULL);
1845 }
1846 break;
1847 case dw_val_class_wide_int:
1848 {
1849 int i;
1850 int len = get_full_len (*val2->v.val_wide);
1851 if (WORDS_BIG_ENDIAN)
1852 for (i = len - 1; i >= 0; --i)
1853 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1854 val2->v.val_wide->elt (i), NULL);
1855 else
1856 for (i = 0; i < len; ++i)
1857 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1858 val2->v.val_wide->elt (i), NULL);
1859 }
1860 break;
1861 case dw_val_class_addr:
1862 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1863 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1864 break;
1865 default:
1866 gcc_unreachable ();
1867 }
1868 break;
1869 #else
1870 case DW_OP_const2u:
1871 case DW_OP_const2s:
1872 case DW_OP_const4u:
1873 case DW_OP_const4s:
1874 case DW_OP_const8u:
1875 case DW_OP_const8s:
1876 case DW_OP_skip:
1877 case DW_OP_bra:
1878 case DW_OP_implicit_value:
1879 /* We currently don't make any attempt to make sure these are
1880 aligned properly like we do for the main unwind info, so
1881 don't support emitting things larger than a byte if we're
1882 only doing unwinding. */
1883 gcc_unreachable ();
1884 #endif
1885 case DW_OP_const1u:
1886 case DW_OP_const1s:
1887 dw2_asm_output_data (1, val1->v.val_int, NULL);
1888 break;
1889 case DW_OP_constu:
1890 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1891 break;
1892 case DW_OP_consts:
1893 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1894 break;
1895 case DW_OP_pick:
1896 dw2_asm_output_data (1, val1->v.val_int, NULL);
1897 break;
1898 case DW_OP_plus_uconst:
1899 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1900 break;
1901 case DW_OP_breg0:
1902 case DW_OP_breg1:
1903 case DW_OP_breg2:
1904 case DW_OP_breg3:
1905 case DW_OP_breg4:
1906 case DW_OP_breg5:
1907 case DW_OP_breg6:
1908 case DW_OP_breg7:
1909 case DW_OP_breg8:
1910 case DW_OP_breg9:
1911 case DW_OP_breg10:
1912 case DW_OP_breg11:
1913 case DW_OP_breg12:
1914 case DW_OP_breg13:
1915 case DW_OP_breg14:
1916 case DW_OP_breg15:
1917 case DW_OP_breg16:
1918 case DW_OP_breg17:
1919 case DW_OP_breg18:
1920 case DW_OP_breg19:
1921 case DW_OP_breg20:
1922 case DW_OP_breg21:
1923 case DW_OP_breg22:
1924 case DW_OP_breg23:
1925 case DW_OP_breg24:
1926 case DW_OP_breg25:
1927 case DW_OP_breg26:
1928 case DW_OP_breg27:
1929 case DW_OP_breg28:
1930 case DW_OP_breg29:
1931 case DW_OP_breg30:
1932 case DW_OP_breg31:
1933 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1934 break;
1935 case DW_OP_regx:
1936 {
1937 unsigned r = val1->v.val_unsigned;
1938 if (for_eh_or_skip >= 0)
1939 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1940 gcc_assert (size_of_uleb128 (r)
1941 == size_of_uleb128 (val1->v.val_unsigned));
1942 dw2_asm_output_data_uleb128 (r, NULL);
1943 }
1944 break;
1945 case DW_OP_fbreg:
1946 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1947 break;
1948 case DW_OP_bregx:
1949 {
1950 unsigned r = val1->v.val_unsigned;
1951 if (for_eh_or_skip >= 0)
1952 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1953 gcc_assert (size_of_uleb128 (r)
1954 == size_of_uleb128 (val1->v.val_unsigned));
1955 dw2_asm_output_data_uleb128 (r, NULL);
1956 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1957 }
1958 break;
1959 case DW_OP_piece:
1960 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1961 break;
1962 case DW_OP_bit_piece:
1963 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1964 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1965 break;
1966 case DW_OP_deref_size:
1967 case DW_OP_xderef_size:
1968 dw2_asm_output_data (1, val1->v.val_int, NULL);
1969 break;
1970
1971 case DW_OP_addr:
1972 if (loc->dtprel)
1973 {
1974 if (targetm.asm_out.output_dwarf_dtprel)
1975 {
1976 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1977 DWARF2_ADDR_SIZE,
1978 val1->v.val_addr);
1979 fputc ('\n', asm_out_file);
1980 }
1981 else
1982 gcc_unreachable ();
1983 }
1984 else
1985 {
1986 #ifdef DWARF2_DEBUGGING_INFO
1987 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1988 #else
1989 gcc_unreachable ();
1990 #endif
1991 }
1992 break;
1993
1994 case DW_OP_GNU_addr_index:
1995 case DW_OP_GNU_const_index:
1996 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1997 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1998 "(index into .debug_addr)");
1999 break;
2000
2001 case DW_OP_GNU_implicit_pointer:
2002 {
2003 char label[MAX_ARTIFICIAL_LABEL_BYTES
2004 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2005 gcc_assert (val1->val_class == dw_val_class_die_ref);
2006 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2007 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2008 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2009 }
2010 break;
2011
2012 case DW_OP_GNU_entry_value:
2013 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2014 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2015 break;
2016
2017 case DW_OP_GNU_const_type:
2018 {
2019 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2020 gcc_assert (o);
2021 dw2_asm_output_data_uleb128 (o, NULL);
2022 switch (val2->val_class)
2023 {
2024 case dw_val_class_const:
2025 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2026 dw2_asm_output_data (1, l, NULL);
2027 dw2_asm_output_data (l, val2->v.val_int, NULL);
2028 break;
2029 case dw_val_class_vec:
2030 {
2031 unsigned int elt_size = val2->v.val_vec.elt_size;
2032 unsigned int len = val2->v.val_vec.length;
2033 unsigned int i;
2034 unsigned char *p;
2035
2036 l = len * elt_size;
2037 dw2_asm_output_data (1, l, NULL);
2038 if (elt_size > sizeof (HOST_WIDE_INT))
2039 {
2040 elt_size /= 2;
2041 len *= 2;
2042 }
2043 for (i = 0, p = val2->v.val_vec.array;
2044 i < len;
2045 i++, p += elt_size)
2046 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2047 "fp or vector constant word %u", i);
2048 }
2049 break;
2050 case dw_val_class_const_double:
2051 {
2052 unsigned HOST_WIDE_INT first, second;
2053 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2054
2055 dw2_asm_output_data (1, 2 * l, NULL);
2056 if (WORDS_BIG_ENDIAN)
2057 {
2058 first = val2->v.val_double.high;
2059 second = val2->v.val_double.low;
2060 }
2061 else
2062 {
2063 first = val2->v.val_double.low;
2064 second = val2->v.val_double.high;
2065 }
2066 dw2_asm_output_data (l, first, NULL);
2067 dw2_asm_output_data (l, second, NULL);
2068 }
2069 break;
2070 case dw_val_class_wide_int:
2071 {
2072 int i;
2073 int len = get_full_len (*val2->v.val_wide);
2074 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2075
2076 dw2_asm_output_data (1, len * l, NULL);
2077 if (WORDS_BIG_ENDIAN)
2078 for (i = len - 1; i >= 0; --i)
2079 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2080 else
2081 for (i = 0; i < len; ++i)
2082 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2083 }
2084 break;
2085 default:
2086 gcc_unreachable ();
2087 }
2088 }
2089 break;
2090 case DW_OP_GNU_regval_type:
2091 {
2092 unsigned r = val1->v.val_unsigned;
2093 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2094 gcc_assert (o);
2095 if (for_eh_or_skip >= 0)
2096 {
2097 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2098 gcc_assert (size_of_uleb128 (r)
2099 == size_of_uleb128 (val1->v.val_unsigned));
2100 }
2101 dw2_asm_output_data_uleb128 (r, NULL);
2102 dw2_asm_output_data_uleb128 (o, NULL);
2103 }
2104 break;
2105 case DW_OP_GNU_deref_type:
2106 {
2107 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2108 gcc_assert (o);
2109 dw2_asm_output_data (1, val1->v.val_int, NULL);
2110 dw2_asm_output_data_uleb128 (o, NULL);
2111 }
2112 break;
2113 case DW_OP_GNU_convert:
2114 case DW_OP_GNU_reinterpret:
2115 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2116 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2117 else
2118 {
2119 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2120 gcc_assert (o);
2121 dw2_asm_output_data_uleb128 (o, NULL);
2122 }
2123 break;
2124
2125 case DW_OP_GNU_parameter_ref:
2126 {
2127 unsigned long o;
2128 gcc_assert (val1->val_class == dw_val_class_die_ref);
2129 o = get_ref_die_offset (val1->v.val_die_ref.die);
2130 dw2_asm_output_data (4, o, NULL);
2131 }
2132 break;
2133
2134 default:
2135 /* Other codes have no operands. */
2136 break;
2137 }
2138 }
2139
2140 /* Output a sequence of location operations.
2141 The for_eh_or_skip parameter controls whether register numbers are
2142 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2143 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2144 info). This should be suppressed for the cases that have not been converted
2145 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2146
2147 void
2148 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2149 {
2150 for (; loc != NULL; loc = loc->dw_loc_next)
2151 {
2152 enum dwarf_location_atom opc = loc->dw_loc_opc;
2153 /* Output the opcode. */
2154 if (for_eh_or_skip >= 0
2155 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2156 {
2157 unsigned r = (opc - DW_OP_breg0);
2158 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2159 gcc_assert (r <= 31);
2160 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2161 }
2162 else if (for_eh_or_skip >= 0
2163 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2164 {
2165 unsigned r = (opc - DW_OP_reg0);
2166 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2167 gcc_assert (r <= 31);
2168 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2169 }
2170
2171 dw2_asm_output_data (1, opc,
2172 "%s", dwarf_stack_op_name (opc));
2173
2174 /* Output the operand(s) (if any). */
2175 output_loc_operands (loc, for_eh_or_skip);
2176 }
2177 }
2178
2179 /* Output location description stack opcode's operands (if any).
2180 The output is single bytes on a line, suitable for .cfi_escape. */
2181
2182 static void
2183 output_loc_operands_raw (dw_loc_descr_ref loc)
2184 {
2185 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2186 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2187
2188 switch (loc->dw_loc_opc)
2189 {
2190 case DW_OP_addr:
2191 case DW_OP_GNU_addr_index:
2192 case DW_OP_GNU_const_index:
2193 case DW_OP_implicit_value:
2194 /* We cannot output addresses in .cfi_escape, only bytes. */
2195 gcc_unreachable ();
2196
2197 case DW_OP_const1u:
2198 case DW_OP_const1s:
2199 case DW_OP_pick:
2200 case DW_OP_deref_size:
2201 case DW_OP_xderef_size:
2202 fputc (',', asm_out_file);
2203 dw2_asm_output_data_raw (1, val1->v.val_int);
2204 break;
2205
2206 case DW_OP_const2u:
2207 case DW_OP_const2s:
2208 fputc (',', asm_out_file);
2209 dw2_asm_output_data_raw (2, val1->v.val_int);
2210 break;
2211
2212 case DW_OP_const4u:
2213 case DW_OP_const4s:
2214 fputc (',', asm_out_file);
2215 dw2_asm_output_data_raw (4, val1->v.val_int);
2216 break;
2217
2218 case DW_OP_const8u:
2219 case DW_OP_const8s:
2220 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2221 fputc (',', asm_out_file);
2222 dw2_asm_output_data_raw (8, val1->v.val_int);
2223 break;
2224
2225 case DW_OP_skip:
2226 case DW_OP_bra:
2227 {
2228 int offset;
2229
2230 gcc_assert (val1->val_class == dw_val_class_loc);
2231 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2232
2233 fputc (',', asm_out_file);
2234 dw2_asm_output_data_raw (2, offset);
2235 }
2236 break;
2237
2238 case DW_OP_regx:
2239 {
2240 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2241 gcc_assert (size_of_uleb128 (r)
2242 == size_of_uleb128 (val1->v.val_unsigned));
2243 fputc (',', asm_out_file);
2244 dw2_asm_output_data_uleb128_raw (r);
2245 }
2246 break;
2247
2248 case DW_OP_constu:
2249 case DW_OP_plus_uconst:
2250 case DW_OP_piece:
2251 fputc (',', asm_out_file);
2252 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2253 break;
2254
2255 case DW_OP_bit_piece:
2256 fputc (',', asm_out_file);
2257 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2258 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2259 break;
2260
2261 case DW_OP_consts:
2262 case DW_OP_breg0:
2263 case DW_OP_breg1:
2264 case DW_OP_breg2:
2265 case DW_OP_breg3:
2266 case DW_OP_breg4:
2267 case DW_OP_breg5:
2268 case DW_OP_breg6:
2269 case DW_OP_breg7:
2270 case DW_OP_breg8:
2271 case DW_OP_breg9:
2272 case DW_OP_breg10:
2273 case DW_OP_breg11:
2274 case DW_OP_breg12:
2275 case DW_OP_breg13:
2276 case DW_OP_breg14:
2277 case DW_OP_breg15:
2278 case DW_OP_breg16:
2279 case DW_OP_breg17:
2280 case DW_OP_breg18:
2281 case DW_OP_breg19:
2282 case DW_OP_breg20:
2283 case DW_OP_breg21:
2284 case DW_OP_breg22:
2285 case DW_OP_breg23:
2286 case DW_OP_breg24:
2287 case DW_OP_breg25:
2288 case DW_OP_breg26:
2289 case DW_OP_breg27:
2290 case DW_OP_breg28:
2291 case DW_OP_breg29:
2292 case DW_OP_breg30:
2293 case DW_OP_breg31:
2294 case DW_OP_fbreg:
2295 fputc (',', asm_out_file);
2296 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2297 break;
2298
2299 case DW_OP_bregx:
2300 {
2301 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2302 gcc_assert (size_of_uleb128 (r)
2303 == size_of_uleb128 (val1->v.val_unsigned));
2304 fputc (',', asm_out_file);
2305 dw2_asm_output_data_uleb128_raw (r);
2306 fputc (',', asm_out_file);
2307 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2308 }
2309 break;
2310
2311 case DW_OP_GNU_implicit_pointer:
2312 case DW_OP_GNU_entry_value:
2313 case DW_OP_GNU_const_type:
2314 case DW_OP_GNU_regval_type:
2315 case DW_OP_GNU_deref_type:
2316 case DW_OP_GNU_convert:
2317 case DW_OP_GNU_reinterpret:
2318 case DW_OP_GNU_parameter_ref:
2319 gcc_unreachable ();
2320 break;
2321
2322 default:
2323 /* Other codes have no operands. */
2324 break;
2325 }
2326 }
2327
2328 void
2329 output_loc_sequence_raw (dw_loc_descr_ref loc)
2330 {
2331 while (1)
2332 {
2333 enum dwarf_location_atom opc = loc->dw_loc_opc;
2334 /* Output the opcode. */
2335 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2336 {
2337 unsigned r = (opc - DW_OP_breg0);
2338 r = DWARF2_FRAME_REG_OUT (r, 1);
2339 gcc_assert (r <= 31);
2340 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2341 }
2342 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2343 {
2344 unsigned r = (opc - DW_OP_reg0);
2345 r = DWARF2_FRAME_REG_OUT (r, 1);
2346 gcc_assert (r <= 31);
2347 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2348 }
2349 /* Output the opcode. */
2350 fprintf (asm_out_file, "%#x", opc);
2351 output_loc_operands_raw (loc);
2352
2353 if (!loc->dw_loc_next)
2354 break;
2355 loc = loc->dw_loc_next;
2356
2357 fputc (',', asm_out_file);
2358 }
2359 }
2360
2361 /* This function builds a dwarf location descriptor sequence from a
2362 dw_cfa_location, adding the given OFFSET to the result of the
2363 expression. */
2364
2365 struct dw_loc_descr_node *
2366 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2367 {
2368 struct dw_loc_descr_node *head, *tmp;
2369
2370 offset += cfa->offset;
2371
2372 if (cfa->indirect)
2373 {
2374 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2375 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2376 head->dw_loc_oprnd1.val_entry = NULL;
2377 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2378 add_loc_descr (&head, tmp);
2379 if (offset != 0)
2380 {
2381 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2382 add_loc_descr (&head, tmp);
2383 }
2384 }
2385 else
2386 head = new_reg_loc_descr (cfa->reg, offset);
2387
2388 return head;
2389 }
2390
2391 /* This function builds a dwarf location descriptor sequence for
2392 the address at OFFSET from the CFA when stack is aligned to
2393 ALIGNMENT byte. */
2394
2395 struct dw_loc_descr_node *
2396 build_cfa_aligned_loc (dw_cfa_location *cfa,
2397 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2398 {
2399 struct dw_loc_descr_node *head;
2400 unsigned int dwarf_fp
2401 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2402
2403 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2404 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2405 {
2406 head = new_reg_loc_descr (dwarf_fp, 0);
2407 add_loc_descr (&head, int_loc_descriptor (alignment));
2408 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2409 loc_descr_plus_const (&head, offset);
2410 }
2411 else
2412 head = new_reg_loc_descr (dwarf_fp, offset);
2413 return head;
2414 }
2415 \f
2416 /* And now, the support for symbolic debugging information. */
2417
2418 /* .debug_str support. */
2419 static int output_indirect_string (void **, void *);
2420
2421 static void dwarf2out_init (const char *);
2422 static void dwarf2out_finish (const char *);
2423 static void dwarf2out_assembly_start (void);
2424 static void dwarf2out_define (unsigned int, const char *);
2425 static void dwarf2out_undef (unsigned int, const char *);
2426 static void dwarf2out_start_source_file (unsigned, const char *);
2427 static void dwarf2out_end_source_file (unsigned);
2428 static void dwarf2out_function_decl (tree);
2429 static void dwarf2out_begin_block (unsigned, unsigned);
2430 static void dwarf2out_end_block (unsigned, unsigned);
2431 static bool dwarf2out_ignore_block (const_tree);
2432 static void dwarf2out_global_decl (tree);
2433 static void dwarf2out_type_decl (tree, int);
2434 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2435 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2436 dw_die_ref);
2437 static void dwarf2out_abstract_function (tree);
2438 static void dwarf2out_var_location (rtx_insn *);
2439 static void dwarf2out_begin_function (tree);
2440 static void dwarf2out_end_function (unsigned int);
2441 static void dwarf2out_set_name (tree, tree);
2442
2443 /* The debug hooks structure. */
2444
2445 const struct gcc_debug_hooks dwarf2_debug_hooks =
2446 {
2447 dwarf2out_init,
2448 dwarf2out_finish,
2449 dwarf2out_assembly_start,
2450 dwarf2out_define,
2451 dwarf2out_undef,
2452 dwarf2out_start_source_file,
2453 dwarf2out_end_source_file,
2454 dwarf2out_begin_block,
2455 dwarf2out_end_block,
2456 dwarf2out_ignore_block,
2457 dwarf2out_source_line,
2458 dwarf2out_begin_prologue,
2459 #if VMS_DEBUGGING_INFO
2460 dwarf2out_vms_end_prologue,
2461 dwarf2out_vms_begin_epilogue,
2462 #else
2463 debug_nothing_int_charstar,
2464 debug_nothing_int_charstar,
2465 #endif
2466 dwarf2out_end_epilogue,
2467 dwarf2out_begin_function,
2468 dwarf2out_end_function, /* end_function */
2469 dwarf2out_function_decl, /* function_decl */
2470 dwarf2out_global_decl,
2471 dwarf2out_type_decl, /* type_decl */
2472 dwarf2out_imported_module_or_decl,
2473 debug_nothing_tree, /* deferred_inline_function */
2474 /* The DWARF 2 backend tries to reduce debugging bloat by not
2475 emitting the abstract description of inline functions until
2476 something tries to reference them. */
2477 dwarf2out_abstract_function, /* outlining_inline_function */
2478 debug_nothing_rtx_code_label, /* label */
2479 debug_nothing_int, /* handle_pch */
2480 dwarf2out_var_location,
2481 dwarf2out_switch_text_section,
2482 dwarf2out_set_name,
2483 1, /* start_end_main_source_file */
2484 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2485 };
2486 \f
2487 /* NOTE: In the comments in this file, many references are made to
2488 "Debugging Information Entries". This term is abbreviated as `DIE'
2489 throughout the remainder of this file. */
2490
2491 /* An internal representation of the DWARF output is built, and then
2492 walked to generate the DWARF debugging info. The walk of the internal
2493 representation is done after the entire program has been compiled.
2494 The types below are used to describe the internal representation. */
2495
2496 /* Whether to put type DIEs into their own section .debug_types instead
2497 of making them part of the .debug_info section. Only supported for
2498 Dwarf V4 or higher and the user didn't disable them through
2499 -fno-debug-types-section. It is more efficient to put them in a
2500 separate comdat sections since the linker will then be able to
2501 remove duplicates. But not all tools support .debug_types sections
2502 yet. */
2503
2504 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2505
2506 /* Various DIE's use offsets relative to the beginning of the
2507 .debug_info section to refer to each other. */
2508
2509 typedef long int dw_offset;
2510
2511 /* Define typedefs here to avoid circular dependencies. */
2512
2513 typedef struct dw_attr_struct *dw_attr_ref;
2514 typedef struct dw_line_info_struct *dw_line_info_ref;
2515 typedef struct pubname_struct *pubname_ref;
2516 typedef struct dw_ranges_struct *dw_ranges_ref;
2517 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2518 typedef struct comdat_type_struct *comdat_type_node_ref;
2519
2520 /* The entries in the line_info table more-or-less mirror the opcodes
2521 that are used in the real dwarf line table. Arrays of these entries
2522 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2523 supported. */
2524
2525 enum dw_line_info_opcode {
2526 /* Emit DW_LNE_set_address; the operand is the label index. */
2527 LI_set_address,
2528
2529 /* Emit a row to the matrix with the given line. This may be done
2530 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2531 special opcodes. */
2532 LI_set_line,
2533
2534 /* Emit a DW_LNS_set_file. */
2535 LI_set_file,
2536
2537 /* Emit a DW_LNS_set_column. */
2538 LI_set_column,
2539
2540 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2541 LI_negate_stmt,
2542
2543 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2544 LI_set_prologue_end,
2545 LI_set_epilogue_begin,
2546
2547 /* Emit a DW_LNE_set_discriminator. */
2548 LI_set_discriminator
2549 };
2550
2551 typedef struct GTY(()) dw_line_info_struct {
2552 enum dw_line_info_opcode opcode;
2553 unsigned int val;
2554 } dw_line_info_entry;
2555
2556
2557 typedef struct GTY(()) dw_line_info_table_struct {
2558 /* The label that marks the end of this section. */
2559 const char *end_label;
2560
2561 /* The values for the last row of the matrix, as collected in the table.
2562 These are used to minimize the changes to the next row. */
2563 unsigned int file_num;
2564 unsigned int line_num;
2565 unsigned int column_num;
2566 int discrim_num;
2567 bool is_stmt;
2568 bool in_use;
2569
2570 vec<dw_line_info_entry, va_gc> *entries;
2571 } dw_line_info_table;
2572
2573 typedef dw_line_info_table *dw_line_info_table_p;
2574
2575
2576 /* Each DIE attribute has a field specifying the attribute kind,
2577 a link to the next attribute in the chain, and an attribute value.
2578 Attributes are typically linked below the DIE they modify. */
2579
2580 typedef struct GTY(()) dw_attr_struct {
2581 enum dwarf_attribute dw_attr;
2582 dw_val_node dw_attr_val;
2583 }
2584 dw_attr_node;
2585
2586
2587 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2588 The children of each node form a circular list linked by
2589 die_sib. die_child points to the node *before* the "first" child node. */
2590
2591 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2592 union die_symbol_or_type_node
2593 {
2594 const char * GTY ((tag ("0"))) die_symbol;
2595 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2596 }
2597 GTY ((desc ("%0.comdat_type_p"))) die_id;
2598 vec<dw_attr_node, va_gc> *die_attr;
2599 dw_die_ref die_parent;
2600 dw_die_ref die_child;
2601 dw_die_ref die_sib;
2602 dw_die_ref die_definition; /* ref from a specification to its definition */
2603 dw_offset die_offset;
2604 unsigned long die_abbrev;
2605 int die_mark;
2606 unsigned int decl_id;
2607 enum dwarf_tag die_tag;
2608 /* Die is used and must not be pruned as unused. */
2609 BOOL_BITFIELD die_perennial_p : 1;
2610 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2611 /* Lots of spare bits. */
2612 }
2613 die_node;
2614
2615 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2616 #define FOR_EACH_CHILD(die, c, expr) do { \
2617 c = die->die_child; \
2618 if (c) do { \
2619 c = c->die_sib; \
2620 expr; \
2621 } while (c != die->die_child); \
2622 } while (0)
2623
2624 /* The pubname structure */
2625
2626 typedef struct GTY(()) pubname_struct {
2627 dw_die_ref die;
2628 const char *name;
2629 }
2630 pubname_entry;
2631
2632
2633 struct GTY(()) dw_ranges_struct {
2634 /* If this is positive, it's a block number, otherwise it's a
2635 bitwise-negated index into dw_ranges_by_label. */
2636 int num;
2637 };
2638
2639 /* A structure to hold a macinfo entry. */
2640
2641 typedef struct GTY(()) macinfo_struct {
2642 unsigned char code;
2643 unsigned HOST_WIDE_INT lineno;
2644 const char *info;
2645 }
2646 macinfo_entry;
2647
2648
2649 struct GTY(()) dw_ranges_by_label_struct {
2650 const char *begin;
2651 const char *end;
2652 };
2653
2654 /* The comdat type node structure. */
2655 typedef struct GTY(()) comdat_type_struct
2656 {
2657 dw_die_ref root_die;
2658 dw_die_ref type_die;
2659 dw_die_ref skeleton_die;
2660 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2661 struct comdat_type_struct *next;
2662 }
2663 comdat_type_node;
2664
2665 /* The limbo die list structure. */
2666 typedef struct GTY(()) limbo_die_struct {
2667 dw_die_ref die;
2668 tree created_for;
2669 struct limbo_die_struct *next;
2670 }
2671 limbo_die_node;
2672
2673 typedef struct skeleton_chain_struct
2674 {
2675 dw_die_ref old_die;
2676 dw_die_ref new_die;
2677 struct skeleton_chain_struct *parent;
2678 }
2679 skeleton_chain_node;
2680
2681 /* Define a macro which returns nonzero for a TYPE_DECL which was
2682 implicitly generated for a type.
2683
2684 Note that, unlike the C front-end (which generates a NULL named
2685 TYPE_DECL node for each complete tagged type, each array type,
2686 and each function type node created) the C++ front-end generates
2687 a _named_ TYPE_DECL node for each tagged type node created.
2688 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2689 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2690 front-end, but for each type, tagged or not. */
2691
2692 #define TYPE_DECL_IS_STUB(decl) \
2693 (DECL_NAME (decl) == NULL_TREE \
2694 || (DECL_ARTIFICIAL (decl) \
2695 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2696 /* This is necessary for stub decls that \
2697 appear in nested inline functions. */ \
2698 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2699 && (decl_ultimate_origin (decl) \
2700 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2701
2702 /* Information concerning the compilation unit's programming
2703 language, and compiler version. */
2704
2705 /* Fixed size portion of the DWARF compilation unit header. */
2706 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2707 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2708
2709 /* Fixed size portion of the DWARF comdat type unit header. */
2710 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2711 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2712 + DWARF_OFFSET_SIZE)
2713
2714 /* Fixed size portion of public names info. */
2715 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2716
2717 /* Fixed size portion of the address range info. */
2718 #define DWARF_ARANGES_HEADER_SIZE \
2719 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2720 DWARF2_ADDR_SIZE * 2) \
2721 - DWARF_INITIAL_LENGTH_SIZE)
2722
2723 /* Size of padding portion in the address range info. It must be
2724 aligned to twice the pointer size. */
2725 #define DWARF_ARANGES_PAD_SIZE \
2726 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2727 DWARF2_ADDR_SIZE * 2) \
2728 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2729
2730 /* Use assembler line directives if available. */
2731 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2732 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2733 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2734 #else
2735 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2736 #endif
2737 #endif
2738
2739 /* Minimum line offset in a special line info. opcode.
2740 This value was chosen to give a reasonable range of values. */
2741 #define DWARF_LINE_BASE -10
2742
2743 /* First special line opcode - leave room for the standard opcodes. */
2744 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2745
2746 /* Range of line offsets in a special line info. opcode. */
2747 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2748
2749 /* Flag that indicates the initial value of the is_stmt_start flag.
2750 In the present implementation, we do not mark any lines as
2751 the beginning of a source statement, because that information
2752 is not made available by the GCC front-end. */
2753 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2754
2755 /* Maximum number of operations per instruction bundle. */
2756 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2757 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2758 #endif
2759
2760 /* This location is used by calc_die_sizes() to keep track
2761 the offset of each DIE within the .debug_info section. */
2762 static unsigned long next_die_offset;
2763
2764 /* Record the root of the DIE's built for the current compilation unit. */
2765 static GTY(()) dw_die_ref single_comp_unit_die;
2766
2767 /* A list of type DIEs that have been separated into comdat sections. */
2768 static GTY(()) comdat_type_node *comdat_type_list;
2769
2770 /* A list of DIEs with a NULL parent waiting to be relocated. */
2771 static GTY(()) limbo_die_node *limbo_die_list;
2772
2773 /* A list of DIEs for which we may have to generate
2774 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2775 static GTY(()) limbo_die_node *deferred_asm_name;
2776
2777 /* Filenames referenced by this compilation unit. */
2778 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2779
2780 /* A hash table of references to DIE's that describe declarations.
2781 The key is a DECL_UID() which is a unique number identifying each decl. */
2782 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2783
2784 /* A hash table of references to DIE's that describe COMMON blocks.
2785 The key is DECL_UID() ^ die_parent. */
2786 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2787
2788 typedef struct GTY(()) die_arg_entry_struct {
2789 dw_die_ref die;
2790 tree arg;
2791 } die_arg_entry;
2792
2793
2794 /* Node of the variable location list. */
2795 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2796 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2797 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2798 in mode of the EXPR_LIST node and first EXPR_LIST operand
2799 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2800 location or NULL for padding. For larger bitsizes,
2801 mode is 0 and first operand is a CONCAT with bitsize
2802 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2803 NULL as second operand. */
2804 rtx GTY (()) loc;
2805 const char * GTY (()) label;
2806 struct var_loc_node * GTY (()) next;
2807 };
2808
2809 /* Variable location list. */
2810 struct GTY (()) var_loc_list_def {
2811 struct var_loc_node * GTY (()) first;
2812
2813 /* Pointer to the last but one or last element of the
2814 chained list. If the list is empty, both first and
2815 last are NULL, if the list contains just one node
2816 or the last node certainly is not redundant, it points
2817 to the last node, otherwise points to the last but one.
2818 Do not mark it for GC because it is marked through the chain. */
2819 struct var_loc_node * GTY ((skip ("%h"))) last;
2820
2821 /* Pointer to the last element before section switch,
2822 if NULL, either sections weren't switched or first
2823 is after section switch. */
2824 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2825
2826 /* DECL_UID of the variable decl. */
2827 unsigned int decl_id;
2828 };
2829 typedef struct var_loc_list_def var_loc_list;
2830
2831 /* Call argument location list. */
2832 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2833 rtx GTY (()) call_arg_loc_note;
2834 const char * GTY (()) label;
2835 tree GTY (()) block;
2836 bool tail_call_p;
2837 rtx GTY (()) symbol_ref;
2838 struct call_arg_loc_node * GTY (()) next;
2839 };
2840
2841
2842 /* Table of decl location linked lists. */
2843 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2844
2845 /* Head and tail of call_arg_loc chain. */
2846 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2847 static struct call_arg_loc_node *call_arg_loc_last;
2848
2849 /* Number of call sites in the current function. */
2850 static int call_site_count = -1;
2851 /* Number of tail call sites in the current function. */
2852 static int tail_call_site_count = -1;
2853
2854 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2855 DIEs. */
2856 static vec<dw_die_ref> block_map;
2857
2858 /* A cached location list. */
2859 struct GTY (()) cached_dw_loc_list_def {
2860 /* The DECL_UID of the decl that this entry describes. */
2861 unsigned int decl_id;
2862
2863 /* The cached location list. */
2864 dw_loc_list_ref loc_list;
2865 };
2866 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2867
2868 /* Table of cached location lists. */
2869 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2870
2871 /* A pointer to the base of a list of references to DIE's that
2872 are uniquely identified by their tag, presence/absence of
2873 children DIE's, and list of attribute/value pairs. */
2874 static GTY((length ("abbrev_die_table_allocated")))
2875 dw_die_ref *abbrev_die_table;
2876
2877 /* Number of elements currently allocated for abbrev_die_table. */
2878 static GTY(()) unsigned abbrev_die_table_allocated;
2879
2880 /* Number of elements in type_die_table currently in use. */
2881 static GTY(()) unsigned abbrev_die_table_in_use;
2882
2883 /* Size (in elements) of increments by which we may expand the
2884 abbrev_die_table. */
2885 #define ABBREV_DIE_TABLE_INCREMENT 256
2886
2887 /* A global counter for generating labels for line number data. */
2888 static unsigned int line_info_label_num;
2889
2890 /* The current table to which we should emit line number information
2891 for the current function. This will be set up at the beginning of
2892 assembly for the function. */
2893 static dw_line_info_table *cur_line_info_table;
2894
2895 /* The two default tables of line number info. */
2896 static GTY(()) dw_line_info_table *text_section_line_info;
2897 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2898
2899 /* The set of all non-default tables of line number info. */
2900 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2901
2902 /* A flag to tell pubnames/types export if there is an info section to
2903 refer to. */
2904 static bool info_section_emitted;
2905
2906 /* A pointer to the base of a table that contains a list of publicly
2907 accessible names. */
2908 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2909
2910 /* A pointer to the base of a table that contains a list of publicly
2911 accessible types. */
2912 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2913
2914 /* A pointer to the base of a table that contains a list of macro
2915 defines/undefines (and file start/end markers). */
2916 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2917
2918 /* True if .debug_macinfo or .debug_macros section is going to be
2919 emitted. */
2920 #define have_macinfo \
2921 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2922 && !macinfo_table->is_empty ())
2923
2924 /* Array of dies for which we should generate .debug_ranges info. */
2925 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2926
2927 /* Number of elements currently allocated for ranges_table. */
2928 static GTY(()) unsigned ranges_table_allocated;
2929
2930 /* Number of elements in ranges_table currently in use. */
2931 static GTY(()) unsigned ranges_table_in_use;
2932
2933 /* Array of pairs of labels referenced in ranges_table. */
2934 static GTY ((length ("ranges_by_label_allocated")))
2935 dw_ranges_by_label_ref ranges_by_label;
2936
2937 /* Number of elements currently allocated for ranges_by_label. */
2938 static GTY(()) unsigned ranges_by_label_allocated;
2939
2940 /* Number of elements in ranges_by_label currently in use. */
2941 static GTY(()) unsigned ranges_by_label_in_use;
2942
2943 /* Size (in elements) of increments by which we may expand the
2944 ranges_table. */
2945 #define RANGES_TABLE_INCREMENT 64
2946
2947 /* Whether we have location lists that need outputting */
2948 static GTY(()) bool have_location_lists;
2949
2950 /* Unique label counter. */
2951 static GTY(()) unsigned int loclabel_num;
2952
2953 /* Unique label counter for point-of-call tables. */
2954 static GTY(()) unsigned int poc_label_num;
2955
2956 /* Record whether the function being analyzed contains inlined functions. */
2957 static int current_function_has_inlines;
2958
2959 /* The last file entry emitted by maybe_emit_file(). */
2960 static GTY(()) struct dwarf_file_data * last_emitted_file;
2961
2962 /* Number of internal labels generated by gen_internal_sym(). */
2963 static GTY(()) int label_num;
2964
2965 /* Cached result of previous call to lookup_filename. */
2966 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2967
2968 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2969
2970 /* Instances of generic types for which we need to generate debug
2971 info that describe their generic parameters and arguments. That
2972 generation needs to happen once all types are properly laid out so
2973 we do it at the end of compilation. */
2974 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2975
2976 /* Offset from the "steady-state frame pointer" to the frame base,
2977 within the current function. */
2978 static HOST_WIDE_INT frame_pointer_fb_offset;
2979 static bool frame_pointer_fb_offset_valid;
2980
2981 static vec<dw_die_ref> base_types;
2982
2983 /* Forward declarations for functions defined in this file. */
2984
2985 static int is_pseudo_reg (const_rtx);
2986 static tree type_main_variant (tree);
2987 static int is_tagged_type (const_tree);
2988 static const char *dwarf_tag_name (unsigned);
2989 static const char *dwarf_attr_name (unsigned);
2990 static const char *dwarf_form_name (unsigned);
2991 static tree decl_ultimate_origin (const_tree);
2992 static tree decl_class_context (tree);
2993 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2994 static inline enum dw_val_class AT_class (dw_attr_ref);
2995 static inline unsigned int AT_index (dw_attr_ref);
2996 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2997 static inline unsigned AT_flag (dw_attr_ref);
2998 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2999 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3000 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3001 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3002 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3003 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3004 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3005 unsigned int, unsigned char *);
3006 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3007 static hashval_t debug_str_do_hash (const void *);
3008 static int debug_str_eq (const void *, const void *);
3009 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3010 static inline const char *AT_string (dw_attr_ref);
3011 static enum dwarf_form AT_string_form (dw_attr_ref);
3012 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3013 static void add_AT_specification (dw_die_ref, dw_die_ref);
3014 static inline dw_die_ref AT_ref (dw_attr_ref);
3015 static inline int AT_ref_external (dw_attr_ref);
3016 static inline void set_AT_ref_external (dw_attr_ref, int);
3017 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3018 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3019 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3020 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3021 dw_loc_list_ref);
3022 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3023 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3024 static void remove_addr_table_entry (addr_table_entry *);
3025 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3026 static inline rtx AT_addr (dw_attr_ref);
3027 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3028 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3029 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3030 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3031 unsigned HOST_WIDE_INT);
3032 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3033 unsigned long, bool);
3034 static inline const char *AT_lbl (dw_attr_ref);
3035 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3036 static const char *get_AT_low_pc (dw_die_ref);
3037 static const char *get_AT_hi_pc (dw_die_ref);
3038 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3039 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3040 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3041 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3042 static bool is_cxx (void);
3043 static bool is_fortran (void);
3044 static bool is_ada (void);
3045 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3046 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3047 static void add_child_die (dw_die_ref, dw_die_ref);
3048 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3049 static dw_die_ref lookup_type_die (tree);
3050 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3051 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3052 static void equate_type_number_to_die (tree, dw_die_ref);
3053 static hashval_t decl_die_table_hash (const void *);
3054 static int decl_die_table_eq (const void *, const void *);
3055 static dw_die_ref lookup_decl_die (tree);
3056 static hashval_t common_block_die_table_hash (const void *);
3057 static int common_block_die_table_eq (const void *, const void *);
3058 static hashval_t decl_loc_table_hash (const void *);
3059 static int decl_loc_table_eq (const void *, const void *);
3060 static var_loc_list *lookup_decl_loc (const_tree);
3061 static void equate_decl_number_to_die (tree, dw_die_ref);
3062 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3063 static void print_spaces (FILE *);
3064 static void print_die (dw_die_ref, FILE *);
3065 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3066 static dw_die_ref pop_compile_unit (dw_die_ref);
3067 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3068 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3069 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3070 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3071 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3072 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3073 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3074 struct md5_ctx *, int *);
3075 struct checksum_attributes;
3076 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3077 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3078 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3079 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3080 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3081 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3082 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3083 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3084 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3085 static void compute_section_prefix (dw_die_ref);
3086 static int is_type_die (dw_die_ref);
3087 static int is_comdat_die (dw_die_ref);
3088 static int is_symbol_die (dw_die_ref);
3089 static inline bool is_template_instantiation (dw_die_ref);
3090 static void assign_symbol_names (dw_die_ref);
3091 static void break_out_includes (dw_die_ref);
3092 static int is_declaration_die (dw_die_ref);
3093 static int should_move_die_to_comdat (dw_die_ref);
3094 static dw_die_ref clone_as_declaration (dw_die_ref);
3095 static dw_die_ref clone_die (dw_die_ref);
3096 static dw_die_ref clone_tree (dw_die_ref);
3097 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3098 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3099 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3100 static dw_die_ref generate_skeleton (dw_die_ref);
3101 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3102 dw_die_ref,
3103 dw_die_ref);
3104 static void break_out_comdat_types (dw_die_ref);
3105 static void copy_decls_for_unworthy_types (dw_die_ref);
3106
3107 static void add_sibling_attributes (dw_die_ref);
3108 static void output_location_lists (dw_die_ref);
3109 static int constant_size (unsigned HOST_WIDE_INT);
3110 static unsigned long size_of_die (dw_die_ref);
3111 static void calc_die_sizes (dw_die_ref);
3112 static void calc_base_type_die_sizes (void);
3113 static void mark_dies (dw_die_ref);
3114 static void unmark_dies (dw_die_ref);
3115 static void unmark_all_dies (dw_die_ref);
3116 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3117 static unsigned long size_of_aranges (void);
3118 static enum dwarf_form value_format (dw_attr_ref);
3119 static void output_value_format (dw_attr_ref);
3120 static void output_abbrev_section (void);
3121 static void output_die_abbrevs (unsigned long, dw_die_ref);
3122 static void output_die_symbol (dw_die_ref);
3123 static void output_die (dw_die_ref);
3124 static void output_compilation_unit_header (void);
3125 static void output_comp_unit (dw_die_ref, int);
3126 static void output_comdat_type_unit (comdat_type_node *);
3127 static const char *dwarf2_name (tree, int);
3128 static void add_pubname (tree, dw_die_ref);
3129 static void add_enumerator_pubname (const char *, dw_die_ref);
3130 static void add_pubname_string (const char *, dw_die_ref);
3131 static void add_pubtype (tree, dw_die_ref);
3132 static void output_pubnames (vec<pubname_entry, va_gc> *);
3133 static void output_aranges (unsigned long);
3134 static unsigned int add_ranges_num (int);
3135 static unsigned int add_ranges (const_tree);
3136 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3137 bool *, bool);
3138 static void output_ranges (void);
3139 static dw_line_info_table *new_line_info_table (void);
3140 static void output_line_info (bool);
3141 static void output_file_names (void);
3142 static dw_die_ref base_type_die (tree);
3143 static int is_base_type (tree);
3144 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3145 static int decl_quals (const_tree);
3146 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3147 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3148 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3149 static int type_is_enum (const_tree);
3150 static unsigned int dbx_reg_number (const_rtx);
3151 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3152 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3153 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3154 enum var_init_status);
3155 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3156 enum var_init_status);
3157 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3158 enum var_init_status);
3159 static int is_based_loc (const_rtx);
3160 static int resolve_one_addr (rtx *, void *);
3161 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3162 enum var_init_status);
3163 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3164 enum var_init_status);
3165 static dw_loc_list_ref loc_list_from_tree (tree, int);
3166 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3167 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3168 static tree field_type (const_tree);
3169 static unsigned int simple_type_align_in_bits (const_tree);
3170 static unsigned int simple_decl_align_in_bits (const_tree);
3171 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3172 static HOST_WIDE_INT field_byte_offset (const_tree);
3173 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3174 dw_loc_list_ref);
3175 static void add_data_member_location_attribute (dw_die_ref, tree);
3176 static bool add_const_value_attribute (dw_die_ref, rtx);
3177 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3178 static void insert_wide_int (const wide_int &, unsigned char *, int);
3179 static void insert_float (const_rtx, unsigned char *);
3180 static rtx rtl_for_decl_location (tree);
3181 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3182 enum dwarf_attribute);
3183 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3184 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3185 static void add_name_attribute (dw_die_ref, const char *);
3186 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3187 static void add_comp_dir_attribute (dw_die_ref);
3188 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3189 static void add_subscript_info (dw_die_ref, tree, bool);
3190 static void add_byte_size_attribute (dw_die_ref, tree);
3191 static void add_bit_offset_attribute (dw_die_ref, tree);
3192 static void add_bit_size_attribute (dw_die_ref, tree);
3193 static void add_prototyped_attribute (dw_die_ref, tree);
3194 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3195 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3196 static void add_src_coords_attributes (dw_die_ref, tree);
3197 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3198 static void push_decl_scope (tree);
3199 static void pop_decl_scope (void);
3200 static dw_die_ref scope_die_for (tree, dw_die_ref);
3201 static inline int local_scope_p (dw_die_ref);
3202 static inline int class_scope_p (dw_die_ref);
3203 static inline int class_or_namespace_scope_p (dw_die_ref);
3204 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3205 static void add_calling_convention_attribute (dw_die_ref, tree);
3206 static const char *type_tag (const_tree);
3207 static tree member_declared_type (const_tree);
3208 #if 0
3209 static const char *decl_start_label (tree);
3210 #endif
3211 static void gen_array_type_die (tree, dw_die_ref);
3212 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3213 #if 0
3214 static void gen_entry_point_die (tree, dw_die_ref);
3215 #endif
3216 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3217 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3218 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3219 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3220 static void gen_formal_types_die (tree, dw_die_ref);
3221 static void gen_subprogram_die (tree, dw_die_ref);
3222 static void gen_variable_die (tree, tree, dw_die_ref);
3223 static void gen_const_die (tree, dw_die_ref);
3224 static void gen_label_die (tree, dw_die_ref);
3225 static void gen_lexical_block_die (tree, dw_die_ref, int);
3226 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3227 static void gen_field_die (tree, dw_die_ref);
3228 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3229 static dw_die_ref gen_compile_unit_die (const char *);
3230 static void gen_inheritance_die (tree, tree, dw_die_ref);
3231 static void gen_member_die (tree, dw_die_ref);
3232 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3233 enum debug_info_usage);
3234 static void gen_subroutine_type_die (tree, dw_die_ref);
3235 static void gen_typedef_die (tree, dw_die_ref);
3236 static void gen_type_die (tree, dw_die_ref);
3237 static void gen_block_die (tree, dw_die_ref, int);
3238 static void decls_for_scope (tree, dw_die_ref, int);
3239 static inline int is_redundant_typedef (const_tree);
3240 static bool is_naming_typedef_decl (const_tree);
3241 static inline dw_die_ref get_context_die (tree);
3242 static void gen_namespace_die (tree, dw_die_ref);
3243 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3244 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3245 static dw_die_ref force_decl_die (tree);
3246 static dw_die_ref force_type_die (tree);
3247 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3248 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3249 static struct dwarf_file_data * lookup_filename (const char *);
3250 static void retry_incomplete_types (void);
3251 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3252 static void gen_generic_params_dies (tree);
3253 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3254 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3255 static void splice_child_die (dw_die_ref, dw_die_ref);
3256 static int file_info_cmp (const void *, const void *);
3257 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3258 const char *, const char *);
3259 static void output_loc_list (dw_loc_list_ref);
3260 static char *gen_internal_sym (const char *);
3261 static bool want_pubnames (void);
3262
3263 static void prune_unmark_dies (dw_die_ref);
3264 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3265 static void prune_unused_types_mark (dw_die_ref, int);
3266 static void prune_unused_types_walk (dw_die_ref);
3267 static void prune_unused_types_walk_attribs (dw_die_ref);
3268 static void prune_unused_types_prune (dw_die_ref);
3269 static void prune_unused_types (void);
3270 static int maybe_emit_file (struct dwarf_file_data *fd);
3271 static inline const char *AT_vms_delta1 (dw_attr_ref);
3272 static inline const char *AT_vms_delta2 (dw_attr_ref);
3273 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3274 const char *, const char *);
3275 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3276 static void gen_remaining_tmpl_value_param_die_attribute (void);
3277 static bool generic_type_p (tree);
3278 static void schedule_generic_params_dies_gen (tree t);
3279 static void gen_scheduled_generic_parms_dies (void);
3280
3281 static const char *comp_dir_string (void);
3282
3283 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3284
3285 /* enum for tracking thread-local variables whose address is really an offset
3286 relative to the TLS pointer, which will need link-time relocation, but will
3287 not need relocation by the DWARF consumer. */
3288
3289 enum dtprel_bool
3290 {
3291 dtprel_false = 0,
3292 dtprel_true = 1
3293 };
3294
3295 /* Return the operator to use for an address of a variable. For dtprel_true, we
3296 use DW_OP_const*. For regular variables, which need both link-time
3297 relocation and consumer-level relocation (e.g., to account for shared objects
3298 loaded at a random address), we use DW_OP_addr*. */
3299
3300 static inline enum dwarf_location_atom
3301 dw_addr_op (enum dtprel_bool dtprel)
3302 {
3303 if (dtprel == dtprel_true)
3304 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3305 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3306 else
3307 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3308 }
3309
3310 /* Return a pointer to a newly allocated address location description. If
3311 dwarf_split_debug_info is true, then record the address with the appropriate
3312 relocation. */
3313 static inline dw_loc_descr_ref
3314 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3315 {
3316 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3317
3318 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3319 ref->dw_loc_oprnd1.v.val_addr = addr;
3320 ref->dtprel = dtprel;
3321 if (dwarf_split_debug_info)
3322 ref->dw_loc_oprnd1.val_entry
3323 = add_addr_table_entry (addr,
3324 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3325 else
3326 ref->dw_loc_oprnd1.val_entry = NULL;
3327
3328 return ref;
3329 }
3330
3331 /* Section names used to hold DWARF debugging information. */
3332
3333 #ifndef DEBUG_INFO_SECTION
3334 #define DEBUG_INFO_SECTION ".debug_info"
3335 #endif
3336 #ifndef DEBUG_DWO_INFO_SECTION
3337 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3338 #endif
3339 #ifndef DEBUG_ABBREV_SECTION
3340 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3341 #endif
3342 #ifndef DEBUG_DWO_ABBREV_SECTION
3343 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3344 #endif
3345 #ifndef DEBUG_ARANGES_SECTION
3346 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3347 #endif
3348 #ifndef DEBUG_ADDR_SECTION
3349 #define DEBUG_ADDR_SECTION ".debug_addr"
3350 #endif
3351 #ifndef DEBUG_NORM_MACINFO_SECTION
3352 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3353 #endif
3354 #ifndef DEBUG_DWO_MACINFO_SECTION
3355 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3356 #endif
3357 #ifndef DEBUG_MACINFO_SECTION
3358 #define DEBUG_MACINFO_SECTION \
3359 (!dwarf_split_debug_info \
3360 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3361 #endif
3362 #ifndef DEBUG_NORM_MACRO_SECTION
3363 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3364 #endif
3365 #ifndef DEBUG_DWO_MACRO_SECTION
3366 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3367 #endif
3368 #ifndef DEBUG_MACRO_SECTION
3369 #define DEBUG_MACRO_SECTION \
3370 (!dwarf_split_debug_info \
3371 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3372 #endif
3373 #ifndef DEBUG_LINE_SECTION
3374 #define DEBUG_LINE_SECTION ".debug_line"
3375 #endif
3376 #ifndef DEBUG_DWO_LINE_SECTION
3377 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3378 #endif
3379 #ifndef DEBUG_LOC_SECTION
3380 #define DEBUG_LOC_SECTION ".debug_loc"
3381 #endif
3382 #ifndef DEBUG_DWO_LOC_SECTION
3383 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3384 #endif
3385 #ifndef DEBUG_PUBNAMES_SECTION
3386 #define DEBUG_PUBNAMES_SECTION \
3387 ((debug_generate_pub_sections == 2) \
3388 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3389 #endif
3390 #ifndef DEBUG_PUBTYPES_SECTION
3391 #define DEBUG_PUBTYPES_SECTION \
3392 ((debug_generate_pub_sections == 2) \
3393 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3394 #endif
3395 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3396 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3397 #ifndef DEBUG_STR_OFFSETS_SECTION
3398 #define DEBUG_STR_OFFSETS_SECTION \
3399 (!dwarf_split_debug_info \
3400 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3401 #endif
3402 #ifndef DEBUG_STR_DWO_SECTION
3403 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3404 #endif
3405 #ifndef DEBUG_STR_SECTION
3406 #define DEBUG_STR_SECTION ".debug_str"
3407 #endif
3408 #ifndef DEBUG_RANGES_SECTION
3409 #define DEBUG_RANGES_SECTION ".debug_ranges"
3410 #endif
3411
3412 /* Standard ELF section names for compiled code and data. */
3413 #ifndef TEXT_SECTION_NAME
3414 #define TEXT_SECTION_NAME ".text"
3415 #endif
3416
3417 /* Section flags for .debug_macinfo/.debug_macro section. */
3418 #define DEBUG_MACRO_SECTION_FLAGS \
3419 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3420
3421 /* Section flags for .debug_str section. */
3422 #define DEBUG_STR_SECTION_FLAGS \
3423 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3424 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3425 : SECTION_DEBUG)
3426
3427 /* Section flags for .debug_str.dwo section. */
3428 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3429
3430 /* Labels we insert at beginning sections we can reference instead of
3431 the section names themselves. */
3432
3433 #ifndef TEXT_SECTION_LABEL
3434 #define TEXT_SECTION_LABEL "Ltext"
3435 #endif
3436 #ifndef COLD_TEXT_SECTION_LABEL
3437 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3438 #endif
3439 #ifndef DEBUG_LINE_SECTION_LABEL
3440 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3441 #endif
3442 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3443 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3444 #endif
3445 #ifndef DEBUG_INFO_SECTION_LABEL
3446 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3447 #endif
3448 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3449 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3450 #endif
3451 #ifndef DEBUG_ABBREV_SECTION_LABEL
3452 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3453 #endif
3454 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3455 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3456 #endif
3457 #ifndef DEBUG_ADDR_SECTION_LABEL
3458 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3459 #endif
3460 #ifndef DEBUG_LOC_SECTION_LABEL
3461 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3462 #endif
3463 #ifndef DEBUG_RANGES_SECTION_LABEL
3464 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3465 #endif
3466 #ifndef DEBUG_MACINFO_SECTION_LABEL
3467 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3468 #endif
3469 #ifndef DEBUG_MACRO_SECTION_LABEL
3470 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3471 #endif
3472 #define SKELETON_COMP_DIE_ABBREV 1
3473 #define SKELETON_TYPE_DIE_ABBREV 2
3474
3475 /* Definitions of defaults for formats and names of various special
3476 (artificial) labels which may be generated within this file (when the -g
3477 options is used and DWARF2_DEBUGGING_INFO is in effect.
3478 If necessary, these may be overridden from within the tm.h file, but
3479 typically, overriding these defaults is unnecessary. */
3480
3481 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3482 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3483 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3484 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3485 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3486 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3487 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3488 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3489 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3490 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3491 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3492 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3493 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3494 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3495
3496 #ifndef TEXT_END_LABEL
3497 #define TEXT_END_LABEL "Letext"
3498 #endif
3499 #ifndef COLD_END_LABEL
3500 #define COLD_END_LABEL "Letext_cold"
3501 #endif
3502 #ifndef BLOCK_BEGIN_LABEL
3503 #define BLOCK_BEGIN_LABEL "LBB"
3504 #endif
3505 #ifndef BLOCK_END_LABEL
3506 #define BLOCK_END_LABEL "LBE"
3507 #endif
3508 #ifndef LINE_CODE_LABEL
3509 #define LINE_CODE_LABEL "LM"
3510 #endif
3511
3512 \f
3513 /* Return the root of the DIE's built for the current compilation unit. */
3514 static dw_die_ref
3515 comp_unit_die (void)
3516 {
3517 if (!single_comp_unit_die)
3518 single_comp_unit_die = gen_compile_unit_die (NULL);
3519 return single_comp_unit_die;
3520 }
3521
3522 /* We allow a language front-end to designate a function that is to be
3523 called to "demangle" any name before it is put into a DIE. */
3524
3525 static const char *(*demangle_name_func) (const char *);
3526
3527 void
3528 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3529 {
3530 demangle_name_func = func;
3531 }
3532
3533 /* Test if rtl node points to a pseudo register. */
3534
3535 static inline int
3536 is_pseudo_reg (const_rtx rtl)
3537 {
3538 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3539 || (GET_CODE (rtl) == SUBREG
3540 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3541 }
3542
3543 /* Return a reference to a type, with its const and volatile qualifiers
3544 removed. */
3545
3546 static inline tree
3547 type_main_variant (tree type)
3548 {
3549 type = TYPE_MAIN_VARIANT (type);
3550
3551 /* ??? There really should be only one main variant among any group of
3552 variants of a given type (and all of the MAIN_VARIANT values for all
3553 members of the group should point to that one type) but sometimes the C
3554 front-end messes this up for array types, so we work around that bug
3555 here. */
3556 if (TREE_CODE (type) == ARRAY_TYPE)
3557 while (type != TYPE_MAIN_VARIANT (type))
3558 type = TYPE_MAIN_VARIANT (type);
3559
3560 return type;
3561 }
3562
3563 /* Return nonzero if the given type node represents a tagged type. */
3564
3565 static inline int
3566 is_tagged_type (const_tree type)
3567 {
3568 enum tree_code code = TREE_CODE (type);
3569
3570 return (code == RECORD_TYPE || code == UNION_TYPE
3571 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3572 }
3573
3574 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3575
3576 static void
3577 get_ref_die_offset_label (char *label, dw_die_ref ref)
3578 {
3579 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3580 }
3581
3582 /* Return die_offset of a DIE reference to a base type. */
3583
3584 static unsigned long int
3585 get_base_type_offset (dw_die_ref ref)
3586 {
3587 if (ref->die_offset)
3588 return ref->die_offset;
3589 if (comp_unit_die ()->die_abbrev)
3590 {
3591 calc_base_type_die_sizes ();
3592 gcc_assert (ref->die_offset);
3593 }
3594 return ref->die_offset;
3595 }
3596
3597 /* Return die_offset of a DIE reference other than base type. */
3598
3599 static unsigned long int
3600 get_ref_die_offset (dw_die_ref ref)
3601 {
3602 gcc_assert (ref->die_offset);
3603 return ref->die_offset;
3604 }
3605
3606 /* Convert a DIE tag into its string name. */
3607
3608 static const char *
3609 dwarf_tag_name (unsigned int tag)
3610 {
3611 const char *name = get_DW_TAG_name (tag);
3612
3613 if (name != NULL)
3614 return name;
3615
3616 return "DW_TAG_<unknown>";
3617 }
3618
3619 /* Convert a DWARF attribute code into its string name. */
3620
3621 static const char *
3622 dwarf_attr_name (unsigned int attr)
3623 {
3624 const char *name;
3625
3626 switch (attr)
3627 {
3628 #if VMS_DEBUGGING_INFO
3629 case DW_AT_HP_prologue:
3630 return "DW_AT_HP_prologue";
3631 #else
3632 case DW_AT_MIPS_loop_unroll_factor:
3633 return "DW_AT_MIPS_loop_unroll_factor";
3634 #endif
3635
3636 #if VMS_DEBUGGING_INFO
3637 case DW_AT_HP_epilogue:
3638 return "DW_AT_HP_epilogue";
3639 #else
3640 case DW_AT_MIPS_stride:
3641 return "DW_AT_MIPS_stride";
3642 #endif
3643 }
3644
3645 name = get_DW_AT_name (attr);
3646
3647 if (name != NULL)
3648 return name;
3649
3650 return "DW_AT_<unknown>";
3651 }
3652
3653 /* Convert a DWARF value form code into its string name. */
3654
3655 static const char *
3656 dwarf_form_name (unsigned int form)
3657 {
3658 const char *name = get_DW_FORM_name (form);
3659
3660 if (name != NULL)
3661 return name;
3662
3663 return "DW_FORM_<unknown>";
3664 }
3665 \f
3666 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3667 instance of an inlined instance of a decl which is local to an inline
3668 function, so we have to trace all of the way back through the origin chain
3669 to find out what sort of node actually served as the original seed for the
3670 given block. */
3671
3672 static tree
3673 decl_ultimate_origin (const_tree decl)
3674 {
3675 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3676 return NULL_TREE;
3677
3678 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3679 nodes in the function to point to themselves; ignore that if
3680 we're trying to output the abstract instance of this function. */
3681 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3682 return NULL_TREE;
3683
3684 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3685 most distant ancestor, this should never happen. */
3686 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3687
3688 return DECL_ABSTRACT_ORIGIN (decl);
3689 }
3690
3691 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3692 of a virtual function may refer to a base class, so we check the 'this'
3693 parameter. */
3694
3695 static tree
3696 decl_class_context (tree decl)
3697 {
3698 tree context = NULL_TREE;
3699
3700 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3701 context = DECL_CONTEXT (decl);
3702 else
3703 context = TYPE_MAIN_VARIANT
3704 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3705
3706 if (context && !TYPE_P (context))
3707 context = NULL_TREE;
3708
3709 return context;
3710 }
3711 \f
3712 /* Add an attribute/value pair to a DIE. */
3713
3714 static inline void
3715 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3716 {
3717 /* Maybe this should be an assert? */
3718 if (die == NULL)
3719 return;
3720
3721 vec_safe_reserve (die->die_attr, 1);
3722 vec_safe_push (die->die_attr, *attr);
3723 }
3724
3725 static inline enum dw_val_class
3726 AT_class (dw_attr_ref a)
3727 {
3728 return a->dw_attr_val.val_class;
3729 }
3730
3731 /* Return the index for any attribute that will be referenced with a
3732 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3733 are stored in dw_attr_val.v.val_str for reference counting
3734 pruning. */
3735
3736 static inline unsigned int
3737 AT_index (dw_attr_ref a)
3738 {
3739 if (AT_class (a) == dw_val_class_str)
3740 return a->dw_attr_val.v.val_str->index;
3741 else if (a->dw_attr_val.val_entry != NULL)
3742 return a->dw_attr_val.val_entry->index;
3743 return NOT_INDEXED;
3744 }
3745
3746 /* Add a flag value attribute to a DIE. */
3747
3748 static inline void
3749 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3750 {
3751 dw_attr_node attr;
3752
3753 attr.dw_attr = attr_kind;
3754 attr.dw_attr_val.val_class = dw_val_class_flag;
3755 attr.dw_attr_val.val_entry = NULL;
3756 attr.dw_attr_val.v.val_flag = flag;
3757 add_dwarf_attr (die, &attr);
3758 }
3759
3760 static inline unsigned
3761 AT_flag (dw_attr_ref a)
3762 {
3763 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3764 return a->dw_attr_val.v.val_flag;
3765 }
3766
3767 /* Add a signed integer attribute value to a DIE. */
3768
3769 static inline void
3770 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3771 {
3772 dw_attr_node attr;
3773
3774 attr.dw_attr = attr_kind;
3775 attr.dw_attr_val.val_class = dw_val_class_const;
3776 attr.dw_attr_val.val_entry = NULL;
3777 attr.dw_attr_val.v.val_int = int_val;
3778 add_dwarf_attr (die, &attr);
3779 }
3780
3781 static inline HOST_WIDE_INT
3782 AT_int (dw_attr_ref a)
3783 {
3784 gcc_assert (a && AT_class (a) == dw_val_class_const);
3785 return a->dw_attr_val.v.val_int;
3786 }
3787
3788 /* Add an unsigned integer attribute value to a DIE. */
3789
3790 static inline void
3791 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3792 unsigned HOST_WIDE_INT unsigned_val)
3793 {
3794 dw_attr_node attr;
3795
3796 attr.dw_attr = attr_kind;
3797 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3798 attr.dw_attr_val.val_entry = NULL;
3799 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3800 add_dwarf_attr (die, &attr);
3801 }
3802
3803 static inline unsigned HOST_WIDE_INT
3804 AT_unsigned (dw_attr_ref a)
3805 {
3806 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3807 return a->dw_attr_val.v.val_unsigned;
3808 }
3809
3810 /* Add an unsigned wide integer attribute value to a DIE. */
3811
3812 static inline void
3813 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3814 const wide_int& w)
3815 {
3816 dw_attr_node attr;
3817
3818 attr.dw_attr = attr_kind;
3819 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3820 attr.dw_attr_val.v.val_wide = ggc_cleared_alloc<wide_int> ();
3821 *attr.dw_attr_val.v.val_wide = w;
3822 add_dwarf_attr (die, &attr);
3823 }
3824
3825 /* Add an unsigned double integer attribute value to a DIE. */
3826
3827 static inline void
3828 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3829 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3830 {
3831 dw_attr_node attr;
3832
3833 attr.dw_attr = attr_kind;
3834 attr.dw_attr_val.val_class = dw_val_class_const_double;
3835 attr.dw_attr_val.val_entry = NULL;
3836 attr.dw_attr_val.v.val_double.high = high;
3837 attr.dw_attr_val.v.val_double.low = low;
3838 add_dwarf_attr (die, &attr);
3839 }
3840
3841 /* Add a floating point attribute value to a DIE and return it. */
3842
3843 static inline void
3844 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3845 unsigned int length, unsigned int elt_size, unsigned char *array)
3846 {
3847 dw_attr_node attr;
3848
3849 attr.dw_attr = attr_kind;
3850 attr.dw_attr_val.val_class = dw_val_class_vec;
3851 attr.dw_attr_val.val_entry = NULL;
3852 attr.dw_attr_val.v.val_vec.length = length;
3853 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3854 attr.dw_attr_val.v.val_vec.array = array;
3855 add_dwarf_attr (die, &attr);
3856 }
3857
3858 /* Add an 8-byte data attribute value to a DIE. */
3859
3860 static inline void
3861 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3862 unsigned char data8[8])
3863 {
3864 dw_attr_node attr;
3865
3866 attr.dw_attr = attr_kind;
3867 attr.dw_attr_val.val_class = dw_val_class_data8;
3868 attr.dw_attr_val.val_entry = NULL;
3869 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3870 add_dwarf_attr (die, &attr);
3871 }
3872
3873 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3874 dwarf_split_debug_info, address attributes in dies destined for the
3875 final executable have force_direct set to avoid using indexed
3876 references. */
3877
3878 static inline void
3879 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3880 bool force_direct)
3881 {
3882 dw_attr_node attr;
3883 char * lbl_id;
3884
3885 lbl_id = xstrdup (lbl_low);
3886 attr.dw_attr = DW_AT_low_pc;
3887 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3888 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3889 if (dwarf_split_debug_info && !force_direct)
3890 attr.dw_attr_val.val_entry
3891 = add_addr_table_entry (lbl_id, ate_kind_label);
3892 else
3893 attr.dw_attr_val.val_entry = NULL;
3894 add_dwarf_attr (die, &attr);
3895
3896 attr.dw_attr = DW_AT_high_pc;
3897 if (dwarf_version < 4)
3898 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3899 else
3900 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3901 lbl_id = xstrdup (lbl_high);
3902 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3903 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3904 && dwarf_split_debug_info && !force_direct)
3905 attr.dw_attr_val.val_entry
3906 = add_addr_table_entry (lbl_id, ate_kind_label);
3907 else
3908 attr.dw_attr_val.val_entry = NULL;
3909 add_dwarf_attr (die, &attr);
3910 }
3911
3912 /* Hash and equality functions for debug_str_hash. */
3913
3914 static hashval_t
3915 debug_str_do_hash (const void *x)
3916 {
3917 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3918 }
3919
3920 static int
3921 debug_str_eq (const void *x1, const void *x2)
3922 {
3923 return strcmp ((((const struct indirect_string_node *)x1)->str),
3924 (const char *)x2) == 0;
3925 }
3926
3927 /* Add STR to the given string hash table. */
3928
3929 static struct indirect_string_node *
3930 find_AT_string_in_table (const char *str, htab_t table)
3931 {
3932 struct indirect_string_node *node;
3933 void **slot;
3934
3935 slot = htab_find_slot_with_hash (table, str,
3936 htab_hash_string (str), INSERT);
3937 if (*slot == NULL)
3938 {
3939 node = ggc_cleared_alloc<indirect_string_node> ();
3940 node->str = ggc_strdup (str);
3941 *slot = node;
3942 }
3943 else
3944 node = (struct indirect_string_node *) *slot;
3945
3946 node->refcount++;
3947 return node;
3948 }
3949
3950 /* Add STR to the indirect string hash table. */
3951
3952 static struct indirect_string_node *
3953 find_AT_string (const char *str)
3954 {
3955 if (! debug_str_hash)
3956 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3957 debug_str_eq, NULL);
3958
3959 return find_AT_string_in_table (str, debug_str_hash);
3960 }
3961
3962 /* Add a string attribute value to a DIE. */
3963
3964 static inline void
3965 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3966 {
3967 dw_attr_node attr;
3968 struct indirect_string_node *node;
3969
3970 node = find_AT_string (str);
3971
3972 attr.dw_attr = attr_kind;
3973 attr.dw_attr_val.val_class = dw_val_class_str;
3974 attr.dw_attr_val.val_entry = NULL;
3975 attr.dw_attr_val.v.val_str = node;
3976 add_dwarf_attr (die, &attr);
3977 }
3978
3979 static inline const char *
3980 AT_string (dw_attr_ref a)
3981 {
3982 gcc_assert (a && AT_class (a) == dw_val_class_str);
3983 return a->dw_attr_val.v.val_str->str;
3984 }
3985
3986 /* Call this function directly to bypass AT_string_form's logic to put
3987 the string inline in the die. */
3988
3989 static void
3990 set_indirect_string (struct indirect_string_node *node)
3991 {
3992 char label[32];
3993 /* Already indirect is a no op. */
3994 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3995 {
3996 gcc_assert (node->label);
3997 return;
3998 }
3999 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4000 ++dw2_string_counter;
4001 node->label = xstrdup (label);
4002
4003 if (!dwarf_split_debug_info)
4004 {
4005 node->form = DW_FORM_strp;
4006 node->index = NOT_INDEXED;
4007 }
4008 else
4009 {
4010 node->form = DW_FORM_GNU_str_index;
4011 node->index = NO_INDEX_ASSIGNED;
4012 }
4013 }
4014
4015 /* Find out whether a string should be output inline in DIE
4016 or out-of-line in .debug_str section. */
4017
4018 static enum dwarf_form
4019 find_string_form (struct indirect_string_node *node)
4020 {
4021 unsigned int len;
4022
4023 if (node->form)
4024 return node->form;
4025
4026 len = strlen (node->str) + 1;
4027
4028 /* If the string is shorter or equal to the size of the reference, it is
4029 always better to put it inline. */
4030 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4031 return node->form = DW_FORM_string;
4032
4033 /* If we cannot expect the linker to merge strings in .debug_str
4034 section, only put it into .debug_str if it is worth even in this
4035 single module. */
4036 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4037 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4038 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4039 return node->form = DW_FORM_string;
4040
4041 set_indirect_string (node);
4042
4043 return node->form;
4044 }
4045
4046 /* Find out whether the string referenced from the attribute should be
4047 output inline in DIE or out-of-line in .debug_str section. */
4048
4049 static enum dwarf_form
4050 AT_string_form (dw_attr_ref a)
4051 {
4052 gcc_assert (a && AT_class (a) == dw_val_class_str);
4053 return find_string_form (a->dw_attr_val.v.val_str);
4054 }
4055
4056 /* Add a DIE reference attribute value to a DIE. */
4057
4058 static inline void
4059 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4060 {
4061 dw_attr_node attr;
4062
4063 #ifdef ENABLE_CHECKING
4064 gcc_assert (targ_die != NULL);
4065 #else
4066 /* With LTO we can end up trying to reference something we didn't create
4067 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4068 if (targ_die == NULL)
4069 return;
4070 #endif
4071
4072 attr.dw_attr = attr_kind;
4073 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4074 attr.dw_attr_val.val_entry = NULL;
4075 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4076 attr.dw_attr_val.v.val_die_ref.external = 0;
4077 add_dwarf_attr (die, &attr);
4078 }
4079
4080 /* Change DIE reference REF to point to NEW_DIE instead. */
4081
4082 static inline void
4083 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4084 {
4085 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4086 ref->dw_attr_val.v.val_die_ref.die = new_die;
4087 ref->dw_attr_val.v.val_die_ref.external = 0;
4088 }
4089
4090 /* Add an AT_specification attribute to a DIE, and also make the back
4091 pointer from the specification to the definition. */
4092
4093 static inline void
4094 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4095 {
4096 add_AT_die_ref (die, DW_AT_specification, targ_die);
4097 gcc_assert (!targ_die->die_definition);
4098 targ_die->die_definition = die;
4099 }
4100
4101 static inline dw_die_ref
4102 AT_ref (dw_attr_ref a)
4103 {
4104 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4105 return a->dw_attr_val.v.val_die_ref.die;
4106 }
4107
4108 static inline int
4109 AT_ref_external (dw_attr_ref a)
4110 {
4111 if (a && AT_class (a) == dw_val_class_die_ref)
4112 return a->dw_attr_val.v.val_die_ref.external;
4113
4114 return 0;
4115 }
4116
4117 static inline void
4118 set_AT_ref_external (dw_attr_ref a, int i)
4119 {
4120 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4121 a->dw_attr_val.v.val_die_ref.external = i;
4122 }
4123
4124 /* Add an FDE reference attribute value to a DIE. */
4125
4126 static inline void
4127 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4128 {
4129 dw_attr_node attr;
4130
4131 attr.dw_attr = attr_kind;
4132 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4133 attr.dw_attr_val.val_entry = NULL;
4134 attr.dw_attr_val.v.val_fde_index = targ_fde;
4135 add_dwarf_attr (die, &attr);
4136 }
4137
4138 /* Add a location description attribute value to a DIE. */
4139
4140 static inline void
4141 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4142 {
4143 dw_attr_node attr;
4144
4145 attr.dw_attr = attr_kind;
4146 attr.dw_attr_val.val_class = dw_val_class_loc;
4147 attr.dw_attr_val.val_entry = NULL;
4148 attr.dw_attr_val.v.val_loc = loc;
4149 add_dwarf_attr (die, &attr);
4150 }
4151
4152 static inline dw_loc_descr_ref
4153 AT_loc (dw_attr_ref a)
4154 {
4155 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4156 return a->dw_attr_val.v.val_loc;
4157 }
4158
4159 static inline void
4160 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4161 {
4162 dw_attr_node attr;
4163
4164 attr.dw_attr = attr_kind;
4165 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4166 attr.dw_attr_val.val_entry = NULL;
4167 attr.dw_attr_val.v.val_loc_list = loc_list;
4168 add_dwarf_attr (die, &attr);
4169 have_location_lists = true;
4170 }
4171
4172 static inline dw_loc_list_ref
4173 AT_loc_list (dw_attr_ref a)
4174 {
4175 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4176 return a->dw_attr_val.v.val_loc_list;
4177 }
4178
4179 static inline dw_loc_list_ref *
4180 AT_loc_list_ptr (dw_attr_ref a)
4181 {
4182 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4183 return &a->dw_attr_val.v.val_loc_list;
4184 }
4185
4186 /* Table of entries into the .debug_addr section. */
4187
4188 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4189
4190 /* Hash an address_table_entry. */
4191
4192 static hashval_t
4193 addr_table_entry_do_hash (const void *x)
4194 {
4195 const addr_table_entry *a = (const addr_table_entry *) x;
4196 inchash::hash hstate;
4197 switch (a->kind)
4198 {
4199 case ate_kind_rtx:
4200 hstate.add_int (0);
4201 break;
4202 case ate_kind_rtx_dtprel:
4203 hstate.add_int (1);
4204 break;
4205 case ate_kind_label:
4206 return htab_hash_string (a->addr.label);
4207 default:
4208 gcc_unreachable ();
4209 }
4210 inchash::add_rtx (a->addr.rtl, hstate);
4211 return hstate.end ();
4212 }
4213
4214 /* Determine equality for two address_table_entries. */
4215
4216 static int
4217 addr_table_entry_eq (const void *x1, const void *x2)
4218 {
4219 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4220 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4221
4222 if (a1->kind != a2->kind)
4223 return 0;
4224 switch (a1->kind)
4225 {
4226 case ate_kind_rtx:
4227 case ate_kind_rtx_dtprel:
4228 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4229 case ate_kind_label:
4230 return strcmp (a1->addr.label, a2->addr.label) == 0;
4231 default:
4232 gcc_unreachable ();
4233 }
4234 }
4235
4236 /* Initialize an addr_table_entry. */
4237
4238 void
4239 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4240 {
4241 e->kind = kind;
4242 switch (kind)
4243 {
4244 case ate_kind_rtx:
4245 case ate_kind_rtx_dtprel:
4246 e->addr.rtl = (rtx) addr;
4247 break;
4248 case ate_kind_label:
4249 e->addr.label = (char *) addr;
4250 break;
4251 }
4252 e->refcount = 0;
4253 e->index = NO_INDEX_ASSIGNED;
4254 }
4255
4256 /* Add attr to the address table entry to the table. Defer setting an
4257 index until output time. */
4258
4259 static addr_table_entry *
4260 add_addr_table_entry (void *addr, enum ate_kind kind)
4261 {
4262 addr_table_entry *node;
4263 addr_table_entry finder;
4264 void **slot;
4265
4266 gcc_assert (dwarf_split_debug_info);
4267 if (! addr_index_table)
4268 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4269 addr_table_entry_eq, NULL);
4270 init_addr_table_entry (&finder, kind, addr);
4271 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4272
4273 if (*slot == HTAB_EMPTY_ENTRY)
4274 {
4275 node = ggc_cleared_alloc<addr_table_entry> ();
4276 init_addr_table_entry (node, kind, addr);
4277 *slot = node;
4278 }
4279 else
4280 node = (addr_table_entry *) *slot;
4281
4282 node->refcount++;
4283 return node;
4284 }
4285
4286 /* Remove an entry from the addr table by decrementing its refcount.
4287 Strictly, decrementing the refcount would be enough, but the
4288 assertion that the entry is actually in the table has found
4289 bugs. */
4290
4291 static void
4292 remove_addr_table_entry (addr_table_entry *entry)
4293 {
4294 gcc_assert (dwarf_split_debug_info && addr_index_table);
4295 /* After an index is assigned, the table is frozen. */
4296 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4297 entry->refcount--;
4298 }
4299
4300 /* Given a location list, remove all addresses it refers to from the
4301 address_table. */
4302
4303 static void
4304 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4305 {
4306 for (; descr; descr = descr->dw_loc_next)
4307 if (descr->dw_loc_oprnd1.val_entry != NULL)
4308 {
4309 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4310 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4311 }
4312 }
4313
4314 /* A helper function for dwarf2out_finish called through
4315 htab_traverse. Assign an addr_table_entry its index. All entries
4316 must be collected into the table when this function is called,
4317 because the indexing code relies on htab_traverse to traverse nodes
4318 in the same order for each run. */
4319
4320 static int
4321 index_addr_table_entry (void **h, void *v)
4322 {
4323 addr_table_entry *node = (addr_table_entry *) *h;
4324 unsigned int *index = (unsigned int *) v;
4325
4326 /* Don't index unreferenced nodes. */
4327 if (node->refcount == 0)
4328 return 1;
4329
4330 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4331 node->index = *index;
4332 *index += 1;
4333
4334 return 1;
4335 }
4336
4337 /* Add an address constant attribute value to a DIE. When using
4338 dwarf_split_debug_info, address attributes in dies destined for the
4339 final executable should be direct references--setting the parameter
4340 force_direct ensures this behavior. */
4341
4342 static inline void
4343 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4344 bool force_direct)
4345 {
4346 dw_attr_node attr;
4347
4348 attr.dw_attr = attr_kind;
4349 attr.dw_attr_val.val_class = dw_val_class_addr;
4350 attr.dw_attr_val.v.val_addr = addr;
4351 if (dwarf_split_debug_info && !force_direct)
4352 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4353 else
4354 attr.dw_attr_val.val_entry = NULL;
4355 add_dwarf_attr (die, &attr);
4356 }
4357
4358 /* Get the RTX from to an address DIE attribute. */
4359
4360 static inline rtx
4361 AT_addr (dw_attr_ref a)
4362 {
4363 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4364 return a->dw_attr_val.v.val_addr;
4365 }
4366
4367 /* Add a file attribute value to a DIE. */
4368
4369 static inline void
4370 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4371 struct dwarf_file_data *fd)
4372 {
4373 dw_attr_node attr;
4374
4375 attr.dw_attr = attr_kind;
4376 attr.dw_attr_val.val_class = dw_val_class_file;
4377 attr.dw_attr_val.val_entry = NULL;
4378 attr.dw_attr_val.v.val_file = fd;
4379 add_dwarf_attr (die, &attr);
4380 }
4381
4382 /* Get the dwarf_file_data from a file DIE attribute. */
4383
4384 static inline struct dwarf_file_data *
4385 AT_file (dw_attr_ref a)
4386 {
4387 gcc_assert (a && AT_class (a) == dw_val_class_file);
4388 return a->dw_attr_val.v.val_file;
4389 }
4390
4391 /* Add a vms delta attribute value to a DIE. */
4392
4393 static inline void
4394 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4395 const char *lbl1, const char *lbl2)
4396 {
4397 dw_attr_node attr;
4398
4399 attr.dw_attr = attr_kind;
4400 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4401 attr.dw_attr_val.val_entry = NULL;
4402 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4403 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4404 add_dwarf_attr (die, &attr);
4405 }
4406
4407 /* Add a label identifier attribute value to a DIE. */
4408
4409 static inline void
4410 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4411 const char *lbl_id)
4412 {
4413 dw_attr_node attr;
4414
4415 attr.dw_attr = attr_kind;
4416 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4417 attr.dw_attr_val.val_entry = NULL;
4418 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4419 if (dwarf_split_debug_info)
4420 attr.dw_attr_val.val_entry
4421 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4422 ate_kind_label);
4423 add_dwarf_attr (die, &attr);
4424 }
4425
4426 /* Add a section offset attribute value to a DIE, an offset into the
4427 debug_line section. */
4428
4429 static inline void
4430 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4431 const char *label)
4432 {
4433 dw_attr_node attr;
4434
4435 attr.dw_attr = attr_kind;
4436 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4437 attr.dw_attr_val.val_entry = NULL;
4438 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4439 add_dwarf_attr (die, &attr);
4440 }
4441
4442 /* Add a section offset attribute value to a DIE, an offset into the
4443 debug_macinfo section. */
4444
4445 static inline void
4446 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4447 const char *label)
4448 {
4449 dw_attr_node attr;
4450
4451 attr.dw_attr = attr_kind;
4452 attr.dw_attr_val.val_class = dw_val_class_macptr;
4453 attr.dw_attr_val.val_entry = NULL;
4454 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4455 add_dwarf_attr (die, &attr);
4456 }
4457
4458 /* Add an offset attribute value to a DIE. */
4459
4460 static inline void
4461 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4462 unsigned HOST_WIDE_INT offset)
4463 {
4464 dw_attr_node attr;
4465
4466 attr.dw_attr = attr_kind;
4467 attr.dw_attr_val.val_class = dw_val_class_offset;
4468 attr.dw_attr_val.val_entry = NULL;
4469 attr.dw_attr_val.v.val_offset = offset;
4470 add_dwarf_attr (die, &attr);
4471 }
4472
4473 /* Add a range_list attribute value to a DIE. When using
4474 dwarf_split_debug_info, address attributes in dies destined for the
4475 final executable should be direct references--setting the parameter
4476 force_direct ensures this behavior. */
4477
4478 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4479 #define RELOCATED_OFFSET (NULL)
4480
4481 static void
4482 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4483 long unsigned int offset, bool force_direct)
4484 {
4485 dw_attr_node attr;
4486
4487 attr.dw_attr = attr_kind;
4488 attr.dw_attr_val.val_class = dw_val_class_range_list;
4489 /* For the range_list attribute, use val_entry to store whether the
4490 offset should follow split-debug-info or normal semantics. This
4491 value is read in output_range_list_offset. */
4492 if (dwarf_split_debug_info && !force_direct)
4493 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4494 else
4495 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4496 attr.dw_attr_val.v.val_offset = offset;
4497 add_dwarf_attr (die, &attr);
4498 }
4499
4500 /* Return the start label of a delta attribute. */
4501
4502 static inline const char *
4503 AT_vms_delta1 (dw_attr_ref a)
4504 {
4505 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4506 return a->dw_attr_val.v.val_vms_delta.lbl1;
4507 }
4508
4509 /* Return the end label of a delta attribute. */
4510
4511 static inline const char *
4512 AT_vms_delta2 (dw_attr_ref a)
4513 {
4514 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4515 return a->dw_attr_val.v.val_vms_delta.lbl2;
4516 }
4517
4518 static inline const char *
4519 AT_lbl (dw_attr_ref a)
4520 {
4521 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4522 || AT_class (a) == dw_val_class_lineptr
4523 || AT_class (a) == dw_val_class_macptr
4524 || AT_class (a) == dw_val_class_high_pc));
4525 return a->dw_attr_val.v.val_lbl_id;
4526 }
4527
4528 /* Get the attribute of type attr_kind. */
4529
4530 static dw_attr_ref
4531 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4532 {
4533 dw_attr_ref a;
4534 unsigned ix;
4535 dw_die_ref spec = NULL;
4536
4537 if (! die)
4538 return NULL;
4539
4540 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4541 if (a->dw_attr == attr_kind)
4542 return a;
4543 else if (a->dw_attr == DW_AT_specification
4544 || a->dw_attr == DW_AT_abstract_origin)
4545 spec = AT_ref (a);
4546
4547 if (spec)
4548 return get_AT (spec, attr_kind);
4549
4550 return NULL;
4551 }
4552
4553 /* Returns the parent of the declaration of DIE. */
4554
4555 static dw_die_ref
4556 get_die_parent (dw_die_ref die)
4557 {
4558 dw_die_ref t;
4559
4560 if (!die)
4561 return NULL;
4562
4563 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4564 || (t = get_AT_ref (die, DW_AT_specification)))
4565 die = t;
4566
4567 return die->die_parent;
4568 }
4569
4570 /* Return the "low pc" attribute value, typically associated with a subprogram
4571 DIE. Return null if the "low pc" attribute is either not present, or if it
4572 cannot be represented as an assembler label identifier. */
4573
4574 static inline const char *
4575 get_AT_low_pc (dw_die_ref die)
4576 {
4577 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4578
4579 return a ? AT_lbl (a) : NULL;
4580 }
4581
4582 /* Return the "high pc" attribute value, typically associated with a subprogram
4583 DIE. Return null if the "high pc" attribute is either not present, or if it
4584 cannot be represented as an assembler label identifier. */
4585
4586 static inline const char *
4587 get_AT_hi_pc (dw_die_ref die)
4588 {
4589 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4590
4591 return a ? AT_lbl (a) : NULL;
4592 }
4593
4594 /* Return the value of the string attribute designated by ATTR_KIND, or
4595 NULL if it is not present. */
4596
4597 static inline const char *
4598 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4599 {
4600 dw_attr_ref a = get_AT (die, attr_kind);
4601
4602 return a ? AT_string (a) : NULL;
4603 }
4604
4605 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4606 if it is not present. */
4607
4608 static inline int
4609 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4610 {
4611 dw_attr_ref a = get_AT (die, attr_kind);
4612
4613 return a ? AT_flag (a) : 0;
4614 }
4615
4616 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4617 if it is not present. */
4618
4619 static inline unsigned
4620 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4621 {
4622 dw_attr_ref a = get_AT (die, attr_kind);
4623
4624 return a ? AT_unsigned (a) : 0;
4625 }
4626
4627 static inline dw_die_ref
4628 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4629 {
4630 dw_attr_ref a = get_AT (die, attr_kind);
4631
4632 return a ? AT_ref (a) : NULL;
4633 }
4634
4635 static inline struct dwarf_file_data *
4636 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4637 {
4638 dw_attr_ref a = get_AT (die, attr_kind);
4639
4640 return a ? AT_file (a) : NULL;
4641 }
4642
4643 /* Return TRUE if the language is C++. */
4644
4645 static inline bool
4646 is_cxx (void)
4647 {
4648 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4649
4650 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4651 }
4652
4653 /* Return TRUE if the language is Java. */
4654
4655 static inline bool
4656 is_java (void)
4657 {
4658 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4659
4660 return lang == DW_LANG_Java;
4661 }
4662
4663 /* Return TRUE if the language is Fortran. */
4664
4665 static inline bool
4666 is_fortran (void)
4667 {
4668 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4669
4670 return (lang == DW_LANG_Fortran77
4671 || lang == DW_LANG_Fortran90
4672 || lang == DW_LANG_Fortran95);
4673 }
4674
4675 /* Return TRUE if the language is Ada. */
4676
4677 static inline bool
4678 is_ada (void)
4679 {
4680 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4681
4682 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4683 }
4684
4685 /* Remove the specified attribute if present. */
4686
4687 static void
4688 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4689 {
4690 dw_attr_ref a;
4691 unsigned ix;
4692
4693 if (! die)
4694 return;
4695
4696 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4697 if (a->dw_attr == attr_kind)
4698 {
4699 if (AT_class (a) == dw_val_class_str)
4700 if (a->dw_attr_val.v.val_str->refcount)
4701 a->dw_attr_val.v.val_str->refcount--;
4702
4703 /* vec::ordered_remove should help reduce the number of abbrevs
4704 that are needed. */
4705 die->die_attr->ordered_remove (ix);
4706 return;
4707 }
4708 }
4709
4710 /* Remove CHILD from its parent. PREV must have the property that
4711 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4712
4713 static void
4714 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4715 {
4716 gcc_assert (child->die_parent == prev->die_parent);
4717 gcc_assert (prev->die_sib == child);
4718 if (prev == child)
4719 {
4720 gcc_assert (child->die_parent->die_child == child);
4721 prev = NULL;
4722 }
4723 else
4724 prev->die_sib = child->die_sib;
4725 if (child->die_parent->die_child == child)
4726 child->die_parent->die_child = prev;
4727 }
4728
4729 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4730 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4731
4732 static void
4733 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4734 {
4735 dw_die_ref parent = old_child->die_parent;
4736
4737 gcc_assert (parent == prev->die_parent);
4738 gcc_assert (prev->die_sib == old_child);
4739
4740 new_child->die_parent = parent;
4741 if (prev == old_child)
4742 {
4743 gcc_assert (parent->die_child == old_child);
4744 new_child->die_sib = new_child;
4745 }
4746 else
4747 {
4748 prev->die_sib = new_child;
4749 new_child->die_sib = old_child->die_sib;
4750 }
4751 if (old_child->die_parent->die_child == old_child)
4752 old_child->die_parent->die_child = new_child;
4753 }
4754
4755 /* Move all children from OLD_PARENT to NEW_PARENT. */
4756
4757 static void
4758 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4759 {
4760 dw_die_ref c;
4761 new_parent->die_child = old_parent->die_child;
4762 old_parent->die_child = NULL;
4763 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4764 }
4765
4766 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4767 matches TAG. */
4768
4769 static void
4770 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4771 {
4772 dw_die_ref c;
4773
4774 c = die->die_child;
4775 if (c) do {
4776 dw_die_ref prev = c;
4777 c = c->die_sib;
4778 while (c->die_tag == tag)
4779 {
4780 remove_child_with_prev (c, prev);
4781 /* Might have removed every child. */
4782 if (c == c->die_sib)
4783 return;
4784 c = c->die_sib;
4785 }
4786 } while (c != die->die_child);
4787 }
4788
4789 /* Add a CHILD_DIE as the last child of DIE. */
4790
4791 static void
4792 add_child_die (dw_die_ref die, dw_die_ref child_die)
4793 {
4794 /* FIXME this should probably be an assert. */
4795 if (! die || ! child_die)
4796 return;
4797 gcc_assert (die != child_die);
4798
4799 child_die->die_parent = die;
4800 if (die->die_child)
4801 {
4802 child_die->die_sib = die->die_child->die_sib;
4803 die->die_child->die_sib = child_die;
4804 }
4805 else
4806 child_die->die_sib = child_die;
4807 die->die_child = child_die;
4808 }
4809
4810 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4811 is the specification, to the end of PARENT's list of children.
4812 This is done by removing and re-adding it. */
4813
4814 static void
4815 splice_child_die (dw_die_ref parent, dw_die_ref child)
4816 {
4817 dw_die_ref p;
4818
4819 /* We want the declaration DIE from inside the class, not the
4820 specification DIE at toplevel. */
4821 if (child->die_parent != parent)
4822 {
4823 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4824
4825 if (tmp)
4826 child = tmp;
4827 }
4828
4829 gcc_assert (child->die_parent == parent
4830 || (child->die_parent
4831 == get_AT_ref (parent, DW_AT_specification)));
4832
4833 for (p = child->die_parent->die_child; ; p = p->die_sib)
4834 if (p->die_sib == child)
4835 {
4836 remove_child_with_prev (child, p);
4837 break;
4838 }
4839
4840 add_child_die (parent, child);
4841 }
4842
4843 /* Return a pointer to a newly created DIE node. */
4844
4845 static inline dw_die_ref
4846 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4847 {
4848 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4849
4850 die->die_tag = tag_value;
4851
4852 if (parent_die != NULL)
4853 add_child_die (parent_die, die);
4854 else
4855 {
4856 limbo_die_node *limbo_node;
4857
4858 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4859 limbo_node->die = die;
4860 limbo_node->created_for = t;
4861 limbo_node->next = limbo_die_list;
4862 limbo_die_list = limbo_node;
4863 }
4864
4865 return die;
4866 }
4867
4868 /* Return the DIE associated with the given type specifier. */
4869
4870 static inline dw_die_ref
4871 lookup_type_die (tree type)
4872 {
4873 return TYPE_SYMTAB_DIE (type);
4874 }
4875
4876 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4877 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4878 anonymous type instead the one of the naming typedef. */
4879
4880 static inline dw_die_ref
4881 strip_naming_typedef (tree type, dw_die_ref type_die)
4882 {
4883 if (type
4884 && TREE_CODE (type) == RECORD_TYPE
4885 && type_die
4886 && type_die->die_tag == DW_TAG_typedef
4887 && is_naming_typedef_decl (TYPE_NAME (type)))
4888 type_die = get_AT_ref (type_die, DW_AT_type);
4889 return type_die;
4890 }
4891
4892 /* Like lookup_type_die, but if type is an anonymous type named by a
4893 typedef[1], return the DIE of the anonymous type instead the one of
4894 the naming typedef. This is because in gen_typedef_die, we did
4895 equate the anonymous struct named by the typedef with the DIE of
4896 the naming typedef. So by default, lookup_type_die on an anonymous
4897 struct yields the DIE of the naming typedef.
4898
4899 [1]: Read the comment of is_naming_typedef_decl to learn about what
4900 a naming typedef is. */
4901
4902 static inline dw_die_ref
4903 lookup_type_die_strip_naming_typedef (tree type)
4904 {
4905 dw_die_ref die = lookup_type_die (type);
4906 return strip_naming_typedef (type, die);
4907 }
4908
4909 /* Equate a DIE to a given type specifier. */
4910
4911 static inline void
4912 equate_type_number_to_die (tree type, dw_die_ref type_die)
4913 {
4914 TYPE_SYMTAB_DIE (type) = type_die;
4915 }
4916
4917 /* Returns a hash value for X (which really is a die_struct). */
4918
4919 static hashval_t
4920 decl_die_table_hash (const void *x)
4921 {
4922 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4923 }
4924
4925 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4926
4927 static int
4928 decl_die_table_eq (const void *x, const void *y)
4929 {
4930 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4931 }
4932
4933 /* Return the DIE associated with a given declaration. */
4934
4935 static inline dw_die_ref
4936 lookup_decl_die (tree decl)
4937 {
4938 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4939 }
4940
4941 /* Returns a hash value for X (which really is a var_loc_list). */
4942
4943 static hashval_t
4944 decl_loc_table_hash (const void *x)
4945 {
4946 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4947 }
4948
4949 /* Return nonzero if decl_id of var_loc_list X is the same as
4950 UID of decl *Y. */
4951
4952 static int
4953 decl_loc_table_eq (const void *x, const void *y)
4954 {
4955 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4956 }
4957
4958 /* Return the var_loc list associated with a given declaration. */
4959
4960 static inline var_loc_list *
4961 lookup_decl_loc (const_tree decl)
4962 {
4963 if (!decl_loc_table)
4964 return NULL;
4965 return (var_loc_list *)
4966 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4967 }
4968
4969 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4970
4971 static hashval_t
4972 cached_dw_loc_list_table_hash (const void *x)
4973 {
4974 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4975 }
4976
4977 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4978 UID of decl *Y. */
4979
4980 static int
4981 cached_dw_loc_list_table_eq (const void *x, const void *y)
4982 {
4983 return (((const cached_dw_loc_list *) x)->decl_id
4984 == DECL_UID ((const_tree) y));
4985 }
4986
4987 /* Equate a DIE to a particular declaration. */
4988
4989 static void
4990 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4991 {
4992 unsigned int decl_id = DECL_UID (decl);
4993 void **slot;
4994
4995 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4996 *slot = decl_die;
4997 decl_die->decl_id = decl_id;
4998 }
4999
5000 /* Return how many bits covers PIECE EXPR_LIST. */
5001
5002 static int
5003 decl_piece_bitsize (rtx piece)
5004 {
5005 int ret = (int) GET_MODE (piece);
5006 if (ret)
5007 return ret;
5008 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5009 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5010 return INTVAL (XEXP (XEXP (piece, 0), 0));
5011 }
5012
5013 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5014
5015 static rtx *
5016 decl_piece_varloc_ptr (rtx piece)
5017 {
5018 if ((int) GET_MODE (piece))
5019 return &XEXP (piece, 0);
5020 else
5021 return &XEXP (XEXP (piece, 0), 1);
5022 }
5023
5024 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5025 Next is the chain of following piece nodes. */
5026
5027 static rtx_expr_list *
5028 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5029 {
5030 if (bitsize <= (int) MAX_MACHINE_MODE)
5031 return alloc_EXPR_LIST (bitsize, loc_note, next);
5032 else
5033 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5034 GEN_INT (bitsize),
5035 loc_note), next);
5036 }
5037
5038 /* Return rtx that should be stored into loc field for
5039 LOC_NOTE and BITPOS/BITSIZE. */
5040
5041 static rtx
5042 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5043 HOST_WIDE_INT bitsize)
5044 {
5045 if (bitsize != -1)
5046 {
5047 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5048 if (bitpos != 0)
5049 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5050 }
5051 return loc_note;
5052 }
5053
5054 /* This function either modifies location piece list *DEST in
5055 place (if SRC and INNER is NULL), or copies location piece list
5056 *SRC to *DEST while modifying it. Location BITPOS is modified
5057 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5058 not copied and if needed some padding around it is added.
5059 When modifying in place, DEST should point to EXPR_LIST where
5060 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5061 to the start of the whole list and INNER points to the EXPR_LIST
5062 where earlier pieces cover PIECE_BITPOS bits. */
5063
5064 static void
5065 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5066 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5067 HOST_WIDE_INT bitsize, rtx loc_note)
5068 {
5069 int diff;
5070 bool copy = inner != NULL;
5071
5072 if (copy)
5073 {
5074 /* First copy all nodes preceding the current bitpos. */
5075 while (src != inner)
5076 {
5077 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5078 decl_piece_bitsize (*src), NULL_RTX);
5079 dest = &XEXP (*dest, 1);
5080 src = &XEXP (*src, 1);
5081 }
5082 }
5083 /* Add padding if needed. */
5084 if (bitpos != piece_bitpos)
5085 {
5086 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5087 copy ? NULL_RTX : *dest);
5088 dest = &XEXP (*dest, 1);
5089 }
5090 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5091 {
5092 gcc_assert (!copy);
5093 /* A piece with correct bitpos and bitsize already exist,
5094 just update the location for it and return. */
5095 *decl_piece_varloc_ptr (*dest) = loc_note;
5096 return;
5097 }
5098 /* Add the piece that changed. */
5099 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5100 dest = &XEXP (*dest, 1);
5101 /* Skip over pieces that overlap it. */
5102 diff = bitpos - piece_bitpos + bitsize;
5103 if (!copy)
5104 src = dest;
5105 while (diff > 0 && *src)
5106 {
5107 rtx piece = *src;
5108 diff -= decl_piece_bitsize (piece);
5109 if (copy)
5110 src = &XEXP (piece, 1);
5111 else
5112 {
5113 *src = XEXP (piece, 1);
5114 free_EXPR_LIST_node (piece);
5115 }
5116 }
5117 /* Add padding if needed. */
5118 if (diff < 0 && *src)
5119 {
5120 if (!copy)
5121 dest = src;
5122 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5123 dest = &XEXP (*dest, 1);
5124 }
5125 if (!copy)
5126 return;
5127 /* Finally copy all nodes following it. */
5128 while (*src)
5129 {
5130 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5131 decl_piece_bitsize (*src), NULL_RTX);
5132 dest = &XEXP (*dest, 1);
5133 src = &XEXP (*src, 1);
5134 }
5135 }
5136
5137 /* Add a variable location node to the linked list for DECL. */
5138
5139 static struct var_loc_node *
5140 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5141 {
5142 unsigned int decl_id;
5143 var_loc_list *temp;
5144 void **slot;
5145 struct var_loc_node *loc = NULL;
5146 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5147
5148 if (TREE_CODE (decl) == VAR_DECL
5149 && DECL_HAS_DEBUG_EXPR_P (decl))
5150 {
5151 tree realdecl = DECL_DEBUG_EXPR (decl);
5152 if (handled_component_p (realdecl)
5153 || (TREE_CODE (realdecl) == MEM_REF
5154 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5155 {
5156 HOST_WIDE_INT maxsize;
5157 tree innerdecl;
5158 innerdecl
5159 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5160 if (!DECL_P (innerdecl)
5161 || DECL_IGNORED_P (innerdecl)
5162 || TREE_STATIC (innerdecl)
5163 || bitsize <= 0
5164 || bitpos + bitsize > 256
5165 || bitsize != maxsize)
5166 return NULL;
5167 decl = innerdecl;
5168 }
5169 }
5170
5171 decl_id = DECL_UID (decl);
5172 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5173 if (*slot == NULL)
5174 {
5175 temp = ggc_cleared_alloc<var_loc_list> ();
5176 temp->decl_id = decl_id;
5177 *slot = temp;
5178 }
5179 else
5180 temp = (var_loc_list *) *slot;
5181
5182 /* For PARM_DECLs try to keep around the original incoming value,
5183 even if that means we'll emit a zero-range .debug_loc entry. */
5184 if (temp->last
5185 && temp->first == temp->last
5186 && TREE_CODE (decl) == PARM_DECL
5187 && NOTE_P (temp->first->loc)
5188 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5189 && DECL_INCOMING_RTL (decl)
5190 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5191 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5192 == GET_CODE (DECL_INCOMING_RTL (decl))
5193 && prev_real_insn (temp->first->loc) == NULL_RTX
5194 && (bitsize != -1
5195 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5196 NOTE_VAR_LOCATION_LOC (loc_note))
5197 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5198 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5199 {
5200 loc = ggc_cleared_alloc<var_loc_node> ();
5201 temp->first->next = loc;
5202 temp->last = loc;
5203 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5204 }
5205 else if (temp->last)
5206 {
5207 struct var_loc_node *last = temp->last, *unused = NULL;
5208 rtx *piece_loc = NULL, last_loc_note;
5209 int piece_bitpos = 0;
5210 if (last->next)
5211 {
5212 last = last->next;
5213 gcc_assert (last->next == NULL);
5214 }
5215 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5216 {
5217 piece_loc = &last->loc;
5218 do
5219 {
5220 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5221 if (piece_bitpos + cur_bitsize > bitpos)
5222 break;
5223 piece_bitpos += cur_bitsize;
5224 piece_loc = &XEXP (*piece_loc, 1);
5225 }
5226 while (*piece_loc);
5227 }
5228 /* TEMP->LAST here is either pointer to the last but one or
5229 last element in the chained list, LAST is pointer to the
5230 last element. */
5231 if (label && strcmp (last->label, label) == 0)
5232 {
5233 /* For SRA optimized variables if there weren't any real
5234 insns since last note, just modify the last node. */
5235 if (piece_loc != NULL)
5236 {
5237 adjust_piece_list (piece_loc, NULL, NULL,
5238 bitpos, piece_bitpos, bitsize, loc_note);
5239 return NULL;
5240 }
5241 /* If the last note doesn't cover any instructions, remove it. */
5242 if (temp->last != last)
5243 {
5244 temp->last->next = NULL;
5245 unused = last;
5246 last = temp->last;
5247 gcc_assert (strcmp (last->label, label) != 0);
5248 }
5249 else
5250 {
5251 gcc_assert (temp->first == temp->last
5252 || (temp->first->next == temp->last
5253 && TREE_CODE (decl) == PARM_DECL));
5254 memset (temp->last, '\0', sizeof (*temp->last));
5255 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5256 return temp->last;
5257 }
5258 }
5259 if (bitsize == -1 && NOTE_P (last->loc))
5260 last_loc_note = last->loc;
5261 else if (piece_loc != NULL
5262 && *piece_loc != NULL_RTX
5263 && piece_bitpos == bitpos
5264 && decl_piece_bitsize (*piece_loc) == bitsize)
5265 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5266 else
5267 last_loc_note = NULL_RTX;
5268 /* If the current location is the same as the end of the list,
5269 and either both or neither of the locations is uninitialized,
5270 we have nothing to do. */
5271 if (last_loc_note == NULL_RTX
5272 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5273 NOTE_VAR_LOCATION_LOC (loc_note)))
5274 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5275 != NOTE_VAR_LOCATION_STATUS (loc_note))
5276 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5277 == VAR_INIT_STATUS_UNINITIALIZED)
5278 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5279 == VAR_INIT_STATUS_UNINITIALIZED))))
5280 {
5281 /* Add LOC to the end of list and update LAST. If the last
5282 element of the list has been removed above, reuse its
5283 memory for the new node, otherwise allocate a new one. */
5284 if (unused)
5285 {
5286 loc = unused;
5287 memset (loc, '\0', sizeof (*loc));
5288 }
5289 else
5290 loc = ggc_cleared_alloc<var_loc_node> ();
5291 if (bitsize == -1 || piece_loc == NULL)
5292 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5293 else
5294 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5295 bitpos, piece_bitpos, bitsize, loc_note);
5296 last->next = loc;
5297 /* Ensure TEMP->LAST will point either to the new last but one
5298 element of the chain, or to the last element in it. */
5299 if (last != temp->last)
5300 temp->last = last;
5301 }
5302 else if (unused)
5303 ggc_free (unused);
5304 }
5305 else
5306 {
5307 loc = ggc_cleared_alloc<var_loc_node> ();
5308 temp->first = loc;
5309 temp->last = loc;
5310 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5311 }
5312 return loc;
5313 }
5314 \f
5315 /* Keep track of the number of spaces used to indent the
5316 output of the debugging routines that print the structure of
5317 the DIE internal representation. */
5318 static int print_indent;
5319
5320 /* Indent the line the number of spaces given by print_indent. */
5321
5322 static inline void
5323 print_spaces (FILE *outfile)
5324 {
5325 fprintf (outfile, "%*s", print_indent, "");
5326 }
5327
5328 /* Print a type signature in hex. */
5329
5330 static inline void
5331 print_signature (FILE *outfile, char *sig)
5332 {
5333 int i;
5334
5335 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5336 fprintf (outfile, "%02x", sig[i] & 0xff);
5337 }
5338
5339 /* Print the information associated with a given DIE, and its children.
5340 This routine is a debugging aid only. */
5341
5342 static void
5343 print_die (dw_die_ref die, FILE *outfile)
5344 {
5345 dw_attr_ref a;
5346 dw_die_ref c;
5347 unsigned ix;
5348
5349 print_spaces (outfile);
5350 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5351 die->die_offset, dwarf_tag_name (die->die_tag),
5352 (void*) die);
5353 print_spaces (outfile);
5354 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5355 fprintf (outfile, " offset: %ld", die->die_offset);
5356 fprintf (outfile, " mark: %d\n", die->die_mark);
5357
5358 if (die->comdat_type_p)
5359 {
5360 print_spaces (outfile);
5361 fprintf (outfile, " signature: ");
5362 print_signature (outfile, die->die_id.die_type_node->signature);
5363 fprintf (outfile, "\n");
5364 }
5365
5366 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5367 {
5368 print_spaces (outfile);
5369 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5370
5371 switch (AT_class (a))
5372 {
5373 case dw_val_class_addr:
5374 fprintf (outfile, "address");
5375 break;
5376 case dw_val_class_offset:
5377 fprintf (outfile, "offset");
5378 break;
5379 case dw_val_class_loc:
5380 fprintf (outfile, "location descriptor");
5381 break;
5382 case dw_val_class_loc_list:
5383 fprintf (outfile, "location list -> label:%s",
5384 AT_loc_list (a)->ll_symbol);
5385 break;
5386 case dw_val_class_range_list:
5387 fprintf (outfile, "range list");
5388 break;
5389 case dw_val_class_const:
5390 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5391 break;
5392 case dw_val_class_unsigned_const:
5393 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5394 break;
5395 case dw_val_class_const_double:
5396 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5397 HOST_WIDE_INT_PRINT_UNSIGNED")",
5398 a->dw_attr_val.v.val_double.high,
5399 a->dw_attr_val.v.val_double.low);
5400 break;
5401 case dw_val_class_wide_int:
5402 {
5403 int i = a->dw_attr_val.v.val_wide->get_len ();
5404 fprintf (outfile, "constant (");
5405 gcc_assert (i > 0);
5406 if (a->dw_attr_val.v.val_wide->elt (i - 1) == 0)
5407 fprintf (outfile, "0x");
5408 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5409 a->dw_attr_val.v.val_wide->elt (--i));
5410 while (--i >= 0)
5411 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5412 a->dw_attr_val.v.val_wide->elt (i));
5413 fprintf (outfile, ")");
5414 break;
5415 }
5416 case dw_val_class_vec:
5417 fprintf (outfile, "floating-point or vector constant");
5418 break;
5419 case dw_val_class_flag:
5420 fprintf (outfile, "%u", AT_flag (a));
5421 break;
5422 case dw_val_class_die_ref:
5423 if (AT_ref (a) != NULL)
5424 {
5425 if (AT_ref (a)->comdat_type_p)
5426 {
5427 fprintf (outfile, "die -> signature: ");
5428 print_signature (outfile,
5429 AT_ref (a)->die_id.die_type_node->signature);
5430 }
5431 else if (AT_ref (a)->die_id.die_symbol)
5432 fprintf (outfile, "die -> label: %s",
5433 AT_ref (a)->die_id.die_symbol);
5434 else
5435 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5436 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5437 }
5438 else
5439 fprintf (outfile, "die -> <null>");
5440 break;
5441 case dw_val_class_vms_delta:
5442 fprintf (outfile, "delta: @slotcount(%s-%s)",
5443 AT_vms_delta2 (a), AT_vms_delta1 (a));
5444 break;
5445 case dw_val_class_lbl_id:
5446 case dw_val_class_lineptr:
5447 case dw_val_class_macptr:
5448 case dw_val_class_high_pc:
5449 fprintf (outfile, "label: %s", AT_lbl (a));
5450 break;
5451 case dw_val_class_str:
5452 if (AT_string (a) != NULL)
5453 fprintf (outfile, "\"%s\"", AT_string (a));
5454 else
5455 fprintf (outfile, "<null>");
5456 break;
5457 case dw_val_class_file:
5458 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5459 AT_file (a)->emitted_number);
5460 break;
5461 case dw_val_class_data8:
5462 {
5463 int i;
5464
5465 for (i = 0; i < 8; i++)
5466 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5467 break;
5468 }
5469 default:
5470 break;
5471 }
5472
5473 fprintf (outfile, "\n");
5474 }
5475
5476 if (die->die_child != NULL)
5477 {
5478 print_indent += 4;
5479 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5480 print_indent -= 4;
5481 }
5482 if (print_indent == 0)
5483 fprintf (outfile, "\n");
5484 }
5485
5486 /* Print the information collected for a given DIE. */
5487
5488 DEBUG_FUNCTION void
5489 debug_dwarf_die (dw_die_ref die)
5490 {
5491 print_die (die, stderr);
5492 }
5493
5494 DEBUG_FUNCTION void
5495 debug (die_struct &ref)
5496 {
5497 print_die (&ref, stderr);
5498 }
5499
5500 DEBUG_FUNCTION void
5501 debug (die_struct *ptr)
5502 {
5503 if (ptr)
5504 debug (*ptr);
5505 else
5506 fprintf (stderr, "<nil>\n");
5507 }
5508
5509
5510 /* Print all DWARF information collected for the compilation unit.
5511 This routine is a debugging aid only. */
5512
5513 DEBUG_FUNCTION void
5514 debug_dwarf (void)
5515 {
5516 print_indent = 0;
5517 print_die (comp_unit_die (), stderr);
5518 }
5519 \f
5520 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5521 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5522 DIE that marks the start of the DIEs for this include file. */
5523
5524 static dw_die_ref
5525 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5526 {
5527 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5528 dw_die_ref new_unit = gen_compile_unit_die (filename);
5529
5530 new_unit->die_sib = old_unit;
5531 return new_unit;
5532 }
5533
5534 /* Close an include-file CU and reopen the enclosing one. */
5535
5536 static dw_die_ref
5537 pop_compile_unit (dw_die_ref old_unit)
5538 {
5539 dw_die_ref new_unit = old_unit->die_sib;
5540
5541 old_unit->die_sib = NULL;
5542 return new_unit;
5543 }
5544
5545 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5546 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5547 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5548
5549 /* Calculate the checksum of a location expression. */
5550
5551 static inline void
5552 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5553 {
5554 int tem;
5555 inchash::hash hstate;
5556 hashval_t hash;
5557
5558 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5559 CHECKSUM (tem);
5560 hash_loc_operands (loc, hstate);
5561 hash = hstate.end();
5562 CHECKSUM (hash);
5563 }
5564
5565 /* Calculate the checksum of an attribute. */
5566
5567 static void
5568 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5569 {
5570 dw_loc_descr_ref loc;
5571 rtx r;
5572
5573 CHECKSUM (at->dw_attr);
5574
5575 /* We don't care that this was compiled with a different compiler
5576 snapshot; if the output is the same, that's what matters. */
5577 if (at->dw_attr == DW_AT_producer)
5578 return;
5579
5580 switch (AT_class (at))
5581 {
5582 case dw_val_class_const:
5583 CHECKSUM (at->dw_attr_val.v.val_int);
5584 break;
5585 case dw_val_class_unsigned_const:
5586 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5587 break;
5588 case dw_val_class_const_double:
5589 CHECKSUM (at->dw_attr_val.v.val_double);
5590 break;
5591 case dw_val_class_wide_int:
5592 CHECKSUM (*at->dw_attr_val.v.val_wide);
5593 break;
5594 case dw_val_class_vec:
5595 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5596 (at->dw_attr_val.v.val_vec.length
5597 * at->dw_attr_val.v.val_vec.elt_size));
5598 break;
5599 case dw_val_class_flag:
5600 CHECKSUM (at->dw_attr_val.v.val_flag);
5601 break;
5602 case dw_val_class_str:
5603 CHECKSUM_STRING (AT_string (at));
5604 break;
5605
5606 case dw_val_class_addr:
5607 r = AT_addr (at);
5608 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5609 CHECKSUM_STRING (XSTR (r, 0));
5610 break;
5611
5612 case dw_val_class_offset:
5613 CHECKSUM (at->dw_attr_val.v.val_offset);
5614 break;
5615
5616 case dw_val_class_loc:
5617 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5618 loc_checksum (loc, ctx);
5619 break;
5620
5621 case dw_val_class_die_ref:
5622 die_checksum (AT_ref (at), ctx, mark);
5623 break;
5624
5625 case dw_val_class_fde_ref:
5626 case dw_val_class_vms_delta:
5627 case dw_val_class_lbl_id:
5628 case dw_val_class_lineptr:
5629 case dw_val_class_macptr:
5630 case dw_val_class_high_pc:
5631 break;
5632
5633 case dw_val_class_file:
5634 CHECKSUM_STRING (AT_file (at)->filename);
5635 break;
5636
5637 case dw_val_class_data8:
5638 CHECKSUM (at->dw_attr_val.v.val_data8);
5639 break;
5640
5641 default:
5642 break;
5643 }
5644 }
5645
5646 /* Calculate the checksum of a DIE. */
5647
5648 static void
5649 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5650 {
5651 dw_die_ref c;
5652 dw_attr_ref a;
5653 unsigned ix;
5654
5655 /* To avoid infinite recursion. */
5656 if (die->die_mark)
5657 {
5658 CHECKSUM (die->die_mark);
5659 return;
5660 }
5661 die->die_mark = ++(*mark);
5662
5663 CHECKSUM (die->die_tag);
5664
5665 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5666 attr_checksum (a, ctx, mark);
5667
5668 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5669 }
5670
5671 #undef CHECKSUM
5672 #undef CHECKSUM_BLOCK
5673 #undef CHECKSUM_STRING
5674
5675 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5676 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5677 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5678 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5679 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5680 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5681 #define CHECKSUM_ATTR(FOO) \
5682 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5683
5684 /* Calculate the checksum of a number in signed LEB128 format. */
5685
5686 static void
5687 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5688 {
5689 unsigned char byte;
5690 bool more;
5691
5692 while (1)
5693 {
5694 byte = (value & 0x7f);
5695 value >>= 7;
5696 more = !((value == 0 && (byte & 0x40) == 0)
5697 || (value == -1 && (byte & 0x40) != 0));
5698 if (more)
5699 byte |= 0x80;
5700 CHECKSUM (byte);
5701 if (!more)
5702 break;
5703 }
5704 }
5705
5706 /* Calculate the checksum of a number in unsigned LEB128 format. */
5707
5708 static void
5709 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5710 {
5711 while (1)
5712 {
5713 unsigned char byte = (value & 0x7f);
5714 value >>= 7;
5715 if (value != 0)
5716 /* More bytes to follow. */
5717 byte |= 0x80;
5718 CHECKSUM (byte);
5719 if (value == 0)
5720 break;
5721 }
5722 }
5723
5724 /* Checksum the context of the DIE. This adds the names of any
5725 surrounding namespaces or structures to the checksum. */
5726
5727 static void
5728 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5729 {
5730 const char *name;
5731 dw_die_ref spec;
5732 int tag = die->die_tag;
5733
5734 if (tag != DW_TAG_namespace
5735 && tag != DW_TAG_structure_type
5736 && tag != DW_TAG_class_type)
5737 return;
5738
5739 name = get_AT_string (die, DW_AT_name);
5740
5741 spec = get_AT_ref (die, DW_AT_specification);
5742 if (spec != NULL)
5743 die = spec;
5744
5745 if (die->die_parent != NULL)
5746 checksum_die_context (die->die_parent, ctx);
5747
5748 CHECKSUM_ULEB128 ('C');
5749 CHECKSUM_ULEB128 (tag);
5750 if (name != NULL)
5751 CHECKSUM_STRING (name);
5752 }
5753
5754 /* Calculate the checksum of a location expression. */
5755
5756 static inline void
5757 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5758 {
5759 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5760 were emitted as a DW_FORM_sdata instead of a location expression. */
5761 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5762 {
5763 CHECKSUM_ULEB128 (DW_FORM_sdata);
5764 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5765 return;
5766 }
5767
5768 /* Otherwise, just checksum the raw location expression. */
5769 while (loc != NULL)
5770 {
5771 inchash::hash hstate;
5772 hashval_t hash;
5773
5774 CHECKSUM_ULEB128 (loc->dtprel);
5775 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5776 hash_loc_operands (loc, hstate);
5777 hash = hstate.end ();
5778 CHECKSUM (hash);
5779 loc = loc->dw_loc_next;
5780 }
5781 }
5782
5783 /* Calculate the checksum of an attribute. */
5784
5785 static void
5786 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5787 struct md5_ctx *ctx, int *mark)
5788 {
5789 dw_loc_descr_ref loc;
5790 rtx r;
5791
5792 if (AT_class (at) == dw_val_class_die_ref)
5793 {
5794 dw_die_ref target_die = AT_ref (at);
5795
5796 /* For pointer and reference types, we checksum only the (qualified)
5797 name of the target type (if there is a name). For friend entries,
5798 we checksum only the (qualified) name of the target type or function.
5799 This allows the checksum to remain the same whether the target type
5800 is complete or not. */
5801 if ((at->dw_attr == DW_AT_type
5802 && (tag == DW_TAG_pointer_type
5803 || tag == DW_TAG_reference_type
5804 || tag == DW_TAG_rvalue_reference_type
5805 || tag == DW_TAG_ptr_to_member_type))
5806 || (at->dw_attr == DW_AT_friend
5807 && tag == DW_TAG_friend))
5808 {
5809 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5810
5811 if (name_attr != NULL)
5812 {
5813 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5814
5815 if (decl == NULL)
5816 decl = target_die;
5817 CHECKSUM_ULEB128 ('N');
5818 CHECKSUM_ULEB128 (at->dw_attr);
5819 if (decl->die_parent != NULL)
5820 checksum_die_context (decl->die_parent, ctx);
5821 CHECKSUM_ULEB128 ('E');
5822 CHECKSUM_STRING (AT_string (name_attr));
5823 return;
5824 }
5825 }
5826
5827 /* For all other references to another DIE, we check to see if the
5828 target DIE has already been visited. If it has, we emit a
5829 backward reference; if not, we descend recursively. */
5830 if (target_die->die_mark > 0)
5831 {
5832 CHECKSUM_ULEB128 ('R');
5833 CHECKSUM_ULEB128 (at->dw_attr);
5834 CHECKSUM_ULEB128 (target_die->die_mark);
5835 }
5836 else
5837 {
5838 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5839
5840 if (decl == NULL)
5841 decl = target_die;
5842 target_die->die_mark = ++(*mark);
5843 CHECKSUM_ULEB128 ('T');
5844 CHECKSUM_ULEB128 (at->dw_attr);
5845 if (decl->die_parent != NULL)
5846 checksum_die_context (decl->die_parent, ctx);
5847 die_checksum_ordered (target_die, ctx, mark);
5848 }
5849 return;
5850 }
5851
5852 CHECKSUM_ULEB128 ('A');
5853 CHECKSUM_ULEB128 (at->dw_attr);
5854
5855 switch (AT_class (at))
5856 {
5857 case dw_val_class_const:
5858 CHECKSUM_ULEB128 (DW_FORM_sdata);
5859 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5860 break;
5861
5862 case dw_val_class_unsigned_const:
5863 CHECKSUM_ULEB128 (DW_FORM_sdata);
5864 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5865 break;
5866
5867 case dw_val_class_const_double:
5868 CHECKSUM_ULEB128 (DW_FORM_block);
5869 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5870 CHECKSUM (at->dw_attr_val.v.val_double);
5871 break;
5872
5873 case dw_val_class_wide_int:
5874 CHECKSUM_ULEB128 (DW_FORM_block);
5875 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5876 CHECKSUM (*at->dw_attr_val.v.val_wide);
5877 break;
5878
5879 case dw_val_class_vec:
5880 CHECKSUM_ULEB128 (DW_FORM_block);
5881 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5882 * at->dw_attr_val.v.val_vec.elt_size);
5883 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5884 (at->dw_attr_val.v.val_vec.length
5885 * at->dw_attr_val.v.val_vec.elt_size));
5886 break;
5887
5888 case dw_val_class_flag:
5889 CHECKSUM_ULEB128 (DW_FORM_flag);
5890 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5891 break;
5892
5893 case dw_val_class_str:
5894 CHECKSUM_ULEB128 (DW_FORM_string);
5895 CHECKSUM_STRING (AT_string (at));
5896 break;
5897
5898 case dw_val_class_addr:
5899 r = AT_addr (at);
5900 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5901 CHECKSUM_ULEB128 (DW_FORM_string);
5902 CHECKSUM_STRING (XSTR (r, 0));
5903 break;
5904
5905 case dw_val_class_offset:
5906 CHECKSUM_ULEB128 (DW_FORM_sdata);
5907 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5908 break;
5909
5910 case dw_val_class_loc:
5911 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5912 loc_checksum_ordered (loc, ctx);
5913 break;
5914
5915 case dw_val_class_fde_ref:
5916 case dw_val_class_lbl_id:
5917 case dw_val_class_lineptr:
5918 case dw_val_class_macptr:
5919 case dw_val_class_high_pc:
5920 break;
5921
5922 case dw_val_class_file:
5923 CHECKSUM_ULEB128 (DW_FORM_string);
5924 CHECKSUM_STRING (AT_file (at)->filename);
5925 break;
5926
5927 case dw_val_class_data8:
5928 CHECKSUM (at->dw_attr_val.v.val_data8);
5929 break;
5930
5931 default:
5932 break;
5933 }
5934 }
5935
5936 struct checksum_attributes
5937 {
5938 dw_attr_ref at_name;
5939 dw_attr_ref at_type;
5940 dw_attr_ref at_friend;
5941 dw_attr_ref at_accessibility;
5942 dw_attr_ref at_address_class;
5943 dw_attr_ref at_allocated;
5944 dw_attr_ref at_artificial;
5945 dw_attr_ref at_associated;
5946 dw_attr_ref at_binary_scale;
5947 dw_attr_ref at_bit_offset;
5948 dw_attr_ref at_bit_size;
5949 dw_attr_ref at_bit_stride;
5950 dw_attr_ref at_byte_size;
5951 dw_attr_ref at_byte_stride;
5952 dw_attr_ref at_const_value;
5953 dw_attr_ref at_containing_type;
5954 dw_attr_ref at_count;
5955 dw_attr_ref at_data_location;
5956 dw_attr_ref at_data_member_location;
5957 dw_attr_ref at_decimal_scale;
5958 dw_attr_ref at_decimal_sign;
5959 dw_attr_ref at_default_value;
5960 dw_attr_ref at_digit_count;
5961 dw_attr_ref at_discr;
5962 dw_attr_ref at_discr_list;
5963 dw_attr_ref at_discr_value;
5964 dw_attr_ref at_encoding;
5965 dw_attr_ref at_endianity;
5966 dw_attr_ref at_explicit;
5967 dw_attr_ref at_is_optional;
5968 dw_attr_ref at_location;
5969 dw_attr_ref at_lower_bound;
5970 dw_attr_ref at_mutable;
5971 dw_attr_ref at_ordering;
5972 dw_attr_ref at_picture_string;
5973 dw_attr_ref at_prototyped;
5974 dw_attr_ref at_small;
5975 dw_attr_ref at_segment;
5976 dw_attr_ref at_string_length;
5977 dw_attr_ref at_threads_scaled;
5978 dw_attr_ref at_upper_bound;
5979 dw_attr_ref at_use_location;
5980 dw_attr_ref at_use_UTF8;
5981 dw_attr_ref at_variable_parameter;
5982 dw_attr_ref at_virtuality;
5983 dw_attr_ref at_visibility;
5984 dw_attr_ref at_vtable_elem_location;
5985 };
5986
5987 /* Collect the attributes that we will want to use for the checksum. */
5988
5989 static void
5990 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5991 {
5992 dw_attr_ref a;
5993 unsigned ix;
5994
5995 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5996 {
5997 switch (a->dw_attr)
5998 {
5999 case DW_AT_name:
6000 attrs->at_name = a;
6001 break;
6002 case DW_AT_type:
6003 attrs->at_type = a;
6004 break;
6005 case DW_AT_friend:
6006 attrs->at_friend = a;
6007 break;
6008 case DW_AT_accessibility:
6009 attrs->at_accessibility = a;
6010 break;
6011 case DW_AT_address_class:
6012 attrs->at_address_class = a;
6013 break;
6014 case DW_AT_allocated:
6015 attrs->at_allocated = a;
6016 break;
6017 case DW_AT_artificial:
6018 attrs->at_artificial = a;
6019 break;
6020 case DW_AT_associated:
6021 attrs->at_associated = a;
6022 break;
6023 case DW_AT_binary_scale:
6024 attrs->at_binary_scale = a;
6025 break;
6026 case DW_AT_bit_offset:
6027 attrs->at_bit_offset = a;
6028 break;
6029 case DW_AT_bit_size:
6030 attrs->at_bit_size = a;
6031 break;
6032 case DW_AT_bit_stride:
6033 attrs->at_bit_stride = a;
6034 break;
6035 case DW_AT_byte_size:
6036 attrs->at_byte_size = a;
6037 break;
6038 case DW_AT_byte_stride:
6039 attrs->at_byte_stride = a;
6040 break;
6041 case DW_AT_const_value:
6042 attrs->at_const_value = a;
6043 break;
6044 case DW_AT_containing_type:
6045 attrs->at_containing_type = a;
6046 break;
6047 case DW_AT_count:
6048 attrs->at_count = a;
6049 break;
6050 case DW_AT_data_location:
6051 attrs->at_data_location = a;
6052 break;
6053 case DW_AT_data_member_location:
6054 attrs->at_data_member_location = a;
6055 break;
6056 case DW_AT_decimal_scale:
6057 attrs->at_decimal_scale = a;
6058 break;
6059 case DW_AT_decimal_sign:
6060 attrs->at_decimal_sign = a;
6061 break;
6062 case DW_AT_default_value:
6063 attrs->at_default_value = a;
6064 break;
6065 case DW_AT_digit_count:
6066 attrs->at_digit_count = a;
6067 break;
6068 case DW_AT_discr:
6069 attrs->at_discr = a;
6070 break;
6071 case DW_AT_discr_list:
6072 attrs->at_discr_list = a;
6073 break;
6074 case DW_AT_discr_value:
6075 attrs->at_discr_value = a;
6076 break;
6077 case DW_AT_encoding:
6078 attrs->at_encoding = a;
6079 break;
6080 case DW_AT_endianity:
6081 attrs->at_endianity = a;
6082 break;
6083 case DW_AT_explicit:
6084 attrs->at_explicit = a;
6085 break;
6086 case DW_AT_is_optional:
6087 attrs->at_is_optional = a;
6088 break;
6089 case DW_AT_location:
6090 attrs->at_location = a;
6091 break;
6092 case DW_AT_lower_bound:
6093 attrs->at_lower_bound = a;
6094 break;
6095 case DW_AT_mutable:
6096 attrs->at_mutable = a;
6097 break;
6098 case DW_AT_ordering:
6099 attrs->at_ordering = a;
6100 break;
6101 case DW_AT_picture_string:
6102 attrs->at_picture_string = a;
6103 break;
6104 case DW_AT_prototyped:
6105 attrs->at_prototyped = a;
6106 break;
6107 case DW_AT_small:
6108 attrs->at_small = a;
6109 break;
6110 case DW_AT_segment:
6111 attrs->at_segment = a;
6112 break;
6113 case DW_AT_string_length:
6114 attrs->at_string_length = a;
6115 break;
6116 case DW_AT_threads_scaled:
6117 attrs->at_threads_scaled = a;
6118 break;
6119 case DW_AT_upper_bound:
6120 attrs->at_upper_bound = a;
6121 break;
6122 case DW_AT_use_location:
6123 attrs->at_use_location = a;
6124 break;
6125 case DW_AT_use_UTF8:
6126 attrs->at_use_UTF8 = a;
6127 break;
6128 case DW_AT_variable_parameter:
6129 attrs->at_variable_parameter = a;
6130 break;
6131 case DW_AT_virtuality:
6132 attrs->at_virtuality = a;
6133 break;
6134 case DW_AT_visibility:
6135 attrs->at_visibility = a;
6136 break;
6137 case DW_AT_vtable_elem_location:
6138 attrs->at_vtable_elem_location = a;
6139 break;
6140 default:
6141 break;
6142 }
6143 }
6144 }
6145
6146 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6147
6148 static void
6149 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6150 {
6151 dw_die_ref c;
6152 dw_die_ref decl;
6153 struct checksum_attributes attrs;
6154
6155 CHECKSUM_ULEB128 ('D');
6156 CHECKSUM_ULEB128 (die->die_tag);
6157
6158 memset (&attrs, 0, sizeof (attrs));
6159
6160 decl = get_AT_ref (die, DW_AT_specification);
6161 if (decl != NULL)
6162 collect_checksum_attributes (&attrs, decl);
6163 collect_checksum_attributes (&attrs, die);
6164
6165 CHECKSUM_ATTR (attrs.at_name);
6166 CHECKSUM_ATTR (attrs.at_accessibility);
6167 CHECKSUM_ATTR (attrs.at_address_class);
6168 CHECKSUM_ATTR (attrs.at_allocated);
6169 CHECKSUM_ATTR (attrs.at_artificial);
6170 CHECKSUM_ATTR (attrs.at_associated);
6171 CHECKSUM_ATTR (attrs.at_binary_scale);
6172 CHECKSUM_ATTR (attrs.at_bit_offset);
6173 CHECKSUM_ATTR (attrs.at_bit_size);
6174 CHECKSUM_ATTR (attrs.at_bit_stride);
6175 CHECKSUM_ATTR (attrs.at_byte_size);
6176 CHECKSUM_ATTR (attrs.at_byte_stride);
6177 CHECKSUM_ATTR (attrs.at_const_value);
6178 CHECKSUM_ATTR (attrs.at_containing_type);
6179 CHECKSUM_ATTR (attrs.at_count);
6180 CHECKSUM_ATTR (attrs.at_data_location);
6181 CHECKSUM_ATTR (attrs.at_data_member_location);
6182 CHECKSUM_ATTR (attrs.at_decimal_scale);
6183 CHECKSUM_ATTR (attrs.at_decimal_sign);
6184 CHECKSUM_ATTR (attrs.at_default_value);
6185 CHECKSUM_ATTR (attrs.at_digit_count);
6186 CHECKSUM_ATTR (attrs.at_discr);
6187 CHECKSUM_ATTR (attrs.at_discr_list);
6188 CHECKSUM_ATTR (attrs.at_discr_value);
6189 CHECKSUM_ATTR (attrs.at_encoding);
6190 CHECKSUM_ATTR (attrs.at_endianity);
6191 CHECKSUM_ATTR (attrs.at_explicit);
6192 CHECKSUM_ATTR (attrs.at_is_optional);
6193 CHECKSUM_ATTR (attrs.at_location);
6194 CHECKSUM_ATTR (attrs.at_lower_bound);
6195 CHECKSUM_ATTR (attrs.at_mutable);
6196 CHECKSUM_ATTR (attrs.at_ordering);
6197 CHECKSUM_ATTR (attrs.at_picture_string);
6198 CHECKSUM_ATTR (attrs.at_prototyped);
6199 CHECKSUM_ATTR (attrs.at_small);
6200 CHECKSUM_ATTR (attrs.at_segment);
6201 CHECKSUM_ATTR (attrs.at_string_length);
6202 CHECKSUM_ATTR (attrs.at_threads_scaled);
6203 CHECKSUM_ATTR (attrs.at_upper_bound);
6204 CHECKSUM_ATTR (attrs.at_use_location);
6205 CHECKSUM_ATTR (attrs.at_use_UTF8);
6206 CHECKSUM_ATTR (attrs.at_variable_parameter);
6207 CHECKSUM_ATTR (attrs.at_virtuality);
6208 CHECKSUM_ATTR (attrs.at_visibility);
6209 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6210 CHECKSUM_ATTR (attrs.at_type);
6211 CHECKSUM_ATTR (attrs.at_friend);
6212
6213 /* Checksum the child DIEs. */
6214 c = die->die_child;
6215 if (c) do {
6216 dw_attr_ref name_attr;
6217
6218 c = c->die_sib;
6219 name_attr = get_AT (c, DW_AT_name);
6220 if (is_template_instantiation (c))
6221 {
6222 /* Ignore instantiations of member type and function templates. */
6223 }
6224 else if (name_attr != NULL
6225 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6226 {
6227 /* Use a shallow checksum for named nested types and member
6228 functions. */
6229 CHECKSUM_ULEB128 ('S');
6230 CHECKSUM_ULEB128 (c->die_tag);
6231 CHECKSUM_STRING (AT_string (name_attr));
6232 }
6233 else
6234 {
6235 /* Use a deep checksum for other children. */
6236 /* Mark this DIE so it gets processed when unmarking. */
6237 if (c->die_mark == 0)
6238 c->die_mark = -1;
6239 die_checksum_ordered (c, ctx, mark);
6240 }
6241 } while (c != die->die_child);
6242
6243 CHECKSUM_ULEB128 (0);
6244 }
6245
6246 /* Add a type name and tag to a hash. */
6247 static void
6248 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6249 {
6250 CHECKSUM_ULEB128 (tag);
6251 CHECKSUM_STRING (name);
6252 }
6253
6254 #undef CHECKSUM
6255 #undef CHECKSUM_STRING
6256 #undef CHECKSUM_ATTR
6257 #undef CHECKSUM_LEB128
6258 #undef CHECKSUM_ULEB128
6259
6260 /* Generate the type signature for DIE. This is computed by generating an
6261 MD5 checksum over the DIE's tag, its relevant attributes, and its
6262 children. Attributes that are references to other DIEs are processed
6263 by recursion, using the MARK field to prevent infinite recursion.
6264 If the DIE is nested inside a namespace or another type, we also
6265 need to include that context in the signature. The lower 64 bits
6266 of the resulting MD5 checksum comprise the signature. */
6267
6268 static void
6269 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6270 {
6271 int mark;
6272 const char *name;
6273 unsigned char checksum[16];
6274 struct md5_ctx ctx;
6275 dw_die_ref decl;
6276 dw_die_ref parent;
6277
6278 name = get_AT_string (die, DW_AT_name);
6279 decl = get_AT_ref (die, DW_AT_specification);
6280 parent = get_die_parent (die);
6281
6282 /* First, compute a signature for just the type name (and its surrounding
6283 context, if any. This is stored in the type unit DIE for link-time
6284 ODR (one-definition rule) checking. */
6285
6286 if (is_cxx () && name != NULL)
6287 {
6288 md5_init_ctx (&ctx);
6289
6290 /* Checksum the names of surrounding namespaces and structures. */
6291 if (parent != NULL)
6292 checksum_die_context (parent, &ctx);
6293
6294 /* Checksum the current DIE. */
6295 die_odr_checksum (die->die_tag, name, &ctx);
6296 md5_finish_ctx (&ctx, checksum);
6297
6298 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6299 }
6300
6301 /* Next, compute the complete type signature. */
6302
6303 md5_init_ctx (&ctx);
6304 mark = 1;
6305 die->die_mark = mark;
6306
6307 /* Checksum the names of surrounding namespaces and structures. */
6308 if (parent != NULL)
6309 checksum_die_context (parent, &ctx);
6310
6311 /* Checksum the DIE and its children. */
6312 die_checksum_ordered (die, &ctx, &mark);
6313 unmark_all_dies (die);
6314 md5_finish_ctx (&ctx, checksum);
6315
6316 /* Store the signature in the type node and link the type DIE and the
6317 type node together. */
6318 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6319 DWARF_TYPE_SIGNATURE_SIZE);
6320 die->comdat_type_p = true;
6321 die->die_id.die_type_node = type_node;
6322 type_node->type_die = die;
6323
6324 /* If the DIE is a specification, link its declaration to the type node
6325 as well. */
6326 if (decl != NULL)
6327 {
6328 decl->comdat_type_p = true;
6329 decl->die_id.die_type_node = type_node;
6330 }
6331 }
6332
6333 /* Do the location expressions look same? */
6334 static inline int
6335 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6336 {
6337 return loc1->dw_loc_opc == loc2->dw_loc_opc
6338 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6339 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6340 }
6341
6342 /* Do the values look the same? */
6343 static int
6344 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6345 {
6346 dw_loc_descr_ref loc1, loc2;
6347 rtx r1, r2;
6348
6349 if (v1->val_class != v2->val_class)
6350 return 0;
6351
6352 switch (v1->val_class)
6353 {
6354 case dw_val_class_const:
6355 return v1->v.val_int == v2->v.val_int;
6356 case dw_val_class_unsigned_const:
6357 return v1->v.val_unsigned == v2->v.val_unsigned;
6358 case dw_val_class_const_double:
6359 return v1->v.val_double.high == v2->v.val_double.high
6360 && v1->v.val_double.low == v2->v.val_double.low;
6361 case dw_val_class_wide_int:
6362 return *v1->v.val_wide == *v2->v.val_wide;
6363 case dw_val_class_vec:
6364 if (v1->v.val_vec.length != v2->v.val_vec.length
6365 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6366 return 0;
6367 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6368 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6369 return 0;
6370 return 1;
6371 case dw_val_class_flag:
6372 return v1->v.val_flag == v2->v.val_flag;
6373 case dw_val_class_str:
6374 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6375
6376 case dw_val_class_addr:
6377 r1 = v1->v.val_addr;
6378 r2 = v2->v.val_addr;
6379 if (GET_CODE (r1) != GET_CODE (r2))
6380 return 0;
6381 return !rtx_equal_p (r1, r2);
6382
6383 case dw_val_class_offset:
6384 return v1->v.val_offset == v2->v.val_offset;
6385
6386 case dw_val_class_loc:
6387 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6388 loc1 && loc2;
6389 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6390 if (!same_loc_p (loc1, loc2, mark))
6391 return 0;
6392 return !loc1 && !loc2;
6393
6394 case dw_val_class_die_ref:
6395 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6396
6397 case dw_val_class_fde_ref:
6398 case dw_val_class_vms_delta:
6399 case dw_val_class_lbl_id:
6400 case dw_val_class_lineptr:
6401 case dw_val_class_macptr:
6402 case dw_val_class_high_pc:
6403 return 1;
6404
6405 case dw_val_class_file:
6406 return v1->v.val_file == v2->v.val_file;
6407
6408 case dw_val_class_data8:
6409 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6410
6411 default:
6412 return 1;
6413 }
6414 }
6415
6416 /* Do the attributes look the same? */
6417
6418 static int
6419 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6420 {
6421 if (at1->dw_attr != at2->dw_attr)
6422 return 0;
6423
6424 /* We don't care that this was compiled with a different compiler
6425 snapshot; if the output is the same, that's what matters. */
6426 if (at1->dw_attr == DW_AT_producer)
6427 return 1;
6428
6429 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6430 }
6431
6432 /* Do the dies look the same? */
6433
6434 static int
6435 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6436 {
6437 dw_die_ref c1, c2;
6438 dw_attr_ref a1;
6439 unsigned ix;
6440
6441 /* To avoid infinite recursion. */
6442 if (die1->die_mark)
6443 return die1->die_mark == die2->die_mark;
6444 die1->die_mark = die2->die_mark = ++(*mark);
6445
6446 if (die1->die_tag != die2->die_tag)
6447 return 0;
6448
6449 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6450 return 0;
6451
6452 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6453 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6454 return 0;
6455
6456 c1 = die1->die_child;
6457 c2 = die2->die_child;
6458 if (! c1)
6459 {
6460 if (c2)
6461 return 0;
6462 }
6463 else
6464 for (;;)
6465 {
6466 if (!same_die_p (c1, c2, mark))
6467 return 0;
6468 c1 = c1->die_sib;
6469 c2 = c2->die_sib;
6470 if (c1 == die1->die_child)
6471 {
6472 if (c2 == die2->die_child)
6473 break;
6474 else
6475 return 0;
6476 }
6477 }
6478
6479 return 1;
6480 }
6481
6482 /* Do the dies look the same? Wrapper around same_die_p. */
6483
6484 static int
6485 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6486 {
6487 int mark = 0;
6488 int ret = same_die_p (die1, die2, &mark);
6489
6490 unmark_all_dies (die1);
6491 unmark_all_dies (die2);
6492
6493 return ret;
6494 }
6495
6496 /* The prefix to attach to symbols on DIEs in the current comdat debug
6497 info section. */
6498 static const char *comdat_symbol_id;
6499
6500 /* The index of the current symbol within the current comdat CU. */
6501 static unsigned int comdat_symbol_number;
6502
6503 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6504 children, and set comdat_symbol_id accordingly. */
6505
6506 static void
6507 compute_section_prefix (dw_die_ref unit_die)
6508 {
6509 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6510 const char *base = die_name ? lbasename (die_name) : "anonymous";
6511 char *name = XALLOCAVEC (char, strlen (base) + 64);
6512 char *p;
6513 int i, mark;
6514 unsigned char checksum[16];
6515 struct md5_ctx ctx;
6516
6517 /* Compute the checksum of the DIE, then append part of it as hex digits to
6518 the name filename of the unit. */
6519
6520 md5_init_ctx (&ctx);
6521 mark = 0;
6522 die_checksum (unit_die, &ctx, &mark);
6523 unmark_all_dies (unit_die);
6524 md5_finish_ctx (&ctx, checksum);
6525
6526 sprintf (name, "%s.", base);
6527 clean_symbol_name (name);
6528
6529 p = name + strlen (name);
6530 for (i = 0; i < 4; i++)
6531 {
6532 sprintf (p, "%.2x", checksum[i]);
6533 p += 2;
6534 }
6535
6536 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6537 comdat_symbol_number = 0;
6538 }
6539
6540 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6541
6542 static int
6543 is_type_die (dw_die_ref die)
6544 {
6545 switch (die->die_tag)
6546 {
6547 case DW_TAG_array_type:
6548 case DW_TAG_class_type:
6549 case DW_TAG_interface_type:
6550 case DW_TAG_enumeration_type:
6551 case DW_TAG_pointer_type:
6552 case DW_TAG_reference_type:
6553 case DW_TAG_rvalue_reference_type:
6554 case DW_TAG_string_type:
6555 case DW_TAG_structure_type:
6556 case DW_TAG_subroutine_type:
6557 case DW_TAG_union_type:
6558 case DW_TAG_ptr_to_member_type:
6559 case DW_TAG_set_type:
6560 case DW_TAG_subrange_type:
6561 case DW_TAG_base_type:
6562 case DW_TAG_const_type:
6563 case DW_TAG_file_type:
6564 case DW_TAG_packed_type:
6565 case DW_TAG_volatile_type:
6566 case DW_TAG_typedef:
6567 return 1;
6568 default:
6569 return 0;
6570 }
6571 }
6572
6573 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6574 Basically, we want to choose the bits that are likely to be shared between
6575 compilations (types) and leave out the bits that are specific to individual
6576 compilations (functions). */
6577
6578 static int
6579 is_comdat_die (dw_die_ref c)
6580 {
6581 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6582 we do for stabs. The advantage is a greater likelihood of sharing between
6583 objects that don't include headers in the same order (and therefore would
6584 put the base types in a different comdat). jason 8/28/00 */
6585
6586 if (c->die_tag == DW_TAG_base_type)
6587 return 0;
6588
6589 if (c->die_tag == DW_TAG_pointer_type
6590 || c->die_tag == DW_TAG_reference_type
6591 || c->die_tag == DW_TAG_rvalue_reference_type
6592 || c->die_tag == DW_TAG_const_type
6593 || c->die_tag == DW_TAG_volatile_type)
6594 {
6595 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6596
6597 return t ? is_comdat_die (t) : 0;
6598 }
6599
6600 return is_type_die (c);
6601 }
6602
6603 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6604 compilation unit. */
6605
6606 static int
6607 is_symbol_die (dw_die_ref c)
6608 {
6609 return (is_type_die (c)
6610 || is_declaration_die (c)
6611 || c->die_tag == DW_TAG_namespace
6612 || c->die_tag == DW_TAG_module);
6613 }
6614
6615 /* Returns true iff C is a compile-unit DIE. */
6616
6617 static inline bool
6618 is_cu_die (dw_die_ref c)
6619 {
6620 return c && c->die_tag == DW_TAG_compile_unit;
6621 }
6622
6623 /* Returns true iff C is a unit DIE of some sort. */
6624
6625 static inline bool
6626 is_unit_die (dw_die_ref c)
6627 {
6628 return c && (c->die_tag == DW_TAG_compile_unit
6629 || c->die_tag == DW_TAG_partial_unit
6630 || c->die_tag == DW_TAG_type_unit);
6631 }
6632
6633 /* Returns true iff C is a namespace DIE. */
6634
6635 static inline bool
6636 is_namespace_die (dw_die_ref c)
6637 {
6638 return c && c->die_tag == DW_TAG_namespace;
6639 }
6640
6641 /* Returns true iff C is a class or structure DIE. */
6642
6643 static inline bool
6644 is_class_die (dw_die_ref c)
6645 {
6646 return c && (c->die_tag == DW_TAG_class_type
6647 || c->die_tag == DW_TAG_structure_type);
6648 }
6649
6650 /* Return non-zero if this DIE is a template parameter. */
6651
6652 static inline bool
6653 is_template_parameter (dw_die_ref die)
6654 {
6655 switch (die->die_tag)
6656 {
6657 case DW_TAG_template_type_param:
6658 case DW_TAG_template_value_param:
6659 case DW_TAG_GNU_template_template_param:
6660 case DW_TAG_GNU_template_parameter_pack:
6661 return true;
6662 default:
6663 return false;
6664 }
6665 }
6666
6667 /* Return non-zero if this DIE represents a template instantiation. */
6668
6669 static inline bool
6670 is_template_instantiation (dw_die_ref die)
6671 {
6672 dw_die_ref c;
6673
6674 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6675 return false;
6676 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6677 return false;
6678 }
6679
6680 static char *
6681 gen_internal_sym (const char *prefix)
6682 {
6683 char buf[256];
6684
6685 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6686 return xstrdup (buf);
6687 }
6688
6689 /* Assign symbols to all worthy DIEs under DIE. */
6690
6691 static void
6692 assign_symbol_names (dw_die_ref die)
6693 {
6694 dw_die_ref c;
6695
6696 if (is_symbol_die (die) && !die->comdat_type_p)
6697 {
6698 if (comdat_symbol_id)
6699 {
6700 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6701
6702 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6703 comdat_symbol_id, comdat_symbol_number++);
6704 die->die_id.die_symbol = xstrdup (p);
6705 }
6706 else
6707 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6708 }
6709
6710 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6711 }
6712
6713 struct cu_hash_table_entry
6714 {
6715 dw_die_ref cu;
6716 unsigned min_comdat_num, max_comdat_num;
6717 struct cu_hash_table_entry *next;
6718 };
6719
6720 /* Helpers to manipulate hash table of CUs. */
6721
6722 struct cu_hash_table_entry_hasher
6723 {
6724 typedef cu_hash_table_entry value_type;
6725 typedef die_struct compare_type;
6726 static inline hashval_t hash (const value_type *);
6727 static inline bool equal (const value_type *, const compare_type *);
6728 static inline void remove (value_type *);
6729 };
6730
6731 inline hashval_t
6732 cu_hash_table_entry_hasher::hash (const value_type *entry)
6733 {
6734 return htab_hash_string (entry->cu->die_id.die_symbol);
6735 }
6736
6737 inline bool
6738 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6739 const compare_type *entry2)
6740 {
6741 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6742 }
6743
6744 inline void
6745 cu_hash_table_entry_hasher::remove (value_type *entry)
6746 {
6747 struct cu_hash_table_entry *next;
6748
6749 while (entry)
6750 {
6751 next = entry->next;
6752 free (entry);
6753 entry = next;
6754 }
6755 }
6756
6757 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6758
6759 /* Check whether we have already seen this CU and set up SYM_NUM
6760 accordingly. */
6761 static int
6762 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6763 {
6764 struct cu_hash_table_entry dummy;
6765 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6766
6767 dummy.max_comdat_num = 0;
6768
6769 slot = htable->find_slot_with_hash (cu,
6770 htab_hash_string (cu->die_id.die_symbol),
6771 INSERT);
6772 entry = *slot;
6773
6774 for (; entry; last = entry, entry = entry->next)
6775 {
6776 if (same_die_p_wrap (cu, entry->cu))
6777 break;
6778 }
6779
6780 if (entry)
6781 {
6782 *sym_num = entry->min_comdat_num;
6783 return 1;
6784 }
6785
6786 entry = XCNEW (struct cu_hash_table_entry);
6787 entry->cu = cu;
6788 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6789 entry->next = *slot;
6790 *slot = entry;
6791
6792 return 0;
6793 }
6794
6795 /* Record SYM_NUM to record of CU in HTABLE. */
6796 static void
6797 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6798 unsigned int sym_num)
6799 {
6800 struct cu_hash_table_entry **slot, *entry;
6801
6802 slot = htable->find_slot_with_hash (cu,
6803 htab_hash_string (cu->die_id.die_symbol),
6804 NO_INSERT);
6805 entry = *slot;
6806
6807 entry->max_comdat_num = sym_num;
6808 }
6809
6810 /* Traverse the DIE (which is always comp_unit_die), and set up
6811 additional compilation units for each of the include files we see
6812 bracketed by BINCL/EINCL. */
6813
6814 static void
6815 break_out_includes (dw_die_ref die)
6816 {
6817 dw_die_ref c;
6818 dw_die_ref unit = NULL;
6819 limbo_die_node *node, **pnode;
6820
6821 c = die->die_child;
6822 if (c) do {
6823 dw_die_ref prev = c;
6824 c = c->die_sib;
6825 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6826 || (unit && is_comdat_die (c)))
6827 {
6828 dw_die_ref next = c->die_sib;
6829
6830 /* This DIE is for a secondary CU; remove it from the main one. */
6831 remove_child_with_prev (c, prev);
6832
6833 if (c->die_tag == DW_TAG_GNU_BINCL)
6834 unit = push_new_compile_unit (unit, c);
6835 else if (c->die_tag == DW_TAG_GNU_EINCL)
6836 unit = pop_compile_unit (unit);
6837 else
6838 add_child_die (unit, c);
6839 c = next;
6840 if (c == die->die_child)
6841 break;
6842 }
6843 } while (c != die->die_child);
6844
6845 #if 0
6846 /* We can only use this in debugging, since the frontend doesn't check
6847 to make sure that we leave every include file we enter. */
6848 gcc_assert (!unit);
6849 #endif
6850
6851 assign_symbol_names (die);
6852 cu_hash_type cu_hash_table (10);
6853 for (node = limbo_die_list, pnode = &limbo_die_list;
6854 node;
6855 node = node->next)
6856 {
6857 int is_dupl;
6858
6859 compute_section_prefix (node->die);
6860 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6861 &comdat_symbol_number);
6862 assign_symbol_names (node->die);
6863 if (is_dupl)
6864 *pnode = node->next;
6865 else
6866 {
6867 pnode = &node->next;
6868 record_comdat_symbol_number (node->die, &cu_hash_table,
6869 comdat_symbol_number);
6870 }
6871 }
6872 }
6873
6874 /* Return non-zero if this DIE is a declaration. */
6875
6876 static int
6877 is_declaration_die (dw_die_ref die)
6878 {
6879 dw_attr_ref a;
6880 unsigned ix;
6881
6882 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6883 if (a->dw_attr == DW_AT_declaration)
6884 return 1;
6885
6886 return 0;
6887 }
6888
6889 /* Return non-zero if this DIE is nested inside a subprogram. */
6890
6891 static int
6892 is_nested_in_subprogram (dw_die_ref die)
6893 {
6894 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6895
6896 if (decl == NULL)
6897 decl = die;
6898 return local_scope_p (decl);
6899 }
6900
6901 /* Return non-zero if this DIE contains a defining declaration of a
6902 subprogram. */
6903
6904 static int
6905 contains_subprogram_definition (dw_die_ref die)
6906 {
6907 dw_die_ref c;
6908
6909 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6910 return 1;
6911 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6912 return 0;
6913 }
6914
6915 /* Return non-zero if this is a type DIE that should be moved to a
6916 COMDAT .debug_types section. */
6917
6918 static int
6919 should_move_die_to_comdat (dw_die_ref die)
6920 {
6921 switch (die->die_tag)
6922 {
6923 case DW_TAG_class_type:
6924 case DW_TAG_structure_type:
6925 case DW_TAG_enumeration_type:
6926 case DW_TAG_union_type:
6927 /* Don't move declarations, inlined instances, types nested in a
6928 subprogram, or types that contain subprogram definitions. */
6929 if (is_declaration_die (die)
6930 || get_AT (die, DW_AT_abstract_origin)
6931 || is_nested_in_subprogram (die)
6932 || contains_subprogram_definition (die))
6933 return 0;
6934 return 1;
6935 case DW_TAG_array_type:
6936 case DW_TAG_interface_type:
6937 case DW_TAG_pointer_type:
6938 case DW_TAG_reference_type:
6939 case DW_TAG_rvalue_reference_type:
6940 case DW_TAG_string_type:
6941 case DW_TAG_subroutine_type:
6942 case DW_TAG_ptr_to_member_type:
6943 case DW_TAG_set_type:
6944 case DW_TAG_subrange_type:
6945 case DW_TAG_base_type:
6946 case DW_TAG_const_type:
6947 case DW_TAG_file_type:
6948 case DW_TAG_packed_type:
6949 case DW_TAG_volatile_type:
6950 case DW_TAG_typedef:
6951 default:
6952 return 0;
6953 }
6954 }
6955
6956 /* Make a clone of DIE. */
6957
6958 static dw_die_ref
6959 clone_die (dw_die_ref die)
6960 {
6961 dw_die_ref clone;
6962 dw_attr_ref a;
6963 unsigned ix;
6964
6965 clone = ggc_cleared_alloc<die_node> ();
6966 clone->die_tag = die->die_tag;
6967
6968 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6969 add_dwarf_attr (clone, a);
6970
6971 return clone;
6972 }
6973
6974 /* Make a clone of the tree rooted at DIE. */
6975
6976 static dw_die_ref
6977 clone_tree (dw_die_ref die)
6978 {
6979 dw_die_ref c;
6980 dw_die_ref clone = clone_die (die);
6981
6982 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6983
6984 return clone;
6985 }
6986
6987 /* Make a clone of DIE as a declaration. */
6988
6989 static dw_die_ref
6990 clone_as_declaration (dw_die_ref die)
6991 {
6992 dw_die_ref clone;
6993 dw_die_ref decl;
6994 dw_attr_ref a;
6995 unsigned ix;
6996
6997 /* If the DIE is already a declaration, just clone it. */
6998 if (is_declaration_die (die))
6999 return clone_die (die);
7000
7001 /* If the DIE is a specification, just clone its declaration DIE. */
7002 decl = get_AT_ref (die, DW_AT_specification);
7003 if (decl != NULL)
7004 {
7005 clone = clone_die (decl);
7006 if (die->comdat_type_p)
7007 add_AT_die_ref (clone, DW_AT_signature, die);
7008 return clone;
7009 }
7010
7011 clone = ggc_cleared_alloc<die_node> ();
7012 clone->die_tag = die->die_tag;
7013
7014 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7015 {
7016 /* We don't want to copy over all attributes.
7017 For example we don't want DW_AT_byte_size because otherwise we will no
7018 longer have a declaration and GDB will treat it as a definition. */
7019
7020 switch (a->dw_attr)
7021 {
7022 case DW_AT_abstract_origin:
7023 case DW_AT_artificial:
7024 case DW_AT_containing_type:
7025 case DW_AT_external:
7026 case DW_AT_name:
7027 case DW_AT_type:
7028 case DW_AT_virtuality:
7029 case DW_AT_linkage_name:
7030 case DW_AT_MIPS_linkage_name:
7031 add_dwarf_attr (clone, a);
7032 break;
7033 case DW_AT_byte_size:
7034 default:
7035 break;
7036 }
7037 }
7038
7039 if (die->comdat_type_p)
7040 add_AT_die_ref (clone, DW_AT_signature, die);
7041
7042 add_AT_flag (clone, DW_AT_declaration, 1);
7043 return clone;
7044 }
7045
7046
7047 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7048
7049 struct decl_table_entry
7050 {
7051 dw_die_ref orig;
7052 dw_die_ref copy;
7053 };
7054
7055 /* Helpers to manipulate hash table of copied declarations. */
7056
7057 /* Hashtable helpers. */
7058
7059 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7060 {
7061 typedef decl_table_entry value_type;
7062 typedef die_struct compare_type;
7063 static inline hashval_t hash (const value_type *);
7064 static inline bool equal (const value_type *, const compare_type *);
7065 };
7066
7067 inline hashval_t
7068 decl_table_entry_hasher::hash (const value_type *entry)
7069 {
7070 return htab_hash_pointer (entry->orig);
7071 }
7072
7073 inline bool
7074 decl_table_entry_hasher::equal (const value_type *entry1,
7075 const compare_type *entry2)
7076 {
7077 return entry1->orig == entry2;
7078 }
7079
7080 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7081
7082 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7083 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7084 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7085 to check if the ancestor has already been copied into UNIT. */
7086
7087 static dw_die_ref
7088 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7089 decl_hash_type *decl_table)
7090 {
7091 dw_die_ref parent = die->die_parent;
7092 dw_die_ref new_parent = unit;
7093 dw_die_ref copy;
7094 decl_table_entry **slot = NULL;
7095 struct decl_table_entry *entry = NULL;
7096
7097 if (decl_table)
7098 {
7099 /* Check if the entry has already been copied to UNIT. */
7100 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7101 INSERT);
7102 if (*slot != HTAB_EMPTY_ENTRY)
7103 {
7104 entry = *slot;
7105 return entry->copy;
7106 }
7107
7108 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7109 entry = XCNEW (struct decl_table_entry);
7110 entry->orig = die;
7111 entry->copy = NULL;
7112 *slot = entry;
7113 }
7114
7115 if (parent != NULL)
7116 {
7117 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7118 if (spec != NULL)
7119 parent = spec;
7120 if (!is_unit_die (parent))
7121 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7122 }
7123
7124 copy = clone_as_declaration (die);
7125 add_child_die (new_parent, copy);
7126
7127 if (decl_table)
7128 {
7129 /* Record the pointer to the copy. */
7130 entry->copy = copy;
7131 }
7132
7133 return copy;
7134 }
7135 /* Copy the declaration context to the new type unit DIE. This includes
7136 any surrounding namespace or type declarations. If the DIE has an
7137 AT_specification attribute, it also includes attributes and children
7138 attached to the specification, and returns a pointer to the original
7139 parent of the declaration DIE. Returns NULL otherwise. */
7140
7141 static dw_die_ref
7142 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7143 {
7144 dw_die_ref decl;
7145 dw_die_ref new_decl;
7146 dw_die_ref orig_parent = NULL;
7147
7148 decl = get_AT_ref (die, DW_AT_specification);
7149 if (decl == NULL)
7150 decl = die;
7151 else
7152 {
7153 unsigned ix;
7154 dw_die_ref c;
7155 dw_attr_ref a;
7156
7157 /* The original DIE will be changed to a declaration, and must
7158 be moved to be a child of the original declaration DIE. */
7159 orig_parent = decl->die_parent;
7160
7161 /* Copy the type node pointer from the new DIE to the original
7162 declaration DIE so we can forward references later. */
7163 decl->comdat_type_p = true;
7164 decl->die_id.die_type_node = die->die_id.die_type_node;
7165
7166 remove_AT (die, DW_AT_specification);
7167
7168 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7169 {
7170 if (a->dw_attr != DW_AT_name
7171 && a->dw_attr != DW_AT_declaration
7172 && a->dw_attr != DW_AT_external)
7173 add_dwarf_attr (die, a);
7174 }
7175
7176 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7177 }
7178
7179 if (decl->die_parent != NULL
7180 && !is_unit_die (decl->die_parent))
7181 {
7182 new_decl = copy_ancestor_tree (unit, decl, NULL);
7183 if (new_decl != NULL)
7184 {
7185 remove_AT (new_decl, DW_AT_signature);
7186 add_AT_specification (die, new_decl);
7187 }
7188 }
7189
7190 return orig_parent;
7191 }
7192
7193 /* Generate the skeleton ancestor tree for the given NODE, then clone
7194 the DIE and add the clone into the tree. */
7195
7196 static void
7197 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7198 {
7199 if (node->new_die != NULL)
7200 return;
7201
7202 node->new_die = clone_as_declaration (node->old_die);
7203
7204 if (node->parent != NULL)
7205 {
7206 generate_skeleton_ancestor_tree (node->parent);
7207 add_child_die (node->parent->new_die, node->new_die);
7208 }
7209 }
7210
7211 /* Generate a skeleton tree of DIEs containing any declarations that are
7212 found in the original tree. We traverse the tree looking for declaration
7213 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7214
7215 static void
7216 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7217 {
7218 skeleton_chain_node node;
7219 dw_die_ref c;
7220 dw_die_ref first;
7221 dw_die_ref prev = NULL;
7222 dw_die_ref next = NULL;
7223
7224 node.parent = parent;
7225
7226 first = c = parent->old_die->die_child;
7227 if (c)
7228 next = c->die_sib;
7229 if (c) do {
7230 if (prev == NULL || prev->die_sib == c)
7231 prev = c;
7232 c = next;
7233 next = (c == first ? NULL : c->die_sib);
7234 node.old_die = c;
7235 node.new_die = NULL;
7236 if (is_declaration_die (c))
7237 {
7238 if (is_template_instantiation (c))
7239 {
7240 /* Instantiated templates do not need to be cloned into the
7241 type unit. Just move the DIE and its children back to
7242 the skeleton tree (in the main CU). */
7243 remove_child_with_prev (c, prev);
7244 add_child_die (parent->new_die, c);
7245 c = prev;
7246 }
7247 else
7248 {
7249 /* Clone the existing DIE, move the original to the skeleton
7250 tree (which is in the main CU), and put the clone, with
7251 all the original's children, where the original came from
7252 (which is about to be moved to the type unit). */
7253 dw_die_ref clone = clone_die (c);
7254 move_all_children (c, clone);
7255
7256 /* If the original has a DW_AT_object_pointer attribute,
7257 it would now point to a child DIE just moved to the
7258 cloned tree, so we need to remove that attribute from
7259 the original. */
7260 remove_AT (c, DW_AT_object_pointer);
7261
7262 replace_child (c, clone, prev);
7263 generate_skeleton_ancestor_tree (parent);
7264 add_child_die (parent->new_die, c);
7265 node.new_die = c;
7266 c = clone;
7267 }
7268 }
7269 generate_skeleton_bottom_up (&node);
7270 } while (next != NULL);
7271 }
7272
7273 /* Wrapper function for generate_skeleton_bottom_up. */
7274
7275 static dw_die_ref
7276 generate_skeleton (dw_die_ref die)
7277 {
7278 skeleton_chain_node node;
7279
7280 node.old_die = die;
7281 node.new_die = NULL;
7282 node.parent = NULL;
7283
7284 /* If this type definition is nested inside another type,
7285 and is not an instantiation of a template, always leave
7286 at least a declaration in its place. */
7287 if (die->die_parent != NULL
7288 && is_type_die (die->die_parent)
7289 && !is_template_instantiation (die))
7290 node.new_die = clone_as_declaration (die);
7291
7292 generate_skeleton_bottom_up (&node);
7293 return node.new_die;
7294 }
7295
7296 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7297 declaration. The original DIE is moved to a new compile unit so that
7298 existing references to it follow it to the new location. If any of the
7299 original DIE's descendants is a declaration, we need to replace the
7300 original DIE with a skeleton tree and move the declarations back into the
7301 skeleton tree. */
7302
7303 static dw_die_ref
7304 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7305 dw_die_ref prev)
7306 {
7307 dw_die_ref skeleton, orig_parent;
7308
7309 /* Copy the declaration context to the type unit DIE. If the returned
7310 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7311 that DIE. */
7312 orig_parent = copy_declaration_context (unit, child);
7313
7314 skeleton = generate_skeleton (child);
7315 if (skeleton == NULL)
7316 remove_child_with_prev (child, prev);
7317 else
7318 {
7319 skeleton->comdat_type_p = true;
7320 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7321
7322 /* If the original DIE was a specification, we need to put
7323 the skeleton under the parent DIE of the declaration.
7324 This leaves the original declaration in the tree, but
7325 it will be pruned later since there are no longer any
7326 references to it. */
7327 if (orig_parent != NULL)
7328 {
7329 remove_child_with_prev (child, prev);
7330 add_child_die (orig_parent, skeleton);
7331 }
7332 else
7333 replace_child (child, skeleton, prev);
7334 }
7335
7336 return skeleton;
7337 }
7338
7339 /* Traverse the DIE and set up additional .debug_types sections for each
7340 type worthy of being placed in a COMDAT section. */
7341
7342 static void
7343 break_out_comdat_types (dw_die_ref die)
7344 {
7345 dw_die_ref c;
7346 dw_die_ref first;
7347 dw_die_ref prev = NULL;
7348 dw_die_ref next = NULL;
7349 dw_die_ref unit = NULL;
7350
7351 first = c = die->die_child;
7352 if (c)
7353 next = c->die_sib;
7354 if (c) do {
7355 if (prev == NULL || prev->die_sib == c)
7356 prev = c;
7357 c = next;
7358 next = (c == first ? NULL : c->die_sib);
7359 if (should_move_die_to_comdat (c))
7360 {
7361 dw_die_ref replacement;
7362 comdat_type_node_ref type_node;
7363
7364 /* Break out nested types into their own type units. */
7365 break_out_comdat_types (c);
7366
7367 /* Create a new type unit DIE as the root for the new tree, and
7368 add it to the list of comdat types. */
7369 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7370 add_AT_unsigned (unit, DW_AT_language,
7371 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7372 type_node = ggc_cleared_alloc<comdat_type_node> ();
7373 type_node->root_die = unit;
7374 type_node->next = comdat_type_list;
7375 comdat_type_list = type_node;
7376
7377 /* Generate the type signature. */
7378 generate_type_signature (c, type_node);
7379
7380 /* Copy the declaration context, attributes, and children of the
7381 declaration into the new type unit DIE, then remove this DIE
7382 from the main CU (or replace it with a skeleton if necessary). */
7383 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7384 type_node->skeleton_die = replacement;
7385
7386 /* Add the DIE to the new compunit. */
7387 add_child_die (unit, c);
7388
7389 if (replacement != NULL)
7390 c = replacement;
7391 }
7392 else if (c->die_tag == DW_TAG_namespace
7393 || c->die_tag == DW_TAG_class_type
7394 || c->die_tag == DW_TAG_structure_type
7395 || c->die_tag == DW_TAG_union_type)
7396 {
7397 /* Look for nested types that can be broken out. */
7398 break_out_comdat_types (c);
7399 }
7400 } while (next != NULL);
7401 }
7402
7403 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7404 Enter all the cloned children into the hash table decl_table. */
7405
7406 static dw_die_ref
7407 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7408 {
7409 dw_die_ref c;
7410 dw_die_ref clone;
7411 struct decl_table_entry *entry;
7412 decl_table_entry **slot;
7413
7414 if (die->die_tag == DW_TAG_subprogram)
7415 clone = clone_as_declaration (die);
7416 else
7417 clone = clone_die (die);
7418
7419 slot = decl_table->find_slot_with_hash (die,
7420 htab_hash_pointer (die), INSERT);
7421
7422 /* Assert that DIE isn't in the hash table yet. If it would be there
7423 before, the ancestors would be necessarily there as well, therefore
7424 clone_tree_partial wouldn't be called. */
7425 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7426
7427 entry = XCNEW (struct decl_table_entry);
7428 entry->orig = die;
7429 entry->copy = clone;
7430 *slot = entry;
7431
7432 if (die->die_tag != DW_TAG_subprogram)
7433 FOR_EACH_CHILD (die, c,
7434 add_child_die (clone, clone_tree_partial (c, decl_table)));
7435
7436 return clone;
7437 }
7438
7439 /* Walk the DIE and its children, looking for references to incomplete
7440 or trivial types that are unmarked (i.e., that are not in the current
7441 type_unit). */
7442
7443 static void
7444 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7445 {
7446 dw_die_ref c;
7447 dw_attr_ref a;
7448 unsigned ix;
7449
7450 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7451 {
7452 if (AT_class (a) == dw_val_class_die_ref)
7453 {
7454 dw_die_ref targ = AT_ref (a);
7455 decl_table_entry **slot;
7456 struct decl_table_entry *entry;
7457
7458 if (targ->die_mark != 0 || targ->comdat_type_p)
7459 continue;
7460
7461 slot = decl_table->find_slot_with_hash (targ,
7462 htab_hash_pointer (targ),
7463 INSERT);
7464
7465 if (*slot != HTAB_EMPTY_ENTRY)
7466 {
7467 /* TARG has already been copied, so we just need to
7468 modify the reference to point to the copy. */
7469 entry = *slot;
7470 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7471 }
7472 else
7473 {
7474 dw_die_ref parent = unit;
7475 dw_die_ref copy = clone_die (targ);
7476
7477 /* Record in DECL_TABLE that TARG has been copied.
7478 Need to do this now, before the recursive call,
7479 because DECL_TABLE may be expanded and SLOT
7480 would no longer be a valid pointer. */
7481 entry = XCNEW (struct decl_table_entry);
7482 entry->orig = targ;
7483 entry->copy = copy;
7484 *slot = entry;
7485
7486 /* If TARG is not a declaration DIE, we need to copy its
7487 children. */
7488 if (!is_declaration_die (targ))
7489 {
7490 FOR_EACH_CHILD (
7491 targ, c,
7492 add_child_die (copy,
7493 clone_tree_partial (c, decl_table)));
7494 }
7495
7496 /* Make sure the cloned tree is marked as part of the
7497 type unit. */
7498 mark_dies (copy);
7499
7500 /* If TARG has surrounding context, copy its ancestor tree
7501 into the new type unit. */
7502 if (targ->die_parent != NULL
7503 && !is_unit_die (targ->die_parent))
7504 parent = copy_ancestor_tree (unit, targ->die_parent,
7505 decl_table);
7506
7507 add_child_die (parent, copy);
7508 a->dw_attr_val.v.val_die_ref.die = copy;
7509
7510 /* Make sure the newly-copied DIE is walked. If it was
7511 installed in a previously-added context, it won't
7512 get visited otherwise. */
7513 if (parent != unit)
7514 {
7515 /* Find the highest point of the newly-added tree,
7516 mark each node along the way, and walk from there. */
7517 parent->die_mark = 1;
7518 while (parent->die_parent
7519 && parent->die_parent->die_mark == 0)
7520 {
7521 parent = parent->die_parent;
7522 parent->die_mark = 1;
7523 }
7524 copy_decls_walk (unit, parent, decl_table);
7525 }
7526 }
7527 }
7528 }
7529
7530 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7531 }
7532
7533 /* Copy declarations for "unworthy" types into the new comdat section.
7534 Incomplete types, modified types, and certain other types aren't broken
7535 out into comdat sections of their own, so they don't have a signature,
7536 and we need to copy the declaration into the same section so that we
7537 don't have an external reference. */
7538
7539 static void
7540 copy_decls_for_unworthy_types (dw_die_ref unit)
7541 {
7542 mark_dies (unit);
7543 decl_hash_type decl_table (10);
7544 copy_decls_walk (unit, unit, &decl_table);
7545 unmark_dies (unit);
7546 }
7547
7548 /* Traverse the DIE and add a sibling attribute if it may have the
7549 effect of speeding up access to siblings. To save some space,
7550 avoid generating sibling attributes for DIE's without children. */
7551
7552 static void
7553 add_sibling_attributes (dw_die_ref die)
7554 {
7555 dw_die_ref c;
7556
7557 if (! die->die_child)
7558 return;
7559
7560 if (die->die_parent && die != die->die_parent->die_child)
7561 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7562
7563 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7564 }
7565
7566 /* Output all location lists for the DIE and its children. */
7567
7568 static void
7569 output_location_lists (dw_die_ref die)
7570 {
7571 dw_die_ref c;
7572 dw_attr_ref a;
7573 unsigned ix;
7574
7575 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7576 if (AT_class (a) == dw_val_class_loc_list)
7577 output_loc_list (AT_loc_list (a));
7578
7579 FOR_EACH_CHILD (die, c, output_location_lists (c));
7580 }
7581
7582 /* We want to limit the number of external references, because they are
7583 larger than local references: a relocation takes multiple words, and
7584 even a sig8 reference is always eight bytes, whereas a local reference
7585 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7586 So if we encounter multiple external references to the same type DIE, we
7587 make a local typedef stub for it and redirect all references there.
7588
7589 This is the element of the hash table for keeping track of these
7590 references. */
7591
7592 struct external_ref
7593 {
7594 dw_die_ref type;
7595 dw_die_ref stub;
7596 unsigned n_refs;
7597 };
7598
7599 /* Hashtable helpers. */
7600
7601 struct external_ref_hasher : typed_free_remove <external_ref>
7602 {
7603 typedef external_ref value_type;
7604 typedef external_ref compare_type;
7605 static inline hashval_t hash (const value_type *);
7606 static inline bool equal (const value_type *, const compare_type *);
7607 };
7608
7609 inline hashval_t
7610 external_ref_hasher::hash (const value_type *r)
7611 {
7612 dw_die_ref die = r->type;
7613 hashval_t h = 0;
7614
7615 /* We can't use the address of the DIE for hashing, because
7616 that will make the order of the stub DIEs non-deterministic. */
7617 if (! die->comdat_type_p)
7618 /* We have a symbol; use it to compute a hash. */
7619 h = htab_hash_string (die->die_id.die_symbol);
7620 else
7621 {
7622 /* We have a type signature; use a subset of the bits as the hash.
7623 The 8-byte signature is at least as large as hashval_t. */
7624 comdat_type_node_ref type_node = die->die_id.die_type_node;
7625 memcpy (&h, type_node->signature, sizeof (h));
7626 }
7627 return h;
7628 }
7629
7630 inline bool
7631 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7632 {
7633 return r1->type == r2->type;
7634 }
7635
7636 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7637
7638 /* Return a pointer to the external_ref for references to DIE. */
7639
7640 static struct external_ref *
7641 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7642 {
7643 struct external_ref ref, *ref_p;
7644 external_ref **slot;
7645
7646 ref.type = die;
7647 slot = map->find_slot (&ref, INSERT);
7648 if (*slot != HTAB_EMPTY_ENTRY)
7649 return *slot;
7650
7651 ref_p = XCNEW (struct external_ref);
7652 ref_p->type = die;
7653 *slot = ref_p;
7654 return ref_p;
7655 }
7656
7657 /* Subroutine of optimize_external_refs, below.
7658
7659 If we see a type skeleton, record it as our stub. If we see external
7660 references, remember how many we've seen. */
7661
7662 static void
7663 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7664 {
7665 dw_die_ref c;
7666 dw_attr_ref a;
7667 unsigned ix;
7668 struct external_ref *ref_p;
7669
7670 if (is_type_die (die)
7671 && (c = get_AT_ref (die, DW_AT_signature)))
7672 {
7673 /* This is a local skeleton; use it for local references. */
7674 ref_p = lookup_external_ref (map, c);
7675 ref_p->stub = die;
7676 }
7677
7678 /* Scan the DIE references, and remember any that refer to DIEs from
7679 other CUs (i.e. those which are not marked). */
7680 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7681 if (AT_class (a) == dw_val_class_die_ref
7682 && (c = AT_ref (a))->die_mark == 0
7683 && is_type_die (c))
7684 {
7685 ref_p = lookup_external_ref (map, c);
7686 ref_p->n_refs++;
7687 }
7688
7689 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7690 }
7691
7692 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7693 points to an external_ref, DATA is the CU we're processing. If we don't
7694 already have a local stub, and we have multiple refs, build a stub. */
7695
7696 int
7697 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7698 {
7699 struct external_ref *ref_p = *slot;
7700
7701 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7702 {
7703 /* We have multiple references to this type, so build a small stub.
7704 Both of these forms are a bit dodgy from the perspective of the
7705 DWARF standard, since technically they should have names. */
7706 dw_die_ref cu = data;
7707 dw_die_ref type = ref_p->type;
7708 dw_die_ref stub = NULL;
7709
7710 if (type->comdat_type_p)
7711 {
7712 /* If we refer to this type via sig8, use AT_signature. */
7713 stub = new_die (type->die_tag, cu, NULL_TREE);
7714 add_AT_die_ref (stub, DW_AT_signature, type);
7715 }
7716 else
7717 {
7718 /* Otherwise, use a typedef with no name. */
7719 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7720 add_AT_die_ref (stub, DW_AT_type, type);
7721 }
7722
7723 stub->die_mark++;
7724 ref_p->stub = stub;
7725 }
7726 return 1;
7727 }
7728
7729 /* DIE is a unit; look through all the DIE references to see if there are
7730 any external references to types, and if so, create local stubs for
7731 them which will be applied in build_abbrev_table. This is useful because
7732 references to local DIEs are smaller. */
7733
7734 static external_ref_hash_type *
7735 optimize_external_refs (dw_die_ref die)
7736 {
7737 external_ref_hash_type *map = new external_ref_hash_type (10);
7738 optimize_external_refs_1 (die, map);
7739 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7740 return map;
7741 }
7742
7743 /* The format of each DIE (and its attribute value pairs) is encoded in an
7744 abbreviation table. This routine builds the abbreviation table and assigns
7745 a unique abbreviation id for each abbreviation entry. The children of each
7746 die are visited recursively. */
7747
7748 static void
7749 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7750 {
7751 unsigned long abbrev_id;
7752 unsigned int n_alloc;
7753 dw_die_ref c;
7754 dw_attr_ref a;
7755 unsigned ix;
7756
7757 /* Scan the DIE references, and replace any that refer to
7758 DIEs from other CUs (i.e. those which are not marked) with
7759 the local stubs we built in optimize_external_refs. */
7760 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7761 if (AT_class (a) == dw_val_class_die_ref
7762 && (c = AT_ref (a))->die_mark == 0)
7763 {
7764 struct external_ref *ref_p;
7765 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7766
7767 ref_p = lookup_external_ref (extern_map, c);
7768 if (ref_p->stub && ref_p->stub != die)
7769 change_AT_die_ref (a, ref_p->stub);
7770 else
7771 /* We aren't changing this reference, so mark it external. */
7772 set_AT_ref_external (a, 1);
7773 }
7774
7775 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7776 {
7777 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7778 dw_attr_ref die_a, abbrev_a;
7779 unsigned ix;
7780 bool ok = true;
7781
7782 if (abbrev->die_tag != die->die_tag)
7783 continue;
7784 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7785 continue;
7786
7787 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7788 continue;
7789
7790 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7791 {
7792 abbrev_a = &(*abbrev->die_attr)[ix];
7793 if ((abbrev_a->dw_attr != die_a->dw_attr)
7794 || (value_format (abbrev_a) != value_format (die_a)))
7795 {
7796 ok = false;
7797 break;
7798 }
7799 }
7800 if (ok)
7801 break;
7802 }
7803
7804 if (abbrev_id >= abbrev_die_table_in_use)
7805 {
7806 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7807 {
7808 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7809 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7810 n_alloc);
7811
7812 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7813 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7814 abbrev_die_table_allocated = n_alloc;
7815 }
7816
7817 ++abbrev_die_table_in_use;
7818 abbrev_die_table[abbrev_id] = die;
7819 }
7820
7821 die->die_abbrev = abbrev_id;
7822 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7823 }
7824 \f
7825 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7826
7827 static int
7828 constant_size (unsigned HOST_WIDE_INT value)
7829 {
7830 int log;
7831
7832 if (value == 0)
7833 log = 0;
7834 else
7835 log = floor_log2 (value);
7836
7837 log = log / 8;
7838 log = 1 << (floor_log2 (log) + 1);
7839
7840 return log;
7841 }
7842
7843 /* Return the size of a DIE as it is represented in the
7844 .debug_info section. */
7845
7846 static unsigned long
7847 size_of_die (dw_die_ref die)
7848 {
7849 unsigned long size = 0;
7850 dw_attr_ref a;
7851 unsigned ix;
7852 enum dwarf_form form;
7853
7854 size += size_of_uleb128 (die->die_abbrev);
7855 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7856 {
7857 switch (AT_class (a))
7858 {
7859 case dw_val_class_addr:
7860 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7861 {
7862 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7863 size += size_of_uleb128 (AT_index (a));
7864 }
7865 else
7866 size += DWARF2_ADDR_SIZE;
7867 break;
7868 case dw_val_class_offset:
7869 size += DWARF_OFFSET_SIZE;
7870 break;
7871 case dw_val_class_loc:
7872 {
7873 unsigned long lsize = size_of_locs (AT_loc (a));
7874
7875 /* Block length. */
7876 if (dwarf_version >= 4)
7877 size += size_of_uleb128 (lsize);
7878 else
7879 size += constant_size (lsize);
7880 size += lsize;
7881 }
7882 break;
7883 case dw_val_class_loc_list:
7884 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7885 {
7886 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7887 size += size_of_uleb128 (AT_index (a));
7888 }
7889 else
7890 size += DWARF_OFFSET_SIZE;
7891 break;
7892 case dw_val_class_range_list:
7893 size += DWARF_OFFSET_SIZE;
7894 break;
7895 case dw_val_class_const:
7896 size += size_of_sleb128 (AT_int (a));
7897 break;
7898 case dw_val_class_unsigned_const:
7899 {
7900 int csize = constant_size (AT_unsigned (a));
7901 if (dwarf_version == 3
7902 && a->dw_attr == DW_AT_data_member_location
7903 && csize >= 4)
7904 size += size_of_uleb128 (AT_unsigned (a));
7905 else
7906 size += csize;
7907 }
7908 break;
7909 case dw_val_class_const_double:
7910 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7911 if (HOST_BITS_PER_WIDE_INT >= 64)
7912 size++; /* block */
7913 break;
7914 case dw_val_class_wide_int:
7915 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7916 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7917 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7918 > 64)
7919 size++; /* block */
7920 break;
7921 case dw_val_class_vec:
7922 size += constant_size (a->dw_attr_val.v.val_vec.length
7923 * a->dw_attr_val.v.val_vec.elt_size)
7924 + a->dw_attr_val.v.val_vec.length
7925 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7926 break;
7927 case dw_val_class_flag:
7928 if (dwarf_version >= 4)
7929 /* Currently all add_AT_flag calls pass in 1 as last argument,
7930 so DW_FORM_flag_present can be used. If that ever changes,
7931 we'll need to use DW_FORM_flag and have some optimization
7932 in build_abbrev_table that will change those to
7933 DW_FORM_flag_present if it is set to 1 in all DIEs using
7934 the same abbrev entry. */
7935 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7936 else
7937 size += 1;
7938 break;
7939 case dw_val_class_die_ref:
7940 if (AT_ref_external (a))
7941 {
7942 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7943 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7944 is sized by target address length, whereas in DWARF3
7945 it's always sized as an offset. */
7946 if (use_debug_types)
7947 size += DWARF_TYPE_SIGNATURE_SIZE;
7948 else if (dwarf_version == 2)
7949 size += DWARF2_ADDR_SIZE;
7950 else
7951 size += DWARF_OFFSET_SIZE;
7952 }
7953 else
7954 size += DWARF_OFFSET_SIZE;
7955 break;
7956 case dw_val_class_fde_ref:
7957 size += DWARF_OFFSET_SIZE;
7958 break;
7959 case dw_val_class_lbl_id:
7960 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7961 {
7962 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7963 size += size_of_uleb128 (AT_index (a));
7964 }
7965 else
7966 size += DWARF2_ADDR_SIZE;
7967 break;
7968 case dw_val_class_lineptr:
7969 case dw_val_class_macptr:
7970 size += DWARF_OFFSET_SIZE;
7971 break;
7972 case dw_val_class_str:
7973 form = AT_string_form (a);
7974 if (form == DW_FORM_strp)
7975 size += DWARF_OFFSET_SIZE;
7976 else if (form == DW_FORM_GNU_str_index)
7977 size += size_of_uleb128 (AT_index (a));
7978 else
7979 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7980 break;
7981 case dw_val_class_file:
7982 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7983 break;
7984 case dw_val_class_data8:
7985 size += 8;
7986 break;
7987 case dw_val_class_vms_delta:
7988 size += DWARF_OFFSET_SIZE;
7989 break;
7990 case dw_val_class_high_pc:
7991 size += DWARF2_ADDR_SIZE;
7992 break;
7993 default:
7994 gcc_unreachable ();
7995 }
7996 }
7997
7998 return size;
7999 }
8000
8001 /* Size the debugging information associated with a given DIE. Visits the
8002 DIE's children recursively. Updates the global variable next_die_offset, on
8003 each time through. Uses the current value of next_die_offset to update the
8004 die_offset field in each DIE. */
8005
8006 static void
8007 calc_die_sizes (dw_die_ref die)
8008 {
8009 dw_die_ref c;
8010
8011 gcc_assert (die->die_offset == 0
8012 || (unsigned long int) die->die_offset == next_die_offset);
8013 die->die_offset = next_die_offset;
8014 next_die_offset += size_of_die (die);
8015
8016 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8017
8018 if (die->die_child != NULL)
8019 /* Count the null byte used to terminate sibling lists. */
8020 next_die_offset += 1;
8021 }
8022
8023 /* Size just the base type children at the start of the CU.
8024 This is needed because build_abbrev needs to size locs
8025 and sizing of type based stack ops needs to know die_offset
8026 values for the base types. */
8027
8028 static void
8029 calc_base_type_die_sizes (void)
8030 {
8031 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8032 unsigned int i;
8033 dw_die_ref base_type;
8034 #if ENABLE_ASSERT_CHECKING
8035 dw_die_ref prev = comp_unit_die ()->die_child;
8036 #endif
8037
8038 die_offset += size_of_die (comp_unit_die ());
8039 for (i = 0; base_types.iterate (i, &base_type); i++)
8040 {
8041 #if ENABLE_ASSERT_CHECKING
8042 gcc_assert (base_type->die_offset == 0
8043 && prev->die_sib == base_type
8044 && base_type->die_child == NULL
8045 && base_type->die_abbrev);
8046 prev = base_type;
8047 #endif
8048 base_type->die_offset = die_offset;
8049 die_offset += size_of_die (base_type);
8050 }
8051 }
8052
8053 /* Set the marks for a die and its children. We do this so
8054 that we know whether or not a reference needs to use FORM_ref_addr; only
8055 DIEs in the same CU will be marked. We used to clear out the offset
8056 and use that as the flag, but ran into ordering problems. */
8057
8058 static void
8059 mark_dies (dw_die_ref die)
8060 {
8061 dw_die_ref c;
8062
8063 gcc_assert (!die->die_mark);
8064
8065 die->die_mark = 1;
8066 FOR_EACH_CHILD (die, c, mark_dies (c));
8067 }
8068
8069 /* Clear the marks for a die and its children. */
8070
8071 static void
8072 unmark_dies (dw_die_ref die)
8073 {
8074 dw_die_ref c;
8075
8076 if (! use_debug_types)
8077 gcc_assert (die->die_mark);
8078
8079 die->die_mark = 0;
8080 FOR_EACH_CHILD (die, c, unmark_dies (c));
8081 }
8082
8083 /* Clear the marks for a die, its children and referred dies. */
8084
8085 static void
8086 unmark_all_dies (dw_die_ref die)
8087 {
8088 dw_die_ref c;
8089 dw_attr_ref a;
8090 unsigned ix;
8091
8092 if (!die->die_mark)
8093 return;
8094 die->die_mark = 0;
8095
8096 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8097
8098 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8099 if (AT_class (a) == dw_val_class_die_ref)
8100 unmark_all_dies (AT_ref (a));
8101 }
8102
8103 /* Calculate if the entry should appear in the final output file. It may be
8104 from a pruned a type. */
8105
8106 static bool
8107 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8108 {
8109 /* By limiting gnu pubnames to definitions only, gold can generate a
8110 gdb index without entries for declarations, which don't include
8111 enough information to be useful. */
8112 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8113 return false;
8114
8115 if (table == pubname_table)
8116 {
8117 /* Enumerator names are part of the pubname table, but the
8118 parent DW_TAG_enumeration_type die may have been pruned.
8119 Don't output them if that is the case. */
8120 if (p->die->die_tag == DW_TAG_enumerator &&
8121 (p->die->die_parent == NULL
8122 || !p->die->die_parent->die_perennial_p))
8123 return false;
8124
8125 /* Everything else in the pubname table is included. */
8126 return true;
8127 }
8128
8129 /* The pubtypes table shouldn't include types that have been
8130 pruned. */
8131 return (p->die->die_offset != 0
8132 || !flag_eliminate_unused_debug_types);
8133 }
8134
8135 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8136 generated for the compilation unit. */
8137
8138 static unsigned long
8139 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8140 {
8141 unsigned long size;
8142 unsigned i;
8143 pubname_ref p;
8144 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8145
8146 size = DWARF_PUBNAMES_HEADER_SIZE;
8147 FOR_EACH_VEC_ELT (*names, i, p)
8148 if (include_pubname_in_output (names, p))
8149 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8150
8151 size += DWARF_OFFSET_SIZE;
8152 return size;
8153 }
8154
8155 /* Return the size of the information in the .debug_aranges section. */
8156
8157 static unsigned long
8158 size_of_aranges (void)
8159 {
8160 unsigned long size;
8161
8162 size = DWARF_ARANGES_HEADER_SIZE;
8163
8164 /* Count the address/length pair for this compilation unit. */
8165 if (text_section_used)
8166 size += 2 * DWARF2_ADDR_SIZE;
8167 if (cold_text_section_used)
8168 size += 2 * DWARF2_ADDR_SIZE;
8169 if (have_multiple_function_sections)
8170 {
8171 unsigned fde_idx;
8172 dw_fde_ref fde;
8173
8174 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8175 {
8176 if (DECL_IGNORED_P (fde->decl))
8177 continue;
8178 if (!fde->in_std_section)
8179 size += 2 * DWARF2_ADDR_SIZE;
8180 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8181 size += 2 * DWARF2_ADDR_SIZE;
8182 }
8183 }
8184
8185 /* Count the two zero words used to terminated the address range table. */
8186 size += 2 * DWARF2_ADDR_SIZE;
8187 return size;
8188 }
8189 \f
8190 /* Select the encoding of an attribute value. */
8191
8192 static enum dwarf_form
8193 value_format (dw_attr_ref a)
8194 {
8195 switch (AT_class (a))
8196 {
8197 case dw_val_class_addr:
8198 /* Only very few attributes allow DW_FORM_addr. */
8199 switch (a->dw_attr)
8200 {
8201 case DW_AT_low_pc:
8202 case DW_AT_high_pc:
8203 case DW_AT_entry_pc:
8204 case DW_AT_trampoline:
8205 return (AT_index (a) == NOT_INDEXED
8206 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8207 default:
8208 break;
8209 }
8210 switch (DWARF2_ADDR_SIZE)
8211 {
8212 case 1:
8213 return DW_FORM_data1;
8214 case 2:
8215 return DW_FORM_data2;
8216 case 4:
8217 return DW_FORM_data4;
8218 case 8:
8219 return DW_FORM_data8;
8220 default:
8221 gcc_unreachable ();
8222 }
8223 case dw_val_class_range_list:
8224 case dw_val_class_loc_list:
8225 if (dwarf_version >= 4)
8226 return DW_FORM_sec_offset;
8227 /* FALLTHRU */
8228 case dw_val_class_vms_delta:
8229 case dw_val_class_offset:
8230 switch (DWARF_OFFSET_SIZE)
8231 {
8232 case 4:
8233 return DW_FORM_data4;
8234 case 8:
8235 return DW_FORM_data8;
8236 default:
8237 gcc_unreachable ();
8238 }
8239 case dw_val_class_loc:
8240 if (dwarf_version >= 4)
8241 return DW_FORM_exprloc;
8242 switch (constant_size (size_of_locs (AT_loc (a))))
8243 {
8244 case 1:
8245 return DW_FORM_block1;
8246 case 2:
8247 return DW_FORM_block2;
8248 case 4:
8249 return DW_FORM_block4;
8250 default:
8251 gcc_unreachable ();
8252 }
8253 case dw_val_class_const:
8254 return DW_FORM_sdata;
8255 case dw_val_class_unsigned_const:
8256 switch (constant_size (AT_unsigned (a)))
8257 {
8258 case 1:
8259 return DW_FORM_data1;
8260 case 2:
8261 return DW_FORM_data2;
8262 case 4:
8263 /* In DWARF3 DW_AT_data_member_location with
8264 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8265 constant, so we need to use DW_FORM_udata if we need
8266 a large constant. */
8267 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8268 return DW_FORM_udata;
8269 return DW_FORM_data4;
8270 case 8:
8271 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8272 return DW_FORM_udata;
8273 return DW_FORM_data8;
8274 default:
8275 gcc_unreachable ();
8276 }
8277 case dw_val_class_const_double:
8278 switch (HOST_BITS_PER_WIDE_INT)
8279 {
8280 case 8:
8281 return DW_FORM_data2;
8282 case 16:
8283 return DW_FORM_data4;
8284 case 32:
8285 return DW_FORM_data8;
8286 case 64:
8287 default:
8288 return DW_FORM_block1;
8289 }
8290 case dw_val_class_wide_int:
8291 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8292 {
8293 case 8:
8294 return DW_FORM_data1;
8295 case 16:
8296 return DW_FORM_data2;
8297 case 32:
8298 return DW_FORM_data4;
8299 case 64:
8300 return DW_FORM_data8;
8301 default:
8302 return DW_FORM_block1;
8303 }
8304 case dw_val_class_vec:
8305 switch (constant_size (a->dw_attr_val.v.val_vec.length
8306 * a->dw_attr_val.v.val_vec.elt_size))
8307 {
8308 case 1:
8309 return DW_FORM_block1;
8310 case 2:
8311 return DW_FORM_block2;
8312 case 4:
8313 return DW_FORM_block4;
8314 default:
8315 gcc_unreachable ();
8316 }
8317 case dw_val_class_flag:
8318 if (dwarf_version >= 4)
8319 {
8320 /* Currently all add_AT_flag calls pass in 1 as last argument,
8321 so DW_FORM_flag_present can be used. If that ever changes,
8322 we'll need to use DW_FORM_flag and have some optimization
8323 in build_abbrev_table that will change those to
8324 DW_FORM_flag_present if it is set to 1 in all DIEs using
8325 the same abbrev entry. */
8326 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8327 return DW_FORM_flag_present;
8328 }
8329 return DW_FORM_flag;
8330 case dw_val_class_die_ref:
8331 if (AT_ref_external (a))
8332 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8333 else
8334 return DW_FORM_ref;
8335 case dw_val_class_fde_ref:
8336 return DW_FORM_data;
8337 case dw_val_class_lbl_id:
8338 return (AT_index (a) == NOT_INDEXED
8339 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8340 case dw_val_class_lineptr:
8341 case dw_val_class_macptr:
8342 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8343 case dw_val_class_str:
8344 return AT_string_form (a);
8345 case dw_val_class_file:
8346 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8347 {
8348 case 1:
8349 return DW_FORM_data1;
8350 case 2:
8351 return DW_FORM_data2;
8352 case 4:
8353 return DW_FORM_data4;
8354 default:
8355 gcc_unreachable ();
8356 }
8357
8358 case dw_val_class_data8:
8359 return DW_FORM_data8;
8360
8361 case dw_val_class_high_pc:
8362 switch (DWARF2_ADDR_SIZE)
8363 {
8364 case 1:
8365 return DW_FORM_data1;
8366 case 2:
8367 return DW_FORM_data2;
8368 case 4:
8369 return DW_FORM_data4;
8370 case 8:
8371 return DW_FORM_data8;
8372 default:
8373 gcc_unreachable ();
8374 }
8375
8376 default:
8377 gcc_unreachable ();
8378 }
8379 }
8380
8381 /* Output the encoding of an attribute value. */
8382
8383 static void
8384 output_value_format (dw_attr_ref a)
8385 {
8386 enum dwarf_form form = value_format (a);
8387
8388 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8389 }
8390
8391 /* Given a die and id, produce the appropriate abbreviations. */
8392
8393 static void
8394 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8395 {
8396 unsigned ix;
8397 dw_attr_ref a_attr;
8398
8399 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8400 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8401 dwarf_tag_name (abbrev->die_tag));
8402
8403 if (abbrev->die_child != NULL)
8404 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8405 else
8406 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8407
8408 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8409 {
8410 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8411 dwarf_attr_name (a_attr->dw_attr));
8412 output_value_format (a_attr);
8413 }
8414
8415 dw2_asm_output_data (1, 0, NULL);
8416 dw2_asm_output_data (1, 0, NULL);
8417 }
8418
8419
8420 /* Output the .debug_abbrev section which defines the DIE abbreviation
8421 table. */
8422
8423 static void
8424 output_abbrev_section (void)
8425 {
8426 unsigned long abbrev_id;
8427
8428 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8429 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8430
8431 /* Terminate the table. */
8432 dw2_asm_output_data (1, 0, NULL);
8433 }
8434
8435 /* Output a symbol we can use to refer to this DIE from another CU. */
8436
8437 static inline void
8438 output_die_symbol (dw_die_ref die)
8439 {
8440 const char *sym = die->die_id.die_symbol;
8441
8442 gcc_assert (!die->comdat_type_p);
8443
8444 if (sym == 0)
8445 return;
8446
8447 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8448 /* We make these global, not weak; if the target doesn't support
8449 .linkonce, it doesn't support combining the sections, so debugging
8450 will break. */
8451 targetm.asm_out.globalize_label (asm_out_file, sym);
8452
8453 ASM_OUTPUT_LABEL (asm_out_file, sym);
8454 }
8455
8456 /* Return a new location list, given the begin and end range, and the
8457 expression. */
8458
8459 static inline dw_loc_list_ref
8460 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8461 const char *section)
8462 {
8463 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8464
8465 retlist->begin = begin;
8466 retlist->begin_entry = NULL;
8467 retlist->end = end;
8468 retlist->expr = expr;
8469 retlist->section = section;
8470
8471 return retlist;
8472 }
8473
8474 /* Generate a new internal symbol for this location list node, if it
8475 hasn't got one yet. */
8476
8477 static inline void
8478 gen_llsym (dw_loc_list_ref list)
8479 {
8480 gcc_assert (!list->ll_symbol);
8481 list->ll_symbol = gen_internal_sym ("LLST");
8482 }
8483
8484 /* Output the location list given to us. */
8485
8486 static void
8487 output_loc_list (dw_loc_list_ref list_head)
8488 {
8489 dw_loc_list_ref curr = list_head;
8490
8491 if (list_head->emitted)
8492 return;
8493 list_head->emitted = true;
8494
8495 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8496
8497 /* Walk the location list, and output each range + expression. */
8498 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8499 {
8500 unsigned long size;
8501 /* Don't output an entry that starts and ends at the same address. */
8502 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8503 continue;
8504 size = size_of_locs (curr->expr);
8505 /* If the expression is too large, drop it on the floor. We could
8506 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8507 in the expression, but >= 64KB expressions for a single value
8508 in a single range are unlikely very useful. */
8509 if (size > 0xffff)
8510 continue;
8511 if (dwarf_split_debug_info)
8512 {
8513 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8514 "Location list start/length entry (%s)",
8515 list_head->ll_symbol);
8516 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8517 "Location list range start index (%s)",
8518 curr->begin);
8519 /* The length field is 4 bytes. If we ever need to support
8520 an 8-byte length, we can add a new DW_LLE code or fall back
8521 to DW_LLE_GNU_start_end_entry. */
8522 dw2_asm_output_delta (4, curr->end, curr->begin,
8523 "Location list range length (%s)",
8524 list_head->ll_symbol);
8525 }
8526 else if (!have_multiple_function_sections)
8527 {
8528 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8529 "Location list begin address (%s)",
8530 list_head->ll_symbol);
8531 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8532 "Location list end address (%s)",
8533 list_head->ll_symbol);
8534 }
8535 else
8536 {
8537 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8538 "Location list begin address (%s)",
8539 list_head->ll_symbol);
8540 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8541 "Location list end address (%s)",
8542 list_head->ll_symbol);
8543 }
8544
8545 /* Output the block length for this list of location operations. */
8546 gcc_assert (size <= 0xffff);
8547 dw2_asm_output_data (2, size, "%s", "Location expression size");
8548
8549 output_loc_sequence (curr->expr, -1);
8550 }
8551
8552 if (dwarf_split_debug_info)
8553 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8554 "Location list terminator (%s)",
8555 list_head->ll_symbol);
8556 else
8557 {
8558 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8559 "Location list terminator begin (%s)",
8560 list_head->ll_symbol);
8561 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8562 "Location list terminator end (%s)",
8563 list_head->ll_symbol);
8564 }
8565 }
8566
8567 /* Output a range_list offset into the debug_range section. Emit a
8568 relocated reference if val_entry is NULL, otherwise, emit an
8569 indirect reference. */
8570
8571 static void
8572 output_range_list_offset (dw_attr_ref a)
8573 {
8574 const char *name = dwarf_attr_name (a->dw_attr);
8575
8576 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8577 {
8578 char *p = strchr (ranges_section_label, '\0');
8579 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8580 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8581 debug_ranges_section, "%s", name);
8582 *p = '\0';
8583 }
8584 else
8585 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8586 "%s (offset from %s)", name, ranges_section_label);
8587 }
8588
8589 /* Output the offset into the debug_loc section. */
8590
8591 static void
8592 output_loc_list_offset (dw_attr_ref a)
8593 {
8594 char *sym = AT_loc_list (a)->ll_symbol;
8595
8596 gcc_assert (sym);
8597 if (dwarf_split_debug_info)
8598 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8599 "%s", dwarf_attr_name (a->dw_attr));
8600 else
8601 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8602 "%s", dwarf_attr_name (a->dw_attr));
8603 }
8604
8605 /* Output an attribute's index or value appropriately. */
8606
8607 static void
8608 output_attr_index_or_value (dw_attr_ref a)
8609 {
8610 const char *name = dwarf_attr_name (a->dw_attr);
8611
8612 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8613 {
8614 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8615 return;
8616 }
8617 switch (AT_class (a))
8618 {
8619 case dw_val_class_addr:
8620 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8621 break;
8622 case dw_val_class_high_pc:
8623 case dw_val_class_lbl_id:
8624 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8625 break;
8626 case dw_val_class_loc_list:
8627 output_loc_list_offset (a);
8628 break;
8629 default:
8630 gcc_unreachable ();
8631 }
8632 }
8633
8634 /* Output a type signature. */
8635
8636 static inline void
8637 output_signature (const char *sig, const char *name)
8638 {
8639 int i;
8640
8641 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8642 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8643 }
8644
8645 /* Output the DIE and its attributes. Called recursively to generate
8646 the definitions of each child DIE. */
8647
8648 static void
8649 output_die (dw_die_ref die)
8650 {
8651 dw_attr_ref a;
8652 dw_die_ref c;
8653 unsigned long size;
8654 unsigned ix;
8655
8656 /* If someone in another CU might refer to us, set up a symbol for
8657 them to point to. */
8658 if (! die->comdat_type_p && die->die_id.die_symbol)
8659 output_die_symbol (die);
8660
8661 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8662 (unsigned long)die->die_offset,
8663 dwarf_tag_name (die->die_tag));
8664
8665 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8666 {
8667 const char *name = dwarf_attr_name (a->dw_attr);
8668
8669 switch (AT_class (a))
8670 {
8671 case dw_val_class_addr:
8672 output_attr_index_or_value (a);
8673 break;
8674
8675 case dw_val_class_offset:
8676 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8677 "%s", name);
8678 break;
8679
8680 case dw_val_class_range_list:
8681 output_range_list_offset (a);
8682 break;
8683
8684 case dw_val_class_loc:
8685 size = size_of_locs (AT_loc (a));
8686
8687 /* Output the block length for this list of location operations. */
8688 if (dwarf_version >= 4)
8689 dw2_asm_output_data_uleb128 (size, "%s", name);
8690 else
8691 dw2_asm_output_data (constant_size (size), size, "%s", name);
8692
8693 output_loc_sequence (AT_loc (a), -1);
8694 break;
8695
8696 case dw_val_class_const:
8697 /* ??? It would be slightly more efficient to use a scheme like is
8698 used for unsigned constants below, but gdb 4.x does not sign
8699 extend. Gdb 5.x does sign extend. */
8700 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8701 break;
8702
8703 case dw_val_class_unsigned_const:
8704 {
8705 int csize = constant_size (AT_unsigned (a));
8706 if (dwarf_version == 3
8707 && a->dw_attr == DW_AT_data_member_location
8708 && csize >= 4)
8709 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8710 else
8711 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8712 }
8713 break;
8714
8715 case dw_val_class_const_double:
8716 {
8717 unsigned HOST_WIDE_INT first, second;
8718
8719 if (HOST_BITS_PER_WIDE_INT >= 64)
8720 dw2_asm_output_data (1,
8721 HOST_BITS_PER_DOUBLE_INT
8722 / HOST_BITS_PER_CHAR,
8723 NULL);
8724
8725 if (WORDS_BIG_ENDIAN)
8726 {
8727 first = a->dw_attr_val.v.val_double.high;
8728 second = a->dw_attr_val.v.val_double.low;
8729 }
8730 else
8731 {
8732 first = a->dw_attr_val.v.val_double.low;
8733 second = a->dw_attr_val.v.val_double.high;
8734 }
8735
8736 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8737 first, "%s", name);
8738 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8739 second, NULL);
8740 }
8741 break;
8742
8743 case dw_val_class_wide_int:
8744 {
8745 int i;
8746 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8747 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8748 if (len * HOST_BITS_PER_WIDE_INT > 64)
8749 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8750 NULL);
8751
8752 if (WORDS_BIG_ENDIAN)
8753 for (i = len - 1; i >= 0; --i)
8754 {
8755 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8756 name);
8757 name = NULL;
8758 }
8759 else
8760 for (i = 0; i < len; ++i)
8761 {
8762 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8763 name);
8764 name = NULL;
8765 }
8766 }
8767 break;
8768
8769 case dw_val_class_vec:
8770 {
8771 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8772 unsigned int len = a->dw_attr_val.v.val_vec.length;
8773 unsigned int i;
8774 unsigned char *p;
8775
8776 dw2_asm_output_data (constant_size (len * elt_size),
8777 len * elt_size, "%s", name);
8778 if (elt_size > sizeof (HOST_WIDE_INT))
8779 {
8780 elt_size /= 2;
8781 len *= 2;
8782 }
8783 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8784 i < len;
8785 i++, p += elt_size)
8786 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8787 "fp or vector constant word %u", i);
8788 break;
8789 }
8790
8791 case dw_val_class_flag:
8792 if (dwarf_version >= 4)
8793 {
8794 /* Currently all add_AT_flag calls pass in 1 as last argument,
8795 so DW_FORM_flag_present can be used. If that ever changes,
8796 we'll need to use DW_FORM_flag and have some optimization
8797 in build_abbrev_table that will change those to
8798 DW_FORM_flag_present if it is set to 1 in all DIEs using
8799 the same abbrev entry. */
8800 gcc_assert (AT_flag (a) == 1);
8801 if (flag_debug_asm)
8802 fprintf (asm_out_file, "\t\t\t%s %s\n",
8803 ASM_COMMENT_START, name);
8804 break;
8805 }
8806 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8807 break;
8808
8809 case dw_val_class_loc_list:
8810 output_attr_index_or_value (a);
8811 break;
8812
8813 case dw_val_class_die_ref:
8814 if (AT_ref_external (a))
8815 {
8816 if (AT_ref (a)->comdat_type_p)
8817 {
8818 comdat_type_node_ref type_node =
8819 AT_ref (a)->die_id.die_type_node;
8820
8821 gcc_assert (type_node);
8822 output_signature (type_node->signature, name);
8823 }
8824 else
8825 {
8826 const char *sym = AT_ref (a)->die_id.die_symbol;
8827 int size;
8828
8829 gcc_assert (sym);
8830 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8831 length, whereas in DWARF3 it's always sized as an
8832 offset. */
8833 if (dwarf_version == 2)
8834 size = DWARF2_ADDR_SIZE;
8835 else
8836 size = DWARF_OFFSET_SIZE;
8837 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8838 name);
8839 }
8840 }
8841 else
8842 {
8843 gcc_assert (AT_ref (a)->die_offset);
8844 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8845 "%s", name);
8846 }
8847 break;
8848
8849 case dw_val_class_fde_ref:
8850 {
8851 char l1[20];
8852
8853 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8854 a->dw_attr_val.v.val_fde_index * 2);
8855 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8856 "%s", name);
8857 }
8858 break;
8859
8860 case dw_val_class_vms_delta:
8861 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8862 AT_vms_delta2 (a), AT_vms_delta1 (a),
8863 "%s", name);
8864 break;
8865
8866 case dw_val_class_lbl_id:
8867 output_attr_index_or_value (a);
8868 break;
8869
8870 case dw_val_class_lineptr:
8871 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8872 debug_line_section, "%s", name);
8873 break;
8874
8875 case dw_val_class_macptr:
8876 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8877 debug_macinfo_section, "%s", name);
8878 break;
8879
8880 case dw_val_class_str:
8881 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8882 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8883 a->dw_attr_val.v.val_str->label,
8884 debug_str_section,
8885 "%s: \"%s\"", name, AT_string (a));
8886 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8887 dw2_asm_output_data_uleb128 (AT_index (a),
8888 "%s: \"%s\"", name, AT_string (a));
8889 else
8890 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8891 break;
8892
8893 case dw_val_class_file:
8894 {
8895 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8896
8897 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8898 a->dw_attr_val.v.val_file->filename);
8899 break;
8900 }
8901
8902 case dw_val_class_data8:
8903 {
8904 int i;
8905
8906 for (i = 0; i < 8; i++)
8907 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8908 i == 0 ? "%s" : NULL, name);
8909 break;
8910 }
8911
8912 case dw_val_class_high_pc:
8913 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8914 get_AT_low_pc (die), "DW_AT_high_pc");
8915 break;
8916
8917 default:
8918 gcc_unreachable ();
8919 }
8920 }
8921
8922 FOR_EACH_CHILD (die, c, output_die (c));
8923
8924 /* Add null byte to terminate sibling list. */
8925 if (die->die_child != NULL)
8926 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8927 (unsigned long) die->die_offset);
8928 }
8929
8930 /* Output the compilation unit that appears at the beginning of the
8931 .debug_info section, and precedes the DIE descriptions. */
8932
8933 static void
8934 output_compilation_unit_header (void)
8935 {
8936 int ver = dwarf_version;
8937
8938 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8939 dw2_asm_output_data (4, 0xffffffff,
8940 "Initial length escape value indicating 64-bit DWARF extension");
8941 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8942 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8943 "Length of Compilation Unit Info");
8944 dw2_asm_output_data (2, ver, "DWARF version number");
8945 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8946 debug_abbrev_section,
8947 "Offset Into Abbrev. Section");
8948 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8949 }
8950
8951 /* Output the compilation unit DIE and its children. */
8952
8953 static void
8954 output_comp_unit (dw_die_ref die, int output_if_empty)
8955 {
8956 const char *secname, *oldsym;
8957 char *tmp;
8958
8959 /* Unless we are outputting main CU, we may throw away empty ones. */
8960 if (!output_if_empty && die->die_child == NULL)
8961 return;
8962
8963 /* Even if there are no children of this DIE, we must output the information
8964 about the compilation unit. Otherwise, on an empty translation unit, we
8965 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8966 will then complain when examining the file. First mark all the DIEs in
8967 this CU so we know which get local refs. */
8968 mark_dies (die);
8969
8970 external_ref_hash_type *extern_map = optimize_external_refs (die);
8971
8972 build_abbrev_table (die, extern_map);
8973
8974 delete extern_map;
8975
8976 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8977 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8978 calc_die_sizes (die);
8979
8980 oldsym = die->die_id.die_symbol;
8981 if (oldsym)
8982 {
8983 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8984
8985 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8986 secname = tmp;
8987 die->die_id.die_symbol = NULL;
8988 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8989 }
8990 else
8991 {
8992 switch_to_section (debug_info_section);
8993 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8994 info_section_emitted = true;
8995 }
8996
8997 /* Output debugging information. */
8998 output_compilation_unit_header ();
8999 output_die (die);
9000
9001 /* Leave the marks on the main CU, so we can check them in
9002 output_pubnames. */
9003 if (oldsym)
9004 {
9005 unmark_dies (die);
9006 die->die_id.die_symbol = oldsym;
9007 }
9008 }
9009
9010 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9011 and .debug_pubtypes. This is configured per-target, but can be
9012 overridden by the -gpubnames or -gno-pubnames options. */
9013
9014 static inline bool
9015 want_pubnames (void)
9016 {
9017 if (debug_info_level <= DINFO_LEVEL_TERSE)
9018 return false;
9019 if (debug_generate_pub_sections != -1)
9020 return debug_generate_pub_sections;
9021 return targetm.want_debug_pub_sections;
9022 }
9023
9024 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9025
9026 static void
9027 add_AT_pubnames (dw_die_ref die)
9028 {
9029 if (want_pubnames ())
9030 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9031 }
9032
9033 /* Add a string attribute value to a skeleton DIE. */
9034
9035 static inline void
9036 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9037 const char *str)
9038 {
9039 dw_attr_node attr;
9040 struct indirect_string_node *node;
9041
9042 if (! skeleton_debug_str_hash)
9043 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
9044 debug_str_eq, NULL);
9045
9046 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9047 find_string_form (node);
9048 if (node->form == DW_FORM_GNU_str_index)
9049 node->form = DW_FORM_strp;
9050
9051 attr.dw_attr = attr_kind;
9052 attr.dw_attr_val.val_class = dw_val_class_str;
9053 attr.dw_attr_val.val_entry = NULL;
9054 attr.dw_attr_val.v.val_str = node;
9055 add_dwarf_attr (die, &attr);
9056 }
9057
9058 /* Helper function to generate top-level dies for skeleton debug_info and
9059 debug_types. */
9060
9061 static void
9062 add_top_level_skeleton_die_attrs (dw_die_ref die)
9063 {
9064 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9065 const char *comp_dir = comp_dir_string ();
9066
9067 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9068 if (comp_dir != NULL)
9069 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9070 add_AT_pubnames (die);
9071 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9072 }
9073
9074 /* Output skeleton debug sections that point to the dwo file. */
9075
9076 static void
9077 output_skeleton_debug_sections (dw_die_ref comp_unit)
9078 {
9079 /* These attributes will be found in the full debug_info section. */
9080 remove_AT (comp_unit, DW_AT_producer);
9081 remove_AT (comp_unit, DW_AT_language);
9082
9083 switch_to_section (debug_skeleton_info_section);
9084 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9085
9086 /* Produce the skeleton compilation-unit header. This one differs enough from
9087 a normal CU header that it's better not to call output_compilation_unit
9088 header. */
9089 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9090 dw2_asm_output_data (4, 0xffffffff,
9091 "Initial length escape value indicating 64-bit DWARF extension");
9092
9093 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9094 DWARF_COMPILE_UNIT_HEADER_SIZE
9095 - DWARF_INITIAL_LENGTH_SIZE
9096 + size_of_die (comp_unit),
9097 "Length of Compilation Unit Info");
9098 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9099 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9100 debug_abbrev_section,
9101 "Offset Into Abbrev. Section");
9102 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9103
9104 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9105 output_die (comp_unit);
9106
9107 /* Build the skeleton debug_abbrev section. */
9108 switch_to_section (debug_skeleton_abbrev_section);
9109 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9110
9111 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9112
9113 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9114 }
9115
9116 /* Output a comdat type unit DIE and its children. */
9117
9118 static void
9119 output_comdat_type_unit (comdat_type_node *node)
9120 {
9121 const char *secname;
9122 char *tmp;
9123 int i;
9124 #if defined (OBJECT_FORMAT_ELF)
9125 tree comdat_key;
9126 #endif
9127
9128 /* First mark all the DIEs in this CU so we know which get local refs. */
9129 mark_dies (node->root_die);
9130
9131 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9132
9133 build_abbrev_table (node->root_die, extern_map);
9134
9135 delete extern_map;
9136 extern_map = NULL;
9137
9138 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9139 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9140 calc_die_sizes (node->root_die);
9141
9142 #if defined (OBJECT_FORMAT_ELF)
9143 if (!dwarf_split_debug_info)
9144 secname = ".debug_types";
9145 else
9146 secname = ".debug_types.dwo";
9147
9148 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9149 sprintf (tmp, "wt.");
9150 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9151 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9152 comdat_key = get_identifier (tmp);
9153 targetm.asm_out.named_section (secname,
9154 SECTION_DEBUG | SECTION_LINKONCE,
9155 comdat_key);
9156 #else
9157 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9158 sprintf (tmp, ".gnu.linkonce.wt.");
9159 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9160 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9161 secname = tmp;
9162 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9163 #endif
9164
9165 /* Output debugging information. */
9166 output_compilation_unit_header ();
9167 output_signature (node->signature, "Type Signature");
9168 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9169 "Offset to Type DIE");
9170 output_die (node->root_die);
9171
9172 unmark_dies (node->root_die);
9173 }
9174
9175 /* Return the DWARF2/3 pubname associated with a decl. */
9176
9177 static const char *
9178 dwarf2_name (tree decl, int scope)
9179 {
9180 if (DECL_NAMELESS (decl))
9181 return NULL;
9182 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9183 }
9184
9185 /* Add a new entry to .debug_pubnames if appropriate. */
9186
9187 static void
9188 add_pubname_string (const char *str, dw_die_ref die)
9189 {
9190 pubname_entry e;
9191
9192 e.die = die;
9193 e.name = xstrdup (str);
9194 vec_safe_push (pubname_table, e);
9195 }
9196
9197 static void
9198 add_pubname (tree decl, dw_die_ref die)
9199 {
9200 if (!want_pubnames ())
9201 return;
9202
9203 /* Don't add items to the table when we expect that the consumer will have
9204 just read the enclosing die. For example, if the consumer is looking at a
9205 class_member, it will either be inside the class already, or will have just
9206 looked up the class to find the member. Either way, searching the class is
9207 faster than searching the index. */
9208 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9209 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9210 {
9211 const char *name = dwarf2_name (decl, 1);
9212
9213 if (name)
9214 add_pubname_string (name, die);
9215 }
9216 }
9217
9218 /* Add an enumerator to the pubnames section. */
9219
9220 static void
9221 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9222 {
9223 pubname_entry e;
9224
9225 gcc_assert (scope_name);
9226 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9227 e.die = die;
9228 vec_safe_push (pubname_table, e);
9229 }
9230
9231 /* Add a new entry to .debug_pubtypes if appropriate. */
9232
9233 static void
9234 add_pubtype (tree decl, dw_die_ref die)
9235 {
9236 pubname_entry e;
9237
9238 if (!want_pubnames ())
9239 return;
9240
9241 if ((TREE_PUBLIC (decl)
9242 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9243 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9244 {
9245 tree scope = NULL;
9246 const char *scope_name = "";
9247 const char *sep = is_cxx () ? "::" : ".";
9248 const char *name;
9249
9250 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9251 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9252 {
9253 scope_name = lang_hooks.dwarf_name (scope, 1);
9254 if (scope_name != NULL && scope_name[0] != '\0')
9255 scope_name = concat (scope_name, sep, NULL);
9256 else
9257 scope_name = "";
9258 }
9259
9260 if (TYPE_P (decl))
9261 name = type_tag (decl);
9262 else
9263 name = lang_hooks.dwarf_name (decl, 1);
9264
9265 /* If we don't have a name for the type, there's no point in adding
9266 it to the table. */
9267 if (name != NULL && name[0] != '\0')
9268 {
9269 e.die = die;
9270 e.name = concat (scope_name, name, NULL);
9271 vec_safe_push (pubtype_table, e);
9272 }
9273
9274 /* Although it might be more consistent to add the pubinfo for the
9275 enumerators as their dies are created, they should only be added if the
9276 enum type meets the criteria above. So rather than re-check the parent
9277 enum type whenever an enumerator die is created, just output them all
9278 here. This isn't protected by the name conditional because anonymous
9279 enums don't have names. */
9280 if (die->die_tag == DW_TAG_enumeration_type)
9281 {
9282 dw_die_ref c;
9283
9284 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9285 }
9286 }
9287 }
9288
9289 /* Output a single entry in the pubnames table. */
9290
9291 static void
9292 output_pubname (dw_offset die_offset, pubname_entry *entry)
9293 {
9294 dw_die_ref die = entry->die;
9295 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9296
9297 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9298
9299 if (debug_generate_pub_sections == 2)
9300 {
9301 /* This logic follows gdb's method for determining the value of the flag
9302 byte. */
9303 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9304 switch (die->die_tag)
9305 {
9306 case DW_TAG_typedef:
9307 case DW_TAG_base_type:
9308 case DW_TAG_subrange_type:
9309 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9310 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9311 break;
9312 case DW_TAG_enumerator:
9313 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9314 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9315 if (!is_cxx () && !is_java ())
9316 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9317 break;
9318 case DW_TAG_subprogram:
9319 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9320 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9321 if (!is_ada ())
9322 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9323 break;
9324 case DW_TAG_constant:
9325 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9326 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9327 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9328 break;
9329 case DW_TAG_variable:
9330 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9331 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9332 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9333 break;
9334 case DW_TAG_namespace:
9335 case DW_TAG_imported_declaration:
9336 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9337 break;
9338 case DW_TAG_class_type:
9339 case DW_TAG_interface_type:
9340 case DW_TAG_structure_type:
9341 case DW_TAG_union_type:
9342 case DW_TAG_enumeration_type:
9343 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9344 if (!is_cxx () && !is_java ())
9345 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9346 break;
9347 default:
9348 /* An unusual tag. Leave the flag-byte empty. */
9349 break;
9350 }
9351 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9352 "GDB-index flags");
9353 }
9354
9355 dw2_asm_output_nstring (entry->name, -1, "external name");
9356 }
9357
9358
9359 /* Output the public names table used to speed up access to externally
9360 visible names; or the public types table used to find type definitions. */
9361
9362 static void
9363 output_pubnames (vec<pubname_entry, va_gc> *names)
9364 {
9365 unsigned i;
9366 unsigned long pubnames_length = size_of_pubnames (names);
9367 pubname_ref pub;
9368
9369 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9370 dw2_asm_output_data (4, 0xffffffff,
9371 "Initial length escape value indicating 64-bit DWARF extension");
9372 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9373
9374 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9375 dw2_asm_output_data (2, 2, "DWARF Version");
9376
9377 if (dwarf_split_debug_info)
9378 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9379 debug_skeleton_info_section,
9380 "Offset of Compilation Unit Info");
9381 else
9382 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9383 debug_info_section,
9384 "Offset of Compilation Unit Info");
9385 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9386 "Compilation Unit Length");
9387
9388 FOR_EACH_VEC_ELT (*names, i, pub)
9389 {
9390 if (include_pubname_in_output (names, pub))
9391 {
9392 dw_offset die_offset = pub->die->die_offset;
9393
9394 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9395 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9396 gcc_assert (pub->die->die_mark);
9397
9398 /* If we're putting types in their own .debug_types sections,
9399 the .debug_pubtypes table will still point to the compile
9400 unit (not the type unit), so we want to use the offset of
9401 the skeleton DIE (if there is one). */
9402 if (pub->die->comdat_type_p && names == pubtype_table)
9403 {
9404 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9405
9406 if (type_node != NULL)
9407 die_offset = (type_node->skeleton_die != NULL
9408 ? type_node->skeleton_die->die_offset
9409 : comp_unit_die ()->die_offset);
9410 }
9411
9412 output_pubname (die_offset, pub);
9413 }
9414 }
9415
9416 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9417 }
9418
9419 /* Output public names and types tables if necessary. */
9420
9421 static void
9422 output_pubtables (void)
9423 {
9424 if (!want_pubnames () || !info_section_emitted)
9425 return;
9426
9427 switch_to_section (debug_pubnames_section);
9428 output_pubnames (pubname_table);
9429 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9430 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9431 simply won't look for the section. */
9432 switch_to_section (debug_pubtypes_section);
9433 output_pubnames (pubtype_table);
9434 }
9435
9436
9437 /* Output the information that goes into the .debug_aranges table.
9438 Namely, define the beginning and ending address range of the
9439 text section generated for this compilation unit. */
9440
9441 static void
9442 output_aranges (unsigned long aranges_length)
9443 {
9444 unsigned i;
9445
9446 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9447 dw2_asm_output_data (4, 0xffffffff,
9448 "Initial length escape value indicating 64-bit DWARF extension");
9449 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9450 "Length of Address Ranges Info");
9451 /* Version number for aranges is still 2, even in DWARF3. */
9452 dw2_asm_output_data (2, 2, "DWARF Version");
9453 if (dwarf_split_debug_info)
9454 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9455 debug_skeleton_info_section,
9456 "Offset of Compilation Unit Info");
9457 else
9458 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9459 debug_info_section,
9460 "Offset of Compilation Unit Info");
9461 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9462 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9463
9464 /* We need to align to twice the pointer size here. */
9465 if (DWARF_ARANGES_PAD_SIZE)
9466 {
9467 /* Pad using a 2 byte words so that padding is correct for any
9468 pointer size. */
9469 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9470 2 * DWARF2_ADDR_SIZE);
9471 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9472 dw2_asm_output_data (2, 0, NULL);
9473 }
9474
9475 /* It is necessary not to output these entries if the sections were
9476 not used; if the sections were not used, the length will be 0 and
9477 the address may end up as 0 if the section is discarded by ld
9478 --gc-sections, leaving an invalid (0, 0) entry that can be
9479 confused with the terminator. */
9480 if (text_section_used)
9481 {
9482 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9483 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9484 text_section_label, "Length");
9485 }
9486 if (cold_text_section_used)
9487 {
9488 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9489 "Address");
9490 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9491 cold_text_section_label, "Length");
9492 }
9493
9494 if (have_multiple_function_sections)
9495 {
9496 unsigned fde_idx;
9497 dw_fde_ref fde;
9498
9499 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9500 {
9501 if (DECL_IGNORED_P (fde->decl))
9502 continue;
9503 if (!fde->in_std_section)
9504 {
9505 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9506 "Address");
9507 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9508 fde->dw_fde_begin, "Length");
9509 }
9510 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9511 {
9512 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9513 "Address");
9514 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9515 fde->dw_fde_second_begin, "Length");
9516 }
9517 }
9518 }
9519
9520 /* Output the terminator words. */
9521 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9522 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9523 }
9524
9525 /* Add a new entry to .debug_ranges. Return the offset at which it
9526 was placed. */
9527
9528 static unsigned int
9529 add_ranges_num (int num)
9530 {
9531 unsigned int in_use = ranges_table_in_use;
9532
9533 if (in_use == ranges_table_allocated)
9534 {
9535 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9536 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9537 ranges_table_allocated);
9538 memset (ranges_table + ranges_table_in_use, 0,
9539 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9540 }
9541
9542 ranges_table[in_use].num = num;
9543 ranges_table_in_use = in_use + 1;
9544
9545 return in_use * 2 * DWARF2_ADDR_SIZE;
9546 }
9547
9548 /* Add a new entry to .debug_ranges corresponding to a block, or a
9549 range terminator if BLOCK is NULL. */
9550
9551 static unsigned int
9552 add_ranges (const_tree block)
9553 {
9554 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9555 }
9556
9557 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9558 When using dwarf_split_debug_info, address attributes in dies destined
9559 for the final executable should be direct references--setting the
9560 parameter force_direct ensures this behavior. */
9561
9562 static void
9563 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9564 bool *added, bool force_direct)
9565 {
9566 unsigned int in_use = ranges_by_label_in_use;
9567 unsigned int offset;
9568
9569 if (in_use == ranges_by_label_allocated)
9570 {
9571 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9572 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9573 ranges_by_label,
9574 ranges_by_label_allocated);
9575 memset (ranges_by_label + ranges_by_label_in_use, 0,
9576 RANGES_TABLE_INCREMENT
9577 * sizeof (struct dw_ranges_by_label_struct));
9578 }
9579
9580 ranges_by_label[in_use].begin = begin;
9581 ranges_by_label[in_use].end = end;
9582 ranges_by_label_in_use = in_use + 1;
9583
9584 offset = add_ranges_num (-(int)in_use - 1);
9585 if (!*added)
9586 {
9587 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9588 *added = true;
9589 }
9590 }
9591
9592 static void
9593 output_ranges (void)
9594 {
9595 unsigned i;
9596 static const char *const start_fmt = "Offset %#x";
9597 const char *fmt = start_fmt;
9598
9599 for (i = 0; i < ranges_table_in_use; i++)
9600 {
9601 int block_num = ranges_table[i].num;
9602
9603 if (block_num > 0)
9604 {
9605 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9606 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9607
9608 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9609 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9610
9611 /* If all code is in the text section, then the compilation
9612 unit base address defaults to DW_AT_low_pc, which is the
9613 base of the text section. */
9614 if (!have_multiple_function_sections)
9615 {
9616 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9617 text_section_label,
9618 fmt, i * 2 * DWARF2_ADDR_SIZE);
9619 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9620 text_section_label, NULL);
9621 }
9622
9623 /* Otherwise, the compilation unit base address is zero,
9624 which allows us to use absolute addresses, and not worry
9625 about whether the target supports cross-section
9626 arithmetic. */
9627 else
9628 {
9629 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9630 fmt, i * 2 * DWARF2_ADDR_SIZE);
9631 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9632 }
9633
9634 fmt = NULL;
9635 }
9636
9637 /* Negative block_num stands for an index into ranges_by_label. */
9638 else if (block_num < 0)
9639 {
9640 int lab_idx = - block_num - 1;
9641
9642 if (!have_multiple_function_sections)
9643 {
9644 gcc_unreachable ();
9645 #if 0
9646 /* If we ever use add_ranges_by_labels () for a single
9647 function section, all we have to do is to take out
9648 the #if 0 above. */
9649 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9650 ranges_by_label[lab_idx].begin,
9651 text_section_label,
9652 fmt, i * 2 * DWARF2_ADDR_SIZE);
9653 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9654 ranges_by_label[lab_idx].end,
9655 text_section_label, NULL);
9656 #endif
9657 }
9658 else
9659 {
9660 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9661 ranges_by_label[lab_idx].begin,
9662 fmt, i * 2 * DWARF2_ADDR_SIZE);
9663 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9664 ranges_by_label[lab_idx].end,
9665 NULL);
9666 }
9667 }
9668 else
9669 {
9670 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9671 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9672 fmt = start_fmt;
9673 }
9674 }
9675 }
9676
9677 /* Data structure containing information about input files. */
9678 struct file_info
9679 {
9680 const char *path; /* Complete file name. */
9681 const char *fname; /* File name part. */
9682 int length; /* Length of entire string. */
9683 struct dwarf_file_data * file_idx; /* Index in input file table. */
9684 int dir_idx; /* Index in directory table. */
9685 };
9686
9687 /* Data structure containing information about directories with source
9688 files. */
9689 struct dir_info
9690 {
9691 const char *path; /* Path including directory name. */
9692 int length; /* Path length. */
9693 int prefix; /* Index of directory entry which is a prefix. */
9694 int count; /* Number of files in this directory. */
9695 int dir_idx; /* Index of directory used as base. */
9696 };
9697
9698 /* Callback function for file_info comparison. We sort by looking at
9699 the directories in the path. */
9700
9701 static int
9702 file_info_cmp (const void *p1, const void *p2)
9703 {
9704 const struct file_info *const s1 = (const struct file_info *) p1;
9705 const struct file_info *const s2 = (const struct file_info *) p2;
9706 const unsigned char *cp1;
9707 const unsigned char *cp2;
9708
9709 /* Take care of file names without directories. We need to make sure that
9710 we return consistent values to qsort since some will get confused if
9711 we return the same value when identical operands are passed in opposite
9712 orders. So if neither has a directory, return 0 and otherwise return
9713 1 or -1 depending on which one has the directory. */
9714 if ((s1->path == s1->fname || s2->path == s2->fname))
9715 return (s2->path == s2->fname) - (s1->path == s1->fname);
9716
9717 cp1 = (const unsigned char *) s1->path;
9718 cp2 = (const unsigned char *) s2->path;
9719
9720 while (1)
9721 {
9722 ++cp1;
9723 ++cp2;
9724 /* Reached the end of the first path? If so, handle like above. */
9725 if ((cp1 == (const unsigned char *) s1->fname)
9726 || (cp2 == (const unsigned char *) s2->fname))
9727 return ((cp2 == (const unsigned char *) s2->fname)
9728 - (cp1 == (const unsigned char *) s1->fname));
9729
9730 /* Character of current path component the same? */
9731 else if (*cp1 != *cp2)
9732 return *cp1 - *cp2;
9733 }
9734 }
9735
9736 struct file_name_acquire_data
9737 {
9738 struct file_info *files;
9739 int used_files;
9740 int max_files;
9741 };
9742
9743 /* Traversal function for the hash table. */
9744
9745 static int
9746 file_name_acquire (void ** slot, void *data)
9747 {
9748 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9749 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9750 struct file_info *fi;
9751 const char *f;
9752
9753 gcc_assert (fnad->max_files >= d->emitted_number);
9754
9755 if (! d->emitted_number)
9756 return 1;
9757
9758 gcc_assert (fnad->max_files != fnad->used_files);
9759
9760 fi = fnad->files + fnad->used_files++;
9761
9762 /* Skip all leading "./". */
9763 f = d->filename;
9764 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9765 f += 2;
9766
9767 /* Create a new array entry. */
9768 fi->path = f;
9769 fi->length = strlen (f);
9770 fi->file_idx = d;
9771
9772 /* Search for the file name part. */
9773 f = strrchr (f, DIR_SEPARATOR);
9774 #if defined (DIR_SEPARATOR_2)
9775 {
9776 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9777
9778 if (g != NULL)
9779 {
9780 if (f == NULL || f < g)
9781 f = g;
9782 }
9783 }
9784 #endif
9785
9786 fi->fname = f == NULL ? fi->path : f + 1;
9787 return 1;
9788 }
9789
9790 /* Output the directory table and the file name table. We try to minimize
9791 the total amount of memory needed. A heuristic is used to avoid large
9792 slowdowns with many input files. */
9793
9794 static void
9795 output_file_names (void)
9796 {
9797 struct file_name_acquire_data fnad;
9798 int numfiles;
9799 struct file_info *files;
9800 struct dir_info *dirs;
9801 int *saved;
9802 int *savehere;
9803 int *backmap;
9804 int ndirs;
9805 int idx_offset;
9806 int i;
9807
9808 if (!last_emitted_file)
9809 {
9810 dw2_asm_output_data (1, 0, "End directory table");
9811 dw2_asm_output_data (1, 0, "End file name table");
9812 return;
9813 }
9814
9815 numfiles = last_emitted_file->emitted_number;
9816
9817 /* Allocate the various arrays we need. */
9818 files = XALLOCAVEC (struct file_info, numfiles);
9819 dirs = XALLOCAVEC (struct dir_info, numfiles);
9820
9821 fnad.files = files;
9822 fnad.used_files = 0;
9823 fnad.max_files = numfiles;
9824 htab_traverse (file_table, file_name_acquire, &fnad);
9825 gcc_assert (fnad.used_files == fnad.max_files);
9826
9827 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9828
9829 /* Find all the different directories used. */
9830 dirs[0].path = files[0].path;
9831 dirs[0].length = files[0].fname - files[0].path;
9832 dirs[0].prefix = -1;
9833 dirs[0].count = 1;
9834 dirs[0].dir_idx = 0;
9835 files[0].dir_idx = 0;
9836 ndirs = 1;
9837
9838 for (i = 1; i < numfiles; i++)
9839 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9840 && memcmp (dirs[ndirs - 1].path, files[i].path,
9841 dirs[ndirs - 1].length) == 0)
9842 {
9843 /* Same directory as last entry. */
9844 files[i].dir_idx = ndirs - 1;
9845 ++dirs[ndirs - 1].count;
9846 }
9847 else
9848 {
9849 int j;
9850
9851 /* This is a new directory. */
9852 dirs[ndirs].path = files[i].path;
9853 dirs[ndirs].length = files[i].fname - files[i].path;
9854 dirs[ndirs].count = 1;
9855 dirs[ndirs].dir_idx = ndirs;
9856 files[i].dir_idx = ndirs;
9857
9858 /* Search for a prefix. */
9859 dirs[ndirs].prefix = -1;
9860 for (j = 0; j < ndirs; j++)
9861 if (dirs[j].length < dirs[ndirs].length
9862 && dirs[j].length > 1
9863 && (dirs[ndirs].prefix == -1
9864 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9865 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9866 dirs[ndirs].prefix = j;
9867
9868 ++ndirs;
9869 }
9870
9871 /* Now to the actual work. We have to find a subset of the directories which
9872 allow expressing the file name using references to the directory table
9873 with the least amount of characters. We do not do an exhaustive search
9874 where we would have to check out every combination of every single
9875 possible prefix. Instead we use a heuristic which provides nearly optimal
9876 results in most cases and never is much off. */
9877 saved = XALLOCAVEC (int, ndirs);
9878 savehere = XALLOCAVEC (int, ndirs);
9879
9880 memset (saved, '\0', ndirs * sizeof (saved[0]));
9881 for (i = 0; i < ndirs; i++)
9882 {
9883 int j;
9884 int total;
9885
9886 /* We can always save some space for the current directory. But this
9887 does not mean it will be enough to justify adding the directory. */
9888 savehere[i] = dirs[i].length;
9889 total = (savehere[i] - saved[i]) * dirs[i].count;
9890
9891 for (j = i + 1; j < ndirs; j++)
9892 {
9893 savehere[j] = 0;
9894 if (saved[j] < dirs[i].length)
9895 {
9896 /* Determine whether the dirs[i] path is a prefix of the
9897 dirs[j] path. */
9898 int k;
9899
9900 k = dirs[j].prefix;
9901 while (k != -1 && k != (int) i)
9902 k = dirs[k].prefix;
9903
9904 if (k == (int) i)
9905 {
9906 /* Yes it is. We can possibly save some memory by
9907 writing the filenames in dirs[j] relative to
9908 dirs[i]. */
9909 savehere[j] = dirs[i].length;
9910 total += (savehere[j] - saved[j]) * dirs[j].count;
9911 }
9912 }
9913 }
9914
9915 /* Check whether we can save enough to justify adding the dirs[i]
9916 directory. */
9917 if (total > dirs[i].length + 1)
9918 {
9919 /* It's worthwhile adding. */
9920 for (j = i; j < ndirs; j++)
9921 if (savehere[j] > 0)
9922 {
9923 /* Remember how much we saved for this directory so far. */
9924 saved[j] = savehere[j];
9925
9926 /* Remember the prefix directory. */
9927 dirs[j].dir_idx = i;
9928 }
9929 }
9930 }
9931
9932 /* Emit the directory name table. */
9933 idx_offset = dirs[0].length > 0 ? 1 : 0;
9934 for (i = 1 - idx_offset; i < ndirs; i++)
9935 dw2_asm_output_nstring (dirs[i].path,
9936 dirs[i].length
9937 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9938 "Directory Entry: %#x", i + idx_offset);
9939
9940 dw2_asm_output_data (1, 0, "End directory table");
9941
9942 /* We have to emit them in the order of emitted_number since that's
9943 used in the debug info generation. To do this efficiently we
9944 generate a back-mapping of the indices first. */
9945 backmap = XALLOCAVEC (int, numfiles);
9946 for (i = 0; i < numfiles; i++)
9947 backmap[files[i].file_idx->emitted_number - 1] = i;
9948
9949 /* Now write all the file names. */
9950 for (i = 0; i < numfiles; i++)
9951 {
9952 int file_idx = backmap[i];
9953 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9954
9955 #ifdef VMS_DEBUGGING_INFO
9956 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9957
9958 /* Setting these fields can lead to debugger miscomparisons,
9959 but VMS Debug requires them to be set correctly. */
9960
9961 int ver;
9962 long long cdt;
9963 long siz;
9964 int maxfilelen = strlen (files[file_idx].path)
9965 + dirs[dir_idx].length
9966 + MAX_VMS_VERSION_LEN + 1;
9967 char *filebuf = XALLOCAVEC (char, maxfilelen);
9968
9969 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9970 snprintf (filebuf, maxfilelen, "%s;%d",
9971 files[file_idx].path + dirs[dir_idx].length, ver);
9972
9973 dw2_asm_output_nstring
9974 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9975
9976 /* Include directory index. */
9977 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9978
9979 /* Modification time. */
9980 dw2_asm_output_data_uleb128
9981 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9982 ? cdt : 0,
9983 NULL);
9984
9985 /* File length in bytes. */
9986 dw2_asm_output_data_uleb128
9987 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9988 ? siz : 0,
9989 NULL);
9990 #else
9991 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9992 "File Entry: %#x", (unsigned) i + 1);
9993
9994 /* Include directory index. */
9995 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9996
9997 /* Modification time. */
9998 dw2_asm_output_data_uleb128 (0, NULL);
9999
10000 /* File length in bytes. */
10001 dw2_asm_output_data_uleb128 (0, NULL);
10002 #endif /* VMS_DEBUGGING_INFO */
10003 }
10004
10005 dw2_asm_output_data (1, 0, "End file name table");
10006 }
10007
10008
10009 /* Output one line number table into the .debug_line section. */
10010
10011 static void
10012 output_one_line_info_table (dw_line_info_table *table)
10013 {
10014 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10015 unsigned int current_line = 1;
10016 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10017 dw_line_info_entry *ent;
10018 size_t i;
10019
10020 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10021 {
10022 switch (ent->opcode)
10023 {
10024 case LI_set_address:
10025 /* ??? Unfortunately, we have little choice here currently, and
10026 must always use the most general form. GCC does not know the
10027 address delta itself, so we can't use DW_LNS_advance_pc. Many
10028 ports do have length attributes which will give an upper bound
10029 on the address range. We could perhaps use length attributes
10030 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10031 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10032
10033 /* This can handle any delta. This takes
10034 4+DWARF2_ADDR_SIZE bytes. */
10035 dw2_asm_output_data (1, 0, "set address %s", line_label);
10036 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10037 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10038 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10039 break;
10040
10041 case LI_set_line:
10042 if (ent->val == current_line)
10043 {
10044 /* We still need to start a new row, so output a copy insn. */
10045 dw2_asm_output_data (1, DW_LNS_copy,
10046 "copy line %u", current_line);
10047 }
10048 else
10049 {
10050 int line_offset = ent->val - current_line;
10051 int line_delta = line_offset - DWARF_LINE_BASE;
10052
10053 current_line = ent->val;
10054 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10055 {
10056 /* This can handle deltas from -10 to 234, using the current
10057 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10058 This takes 1 byte. */
10059 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10060 "line %u", current_line);
10061 }
10062 else
10063 {
10064 /* This can handle any delta. This takes at least 4 bytes,
10065 depending on the value being encoded. */
10066 dw2_asm_output_data (1, DW_LNS_advance_line,
10067 "advance to line %u", current_line);
10068 dw2_asm_output_data_sleb128 (line_offset, NULL);
10069 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10070 }
10071 }
10072 break;
10073
10074 case LI_set_file:
10075 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10076 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10077 break;
10078
10079 case LI_set_column:
10080 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10081 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10082 break;
10083
10084 case LI_negate_stmt:
10085 current_is_stmt = !current_is_stmt;
10086 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10087 "is_stmt %d", current_is_stmt);
10088 break;
10089
10090 case LI_set_prologue_end:
10091 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10092 "set prologue end");
10093 break;
10094
10095 case LI_set_epilogue_begin:
10096 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10097 "set epilogue begin");
10098 break;
10099
10100 case LI_set_discriminator:
10101 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10102 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10103 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10104 dw2_asm_output_data_uleb128 (ent->val, NULL);
10105 break;
10106 }
10107 }
10108
10109 /* Emit debug info for the address of the end of the table. */
10110 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10111 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10112 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10113 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10114
10115 dw2_asm_output_data (1, 0, "end sequence");
10116 dw2_asm_output_data_uleb128 (1, NULL);
10117 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10118 }
10119
10120 /* Output the source line number correspondence information. This
10121 information goes into the .debug_line section. */
10122
10123 static void
10124 output_line_info (bool prologue_only)
10125 {
10126 char l1[20], l2[20], p1[20], p2[20];
10127 int ver = dwarf_version;
10128 bool saw_one = false;
10129 int opc;
10130
10131 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10132 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10133 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10134 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10135
10136 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10137 dw2_asm_output_data (4, 0xffffffff,
10138 "Initial length escape value indicating 64-bit DWARF extension");
10139 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10140 "Length of Source Line Info");
10141 ASM_OUTPUT_LABEL (asm_out_file, l1);
10142
10143 dw2_asm_output_data (2, ver, "DWARF Version");
10144 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10145 ASM_OUTPUT_LABEL (asm_out_file, p1);
10146
10147 /* Define the architecture-dependent minimum instruction length (in bytes).
10148 In this implementation of DWARF, this field is used for information
10149 purposes only. Since GCC generates assembly language, we have no
10150 a priori knowledge of how many instruction bytes are generated for each
10151 source line, and therefore can use only the DW_LNE_set_address and
10152 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10153 this as '1', which is "correct enough" for all architectures,
10154 and don't let the target override. */
10155 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10156
10157 if (ver >= 4)
10158 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10159 "Maximum Operations Per Instruction");
10160 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10161 "Default is_stmt_start flag");
10162 dw2_asm_output_data (1, DWARF_LINE_BASE,
10163 "Line Base Value (Special Opcodes)");
10164 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10165 "Line Range Value (Special Opcodes)");
10166 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10167 "Special Opcode Base");
10168
10169 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10170 {
10171 int n_op_args;
10172 switch (opc)
10173 {
10174 case DW_LNS_advance_pc:
10175 case DW_LNS_advance_line:
10176 case DW_LNS_set_file:
10177 case DW_LNS_set_column:
10178 case DW_LNS_fixed_advance_pc:
10179 case DW_LNS_set_isa:
10180 n_op_args = 1;
10181 break;
10182 default:
10183 n_op_args = 0;
10184 break;
10185 }
10186
10187 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10188 opc, n_op_args);
10189 }
10190
10191 /* Write out the information about the files we use. */
10192 output_file_names ();
10193 ASM_OUTPUT_LABEL (asm_out_file, p2);
10194 if (prologue_only)
10195 {
10196 /* Output the marker for the end of the line number info. */
10197 ASM_OUTPUT_LABEL (asm_out_file, l2);
10198 return;
10199 }
10200
10201 if (separate_line_info)
10202 {
10203 dw_line_info_table *table;
10204 size_t i;
10205
10206 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10207 if (table->in_use)
10208 {
10209 output_one_line_info_table (table);
10210 saw_one = true;
10211 }
10212 }
10213 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10214 {
10215 output_one_line_info_table (cold_text_section_line_info);
10216 saw_one = true;
10217 }
10218
10219 /* ??? Some Darwin linkers crash on a .debug_line section with no
10220 sequences. Further, merely a DW_LNE_end_sequence entry is not
10221 sufficient -- the address column must also be initialized.
10222 Make sure to output at least one set_address/end_sequence pair,
10223 choosing .text since that section is always present. */
10224 if (text_section_line_info->in_use || !saw_one)
10225 output_one_line_info_table (text_section_line_info);
10226
10227 /* Output the marker for the end of the line number info. */
10228 ASM_OUTPUT_LABEL (asm_out_file, l2);
10229 }
10230 \f
10231 /* Given a pointer to a tree node for some base type, return a pointer to
10232 a DIE that describes the given type.
10233
10234 This routine must only be called for GCC type nodes that correspond to
10235 Dwarf base (fundamental) types. */
10236
10237 static dw_die_ref
10238 base_type_die (tree type)
10239 {
10240 dw_die_ref base_type_result;
10241 enum dwarf_type encoding;
10242
10243 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10244 return 0;
10245
10246 /* If this is a subtype that should not be emitted as a subrange type,
10247 use the base type. See subrange_type_for_debug_p. */
10248 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10249 type = TREE_TYPE (type);
10250
10251 switch (TREE_CODE (type))
10252 {
10253 case INTEGER_TYPE:
10254 if ((dwarf_version >= 4 || !dwarf_strict)
10255 && TYPE_NAME (type)
10256 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10257 && DECL_IS_BUILTIN (TYPE_NAME (type))
10258 && DECL_NAME (TYPE_NAME (type)))
10259 {
10260 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10261 if (strcmp (name, "char16_t") == 0
10262 || strcmp (name, "char32_t") == 0)
10263 {
10264 encoding = DW_ATE_UTF;
10265 break;
10266 }
10267 }
10268 if (TYPE_STRING_FLAG (type))
10269 {
10270 if (TYPE_UNSIGNED (type))
10271 encoding = DW_ATE_unsigned_char;
10272 else
10273 encoding = DW_ATE_signed_char;
10274 }
10275 else if (TYPE_UNSIGNED (type))
10276 encoding = DW_ATE_unsigned;
10277 else
10278 encoding = DW_ATE_signed;
10279 break;
10280
10281 case REAL_TYPE:
10282 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10283 {
10284 if (dwarf_version >= 3 || !dwarf_strict)
10285 encoding = DW_ATE_decimal_float;
10286 else
10287 encoding = DW_ATE_lo_user;
10288 }
10289 else
10290 encoding = DW_ATE_float;
10291 break;
10292
10293 case FIXED_POINT_TYPE:
10294 if (!(dwarf_version >= 3 || !dwarf_strict))
10295 encoding = DW_ATE_lo_user;
10296 else if (TYPE_UNSIGNED (type))
10297 encoding = DW_ATE_unsigned_fixed;
10298 else
10299 encoding = DW_ATE_signed_fixed;
10300 break;
10301
10302 /* Dwarf2 doesn't know anything about complex ints, so use
10303 a user defined type for it. */
10304 case COMPLEX_TYPE:
10305 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10306 encoding = DW_ATE_complex_float;
10307 else
10308 encoding = DW_ATE_lo_user;
10309 break;
10310
10311 case BOOLEAN_TYPE:
10312 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10313 encoding = DW_ATE_boolean;
10314 break;
10315
10316 default:
10317 /* No other TREE_CODEs are Dwarf fundamental types. */
10318 gcc_unreachable ();
10319 }
10320
10321 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10322
10323 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10324 int_size_in_bytes (type));
10325 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10326 add_pubtype (type, base_type_result);
10327
10328 return base_type_result;
10329 }
10330
10331 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10332 named 'auto' in its type: return true for it, false otherwise. */
10333
10334 static inline bool
10335 is_cxx_auto (tree type)
10336 {
10337 if (is_cxx ())
10338 {
10339 tree name = TYPE_IDENTIFIER (type);
10340 if (name == get_identifier ("auto")
10341 || name == get_identifier ("decltype(auto)"))
10342 return true;
10343 }
10344 return false;
10345 }
10346
10347 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10348 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10349
10350 static inline int
10351 is_base_type (tree type)
10352 {
10353 switch (TREE_CODE (type))
10354 {
10355 case ERROR_MARK:
10356 case VOID_TYPE:
10357 case INTEGER_TYPE:
10358 case REAL_TYPE:
10359 case FIXED_POINT_TYPE:
10360 case COMPLEX_TYPE:
10361 case BOOLEAN_TYPE:
10362 return 1;
10363
10364 case ARRAY_TYPE:
10365 case RECORD_TYPE:
10366 case UNION_TYPE:
10367 case QUAL_UNION_TYPE:
10368 case ENUMERAL_TYPE:
10369 case FUNCTION_TYPE:
10370 case METHOD_TYPE:
10371 case POINTER_TYPE:
10372 case REFERENCE_TYPE:
10373 case NULLPTR_TYPE:
10374 case OFFSET_TYPE:
10375 case LANG_TYPE:
10376 case VECTOR_TYPE:
10377 return 0;
10378
10379 default:
10380 if (is_cxx_auto (type))
10381 return 0;
10382 gcc_unreachable ();
10383 }
10384
10385 return 0;
10386 }
10387
10388 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10389 node, return the size in bits for the type if it is a constant, or else
10390 return the alignment for the type if the type's size is not constant, or
10391 else return BITS_PER_WORD if the type actually turns out to be an
10392 ERROR_MARK node. */
10393
10394 static inline unsigned HOST_WIDE_INT
10395 simple_type_size_in_bits (const_tree type)
10396 {
10397 if (TREE_CODE (type) == ERROR_MARK)
10398 return BITS_PER_WORD;
10399 else if (TYPE_SIZE (type) == NULL_TREE)
10400 return 0;
10401 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10402 return tree_to_uhwi (TYPE_SIZE (type));
10403 else
10404 return TYPE_ALIGN (type);
10405 }
10406
10407 /* Similarly, but return an offset_int instead of UHWI. */
10408
10409 static inline offset_int
10410 offset_int_type_size_in_bits (const_tree type)
10411 {
10412 if (TREE_CODE (type) == ERROR_MARK)
10413 return BITS_PER_WORD;
10414 else if (TYPE_SIZE (type) == NULL_TREE)
10415 return 0;
10416 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10417 return wi::to_offset (TYPE_SIZE (type));
10418 else
10419 return TYPE_ALIGN (type);
10420 }
10421
10422 /* Given a pointer to a tree node for a subrange type, return a pointer
10423 to a DIE that describes the given type. */
10424
10425 static dw_die_ref
10426 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10427 {
10428 dw_die_ref subrange_die;
10429 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10430
10431 if (context_die == NULL)
10432 context_die = comp_unit_die ();
10433
10434 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10435
10436 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10437 {
10438 /* The size of the subrange type and its base type do not match,
10439 so we need to generate a size attribute for the subrange type. */
10440 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10441 }
10442
10443 if (low)
10444 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10445 if (high)
10446 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10447
10448 return subrange_die;
10449 }
10450
10451 /* Returns the (const and/or volatile) cv_qualifiers associated with
10452 the decl node. This will normally be augmented with the
10453 cv_qualifiers of the underlying type in add_type_attribute. */
10454
10455 static int
10456 decl_quals (const_tree decl)
10457 {
10458 return ((TREE_READONLY (decl)
10459 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10460 | (TREE_THIS_VOLATILE (decl)
10461 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10462 }
10463
10464 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10465 entry that chains various modifiers in front of the given type. */
10466
10467 static dw_die_ref
10468 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10469 {
10470 enum tree_code code = TREE_CODE (type);
10471 dw_die_ref mod_type_die;
10472 dw_die_ref sub_die = NULL;
10473 tree item_type = NULL;
10474 tree qualified_type;
10475 tree name, low, high;
10476 dw_die_ref mod_scope;
10477
10478 if (code == ERROR_MARK)
10479 return NULL;
10480
10481 /* Only these cv-qualifiers are currently handled. */
10482 cv_quals &= (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT);
10483
10484 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10485 tag modifier (and not an attribute) old consumers won't be able
10486 to handle it. */
10487 if (dwarf_version < 3)
10488 cv_quals &= ~TYPE_QUAL_RESTRICT;
10489
10490 /* See if we already have the appropriately qualified variant of
10491 this type. */
10492 qualified_type = get_qualified_type (type, cv_quals);
10493
10494 if (qualified_type == sizetype
10495 && TYPE_NAME (qualified_type)
10496 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10497 {
10498 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10499
10500 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10501 && TYPE_PRECISION (t)
10502 == TYPE_PRECISION (qualified_type)
10503 && TYPE_UNSIGNED (t)
10504 == TYPE_UNSIGNED (qualified_type));
10505 qualified_type = t;
10506 }
10507
10508 /* If we do, then we can just use its DIE, if it exists. */
10509 if (qualified_type)
10510 {
10511 mod_type_die = lookup_type_die (qualified_type);
10512 if (mod_type_die)
10513 return mod_type_die;
10514 }
10515
10516 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10517
10518 /* Handle C typedef types. */
10519 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10520 && !DECL_ARTIFICIAL (name))
10521 {
10522 tree dtype = TREE_TYPE (name);
10523
10524 if (qualified_type == dtype)
10525 {
10526 /* For a named type, use the typedef. */
10527 gen_type_die (qualified_type, context_die);
10528 return lookup_type_die (qualified_type);
10529 }
10530 else
10531 {
10532 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10533 dquals &= (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT);
10534 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10535 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10536 /* cv-unqualified version of named type. Just use
10537 the unnamed type to which it refers. */
10538 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10539 cv_quals, context_die);
10540 /* Else cv-qualified version of named type; fall through. */
10541 }
10542 }
10543
10544 mod_scope = scope_die_for (type, context_die);
10545
10546 if ((cv_quals & TYPE_QUAL_CONST)
10547 /* If there are multiple type modifiers, prefer a path which
10548 leads to a qualified type. */
10549 && (((cv_quals & ~TYPE_QUAL_CONST) == TYPE_UNQUALIFIED)
10550 || get_qualified_type (type, cv_quals) == NULL_TREE
10551 || (get_qualified_type (type, cv_quals & ~TYPE_QUAL_CONST)
10552 != NULL_TREE)))
10553 {
10554 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10555 sub_die = modified_type_die (type, cv_quals & ~TYPE_QUAL_CONST,
10556 context_die);
10557 }
10558 else if ((cv_quals & TYPE_QUAL_VOLATILE)
10559 && (((cv_quals & ~TYPE_QUAL_VOLATILE) == TYPE_UNQUALIFIED)
10560 || get_qualified_type (type, cv_quals) == NULL_TREE
10561 || (get_qualified_type (type, cv_quals & ~TYPE_QUAL_VOLATILE)
10562 != NULL_TREE)))
10563 {
10564 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10565 sub_die = modified_type_die (type, cv_quals & ~TYPE_QUAL_VOLATILE,
10566 context_die);
10567 }
10568 else if (cv_quals & TYPE_QUAL_RESTRICT)
10569 {
10570 mod_type_die = new_die (DW_TAG_restrict_type, mod_scope, type);
10571 sub_die = modified_type_die (type, cv_quals & ~TYPE_QUAL_RESTRICT,
10572 context_die);
10573 }
10574 else if (code == POINTER_TYPE)
10575 {
10576 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10577 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10578 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10579 item_type = TREE_TYPE (type);
10580 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10581 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10582 TYPE_ADDR_SPACE (item_type));
10583 }
10584 else if (code == REFERENCE_TYPE)
10585 {
10586 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10587 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10588 type);
10589 else
10590 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10591 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10592 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10593 item_type = TREE_TYPE (type);
10594 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10595 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10596 TYPE_ADDR_SPACE (item_type));
10597 }
10598 else if (code == INTEGER_TYPE
10599 && TREE_TYPE (type) != NULL_TREE
10600 && subrange_type_for_debug_p (type, &low, &high))
10601 {
10602 mod_type_die = subrange_type_die (type, low, high, context_die);
10603 item_type = TREE_TYPE (type);
10604 }
10605 else if (is_base_type (type))
10606 mod_type_die = base_type_die (type);
10607 else
10608 {
10609 gen_type_die (type, context_die);
10610
10611 /* We have to get the type_main_variant here (and pass that to the
10612 `lookup_type_die' routine) because the ..._TYPE node we have
10613 might simply be a *copy* of some original type node (where the
10614 copy was created to help us keep track of typedef names) and
10615 that copy might have a different TYPE_UID from the original
10616 ..._TYPE node. */
10617 if (TREE_CODE (type) != VECTOR_TYPE)
10618 return lookup_type_die (type_main_variant (type));
10619 else
10620 /* Vectors have the debugging information in the type,
10621 not the main variant. */
10622 return lookup_type_die (type);
10623 }
10624
10625 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10626 don't output a DW_TAG_typedef, since there isn't one in the
10627 user's program; just attach a DW_AT_name to the type.
10628 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10629 if the base type already has the same name. */
10630 if (name
10631 && ((TREE_CODE (name) != TYPE_DECL
10632 && (qualified_type == TYPE_MAIN_VARIANT (type)
10633 || (cv_quals == TYPE_UNQUALIFIED)))
10634 || (TREE_CODE (name) == TYPE_DECL
10635 && TREE_TYPE (name) == qualified_type
10636 && DECL_NAME (name))))
10637 {
10638 if (TREE_CODE (name) == TYPE_DECL)
10639 /* Could just call add_name_and_src_coords_attributes here,
10640 but since this is a builtin type it doesn't have any
10641 useful source coordinates anyway. */
10642 name = DECL_NAME (name);
10643 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10644 }
10645 /* This probably indicates a bug. */
10646 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10647 {
10648 name = TYPE_IDENTIFIER (type);
10649 add_name_attribute (mod_type_die,
10650 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10651 }
10652
10653 if (qualified_type)
10654 equate_type_number_to_die (qualified_type, mod_type_die);
10655
10656 if (item_type)
10657 /* We must do this after the equate_type_number_to_die call, in case
10658 this is a recursive type. This ensures that the modified_type_die
10659 recursion will terminate even if the type is recursive. Recursive
10660 types are possible in Ada. */
10661 sub_die = modified_type_die (item_type,
10662 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10663 context_die);
10664
10665 if (sub_die != NULL)
10666 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10667
10668 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10669 if (TYPE_ARTIFICIAL (type))
10670 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10671
10672 return mod_type_die;
10673 }
10674
10675 /* Generate DIEs for the generic parameters of T.
10676 T must be either a generic type or a generic function.
10677 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10678
10679 static void
10680 gen_generic_params_dies (tree t)
10681 {
10682 tree parms, args;
10683 int parms_num, i;
10684 dw_die_ref die = NULL;
10685 int non_default;
10686
10687 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10688 return;
10689
10690 if (TYPE_P (t))
10691 die = lookup_type_die (t);
10692 else if (DECL_P (t))
10693 die = lookup_decl_die (t);
10694
10695 gcc_assert (die);
10696
10697 parms = lang_hooks.get_innermost_generic_parms (t);
10698 if (!parms)
10699 /* T has no generic parameter. It means T is neither a generic type
10700 or function. End of story. */
10701 return;
10702
10703 parms_num = TREE_VEC_LENGTH (parms);
10704 args = lang_hooks.get_innermost_generic_args (t);
10705 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10706 non_default = int_cst_value (TREE_CHAIN (args));
10707 else
10708 non_default = TREE_VEC_LENGTH (args);
10709 for (i = 0; i < parms_num; i++)
10710 {
10711 tree parm, arg, arg_pack_elems;
10712 dw_die_ref parm_die;
10713
10714 parm = TREE_VEC_ELT (parms, i);
10715 arg = TREE_VEC_ELT (args, i);
10716 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10717 gcc_assert (parm && TREE_VALUE (parm) && arg);
10718
10719 if (parm && TREE_VALUE (parm) && arg)
10720 {
10721 /* If PARM represents a template parameter pack,
10722 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10723 by DW_TAG_template_*_parameter DIEs for the argument
10724 pack elements of ARG. Note that ARG would then be
10725 an argument pack. */
10726 if (arg_pack_elems)
10727 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10728 arg_pack_elems,
10729 die);
10730 else
10731 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10732 true /* emit name */, die);
10733 if (i >= non_default)
10734 add_AT_flag (parm_die, DW_AT_default_value, 1);
10735 }
10736 }
10737 }
10738
10739 /* Create and return a DIE for PARM which should be
10740 the representation of a generic type parameter.
10741 For instance, in the C++ front end, PARM would be a template parameter.
10742 ARG is the argument to PARM.
10743 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10744 name of the PARM.
10745 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10746 as a child node. */
10747
10748 static dw_die_ref
10749 generic_parameter_die (tree parm, tree arg,
10750 bool emit_name_p,
10751 dw_die_ref parent_die)
10752 {
10753 dw_die_ref tmpl_die = NULL;
10754 const char *name = NULL;
10755
10756 if (!parm || !DECL_NAME (parm) || !arg)
10757 return NULL;
10758
10759 /* We support non-type generic parameters and arguments,
10760 type generic parameters and arguments, as well as
10761 generic generic parameters (a.k.a. template template parameters in C++)
10762 and arguments. */
10763 if (TREE_CODE (parm) == PARM_DECL)
10764 /* PARM is a nontype generic parameter */
10765 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10766 else if (TREE_CODE (parm) == TYPE_DECL)
10767 /* PARM is a type generic parameter. */
10768 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10769 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10770 /* PARM is a generic generic parameter.
10771 Its DIE is a GNU extension. It shall have a
10772 DW_AT_name attribute to represent the name of the template template
10773 parameter, and a DW_AT_GNU_template_name attribute to represent the
10774 name of the template template argument. */
10775 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10776 parent_die, parm);
10777 else
10778 gcc_unreachable ();
10779
10780 if (tmpl_die)
10781 {
10782 tree tmpl_type;
10783
10784 /* If PARM is a generic parameter pack, it means we are
10785 emitting debug info for a template argument pack element.
10786 In other terms, ARG is a template argument pack element.
10787 In that case, we don't emit any DW_AT_name attribute for
10788 the die. */
10789 if (emit_name_p)
10790 {
10791 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10792 gcc_assert (name);
10793 add_AT_string (tmpl_die, DW_AT_name, name);
10794 }
10795
10796 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10797 {
10798 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10799 TMPL_DIE should have a child DW_AT_type attribute that is set
10800 to the type of the argument to PARM, which is ARG.
10801 If PARM is a type generic parameter, TMPL_DIE should have a
10802 child DW_AT_type that is set to ARG. */
10803 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10804 add_type_attribute (tmpl_die, tmpl_type,
10805 (TREE_THIS_VOLATILE (tmpl_type)
10806 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10807 parent_die);
10808 }
10809 else
10810 {
10811 /* So TMPL_DIE is a DIE representing a
10812 a generic generic template parameter, a.k.a template template
10813 parameter in C++ and arg is a template. */
10814
10815 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10816 to the name of the argument. */
10817 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10818 if (name)
10819 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10820 }
10821
10822 if (TREE_CODE (parm) == PARM_DECL)
10823 /* So PARM is a non-type generic parameter.
10824 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10825 attribute of TMPL_DIE which value represents the value
10826 of ARG.
10827 We must be careful here:
10828 The value of ARG might reference some function decls.
10829 We might currently be emitting debug info for a generic
10830 type and types are emitted before function decls, we don't
10831 know if the function decls referenced by ARG will actually be
10832 emitted after cgraph computations.
10833 So must defer the generation of the DW_AT_const_value to
10834 after cgraph is ready. */
10835 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10836 }
10837
10838 return tmpl_die;
10839 }
10840
10841 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10842 PARM_PACK must be a template parameter pack. The returned DIE
10843 will be child DIE of PARENT_DIE. */
10844
10845 static dw_die_ref
10846 template_parameter_pack_die (tree parm_pack,
10847 tree parm_pack_args,
10848 dw_die_ref parent_die)
10849 {
10850 dw_die_ref die;
10851 int j;
10852
10853 gcc_assert (parent_die && parm_pack);
10854
10855 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10856 add_name_and_src_coords_attributes (die, parm_pack);
10857 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10858 generic_parameter_die (parm_pack,
10859 TREE_VEC_ELT (parm_pack_args, j),
10860 false /* Don't emit DW_AT_name */,
10861 die);
10862 return die;
10863 }
10864
10865 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10866 an enumerated type. */
10867
10868 static inline int
10869 type_is_enum (const_tree type)
10870 {
10871 return TREE_CODE (type) == ENUMERAL_TYPE;
10872 }
10873
10874 /* Return the DBX register number described by a given RTL node. */
10875
10876 static unsigned int
10877 dbx_reg_number (const_rtx rtl)
10878 {
10879 unsigned regno = REGNO (rtl);
10880
10881 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10882
10883 #ifdef LEAF_REG_REMAP
10884 if (crtl->uses_only_leaf_regs)
10885 {
10886 int leaf_reg = LEAF_REG_REMAP (regno);
10887 if (leaf_reg != -1)
10888 regno = (unsigned) leaf_reg;
10889 }
10890 #endif
10891
10892 regno = DBX_REGISTER_NUMBER (regno);
10893 gcc_assert (regno != INVALID_REGNUM);
10894 return regno;
10895 }
10896
10897 /* Optionally add a DW_OP_piece term to a location description expression.
10898 DW_OP_piece is only added if the location description expression already
10899 doesn't end with DW_OP_piece. */
10900
10901 static void
10902 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10903 {
10904 dw_loc_descr_ref loc;
10905
10906 if (*list_head != NULL)
10907 {
10908 /* Find the end of the chain. */
10909 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10910 ;
10911
10912 if (loc->dw_loc_opc != DW_OP_piece)
10913 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10914 }
10915 }
10916
10917 /* Return a location descriptor that designates a machine register or
10918 zero if there is none. */
10919
10920 static dw_loc_descr_ref
10921 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10922 {
10923 rtx regs;
10924
10925 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10926 return 0;
10927
10928 /* We only use "frame base" when we're sure we're talking about the
10929 post-prologue local stack frame. We do this by *not* running
10930 register elimination until this point, and recognizing the special
10931 argument pointer and soft frame pointer rtx's.
10932 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10933 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10934 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10935 {
10936 dw_loc_descr_ref result = NULL;
10937
10938 if (dwarf_version >= 4 || !dwarf_strict)
10939 {
10940 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10941 initialized);
10942 if (result)
10943 add_loc_descr (&result,
10944 new_loc_descr (DW_OP_stack_value, 0, 0));
10945 }
10946 return result;
10947 }
10948
10949 regs = targetm.dwarf_register_span (rtl);
10950
10951 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10952 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10953 else
10954 {
10955 unsigned int dbx_regnum = dbx_reg_number (rtl);
10956 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10957 return 0;
10958 return one_reg_loc_descriptor (dbx_regnum, initialized);
10959 }
10960 }
10961
10962 /* Return a location descriptor that designates a machine register for
10963 a given hard register number. */
10964
10965 static dw_loc_descr_ref
10966 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10967 {
10968 dw_loc_descr_ref reg_loc_descr;
10969
10970 if (regno <= 31)
10971 reg_loc_descr
10972 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10973 else
10974 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10975
10976 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10977 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10978
10979 return reg_loc_descr;
10980 }
10981
10982 /* Given an RTL of a register, return a location descriptor that
10983 designates a value that spans more than one register. */
10984
10985 static dw_loc_descr_ref
10986 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10987 enum var_init_status initialized)
10988 {
10989 int size, i;
10990 dw_loc_descr_ref loc_result = NULL;
10991
10992 /* Simple, contiguous registers. */
10993 if (regs == NULL_RTX)
10994 {
10995 unsigned reg = REGNO (rtl);
10996 int nregs;
10997
10998 #ifdef LEAF_REG_REMAP
10999 if (crtl->uses_only_leaf_regs)
11000 {
11001 int leaf_reg = LEAF_REG_REMAP (reg);
11002 if (leaf_reg != -1)
11003 reg = (unsigned) leaf_reg;
11004 }
11005 #endif
11006
11007 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11008 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11009
11010 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11011
11012 loc_result = NULL;
11013 while (nregs--)
11014 {
11015 dw_loc_descr_ref t;
11016
11017 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11018 VAR_INIT_STATUS_INITIALIZED);
11019 add_loc_descr (&loc_result, t);
11020 add_loc_descr_op_piece (&loc_result, size);
11021 ++reg;
11022 }
11023 return loc_result;
11024 }
11025
11026 /* Now onto stupid register sets in non contiguous locations. */
11027
11028 gcc_assert (GET_CODE (regs) == PARALLEL);
11029
11030 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11031 loc_result = NULL;
11032
11033 for (i = 0; i < XVECLEN (regs, 0); ++i)
11034 {
11035 dw_loc_descr_ref t;
11036
11037 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11038 VAR_INIT_STATUS_INITIALIZED);
11039 add_loc_descr (&loc_result, t);
11040 add_loc_descr_op_piece (&loc_result, size);
11041 }
11042
11043 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11044 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11045 return loc_result;
11046 }
11047
11048 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11049
11050 /* Return a location descriptor that designates a constant i,
11051 as a compound operation from constant (i >> shift), constant shift
11052 and DW_OP_shl. */
11053
11054 static dw_loc_descr_ref
11055 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11056 {
11057 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11058 add_loc_descr (&ret, int_loc_descriptor (shift));
11059 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11060 return ret;
11061 }
11062
11063 /* Return a location descriptor that designates a constant. */
11064
11065 static dw_loc_descr_ref
11066 int_loc_descriptor (HOST_WIDE_INT i)
11067 {
11068 enum dwarf_location_atom op;
11069
11070 /* Pick the smallest representation of a constant, rather than just
11071 defaulting to the LEB encoding. */
11072 if (i >= 0)
11073 {
11074 int clz = clz_hwi (i);
11075 int ctz = ctz_hwi (i);
11076 if (i <= 31)
11077 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11078 else if (i <= 0xff)
11079 op = DW_OP_const1u;
11080 else if (i <= 0xffff)
11081 op = DW_OP_const2u;
11082 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11083 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11084 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11085 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11086 while DW_OP_const4u is 5 bytes. */
11087 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11088 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11089 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11090 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11091 while DW_OP_const4u is 5 bytes. */
11092 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11093 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11094 op = DW_OP_const4u;
11095 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11096 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11097 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11098 while DW_OP_constu of constant >= 0x100000000 takes at least
11099 6 bytes. */
11100 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11101 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11102 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11103 >= HOST_BITS_PER_WIDE_INT)
11104 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11105 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11106 while DW_OP_constu takes in this case at least 6 bytes. */
11107 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11108 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11109 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11110 && size_of_uleb128 (i) > 6)
11111 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11112 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11113 else
11114 op = DW_OP_constu;
11115 }
11116 else
11117 {
11118 if (i >= -0x80)
11119 op = DW_OP_const1s;
11120 else if (i >= -0x8000)
11121 op = DW_OP_const2s;
11122 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11123 {
11124 if (size_of_int_loc_descriptor (i) < 5)
11125 {
11126 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11127 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11128 return ret;
11129 }
11130 op = DW_OP_const4s;
11131 }
11132 else
11133 {
11134 if (size_of_int_loc_descriptor (i)
11135 < (unsigned long) 1 + size_of_sleb128 (i))
11136 {
11137 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11138 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11139 return ret;
11140 }
11141 op = DW_OP_consts;
11142 }
11143 }
11144
11145 return new_loc_descr (op, i, 0);
11146 }
11147
11148 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11149 without actually allocating it. */
11150
11151 static unsigned long
11152 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11153 {
11154 return size_of_int_loc_descriptor (i >> shift)
11155 + size_of_int_loc_descriptor (shift)
11156 + 1;
11157 }
11158
11159 /* Return size_of_locs (int_loc_descriptor (i)) without
11160 actually allocating it. */
11161
11162 static unsigned long
11163 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11164 {
11165 unsigned long s;
11166
11167 if (i >= 0)
11168 {
11169 int clz, ctz;
11170 if (i <= 31)
11171 return 1;
11172 else if (i <= 0xff)
11173 return 2;
11174 else if (i <= 0xffff)
11175 return 3;
11176 clz = clz_hwi (i);
11177 ctz = ctz_hwi (i);
11178 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11179 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11180 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11181 - clz - 5);
11182 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11183 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11184 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11185 - clz - 8);
11186 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11187 return 5;
11188 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11189 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11190 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11191 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11192 - clz - 8);
11193 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11194 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11195 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11196 - clz - 16);
11197 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11198 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11199 && s > 6)
11200 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11201 - clz - 32);
11202 else
11203 return 1 + s;
11204 }
11205 else
11206 {
11207 if (i >= -0x80)
11208 return 2;
11209 else if (i >= -0x8000)
11210 return 3;
11211 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11212 {
11213 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11214 {
11215 s = size_of_int_loc_descriptor (-i) + 1;
11216 if (s < 5)
11217 return s;
11218 }
11219 return 5;
11220 }
11221 else
11222 {
11223 unsigned long r = 1 + size_of_sleb128 (i);
11224 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11225 {
11226 s = size_of_int_loc_descriptor (-i) + 1;
11227 if (s < r)
11228 return s;
11229 }
11230 return r;
11231 }
11232 }
11233 }
11234
11235 /* Return loc description representing "address" of integer value.
11236 This can appear only as toplevel expression. */
11237
11238 static dw_loc_descr_ref
11239 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11240 {
11241 int litsize;
11242 dw_loc_descr_ref loc_result = NULL;
11243
11244 if (!(dwarf_version >= 4 || !dwarf_strict))
11245 return NULL;
11246
11247 litsize = size_of_int_loc_descriptor (i);
11248 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11249 is more compact. For DW_OP_stack_value we need:
11250 litsize + 1 (DW_OP_stack_value)
11251 and for DW_OP_implicit_value:
11252 1 (DW_OP_implicit_value) + 1 (length) + size. */
11253 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11254 {
11255 loc_result = int_loc_descriptor (i);
11256 add_loc_descr (&loc_result,
11257 new_loc_descr (DW_OP_stack_value, 0, 0));
11258 return loc_result;
11259 }
11260
11261 loc_result = new_loc_descr (DW_OP_implicit_value,
11262 size, 0);
11263 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11264 loc_result->dw_loc_oprnd2.v.val_int = i;
11265 return loc_result;
11266 }
11267
11268 /* Return a location descriptor that designates a base+offset location. */
11269
11270 static dw_loc_descr_ref
11271 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11272 enum var_init_status initialized)
11273 {
11274 unsigned int regno;
11275 dw_loc_descr_ref result;
11276 dw_fde_ref fde = cfun->fde;
11277
11278 /* We only use "frame base" when we're sure we're talking about the
11279 post-prologue local stack frame. We do this by *not* running
11280 register elimination until this point, and recognizing the special
11281 argument pointer and soft frame pointer rtx's. */
11282 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11283 {
11284 rtx elim = (ira_use_lra_p
11285 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11286 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11287
11288 if (elim != reg)
11289 {
11290 if (GET_CODE (elim) == PLUS)
11291 {
11292 offset += INTVAL (XEXP (elim, 1));
11293 elim = XEXP (elim, 0);
11294 }
11295 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11296 && (elim == hard_frame_pointer_rtx
11297 || elim == stack_pointer_rtx))
11298 || elim == (frame_pointer_needed
11299 ? hard_frame_pointer_rtx
11300 : stack_pointer_rtx));
11301
11302 /* If drap register is used to align stack, use frame
11303 pointer + offset to access stack variables. If stack
11304 is aligned without drap, use stack pointer + offset to
11305 access stack variables. */
11306 if (crtl->stack_realign_tried
11307 && reg == frame_pointer_rtx)
11308 {
11309 int base_reg
11310 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11311 ? HARD_FRAME_POINTER_REGNUM
11312 : REGNO (elim));
11313 return new_reg_loc_descr (base_reg, offset);
11314 }
11315
11316 gcc_assert (frame_pointer_fb_offset_valid);
11317 offset += frame_pointer_fb_offset;
11318 return new_loc_descr (DW_OP_fbreg, offset, 0);
11319 }
11320 }
11321
11322 regno = REGNO (reg);
11323 #ifdef LEAF_REG_REMAP
11324 if (crtl->uses_only_leaf_regs)
11325 {
11326 int leaf_reg = LEAF_REG_REMAP (regno);
11327 if (leaf_reg != -1)
11328 regno = (unsigned) leaf_reg;
11329 }
11330 #endif
11331 regno = DWARF_FRAME_REGNUM (regno);
11332
11333 if (!optimize && fde
11334 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11335 {
11336 /* Use cfa+offset to represent the location of arguments passed
11337 on the stack when drap is used to align stack.
11338 Only do this when not optimizing, for optimized code var-tracking
11339 is supposed to track where the arguments live and the register
11340 used as vdrap or drap in some spot might be used for something
11341 else in other part of the routine. */
11342 return new_loc_descr (DW_OP_fbreg, offset, 0);
11343 }
11344
11345 if (regno <= 31)
11346 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11347 offset, 0);
11348 else
11349 result = new_loc_descr (DW_OP_bregx, regno, offset);
11350
11351 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11352 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11353
11354 return result;
11355 }
11356
11357 /* Return true if this RTL expression describes a base+offset calculation. */
11358
11359 static inline int
11360 is_based_loc (const_rtx rtl)
11361 {
11362 return (GET_CODE (rtl) == PLUS
11363 && ((REG_P (XEXP (rtl, 0))
11364 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11365 && CONST_INT_P (XEXP (rtl, 1)))));
11366 }
11367
11368 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11369 failed. */
11370
11371 static dw_loc_descr_ref
11372 tls_mem_loc_descriptor (rtx mem)
11373 {
11374 tree base;
11375 dw_loc_descr_ref loc_result;
11376
11377 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11378 return NULL;
11379
11380 base = get_base_address (MEM_EXPR (mem));
11381 if (base == NULL
11382 || TREE_CODE (base) != VAR_DECL
11383 || !DECL_THREAD_LOCAL_P (base))
11384 return NULL;
11385
11386 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11387 if (loc_result == NULL)
11388 return NULL;
11389
11390 if (MEM_OFFSET (mem))
11391 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11392
11393 return loc_result;
11394 }
11395
11396 /* Output debug info about reason why we failed to expand expression as dwarf
11397 expression. */
11398
11399 static void
11400 expansion_failed (tree expr, rtx rtl, char const *reason)
11401 {
11402 if (dump_file && (dump_flags & TDF_DETAILS))
11403 {
11404 fprintf (dump_file, "Failed to expand as dwarf: ");
11405 if (expr)
11406 print_generic_expr (dump_file, expr, dump_flags);
11407 if (rtl)
11408 {
11409 fprintf (dump_file, "\n");
11410 print_rtl (dump_file, rtl);
11411 }
11412 fprintf (dump_file, "\nReason: %s\n", reason);
11413 }
11414 }
11415
11416 /* Helper function for const_ok_for_output, called either directly
11417 or via for_each_rtx. */
11418
11419 static int
11420 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11421 {
11422 rtx rtl = *rtlp;
11423
11424 if (GET_CODE (rtl) == UNSPEC)
11425 {
11426 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11427 we can't express it in the debug info. */
11428 #ifdef ENABLE_CHECKING
11429 /* Don't complain about TLS UNSPECs, those are just too hard to
11430 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11431 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11432 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11433 if (XVECLEN (rtl, 0) == 0
11434 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11435 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11436 inform (current_function_decl
11437 ? DECL_SOURCE_LOCATION (current_function_decl)
11438 : UNKNOWN_LOCATION,
11439 #if NUM_UNSPEC_VALUES > 0
11440 "non-delegitimized UNSPEC %s (%d) found in variable location",
11441 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11442 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11443 XINT (rtl, 1));
11444 #else
11445 "non-delegitimized UNSPEC %d found in variable location",
11446 XINT (rtl, 1));
11447 #endif
11448 #endif
11449 expansion_failed (NULL_TREE, rtl,
11450 "UNSPEC hasn't been delegitimized.\n");
11451 return 1;
11452 }
11453
11454 if (targetm.const_not_ok_for_debug_p (rtl))
11455 {
11456 expansion_failed (NULL_TREE, rtl,
11457 "Expression rejected for debug by the backend.\n");
11458 return 1;
11459 }
11460
11461 /* FIXME: Refer to PR60655. It is possible for simplification
11462 of rtl expressions in var tracking to produce such expressions.
11463 We should really identify / validate expressions
11464 enclosed in CONST that can be handled by assemblers on various
11465 targets and only handle legitimate cases here. */
11466 if (GET_CODE (rtl) != SYMBOL_REF)
11467 {
11468 if (GET_CODE (rtl) == NOT)
11469 return 1;
11470
11471 return 0;
11472 }
11473
11474 if (CONSTANT_POOL_ADDRESS_P (rtl))
11475 {
11476 bool marked;
11477 get_pool_constant_mark (rtl, &marked);
11478 /* If all references to this pool constant were optimized away,
11479 it was not output and thus we can't represent it. */
11480 if (!marked)
11481 {
11482 expansion_failed (NULL_TREE, rtl,
11483 "Constant was removed from constant pool.\n");
11484 return 1;
11485 }
11486 }
11487
11488 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11489 return 1;
11490
11491 /* Avoid references to external symbols in debug info, on several targets
11492 the linker might even refuse to link when linking a shared library,
11493 and in many other cases the relocations for .debug_info/.debug_loc are
11494 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11495 to be defined within the same shared library or executable are fine. */
11496 if (SYMBOL_REF_EXTERNAL_P (rtl))
11497 {
11498 tree decl = SYMBOL_REF_DECL (rtl);
11499
11500 if (decl == NULL || !targetm.binds_local_p (decl))
11501 {
11502 expansion_failed (NULL_TREE, rtl,
11503 "Symbol not defined in current TU.\n");
11504 return 1;
11505 }
11506 }
11507
11508 return 0;
11509 }
11510
11511 /* Return true if constant RTL can be emitted in DW_OP_addr or
11512 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11513 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11514
11515 static bool
11516 const_ok_for_output (rtx rtl)
11517 {
11518 if (GET_CODE (rtl) == SYMBOL_REF)
11519 return const_ok_for_output_1 (&rtl, NULL) == 0;
11520
11521 if (GET_CODE (rtl) == CONST)
11522 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11523
11524 return true;
11525 }
11526
11527 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11528 if possible, NULL otherwise. */
11529
11530 static dw_die_ref
11531 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11532 {
11533 dw_die_ref type_die;
11534 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11535
11536 if (type == NULL)
11537 return NULL;
11538 switch (TREE_CODE (type))
11539 {
11540 case INTEGER_TYPE:
11541 case REAL_TYPE:
11542 break;
11543 default:
11544 return NULL;
11545 }
11546 type_die = lookup_type_die (type);
11547 if (!type_die)
11548 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11549 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11550 return NULL;
11551 return type_die;
11552 }
11553
11554 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11555 type matching MODE, or, if MODE is narrower than or as wide as
11556 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11557 possible. */
11558
11559 static dw_loc_descr_ref
11560 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11561 {
11562 enum machine_mode outer_mode = mode;
11563 dw_die_ref type_die;
11564 dw_loc_descr_ref cvt;
11565
11566 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11567 {
11568 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11569 return op;
11570 }
11571 type_die = base_type_for_mode (outer_mode, 1);
11572 if (type_die == NULL)
11573 return NULL;
11574 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11575 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11576 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11577 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11578 add_loc_descr (&op, cvt);
11579 return op;
11580 }
11581
11582 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11583
11584 static dw_loc_descr_ref
11585 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11586 dw_loc_descr_ref op1)
11587 {
11588 dw_loc_descr_ref ret = op0;
11589 add_loc_descr (&ret, op1);
11590 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11591 if (STORE_FLAG_VALUE != 1)
11592 {
11593 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11594 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11595 }
11596 return ret;
11597 }
11598
11599 /* Return location descriptor for signed comparison OP RTL. */
11600
11601 static dw_loc_descr_ref
11602 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11603 enum machine_mode mem_mode)
11604 {
11605 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11606 dw_loc_descr_ref op0, op1;
11607 int shift;
11608
11609 if (op_mode == VOIDmode)
11610 op_mode = GET_MODE (XEXP (rtl, 1));
11611 if (op_mode == VOIDmode)
11612 return NULL;
11613
11614 if (dwarf_strict
11615 && (GET_MODE_CLASS (op_mode) != MODE_INT
11616 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11617 return NULL;
11618
11619 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11620 VAR_INIT_STATUS_INITIALIZED);
11621 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11622 VAR_INIT_STATUS_INITIALIZED);
11623
11624 if (op0 == NULL || op1 == NULL)
11625 return NULL;
11626
11627 if (GET_MODE_CLASS (op_mode) != MODE_INT
11628 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11629 return compare_loc_descriptor (op, op0, op1);
11630
11631 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11632 {
11633 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11634 dw_loc_descr_ref cvt;
11635
11636 if (type_die == NULL)
11637 return NULL;
11638 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11639 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11640 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11641 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11642 add_loc_descr (&op0, cvt);
11643 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11644 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11645 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11646 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11647 add_loc_descr (&op1, cvt);
11648 return compare_loc_descriptor (op, op0, op1);
11649 }
11650
11651 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11652 /* For eq/ne, if the operands are known to be zero-extended,
11653 there is no need to do the fancy shifting up. */
11654 if (op == DW_OP_eq || op == DW_OP_ne)
11655 {
11656 dw_loc_descr_ref last0, last1;
11657 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11658 ;
11659 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11660 ;
11661 /* deref_size zero extends, and for constants we can check
11662 whether they are zero extended or not. */
11663 if (((last0->dw_loc_opc == DW_OP_deref_size
11664 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11665 || (CONST_INT_P (XEXP (rtl, 0))
11666 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11667 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11668 && ((last1->dw_loc_opc == DW_OP_deref_size
11669 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11670 || (CONST_INT_P (XEXP (rtl, 1))
11671 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11672 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11673 return compare_loc_descriptor (op, op0, op1);
11674
11675 /* EQ/NE comparison against constant in narrower type than
11676 DWARF2_ADDR_SIZE can be performed either as
11677 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11678 DW_OP_{eq,ne}
11679 or
11680 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11681 DW_OP_{eq,ne}. Pick whatever is shorter. */
11682 if (CONST_INT_P (XEXP (rtl, 1))
11683 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11684 && (size_of_int_loc_descriptor (shift) + 1
11685 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11686 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11687 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11688 & GET_MODE_MASK (op_mode))))
11689 {
11690 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11691 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11692 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11693 & GET_MODE_MASK (op_mode));
11694 return compare_loc_descriptor (op, op0, op1);
11695 }
11696 }
11697 add_loc_descr (&op0, int_loc_descriptor (shift));
11698 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11699 if (CONST_INT_P (XEXP (rtl, 1)))
11700 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11701 else
11702 {
11703 add_loc_descr (&op1, int_loc_descriptor (shift));
11704 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11705 }
11706 return compare_loc_descriptor (op, op0, op1);
11707 }
11708
11709 /* Return location descriptor for unsigned comparison OP RTL. */
11710
11711 static dw_loc_descr_ref
11712 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11713 enum machine_mode mem_mode)
11714 {
11715 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11716 dw_loc_descr_ref op0, op1;
11717
11718 if (op_mode == VOIDmode)
11719 op_mode = GET_MODE (XEXP (rtl, 1));
11720 if (op_mode == VOIDmode)
11721 return NULL;
11722 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11723 return NULL;
11724
11725 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11726 return NULL;
11727
11728 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11729 VAR_INIT_STATUS_INITIALIZED);
11730 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11731 VAR_INIT_STATUS_INITIALIZED);
11732
11733 if (op0 == NULL || op1 == NULL)
11734 return NULL;
11735
11736 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11737 {
11738 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11739 dw_loc_descr_ref last0, last1;
11740 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11741 ;
11742 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11743 ;
11744 if (CONST_INT_P (XEXP (rtl, 0)))
11745 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11746 /* deref_size zero extends, so no need to mask it again. */
11747 else if (last0->dw_loc_opc != DW_OP_deref_size
11748 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11749 {
11750 add_loc_descr (&op0, int_loc_descriptor (mask));
11751 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11752 }
11753 if (CONST_INT_P (XEXP (rtl, 1)))
11754 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11755 /* deref_size zero extends, so no need to mask it again. */
11756 else if (last1->dw_loc_opc != DW_OP_deref_size
11757 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11758 {
11759 add_loc_descr (&op1, int_loc_descriptor (mask));
11760 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11761 }
11762 }
11763 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11764 {
11765 HOST_WIDE_INT bias = 1;
11766 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11767 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11768 if (CONST_INT_P (XEXP (rtl, 1)))
11769 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11770 + INTVAL (XEXP (rtl, 1)));
11771 else
11772 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11773 bias, 0));
11774 }
11775 return compare_loc_descriptor (op, op0, op1);
11776 }
11777
11778 /* Return location descriptor for {U,S}{MIN,MAX}. */
11779
11780 static dw_loc_descr_ref
11781 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11782 enum machine_mode mem_mode)
11783 {
11784 enum dwarf_location_atom op;
11785 dw_loc_descr_ref op0, op1, ret;
11786 dw_loc_descr_ref bra_node, drop_node;
11787
11788 if (dwarf_strict
11789 && (GET_MODE_CLASS (mode) != MODE_INT
11790 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11791 return NULL;
11792
11793 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11794 VAR_INIT_STATUS_INITIALIZED);
11795 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11796 VAR_INIT_STATUS_INITIALIZED);
11797
11798 if (op0 == NULL || op1 == NULL)
11799 return NULL;
11800
11801 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11802 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11803 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11804 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11805 {
11806 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11807 {
11808 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11809 add_loc_descr (&op0, int_loc_descriptor (mask));
11810 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11811 add_loc_descr (&op1, int_loc_descriptor (mask));
11812 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11813 }
11814 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11815 {
11816 HOST_WIDE_INT bias = 1;
11817 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11818 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11819 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11820 }
11821 }
11822 else if (GET_MODE_CLASS (mode) == MODE_INT
11823 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11824 {
11825 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11826 add_loc_descr (&op0, int_loc_descriptor (shift));
11827 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11828 add_loc_descr (&op1, int_loc_descriptor (shift));
11829 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11830 }
11831 else if (GET_MODE_CLASS (mode) == MODE_INT
11832 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11833 {
11834 dw_die_ref type_die = base_type_for_mode (mode, 0);
11835 dw_loc_descr_ref cvt;
11836 if (type_die == NULL)
11837 return NULL;
11838 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11839 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11840 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11841 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11842 add_loc_descr (&op0, cvt);
11843 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11844 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11845 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11846 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11847 add_loc_descr (&op1, cvt);
11848 }
11849
11850 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11851 op = DW_OP_lt;
11852 else
11853 op = DW_OP_gt;
11854 ret = op0;
11855 add_loc_descr (&ret, op1);
11856 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11857 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11858 add_loc_descr (&ret, bra_node);
11859 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11860 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11861 add_loc_descr (&ret, drop_node);
11862 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11863 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11864 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11865 && GET_MODE_CLASS (mode) == MODE_INT
11866 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11867 ret = convert_descriptor_to_mode (mode, ret);
11868 return ret;
11869 }
11870
11871 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11872 but after converting arguments to type_die, afterwards
11873 convert back to unsigned. */
11874
11875 static dw_loc_descr_ref
11876 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11877 enum machine_mode mode, enum machine_mode mem_mode)
11878 {
11879 dw_loc_descr_ref cvt, op0, op1;
11880
11881 if (type_die == NULL)
11882 return NULL;
11883 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11884 VAR_INIT_STATUS_INITIALIZED);
11885 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11886 VAR_INIT_STATUS_INITIALIZED);
11887 if (op0 == NULL || op1 == NULL)
11888 return NULL;
11889 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11890 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11891 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11892 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11893 add_loc_descr (&op0, cvt);
11894 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11895 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11896 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11897 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11898 add_loc_descr (&op1, cvt);
11899 add_loc_descr (&op0, op1);
11900 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11901 return convert_descriptor_to_mode (mode, op0);
11902 }
11903
11904 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11905 const0 is DW_OP_lit0 or corresponding typed constant,
11906 const1 is DW_OP_lit1 or corresponding typed constant
11907 and constMSB is constant with just the MSB bit set
11908 for the mode):
11909 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11910 L1: const0 DW_OP_swap
11911 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11912 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11913 L3: DW_OP_drop
11914 L4: DW_OP_nop
11915
11916 CTZ is similar:
11917 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11918 L1: const0 DW_OP_swap
11919 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11920 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11921 L3: DW_OP_drop
11922 L4: DW_OP_nop
11923
11924 FFS is similar:
11925 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11926 L1: const1 DW_OP_swap
11927 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11928 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11929 L3: DW_OP_drop
11930 L4: DW_OP_nop */
11931
11932 static dw_loc_descr_ref
11933 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11934 enum machine_mode mem_mode)
11935 {
11936 dw_loc_descr_ref op0, ret, tmp;
11937 HOST_WIDE_INT valv;
11938 dw_loc_descr_ref l1jump, l1label;
11939 dw_loc_descr_ref l2jump, l2label;
11940 dw_loc_descr_ref l3jump, l3label;
11941 dw_loc_descr_ref l4jump, l4label;
11942 rtx msb;
11943
11944 if (GET_MODE_CLASS (mode) != MODE_INT
11945 || GET_MODE (XEXP (rtl, 0)) != mode)
11946 return NULL;
11947
11948 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11949 VAR_INIT_STATUS_INITIALIZED);
11950 if (op0 == NULL)
11951 return NULL;
11952 ret = op0;
11953 if (GET_CODE (rtl) == CLZ)
11954 {
11955 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11956 valv = GET_MODE_BITSIZE (mode);
11957 }
11958 else if (GET_CODE (rtl) == FFS)
11959 valv = 0;
11960 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11961 valv = GET_MODE_BITSIZE (mode);
11962 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11963 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11964 add_loc_descr (&ret, l1jump);
11965 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11966 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11967 VAR_INIT_STATUS_INITIALIZED);
11968 if (tmp == NULL)
11969 return NULL;
11970 add_loc_descr (&ret, tmp);
11971 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11972 add_loc_descr (&ret, l4jump);
11973 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11974 ? const1_rtx : const0_rtx,
11975 mode, mem_mode,
11976 VAR_INIT_STATUS_INITIALIZED);
11977 if (l1label == NULL)
11978 return NULL;
11979 add_loc_descr (&ret, l1label);
11980 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11981 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11982 add_loc_descr (&ret, l2label);
11983 if (GET_CODE (rtl) != CLZ)
11984 msb = const1_rtx;
11985 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11986 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11987 << (GET_MODE_BITSIZE (mode) - 1));
11988 else
11989 msb = immed_wide_int_const
11990 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
11991 GET_MODE_PRECISION (mode)), mode);
11992 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11993 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11994 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11995 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11996 else
11997 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11998 VAR_INIT_STATUS_INITIALIZED);
11999 if (tmp == NULL)
12000 return NULL;
12001 add_loc_descr (&ret, tmp);
12002 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12003 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12004 add_loc_descr (&ret, l3jump);
12005 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12006 VAR_INIT_STATUS_INITIALIZED);
12007 if (tmp == NULL)
12008 return NULL;
12009 add_loc_descr (&ret, tmp);
12010 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12011 ? DW_OP_shl : DW_OP_shr, 0, 0));
12012 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12013 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12014 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12015 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12016 add_loc_descr (&ret, l2jump);
12017 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12018 add_loc_descr (&ret, l3label);
12019 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12020 add_loc_descr (&ret, l4label);
12021 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12022 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12023 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12024 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12025 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12026 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12027 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12028 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12029 return ret;
12030 }
12031
12032 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12033 const1 is DW_OP_lit1 or corresponding typed constant):
12034 const0 DW_OP_swap
12035 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12036 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12037 L2: DW_OP_drop
12038
12039 PARITY is similar:
12040 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12041 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12042 L2: DW_OP_drop */
12043
12044 static dw_loc_descr_ref
12045 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
12046 enum machine_mode mem_mode)
12047 {
12048 dw_loc_descr_ref op0, ret, tmp;
12049 dw_loc_descr_ref l1jump, l1label;
12050 dw_loc_descr_ref l2jump, l2label;
12051
12052 if (GET_MODE_CLASS (mode) != MODE_INT
12053 || GET_MODE (XEXP (rtl, 0)) != mode)
12054 return NULL;
12055
12056 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12057 VAR_INIT_STATUS_INITIALIZED);
12058 if (op0 == NULL)
12059 return NULL;
12060 ret = op0;
12061 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12062 VAR_INIT_STATUS_INITIALIZED);
12063 if (tmp == NULL)
12064 return NULL;
12065 add_loc_descr (&ret, tmp);
12066 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12067 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12068 add_loc_descr (&ret, l1label);
12069 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12070 add_loc_descr (&ret, l2jump);
12071 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12072 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12073 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12074 VAR_INIT_STATUS_INITIALIZED);
12075 if (tmp == NULL)
12076 return NULL;
12077 add_loc_descr (&ret, tmp);
12078 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12079 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12080 ? DW_OP_plus : DW_OP_xor, 0, 0));
12081 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12082 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12083 VAR_INIT_STATUS_INITIALIZED);
12084 add_loc_descr (&ret, tmp);
12085 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12086 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12087 add_loc_descr (&ret, l1jump);
12088 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12089 add_loc_descr (&ret, l2label);
12090 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12091 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12092 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12093 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12094 return ret;
12095 }
12096
12097 /* BSWAP (constS is initial shift count, either 56 or 24):
12098 constS const0
12099 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12100 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12101 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12102 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12103 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12104
12105 static dw_loc_descr_ref
12106 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
12107 enum machine_mode mem_mode)
12108 {
12109 dw_loc_descr_ref op0, ret, tmp;
12110 dw_loc_descr_ref l1jump, l1label;
12111 dw_loc_descr_ref l2jump, l2label;
12112
12113 if (GET_MODE_CLASS (mode) != MODE_INT
12114 || BITS_PER_UNIT != 8
12115 || (GET_MODE_BITSIZE (mode) != 32
12116 && GET_MODE_BITSIZE (mode) != 64))
12117 return NULL;
12118
12119 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12120 VAR_INIT_STATUS_INITIALIZED);
12121 if (op0 == NULL)
12122 return NULL;
12123
12124 ret = op0;
12125 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12126 mode, mem_mode,
12127 VAR_INIT_STATUS_INITIALIZED);
12128 if (tmp == NULL)
12129 return NULL;
12130 add_loc_descr (&ret, tmp);
12131 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12132 VAR_INIT_STATUS_INITIALIZED);
12133 if (tmp == NULL)
12134 return NULL;
12135 add_loc_descr (&ret, tmp);
12136 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12137 add_loc_descr (&ret, l1label);
12138 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12139 mode, mem_mode,
12140 VAR_INIT_STATUS_INITIALIZED);
12141 add_loc_descr (&ret, tmp);
12142 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12143 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12144 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12145 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12146 VAR_INIT_STATUS_INITIALIZED);
12147 if (tmp == NULL)
12148 return NULL;
12149 add_loc_descr (&ret, tmp);
12150 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12151 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12152 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12153 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12154 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12155 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12156 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12157 VAR_INIT_STATUS_INITIALIZED);
12158 add_loc_descr (&ret, tmp);
12159 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12160 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12161 add_loc_descr (&ret, l2jump);
12162 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12163 VAR_INIT_STATUS_INITIALIZED);
12164 add_loc_descr (&ret, tmp);
12165 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12166 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12167 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12168 add_loc_descr (&ret, l1jump);
12169 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12170 add_loc_descr (&ret, l2label);
12171 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12172 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12173 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12174 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12175 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12176 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12177 return ret;
12178 }
12179
12180 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12181 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12182 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12183 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12184
12185 ROTATERT is similar:
12186 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12187 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12188 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12189
12190 static dw_loc_descr_ref
12191 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12192 enum machine_mode mem_mode)
12193 {
12194 rtx rtlop1 = XEXP (rtl, 1);
12195 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12196 int i;
12197
12198 if (GET_MODE_CLASS (mode) != MODE_INT)
12199 return NULL;
12200
12201 if (GET_MODE (rtlop1) != VOIDmode
12202 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12203 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12204 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12205 VAR_INIT_STATUS_INITIALIZED);
12206 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12207 VAR_INIT_STATUS_INITIALIZED);
12208 if (op0 == NULL || op1 == NULL)
12209 return NULL;
12210 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12211 for (i = 0; i < 2; i++)
12212 {
12213 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12214 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12215 mode, mem_mode,
12216 VAR_INIT_STATUS_INITIALIZED);
12217 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12218 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12219 ? DW_OP_const4u
12220 : HOST_BITS_PER_WIDE_INT == 64
12221 ? DW_OP_const8u : DW_OP_constu,
12222 GET_MODE_MASK (mode), 0);
12223 else
12224 mask[i] = NULL;
12225 if (mask[i] == NULL)
12226 return NULL;
12227 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12228 }
12229 ret = op0;
12230 add_loc_descr (&ret, op1);
12231 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12232 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12233 if (GET_CODE (rtl) == ROTATERT)
12234 {
12235 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12236 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12237 GET_MODE_BITSIZE (mode), 0));
12238 }
12239 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12240 if (mask[0] != NULL)
12241 add_loc_descr (&ret, mask[0]);
12242 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12243 if (mask[1] != NULL)
12244 {
12245 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12246 add_loc_descr (&ret, mask[1]);
12247 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12248 }
12249 if (GET_CODE (rtl) == ROTATE)
12250 {
12251 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12252 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12253 GET_MODE_BITSIZE (mode), 0));
12254 }
12255 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12256 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12257 return ret;
12258 }
12259
12260 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12261 for DEBUG_PARAMETER_REF RTL. */
12262
12263 static dw_loc_descr_ref
12264 parameter_ref_descriptor (rtx rtl)
12265 {
12266 dw_loc_descr_ref ret;
12267 dw_die_ref ref;
12268
12269 if (dwarf_strict)
12270 return NULL;
12271 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12272 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12273 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12274 if (ref)
12275 {
12276 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12277 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12278 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12279 }
12280 else
12281 {
12282 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12283 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12284 }
12285 return ret;
12286 }
12287
12288 /* The following routine converts the RTL for a variable or parameter
12289 (resident in memory) into an equivalent Dwarf representation of a
12290 mechanism for getting the address of that same variable onto the top of a
12291 hypothetical "address evaluation" stack.
12292
12293 When creating memory location descriptors, we are effectively transforming
12294 the RTL for a memory-resident object into its Dwarf postfix expression
12295 equivalent. This routine recursively descends an RTL tree, turning
12296 it into Dwarf postfix code as it goes.
12297
12298 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12299
12300 MEM_MODE is the mode of the memory reference, needed to handle some
12301 autoincrement addressing modes.
12302
12303 Return 0 if we can't represent the location. */
12304
12305 dw_loc_descr_ref
12306 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12307 enum machine_mode mem_mode,
12308 enum var_init_status initialized)
12309 {
12310 dw_loc_descr_ref mem_loc_result = NULL;
12311 enum dwarf_location_atom op;
12312 dw_loc_descr_ref op0, op1;
12313 rtx inner = NULL_RTX;
12314
12315 if (mode == VOIDmode)
12316 mode = GET_MODE (rtl);
12317
12318 /* Note that for a dynamically sized array, the location we will generate a
12319 description of here will be the lowest numbered location which is
12320 actually within the array. That's *not* necessarily the same as the
12321 zeroth element of the array. */
12322
12323 rtl = targetm.delegitimize_address (rtl);
12324
12325 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12326 return NULL;
12327
12328 switch (GET_CODE (rtl))
12329 {
12330 case POST_INC:
12331 case POST_DEC:
12332 case POST_MODIFY:
12333 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12334
12335 case SUBREG:
12336 /* The case of a subreg may arise when we have a local (register)
12337 variable or a formal (register) parameter which doesn't quite fill
12338 up an entire register. For now, just assume that it is
12339 legitimate to make the Dwarf info refer to the whole register which
12340 contains the given subreg. */
12341 if (!subreg_lowpart_p (rtl))
12342 break;
12343 inner = SUBREG_REG (rtl);
12344 case TRUNCATE:
12345 if (inner == NULL_RTX)
12346 inner = XEXP (rtl, 0);
12347 if (GET_MODE_CLASS (mode) == MODE_INT
12348 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12349 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12350 #ifdef POINTERS_EXTEND_UNSIGNED
12351 || (mode == Pmode && mem_mode != VOIDmode)
12352 #endif
12353 )
12354 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12355 {
12356 mem_loc_result = mem_loc_descriptor (inner,
12357 GET_MODE (inner),
12358 mem_mode, initialized);
12359 break;
12360 }
12361 if (dwarf_strict)
12362 break;
12363 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12364 break;
12365 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12366 && (GET_MODE_CLASS (mode) != MODE_INT
12367 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12368 break;
12369 else
12370 {
12371 dw_die_ref type_die;
12372 dw_loc_descr_ref cvt;
12373
12374 mem_loc_result = mem_loc_descriptor (inner,
12375 GET_MODE (inner),
12376 mem_mode, initialized);
12377 if (mem_loc_result == NULL)
12378 break;
12379 type_die = base_type_for_mode (mode,
12380 GET_MODE_CLASS (mode) == MODE_INT);
12381 if (type_die == NULL)
12382 {
12383 mem_loc_result = NULL;
12384 break;
12385 }
12386 if (GET_MODE_SIZE (mode)
12387 != GET_MODE_SIZE (GET_MODE (inner)))
12388 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12389 else
12390 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12391 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12392 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12393 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12394 add_loc_descr (&mem_loc_result, cvt);
12395 }
12396 break;
12397
12398 case REG:
12399 if (GET_MODE_CLASS (mode) != MODE_INT
12400 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12401 && rtl != arg_pointer_rtx
12402 && rtl != frame_pointer_rtx
12403 #ifdef POINTERS_EXTEND_UNSIGNED
12404 && (mode != Pmode || mem_mode == VOIDmode)
12405 #endif
12406 ))
12407 {
12408 dw_die_ref type_die;
12409 unsigned int dbx_regnum;
12410
12411 if (dwarf_strict)
12412 break;
12413 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12414 break;
12415 type_die = base_type_for_mode (mode,
12416 GET_MODE_CLASS (mode) == MODE_INT);
12417 if (type_die == NULL)
12418 break;
12419
12420 dbx_regnum = dbx_reg_number (rtl);
12421 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12422 break;
12423 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12424 dbx_regnum, 0);
12425 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12426 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12427 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12428 break;
12429 }
12430 /* Whenever a register number forms a part of the description of the
12431 method for calculating the (dynamic) address of a memory resident
12432 object, DWARF rules require the register number be referred to as
12433 a "base register". This distinction is not based in any way upon
12434 what category of register the hardware believes the given register
12435 belongs to. This is strictly DWARF terminology we're dealing with
12436 here. Note that in cases where the location of a memory-resident
12437 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12438 OP_CONST (0)) the actual DWARF location descriptor that we generate
12439 may just be OP_BASEREG (basereg). This may look deceptively like
12440 the object in question was allocated to a register (rather than in
12441 memory) so DWARF consumers need to be aware of the subtle
12442 distinction between OP_REG and OP_BASEREG. */
12443 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12444 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12445 else if (stack_realign_drap
12446 && crtl->drap_reg
12447 && crtl->args.internal_arg_pointer == rtl
12448 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12449 {
12450 /* If RTL is internal_arg_pointer, which has been optimized
12451 out, use DRAP instead. */
12452 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12453 VAR_INIT_STATUS_INITIALIZED);
12454 }
12455 break;
12456
12457 case SIGN_EXTEND:
12458 case ZERO_EXTEND:
12459 if (GET_MODE_CLASS (mode) != MODE_INT)
12460 break;
12461 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12462 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12463 if (op0 == 0)
12464 break;
12465 else if (GET_CODE (rtl) == ZERO_EXTEND
12466 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12467 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12468 < HOST_BITS_PER_WIDE_INT
12469 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12470 to expand zero extend as two shifts instead of
12471 masking. */
12472 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12473 {
12474 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12475 mem_loc_result = op0;
12476 add_loc_descr (&mem_loc_result,
12477 int_loc_descriptor (GET_MODE_MASK (imode)));
12478 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12479 }
12480 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12481 {
12482 int shift = DWARF2_ADDR_SIZE
12483 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12484 shift *= BITS_PER_UNIT;
12485 if (GET_CODE (rtl) == SIGN_EXTEND)
12486 op = DW_OP_shra;
12487 else
12488 op = DW_OP_shr;
12489 mem_loc_result = op0;
12490 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12491 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12492 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12493 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12494 }
12495 else if (!dwarf_strict)
12496 {
12497 dw_die_ref type_die1, type_die2;
12498 dw_loc_descr_ref cvt;
12499
12500 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12501 GET_CODE (rtl) == ZERO_EXTEND);
12502 if (type_die1 == NULL)
12503 break;
12504 type_die2 = base_type_for_mode (mode, 1);
12505 if (type_die2 == NULL)
12506 break;
12507 mem_loc_result = op0;
12508 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12509 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12510 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12511 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12512 add_loc_descr (&mem_loc_result, cvt);
12513 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12514 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12515 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12516 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12517 add_loc_descr (&mem_loc_result, cvt);
12518 }
12519 break;
12520
12521 case MEM:
12522 {
12523 rtx new_rtl = avoid_constant_pool_reference (rtl);
12524 if (new_rtl != rtl)
12525 {
12526 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12527 initialized);
12528 if (mem_loc_result != NULL)
12529 return mem_loc_result;
12530 }
12531 }
12532 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12533 get_address_mode (rtl), mode,
12534 VAR_INIT_STATUS_INITIALIZED);
12535 if (mem_loc_result == NULL)
12536 mem_loc_result = tls_mem_loc_descriptor (rtl);
12537 if (mem_loc_result != NULL)
12538 {
12539 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12540 || GET_MODE_CLASS (mode) != MODE_INT)
12541 {
12542 dw_die_ref type_die;
12543 dw_loc_descr_ref deref;
12544
12545 if (dwarf_strict)
12546 return NULL;
12547 type_die
12548 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12549 if (type_die == NULL)
12550 return NULL;
12551 deref = new_loc_descr (DW_OP_GNU_deref_type,
12552 GET_MODE_SIZE (mode), 0);
12553 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12554 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12555 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12556 add_loc_descr (&mem_loc_result, deref);
12557 }
12558 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12559 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12560 else
12561 add_loc_descr (&mem_loc_result,
12562 new_loc_descr (DW_OP_deref_size,
12563 GET_MODE_SIZE (mode), 0));
12564 }
12565 break;
12566
12567 case LO_SUM:
12568 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12569
12570 case LABEL_REF:
12571 /* Some ports can transform a symbol ref into a label ref, because
12572 the symbol ref is too far away and has to be dumped into a constant
12573 pool. */
12574 case CONST:
12575 case SYMBOL_REF:
12576 if (GET_MODE_CLASS (mode) != MODE_INT
12577 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12578 #ifdef POINTERS_EXTEND_UNSIGNED
12579 && (mode != Pmode || mem_mode == VOIDmode)
12580 #endif
12581 ))
12582 break;
12583 if (GET_CODE (rtl) == SYMBOL_REF
12584 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12585 {
12586 dw_loc_descr_ref temp;
12587
12588 /* If this is not defined, we have no way to emit the data. */
12589 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12590 break;
12591
12592 temp = new_addr_loc_descr (rtl, dtprel_true);
12593
12594 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12595 add_loc_descr (&mem_loc_result, temp);
12596
12597 break;
12598 }
12599
12600 if (!const_ok_for_output (rtl))
12601 break;
12602
12603 symref:
12604 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12605 vec_safe_push (used_rtx_array, rtl);
12606 break;
12607
12608 case CONCAT:
12609 case CONCATN:
12610 case VAR_LOCATION:
12611 case DEBUG_IMPLICIT_PTR:
12612 expansion_failed (NULL_TREE, rtl,
12613 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12614 return 0;
12615
12616 case ENTRY_VALUE:
12617 if (dwarf_strict)
12618 return NULL;
12619 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12620 {
12621 if (GET_MODE_CLASS (mode) != MODE_INT
12622 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12623 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12624 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12625 else
12626 {
12627 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12628 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12629 return NULL;
12630 op0 = one_reg_loc_descriptor (dbx_regnum,
12631 VAR_INIT_STATUS_INITIALIZED);
12632 }
12633 }
12634 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12635 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12636 {
12637 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12638 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12639 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12640 return NULL;
12641 }
12642 else
12643 gcc_unreachable ();
12644 if (op0 == NULL)
12645 return NULL;
12646 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12647 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12648 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12649 break;
12650
12651 case DEBUG_PARAMETER_REF:
12652 mem_loc_result = parameter_ref_descriptor (rtl);
12653 break;
12654
12655 case PRE_MODIFY:
12656 /* Extract the PLUS expression nested inside and fall into
12657 PLUS code below. */
12658 rtl = XEXP (rtl, 1);
12659 goto plus;
12660
12661 case PRE_INC:
12662 case PRE_DEC:
12663 /* Turn these into a PLUS expression and fall into the PLUS code
12664 below. */
12665 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12666 gen_int_mode (GET_CODE (rtl) == PRE_INC
12667 ? GET_MODE_UNIT_SIZE (mem_mode)
12668 : -GET_MODE_UNIT_SIZE (mem_mode),
12669 mode));
12670
12671 /* ... fall through ... */
12672
12673 case PLUS:
12674 plus:
12675 if (is_based_loc (rtl)
12676 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12677 || XEXP (rtl, 0) == arg_pointer_rtx
12678 || XEXP (rtl, 0) == frame_pointer_rtx)
12679 && GET_MODE_CLASS (mode) == MODE_INT)
12680 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12681 INTVAL (XEXP (rtl, 1)),
12682 VAR_INIT_STATUS_INITIALIZED);
12683 else
12684 {
12685 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12686 VAR_INIT_STATUS_INITIALIZED);
12687 if (mem_loc_result == 0)
12688 break;
12689
12690 if (CONST_INT_P (XEXP (rtl, 1))
12691 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12692 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12693 else
12694 {
12695 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12696 VAR_INIT_STATUS_INITIALIZED);
12697 if (op1 == 0)
12698 break;
12699 add_loc_descr (&mem_loc_result, op1);
12700 add_loc_descr (&mem_loc_result,
12701 new_loc_descr (DW_OP_plus, 0, 0));
12702 }
12703 }
12704 break;
12705
12706 /* If a pseudo-reg is optimized away, it is possible for it to
12707 be replaced with a MEM containing a multiply or shift. */
12708 case MINUS:
12709 op = DW_OP_minus;
12710 goto do_binop;
12711
12712 case MULT:
12713 op = DW_OP_mul;
12714 goto do_binop;
12715
12716 case DIV:
12717 if (!dwarf_strict
12718 && GET_MODE_CLASS (mode) == MODE_INT
12719 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12720 {
12721 mem_loc_result = typed_binop (DW_OP_div, rtl,
12722 base_type_for_mode (mode, 0),
12723 mode, mem_mode);
12724 break;
12725 }
12726 op = DW_OP_div;
12727 goto do_binop;
12728
12729 case UMOD:
12730 op = DW_OP_mod;
12731 goto do_binop;
12732
12733 case ASHIFT:
12734 op = DW_OP_shl;
12735 goto do_shift;
12736
12737 case ASHIFTRT:
12738 op = DW_OP_shra;
12739 goto do_shift;
12740
12741 case LSHIFTRT:
12742 op = DW_OP_shr;
12743 goto do_shift;
12744
12745 do_shift:
12746 if (GET_MODE_CLASS (mode) != MODE_INT)
12747 break;
12748 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12749 VAR_INIT_STATUS_INITIALIZED);
12750 {
12751 rtx rtlop1 = XEXP (rtl, 1);
12752 if (GET_MODE (rtlop1) != VOIDmode
12753 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12754 < GET_MODE_BITSIZE (mode))
12755 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12756 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12757 VAR_INIT_STATUS_INITIALIZED);
12758 }
12759
12760 if (op0 == 0 || op1 == 0)
12761 break;
12762
12763 mem_loc_result = op0;
12764 add_loc_descr (&mem_loc_result, op1);
12765 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12766 break;
12767
12768 case AND:
12769 op = DW_OP_and;
12770 goto do_binop;
12771
12772 case IOR:
12773 op = DW_OP_or;
12774 goto do_binop;
12775
12776 case XOR:
12777 op = DW_OP_xor;
12778 goto do_binop;
12779
12780 do_binop:
12781 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12782 VAR_INIT_STATUS_INITIALIZED);
12783 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12784 VAR_INIT_STATUS_INITIALIZED);
12785
12786 if (op0 == 0 || op1 == 0)
12787 break;
12788
12789 mem_loc_result = op0;
12790 add_loc_descr (&mem_loc_result, op1);
12791 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12792 break;
12793
12794 case MOD:
12795 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12796 {
12797 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12798 base_type_for_mode (mode, 0),
12799 mode, mem_mode);
12800 break;
12801 }
12802
12803 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12804 VAR_INIT_STATUS_INITIALIZED);
12805 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12806 VAR_INIT_STATUS_INITIALIZED);
12807
12808 if (op0 == 0 || op1 == 0)
12809 break;
12810
12811 mem_loc_result = op0;
12812 add_loc_descr (&mem_loc_result, op1);
12813 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12814 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12815 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12816 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12817 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12818 break;
12819
12820 case UDIV:
12821 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12822 {
12823 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12824 {
12825 op = DW_OP_div;
12826 goto do_binop;
12827 }
12828 mem_loc_result = typed_binop (DW_OP_div, rtl,
12829 base_type_for_mode (mode, 1),
12830 mode, mem_mode);
12831 }
12832 break;
12833
12834 case NOT:
12835 op = DW_OP_not;
12836 goto do_unop;
12837
12838 case ABS:
12839 op = DW_OP_abs;
12840 goto do_unop;
12841
12842 case NEG:
12843 op = DW_OP_neg;
12844 goto do_unop;
12845
12846 do_unop:
12847 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12848 VAR_INIT_STATUS_INITIALIZED);
12849
12850 if (op0 == 0)
12851 break;
12852
12853 mem_loc_result = op0;
12854 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12855 break;
12856
12857 case CONST_INT:
12858 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12859 #ifdef POINTERS_EXTEND_UNSIGNED
12860 || (mode == Pmode
12861 && mem_mode != VOIDmode
12862 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12863 #endif
12864 )
12865 {
12866 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12867 break;
12868 }
12869 if (!dwarf_strict
12870 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12871 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12872 {
12873 dw_die_ref type_die = base_type_for_mode (mode, 1);
12874 enum machine_mode amode;
12875 if (type_die == NULL)
12876 return NULL;
12877 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12878 MODE_INT, 0);
12879 if (INTVAL (rtl) >= 0
12880 && amode != BLKmode
12881 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12882 /* const DW_OP_GNU_convert <XXX> vs.
12883 DW_OP_GNU_const_type <XXX, 1, const>. */
12884 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12885 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12886 {
12887 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12888 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12889 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12890 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12891 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12892 add_loc_descr (&mem_loc_result, op0);
12893 return mem_loc_result;
12894 }
12895 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12896 INTVAL (rtl));
12897 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12898 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12899 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12900 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12901 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12902 else
12903 {
12904 mem_loc_result->dw_loc_oprnd2.val_class
12905 = dw_val_class_const_double;
12906 mem_loc_result->dw_loc_oprnd2.v.val_double
12907 = double_int::from_shwi (INTVAL (rtl));
12908 }
12909 }
12910 break;
12911
12912 case CONST_DOUBLE:
12913 if (!dwarf_strict)
12914 {
12915 dw_die_ref type_die;
12916
12917 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
12918 CONST_DOUBLE rtx could represent either a large integer
12919 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
12920 the value is always a floating point constant.
12921
12922 When it is an integer, a CONST_DOUBLE is used whenever
12923 the constant requires 2 HWIs to be adequately represented.
12924 We output CONST_DOUBLEs as blocks. */
12925 if (mode == VOIDmode
12926 || (GET_MODE (rtl) == VOIDmode
12927 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12928 break;
12929 type_die = base_type_for_mode (mode,
12930 GET_MODE_CLASS (mode) == MODE_INT);
12931 if (type_die == NULL)
12932 return NULL;
12933 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12934 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12935 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12936 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12937 #if TARGET_SUPPORTS_WIDE_INT == 0
12938 if (!SCALAR_FLOAT_MODE_P (mode))
12939 {
12940 mem_loc_result->dw_loc_oprnd2.val_class
12941 = dw_val_class_const_double;
12942 mem_loc_result->dw_loc_oprnd2.v.val_double
12943 = rtx_to_double_int (rtl);
12944 }
12945 else
12946 #endif
12947 {
12948 unsigned int length = GET_MODE_SIZE (mode);
12949 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
12950
12951 insert_float (rtl, array);
12952 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12953 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12954 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12955 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12956 }
12957 }
12958 break;
12959
12960 case CONST_WIDE_INT:
12961 if (!dwarf_strict)
12962 {
12963 dw_die_ref type_die;
12964
12965 type_die = base_type_for_mode (mode,
12966 GET_MODE_CLASS (mode) == MODE_INT);
12967 if (type_die == NULL)
12968 return NULL;
12969 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12970 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12971 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12972 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12973 mem_loc_result->dw_loc_oprnd2.val_class
12974 = dw_val_class_wide_int;
12975 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
12976 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
12977 }
12978 break;
12979
12980 case EQ:
12981 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12982 break;
12983
12984 case GE:
12985 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12986 break;
12987
12988 case GT:
12989 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12990 break;
12991
12992 case LE:
12993 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12994 break;
12995
12996 case LT:
12997 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12998 break;
12999
13000 case NE:
13001 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13002 break;
13003
13004 case GEU:
13005 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13006 break;
13007
13008 case GTU:
13009 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13010 break;
13011
13012 case LEU:
13013 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13014 break;
13015
13016 case LTU:
13017 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13018 break;
13019
13020 case UMIN:
13021 case UMAX:
13022 if (GET_MODE_CLASS (mode) != MODE_INT)
13023 break;
13024 /* FALLTHRU */
13025 case SMIN:
13026 case SMAX:
13027 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13028 break;
13029
13030 case ZERO_EXTRACT:
13031 case SIGN_EXTRACT:
13032 if (CONST_INT_P (XEXP (rtl, 1))
13033 && CONST_INT_P (XEXP (rtl, 2))
13034 && ((unsigned) INTVAL (XEXP (rtl, 1))
13035 + (unsigned) INTVAL (XEXP (rtl, 2))
13036 <= GET_MODE_BITSIZE (mode))
13037 && GET_MODE_CLASS (mode) == MODE_INT
13038 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13039 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13040 {
13041 int shift, size;
13042 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13043 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13044 if (op0 == 0)
13045 break;
13046 if (GET_CODE (rtl) == SIGN_EXTRACT)
13047 op = DW_OP_shra;
13048 else
13049 op = DW_OP_shr;
13050 mem_loc_result = op0;
13051 size = INTVAL (XEXP (rtl, 1));
13052 shift = INTVAL (XEXP (rtl, 2));
13053 if (BITS_BIG_ENDIAN)
13054 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13055 - shift - size;
13056 if (shift + size != (int) DWARF2_ADDR_SIZE)
13057 {
13058 add_loc_descr (&mem_loc_result,
13059 int_loc_descriptor (DWARF2_ADDR_SIZE
13060 - shift - size));
13061 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13062 }
13063 if (size != (int) DWARF2_ADDR_SIZE)
13064 {
13065 add_loc_descr (&mem_loc_result,
13066 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13067 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13068 }
13069 }
13070 break;
13071
13072 case IF_THEN_ELSE:
13073 {
13074 dw_loc_descr_ref op2, bra_node, drop_node;
13075 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13076 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13077 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13078 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13079 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13080 VAR_INIT_STATUS_INITIALIZED);
13081 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13082 VAR_INIT_STATUS_INITIALIZED);
13083 if (op0 == NULL || op1 == NULL || op2 == NULL)
13084 break;
13085
13086 mem_loc_result = op1;
13087 add_loc_descr (&mem_loc_result, op2);
13088 add_loc_descr (&mem_loc_result, op0);
13089 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13090 add_loc_descr (&mem_loc_result, bra_node);
13091 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13092 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13093 add_loc_descr (&mem_loc_result, drop_node);
13094 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13095 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13096 }
13097 break;
13098
13099 case FLOAT_EXTEND:
13100 case FLOAT_TRUNCATE:
13101 case FLOAT:
13102 case UNSIGNED_FLOAT:
13103 case FIX:
13104 case UNSIGNED_FIX:
13105 if (!dwarf_strict)
13106 {
13107 dw_die_ref type_die;
13108 dw_loc_descr_ref cvt;
13109
13110 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13111 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13112 if (op0 == NULL)
13113 break;
13114 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13115 && (GET_CODE (rtl) == FLOAT
13116 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13117 <= DWARF2_ADDR_SIZE))
13118 {
13119 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13120 GET_CODE (rtl) == UNSIGNED_FLOAT);
13121 if (type_die == NULL)
13122 break;
13123 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13124 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13125 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13126 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13127 add_loc_descr (&op0, cvt);
13128 }
13129 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13130 if (type_die == NULL)
13131 break;
13132 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13133 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13134 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13135 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13136 add_loc_descr (&op0, cvt);
13137 if (GET_MODE_CLASS (mode) == MODE_INT
13138 && (GET_CODE (rtl) == FIX
13139 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13140 {
13141 op0 = convert_descriptor_to_mode (mode, op0);
13142 if (op0 == NULL)
13143 break;
13144 }
13145 mem_loc_result = op0;
13146 }
13147 break;
13148
13149 case CLZ:
13150 case CTZ:
13151 case FFS:
13152 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13153 break;
13154
13155 case POPCOUNT:
13156 case PARITY:
13157 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13158 break;
13159
13160 case BSWAP:
13161 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13162 break;
13163
13164 case ROTATE:
13165 case ROTATERT:
13166 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13167 break;
13168
13169 case COMPARE:
13170 /* In theory, we could implement the above. */
13171 /* DWARF cannot represent the unsigned compare operations
13172 natively. */
13173 case SS_MULT:
13174 case US_MULT:
13175 case SS_DIV:
13176 case US_DIV:
13177 case SS_PLUS:
13178 case US_PLUS:
13179 case SS_MINUS:
13180 case US_MINUS:
13181 case SS_NEG:
13182 case US_NEG:
13183 case SS_ABS:
13184 case SS_ASHIFT:
13185 case US_ASHIFT:
13186 case SS_TRUNCATE:
13187 case US_TRUNCATE:
13188 case UNORDERED:
13189 case ORDERED:
13190 case UNEQ:
13191 case UNGE:
13192 case UNGT:
13193 case UNLE:
13194 case UNLT:
13195 case LTGT:
13196 case FRACT_CONVERT:
13197 case UNSIGNED_FRACT_CONVERT:
13198 case SAT_FRACT:
13199 case UNSIGNED_SAT_FRACT:
13200 case SQRT:
13201 case ASM_OPERANDS:
13202 case VEC_MERGE:
13203 case VEC_SELECT:
13204 case VEC_CONCAT:
13205 case VEC_DUPLICATE:
13206 case UNSPEC:
13207 case HIGH:
13208 case FMA:
13209 case STRICT_LOW_PART:
13210 case CONST_VECTOR:
13211 case CONST_FIXED:
13212 case CLRSB:
13213 case CLOBBER:
13214 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13215 can't express it in the debug info. This can happen e.g. with some
13216 TLS UNSPECs. */
13217 break;
13218
13219 case CONST_STRING:
13220 resolve_one_addr (&rtl, NULL);
13221 goto symref;
13222
13223 default:
13224 #ifdef ENABLE_CHECKING
13225 print_rtl (stderr, rtl);
13226 gcc_unreachable ();
13227 #else
13228 break;
13229 #endif
13230 }
13231
13232 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13233 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13234
13235 return mem_loc_result;
13236 }
13237
13238 /* Return a descriptor that describes the concatenation of two locations.
13239 This is typically a complex variable. */
13240
13241 static dw_loc_descr_ref
13242 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13243 {
13244 dw_loc_descr_ref cc_loc_result = NULL;
13245 dw_loc_descr_ref x0_ref
13246 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13247 dw_loc_descr_ref x1_ref
13248 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13249
13250 if (x0_ref == 0 || x1_ref == 0)
13251 return 0;
13252
13253 cc_loc_result = x0_ref;
13254 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13255
13256 add_loc_descr (&cc_loc_result, x1_ref);
13257 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13258
13259 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13260 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13261
13262 return cc_loc_result;
13263 }
13264
13265 /* Return a descriptor that describes the concatenation of N
13266 locations. */
13267
13268 static dw_loc_descr_ref
13269 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13270 {
13271 unsigned int i;
13272 dw_loc_descr_ref cc_loc_result = NULL;
13273 unsigned int n = XVECLEN (concatn, 0);
13274
13275 for (i = 0; i < n; ++i)
13276 {
13277 dw_loc_descr_ref ref;
13278 rtx x = XVECEXP (concatn, 0, i);
13279
13280 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13281 if (ref == NULL)
13282 return NULL;
13283
13284 add_loc_descr (&cc_loc_result, ref);
13285 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13286 }
13287
13288 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13289 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13290
13291 return cc_loc_result;
13292 }
13293
13294 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13295 for DEBUG_IMPLICIT_PTR RTL. */
13296
13297 static dw_loc_descr_ref
13298 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13299 {
13300 dw_loc_descr_ref ret;
13301 dw_die_ref ref;
13302
13303 if (dwarf_strict)
13304 return NULL;
13305 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13306 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13307 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13308 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13309 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13310 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13311 if (ref)
13312 {
13313 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13314 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13315 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13316 }
13317 else
13318 {
13319 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13320 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13321 }
13322 return ret;
13323 }
13324
13325 /* Output a proper Dwarf location descriptor for a variable or parameter
13326 which is either allocated in a register or in a memory location. For a
13327 register, we just generate an OP_REG and the register number. For a
13328 memory location we provide a Dwarf postfix expression describing how to
13329 generate the (dynamic) address of the object onto the address stack.
13330
13331 MODE is mode of the decl if this loc_descriptor is going to be used in
13332 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13333 allowed, VOIDmode otherwise.
13334
13335 If we don't know how to describe it, return 0. */
13336
13337 static dw_loc_descr_ref
13338 loc_descriptor (rtx rtl, enum machine_mode mode,
13339 enum var_init_status initialized)
13340 {
13341 dw_loc_descr_ref loc_result = NULL;
13342
13343 switch (GET_CODE (rtl))
13344 {
13345 case SUBREG:
13346 /* The case of a subreg may arise when we have a local (register)
13347 variable or a formal (register) parameter which doesn't quite fill
13348 up an entire register. For now, just assume that it is
13349 legitimate to make the Dwarf info refer to the whole register which
13350 contains the given subreg. */
13351 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13352 loc_result = loc_descriptor (SUBREG_REG (rtl),
13353 GET_MODE (SUBREG_REG (rtl)), initialized);
13354 else
13355 goto do_default;
13356 break;
13357
13358 case REG:
13359 loc_result = reg_loc_descriptor (rtl, initialized);
13360 break;
13361
13362 case MEM:
13363 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13364 GET_MODE (rtl), initialized);
13365 if (loc_result == NULL)
13366 loc_result = tls_mem_loc_descriptor (rtl);
13367 if (loc_result == NULL)
13368 {
13369 rtx new_rtl = avoid_constant_pool_reference (rtl);
13370 if (new_rtl != rtl)
13371 loc_result = loc_descriptor (new_rtl, mode, initialized);
13372 }
13373 break;
13374
13375 case CONCAT:
13376 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13377 initialized);
13378 break;
13379
13380 case CONCATN:
13381 loc_result = concatn_loc_descriptor (rtl, initialized);
13382 break;
13383
13384 case VAR_LOCATION:
13385 /* Single part. */
13386 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13387 {
13388 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13389 if (GET_CODE (loc) == EXPR_LIST)
13390 loc = XEXP (loc, 0);
13391 loc_result = loc_descriptor (loc, mode, initialized);
13392 break;
13393 }
13394
13395 rtl = XEXP (rtl, 1);
13396 /* FALLTHRU */
13397
13398 case PARALLEL:
13399 {
13400 rtvec par_elems = XVEC (rtl, 0);
13401 int num_elem = GET_NUM_ELEM (par_elems);
13402 enum machine_mode mode;
13403 int i;
13404
13405 /* Create the first one, so we have something to add to. */
13406 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13407 VOIDmode, initialized);
13408 if (loc_result == NULL)
13409 return NULL;
13410 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13411 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13412 for (i = 1; i < num_elem; i++)
13413 {
13414 dw_loc_descr_ref temp;
13415
13416 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13417 VOIDmode, initialized);
13418 if (temp == NULL)
13419 return NULL;
13420 add_loc_descr (&loc_result, temp);
13421 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13422 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13423 }
13424 }
13425 break;
13426
13427 case CONST_INT:
13428 if (mode != VOIDmode && mode != BLKmode)
13429 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13430 INTVAL (rtl));
13431 break;
13432
13433 case CONST_DOUBLE:
13434 if (mode == VOIDmode)
13435 mode = GET_MODE (rtl);
13436
13437 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13438 {
13439 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13440
13441 /* Note that a CONST_DOUBLE rtx could represent either an integer
13442 or a floating-point constant. A CONST_DOUBLE is used whenever
13443 the constant requires more than one word in order to be
13444 adequately represented. We output CONST_DOUBLEs as blocks. */
13445 loc_result = new_loc_descr (DW_OP_implicit_value,
13446 GET_MODE_SIZE (mode), 0);
13447 #if TARGET_SUPPORTS_WIDE_INT == 0
13448 if (!SCALAR_FLOAT_MODE_P (mode))
13449 {
13450 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13451 loc_result->dw_loc_oprnd2.v.val_double
13452 = rtx_to_double_int (rtl);
13453 }
13454 else
13455 #endif
13456 {
13457 unsigned int length = GET_MODE_SIZE (mode);
13458 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13459
13460 insert_float (rtl, array);
13461 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13462 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13463 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13464 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13465 }
13466 }
13467 break;
13468
13469 case CONST_WIDE_INT:
13470 if (mode == VOIDmode)
13471 mode = GET_MODE (rtl);
13472
13473 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13474 {
13475 loc_result = new_loc_descr (DW_OP_implicit_value,
13476 GET_MODE_SIZE (mode), 0);
13477 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13478 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13479 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13480 }
13481 break;
13482
13483 case CONST_VECTOR:
13484 if (mode == VOIDmode)
13485 mode = GET_MODE (rtl);
13486
13487 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13488 {
13489 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13490 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13491 unsigned char *array
13492 = ggc_vec_alloc<unsigned char> (length * elt_size);
13493 unsigned int i;
13494 unsigned char *p;
13495 enum machine_mode imode = GET_MODE_INNER (mode);
13496
13497 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13498 switch (GET_MODE_CLASS (mode))
13499 {
13500 case MODE_VECTOR_INT:
13501 for (i = 0, p = array; i < length; i++, p += elt_size)
13502 {
13503 rtx elt = CONST_VECTOR_ELT (rtl, i);
13504 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13505 }
13506 break;
13507
13508 case MODE_VECTOR_FLOAT:
13509 for (i = 0, p = array; i < length; i++, p += elt_size)
13510 {
13511 rtx elt = CONST_VECTOR_ELT (rtl, i);
13512 insert_float (elt, p);
13513 }
13514 break;
13515
13516 default:
13517 gcc_unreachable ();
13518 }
13519
13520 loc_result = new_loc_descr (DW_OP_implicit_value,
13521 length * elt_size, 0);
13522 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13523 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13524 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13525 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13526 }
13527 break;
13528
13529 case CONST:
13530 if (mode == VOIDmode
13531 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13532 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13533 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13534 {
13535 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13536 break;
13537 }
13538 /* FALLTHROUGH */
13539 case SYMBOL_REF:
13540 if (!const_ok_for_output (rtl))
13541 break;
13542 case LABEL_REF:
13543 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13544 && (dwarf_version >= 4 || !dwarf_strict))
13545 {
13546 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13547 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13548 vec_safe_push (used_rtx_array, rtl);
13549 }
13550 break;
13551
13552 case DEBUG_IMPLICIT_PTR:
13553 loc_result = implicit_ptr_descriptor (rtl, 0);
13554 break;
13555
13556 case PLUS:
13557 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13558 && CONST_INT_P (XEXP (rtl, 1)))
13559 {
13560 loc_result
13561 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13562 break;
13563 }
13564 /* FALLTHRU */
13565 do_default:
13566 default:
13567 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13568 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13569 && dwarf_version >= 4)
13570 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13571 {
13572 /* Value expression. */
13573 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13574 if (loc_result)
13575 add_loc_descr (&loc_result,
13576 new_loc_descr (DW_OP_stack_value, 0, 0));
13577 }
13578 break;
13579 }
13580
13581 return loc_result;
13582 }
13583
13584 /* We need to figure out what section we should use as the base for the
13585 address ranges where a given location is valid.
13586 1. If this particular DECL has a section associated with it, use that.
13587 2. If this function has a section associated with it, use that.
13588 3. Otherwise, use the text section.
13589 XXX: If you split a variable across multiple sections, we won't notice. */
13590
13591 static const char *
13592 secname_for_decl (const_tree decl)
13593 {
13594 const char *secname;
13595
13596 if (VAR_OR_FUNCTION_DECL_P (decl)
13597 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13598 && DECL_SECTION_NAME (decl))
13599 secname = DECL_SECTION_NAME (decl);
13600 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13601 secname = DECL_SECTION_NAME (current_function_decl);
13602 else if (cfun && in_cold_section_p)
13603 secname = crtl->subsections.cold_section_label;
13604 else
13605 secname = text_section_label;
13606
13607 return secname;
13608 }
13609
13610 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13611
13612 static bool
13613 decl_by_reference_p (tree decl)
13614 {
13615 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13616 || TREE_CODE (decl) == VAR_DECL)
13617 && DECL_BY_REFERENCE (decl));
13618 }
13619
13620 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13621 for VARLOC. */
13622
13623 static dw_loc_descr_ref
13624 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13625 enum var_init_status initialized)
13626 {
13627 int have_address = 0;
13628 dw_loc_descr_ref descr;
13629 enum machine_mode mode;
13630
13631 if (want_address != 2)
13632 {
13633 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13634 /* Single part. */
13635 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13636 {
13637 varloc = PAT_VAR_LOCATION_LOC (varloc);
13638 if (GET_CODE (varloc) == EXPR_LIST)
13639 varloc = XEXP (varloc, 0);
13640 mode = GET_MODE (varloc);
13641 if (MEM_P (varloc))
13642 {
13643 rtx addr = XEXP (varloc, 0);
13644 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13645 mode, initialized);
13646 if (descr)
13647 have_address = 1;
13648 else
13649 {
13650 rtx x = avoid_constant_pool_reference (varloc);
13651 if (x != varloc)
13652 descr = mem_loc_descriptor (x, mode, VOIDmode,
13653 initialized);
13654 }
13655 }
13656 else
13657 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13658 }
13659 else
13660 return 0;
13661 }
13662 else
13663 {
13664 if (GET_CODE (varloc) == VAR_LOCATION)
13665 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13666 else
13667 mode = DECL_MODE (loc);
13668 descr = loc_descriptor (varloc, mode, initialized);
13669 have_address = 1;
13670 }
13671
13672 if (!descr)
13673 return 0;
13674
13675 if (want_address == 2 && !have_address
13676 && (dwarf_version >= 4 || !dwarf_strict))
13677 {
13678 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13679 {
13680 expansion_failed (loc, NULL_RTX,
13681 "DWARF address size mismatch");
13682 return 0;
13683 }
13684 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13685 have_address = 1;
13686 }
13687 /* Show if we can't fill the request for an address. */
13688 if (want_address && !have_address)
13689 {
13690 expansion_failed (loc, NULL_RTX,
13691 "Want address and only have value");
13692 return 0;
13693 }
13694
13695 /* If we've got an address and don't want one, dereference. */
13696 if (!want_address && have_address)
13697 {
13698 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13699 enum dwarf_location_atom op;
13700
13701 if (size > DWARF2_ADDR_SIZE || size == -1)
13702 {
13703 expansion_failed (loc, NULL_RTX,
13704 "DWARF address size mismatch");
13705 return 0;
13706 }
13707 else if (size == DWARF2_ADDR_SIZE)
13708 op = DW_OP_deref;
13709 else
13710 op = DW_OP_deref_size;
13711
13712 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13713 }
13714
13715 return descr;
13716 }
13717
13718 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13719 if it is not possible. */
13720
13721 static dw_loc_descr_ref
13722 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13723 {
13724 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13725 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13726 else if (dwarf_version >= 3 || !dwarf_strict)
13727 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13728 else
13729 return NULL;
13730 }
13731
13732 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13733 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13734
13735 static dw_loc_descr_ref
13736 dw_sra_loc_expr (tree decl, rtx loc)
13737 {
13738 rtx p;
13739 unsigned int padsize = 0;
13740 dw_loc_descr_ref descr, *descr_tail;
13741 unsigned HOST_WIDE_INT decl_size;
13742 rtx varloc;
13743 enum var_init_status initialized;
13744
13745 if (DECL_SIZE (decl) == NULL
13746 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13747 return NULL;
13748
13749 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13750 descr = NULL;
13751 descr_tail = &descr;
13752
13753 for (p = loc; p; p = XEXP (p, 1))
13754 {
13755 unsigned int bitsize = decl_piece_bitsize (p);
13756 rtx loc_note = *decl_piece_varloc_ptr (p);
13757 dw_loc_descr_ref cur_descr;
13758 dw_loc_descr_ref *tail, last = NULL;
13759 unsigned int opsize = 0;
13760
13761 if (loc_note == NULL_RTX
13762 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13763 {
13764 padsize += bitsize;
13765 continue;
13766 }
13767 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13768 varloc = NOTE_VAR_LOCATION (loc_note);
13769 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13770 if (cur_descr == NULL)
13771 {
13772 padsize += bitsize;
13773 continue;
13774 }
13775
13776 /* Check that cur_descr either doesn't use
13777 DW_OP_*piece operations, or their sum is equal
13778 to bitsize. Otherwise we can't embed it. */
13779 for (tail = &cur_descr; *tail != NULL;
13780 tail = &(*tail)->dw_loc_next)
13781 if ((*tail)->dw_loc_opc == DW_OP_piece)
13782 {
13783 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13784 * BITS_PER_UNIT;
13785 last = *tail;
13786 }
13787 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13788 {
13789 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13790 last = *tail;
13791 }
13792
13793 if (last != NULL && opsize != bitsize)
13794 {
13795 padsize += bitsize;
13796 /* Discard the current piece of the descriptor and release any
13797 addr_table entries it uses. */
13798 remove_loc_list_addr_table_entries (cur_descr);
13799 continue;
13800 }
13801
13802 /* If there is a hole, add DW_OP_*piece after empty DWARF
13803 expression, which means that those bits are optimized out. */
13804 if (padsize)
13805 {
13806 if (padsize > decl_size)
13807 {
13808 remove_loc_list_addr_table_entries (cur_descr);
13809 goto discard_descr;
13810 }
13811 decl_size -= padsize;
13812 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13813 if (*descr_tail == NULL)
13814 {
13815 remove_loc_list_addr_table_entries (cur_descr);
13816 goto discard_descr;
13817 }
13818 descr_tail = &(*descr_tail)->dw_loc_next;
13819 padsize = 0;
13820 }
13821 *descr_tail = cur_descr;
13822 descr_tail = tail;
13823 if (bitsize > decl_size)
13824 goto discard_descr;
13825 decl_size -= bitsize;
13826 if (last == NULL)
13827 {
13828 HOST_WIDE_INT offset = 0;
13829 if (GET_CODE (varloc) == VAR_LOCATION
13830 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13831 {
13832 varloc = PAT_VAR_LOCATION_LOC (varloc);
13833 if (GET_CODE (varloc) == EXPR_LIST)
13834 varloc = XEXP (varloc, 0);
13835 }
13836 do
13837 {
13838 if (GET_CODE (varloc) == CONST
13839 || GET_CODE (varloc) == SIGN_EXTEND
13840 || GET_CODE (varloc) == ZERO_EXTEND)
13841 varloc = XEXP (varloc, 0);
13842 else if (GET_CODE (varloc) == SUBREG)
13843 varloc = SUBREG_REG (varloc);
13844 else
13845 break;
13846 }
13847 while (1);
13848 /* DW_OP_bit_size offset should be zero for register
13849 or implicit location descriptions and empty location
13850 descriptions, but for memory addresses needs big endian
13851 adjustment. */
13852 if (MEM_P (varloc))
13853 {
13854 unsigned HOST_WIDE_INT memsize
13855 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13856 if (memsize != bitsize)
13857 {
13858 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13859 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13860 goto discard_descr;
13861 if (memsize < bitsize)
13862 goto discard_descr;
13863 if (BITS_BIG_ENDIAN)
13864 offset = memsize - bitsize;
13865 }
13866 }
13867
13868 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13869 if (*descr_tail == NULL)
13870 goto discard_descr;
13871 descr_tail = &(*descr_tail)->dw_loc_next;
13872 }
13873 }
13874
13875 /* If there were any non-empty expressions, add padding till the end of
13876 the decl. */
13877 if (descr != NULL && decl_size != 0)
13878 {
13879 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13880 if (*descr_tail == NULL)
13881 goto discard_descr;
13882 }
13883 return descr;
13884
13885 discard_descr:
13886 /* Discard the descriptor and release any addr_table entries it uses. */
13887 remove_loc_list_addr_table_entries (descr);
13888 return NULL;
13889 }
13890
13891 /* Return the dwarf representation of the location list LOC_LIST of
13892 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13893 function. */
13894
13895 static dw_loc_list_ref
13896 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13897 {
13898 const char *endname, *secname;
13899 rtx varloc;
13900 enum var_init_status initialized;
13901 struct var_loc_node *node;
13902 dw_loc_descr_ref descr;
13903 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13904 dw_loc_list_ref list = NULL;
13905 dw_loc_list_ref *listp = &list;
13906
13907 /* Now that we know what section we are using for a base,
13908 actually construct the list of locations.
13909 The first location information is what is passed to the
13910 function that creates the location list, and the remaining
13911 locations just get added on to that list.
13912 Note that we only know the start address for a location
13913 (IE location changes), so to build the range, we use
13914 the range [current location start, next location start].
13915 This means we have to special case the last node, and generate
13916 a range of [last location start, end of function label]. */
13917
13918 secname = secname_for_decl (decl);
13919
13920 for (node = loc_list->first; node; node = node->next)
13921 if (GET_CODE (node->loc) == EXPR_LIST
13922 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13923 {
13924 if (GET_CODE (node->loc) == EXPR_LIST)
13925 {
13926 /* This requires DW_OP_{,bit_}piece, which is not usable
13927 inside DWARF expressions. */
13928 if (want_address != 2)
13929 continue;
13930 descr = dw_sra_loc_expr (decl, node->loc);
13931 if (descr == NULL)
13932 continue;
13933 }
13934 else
13935 {
13936 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13937 varloc = NOTE_VAR_LOCATION (node->loc);
13938 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13939 }
13940 if (descr)
13941 {
13942 bool range_across_switch = false;
13943 /* If section switch happens in between node->label
13944 and node->next->label (or end of function) and
13945 we can't emit it as a single entry list,
13946 emit two ranges, first one ending at the end
13947 of first partition and second one starting at the
13948 beginning of second partition. */
13949 if (node == loc_list->last_before_switch
13950 && (node != loc_list->first || loc_list->first->next)
13951 && current_function_decl)
13952 {
13953 endname = cfun->fde->dw_fde_end;
13954 range_across_switch = true;
13955 }
13956 /* The variable has a location between NODE->LABEL and
13957 NODE->NEXT->LABEL. */
13958 else if (node->next)
13959 endname = node->next->label;
13960 /* If the variable has a location at the last label
13961 it keeps its location until the end of function. */
13962 else if (!current_function_decl)
13963 endname = text_end_label;
13964 else
13965 {
13966 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13967 current_function_funcdef_no);
13968 endname = ggc_strdup (label_id);
13969 }
13970
13971 *listp = new_loc_list (descr, node->label, endname, secname);
13972 if (TREE_CODE (decl) == PARM_DECL
13973 && node == loc_list->first
13974 && NOTE_P (node->loc)
13975 && strcmp (node->label, endname) == 0)
13976 (*listp)->force = true;
13977 listp = &(*listp)->dw_loc_next;
13978
13979 if (range_across_switch)
13980 {
13981 if (GET_CODE (node->loc) == EXPR_LIST)
13982 descr = dw_sra_loc_expr (decl, node->loc);
13983 else
13984 {
13985 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13986 varloc = NOTE_VAR_LOCATION (node->loc);
13987 descr = dw_loc_list_1 (decl, varloc, want_address,
13988 initialized);
13989 }
13990 gcc_assert (descr);
13991 /* The variable has a location between NODE->LABEL and
13992 NODE->NEXT->LABEL. */
13993 if (node->next)
13994 endname = node->next->label;
13995 else
13996 endname = cfun->fde->dw_fde_second_end;
13997 *listp = new_loc_list (descr,
13998 cfun->fde->dw_fde_second_begin,
13999 endname, secname);
14000 listp = &(*listp)->dw_loc_next;
14001 }
14002 }
14003 }
14004
14005 /* Try to avoid the overhead of a location list emitting a location
14006 expression instead, but only if we didn't have more than one
14007 location entry in the first place. If some entries were not
14008 representable, we don't want to pretend a single entry that was
14009 applies to the entire scope in which the variable is
14010 available. */
14011 if (list && loc_list->first->next)
14012 gen_llsym (list);
14013
14014 return list;
14015 }
14016
14017 /* Return if the loc_list has only single element and thus can be represented
14018 as location description. */
14019
14020 static bool
14021 single_element_loc_list_p (dw_loc_list_ref list)
14022 {
14023 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14024 return !list->ll_symbol;
14025 }
14026
14027 /* To each location in list LIST add loc descr REF. */
14028
14029 static void
14030 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14031 {
14032 dw_loc_descr_ref copy;
14033 add_loc_descr (&list->expr, ref);
14034 list = list->dw_loc_next;
14035 while (list)
14036 {
14037 copy = ggc_alloc<dw_loc_descr_node> ();
14038 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14039 add_loc_descr (&list->expr, copy);
14040 while (copy->dw_loc_next)
14041 {
14042 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14043 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14044 copy->dw_loc_next = new_copy;
14045 copy = new_copy;
14046 }
14047 list = list->dw_loc_next;
14048 }
14049 }
14050
14051 /* Given two lists RET and LIST
14052 produce location list that is result of adding expression in LIST
14053 to expression in RET on each position in program.
14054 Might be destructive on both RET and LIST.
14055
14056 TODO: We handle only simple cases of RET or LIST having at most one
14057 element. General case would inolve sorting the lists in program order
14058 and merging them that will need some additional work.
14059 Adding that will improve quality of debug info especially for SRA-ed
14060 structures. */
14061
14062 static void
14063 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14064 {
14065 if (!list)
14066 return;
14067 if (!*ret)
14068 {
14069 *ret = list;
14070 return;
14071 }
14072 if (!list->dw_loc_next)
14073 {
14074 add_loc_descr_to_each (*ret, list->expr);
14075 return;
14076 }
14077 if (!(*ret)->dw_loc_next)
14078 {
14079 add_loc_descr_to_each (list, (*ret)->expr);
14080 *ret = list;
14081 return;
14082 }
14083 expansion_failed (NULL_TREE, NULL_RTX,
14084 "Don't know how to merge two non-trivial"
14085 " location lists.\n");
14086 *ret = NULL;
14087 return;
14088 }
14089
14090 /* LOC is constant expression. Try a luck, look it up in constant
14091 pool and return its loc_descr of its address. */
14092
14093 static dw_loc_descr_ref
14094 cst_pool_loc_descr (tree loc)
14095 {
14096 /* Get an RTL for this, if something has been emitted. */
14097 rtx rtl = lookup_constant_def (loc);
14098
14099 if (!rtl || !MEM_P (rtl))
14100 {
14101 gcc_assert (!rtl);
14102 return 0;
14103 }
14104 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14105
14106 /* TODO: We might get more coverage if we was actually delaying expansion
14107 of all expressions till end of compilation when constant pools are fully
14108 populated. */
14109 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14110 {
14111 expansion_failed (loc, NULL_RTX,
14112 "CST value in contant pool but not marked.");
14113 return 0;
14114 }
14115 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14116 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14117 }
14118
14119 /* Return dw_loc_list representing address of addr_expr LOC
14120 by looking for inner INDIRECT_REF expression and turning
14121 it into simple arithmetics. */
14122
14123 static dw_loc_list_ref
14124 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14125 {
14126 tree obj, offset;
14127 HOST_WIDE_INT bitsize, bitpos, bytepos;
14128 enum machine_mode mode;
14129 int unsignedp, volatilep = 0;
14130 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14131
14132 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14133 &bitsize, &bitpos, &offset, &mode,
14134 &unsignedp, &volatilep, false);
14135 STRIP_NOPS (obj);
14136 if (bitpos % BITS_PER_UNIT)
14137 {
14138 expansion_failed (loc, NULL_RTX, "bitfield access");
14139 return 0;
14140 }
14141 if (!INDIRECT_REF_P (obj))
14142 {
14143 expansion_failed (obj,
14144 NULL_RTX, "no indirect ref in inner refrence");
14145 return 0;
14146 }
14147 if (!offset && !bitpos)
14148 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14149 else if (toplev
14150 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14151 && (dwarf_version >= 4 || !dwarf_strict))
14152 {
14153 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14154 if (!list_ret)
14155 return 0;
14156 if (offset)
14157 {
14158 /* Variable offset. */
14159 list_ret1 = loc_list_from_tree (offset, 0);
14160 if (list_ret1 == 0)
14161 return 0;
14162 add_loc_list (&list_ret, list_ret1);
14163 if (!list_ret)
14164 return 0;
14165 add_loc_descr_to_each (list_ret,
14166 new_loc_descr (DW_OP_plus, 0, 0));
14167 }
14168 bytepos = bitpos / BITS_PER_UNIT;
14169 if (bytepos > 0)
14170 add_loc_descr_to_each (list_ret,
14171 new_loc_descr (DW_OP_plus_uconst,
14172 bytepos, 0));
14173 else if (bytepos < 0)
14174 loc_list_plus_const (list_ret, bytepos);
14175 add_loc_descr_to_each (list_ret,
14176 new_loc_descr (DW_OP_stack_value, 0, 0));
14177 }
14178 return list_ret;
14179 }
14180
14181
14182 /* Generate Dwarf location list representing LOC.
14183 If WANT_ADDRESS is false, expression computing LOC will be computed
14184 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14185 if WANT_ADDRESS is 2, expression computing address useable in location
14186 will be returned (i.e. DW_OP_reg can be used
14187 to refer to register values). */
14188
14189 static dw_loc_list_ref
14190 loc_list_from_tree (tree loc, int want_address)
14191 {
14192 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14193 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14194 int have_address = 0;
14195 enum dwarf_location_atom op;
14196
14197 /* ??? Most of the time we do not take proper care for sign/zero
14198 extending the values properly. Hopefully this won't be a real
14199 problem... */
14200
14201 switch (TREE_CODE (loc))
14202 {
14203 case ERROR_MARK:
14204 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14205 return 0;
14206
14207 case PLACEHOLDER_EXPR:
14208 /* This case involves extracting fields from an object to determine the
14209 position of other fields. We don't try to encode this here. The
14210 only user of this is Ada, which encodes the needed information using
14211 the names of types. */
14212 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14213 return 0;
14214
14215 case CALL_EXPR:
14216 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14217 /* There are no opcodes for these operations. */
14218 return 0;
14219
14220 case PREINCREMENT_EXPR:
14221 case PREDECREMENT_EXPR:
14222 case POSTINCREMENT_EXPR:
14223 case POSTDECREMENT_EXPR:
14224 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14225 /* There are no opcodes for these operations. */
14226 return 0;
14227
14228 case ADDR_EXPR:
14229 /* If we already want an address, see if there is INDIRECT_REF inside
14230 e.g. for &this->field. */
14231 if (want_address)
14232 {
14233 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14234 (loc, want_address == 2);
14235 if (list_ret)
14236 have_address = 1;
14237 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14238 && (ret = cst_pool_loc_descr (loc)))
14239 have_address = 1;
14240 }
14241 /* Otherwise, process the argument and look for the address. */
14242 if (!list_ret && !ret)
14243 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14244 else
14245 {
14246 if (want_address)
14247 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14248 return NULL;
14249 }
14250 break;
14251
14252 case VAR_DECL:
14253 if (DECL_THREAD_LOCAL_P (loc))
14254 {
14255 rtx rtl;
14256 enum dwarf_location_atom tls_op;
14257 enum dtprel_bool dtprel = dtprel_false;
14258
14259 if (targetm.have_tls)
14260 {
14261 /* If this is not defined, we have no way to emit the
14262 data. */
14263 if (!targetm.asm_out.output_dwarf_dtprel)
14264 return 0;
14265
14266 /* The way DW_OP_GNU_push_tls_address is specified, we
14267 can only look up addresses of objects in the current
14268 module. We used DW_OP_addr as first op, but that's
14269 wrong, because DW_OP_addr is relocated by the debug
14270 info consumer, while DW_OP_GNU_push_tls_address
14271 operand shouldn't be. */
14272 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14273 return 0;
14274 dtprel = dtprel_true;
14275 tls_op = DW_OP_GNU_push_tls_address;
14276 }
14277 else
14278 {
14279 if (!targetm.emutls.debug_form_tls_address
14280 || !(dwarf_version >= 3 || !dwarf_strict))
14281 return 0;
14282 /* We stuffed the control variable into the DECL_VALUE_EXPR
14283 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14284 no longer appear in gimple code. We used the control
14285 variable in specific so that we could pick it up here. */
14286 loc = DECL_VALUE_EXPR (loc);
14287 tls_op = DW_OP_form_tls_address;
14288 }
14289
14290 rtl = rtl_for_decl_location (loc);
14291 if (rtl == NULL_RTX)
14292 return 0;
14293
14294 if (!MEM_P (rtl))
14295 return 0;
14296 rtl = XEXP (rtl, 0);
14297 if (! CONSTANT_P (rtl))
14298 return 0;
14299
14300 ret = new_addr_loc_descr (rtl, dtprel);
14301 ret1 = new_loc_descr (tls_op, 0, 0);
14302 add_loc_descr (&ret, ret1);
14303
14304 have_address = 1;
14305 break;
14306 }
14307 /* FALLTHRU */
14308
14309 case PARM_DECL:
14310 case RESULT_DECL:
14311 if (DECL_HAS_VALUE_EXPR_P (loc))
14312 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14313 want_address);
14314 /* FALLTHRU */
14315
14316 case FUNCTION_DECL:
14317 {
14318 rtx rtl;
14319 var_loc_list *loc_list = lookup_decl_loc (loc);
14320
14321 if (loc_list && loc_list->first)
14322 {
14323 list_ret = dw_loc_list (loc_list, loc, want_address);
14324 have_address = want_address != 0;
14325 break;
14326 }
14327 rtl = rtl_for_decl_location (loc);
14328 if (rtl == NULL_RTX)
14329 {
14330 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14331 return 0;
14332 }
14333 else if (CONST_INT_P (rtl))
14334 {
14335 HOST_WIDE_INT val = INTVAL (rtl);
14336 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14337 val &= GET_MODE_MASK (DECL_MODE (loc));
14338 ret = int_loc_descriptor (val);
14339 }
14340 else if (GET_CODE (rtl) == CONST_STRING)
14341 {
14342 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14343 return 0;
14344 }
14345 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14346 ret = new_addr_loc_descr (rtl, dtprel_false);
14347 else
14348 {
14349 enum machine_mode mode, mem_mode;
14350
14351 /* Certain constructs can only be represented at top-level. */
14352 if (want_address == 2)
14353 {
14354 ret = loc_descriptor (rtl, VOIDmode,
14355 VAR_INIT_STATUS_INITIALIZED);
14356 have_address = 1;
14357 }
14358 else
14359 {
14360 mode = GET_MODE (rtl);
14361 mem_mode = VOIDmode;
14362 if (MEM_P (rtl))
14363 {
14364 mem_mode = mode;
14365 mode = get_address_mode (rtl);
14366 rtl = XEXP (rtl, 0);
14367 have_address = 1;
14368 }
14369 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14370 VAR_INIT_STATUS_INITIALIZED);
14371 }
14372 if (!ret)
14373 expansion_failed (loc, rtl,
14374 "failed to produce loc descriptor for rtl");
14375 }
14376 }
14377 break;
14378
14379 case MEM_REF:
14380 /* ??? FIXME. */
14381 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14382 return 0;
14383 /* Fallthru. */
14384 case INDIRECT_REF:
14385 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14386 have_address = 1;
14387 break;
14388
14389 case COMPOUND_EXPR:
14390 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14391
14392 CASE_CONVERT:
14393 case VIEW_CONVERT_EXPR:
14394 case SAVE_EXPR:
14395 case MODIFY_EXPR:
14396 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14397
14398 case COMPONENT_REF:
14399 case BIT_FIELD_REF:
14400 case ARRAY_REF:
14401 case ARRAY_RANGE_REF:
14402 case REALPART_EXPR:
14403 case IMAGPART_EXPR:
14404 {
14405 tree obj, offset;
14406 HOST_WIDE_INT bitsize, bitpos, bytepos;
14407 enum machine_mode mode;
14408 int unsignedp, volatilep = 0;
14409
14410 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14411 &unsignedp, &volatilep, false);
14412
14413 gcc_assert (obj != loc);
14414
14415 list_ret = loc_list_from_tree (obj,
14416 want_address == 2
14417 && !bitpos && !offset ? 2 : 1);
14418 /* TODO: We can extract value of the small expression via shifting even
14419 for nonzero bitpos. */
14420 if (list_ret == 0)
14421 return 0;
14422 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14423 {
14424 expansion_failed (loc, NULL_RTX,
14425 "bitfield access");
14426 return 0;
14427 }
14428
14429 if (offset != NULL_TREE)
14430 {
14431 /* Variable offset. */
14432 list_ret1 = loc_list_from_tree (offset, 0);
14433 if (list_ret1 == 0)
14434 return 0;
14435 add_loc_list (&list_ret, list_ret1);
14436 if (!list_ret)
14437 return 0;
14438 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14439 }
14440
14441 bytepos = bitpos / BITS_PER_UNIT;
14442 if (bytepos > 0)
14443 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14444 else if (bytepos < 0)
14445 loc_list_plus_const (list_ret, bytepos);
14446
14447 have_address = 1;
14448 break;
14449 }
14450
14451 case INTEGER_CST:
14452 if ((want_address || !tree_fits_shwi_p (loc))
14453 && (ret = cst_pool_loc_descr (loc)))
14454 have_address = 1;
14455 else if (want_address == 2
14456 && tree_fits_shwi_p (loc)
14457 && (ret = address_of_int_loc_descriptor
14458 (int_size_in_bytes (TREE_TYPE (loc)),
14459 tree_to_shwi (loc))))
14460 have_address = 1;
14461 else if (tree_fits_shwi_p (loc))
14462 ret = int_loc_descriptor (tree_to_shwi (loc));
14463 else
14464 {
14465 expansion_failed (loc, NULL_RTX,
14466 "Integer operand is not host integer");
14467 return 0;
14468 }
14469 break;
14470
14471 case CONSTRUCTOR:
14472 case REAL_CST:
14473 case STRING_CST:
14474 case COMPLEX_CST:
14475 if ((ret = cst_pool_loc_descr (loc)))
14476 have_address = 1;
14477 else
14478 /* We can construct small constants here using int_loc_descriptor. */
14479 expansion_failed (loc, NULL_RTX,
14480 "constructor or constant not in constant pool");
14481 break;
14482
14483 case TRUTH_AND_EXPR:
14484 case TRUTH_ANDIF_EXPR:
14485 case BIT_AND_EXPR:
14486 op = DW_OP_and;
14487 goto do_binop;
14488
14489 case TRUTH_XOR_EXPR:
14490 case BIT_XOR_EXPR:
14491 op = DW_OP_xor;
14492 goto do_binop;
14493
14494 case TRUTH_OR_EXPR:
14495 case TRUTH_ORIF_EXPR:
14496 case BIT_IOR_EXPR:
14497 op = DW_OP_or;
14498 goto do_binop;
14499
14500 case FLOOR_DIV_EXPR:
14501 case CEIL_DIV_EXPR:
14502 case ROUND_DIV_EXPR:
14503 case TRUNC_DIV_EXPR:
14504 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14505 return 0;
14506 op = DW_OP_div;
14507 goto do_binop;
14508
14509 case MINUS_EXPR:
14510 op = DW_OP_minus;
14511 goto do_binop;
14512
14513 case FLOOR_MOD_EXPR:
14514 case CEIL_MOD_EXPR:
14515 case ROUND_MOD_EXPR:
14516 case TRUNC_MOD_EXPR:
14517 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14518 {
14519 op = DW_OP_mod;
14520 goto do_binop;
14521 }
14522 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14523 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14524 if (list_ret == 0 || list_ret1 == 0)
14525 return 0;
14526
14527 add_loc_list (&list_ret, list_ret1);
14528 if (list_ret == 0)
14529 return 0;
14530 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14531 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14532 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14533 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14534 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14535 break;
14536
14537 case MULT_EXPR:
14538 op = DW_OP_mul;
14539 goto do_binop;
14540
14541 case LSHIFT_EXPR:
14542 op = DW_OP_shl;
14543 goto do_binop;
14544
14545 case RSHIFT_EXPR:
14546 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14547 goto do_binop;
14548
14549 case POINTER_PLUS_EXPR:
14550 case PLUS_EXPR:
14551 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14552 {
14553 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14554 if (list_ret == 0)
14555 return 0;
14556
14557 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14558 break;
14559 }
14560
14561 op = DW_OP_plus;
14562 goto do_binop;
14563
14564 case LE_EXPR:
14565 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14566 return 0;
14567
14568 op = DW_OP_le;
14569 goto do_binop;
14570
14571 case GE_EXPR:
14572 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14573 return 0;
14574
14575 op = DW_OP_ge;
14576 goto do_binop;
14577
14578 case LT_EXPR:
14579 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14580 return 0;
14581
14582 op = DW_OP_lt;
14583 goto do_binop;
14584
14585 case GT_EXPR:
14586 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14587 return 0;
14588
14589 op = DW_OP_gt;
14590 goto do_binop;
14591
14592 case EQ_EXPR:
14593 op = DW_OP_eq;
14594 goto do_binop;
14595
14596 case NE_EXPR:
14597 op = DW_OP_ne;
14598 goto do_binop;
14599
14600 do_binop:
14601 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14602 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14603 if (list_ret == 0 || list_ret1 == 0)
14604 return 0;
14605
14606 add_loc_list (&list_ret, list_ret1);
14607 if (list_ret == 0)
14608 return 0;
14609 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14610 break;
14611
14612 case TRUTH_NOT_EXPR:
14613 case BIT_NOT_EXPR:
14614 op = DW_OP_not;
14615 goto do_unop;
14616
14617 case ABS_EXPR:
14618 op = DW_OP_abs;
14619 goto do_unop;
14620
14621 case NEGATE_EXPR:
14622 op = DW_OP_neg;
14623 goto do_unop;
14624
14625 do_unop:
14626 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14627 if (list_ret == 0)
14628 return 0;
14629
14630 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14631 break;
14632
14633 case MIN_EXPR:
14634 case MAX_EXPR:
14635 {
14636 const enum tree_code code =
14637 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14638
14639 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14640 build2 (code, integer_type_node,
14641 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14642 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14643 }
14644
14645 /* ... fall through ... */
14646
14647 case COND_EXPR:
14648 {
14649 dw_loc_descr_ref lhs
14650 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14651 dw_loc_list_ref rhs
14652 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14653 dw_loc_descr_ref bra_node, jump_node, tmp;
14654
14655 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14656 if (list_ret == 0 || lhs == 0 || rhs == 0)
14657 return 0;
14658
14659 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14660 add_loc_descr_to_each (list_ret, bra_node);
14661
14662 add_loc_list (&list_ret, rhs);
14663 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14664 add_loc_descr_to_each (list_ret, jump_node);
14665
14666 add_loc_descr_to_each (list_ret, lhs);
14667 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14668 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14669
14670 /* ??? Need a node to point the skip at. Use a nop. */
14671 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14672 add_loc_descr_to_each (list_ret, tmp);
14673 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14674 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14675 }
14676 break;
14677
14678 case FIX_TRUNC_EXPR:
14679 return 0;
14680
14681 default:
14682 /* Leave front-end specific codes as simply unknown. This comes
14683 up, for instance, with the C STMT_EXPR. */
14684 if ((unsigned int) TREE_CODE (loc)
14685 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14686 {
14687 expansion_failed (loc, NULL_RTX,
14688 "language specific tree node");
14689 return 0;
14690 }
14691
14692 #ifdef ENABLE_CHECKING
14693 /* Otherwise this is a generic code; we should just lists all of
14694 these explicitly. We forgot one. */
14695 gcc_unreachable ();
14696 #else
14697 /* In a release build, we want to degrade gracefully: better to
14698 generate incomplete debugging information than to crash. */
14699 return NULL;
14700 #endif
14701 }
14702
14703 if (!ret && !list_ret)
14704 return 0;
14705
14706 if (want_address == 2 && !have_address
14707 && (dwarf_version >= 4 || !dwarf_strict))
14708 {
14709 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14710 {
14711 expansion_failed (loc, NULL_RTX,
14712 "DWARF address size mismatch");
14713 return 0;
14714 }
14715 if (ret)
14716 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14717 else
14718 add_loc_descr_to_each (list_ret,
14719 new_loc_descr (DW_OP_stack_value, 0, 0));
14720 have_address = 1;
14721 }
14722 /* Show if we can't fill the request for an address. */
14723 if (want_address && !have_address)
14724 {
14725 expansion_failed (loc, NULL_RTX,
14726 "Want address and only have value");
14727 return 0;
14728 }
14729
14730 gcc_assert (!ret || !list_ret);
14731
14732 /* If we've got an address and don't want one, dereference. */
14733 if (!want_address && have_address)
14734 {
14735 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14736
14737 if (size > DWARF2_ADDR_SIZE || size == -1)
14738 {
14739 expansion_failed (loc, NULL_RTX,
14740 "DWARF address size mismatch");
14741 return 0;
14742 }
14743 else if (size == DWARF2_ADDR_SIZE)
14744 op = DW_OP_deref;
14745 else
14746 op = DW_OP_deref_size;
14747
14748 if (ret)
14749 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14750 else
14751 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14752 }
14753 if (ret)
14754 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14755
14756 return list_ret;
14757 }
14758
14759 /* Same as above but return only single location expression. */
14760 static dw_loc_descr_ref
14761 loc_descriptor_from_tree (tree loc, int want_address)
14762 {
14763 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14764 if (!ret)
14765 return NULL;
14766 if (ret->dw_loc_next)
14767 {
14768 expansion_failed (loc, NULL_RTX,
14769 "Location list where only loc descriptor needed");
14770 return NULL;
14771 }
14772 return ret->expr;
14773 }
14774
14775 /* Given a value, round it up to the lowest multiple of `boundary'
14776 which is not less than the value itself. */
14777
14778 static inline HOST_WIDE_INT
14779 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14780 {
14781 return (((value + boundary - 1) / boundary) * boundary);
14782 }
14783
14784 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14785 pointer to the declared type for the relevant field variable, or return
14786 `integer_type_node' if the given node turns out to be an
14787 ERROR_MARK node. */
14788
14789 static inline tree
14790 field_type (const_tree decl)
14791 {
14792 tree type;
14793
14794 if (TREE_CODE (decl) == ERROR_MARK)
14795 return integer_type_node;
14796
14797 type = DECL_BIT_FIELD_TYPE (decl);
14798 if (type == NULL_TREE)
14799 type = TREE_TYPE (decl);
14800
14801 return type;
14802 }
14803
14804 /* Given a pointer to a tree node, return the alignment in bits for
14805 it, or else return BITS_PER_WORD if the node actually turns out to
14806 be an ERROR_MARK node. */
14807
14808 static inline unsigned
14809 simple_type_align_in_bits (const_tree type)
14810 {
14811 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14812 }
14813
14814 static inline unsigned
14815 simple_decl_align_in_bits (const_tree decl)
14816 {
14817 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14818 }
14819
14820 /* Return the result of rounding T up to ALIGN. */
14821
14822 static inline offset_int
14823 round_up_to_align (const offset_int &t, unsigned int align)
14824 {
14825 return wi::udiv_trunc (t + align - 1, align) * align;
14826 }
14827
14828 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14829 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14830 or return 0 if we are unable to determine what that offset is, either
14831 because the argument turns out to be a pointer to an ERROR_MARK node, or
14832 because the offset is actually variable. (We can't handle the latter case
14833 just yet). */
14834
14835 static HOST_WIDE_INT
14836 field_byte_offset (const_tree decl)
14837 {
14838 offset_int object_offset_in_bits;
14839 offset_int object_offset_in_bytes;
14840 offset_int bitpos_int;
14841
14842 if (TREE_CODE (decl) == ERROR_MARK)
14843 return 0;
14844
14845 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14846
14847 /* We cannot yet cope with fields whose positions are variable, so
14848 for now, when we see such things, we simply return 0. Someday, we may
14849 be able to handle such cases, but it will be damn difficult. */
14850 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14851 return 0;
14852
14853 bitpos_int = wi::to_offset (bit_position (decl));
14854
14855 #ifdef PCC_BITFIELD_TYPE_MATTERS
14856 if (PCC_BITFIELD_TYPE_MATTERS)
14857 {
14858 tree type;
14859 tree field_size_tree;
14860 offset_int deepest_bitpos;
14861 offset_int field_size_in_bits;
14862 unsigned int type_align_in_bits;
14863 unsigned int decl_align_in_bits;
14864 offset_int type_size_in_bits;
14865
14866 type = field_type (decl);
14867 type_size_in_bits = offset_int_type_size_in_bits (type);
14868 type_align_in_bits = simple_type_align_in_bits (type);
14869
14870 field_size_tree = DECL_SIZE (decl);
14871
14872 /* The size could be unspecified if there was an error, or for
14873 a flexible array member. */
14874 if (!field_size_tree)
14875 field_size_tree = bitsize_zero_node;
14876
14877 /* If the size of the field is not constant, use the type size. */
14878 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14879 field_size_in_bits = wi::to_offset (field_size_tree);
14880 else
14881 field_size_in_bits = type_size_in_bits;
14882
14883 decl_align_in_bits = simple_decl_align_in_bits (decl);
14884
14885 /* The GCC front-end doesn't make any attempt to keep track of the
14886 starting bit offset (relative to the start of the containing
14887 structure type) of the hypothetical "containing object" for a
14888 bit-field. Thus, when computing the byte offset value for the
14889 start of the "containing object" of a bit-field, we must deduce
14890 this information on our own. This can be rather tricky to do in
14891 some cases. For example, handling the following structure type
14892 definition when compiling for an i386/i486 target (which only
14893 aligns long long's to 32-bit boundaries) can be very tricky:
14894
14895 struct S { int field1; long long field2:31; };
14896
14897 Fortunately, there is a simple rule-of-thumb which can be used
14898 in such cases. When compiling for an i386/i486, GCC will
14899 allocate 8 bytes for the structure shown above. It decides to
14900 do this based upon one simple rule for bit-field allocation.
14901 GCC allocates each "containing object" for each bit-field at
14902 the first (i.e. lowest addressed) legitimate alignment boundary
14903 (based upon the required minimum alignment for the declared
14904 type of the field) which it can possibly use, subject to the
14905 condition that there is still enough available space remaining
14906 in the containing object (when allocated at the selected point)
14907 to fully accommodate all of the bits of the bit-field itself.
14908
14909 This simple rule makes it obvious why GCC allocates 8 bytes for
14910 each object of the structure type shown above. When looking
14911 for a place to allocate the "containing object" for `field2',
14912 the compiler simply tries to allocate a 64-bit "containing
14913 object" at each successive 32-bit boundary (starting at zero)
14914 until it finds a place to allocate that 64- bit field such that
14915 at least 31 contiguous (and previously unallocated) bits remain
14916 within that selected 64 bit field. (As it turns out, for the
14917 example above, the compiler finds it is OK to allocate the
14918 "containing object" 64-bit field at bit-offset zero within the
14919 structure type.)
14920
14921 Here we attempt to work backwards from the limited set of facts
14922 we're given, and we try to deduce from those facts, where GCC
14923 must have believed that the containing object started (within
14924 the structure type). The value we deduce is then used (by the
14925 callers of this routine) to generate DW_AT_location and
14926 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14927 the case of DW_AT_location, regular fields as well). */
14928
14929 /* Figure out the bit-distance from the start of the structure to
14930 the "deepest" bit of the bit-field. */
14931 deepest_bitpos = bitpos_int + field_size_in_bits;
14932
14933 /* This is the tricky part. Use some fancy footwork to deduce
14934 where the lowest addressed bit of the containing object must
14935 be. */
14936 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14937
14938 /* Round up to type_align by default. This works best for
14939 bitfields. */
14940 object_offset_in_bits
14941 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14942
14943 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
14944 {
14945 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14946
14947 /* Round up to decl_align instead. */
14948 object_offset_in_bits
14949 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14950 }
14951 }
14952 else
14953 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14954 object_offset_in_bits = bitpos_int;
14955
14956 object_offset_in_bytes
14957 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
14958 return object_offset_in_bytes.to_shwi ();
14959 }
14960 \f
14961 /* The following routines define various Dwarf attributes and any data
14962 associated with them. */
14963
14964 /* Add a location description attribute value to a DIE.
14965
14966 This emits location attributes suitable for whole variables and
14967 whole parameters. Note that the location attributes for struct fields are
14968 generated by the routine `data_member_location_attribute' below. */
14969
14970 static inline void
14971 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14972 dw_loc_list_ref descr)
14973 {
14974 if (descr == 0)
14975 return;
14976 if (single_element_loc_list_p (descr))
14977 add_AT_loc (die, attr_kind, descr->expr);
14978 else
14979 add_AT_loc_list (die, attr_kind, descr);
14980 }
14981
14982 /* Add DW_AT_accessibility attribute to DIE if needed. */
14983
14984 static void
14985 add_accessibility_attribute (dw_die_ref die, tree decl)
14986 {
14987 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14988 children, otherwise the default is DW_ACCESS_public. In DWARF2
14989 the default has always been DW_ACCESS_public. */
14990 if (TREE_PROTECTED (decl))
14991 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14992 else if (TREE_PRIVATE (decl))
14993 {
14994 if (dwarf_version == 2
14995 || die->die_parent == NULL
14996 || die->die_parent->die_tag != DW_TAG_class_type)
14997 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14998 }
14999 else if (dwarf_version > 2
15000 && die->die_parent
15001 && die->die_parent->die_tag == DW_TAG_class_type)
15002 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15003 }
15004
15005 /* Attach the specialized form of location attribute used for data members of
15006 struct and union types. In the special case of a FIELD_DECL node which
15007 represents a bit-field, the "offset" part of this special location
15008 descriptor must indicate the distance in bytes from the lowest-addressed
15009 byte of the containing struct or union type to the lowest-addressed byte of
15010 the "containing object" for the bit-field. (See the `field_byte_offset'
15011 function above).
15012
15013 For any given bit-field, the "containing object" is a hypothetical object
15014 (of some integral or enum type) within which the given bit-field lives. The
15015 type of this hypothetical "containing object" is always the same as the
15016 declared type of the individual bit-field itself (for GCC anyway... the
15017 DWARF spec doesn't actually mandate this). Note that it is the size (in
15018 bytes) of the hypothetical "containing object" which will be given in the
15019 DW_AT_byte_size attribute for this bit-field. (See the
15020 `byte_size_attribute' function below.) It is also used when calculating the
15021 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15022 function below.) */
15023
15024 static void
15025 add_data_member_location_attribute (dw_die_ref die, tree decl)
15026 {
15027 HOST_WIDE_INT offset;
15028 dw_loc_descr_ref loc_descr = 0;
15029
15030 if (TREE_CODE (decl) == TREE_BINFO)
15031 {
15032 /* We're working on the TAG_inheritance for a base class. */
15033 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15034 {
15035 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15036 aren't at a fixed offset from all (sub)objects of the same
15037 type. We need to extract the appropriate offset from our
15038 vtable. The following dwarf expression means
15039
15040 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15041
15042 This is specific to the V3 ABI, of course. */
15043
15044 dw_loc_descr_ref tmp;
15045
15046 /* Make a copy of the object address. */
15047 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15048 add_loc_descr (&loc_descr, tmp);
15049
15050 /* Extract the vtable address. */
15051 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15052 add_loc_descr (&loc_descr, tmp);
15053
15054 /* Calculate the address of the offset. */
15055 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15056 gcc_assert (offset < 0);
15057
15058 tmp = int_loc_descriptor (-offset);
15059 add_loc_descr (&loc_descr, tmp);
15060 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15061 add_loc_descr (&loc_descr, tmp);
15062
15063 /* Extract the offset. */
15064 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15065 add_loc_descr (&loc_descr, tmp);
15066
15067 /* Add it to the object address. */
15068 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15069 add_loc_descr (&loc_descr, tmp);
15070 }
15071 else
15072 offset = tree_to_shwi (BINFO_OFFSET (decl));
15073 }
15074 else
15075 offset = field_byte_offset (decl);
15076
15077 if (! loc_descr)
15078 {
15079 if (dwarf_version > 2)
15080 {
15081 /* Don't need to output a location expression, just the constant. */
15082 if (offset < 0)
15083 add_AT_int (die, DW_AT_data_member_location, offset);
15084 else
15085 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15086 return;
15087 }
15088 else
15089 {
15090 enum dwarf_location_atom op;
15091
15092 /* The DWARF2 standard says that we should assume that the structure
15093 address is already on the stack, so we can specify a structure
15094 field address by using DW_OP_plus_uconst. */
15095 op = DW_OP_plus_uconst;
15096 loc_descr = new_loc_descr (op, offset, 0);
15097 }
15098 }
15099
15100 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15101 }
15102
15103 /* Writes integer values to dw_vec_const array. */
15104
15105 static void
15106 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15107 {
15108 while (size != 0)
15109 {
15110 *dest++ = val & 0xff;
15111 val >>= 8;
15112 --size;
15113 }
15114 }
15115
15116 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15117
15118 static HOST_WIDE_INT
15119 extract_int (const unsigned char *src, unsigned int size)
15120 {
15121 HOST_WIDE_INT val = 0;
15122
15123 src += size;
15124 while (size != 0)
15125 {
15126 val <<= 8;
15127 val |= *--src & 0xff;
15128 --size;
15129 }
15130 return val;
15131 }
15132
15133 /* Writes wide_int values to dw_vec_const array. */
15134
15135 static void
15136 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15137 {
15138 int i;
15139
15140 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15141 {
15142 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15143 return;
15144 }
15145
15146 /* We'd have to extend this code to support odd sizes. */
15147 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15148
15149 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15150
15151 if (WORDS_BIG_ENDIAN)
15152 for (i = n - 1; i >= 0; i--)
15153 {
15154 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15155 dest += sizeof (HOST_WIDE_INT);
15156 }
15157 else
15158 for (i = 0; i < n; i++)
15159 {
15160 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15161 dest += sizeof (HOST_WIDE_INT);
15162 }
15163 }
15164
15165 /* Writes floating point values to dw_vec_const array. */
15166
15167 static void
15168 insert_float (const_rtx rtl, unsigned char *array)
15169 {
15170 REAL_VALUE_TYPE rv;
15171 long val[4];
15172 int i;
15173
15174 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15175 real_to_target (val, &rv, GET_MODE (rtl));
15176
15177 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15178 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15179 {
15180 insert_int (val[i], 4, array);
15181 array += 4;
15182 }
15183 }
15184
15185 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15186 does not have a "location" either in memory or in a register. These
15187 things can arise in GNU C when a constant is passed as an actual parameter
15188 to an inlined function. They can also arise in C++ where declared
15189 constants do not necessarily get memory "homes". */
15190
15191 static bool
15192 add_const_value_attribute (dw_die_ref die, rtx rtl)
15193 {
15194 switch (GET_CODE (rtl))
15195 {
15196 case CONST_INT:
15197 {
15198 HOST_WIDE_INT val = INTVAL (rtl);
15199
15200 if (val < 0)
15201 add_AT_int (die, DW_AT_const_value, val);
15202 else
15203 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15204 }
15205 return true;
15206
15207 case CONST_WIDE_INT:
15208 add_AT_wide (die, DW_AT_const_value,
15209 std::make_pair (rtl, GET_MODE (rtl)));
15210 return true;
15211
15212 case CONST_DOUBLE:
15213 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15214 floating-point constant. A CONST_DOUBLE is used whenever the
15215 constant requires more than one word in order to be adequately
15216 represented. */
15217 {
15218 enum machine_mode mode = GET_MODE (rtl);
15219
15220 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15221 add_AT_double (die, DW_AT_const_value,
15222 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15223 else
15224 {
15225 unsigned int length = GET_MODE_SIZE (mode);
15226 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15227
15228 insert_float (rtl, array);
15229 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15230 }
15231 }
15232 return true;
15233
15234 case CONST_VECTOR:
15235 {
15236 enum machine_mode mode = GET_MODE (rtl);
15237 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15238 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15239 unsigned char *array
15240 = ggc_vec_alloc<unsigned char> (length * elt_size);
15241 unsigned int i;
15242 unsigned char *p;
15243 enum machine_mode imode = GET_MODE_INNER (mode);
15244
15245 switch (GET_MODE_CLASS (mode))
15246 {
15247 case MODE_VECTOR_INT:
15248 for (i = 0, p = array; i < length; i++, p += elt_size)
15249 {
15250 rtx elt = CONST_VECTOR_ELT (rtl, i);
15251 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15252 }
15253 break;
15254
15255 case MODE_VECTOR_FLOAT:
15256 for (i = 0, p = array; i < length; i++, p += elt_size)
15257 {
15258 rtx elt = CONST_VECTOR_ELT (rtl, i);
15259 insert_float (elt, p);
15260 }
15261 break;
15262
15263 default:
15264 gcc_unreachable ();
15265 }
15266
15267 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15268 }
15269 return true;
15270
15271 case CONST_STRING:
15272 if (dwarf_version >= 4 || !dwarf_strict)
15273 {
15274 dw_loc_descr_ref loc_result;
15275 resolve_one_addr (&rtl, NULL);
15276 rtl_addr:
15277 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15278 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15279 add_AT_loc (die, DW_AT_location, loc_result);
15280 vec_safe_push (used_rtx_array, rtl);
15281 return true;
15282 }
15283 return false;
15284
15285 case CONST:
15286 if (CONSTANT_P (XEXP (rtl, 0)))
15287 return add_const_value_attribute (die, XEXP (rtl, 0));
15288 /* FALLTHROUGH */
15289 case SYMBOL_REF:
15290 if (!const_ok_for_output (rtl))
15291 return false;
15292 case LABEL_REF:
15293 if (dwarf_version >= 4 || !dwarf_strict)
15294 goto rtl_addr;
15295 return false;
15296
15297 case PLUS:
15298 /* In cases where an inlined instance of an inline function is passed
15299 the address of an `auto' variable (which is local to the caller) we
15300 can get a situation where the DECL_RTL of the artificial local
15301 variable (for the inlining) which acts as a stand-in for the
15302 corresponding formal parameter (of the inline function) will look
15303 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15304 exactly a compile-time constant expression, but it isn't the address
15305 of the (artificial) local variable either. Rather, it represents the
15306 *value* which the artificial local variable always has during its
15307 lifetime. We currently have no way to represent such quasi-constant
15308 values in Dwarf, so for now we just punt and generate nothing. */
15309 return false;
15310
15311 case HIGH:
15312 case CONST_FIXED:
15313 return false;
15314
15315 case MEM:
15316 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15317 && MEM_READONLY_P (rtl)
15318 && GET_MODE (rtl) == BLKmode)
15319 {
15320 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15321 return true;
15322 }
15323 return false;
15324
15325 default:
15326 /* No other kinds of rtx should be possible here. */
15327 gcc_unreachable ();
15328 }
15329 return false;
15330 }
15331
15332 /* Determine whether the evaluation of EXPR references any variables
15333 or functions which aren't otherwise used (and therefore may not be
15334 output). */
15335 static tree
15336 reference_to_unused (tree * tp, int * walk_subtrees,
15337 void * data ATTRIBUTE_UNUSED)
15338 {
15339 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15340 *walk_subtrees = 0;
15341
15342 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15343 && ! TREE_ASM_WRITTEN (*tp))
15344 return *tp;
15345 /* ??? The C++ FE emits debug information for using decls, so
15346 putting gcc_unreachable here falls over. See PR31899. For now
15347 be conservative. */
15348 else if (!symtab->global_info_ready
15349 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15350 return *tp;
15351 else if (TREE_CODE (*tp) == VAR_DECL)
15352 {
15353 varpool_node *node = varpool_node::get (*tp);
15354 if (!node || !node->definition)
15355 return *tp;
15356 }
15357 else if (TREE_CODE (*tp) == FUNCTION_DECL
15358 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15359 {
15360 /* The call graph machinery must have finished analyzing,
15361 optimizing and gimplifying the CU by now.
15362 So if *TP has no call graph node associated
15363 to it, it means *TP will not be emitted. */
15364 if (!cgraph_node::get (*tp))
15365 return *tp;
15366 }
15367 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15368 return *tp;
15369
15370 return NULL_TREE;
15371 }
15372
15373 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15374 for use in a later add_const_value_attribute call. */
15375
15376 static rtx
15377 rtl_for_decl_init (tree init, tree type)
15378 {
15379 rtx rtl = NULL_RTX;
15380
15381 STRIP_NOPS (init);
15382
15383 /* If a variable is initialized with a string constant without embedded
15384 zeros, build CONST_STRING. */
15385 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15386 {
15387 tree enttype = TREE_TYPE (type);
15388 tree domain = TYPE_DOMAIN (type);
15389 enum machine_mode mode = TYPE_MODE (enttype);
15390
15391 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15392 && domain
15393 && integer_zerop (TYPE_MIN_VALUE (domain))
15394 && compare_tree_int (TYPE_MAX_VALUE (domain),
15395 TREE_STRING_LENGTH (init) - 1) == 0
15396 && ((size_t) TREE_STRING_LENGTH (init)
15397 == strlen (TREE_STRING_POINTER (init)) + 1))
15398 {
15399 rtl = gen_rtx_CONST_STRING (VOIDmode,
15400 ggc_strdup (TREE_STRING_POINTER (init)));
15401 rtl = gen_rtx_MEM (BLKmode, rtl);
15402 MEM_READONLY_P (rtl) = 1;
15403 }
15404 }
15405 /* Other aggregates, and complex values, could be represented using
15406 CONCAT: FIXME! */
15407 else if (AGGREGATE_TYPE_P (type)
15408 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15409 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15410 || TREE_CODE (type) == COMPLEX_TYPE)
15411 ;
15412 /* Vectors only work if their mode is supported by the target.
15413 FIXME: generic vectors ought to work too. */
15414 else if (TREE_CODE (type) == VECTOR_TYPE
15415 && !VECTOR_MODE_P (TYPE_MODE (type)))
15416 ;
15417 /* If the initializer is something that we know will expand into an
15418 immediate RTL constant, expand it now. We must be careful not to
15419 reference variables which won't be output. */
15420 else if (initializer_constant_valid_p (init, type)
15421 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15422 {
15423 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15424 possible. */
15425 if (TREE_CODE (type) == VECTOR_TYPE)
15426 switch (TREE_CODE (init))
15427 {
15428 case VECTOR_CST:
15429 break;
15430 case CONSTRUCTOR:
15431 if (TREE_CONSTANT (init))
15432 {
15433 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15434 bool constant_p = true;
15435 tree value;
15436 unsigned HOST_WIDE_INT ix;
15437
15438 /* Even when ctor is constant, it might contain non-*_CST
15439 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15440 belong into VECTOR_CST nodes. */
15441 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15442 if (!CONSTANT_CLASS_P (value))
15443 {
15444 constant_p = false;
15445 break;
15446 }
15447
15448 if (constant_p)
15449 {
15450 init = build_vector_from_ctor (type, elts);
15451 break;
15452 }
15453 }
15454 /* FALLTHRU */
15455
15456 default:
15457 return NULL;
15458 }
15459
15460 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15461
15462 /* If expand_expr returns a MEM, it wasn't immediate. */
15463 gcc_assert (!rtl || !MEM_P (rtl));
15464 }
15465
15466 return rtl;
15467 }
15468
15469 /* Generate RTL for the variable DECL to represent its location. */
15470
15471 static rtx
15472 rtl_for_decl_location (tree decl)
15473 {
15474 rtx rtl;
15475
15476 /* Here we have to decide where we are going to say the parameter "lives"
15477 (as far as the debugger is concerned). We only have a couple of
15478 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15479
15480 DECL_RTL normally indicates where the parameter lives during most of the
15481 activation of the function. If optimization is enabled however, this
15482 could be either NULL or else a pseudo-reg. Both of those cases indicate
15483 that the parameter doesn't really live anywhere (as far as the code
15484 generation parts of GCC are concerned) during most of the function's
15485 activation. That will happen (for example) if the parameter is never
15486 referenced within the function.
15487
15488 We could just generate a location descriptor here for all non-NULL
15489 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15490 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15491 where DECL_RTL is NULL or is a pseudo-reg.
15492
15493 Note however that we can only get away with using DECL_INCOMING_RTL as
15494 a backup substitute for DECL_RTL in certain limited cases. In cases
15495 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15496 we can be sure that the parameter was passed using the same type as it is
15497 declared to have within the function, and that its DECL_INCOMING_RTL
15498 points us to a place where a value of that type is passed.
15499
15500 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15501 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15502 because in these cases DECL_INCOMING_RTL points us to a value of some
15503 type which is *different* from the type of the parameter itself. Thus,
15504 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15505 such cases, the debugger would end up (for example) trying to fetch a
15506 `float' from a place which actually contains the first part of a
15507 `double'. That would lead to really incorrect and confusing
15508 output at debug-time.
15509
15510 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15511 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15512 are a couple of exceptions however. On little-endian machines we can
15513 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15514 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15515 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15516 when (on a little-endian machine) a non-prototyped function has a
15517 parameter declared to be of type `short' or `char'. In such cases,
15518 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15519 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15520 passed `int' value. If the debugger then uses that address to fetch
15521 a `short' or a `char' (on a little-endian machine) the result will be
15522 the correct data, so we allow for such exceptional cases below.
15523
15524 Note that our goal here is to describe the place where the given formal
15525 parameter lives during most of the function's activation (i.e. between the
15526 end of the prologue and the start of the epilogue). We'll do that as best
15527 as we can. Note however that if the given formal parameter is modified
15528 sometime during the execution of the function, then a stack backtrace (at
15529 debug-time) will show the function as having been called with the *new*
15530 value rather than the value which was originally passed in. This happens
15531 rarely enough that it is not a major problem, but it *is* a problem, and
15532 I'd like to fix it.
15533
15534 A future version of dwarf2out.c may generate two additional attributes for
15535 any given DW_TAG_formal_parameter DIE which will describe the "passed
15536 type" and the "passed location" for the given formal parameter in addition
15537 to the attributes we now generate to indicate the "declared type" and the
15538 "active location" for each parameter. This additional set of attributes
15539 could be used by debuggers for stack backtraces. Separately, note that
15540 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15541 This happens (for example) for inlined-instances of inline function formal
15542 parameters which are never referenced. This really shouldn't be
15543 happening. All PARM_DECL nodes should get valid non-NULL
15544 DECL_INCOMING_RTL values. FIXME. */
15545
15546 /* Use DECL_RTL as the "location" unless we find something better. */
15547 rtl = DECL_RTL_IF_SET (decl);
15548
15549 /* When generating abstract instances, ignore everything except
15550 constants, symbols living in memory, and symbols living in
15551 fixed registers. */
15552 if (! reload_completed)
15553 {
15554 if (rtl
15555 && (CONSTANT_P (rtl)
15556 || (MEM_P (rtl)
15557 && CONSTANT_P (XEXP (rtl, 0)))
15558 || (REG_P (rtl)
15559 && TREE_CODE (decl) == VAR_DECL
15560 && TREE_STATIC (decl))))
15561 {
15562 rtl = targetm.delegitimize_address (rtl);
15563 return rtl;
15564 }
15565 rtl = NULL_RTX;
15566 }
15567 else if (TREE_CODE (decl) == PARM_DECL)
15568 {
15569 if (rtl == NULL_RTX
15570 || is_pseudo_reg (rtl)
15571 || (MEM_P (rtl)
15572 && is_pseudo_reg (XEXP (rtl, 0))
15573 && DECL_INCOMING_RTL (decl)
15574 && MEM_P (DECL_INCOMING_RTL (decl))
15575 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15576 {
15577 tree declared_type = TREE_TYPE (decl);
15578 tree passed_type = DECL_ARG_TYPE (decl);
15579 enum machine_mode dmode = TYPE_MODE (declared_type);
15580 enum machine_mode pmode = TYPE_MODE (passed_type);
15581
15582 /* This decl represents a formal parameter which was optimized out.
15583 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15584 all cases where (rtl == NULL_RTX) just below. */
15585 if (dmode == pmode)
15586 rtl = DECL_INCOMING_RTL (decl);
15587 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15588 && SCALAR_INT_MODE_P (dmode)
15589 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15590 && DECL_INCOMING_RTL (decl))
15591 {
15592 rtx inc = DECL_INCOMING_RTL (decl);
15593 if (REG_P (inc))
15594 rtl = inc;
15595 else if (MEM_P (inc))
15596 {
15597 if (BYTES_BIG_ENDIAN)
15598 rtl = adjust_address_nv (inc, dmode,
15599 GET_MODE_SIZE (pmode)
15600 - GET_MODE_SIZE (dmode));
15601 else
15602 rtl = inc;
15603 }
15604 }
15605 }
15606
15607 /* If the parm was passed in registers, but lives on the stack, then
15608 make a big endian correction if the mode of the type of the
15609 parameter is not the same as the mode of the rtl. */
15610 /* ??? This is the same series of checks that are made in dbxout.c before
15611 we reach the big endian correction code there. It isn't clear if all
15612 of these checks are necessary here, but keeping them all is the safe
15613 thing to do. */
15614 else if (MEM_P (rtl)
15615 && XEXP (rtl, 0) != const0_rtx
15616 && ! CONSTANT_P (XEXP (rtl, 0))
15617 /* Not passed in memory. */
15618 && !MEM_P (DECL_INCOMING_RTL (decl))
15619 /* Not passed by invisible reference. */
15620 && (!REG_P (XEXP (rtl, 0))
15621 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15622 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15623 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15624 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15625 #endif
15626 )
15627 /* Big endian correction check. */
15628 && BYTES_BIG_ENDIAN
15629 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15630 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15631 < UNITS_PER_WORD))
15632 {
15633 enum machine_mode addr_mode = get_address_mode (rtl);
15634 int offset = (UNITS_PER_WORD
15635 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15636
15637 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15638 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15639 }
15640 }
15641 else if (TREE_CODE (decl) == VAR_DECL
15642 && rtl
15643 && MEM_P (rtl)
15644 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15645 && BYTES_BIG_ENDIAN)
15646 {
15647 enum machine_mode addr_mode = get_address_mode (rtl);
15648 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15649 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15650
15651 /* If a variable is declared "register" yet is smaller than
15652 a register, then if we store the variable to memory, it
15653 looks like we're storing a register-sized value, when in
15654 fact we are not. We need to adjust the offset of the
15655 storage location to reflect the actual value's bytes,
15656 else gdb will not be able to display it. */
15657 if (rsize > dsize)
15658 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15659 plus_constant (addr_mode, XEXP (rtl, 0),
15660 rsize - dsize));
15661 }
15662
15663 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15664 and will have been substituted directly into all expressions that use it.
15665 C does not have such a concept, but C++ and other languages do. */
15666 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15667 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15668
15669 if (rtl)
15670 rtl = targetm.delegitimize_address (rtl);
15671
15672 /* If we don't look past the constant pool, we risk emitting a
15673 reference to a constant pool entry that isn't referenced from
15674 code, and thus is not emitted. */
15675 if (rtl)
15676 rtl = avoid_constant_pool_reference (rtl);
15677
15678 /* Try harder to get a rtl. If this symbol ends up not being emitted
15679 in the current CU, resolve_addr will remove the expression referencing
15680 it. */
15681 if (rtl == NULL_RTX
15682 && TREE_CODE (decl) == VAR_DECL
15683 && !DECL_EXTERNAL (decl)
15684 && TREE_STATIC (decl)
15685 && DECL_NAME (decl)
15686 && !DECL_HARD_REGISTER (decl)
15687 && DECL_MODE (decl) != VOIDmode)
15688 {
15689 rtl = make_decl_rtl_for_debug (decl);
15690 if (!MEM_P (rtl)
15691 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15692 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15693 rtl = NULL_RTX;
15694 }
15695
15696 return rtl;
15697 }
15698
15699 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15700 returned. If so, the decl for the COMMON block is returned, and the
15701 value is the offset into the common block for the symbol. */
15702
15703 static tree
15704 fortran_common (tree decl, HOST_WIDE_INT *value)
15705 {
15706 tree val_expr, cvar;
15707 enum machine_mode mode;
15708 HOST_WIDE_INT bitsize, bitpos;
15709 tree offset;
15710 int unsignedp, volatilep = 0;
15711
15712 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15713 it does not have a value (the offset into the common area), or if it
15714 is thread local (as opposed to global) then it isn't common, and shouldn't
15715 be handled as such. */
15716 if (TREE_CODE (decl) != VAR_DECL
15717 || !TREE_STATIC (decl)
15718 || !DECL_HAS_VALUE_EXPR_P (decl)
15719 || !is_fortran ())
15720 return NULL_TREE;
15721
15722 val_expr = DECL_VALUE_EXPR (decl);
15723 if (TREE_CODE (val_expr) != COMPONENT_REF)
15724 return NULL_TREE;
15725
15726 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15727 &mode, &unsignedp, &volatilep, true);
15728
15729 if (cvar == NULL_TREE
15730 || TREE_CODE (cvar) != VAR_DECL
15731 || DECL_ARTIFICIAL (cvar)
15732 || !TREE_PUBLIC (cvar))
15733 return NULL_TREE;
15734
15735 *value = 0;
15736 if (offset != NULL)
15737 {
15738 if (!tree_fits_shwi_p (offset))
15739 return NULL_TREE;
15740 *value = tree_to_shwi (offset);
15741 }
15742 if (bitpos != 0)
15743 *value += bitpos / BITS_PER_UNIT;
15744
15745 return cvar;
15746 }
15747
15748 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15749 data attribute for a variable or a parameter. We generate the
15750 DW_AT_const_value attribute only in those cases where the given variable
15751 or parameter does not have a true "location" either in memory or in a
15752 register. This can happen (for example) when a constant is passed as an
15753 actual argument in a call to an inline function. (It's possible that
15754 these things can crop up in other ways also.) Note that one type of
15755 constant value which can be passed into an inlined function is a constant
15756 pointer. This can happen for example if an actual argument in an inlined
15757 function call evaluates to a compile-time constant address.
15758
15759 CACHE_P is true if it is worth caching the location list for DECL,
15760 so that future calls can reuse it rather than regenerate it from scratch.
15761 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15762 since we will need to refer to them each time the function is inlined. */
15763
15764 static bool
15765 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15766 enum dwarf_attribute attr)
15767 {
15768 rtx rtl;
15769 dw_loc_list_ref list;
15770 var_loc_list *loc_list;
15771 cached_dw_loc_list *cache;
15772 void **slot;
15773
15774 if (TREE_CODE (decl) == ERROR_MARK)
15775 return false;
15776
15777 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15778 || TREE_CODE (decl) == RESULT_DECL);
15779
15780 /* Try to get some constant RTL for this decl, and use that as the value of
15781 the location. */
15782
15783 rtl = rtl_for_decl_location (decl);
15784 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15785 && add_const_value_attribute (die, rtl))
15786 return true;
15787
15788 /* See if we have single element location list that is equivalent to
15789 a constant value. That way we are better to use add_const_value_attribute
15790 rather than expanding constant value equivalent. */
15791 loc_list = lookup_decl_loc (decl);
15792 if (loc_list
15793 && loc_list->first
15794 && loc_list->first->next == NULL
15795 && NOTE_P (loc_list->first->loc)
15796 && NOTE_VAR_LOCATION (loc_list->first->loc)
15797 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15798 {
15799 struct var_loc_node *node;
15800
15801 node = loc_list->first;
15802 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15803 if (GET_CODE (rtl) == EXPR_LIST)
15804 rtl = XEXP (rtl, 0);
15805 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15806 && add_const_value_attribute (die, rtl))
15807 return true;
15808 }
15809 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15810 list several times. See if we've already cached the contents. */
15811 list = NULL;
15812 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15813 cache_p = false;
15814 if (cache_p)
15815 {
15816 cache = (cached_dw_loc_list *)
15817 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15818 if (cache)
15819 list = cache->loc_list;
15820 }
15821 if (list == NULL)
15822 {
15823 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15824 /* It is usually worth caching this result if the decl is from
15825 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15826 if (cache_p && list && list->dw_loc_next)
15827 {
15828 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15829 DECL_UID (decl), INSERT);
15830 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
15831 cache->decl_id = DECL_UID (decl);
15832 cache->loc_list = list;
15833 *slot = cache;
15834 }
15835 }
15836 if (list)
15837 {
15838 add_AT_location_description (die, attr, list);
15839 return true;
15840 }
15841 /* None of that worked, so it must not really have a location;
15842 try adding a constant value attribute from the DECL_INITIAL. */
15843 return tree_add_const_value_attribute_for_decl (die, decl);
15844 }
15845
15846 /* Add VARIABLE and DIE into deferred locations list. */
15847
15848 static void
15849 defer_location (tree variable, dw_die_ref die)
15850 {
15851 deferred_locations entry;
15852 entry.variable = variable;
15853 entry.die = die;
15854 vec_safe_push (deferred_locations_list, entry);
15855 }
15856
15857 /* Helper function for tree_add_const_value_attribute. Natively encode
15858 initializer INIT into an array. Return true if successful. */
15859
15860 static bool
15861 native_encode_initializer (tree init, unsigned char *array, int size)
15862 {
15863 tree type;
15864
15865 if (init == NULL_TREE)
15866 return false;
15867
15868 STRIP_NOPS (init);
15869 switch (TREE_CODE (init))
15870 {
15871 case STRING_CST:
15872 type = TREE_TYPE (init);
15873 if (TREE_CODE (type) == ARRAY_TYPE)
15874 {
15875 tree enttype = TREE_TYPE (type);
15876 enum machine_mode mode = TYPE_MODE (enttype);
15877
15878 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15879 return false;
15880 if (int_size_in_bytes (type) != size)
15881 return false;
15882 if (size > TREE_STRING_LENGTH (init))
15883 {
15884 memcpy (array, TREE_STRING_POINTER (init),
15885 TREE_STRING_LENGTH (init));
15886 memset (array + TREE_STRING_LENGTH (init),
15887 '\0', size - TREE_STRING_LENGTH (init));
15888 }
15889 else
15890 memcpy (array, TREE_STRING_POINTER (init), size);
15891 return true;
15892 }
15893 return false;
15894 case CONSTRUCTOR:
15895 type = TREE_TYPE (init);
15896 if (int_size_in_bytes (type) != size)
15897 return false;
15898 if (TREE_CODE (type) == ARRAY_TYPE)
15899 {
15900 HOST_WIDE_INT min_index;
15901 unsigned HOST_WIDE_INT cnt;
15902 int curpos = 0, fieldsize;
15903 constructor_elt *ce;
15904
15905 if (TYPE_DOMAIN (type) == NULL_TREE
15906 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15907 return false;
15908
15909 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15910 if (fieldsize <= 0)
15911 return false;
15912
15913 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15914 memset (array, '\0', size);
15915 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15916 {
15917 tree val = ce->value;
15918 tree index = ce->index;
15919 int pos = curpos;
15920 if (index && TREE_CODE (index) == RANGE_EXPR)
15921 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15922 * fieldsize;
15923 else if (index)
15924 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15925
15926 if (val)
15927 {
15928 STRIP_NOPS (val);
15929 if (!native_encode_initializer (val, array + pos, fieldsize))
15930 return false;
15931 }
15932 curpos = pos + fieldsize;
15933 if (index && TREE_CODE (index) == RANGE_EXPR)
15934 {
15935 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15936 - tree_to_shwi (TREE_OPERAND (index, 0));
15937 while (count-- > 0)
15938 {
15939 if (val)
15940 memcpy (array + curpos, array + pos, fieldsize);
15941 curpos += fieldsize;
15942 }
15943 }
15944 gcc_assert (curpos <= size);
15945 }
15946 return true;
15947 }
15948 else if (TREE_CODE (type) == RECORD_TYPE
15949 || TREE_CODE (type) == UNION_TYPE)
15950 {
15951 tree field = NULL_TREE;
15952 unsigned HOST_WIDE_INT cnt;
15953 constructor_elt *ce;
15954
15955 if (int_size_in_bytes (type) != size)
15956 return false;
15957
15958 if (TREE_CODE (type) == RECORD_TYPE)
15959 field = TYPE_FIELDS (type);
15960
15961 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15962 {
15963 tree val = ce->value;
15964 int pos, fieldsize;
15965
15966 if (ce->index != 0)
15967 field = ce->index;
15968
15969 if (val)
15970 STRIP_NOPS (val);
15971
15972 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15973 return false;
15974
15975 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15976 && TYPE_DOMAIN (TREE_TYPE (field))
15977 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15978 return false;
15979 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15980 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
15981 return false;
15982 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
15983 pos = int_byte_position (field);
15984 gcc_assert (pos + fieldsize <= size);
15985 if (val
15986 && !native_encode_initializer (val, array + pos, fieldsize))
15987 return false;
15988 }
15989 return true;
15990 }
15991 return false;
15992 case VIEW_CONVERT_EXPR:
15993 case NON_LVALUE_EXPR:
15994 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15995 default:
15996 return native_encode_expr (init, array, size) == size;
15997 }
15998 }
15999
16000 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16001 attribute is the const value T. */
16002
16003 static bool
16004 tree_add_const_value_attribute (dw_die_ref die, tree t)
16005 {
16006 tree init;
16007 tree type = TREE_TYPE (t);
16008 rtx rtl;
16009
16010 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16011 return false;
16012
16013 init = t;
16014 gcc_assert (!DECL_P (init));
16015
16016 rtl = rtl_for_decl_init (init, type);
16017 if (rtl)
16018 return add_const_value_attribute (die, rtl);
16019 /* If the host and target are sane, try harder. */
16020 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16021 && initializer_constant_valid_p (init, type))
16022 {
16023 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16024 if (size > 0 && (int) size == size)
16025 {
16026 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16027
16028 if (native_encode_initializer (init, array, size))
16029 {
16030 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16031 return true;
16032 }
16033 ggc_free (array);
16034 }
16035 }
16036 return false;
16037 }
16038
16039 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16040 attribute is the const value of T, where T is an integral constant
16041 variable with static storage duration
16042 (so it can't be a PARM_DECL or a RESULT_DECL). */
16043
16044 static bool
16045 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16046 {
16047
16048 if (!decl
16049 || (TREE_CODE (decl) != VAR_DECL
16050 && TREE_CODE (decl) != CONST_DECL)
16051 || (TREE_CODE (decl) == VAR_DECL
16052 && !TREE_STATIC (decl)))
16053 return false;
16054
16055 if (TREE_READONLY (decl)
16056 && ! TREE_THIS_VOLATILE (decl)
16057 && DECL_INITIAL (decl))
16058 /* OK */;
16059 else
16060 return false;
16061
16062 /* Don't add DW_AT_const_value if abstract origin already has one. */
16063 if (get_AT (var_die, DW_AT_const_value))
16064 return false;
16065
16066 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16067 }
16068
16069 /* Convert the CFI instructions for the current function into a
16070 location list. This is used for DW_AT_frame_base when we targeting
16071 a dwarf2 consumer that does not support the dwarf3
16072 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16073 expressions. */
16074
16075 static dw_loc_list_ref
16076 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16077 {
16078 int ix;
16079 dw_fde_ref fde;
16080 dw_loc_list_ref list, *list_tail;
16081 dw_cfi_ref cfi;
16082 dw_cfa_location last_cfa, next_cfa;
16083 const char *start_label, *last_label, *section;
16084 dw_cfa_location remember;
16085
16086 fde = cfun->fde;
16087 gcc_assert (fde != NULL);
16088
16089 section = secname_for_decl (current_function_decl);
16090 list_tail = &list;
16091 list = NULL;
16092
16093 memset (&next_cfa, 0, sizeof (next_cfa));
16094 next_cfa.reg = INVALID_REGNUM;
16095 remember = next_cfa;
16096
16097 start_label = fde->dw_fde_begin;
16098
16099 /* ??? Bald assumption that the CIE opcode list does not contain
16100 advance opcodes. */
16101 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16102 lookup_cfa_1 (cfi, &next_cfa, &remember);
16103
16104 last_cfa = next_cfa;
16105 last_label = start_label;
16106
16107 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16108 {
16109 /* If the first partition contained no CFI adjustments, the
16110 CIE opcodes apply to the whole first partition. */
16111 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16112 fde->dw_fde_begin, fde->dw_fde_end, section);
16113 list_tail =&(*list_tail)->dw_loc_next;
16114 start_label = last_label = fde->dw_fde_second_begin;
16115 }
16116
16117 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16118 {
16119 switch (cfi->dw_cfi_opc)
16120 {
16121 case DW_CFA_set_loc:
16122 case DW_CFA_advance_loc1:
16123 case DW_CFA_advance_loc2:
16124 case DW_CFA_advance_loc4:
16125 if (!cfa_equal_p (&last_cfa, &next_cfa))
16126 {
16127 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16128 start_label, last_label, section);
16129
16130 list_tail = &(*list_tail)->dw_loc_next;
16131 last_cfa = next_cfa;
16132 start_label = last_label;
16133 }
16134 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16135 break;
16136
16137 case DW_CFA_advance_loc:
16138 /* The encoding is complex enough that we should never emit this. */
16139 gcc_unreachable ();
16140
16141 default:
16142 lookup_cfa_1 (cfi, &next_cfa, &remember);
16143 break;
16144 }
16145 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16146 {
16147 if (!cfa_equal_p (&last_cfa, &next_cfa))
16148 {
16149 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16150 start_label, last_label, section);
16151
16152 list_tail = &(*list_tail)->dw_loc_next;
16153 last_cfa = next_cfa;
16154 start_label = last_label;
16155 }
16156 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16157 start_label, fde->dw_fde_end, section);
16158 list_tail = &(*list_tail)->dw_loc_next;
16159 start_label = last_label = fde->dw_fde_second_begin;
16160 }
16161 }
16162
16163 if (!cfa_equal_p (&last_cfa, &next_cfa))
16164 {
16165 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16166 start_label, last_label, section);
16167 list_tail = &(*list_tail)->dw_loc_next;
16168 start_label = last_label;
16169 }
16170
16171 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16172 start_label,
16173 fde->dw_fde_second_begin
16174 ? fde->dw_fde_second_end : fde->dw_fde_end,
16175 section);
16176
16177 if (list && list->dw_loc_next)
16178 gen_llsym (list);
16179
16180 return list;
16181 }
16182
16183 /* Compute a displacement from the "steady-state frame pointer" to the
16184 frame base (often the same as the CFA), and store it in
16185 frame_pointer_fb_offset. OFFSET is added to the displacement
16186 before the latter is negated. */
16187
16188 static void
16189 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16190 {
16191 rtx reg, elim;
16192
16193 #ifdef FRAME_POINTER_CFA_OFFSET
16194 reg = frame_pointer_rtx;
16195 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16196 #else
16197 reg = arg_pointer_rtx;
16198 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16199 #endif
16200
16201 elim = (ira_use_lra_p
16202 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16203 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16204 if (GET_CODE (elim) == PLUS)
16205 {
16206 offset += INTVAL (XEXP (elim, 1));
16207 elim = XEXP (elim, 0);
16208 }
16209
16210 frame_pointer_fb_offset = -offset;
16211
16212 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16213 in which to eliminate. This is because it's stack pointer isn't
16214 directly accessible as a register within the ISA. To work around
16215 this, assume that while we cannot provide a proper value for
16216 frame_pointer_fb_offset, we won't need one either. */
16217 frame_pointer_fb_offset_valid
16218 = ((SUPPORTS_STACK_ALIGNMENT
16219 && (elim == hard_frame_pointer_rtx
16220 || elim == stack_pointer_rtx))
16221 || elim == (frame_pointer_needed
16222 ? hard_frame_pointer_rtx
16223 : stack_pointer_rtx));
16224 }
16225
16226 /* Generate a DW_AT_name attribute given some string value to be included as
16227 the value of the attribute. */
16228
16229 static void
16230 add_name_attribute (dw_die_ref die, const char *name_string)
16231 {
16232 if (name_string != NULL && *name_string != 0)
16233 {
16234 if (demangle_name_func)
16235 name_string = (*demangle_name_func) (name_string);
16236
16237 add_AT_string (die, DW_AT_name, name_string);
16238 }
16239 }
16240
16241 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16242 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16243 of TYPE accordingly.
16244
16245 ??? This is a temporary measure until after we're able to generate
16246 regular DWARF for the complex Ada type system. */
16247
16248 static void
16249 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16250 dw_die_ref context_die)
16251 {
16252 tree dtype;
16253 dw_die_ref dtype_die;
16254
16255 if (!lang_hooks.types.descriptive_type)
16256 return;
16257
16258 dtype = lang_hooks.types.descriptive_type (type);
16259 if (!dtype)
16260 return;
16261
16262 dtype_die = lookup_type_die (dtype);
16263 if (!dtype_die)
16264 {
16265 gen_type_die (dtype, context_die);
16266 dtype_die = lookup_type_die (dtype);
16267 gcc_assert (dtype_die);
16268 }
16269
16270 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16271 }
16272
16273 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16274
16275 static const char *
16276 comp_dir_string (void)
16277 {
16278 const char *wd;
16279 char *wd1;
16280 static const char *cached_wd = NULL;
16281
16282 if (cached_wd != NULL)
16283 return cached_wd;
16284
16285 wd = get_src_pwd ();
16286 if (wd == NULL)
16287 return NULL;
16288
16289 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16290 {
16291 int wdlen;
16292
16293 wdlen = strlen (wd);
16294 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16295 strcpy (wd1, wd);
16296 wd1 [wdlen] = DIR_SEPARATOR;
16297 wd1 [wdlen + 1] = 0;
16298 wd = wd1;
16299 }
16300
16301 cached_wd = remap_debug_filename (wd);
16302 return cached_wd;
16303 }
16304
16305 /* Generate a DW_AT_comp_dir attribute for DIE. */
16306
16307 static void
16308 add_comp_dir_attribute (dw_die_ref die)
16309 {
16310 const char * wd = comp_dir_string ();
16311 if (wd != NULL)
16312 add_AT_string (die, DW_AT_comp_dir, wd);
16313 }
16314
16315 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16316 default. */
16317
16318 static int
16319 lower_bound_default (void)
16320 {
16321 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16322 {
16323 case DW_LANG_C:
16324 case DW_LANG_C89:
16325 case DW_LANG_C99:
16326 case DW_LANG_C_plus_plus:
16327 case DW_LANG_ObjC:
16328 case DW_LANG_ObjC_plus_plus:
16329 case DW_LANG_Java:
16330 return 0;
16331 case DW_LANG_Fortran77:
16332 case DW_LANG_Fortran90:
16333 case DW_LANG_Fortran95:
16334 return 1;
16335 case DW_LANG_UPC:
16336 case DW_LANG_D:
16337 case DW_LANG_Python:
16338 return dwarf_version >= 4 ? 0 : -1;
16339 case DW_LANG_Ada95:
16340 case DW_LANG_Ada83:
16341 case DW_LANG_Cobol74:
16342 case DW_LANG_Cobol85:
16343 case DW_LANG_Pascal83:
16344 case DW_LANG_Modula2:
16345 case DW_LANG_PLI:
16346 return dwarf_version >= 4 ? 1 : -1;
16347 default:
16348 return -1;
16349 }
16350 }
16351
16352 /* Given a tree node describing an array bound (either lower or upper) output
16353 a representation for that bound. */
16354
16355 static void
16356 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16357 {
16358 switch (TREE_CODE (bound))
16359 {
16360 case ERROR_MARK:
16361 return;
16362
16363 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16364 case INTEGER_CST:
16365 {
16366 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16367 int dflt;
16368
16369 /* Use the default if possible. */
16370 if (bound_attr == DW_AT_lower_bound
16371 && tree_fits_shwi_p (bound)
16372 && (dflt = lower_bound_default ()) != -1
16373 && tree_to_shwi (bound) == dflt)
16374 ;
16375
16376 /* If HOST_WIDE_INT is big enough then represent the bound as
16377 a constant value. We need to choose a form based on
16378 whether the type is signed or unsigned. We cannot just
16379 call add_AT_unsigned if the value itself is positive
16380 (add_AT_unsigned might add the unsigned value encoded as
16381 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16382 bounds type and then sign extend any unsigned values found
16383 for signed types. This is needed only for
16384 DW_AT_{lower,upper}_bound, since for most other attributes,
16385 consumers will treat DW_FORM_data[1248] as unsigned values,
16386 regardless of the underlying type. */
16387 else if (prec <= HOST_BITS_PER_WIDE_INT
16388 || tree_fits_uhwi_p (bound))
16389 {
16390 if (TYPE_UNSIGNED (TREE_TYPE (bound)))
16391 add_AT_unsigned (subrange_die, bound_attr,
16392 TREE_INT_CST_LOW (bound));
16393 else
16394 add_AT_int (subrange_die, bound_attr, TREE_INT_CST_LOW (bound));
16395 }
16396 else
16397 /* Otherwise represent the bound as an unsigned value with
16398 the precision of its type. The precision and signedness
16399 of the type will be necessary to re-interpret it
16400 unambiguously. */
16401 add_AT_wide (subrange_die, bound_attr, bound);
16402 }
16403 break;
16404
16405 CASE_CONVERT:
16406 case VIEW_CONVERT_EXPR:
16407 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16408 break;
16409
16410 case SAVE_EXPR:
16411 break;
16412
16413 case VAR_DECL:
16414 case PARM_DECL:
16415 case RESULT_DECL:
16416 {
16417 dw_die_ref decl_die = lookup_decl_die (bound);
16418
16419 /* ??? Can this happen, or should the variable have been bound
16420 first? Probably it can, since I imagine that we try to create
16421 the types of parameters in the order in which they exist in
16422 the list, and won't have created a forward reference to a
16423 later parameter. */
16424 if (decl_die != NULL)
16425 {
16426 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16427 break;
16428 }
16429 }
16430 /* FALLTHRU */
16431
16432 default:
16433 {
16434 /* Otherwise try to create a stack operation procedure to
16435 evaluate the value of the array bound. */
16436
16437 dw_die_ref ctx, decl_die;
16438 dw_loc_list_ref list;
16439
16440 list = loc_list_from_tree (bound, 2);
16441 if (list == NULL || single_element_loc_list_p (list))
16442 {
16443 /* If DW_AT_*bound is not a reference nor constant, it is
16444 a DWARF expression rather than location description.
16445 For that loc_list_from_tree (bound, 0) is needed.
16446 If that fails to give a single element list,
16447 fall back to outputting this as a reference anyway. */
16448 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16449 if (list2 && single_element_loc_list_p (list2))
16450 {
16451 add_AT_loc (subrange_die, bound_attr, list2->expr);
16452 break;
16453 }
16454 }
16455 if (list == NULL)
16456 break;
16457
16458 if (current_function_decl == 0)
16459 ctx = comp_unit_die ();
16460 else
16461 ctx = lookup_decl_die (current_function_decl);
16462
16463 decl_die = new_die (DW_TAG_variable, ctx, bound);
16464 add_AT_flag (decl_die, DW_AT_artificial, 1);
16465 add_type_attribute (decl_die, TREE_TYPE (bound), TYPE_QUAL_CONST, ctx);
16466 add_AT_location_description (decl_die, DW_AT_location, list);
16467 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16468 break;
16469 }
16470 }
16471 }
16472
16473 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16474 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16475 Note that the block of subscript information for an array type also
16476 includes information about the element type of the given array type. */
16477
16478 static void
16479 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16480 {
16481 unsigned dimension_number;
16482 tree lower, upper;
16483 dw_die_ref subrange_die;
16484
16485 for (dimension_number = 0;
16486 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16487 type = TREE_TYPE (type), dimension_number++)
16488 {
16489 tree domain = TYPE_DOMAIN (type);
16490
16491 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16492 break;
16493
16494 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16495 and (in GNU C only) variable bounds. Handle all three forms
16496 here. */
16497 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16498 if (domain)
16499 {
16500 /* We have an array type with specified bounds. */
16501 lower = TYPE_MIN_VALUE (domain);
16502 upper = TYPE_MAX_VALUE (domain);
16503
16504 /* Define the index type. */
16505 if (TREE_TYPE (domain))
16506 {
16507 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16508 TREE_TYPE field. We can't emit debug info for this
16509 because it is an unnamed integral type. */
16510 if (TREE_CODE (domain) == INTEGER_TYPE
16511 && TYPE_NAME (domain) == NULL_TREE
16512 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16513 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16514 ;
16515 else
16516 add_type_attribute (subrange_die, TREE_TYPE (domain),
16517 TYPE_UNQUALIFIED, type_die);
16518 }
16519
16520 /* ??? If upper is NULL, the array has unspecified length,
16521 but it does have a lower bound. This happens with Fortran
16522 dimension arr(N:*)
16523 Since the debugger is definitely going to need to know N
16524 to produce useful results, go ahead and output the lower
16525 bound solo, and hope the debugger can cope. */
16526
16527 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16528 if (upper)
16529 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16530 }
16531
16532 /* Otherwise we have an array type with an unspecified length. The
16533 DWARF-2 spec does not say how to handle this; let's just leave out the
16534 bounds. */
16535 }
16536 }
16537
16538 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16539
16540 static void
16541 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16542 {
16543 dw_die_ref decl_die;
16544 HOST_WIDE_INT size;
16545
16546 switch (TREE_CODE (tree_node))
16547 {
16548 case ERROR_MARK:
16549 size = 0;
16550 break;
16551 case ENUMERAL_TYPE:
16552 case RECORD_TYPE:
16553 case UNION_TYPE:
16554 case QUAL_UNION_TYPE:
16555 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16556 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16557 {
16558 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16559 return;
16560 }
16561 size = int_size_in_bytes (tree_node);
16562 break;
16563 case FIELD_DECL:
16564 /* For a data member of a struct or union, the DW_AT_byte_size is
16565 generally given as the number of bytes normally allocated for an
16566 object of the *declared* type of the member itself. This is true
16567 even for bit-fields. */
16568 size = int_size_in_bytes (field_type (tree_node));
16569 break;
16570 default:
16571 gcc_unreachable ();
16572 }
16573
16574 /* Note that `size' might be -1 when we get to this point. If it is, that
16575 indicates that the byte size of the entity in question is variable. We
16576 have no good way of expressing this fact in Dwarf at the present time,
16577 when location description was not used by the caller code instead. */
16578 if (size >= 0)
16579 add_AT_unsigned (die, DW_AT_byte_size, size);
16580 }
16581
16582 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16583 which specifies the distance in bits from the highest order bit of the
16584 "containing object" for the bit-field to the highest order bit of the
16585 bit-field itself.
16586
16587 For any given bit-field, the "containing object" is a hypothetical object
16588 (of some integral or enum type) within which the given bit-field lives. The
16589 type of this hypothetical "containing object" is always the same as the
16590 declared type of the individual bit-field itself. The determination of the
16591 exact location of the "containing object" for a bit-field is rather
16592 complicated. It's handled by the `field_byte_offset' function (above).
16593
16594 Note that it is the size (in bytes) of the hypothetical "containing object"
16595 which will be given in the DW_AT_byte_size attribute for this bit-field.
16596 (See `byte_size_attribute' above). */
16597
16598 static inline void
16599 add_bit_offset_attribute (dw_die_ref die, tree decl)
16600 {
16601 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16602 tree type = DECL_BIT_FIELD_TYPE (decl);
16603 HOST_WIDE_INT bitpos_int;
16604 HOST_WIDE_INT highest_order_object_bit_offset;
16605 HOST_WIDE_INT highest_order_field_bit_offset;
16606 HOST_WIDE_INT bit_offset;
16607
16608 /* Must be a field and a bit field. */
16609 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16610
16611 /* We can't yet handle bit-fields whose offsets are variable, so if we
16612 encounter such things, just return without generating any attribute
16613 whatsoever. Likewise for variable or too large size. */
16614 if (! tree_fits_shwi_p (bit_position (decl))
16615 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16616 return;
16617
16618 bitpos_int = int_bit_position (decl);
16619
16620 /* Note that the bit offset is always the distance (in bits) from the
16621 highest-order bit of the "containing object" to the highest-order bit of
16622 the bit-field itself. Since the "high-order end" of any object or field
16623 is different on big-endian and little-endian machines, the computation
16624 below must take account of these differences. */
16625 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16626 highest_order_field_bit_offset = bitpos_int;
16627
16628 if (! BYTES_BIG_ENDIAN)
16629 {
16630 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16631 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16632 }
16633
16634 bit_offset
16635 = (! BYTES_BIG_ENDIAN
16636 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16637 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16638
16639 if (bit_offset < 0)
16640 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16641 else
16642 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16643 }
16644
16645 /* For a FIELD_DECL node which represents a bit field, output an attribute
16646 which specifies the length in bits of the given field. */
16647
16648 static inline void
16649 add_bit_size_attribute (dw_die_ref die, tree decl)
16650 {
16651 /* Must be a field and a bit field. */
16652 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16653 && DECL_BIT_FIELD_TYPE (decl));
16654
16655 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16656 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16657 }
16658
16659 /* If the compiled language is ANSI C, then add a 'prototyped'
16660 attribute, if arg types are given for the parameters of a function. */
16661
16662 static inline void
16663 add_prototyped_attribute (dw_die_ref die, tree func_type)
16664 {
16665 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16666 && prototype_p (func_type))
16667 add_AT_flag (die, DW_AT_prototyped, 1);
16668 }
16669
16670 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16671 by looking in either the type declaration or object declaration
16672 equate table. */
16673
16674 static inline dw_die_ref
16675 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16676 {
16677 dw_die_ref origin_die = NULL;
16678
16679 if (TREE_CODE (origin) != FUNCTION_DECL)
16680 {
16681 /* We may have gotten separated from the block for the inlined
16682 function, if we're in an exception handler or some such; make
16683 sure that the abstract function has been written out.
16684
16685 Doing this for nested functions is wrong, however; functions are
16686 distinct units, and our context might not even be inline. */
16687 tree fn = origin;
16688
16689 if (TYPE_P (fn))
16690 fn = TYPE_STUB_DECL (fn);
16691
16692 fn = decl_function_context (fn);
16693 if (fn)
16694 dwarf2out_abstract_function (fn);
16695 }
16696
16697 if (DECL_P (origin))
16698 origin_die = lookup_decl_die (origin);
16699 else if (TYPE_P (origin))
16700 origin_die = lookup_type_die (origin);
16701
16702 /* XXX: Functions that are never lowered don't always have correct block
16703 trees (in the case of java, they simply have no block tree, in some other
16704 languages). For these functions, there is nothing we can really do to
16705 output correct debug info for inlined functions in all cases. Rather
16706 than die, we'll just produce deficient debug info now, in that we will
16707 have variables without a proper abstract origin. In the future, when all
16708 functions are lowered, we should re-add a gcc_assert (origin_die)
16709 here. */
16710
16711 if (origin_die)
16712 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16713 return origin_die;
16714 }
16715
16716 /* We do not currently support the pure_virtual attribute. */
16717
16718 static inline void
16719 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16720 {
16721 if (DECL_VINDEX (func_decl))
16722 {
16723 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16724
16725 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16726 add_AT_loc (die, DW_AT_vtable_elem_location,
16727 new_loc_descr (DW_OP_constu,
16728 tree_to_shwi (DECL_VINDEX (func_decl)),
16729 0));
16730
16731 /* GNU extension: Record what type this method came from originally. */
16732 if (debug_info_level > DINFO_LEVEL_TERSE
16733 && DECL_CONTEXT (func_decl))
16734 add_AT_die_ref (die, DW_AT_containing_type,
16735 lookup_type_die (DECL_CONTEXT (func_decl)));
16736 }
16737 }
16738 \f
16739 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16740 given decl. This used to be a vendor extension until after DWARF 4
16741 standardized it. */
16742
16743 static void
16744 add_linkage_attr (dw_die_ref die, tree decl)
16745 {
16746 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16747
16748 /* Mimic what assemble_name_raw does with a leading '*'. */
16749 if (name[0] == '*')
16750 name = &name[1];
16751
16752 if (dwarf_version >= 4)
16753 add_AT_string (die, DW_AT_linkage_name, name);
16754 else
16755 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16756 }
16757
16758 /* Add source coordinate attributes for the given decl. */
16759
16760 static void
16761 add_src_coords_attributes (dw_die_ref die, tree decl)
16762 {
16763 expanded_location s;
16764
16765 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16766 return;
16767 s = expand_location (DECL_SOURCE_LOCATION (decl));
16768 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16769 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16770 }
16771
16772 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16773
16774 static void
16775 add_linkage_name (dw_die_ref die, tree decl)
16776 {
16777 if (debug_info_level > DINFO_LEVEL_NONE
16778 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16779 && TREE_PUBLIC (decl)
16780 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16781 && die->die_tag != DW_TAG_member)
16782 {
16783 /* Defer until we have an assembler name set. */
16784 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16785 {
16786 limbo_die_node *asm_name;
16787
16788 asm_name = ggc_cleared_alloc<limbo_die_node> ();
16789 asm_name->die = die;
16790 asm_name->created_for = decl;
16791 asm_name->next = deferred_asm_name;
16792 deferred_asm_name = asm_name;
16793 }
16794 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16795 add_linkage_attr (die, decl);
16796 }
16797 }
16798
16799 /* Add a DW_AT_name attribute and source coordinate attribute for the
16800 given decl, but only if it actually has a name. */
16801
16802 static void
16803 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16804 {
16805 tree decl_name;
16806
16807 decl_name = DECL_NAME (decl);
16808 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16809 {
16810 const char *name = dwarf2_name (decl, 0);
16811 if (name)
16812 add_name_attribute (die, name);
16813 if (! DECL_ARTIFICIAL (decl))
16814 add_src_coords_attributes (die, decl);
16815
16816 add_linkage_name (die, decl);
16817 }
16818
16819 #ifdef VMS_DEBUGGING_INFO
16820 /* Get the function's name, as described by its RTL. This may be different
16821 from the DECL_NAME name used in the source file. */
16822 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16823 {
16824 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16825 XEXP (DECL_RTL (decl), 0), false);
16826 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16827 }
16828 #endif /* VMS_DEBUGGING_INFO */
16829 }
16830
16831 #ifdef VMS_DEBUGGING_INFO
16832 /* Output the debug main pointer die for VMS */
16833
16834 void
16835 dwarf2out_vms_debug_main_pointer (void)
16836 {
16837 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16838 dw_die_ref die;
16839
16840 /* Allocate the VMS debug main subprogram die. */
16841 die = ggc_cleared_alloc<die_node> ();
16842 die->die_tag = DW_TAG_subprogram;
16843 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16844 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16845 current_function_funcdef_no);
16846 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16847
16848 /* Make it the first child of comp_unit_die (). */
16849 die->die_parent = comp_unit_die ();
16850 if (comp_unit_die ()->die_child)
16851 {
16852 die->die_sib = comp_unit_die ()->die_child->die_sib;
16853 comp_unit_die ()->die_child->die_sib = die;
16854 }
16855 else
16856 {
16857 die->die_sib = die;
16858 comp_unit_die ()->die_child = die;
16859 }
16860 }
16861 #endif /* VMS_DEBUGGING_INFO */
16862
16863 /* Push a new declaration scope. */
16864
16865 static void
16866 push_decl_scope (tree scope)
16867 {
16868 vec_safe_push (decl_scope_table, scope);
16869 }
16870
16871 /* Pop a declaration scope. */
16872
16873 static inline void
16874 pop_decl_scope (void)
16875 {
16876 decl_scope_table->pop ();
16877 }
16878
16879 /* walk_tree helper function for uses_local_type, below. */
16880
16881 static tree
16882 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16883 {
16884 if (!TYPE_P (*tp))
16885 *walk_subtrees = 0;
16886 else
16887 {
16888 tree name = TYPE_NAME (*tp);
16889 if (name && DECL_P (name) && decl_function_context (name))
16890 return *tp;
16891 }
16892 return NULL_TREE;
16893 }
16894
16895 /* If TYPE involves a function-local type (including a local typedef to a
16896 non-local type), returns that type; otherwise returns NULL_TREE. */
16897
16898 static tree
16899 uses_local_type (tree type)
16900 {
16901 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16902 return used;
16903 }
16904
16905 /* Return the DIE for the scope that immediately contains this type.
16906 Non-named types that do not involve a function-local type get global
16907 scope. Named types nested in namespaces or other types get their
16908 containing scope. All other types (i.e. function-local named types) get
16909 the current active scope. */
16910
16911 static dw_die_ref
16912 scope_die_for (tree t, dw_die_ref context_die)
16913 {
16914 dw_die_ref scope_die = NULL;
16915 tree containing_scope;
16916
16917 /* Non-types always go in the current scope. */
16918 gcc_assert (TYPE_P (t));
16919
16920 /* Use the scope of the typedef, rather than the scope of the type
16921 it refers to. */
16922 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16923 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16924 else
16925 containing_scope = TYPE_CONTEXT (t);
16926
16927 /* Use the containing namespace if there is one. */
16928 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16929 {
16930 if (context_die == lookup_decl_die (containing_scope))
16931 /* OK */;
16932 else if (debug_info_level > DINFO_LEVEL_TERSE)
16933 context_die = get_context_die (containing_scope);
16934 else
16935 containing_scope = NULL_TREE;
16936 }
16937
16938 /* Ignore function type "scopes" from the C frontend. They mean that
16939 a tagged type is local to a parmlist of a function declarator, but
16940 that isn't useful to DWARF. */
16941 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16942 containing_scope = NULL_TREE;
16943
16944 if (SCOPE_FILE_SCOPE_P (containing_scope))
16945 {
16946 /* If T uses a local type keep it local as well, to avoid references
16947 to function-local DIEs from outside the function. */
16948 if (current_function_decl && uses_local_type (t))
16949 scope_die = context_die;
16950 else
16951 scope_die = comp_unit_die ();
16952 }
16953 else if (TYPE_P (containing_scope))
16954 {
16955 /* For types, we can just look up the appropriate DIE. */
16956 if (debug_info_level > DINFO_LEVEL_TERSE)
16957 scope_die = get_context_die (containing_scope);
16958 else
16959 {
16960 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16961 if (scope_die == NULL)
16962 scope_die = comp_unit_die ();
16963 }
16964 }
16965 else
16966 scope_die = context_die;
16967
16968 return scope_die;
16969 }
16970
16971 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16972
16973 static inline int
16974 local_scope_p (dw_die_ref context_die)
16975 {
16976 for (; context_die; context_die = context_die->die_parent)
16977 if (context_die->die_tag == DW_TAG_inlined_subroutine
16978 || context_die->die_tag == DW_TAG_subprogram)
16979 return 1;
16980
16981 return 0;
16982 }
16983
16984 /* Returns nonzero if CONTEXT_DIE is a class. */
16985
16986 static inline int
16987 class_scope_p (dw_die_ref context_die)
16988 {
16989 return (context_die
16990 && (context_die->die_tag == DW_TAG_structure_type
16991 || context_die->die_tag == DW_TAG_class_type
16992 || context_die->die_tag == DW_TAG_interface_type
16993 || context_die->die_tag == DW_TAG_union_type));
16994 }
16995
16996 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16997 whether or not to treat a DIE in this context as a declaration. */
16998
16999 static inline int
17000 class_or_namespace_scope_p (dw_die_ref context_die)
17001 {
17002 return (class_scope_p (context_die)
17003 || (context_die && context_die->die_tag == DW_TAG_namespace));
17004 }
17005
17006 /* Many forms of DIEs require a "type description" attribute. This
17007 routine locates the proper "type descriptor" die for the type given
17008 by 'type' plus any additional qualifiers given by 'cv_quals', and
17009 adds a DW_AT_type attribute below the given die. */
17010
17011 static void
17012 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17013 dw_die_ref context_die)
17014 {
17015 enum tree_code code = TREE_CODE (type);
17016 dw_die_ref type_die = NULL;
17017
17018 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17019 or fixed-point type, use the inner type. This is because we have no
17020 support for unnamed types in base_type_die. This can happen if this is
17021 an Ada subrange type. Correct solution is emit a subrange type die. */
17022 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17023 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17024 type = TREE_TYPE (type), code = TREE_CODE (type);
17025
17026 if (code == ERROR_MARK
17027 /* Handle a special case. For functions whose return type is void, we
17028 generate *no* type attribute. (Note that no object may have type
17029 `void', so this only applies to function return types). */
17030 || code == VOID_TYPE)
17031 return;
17032
17033 type_die = modified_type_die (type,
17034 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17035 context_die);
17036
17037 if (type_die != NULL)
17038 add_AT_die_ref (object_die, DW_AT_type, type_die);
17039 }
17040
17041 /* Given an object die, add the calling convention attribute for the
17042 function call type. */
17043 static void
17044 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17045 {
17046 enum dwarf_calling_convention value = DW_CC_normal;
17047
17048 value = ((enum dwarf_calling_convention)
17049 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17050
17051 if (is_fortran ()
17052 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17053 {
17054 /* DWARF 2 doesn't provide a way to identify a program's source-level
17055 entry point. DW_AT_calling_convention attributes are only meant
17056 to describe functions' calling conventions. However, lacking a
17057 better way to signal the Fortran main program, we used this for
17058 a long time, following existing custom. Now, DWARF 4 has
17059 DW_AT_main_subprogram, which we add below, but some tools still
17060 rely on the old way, which we thus keep. */
17061 value = DW_CC_program;
17062
17063 if (dwarf_version >= 4 || !dwarf_strict)
17064 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17065 }
17066
17067 /* Only add the attribute if the backend requests it, and
17068 is not DW_CC_normal. */
17069 if (value && (value != DW_CC_normal))
17070 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17071 }
17072
17073 /* Given a tree pointer to a struct, class, union, or enum type node, return
17074 a pointer to the (string) tag name for the given type, or zero if the type
17075 was declared without a tag. */
17076
17077 static const char *
17078 type_tag (const_tree type)
17079 {
17080 const char *name = 0;
17081
17082 if (TYPE_NAME (type) != 0)
17083 {
17084 tree t = 0;
17085
17086 /* Find the IDENTIFIER_NODE for the type name. */
17087 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17088 && !TYPE_NAMELESS (type))
17089 t = TYPE_NAME (type);
17090
17091 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17092 a TYPE_DECL node, regardless of whether or not a `typedef' was
17093 involved. */
17094 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17095 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17096 {
17097 /* We want to be extra verbose. Don't call dwarf_name if
17098 DECL_NAME isn't set. The default hook for decl_printable_name
17099 doesn't like that, and in this context it's correct to return
17100 0, instead of "<anonymous>" or the like. */
17101 if (DECL_NAME (TYPE_NAME (type))
17102 && !DECL_NAMELESS (TYPE_NAME (type)))
17103 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17104 }
17105
17106 /* Now get the name as a string, or invent one. */
17107 if (!name && t != 0)
17108 name = IDENTIFIER_POINTER (t);
17109 }
17110
17111 return (name == 0 || *name == '\0') ? 0 : name;
17112 }
17113
17114 /* Return the type associated with a data member, make a special check
17115 for bit field types. */
17116
17117 static inline tree
17118 member_declared_type (const_tree member)
17119 {
17120 return (DECL_BIT_FIELD_TYPE (member)
17121 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17122 }
17123
17124 /* Get the decl's label, as described by its RTL. This may be different
17125 from the DECL_NAME name used in the source file. */
17126
17127 #if 0
17128 static const char *
17129 decl_start_label (tree decl)
17130 {
17131 rtx x;
17132 const char *fnname;
17133
17134 x = DECL_RTL (decl);
17135 gcc_assert (MEM_P (x));
17136
17137 x = XEXP (x, 0);
17138 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17139
17140 fnname = XSTR (x, 0);
17141 return fnname;
17142 }
17143 #endif
17144 \f
17145 /* These routines generate the internal representation of the DIE's for
17146 the compilation unit. Debugging information is collected by walking
17147 the declaration trees passed in from dwarf2out_decl(). */
17148
17149 static void
17150 gen_array_type_die (tree type, dw_die_ref context_die)
17151 {
17152 dw_die_ref scope_die = scope_die_for (type, context_die);
17153 dw_die_ref array_die;
17154
17155 /* GNU compilers represent multidimensional array types as sequences of one
17156 dimensional array types whose element types are themselves array types.
17157 We sometimes squish that down to a single array_type DIE with multiple
17158 subscripts in the Dwarf debugging info. The draft Dwarf specification
17159 say that we are allowed to do this kind of compression in C, because
17160 there is no difference between an array of arrays and a multidimensional
17161 array. We don't do this for Ada to remain as close as possible to the
17162 actual representation, which is especially important against the language
17163 flexibilty wrt arrays of variable size. */
17164
17165 bool collapse_nested_arrays = !is_ada ();
17166 tree element_type;
17167
17168 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17169 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17170 if (TYPE_STRING_FLAG (type)
17171 && TREE_CODE (type) == ARRAY_TYPE
17172 && is_fortran ()
17173 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17174 {
17175 HOST_WIDE_INT size;
17176
17177 array_die = new_die (DW_TAG_string_type, scope_die, type);
17178 add_name_attribute (array_die, type_tag (type));
17179 equate_type_number_to_die (type, array_die);
17180 size = int_size_in_bytes (type);
17181 if (size >= 0)
17182 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17183 else if (TYPE_DOMAIN (type) != NULL_TREE
17184 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17185 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17186 {
17187 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17188 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17189
17190 size = int_size_in_bytes (TREE_TYPE (szdecl));
17191 if (loc && size > 0)
17192 {
17193 add_AT_location_description (array_die, DW_AT_string_length, loc);
17194 if (size != DWARF2_ADDR_SIZE)
17195 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17196 }
17197 }
17198 return;
17199 }
17200
17201 array_die = new_die (DW_TAG_array_type, scope_die, type);
17202 add_name_attribute (array_die, type_tag (type));
17203 equate_type_number_to_die (type, array_die);
17204
17205 if (TREE_CODE (type) == VECTOR_TYPE)
17206 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17207
17208 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17209 if (is_fortran ()
17210 && TREE_CODE (type) == ARRAY_TYPE
17211 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17212 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17213 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17214
17215 #if 0
17216 /* We default the array ordering. SDB will probably do
17217 the right things even if DW_AT_ordering is not present. It's not even
17218 an issue until we start to get into multidimensional arrays anyway. If
17219 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17220 then we'll have to put the DW_AT_ordering attribute back in. (But if
17221 and when we find out that we need to put these in, we will only do so
17222 for multidimensional arrays. */
17223 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17224 #endif
17225
17226 if (TREE_CODE (type) == VECTOR_TYPE)
17227 {
17228 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17229 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17230 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17231 add_bound_info (subrange_die, DW_AT_upper_bound,
17232 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17233 }
17234 else
17235 add_subscript_info (array_die, type, collapse_nested_arrays);
17236
17237 /* Add representation of the type of the elements of this array type and
17238 emit the corresponding DIE if we haven't done it already. */
17239 element_type = TREE_TYPE (type);
17240 if (collapse_nested_arrays)
17241 while (TREE_CODE (element_type) == ARRAY_TYPE)
17242 {
17243 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17244 break;
17245 element_type = TREE_TYPE (element_type);
17246 }
17247
17248 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17249
17250 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17251 if (TYPE_ARTIFICIAL (type))
17252 add_AT_flag (array_die, DW_AT_artificial, 1);
17253
17254 if (get_AT (array_die, DW_AT_name))
17255 add_pubtype (type, array_die);
17256 }
17257
17258 static dw_loc_descr_ref
17259 descr_info_loc (tree val, tree base_decl)
17260 {
17261 HOST_WIDE_INT size;
17262 dw_loc_descr_ref loc, loc2;
17263 enum dwarf_location_atom op;
17264
17265 if (val == base_decl)
17266 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17267
17268 switch (TREE_CODE (val))
17269 {
17270 CASE_CONVERT:
17271 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17272 case VAR_DECL:
17273 return loc_descriptor_from_tree (val, 0);
17274 case INTEGER_CST:
17275 if (tree_fits_shwi_p (val))
17276 return int_loc_descriptor (tree_to_shwi (val));
17277 break;
17278 case INDIRECT_REF:
17279 size = int_size_in_bytes (TREE_TYPE (val));
17280 if (size < 0)
17281 break;
17282 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17283 if (!loc)
17284 break;
17285 if (size == DWARF2_ADDR_SIZE)
17286 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17287 else
17288 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17289 return loc;
17290 case POINTER_PLUS_EXPR:
17291 case PLUS_EXPR:
17292 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17293 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17294 {
17295 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17296 if (!loc)
17297 break;
17298 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17299 }
17300 else
17301 {
17302 op = DW_OP_plus;
17303 do_binop:
17304 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17305 if (!loc)
17306 break;
17307 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17308 if (!loc2)
17309 break;
17310 add_loc_descr (&loc, loc2);
17311 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17312 }
17313 return loc;
17314 case MINUS_EXPR:
17315 op = DW_OP_minus;
17316 goto do_binop;
17317 case MULT_EXPR:
17318 op = DW_OP_mul;
17319 goto do_binop;
17320 case EQ_EXPR:
17321 op = DW_OP_eq;
17322 goto do_binop;
17323 case NE_EXPR:
17324 op = DW_OP_ne;
17325 goto do_binop;
17326 default:
17327 break;
17328 }
17329 return NULL;
17330 }
17331
17332 static void
17333 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17334 tree val, tree base_decl)
17335 {
17336 dw_loc_descr_ref loc;
17337
17338 if (tree_fits_shwi_p (val))
17339 {
17340 add_AT_unsigned (die, attr, tree_to_shwi (val));
17341 return;
17342 }
17343
17344 loc = descr_info_loc (val, base_decl);
17345 if (!loc)
17346 return;
17347
17348 add_AT_loc (die, attr, loc);
17349 }
17350
17351 /* This routine generates DIE for array with hidden descriptor, details
17352 are filled into *info by a langhook. */
17353
17354 static void
17355 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17356 dw_die_ref context_die)
17357 {
17358 dw_die_ref scope_die = scope_die_for (type, context_die);
17359 dw_die_ref array_die;
17360 int dim;
17361
17362 array_die = new_die (DW_TAG_array_type, scope_die, type);
17363 add_name_attribute (array_die, type_tag (type));
17364 equate_type_number_to_die (type, array_die);
17365
17366 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17367 if (is_fortran ()
17368 && info->ndimensions >= 2)
17369 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17370
17371 if (info->data_location)
17372 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17373 info->base_decl);
17374 if (info->associated)
17375 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17376 info->base_decl);
17377 if (info->allocated)
17378 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17379 info->base_decl);
17380
17381 for (dim = 0; dim < info->ndimensions; dim++)
17382 {
17383 dw_die_ref subrange_die
17384 = new_die (DW_TAG_subrange_type, array_die, NULL);
17385
17386 if (info->dimen[dim].lower_bound)
17387 {
17388 /* If it is the default value, omit it. */
17389 int dflt;
17390
17391 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17392 && (dflt = lower_bound_default ()) != -1
17393 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17394 ;
17395 else
17396 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17397 info->dimen[dim].lower_bound,
17398 info->base_decl);
17399 }
17400 if (info->dimen[dim].upper_bound)
17401 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17402 info->dimen[dim].upper_bound,
17403 info->base_decl);
17404 if (info->dimen[dim].stride)
17405 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17406 info->dimen[dim].stride,
17407 info->base_decl);
17408 }
17409
17410 gen_type_die (info->element_type, context_die);
17411 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17412 context_die);
17413
17414 if (get_AT (array_die, DW_AT_name))
17415 add_pubtype (type, array_die);
17416 }
17417
17418 #if 0
17419 static void
17420 gen_entry_point_die (tree decl, dw_die_ref context_die)
17421 {
17422 tree origin = decl_ultimate_origin (decl);
17423 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17424
17425 if (origin != NULL)
17426 add_abstract_origin_attribute (decl_die, origin);
17427 else
17428 {
17429 add_name_and_src_coords_attributes (decl_die, decl);
17430 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17431 TYPE_UNQUALIFIED, context_die);
17432 }
17433
17434 if (DECL_ABSTRACT (decl))
17435 equate_decl_number_to_die (decl, decl_die);
17436 else
17437 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17438 }
17439 #endif
17440
17441 /* Walk through the list of incomplete types again, trying once more to
17442 emit full debugging info for them. */
17443
17444 static void
17445 retry_incomplete_types (void)
17446 {
17447 int i;
17448
17449 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17450 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17451 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17452 }
17453
17454 /* Determine what tag to use for a record type. */
17455
17456 static enum dwarf_tag
17457 record_type_tag (tree type)
17458 {
17459 if (! lang_hooks.types.classify_record)
17460 return DW_TAG_structure_type;
17461
17462 switch (lang_hooks.types.classify_record (type))
17463 {
17464 case RECORD_IS_STRUCT:
17465 return DW_TAG_structure_type;
17466
17467 case RECORD_IS_CLASS:
17468 return DW_TAG_class_type;
17469
17470 case RECORD_IS_INTERFACE:
17471 if (dwarf_version >= 3 || !dwarf_strict)
17472 return DW_TAG_interface_type;
17473 return DW_TAG_structure_type;
17474
17475 default:
17476 gcc_unreachable ();
17477 }
17478 }
17479
17480 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17481 include all of the information about the enumeration values also. Each
17482 enumerated type name/value is listed as a child of the enumerated type
17483 DIE. */
17484
17485 static dw_die_ref
17486 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17487 {
17488 dw_die_ref type_die = lookup_type_die (type);
17489
17490 if (type_die == NULL)
17491 {
17492 type_die = new_die (DW_TAG_enumeration_type,
17493 scope_die_for (type, context_die), type);
17494 equate_type_number_to_die (type, type_die);
17495 add_name_attribute (type_die, type_tag (type));
17496 if (dwarf_version >= 4 || !dwarf_strict)
17497 {
17498 if (ENUM_IS_SCOPED (type))
17499 add_AT_flag (type_die, DW_AT_enum_class, 1);
17500 if (ENUM_IS_OPAQUE (type))
17501 add_AT_flag (type_die, DW_AT_declaration, 1);
17502 }
17503 }
17504 else if (! TYPE_SIZE (type))
17505 return type_die;
17506 else
17507 remove_AT (type_die, DW_AT_declaration);
17508
17509 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17510 given enum type is incomplete, do not generate the DW_AT_byte_size
17511 attribute or the DW_AT_element_list attribute. */
17512 if (TYPE_SIZE (type))
17513 {
17514 tree link;
17515
17516 TREE_ASM_WRITTEN (type) = 1;
17517 add_byte_size_attribute (type_die, type);
17518 if (dwarf_version >= 3 || !dwarf_strict)
17519 {
17520 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17521 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17522 context_die);
17523 }
17524 if (TYPE_STUB_DECL (type) != NULL_TREE)
17525 {
17526 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17527 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17528 }
17529
17530 /* If the first reference to this type was as the return type of an
17531 inline function, then it may not have a parent. Fix this now. */
17532 if (type_die->die_parent == NULL)
17533 add_child_die (scope_die_for (type, context_die), type_die);
17534
17535 for (link = TYPE_VALUES (type);
17536 link != NULL; link = TREE_CHAIN (link))
17537 {
17538 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17539 tree value = TREE_VALUE (link);
17540
17541 add_name_attribute (enum_die,
17542 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17543
17544 if (TREE_CODE (value) == CONST_DECL)
17545 value = DECL_INITIAL (value);
17546
17547 if (simple_type_size_in_bits (TREE_TYPE (value))
17548 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17549 {
17550 /* For constant forms created by add_AT_unsigned DWARF
17551 consumers (GDB, elfutils, etc.) always zero extend
17552 the value. Only when the actual value is negative
17553 do we need to use add_AT_int to generate a constant
17554 form that can represent negative values. */
17555 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17556 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17557 add_AT_unsigned (enum_die, DW_AT_const_value,
17558 (unsigned HOST_WIDE_INT) val);
17559 else
17560 add_AT_int (enum_die, DW_AT_const_value, val);
17561 }
17562 else
17563 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17564 that here. TODO: This should be re-worked to use correct
17565 signed/unsigned double tags for all cases. */
17566 add_AT_wide (enum_die, DW_AT_const_value, value);
17567 }
17568
17569 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17570 if (TYPE_ARTIFICIAL (type))
17571 add_AT_flag (type_die, DW_AT_artificial, 1);
17572 }
17573 else
17574 add_AT_flag (type_die, DW_AT_declaration, 1);
17575
17576 add_pubtype (type, type_die);
17577
17578 return type_die;
17579 }
17580
17581 /* Generate a DIE to represent either a real live formal parameter decl or to
17582 represent just the type of some formal parameter position in some function
17583 type.
17584
17585 Note that this routine is a bit unusual because its argument may be a
17586 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17587 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17588 node. If it's the former then this function is being called to output a
17589 DIE to represent a formal parameter object (or some inlining thereof). If
17590 it's the latter, then this function is only being called to output a
17591 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17592 argument type of some subprogram type.
17593 If EMIT_NAME_P is true, name and source coordinate attributes
17594 are emitted. */
17595
17596 static dw_die_ref
17597 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17598 dw_die_ref context_die)
17599 {
17600 tree node_or_origin = node ? node : origin;
17601 tree ultimate_origin;
17602 dw_die_ref parm_die
17603 = new_die (DW_TAG_formal_parameter, context_die, node);
17604
17605 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17606 {
17607 case tcc_declaration:
17608 ultimate_origin = decl_ultimate_origin (node_or_origin);
17609 if (node || ultimate_origin)
17610 origin = ultimate_origin;
17611 if (origin != NULL)
17612 add_abstract_origin_attribute (parm_die, origin);
17613 else if (emit_name_p)
17614 add_name_and_src_coords_attributes (parm_die, node);
17615 if (origin == NULL
17616 || (! DECL_ABSTRACT (node_or_origin)
17617 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17618 decl_function_context
17619 (node_or_origin))))
17620 {
17621 tree type = TREE_TYPE (node_or_origin);
17622 if (decl_by_reference_p (node_or_origin))
17623 add_type_attribute (parm_die, TREE_TYPE (type),
17624 TYPE_UNQUALIFIED, context_die);
17625 else
17626 add_type_attribute (parm_die, type,
17627 decl_quals (node_or_origin),
17628 context_die);
17629 }
17630 if (origin == NULL && DECL_ARTIFICIAL (node))
17631 add_AT_flag (parm_die, DW_AT_artificial, 1);
17632
17633 if (node && node != origin)
17634 equate_decl_number_to_die (node, parm_die);
17635 if (! DECL_ABSTRACT (node_or_origin))
17636 add_location_or_const_value_attribute (parm_die, node_or_origin,
17637 node == NULL, DW_AT_location);
17638
17639 break;
17640
17641 case tcc_type:
17642 /* We were called with some kind of a ..._TYPE node. */
17643 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17644 context_die);
17645 break;
17646
17647 default:
17648 gcc_unreachable ();
17649 }
17650
17651 return parm_die;
17652 }
17653
17654 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17655 children DW_TAG_formal_parameter DIEs representing the arguments of the
17656 parameter pack.
17657
17658 PARM_PACK must be a function parameter pack.
17659 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17660 must point to the subsequent arguments of the function PACK_ARG belongs to.
17661 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17662 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17663 following the last one for which a DIE was generated. */
17664
17665 static dw_die_ref
17666 gen_formal_parameter_pack_die (tree parm_pack,
17667 tree pack_arg,
17668 dw_die_ref subr_die,
17669 tree *next_arg)
17670 {
17671 tree arg;
17672 dw_die_ref parm_pack_die;
17673
17674 gcc_assert (parm_pack
17675 && lang_hooks.function_parameter_pack_p (parm_pack)
17676 && subr_die);
17677
17678 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17679 add_src_coords_attributes (parm_pack_die, parm_pack);
17680
17681 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17682 {
17683 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17684 parm_pack))
17685 break;
17686 gen_formal_parameter_die (arg, NULL,
17687 false /* Don't emit name attribute. */,
17688 parm_pack_die);
17689 }
17690 if (next_arg)
17691 *next_arg = arg;
17692 return parm_pack_die;
17693 }
17694
17695 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17696 at the end of an (ANSI prototyped) formal parameters list. */
17697
17698 static void
17699 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17700 {
17701 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17702 }
17703
17704 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17705 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17706 parameters as specified in some function type specification (except for
17707 those which appear as part of a function *definition*). */
17708
17709 static void
17710 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17711 {
17712 tree link;
17713 tree formal_type = NULL;
17714 tree first_parm_type;
17715 tree arg;
17716
17717 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17718 {
17719 arg = DECL_ARGUMENTS (function_or_method_type);
17720 function_or_method_type = TREE_TYPE (function_or_method_type);
17721 }
17722 else
17723 arg = NULL_TREE;
17724
17725 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17726
17727 /* Make our first pass over the list of formal parameter types and output a
17728 DW_TAG_formal_parameter DIE for each one. */
17729 for (link = first_parm_type; link; )
17730 {
17731 dw_die_ref parm_die;
17732
17733 formal_type = TREE_VALUE (link);
17734 if (formal_type == void_type_node)
17735 break;
17736
17737 /* Output a (nameless) DIE to represent the formal parameter itself. */
17738 parm_die = gen_formal_parameter_die (formal_type, NULL,
17739 true /* Emit name attribute. */,
17740 context_die);
17741 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17742 && link == first_parm_type)
17743 {
17744 add_AT_flag (parm_die, DW_AT_artificial, 1);
17745 if (dwarf_version >= 3 || !dwarf_strict)
17746 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17747 }
17748 else if (arg && DECL_ARTIFICIAL (arg))
17749 add_AT_flag (parm_die, DW_AT_artificial, 1);
17750
17751 link = TREE_CHAIN (link);
17752 if (arg)
17753 arg = DECL_CHAIN (arg);
17754 }
17755
17756 /* If this function type has an ellipsis, add a
17757 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17758 if (formal_type != void_type_node)
17759 gen_unspecified_parameters_die (function_or_method_type, context_die);
17760
17761 /* Make our second (and final) pass over the list of formal parameter types
17762 and output DIEs to represent those types (as necessary). */
17763 for (link = TYPE_ARG_TYPES (function_or_method_type);
17764 link && TREE_VALUE (link);
17765 link = TREE_CHAIN (link))
17766 gen_type_die (TREE_VALUE (link), context_die);
17767 }
17768
17769 /* We want to generate the DIE for TYPE so that we can generate the
17770 die for MEMBER, which has been defined; we will need to refer back
17771 to the member declaration nested within TYPE. If we're trying to
17772 generate minimal debug info for TYPE, processing TYPE won't do the
17773 trick; we need to attach the member declaration by hand. */
17774
17775 static void
17776 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17777 {
17778 gen_type_die (type, context_die);
17779
17780 /* If we're trying to avoid duplicate debug info, we may not have
17781 emitted the member decl for this function. Emit it now. */
17782 if (TYPE_STUB_DECL (type)
17783 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17784 && ! lookup_decl_die (member))
17785 {
17786 dw_die_ref type_die;
17787 gcc_assert (!decl_ultimate_origin (member));
17788
17789 push_decl_scope (type);
17790 type_die = lookup_type_die_strip_naming_typedef (type);
17791 if (TREE_CODE (member) == FUNCTION_DECL)
17792 gen_subprogram_die (member, type_die);
17793 else if (TREE_CODE (member) == FIELD_DECL)
17794 {
17795 /* Ignore the nameless fields that are used to skip bits but handle
17796 C++ anonymous unions and structs. */
17797 if (DECL_NAME (member) != NULL_TREE
17798 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17799 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17800 {
17801 gen_type_die (member_declared_type (member), type_die);
17802 gen_field_die (member, type_die);
17803 }
17804 }
17805 else
17806 gen_variable_die (member, NULL_TREE, type_die);
17807
17808 pop_decl_scope ();
17809 }
17810 }
17811 \f
17812 /* Forward declare these functions, because they are mutually recursive
17813 with their set_block_* pairing functions. */
17814 static void set_decl_origin_self (tree);
17815 static void set_decl_abstract_flags (tree, int);
17816
17817 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17818 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17819 that it points to the node itself, thus indicating that the node is its
17820 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17821 the given node is NULL, recursively descend the decl/block tree which
17822 it is the root of, and for each other ..._DECL or BLOCK node contained
17823 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17824 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17825 values to point to themselves. */
17826
17827 static void
17828 set_block_origin_self (tree stmt)
17829 {
17830 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17831 {
17832 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17833
17834 {
17835 tree local_decl;
17836
17837 for (local_decl = BLOCK_VARS (stmt);
17838 local_decl != NULL_TREE;
17839 local_decl = DECL_CHAIN (local_decl))
17840 if (! DECL_EXTERNAL (local_decl))
17841 set_decl_origin_self (local_decl); /* Potential recursion. */
17842 }
17843
17844 {
17845 tree subblock;
17846
17847 for (subblock = BLOCK_SUBBLOCKS (stmt);
17848 subblock != NULL_TREE;
17849 subblock = BLOCK_CHAIN (subblock))
17850 set_block_origin_self (subblock); /* Recurse. */
17851 }
17852 }
17853 }
17854
17855 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17856 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17857 node to so that it points to the node itself, thus indicating that the
17858 node represents its own (abstract) origin. Additionally, if the
17859 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17860 the decl/block tree of which the given node is the root of, and for
17861 each other ..._DECL or BLOCK node contained therein whose
17862 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17863 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17864 point to themselves. */
17865
17866 static void
17867 set_decl_origin_self (tree decl)
17868 {
17869 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17870 {
17871 DECL_ABSTRACT_ORIGIN (decl) = decl;
17872 if (TREE_CODE (decl) == FUNCTION_DECL)
17873 {
17874 tree arg;
17875
17876 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17877 DECL_ABSTRACT_ORIGIN (arg) = arg;
17878 if (DECL_INITIAL (decl) != NULL_TREE
17879 && DECL_INITIAL (decl) != error_mark_node)
17880 set_block_origin_self (DECL_INITIAL (decl));
17881 }
17882 }
17883 }
17884 \f
17885 /* Given a pointer to some BLOCK node, and a boolean value to set the
17886 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17887 the given block, and for all local decls and all local sub-blocks
17888 (recursively) which are contained therein. */
17889
17890 static void
17891 set_block_abstract_flags (tree stmt, int setting)
17892 {
17893 tree local_decl;
17894 tree subblock;
17895 unsigned int i;
17896
17897 BLOCK_ABSTRACT (stmt) = setting;
17898
17899 for (local_decl = BLOCK_VARS (stmt);
17900 local_decl != NULL_TREE;
17901 local_decl = DECL_CHAIN (local_decl))
17902 if (! DECL_EXTERNAL (local_decl))
17903 set_decl_abstract_flags (local_decl, setting);
17904
17905 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17906 {
17907 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17908 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17909 || TREE_CODE (local_decl) == PARM_DECL)
17910 set_decl_abstract_flags (local_decl, setting);
17911 }
17912
17913 for (subblock = BLOCK_SUBBLOCKS (stmt);
17914 subblock != NULL_TREE;
17915 subblock = BLOCK_CHAIN (subblock))
17916 set_block_abstract_flags (subblock, setting);
17917 }
17918
17919 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17920 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17921 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17922 set the abstract flags for all of the parameters, local vars, local
17923 blocks and sub-blocks (recursively) to the same setting. */
17924
17925 static void
17926 set_decl_abstract_flags (tree decl, int setting)
17927 {
17928 DECL_ABSTRACT (decl) = setting;
17929 if (TREE_CODE (decl) == FUNCTION_DECL)
17930 {
17931 tree arg;
17932
17933 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17934 DECL_ABSTRACT (arg) = setting;
17935 if (DECL_INITIAL (decl) != NULL_TREE
17936 && DECL_INITIAL (decl) != error_mark_node)
17937 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17938 }
17939 }
17940
17941 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17942 may later generate inlined and/or out-of-line instances of. */
17943
17944 static void
17945 dwarf2out_abstract_function (tree decl)
17946 {
17947 dw_die_ref old_die;
17948 tree save_fn;
17949 tree context;
17950 int was_abstract;
17951 htab_t old_decl_loc_table;
17952 htab_t old_cached_dw_loc_list_table;
17953 int old_call_site_count, old_tail_call_site_count;
17954 struct call_arg_loc_node *old_call_arg_locations;
17955
17956 /* Make sure we have the actual abstract inline, not a clone. */
17957 decl = DECL_ORIGIN (decl);
17958
17959 old_die = lookup_decl_die (decl);
17960 if (old_die && get_AT (old_die, DW_AT_inline))
17961 /* We've already generated the abstract instance. */
17962 return;
17963
17964 /* We can be called while recursively when seeing block defining inlined subroutine
17965 DIE. Be sure to not clobber the outer location table nor use it or we would
17966 get locations in abstract instantces. */
17967 old_decl_loc_table = decl_loc_table;
17968 decl_loc_table = NULL;
17969 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17970 cached_dw_loc_list_table = NULL;
17971 old_call_arg_locations = call_arg_locations;
17972 call_arg_locations = NULL;
17973 old_call_site_count = call_site_count;
17974 call_site_count = -1;
17975 old_tail_call_site_count = tail_call_site_count;
17976 tail_call_site_count = -1;
17977
17978 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17979 we don't get confused by DECL_ABSTRACT. */
17980 if (debug_info_level > DINFO_LEVEL_TERSE)
17981 {
17982 context = decl_class_context (decl);
17983 if (context)
17984 gen_type_die_for_member
17985 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17986 }
17987
17988 /* Pretend we've just finished compiling this function. */
17989 save_fn = current_function_decl;
17990 current_function_decl = decl;
17991
17992 was_abstract = DECL_ABSTRACT (decl);
17993 set_decl_abstract_flags (decl, 1);
17994 dwarf2out_decl (decl);
17995 if (! was_abstract)
17996 set_decl_abstract_flags (decl, 0);
17997
17998 current_function_decl = save_fn;
17999 decl_loc_table = old_decl_loc_table;
18000 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18001 call_arg_locations = old_call_arg_locations;
18002 call_site_count = old_call_site_count;
18003 tail_call_site_count = old_tail_call_site_count;
18004 }
18005
18006 /* Helper function of premark_used_types() which gets called through
18007 htab_traverse.
18008
18009 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18010 marked as unused by prune_unused_types. */
18011
18012 static int
18013 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18014 {
18015 tree type;
18016 dw_die_ref die;
18017
18018 type = (tree) *slot;
18019 die = lookup_type_die (type);
18020 if (die != NULL)
18021 die->die_perennial_p = 1;
18022 return 1;
18023 }
18024
18025 /* Helper function of premark_types_used_by_global_vars which gets called
18026 through htab_traverse.
18027
18028 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18029 marked as unused by prune_unused_types. The DIE of the type is marked
18030 only if the global variable using the type will actually be emitted. */
18031
18032 static int
18033 premark_types_used_by_global_vars_helper (void **slot,
18034 void *data ATTRIBUTE_UNUSED)
18035 {
18036 struct types_used_by_vars_entry *entry;
18037 dw_die_ref die;
18038
18039 entry = (struct types_used_by_vars_entry *) *slot;
18040 gcc_assert (entry->type != NULL
18041 && entry->var_decl != NULL);
18042 die = lookup_type_die (entry->type);
18043 if (die)
18044 {
18045 /* Ask cgraph if the global variable really is to be emitted.
18046 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18047 varpool_node *node = varpool_node::get (entry->var_decl);
18048 if (node && node->definition)
18049 {
18050 die->die_perennial_p = 1;
18051 /* Keep the parent DIEs as well. */
18052 while ((die = die->die_parent) && die->die_perennial_p == 0)
18053 die->die_perennial_p = 1;
18054 }
18055 }
18056 return 1;
18057 }
18058
18059 /* Mark all members of used_types_hash as perennial. */
18060
18061 static void
18062 premark_used_types (struct function *fun)
18063 {
18064 if (fun && fun->used_types_hash)
18065 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
18066 }
18067
18068 /* Mark all members of types_used_by_vars_entry as perennial. */
18069
18070 static void
18071 premark_types_used_by_global_vars (void)
18072 {
18073 if (types_used_by_vars_hash)
18074 htab_traverse (types_used_by_vars_hash,
18075 premark_types_used_by_global_vars_helper, NULL);
18076 }
18077
18078 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18079 for CA_LOC call arg loc node. */
18080
18081 static dw_die_ref
18082 gen_call_site_die (tree decl, dw_die_ref subr_die,
18083 struct call_arg_loc_node *ca_loc)
18084 {
18085 dw_die_ref stmt_die = NULL, die;
18086 tree block = ca_loc->block;
18087
18088 while (block
18089 && block != DECL_INITIAL (decl)
18090 && TREE_CODE (block) == BLOCK)
18091 {
18092 if (block_map.length () > BLOCK_NUMBER (block))
18093 stmt_die = block_map[BLOCK_NUMBER (block)];
18094 if (stmt_die)
18095 break;
18096 block = BLOCK_SUPERCONTEXT (block);
18097 }
18098 if (stmt_die == NULL)
18099 stmt_die = subr_die;
18100 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18101 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18102 if (ca_loc->tail_call_p)
18103 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18104 if (ca_loc->symbol_ref)
18105 {
18106 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18107 if (tdie)
18108 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18109 else
18110 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18111 }
18112 return die;
18113 }
18114
18115 /* Generate a DIE to represent a declared function (either file-scope or
18116 block-local). */
18117
18118 static void
18119 gen_subprogram_die (tree decl, dw_die_ref context_die)
18120 {
18121 tree origin = decl_ultimate_origin (decl);
18122 dw_die_ref subr_die;
18123 tree outer_scope;
18124 dw_die_ref old_die = lookup_decl_die (decl);
18125 int declaration = (current_function_decl != decl
18126 || class_or_namespace_scope_p (context_die));
18127
18128 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18129
18130 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18131 started to generate the abstract instance of an inline, decided to output
18132 its containing class, and proceeded to emit the declaration of the inline
18133 from the member list for the class. If so, DECLARATION takes priority;
18134 we'll get back to the abstract instance when done with the class. */
18135
18136 /* The class-scope declaration DIE must be the primary DIE. */
18137 if (origin && declaration && class_or_namespace_scope_p (context_die))
18138 {
18139 origin = NULL;
18140 gcc_assert (!old_die);
18141 }
18142
18143 /* Now that the C++ front end lazily declares artificial member fns, we
18144 might need to retrofit the declaration into its class. */
18145 if (!declaration && !origin && !old_die
18146 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18147 && !class_or_namespace_scope_p (context_die)
18148 && debug_info_level > DINFO_LEVEL_TERSE)
18149 old_die = force_decl_die (decl);
18150
18151 if (origin != NULL)
18152 {
18153 gcc_assert (!declaration || local_scope_p (context_die));
18154
18155 /* Fixup die_parent for the abstract instance of a nested
18156 inline function. */
18157 if (old_die && old_die->die_parent == NULL)
18158 add_child_die (context_die, old_die);
18159
18160 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18161 add_abstract_origin_attribute (subr_die, origin);
18162 /* This is where the actual code for a cloned function is.
18163 Let's emit linkage name attribute for it. This helps
18164 debuggers to e.g, set breakpoints into
18165 constructors/destructors when the user asks "break
18166 K::K". */
18167 add_linkage_name (subr_die, decl);
18168 }
18169 else if (old_die)
18170 {
18171 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18172 struct dwarf_file_data * file_index = lookup_filename (s.file);
18173
18174 if (!get_AT_flag (old_die, DW_AT_declaration)
18175 /* We can have a normal definition following an inline one in the
18176 case of redefinition of GNU C extern inlines.
18177 It seems reasonable to use AT_specification in this case. */
18178 && !get_AT (old_die, DW_AT_inline))
18179 {
18180 /* Detect and ignore this case, where we are trying to output
18181 something we have already output. */
18182 return;
18183 }
18184
18185 /* If the definition comes from the same place as the declaration,
18186 maybe use the old DIE. We always want the DIE for this function
18187 that has the *_pc attributes to be under comp_unit_die so the
18188 debugger can find it. We also need to do this for abstract
18189 instances of inlines, since the spec requires the out-of-line copy
18190 to have the same parent. For local class methods, this doesn't
18191 apply; we just use the old DIE. */
18192 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18193 && (DECL_ARTIFICIAL (decl)
18194 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18195 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18196 == (unsigned) s.line))))
18197 {
18198 subr_die = old_die;
18199
18200 /* Clear out the declaration attribute and the formal parameters.
18201 Do not remove all children, because it is possible that this
18202 declaration die was forced using force_decl_die(). In such
18203 cases die that forced declaration die (e.g. TAG_imported_module)
18204 is one of the children that we do not want to remove. */
18205 remove_AT (subr_die, DW_AT_declaration);
18206 remove_AT (subr_die, DW_AT_object_pointer);
18207 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18208 }
18209 else
18210 {
18211 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18212 add_AT_specification (subr_die, old_die);
18213 add_pubname (decl, subr_die);
18214 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18215 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18216 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18217 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18218
18219 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18220 emit the real type on the definition die. */
18221 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18222 {
18223 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18224 if (die == auto_die || die == decltype_auto_die)
18225 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18226 TYPE_UNQUALIFIED, context_die);
18227 }
18228 }
18229 }
18230 else
18231 {
18232 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18233
18234 if (TREE_PUBLIC (decl))
18235 add_AT_flag (subr_die, DW_AT_external, 1);
18236
18237 add_name_and_src_coords_attributes (subr_die, decl);
18238 add_pubname (decl, subr_die);
18239 if (debug_info_level > DINFO_LEVEL_TERSE)
18240 {
18241 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18242 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18243 TYPE_UNQUALIFIED, context_die);
18244 }
18245
18246 add_pure_or_virtual_attribute (subr_die, decl);
18247 if (DECL_ARTIFICIAL (decl))
18248 add_AT_flag (subr_die, DW_AT_artificial, 1);
18249
18250 add_accessibility_attribute (subr_die, decl);
18251 }
18252
18253 if (declaration)
18254 {
18255 if (!old_die || !get_AT (old_die, DW_AT_inline))
18256 {
18257 add_AT_flag (subr_die, DW_AT_declaration, 1);
18258
18259 /* If this is an explicit function declaration then generate
18260 a DW_AT_explicit attribute. */
18261 if (lang_hooks.decls.function_decl_explicit_p (decl)
18262 && (dwarf_version >= 3 || !dwarf_strict))
18263 add_AT_flag (subr_die, DW_AT_explicit, 1);
18264
18265 /* The first time we see a member function, it is in the context of
18266 the class to which it belongs. We make sure of this by emitting
18267 the class first. The next time is the definition, which is
18268 handled above. The two may come from the same source text.
18269
18270 Note that force_decl_die() forces function declaration die. It is
18271 later reused to represent definition. */
18272 equate_decl_number_to_die (decl, subr_die);
18273 }
18274 }
18275 else if (DECL_ABSTRACT (decl))
18276 {
18277 if (DECL_DECLARED_INLINE_P (decl))
18278 {
18279 if (cgraph_function_possibly_inlined_p (decl))
18280 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18281 else
18282 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18283 }
18284 else
18285 {
18286 if (cgraph_function_possibly_inlined_p (decl))
18287 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18288 else
18289 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18290 }
18291
18292 if (DECL_DECLARED_INLINE_P (decl)
18293 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18294 add_AT_flag (subr_die, DW_AT_artificial, 1);
18295
18296 equate_decl_number_to_die (decl, subr_die);
18297 }
18298 else if (!DECL_EXTERNAL (decl))
18299 {
18300 HOST_WIDE_INT cfa_fb_offset;
18301 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18302
18303 if (!old_die || !get_AT (old_die, DW_AT_inline))
18304 equate_decl_number_to_die (decl, subr_die);
18305
18306 gcc_checking_assert (fun);
18307 if (!flag_reorder_blocks_and_partition)
18308 {
18309 dw_fde_ref fde = fun->fde;
18310 if (fde->dw_fde_begin)
18311 {
18312 /* We have already generated the labels. */
18313 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18314 fde->dw_fde_end, false);
18315 }
18316 else
18317 {
18318 /* Create start/end labels and add the range. */
18319 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18320 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18321 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18322 current_function_funcdef_no);
18323 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18324 current_function_funcdef_no);
18325 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18326 false);
18327 }
18328
18329 #if VMS_DEBUGGING_INFO
18330 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18331 Section 2.3 Prologue and Epilogue Attributes:
18332 When a breakpoint is set on entry to a function, it is generally
18333 desirable for execution to be suspended, not on the very first
18334 instruction of the function, but rather at a point after the
18335 function's frame has been set up, after any language defined local
18336 declaration processing has been completed, and before execution of
18337 the first statement of the function begins. Debuggers generally
18338 cannot properly determine where this point is. Similarly for a
18339 breakpoint set on exit from a function. The prologue and epilogue
18340 attributes allow a compiler to communicate the location(s) to use. */
18341
18342 {
18343 if (fde->dw_fde_vms_end_prologue)
18344 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18345 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18346
18347 if (fde->dw_fde_vms_begin_epilogue)
18348 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18349 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18350 }
18351 #endif
18352
18353 }
18354 else
18355 {
18356 /* Generate pubnames entries for the split function code ranges. */
18357 dw_fde_ref fde = fun->fde;
18358
18359 if (fde->dw_fde_second_begin)
18360 {
18361 if (dwarf_version >= 3 || !dwarf_strict)
18362 {
18363 /* We should use ranges for non-contiguous code section
18364 addresses. Use the actual code range for the initial
18365 section, since the HOT/COLD labels might precede an
18366 alignment offset. */
18367 bool range_list_added = false;
18368 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18369 fde->dw_fde_end, &range_list_added,
18370 false);
18371 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18372 fde->dw_fde_second_end,
18373 &range_list_added, false);
18374 if (range_list_added)
18375 add_ranges (NULL);
18376 }
18377 else
18378 {
18379 /* There is no real support in DW2 for this .. so we make
18380 a work-around. First, emit the pub name for the segment
18381 containing the function label. Then make and emit a
18382 simplified subprogram DIE for the second segment with the
18383 name pre-fixed by __hot/cold_sect_of_. We use the same
18384 linkage name for the second die so that gdb will find both
18385 sections when given "b foo". */
18386 const char *name = NULL;
18387 tree decl_name = DECL_NAME (decl);
18388 dw_die_ref seg_die;
18389
18390 /* Do the 'primary' section. */
18391 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18392 fde->dw_fde_end, false);
18393
18394 /* Build a minimal DIE for the secondary section. */
18395 seg_die = new_die (DW_TAG_subprogram,
18396 subr_die->die_parent, decl);
18397
18398 if (TREE_PUBLIC (decl))
18399 add_AT_flag (seg_die, DW_AT_external, 1);
18400
18401 if (decl_name != NULL
18402 && IDENTIFIER_POINTER (decl_name) != NULL)
18403 {
18404 name = dwarf2_name (decl, 1);
18405 if (! DECL_ARTIFICIAL (decl))
18406 add_src_coords_attributes (seg_die, decl);
18407
18408 add_linkage_name (seg_die, decl);
18409 }
18410 gcc_assert (name != NULL);
18411 add_pure_or_virtual_attribute (seg_die, decl);
18412 if (DECL_ARTIFICIAL (decl))
18413 add_AT_flag (seg_die, DW_AT_artificial, 1);
18414
18415 name = concat ("__second_sect_of_", name, NULL);
18416 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18417 fde->dw_fde_second_end, false);
18418 add_name_attribute (seg_die, name);
18419 if (want_pubnames ())
18420 add_pubname_string (name, seg_die);
18421 }
18422 }
18423 else
18424 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18425 false);
18426 }
18427
18428 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18429
18430 /* We define the "frame base" as the function's CFA. This is more
18431 convenient for several reasons: (1) It's stable across the prologue
18432 and epilogue, which makes it better than just a frame pointer,
18433 (2) With dwarf3, there exists a one-byte encoding that allows us
18434 to reference the .debug_frame data by proxy, but failing that,
18435 (3) We can at least reuse the code inspection and interpretation
18436 code that determines the CFA position at various points in the
18437 function. */
18438 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18439 {
18440 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18441 add_AT_loc (subr_die, DW_AT_frame_base, op);
18442 }
18443 else
18444 {
18445 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18446 if (list->dw_loc_next)
18447 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18448 else
18449 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18450 }
18451
18452 /* Compute a displacement from the "steady-state frame pointer" to
18453 the CFA. The former is what all stack slots and argument slots
18454 will reference in the rtl; the latter is what we've told the
18455 debugger about. We'll need to adjust all frame_base references
18456 by this displacement. */
18457 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18458
18459 if (fun->static_chain_decl)
18460 add_AT_location_description (subr_die, DW_AT_static_link,
18461 loc_list_from_tree (fun->static_chain_decl, 2));
18462 }
18463
18464 /* Generate child dies for template paramaters. */
18465 if (debug_info_level > DINFO_LEVEL_TERSE)
18466 gen_generic_params_dies (decl);
18467
18468 /* Now output descriptions of the arguments for this function. This gets
18469 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18470 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18471 `...' at the end of the formal parameter list. In order to find out if
18472 there was a trailing ellipsis or not, we must instead look at the type
18473 associated with the FUNCTION_DECL. This will be a node of type
18474 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18475 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18476 an ellipsis at the end. */
18477
18478 /* In the case where we are describing a mere function declaration, all we
18479 need to do here (and all we *can* do here) is to describe the *types* of
18480 its formal parameters. */
18481 if (debug_info_level <= DINFO_LEVEL_TERSE)
18482 ;
18483 else if (declaration)
18484 gen_formal_types_die (decl, subr_die);
18485 else
18486 {
18487 /* Generate DIEs to represent all known formal parameters. */
18488 tree parm = DECL_ARGUMENTS (decl);
18489 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18490 tree generic_decl_parm = generic_decl
18491 ? DECL_ARGUMENTS (generic_decl)
18492 : NULL;
18493
18494 /* Now we want to walk the list of parameters of the function and
18495 emit their relevant DIEs.
18496
18497 We consider the case of DECL being an instance of a generic function
18498 as well as it being a normal function.
18499
18500 If DECL is an instance of a generic function we walk the
18501 parameters of the generic function declaration _and_ the parameters of
18502 DECL itself. This is useful because we want to emit specific DIEs for
18503 function parameter packs and those are declared as part of the
18504 generic function declaration. In that particular case,
18505 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18506 That DIE has children DIEs representing the set of arguments
18507 of the pack. Note that the set of pack arguments can be empty.
18508 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18509 children DIE.
18510
18511 Otherwise, we just consider the parameters of DECL. */
18512 while (generic_decl_parm || parm)
18513 {
18514 if (generic_decl_parm
18515 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18516 gen_formal_parameter_pack_die (generic_decl_parm,
18517 parm, subr_die,
18518 &parm);
18519 else if (parm)
18520 {
18521 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18522
18523 if (parm == DECL_ARGUMENTS (decl)
18524 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18525 && parm_die
18526 && (dwarf_version >= 3 || !dwarf_strict))
18527 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18528
18529 parm = DECL_CHAIN (parm);
18530 }
18531
18532 if (generic_decl_parm)
18533 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18534 }
18535
18536 /* Decide whether we need an unspecified_parameters DIE at the end.
18537 There are 2 more cases to do this for: 1) the ansi ... declaration -
18538 this is detectable when the end of the arg list is not a
18539 void_type_node 2) an unprototyped function declaration (not a
18540 definition). This just means that we have no info about the
18541 parameters at all. */
18542 if (prototype_p (TREE_TYPE (decl)))
18543 {
18544 /* This is the prototyped case, check for.... */
18545 if (stdarg_p (TREE_TYPE (decl)))
18546 gen_unspecified_parameters_die (decl, subr_die);
18547 }
18548 else if (DECL_INITIAL (decl) == NULL_TREE)
18549 gen_unspecified_parameters_die (decl, subr_die);
18550 }
18551
18552 /* Output Dwarf info for all of the stuff within the body of the function
18553 (if it has one - it may be just a declaration). */
18554 outer_scope = DECL_INITIAL (decl);
18555
18556 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18557 a function. This BLOCK actually represents the outermost binding contour
18558 for the function, i.e. the contour in which the function's formal
18559 parameters and labels get declared. Curiously, it appears that the front
18560 end doesn't actually put the PARM_DECL nodes for the current function onto
18561 the BLOCK_VARS list for this outer scope, but are strung off of the
18562 DECL_ARGUMENTS list for the function instead.
18563
18564 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18565 the LABEL_DECL nodes for the function however, and we output DWARF info
18566 for those in decls_for_scope. Just within the `outer_scope' there will be
18567 a BLOCK node representing the function's outermost pair of curly braces,
18568 and any blocks used for the base and member initializers of a C++
18569 constructor function. */
18570 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18571 {
18572 int call_site_note_count = 0;
18573 int tail_call_site_note_count = 0;
18574
18575 /* Emit a DW_TAG_variable DIE for a named return value. */
18576 if (DECL_NAME (DECL_RESULT (decl)))
18577 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18578
18579 current_function_has_inlines = 0;
18580 decls_for_scope (outer_scope, subr_die, 0);
18581
18582 if (call_arg_locations && !dwarf_strict)
18583 {
18584 struct call_arg_loc_node *ca_loc;
18585 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18586 {
18587 dw_die_ref die = NULL;
18588 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18589 rtx arg, next_arg;
18590
18591 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18592 arg; arg = next_arg)
18593 {
18594 dw_loc_descr_ref reg, val;
18595 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18596 dw_die_ref cdie, tdie = NULL;
18597
18598 next_arg = XEXP (arg, 1);
18599 if (REG_P (XEXP (XEXP (arg, 0), 0))
18600 && next_arg
18601 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18602 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18603 && REGNO (XEXP (XEXP (arg, 0), 0))
18604 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18605 next_arg = XEXP (next_arg, 1);
18606 if (mode == VOIDmode)
18607 {
18608 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18609 if (mode == VOIDmode)
18610 mode = GET_MODE (XEXP (arg, 0));
18611 }
18612 if (mode == VOIDmode || mode == BLKmode)
18613 continue;
18614 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18615 {
18616 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18617 tloc = XEXP (XEXP (arg, 0), 1);
18618 continue;
18619 }
18620 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18621 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18622 {
18623 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18624 tlocc = XEXP (XEXP (arg, 0), 1);
18625 continue;
18626 }
18627 reg = NULL;
18628 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18629 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18630 VAR_INIT_STATUS_INITIALIZED);
18631 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18632 {
18633 rtx mem = XEXP (XEXP (arg, 0), 0);
18634 reg = mem_loc_descriptor (XEXP (mem, 0),
18635 get_address_mode (mem),
18636 GET_MODE (mem),
18637 VAR_INIT_STATUS_INITIALIZED);
18638 }
18639 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18640 == DEBUG_PARAMETER_REF)
18641 {
18642 tree tdecl
18643 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18644 tdie = lookup_decl_die (tdecl);
18645 if (tdie == NULL)
18646 continue;
18647 }
18648 else
18649 continue;
18650 if (reg == NULL
18651 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18652 != DEBUG_PARAMETER_REF)
18653 continue;
18654 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18655 VOIDmode,
18656 VAR_INIT_STATUS_INITIALIZED);
18657 if (val == NULL)
18658 continue;
18659 if (die == NULL)
18660 die = gen_call_site_die (decl, subr_die, ca_loc);
18661 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18662 NULL_TREE);
18663 if (reg != NULL)
18664 add_AT_loc (cdie, DW_AT_location, reg);
18665 else if (tdie != NULL)
18666 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18667 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18668 if (next_arg != XEXP (arg, 1))
18669 {
18670 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18671 if (mode == VOIDmode)
18672 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18673 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18674 0), 1),
18675 mode, VOIDmode,
18676 VAR_INIT_STATUS_INITIALIZED);
18677 if (val != NULL)
18678 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18679 }
18680 }
18681 if (die == NULL
18682 && (ca_loc->symbol_ref || tloc))
18683 die = gen_call_site_die (decl, subr_die, ca_loc);
18684 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18685 {
18686 dw_loc_descr_ref tval = NULL;
18687
18688 if (tloc != NULL_RTX)
18689 tval = mem_loc_descriptor (tloc,
18690 GET_MODE (tloc) == VOIDmode
18691 ? Pmode : GET_MODE (tloc),
18692 VOIDmode,
18693 VAR_INIT_STATUS_INITIALIZED);
18694 if (tval)
18695 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18696 else if (tlocc != NULL_RTX)
18697 {
18698 tval = mem_loc_descriptor (tlocc,
18699 GET_MODE (tlocc) == VOIDmode
18700 ? Pmode : GET_MODE (tlocc),
18701 VOIDmode,
18702 VAR_INIT_STATUS_INITIALIZED);
18703 if (tval)
18704 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18705 tval);
18706 }
18707 }
18708 if (die != NULL)
18709 {
18710 call_site_note_count++;
18711 if (ca_loc->tail_call_p)
18712 tail_call_site_note_count++;
18713 }
18714 }
18715 }
18716 call_arg_locations = NULL;
18717 call_arg_loc_last = NULL;
18718 if (tail_call_site_count >= 0
18719 && tail_call_site_count == tail_call_site_note_count
18720 && !dwarf_strict)
18721 {
18722 if (call_site_count >= 0
18723 && call_site_count == call_site_note_count)
18724 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18725 else
18726 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18727 }
18728 call_site_count = -1;
18729 tail_call_site_count = -1;
18730 }
18731
18732 if (subr_die != old_die)
18733 /* Add the calling convention attribute if requested. */
18734 add_calling_convention_attribute (subr_die, decl);
18735 }
18736
18737 /* Returns a hash value for X (which really is a die_struct). */
18738
18739 static hashval_t
18740 common_block_die_table_hash (const void *x)
18741 {
18742 const_dw_die_ref d = (const_dw_die_ref) x;
18743 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18744 }
18745
18746 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18747 as decl_id and die_parent of die_struct Y. */
18748
18749 static int
18750 common_block_die_table_eq (const void *x, const void *y)
18751 {
18752 const_dw_die_ref d = (const_dw_die_ref) x;
18753 const_dw_die_ref e = (const_dw_die_ref) y;
18754 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18755 }
18756
18757 /* Generate a DIE to represent a declared data object.
18758 Either DECL or ORIGIN must be non-null. */
18759
18760 static void
18761 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18762 {
18763 HOST_WIDE_INT off = 0;
18764 tree com_decl;
18765 tree decl_or_origin = decl ? decl : origin;
18766 tree ultimate_origin;
18767 dw_die_ref var_die;
18768 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18769 dw_die_ref origin_die;
18770 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18771 || class_or_namespace_scope_p (context_die));
18772 bool specialization_p = false;
18773
18774 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18775 if (decl || ultimate_origin)
18776 origin = ultimate_origin;
18777 com_decl = fortran_common (decl_or_origin, &off);
18778
18779 /* Symbol in common gets emitted as a child of the common block, in the form
18780 of a data member. */
18781 if (com_decl)
18782 {
18783 dw_die_ref com_die;
18784 dw_loc_list_ref loc;
18785 die_node com_die_arg;
18786
18787 var_die = lookup_decl_die (decl_or_origin);
18788 if (var_die)
18789 {
18790 if (get_AT (var_die, DW_AT_location) == NULL)
18791 {
18792 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18793 if (loc)
18794 {
18795 if (off)
18796 {
18797 /* Optimize the common case. */
18798 if (single_element_loc_list_p (loc)
18799 && loc->expr->dw_loc_opc == DW_OP_addr
18800 && loc->expr->dw_loc_next == NULL
18801 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18802 == SYMBOL_REF)
18803 {
18804 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18805 loc->expr->dw_loc_oprnd1.v.val_addr
18806 = plus_constant (GET_MODE (x), x , off);
18807 }
18808 else
18809 loc_list_plus_const (loc, off);
18810 }
18811 add_AT_location_description (var_die, DW_AT_location, loc);
18812 remove_AT (var_die, DW_AT_declaration);
18813 }
18814 }
18815 return;
18816 }
18817
18818 if (common_block_die_table == NULL)
18819 common_block_die_table
18820 = htab_create_ggc (10, common_block_die_table_hash,
18821 common_block_die_table_eq, NULL);
18822
18823 com_die_arg.decl_id = DECL_UID (com_decl);
18824 com_die_arg.die_parent = context_die;
18825 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18826 loc = loc_list_from_tree (com_decl, 2);
18827 if (com_die == NULL)
18828 {
18829 const char *cnam
18830 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18831 void **slot;
18832
18833 com_die = new_die (DW_TAG_common_block, context_die, decl);
18834 add_name_and_src_coords_attributes (com_die, com_decl);
18835 if (loc)
18836 {
18837 add_AT_location_description (com_die, DW_AT_location, loc);
18838 /* Avoid sharing the same loc descriptor between
18839 DW_TAG_common_block and DW_TAG_variable. */
18840 loc = loc_list_from_tree (com_decl, 2);
18841 }
18842 else if (DECL_EXTERNAL (decl))
18843 add_AT_flag (com_die, DW_AT_declaration, 1);
18844 if (want_pubnames ())
18845 add_pubname_string (cnam, com_die); /* ??? needed? */
18846 com_die->decl_id = DECL_UID (com_decl);
18847 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18848 *slot = (void *) com_die;
18849 }
18850 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18851 {
18852 add_AT_location_description (com_die, DW_AT_location, loc);
18853 loc = loc_list_from_tree (com_decl, 2);
18854 remove_AT (com_die, DW_AT_declaration);
18855 }
18856 var_die = new_die (DW_TAG_variable, com_die, decl);
18857 add_name_and_src_coords_attributes (var_die, decl);
18858 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
18859 context_die);
18860 add_AT_flag (var_die, DW_AT_external, 1);
18861 if (loc)
18862 {
18863 if (off)
18864 {
18865 /* Optimize the common case. */
18866 if (single_element_loc_list_p (loc)
18867 && loc->expr->dw_loc_opc == DW_OP_addr
18868 && loc->expr->dw_loc_next == NULL
18869 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18870 {
18871 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18872 loc->expr->dw_loc_oprnd1.v.val_addr
18873 = plus_constant (GET_MODE (x), x, off);
18874 }
18875 else
18876 loc_list_plus_const (loc, off);
18877 }
18878 add_AT_location_description (var_die, DW_AT_location, loc);
18879 }
18880 else if (DECL_EXTERNAL (decl))
18881 add_AT_flag (var_die, DW_AT_declaration, 1);
18882 equate_decl_number_to_die (decl, var_die);
18883 return;
18884 }
18885
18886 /* If the compiler emitted a definition for the DECL declaration
18887 and if we already emitted a DIE for it, don't emit a second
18888 DIE for it again. Allow re-declarations of DECLs that are
18889 inside functions, though. */
18890 if (old_die && declaration && !local_scope_p (context_die))
18891 return;
18892
18893 /* For static data members, the declaration in the class is supposed
18894 to have DW_TAG_member tag; the specification should still be
18895 DW_TAG_variable referencing the DW_TAG_member DIE. */
18896 if (declaration && class_scope_p (context_die))
18897 var_die = new_die (DW_TAG_member, context_die, decl);
18898 else
18899 var_die = new_die (DW_TAG_variable, context_die, decl);
18900
18901 origin_die = NULL;
18902 if (origin != NULL)
18903 origin_die = add_abstract_origin_attribute (var_die, origin);
18904
18905 /* Loop unrolling can create multiple blocks that refer to the same
18906 static variable, so we must test for the DW_AT_declaration flag.
18907
18908 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18909 copy decls and set the DECL_ABSTRACT flag on them instead of
18910 sharing them.
18911
18912 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18913
18914 ??? The declare_in_namespace support causes us to get two DIEs for one
18915 variable, both of which are declarations. We want to avoid considering
18916 one to be a specification, so we must test that this DIE is not a
18917 declaration. */
18918 else if (old_die && TREE_STATIC (decl) && ! declaration
18919 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18920 {
18921 /* This is a definition of a C++ class level static. */
18922 add_AT_specification (var_die, old_die);
18923 specialization_p = true;
18924 if (DECL_NAME (decl))
18925 {
18926 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18927 struct dwarf_file_data * file_index = lookup_filename (s.file);
18928
18929 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18930 add_AT_file (var_die, DW_AT_decl_file, file_index);
18931
18932 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18933 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18934
18935 if (old_die->die_tag == DW_TAG_member)
18936 add_linkage_name (var_die, decl);
18937 }
18938 }
18939 else
18940 add_name_and_src_coords_attributes (var_die, decl);
18941
18942 if ((origin == NULL && !specialization_p)
18943 || (origin != NULL
18944 && !DECL_ABSTRACT (decl_or_origin)
18945 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18946 decl_function_context
18947 (decl_or_origin))))
18948 {
18949 tree type = TREE_TYPE (decl_or_origin);
18950
18951 if (decl_by_reference_p (decl_or_origin))
18952 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
18953 context_die);
18954 else
18955 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
18956 context_die);
18957 }
18958
18959 if (origin == NULL && !specialization_p)
18960 {
18961 if (TREE_PUBLIC (decl))
18962 add_AT_flag (var_die, DW_AT_external, 1);
18963
18964 if (DECL_ARTIFICIAL (decl))
18965 add_AT_flag (var_die, DW_AT_artificial, 1);
18966
18967 add_accessibility_attribute (var_die, decl);
18968 }
18969
18970 if (declaration)
18971 add_AT_flag (var_die, DW_AT_declaration, 1);
18972
18973 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18974 equate_decl_number_to_die (decl, var_die);
18975
18976 if (! declaration
18977 && (! DECL_ABSTRACT (decl_or_origin)
18978 /* Local static vars are shared between all clones/inlines,
18979 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18980 already set. */
18981 || (TREE_CODE (decl_or_origin) == VAR_DECL
18982 && TREE_STATIC (decl_or_origin)
18983 && DECL_RTL_SET_P (decl_or_origin)))
18984 /* When abstract origin already has DW_AT_location attribute, no need
18985 to add it again. */
18986 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18987 {
18988 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18989 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18990 defer_location (decl_or_origin, var_die);
18991 else
18992 add_location_or_const_value_attribute (var_die, decl_or_origin,
18993 decl == NULL, DW_AT_location);
18994 add_pubname (decl_or_origin, var_die);
18995 }
18996 else
18997 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18998 }
18999
19000 /* Generate a DIE to represent a named constant. */
19001
19002 static void
19003 gen_const_die (tree decl, dw_die_ref context_die)
19004 {
19005 dw_die_ref const_die;
19006 tree type = TREE_TYPE (decl);
19007
19008 const_die = new_die (DW_TAG_constant, context_die, decl);
19009 add_name_and_src_coords_attributes (const_die, decl);
19010 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19011 if (TREE_PUBLIC (decl))
19012 add_AT_flag (const_die, DW_AT_external, 1);
19013 if (DECL_ARTIFICIAL (decl))
19014 add_AT_flag (const_die, DW_AT_artificial, 1);
19015 tree_add_const_value_attribute_for_decl (const_die, decl);
19016 }
19017
19018 /* Generate a DIE to represent a label identifier. */
19019
19020 static void
19021 gen_label_die (tree decl, dw_die_ref context_die)
19022 {
19023 tree origin = decl_ultimate_origin (decl);
19024 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19025 rtx insn;
19026 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19027
19028 if (origin != NULL)
19029 add_abstract_origin_attribute (lbl_die, origin);
19030 else
19031 add_name_and_src_coords_attributes (lbl_die, decl);
19032
19033 if (DECL_ABSTRACT (decl))
19034 equate_decl_number_to_die (decl, lbl_die);
19035 else
19036 {
19037 insn = DECL_RTL_IF_SET (decl);
19038
19039 /* Deleted labels are programmer specified labels which have been
19040 eliminated because of various optimizations. We still emit them
19041 here so that it is possible to put breakpoints on them. */
19042 if (insn
19043 && (LABEL_P (insn)
19044 || ((NOTE_P (insn)
19045 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19046 {
19047 /* When optimization is enabled (via -O) some parts of the compiler
19048 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19049 represent source-level labels which were explicitly declared by
19050 the user. This really shouldn't be happening though, so catch
19051 it if it ever does happen. */
19052 gcc_assert (!INSN_DELETED_P (insn));
19053
19054 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19055 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19056 }
19057 else if (insn
19058 && NOTE_P (insn)
19059 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19060 && CODE_LABEL_NUMBER (insn) != -1)
19061 {
19062 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19063 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19064 }
19065 }
19066 }
19067
19068 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19069 attributes to the DIE for a block STMT, to describe where the inlined
19070 function was called from. This is similar to add_src_coords_attributes. */
19071
19072 static inline void
19073 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19074 {
19075 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19076
19077 if (dwarf_version >= 3 || !dwarf_strict)
19078 {
19079 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19080 add_AT_unsigned (die, DW_AT_call_line, s.line);
19081 }
19082 }
19083
19084
19085 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19086 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19087
19088 static inline void
19089 add_high_low_attributes (tree stmt, dw_die_ref die)
19090 {
19091 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19092
19093 if (BLOCK_FRAGMENT_CHAIN (stmt)
19094 && (dwarf_version >= 3 || !dwarf_strict))
19095 {
19096 tree chain, superblock = NULL_TREE;
19097 dw_die_ref pdie;
19098 dw_attr_ref attr = NULL;
19099
19100 if (inlined_function_outer_scope_p (stmt))
19101 {
19102 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19103 BLOCK_NUMBER (stmt));
19104 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19105 }
19106
19107 /* Optimize duplicate .debug_ranges lists or even tails of
19108 lists. If this BLOCK has same ranges as its supercontext,
19109 lookup DW_AT_ranges attribute in the supercontext (and
19110 recursively so), verify that the ranges_table contains the
19111 right values and use it instead of adding a new .debug_range. */
19112 for (chain = stmt, pdie = die;
19113 BLOCK_SAME_RANGE (chain);
19114 chain = BLOCK_SUPERCONTEXT (chain))
19115 {
19116 dw_attr_ref new_attr;
19117
19118 pdie = pdie->die_parent;
19119 if (pdie == NULL)
19120 break;
19121 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19122 break;
19123 new_attr = get_AT (pdie, DW_AT_ranges);
19124 if (new_attr == NULL
19125 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19126 break;
19127 attr = new_attr;
19128 superblock = BLOCK_SUPERCONTEXT (chain);
19129 }
19130 if (attr != NULL
19131 && (ranges_table[attr->dw_attr_val.v.val_offset
19132 / 2 / DWARF2_ADDR_SIZE].num
19133 == BLOCK_NUMBER (superblock))
19134 && BLOCK_FRAGMENT_CHAIN (superblock))
19135 {
19136 unsigned long off = attr->dw_attr_val.v.val_offset
19137 / 2 / DWARF2_ADDR_SIZE;
19138 unsigned long supercnt = 0, thiscnt = 0;
19139 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19140 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19141 {
19142 ++supercnt;
19143 gcc_checking_assert (ranges_table[off + supercnt].num
19144 == BLOCK_NUMBER (chain));
19145 }
19146 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19147 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19148 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19149 ++thiscnt;
19150 gcc_assert (supercnt >= thiscnt);
19151 add_AT_range_list (die, DW_AT_ranges,
19152 ((off + supercnt - thiscnt)
19153 * 2 * DWARF2_ADDR_SIZE),
19154 false);
19155 return;
19156 }
19157
19158 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19159
19160 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19161 do
19162 {
19163 add_ranges (chain);
19164 chain = BLOCK_FRAGMENT_CHAIN (chain);
19165 }
19166 while (chain);
19167 add_ranges (NULL);
19168 }
19169 else
19170 {
19171 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19172 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19173 BLOCK_NUMBER (stmt));
19174 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19175 BLOCK_NUMBER (stmt));
19176 add_AT_low_high_pc (die, label, label_high, false);
19177 }
19178 }
19179
19180 /* Generate a DIE for a lexical block. */
19181
19182 static void
19183 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19184 {
19185 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19186
19187 if (call_arg_locations)
19188 {
19189 if (block_map.length () <= BLOCK_NUMBER (stmt))
19190 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19191 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19192 }
19193
19194 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19195 add_high_low_attributes (stmt, stmt_die);
19196
19197 decls_for_scope (stmt, stmt_die, depth);
19198 }
19199
19200 /* Generate a DIE for an inlined subprogram. */
19201
19202 static void
19203 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19204 {
19205 tree decl;
19206
19207 /* The instance of function that is effectively being inlined shall not
19208 be abstract. */
19209 gcc_assert (! BLOCK_ABSTRACT (stmt));
19210
19211 decl = block_ultimate_origin (stmt);
19212
19213 /* Emit info for the abstract instance first, if we haven't yet. We
19214 must emit this even if the block is abstract, otherwise when we
19215 emit the block below (or elsewhere), we may end up trying to emit
19216 a die whose origin die hasn't been emitted, and crashing. */
19217 dwarf2out_abstract_function (decl);
19218
19219 if (! BLOCK_ABSTRACT (stmt))
19220 {
19221 dw_die_ref subr_die
19222 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19223
19224 if (call_arg_locations)
19225 {
19226 if (block_map.length () <= BLOCK_NUMBER (stmt))
19227 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19228 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19229 }
19230 add_abstract_origin_attribute (subr_die, decl);
19231 if (TREE_ASM_WRITTEN (stmt))
19232 add_high_low_attributes (stmt, subr_die);
19233 add_call_src_coords_attributes (stmt, subr_die);
19234
19235 decls_for_scope (stmt, subr_die, depth);
19236 current_function_has_inlines = 1;
19237 }
19238 }
19239
19240 /* Generate a DIE for a field in a record, or structure. */
19241
19242 static void
19243 gen_field_die (tree decl, dw_die_ref context_die)
19244 {
19245 dw_die_ref decl_die;
19246
19247 if (TREE_TYPE (decl) == error_mark_node)
19248 return;
19249
19250 decl_die = new_die (DW_TAG_member, context_die, decl);
19251 add_name_and_src_coords_attributes (decl_die, decl);
19252 add_type_attribute (decl_die, member_declared_type (decl),
19253 decl_quals (decl), context_die);
19254
19255 if (DECL_BIT_FIELD_TYPE (decl))
19256 {
19257 add_byte_size_attribute (decl_die, decl);
19258 add_bit_size_attribute (decl_die, decl);
19259 add_bit_offset_attribute (decl_die, decl);
19260 }
19261
19262 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19263 add_data_member_location_attribute (decl_die, decl);
19264
19265 if (DECL_ARTIFICIAL (decl))
19266 add_AT_flag (decl_die, DW_AT_artificial, 1);
19267
19268 add_accessibility_attribute (decl_die, decl);
19269
19270 /* Equate decl number to die, so that we can look up this decl later on. */
19271 equate_decl_number_to_die (decl, decl_die);
19272 }
19273
19274 #if 0
19275 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19276 Use modified_type_die instead.
19277 We keep this code here just in case these types of DIEs may be needed to
19278 represent certain things in other languages (e.g. Pascal) someday. */
19279
19280 static void
19281 gen_pointer_type_die (tree type, dw_die_ref context_die)
19282 {
19283 dw_die_ref ptr_die
19284 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19285
19286 equate_type_number_to_die (type, ptr_die);
19287 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19288 context_die);
19289 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19290 }
19291
19292 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19293 Use modified_type_die instead.
19294 We keep this code here just in case these types of DIEs may be needed to
19295 represent certain things in other languages (e.g. Pascal) someday. */
19296
19297 static void
19298 gen_reference_type_die (tree type, dw_die_ref context_die)
19299 {
19300 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19301
19302 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19303 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19304 else
19305 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19306
19307 equate_type_number_to_die (type, ref_die);
19308 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19309 context_die);
19310 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19311 }
19312 #endif
19313
19314 /* Generate a DIE for a pointer to a member type. */
19315
19316 static void
19317 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19318 {
19319 dw_die_ref ptr_die
19320 = new_die (DW_TAG_ptr_to_member_type,
19321 scope_die_for (type, context_die), type);
19322
19323 equate_type_number_to_die (type, ptr_die);
19324 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19325 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19326 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19327 context_die);
19328 }
19329
19330 typedef const char *dchar_p; /* For DEF_VEC_P. */
19331
19332 static char *producer_string;
19333
19334 /* Return a heap allocated producer string including command line options
19335 if -grecord-gcc-switches. */
19336
19337 static char *
19338 gen_producer_string (void)
19339 {
19340 size_t j;
19341 auto_vec<dchar_p> switches;
19342 const char *language_string = lang_hooks.name;
19343 char *producer, *tail;
19344 const char *p;
19345 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19346 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19347
19348 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19349 switch (save_decoded_options[j].opt_index)
19350 {
19351 case OPT_o:
19352 case OPT_d:
19353 case OPT_dumpbase:
19354 case OPT_dumpdir:
19355 case OPT_auxbase:
19356 case OPT_auxbase_strip:
19357 case OPT_quiet:
19358 case OPT_version:
19359 case OPT_v:
19360 case OPT_w:
19361 case OPT_L:
19362 case OPT_D:
19363 case OPT_I:
19364 case OPT_U:
19365 case OPT_SPECIAL_unknown:
19366 case OPT_SPECIAL_ignore:
19367 case OPT_SPECIAL_program_name:
19368 case OPT_SPECIAL_input_file:
19369 case OPT_grecord_gcc_switches:
19370 case OPT_gno_record_gcc_switches:
19371 case OPT__output_pch_:
19372 case OPT_fdiagnostics_show_location_:
19373 case OPT_fdiagnostics_show_option:
19374 case OPT_fdiagnostics_show_caret:
19375 case OPT_fdiagnostics_color_:
19376 case OPT_fverbose_asm:
19377 case OPT____:
19378 case OPT__sysroot_:
19379 case OPT_nostdinc:
19380 case OPT_nostdinc__:
19381 /* Ignore these. */
19382 continue;
19383 default:
19384 if (cl_options[save_decoded_options[j].opt_index].flags
19385 & CL_NO_DWARF_RECORD)
19386 continue;
19387 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19388 == '-');
19389 switch (save_decoded_options[j].canonical_option[0][1])
19390 {
19391 case 'M':
19392 case 'i':
19393 case 'W':
19394 continue;
19395 case 'f':
19396 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19397 "dump", 4) == 0)
19398 continue;
19399 break;
19400 default:
19401 break;
19402 }
19403 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19404 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19405 break;
19406 }
19407
19408 producer = XNEWVEC (char, plen + 1 + len + 1);
19409 tail = producer;
19410 sprintf (tail, "%s %s", language_string, version_string);
19411 tail += plen;
19412
19413 FOR_EACH_VEC_ELT (switches, j, p)
19414 {
19415 len = strlen (p);
19416 *tail = ' ';
19417 memcpy (tail + 1, p, len);
19418 tail += len + 1;
19419 }
19420
19421 *tail = '\0';
19422 return producer;
19423 }
19424
19425 /* Generate the DIE for the compilation unit. */
19426
19427 static dw_die_ref
19428 gen_compile_unit_die (const char *filename)
19429 {
19430 dw_die_ref die;
19431 const char *language_string = lang_hooks.name;
19432 int language;
19433
19434 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19435
19436 if (filename)
19437 {
19438 add_name_attribute (die, filename);
19439 /* Don't add cwd for <built-in>. */
19440 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19441 add_comp_dir_attribute (die);
19442 }
19443
19444 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19445
19446 /* If our producer is LTO try to figure out a common language to use
19447 from the global list of translation units. */
19448 if (strcmp (language_string, "GNU GIMPLE") == 0)
19449 {
19450 unsigned i;
19451 tree t;
19452 const char *common_lang = NULL;
19453
19454 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19455 {
19456 if (!TRANSLATION_UNIT_LANGUAGE (t))
19457 continue;
19458 if (!common_lang)
19459 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19460 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19461 ;
19462 else if (strncmp (common_lang, "GNU C", 5) == 0
19463 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19464 /* Mixing C and C++ is ok, use C++ in that case. */
19465 common_lang = "GNU C++";
19466 else
19467 {
19468 /* Fall back to C. */
19469 common_lang = NULL;
19470 break;
19471 }
19472 }
19473
19474 if (common_lang)
19475 language_string = common_lang;
19476 }
19477
19478 language = DW_LANG_C89;
19479 if (strcmp (language_string, "GNU C++") == 0)
19480 language = DW_LANG_C_plus_plus;
19481 else if (strcmp (language_string, "GNU F77") == 0)
19482 language = DW_LANG_Fortran77;
19483 else if (strcmp (language_string, "GNU Pascal") == 0)
19484 language = DW_LANG_Pascal83;
19485 else if (dwarf_version >= 3 || !dwarf_strict)
19486 {
19487 if (strcmp (language_string, "GNU Ada") == 0)
19488 language = DW_LANG_Ada95;
19489 else if (strcmp (language_string, "GNU Fortran") == 0)
19490 language = DW_LANG_Fortran95;
19491 else if (strcmp (language_string, "GNU Java") == 0)
19492 language = DW_LANG_Java;
19493 else if (strcmp (language_string, "GNU Objective-C") == 0)
19494 language = DW_LANG_ObjC;
19495 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19496 language = DW_LANG_ObjC_plus_plus;
19497 else if (dwarf_version >= 5 || !dwarf_strict)
19498 {
19499 if (strcmp (language_string, "GNU Go") == 0)
19500 language = DW_LANG_Go;
19501 }
19502 }
19503 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19504 else if (strcmp (language_string, "GNU Fortran") == 0)
19505 language = DW_LANG_Fortran90;
19506
19507 add_AT_unsigned (die, DW_AT_language, language);
19508
19509 switch (language)
19510 {
19511 case DW_LANG_Fortran77:
19512 case DW_LANG_Fortran90:
19513 case DW_LANG_Fortran95:
19514 /* Fortran has case insensitive identifiers and the front-end
19515 lowercases everything. */
19516 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19517 break;
19518 default:
19519 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19520 break;
19521 }
19522 return die;
19523 }
19524
19525 /* Generate the DIE for a base class. */
19526
19527 static void
19528 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19529 {
19530 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19531
19532 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19533 add_data_member_location_attribute (die, binfo);
19534
19535 if (BINFO_VIRTUAL_P (binfo))
19536 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19537
19538 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19539 children, otherwise the default is DW_ACCESS_public. In DWARF2
19540 the default has always been DW_ACCESS_private. */
19541 if (access == access_public_node)
19542 {
19543 if (dwarf_version == 2
19544 || context_die->die_tag == DW_TAG_class_type)
19545 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19546 }
19547 else if (access == access_protected_node)
19548 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19549 else if (dwarf_version > 2
19550 && context_die->die_tag != DW_TAG_class_type)
19551 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19552 }
19553
19554 /* Generate a DIE for a class member. */
19555
19556 static void
19557 gen_member_die (tree type, dw_die_ref context_die)
19558 {
19559 tree member;
19560 tree binfo = TYPE_BINFO (type);
19561 dw_die_ref child;
19562
19563 /* If this is not an incomplete type, output descriptions of each of its
19564 members. Note that as we output the DIEs necessary to represent the
19565 members of this record or union type, we will also be trying to output
19566 DIEs to represent the *types* of those members. However the `type'
19567 function (above) will specifically avoid generating type DIEs for member
19568 types *within* the list of member DIEs for this (containing) type except
19569 for those types (of members) which are explicitly marked as also being
19570 members of this (containing) type themselves. The g++ front- end can
19571 force any given type to be treated as a member of some other (containing)
19572 type by setting the TYPE_CONTEXT of the given (member) type to point to
19573 the TREE node representing the appropriate (containing) type. */
19574
19575 /* First output info about the base classes. */
19576 if (binfo)
19577 {
19578 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19579 int i;
19580 tree base;
19581
19582 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19583 gen_inheritance_die (base,
19584 (accesses ? (*accesses)[i] : access_public_node),
19585 context_die);
19586 }
19587
19588 /* Now output info about the data members and type members. */
19589 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19590 {
19591 /* If we thought we were generating minimal debug info for TYPE
19592 and then changed our minds, some of the member declarations
19593 may have already been defined. Don't define them again, but
19594 do put them in the right order. */
19595
19596 child = lookup_decl_die (member);
19597 if (child)
19598 splice_child_die (context_die, child);
19599 else
19600 gen_decl_die (member, NULL, context_die);
19601 }
19602
19603 /* Now output info about the function members (if any). */
19604 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19605 {
19606 /* Don't include clones in the member list. */
19607 if (DECL_ABSTRACT_ORIGIN (member))
19608 continue;
19609
19610 child = lookup_decl_die (member);
19611 if (child)
19612 splice_child_die (context_die, child);
19613 else
19614 gen_decl_die (member, NULL, context_die);
19615 }
19616 }
19617
19618 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19619 is set, we pretend that the type was never defined, so we only get the
19620 member DIEs needed by later specification DIEs. */
19621
19622 static void
19623 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19624 enum debug_info_usage usage)
19625 {
19626 dw_die_ref type_die = lookup_type_die (type);
19627 dw_die_ref scope_die = 0;
19628 int nested = 0;
19629 int complete = (TYPE_SIZE (type)
19630 && (! TYPE_STUB_DECL (type)
19631 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19632 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19633 complete = complete && should_emit_struct_debug (type, usage);
19634
19635 if (type_die && ! complete)
19636 return;
19637
19638 if (TYPE_CONTEXT (type) != NULL_TREE
19639 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19640 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19641 nested = 1;
19642
19643 scope_die = scope_die_for (type, context_die);
19644
19645 /* Generate child dies for template paramaters. */
19646 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19647 schedule_generic_params_dies_gen (type);
19648
19649 if (! type_die || (nested && is_cu_die (scope_die)))
19650 /* First occurrence of type or toplevel definition of nested class. */
19651 {
19652 dw_die_ref old_die = type_die;
19653
19654 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19655 ? record_type_tag (type) : DW_TAG_union_type,
19656 scope_die, type);
19657 equate_type_number_to_die (type, type_die);
19658 if (old_die)
19659 add_AT_specification (type_die, old_die);
19660 else
19661 add_name_attribute (type_die, type_tag (type));
19662 }
19663 else
19664 remove_AT (type_die, DW_AT_declaration);
19665
19666 /* If this type has been completed, then give it a byte_size attribute and
19667 then give a list of members. */
19668 if (complete && !ns_decl)
19669 {
19670 /* Prevent infinite recursion in cases where the type of some member of
19671 this type is expressed in terms of this type itself. */
19672 TREE_ASM_WRITTEN (type) = 1;
19673 add_byte_size_attribute (type_die, type);
19674 if (TYPE_STUB_DECL (type) != NULL_TREE)
19675 {
19676 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19677 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19678 }
19679
19680 /* If the first reference to this type was as the return type of an
19681 inline function, then it may not have a parent. Fix this now. */
19682 if (type_die->die_parent == NULL)
19683 add_child_die (scope_die, type_die);
19684
19685 push_decl_scope (type);
19686 gen_member_die (type, type_die);
19687 pop_decl_scope ();
19688
19689 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19690 if (TYPE_ARTIFICIAL (type))
19691 add_AT_flag (type_die, DW_AT_artificial, 1);
19692
19693 /* GNU extension: Record what type our vtable lives in. */
19694 if (TYPE_VFIELD (type))
19695 {
19696 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19697
19698 gen_type_die (vtype, context_die);
19699 add_AT_die_ref (type_die, DW_AT_containing_type,
19700 lookup_type_die (vtype));
19701 }
19702 }
19703 else
19704 {
19705 add_AT_flag (type_die, DW_AT_declaration, 1);
19706
19707 /* We don't need to do this for function-local types. */
19708 if (TYPE_STUB_DECL (type)
19709 && ! decl_function_context (TYPE_STUB_DECL (type)))
19710 vec_safe_push (incomplete_types, type);
19711 }
19712
19713 if (get_AT (type_die, DW_AT_name))
19714 add_pubtype (type, type_die);
19715 }
19716
19717 /* Generate a DIE for a subroutine _type_. */
19718
19719 static void
19720 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19721 {
19722 tree return_type = TREE_TYPE (type);
19723 dw_die_ref subr_die
19724 = new_die (DW_TAG_subroutine_type,
19725 scope_die_for (type, context_die), type);
19726
19727 equate_type_number_to_die (type, subr_die);
19728 add_prototyped_attribute (subr_die, type);
19729 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
19730 gen_formal_types_die (type, subr_die);
19731
19732 if (get_AT (subr_die, DW_AT_name))
19733 add_pubtype (type, subr_die);
19734 }
19735
19736 /* Generate a DIE for a type definition. */
19737
19738 static void
19739 gen_typedef_die (tree decl, dw_die_ref context_die)
19740 {
19741 dw_die_ref type_die;
19742 tree origin;
19743
19744 if (TREE_ASM_WRITTEN (decl))
19745 return;
19746
19747 TREE_ASM_WRITTEN (decl) = 1;
19748 type_die = new_die (DW_TAG_typedef, context_die, decl);
19749 origin = decl_ultimate_origin (decl);
19750 if (origin != NULL)
19751 add_abstract_origin_attribute (type_die, origin);
19752 else
19753 {
19754 tree type;
19755
19756 add_name_and_src_coords_attributes (type_die, decl);
19757 if (DECL_ORIGINAL_TYPE (decl))
19758 {
19759 type = DECL_ORIGINAL_TYPE (decl);
19760
19761 gcc_assert (type != TREE_TYPE (decl));
19762 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19763 }
19764 else
19765 {
19766 type = TREE_TYPE (decl);
19767
19768 if (is_naming_typedef_decl (TYPE_NAME (type)))
19769 {
19770 /* Here, we are in the case of decl being a typedef naming
19771 an anonymous type, e.g:
19772 typedef struct {...} foo;
19773 In that case TREE_TYPE (decl) is not a typedef variant
19774 type and TYPE_NAME of the anonymous type is set to the
19775 TYPE_DECL of the typedef. This construct is emitted by
19776 the C++ FE.
19777
19778 TYPE is the anonymous struct named by the typedef
19779 DECL. As we need the DW_AT_type attribute of the
19780 DW_TAG_typedef to point to the DIE of TYPE, let's
19781 generate that DIE right away. add_type_attribute
19782 called below will then pick (via lookup_type_die) that
19783 anonymous struct DIE. */
19784 if (!TREE_ASM_WRITTEN (type))
19785 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19786
19787 /* This is a GNU Extension. We are adding a
19788 DW_AT_linkage_name attribute to the DIE of the
19789 anonymous struct TYPE. The value of that attribute
19790 is the name of the typedef decl naming the anonymous
19791 struct. This greatly eases the work of consumers of
19792 this debug info. */
19793 add_linkage_attr (lookup_type_die (type), decl);
19794 }
19795 }
19796
19797 add_type_attribute (type_die, type, decl_quals (decl), context_die);
19798
19799 if (is_naming_typedef_decl (decl))
19800 /* We want that all subsequent calls to lookup_type_die with
19801 TYPE in argument yield the DW_TAG_typedef we have just
19802 created. */
19803 equate_type_number_to_die (type, type_die);
19804
19805 add_accessibility_attribute (type_die, decl);
19806 }
19807
19808 if (DECL_ABSTRACT (decl))
19809 equate_decl_number_to_die (decl, type_die);
19810
19811 if (get_AT (type_die, DW_AT_name))
19812 add_pubtype (decl, type_die);
19813 }
19814
19815 /* Generate a DIE for a struct, class, enum or union type. */
19816
19817 static void
19818 gen_tagged_type_die (tree type,
19819 dw_die_ref context_die,
19820 enum debug_info_usage usage)
19821 {
19822 int need_pop;
19823
19824 if (type == NULL_TREE
19825 || !is_tagged_type (type))
19826 return;
19827
19828 /* If this is a nested type whose containing class hasn't been written
19829 out yet, writing it out will cover this one, too. This does not apply
19830 to instantiations of member class templates; they need to be added to
19831 the containing class as they are generated. FIXME: This hurts the
19832 idea of combining type decls from multiple TUs, since we can't predict
19833 what set of template instantiations we'll get. */
19834 if (TYPE_CONTEXT (type)
19835 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19836 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19837 {
19838 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19839
19840 if (TREE_ASM_WRITTEN (type))
19841 return;
19842
19843 /* If that failed, attach ourselves to the stub. */
19844 push_decl_scope (TYPE_CONTEXT (type));
19845 context_die = lookup_type_die (TYPE_CONTEXT (type));
19846 need_pop = 1;
19847 }
19848 else if (TYPE_CONTEXT (type) != NULL_TREE
19849 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19850 {
19851 /* If this type is local to a function that hasn't been written
19852 out yet, use a NULL context for now; it will be fixed up in
19853 decls_for_scope. */
19854 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19855 /* A declaration DIE doesn't count; nested types need to go in the
19856 specification. */
19857 if (context_die && is_declaration_die (context_die))
19858 context_die = NULL;
19859 need_pop = 0;
19860 }
19861 else
19862 {
19863 context_die = declare_in_namespace (type, context_die);
19864 need_pop = 0;
19865 }
19866
19867 if (TREE_CODE (type) == ENUMERAL_TYPE)
19868 {
19869 /* This might have been written out by the call to
19870 declare_in_namespace. */
19871 if (!TREE_ASM_WRITTEN (type))
19872 gen_enumeration_type_die (type, context_die);
19873 }
19874 else
19875 gen_struct_or_union_type_die (type, context_die, usage);
19876
19877 if (need_pop)
19878 pop_decl_scope ();
19879
19880 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19881 it up if it is ever completed. gen_*_type_die will set it for us
19882 when appropriate. */
19883 }
19884
19885 /* Generate a type description DIE. */
19886
19887 static void
19888 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19889 enum debug_info_usage usage)
19890 {
19891 struct array_descr_info info;
19892
19893 if (type == NULL_TREE || type == error_mark_node)
19894 return;
19895
19896 if (TYPE_NAME (type) != NULL_TREE
19897 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19898 && is_redundant_typedef (TYPE_NAME (type))
19899 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19900 /* The DECL of this type is a typedef we don't want to emit debug
19901 info for but we want debug info for its underlying typedef.
19902 This can happen for e.g, the injected-class-name of a C++
19903 type. */
19904 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19905
19906 /* If TYPE is a typedef type variant, let's generate debug info
19907 for the parent typedef which TYPE is a type of. */
19908 if (typedef_variant_p (type))
19909 {
19910 if (TREE_ASM_WRITTEN (type))
19911 return;
19912
19913 /* Prevent broken recursion; we can't hand off to the same type. */
19914 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19915
19916 /* Give typedefs the right scope. */
19917 context_die = scope_die_for (type, context_die);
19918
19919 TREE_ASM_WRITTEN (type) = 1;
19920
19921 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19922 return;
19923 }
19924
19925 /* If type is an anonymous tagged type named by a typedef, let's
19926 generate debug info for the typedef. */
19927 if (is_naming_typedef_decl (TYPE_NAME (type)))
19928 {
19929 /* Use the DIE of the containing namespace as the parent DIE of
19930 the type description DIE we want to generate. */
19931 if (DECL_CONTEXT (TYPE_NAME (type))
19932 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19933 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19934
19935 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19936 return;
19937 }
19938
19939 /* If this is an array type with hidden descriptor, handle it first. */
19940 if (!TREE_ASM_WRITTEN (type)
19941 && lang_hooks.types.get_array_descr_info
19942 && lang_hooks.types.get_array_descr_info (type, &info)
19943 && (dwarf_version >= 3 || !dwarf_strict))
19944 {
19945 gen_descr_array_type_die (type, &info, context_die);
19946 TREE_ASM_WRITTEN (type) = 1;
19947 return;
19948 }
19949
19950 /* We are going to output a DIE to represent the unqualified version
19951 of this type (i.e. without any const or volatile qualifiers) so
19952 get the main variant (i.e. the unqualified version) of this type
19953 now. (Vectors are special because the debugging info is in the
19954 cloned type itself). */
19955 if (TREE_CODE (type) != VECTOR_TYPE)
19956 type = type_main_variant (type);
19957
19958 if (TREE_ASM_WRITTEN (type))
19959 return;
19960
19961 switch (TREE_CODE (type))
19962 {
19963 case ERROR_MARK:
19964 break;
19965
19966 case POINTER_TYPE:
19967 case REFERENCE_TYPE:
19968 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19969 ensures that the gen_type_die recursion will terminate even if the
19970 type is recursive. Recursive types are possible in Ada. */
19971 /* ??? We could perhaps do this for all types before the switch
19972 statement. */
19973 TREE_ASM_WRITTEN (type) = 1;
19974
19975 /* For these types, all that is required is that we output a DIE (or a
19976 set of DIEs) to represent the "basis" type. */
19977 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19978 DINFO_USAGE_IND_USE);
19979 break;
19980
19981 case OFFSET_TYPE:
19982 /* This code is used for C++ pointer-to-data-member types.
19983 Output a description of the relevant class type. */
19984 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19985 DINFO_USAGE_IND_USE);
19986
19987 /* Output a description of the type of the object pointed to. */
19988 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19989 DINFO_USAGE_IND_USE);
19990
19991 /* Now output a DIE to represent this pointer-to-data-member type
19992 itself. */
19993 gen_ptr_to_mbr_type_die (type, context_die);
19994 break;
19995
19996 case FUNCTION_TYPE:
19997 /* Force out return type (in case it wasn't forced out already). */
19998 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19999 DINFO_USAGE_DIR_USE);
20000 gen_subroutine_type_die (type, context_die);
20001 break;
20002
20003 case METHOD_TYPE:
20004 /* Force out return type (in case it wasn't forced out already). */
20005 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20006 DINFO_USAGE_DIR_USE);
20007 gen_subroutine_type_die (type, context_die);
20008 break;
20009
20010 case ARRAY_TYPE:
20011 gen_array_type_die (type, context_die);
20012 break;
20013
20014 case VECTOR_TYPE:
20015 gen_array_type_die (type, context_die);
20016 break;
20017
20018 case ENUMERAL_TYPE:
20019 case RECORD_TYPE:
20020 case UNION_TYPE:
20021 case QUAL_UNION_TYPE:
20022 gen_tagged_type_die (type, context_die, usage);
20023 return;
20024
20025 case VOID_TYPE:
20026 case INTEGER_TYPE:
20027 case REAL_TYPE:
20028 case FIXED_POINT_TYPE:
20029 case COMPLEX_TYPE:
20030 case BOOLEAN_TYPE:
20031 /* No DIEs needed for fundamental types. */
20032 break;
20033
20034 case NULLPTR_TYPE:
20035 case LANG_TYPE:
20036 /* Just use DW_TAG_unspecified_type. */
20037 {
20038 dw_die_ref type_die = lookup_type_die (type);
20039 if (type_die == NULL)
20040 {
20041 tree name = TYPE_IDENTIFIER (type);
20042 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20043 type);
20044 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20045 equate_type_number_to_die (type, type_die);
20046 }
20047 }
20048 break;
20049
20050 default:
20051 if (is_cxx_auto (type))
20052 {
20053 tree name = TYPE_IDENTIFIER (type);
20054 dw_die_ref *die = (name == get_identifier ("auto")
20055 ? &auto_die : &decltype_auto_die);
20056 if (!*die)
20057 {
20058 *die = new_die (DW_TAG_unspecified_type,
20059 comp_unit_die (), NULL_TREE);
20060 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20061 }
20062 equate_type_number_to_die (type, *die);
20063 break;
20064 }
20065 gcc_unreachable ();
20066 }
20067
20068 TREE_ASM_WRITTEN (type) = 1;
20069 }
20070
20071 static void
20072 gen_type_die (tree type, dw_die_ref context_die)
20073 {
20074 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20075 }
20076
20077 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20078 things which are local to the given block. */
20079
20080 static void
20081 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20082 {
20083 int must_output_die = 0;
20084 bool inlined_func;
20085
20086 /* Ignore blocks that are NULL. */
20087 if (stmt == NULL_TREE)
20088 return;
20089
20090 inlined_func = inlined_function_outer_scope_p (stmt);
20091
20092 /* If the block is one fragment of a non-contiguous block, do not
20093 process the variables, since they will have been done by the
20094 origin block. Do process subblocks. */
20095 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20096 {
20097 tree sub;
20098
20099 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20100 gen_block_die (sub, context_die, depth + 1);
20101
20102 return;
20103 }
20104
20105 /* Determine if we need to output any Dwarf DIEs at all to represent this
20106 block. */
20107 if (inlined_func)
20108 /* The outer scopes for inlinings *must* always be represented. We
20109 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20110 must_output_die = 1;
20111 else
20112 {
20113 /* Determine if this block directly contains any "significant"
20114 local declarations which we will need to output DIEs for. */
20115 if (debug_info_level > DINFO_LEVEL_TERSE)
20116 /* We are not in terse mode so *any* local declaration counts
20117 as being a "significant" one. */
20118 must_output_die = ((BLOCK_VARS (stmt) != NULL
20119 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20120 && (TREE_USED (stmt)
20121 || TREE_ASM_WRITTEN (stmt)
20122 || BLOCK_ABSTRACT (stmt)));
20123 else if ((TREE_USED (stmt)
20124 || TREE_ASM_WRITTEN (stmt)
20125 || BLOCK_ABSTRACT (stmt))
20126 && !dwarf2out_ignore_block (stmt))
20127 must_output_die = 1;
20128 }
20129
20130 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20131 DIE for any block which contains no significant local declarations at
20132 all. Rather, in such cases we just call `decls_for_scope' so that any
20133 needed Dwarf info for any sub-blocks will get properly generated. Note
20134 that in terse mode, our definition of what constitutes a "significant"
20135 local declaration gets restricted to include only inlined function
20136 instances and local (nested) function definitions. */
20137 if (must_output_die)
20138 {
20139 if (inlined_func)
20140 {
20141 /* If STMT block is abstract, that means we have been called
20142 indirectly from dwarf2out_abstract_function.
20143 That function rightfully marks the descendent blocks (of
20144 the abstract function it is dealing with) as being abstract,
20145 precisely to prevent us from emitting any
20146 DW_TAG_inlined_subroutine DIE as a descendent
20147 of an abstract function instance. So in that case, we should
20148 not call gen_inlined_subroutine_die.
20149
20150 Later though, when cgraph asks dwarf2out to emit info
20151 for the concrete instance of the function decl into which
20152 the concrete instance of STMT got inlined, the later will lead
20153 to the generation of a DW_TAG_inlined_subroutine DIE. */
20154 if (! BLOCK_ABSTRACT (stmt))
20155 gen_inlined_subroutine_die (stmt, context_die, depth);
20156 }
20157 else
20158 gen_lexical_block_die (stmt, context_die, depth);
20159 }
20160 else
20161 decls_for_scope (stmt, context_die, depth);
20162 }
20163
20164 /* Process variable DECL (or variable with origin ORIGIN) within
20165 block STMT and add it to CONTEXT_DIE. */
20166 static void
20167 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20168 {
20169 dw_die_ref die;
20170 tree decl_or_origin = decl ? decl : origin;
20171
20172 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20173 die = lookup_decl_die (decl_or_origin);
20174 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20175 && TYPE_DECL_IS_STUB (decl_or_origin))
20176 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20177 else
20178 die = NULL;
20179
20180 if (die != NULL && die->die_parent == NULL)
20181 add_child_die (context_die, die);
20182 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20183 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20184 stmt, context_die);
20185 else
20186 gen_decl_die (decl, origin, context_die);
20187 }
20188
20189 /* Generate all of the decls declared within a given scope and (recursively)
20190 all of its sub-blocks. */
20191
20192 static void
20193 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20194 {
20195 tree decl;
20196 unsigned int i;
20197 tree subblocks;
20198
20199 /* Ignore NULL blocks. */
20200 if (stmt == NULL_TREE)
20201 return;
20202
20203 /* Output the DIEs to represent all of the data objects and typedefs
20204 declared directly within this block but not within any nested
20205 sub-blocks. Also, nested function and tag DIEs have been
20206 generated with a parent of NULL; fix that up now. We don't
20207 have to do this if we're at -g1. */
20208 if (debug_info_level > DINFO_LEVEL_TERSE)
20209 {
20210 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20211 process_scope_var (stmt, decl, NULL_TREE, context_die);
20212 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20213 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20214 context_die);
20215 }
20216
20217 /* Even if we're at -g1, we need to process the subblocks in order to get
20218 inlined call information. */
20219
20220 /* Output the DIEs to represent all sub-blocks (and the items declared
20221 therein) of this block. */
20222 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20223 subblocks != NULL;
20224 subblocks = BLOCK_CHAIN (subblocks))
20225 gen_block_die (subblocks, context_die, depth + 1);
20226 }
20227
20228 /* Is this a typedef we can avoid emitting? */
20229
20230 static inline int
20231 is_redundant_typedef (const_tree decl)
20232 {
20233 if (TYPE_DECL_IS_STUB (decl))
20234 return 1;
20235
20236 if (DECL_ARTIFICIAL (decl)
20237 && DECL_CONTEXT (decl)
20238 && is_tagged_type (DECL_CONTEXT (decl))
20239 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20240 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20241 /* Also ignore the artificial member typedef for the class name. */
20242 return 1;
20243
20244 return 0;
20245 }
20246
20247 /* Return TRUE if TYPE is a typedef that names a type for linkage
20248 purposes. This kind of typedefs is produced by the C++ FE for
20249 constructs like:
20250
20251 typedef struct {...} foo;
20252
20253 In that case, there is no typedef variant type produced for foo.
20254 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20255 struct type. */
20256
20257 static bool
20258 is_naming_typedef_decl (const_tree decl)
20259 {
20260 if (decl == NULL_TREE
20261 || TREE_CODE (decl) != TYPE_DECL
20262 || !is_tagged_type (TREE_TYPE (decl))
20263 || DECL_IS_BUILTIN (decl)
20264 || is_redundant_typedef (decl)
20265 /* It looks like Ada produces TYPE_DECLs that are very similar
20266 to C++ naming typedefs but that have different
20267 semantics. Let's be specific to c++ for now. */
20268 || !is_cxx ())
20269 return FALSE;
20270
20271 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20272 && TYPE_NAME (TREE_TYPE (decl)) == decl
20273 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20274 != TYPE_NAME (TREE_TYPE (decl))));
20275 }
20276
20277 /* Returns the DIE for a context. */
20278
20279 static inline dw_die_ref
20280 get_context_die (tree context)
20281 {
20282 if (context)
20283 {
20284 /* Find die that represents this context. */
20285 if (TYPE_P (context))
20286 {
20287 context = TYPE_MAIN_VARIANT (context);
20288 return strip_naming_typedef (context, force_type_die (context));
20289 }
20290 else
20291 return force_decl_die (context);
20292 }
20293 return comp_unit_die ();
20294 }
20295
20296 /* Returns the DIE for decl. A DIE will always be returned. */
20297
20298 static dw_die_ref
20299 force_decl_die (tree decl)
20300 {
20301 dw_die_ref decl_die;
20302 unsigned saved_external_flag;
20303 tree save_fn = NULL_TREE;
20304 decl_die = lookup_decl_die (decl);
20305 if (!decl_die)
20306 {
20307 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20308
20309 decl_die = lookup_decl_die (decl);
20310 if (decl_die)
20311 return decl_die;
20312
20313 switch (TREE_CODE (decl))
20314 {
20315 case FUNCTION_DECL:
20316 /* Clear current_function_decl, so that gen_subprogram_die thinks
20317 that this is a declaration. At this point, we just want to force
20318 declaration die. */
20319 save_fn = current_function_decl;
20320 current_function_decl = NULL_TREE;
20321 gen_subprogram_die (decl, context_die);
20322 current_function_decl = save_fn;
20323 break;
20324
20325 case VAR_DECL:
20326 /* Set external flag to force declaration die. Restore it after
20327 gen_decl_die() call. */
20328 saved_external_flag = DECL_EXTERNAL (decl);
20329 DECL_EXTERNAL (decl) = 1;
20330 gen_decl_die (decl, NULL, context_die);
20331 DECL_EXTERNAL (decl) = saved_external_flag;
20332 break;
20333
20334 case NAMESPACE_DECL:
20335 if (dwarf_version >= 3 || !dwarf_strict)
20336 dwarf2out_decl (decl);
20337 else
20338 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20339 decl_die = comp_unit_die ();
20340 break;
20341
20342 case TRANSLATION_UNIT_DECL:
20343 decl_die = comp_unit_die ();
20344 break;
20345
20346 default:
20347 gcc_unreachable ();
20348 }
20349
20350 /* We should be able to find the DIE now. */
20351 if (!decl_die)
20352 decl_die = lookup_decl_die (decl);
20353 gcc_assert (decl_die);
20354 }
20355
20356 return decl_die;
20357 }
20358
20359 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20360 always returned. */
20361
20362 static dw_die_ref
20363 force_type_die (tree type)
20364 {
20365 dw_die_ref type_die;
20366
20367 type_die = lookup_type_die (type);
20368 if (!type_die)
20369 {
20370 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20371
20372 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20373 context_die);
20374 gcc_assert (type_die);
20375 }
20376 return type_die;
20377 }
20378
20379 /* Force out any required namespaces to be able to output DECL,
20380 and return the new context_die for it, if it's changed. */
20381
20382 static dw_die_ref
20383 setup_namespace_context (tree thing, dw_die_ref context_die)
20384 {
20385 tree context = (DECL_P (thing)
20386 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20387 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20388 /* Force out the namespace. */
20389 context_die = force_decl_die (context);
20390
20391 return context_die;
20392 }
20393
20394 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20395 type) within its namespace, if appropriate.
20396
20397 For compatibility with older debuggers, namespace DIEs only contain
20398 declarations; all definitions are emitted at CU scope. */
20399
20400 static dw_die_ref
20401 declare_in_namespace (tree thing, dw_die_ref context_die)
20402 {
20403 dw_die_ref ns_context;
20404
20405 if (debug_info_level <= DINFO_LEVEL_TERSE)
20406 return context_die;
20407
20408 /* If this decl is from an inlined function, then don't try to emit it in its
20409 namespace, as we will get confused. It would have already been emitted
20410 when the abstract instance of the inline function was emitted anyways. */
20411 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20412 return context_die;
20413
20414 ns_context = setup_namespace_context (thing, context_die);
20415
20416 if (ns_context != context_die)
20417 {
20418 if (is_fortran ())
20419 return ns_context;
20420 if (DECL_P (thing))
20421 gen_decl_die (thing, NULL, ns_context);
20422 else
20423 gen_type_die (thing, ns_context);
20424 }
20425 return context_die;
20426 }
20427
20428 /* Generate a DIE for a namespace or namespace alias. */
20429
20430 static void
20431 gen_namespace_die (tree decl, dw_die_ref context_die)
20432 {
20433 dw_die_ref namespace_die;
20434
20435 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20436 they are an alias of. */
20437 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20438 {
20439 /* Output a real namespace or module. */
20440 context_die = setup_namespace_context (decl, comp_unit_die ());
20441 namespace_die = new_die (is_fortran ()
20442 ? DW_TAG_module : DW_TAG_namespace,
20443 context_die, decl);
20444 /* For Fortran modules defined in different CU don't add src coords. */
20445 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20446 {
20447 const char *name = dwarf2_name (decl, 0);
20448 if (name)
20449 add_name_attribute (namespace_die, name);
20450 }
20451 else
20452 add_name_and_src_coords_attributes (namespace_die, decl);
20453 if (DECL_EXTERNAL (decl))
20454 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20455 equate_decl_number_to_die (decl, namespace_die);
20456 }
20457 else
20458 {
20459 /* Output a namespace alias. */
20460
20461 /* Force out the namespace we are an alias of, if necessary. */
20462 dw_die_ref origin_die
20463 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20464
20465 if (DECL_FILE_SCOPE_P (decl)
20466 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20467 context_die = setup_namespace_context (decl, comp_unit_die ());
20468 /* Now create the namespace alias DIE. */
20469 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20470 add_name_and_src_coords_attributes (namespace_die, decl);
20471 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20472 equate_decl_number_to_die (decl, namespace_die);
20473 }
20474 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20475 if (want_pubnames ())
20476 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20477 }
20478
20479 /* Generate Dwarf debug information for a decl described by DECL.
20480 The return value is currently only meaningful for PARM_DECLs,
20481 for all other decls it returns NULL. */
20482
20483 static dw_die_ref
20484 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20485 {
20486 tree decl_or_origin = decl ? decl : origin;
20487 tree class_origin = NULL, ultimate_origin;
20488
20489 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20490 return NULL;
20491
20492 switch (TREE_CODE (decl_or_origin))
20493 {
20494 case ERROR_MARK:
20495 break;
20496
20497 case CONST_DECL:
20498 if (!is_fortran () && !is_ada ())
20499 {
20500 /* The individual enumerators of an enum type get output when we output
20501 the Dwarf representation of the relevant enum type itself. */
20502 break;
20503 }
20504
20505 /* Emit its type. */
20506 gen_type_die (TREE_TYPE (decl), context_die);
20507
20508 /* And its containing namespace. */
20509 context_die = declare_in_namespace (decl, context_die);
20510
20511 gen_const_die (decl, context_die);
20512 break;
20513
20514 case FUNCTION_DECL:
20515 /* Don't output any DIEs to represent mere function declarations,
20516 unless they are class members or explicit block externs. */
20517 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20518 && DECL_FILE_SCOPE_P (decl_or_origin)
20519 && (current_function_decl == NULL_TREE
20520 || DECL_ARTIFICIAL (decl_or_origin)))
20521 break;
20522
20523 #if 0
20524 /* FIXME */
20525 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20526 on local redeclarations of global functions. That seems broken. */
20527 if (current_function_decl != decl)
20528 /* This is only a declaration. */;
20529 #endif
20530
20531 /* If we're emitting a clone, emit info for the abstract instance. */
20532 if (origin || DECL_ORIGIN (decl) != decl)
20533 dwarf2out_abstract_function (origin
20534 ? DECL_ORIGIN (origin)
20535 : DECL_ABSTRACT_ORIGIN (decl));
20536
20537 /* If we're emitting an out-of-line copy of an inline function,
20538 emit info for the abstract instance and set up to refer to it. */
20539 else if (cgraph_function_possibly_inlined_p (decl)
20540 && ! DECL_ABSTRACT (decl)
20541 && ! class_or_namespace_scope_p (context_die)
20542 /* dwarf2out_abstract_function won't emit a die if this is just
20543 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20544 that case, because that works only if we have a die. */
20545 && DECL_INITIAL (decl) != NULL_TREE)
20546 {
20547 dwarf2out_abstract_function (decl);
20548 set_decl_origin_self (decl);
20549 }
20550
20551 /* Otherwise we're emitting the primary DIE for this decl. */
20552 else if (debug_info_level > DINFO_LEVEL_TERSE)
20553 {
20554 /* Before we describe the FUNCTION_DECL itself, make sure that we
20555 have its containing type. */
20556 if (!origin)
20557 origin = decl_class_context (decl);
20558 if (origin != NULL_TREE)
20559 gen_type_die (origin, context_die);
20560
20561 /* And its return type. */
20562 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20563
20564 /* And its virtual context. */
20565 if (DECL_VINDEX (decl) != NULL_TREE)
20566 gen_type_die (DECL_CONTEXT (decl), context_die);
20567
20568 /* Make sure we have a member DIE for decl. */
20569 if (origin != NULL_TREE)
20570 gen_type_die_for_member (origin, decl, context_die);
20571
20572 /* And its containing namespace. */
20573 context_die = declare_in_namespace (decl, context_die);
20574 }
20575
20576 /* Now output a DIE to represent the function itself. */
20577 if (decl)
20578 gen_subprogram_die (decl, context_die);
20579 break;
20580
20581 case TYPE_DECL:
20582 /* If we are in terse mode, don't generate any DIEs to represent any
20583 actual typedefs. */
20584 if (debug_info_level <= DINFO_LEVEL_TERSE)
20585 break;
20586
20587 /* In the special case of a TYPE_DECL node representing the declaration
20588 of some type tag, if the given TYPE_DECL is marked as having been
20589 instantiated from some other (original) TYPE_DECL node (e.g. one which
20590 was generated within the original definition of an inline function) we
20591 used to generate a special (abbreviated) DW_TAG_structure_type,
20592 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20593 should be actually referencing those DIEs, as variable DIEs with that
20594 type would be emitted already in the abstract origin, so it was always
20595 removed during unused type prunning. Don't add anything in this
20596 case. */
20597 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20598 break;
20599
20600 if (is_redundant_typedef (decl))
20601 gen_type_die (TREE_TYPE (decl), context_die);
20602 else
20603 /* Output a DIE to represent the typedef itself. */
20604 gen_typedef_die (decl, context_die);
20605 break;
20606
20607 case LABEL_DECL:
20608 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20609 gen_label_die (decl, context_die);
20610 break;
20611
20612 case VAR_DECL:
20613 case RESULT_DECL:
20614 /* If we are in terse mode, don't generate any DIEs to represent any
20615 variable declarations or definitions. */
20616 if (debug_info_level <= DINFO_LEVEL_TERSE)
20617 break;
20618
20619 /* Output any DIEs that are needed to specify the type of this data
20620 object. */
20621 if (decl_by_reference_p (decl_or_origin))
20622 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20623 else
20624 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20625
20626 /* And its containing type. */
20627 class_origin = decl_class_context (decl_or_origin);
20628 if (class_origin != NULL_TREE)
20629 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20630
20631 /* And its containing namespace. */
20632 context_die = declare_in_namespace (decl_or_origin, context_die);
20633
20634 /* Now output the DIE to represent the data object itself. This gets
20635 complicated because of the possibility that the VAR_DECL really
20636 represents an inlined instance of a formal parameter for an inline
20637 function. */
20638 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20639 if (ultimate_origin != NULL_TREE
20640 && TREE_CODE (ultimate_origin) == PARM_DECL)
20641 gen_formal_parameter_die (decl, origin,
20642 true /* Emit name attribute. */,
20643 context_die);
20644 else
20645 gen_variable_die (decl, origin, context_die);
20646 break;
20647
20648 case FIELD_DECL:
20649 /* Ignore the nameless fields that are used to skip bits but handle C++
20650 anonymous unions and structs. */
20651 if (DECL_NAME (decl) != NULL_TREE
20652 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20653 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20654 {
20655 gen_type_die (member_declared_type (decl), context_die);
20656 gen_field_die (decl, context_die);
20657 }
20658 break;
20659
20660 case PARM_DECL:
20661 if (DECL_BY_REFERENCE (decl_or_origin))
20662 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20663 else
20664 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20665 return gen_formal_parameter_die (decl, origin,
20666 true /* Emit name attribute. */,
20667 context_die);
20668
20669 case NAMESPACE_DECL:
20670 case IMPORTED_DECL:
20671 if (dwarf_version >= 3 || !dwarf_strict)
20672 gen_namespace_die (decl, context_die);
20673 break;
20674
20675 case NAMELIST_DECL:
20676 gen_namelist_decl (DECL_NAME (decl), context_die,
20677 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20678 break;
20679
20680 default:
20681 /* Probably some frontend-internal decl. Assume we don't care. */
20682 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20683 break;
20684 }
20685
20686 return NULL;
20687 }
20688 \f
20689 /* Output debug information for global decl DECL. Called from toplev.c after
20690 compilation proper has finished. */
20691
20692 static void
20693 dwarf2out_global_decl (tree decl)
20694 {
20695 /* Output DWARF2 information for file-scope tentative data object
20696 declarations, file-scope (extern) function declarations (which
20697 had no corresponding body) and file-scope tagged type declarations
20698 and definitions which have not yet been forced out. */
20699 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20700 dwarf2out_decl (decl);
20701 }
20702
20703 /* Output debug information for type decl DECL. Called from toplev.c
20704 and from language front ends (to record built-in types). */
20705 static void
20706 dwarf2out_type_decl (tree decl, int local)
20707 {
20708 if (!local)
20709 dwarf2out_decl (decl);
20710 }
20711
20712 /* Output debug information for imported module or decl DECL.
20713 NAME is non-NULL name in the lexical block if the decl has been renamed.
20714 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20715 that DECL belongs to.
20716 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20717 static void
20718 dwarf2out_imported_module_or_decl_1 (tree decl,
20719 tree name,
20720 tree lexical_block,
20721 dw_die_ref lexical_block_die)
20722 {
20723 expanded_location xloc;
20724 dw_die_ref imported_die = NULL;
20725 dw_die_ref at_import_die;
20726
20727 if (TREE_CODE (decl) == IMPORTED_DECL)
20728 {
20729 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20730 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20731 gcc_assert (decl);
20732 }
20733 else
20734 xloc = expand_location (input_location);
20735
20736 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20737 {
20738 at_import_die = force_type_die (TREE_TYPE (decl));
20739 /* For namespace N { typedef void T; } using N::T; base_type_die
20740 returns NULL, but DW_TAG_imported_declaration requires
20741 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20742 if (!at_import_die)
20743 {
20744 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20745 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20746 at_import_die = lookup_type_die (TREE_TYPE (decl));
20747 gcc_assert (at_import_die);
20748 }
20749 }
20750 else
20751 {
20752 at_import_die = lookup_decl_die (decl);
20753 if (!at_import_die)
20754 {
20755 /* If we're trying to avoid duplicate debug info, we may not have
20756 emitted the member decl for this field. Emit it now. */
20757 if (TREE_CODE (decl) == FIELD_DECL)
20758 {
20759 tree type = DECL_CONTEXT (decl);
20760
20761 if (TYPE_CONTEXT (type)
20762 && TYPE_P (TYPE_CONTEXT (type))
20763 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20764 DINFO_USAGE_DIR_USE))
20765 return;
20766 gen_type_die_for_member (type, decl,
20767 get_context_die (TYPE_CONTEXT (type)));
20768 }
20769 if (TREE_CODE (decl) == NAMELIST_DECL)
20770 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20771 get_context_die (DECL_CONTEXT (decl)),
20772 NULL_TREE);
20773 else
20774 at_import_die = force_decl_die (decl);
20775 }
20776 }
20777
20778 if (TREE_CODE (decl) == NAMESPACE_DECL)
20779 {
20780 if (dwarf_version >= 3 || !dwarf_strict)
20781 imported_die = new_die (DW_TAG_imported_module,
20782 lexical_block_die,
20783 lexical_block);
20784 else
20785 return;
20786 }
20787 else
20788 imported_die = new_die (DW_TAG_imported_declaration,
20789 lexical_block_die,
20790 lexical_block);
20791
20792 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20793 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20794 if (name)
20795 add_AT_string (imported_die, DW_AT_name,
20796 IDENTIFIER_POINTER (name));
20797 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20798 }
20799
20800 /* Output debug information for imported module or decl DECL.
20801 NAME is non-NULL name in context if the decl has been renamed.
20802 CHILD is true if decl is one of the renamed decls as part of
20803 importing whole module. */
20804
20805 static void
20806 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20807 bool child)
20808 {
20809 /* dw_die_ref at_import_die; */
20810 dw_die_ref scope_die;
20811
20812 if (debug_info_level <= DINFO_LEVEL_TERSE)
20813 return;
20814
20815 gcc_assert (decl);
20816
20817 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20818 We need decl DIE for reference and scope die. First, get DIE for the decl
20819 itself. */
20820
20821 /* Get the scope die for decl context. Use comp_unit_die for global module
20822 or decl. If die is not found for non globals, force new die. */
20823 if (context
20824 && TYPE_P (context)
20825 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20826 return;
20827
20828 if (!(dwarf_version >= 3 || !dwarf_strict))
20829 return;
20830
20831 scope_die = get_context_die (context);
20832
20833 if (child)
20834 {
20835 gcc_assert (scope_die->die_child);
20836 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20837 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20838 scope_die = scope_die->die_child;
20839 }
20840
20841 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20842 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20843
20844 }
20845
20846 /* Output debug information for namelists. */
20847
20848 static dw_die_ref
20849 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20850 {
20851 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20852 tree value;
20853 unsigned i;
20854
20855 if (debug_info_level <= DINFO_LEVEL_TERSE)
20856 return NULL;
20857
20858 gcc_assert (scope_die != NULL);
20859 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20860 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20861
20862 /* If there are no item_decls, we have a nondefining namelist, e.g.
20863 with USE association; hence, set DW_AT_declaration. */
20864 if (item_decls == NULL_TREE)
20865 {
20866 add_AT_flag (nml_die, DW_AT_declaration, 1);
20867 return nml_die;
20868 }
20869
20870 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20871 {
20872 nml_item_ref_die = lookup_decl_die (value);
20873 if (!nml_item_ref_die)
20874 nml_item_ref_die = force_decl_die (value);
20875
20876 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20877 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20878 }
20879 return nml_die;
20880 }
20881
20882
20883 /* Write the debugging output for DECL. */
20884
20885 static void
20886 dwarf2out_decl (tree decl)
20887 {
20888 dw_die_ref context_die = comp_unit_die ();
20889
20890 switch (TREE_CODE (decl))
20891 {
20892 case ERROR_MARK:
20893 return;
20894
20895 case FUNCTION_DECL:
20896 /* What we would really like to do here is to filter out all mere
20897 file-scope declarations of file-scope functions which are never
20898 referenced later within this translation unit (and keep all of ones
20899 that *are* referenced later on) but we aren't clairvoyant, so we have
20900 no idea which functions will be referenced in the future (i.e. later
20901 on within the current translation unit). So here we just ignore all
20902 file-scope function declarations which are not also definitions. If
20903 and when the debugger needs to know something about these functions,
20904 it will have to hunt around and find the DWARF information associated
20905 with the definition of the function.
20906
20907 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20908 nodes represent definitions and which ones represent mere
20909 declarations. We have to check DECL_INITIAL instead. That's because
20910 the C front-end supports some weird semantics for "extern inline"
20911 function definitions. These can get inlined within the current
20912 translation unit (and thus, we need to generate Dwarf info for their
20913 abstract instances so that the Dwarf info for the concrete inlined
20914 instances can have something to refer to) but the compiler never
20915 generates any out-of-lines instances of such things (despite the fact
20916 that they *are* definitions).
20917
20918 The important point is that the C front-end marks these "extern
20919 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20920 them anyway. Note that the C++ front-end also plays some similar games
20921 for inline function definitions appearing within include files which
20922 also contain `#pragma interface' pragmas.
20923
20924 If we are called from dwarf2out_abstract_function output a DIE
20925 anyway. We can end up here this way with early inlining and LTO
20926 where the inlined function is output in a different LTRANS unit
20927 or not at all. */
20928 if (DECL_INITIAL (decl) == NULL_TREE
20929 && ! DECL_ABSTRACT (decl))
20930 return;
20931
20932 /* If we're a nested function, initially use a parent of NULL; if we're
20933 a plain function, this will be fixed up in decls_for_scope. If
20934 we're a method, it will be ignored, since we already have a DIE. */
20935 if (decl_function_context (decl)
20936 /* But if we're in terse mode, we don't care about scope. */
20937 && debug_info_level > DINFO_LEVEL_TERSE)
20938 context_die = NULL;
20939 break;
20940
20941 case VAR_DECL:
20942 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20943 declaration and if the declaration was never even referenced from
20944 within this entire compilation unit. We suppress these DIEs in
20945 order to save space in the .debug section (by eliminating entries
20946 which are probably useless). Note that we must not suppress
20947 block-local extern declarations (whether used or not) because that
20948 would screw-up the debugger's name lookup mechanism and cause it to
20949 miss things which really ought to be in scope at a given point. */
20950 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20951 return;
20952
20953 /* For local statics lookup proper context die. */
20954 if (TREE_STATIC (decl)
20955 && DECL_CONTEXT (decl)
20956 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20957 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20958
20959 /* If we are in terse mode, don't generate any DIEs to represent any
20960 variable declarations or definitions. */
20961 if (debug_info_level <= DINFO_LEVEL_TERSE)
20962 return;
20963 break;
20964
20965 case CONST_DECL:
20966 if (debug_info_level <= DINFO_LEVEL_TERSE)
20967 return;
20968 if (!is_fortran () && !is_ada ())
20969 return;
20970 if (TREE_STATIC (decl) && decl_function_context (decl))
20971 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20972 break;
20973
20974 case NAMESPACE_DECL:
20975 case IMPORTED_DECL:
20976 if (debug_info_level <= DINFO_LEVEL_TERSE)
20977 return;
20978 if (lookup_decl_die (decl) != NULL)
20979 return;
20980 break;
20981
20982 case TYPE_DECL:
20983 /* Don't emit stubs for types unless they are needed by other DIEs. */
20984 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20985 return;
20986
20987 /* Don't bother trying to generate any DIEs to represent any of the
20988 normal built-in types for the language we are compiling. */
20989 if (DECL_IS_BUILTIN (decl))
20990 return;
20991
20992 /* If we are in terse mode, don't generate any DIEs for types. */
20993 if (debug_info_level <= DINFO_LEVEL_TERSE)
20994 return;
20995
20996 /* If we're a function-scope tag, initially use a parent of NULL;
20997 this will be fixed up in decls_for_scope. */
20998 if (decl_function_context (decl))
20999 context_die = NULL;
21000
21001 break;
21002
21003 case NAMELIST_DECL:
21004 break;
21005
21006 default:
21007 return;
21008 }
21009
21010 gen_decl_die (decl, NULL, context_die);
21011 }
21012
21013 /* Write the debugging output for DECL. */
21014
21015 static void
21016 dwarf2out_function_decl (tree decl)
21017 {
21018 dwarf2out_decl (decl);
21019 call_arg_locations = NULL;
21020 call_arg_loc_last = NULL;
21021 call_site_count = -1;
21022 tail_call_site_count = -1;
21023 block_map.release ();
21024 htab_empty (decl_loc_table);
21025 htab_empty (cached_dw_loc_list_table);
21026 }
21027
21028 /* Output a marker (i.e. a label) for the beginning of the generated code for
21029 a lexical block. */
21030
21031 static void
21032 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21033 unsigned int blocknum)
21034 {
21035 switch_to_section (current_function_section ());
21036 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21037 }
21038
21039 /* Output a marker (i.e. a label) for the end of the generated code for a
21040 lexical block. */
21041
21042 static void
21043 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21044 {
21045 switch_to_section (current_function_section ());
21046 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21047 }
21048
21049 /* Returns nonzero if it is appropriate not to emit any debugging
21050 information for BLOCK, because it doesn't contain any instructions.
21051
21052 Don't allow this for blocks with nested functions or local classes
21053 as we would end up with orphans, and in the presence of scheduling
21054 we may end up calling them anyway. */
21055
21056 static bool
21057 dwarf2out_ignore_block (const_tree block)
21058 {
21059 tree decl;
21060 unsigned int i;
21061
21062 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21063 if (TREE_CODE (decl) == FUNCTION_DECL
21064 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21065 return 0;
21066 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21067 {
21068 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21069 if (TREE_CODE (decl) == FUNCTION_DECL
21070 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21071 return 0;
21072 }
21073
21074 return 1;
21075 }
21076
21077 /* Hash table routines for file_hash. */
21078
21079 static int
21080 file_table_eq (const void *p1_p, const void *p2_p)
21081 {
21082 const struct dwarf_file_data *const p1 =
21083 (const struct dwarf_file_data *) p1_p;
21084 const char *const p2 = (const char *) p2_p;
21085 return filename_cmp (p1->filename, p2) == 0;
21086 }
21087
21088 static hashval_t
21089 file_table_hash (const void *p_p)
21090 {
21091 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21092 return htab_hash_string (p->filename);
21093 }
21094
21095 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21096 dwarf2out.c) and return its "index". The index of each (known) filename is
21097 just a unique number which is associated with only that one filename. We
21098 need such numbers for the sake of generating labels (in the .debug_sfnames
21099 section) and references to those files numbers (in the .debug_srcinfo
21100 and.debug_macinfo sections). If the filename given as an argument is not
21101 found in our current list, add it to the list and assign it the next
21102 available unique index number. In order to speed up searches, we remember
21103 the index of the filename was looked up last. This handles the majority of
21104 all searches. */
21105
21106 static struct dwarf_file_data *
21107 lookup_filename (const char *file_name)
21108 {
21109 void ** slot;
21110 struct dwarf_file_data * created;
21111
21112 /* Check to see if the file name that was searched on the previous
21113 call matches this file name. If so, return the index. */
21114 if (file_table_last_lookup
21115 && (file_name == file_table_last_lookup->filename
21116 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21117 return file_table_last_lookup;
21118
21119 /* Didn't match the previous lookup, search the table. */
21120 slot = htab_find_slot_with_hash (file_table, file_name,
21121 htab_hash_string (file_name), INSERT);
21122 if (*slot)
21123 return (struct dwarf_file_data *) *slot;
21124
21125 created = ggc_alloc<dwarf_file_data> ();
21126 created->filename = file_name;
21127 created->emitted_number = 0;
21128 *slot = created;
21129 return created;
21130 }
21131
21132 /* If the assembler will construct the file table, then translate the compiler
21133 internal file table number into the assembler file table number, and emit
21134 a .file directive if we haven't already emitted one yet. The file table
21135 numbers are different because we prune debug info for unused variables and
21136 types, which may include filenames. */
21137
21138 static int
21139 maybe_emit_file (struct dwarf_file_data * fd)
21140 {
21141 if (! fd->emitted_number)
21142 {
21143 if (last_emitted_file)
21144 fd->emitted_number = last_emitted_file->emitted_number + 1;
21145 else
21146 fd->emitted_number = 1;
21147 last_emitted_file = fd;
21148
21149 if (DWARF2_ASM_LINE_DEBUG_INFO)
21150 {
21151 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21152 output_quoted_string (asm_out_file,
21153 remap_debug_filename (fd->filename));
21154 fputc ('\n', asm_out_file);
21155 }
21156 }
21157
21158 return fd->emitted_number;
21159 }
21160
21161 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21162 That generation should happen after function debug info has been
21163 generated. The value of the attribute is the constant value of ARG. */
21164
21165 static void
21166 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21167 {
21168 die_arg_entry entry;
21169
21170 if (!die || !arg)
21171 return;
21172
21173 if (!tmpl_value_parm_die_table)
21174 vec_alloc (tmpl_value_parm_die_table, 32);
21175
21176 entry.die = die;
21177 entry.arg = arg;
21178 vec_safe_push (tmpl_value_parm_die_table, entry);
21179 }
21180
21181 /* Return TRUE if T is an instance of generic type, FALSE
21182 otherwise. */
21183
21184 static bool
21185 generic_type_p (tree t)
21186 {
21187 if (t == NULL_TREE || !TYPE_P (t))
21188 return false;
21189 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21190 }
21191
21192 /* Schedule the generation of the generic parameter dies for the
21193 instance of generic type T. The proper generation itself is later
21194 done by gen_scheduled_generic_parms_dies. */
21195
21196 static void
21197 schedule_generic_params_dies_gen (tree t)
21198 {
21199 if (!generic_type_p (t))
21200 return;
21201
21202 if (!generic_type_instances)
21203 vec_alloc (generic_type_instances, 256);
21204
21205 vec_safe_push (generic_type_instances, t);
21206 }
21207
21208 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21209 by append_entry_to_tmpl_value_parm_die_table. This function must
21210 be called after function DIEs have been generated. */
21211
21212 static void
21213 gen_remaining_tmpl_value_param_die_attribute (void)
21214 {
21215 if (tmpl_value_parm_die_table)
21216 {
21217 unsigned i;
21218 die_arg_entry *e;
21219
21220 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21221 tree_add_const_value_attribute (e->die, e->arg);
21222 }
21223 }
21224
21225 /* Generate generic parameters DIEs for instances of generic types
21226 that have been previously scheduled by
21227 schedule_generic_params_dies_gen. This function must be called
21228 after all the types of the CU have been laid out. */
21229
21230 static void
21231 gen_scheduled_generic_parms_dies (void)
21232 {
21233 unsigned i;
21234 tree t;
21235
21236 if (!generic_type_instances)
21237 return;
21238
21239 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21240 if (COMPLETE_TYPE_P (t))
21241 gen_generic_params_dies (t);
21242 }
21243
21244
21245 /* Replace DW_AT_name for the decl with name. */
21246
21247 static void
21248 dwarf2out_set_name (tree decl, tree name)
21249 {
21250 dw_die_ref die;
21251 dw_attr_ref attr;
21252 const char *dname;
21253
21254 die = TYPE_SYMTAB_DIE (decl);
21255 if (!die)
21256 return;
21257
21258 dname = dwarf2_name (name, 0);
21259 if (!dname)
21260 return;
21261
21262 attr = get_AT (die, DW_AT_name);
21263 if (attr)
21264 {
21265 struct indirect_string_node *node;
21266
21267 node = find_AT_string (dname);
21268 /* replace the string. */
21269 attr->dw_attr_val.v.val_str = node;
21270 }
21271
21272 else
21273 add_name_attribute (die, dname);
21274 }
21275
21276 /* True if before or during processing of the first function being emitted. */
21277 static bool in_first_function_p = true;
21278 /* True if loc_note during dwarf2out_var_location call might still be
21279 before first real instruction at address equal to .Ltext0. */
21280 static bool maybe_at_text_label_p = true;
21281 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21282 static unsigned int first_loclabel_num_not_at_text_label;
21283
21284 /* Called by the final INSN scan whenever we see a var location. We
21285 use it to drop labels in the right places, and throw the location in
21286 our lookup table. */
21287
21288 static void
21289 dwarf2out_var_location (rtx_insn *loc_note)
21290 {
21291 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21292 struct var_loc_node *newloc;
21293 rtx_insn *next_real, *next_note;
21294 static const char *last_label;
21295 static const char *last_postcall_label;
21296 static bool last_in_cold_section_p;
21297 static rtx_insn *expected_next_loc_note;
21298 tree decl;
21299 bool var_loc_p;
21300
21301 if (!NOTE_P (loc_note))
21302 {
21303 if (CALL_P (loc_note))
21304 {
21305 call_site_count++;
21306 if (SIBLING_CALL_P (loc_note))
21307 tail_call_site_count++;
21308 }
21309 return;
21310 }
21311
21312 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21313 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21314 return;
21315
21316 /* Optimize processing a large consecutive sequence of location
21317 notes so we don't spend too much time in next_real_insn. If the
21318 next insn is another location note, remember the next_real_insn
21319 calculation for next time. */
21320 next_real = cached_next_real_insn;
21321 if (next_real)
21322 {
21323 if (expected_next_loc_note != loc_note)
21324 next_real = NULL;
21325 }
21326
21327 next_note = NEXT_INSN (loc_note);
21328 if (! next_note
21329 || INSN_DELETED_P (next_note)
21330 || ! NOTE_P (next_note)
21331 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21332 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21333 next_note = NULL;
21334
21335 if (! next_real)
21336 next_real = next_real_insn (loc_note);
21337
21338 if (next_note)
21339 {
21340 expected_next_loc_note = next_note;
21341 cached_next_real_insn = next_real;
21342 }
21343 else
21344 cached_next_real_insn = NULL;
21345
21346 /* If there are no instructions which would be affected by this note,
21347 don't do anything. */
21348 if (var_loc_p
21349 && next_real == NULL_RTX
21350 && !NOTE_DURING_CALL_P (loc_note))
21351 return;
21352
21353 if (next_real == NULL_RTX)
21354 next_real = get_last_insn ();
21355
21356 /* If there were any real insns between note we processed last time
21357 and this note (or if it is the first note), clear
21358 last_{,postcall_}label so that they are not reused this time. */
21359 if (last_var_location_insn == NULL_RTX
21360 || last_var_location_insn != next_real
21361 || last_in_cold_section_p != in_cold_section_p)
21362 {
21363 last_label = NULL;
21364 last_postcall_label = NULL;
21365 }
21366
21367 if (var_loc_p)
21368 {
21369 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21370 newloc = add_var_loc_to_decl (decl, loc_note,
21371 NOTE_DURING_CALL_P (loc_note)
21372 ? last_postcall_label : last_label);
21373 if (newloc == NULL)
21374 return;
21375 }
21376 else
21377 {
21378 decl = NULL_TREE;
21379 newloc = NULL;
21380 }
21381
21382 /* If there were no real insns between note we processed last time
21383 and this note, use the label we emitted last time. Otherwise
21384 create a new label and emit it. */
21385 if (last_label == NULL)
21386 {
21387 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21388 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21389 loclabel_num++;
21390 last_label = ggc_strdup (loclabel);
21391 /* See if loclabel might be equal to .Ltext0. If yes,
21392 bump first_loclabel_num_not_at_text_label. */
21393 if (!have_multiple_function_sections
21394 && in_first_function_p
21395 && maybe_at_text_label_p)
21396 {
21397 static rtx_insn *last_start;
21398 rtx_insn *insn;
21399 for (insn = loc_note; insn; insn = previous_insn (insn))
21400 if (insn == last_start)
21401 break;
21402 else if (!NONDEBUG_INSN_P (insn))
21403 continue;
21404 else
21405 {
21406 rtx body = PATTERN (insn);
21407 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21408 continue;
21409 /* Inline asm could occupy zero bytes. */
21410 else if (GET_CODE (body) == ASM_INPUT
21411 || asm_noperands (body) >= 0)
21412 continue;
21413 #ifdef HAVE_attr_length
21414 else if (get_attr_min_length (insn) == 0)
21415 continue;
21416 #endif
21417 else
21418 {
21419 /* Assume insn has non-zero length. */
21420 maybe_at_text_label_p = false;
21421 break;
21422 }
21423 }
21424 if (maybe_at_text_label_p)
21425 {
21426 last_start = loc_note;
21427 first_loclabel_num_not_at_text_label = loclabel_num;
21428 }
21429 }
21430 }
21431
21432 if (!var_loc_p)
21433 {
21434 struct call_arg_loc_node *ca_loc
21435 = ggc_cleared_alloc<call_arg_loc_node> ();
21436 rtx prev = prev_real_insn (loc_note), x;
21437 ca_loc->call_arg_loc_note = loc_note;
21438 ca_loc->next = NULL;
21439 ca_loc->label = last_label;
21440 gcc_assert (prev
21441 && (CALL_P (prev)
21442 || (NONJUMP_INSN_P (prev)
21443 && GET_CODE (PATTERN (prev)) == SEQUENCE
21444 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21445 if (!CALL_P (prev))
21446 prev = XVECEXP (PATTERN (prev), 0, 0);
21447 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21448 x = get_call_rtx_from (PATTERN (prev));
21449 if (x)
21450 {
21451 x = XEXP (XEXP (x, 0), 0);
21452 if (GET_CODE (x) == SYMBOL_REF
21453 && SYMBOL_REF_DECL (x)
21454 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21455 ca_loc->symbol_ref = x;
21456 }
21457 ca_loc->block = insn_scope (prev);
21458 if (call_arg_locations)
21459 call_arg_loc_last->next = ca_loc;
21460 else
21461 call_arg_locations = ca_loc;
21462 call_arg_loc_last = ca_loc;
21463 }
21464 else if (!NOTE_DURING_CALL_P (loc_note))
21465 newloc->label = last_label;
21466 else
21467 {
21468 if (!last_postcall_label)
21469 {
21470 sprintf (loclabel, "%s-1", last_label);
21471 last_postcall_label = ggc_strdup (loclabel);
21472 }
21473 newloc->label = last_postcall_label;
21474 }
21475
21476 last_var_location_insn = next_real;
21477 last_in_cold_section_p = in_cold_section_p;
21478 }
21479
21480 /* Note in one location list that text section has changed. */
21481
21482 static int
21483 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21484 {
21485 var_loc_list *list = (var_loc_list *) *slot;
21486 if (list->first)
21487 list->last_before_switch
21488 = list->last->next ? list->last->next : list->last;
21489 return 1;
21490 }
21491
21492 /* Note in all location lists that text section has changed. */
21493
21494 static void
21495 var_location_switch_text_section (void)
21496 {
21497 if (decl_loc_table == NULL)
21498 return;
21499
21500 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21501 }
21502
21503 /* Create a new line number table. */
21504
21505 static dw_line_info_table *
21506 new_line_info_table (void)
21507 {
21508 dw_line_info_table *table;
21509
21510 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21511 table->file_num = 1;
21512 table->line_num = 1;
21513 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21514
21515 return table;
21516 }
21517
21518 /* Lookup the "current" table into which we emit line info, so
21519 that we don't have to do it for every source line. */
21520
21521 static void
21522 set_cur_line_info_table (section *sec)
21523 {
21524 dw_line_info_table *table;
21525
21526 if (sec == text_section)
21527 table = text_section_line_info;
21528 else if (sec == cold_text_section)
21529 {
21530 table = cold_text_section_line_info;
21531 if (!table)
21532 {
21533 cold_text_section_line_info = table = new_line_info_table ();
21534 table->end_label = cold_end_label;
21535 }
21536 }
21537 else
21538 {
21539 const char *end_label;
21540
21541 if (flag_reorder_blocks_and_partition)
21542 {
21543 if (in_cold_section_p)
21544 end_label = crtl->subsections.cold_section_end_label;
21545 else
21546 end_label = crtl->subsections.hot_section_end_label;
21547 }
21548 else
21549 {
21550 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21551 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21552 current_function_funcdef_no);
21553 end_label = ggc_strdup (label);
21554 }
21555
21556 table = new_line_info_table ();
21557 table->end_label = end_label;
21558
21559 vec_safe_push (separate_line_info, table);
21560 }
21561
21562 if (DWARF2_ASM_LINE_DEBUG_INFO)
21563 table->is_stmt = (cur_line_info_table
21564 ? cur_line_info_table->is_stmt
21565 : DWARF_LINE_DEFAULT_IS_STMT_START);
21566 cur_line_info_table = table;
21567 }
21568
21569
21570 /* We need to reset the locations at the beginning of each
21571 function. We can't do this in the end_function hook, because the
21572 declarations that use the locations won't have been output when
21573 that hook is called. Also compute have_multiple_function_sections here. */
21574
21575 static void
21576 dwarf2out_begin_function (tree fun)
21577 {
21578 section *sec = function_section (fun);
21579
21580 if (sec != text_section)
21581 have_multiple_function_sections = true;
21582
21583 if (flag_reorder_blocks_and_partition && !cold_text_section)
21584 {
21585 gcc_assert (current_function_decl == fun);
21586 cold_text_section = unlikely_text_section ();
21587 switch_to_section (cold_text_section);
21588 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21589 switch_to_section (sec);
21590 }
21591
21592 dwarf2out_note_section_used ();
21593 call_site_count = 0;
21594 tail_call_site_count = 0;
21595
21596 set_cur_line_info_table (sec);
21597 }
21598
21599 /* Helper function of dwarf2out_end_function, called only after emitting
21600 the very first function into assembly. Check if some .debug_loc range
21601 might end with a .LVL* label that could be equal to .Ltext0.
21602 In that case we must force using absolute addresses in .debug_loc ranges,
21603 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21604 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21605 list terminator.
21606 Set have_multiple_function_sections to true in that case and
21607 terminate htab traversal. */
21608
21609 static int
21610 find_empty_loc_ranges_at_text_label (void **slot, void *)
21611 {
21612 var_loc_list *entry;
21613 struct var_loc_node *node;
21614
21615 entry = (var_loc_list *) *slot;
21616 node = entry->first;
21617 if (node && node->next && node->next->label)
21618 {
21619 unsigned int i;
21620 const char *label = node->next->label;
21621 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21622
21623 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21624 {
21625 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21626 if (strcmp (label, loclabel) == 0)
21627 {
21628 have_multiple_function_sections = true;
21629 return 0;
21630 }
21631 }
21632 }
21633 return 1;
21634 }
21635
21636 /* Hook called after emitting a function into assembly.
21637 This does something only for the very first function emitted. */
21638
21639 static void
21640 dwarf2out_end_function (unsigned int)
21641 {
21642 if (in_first_function_p
21643 && !have_multiple_function_sections
21644 && first_loclabel_num_not_at_text_label
21645 && decl_loc_table)
21646 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21647 NULL);
21648 in_first_function_p = false;
21649 maybe_at_text_label_p = false;
21650 }
21651
21652 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21653
21654 static void
21655 push_dw_line_info_entry (dw_line_info_table *table,
21656 enum dw_line_info_opcode opcode, unsigned int val)
21657 {
21658 dw_line_info_entry e;
21659 e.opcode = opcode;
21660 e.val = val;
21661 vec_safe_push (table->entries, e);
21662 }
21663
21664 /* Output a label to mark the beginning of a source code line entry
21665 and record information relating to this source line, in
21666 'line_info_table' for later output of the .debug_line section. */
21667 /* ??? The discriminator parameter ought to be unsigned. */
21668
21669 static void
21670 dwarf2out_source_line (unsigned int line, const char *filename,
21671 int discriminator, bool is_stmt)
21672 {
21673 unsigned int file_num;
21674 dw_line_info_table *table;
21675
21676 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21677 return;
21678
21679 /* The discriminator column was added in dwarf4. Simplify the below
21680 by simply removing it if we're not supposed to output it. */
21681 if (dwarf_version < 4 && dwarf_strict)
21682 discriminator = 0;
21683
21684 table = cur_line_info_table;
21685 file_num = maybe_emit_file (lookup_filename (filename));
21686
21687 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21688 the debugger has used the second (possibly duplicate) line number
21689 at the beginning of the function to mark the end of the prologue.
21690 We could eliminate any other duplicates within the function. For
21691 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21692 that second line number entry. */
21693 /* Recall that this end-of-prologue indication is *not* the same thing
21694 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21695 to which the hook corresponds, follows the last insn that was
21696 emitted by gen_prologue. What we need is to precede the first insn
21697 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21698 insn that corresponds to something the user wrote. These may be
21699 very different locations once scheduling is enabled. */
21700
21701 if (0 && file_num == table->file_num
21702 && line == table->line_num
21703 && discriminator == table->discrim_num
21704 && is_stmt == table->is_stmt)
21705 return;
21706
21707 switch_to_section (current_function_section ());
21708
21709 /* If requested, emit something human-readable. */
21710 if (flag_debug_asm)
21711 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21712
21713 if (DWARF2_ASM_LINE_DEBUG_INFO)
21714 {
21715 /* Emit the .loc directive understood by GNU as. */
21716 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21717 file_num, line, is_stmt, discriminator */
21718 fputs ("\t.loc ", asm_out_file);
21719 fprint_ul (asm_out_file, file_num);
21720 putc (' ', asm_out_file);
21721 fprint_ul (asm_out_file, line);
21722 putc (' ', asm_out_file);
21723 putc ('0', asm_out_file);
21724
21725 if (is_stmt != table->is_stmt)
21726 {
21727 fputs (" is_stmt ", asm_out_file);
21728 putc (is_stmt ? '1' : '0', asm_out_file);
21729 }
21730 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21731 {
21732 gcc_assert (discriminator > 0);
21733 fputs (" discriminator ", asm_out_file);
21734 fprint_ul (asm_out_file, (unsigned long) discriminator);
21735 }
21736 putc ('\n', asm_out_file);
21737 }
21738 else
21739 {
21740 unsigned int label_num = ++line_info_label_num;
21741
21742 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21743
21744 push_dw_line_info_entry (table, LI_set_address, label_num);
21745 if (file_num != table->file_num)
21746 push_dw_line_info_entry (table, LI_set_file, file_num);
21747 if (discriminator != table->discrim_num)
21748 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21749 if (is_stmt != table->is_stmt)
21750 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21751 push_dw_line_info_entry (table, LI_set_line, line);
21752 }
21753
21754 table->file_num = file_num;
21755 table->line_num = line;
21756 table->discrim_num = discriminator;
21757 table->is_stmt = is_stmt;
21758 table->in_use = true;
21759 }
21760
21761 /* Record the beginning of a new source file. */
21762
21763 static void
21764 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21765 {
21766 if (flag_eliminate_dwarf2_dups)
21767 {
21768 /* Record the beginning of the file for break_out_includes. */
21769 dw_die_ref bincl_die;
21770
21771 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21772 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21773 }
21774
21775 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21776 {
21777 macinfo_entry e;
21778 e.code = DW_MACINFO_start_file;
21779 e.lineno = lineno;
21780 e.info = ggc_strdup (filename);
21781 vec_safe_push (macinfo_table, e);
21782 }
21783 }
21784
21785 /* Record the end of a source file. */
21786
21787 static void
21788 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21789 {
21790 if (flag_eliminate_dwarf2_dups)
21791 /* Record the end of the file for break_out_includes. */
21792 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21793
21794 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21795 {
21796 macinfo_entry e;
21797 e.code = DW_MACINFO_end_file;
21798 e.lineno = lineno;
21799 e.info = NULL;
21800 vec_safe_push (macinfo_table, e);
21801 }
21802 }
21803
21804 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21805 the tail part of the directive line, i.e. the part which is past the
21806 initial whitespace, #, whitespace, directive-name, whitespace part. */
21807
21808 static void
21809 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21810 const char *buffer ATTRIBUTE_UNUSED)
21811 {
21812 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21813 {
21814 macinfo_entry e;
21815 /* Insert a dummy first entry to be able to optimize the whole
21816 predefined macro block using DW_MACRO_GNU_transparent_include. */
21817 if (macinfo_table->is_empty () && lineno <= 1)
21818 {
21819 e.code = 0;
21820 e.lineno = 0;
21821 e.info = NULL;
21822 vec_safe_push (macinfo_table, e);
21823 }
21824 e.code = DW_MACINFO_define;
21825 e.lineno = lineno;
21826 e.info = ggc_strdup (buffer);
21827 vec_safe_push (macinfo_table, e);
21828 }
21829 }
21830
21831 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21832 the tail part of the directive line, i.e. the part which is past the
21833 initial whitespace, #, whitespace, directive-name, whitespace part. */
21834
21835 static void
21836 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21837 const char *buffer ATTRIBUTE_UNUSED)
21838 {
21839 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21840 {
21841 macinfo_entry e;
21842 /* Insert a dummy first entry to be able to optimize the whole
21843 predefined macro block using DW_MACRO_GNU_transparent_include. */
21844 if (macinfo_table->is_empty () && lineno <= 1)
21845 {
21846 e.code = 0;
21847 e.lineno = 0;
21848 e.info = NULL;
21849 vec_safe_push (macinfo_table, e);
21850 }
21851 e.code = DW_MACINFO_undef;
21852 e.lineno = lineno;
21853 e.info = ggc_strdup (buffer);
21854 vec_safe_push (macinfo_table, e);
21855 }
21856 }
21857
21858 /* Helpers to manipulate hash table of CUs. */
21859
21860 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21861 {
21862 typedef macinfo_entry value_type;
21863 typedef macinfo_entry compare_type;
21864 static inline hashval_t hash (const value_type *);
21865 static inline bool equal (const value_type *, const compare_type *);
21866 };
21867
21868 inline hashval_t
21869 macinfo_entry_hasher::hash (const value_type *entry)
21870 {
21871 return htab_hash_string (entry->info);
21872 }
21873
21874 inline bool
21875 macinfo_entry_hasher::equal (const value_type *entry1,
21876 const compare_type *entry2)
21877 {
21878 return !strcmp (entry1->info, entry2->info);
21879 }
21880
21881 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
21882
21883 /* Output a single .debug_macinfo entry. */
21884
21885 static void
21886 output_macinfo_op (macinfo_entry *ref)
21887 {
21888 int file_num;
21889 size_t len;
21890 struct indirect_string_node *node;
21891 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21892 struct dwarf_file_data *fd;
21893
21894 switch (ref->code)
21895 {
21896 case DW_MACINFO_start_file:
21897 fd = lookup_filename (ref->info);
21898 file_num = maybe_emit_file (fd);
21899 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21900 dw2_asm_output_data_uleb128 (ref->lineno,
21901 "Included from line number %lu",
21902 (unsigned long) ref->lineno);
21903 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21904 break;
21905 case DW_MACINFO_end_file:
21906 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21907 break;
21908 case DW_MACINFO_define:
21909 case DW_MACINFO_undef:
21910 len = strlen (ref->info) + 1;
21911 if (!dwarf_strict
21912 && len > DWARF_OFFSET_SIZE
21913 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21914 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21915 {
21916 ref->code = ref->code == DW_MACINFO_define
21917 ? DW_MACRO_GNU_define_indirect
21918 : DW_MACRO_GNU_undef_indirect;
21919 output_macinfo_op (ref);
21920 return;
21921 }
21922 dw2_asm_output_data (1, ref->code,
21923 ref->code == DW_MACINFO_define
21924 ? "Define macro" : "Undefine macro");
21925 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21926 (unsigned long) ref->lineno);
21927 dw2_asm_output_nstring (ref->info, -1, "The macro");
21928 break;
21929 case DW_MACRO_GNU_define_indirect:
21930 case DW_MACRO_GNU_undef_indirect:
21931 node = find_AT_string (ref->info);
21932 gcc_assert (node
21933 && ((node->form == DW_FORM_strp)
21934 || (node->form == DW_FORM_GNU_str_index)));
21935 dw2_asm_output_data (1, ref->code,
21936 ref->code == DW_MACRO_GNU_define_indirect
21937 ? "Define macro indirect"
21938 : "Undefine macro indirect");
21939 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21940 (unsigned long) ref->lineno);
21941 if (node->form == DW_FORM_strp)
21942 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21943 debug_str_section, "The macro: \"%s\"",
21944 ref->info);
21945 else
21946 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21947 ref->info);
21948 break;
21949 case DW_MACRO_GNU_transparent_include:
21950 dw2_asm_output_data (1, ref->code, "Transparent include");
21951 ASM_GENERATE_INTERNAL_LABEL (label,
21952 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21953 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21954 break;
21955 default:
21956 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21957 ASM_COMMENT_START, (unsigned long) ref->code);
21958 break;
21959 }
21960 }
21961
21962 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21963 other compilation unit .debug_macinfo sections. IDX is the first
21964 index of a define/undef, return the number of ops that should be
21965 emitted in a comdat .debug_macinfo section and emit
21966 a DW_MACRO_GNU_transparent_include entry referencing it.
21967 If the define/undef entry should be emitted normally, return 0. */
21968
21969 static unsigned
21970 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21971 macinfo_hash_type **macinfo_htab)
21972 {
21973 macinfo_entry *first, *second, *cur, *inc;
21974 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21975 unsigned char checksum[16];
21976 struct md5_ctx ctx;
21977 char *grp_name, *tail;
21978 const char *base;
21979 unsigned int i, count, encoded_filename_len, linebuf_len;
21980 macinfo_entry **slot;
21981
21982 first = &(*macinfo_table)[idx];
21983 second = &(*macinfo_table)[idx + 1];
21984
21985 /* Optimize only if there are at least two consecutive define/undef ops,
21986 and either all of them are before first DW_MACINFO_start_file
21987 with lineno {0,1} (i.e. predefined macro block), or all of them are
21988 in some included header file. */
21989 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21990 return 0;
21991 if (vec_safe_is_empty (files))
21992 {
21993 if (first->lineno > 1 || second->lineno > 1)
21994 return 0;
21995 }
21996 else if (first->lineno == 0)
21997 return 0;
21998
21999 /* Find the last define/undef entry that can be grouped together
22000 with first and at the same time compute md5 checksum of their
22001 codes, linenumbers and strings. */
22002 md5_init_ctx (&ctx);
22003 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22004 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22005 break;
22006 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22007 break;
22008 else
22009 {
22010 unsigned char code = cur->code;
22011 md5_process_bytes (&code, 1, &ctx);
22012 checksum_uleb128 (cur->lineno, &ctx);
22013 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22014 }
22015 md5_finish_ctx (&ctx, checksum);
22016 count = i - idx;
22017
22018 /* From the containing include filename (if any) pick up just
22019 usable characters from its basename. */
22020 if (vec_safe_is_empty (files))
22021 base = "";
22022 else
22023 base = lbasename (files->last ().info);
22024 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22025 if (ISIDNUM (base[i]) || base[i] == '.')
22026 encoded_filename_len++;
22027 /* Count . at the end. */
22028 if (encoded_filename_len)
22029 encoded_filename_len++;
22030
22031 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22032 linebuf_len = strlen (linebuf);
22033
22034 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22035 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22036 + 16 * 2 + 1);
22037 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22038 tail = grp_name + 4;
22039 if (encoded_filename_len)
22040 {
22041 for (i = 0; base[i]; i++)
22042 if (ISIDNUM (base[i]) || base[i] == '.')
22043 *tail++ = base[i];
22044 *tail++ = '.';
22045 }
22046 memcpy (tail, linebuf, linebuf_len);
22047 tail += linebuf_len;
22048 *tail++ = '.';
22049 for (i = 0; i < 16; i++)
22050 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22051
22052 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22053 in the empty vector entry before the first define/undef. */
22054 inc = &(*macinfo_table)[idx - 1];
22055 inc->code = DW_MACRO_GNU_transparent_include;
22056 inc->lineno = 0;
22057 inc->info = ggc_strdup (grp_name);
22058 if (!*macinfo_htab)
22059 *macinfo_htab = new macinfo_hash_type (10);
22060 /* Avoid emitting duplicates. */
22061 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22062 if (*slot != NULL)
22063 {
22064 inc->code = 0;
22065 inc->info = NULL;
22066 /* If such an entry has been used before, just emit
22067 a DW_MACRO_GNU_transparent_include op. */
22068 inc = *slot;
22069 output_macinfo_op (inc);
22070 /* And clear all macinfo_entry in the range to avoid emitting them
22071 in the second pass. */
22072 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22073 {
22074 cur->code = 0;
22075 cur->info = NULL;
22076 }
22077 }
22078 else
22079 {
22080 *slot = inc;
22081 inc->lineno = (*macinfo_htab)->elements ();
22082 output_macinfo_op (inc);
22083 }
22084 return count;
22085 }
22086
22087 /* Save any strings needed by the macinfo table in the debug str
22088 table. All strings must be collected into the table by the time
22089 index_string is called. */
22090
22091 static void
22092 save_macinfo_strings (void)
22093 {
22094 unsigned len;
22095 unsigned i;
22096 macinfo_entry *ref;
22097
22098 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22099 {
22100 switch (ref->code)
22101 {
22102 /* Match the logic in output_macinfo_op to decide on
22103 indirect strings. */
22104 case DW_MACINFO_define:
22105 case DW_MACINFO_undef:
22106 len = strlen (ref->info) + 1;
22107 if (!dwarf_strict
22108 && len > DWARF_OFFSET_SIZE
22109 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22110 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22111 set_indirect_string (find_AT_string (ref->info));
22112 break;
22113 case DW_MACRO_GNU_define_indirect:
22114 case DW_MACRO_GNU_undef_indirect:
22115 set_indirect_string (find_AT_string (ref->info));
22116 break;
22117 default:
22118 break;
22119 }
22120 }
22121 }
22122
22123 /* Output macinfo section(s). */
22124
22125 static void
22126 output_macinfo (void)
22127 {
22128 unsigned i;
22129 unsigned long length = vec_safe_length (macinfo_table);
22130 macinfo_entry *ref;
22131 vec<macinfo_entry, va_gc> *files = NULL;
22132 macinfo_hash_type *macinfo_htab = NULL;
22133
22134 if (! length)
22135 return;
22136
22137 /* output_macinfo* uses these interchangeably. */
22138 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22139 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22140 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22141 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22142
22143 /* For .debug_macro emit the section header. */
22144 if (!dwarf_strict)
22145 {
22146 dw2_asm_output_data (2, 4, "DWARF macro version number");
22147 if (DWARF_OFFSET_SIZE == 8)
22148 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22149 else
22150 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22151 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22152 (!dwarf_split_debug_info ? debug_line_section_label
22153 : debug_skeleton_line_section_label),
22154 debug_line_section, NULL);
22155 }
22156
22157 /* In the first loop, it emits the primary .debug_macinfo section
22158 and after each emitted op the macinfo_entry is cleared.
22159 If a longer range of define/undef ops can be optimized using
22160 DW_MACRO_GNU_transparent_include, the
22161 DW_MACRO_GNU_transparent_include op is emitted and kept in
22162 the vector before the first define/undef in the range and the
22163 whole range of define/undef ops is not emitted and kept. */
22164 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22165 {
22166 switch (ref->code)
22167 {
22168 case DW_MACINFO_start_file:
22169 vec_safe_push (files, *ref);
22170 break;
22171 case DW_MACINFO_end_file:
22172 if (!vec_safe_is_empty (files))
22173 files->pop ();
22174 break;
22175 case DW_MACINFO_define:
22176 case DW_MACINFO_undef:
22177 if (!dwarf_strict
22178 && HAVE_COMDAT_GROUP
22179 && vec_safe_length (files) != 1
22180 && i > 0
22181 && i + 1 < length
22182 && (*macinfo_table)[i - 1].code == 0)
22183 {
22184 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22185 if (count)
22186 {
22187 i += count - 1;
22188 continue;
22189 }
22190 }
22191 break;
22192 case 0:
22193 /* A dummy entry may be inserted at the beginning to be able
22194 to optimize the whole block of predefined macros. */
22195 if (i == 0)
22196 continue;
22197 default:
22198 break;
22199 }
22200 output_macinfo_op (ref);
22201 ref->info = NULL;
22202 ref->code = 0;
22203 }
22204
22205 if (!macinfo_htab)
22206 return;
22207
22208 delete macinfo_htab;
22209 macinfo_htab = NULL;
22210
22211 /* If any DW_MACRO_GNU_transparent_include were used, on those
22212 DW_MACRO_GNU_transparent_include entries terminate the
22213 current chain and switch to a new comdat .debug_macinfo
22214 section and emit the define/undef entries within it. */
22215 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22216 switch (ref->code)
22217 {
22218 case 0:
22219 continue;
22220 case DW_MACRO_GNU_transparent_include:
22221 {
22222 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22223 tree comdat_key = get_identifier (ref->info);
22224 /* Terminate the previous .debug_macinfo section. */
22225 dw2_asm_output_data (1, 0, "End compilation unit");
22226 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22227 SECTION_DEBUG
22228 | SECTION_LINKONCE,
22229 comdat_key);
22230 ASM_GENERATE_INTERNAL_LABEL (label,
22231 DEBUG_MACRO_SECTION_LABEL,
22232 ref->lineno);
22233 ASM_OUTPUT_LABEL (asm_out_file, label);
22234 ref->code = 0;
22235 ref->info = NULL;
22236 dw2_asm_output_data (2, 4, "DWARF macro version number");
22237 if (DWARF_OFFSET_SIZE == 8)
22238 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22239 else
22240 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22241 }
22242 break;
22243 case DW_MACINFO_define:
22244 case DW_MACINFO_undef:
22245 output_macinfo_op (ref);
22246 ref->code = 0;
22247 ref->info = NULL;
22248 break;
22249 default:
22250 gcc_unreachable ();
22251 }
22252 }
22253
22254 /* Set up for Dwarf output at the start of compilation. */
22255
22256 static void
22257 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22258 {
22259 /* Allocate the file_table. */
22260 file_table = htab_create_ggc (50, file_table_hash,
22261 file_table_eq, NULL);
22262
22263 /* Allocate the decl_die_table. */
22264 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22265 decl_die_table_eq, NULL);
22266
22267 /* Allocate the decl_loc_table. */
22268 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22269 decl_loc_table_eq, NULL);
22270
22271 /* Allocate the cached_dw_loc_list_table. */
22272 cached_dw_loc_list_table
22273 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22274 cached_dw_loc_list_table_eq, NULL);
22275
22276 /* Allocate the initial hunk of the decl_scope_table. */
22277 vec_alloc (decl_scope_table, 256);
22278
22279 /* Allocate the initial hunk of the abbrev_die_table. */
22280 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22281 (ABBREV_DIE_TABLE_INCREMENT);
22282 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22283 /* Zero-th entry is allocated, but unused. */
22284 abbrev_die_table_in_use = 1;
22285
22286 /* Allocate the pubtypes and pubnames vectors. */
22287 vec_alloc (pubname_table, 32);
22288 vec_alloc (pubtype_table, 32);
22289
22290 vec_alloc (incomplete_types, 64);
22291
22292 vec_alloc (used_rtx_array, 32);
22293
22294 if (!dwarf_split_debug_info)
22295 {
22296 debug_info_section = get_section (DEBUG_INFO_SECTION,
22297 SECTION_DEBUG, NULL);
22298 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22299 SECTION_DEBUG, NULL);
22300 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22301 SECTION_DEBUG, NULL);
22302 }
22303 else
22304 {
22305 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22306 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22307 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22308 SECTION_DEBUG | SECTION_EXCLUDE,
22309 NULL);
22310 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22311 SECTION_DEBUG, NULL);
22312 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22313 SECTION_DEBUG, NULL);
22314 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22315 SECTION_DEBUG, NULL);
22316 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22317 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22318
22319 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22320 the main .o, but the skeleton_line goes into the split off dwo. */
22321 debug_skeleton_line_section
22322 = get_section (DEBUG_DWO_LINE_SECTION,
22323 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22324 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22325 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22326 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22327 SECTION_DEBUG | SECTION_EXCLUDE,
22328 NULL);
22329 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22330 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22331 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22332 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22333 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22334 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22335 }
22336 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22337 SECTION_DEBUG, NULL);
22338 debug_macinfo_section = get_section (dwarf_strict
22339 ? DEBUG_MACINFO_SECTION
22340 : DEBUG_MACRO_SECTION,
22341 DEBUG_MACRO_SECTION_FLAGS, NULL);
22342 debug_line_section = get_section (DEBUG_LINE_SECTION,
22343 SECTION_DEBUG, NULL);
22344 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22345 SECTION_DEBUG, NULL);
22346 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22347 SECTION_DEBUG, NULL);
22348 debug_str_section = get_section (DEBUG_STR_SECTION,
22349 DEBUG_STR_SECTION_FLAGS, NULL);
22350 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22351 SECTION_DEBUG, NULL);
22352 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22353 SECTION_DEBUG, NULL);
22354
22355 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22356 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22357 DEBUG_ABBREV_SECTION_LABEL, 0);
22358 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22359 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22360 COLD_TEXT_SECTION_LABEL, 0);
22361 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22362
22363 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22364 DEBUG_INFO_SECTION_LABEL, 0);
22365 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22366 DEBUG_LINE_SECTION_LABEL, 0);
22367 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22368 DEBUG_RANGES_SECTION_LABEL, 0);
22369 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22370 DEBUG_ADDR_SECTION_LABEL, 0);
22371 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22372 dwarf_strict
22373 ? DEBUG_MACINFO_SECTION_LABEL
22374 : DEBUG_MACRO_SECTION_LABEL, 0);
22375 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22376
22377 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22378 vec_alloc (macinfo_table, 64);
22379
22380 switch_to_section (text_section);
22381 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22382
22383 /* Make sure the line number table for .text always exists. */
22384 text_section_line_info = new_line_info_table ();
22385 text_section_line_info->end_label = text_end_label;
22386 }
22387
22388 /* Called before compile () starts outputtting functions, variables
22389 and toplevel asms into assembly. */
22390
22391 static void
22392 dwarf2out_assembly_start (void)
22393 {
22394 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22395 && dwarf2out_do_cfi_asm ()
22396 && (!(flag_unwind_tables || flag_exceptions)
22397 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22398 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22399 }
22400
22401 /* A helper function for dwarf2out_finish called through
22402 htab_traverse. Assign a string its index. All strings must be
22403 collected into the table by the time index_string is called,
22404 because the indexing code relies on htab_traverse to traverse nodes
22405 in the same order for each run. */
22406
22407 static int
22408 index_string (void **h, void *v)
22409 {
22410 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22411 unsigned int *index = (unsigned int *) v;
22412
22413 find_string_form (node);
22414 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22415 {
22416 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22417 node->index = *index;
22418 *index += 1;
22419 }
22420 return 1;
22421 }
22422
22423 /* A helper function for output_indirect_strings called through
22424 htab_traverse. Output the offset to a string and update the
22425 current offset. */
22426
22427 static int
22428 output_index_string_offset (void **h, void *v)
22429 {
22430 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22431 unsigned int *offset = (unsigned int *) v;
22432
22433 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22434 {
22435 /* Assert that this node has been assigned an index. */
22436 gcc_assert (node->index != NO_INDEX_ASSIGNED
22437 && node->index != NOT_INDEXED);
22438 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22439 "indexed string 0x%x: %s", node->index, node->str);
22440 *offset += strlen (node->str) + 1;
22441 }
22442 return 1;
22443 }
22444
22445 /* A helper function for dwarf2out_finish called through
22446 htab_traverse. Output the indexed string. */
22447
22448 static int
22449 output_index_string (void **h, void *v)
22450 {
22451 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22452 unsigned int *cur_idx = (unsigned int *) v;
22453
22454 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22455 {
22456 /* Assert that the strings are output in the same order as their
22457 indexes were assigned. */
22458 gcc_assert (*cur_idx == node->index);
22459 assemble_string (node->str, strlen (node->str) + 1);
22460 *cur_idx += 1;
22461 }
22462 return 1;
22463 }
22464
22465 /* A helper function for dwarf2out_finish called through
22466 htab_traverse. Emit one queued .debug_str string. */
22467
22468 static int
22469 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22470 {
22471 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22472
22473 node->form = find_string_form (node);
22474 if (node->form == DW_FORM_strp && node->refcount > 0)
22475 {
22476 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22477 assemble_string (node->str, strlen (node->str) + 1);
22478 }
22479
22480 return 1;
22481 }
22482
22483 /* Output the indexed string table. */
22484
22485 static void
22486 output_indirect_strings (void)
22487 {
22488 switch_to_section (debug_str_section);
22489 if (!dwarf_split_debug_info)
22490 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22491 else
22492 {
22493 unsigned int offset = 0;
22494 unsigned int cur_idx = 0;
22495
22496 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22497
22498 switch_to_section (debug_str_offsets_section);
22499 htab_traverse_noresize (debug_str_hash,
22500 output_index_string_offset,
22501 &offset);
22502 switch_to_section (debug_str_dwo_section);
22503 htab_traverse_noresize (debug_str_hash,
22504 output_index_string,
22505 &cur_idx);
22506 }
22507 }
22508
22509 /* Callback for htab_traverse to assign an index to an entry in the
22510 table, and to write that entry to the .debug_addr section. */
22511
22512 static int
22513 output_addr_table_entry (void **slot, void *data)
22514 {
22515 addr_table_entry *entry = (addr_table_entry *) *slot;
22516 unsigned int *cur_index = (unsigned int *)data;
22517
22518 if (entry->refcount == 0)
22519 {
22520 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22521 || entry->index == NOT_INDEXED);
22522 return 1;
22523 }
22524
22525 gcc_assert (entry->index == *cur_index);
22526 (*cur_index)++;
22527
22528 switch (entry->kind)
22529 {
22530 case ate_kind_rtx:
22531 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22532 "0x%x", entry->index);
22533 break;
22534 case ate_kind_rtx_dtprel:
22535 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22536 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22537 DWARF2_ADDR_SIZE,
22538 entry->addr.rtl);
22539 fputc ('\n', asm_out_file);
22540 break;
22541 case ate_kind_label:
22542 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22543 "0x%x", entry->index);
22544 break;
22545 default:
22546 gcc_unreachable ();
22547 }
22548 return 1;
22549 }
22550
22551 /* Produce the .debug_addr section. */
22552
22553 static void
22554 output_addr_table (void)
22555 {
22556 unsigned int index = 0;
22557 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22558 return;
22559
22560 switch_to_section (debug_addr_section);
22561 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22562 }
22563
22564 #if ENABLE_ASSERT_CHECKING
22565 /* Verify that all marks are clear. */
22566
22567 static void
22568 verify_marks_clear (dw_die_ref die)
22569 {
22570 dw_die_ref c;
22571
22572 gcc_assert (! die->die_mark);
22573 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22574 }
22575 #endif /* ENABLE_ASSERT_CHECKING */
22576
22577 /* Clear the marks for a die and its children.
22578 Be cool if the mark isn't set. */
22579
22580 static void
22581 prune_unmark_dies (dw_die_ref die)
22582 {
22583 dw_die_ref c;
22584
22585 if (die->die_mark)
22586 die->die_mark = 0;
22587 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22588 }
22589
22590 /* Given DIE that we're marking as used, find any other dies
22591 it references as attributes and mark them as used. */
22592
22593 static void
22594 prune_unused_types_walk_attribs (dw_die_ref die)
22595 {
22596 dw_attr_ref a;
22597 unsigned ix;
22598
22599 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22600 {
22601 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22602 {
22603 /* A reference to another DIE.
22604 Make sure that it will get emitted.
22605 If it was broken out into a comdat group, don't follow it. */
22606 if (! AT_ref (a)->comdat_type_p
22607 || a->dw_attr == DW_AT_specification)
22608 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22609 }
22610 /* Set the string's refcount to 0 so that prune_unused_types_mark
22611 accounts properly for it. */
22612 if (AT_class (a) == dw_val_class_str)
22613 a->dw_attr_val.v.val_str->refcount = 0;
22614 }
22615 }
22616
22617 /* Mark the generic parameters and arguments children DIEs of DIE. */
22618
22619 static void
22620 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22621 {
22622 dw_die_ref c;
22623
22624 if (die == NULL || die->die_child == NULL)
22625 return;
22626 c = die->die_child;
22627 do
22628 {
22629 if (is_template_parameter (c))
22630 prune_unused_types_mark (c, 1);
22631 c = c->die_sib;
22632 } while (c && c != die->die_child);
22633 }
22634
22635 /* Mark DIE as being used. If DOKIDS is true, then walk down
22636 to DIE's children. */
22637
22638 static void
22639 prune_unused_types_mark (dw_die_ref die, int dokids)
22640 {
22641 dw_die_ref c;
22642
22643 if (die->die_mark == 0)
22644 {
22645 /* We haven't done this node yet. Mark it as used. */
22646 die->die_mark = 1;
22647 /* If this is the DIE of a generic type instantiation,
22648 mark the children DIEs that describe its generic parms and
22649 args. */
22650 prune_unused_types_mark_generic_parms_dies (die);
22651
22652 /* We also have to mark its parents as used.
22653 (But we don't want to mark our parent's kids due to this,
22654 unless it is a class.) */
22655 if (die->die_parent)
22656 prune_unused_types_mark (die->die_parent,
22657 class_scope_p (die->die_parent));
22658
22659 /* Mark any referenced nodes. */
22660 prune_unused_types_walk_attribs (die);
22661
22662 /* If this node is a specification,
22663 also mark the definition, if it exists. */
22664 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22665 prune_unused_types_mark (die->die_definition, 1);
22666 }
22667
22668 if (dokids && die->die_mark != 2)
22669 {
22670 /* We need to walk the children, but haven't done so yet.
22671 Remember that we've walked the kids. */
22672 die->die_mark = 2;
22673
22674 /* If this is an array type, we need to make sure our
22675 kids get marked, even if they're types. If we're
22676 breaking out types into comdat sections, do this
22677 for all type definitions. */
22678 if (die->die_tag == DW_TAG_array_type
22679 || (use_debug_types
22680 && is_type_die (die) && ! is_declaration_die (die)))
22681 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22682 else
22683 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22684 }
22685 }
22686
22687 /* For local classes, look if any static member functions were emitted
22688 and if so, mark them. */
22689
22690 static void
22691 prune_unused_types_walk_local_classes (dw_die_ref die)
22692 {
22693 dw_die_ref c;
22694
22695 if (die->die_mark == 2)
22696 return;
22697
22698 switch (die->die_tag)
22699 {
22700 case DW_TAG_structure_type:
22701 case DW_TAG_union_type:
22702 case DW_TAG_class_type:
22703 break;
22704
22705 case DW_TAG_subprogram:
22706 if (!get_AT_flag (die, DW_AT_declaration)
22707 || die->die_definition != NULL)
22708 prune_unused_types_mark (die, 1);
22709 return;
22710
22711 default:
22712 return;
22713 }
22714
22715 /* Mark children. */
22716 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22717 }
22718
22719 /* Walk the tree DIE and mark types that we actually use. */
22720
22721 static void
22722 prune_unused_types_walk (dw_die_ref die)
22723 {
22724 dw_die_ref c;
22725
22726 /* Don't do anything if this node is already marked and
22727 children have been marked as well. */
22728 if (die->die_mark == 2)
22729 return;
22730
22731 switch (die->die_tag)
22732 {
22733 case DW_TAG_structure_type:
22734 case DW_TAG_union_type:
22735 case DW_TAG_class_type:
22736 if (die->die_perennial_p)
22737 break;
22738
22739 for (c = die->die_parent; c; c = c->die_parent)
22740 if (c->die_tag == DW_TAG_subprogram)
22741 break;
22742
22743 /* Finding used static member functions inside of classes
22744 is needed just for local classes, because for other classes
22745 static member function DIEs with DW_AT_specification
22746 are emitted outside of the DW_TAG_*_type. If we ever change
22747 it, we'd need to call this even for non-local classes. */
22748 if (c)
22749 prune_unused_types_walk_local_classes (die);
22750
22751 /* It's a type node --- don't mark it. */
22752 return;
22753
22754 case DW_TAG_const_type:
22755 case DW_TAG_packed_type:
22756 case DW_TAG_pointer_type:
22757 case DW_TAG_reference_type:
22758 case DW_TAG_rvalue_reference_type:
22759 case DW_TAG_volatile_type:
22760 case DW_TAG_typedef:
22761 case DW_TAG_array_type:
22762 case DW_TAG_interface_type:
22763 case DW_TAG_friend:
22764 case DW_TAG_variant_part:
22765 case DW_TAG_enumeration_type:
22766 case DW_TAG_subroutine_type:
22767 case DW_TAG_string_type:
22768 case DW_TAG_set_type:
22769 case DW_TAG_subrange_type:
22770 case DW_TAG_ptr_to_member_type:
22771 case DW_TAG_file_type:
22772 if (die->die_perennial_p)
22773 break;
22774
22775 /* It's a type node --- don't mark it. */
22776 return;
22777
22778 default:
22779 /* Mark everything else. */
22780 break;
22781 }
22782
22783 if (die->die_mark == 0)
22784 {
22785 die->die_mark = 1;
22786
22787 /* Now, mark any dies referenced from here. */
22788 prune_unused_types_walk_attribs (die);
22789 }
22790
22791 die->die_mark = 2;
22792
22793 /* Mark children. */
22794 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22795 }
22796
22797 /* Increment the string counts on strings referred to from DIE's
22798 attributes. */
22799
22800 static void
22801 prune_unused_types_update_strings (dw_die_ref die)
22802 {
22803 dw_attr_ref a;
22804 unsigned ix;
22805
22806 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22807 if (AT_class (a) == dw_val_class_str)
22808 {
22809 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22810 s->refcount++;
22811 /* Avoid unnecessarily putting strings that are used less than
22812 twice in the hash table. */
22813 if (s->refcount
22814 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22815 {
22816 void ** slot;
22817 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22818 htab_hash_string (s->str),
22819 INSERT);
22820 gcc_assert (*slot == NULL);
22821 *slot = s;
22822 }
22823 }
22824 }
22825
22826 /* Remove from the tree DIE any dies that aren't marked. */
22827
22828 static void
22829 prune_unused_types_prune (dw_die_ref die)
22830 {
22831 dw_die_ref c;
22832
22833 gcc_assert (die->die_mark);
22834 prune_unused_types_update_strings (die);
22835
22836 if (! die->die_child)
22837 return;
22838
22839 c = die->die_child;
22840 do {
22841 dw_die_ref prev = c;
22842 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22843 if (c == die->die_child)
22844 {
22845 /* No marked children between 'prev' and the end of the list. */
22846 if (prev == c)
22847 /* No marked children at all. */
22848 die->die_child = NULL;
22849 else
22850 {
22851 prev->die_sib = c->die_sib;
22852 die->die_child = prev;
22853 }
22854 return;
22855 }
22856
22857 if (c != prev->die_sib)
22858 prev->die_sib = c;
22859 prune_unused_types_prune (c);
22860 } while (c != die->die_child);
22861 }
22862
22863 /* Remove dies representing declarations that we never use. */
22864
22865 static void
22866 prune_unused_types (void)
22867 {
22868 unsigned int i;
22869 limbo_die_node *node;
22870 comdat_type_node *ctnode;
22871 pubname_ref pub;
22872 dw_die_ref base_type;
22873
22874 #if ENABLE_ASSERT_CHECKING
22875 /* All the marks should already be clear. */
22876 verify_marks_clear (comp_unit_die ());
22877 for (node = limbo_die_list; node; node = node->next)
22878 verify_marks_clear (node->die);
22879 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22880 verify_marks_clear (ctnode->root_die);
22881 #endif /* ENABLE_ASSERT_CHECKING */
22882
22883 /* Mark types that are used in global variables. */
22884 premark_types_used_by_global_vars ();
22885
22886 /* Set the mark on nodes that are actually used. */
22887 prune_unused_types_walk (comp_unit_die ());
22888 for (node = limbo_die_list; node; node = node->next)
22889 prune_unused_types_walk (node->die);
22890 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22891 {
22892 prune_unused_types_walk (ctnode->root_die);
22893 prune_unused_types_mark (ctnode->type_die, 1);
22894 }
22895
22896 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22897 are unusual in that they are pubnames that are the children of pubtypes.
22898 They should only be marked via their parent DW_TAG_enumeration_type die,
22899 not as roots in themselves. */
22900 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22901 if (pub->die->die_tag != DW_TAG_enumerator)
22902 prune_unused_types_mark (pub->die, 1);
22903 for (i = 0; base_types.iterate (i, &base_type); i++)
22904 prune_unused_types_mark (base_type, 1);
22905
22906 if (debug_str_hash)
22907 htab_empty (debug_str_hash);
22908 if (skeleton_debug_str_hash)
22909 htab_empty (skeleton_debug_str_hash);
22910 prune_unused_types_prune (comp_unit_die ());
22911 for (node = limbo_die_list; node; node = node->next)
22912 prune_unused_types_prune (node->die);
22913 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22914 prune_unused_types_prune (ctnode->root_die);
22915
22916 /* Leave the marks clear. */
22917 prune_unmark_dies (comp_unit_die ());
22918 for (node = limbo_die_list; node; node = node->next)
22919 prune_unmark_dies (node->die);
22920 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22921 prune_unmark_dies (ctnode->root_die);
22922 }
22923
22924 /* Set the parameter to true if there are any relative pathnames in
22925 the file table. */
22926 static int
22927 file_table_relative_p (void ** slot, void *param)
22928 {
22929 bool *p = (bool *) param;
22930 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22931 if (!IS_ABSOLUTE_PATH (d->filename))
22932 {
22933 *p = true;
22934 return 0;
22935 }
22936 return 1;
22937 }
22938
22939 /* Helpers to manipulate hash table of comdat type units. */
22940
22941 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22942 {
22943 typedef comdat_type_node value_type;
22944 typedef comdat_type_node compare_type;
22945 static inline hashval_t hash (const value_type *);
22946 static inline bool equal (const value_type *, const compare_type *);
22947 };
22948
22949 inline hashval_t
22950 comdat_type_hasher::hash (const value_type *type_node)
22951 {
22952 hashval_t h;
22953 memcpy (&h, type_node->signature, sizeof (h));
22954 return h;
22955 }
22956
22957 inline bool
22958 comdat_type_hasher::equal (const value_type *type_node_1,
22959 const compare_type *type_node_2)
22960 {
22961 return (! memcmp (type_node_1->signature, type_node_2->signature,
22962 DWARF_TYPE_SIGNATURE_SIZE));
22963 }
22964
22965 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22966 to the location it would have been added, should we know its
22967 DECL_ASSEMBLER_NAME when we added other attributes. This will
22968 probably improve compactness of debug info, removing equivalent
22969 abbrevs, and hide any differences caused by deferring the
22970 computation of the assembler name, triggered by e.g. PCH. */
22971
22972 static inline void
22973 move_linkage_attr (dw_die_ref die)
22974 {
22975 unsigned ix = vec_safe_length (die->die_attr);
22976 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22977
22978 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22979 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22980
22981 while (--ix > 0)
22982 {
22983 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22984
22985 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22986 break;
22987 }
22988
22989 if (ix != vec_safe_length (die->die_attr) - 1)
22990 {
22991 die->die_attr->pop ();
22992 die->die_attr->quick_insert (ix, linkage);
22993 }
22994 }
22995
22996 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22997 referenced from typed stack ops and count how often they are used. */
22998
22999 static void
23000 mark_base_types (dw_loc_descr_ref loc)
23001 {
23002 dw_die_ref base_type = NULL;
23003
23004 for (; loc; loc = loc->dw_loc_next)
23005 {
23006 switch (loc->dw_loc_opc)
23007 {
23008 case DW_OP_GNU_regval_type:
23009 case DW_OP_GNU_deref_type:
23010 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23011 break;
23012 case DW_OP_GNU_convert:
23013 case DW_OP_GNU_reinterpret:
23014 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23015 continue;
23016 /* FALLTHRU */
23017 case DW_OP_GNU_const_type:
23018 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23019 break;
23020 case DW_OP_GNU_entry_value:
23021 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23022 continue;
23023 default:
23024 continue;
23025 }
23026 gcc_assert (base_type->die_parent == comp_unit_die ());
23027 if (base_type->die_mark)
23028 base_type->die_mark++;
23029 else
23030 {
23031 base_types.safe_push (base_type);
23032 base_type->die_mark = 1;
23033 }
23034 }
23035 }
23036
23037 /* Comparison function for sorting marked base types. */
23038
23039 static int
23040 base_type_cmp (const void *x, const void *y)
23041 {
23042 dw_die_ref dx = *(const dw_die_ref *) x;
23043 dw_die_ref dy = *(const dw_die_ref *) y;
23044 unsigned int byte_size1, byte_size2;
23045 unsigned int encoding1, encoding2;
23046 if (dx->die_mark > dy->die_mark)
23047 return -1;
23048 if (dx->die_mark < dy->die_mark)
23049 return 1;
23050 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23051 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23052 if (byte_size1 < byte_size2)
23053 return 1;
23054 if (byte_size1 > byte_size2)
23055 return -1;
23056 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23057 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23058 if (encoding1 < encoding2)
23059 return 1;
23060 if (encoding1 > encoding2)
23061 return -1;
23062 return 0;
23063 }
23064
23065 /* Move base types marked by mark_base_types as early as possible
23066 in the CU, sorted by decreasing usage count both to make the
23067 uleb128 references as small as possible and to make sure they
23068 will have die_offset already computed by calc_die_sizes when
23069 sizes of typed stack loc ops is computed. */
23070
23071 static void
23072 move_marked_base_types (void)
23073 {
23074 unsigned int i;
23075 dw_die_ref base_type, die, c;
23076
23077 if (base_types.is_empty ())
23078 return;
23079
23080 /* Sort by decreasing usage count, they will be added again in that
23081 order later on. */
23082 base_types.qsort (base_type_cmp);
23083 die = comp_unit_die ();
23084 c = die->die_child;
23085 do
23086 {
23087 dw_die_ref prev = c;
23088 c = c->die_sib;
23089 while (c->die_mark)
23090 {
23091 remove_child_with_prev (c, prev);
23092 /* As base types got marked, there must be at least
23093 one node other than DW_TAG_base_type. */
23094 gcc_assert (c != c->die_sib);
23095 c = c->die_sib;
23096 }
23097 }
23098 while (c != die->die_child);
23099 gcc_assert (die->die_child);
23100 c = die->die_child;
23101 for (i = 0; base_types.iterate (i, &base_type); i++)
23102 {
23103 base_type->die_mark = 0;
23104 base_type->die_sib = c->die_sib;
23105 c->die_sib = base_type;
23106 c = base_type;
23107 }
23108 }
23109
23110 /* Helper function for resolve_addr, attempt to resolve
23111 one CONST_STRING, return non-zero if not successful. Similarly verify that
23112 SYMBOL_REFs refer to variables emitted in the current CU. */
23113
23114 static int
23115 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
23116 {
23117 rtx rtl = *addr;
23118
23119 if (GET_CODE (rtl) == CONST_STRING)
23120 {
23121 size_t len = strlen (XSTR (rtl, 0)) + 1;
23122 tree t = build_string (len, XSTR (rtl, 0));
23123 tree tlen = size_int (len - 1);
23124 TREE_TYPE (t)
23125 = build_array_type (char_type_node, build_index_type (tlen));
23126 rtl = lookup_constant_def (t);
23127 if (!rtl || !MEM_P (rtl))
23128 return 1;
23129 rtl = XEXP (rtl, 0);
23130 if (GET_CODE (rtl) == SYMBOL_REF
23131 && SYMBOL_REF_DECL (rtl)
23132 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23133 return 1;
23134 vec_safe_push (used_rtx_array, rtl);
23135 *addr = rtl;
23136 return 0;
23137 }
23138
23139 if (GET_CODE (rtl) == SYMBOL_REF
23140 && SYMBOL_REF_DECL (rtl))
23141 {
23142 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23143 {
23144 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23145 return 1;
23146 }
23147 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23148 return 1;
23149 }
23150
23151 if (GET_CODE (rtl) == CONST
23152 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
23153 return 1;
23154
23155 return 0;
23156 }
23157
23158 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23159 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23160 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23161
23162 static rtx
23163 string_cst_pool_decl (tree t)
23164 {
23165 rtx rtl = output_constant_def (t, 1);
23166 unsigned char *array;
23167 dw_loc_descr_ref l;
23168 tree decl;
23169 size_t len;
23170 dw_die_ref ref;
23171
23172 if (!rtl || !MEM_P (rtl))
23173 return NULL_RTX;
23174 rtl = XEXP (rtl, 0);
23175 if (GET_CODE (rtl) != SYMBOL_REF
23176 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23177 return NULL_RTX;
23178
23179 decl = SYMBOL_REF_DECL (rtl);
23180 if (!lookup_decl_die (decl))
23181 {
23182 len = TREE_STRING_LENGTH (t);
23183 vec_safe_push (used_rtx_array, rtl);
23184 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23185 array = ggc_vec_alloc<unsigned char> (len);
23186 memcpy (array, TREE_STRING_POINTER (t), len);
23187 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23188 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23189 l->dw_loc_oprnd2.v.val_vec.length = len;
23190 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23191 l->dw_loc_oprnd2.v.val_vec.array = array;
23192 add_AT_loc (ref, DW_AT_location, l);
23193 equate_decl_number_to_die (decl, ref);
23194 }
23195 return rtl;
23196 }
23197
23198 /* Helper function of resolve_addr_in_expr. LOC is
23199 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23200 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23201 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23202 with DW_OP_GNU_implicit_pointer if possible
23203 and return true, if unsuccessful, return false. */
23204
23205 static bool
23206 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23207 {
23208 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23209 HOST_WIDE_INT offset = 0;
23210 dw_die_ref ref = NULL;
23211 tree decl;
23212
23213 if (GET_CODE (rtl) == CONST
23214 && GET_CODE (XEXP (rtl, 0)) == PLUS
23215 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23216 {
23217 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23218 rtl = XEXP (XEXP (rtl, 0), 0);
23219 }
23220 if (GET_CODE (rtl) == CONST_STRING)
23221 {
23222 size_t len = strlen (XSTR (rtl, 0)) + 1;
23223 tree t = build_string (len, XSTR (rtl, 0));
23224 tree tlen = size_int (len - 1);
23225
23226 TREE_TYPE (t)
23227 = build_array_type (char_type_node, build_index_type (tlen));
23228 rtl = string_cst_pool_decl (t);
23229 if (!rtl)
23230 return false;
23231 }
23232 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23233 {
23234 decl = SYMBOL_REF_DECL (rtl);
23235 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23236 {
23237 ref = lookup_decl_die (decl);
23238 if (ref && (get_AT (ref, DW_AT_location)
23239 || get_AT (ref, DW_AT_const_value)))
23240 {
23241 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23242 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23243 loc->dw_loc_oprnd1.val_entry = NULL;
23244 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23245 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23246 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23247 loc->dw_loc_oprnd2.v.val_int = offset;
23248 return true;
23249 }
23250 }
23251 }
23252 return false;
23253 }
23254
23255 /* Helper function for resolve_addr, handle one location
23256 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23257 the location list couldn't be resolved. */
23258
23259 static bool
23260 resolve_addr_in_expr (dw_loc_descr_ref loc)
23261 {
23262 dw_loc_descr_ref keep = NULL;
23263 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23264 switch (loc->dw_loc_opc)
23265 {
23266 case DW_OP_addr:
23267 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23268 {
23269 if ((prev == NULL
23270 || prev->dw_loc_opc == DW_OP_piece
23271 || prev->dw_loc_opc == DW_OP_bit_piece)
23272 && loc->dw_loc_next
23273 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23274 && !dwarf_strict
23275 && optimize_one_addr_into_implicit_ptr (loc))
23276 break;
23277 return false;
23278 }
23279 break;
23280 case DW_OP_GNU_addr_index:
23281 case DW_OP_GNU_const_index:
23282 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23283 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23284 {
23285 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23286 if (resolve_one_addr (&rtl, NULL))
23287 return false;
23288 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23289 loc->dw_loc_oprnd1.val_entry =
23290 add_addr_table_entry (rtl, ate_kind_rtx);
23291 }
23292 break;
23293 case DW_OP_const4u:
23294 case DW_OP_const8u:
23295 if (loc->dtprel
23296 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23297 return false;
23298 break;
23299 case DW_OP_plus_uconst:
23300 if (size_of_loc_descr (loc)
23301 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23302 + 1
23303 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23304 {
23305 dw_loc_descr_ref repl
23306 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23307 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23308 add_loc_descr (&repl, loc->dw_loc_next);
23309 *loc = *repl;
23310 }
23311 break;
23312 case DW_OP_implicit_value:
23313 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23314 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23315 return false;
23316 break;
23317 case DW_OP_GNU_implicit_pointer:
23318 case DW_OP_GNU_parameter_ref:
23319 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23320 {
23321 dw_die_ref ref
23322 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23323 if (ref == NULL)
23324 return false;
23325 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23326 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23327 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23328 }
23329 break;
23330 case DW_OP_GNU_const_type:
23331 case DW_OP_GNU_regval_type:
23332 case DW_OP_GNU_deref_type:
23333 case DW_OP_GNU_convert:
23334 case DW_OP_GNU_reinterpret:
23335 while (loc->dw_loc_next
23336 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23337 {
23338 dw_die_ref base1, base2;
23339 unsigned enc1, enc2, size1, size2;
23340 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23341 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23342 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23343 else if (loc->dw_loc_oprnd1.val_class
23344 == dw_val_class_unsigned_const)
23345 break;
23346 else
23347 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23348 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23349 == dw_val_class_unsigned_const)
23350 break;
23351 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23352 gcc_assert (base1->die_tag == DW_TAG_base_type
23353 && base2->die_tag == DW_TAG_base_type);
23354 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23355 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23356 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23357 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23358 if (size1 == size2
23359 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23360 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23361 && loc != keep)
23362 || enc1 == enc2))
23363 {
23364 /* Optimize away next DW_OP_GNU_convert after
23365 adjusting LOC's base type die reference. */
23366 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23367 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23368 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23369 else
23370 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23371 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23372 continue;
23373 }
23374 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23375 point typed stack entry. */
23376 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23377 keep = loc->dw_loc_next;
23378 break;
23379 }
23380 break;
23381 default:
23382 break;
23383 }
23384 return true;
23385 }
23386
23387 /* Helper function of resolve_addr. DIE had DW_AT_location of
23388 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23389 and DW_OP_addr couldn't be resolved. resolve_addr has already
23390 removed the DW_AT_location attribute. This function attempts to
23391 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23392 to it or DW_AT_const_value attribute, if possible. */
23393
23394 static void
23395 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23396 {
23397 if (TREE_CODE (decl) != VAR_DECL
23398 || lookup_decl_die (decl) != die
23399 || DECL_EXTERNAL (decl)
23400 || !TREE_STATIC (decl)
23401 || DECL_INITIAL (decl) == NULL_TREE
23402 || DECL_P (DECL_INITIAL (decl))
23403 || get_AT (die, DW_AT_const_value))
23404 return;
23405
23406 tree init = DECL_INITIAL (decl);
23407 HOST_WIDE_INT offset = 0;
23408 /* For variables that have been optimized away and thus
23409 don't have a memory location, see if we can emit
23410 DW_AT_const_value instead. */
23411 if (tree_add_const_value_attribute (die, init))
23412 return;
23413 if (dwarf_strict)
23414 return;
23415 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23416 and ADDR_EXPR refers to a decl that has DW_AT_location or
23417 DW_AT_const_value (but isn't addressable, otherwise
23418 resolving the original DW_OP_addr wouldn't fail), see if
23419 we can add DW_OP_GNU_implicit_pointer. */
23420 STRIP_NOPS (init);
23421 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23422 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23423 {
23424 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23425 init = TREE_OPERAND (init, 0);
23426 STRIP_NOPS (init);
23427 }
23428 if (TREE_CODE (init) != ADDR_EXPR)
23429 return;
23430 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23431 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23432 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23433 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23434 && TREE_OPERAND (init, 0) != decl))
23435 {
23436 dw_die_ref ref;
23437 dw_loc_descr_ref l;
23438
23439 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23440 {
23441 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23442 if (!rtl)
23443 return;
23444 decl = SYMBOL_REF_DECL (rtl);
23445 }
23446 else
23447 decl = TREE_OPERAND (init, 0);
23448 ref = lookup_decl_die (decl);
23449 if (ref == NULL
23450 || (!get_AT (ref, DW_AT_location)
23451 && !get_AT (ref, DW_AT_const_value)))
23452 return;
23453 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23454 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23455 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23456 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23457 add_AT_loc (die, DW_AT_location, l);
23458 }
23459 }
23460
23461 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23462 an address in .rodata section if the string literal is emitted there,
23463 or remove the containing location list or replace DW_AT_const_value
23464 with DW_AT_location and empty location expression, if it isn't found
23465 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23466 to something that has been emitted in the current CU. */
23467
23468 static void
23469 resolve_addr (dw_die_ref die)
23470 {
23471 dw_die_ref c;
23472 dw_attr_ref a;
23473 dw_loc_list_ref *curr, *start, loc;
23474 unsigned ix;
23475
23476 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23477 switch (AT_class (a))
23478 {
23479 case dw_val_class_loc_list:
23480 start = curr = AT_loc_list_ptr (a);
23481 loc = *curr;
23482 gcc_assert (loc);
23483 /* The same list can be referenced more than once. See if we have
23484 already recorded the result from a previous pass. */
23485 if (loc->replaced)
23486 *curr = loc->dw_loc_next;
23487 else if (!loc->resolved_addr)
23488 {
23489 /* As things stand, we do not expect or allow one die to
23490 reference a suffix of another die's location list chain.
23491 References must be identical or completely separate.
23492 There is therefore no need to cache the result of this
23493 pass on any list other than the first; doing so
23494 would lead to unnecessary writes. */
23495 while (*curr)
23496 {
23497 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23498 if (!resolve_addr_in_expr ((*curr)->expr))
23499 {
23500 dw_loc_list_ref next = (*curr)->dw_loc_next;
23501 dw_loc_descr_ref l = (*curr)->expr;
23502
23503 if (next && (*curr)->ll_symbol)
23504 {
23505 gcc_assert (!next->ll_symbol);
23506 next->ll_symbol = (*curr)->ll_symbol;
23507 }
23508 if (dwarf_split_debug_info)
23509 remove_loc_list_addr_table_entries (l);
23510 *curr = next;
23511 }
23512 else
23513 {
23514 mark_base_types ((*curr)->expr);
23515 curr = &(*curr)->dw_loc_next;
23516 }
23517 }
23518 if (loc == *start)
23519 loc->resolved_addr = 1;
23520 else
23521 {
23522 loc->replaced = 1;
23523 loc->dw_loc_next = *start;
23524 }
23525 }
23526 if (!*start)
23527 {
23528 remove_AT (die, a->dw_attr);
23529 ix--;
23530 }
23531 break;
23532 case dw_val_class_loc:
23533 {
23534 dw_loc_descr_ref l = AT_loc (a);
23535 /* For -gdwarf-2 don't attempt to optimize
23536 DW_AT_data_member_location containing
23537 DW_OP_plus_uconst - older consumers might
23538 rely on it being that op instead of a more complex,
23539 but shorter, location description. */
23540 if ((dwarf_version > 2
23541 || a->dw_attr != DW_AT_data_member_location
23542 || l == NULL
23543 || l->dw_loc_opc != DW_OP_plus_uconst
23544 || l->dw_loc_next != NULL)
23545 && !resolve_addr_in_expr (l))
23546 {
23547 if (dwarf_split_debug_info)
23548 remove_loc_list_addr_table_entries (l);
23549 if (l != NULL
23550 && l->dw_loc_next == NULL
23551 && l->dw_loc_opc == DW_OP_addr
23552 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23553 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23554 && a->dw_attr == DW_AT_location)
23555 {
23556 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23557 remove_AT (die, a->dw_attr);
23558 ix--;
23559 optimize_location_into_implicit_ptr (die, decl);
23560 break;
23561 }
23562 remove_AT (die, a->dw_attr);
23563 ix--;
23564 }
23565 else
23566 mark_base_types (l);
23567 }
23568 break;
23569 case dw_val_class_addr:
23570 if (a->dw_attr == DW_AT_const_value
23571 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23572 {
23573 if (AT_index (a) != NOT_INDEXED)
23574 remove_addr_table_entry (a->dw_attr_val.val_entry);
23575 remove_AT (die, a->dw_attr);
23576 ix--;
23577 }
23578 if (die->die_tag == DW_TAG_GNU_call_site
23579 && a->dw_attr == DW_AT_abstract_origin)
23580 {
23581 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23582 dw_die_ref tdie = lookup_decl_die (tdecl);
23583 if (tdie == NULL
23584 && DECL_EXTERNAL (tdecl)
23585 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23586 {
23587 force_decl_die (tdecl);
23588 tdie = lookup_decl_die (tdecl);
23589 }
23590 if (tdie)
23591 {
23592 a->dw_attr_val.val_class = dw_val_class_die_ref;
23593 a->dw_attr_val.v.val_die_ref.die = tdie;
23594 a->dw_attr_val.v.val_die_ref.external = 0;
23595 }
23596 else
23597 {
23598 if (AT_index (a) != NOT_INDEXED)
23599 remove_addr_table_entry (a->dw_attr_val.val_entry);
23600 remove_AT (die, a->dw_attr);
23601 ix--;
23602 }
23603 }
23604 break;
23605 default:
23606 break;
23607 }
23608
23609 FOR_EACH_CHILD (die, c, resolve_addr (c));
23610 }
23611 \f
23612 /* Helper routines for optimize_location_lists.
23613 This pass tries to share identical local lists in .debug_loc
23614 section. */
23615
23616 /* Iteratively hash operands of LOC opcode into HSTATE. */
23617
23618 static void
23619 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23620 {
23621 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23622 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23623
23624 switch (loc->dw_loc_opc)
23625 {
23626 case DW_OP_const4u:
23627 case DW_OP_const8u:
23628 if (loc->dtprel)
23629 goto hash_addr;
23630 /* FALLTHRU */
23631 case DW_OP_const1u:
23632 case DW_OP_const1s:
23633 case DW_OP_const2u:
23634 case DW_OP_const2s:
23635 case DW_OP_const4s:
23636 case DW_OP_const8s:
23637 case DW_OP_constu:
23638 case DW_OP_consts:
23639 case DW_OP_pick:
23640 case DW_OP_plus_uconst:
23641 case DW_OP_breg0:
23642 case DW_OP_breg1:
23643 case DW_OP_breg2:
23644 case DW_OP_breg3:
23645 case DW_OP_breg4:
23646 case DW_OP_breg5:
23647 case DW_OP_breg6:
23648 case DW_OP_breg7:
23649 case DW_OP_breg8:
23650 case DW_OP_breg9:
23651 case DW_OP_breg10:
23652 case DW_OP_breg11:
23653 case DW_OP_breg12:
23654 case DW_OP_breg13:
23655 case DW_OP_breg14:
23656 case DW_OP_breg15:
23657 case DW_OP_breg16:
23658 case DW_OP_breg17:
23659 case DW_OP_breg18:
23660 case DW_OP_breg19:
23661 case DW_OP_breg20:
23662 case DW_OP_breg21:
23663 case DW_OP_breg22:
23664 case DW_OP_breg23:
23665 case DW_OP_breg24:
23666 case DW_OP_breg25:
23667 case DW_OP_breg26:
23668 case DW_OP_breg27:
23669 case DW_OP_breg28:
23670 case DW_OP_breg29:
23671 case DW_OP_breg30:
23672 case DW_OP_breg31:
23673 case DW_OP_regx:
23674 case DW_OP_fbreg:
23675 case DW_OP_piece:
23676 case DW_OP_deref_size:
23677 case DW_OP_xderef_size:
23678 hstate.add_object (val1->v.val_int);
23679 break;
23680 case DW_OP_skip:
23681 case DW_OP_bra:
23682 {
23683 int offset;
23684
23685 gcc_assert (val1->val_class == dw_val_class_loc);
23686 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23687 hstate.add_object (offset);
23688 }
23689 break;
23690 case DW_OP_implicit_value:
23691 hstate.add_object (val1->v.val_unsigned);
23692 switch (val2->val_class)
23693 {
23694 case dw_val_class_const:
23695 hstate.add_object (val2->v.val_int);
23696 break;
23697 case dw_val_class_vec:
23698 {
23699 unsigned int elt_size = val2->v.val_vec.elt_size;
23700 unsigned int len = val2->v.val_vec.length;
23701
23702 hstate.add_int (elt_size);
23703 hstate.add_int (len);
23704 hstate.add (val2->v.val_vec.array, len * elt_size);
23705 }
23706 break;
23707 case dw_val_class_const_double:
23708 hstate.add_object (val2->v.val_double.low);
23709 hstate.add_object (val2->v.val_double.high);
23710 break;
23711 case dw_val_class_wide_int:
23712 hstate.add_object (*val2->v.val_wide);
23713 break;
23714 case dw_val_class_addr:
23715 inchash::add_rtx (val2->v.val_addr, hstate);
23716 break;
23717 default:
23718 gcc_unreachable ();
23719 }
23720 break;
23721 case DW_OP_bregx:
23722 case DW_OP_bit_piece:
23723 hstate.add_object (val1->v.val_int);
23724 hstate.add_object (val2->v.val_int);
23725 break;
23726 case DW_OP_addr:
23727 hash_addr:
23728 if (loc->dtprel)
23729 {
23730 unsigned char dtprel = 0xd1;
23731 hstate.add_object (dtprel);
23732 }
23733 inchash::add_rtx (val1->v.val_addr, hstate);
23734 break;
23735 case DW_OP_GNU_addr_index:
23736 case DW_OP_GNU_const_index:
23737 {
23738 if (loc->dtprel)
23739 {
23740 unsigned char dtprel = 0xd1;
23741 hstate.add_object (dtprel);
23742 }
23743 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
23744 }
23745 break;
23746 case DW_OP_GNU_implicit_pointer:
23747 hstate.add_int (val2->v.val_int);
23748 break;
23749 case DW_OP_GNU_entry_value:
23750 hstate.add_object (val1->v.val_loc);
23751 break;
23752 case DW_OP_GNU_regval_type:
23753 case DW_OP_GNU_deref_type:
23754 {
23755 unsigned int byte_size
23756 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23757 unsigned int encoding
23758 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23759 hstate.add_object (val1->v.val_int);
23760 hstate.add_object (byte_size);
23761 hstate.add_object (encoding);
23762 }
23763 break;
23764 case DW_OP_GNU_convert:
23765 case DW_OP_GNU_reinterpret:
23766 if (val1->val_class == dw_val_class_unsigned_const)
23767 {
23768 hstate.add_object (val1->v.val_unsigned);
23769 break;
23770 }
23771 /* FALLTHRU */
23772 case DW_OP_GNU_const_type:
23773 {
23774 unsigned int byte_size
23775 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23776 unsigned int encoding
23777 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23778 hstate.add_object (byte_size);
23779 hstate.add_object (encoding);
23780 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23781 break;
23782 hstate.add_object (val2->val_class);
23783 switch (val2->val_class)
23784 {
23785 case dw_val_class_const:
23786 hstate.add_object (val2->v.val_int);
23787 break;
23788 case dw_val_class_vec:
23789 {
23790 unsigned int elt_size = val2->v.val_vec.elt_size;
23791 unsigned int len = val2->v.val_vec.length;
23792
23793 hstate.add_object (elt_size);
23794 hstate.add_object (len);
23795 hstate.add (val2->v.val_vec.array, len * elt_size);
23796 }
23797 break;
23798 case dw_val_class_const_double:
23799 hstate.add_object (val2->v.val_double.low);
23800 hstate.add_object (val2->v.val_double.high);
23801 break;
23802 case dw_val_class_wide_int:
23803 hstate.add_object (*val2->v.val_wide);
23804 break;
23805 default:
23806 gcc_unreachable ();
23807 }
23808 }
23809 break;
23810
23811 default:
23812 /* Other codes have no operands. */
23813 break;
23814 }
23815 }
23816
23817 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
23818
23819 static inline void
23820 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
23821 {
23822 dw_loc_descr_ref l;
23823 bool sizes_computed = false;
23824 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23825 size_of_locs (loc);
23826
23827 for (l = loc; l != NULL; l = l->dw_loc_next)
23828 {
23829 enum dwarf_location_atom opc = l->dw_loc_opc;
23830 hstate.add_object (opc);
23831 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23832 {
23833 size_of_locs (loc);
23834 sizes_computed = true;
23835 }
23836 hash_loc_operands (l, hstate);
23837 }
23838 }
23839
23840 /* Compute hash of the whole location list LIST_HEAD. */
23841
23842 static inline void
23843 hash_loc_list (dw_loc_list_ref list_head)
23844 {
23845 dw_loc_list_ref curr = list_head;
23846 inchash::hash hstate;
23847
23848 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23849 {
23850 hstate.add (curr->begin, strlen (curr->begin) + 1);
23851 hstate.add (curr->end, strlen (curr->end) + 1);
23852 if (curr->section)
23853 hstate.add (curr->section, strlen (curr->section) + 1);
23854 hash_locs (curr->expr, hstate);
23855 }
23856 list_head->hash = hstate.end ();
23857 }
23858
23859 /* Return true if X and Y opcodes have the same operands. */
23860
23861 static inline bool
23862 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23863 {
23864 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23865 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23866 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23867 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23868
23869 switch (x->dw_loc_opc)
23870 {
23871 case DW_OP_const4u:
23872 case DW_OP_const8u:
23873 if (x->dtprel)
23874 goto hash_addr;
23875 /* FALLTHRU */
23876 case DW_OP_const1u:
23877 case DW_OP_const1s:
23878 case DW_OP_const2u:
23879 case DW_OP_const2s:
23880 case DW_OP_const4s:
23881 case DW_OP_const8s:
23882 case DW_OP_constu:
23883 case DW_OP_consts:
23884 case DW_OP_pick:
23885 case DW_OP_plus_uconst:
23886 case DW_OP_breg0:
23887 case DW_OP_breg1:
23888 case DW_OP_breg2:
23889 case DW_OP_breg3:
23890 case DW_OP_breg4:
23891 case DW_OP_breg5:
23892 case DW_OP_breg6:
23893 case DW_OP_breg7:
23894 case DW_OP_breg8:
23895 case DW_OP_breg9:
23896 case DW_OP_breg10:
23897 case DW_OP_breg11:
23898 case DW_OP_breg12:
23899 case DW_OP_breg13:
23900 case DW_OP_breg14:
23901 case DW_OP_breg15:
23902 case DW_OP_breg16:
23903 case DW_OP_breg17:
23904 case DW_OP_breg18:
23905 case DW_OP_breg19:
23906 case DW_OP_breg20:
23907 case DW_OP_breg21:
23908 case DW_OP_breg22:
23909 case DW_OP_breg23:
23910 case DW_OP_breg24:
23911 case DW_OP_breg25:
23912 case DW_OP_breg26:
23913 case DW_OP_breg27:
23914 case DW_OP_breg28:
23915 case DW_OP_breg29:
23916 case DW_OP_breg30:
23917 case DW_OP_breg31:
23918 case DW_OP_regx:
23919 case DW_OP_fbreg:
23920 case DW_OP_piece:
23921 case DW_OP_deref_size:
23922 case DW_OP_xderef_size:
23923 return valx1->v.val_int == valy1->v.val_int;
23924 case DW_OP_skip:
23925 case DW_OP_bra:
23926 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23927 can cause irrelevant differences in dw_loc_addr. */
23928 gcc_assert (valx1->val_class == dw_val_class_loc
23929 && valy1->val_class == dw_val_class_loc
23930 && (dwarf_split_debug_info
23931 || x->dw_loc_addr == y->dw_loc_addr));
23932 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23933 case DW_OP_implicit_value:
23934 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23935 || valx2->val_class != valy2->val_class)
23936 return false;
23937 switch (valx2->val_class)
23938 {
23939 case dw_val_class_const:
23940 return valx2->v.val_int == valy2->v.val_int;
23941 case dw_val_class_vec:
23942 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23943 && valx2->v.val_vec.length == valy2->v.val_vec.length
23944 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23945 valx2->v.val_vec.elt_size
23946 * valx2->v.val_vec.length) == 0;
23947 case dw_val_class_const_double:
23948 return valx2->v.val_double.low == valy2->v.val_double.low
23949 && valx2->v.val_double.high == valy2->v.val_double.high;
23950 case dw_val_class_wide_int:
23951 return *valx2->v.val_wide == *valy2->v.val_wide;
23952 case dw_val_class_addr:
23953 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23954 default:
23955 gcc_unreachable ();
23956 }
23957 case DW_OP_bregx:
23958 case DW_OP_bit_piece:
23959 return valx1->v.val_int == valy1->v.val_int
23960 && valx2->v.val_int == valy2->v.val_int;
23961 case DW_OP_addr:
23962 hash_addr:
23963 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23964 case DW_OP_GNU_addr_index:
23965 case DW_OP_GNU_const_index:
23966 {
23967 rtx ax1 = valx1->val_entry->addr.rtl;
23968 rtx ay1 = valy1->val_entry->addr.rtl;
23969 return rtx_equal_p (ax1, ay1);
23970 }
23971 case DW_OP_GNU_implicit_pointer:
23972 return valx1->val_class == dw_val_class_die_ref
23973 && valx1->val_class == valy1->val_class
23974 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23975 && valx2->v.val_int == valy2->v.val_int;
23976 case DW_OP_GNU_entry_value:
23977 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23978 case DW_OP_GNU_const_type:
23979 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23980 || valx2->val_class != valy2->val_class)
23981 return false;
23982 switch (valx2->val_class)
23983 {
23984 case dw_val_class_const:
23985 return valx2->v.val_int == valy2->v.val_int;
23986 case dw_val_class_vec:
23987 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23988 && valx2->v.val_vec.length == valy2->v.val_vec.length
23989 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23990 valx2->v.val_vec.elt_size
23991 * valx2->v.val_vec.length) == 0;
23992 case dw_val_class_const_double:
23993 return valx2->v.val_double.low == valy2->v.val_double.low
23994 && valx2->v.val_double.high == valy2->v.val_double.high;
23995 case dw_val_class_wide_int:
23996 return *valx2->v.val_wide == *valy2->v.val_wide;
23997 default:
23998 gcc_unreachable ();
23999 }
24000 case DW_OP_GNU_regval_type:
24001 case DW_OP_GNU_deref_type:
24002 return valx1->v.val_int == valy1->v.val_int
24003 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24004 case DW_OP_GNU_convert:
24005 case DW_OP_GNU_reinterpret:
24006 if (valx1->val_class != valy1->val_class)
24007 return false;
24008 if (valx1->val_class == dw_val_class_unsigned_const)
24009 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24010 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24011 case DW_OP_GNU_parameter_ref:
24012 return valx1->val_class == dw_val_class_die_ref
24013 && valx1->val_class == valy1->val_class
24014 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24015 default:
24016 /* Other codes have no operands. */
24017 return true;
24018 }
24019 }
24020
24021 /* Return true if DWARF location expressions X and Y are the same. */
24022
24023 static inline bool
24024 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24025 {
24026 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24027 if (x->dw_loc_opc != y->dw_loc_opc
24028 || x->dtprel != y->dtprel
24029 || !compare_loc_operands (x, y))
24030 break;
24031 return x == NULL && y == NULL;
24032 }
24033
24034 /* Hashtable helpers. */
24035
24036 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24037 {
24038 typedef dw_loc_list_struct value_type;
24039 typedef dw_loc_list_struct compare_type;
24040 static inline hashval_t hash (const value_type *);
24041 static inline bool equal (const value_type *, const compare_type *);
24042 };
24043
24044 /* Return precomputed hash of location list X. */
24045
24046 inline hashval_t
24047 loc_list_hasher::hash (const value_type *x)
24048 {
24049 return x->hash;
24050 }
24051
24052 /* Return true if location lists A and B are the same. */
24053
24054 inline bool
24055 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24056 {
24057 if (a == b)
24058 return 1;
24059 if (a->hash != b->hash)
24060 return 0;
24061 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24062 if (strcmp (a->begin, b->begin) != 0
24063 || strcmp (a->end, b->end) != 0
24064 || (a->section == NULL) != (b->section == NULL)
24065 || (a->section && strcmp (a->section, b->section) != 0)
24066 || !compare_locs (a->expr, b->expr))
24067 break;
24068 return a == NULL && b == NULL;
24069 }
24070
24071 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24072
24073
24074 /* Recursively optimize location lists referenced from DIE
24075 children and share them whenever possible. */
24076
24077 static void
24078 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24079 {
24080 dw_die_ref c;
24081 dw_attr_ref a;
24082 unsigned ix;
24083 dw_loc_list_struct **slot;
24084
24085 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24086 if (AT_class (a) == dw_val_class_loc_list)
24087 {
24088 dw_loc_list_ref list = AT_loc_list (a);
24089 /* TODO: perform some optimizations here, before hashing
24090 it and storing into the hash table. */
24091 hash_loc_list (list);
24092 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24093 if (*slot == NULL)
24094 *slot = list;
24095 else
24096 a->dw_attr_val.v.val_loc_list = *slot;
24097 }
24098
24099 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24100 }
24101
24102
24103 /* Recursively assign each location list a unique index into the debug_addr
24104 section. */
24105
24106 static void
24107 index_location_lists (dw_die_ref die)
24108 {
24109 dw_die_ref c;
24110 dw_attr_ref a;
24111 unsigned ix;
24112
24113 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24114 if (AT_class (a) == dw_val_class_loc_list)
24115 {
24116 dw_loc_list_ref list = AT_loc_list (a);
24117 dw_loc_list_ref curr;
24118 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24119 {
24120 /* Don't index an entry that has already been indexed
24121 or won't be output. */
24122 if (curr->begin_entry != NULL
24123 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24124 continue;
24125
24126 curr->begin_entry
24127 = add_addr_table_entry (xstrdup (curr->begin),
24128 ate_kind_label);
24129 }
24130 }
24131
24132 FOR_EACH_CHILD (die, c, index_location_lists (c));
24133 }
24134
24135 /* Optimize location lists referenced from DIE
24136 children and share them whenever possible. */
24137
24138 static void
24139 optimize_location_lists (dw_die_ref die)
24140 {
24141 loc_list_hash_type htab (500);
24142 optimize_location_lists_1 (die, &htab);
24143 }
24144 \f
24145 /* Output stuff that dwarf requires at the end of every file,
24146 and generate the DWARF-2 debugging info. */
24147
24148 static void
24149 dwarf2out_finish (const char *filename)
24150 {
24151 limbo_die_node *node, *next_node;
24152 comdat_type_node *ctnode;
24153 unsigned int i;
24154 dw_die_ref main_comp_unit_die;
24155
24156 /* PCH might result in DW_AT_producer string being restored from the
24157 header compilation, so always fill it with empty string initially
24158 and overwrite only here. */
24159 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24160 producer_string = gen_producer_string ();
24161 producer->dw_attr_val.v.val_str->refcount--;
24162 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24163
24164 gen_scheduled_generic_parms_dies ();
24165 gen_remaining_tmpl_value_param_die_attribute ();
24166
24167 /* Add the name for the main input file now. We delayed this from
24168 dwarf2out_init to avoid complications with PCH. */
24169 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24170 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24171 add_comp_dir_attribute (comp_unit_die ());
24172 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24173 {
24174 bool p = false;
24175 htab_traverse (file_table, file_table_relative_p, &p);
24176 if (p)
24177 add_comp_dir_attribute (comp_unit_die ());
24178 }
24179
24180 if (deferred_locations_list)
24181 for (i = 0; i < deferred_locations_list->length (); i++)
24182 {
24183 add_location_or_const_value_attribute (
24184 (*deferred_locations_list)[i].die,
24185 (*deferred_locations_list)[i].variable,
24186 false,
24187 DW_AT_location);
24188 }
24189
24190 /* Traverse the limbo die list, and add parent/child links. The only
24191 dies without parents that should be here are concrete instances of
24192 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24193 For concrete instances, we can get the parent die from the abstract
24194 instance. */
24195 for (node = limbo_die_list; node; node = next_node)
24196 {
24197 dw_die_ref die = node->die;
24198 next_node = node->next;
24199
24200 if (die->die_parent == NULL)
24201 {
24202 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24203
24204 if (origin && origin->die_parent)
24205 add_child_die (origin->die_parent, die);
24206 else if (is_cu_die (die))
24207 ;
24208 else if (seen_error ())
24209 /* It's OK to be confused by errors in the input. */
24210 add_child_die (comp_unit_die (), die);
24211 else
24212 {
24213 /* In certain situations, the lexical block containing a
24214 nested function can be optimized away, which results
24215 in the nested function die being orphaned. Likewise
24216 with the return type of that nested function. Force
24217 this to be a child of the containing function.
24218
24219 It may happen that even the containing function got fully
24220 inlined and optimized out. In that case we are lost and
24221 assign the empty child. This should not be big issue as
24222 the function is likely unreachable too. */
24223 gcc_assert (node->created_for);
24224
24225 if (DECL_P (node->created_for))
24226 origin = get_context_die (DECL_CONTEXT (node->created_for));
24227 else if (TYPE_P (node->created_for))
24228 origin = scope_die_for (node->created_for, comp_unit_die ());
24229 else
24230 origin = comp_unit_die ();
24231
24232 add_child_die (origin, die);
24233 }
24234 }
24235 }
24236
24237 limbo_die_list = NULL;
24238
24239 #if ENABLE_ASSERT_CHECKING
24240 {
24241 dw_die_ref die = comp_unit_die (), c;
24242 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24243 }
24244 #endif
24245 resolve_addr (comp_unit_die ());
24246 move_marked_base_types ();
24247
24248 for (node = deferred_asm_name; node; node = node->next)
24249 {
24250 tree decl = node->created_for;
24251 /* When generating LTO bytecode we can not generate new assembler
24252 names at this point and all important decls got theirs via
24253 free-lang-data. */
24254 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24255 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24256 {
24257 add_linkage_attr (node->die, decl);
24258 move_linkage_attr (node->die);
24259 }
24260 }
24261
24262 deferred_asm_name = NULL;
24263
24264 /* Walk through the list of incomplete types again, trying once more to
24265 emit full debugging info for them. */
24266 retry_incomplete_types ();
24267
24268 if (flag_eliminate_unused_debug_types)
24269 prune_unused_types ();
24270
24271 /* Generate separate COMDAT sections for type DIEs. */
24272 if (use_debug_types)
24273 {
24274 break_out_comdat_types (comp_unit_die ());
24275
24276 /* Each new type_unit DIE was added to the limbo die list when created.
24277 Since these have all been added to comdat_type_list, clear the
24278 limbo die list. */
24279 limbo_die_list = NULL;
24280
24281 /* For each new comdat type unit, copy declarations for incomplete
24282 types to make the new unit self-contained (i.e., no direct
24283 references to the main compile unit). */
24284 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24285 copy_decls_for_unworthy_types (ctnode->root_die);
24286 copy_decls_for_unworthy_types (comp_unit_die ());
24287
24288 /* In the process of copying declarations from one unit to another,
24289 we may have left some declarations behind that are no longer
24290 referenced. Prune them. */
24291 prune_unused_types ();
24292 }
24293
24294 /* Generate separate CUs for each of the include files we've seen.
24295 They will go into limbo_die_list. */
24296 if (flag_eliminate_dwarf2_dups)
24297 break_out_includes (comp_unit_die ());
24298
24299 /* Traverse the DIE's and add add sibling attributes to those DIE's
24300 that have children. */
24301 add_sibling_attributes (comp_unit_die ());
24302 for (node = limbo_die_list; node; node = node->next)
24303 add_sibling_attributes (node->die);
24304 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24305 add_sibling_attributes (ctnode->root_die);
24306
24307 /* When splitting DWARF info, we put some attributes in the
24308 skeleton compile_unit DIE that remains in the .o, while
24309 most attributes go in the DWO compile_unit_die. */
24310 if (dwarf_split_debug_info)
24311 main_comp_unit_die = gen_compile_unit_die (NULL);
24312 else
24313 main_comp_unit_die = comp_unit_die ();
24314
24315 /* Output a terminator label for the .text section. */
24316 switch_to_section (text_section);
24317 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24318 if (cold_text_section)
24319 {
24320 switch_to_section (cold_text_section);
24321 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24322 }
24323
24324 /* We can only use the low/high_pc attributes if all of the code was
24325 in .text. */
24326 if (!have_multiple_function_sections
24327 || (dwarf_version < 3 && dwarf_strict))
24328 {
24329 /* Don't add if the CU has no associated code. */
24330 if (text_section_used)
24331 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24332 text_end_label, true);
24333 }
24334 else
24335 {
24336 unsigned fde_idx;
24337 dw_fde_ref fde;
24338 bool range_list_added = false;
24339
24340 if (text_section_used)
24341 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24342 text_end_label, &range_list_added, true);
24343 if (cold_text_section_used)
24344 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24345 cold_end_label, &range_list_added, true);
24346
24347 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24348 {
24349 if (DECL_IGNORED_P (fde->decl))
24350 continue;
24351 if (!fde->in_std_section)
24352 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24353 fde->dw_fde_end, &range_list_added,
24354 true);
24355 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24356 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24357 fde->dw_fde_second_end, &range_list_added,
24358 true);
24359 }
24360
24361 if (range_list_added)
24362 {
24363 /* We need to give .debug_loc and .debug_ranges an appropriate
24364 "base address". Use zero so that these addresses become
24365 absolute. Historically, we've emitted the unexpected
24366 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24367 Emit both to give time for other tools to adapt. */
24368 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24369 if (! dwarf_strict && dwarf_version < 4)
24370 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24371
24372 add_ranges (NULL);
24373 }
24374 }
24375
24376 if (debug_info_level >= DINFO_LEVEL_TERSE)
24377 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24378 debug_line_section_label);
24379
24380 if (have_macinfo)
24381 add_AT_macptr (comp_unit_die (),
24382 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24383 macinfo_section_label);
24384
24385 if (dwarf_split_debug_info)
24386 {
24387 /* optimize_location_lists calculates the size of the lists,
24388 so index them first, and assign indices to the entries.
24389 Although optimize_location_lists will remove entries from
24390 the table, it only does so for duplicates, and therefore
24391 only reduces ref_counts to 1. */
24392 index_location_lists (comp_unit_die ());
24393
24394 if (addr_index_table != NULL)
24395 {
24396 unsigned int index = 0;
24397 htab_traverse_noresize (addr_index_table,
24398 index_addr_table_entry, &index);
24399 }
24400 }
24401
24402 if (have_location_lists)
24403 optimize_location_lists (comp_unit_die ());
24404
24405 save_macinfo_strings ();
24406
24407 if (dwarf_split_debug_info)
24408 {
24409 unsigned int index = 0;
24410
24411 /* Add attributes common to skeleton compile_units and
24412 type_units. Because these attributes include strings, it
24413 must be done before freezing the string table. Top-level
24414 skeleton die attrs are added when the skeleton type unit is
24415 created, so ensure it is created by this point. */
24416 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24417 htab_traverse_noresize (debug_str_hash, index_string, &index);
24418 }
24419
24420 /* Output all of the compilation units. We put the main one last so that
24421 the offsets are available to output_pubnames. */
24422 for (node = limbo_die_list; node; node = node->next)
24423 output_comp_unit (node->die, 0);
24424
24425 hash_table<comdat_type_hasher> comdat_type_table (100);
24426 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24427 {
24428 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24429
24430 /* Don't output duplicate types. */
24431 if (*slot != HTAB_EMPTY_ENTRY)
24432 continue;
24433
24434 /* Add a pointer to the line table for the main compilation unit
24435 so that the debugger can make sense of DW_AT_decl_file
24436 attributes. */
24437 if (debug_info_level >= DINFO_LEVEL_TERSE)
24438 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24439 (!dwarf_split_debug_info
24440 ? debug_line_section_label
24441 : debug_skeleton_line_section_label));
24442
24443 output_comdat_type_unit (ctnode);
24444 *slot = ctnode;
24445 }
24446
24447 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24448 both the main_cu and all skeleton TUs. Making this call unconditional
24449 would end up either adding a second copy of the AT_pubnames attribute, or
24450 requiring a special case in add_top_level_skeleton_die_attrs. */
24451 if (!dwarf_split_debug_info)
24452 add_AT_pubnames (comp_unit_die ());
24453
24454 if (dwarf_split_debug_info)
24455 {
24456 int mark;
24457 unsigned char checksum[16];
24458 struct md5_ctx ctx;
24459
24460 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24461 md5_init_ctx (&ctx);
24462 mark = 0;
24463 die_checksum (comp_unit_die (), &ctx, &mark);
24464 unmark_all_dies (comp_unit_die ());
24465 md5_finish_ctx (&ctx, checksum);
24466
24467 /* Use the first 8 bytes of the checksum as the dwo_id,
24468 and add it to both comp-unit DIEs. */
24469 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24470 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24471
24472 /* Add the base offset of the ranges table to the skeleton
24473 comp-unit DIE. */
24474 if (ranges_table_in_use)
24475 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24476 ranges_section_label);
24477
24478 switch_to_section (debug_addr_section);
24479 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24480 output_addr_table ();
24481 }
24482
24483 /* Output the main compilation unit if non-empty or if .debug_macinfo
24484 or .debug_macro will be emitted. */
24485 output_comp_unit (comp_unit_die (), have_macinfo);
24486
24487 if (dwarf_split_debug_info && info_section_emitted)
24488 output_skeleton_debug_sections (main_comp_unit_die);
24489
24490 /* Output the abbreviation table. */
24491 if (abbrev_die_table_in_use != 1)
24492 {
24493 switch_to_section (debug_abbrev_section);
24494 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24495 output_abbrev_section ();
24496 }
24497
24498 /* Output location list section if necessary. */
24499 if (have_location_lists)
24500 {
24501 /* Output the location lists info. */
24502 switch_to_section (debug_loc_section);
24503 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24504 output_location_lists (comp_unit_die ());
24505 }
24506
24507 output_pubtables ();
24508
24509 /* Output the address range information if a CU (.debug_info section)
24510 was emitted. We output an empty table even if we had no functions
24511 to put in it. This because the consumer has no way to tell the
24512 difference between an empty table that we omitted and failure to
24513 generate a table that would have contained data. */
24514 if (info_section_emitted)
24515 {
24516 unsigned long aranges_length = size_of_aranges ();
24517
24518 switch_to_section (debug_aranges_section);
24519 output_aranges (aranges_length);
24520 }
24521
24522 /* Output ranges section if necessary. */
24523 if (ranges_table_in_use)
24524 {
24525 switch_to_section (debug_ranges_section);
24526 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24527 output_ranges ();
24528 }
24529
24530 /* Have to end the macro section. */
24531 if (have_macinfo)
24532 {
24533 switch_to_section (debug_macinfo_section);
24534 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24535 output_macinfo ();
24536 dw2_asm_output_data (1, 0, "End compilation unit");
24537 }
24538
24539 /* Output the source line correspondence table. We must do this
24540 even if there is no line information. Otherwise, on an empty
24541 translation unit, we will generate a present, but empty,
24542 .debug_info section. IRIX 6.5 `nm' will then complain when
24543 examining the file. This is done late so that any filenames
24544 used by the debug_info section are marked as 'used'. */
24545 switch_to_section (debug_line_section);
24546 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24547 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24548 output_line_info (false);
24549
24550 if (dwarf_split_debug_info && info_section_emitted)
24551 {
24552 switch_to_section (debug_skeleton_line_section);
24553 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24554 output_line_info (true);
24555 }
24556
24557 /* If we emitted any indirect strings, output the string table too. */
24558 if (debug_str_hash || skeleton_debug_str_hash)
24559 output_indirect_strings ();
24560 }
24561
24562 #include "gt-dwarf2out.h"