lto-cgraph.c (output_profile_summary, [...]): Use gcov streaming; stream hot bb thres...
[gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
28
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
34
35 /* DWARF2 Abbreviation Glossary:
36
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
42
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
45
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
48
49 DIE = Debugging Information Entry
50
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
54
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "tree.h"
63 #include "version.h"
64 #include "flags.h"
65 #include "rtl.h"
66 #include "hard-reg-set.h"
67 #include "regs.h"
68 #include "insn-config.h"
69 #include "reload.h"
70 #include "function.h"
71 #include "output.h"
72 #include "expr.h"
73 #include "except.h"
74 #include "dwarf2.h"
75 #include "dwarf2out.h"
76 #include "dwarf2asm.h"
77 #include "toplev.h"
78 #include "ggc.h"
79 #include "md5.h"
80 #include "tm_p.h"
81 #include "diagnostic.h"
82 #include "tree-pretty-print.h"
83 #include "debug.h"
84 #include "target.h"
85 #include "common/common-target.h"
86 #include "langhooks.h"
87 #include "hashtab.h"
88 #include "cgraph.h"
89 #include "input.h"
90 #include "gimple.h"
91 #include "ira.h"
92 #include "lra.h"
93 #include "dumpfile.h"
94 #include "opts.h"
95
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
98 static rtx cached_next_real_insn;
99
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
102
103 /* Define this macro to be a nonzero value if the directory specifications
104 which are output in the debug info should end with a separator. */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107 from generating indirect strings in DWARF2 debug information, for instance
108 if your target is stuck with an old version of GDB that is unable to
109 process them properly or uses VMS Debug. */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
115
116 /* ??? Poison these here until it can be done generically. They've been
117 totally replaced in this file; make sure it stays that way. */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
123
124 /* The size of the target's pointer type. */
125 #ifndef PTR_SIZE
126 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
127 #endif
128
129 /* Array of RTXes referenced by the debugging information, which therefore
130 must be kept around forever. */
131 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
132
133 /* A pointer to the base of a list of incomplete types which might be
134 completed at some later time. incomplete_types_list needs to be a
135 vec<tree, va_gc> *because we want to tell the garbage collector about
136 it. */
137 static GTY(()) vec<tree, va_gc> *incomplete_types;
138
139 /* A pointer to the base of a table of references to declaration
140 scopes. This table is a display which tracks the nesting
141 of declaration scopes at the current scope and containing
142 scopes. This table is used to find the proper place to
143 define type declaration DIE's. */
144 static GTY(()) vec<tree, va_gc> *decl_scope_table;
145
146 /* Pointers to various DWARF2 sections. */
147 static GTY(()) section *debug_info_section;
148 static GTY(()) section *debug_skeleton_info_section;
149 static GTY(()) section *debug_abbrev_section;
150 static GTY(()) section *debug_skeleton_abbrev_section;
151 static GTY(()) section *debug_aranges_section;
152 static GTY(()) section *debug_addr_section;
153 static GTY(()) section *debug_macinfo_section;
154 static GTY(()) section *debug_line_section;
155 static GTY(()) section *debug_skeleton_line_section;
156 static GTY(()) section *debug_loc_section;
157 static GTY(()) section *debug_pubnames_section;
158 static GTY(()) section *debug_pubtypes_section;
159 static GTY(()) section *debug_str_section;
160 static GTY(()) section *debug_str_offsets_section;
161 static GTY(()) section *debug_ranges_section;
162 static GTY(()) section *debug_frame_section;
163
164 /* Maximum size (in bytes) of an artificially generated label. */
165 #define MAX_ARTIFICIAL_LABEL_BYTES 30
166
167 /* According to the (draft) DWARF 3 specification, the initial length
168 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
169 bytes are 0xffffffff, followed by the length stored in the next 8
170 bytes.
171
172 However, the SGI/MIPS ABI uses an initial length which is equal to
173 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
174
175 #ifndef DWARF_INITIAL_LENGTH_SIZE
176 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
177 #endif
178
179 /* Round SIZE up to the nearest BOUNDARY. */
180 #define DWARF_ROUND(SIZE,BOUNDARY) \
181 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
182
183 /* CIE identifier. */
184 #if HOST_BITS_PER_WIDE_INT >= 64
185 #define DWARF_CIE_ID \
186 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
187 #else
188 #define DWARF_CIE_ID DW_CIE_ID
189 #endif
190
191
192 /* A vector for a table that contains frame description
193 information for each routine. */
194 #define NOT_INDEXED (-1U)
195 #define NO_INDEX_ASSIGNED (-2U)
196
197 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
198
199 struct GTY(()) indirect_string_node {
200 const char *str;
201 unsigned int refcount;
202 enum dwarf_form form;
203 char *label;
204 unsigned int index;
205 };
206
207 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
208
209 static GTY(()) int dw2_string_counter;
210
211 /* True if the compilation unit places functions in more than one section. */
212 static GTY(()) bool have_multiple_function_sections = false;
213
214 /* Whether the default text and cold text sections have been used at all. */
215
216 static GTY(()) bool text_section_used = false;
217 static GTY(()) bool cold_text_section_used = false;
218
219 /* The default cold text section. */
220 static GTY(()) section *cold_text_section;
221
222 /* Forward declarations for functions defined in this file. */
223
224 static char *stripattributes (const char *);
225 static void output_call_frame_info (int);
226 static void dwarf2out_note_section_used (void);
227
228 /* Personality decl of current unit. Used only when assembler does not support
229 personality CFI. */
230 static GTY(()) rtx current_unit_personality;
231
232 /* Data and reference forms for relocatable data. */
233 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
234 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
235
236 #ifndef DEBUG_FRAME_SECTION
237 #define DEBUG_FRAME_SECTION ".debug_frame"
238 #endif
239
240 #ifndef FUNC_BEGIN_LABEL
241 #define FUNC_BEGIN_LABEL "LFB"
242 #endif
243
244 #ifndef FUNC_END_LABEL
245 #define FUNC_END_LABEL "LFE"
246 #endif
247
248 #ifndef PROLOGUE_END_LABEL
249 #define PROLOGUE_END_LABEL "LPE"
250 #endif
251
252 #ifndef EPILOGUE_BEGIN_LABEL
253 #define EPILOGUE_BEGIN_LABEL "LEB"
254 #endif
255
256 #ifndef FRAME_BEGIN_LABEL
257 #define FRAME_BEGIN_LABEL "Lframe"
258 #endif
259 #define CIE_AFTER_SIZE_LABEL "LSCIE"
260 #define CIE_END_LABEL "LECIE"
261 #define FDE_LABEL "LSFDE"
262 #define FDE_AFTER_SIZE_LABEL "LASFDE"
263 #define FDE_END_LABEL "LEFDE"
264 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
265 #define LINE_NUMBER_END_LABEL "LELT"
266 #define LN_PROLOG_AS_LABEL "LASLTP"
267 #define LN_PROLOG_END_LABEL "LELTP"
268 #define DIE_LABEL_PREFIX "DW"
269 \f
270 /* Match the base name of a file to the base name of a compilation unit. */
271
272 static int
273 matches_main_base (const char *path)
274 {
275 /* Cache the last query. */
276 static const char *last_path = NULL;
277 static int last_match = 0;
278 if (path != last_path)
279 {
280 const char *base;
281 int length = base_of_path (path, &base);
282 last_path = path;
283 last_match = (length == main_input_baselength
284 && memcmp (base, main_input_basename, length) == 0);
285 }
286 return last_match;
287 }
288
289 #ifdef DEBUG_DEBUG_STRUCT
290
291 static int
292 dump_struct_debug (tree type, enum debug_info_usage usage,
293 enum debug_struct_file criterion, int generic,
294 int matches, int result)
295 {
296 /* Find the type name. */
297 tree type_decl = TYPE_STUB_DECL (type);
298 tree t = type_decl;
299 const char *name = 0;
300 if (TREE_CODE (t) == TYPE_DECL)
301 t = DECL_NAME (t);
302 if (t)
303 name = IDENTIFIER_POINTER (t);
304
305 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
306 criterion,
307 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
308 matches ? "bas" : "hdr",
309 generic ? "gen" : "ord",
310 usage == DINFO_USAGE_DFN ? ";" :
311 usage == DINFO_USAGE_DIR_USE ? "." : "*",
312 result,
313 (void*) type_decl, name);
314 return result;
315 }
316 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
317 dump_struct_debug (type, usage, criterion, generic, matches, result)
318
319 #else
320
321 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
322 (result)
323
324 #endif
325
326 static bool
327 should_emit_struct_debug (tree type, enum debug_info_usage usage)
328 {
329 enum debug_struct_file criterion;
330 tree type_decl;
331 bool generic = lang_hooks.types.generic_p (type);
332
333 if (generic)
334 criterion = debug_struct_generic[usage];
335 else
336 criterion = debug_struct_ordinary[usage];
337
338 if (criterion == DINFO_STRUCT_FILE_NONE)
339 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
340 if (criterion == DINFO_STRUCT_FILE_ANY)
341 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
342
343 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
344
345 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
346 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
347
348 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
349 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
350 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
351 }
352 \f
353 /* Return a pointer to a copy of the section string name S with all
354 attributes stripped off, and an asterisk prepended (for assemble_name). */
355
356 static inline char *
357 stripattributes (const char *s)
358 {
359 char *stripped = XNEWVEC (char, strlen (s) + 2);
360 char *p = stripped;
361
362 *p++ = '*';
363
364 while (*s && *s != ',')
365 *p++ = *s++;
366
367 *p = '\0';
368 return stripped;
369 }
370
371 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
372 switch to the data section instead, and write out a synthetic start label
373 for collect2 the first time around. */
374
375 static void
376 switch_to_eh_frame_section (bool back)
377 {
378 tree label;
379
380 #ifdef EH_FRAME_SECTION_NAME
381 if (eh_frame_section == 0)
382 {
383 int flags;
384
385 if (EH_TABLES_CAN_BE_READ_ONLY)
386 {
387 int fde_encoding;
388 int per_encoding;
389 int lsda_encoding;
390
391 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
392 /*global=*/0);
393 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
394 /*global=*/1);
395 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
396 /*global=*/0);
397 flags = ((! flag_pic
398 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
399 && (fde_encoding & 0x70) != DW_EH_PE_aligned
400 && (per_encoding & 0x70) != DW_EH_PE_absptr
401 && (per_encoding & 0x70) != DW_EH_PE_aligned
402 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
403 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
404 ? 0 : SECTION_WRITE);
405 }
406 else
407 flags = SECTION_WRITE;
408 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
409 }
410 #endif /* EH_FRAME_SECTION_NAME */
411
412 if (eh_frame_section)
413 switch_to_section (eh_frame_section);
414 else
415 {
416 /* We have no special eh_frame section. Put the information in
417 the data section and emit special labels to guide collect2. */
418 switch_to_section (data_section);
419
420 if (!back)
421 {
422 label = get_file_function_name ("F");
423 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
424 targetm.asm_out.globalize_label (asm_out_file,
425 IDENTIFIER_POINTER (label));
426 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
427 }
428 }
429 }
430
431 /* Switch [BACK] to the eh or debug frame table section, depending on
432 FOR_EH. */
433
434 static void
435 switch_to_frame_table_section (int for_eh, bool back)
436 {
437 if (for_eh)
438 switch_to_eh_frame_section (back);
439 else
440 {
441 if (!debug_frame_section)
442 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
443 SECTION_DEBUG, NULL);
444 switch_to_section (debug_frame_section);
445 }
446 }
447
448 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
449
450 enum dw_cfi_oprnd_type
451 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
452 {
453 switch (cfi)
454 {
455 case DW_CFA_nop:
456 case DW_CFA_GNU_window_save:
457 case DW_CFA_remember_state:
458 case DW_CFA_restore_state:
459 return dw_cfi_oprnd_unused;
460
461 case DW_CFA_set_loc:
462 case DW_CFA_advance_loc1:
463 case DW_CFA_advance_loc2:
464 case DW_CFA_advance_loc4:
465 case DW_CFA_MIPS_advance_loc8:
466 return dw_cfi_oprnd_addr;
467
468 case DW_CFA_offset:
469 case DW_CFA_offset_extended:
470 case DW_CFA_def_cfa:
471 case DW_CFA_offset_extended_sf:
472 case DW_CFA_def_cfa_sf:
473 case DW_CFA_restore:
474 case DW_CFA_restore_extended:
475 case DW_CFA_undefined:
476 case DW_CFA_same_value:
477 case DW_CFA_def_cfa_register:
478 case DW_CFA_register:
479 case DW_CFA_expression:
480 return dw_cfi_oprnd_reg_num;
481
482 case DW_CFA_def_cfa_offset:
483 case DW_CFA_GNU_args_size:
484 case DW_CFA_def_cfa_offset_sf:
485 return dw_cfi_oprnd_offset;
486
487 case DW_CFA_def_cfa_expression:
488 return dw_cfi_oprnd_loc;
489
490 default:
491 gcc_unreachable ();
492 }
493 }
494
495 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
496
497 enum dw_cfi_oprnd_type
498 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
499 {
500 switch (cfi)
501 {
502 case DW_CFA_def_cfa:
503 case DW_CFA_def_cfa_sf:
504 case DW_CFA_offset:
505 case DW_CFA_offset_extended_sf:
506 case DW_CFA_offset_extended:
507 return dw_cfi_oprnd_offset;
508
509 case DW_CFA_register:
510 return dw_cfi_oprnd_reg_num;
511
512 case DW_CFA_expression:
513 return dw_cfi_oprnd_loc;
514
515 default:
516 return dw_cfi_oprnd_unused;
517 }
518 }
519
520 /* Output one FDE. */
521
522 static void
523 output_fde (dw_fde_ref fde, bool for_eh, bool second,
524 char *section_start_label, int fde_encoding, char *augmentation,
525 bool any_lsda_needed, int lsda_encoding)
526 {
527 const char *begin, *end;
528 static unsigned int j;
529 char l1[20], l2[20];
530
531 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
532 /* empty */ 0);
533 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
534 for_eh + j);
535 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
536 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
537 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
538 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
539 " indicating 64-bit DWARF extension");
540 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
541 "FDE Length");
542 ASM_OUTPUT_LABEL (asm_out_file, l1);
543
544 if (for_eh)
545 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
546 else
547 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
548 debug_frame_section, "FDE CIE offset");
549
550 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
551 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
552
553 if (for_eh)
554 {
555 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
556 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
557 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
558 "FDE initial location");
559 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
560 end, begin, "FDE address range");
561 }
562 else
563 {
564 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
565 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
566 }
567
568 if (augmentation[0])
569 {
570 if (any_lsda_needed)
571 {
572 int size = size_of_encoded_value (lsda_encoding);
573
574 if (lsda_encoding == DW_EH_PE_aligned)
575 {
576 int offset = ( 4 /* Length */
577 + 4 /* CIE offset */
578 + 2 * size_of_encoded_value (fde_encoding)
579 + 1 /* Augmentation size */ );
580 int pad = -offset & (PTR_SIZE - 1);
581
582 size += pad;
583 gcc_assert (size_of_uleb128 (size) == 1);
584 }
585
586 dw2_asm_output_data_uleb128 (size, "Augmentation size");
587
588 if (fde->uses_eh_lsda)
589 {
590 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
591 fde->funcdef_number);
592 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
593 gen_rtx_SYMBOL_REF (Pmode, l1),
594 false,
595 "Language Specific Data Area");
596 }
597 else
598 {
599 if (lsda_encoding == DW_EH_PE_aligned)
600 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
601 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
602 "Language Specific Data Area (none)");
603 }
604 }
605 else
606 dw2_asm_output_data_uleb128 (0, "Augmentation size");
607 }
608
609 /* Loop through the Call Frame Instructions associated with this FDE. */
610 fde->dw_fde_current_label = begin;
611 {
612 size_t from, until, i;
613
614 from = 0;
615 until = vec_safe_length (fde->dw_fde_cfi);
616
617 if (fde->dw_fde_second_begin == NULL)
618 ;
619 else if (!second)
620 until = fde->dw_fde_switch_cfi_index;
621 else
622 from = fde->dw_fde_switch_cfi_index;
623
624 for (i = from; i < until; i++)
625 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
626 }
627
628 /* If we are to emit a ref/link from function bodies to their frame tables,
629 do it now. This is typically performed to make sure that tables
630 associated with functions are dragged with them and not discarded in
631 garbage collecting links. We need to do this on a per function basis to
632 cope with -ffunction-sections. */
633
634 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
635 /* Switch to the function section, emit the ref to the tables, and
636 switch *back* into the table section. */
637 switch_to_section (function_section (fde->decl));
638 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
639 switch_to_frame_table_section (for_eh, true);
640 #endif
641
642 /* Pad the FDE out to an address sized boundary. */
643 ASM_OUTPUT_ALIGN (asm_out_file,
644 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
645 ASM_OUTPUT_LABEL (asm_out_file, l2);
646
647 j += 2;
648 }
649
650 /* Return true if frame description entry FDE is needed for EH. */
651
652 static bool
653 fde_needed_for_eh_p (dw_fde_ref fde)
654 {
655 if (flag_asynchronous_unwind_tables)
656 return true;
657
658 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
659 return true;
660
661 if (fde->uses_eh_lsda)
662 return true;
663
664 /* If exceptions are enabled, we have collected nothrow info. */
665 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
666 return false;
667
668 return true;
669 }
670
671 /* Output the call frame information used to record information
672 that relates to calculating the frame pointer, and records the
673 location of saved registers. */
674
675 static void
676 output_call_frame_info (int for_eh)
677 {
678 unsigned int i;
679 dw_fde_ref fde;
680 dw_cfi_ref cfi;
681 char l1[20], l2[20], section_start_label[20];
682 bool any_lsda_needed = false;
683 char augmentation[6];
684 int augmentation_size;
685 int fde_encoding = DW_EH_PE_absptr;
686 int per_encoding = DW_EH_PE_absptr;
687 int lsda_encoding = DW_EH_PE_absptr;
688 int return_reg;
689 rtx personality = NULL;
690 int dw_cie_version;
691
692 /* Don't emit a CIE if there won't be any FDEs. */
693 if (!fde_vec)
694 return;
695
696 /* Nothing to do if the assembler's doing it all. */
697 if (dwarf2out_do_cfi_asm ())
698 return;
699
700 /* If we don't have any functions we'll want to unwind out of, don't emit
701 any EH unwind information. If we make FDEs linkonce, we may have to
702 emit an empty label for an FDE that wouldn't otherwise be emitted. We
703 want to avoid having an FDE kept around when the function it refers to
704 is discarded. Example where this matters: a primary function template
705 in C++ requires EH information, an explicit specialization doesn't. */
706 if (for_eh)
707 {
708 bool any_eh_needed = false;
709
710 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
711 {
712 if (fde->uses_eh_lsda)
713 any_eh_needed = any_lsda_needed = true;
714 else if (fde_needed_for_eh_p (fde))
715 any_eh_needed = true;
716 else if (TARGET_USES_WEAK_UNWIND_INFO)
717 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
718 }
719
720 if (!any_eh_needed)
721 return;
722 }
723
724 /* We're going to be generating comments, so turn on app. */
725 if (flag_debug_asm)
726 app_enable ();
727
728 /* Switch to the proper frame section, first time. */
729 switch_to_frame_table_section (for_eh, false);
730
731 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
732 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
733
734 /* Output the CIE. */
735 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
736 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
737 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
738 dw2_asm_output_data (4, 0xffffffff,
739 "Initial length escape value indicating 64-bit DWARF extension");
740 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
741 "Length of Common Information Entry");
742 ASM_OUTPUT_LABEL (asm_out_file, l1);
743
744 /* Now that the CIE pointer is PC-relative for EH,
745 use 0 to identify the CIE. */
746 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
747 (for_eh ? 0 : DWARF_CIE_ID),
748 "CIE Identifier Tag");
749
750 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
751 use CIE version 1, unless that would produce incorrect results
752 due to overflowing the return register column. */
753 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
754 dw_cie_version = 1;
755 if (return_reg >= 256 || dwarf_version > 2)
756 dw_cie_version = 3;
757 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
758
759 augmentation[0] = 0;
760 augmentation_size = 0;
761
762 personality = current_unit_personality;
763 if (for_eh)
764 {
765 char *p;
766
767 /* Augmentation:
768 z Indicates that a uleb128 is present to size the
769 augmentation section.
770 L Indicates the encoding (and thus presence) of
771 an LSDA pointer in the FDE augmentation.
772 R Indicates a non-default pointer encoding for
773 FDE code pointers.
774 P Indicates the presence of an encoding + language
775 personality routine in the CIE augmentation. */
776
777 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
778 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
779 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
780
781 p = augmentation + 1;
782 if (personality)
783 {
784 *p++ = 'P';
785 augmentation_size += 1 + size_of_encoded_value (per_encoding);
786 assemble_external_libcall (personality);
787 }
788 if (any_lsda_needed)
789 {
790 *p++ = 'L';
791 augmentation_size += 1;
792 }
793 if (fde_encoding != DW_EH_PE_absptr)
794 {
795 *p++ = 'R';
796 augmentation_size += 1;
797 }
798 if (p > augmentation + 1)
799 {
800 augmentation[0] = 'z';
801 *p = '\0';
802 }
803
804 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
805 if (personality && per_encoding == DW_EH_PE_aligned)
806 {
807 int offset = ( 4 /* Length */
808 + 4 /* CIE Id */
809 + 1 /* CIE version */
810 + strlen (augmentation) + 1 /* Augmentation */
811 + size_of_uleb128 (1) /* Code alignment */
812 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
813 + 1 /* RA column */
814 + 1 /* Augmentation size */
815 + 1 /* Personality encoding */ );
816 int pad = -offset & (PTR_SIZE - 1);
817
818 augmentation_size += pad;
819
820 /* Augmentations should be small, so there's scarce need to
821 iterate for a solution. Die if we exceed one uleb128 byte. */
822 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
823 }
824 }
825
826 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
827 if (dw_cie_version >= 4)
828 {
829 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
830 dw2_asm_output_data (1, 0, "CIE Segment Size");
831 }
832 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
833 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
834 "CIE Data Alignment Factor");
835
836 if (dw_cie_version == 1)
837 dw2_asm_output_data (1, return_reg, "CIE RA Column");
838 else
839 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
840
841 if (augmentation[0])
842 {
843 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
844 if (personality)
845 {
846 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
847 eh_data_format_name (per_encoding));
848 dw2_asm_output_encoded_addr_rtx (per_encoding,
849 personality,
850 true, NULL);
851 }
852
853 if (any_lsda_needed)
854 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
855 eh_data_format_name (lsda_encoding));
856
857 if (fde_encoding != DW_EH_PE_absptr)
858 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
859 eh_data_format_name (fde_encoding));
860 }
861
862 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
863 output_cfi (cfi, NULL, for_eh);
864
865 /* Pad the CIE out to an address sized boundary. */
866 ASM_OUTPUT_ALIGN (asm_out_file,
867 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
868 ASM_OUTPUT_LABEL (asm_out_file, l2);
869
870 /* Loop through all of the FDE's. */
871 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
872 {
873 unsigned int k;
874
875 /* Don't emit EH unwind info for leaf functions that don't need it. */
876 if (for_eh && !fde_needed_for_eh_p (fde))
877 continue;
878
879 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
880 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
881 augmentation, any_lsda_needed, lsda_encoding);
882 }
883
884 if (for_eh && targetm.terminate_dw2_eh_frame_info)
885 dw2_asm_output_data (4, 0, "End of Table");
886
887 /* Turn off app to make assembly quicker. */
888 if (flag_debug_asm)
889 app_disable ();
890 }
891
892 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
893
894 static void
895 dwarf2out_do_cfi_startproc (bool second)
896 {
897 int enc;
898 rtx ref;
899 rtx personality = get_personality_function (current_function_decl);
900
901 fprintf (asm_out_file, "\t.cfi_startproc\n");
902
903 if (personality)
904 {
905 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
906 ref = personality;
907
908 /* ??? The GAS support isn't entirely consistent. We have to
909 handle indirect support ourselves, but PC-relative is done
910 in the assembler. Further, the assembler can't handle any
911 of the weirder relocation types. */
912 if (enc & DW_EH_PE_indirect)
913 ref = dw2_force_const_mem (ref, true);
914
915 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
916 output_addr_const (asm_out_file, ref);
917 fputc ('\n', asm_out_file);
918 }
919
920 if (crtl->uses_eh_lsda)
921 {
922 char lab[20];
923
924 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
925 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
926 current_function_funcdef_no);
927 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
928 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
929
930 if (enc & DW_EH_PE_indirect)
931 ref = dw2_force_const_mem (ref, true);
932
933 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
934 output_addr_const (asm_out_file, ref);
935 fputc ('\n', asm_out_file);
936 }
937 }
938
939 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
940 this allocation may be done before pass_final. */
941
942 dw_fde_ref
943 dwarf2out_alloc_current_fde (void)
944 {
945 dw_fde_ref fde;
946
947 fde = ggc_alloc_cleared_dw_fde_node ();
948 fde->decl = current_function_decl;
949 fde->funcdef_number = current_function_funcdef_no;
950 fde->fde_index = vec_safe_length (fde_vec);
951 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
952 fde->uses_eh_lsda = crtl->uses_eh_lsda;
953 fde->nothrow = crtl->nothrow;
954 fde->drap_reg = INVALID_REGNUM;
955 fde->vdrap_reg = INVALID_REGNUM;
956
957 /* Record the FDE associated with this function. */
958 cfun->fde = fde;
959 vec_safe_push (fde_vec, fde);
960
961 return fde;
962 }
963
964 /* Output a marker (i.e. a label) for the beginning of a function, before
965 the prologue. */
966
967 void
968 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
969 const char *file ATTRIBUTE_UNUSED)
970 {
971 char label[MAX_ARTIFICIAL_LABEL_BYTES];
972 char * dup_label;
973 dw_fde_ref fde;
974 section *fnsec;
975 bool do_frame;
976
977 current_function_func_begin_label = NULL;
978
979 do_frame = dwarf2out_do_frame ();
980
981 /* ??? current_function_func_begin_label is also used by except.c for
982 call-site information. We must emit this label if it might be used. */
983 if (!do_frame
984 && (!flag_exceptions
985 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
986 return;
987
988 fnsec = function_section (current_function_decl);
989 switch_to_section (fnsec);
990 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
991 current_function_funcdef_no);
992 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
993 current_function_funcdef_no);
994 dup_label = xstrdup (label);
995 current_function_func_begin_label = dup_label;
996
997 /* We can elide the fde allocation if we're not emitting debug info. */
998 if (!do_frame)
999 return;
1000
1001 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1002 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1003 would include pass_dwarf2_frame. If we've not created the FDE yet,
1004 do so now. */
1005 fde = cfun->fde;
1006 if (fde == NULL)
1007 fde = dwarf2out_alloc_current_fde ();
1008
1009 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1010 fde->dw_fde_begin = dup_label;
1011 fde->dw_fde_current_label = dup_label;
1012 fde->in_std_section = (fnsec == text_section
1013 || (cold_text_section && fnsec == cold_text_section));
1014
1015 /* We only want to output line number information for the genuine dwarf2
1016 prologue case, not the eh frame case. */
1017 #ifdef DWARF2_DEBUGGING_INFO
1018 if (file)
1019 dwarf2out_source_line (line, file, 0, true);
1020 #endif
1021
1022 if (dwarf2out_do_cfi_asm ())
1023 dwarf2out_do_cfi_startproc (false);
1024 else
1025 {
1026 rtx personality = get_personality_function (current_function_decl);
1027 if (!current_unit_personality)
1028 current_unit_personality = personality;
1029
1030 /* We cannot keep a current personality per function as without CFI
1031 asm, at the point where we emit the CFI data, there is no current
1032 function anymore. */
1033 if (personality && current_unit_personality != personality)
1034 sorry ("multiple EH personalities are supported only with assemblers "
1035 "supporting .cfi_personality directive");
1036 }
1037 }
1038
1039 /* Output a marker (i.e. a label) for the end of the generated code
1040 for a function prologue. This gets called *after* the prologue code has
1041 been generated. */
1042
1043 void
1044 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1045 const char *file ATTRIBUTE_UNUSED)
1046 {
1047 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1048
1049 /* Output a label to mark the endpoint of the code generated for this
1050 function. */
1051 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1052 current_function_funcdef_no);
1053 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1054 current_function_funcdef_no);
1055 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1056 }
1057
1058 /* Output a marker (i.e. a label) for the beginning of the generated code
1059 for a function epilogue. This gets called *before* the prologue code has
1060 been generated. */
1061
1062 void
1063 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1064 const char *file ATTRIBUTE_UNUSED)
1065 {
1066 dw_fde_ref fde = cfun->fde;
1067 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1068
1069 if (fde->dw_fde_vms_begin_epilogue)
1070 return;
1071
1072 /* Output a label to mark the endpoint of the code generated for this
1073 function. */
1074 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1075 current_function_funcdef_no);
1076 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1077 current_function_funcdef_no);
1078 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1079 }
1080
1081 /* Output a marker (i.e. a label) for the absolute end of the generated code
1082 for a function definition. This gets called *after* the epilogue code has
1083 been generated. */
1084
1085 void
1086 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1087 const char *file ATTRIBUTE_UNUSED)
1088 {
1089 dw_fde_ref fde;
1090 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1091
1092 last_var_location_insn = NULL_RTX;
1093 cached_next_real_insn = NULL_RTX;
1094
1095 if (dwarf2out_do_cfi_asm ())
1096 fprintf (asm_out_file, "\t.cfi_endproc\n");
1097
1098 /* Output a label to mark the endpoint of the code generated for this
1099 function. */
1100 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1101 current_function_funcdef_no);
1102 ASM_OUTPUT_LABEL (asm_out_file, label);
1103 fde = cfun->fde;
1104 gcc_assert (fde != NULL);
1105 if (fde->dw_fde_second_begin == NULL)
1106 fde->dw_fde_end = xstrdup (label);
1107 }
1108
1109 void
1110 dwarf2out_frame_finish (void)
1111 {
1112 /* Output call frame information. */
1113 if (targetm.debug_unwind_info () == UI_DWARF2)
1114 output_call_frame_info (0);
1115
1116 /* Output another copy for the unwinder. */
1117 if ((flag_unwind_tables || flag_exceptions)
1118 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1119 output_call_frame_info (1);
1120 }
1121
1122 /* Note that the current function section is being used for code. */
1123
1124 static void
1125 dwarf2out_note_section_used (void)
1126 {
1127 section *sec = current_function_section ();
1128 if (sec == text_section)
1129 text_section_used = true;
1130 else if (sec == cold_text_section)
1131 cold_text_section_used = true;
1132 }
1133
1134 static void var_location_switch_text_section (void);
1135 static void set_cur_line_info_table (section *);
1136
1137 void
1138 dwarf2out_switch_text_section (void)
1139 {
1140 section *sect;
1141 dw_fde_ref fde = cfun->fde;
1142
1143 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1144
1145 if (!in_cold_section_p)
1146 {
1147 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1148 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1149 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1150 }
1151 else
1152 {
1153 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1154 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1155 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1156 }
1157 have_multiple_function_sections = true;
1158
1159 /* There is no need to mark used sections when not debugging. */
1160 if (cold_text_section != NULL)
1161 dwarf2out_note_section_used ();
1162
1163 if (dwarf2out_do_cfi_asm ())
1164 fprintf (asm_out_file, "\t.cfi_endproc\n");
1165
1166 /* Now do the real section switch. */
1167 sect = current_function_section ();
1168 switch_to_section (sect);
1169
1170 fde->second_in_std_section
1171 = (sect == text_section
1172 || (cold_text_section && sect == cold_text_section));
1173
1174 if (dwarf2out_do_cfi_asm ())
1175 dwarf2out_do_cfi_startproc (true);
1176
1177 var_location_switch_text_section ();
1178
1179 if (cold_text_section != NULL)
1180 set_cur_line_info_table (sect);
1181 }
1182 \f
1183 /* And now, the subset of the debugging information support code necessary
1184 for emitting location expressions. */
1185
1186 /* Data about a single source file. */
1187 struct GTY(()) dwarf_file_data {
1188 const char * filename;
1189 int emitted_number;
1190 };
1191
1192 typedef struct GTY(()) deferred_locations_struct
1193 {
1194 tree variable;
1195 dw_die_ref die;
1196 } deferred_locations;
1197
1198
1199 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1200
1201
1202 /* Describe an entry into the .debug_addr section. */
1203
1204 enum ate_kind {
1205 ate_kind_rtx,
1206 ate_kind_rtx_dtprel,
1207 ate_kind_label
1208 };
1209
1210 typedef struct GTY(()) addr_table_entry_struct {
1211 enum ate_kind kind;
1212 unsigned int refcount;
1213 unsigned int index;
1214 union addr_table_entry_struct_union
1215 {
1216 rtx GTY ((tag ("0"))) rtl;
1217 char * GTY ((tag ("1"))) label;
1218 }
1219 GTY ((desc ("%1.kind"))) addr;
1220 }
1221 addr_table_entry;
1222
1223 /* Location lists are ranges + location descriptions for that range,
1224 so you can track variables that are in different places over
1225 their entire life. */
1226 typedef struct GTY(()) dw_loc_list_struct {
1227 dw_loc_list_ref dw_loc_next;
1228 const char *begin; /* Label and addr_entry for start of range */
1229 addr_table_entry *begin_entry;
1230 const char *end; /* Label for end of range */
1231 char *ll_symbol; /* Label for beginning of location list.
1232 Only on head of list */
1233 const char *section; /* Section this loclist is relative to */
1234 dw_loc_descr_ref expr;
1235 hashval_t hash;
1236 /* True if all addresses in this and subsequent lists are known to be
1237 resolved. */
1238 bool resolved_addr;
1239 /* True if this list has been replaced by dw_loc_next. */
1240 bool replaced;
1241 bool emitted;
1242 /* True if the range should be emitted even if begin and end
1243 are the same. */
1244 bool force;
1245 } dw_loc_list_node;
1246
1247 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1248
1249 /* Convert a DWARF stack opcode into its string name. */
1250
1251 static const char *
1252 dwarf_stack_op_name (unsigned int op)
1253 {
1254 const char *name = get_DW_OP_name (op);
1255
1256 if (name != NULL)
1257 return name;
1258
1259 return "OP_<unknown>";
1260 }
1261
1262 /* Return a pointer to a newly allocated location description. Location
1263 descriptions are simple expression terms that can be strung
1264 together to form more complicated location (address) descriptions. */
1265
1266 static inline dw_loc_descr_ref
1267 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1268 unsigned HOST_WIDE_INT oprnd2)
1269 {
1270 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1271
1272 descr->dw_loc_opc = op;
1273 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1274 descr->dw_loc_oprnd1.val_entry = NULL;
1275 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1276 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1277 descr->dw_loc_oprnd2.val_entry = NULL;
1278 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1279
1280 return descr;
1281 }
1282
1283 /* Return a pointer to a newly allocated location description for
1284 REG and OFFSET. */
1285
1286 static inline dw_loc_descr_ref
1287 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1288 {
1289 if (reg <= 31)
1290 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1291 offset, 0);
1292 else
1293 return new_loc_descr (DW_OP_bregx, reg, offset);
1294 }
1295
1296 /* Add a location description term to a location description expression. */
1297
1298 static inline void
1299 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1300 {
1301 dw_loc_descr_ref *d;
1302
1303 /* Find the end of the chain. */
1304 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1305 ;
1306
1307 *d = descr;
1308 }
1309
1310 /* Compare two location operands for exact equality. */
1311
1312 static bool
1313 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1314 {
1315 if (a->val_class != b->val_class)
1316 return false;
1317 switch (a->val_class)
1318 {
1319 case dw_val_class_none:
1320 return true;
1321 case dw_val_class_addr:
1322 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1323
1324 case dw_val_class_offset:
1325 case dw_val_class_unsigned_const:
1326 case dw_val_class_const:
1327 case dw_val_class_range_list:
1328 case dw_val_class_lineptr:
1329 case dw_val_class_macptr:
1330 /* These are all HOST_WIDE_INT, signed or unsigned. */
1331 return a->v.val_unsigned == b->v.val_unsigned;
1332
1333 case dw_val_class_loc:
1334 return a->v.val_loc == b->v.val_loc;
1335 case dw_val_class_loc_list:
1336 return a->v.val_loc_list == b->v.val_loc_list;
1337 case dw_val_class_die_ref:
1338 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1339 case dw_val_class_fde_ref:
1340 return a->v.val_fde_index == b->v.val_fde_index;
1341 case dw_val_class_lbl_id:
1342 case dw_val_class_high_pc:
1343 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1344 case dw_val_class_str:
1345 return a->v.val_str == b->v.val_str;
1346 case dw_val_class_flag:
1347 return a->v.val_flag == b->v.val_flag;
1348 case dw_val_class_file:
1349 return a->v.val_file == b->v.val_file;
1350 case dw_val_class_decl_ref:
1351 return a->v.val_decl_ref == b->v.val_decl_ref;
1352
1353 case dw_val_class_const_double:
1354 return (a->v.val_double.high == b->v.val_double.high
1355 && a->v.val_double.low == b->v.val_double.low);
1356
1357 case dw_val_class_vec:
1358 {
1359 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1360 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1361
1362 return (a_len == b_len
1363 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1364 }
1365
1366 case dw_val_class_data8:
1367 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1368
1369 case dw_val_class_vms_delta:
1370 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1371 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1372 }
1373 gcc_unreachable ();
1374 }
1375
1376 /* Compare two location atoms for exact equality. */
1377
1378 static bool
1379 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1380 {
1381 if (a->dw_loc_opc != b->dw_loc_opc)
1382 return false;
1383
1384 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1385 address size, but since we always allocate cleared storage it
1386 should be zero for other types of locations. */
1387 if (a->dtprel != b->dtprel)
1388 return false;
1389
1390 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1391 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1392 }
1393
1394 /* Compare two complete location expressions for exact equality. */
1395
1396 bool
1397 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1398 {
1399 while (1)
1400 {
1401 if (a == b)
1402 return true;
1403 if (a == NULL || b == NULL)
1404 return false;
1405 if (!loc_descr_equal_p_1 (a, b))
1406 return false;
1407
1408 a = a->dw_loc_next;
1409 b = b->dw_loc_next;
1410 }
1411 }
1412
1413
1414 /* Add a constant OFFSET to a location expression. */
1415
1416 static void
1417 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1418 {
1419 dw_loc_descr_ref loc;
1420 HOST_WIDE_INT *p;
1421
1422 gcc_assert (*list_head != NULL);
1423
1424 if (!offset)
1425 return;
1426
1427 /* Find the end of the chain. */
1428 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1429 ;
1430
1431 p = NULL;
1432 if (loc->dw_loc_opc == DW_OP_fbreg
1433 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1434 p = &loc->dw_loc_oprnd1.v.val_int;
1435 else if (loc->dw_loc_opc == DW_OP_bregx)
1436 p = &loc->dw_loc_oprnd2.v.val_int;
1437
1438 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1439 offset. Don't optimize if an signed integer overflow would happen. */
1440 if (p != NULL
1441 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1442 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1443 *p += offset;
1444
1445 else if (offset > 0)
1446 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1447
1448 else
1449 {
1450 loc->dw_loc_next = int_loc_descriptor (-offset);
1451 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1452 }
1453 }
1454
1455 /* Add a constant OFFSET to a location list. */
1456
1457 static void
1458 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1459 {
1460 dw_loc_list_ref d;
1461 for (d = list_head; d != NULL; d = d->dw_loc_next)
1462 loc_descr_plus_const (&d->expr, offset);
1463 }
1464
1465 #define DWARF_REF_SIZE \
1466 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1467
1468 static unsigned long int get_base_type_offset (dw_die_ref);
1469
1470 /* Return the size of a location descriptor. */
1471
1472 static unsigned long
1473 size_of_loc_descr (dw_loc_descr_ref loc)
1474 {
1475 unsigned long size = 1;
1476
1477 switch (loc->dw_loc_opc)
1478 {
1479 case DW_OP_addr:
1480 size += DWARF2_ADDR_SIZE;
1481 break;
1482 case DW_OP_GNU_addr_index:
1483 case DW_OP_GNU_const_index:
1484 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1485 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1486 break;
1487 case DW_OP_const1u:
1488 case DW_OP_const1s:
1489 size += 1;
1490 break;
1491 case DW_OP_const2u:
1492 case DW_OP_const2s:
1493 size += 2;
1494 break;
1495 case DW_OP_const4u:
1496 case DW_OP_const4s:
1497 size += 4;
1498 break;
1499 case DW_OP_const8u:
1500 case DW_OP_const8s:
1501 size += 8;
1502 break;
1503 case DW_OP_constu:
1504 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1505 break;
1506 case DW_OP_consts:
1507 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1508 break;
1509 case DW_OP_pick:
1510 size += 1;
1511 break;
1512 case DW_OP_plus_uconst:
1513 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1514 break;
1515 case DW_OP_skip:
1516 case DW_OP_bra:
1517 size += 2;
1518 break;
1519 case DW_OP_breg0:
1520 case DW_OP_breg1:
1521 case DW_OP_breg2:
1522 case DW_OP_breg3:
1523 case DW_OP_breg4:
1524 case DW_OP_breg5:
1525 case DW_OP_breg6:
1526 case DW_OP_breg7:
1527 case DW_OP_breg8:
1528 case DW_OP_breg9:
1529 case DW_OP_breg10:
1530 case DW_OP_breg11:
1531 case DW_OP_breg12:
1532 case DW_OP_breg13:
1533 case DW_OP_breg14:
1534 case DW_OP_breg15:
1535 case DW_OP_breg16:
1536 case DW_OP_breg17:
1537 case DW_OP_breg18:
1538 case DW_OP_breg19:
1539 case DW_OP_breg20:
1540 case DW_OP_breg21:
1541 case DW_OP_breg22:
1542 case DW_OP_breg23:
1543 case DW_OP_breg24:
1544 case DW_OP_breg25:
1545 case DW_OP_breg26:
1546 case DW_OP_breg27:
1547 case DW_OP_breg28:
1548 case DW_OP_breg29:
1549 case DW_OP_breg30:
1550 case DW_OP_breg31:
1551 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1552 break;
1553 case DW_OP_regx:
1554 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1555 break;
1556 case DW_OP_fbreg:
1557 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1558 break;
1559 case DW_OP_bregx:
1560 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1561 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1562 break;
1563 case DW_OP_piece:
1564 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1565 break;
1566 case DW_OP_bit_piece:
1567 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1568 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1569 break;
1570 case DW_OP_deref_size:
1571 case DW_OP_xderef_size:
1572 size += 1;
1573 break;
1574 case DW_OP_call2:
1575 size += 2;
1576 break;
1577 case DW_OP_call4:
1578 size += 4;
1579 break;
1580 case DW_OP_call_ref:
1581 size += DWARF_REF_SIZE;
1582 break;
1583 case DW_OP_implicit_value:
1584 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1585 + loc->dw_loc_oprnd1.v.val_unsigned;
1586 break;
1587 case DW_OP_GNU_implicit_pointer:
1588 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1589 break;
1590 case DW_OP_GNU_entry_value:
1591 {
1592 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1593 size += size_of_uleb128 (op_size) + op_size;
1594 break;
1595 }
1596 case DW_OP_GNU_const_type:
1597 {
1598 unsigned long o
1599 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1600 size += size_of_uleb128 (o) + 1;
1601 switch (loc->dw_loc_oprnd2.val_class)
1602 {
1603 case dw_val_class_vec:
1604 size += loc->dw_loc_oprnd2.v.val_vec.length
1605 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1606 break;
1607 case dw_val_class_const:
1608 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1609 break;
1610 case dw_val_class_const_double:
1611 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1612 break;
1613 default:
1614 gcc_unreachable ();
1615 }
1616 break;
1617 }
1618 case DW_OP_GNU_regval_type:
1619 {
1620 unsigned long o
1621 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1622 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1623 + size_of_uleb128 (o);
1624 }
1625 break;
1626 case DW_OP_GNU_deref_type:
1627 {
1628 unsigned long o
1629 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1630 size += 1 + size_of_uleb128 (o);
1631 }
1632 break;
1633 case DW_OP_GNU_convert:
1634 case DW_OP_GNU_reinterpret:
1635 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1636 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1637 else
1638 {
1639 unsigned long o
1640 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1641 size += size_of_uleb128 (o);
1642 }
1643 break;
1644 case DW_OP_GNU_parameter_ref:
1645 size += 4;
1646 break;
1647 default:
1648 break;
1649 }
1650
1651 return size;
1652 }
1653
1654 /* Return the size of a series of location descriptors. */
1655
1656 unsigned long
1657 size_of_locs (dw_loc_descr_ref loc)
1658 {
1659 dw_loc_descr_ref l;
1660 unsigned long size;
1661
1662 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1663 field, to avoid writing to a PCH file. */
1664 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1665 {
1666 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1667 break;
1668 size += size_of_loc_descr (l);
1669 }
1670 if (! l)
1671 return size;
1672
1673 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1674 {
1675 l->dw_loc_addr = size;
1676 size += size_of_loc_descr (l);
1677 }
1678
1679 return size;
1680 }
1681
1682 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1683 static void get_ref_die_offset_label (char *, dw_die_ref);
1684 static unsigned long int get_ref_die_offset (dw_die_ref);
1685
1686 /* Output location description stack opcode's operands (if any).
1687 The for_eh_or_skip parameter controls whether register numbers are
1688 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1689 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1690 info). This should be suppressed for the cases that have not been converted
1691 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1692
1693 static void
1694 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1695 {
1696 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1697 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1698
1699 switch (loc->dw_loc_opc)
1700 {
1701 #ifdef DWARF2_DEBUGGING_INFO
1702 case DW_OP_const2u:
1703 case DW_OP_const2s:
1704 dw2_asm_output_data (2, val1->v.val_int, NULL);
1705 break;
1706 case DW_OP_const4u:
1707 if (loc->dtprel)
1708 {
1709 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1710 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1711 val1->v.val_addr);
1712 fputc ('\n', asm_out_file);
1713 break;
1714 }
1715 /* FALLTHRU */
1716 case DW_OP_const4s:
1717 dw2_asm_output_data (4, val1->v.val_int, NULL);
1718 break;
1719 case DW_OP_const8u:
1720 if (loc->dtprel)
1721 {
1722 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1723 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1724 val1->v.val_addr);
1725 fputc ('\n', asm_out_file);
1726 break;
1727 }
1728 /* FALLTHRU */
1729 case DW_OP_const8s:
1730 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1731 dw2_asm_output_data (8, val1->v.val_int, NULL);
1732 break;
1733 case DW_OP_skip:
1734 case DW_OP_bra:
1735 {
1736 int offset;
1737
1738 gcc_assert (val1->val_class == dw_val_class_loc);
1739 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1740
1741 dw2_asm_output_data (2, offset, NULL);
1742 }
1743 break;
1744 case DW_OP_implicit_value:
1745 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1746 switch (val2->val_class)
1747 {
1748 case dw_val_class_const:
1749 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1750 break;
1751 case dw_val_class_vec:
1752 {
1753 unsigned int elt_size = val2->v.val_vec.elt_size;
1754 unsigned int len = val2->v.val_vec.length;
1755 unsigned int i;
1756 unsigned char *p;
1757
1758 if (elt_size > sizeof (HOST_WIDE_INT))
1759 {
1760 elt_size /= 2;
1761 len *= 2;
1762 }
1763 for (i = 0, p = val2->v.val_vec.array;
1764 i < len;
1765 i++, p += elt_size)
1766 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1767 "fp or vector constant word %u", i);
1768 }
1769 break;
1770 case dw_val_class_const_double:
1771 {
1772 unsigned HOST_WIDE_INT first, second;
1773
1774 if (WORDS_BIG_ENDIAN)
1775 {
1776 first = val2->v.val_double.high;
1777 second = val2->v.val_double.low;
1778 }
1779 else
1780 {
1781 first = val2->v.val_double.low;
1782 second = val2->v.val_double.high;
1783 }
1784 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1785 first, NULL);
1786 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1787 second, NULL);
1788 }
1789 break;
1790 case dw_val_class_addr:
1791 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1792 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1793 break;
1794 default:
1795 gcc_unreachable ();
1796 }
1797 break;
1798 #else
1799 case DW_OP_const2u:
1800 case DW_OP_const2s:
1801 case DW_OP_const4u:
1802 case DW_OP_const4s:
1803 case DW_OP_const8u:
1804 case DW_OP_const8s:
1805 case DW_OP_skip:
1806 case DW_OP_bra:
1807 case DW_OP_implicit_value:
1808 /* We currently don't make any attempt to make sure these are
1809 aligned properly like we do for the main unwind info, so
1810 don't support emitting things larger than a byte if we're
1811 only doing unwinding. */
1812 gcc_unreachable ();
1813 #endif
1814 case DW_OP_const1u:
1815 case DW_OP_const1s:
1816 dw2_asm_output_data (1, val1->v.val_int, NULL);
1817 break;
1818 case DW_OP_constu:
1819 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1820 break;
1821 case DW_OP_consts:
1822 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1823 break;
1824 case DW_OP_pick:
1825 dw2_asm_output_data (1, val1->v.val_int, NULL);
1826 break;
1827 case DW_OP_plus_uconst:
1828 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1829 break;
1830 case DW_OP_breg0:
1831 case DW_OP_breg1:
1832 case DW_OP_breg2:
1833 case DW_OP_breg3:
1834 case DW_OP_breg4:
1835 case DW_OP_breg5:
1836 case DW_OP_breg6:
1837 case DW_OP_breg7:
1838 case DW_OP_breg8:
1839 case DW_OP_breg9:
1840 case DW_OP_breg10:
1841 case DW_OP_breg11:
1842 case DW_OP_breg12:
1843 case DW_OP_breg13:
1844 case DW_OP_breg14:
1845 case DW_OP_breg15:
1846 case DW_OP_breg16:
1847 case DW_OP_breg17:
1848 case DW_OP_breg18:
1849 case DW_OP_breg19:
1850 case DW_OP_breg20:
1851 case DW_OP_breg21:
1852 case DW_OP_breg22:
1853 case DW_OP_breg23:
1854 case DW_OP_breg24:
1855 case DW_OP_breg25:
1856 case DW_OP_breg26:
1857 case DW_OP_breg27:
1858 case DW_OP_breg28:
1859 case DW_OP_breg29:
1860 case DW_OP_breg30:
1861 case DW_OP_breg31:
1862 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1863 break;
1864 case DW_OP_regx:
1865 {
1866 unsigned r = val1->v.val_unsigned;
1867 if (for_eh_or_skip >= 0)
1868 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1869 gcc_assert (size_of_uleb128 (r)
1870 == size_of_uleb128 (val1->v.val_unsigned));
1871 dw2_asm_output_data_uleb128 (r, NULL);
1872 }
1873 break;
1874 case DW_OP_fbreg:
1875 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1876 break;
1877 case DW_OP_bregx:
1878 {
1879 unsigned r = val1->v.val_unsigned;
1880 if (for_eh_or_skip >= 0)
1881 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1882 gcc_assert (size_of_uleb128 (r)
1883 == size_of_uleb128 (val1->v.val_unsigned));
1884 dw2_asm_output_data_uleb128 (r, NULL);
1885 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1886 }
1887 break;
1888 case DW_OP_piece:
1889 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1890 break;
1891 case DW_OP_bit_piece:
1892 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1893 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1894 break;
1895 case DW_OP_deref_size:
1896 case DW_OP_xderef_size:
1897 dw2_asm_output_data (1, val1->v.val_int, NULL);
1898 break;
1899
1900 case DW_OP_addr:
1901 if (loc->dtprel)
1902 {
1903 if (targetm.asm_out.output_dwarf_dtprel)
1904 {
1905 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1906 DWARF2_ADDR_SIZE,
1907 val1->v.val_addr);
1908 fputc ('\n', asm_out_file);
1909 }
1910 else
1911 gcc_unreachable ();
1912 }
1913 else
1914 {
1915 #ifdef DWARF2_DEBUGGING_INFO
1916 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1917 #else
1918 gcc_unreachable ();
1919 #endif
1920 }
1921 break;
1922
1923 case DW_OP_GNU_addr_index:
1924 case DW_OP_GNU_const_index:
1925 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1926 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1927 "(index into .debug_addr)");
1928 break;
1929
1930 case DW_OP_GNU_implicit_pointer:
1931 {
1932 char label[MAX_ARTIFICIAL_LABEL_BYTES
1933 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1934 gcc_assert (val1->val_class == dw_val_class_die_ref);
1935 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1936 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1937 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1938 }
1939 break;
1940
1941 case DW_OP_GNU_entry_value:
1942 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1943 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1944 break;
1945
1946 case DW_OP_GNU_const_type:
1947 {
1948 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1949 gcc_assert (o);
1950 dw2_asm_output_data_uleb128 (o, NULL);
1951 switch (val2->val_class)
1952 {
1953 case dw_val_class_const:
1954 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1955 dw2_asm_output_data (1, l, NULL);
1956 dw2_asm_output_data (l, val2->v.val_int, NULL);
1957 break;
1958 case dw_val_class_vec:
1959 {
1960 unsigned int elt_size = val2->v.val_vec.elt_size;
1961 unsigned int len = val2->v.val_vec.length;
1962 unsigned int i;
1963 unsigned char *p;
1964
1965 l = len * elt_size;
1966 dw2_asm_output_data (1, l, NULL);
1967 if (elt_size > sizeof (HOST_WIDE_INT))
1968 {
1969 elt_size /= 2;
1970 len *= 2;
1971 }
1972 for (i = 0, p = val2->v.val_vec.array;
1973 i < len;
1974 i++, p += elt_size)
1975 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1976 "fp or vector constant word %u", i);
1977 }
1978 break;
1979 case dw_val_class_const_double:
1980 {
1981 unsigned HOST_WIDE_INT first, second;
1982 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1983
1984 dw2_asm_output_data (1, 2 * l, NULL);
1985 if (WORDS_BIG_ENDIAN)
1986 {
1987 first = val2->v.val_double.high;
1988 second = val2->v.val_double.low;
1989 }
1990 else
1991 {
1992 first = val2->v.val_double.low;
1993 second = val2->v.val_double.high;
1994 }
1995 dw2_asm_output_data (l, first, NULL);
1996 dw2_asm_output_data (l, second, NULL);
1997 }
1998 break;
1999 default:
2000 gcc_unreachable ();
2001 }
2002 }
2003 break;
2004 case DW_OP_GNU_regval_type:
2005 {
2006 unsigned r = val1->v.val_unsigned;
2007 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2008 gcc_assert (o);
2009 if (for_eh_or_skip >= 0)
2010 {
2011 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2012 gcc_assert (size_of_uleb128 (r)
2013 == size_of_uleb128 (val1->v.val_unsigned));
2014 }
2015 dw2_asm_output_data_uleb128 (r, NULL);
2016 dw2_asm_output_data_uleb128 (o, NULL);
2017 }
2018 break;
2019 case DW_OP_GNU_deref_type:
2020 {
2021 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2022 gcc_assert (o);
2023 dw2_asm_output_data (1, val1->v.val_int, NULL);
2024 dw2_asm_output_data_uleb128 (o, NULL);
2025 }
2026 break;
2027 case DW_OP_GNU_convert:
2028 case DW_OP_GNU_reinterpret:
2029 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2030 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2031 else
2032 {
2033 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2034 gcc_assert (o);
2035 dw2_asm_output_data_uleb128 (o, NULL);
2036 }
2037 break;
2038
2039 case DW_OP_GNU_parameter_ref:
2040 {
2041 unsigned long o;
2042 gcc_assert (val1->val_class == dw_val_class_die_ref);
2043 o = get_ref_die_offset (val1->v.val_die_ref.die);
2044 dw2_asm_output_data (4, o, NULL);
2045 }
2046 break;
2047
2048 default:
2049 /* Other codes have no operands. */
2050 break;
2051 }
2052 }
2053
2054 /* Output a sequence of location operations.
2055 The for_eh_or_skip parameter controls whether register numbers are
2056 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2057 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2058 info). This should be suppressed for the cases that have not been converted
2059 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2060
2061 void
2062 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2063 {
2064 for (; loc != NULL; loc = loc->dw_loc_next)
2065 {
2066 enum dwarf_location_atom opc = loc->dw_loc_opc;
2067 /* Output the opcode. */
2068 if (for_eh_or_skip >= 0
2069 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2070 {
2071 unsigned r = (opc - DW_OP_breg0);
2072 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2073 gcc_assert (r <= 31);
2074 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2075 }
2076 else if (for_eh_or_skip >= 0
2077 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2078 {
2079 unsigned r = (opc - DW_OP_reg0);
2080 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2081 gcc_assert (r <= 31);
2082 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2083 }
2084
2085 dw2_asm_output_data (1, opc,
2086 "%s", dwarf_stack_op_name (opc));
2087
2088 /* Output the operand(s) (if any). */
2089 output_loc_operands (loc, for_eh_or_skip);
2090 }
2091 }
2092
2093 /* Output location description stack opcode's operands (if any).
2094 The output is single bytes on a line, suitable for .cfi_escape. */
2095
2096 static void
2097 output_loc_operands_raw (dw_loc_descr_ref loc)
2098 {
2099 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2100 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2101
2102 switch (loc->dw_loc_opc)
2103 {
2104 case DW_OP_addr:
2105 case DW_OP_GNU_addr_index:
2106 case DW_OP_GNU_const_index:
2107 case DW_OP_implicit_value:
2108 /* We cannot output addresses in .cfi_escape, only bytes. */
2109 gcc_unreachable ();
2110
2111 case DW_OP_const1u:
2112 case DW_OP_const1s:
2113 case DW_OP_pick:
2114 case DW_OP_deref_size:
2115 case DW_OP_xderef_size:
2116 fputc (',', asm_out_file);
2117 dw2_asm_output_data_raw (1, val1->v.val_int);
2118 break;
2119
2120 case DW_OP_const2u:
2121 case DW_OP_const2s:
2122 fputc (',', asm_out_file);
2123 dw2_asm_output_data_raw (2, val1->v.val_int);
2124 break;
2125
2126 case DW_OP_const4u:
2127 case DW_OP_const4s:
2128 fputc (',', asm_out_file);
2129 dw2_asm_output_data_raw (4, val1->v.val_int);
2130 break;
2131
2132 case DW_OP_const8u:
2133 case DW_OP_const8s:
2134 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2135 fputc (',', asm_out_file);
2136 dw2_asm_output_data_raw (8, val1->v.val_int);
2137 break;
2138
2139 case DW_OP_skip:
2140 case DW_OP_bra:
2141 {
2142 int offset;
2143
2144 gcc_assert (val1->val_class == dw_val_class_loc);
2145 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2146
2147 fputc (',', asm_out_file);
2148 dw2_asm_output_data_raw (2, offset);
2149 }
2150 break;
2151
2152 case DW_OP_regx:
2153 {
2154 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2155 gcc_assert (size_of_uleb128 (r)
2156 == size_of_uleb128 (val1->v.val_unsigned));
2157 fputc (',', asm_out_file);
2158 dw2_asm_output_data_uleb128_raw (r);
2159 }
2160 break;
2161
2162 case DW_OP_constu:
2163 case DW_OP_plus_uconst:
2164 case DW_OP_piece:
2165 fputc (',', asm_out_file);
2166 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2167 break;
2168
2169 case DW_OP_bit_piece:
2170 fputc (',', asm_out_file);
2171 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2172 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2173 break;
2174
2175 case DW_OP_consts:
2176 case DW_OP_breg0:
2177 case DW_OP_breg1:
2178 case DW_OP_breg2:
2179 case DW_OP_breg3:
2180 case DW_OP_breg4:
2181 case DW_OP_breg5:
2182 case DW_OP_breg6:
2183 case DW_OP_breg7:
2184 case DW_OP_breg8:
2185 case DW_OP_breg9:
2186 case DW_OP_breg10:
2187 case DW_OP_breg11:
2188 case DW_OP_breg12:
2189 case DW_OP_breg13:
2190 case DW_OP_breg14:
2191 case DW_OP_breg15:
2192 case DW_OP_breg16:
2193 case DW_OP_breg17:
2194 case DW_OP_breg18:
2195 case DW_OP_breg19:
2196 case DW_OP_breg20:
2197 case DW_OP_breg21:
2198 case DW_OP_breg22:
2199 case DW_OP_breg23:
2200 case DW_OP_breg24:
2201 case DW_OP_breg25:
2202 case DW_OP_breg26:
2203 case DW_OP_breg27:
2204 case DW_OP_breg28:
2205 case DW_OP_breg29:
2206 case DW_OP_breg30:
2207 case DW_OP_breg31:
2208 case DW_OP_fbreg:
2209 fputc (',', asm_out_file);
2210 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2211 break;
2212
2213 case DW_OP_bregx:
2214 {
2215 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2216 gcc_assert (size_of_uleb128 (r)
2217 == size_of_uleb128 (val1->v.val_unsigned));
2218 fputc (',', asm_out_file);
2219 dw2_asm_output_data_uleb128_raw (r);
2220 fputc (',', asm_out_file);
2221 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2222 }
2223 break;
2224
2225 case DW_OP_GNU_implicit_pointer:
2226 case DW_OP_GNU_entry_value:
2227 case DW_OP_GNU_const_type:
2228 case DW_OP_GNU_regval_type:
2229 case DW_OP_GNU_deref_type:
2230 case DW_OP_GNU_convert:
2231 case DW_OP_GNU_reinterpret:
2232 case DW_OP_GNU_parameter_ref:
2233 gcc_unreachable ();
2234 break;
2235
2236 default:
2237 /* Other codes have no operands. */
2238 break;
2239 }
2240 }
2241
2242 void
2243 output_loc_sequence_raw (dw_loc_descr_ref loc)
2244 {
2245 while (1)
2246 {
2247 enum dwarf_location_atom opc = loc->dw_loc_opc;
2248 /* Output the opcode. */
2249 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2250 {
2251 unsigned r = (opc - DW_OP_breg0);
2252 r = DWARF2_FRAME_REG_OUT (r, 1);
2253 gcc_assert (r <= 31);
2254 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2255 }
2256 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2257 {
2258 unsigned r = (opc - DW_OP_reg0);
2259 r = DWARF2_FRAME_REG_OUT (r, 1);
2260 gcc_assert (r <= 31);
2261 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2262 }
2263 /* Output the opcode. */
2264 fprintf (asm_out_file, "%#x", opc);
2265 output_loc_operands_raw (loc);
2266
2267 if (!loc->dw_loc_next)
2268 break;
2269 loc = loc->dw_loc_next;
2270
2271 fputc (',', asm_out_file);
2272 }
2273 }
2274
2275 /* This function builds a dwarf location descriptor sequence from a
2276 dw_cfa_location, adding the given OFFSET to the result of the
2277 expression. */
2278
2279 struct dw_loc_descr_struct *
2280 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2281 {
2282 struct dw_loc_descr_struct *head, *tmp;
2283
2284 offset += cfa->offset;
2285
2286 if (cfa->indirect)
2287 {
2288 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2289 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2290 head->dw_loc_oprnd1.val_entry = NULL;
2291 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2292 add_loc_descr (&head, tmp);
2293 if (offset != 0)
2294 {
2295 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2296 add_loc_descr (&head, tmp);
2297 }
2298 }
2299 else
2300 head = new_reg_loc_descr (cfa->reg, offset);
2301
2302 return head;
2303 }
2304
2305 /* This function builds a dwarf location descriptor sequence for
2306 the address at OFFSET from the CFA when stack is aligned to
2307 ALIGNMENT byte. */
2308
2309 struct dw_loc_descr_struct *
2310 build_cfa_aligned_loc (dw_cfa_location *cfa,
2311 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2312 {
2313 struct dw_loc_descr_struct *head;
2314 unsigned int dwarf_fp
2315 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2316
2317 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2318 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2319 {
2320 head = new_reg_loc_descr (dwarf_fp, 0);
2321 add_loc_descr (&head, int_loc_descriptor (alignment));
2322 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2323 loc_descr_plus_const (&head, offset);
2324 }
2325 else
2326 head = new_reg_loc_descr (dwarf_fp, offset);
2327 return head;
2328 }
2329 \f
2330 /* And now, the support for symbolic debugging information. */
2331
2332 /* .debug_str support. */
2333 static int output_indirect_string (void **, void *);
2334
2335 static void dwarf2out_init (const char *);
2336 static void dwarf2out_finish (const char *);
2337 static void dwarf2out_assembly_start (void);
2338 static void dwarf2out_define (unsigned int, const char *);
2339 static void dwarf2out_undef (unsigned int, const char *);
2340 static void dwarf2out_start_source_file (unsigned, const char *);
2341 static void dwarf2out_end_source_file (unsigned);
2342 static void dwarf2out_function_decl (tree);
2343 static void dwarf2out_begin_block (unsigned, unsigned);
2344 static void dwarf2out_end_block (unsigned, unsigned);
2345 static bool dwarf2out_ignore_block (const_tree);
2346 static void dwarf2out_global_decl (tree);
2347 static void dwarf2out_type_decl (tree, int);
2348 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2349 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2350 dw_die_ref);
2351 static void dwarf2out_abstract_function (tree);
2352 static void dwarf2out_var_location (rtx);
2353 static void dwarf2out_begin_function (tree);
2354 static void dwarf2out_end_function (unsigned int);
2355 static void dwarf2out_set_name (tree, tree);
2356
2357 /* The debug hooks structure. */
2358
2359 const struct gcc_debug_hooks dwarf2_debug_hooks =
2360 {
2361 dwarf2out_init,
2362 dwarf2out_finish,
2363 dwarf2out_assembly_start,
2364 dwarf2out_define,
2365 dwarf2out_undef,
2366 dwarf2out_start_source_file,
2367 dwarf2out_end_source_file,
2368 dwarf2out_begin_block,
2369 dwarf2out_end_block,
2370 dwarf2out_ignore_block,
2371 dwarf2out_source_line,
2372 dwarf2out_begin_prologue,
2373 #if VMS_DEBUGGING_INFO
2374 dwarf2out_vms_end_prologue,
2375 dwarf2out_vms_begin_epilogue,
2376 #else
2377 debug_nothing_int_charstar,
2378 debug_nothing_int_charstar,
2379 #endif
2380 dwarf2out_end_epilogue,
2381 dwarf2out_begin_function,
2382 dwarf2out_end_function, /* end_function */
2383 dwarf2out_function_decl, /* function_decl */
2384 dwarf2out_global_decl,
2385 dwarf2out_type_decl, /* type_decl */
2386 dwarf2out_imported_module_or_decl,
2387 debug_nothing_tree, /* deferred_inline_function */
2388 /* The DWARF 2 backend tries to reduce debugging bloat by not
2389 emitting the abstract description of inline functions until
2390 something tries to reference them. */
2391 dwarf2out_abstract_function, /* outlining_inline_function */
2392 debug_nothing_rtx, /* label */
2393 debug_nothing_int, /* handle_pch */
2394 dwarf2out_var_location,
2395 dwarf2out_switch_text_section,
2396 dwarf2out_set_name,
2397 1, /* start_end_main_source_file */
2398 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2399 };
2400 \f
2401 /* NOTE: In the comments in this file, many references are made to
2402 "Debugging Information Entries". This term is abbreviated as `DIE'
2403 throughout the remainder of this file. */
2404
2405 /* An internal representation of the DWARF output is built, and then
2406 walked to generate the DWARF debugging info. The walk of the internal
2407 representation is done after the entire program has been compiled.
2408 The types below are used to describe the internal representation. */
2409
2410 /* Whether to put type DIEs into their own section .debug_types instead
2411 of making them part of the .debug_info section. Only supported for
2412 Dwarf V4 or higher and the user didn't disable them through
2413 -fno-debug-types-section. It is more efficient to put them in a
2414 separate comdat sections since the linker will then be able to
2415 remove duplicates. But not all tools support .debug_types sections
2416 yet. */
2417
2418 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2419
2420 /* Various DIE's use offsets relative to the beginning of the
2421 .debug_info section to refer to each other. */
2422
2423 typedef long int dw_offset;
2424
2425 /* Define typedefs here to avoid circular dependencies. */
2426
2427 typedef struct dw_attr_struct *dw_attr_ref;
2428 typedef struct dw_line_info_struct *dw_line_info_ref;
2429 typedef struct pubname_struct *pubname_ref;
2430 typedef struct dw_ranges_struct *dw_ranges_ref;
2431 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2432 typedef struct comdat_type_struct *comdat_type_node_ref;
2433
2434 /* The entries in the line_info table more-or-less mirror the opcodes
2435 that are used in the real dwarf line table. Arrays of these entries
2436 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2437 supported. */
2438
2439 enum dw_line_info_opcode {
2440 /* Emit DW_LNE_set_address; the operand is the label index. */
2441 LI_set_address,
2442
2443 /* Emit a row to the matrix with the given line. This may be done
2444 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2445 special opcodes. */
2446 LI_set_line,
2447
2448 /* Emit a DW_LNS_set_file. */
2449 LI_set_file,
2450
2451 /* Emit a DW_LNS_set_column. */
2452 LI_set_column,
2453
2454 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2455 LI_negate_stmt,
2456
2457 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2458 LI_set_prologue_end,
2459 LI_set_epilogue_begin,
2460
2461 /* Emit a DW_LNE_set_discriminator. */
2462 LI_set_discriminator
2463 };
2464
2465 typedef struct GTY(()) dw_line_info_struct {
2466 enum dw_line_info_opcode opcode;
2467 unsigned int val;
2468 } dw_line_info_entry;
2469
2470
2471 typedef struct GTY(()) dw_line_info_table_struct {
2472 /* The label that marks the end of this section. */
2473 const char *end_label;
2474
2475 /* The values for the last row of the matrix, as collected in the table.
2476 These are used to minimize the changes to the next row. */
2477 unsigned int file_num;
2478 unsigned int line_num;
2479 unsigned int column_num;
2480 int discrim_num;
2481 bool is_stmt;
2482 bool in_use;
2483
2484 vec<dw_line_info_entry, va_gc> *entries;
2485 } dw_line_info_table;
2486
2487 typedef dw_line_info_table *dw_line_info_table_p;
2488
2489
2490 /* Each DIE attribute has a field specifying the attribute kind,
2491 a link to the next attribute in the chain, and an attribute value.
2492 Attributes are typically linked below the DIE they modify. */
2493
2494 typedef struct GTY(()) dw_attr_struct {
2495 enum dwarf_attribute dw_attr;
2496 dw_val_node dw_attr_val;
2497 }
2498 dw_attr_node;
2499
2500
2501 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2502 The children of each node form a circular list linked by
2503 die_sib. die_child points to the node *before* the "first" child node. */
2504
2505 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2506 union die_symbol_or_type_node
2507 {
2508 const char * GTY ((tag ("0"))) die_symbol;
2509 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2510 }
2511 GTY ((desc ("%0.comdat_type_p"))) die_id;
2512 vec<dw_attr_node, va_gc> *die_attr;
2513 dw_die_ref die_parent;
2514 dw_die_ref die_child;
2515 dw_die_ref die_sib;
2516 dw_die_ref die_definition; /* ref from a specification to its definition */
2517 dw_offset die_offset;
2518 unsigned long die_abbrev;
2519 int die_mark;
2520 unsigned int decl_id;
2521 enum dwarf_tag die_tag;
2522 /* Die is used and must not be pruned as unused. */
2523 BOOL_BITFIELD die_perennial_p : 1;
2524 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2525 /* Lots of spare bits. */
2526 }
2527 die_node;
2528
2529 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2530 #define FOR_EACH_CHILD(die, c, expr) do { \
2531 c = die->die_child; \
2532 if (c) do { \
2533 c = c->die_sib; \
2534 expr; \
2535 } while (c != die->die_child); \
2536 } while (0)
2537
2538 /* The pubname structure */
2539
2540 typedef struct GTY(()) pubname_struct {
2541 dw_die_ref die;
2542 const char *name;
2543 }
2544 pubname_entry;
2545
2546
2547 struct GTY(()) dw_ranges_struct {
2548 /* If this is positive, it's a block number, otherwise it's a
2549 bitwise-negated index into dw_ranges_by_label. */
2550 int num;
2551 };
2552
2553 /* A structure to hold a macinfo entry. */
2554
2555 typedef struct GTY(()) macinfo_struct {
2556 unsigned char code;
2557 unsigned HOST_WIDE_INT lineno;
2558 const char *info;
2559 }
2560 macinfo_entry;
2561
2562
2563 struct GTY(()) dw_ranges_by_label_struct {
2564 const char *begin;
2565 const char *end;
2566 };
2567
2568 /* The comdat type node structure. */
2569 typedef struct GTY(()) comdat_type_struct
2570 {
2571 dw_die_ref root_die;
2572 dw_die_ref type_die;
2573 dw_die_ref skeleton_die;
2574 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2575 struct comdat_type_struct *next;
2576 }
2577 comdat_type_node;
2578
2579 /* The limbo die list structure. */
2580 typedef struct GTY(()) limbo_die_struct {
2581 dw_die_ref die;
2582 tree created_for;
2583 struct limbo_die_struct *next;
2584 }
2585 limbo_die_node;
2586
2587 typedef struct skeleton_chain_struct
2588 {
2589 dw_die_ref old_die;
2590 dw_die_ref new_die;
2591 struct skeleton_chain_struct *parent;
2592 }
2593 skeleton_chain_node;
2594
2595 /* Define a macro which returns nonzero for a TYPE_DECL which was
2596 implicitly generated for a type.
2597
2598 Note that, unlike the C front-end (which generates a NULL named
2599 TYPE_DECL node for each complete tagged type, each array type,
2600 and each function type node created) the C++ front-end generates
2601 a _named_ TYPE_DECL node for each tagged type node created.
2602 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2603 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2604 front-end, but for each type, tagged or not. */
2605
2606 #define TYPE_DECL_IS_STUB(decl) \
2607 (DECL_NAME (decl) == NULL_TREE \
2608 || (DECL_ARTIFICIAL (decl) \
2609 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2610 /* This is necessary for stub decls that \
2611 appear in nested inline functions. */ \
2612 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2613 && (decl_ultimate_origin (decl) \
2614 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2615
2616 /* Information concerning the compilation unit's programming
2617 language, and compiler version. */
2618
2619 /* Fixed size portion of the DWARF compilation unit header. */
2620 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2621 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2622
2623 /* Fixed size portion of the DWARF comdat type unit header. */
2624 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2625 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2626 + DWARF_OFFSET_SIZE)
2627
2628 /* Fixed size portion of public names info. */
2629 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2630
2631 /* Fixed size portion of the address range info. */
2632 #define DWARF_ARANGES_HEADER_SIZE \
2633 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2634 DWARF2_ADDR_SIZE * 2) \
2635 - DWARF_INITIAL_LENGTH_SIZE)
2636
2637 /* Size of padding portion in the address range info. It must be
2638 aligned to twice the pointer size. */
2639 #define DWARF_ARANGES_PAD_SIZE \
2640 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2641 DWARF2_ADDR_SIZE * 2) \
2642 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2643
2644 /* Use assembler line directives if available. */
2645 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2646 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2647 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2648 #else
2649 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2650 #endif
2651 #endif
2652
2653 /* Minimum line offset in a special line info. opcode.
2654 This value was chosen to give a reasonable range of values. */
2655 #define DWARF_LINE_BASE -10
2656
2657 /* First special line opcode - leave room for the standard opcodes. */
2658 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2659
2660 /* Range of line offsets in a special line info. opcode. */
2661 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2662
2663 /* Flag that indicates the initial value of the is_stmt_start flag.
2664 In the present implementation, we do not mark any lines as
2665 the beginning of a source statement, because that information
2666 is not made available by the GCC front-end. */
2667 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2668
2669 /* Maximum number of operations per instruction bundle. */
2670 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2671 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2672 #endif
2673
2674 /* This location is used by calc_die_sizes() to keep track
2675 the offset of each DIE within the .debug_info section. */
2676 static unsigned long next_die_offset;
2677
2678 /* Record the root of the DIE's built for the current compilation unit. */
2679 static GTY(()) dw_die_ref single_comp_unit_die;
2680
2681 /* A list of type DIEs that have been separated into comdat sections. */
2682 static GTY(()) comdat_type_node *comdat_type_list;
2683
2684 /* A list of DIEs with a NULL parent waiting to be relocated. */
2685 static GTY(()) limbo_die_node *limbo_die_list;
2686
2687 /* A list of DIEs for which we may have to generate
2688 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2689 static GTY(()) limbo_die_node *deferred_asm_name;
2690
2691 /* Filenames referenced by this compilation unit. */
2692 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2693
2694 /* A hash table of references to DIE's that describe declarations.
2695 The key is a DECL_UID() which is a unique number identifying each decl. */
2696 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2697
2698 /* A hash table of references to DIE's that describe COMMON blocks.
2699 The key is DECL_UID() ^ die_parent. */
2700 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2701
2702 typedef struct GTY(()) die_arg_entry_struct {
2703 dw_die_ref die;
2704 tree arg;
2705 } die_arg_entry;
2706
2707
2708 /* Node of the variable location list. */
2709 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2710 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2711 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2712 in mode of the EXPR_LIST node and first EXPR_LIST operand
2713 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2714 location or NULL for padding. For larger bitsizes,
2715 mode is 0 and first operand is a CONCAT with bitsize
2716 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2717 NULL as second operand. */
2718 rtx GTY (()) loc;
2719 const char * GTY (()) label;
2720 struct var_loc_node * GTY (()) next;
2721 };
2722
2723 /* Variable location list. */
2724 struct GTY (()) var_loc_list_def {
2725 struct var_loc_node * GTY (()) first;
2726
2727 /* Pointer to the last but one or last element of the
2728 chained list. If the list is empty, both first and
2729 last are NULL, if the list contains just one node
2730 or the last node certainly is not redundant, it points
2731 to the last node, otherwise points to the last but one.
2732 Do not mark it for GC because it is marked through the chain. */
2733 struct var_loc_node * GTY ((skip ("%h"))) last;
2734
2735 /* Pointer to the last element before section switch,
2736 if NULL, either sections weren't switched or first
2737 is after section switch. */
2738 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2739
2740 /* DECL_UID of the variable decl. */
2741 unsigned int decl_id;
2742 };
2743 typedef struct var_loc_list_def var_loc_list;
2744
2745 /* Call argument location list. */
2746 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2747 rtx GTY (()) call_arg_loc_note;
2748 const char * GTY (()) label;
2749 tree GTY (()) block;
2750 bool tail_call_p;
2751 rtx GTY (()) symbol_ref;
2752 struct call_arg_loc_node * GTY (()) next;
2753 };
2754
2755
2756 /* Table of decl location linked lists. */
2757 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2758
2759 /* Head and tail of call_arg_loc chain. */
2760 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2761 static struct call_arg_loc_node *call_arg_loc_last;
2762
2763 /* Number of call sites in the current function. */
2764 static int call_site_count = -1;
2765 /* Number of tail call sites in the current function. */
2766 static int tail_call_site_count = -1;
2767
2768 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2769 DIEs. */
2770 static vec<dw_die_ref> block_map;
2771
2772 /* A cached location list. */
2773 struct GTY (()) cached_dw_loc_list_def {
2774 /* The DECL_UID of the decl that this entry describes. */
2775 unsigned int decl_id;
2776
2777 /* The cached location list. */
2778 dw_loc_list_ref loc_list;
2779 };
2780 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2781
2782 /* Table of cached location lists. */
2783 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2784
2785 /* A pointer to the base of a list of references to DIE's that
2786 are uniquely identified by their tag, presence/absence of
2787 children DIE's, and list of attribute/value pairs. */
2788 static GTY((length ("abbrev_die_table_allocated")))
2789 dw_die_ref *abbrev_die_table;
2790
2791 /* Number of elements currently allocated for abbrev_die_table. */
2792 static GTY(()) unsigned abbrev_die_table_allocated;
2793
2794 /* Number of elements in type_die_table currently in use. */
2795 static GTY(()) unsigned abbrev_die_table_in_use;
2796
2797 /* Size (in elements) of increments by which we may expand the
2798 abbrev_die_table. */
2799 #define ABBREV_DIE_TABLE_INCREMENT 256
2800
2801 /* A global counter for generating labels for line number data. */
2802 static unsigned int line_info_label_num;
2803
2804 /* The current table to which we should emit line number information
2805 for the current function. This will be set up at the beginning of
2806 assembly for the function. */
2807 static dw_line_info_table *cur_line_info_table;
2808
2809 /* The two default tables of line number info. */
2810 static GTY(()) dw_line_info_table *text_section_line_info;
2811 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2812
2813 /* The set of all non-default tables of line number info. */
2814 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2815
2816 /* A flag to tell pubnames/types export if there is an info section to
2817 refer to. */
2818 static bool info_section_emitted;
2819
2820 /* A pointer to the base of a table that contains a list of publicly
2821 accessible names. */
2822 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2823
2824 /* A pointer to the base of a table that contains a list of publicly
2825 accessible types. */
2826 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2827
2828 /* A pointer to the base of a table that contains a list of macro
2829 defines/undefines (and file start/end markers). */
2830 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2831
2832 /* True if .debug_macinfo or .debug_macros section is going to be
2833 emitted. */
2834 #define have_macinfo \
2835 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2836 && !macinfo_table->is_empty ())
2837
2838 /* Array of dies for which we should generate .debug_ranges info. */
2839 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2840
2841 /* Number of elements currently allocated for ranges_table. */
2842 static GTY(()) unsigned ranges_table_allocated;
2843
2844 /* Number of elements in ranges_table currently in use. */
2845 static GTY(()) unsigned ranges_table_in_use;
2846
2847 /* Array of pairs of labels referenced in ranges_table. */
2848 static GTY ((length ("ranges_by_label_allocated")))
2849 dw_ranges_by_label_ref ranges_by_label;
2850
2851 /* Number of elements currently allocated for ranges_by_label. */
2852 static GTY(()) unsigned ranges_by_label_allocated;
2853
2854 /* Number of elements in ranges_by_label currently in use. */
2855 static GTY(()) unsigned ranges_by_label_in_use;
2856
2857 /* Size (in elements) of increments by which we may expand the
2858 ranges_table. */
2859 #define RANGES_TABLE_INCREMENT 64
2860
2861 /* Whether we have location lists that need outputting */
2862 static GTY(()) bool have_location_lists;
2863
2864 /* Unique label counter. */
2865 static GTY(()) unsigned int loclabel_num;
2866
2867 /* Unique label counter for point-of-call tables. */
2868 static GTY(()) unsigned int poc_label_num;
2869
2870 /* Record whether the function being analyzed contains inlined functions. */
2871 static int current_function_has_inlines;
2872
2873 /* The last file entry emitted by maybe_emit_file(). */
2874 static GTY(()) struct dwarf_file_data * last_emitted_file;
2875
2876 /* Number of internal labels generated by gen_internal_sym(). */
2877 static GTY(()) int label_num;
2878
2879 /* Cached result of previous call to lookup_filename. */
2880 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2881
2882 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2883
2884 /* Instances of generic types for which we need to generate debug
2885 info that describe their generic parameters and arguments. That
2886 generation needs to happen once all types are properly laid out so
2887 we do it at the end of compilation. */
2888 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2889
2890 /* Offset from the "steady-state frame pointer" to the frame base,
2891 within the current function. */
2892 static HOST_WIDE_INT frame_pointer_fb_offset;
2893 static bool frame_pointer_fb_offset_valid;
2894
2895 static vec<dw_die_ref> base_types;
2896
2897 /* Forward declarations for functions defined in this file. */
2898
2899 static int is_pseudo_reg (const_rtx);
2900 static tree type_main_variant (tree);
2901 static int is_tagged_type (const_tree);
2902 static const char *dwarf_tag_name (unsigned);
2903 static const char *dwarf_attr_name (unsigned);
2904 static const char *dwarf_form_name (unsigned);
2905 static tree decl_ultimate_origin (const_tree);
2906 static tree decl_class_context (tree);
2907 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2908 static inline enum dw_val_class AT_class (dw_attr_ref);
2909 static inline unsigned int AT_index (dw_attr_ref);
2910 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2911 static inline unsigned AT_flag (dw_attr_ref);
2912 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2913 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2914 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2915 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2916 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2917 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2918 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2919 unsigned int, unsigned char *);
2920 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2921 static hashval_t debug_str_do_hash (const void *);
2922 static int debug_str_eq (const void *, const void *);
2923 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2924 static inline const char *AT_string (dw_attr_ref);
2925 static enum dwarf_form AT_string_form (dw_attr_ref);
2926 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2927 static void add_AT_specification (dw_die_ref, dw_die_ref);
2928 static inline dw_die_ref AT_ref (dw_attr_ref);
2929 static inline int AT_ref_external (dw_attr_ref);
2930 static inline void set_AT_ref_external (dw_attr_ref, int);
2931 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2932 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2933 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2934 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2935 dw_loc_list_ref);
2936 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2937 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2938 static void remove_addr_table_entry (addr_table_entry *);
2939 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2940 static inline rtx AT_addr (dw_attr_ref);
2941 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2942 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2943 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2944 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2945 unsigned HOST_WIDE_INT);
2946 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2947 unsigned long, bool);
2948 static inline const char *AT_lbl (dw_attr_ref);
2949 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2950 static const char *get_AT_low_pc (dw_die_ref);
2951 static const char *get_AT_hi_pc (dw_die_ref);
2952 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2953 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2954 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2955 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2956 static bool is_cxx (void);
2957 static bool is_fortran (void);
2958 static bool is_ada (void);
2959 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2960 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2961 static void add_child_die (dw_die_ref, dw_die_ref);
2962 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2963 static dw_die_ref lookup_type_die (tree);
2964 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2965 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2966 static void equate_type_number_to_die (tree, dw_die_ref);
2967 static hashval_t decl_die_table_hash (const void *);
2968 static int decl_die_table_eq (const void *, const void *);
2969 static dw_die_ref lookup_decl_die (tree);
2970 static hashval_t common_block_die_table_hash (const void *);
2971 static int common_block_die_table_eq (const void *, const void *);
2972 static hashval_t decl_loc_table_hash (const void *);
2973 static int decl_loc_table_eq (const void *, const void *);
2974 static var_loc_list *lookup_decl_loc (const_tree);
2975 static void equate_decl_number_to_die (tree, dw_die_ref);
2976 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
2977 static void print_spaces (FILE *);
2978 static void print_die (dw_die_ref, FILE *);
2979 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
2980 static dw_die_ref pop_compile_unit (dw_die_ref);
2981 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
2982 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
2983 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
2984 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
2985 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
2986 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
2987 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
2988 struct md5_ctx *, int *);
2989 struct checksum_attributes;
2990 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
2991 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
2992 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
2993 static void generate_type_signature (dw_die_ref, comdat_type_node *);
2994 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
2995 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
2996 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
2997 static int same_die_p (dw_die_ref, dw_die_ref, int *);
2998 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
2999 static void compute_section_prefix (dw_die_ref);
3000 static int is_type_die (dw_die_ref);
3001 static int is_comdat_die (dw_die_ref);
3002 static int is_symbol_die (dw_die_ref);
3003 static void assign_symbol_names (dw_die_ref);
3004 static void break_out_includes (dw_die_ref);
3005 static int is_declaration_die (dw_die_ref);
3006 static int should_move_die_to_comdat (dw_die_ref);
3007 static dw_die_ref clone_as_declaration (dw_die_ref);
3008 static dw_die_ref clone_die (dw_die_ref);
3009 static dw_die_ref clone_tree (dw_die_ref);
3010 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3011 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3012 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3013 static dw_die_ref generate_skeleton (dw_die_ref);
3014 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3015 dw_die_ref,
3016 dw_die_ref);
3017 static void break_out_comdat_types (dw_die_ref);
3018 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3019 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3020 static void copy_decls_for_unworthy_types (dw_die_ref);
3021
3022 static hashval_t htab_cu_hash (const void *);
3023 static int htab_cu_eq (const void *, const void *);
3024 static void htab_cu_del (void *);
3025 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3026 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3027 static void add_sibling_attributes (dw_die_ref);
3028 static void build_abbrev_table (dw_die_ref, htab_t);
3029 static void output_location_lists (dw_die_ref);
3030 static int constant_size (unsigned HOST_WIDE_INT);
3031 static unsigned long size_of_die (dw_die_ref);
3032 static void calc_die_sizes (dw_die_ref);
3033 static void calc_base_type_die_sizes (void);
3034 static void mark_dies (dw_die_ref);
3035 static void unmark_dies (dw_die_ref);
3036 static void unmark_all_dies (dw_die_ref);
3037 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3038 static unsigned long size_of_aranges (void);
3039 static enum dwarf_form value_format (dw_attr_ref);
3040 static void output_value_format (dw_attr_ref);
3041 static void output_abbrev_section (void);
3042 static void output_die_abbrevs (unsigned long, dw_die_ref);
3043 static void output_die_symbol (dw_die_ref);
3044 static void output_die (dw_die_ref);
3045 static void output_compilation_unit_header (void);
3046 static void output_comp_unit (dw_die_ref, int);
3047 static void output_comdat_type_unit (comdat_type_node *);
3048 static const char *dwarf2_name (tree, int);
3049 static void add_pubname (tree, dw_die_ref);
3050 static void add_enumerator_pubname (const char *, dw_die_ref);
3051 static void add_pubname_string (const char *, dw_die_ref);
3052 static void add_pubtype (tree, dw_die_ref);
3053 static void output_pubnames (vec<pubname_entry, va_gc> *);
3054 static void output_aranges (unsigned long);
3055 static unsigned int add_ranges_num (int);
3056 static unsigned int add_ranges (const_tree);
3057 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3058 bool *, bool);
3059 static void output_ranges (void);
3060 static dw_line_info_table *new_line_info_table (void);
3061 static void output_line_info (bool);
3062 static void output_file_names (void);
3063 static dw_die_ref base_type_die (tree);
3064 static int is_base_type (tree);
3065 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3066 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3067 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3068 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3069 static int type_is_enum (const_tree);
3070 static unsigned int dbx_reg_number (const_rtx);
3071 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3072 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3073 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3074 enum var_init_status);
3075 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3076 enum var_init_status);
3077 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3078 enum var_init_status);
3079 static int is_based_loc (const_rtx);
3080 static int resolve_one_addr (rtx *, void *);
3081 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3082 enum var_init_status);
3083 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3084 enum var_init_status);
3085 static dw_loc_list_ref loc_list_from_tree (tree, int);
3086 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3087 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3088 static tree field_type (const_tree);
3089 static unsigned int simple_type_align_in_bits (const_tree);
3090 static unsigned int simple_decl_align_in_bits (const_tree);
3091 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3092 static HOST_WIDE_INT field_byte_offset (const_tree);
3093 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3094 dw_loc_list_ref);
3095 static void add_data_member_location_attribute (dw_die_ref, tree);
3096 static bool add_const_value_attribute (dw_die_ref, rtx);
3097 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3098 static void insert_double (double_int, unsigned char *);
3099 static void insert_float (const_rtx, unsigned char *);
3100 static rtx rtl_for_decl_location (tree);
3101 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3102 enum dwarf_attribute);
3103 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3104 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3105 static void add_name_attribute (dw_die_ref, const char *);
3106 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3107 static void add_comp_dir_attribute (dw_die_ref);
3108 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3109 static void add_subscript_info (dw_die_ref, tree, bool);
3110 static void add_byte_size_attribute (dw_die_ref, tree);
3111 static void add_bit_offset_attribute (dw_die_ref, tree);
3112 static void add_bit_size_attribute (dw_die_ref, tree);
3113 static void add_prototyped_attribute (dw_die_ref, tree);
3114 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3115 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3116 static void add_src_coords_attributes (dw_die_ref, tree);
3117 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3118 static void push_decl_scope (tree);
3119 static void pop_decl_scope (void);
3120 static dw_die_ref scope_die_for (tree, dw_die_ref);
3121 static inline int local_scope_p (dw_die_ref);
3122 static inline int class_scope_p (dw_die_ref);
3123 static inline int class_or_namespace_scope_p (dw_die_ref);
3124 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3125 static void add_calling_convention_attribute (dw_die_ref, tree);
3126 static const char *type_tag (const_tree);
3127 static tree member_declared_type (const_tree);
3128 #if 0
3129 static const char *decl_start_label (tree);
3130 #endif
3131 static void gen_array_type_die (tree, dw_die_ref);
3132 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3133 #if 0
3134 static void gen_entry_point_die (tree, dw_die_ref);
3135 #endif
3136 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3137 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3138 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3139 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3140 static void gen_formal_types_die (tree, dw_die_ref);
3141 static void gen_subprogram_die (tree, dw_die_ref);
3142 static void gen_variable_die (tree, tree, dw_die_ref);
3143 static void gen_const_die (tree, dw_die_ref);
3144 static void gen_label_die (tree, dw_die_ref);
3145 static void gen_lexical_block_die (tree, dw_die_ref, int);
3146 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3147 static void gen_field_die (tree, dw_die_ref);
3148 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3149 static dw_die_ref gen_compile_unit_die (const char *);
3150 static void gen_inheritance_die (tree, tree, dw_die_ref);
3151 static void gen_member_die (tree, dw_die_ref);
3152 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3153 enum debug_info_usage);
3154 static void gen_subroutine_type_die (tree, dw_die_ref);
3155 static void gen_typedef_die (tree, dw_die_ref);
3156 static void gen_type_die (tree, dw_die_ref);
3157 static void gen_block_die (tree, dw_die_ref, int);
3158 static void decls_for_scope (tree, dw_die_ref, int);
3159 static inline int is_redundant_typedef (const_tree);
3160 static bool is_naming_typedef_decl (const_tree);
3161 static inline dw_die_ref get_context_die (tree);
3162 static void gen_namespace_die (tree, dw_die_ref);
3163 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3164 static dw_die_ref force_decl_die (tree);
3165 static dw_die_ref force_type_die (tree);
3166 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3167 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3168 static struct dwarf_file_data * lookup_filename (const char *);
3169 static void retry_incomplete_types (void);
3170 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3171 static void gen_generic_params_dies (tree);
3172 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3173 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3174 static void splice_child_die (dw_die_ref, dw_die_ref);
3175 static int file_info_cmp (const void *, const void *);
3176 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3177 const char *, const char *);
3178 static void output_loc_list (dw_loc_list_ref);
3179 static char *gen_internal_sym (const char *);
3180 static bool want_pubnames (void);
3181
3182 static void prune_unmark_dies (dw_die_ref);
3183 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3184 static void prune_unused_types_mark (dw_die_ref, int);
3185 static void prune_unused_types_walk (dw_die_ref);
3186 static void prune_unused_types_walk_attribs (dw_die_ref);
3187 static void prune_unused_types_prune (dw_die_ref);
3188 static void prune_unused_types (void);
3189 static int maybe_emit_file (struct dwarf_file_data *fd);
3190 static inline const char *AT_vms_delta1 (dw_attr_ref);
3191 static inline const char *AT_vms_delta2 (dw_attr_ref);
3192 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3193 const char *, const char *);
3194 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3195 static void gen_remaining_tmpl_value_param_die_attribute (void);
3196 static bool generic_type_p (tree);
3197 static void schedule_generic_params_dies_gen (tree t);
3198 static void gen_scheduled_generic_parms_dies (void);
3199
3200 /* enum for tracking thread-local variables whose address is really an offset
3201 relative to the TLS pointer, which will need link-time relocation, but will
3202 not need relocation by the DWARF consumer. */
3203
3204 enum dtprel_bool
3205 {
3206 dtprel_false = 0,
3207 dtprel_true = 1
3208 };
3209
3210 /* Return the operator to use for an address of a variable. For dtprel_true, we
3211 use DW_OP_const*. For regular variables, which need both link-time
3212 relocation and consumer-level relocation (e.g., to account for shared objects
3213 loaded at a random address), we use DW_OP_addr*. */
3214
3215 static inline enum dwarf_location_atom
3216 dw_addr_op (enum dtprel_bool dtprel)
3217 {
3218 if (dtprel == dtprel_true)
3219 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3220 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3221 else
3222 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3223 }
3224
3225 /* Return a pointer to a newly allocated address location description. If
3226 dwarf_split_debug_info is true, then record the address with the appropriate
3227 relocation. */
3228 static inline dw_loc_descr_ref
3229 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3230 {
3231 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3232
3233 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3234 ref->dw_loc_oprnd1.v.val_addr = addr;
3235 ref->dtprel = dtprel;
3236 if (dwarf_split_debug_info)
3237 ref->dw_loc_oprnd1.val_entry
3238 = add_addr_table_entry (addr,
3239 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3240 else
3241 ref->dw_loc_oprnd1.val_entry = NULL;
3242
3243 return ref;
3244 }
3245
3246 /* Section names used to hold DWARF debugging information. */
3247
3248 #ifndef DEBUG_INFO_SECTION
3249 #define DEBUG_INFO_SECTION ".debug_info"
3250 #endif
3251 #ifndef DEBUG_DWO_INFO_SECTION
3252 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3253 #endif
3254 #ifndef DEBUG_ABBREV_SECTION
3255 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3256 #endif
3257 #ifndef DEBUG_DWO_ABBREV_SECTION
3258 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3259 #endif
3260 #ifndef DEBUG_ARANGES_SECTION
3261 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3262 #endif
3263 #ifndef DEBUG_ADDR_SECTION
3264 #define DEBUG_ADDR_SECTION ".debug_addr"
3265 #endif
3266 #ifndef DEBUG_NORM_MACINFO_SECTION
3267 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3268 #endif
3269 #ifndef DEBUG_DWO_MACINFO_SECTION
3270 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3271 #endif
3272 #ifndef DEBUG_MACINFO_SECTION
3273 #define DEBUG_MACINFO_SECTION \
3274 (!dwarf_split_debug_info \
3275 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3276 #endif
3277 #ifndef DEBUG_NORM_MACRO_SECTION
3278 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3279 #endif
3280 #ifndef DEBUG_DWO_MACRO_SECTION
3281 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3282 #endif
3283 #ifndef DEBUG_MACRO_SECTION
3284 #define DEBUG_MACRO_SECTION \
3285 (!dwarf_split_debug_info \
3286 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3287 #endif
3288 #ifndef DEBUG_LINE_SECTION
3289 #define DEBUG_LINE_SECTION ".debug_line"
3290 #endif
3291 #ifndef DEBUG_DWO_LINE_SECTION
3292 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3293 #endif
3294 #ifndef DEBUG_LOC_SECTION
3295 #define DEBUG_LOC_SECTION ".debug_loc"
3296 #endif
3297 #ifndef DEBUG_DWO_LOC_SECTION
3298 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3299 #endif
3300 #ifndef DEBUG_PUBNAMES_SECTION
3301 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3302 #endif
3303 #ifndef DEBUG_PUBTYPES_SECTION
3304 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
3305 #endif
3306 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3307 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3308 #ifndef DEBUG_STR_OFFSETS_SECTION
3309 #define DEBUG_STR_OFFSETS_SECTION \
3310 (!dwarf_split_debug_info \
3311 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3312 #endif
3313 #define DEBUG_DWO_STR_SECTION ".debug_str.dwo"
3314 #define DEBUG_NORM_STR_SECTION ".debug_str"
3315 #ifndef DEBUG_STR_SECTION
3316 #define DEBUG_STR_SECTION \
3317 (!dwarf_split_debug_info ? (DEBUG_NORM_STR_SECTION) : (DEBUG_DWO_STR_SECTION))
3318 #endif
3319 #ifndef DEBUG_RANGES_SECTION
3320 #define DEBUG_RANGES_SECTION ".debug_ranges"
3321 #endif
3322
3323 /* Standard ELF section names for compiled code and data. */
3324 #ifndef TEXT_SECTION_NAME
3325 #define TEXT_SECTION_NAME ".text"
3326 #endif
3327
3328 /* Section flags for .debug_macinfo/.debug_macro section. */
3329 #define DEBUG_MACRO_SECTION_FLAGS \
3330 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3331
3332 /* Section flags for .debug_str section. */
3333 #define DEBUG_STR_SECTION_FLAGS \
3334 (dwarf_split_debug_info \
3335 ? SECTION_DEBUG | SECTION_EXCLUDE \
3336 : (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3337 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3338 : SECTION_DEBUG))
3339
3340 /* Labels we insert at beginning sections we can reference instead of
3341 the section names themselves. */
3342
3343 #ifndef TEXT_SECTION_LABEL
3344 #define TEXT_SECTION_LABEL "Ltext"
3345 #endif
3346 #ifndef COLD_TEXT_SECTION_LABEL
3347 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3348 #endif
3349 #ifndef DEBUG_LINE_SECTION_LABEL
3350 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3351 #endif
3352 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3353 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3354 #endif
3355 #ifndef DEBUG_INFO_SECTION_LABEL
3356 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3357 #endif
3358 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3359 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3360 #endif
3361 #ifndef DEBUG_ABBREV_SECTION_LABEL
3362 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3363 #endif
3364 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3365 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3366 #endif
3367 #ifndef DEBUG_ADDR_SECTION_LABEL
3368 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3369 #endif
3370 #ifndef DEBUG_LOC_SECTION_LABEL
3371 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3372 #endif
3373 #ifndef DEBUG_RANGES_SECTION_LABEL
3374 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3375 #endif
3376 #ifndef DEBUG_MACINFO_SECTION_LABEL
3377 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3378 #endif
3379 #ifndef DEBUG_MACRO_SECTION_LABEL
3380 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3381 #endif
3382 #define SKELETON_COMP_DIE_ABBREV 1
3383 #define SKELETON_TYPE_DIE_ABBREV 2
3384
3385 /* Definitions of defaults for formats and names of various special
3386 (artificial) labels which may be generated within this file (when the -g
3387 options is used and DWARF2_DEBUGGING_INFO is in effect.
3388 If necessary, these may be overridden from within the tm.h file, but
3389 typically, overriding these defaults is unnecessary. */
3390
3391 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3392 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3393 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3394 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3395 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3396 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3397 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3398 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3399 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3400 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3401 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3402 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3403 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3404 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3405
3406 #ifndef TEXT_END_LABEL
3407 #define TEXT_END_LABEL "Letext"
3408 #endif
3409 #ifndef COLD_END_LABEL
3410 #define COLD_END_LABEL "Letext_cold"
3411 #endif
3412 #ifndef BLOCK_BEGIN_LABEL
3413 #define BLOCK_BEGIN_LABEL "LBB"
3414 #endif
3415 #ifndef BLOCK_END_LABEL
3416 #define BLOCK_END_LABEL "LBE"
3417 #endif
3418 #ifndef LINE_CODE_LABEL
3419 #define LINE_CODE_LABEL "LM"
3420 #endif
3421
3422 \f
3423 /* Return the root of the DIE's built for the current compilation unit. */
3424 static dw_die_ref
3425 comp_unit_die (void)
3426 {
3427 if (!single_comp_unit_die)
3428 single_comp_unit_die = gen_compile_unit_die (NULL);
3429 return single_comp_unit_die;
3430 }
3431
3432 /* We allow a language front-end to designate a function that is to be
3433 called to "demangle" any name before it is put into a DIE. */
3434
3435 static const char *(*demangle_name_func) (const char *);
3436
3437 void
3438 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3439 {
3440 demangle_name_func = func;
3441 }
3442
3443 /* Test if rtl node points to a pseudo register. */
3444
3445 static inline int
3446 is_pseudo_reg (const_rtx rtl)
3447 {
3448 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3449 || (GET_CODE (rtl) == SUBREG
3450 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3451 }
3452
3453 /* Return a reference to a type, with its const and volatile qualifiers
3454 removed. */
3455
3456 static inline tree
3457 type_main_variant (tree type)
3458 {
3459 type = TYPE_MAIN_VARIANT (type);
3460
3461 /* ??? There really should be only one main variant among any group of
3462 variants of a given type (and all of the MAIN_VARIANT values for all
3463 members of the group should point to that one type) but sometimes the C
3464 front-end messes this up for array types, so we work around that bug
3465 here. */
3466 if (TREE_CODE (type) == ARRAY_TYPE)
3467 while (type != TYPE_MAIN_VARIANT (type))
3468 type = TYPE_MAIN_VARIANT (type);
3469
3470 return type;
3471 }
3472
3473 /* Return nonzero if the given type node represents a tagged type. */
3474
3475 static inline int
3476 is_tagged_type (const_tree type)
3477 {
3478 enum tree_code code = TREE_CODE (type);
3479
3480 return (code == RECORD_TYPE || code == UNION_TYPE
3481 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3482 }
3483
3484 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3485
3486 static void
3487 get_ref_die_offset_label (char *label, dw_die_ref ref)
3488 {
3489 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3490 }
3491
3492 /* Return die_offset of a DIE reference to a base type. */
3493
3494 static unsigned long int
3495 get_base_type_offset (dw_die_ref ref)
3496 {
3497 if (ref->die_offset)
3498 return ref->die_offset;
3499 if (comp_unit_die ()->die_abbrev)
3500 {
3501 calc_base_type_die_sizes ();
3502 gcc_assert (ref->die_offset);
3503 }
3504 return ref->die_offset;
3505 }
3506
3507 /* Return die_offset of a DIE reference other than base type. */
3508
3509 static unsigned long int
3510 get_ref_die_offset (dw_die_ref ref)
3511 {
3512 gcc_assert (ref->die_offset);
3513 return ref->die_offset;
3514 }
3515
3516 /* Convert a DIE tag into its string name. */
3517
3518 static const char *
3519 dwarf_tag_name (unsigned int tag)
3520 {
3521 const char *name = get_DW_TAG_name (tag);
3522
3523 if (name != NULL)
3524 return name;
3525
3526 return "DW_TAG_<unknown>";
3527 }
3528
3529 /* Convert a DWARF attribute code into its string name. */
3530
3531 static const char *
3532 dwarf_attr_name (unsigned int attr)
3533 {
3534 const char *name;
3535
3536 switch (attr)
3537 {
3538 #if VMS_DEBUGGING_INFO
3539 case DW_AT_HP_prologue:
3540 return "DW_AT_HP_prologue";
3541 #else
3542 case DW_AT_MIPS_loop_unroll_factor:
3543 return "DW_AT_MIPS_loop_unroll_factor";
3544 #endif
3545
3546 #if VMS_DEBUGGING_INFO
3547 case DW_AT_HP_epilogue:
3548 return "DW_AT_HP_epilogue";
3549 #else
3550 case DW_AT_MIPS_stride:
3551 return "DW_AT_MIPS_stride";
3552 #endif
3553 }
3554
3555 name = get_DW_AT_name (attr);
3556
3557 if (name != NULL)
3558 return name;
3559
3560 return "DW_AT_<unknown>";
3561 }
3562
3563 /* Convert a DWARF value form code into its string name. */
3564
3565 static const char *
3566 dwarf_form_name (unsigned int form)
3567 {
3568 const char *name = get_DW_FORM_name (form);
3569
3570 if (name != NULL)
3571 return name;
3572
3573 return "DW_FORM_<unknown>";
3574 }
3575 \f
3576 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3577 instance of an inlined instance of a decl which is local to an inline
3578 function, so we have to trace all of the way back through the origin chain
3579 to find out what sort of node actually served as the original seed for the
3580 given block. */
3581
3582 static tree
3583 decl_ultimate_origin (const_tree decl)
3584 {
3585 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3586 return NULL_TREE;
3587
3588 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3589 nodes in the function to point to themselves; ignore that if
3590 we're trying to output the abstract instance of this function. */
3591 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3592 return NULL_TREE;
3593
3594 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3595 most distant ancestor, this should never happen. */
3596 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3597
3598 return DECL_ABSTRACT_ORIGIN (decl);
3599 }
3600
3601 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3602 of a virtual function may refer to a base class, so we check the 'this'
3603 parameter. */
3604
3605 static tree
3606 decl_class_context (tree decl)
3607 {
3608 tree context = NULL_TREE;
3609
3610 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3611 context = DECL_CONTEXT (decl);
3612 else
3613 context = TYPE_MAIN_VARIANT
3614 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3615
3616 if (context && !TYPE_P (context))
3617 context = NULL_TREE;
3618
3619 return context;
3620 }
3621 \f
3622 /* Add an attribute/value pair to a DIE. */
3623
3624 static inline void
3625 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3626 {
3627 /* Maybe this should be an assert? */
3628 if (die == NULL)
3629 return;
3630
3631 vec_safe_reserve (die->die_attr, 1);
3632 vec_safe_push (die->die_attr, *attr);
3633 }
3634
3635 static inline enum dw_val_class
3636 AT_class (dw_attr_ref a)
3637 {
3638 return a->dw_attr_val.val_class;
3639 }
3640
3641 /* Return the index for any attribute that will be referenced with a
3642 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3643 are stored in dw_attr_val.v.val_str for reference counting
3644 pruning. */
3645
3646 static inline unsigned int
3647 AT_index (dw_attr_ref a)
3648 {
3649 if (AT_class (a) == dw_val_class_str)
3650 return a->dw_attr_val.v.val_str->index;
3651 else if (a->dw_attr_val.val_entry != NULL)
3652 return a->dw_attr_val.val_entry->index;
3653 return NOT_INDEXED;
3654 }
3655
3656 /* Add a flag value attribute to a DIE. */
3657
3658 static inline void
3659 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3660 {
3661 dw_attr_node attr;
3662
3663 attr.dw_attr = attr_kind;
3664 attr.dw_attr_val.val_class = dw_val_class_flag;
3665 attr.dw_attr_val.val_entry = NULL;
3666 attr.dw_attr_val.v.val_flag = flag;
3667 add_dwarf_attr (die, &attr);
3668 }
3669
3670 static inline unsigned
3671 AT_flag (dw_attr_ref a)
3672 {
3673 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3674 return a->dw_attr_val.v.val_flag;
3675 }
3676
3677 /* Add a signed integer attribute value to a DIE. */
3678
3679 static inline void
3680 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3681 {
3682 dw_attr_node attr;
3683
3684 attr.dw_attr = attr_kind;
3685 attr.dw_attr_val.val_class = dw_val_class_const;
3686 attr.dw_attr_val.val_entry = NULL;
3687 attr.dw_attr_val.v.val_int = int_val;
3688 add_dwarf_attr (die, &attr);
3689 }
3690
3691 static inline HOST_WIDE_INT
3692 AT_int (dw_attr_ref a)
3693 {
3694 gcc_assert (a && AT_class (a) == dw_val_class_const);
3695 return a->dw_attr_val.v.val_int;
3696 }
3697
3698 /* Add an unsigned integer attribute value to a DIE. */
3699
3700 static inline void
3701 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3702 unsigned HOST_WIDE_INT unsigned_val)
3703 {
3704 dw_attr_node attr;
3705
3706 attr.dw_attr = attr_kind;
3707 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3708 attr.dw_attr_val.val_entry = NULL;
3709 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3710 add_dwarf_attr (die, &attr);
3711 }
3712
3713 static inline unsigned HOST_WIDE_INT
3714 AT_unsigned (dw_attr_ref a)
3715 {
3716 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3717 return a->dw_attr_val.v.val_unsigned;
3718 }
3719
3720 /* Add an unsigned double integer attribute value to a DIE. */
3721
3722 static inline void
3723 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3724 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3725 {
3726 dw_attr_node attr;
3727
3728 attr.dw_attr = attr_kind;
3729 attr.dw_attr_val.val_class = dw_val_class_const_double;
3730 attr.dw_attr_val.val_entry = NULL;
3731 attr.dw_attr_val.v.val_double.high = high;
3732 attr.dw_attr_val.v.val_double.low = low;
3733 add_dwarf_attr (die, &attr);
3734 }
3735
3736 /* Add a floating point attribute value to a DIE and return it. */
3737
3738 static inline void
3739 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3740 unsigned int length, unsigned int elt_size, unsigned char *array)
3741 {
3742 dw_attr_node attr;
3743
3744 attr.dw_attr = attr_kind;
3745 attr.dw_attr_val.val_class = dw_val_class_vec;
3746 attr.dw_attr_val.val_entry = NULL;
3747 attr.dw_attr_val.v.val_vec.length = length;
3748 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3749 attr.dw_attr_val.v.val_vec.array = array;
3750 add_dwarf_attr (die, &attr);
3751 }
3752
3753 /* Add an 8-byte data attribute value to a DIE. */
3754
3755 static inline void
3756 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3757 unsigned char data8[8])
3758 {
3759 dw_attr_node attr;
3760
3761 attr.dw_attr = attr_kind;
3762 attr.dw_attr_val.val_class = dw_val_class_data8;
3763 attr.dw_attr_val.val_entry = NULL;
3764 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3765 add_dwarf_attr (die, &attr);
3766 }
3767
3768 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3769 dwarf_split_debug_info, address attributes in dies destined for the
3770 final executable have force_direct set to avoid using indexed
3771 references. */
3772
3773 static inline void
3774 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3775 bool force_direct)
3776 {
3777 dw_attr_node attr;
3778 char * lbl_id;
3779
3780 lbl_id = xstrdup (lbl_low);
3781 attr.dw_attr = DW_AT_low_pc;
3782 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3783 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3784 if (dwarf_split_debug_info && !force_direct)
3785 attr.dw_attr_val.val_entry
3786 = add_addr_table_entry (lbl_id, ate_kind_label);
3787 else
3788 attr.dw_attr_val.val_entry = NULL;
3789 add_dwarf_attr (die, &attr);
3790
3791 attr.dw_attr = DW_AT_high_pc;
3792 if (dwarf_version < 4)
3793 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3794 else
3795 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3796 lbl_id = xstrdup (lbl_high);
3797 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3798 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3799 && dwarf_split_debug_info && !force_direct)
3800 attr.dw_attr_val.val_entry
3801 = add_addr_table_entry (lbl_id, ate_kind_label);
3802 else
3803 attr.dw_attr_val.val_entry = NULL;
3804 add_dwarf_attr (die, &attr);
3805 }
3806
3807 /* Hash and equality functions for debug_str_hash. */
3808
3809 static hashval_t
3810 debug_str_do_hash (const void *x)
3811 {
3812 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3813 }
3814
3815 static int
3816 debug_str_eq (const void *x1, const void *x2)
3817 {
3818 return strcmp ((((const struct indirect_string_node *)x1)->str),
3819 (const char *)x2) == 0;
3820 }
3821
3822 /* Add STR to the indirect string hash table. */
3823
3824 static struct indirect_string_node *
3825 find_AT_string (const char *str)
3826 {
3827 struct indirect_string_node *node;
3828 void **slot;
3829
3830 if (! debug_str_hash)
3831 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3832 debug_str_eq, NULL);
3833
3834 slot = htab_find_slot_with_hash (debug_str_hash, str,
3835 htab_hash_string (str), INSERT);
3836 if (*slot == NULL)
3837 {
3838 node = ggc_alloc_cleared_indirect_string_node ();
3839 node->str = ggc_strdup (str);
3840 *slot = node;
3841 }
3842 else
3843 node = (struct indirect_string_node *) *slot;
3844
3845 node->refcount++;
3846 return node;
3847 }
3848
3849 /* Add a string attribute value to a DIE. */
3850
3851 static inline void
3852 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3853 {
3854 dw_attr_node attr;
3855 struct indirect_string_node *node;
3856
3857 node = find_AT_string (str);
3858
3859 attr.dw_attr = attr_kind;
3860 attr.dw_attr_val.val_class = dw_val_class_str;
3861 attr.dw_attr_val.val_entry = NULL;
3862 attr.dw_attr_val.v.val_str = node;
3863 add_dwarf_attr (die, &attr);
3864 }
3865
3866 static inline const char *
3867 AT_string (dw_attr_ref a)
3868 {
3869 gcc_assert (a && AT_class (a) == dw_val_class_str);
3870 return a->dw_attr_val.v.val_str->str;
3871 }
3872
3873 /* Call this function directly to bypass AT_string_form's logic to put
3874 the string inline in the die. */
3875
3876 static void
3877 set_indirect_string (struct indirect_string_node *node)
3878 {
3879 char label[32];
3880 /* Already indirect is a no op. */
3881 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3882 {
3883 gcc_assert (node->label);
3884 return;
3885 }
3886 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3887 ++dw2_string_counter;
3888 node->label = xstrdup (label);
3889
3890 if (!dwarf_split_debug_info)
3891 {
3892 node->form = DW_FORM_strp;
3893 node->index = NOT_INDEXED;
3894 }
3895 else
3896 {
3897 node->form = DW_FORM_GNU_str_index;
3898 node->index = NO_INDEX_ASSIGNED;
3899 }
3900 }
3901
3902 /* Find out whether a string should be output inline in DIE
3903 or out-of-line in .debug_str section. */
3904
3905 static enum dwarf_form
3906 find_string_form (struct indirect_string_node *node)
3907 {
3908 unsigned int len;
3909
3910 if (node->form)
3911 return node->form;
3912
3913 len = strlen (node->str) + 1;
3914
3915 /* If the string is shorter or equal to the size of the reference, it is
3916 always better to put it inline. */
3917 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3918 return node->form = DW_FORM_string;
3919
3920 /* If we cannot expect the linker to merge strings in .debug_str
3921 section, only put it into .debug_str if it is worth even in this
3922 single module. */
3923 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3924 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3925 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3926 return node->form = DW_FORM_string;
3927
3928 set_indirect_string (node);
3929
3930 return node->form;
3931 }
3932
3933 /* Find out whether the string referenced from the attribute should be
3934 output inline in DIE or out-of-line in .debug_str section. */
3935
3936 static enum dwarf_form
3937 AT_string_form (dw_attr_ref a)
3938 {
3939 gcc_assert (a && AT_class (a) == dw_val_class_str);
3940 return find_string_form (a->dw_attr_val.v.val_str);
3941 }
3942
3943 /* Add a DIE reference attribute value to a DIE. */
3944
3945 static inline void
3946 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3947 {
3948 dw_attr_node attr;
3949
3950 #ifdef ENABLE_CHECKING
3951 gcc_assert (targ_die != NULL);
3952 #else
3953 /* With LTO we can end up trying to reference something we didn't create
3954 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3955 if (targ_die == NULL)
3956 return;
3957 #endif
3958
3959 attr.dw_attr = attr_kind;
3960 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3961 attr.dw_attr_val.val_entry = NULL;
3962 attr.dw_attr_val.v.val_die_ref.die = targ_die;
3963 attr.dw_attr_val.v.val_die_ref.external = 0;
3964 add_dwarf_attr (die, &attr);
3965 }
3966
3967 /* Change DIE reference REF to point to NEW_DIE instead. */
3968
3969 static inline void
3970 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
3971 {
3972 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
3973 ref->dw_attr_val.v.val_die_ref.die = new_die;
3974 ref->dw_attr_val.v.val_die_ref.external = 0;
3975 }
3976
3977 /* Add an AT_specification attribute to a DIE, and also make the back
3978 pointer from the specification to the definition. */
3979
3980 static inline void
3981 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
3982 {
3983 add_AT_die_ref (die, DW_AT_specification, targ_die);
3984 gcc_assert (!targ_die->die_definition);
3985 targ_die->die_definition = die;
3986 }
3987
3988 static inline dw_die_ref
3989 AT_ref (dw_attr_ref a)
3990 {
3991 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3992 return a->dw_attr_val.v.val_die_ref.die;
3993 }
3994
3995 static inline int
3996 AT_ref_external (dw_attr_ref a)
3997 {
3998 if (a && AT_class (a) == dw_val_class_die_ref)
3999 return a->dw_attr_val.v.val_die_ref.external;
4000
4001 return 0;
4002 }
4003
4004 static inline void
4005 set_AT_ref_external (dw_attr_ref a, int i)
4006 {
4007 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4008 a->dw_attr_val.v.val_die_ref.external = i;
4009 }
4010
4011 /* Add an FDE reference attribute value to a DIE. */
4012
4013 static inline void
4014 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4015 {
4016 dw_attr_node attr;
4017
4018 attr.dw_attr = attr_kind;
4019 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4020 attr.dw_attr_val.val_entry = NULL;
4021 attr.dw_attr_val.v.val_fde_index = targ_fde;
4022 add_dwarf_attr (die, &attr);
4023 }
4024
4025 /* Add a location description attribute value to a DIE. */
4026
4027 static inline void
4028 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4029 {
4030 dw_attr_node attr;
4031
4032 attr.dw_attr = attr_kind;
4033 attr.dw_attr_val.val_class = dw_val_class_loc;
4034 attr.dw_attr_val.val_entry = NULL;
4035 attr.dw_attr_val.v.val_loc = loc;
4036 add_dwarf_attr (die, &attr);
4037 }
4038
4039 static inline dw_loc_descr_ref
4040 AT_loc (dw_attr_ref a)
4041 {
4042 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4043 return a->dw_attr_val.v.val_loc;
4044 }
4045
4046 static inline void
4047 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4048 {
4049 dw_attr_node attr;
4050
4051 attr.dw_attr = attr_kind;
4052 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4053 attr.dw_attr_val.val_entry = NULL;
4054 attr.dw_attr_val.v.val_loc_list = loc_list;
4055 add_dwarf_attr (die, &attr);
4056 have_location_lists = true;
4057 }
4058
4059 static inline dw_loc_list_ref
4060 AT_loc_list (dw_attr_ref a)
4061 {
4062 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4063 return a->dw_attr_val.v.val_loc_list;
4064 }
4065
4066 static inline dw_loc_list_ref *
4067 AT_loc_list_ptr (dw_attr_ref a)
4068 {
4069 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4070 return &a->dw_attr_val.v.val_loc_list;
4071 }
4072
4073 /* Table of entries into the .debug_addr section. */
4074
4075 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4076
4077 /* Hash an address_table_entry. */
4078
4079 static hashval_t
4080 addr_table_entry_do_hash (const void *x)
4081 {
4082 const addr_table_entry *a = (const addr_table_entry *) x;
4083 switch (a->kind)
4084 {
4085 case ate_kind_rtx:
4086 return iterative_hash_rtx (a->addr.rtl, 0);
4087 case ate_kind_rtx_dtprel:
4088 return iterative_hash_rtx (a->addr.rtl, 1);
4089 case ate_kind_label:
4090 return htab_hash_string (a->addr.label);
4091 default:
4092 gcc_unreachable ();
4093 }
4094 }
4095
4096 /* Determine equality for two address_table_entries. */
4097
4098 static int
4099 addr_table_entry_eq (const void *x1, const void *x2)
4100 {
4101 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4102 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4103
4104 if (a1->kind != a2->kind)
4105 return 0;
4106 switch (a1->kind)
4107 {
4108 case ate_kind_rtx:
4109 case ate_kind_rtx_dtprel:
4110 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4111 case ate_kind_label:
4112 return strcmp (a1->addr.label, a2->addr.label) == 0;
4113 default:
4114 gcc_unreachable ();
4115 }
4116 }
4117
4118 /* Initialize an addr_table_entry. */
4119
4120 void
4121 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4122 {
4123 e->kind = kind;
4124 switch (kind)
4125 {
4126 case ate_kind_rtx:
4127 case ate_kind_rtx_dtprel:
4128 e->addr.rtl = (rtx) addr;
4129 break;
4130 case ate_kind_label:
4131 e->addr.label = (char *) addr;
4132 break;
4133 }
4134 e->refcount = 0;
4135 e->index = NO_INDEX_ASSIGNED;
4136 }
4137
4138 /* Add attr to the address table entry to the table. Defer setting an
4139 index until output time. */
4140
4141 static addr_table_entry *
4142 add_addr_table_entry (void *addr, enum ate_kind kind)
4143 {
4144 addr_table_entry *node;
4145 addr_table_entry finder;
4146 void **slot;
4147
4148 gcc_assert (dwarf_split_debug_info);
4149 if (! addr_index_table)
4150 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4151 addr_table_entry_eq, NULL);
4152 init_addr_table_entry (&finder, kind, addr);
4153 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4154
4155 if (*slot == HTAB_EMPTY_ENTRY)
4156 {
4157 node = ggc_alloc_cleared_addr_table_entry ();
4158 init_addr_table_entry (node, kind, addr);
4159 *slot = node;
4160 }
4161 else
4162 node = (addr_table_entry *) *slot;
4163
4164 node->refcount++;
4165 return node;
4166 }
4167
4168 /* Remove an entry from the addr table by decrementing its refcount.
4169 Strictly, decrementing the refcount would be enough, but the
4170 assertion that the entry is actually in the table has found
4171 bugs. */
4172
4173 static void
4174 remove_addr_table_entry (addr_table_entry *entry)
4175 {
4176 addr_table_entry *node;
4177
4178 gcc_assert (dwarf_split_debug_info && addr_index_table);
4179 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4180 /* After an index is assigned, the table is frozen. */
4181 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4182 node->refcount--;
4183 }
4184
4185 /* Given a location list, remove all addresses it refers to from the
4186 address_table. */
4187
4188 static void
4189 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4190 {
4191 for (; descr; descr = descr->dw_loc_next)
4192 if (descr->dw_loc_oprnd1.val_entry != NULL)
4193 {
4194 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4195 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4196 }
4197 }
4198
4199 /* A helper function for dwarf2out_finish called through
4200 htab_traverse. Assign an addr_table_entry its index. All entries
4201 must be collected into the table when this function is called,
4202 because the indexing code relies on htab_traverse to traverse nodes
4203 in the same order for each run. */
4204
4205 static int
4206 index_addr_table_entry (void **h, void *v)
4207 {
4208 addr_table_entry *node = (addr_table_entry *) *h;
4209 unsigned int *index = (unsigned int *) v;
4210
4211 /* Don't index unreferenced nodes. */
4212 if (node->refcount == 0)
4213 return 1;
4214
4215 gcc_assert(node->index == NO_INDEX_ASSIGNED);
4216 node->index = *index;
4217 *index += 1;
4218
4219 return 1;
4220 }
4221
4222 /* Add an address constant attribute value to a DIE. When using
4223 dwarf_split_debug_info, address attributes in dies destined for the
4224 final executable should be direct references--setting the parameter
4225 force_direct ensures this behavior. */
4226
4227 static inline void
4228 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4229 bool force_direct)
4230 {
4231 dw_attr_node attr;
4232
4233 attr.dw_attr = attr_kind;
4234 attr.dw_attr_val.val_class = dw_val_class_addr;
4235 attr.dw_attr_val.v.val_addr = addr;
4236 if (dwarf_split_debug_info && !force_direct)
4237 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4238 else
4239 attr.dw_attr_val.val_entry = NULL;
4240 add_dwarf_attr (die, &attr);
4241 }
4242
4243 /* Get the RTX from to an address DIE attribute. */
4244
4245 static inline rtx
4246 AT_addr (dw_attr_ref a)
4247 {
4248 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4249 return a->dw_attr_val.v.val_addr;
4250 }
4251
4252 /* Add a file attribute value to a DIE. */
4253
4254 static inline void
4255 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4256 struct dwarf_file_data *fd)
4257 {
4258 dw_attr_node attr;
4259
4260 attr.dw_attr = attr_kind;
4261 attr.dw_attr_val.val_class = dw_val_class_file;
4262 attr.dw_attr_val.val_entry = NULL;
4263 attr.dw_attr_val.v.val_file = fd;
4264 add_dwarf_attr (die, &attr);
4265 }
4266
4267 /* Get the dwarf_file_data from a file DIE attribute. */
4268
4269 static inline struct dwarf_file_data *
4270 AT_file (dw_attr_ref a)
4271 {
4272 gcc_assert (a && AT_class (a) == dw_val_class_file);
4273 return a->dw_attr_val.v.val_file;
4274 }
4275
4276 /* Add a vms delta attribute value to a DIE. */
4277
4278 static inline void
4279 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4280 const char *lbl1, const char *lbl2)
4281 {
4282 dw_attr_node attr;
4283
4284 attr.dw_attr = attr_kind;
4285 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4286 attr.dw_attr_val.val_entry = NULL;
4287 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4288 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4289 add_dwarf_attr (die, &attr);
4290 }
4291
4292 /* Add a label identifier attribute value to a DIE. */
4293
4294 static inline void
4295 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4296 const char *lbl_id)
4297 {
4298 dw_attr_node attr;
4299
4300 attr.dw_attr = attr_kind;
4301 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4302 attr.dw_attr_val.val_entry = NULL;
4303 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4304 if (dwarf_split_debug_info)
4305 attr.dw_attr_val.val_entry
4306 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4307 ate_kind_label);
4308 add_dwarf_attr (die, &attr);
4309 }
4310
4311 /* Add a section offset attribute value to a DIE, an offset into the
4312 debug_line section. */
4313
4314 static inline void
4315 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4316 const char *label)
4317 {
4318 dw_attr_node attr;
4319
4320 attr.dw_attr = attr_kind;
4321 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4322 attr.dw_attr_val.val_entry = NULL;
4323 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4324 add_dwarf_attr (die, &attr);
4325 }
4326
4327 /* Add a section offset attribute value to a DIE, an offset into the
4328 debug_macinfo section. */
4329
4330 static inline void
4331 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4332 const char *label)
4333 {
4334 dw_attr_node attr;
4335
4336 attr.dw_attr = attr_kind;
4337 attr.dw_attr_val.val_class = dw_val_class_macptr;
4338 attr.dw_attr_val.val_entry = NULL;
4339 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4340 add_dwarf_attr (die, &attr);
4341 }
4342
4343 /* Add an offset attribute value to a DIE. */
4344
4345 static inline void
4346 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4347 unsigned HOST_WIDE_INT offset)
4348 {
4349 dw_attr_node attr;
4350
4351 attr.dw_attr = attr_kind;
4352 attr.dw_attr_val.val_class = dw_val_class_offset;
4353 attr.dw_attr_val.val_entry = NULL;
4354 attr.dw_attr_val.v.val_offset = offset;
4355 add_dwarf_attr (die, &attr);
4356 }
4357
4358 /* Add a range_list attribute value to a DIE. When using
4359 dwarf_split_debug_info, address attributes in dies destined for the
4360 final executable should be direct references--setting the parameter
4361 force_direct ensures this behavior. */
4362
4363 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4364 #define RELOCATED_OFFSET (NULL)
4365
4366 static void
4367 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4368 long unsigned int offset, bool force_direct)
4369 {
4370 dw_attr_node attr;
4371
4372 attr.dw_attr = attr_kind;
4373 attr.dw_attr_val.val_class = dw_val_class_range_list;
4374 /* For the range_list attribute, use val_entry to store whether the
4375 offset should follow split-debug-info or normal semantics. This
4376 value is read in output_range_list_offset. */
4377 if (dwarf_split_debug_info && !force_direct)
4378 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4379 else
4380 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4381 attr.dw_attr_val.v.val_offset = offset;
4382 add_dwarf_attr (die, &attr);
4383 }
4384
4385 /* Return the start label of a delta attribute. */
4386
4387 static inline const char *
4388 AT_vms_delta1 (dw_attr_ref a)
4389 {
4390 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4391 return a->dw_attr_val.v.val_vms_delta.lbl1;
4392 }
4393
4394 /* Return the end label of a delta attribute. */
4395
4396 static inline const char *
4397 AT_vms_delta2 (dw_attr_ref a)
4398 {
4399 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4400 return a->dw_attr_val.v.val_vms_delta.lbl2;
4401 }
4402
4403 static inline const char *
4404 AT_lbl (dw_attr_ref a)
4405 {
4406 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4407 || AT_class (a) == dw_val_class_lineptr
4408 || AT_class (a) == dw_val_class_macptr
4409 || AT_class (a) == dw_val_class_high_pc));
4410 return a->dw_attr_val.v.val_lbl_id;
4411 }
4412
4413 /* Get the attribute of type attr_kind. */
4414
4415 static dw_attr_ref
4416 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4417 {
4418 dw_attr_ref a;
4419 unsigned ix;
4420 dw_die_ref spec = NULL;
4421
4422 if (! die)
4423 return NULL;
4424
4425 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4426 if (a->dw_attr == attr_kind)
4427 return a;
4428 else if (a->dw_attr == DW_AT_specification
4429 || a->dw_attr == DW_AT_abstract_origin)
4430 spec = AT_ref (a);
4431
4432 if (spec)
4433 return get_AT (spec, attr_kind);
4434
4435 return NULL;
4436 }
4437
4438 /* Returns the parent of the declaration of DIE. */
4439
4440 static dw_die_ref
4441 get_die_parent (dw_die_ref die)
4442 {
4443 dw_die_ref t;
4444
4445 if (!die)
4446 return NULL;
4447
4448 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4449 || (t = get_AT_ref (die, DW_AT_specification)))
4450 die = t;
4451
4452 return die->die_parent;
4453 }
4454
4455 /* Return the "low pc" attribute value, typically associated with a subprogram
4456 DIE. Return null if the "low pc" attribute is either not present, or if it
4457 cannot be represented as an assembler label identifier. */
4458
4459 static inline const char *
4460 get_AT_low_pc (dw_die_ref die)
4461 {
4462 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4463
4464 return a ? AT_lbl (a) : NULL;
4465 }
4466
4467 /* Return the "high pc" attribute value, typically associated with a subprogram
4468 DIE. Return null if the "high pc" attribute is either not present, or if it
4469 cannot be represented as an assembler label identifier. */
4470
4471 static inline const char *
4472 get_AT_hi_pc (dw_die_ref die)
4473 {
4474 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4475
4476 return a ? AT_lbl (a) : NULL;
4477 }
4478
4479 /* Return the value of the string attribute designated by ATTR_KIND, or
4480 NULL if it is not present. */
4481
4482 static inline const char *
4483 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4484 {
4485 dw_attr_ref a = get_AT (die, attr_kind);
4486
4487 return a ? AT_string (a) : NULL;
4488 }
4489
4490 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4491 if it is not present. */
4492
4493 static inline int
4494 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4495 {
4496 dw_attr_ref a = get_AT (die, attr_kind);
4497
4498 return a ? AT_flag (a) : 0;
4499 }
4500
4501 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4502 if it is not present. */
4503
4504 static inline unsigned
4505 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4506 {
4507 dw_attr_ref a = get_AT (die, attr_kind);
4508
4509 return a ? AT_unsigned (a) : 0;
4510 }
4511
4512 static inline dw_die_ref
4513 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4514 {
4515 dw_attr_ref a = get_AT (die, attr_kind);
4516
4517 return a ? AT_ref (a) : NULL;
4518 }
4519
4520 static inline struct dwarf_file_data *
4521 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4522 {
4523 dw_attr_ref a = get_AT (die, attr_kind);
4524
4525 return a ? AT_file (a) : NULL;
4526 }
4527
4528 /* Return TRUE if the language is C++. */
4529
4530 static inline bool
4531 is_cxx (void)
4532 {
4533 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4534
4535 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4536 }
4537
4538 /* Return TRUE if the language is Fortran. */
4539
4540 static inline bool
4541 is_fortran (void)
4542 {
4543 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4544
4545 return (lang == DW_LANG_Fortran77
4546 || lang == DW_LANG_Fortran90
4547 || lang == DW_LANG_Fortran95);
4548 }
4549
4550 /* Return TRUE if the language is Ada. */
4551
4552 static inline bool
4553 is_ada (void)
4554 {
4555 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4556
4557 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4558 }
4559
4560 /* Remove the specified attribute if present. */
4561
4562 static void
4563 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4564 {
4565 dw_attr_ref a;
4566 unsigned ix;
4567
4568 if (! die)
4569 return;
4570
4571 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4572 if (a->dw_attr == attr_kind)
4573 {
4574 if (AT_class (a) == dw_val_class_str)
4575 if (a->dw_attr_val.v.val_str->refcount)
4576 a->dw_attr_val.v.val_str->refcount--;
4577
4578 /* vec::ordered_remove should help reduce the number of abbrevs
4579 that are needed. */
4580 die->die_attr->ordered_remove (ix);
4581 return;
4582 }
4583 }
4584
4585 /* Remove CHILD from its parent. PREV must have the property that
4586 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4587
4588 static void
4589 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4590 {
4591 gcc_assert (child->die_parent == prev->die_parent);
4592 gcc_assert (prev->die_sib == child);
4593 if (prev == child)
4594 {
4595 gcc_assert (child->die_parent->die_child == child);
4596 prev = NULL;
4597 }
4598 else
4599 prev->die_sib = child->die_sib;
4600 if (child->die_parent->die_child == child)
4601 child->die_parent->die_child = prev;
4602 }
4603
4604 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4605 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4606
4607 static void
4608 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4609 {
4610 dw_die_ref parent = old_child->die_parent;
4611
4612 gcc_assert (parent == prev->die_parent);
4613 gcc_assert (prev->die_sib == old_child);
4614
4615 new_child->die_parent = parent;
4616 if (prev == old_child)
4617 {
4618 gcc_assert (parent->die_child == old_child);
4619 new_child->die_sib = new_child;
4620 }
4621 else
4622 {
4623 prev->die_sib = new_child;
4624 new_child->die_sib = old_child->die_sib;
4625 }
4626 if (old_child->die_parent->die_child == old_child)
4627 old_child->die_parent->die_child = new_child;
4628 }
4629
4630 /* Move all children from OLD_PARENT to NEW_PARENT. */
4631
4632 static void
4633 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4634 {
4635 dw_die_ref c;
4636 new_parent->die_child = old_parent->die_child;
4637 old_parent->die_child = NULL;
4638 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4639 }
4640
4641 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4642 matches TAG. */
4643
4644 static void
4645 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4646 {
4647 dw_die_ref c;
4648
4649 c = die->die_child;
4650 if (c) do {
4651 dw_die_ref prev = c;
4652 c = c->die_sib;
4653 while (c->die_tag == tag)
4654 {
4655 remove_child_with_prev (c, prev);
4656 /* Might have removed every child. */
4657 if (c == c->die_sib)
4658 return;
4659 c = c->die_sib;
4660 }
4661 } while (c != die->die_child);
4662 }
4663
4664 /* Add a CHILD_DIE as the last child of DIE. */
4665
4666 static void
4667 add_child_die (dw_die_ref die, dw_die_ref child_die)
4668 {
4669 /* FIXME this should probably be an assert. */
4670 if (! die || ! child_die)
4671 return;
4672 gcc_assert (die != child_die);
4673
4674 child_die->die_parent = die;
4675 if (die->die_child)
4676 {
4677 child_die->die_sib = die->die_child->die_sib;
4678 die->die_child->die_sib = child_die;
4679 }
4680 else
4681 child_die->die_sib = child_die;
4682 die->die_child = child_die;
4683 }
4684
4685 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4686 is the specification, to the end of PARENT's list of children.
4687 This is done by removing and re-adding it. */
4688
4689 static void
4690 splice_child_die (dw_die_ref parent, dw_die_ref child)
4691 {
4692 dw_die_ref p;
4693
4694 /* We want the declaration DIE from inside the class, not the
4695 specification DIE at toplevel. */
4696 if (child->die_parent != parent)
4697 {
4698 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4699
4700 if (tmp)
4701 child = tmp;
4702 }
4703
4704 gcc_assert (child->die_parent == parent
4705 || (child->die_parent
4706 == get_AT_ref (parent, DW_AT_specification)));
4707
4708 for (p = child->die_parent->die_child; ; p = p->die_sib)
4709 if (p->die_sib == child)
4710 {
4711 remove_child_with_prev (child, p);
4712 break;
4713 }
4714
4715 add_child_die (parent, child);
4716 }
4717
4718 /* Return a pointer to a newly created DIE node. */
4719
4720 static inline dw_die_ref
4721 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4722 {
4723 dw_die_ref die = ggc_alloc_cleared_die_node ();
4724
4725 die->die_tag = tag_value;
4726
4727 if (parent_die != NULL)
4728 add_child_die (parent_die, die);
4729 else
4730 {
4731 limbo_die_node *limbo_node;
4732
4733 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4734 limbo_node->die = die;
4735 limbo_node->created_for = t;
4736 limbo_node->next = limbo_die_list;
4737 limbo_die_list = limbo_node;
4738 }
4739
4740 return die;
4741 }
4742
4743 /* Return the DIE associated with the given type specifier. */
4744
4745 static inline dw_die_ref
4746 lookup_type_die (tree type)
4747 {
4748 return TYPE_SYMTAB_DIE (type);
4749 }
4750
4751 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4752 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4753 anonymous type instead the one of the naming typedef. */
4754
4755 static inline dw_die_ref
4756 strip_naming_typedef (tree type, dw_die_ref type_die)
4757 {
4758 if (type
4759 && TREE_CODE (type) == RECORD_TYPE
4760 && type_die
4761 && type_die->die_tag == DW_TAG_typedef
4762 && is_naming_typedef_decl (TYPE_NAME (type)))
4763 type_die = get_AT_ref (type_die, DW_AT_type);
4764 return type_die;
4765 }
4766
4767 /* Like lookup_type_die, but if type is an anonymous type named by a
4768 typedef[1], return the DIE of the anonymous type instead the one of
4769 the naming typedef. This is because in gen_typedef_die, we did
4770 equate the anonymous struct named by the typedef with the DIE of
4771 the naming typedef. So by default, lookup_type_die on an anonymous
4772 struct yields the DIE of the naming typedef.
4773
4774 [1]: Read the comment of is_naming_typedef_decl to learn about what
4775 a naming typedef is. */
4776
4777 static inline dw_die_ref
4778 lookup_type_die_strip_naming_typedef (tree type)
4779 {
4780 dw_die_ref die = lookup_type_die (type);
4781 return strip_naming_typedef (type, die);
4782 }
4783
4784 /* Equate a DIE to a given type specifier. */
4785
4786 static inline void
4787 equate_type_number_to_die (tree type, dw_die_ref type_die)
4788 {
4789 TYPE_SYMTAB_DIE (type) = type_die;
4790 }
4791
4792 /* Returns a hash value for X (which really is a die_struct). */
4793
4794 static hashval_t
4795 decl_die_table_hash (const void *x)
4796 {
4797 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4798 }
4799
4800 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4801
4802 static int
4803 decl_die_table_eq (const void *x, const void *y)
4804 {
4805 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4806 }
4807
4808 /* Return the DIE associated with a given declaration. */
4809
4810 static inline dw_die_ref
4811 lookup_decl_die (tree decl)
4812 {
4813 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4814 }
4815
4816 /* Returns a hash value for X (which really is a var_loc_list). */
4817
4818 static hashval_t
4819 decl_loc_table_hash (const void *x)
4820 {
4821 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4822 }
4823
4824 /* Return nonzero if decl_id of var_loc_list X is the same as
4825 UID of decl *Y. */
4826
4827 static int
4828 decl_loc_table_eq (const void *x, const void *y)
4829 {
4830 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4831 }
4832
4833 /* Return the var_loc list associated with a given declaration. */
4834
4835 static inline var_loc_list *
4836 lookup_decl_loc (const_tree decl)
4837 {
4838 if (!decl_loc_table)
4839 return NULL;
4840 return (var_loc_list *)
4841 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4842 }
4843
4844 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4845
4846 static hashval_t
4847 cached_dw_loc_list_table_hash (const void *x)
4848 {
4849 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4850 }
4851
4852 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4853 UID of decl *Y. */
4854
4855 static int
4856 cached_dw_loc_list_table_eq (const void *x, const void *y)
4857 {
4858 return (((const cached_dw_loc_list *) x)->decl_id
4859 == DECL_UID ((const_tree) y));
4860 }
4861
4862 /* Equate a DIE to a particular declaration. */
4863
4864 static void
4865 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4866 {
4867 unsigned int decl_id = DECL_UID (decl);
4868 void **slot;
4869
4870 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4871 *slot = decl_die;
4872 decl_die->decl_id = decl_id;
4873 }
4874
4875 /* Return how many bits covers PIECE EXPR_LIST. */
4876
4877 static int
4878 decl_piece_bitsize (rtx piece)
4879 {
4880 int ret = (int) GET_MODE (piece);
4881 if (ret)
4882 return ret;
4883 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4884 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4885 return INTVAL (XEXP (XEXP (piece, 0), 0));
4886 }
4887
4888 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4889
4890 static rtx *
4891 decl_piece_varloc_ptr (rtx piece)
4892 {
4893 if ((int) GET_MODE (piece))
4894 return &XEXP (piece, 0);
4895 else
4896 return &XEXP (XEXP (piece, 0), 1);
4897 }
4898
4899 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4900 Next is the chain of following piece nodes. */
4901
4902 static rtx
4903 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4904 {
4905 if (bitsize <= (int) MAX_MACHINE_MODE)
4906 return alloc_EXPR_LIST (bitsize, loc_note, next);
4907 else
4908 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4909 GEN_INT (bitsize),
4910 loc_note), next);
4911 }
4912
4913 /* Return rtx that should be stored into loc field for
4914 LOC_NOTE and BITPOS/BITSIZE. */
4915
4916 static rtx
4917 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4918 HOST_WIDE_INT bitsize)
4919 {
4920 if (bitsize != -1)
4921 {
4922 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4923 if (bitpos != 0)
4924 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4925 }
4926 return loc_note;
4927 }
4928
4929 /* This function either modifies location piece list *DEST in
4930 place (if SRC and INNER is NULL), or copies location piece list
4931 *SRC to *DEST while modifying it. Location BITPOS is modified
4932 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4933 not copied and if needed some padding around it is added.
4934 When modifying in place, DEST should point to EXPR_LIST where
4935 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4936 to the start of the whole list and INNER points to the EXPR_LIST
4937 where earlier pieces cover PIECE_BITPOS bits. */
4938
4939 static void
4940 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4941 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4942 HOST_WIDE_INT bitsize, rtx loc_note)
4943 {
4944 int diff;
4945 bool copy = inner != NULL;
4946
4947 if (copy)
4948 {
4949 /* First copy all nodes preceding the current bitpos. */
4950 while (src != inner)
4951 {
4952 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4953 decl_piece_bitsize (*src), NULL_RTX);
4954 dest = &XEXP (*dest, 1);
4955 src = &XEXP (*src, 1);
4956 }
4957 }
4958 /* Add padding if needed. */
4959 if (bitpos != piece_bitpos)
4960 {
4961 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4962 copy ? NULL_RTX : *dest);
4963 dest = &XEXP (*dest, 1);
4964 }
4965 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4966 {
4967 gcc_assert (!copy);
4968 /* A piece with correct bitpos and bitsize already exist,
4969 just update the location for it and return. */
4970 *decl_piece_varloc_ptr (*dest) = loc_note;
4971 return;
4972 }
4973 /* Add the piece that changed. */
4974 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
4975 dest = &XEXP (*dest, 1);
4976 /* Skip over pieces that overlap it. */
4977 diff = bitpos - piece_bitpos + bitsize;
4978 if (!copy)
4979 src = dest;
4980 while (diff > 0 && *src)
4981 {
4982 rtx piece = *src;
4983 diff -= decl_piece_bitsize (piece);
4984 if (copy)
4985 src = &XEXP (piece, 1);
4986 else
4987 {
4988 *src = XEXP (piece, 1);
4989 free_EXPR_LIST_node (piece);
4990 }
4991 }
4992 /* Add padding if needed. */
4993 if (diff < 0 && *src)
4994 {
4995 if (!copy)
4996 dest = src;
4997 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
4998 dest = &XEXP (*dest, 1);
4999 }
5000 if (!copy)
5001 return;
5002 /* Finally copy all nodes following it. */
5003 while (*src)
5004 {
5005 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5006 decl_piece_bitsize (*src), NULL_RTX);
5007 dest = &XEXP (*dest, 1);
5008 src = &XEXP (*src, 1);
5009 }
5010 }
5011
5012 /* Add a variable location node to the linked list for DECL. */
5013
5014 static struct var_loc_node *
5015 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5016 {
5017 unsigned int decl_id;
5018 var_loc_list *temp;
5019 void **slot;
5020 struct var_loc_node *loc = NULL;
5021 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5022
5023 if (TREE_CODE (decl) == VAR_DECL
5024 && DECL_HAS_DEBUG_EXPR_P (decl))
5025 {
5026 tree realdecl = DECL_DEBUG_EXPR (decl);
5027 if (handled_component_p (realdecl)
5028 || (TREE_CODE (realdecl) == MEM_REF
5029 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5030 {
5031 HOST_WIDE_INT maxsize;
5032 tree innerdecl;
5033 innerdecl
5034 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5035 if (!DECL_P (innerdecl)
5036 || DECL_IGNORED_P (innerdecl)
5037 || TREE_STATIC (innerdecl)
5038 || bitsize <= 0
5039 || bitpos + bitsize > 256
5040 || bitsize != maxsize)
5041 return NULL;
5042 decl = innerdecl;
5043 }
5044 }
5045
5046 decl_id = DECL_UID (decl);
5047 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5048 if (*slot == NULL)
5049 {
5050 temp = ggc_alloc_cleared_var_loc_list ();
5051 temp->decl_id = decl_id;
5052 *slot = temp;
5053 }
5054 else
5055 temp = (var_loc_list *) *slot;
5056
5057 /* For PARM_DECLs try to keep around the original incoming value,
5058 even if that means we'll emit a zero-range .debug_loc entry. */
5059 if (temp->last
5060 && temp->first == temp->last
5061 && TREE_CODE (decl) == PARM_DECL
5062 && NOTE_P (temp->first->loc)
5063 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5064 && DECL_INCOMING_RTL (decl)
5065 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5066 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5067 == GET_CODE (DECL_INCOMING_RTL (decl))
5068 && prev_real_insn (temp->first->loc) == NULL_RTX
5069 && (bitsize != -1
5070 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5071 NOTE_VAR_LOCATION_LOC (loc_note))
5072 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5073 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5074 {
5075 loc = ggc_alloc_cleared_var_loc_node ();
5076 temp->first->next = loc;
5077 temp->last = loc;
5078 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5079 }
5080 else if (temp->last)
5081 {
5082 struct var_loc_node *last = temp->last, *unused = NULL;
5083 rtx *piece_loc = NULL, last_loc_note;
5084 int piece_bitpos = 0;
5085 if (last->next)
5086 {
5087 last = last->next;
5088 gcc_assert (last->next == NULL);
5089 }
5090 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5091 {
5092 piece_loc = &last->loc;
5093 do
5094 {
5095 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5096 if (piece_bitpos + cur_bitsize > bitpos)
5097 break;
5098 piece_bitpos += cur_bitsize;
5099 piece_loc = &XEXP (*piece_loc, 1);
5100 }
5101 while (*piece_loc);
5102 }
5103 /* TEMP->LAST here is either pointer to the last but one or
5104 last element in the chained list, LAST is pointer to the
5105 last element. */
5106 if (label && strcmp (last->label, label) == 0)
5107 {
5108 /* For SRA optimized variables if there weren't any real
5109 insns since last note, just modify the last node. */
5110 if (piece_loc != NULL)
5111 {
5112 adjust_piece_list (piece_loc, NULL, NULL,
5113 bitpos, piece_bitpos, bitsize, loc_note);
5114 return NULL;
5115 }
5116 /* If the last note doesn't cover any instructions, remove it. */
5117 if (temp->last != last)
5118 {
5119 temp->last->next = NULL;
5120 unused = last;
5121 last = temp->last;
5122 gcc_assert (strcmp (last->label, label) != 0);
5123 }
5124 else
5125 {
5126 gcc_assert (temp->first == temp->last
5127 || (temp->first->next == temp->last
5128 && TREE_CODE (decl) == PARM_DECL));
5129 memset (temp->last, '\0', sizeof (*temp->last));
5130 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5131 return temp->last;
5132 }
5133 }
5134 if (bitsize == -1 && NOTE_P (last->loc))
5135 last_loc_note = last->loc;
5136 else if (piece_loc != NULL
5137 && *piece_loc != NULL_RTX
5138 && piece_bitpos == bitpos
5139 && decl_piece_bitsize (*piece_loc) == bitsize)
5140 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5141 else
5142 last_loc_note = NULL_RTX;
5143 /* If the current location is the same as the end of the list,
5144 and either both or neither of the locations is uninitialized,
5145 we have nothing to do. */
5146 if (last_loc_note == NULL_RTX
5147 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5148 NOTE_VAR_LOCATION_LOC (loc_note)))
5149 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5150 != NOTE_VAR_LOCATION_STATUS (loc_note))
5151 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5152 == VAR_INIT_STATUS_UNINITIALIZED)
5153 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5154 == VAR_INIT_STATUS_UNINITIALIZED))))
5155 {
5156 /* Add LOC to the end of list and update LAST. If the last
5157 element of the list has been removed above, reuse its
5158 memory for the new node, otherwise allocate a new one. */
5159 if (unused)
5160 {
5161 loc = unused;
5162 memset (loc, '\0', sizeof (*loc));
5163 }
5164 else
5165 loc = ggc_alloc_cleared_var_loc_node ();
5166 if (bitsize == -1 || piece_loc == NULL)
5167 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5168 else
5169 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5170 bitpos, piece_bitpos, bitsize, loc_note);
5171 last->next = loc;
5172 /* Ensure TEMP->LAST will point either to the new last but one
5173 element of the chain, or to the last element in it. */
5174 if (last != temp->last)
5175 temp->last = last;
5176 }
5177 else if (unused)
5178 ggc_free (unused);
5179 }
5180 else
5181 {
5182 loc = ggc_alloc_cleared_var_loc_node ();
5183 temp->first = loc;
5184 temp->last = loc;
5185 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5186 }
5187 return loc;
5188 }
5189 \f
5190 /* Keep track of the number of spaces used to indent the
5191 output of the debugging routines that print the structure of
5192 the DIE internal representation. */
5193 static int print_indent;
5194
5195 /* Indent the line the number of spaces given by print_indent. */
5196
5197 static inline void
5198 print_spaces (FILE *outfile)
5199 {
5200 fprintf (outfile, "%*s", print_indent, "");
5201 }
5202
5203 /* Print a type signature in hex. */
5204
5205 static inline void
5206 print_signature (FILE *outfile, char *sig)
5207 {
5208 int i;
5209
5210 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5211 fprintf (outfile, "%02x", sig[i] & 0xff);
5212 }
5213
5214 /* Print the information associated with a given DIE, and its children.
5215 This routine is a debugging aid only. */
5216
5217 static void
5218 print_die (dw_die_ref die, FILE *outfile)
5219 {
5220 dw_attr_ref a;
5221 dw_die_ref c;
5222 unsigned ix;
5223
5224 print_spaces (outfile);
5225 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5226 die->die_offset, dwarf_tag_name (die->die_tag),
5227 (void*) die);
5228 print_spaces (outfile);
5229 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5230 fprintf (outfile, " offset: %ld", die->die_offset);
5231 fprintf (outfile, " mark: %d\n", die->die_mark);
5232
5233 if (die->comdat_type_p)
5234 {
5235 print_spaces (outfile);
5236 fprintf (outfile, " signature: ");
5237 print_signature (outfile, die->die_id.die_type_node->signature);
5238 fprintf (outfile, "\n");
5239 }
5240
5241 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5242 {
5243 print_spaces (outfile);
5244 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5245
5246 switch (AT_class (a))
5247 {
5248 case dw_val_class_addr:
5249 fprintf (outfile, "address");
5250 break;
5251 case dw_val_class_offset:
5252 fprintf (outfile, "offset");
5253 break;
5254 case dw_val_class_loc:
5255 fprintf (outfile, "location descriptor");
5256 break;
5257 case dw_val_class_loc_list:
5258 fprintf (outfile, "location list -> label:%s",
5259 AT_loc_list (a)->ll_symbol);
5260 break;
5261 case dw_val_class_range_list:
5262 fprintf (outfile, "range list");
5263 break;
5264 case dw_val_class_const:
5265 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5266 break;
5267 case dw_val_class_unsigned_const:
5268 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5269 break;
5270 case dw_val_class_const_double:
5271 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5272 HOST_WIDE_INT_PRINT_UNSIGNED")",
5273 a->dw_attr_val.v.val_double.high,
5274 a->dw_attr_val.v.val_double.low);
5275 break;
5276 case dw_val_class_vec:
5277 fprintf (outfile, "floating-point or vector constant");
5278 break;
5279 case dw_val_class_flag:
5280 fprintf (outfile, "%u", AT_flag (a));
5281 break;
5282 case dw_val_class_die_ref:
5283 if (AT_ref (a) != NULL)
5284 {
5285 if (AT_ref (a)->comdat_type_p)
5286 {
5287 fprintf (outfile, "die -> signature: ");
5288 print_signature (outfile,
5289 AT_ref (a)->die_id.die_type_node->signature);
5290 }
5291 else if (AT_ref (a)->die_id.die_symbol)
5292 fprintf (outfile, "die -> label: %s",
5293 AT_ref (a)->die_id.die_symbol);
5294 else
5295 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5296 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5297 }
5298 else
5299 fprintf (outfile, "die -> <null>");
5300 break;
5301 case dw_val_class_vms_delta:
5302 fprintf (outfile, "delta: @slotcount(%s-%s)",
5303 AT_vms_delta2 (a), AT_vms_delta1 (a));
5304 break;
5305 case dw_val_class_lbl_id:
5306 case dw_val_class_lineptr:
5307 case dw_val_class_macptr:
5308 case dw_val_class_high_pc:
5309 fprintf (outfile, "label: %s", AT_lbl (a));
5310 break;
5311 case dw_val_class_str:
5312 if (AT_string (a) != NULL)
5313 fprintf (outfile, "\"%s\"", AT_string (a));
5314 else
5315 fprintf (outfile, "<null>");
5316 break;
5317 case dw_val_class_file:
5318 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5319 AT_file (a)->emitted_number);
5320 break;
5321 case dw_val_class_data8:
5322 {
5323 int i;
5324
5325 for (i = 0; i < 8; i++)
5326 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5327 break;
5328 }
5329 default:
5330 break;
5331 }
5332
5333 fprintf (outfile, "\n");
5334 }
5335
5336 if (die->die_child != NULL)
5337 {
5338 print_indent += 4;
5339 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5340 print_indent -= 4;
5341 }
5342 if (print_indent == 0)
5343 fprintf (outfile, "\n");
5344 }
5345
5346 /* Print the information collected for a given DIE. */
5347
5348 DEBUG_FUNCTION void
5349 debug_dwarf_die (dw_die_ref die)
5350 {
5351 print_die (die, stderr);
5352 }
5353
5354 DEBUG_FUNCTION void
5355 debug (die_struct &ref)
5356 {
5357 print_die (&ref, stderr);
5358 }
5359
5360 DEBUG_FUNCTION void
5361 debug (die_struct *ptr)
5362 {
5363 if (ptr)
5364 debug (*ptr);
5365 else
5366 fprintf (stderr, "<nil>\n");
5367 }
5368
5369
5370 /* Print all DWARF information collected for the compilation unit.
5371 This routine is a debugging aid only. */
5372
5373 DEBUG_FUNCTION void
5374 debug_dwarf (void)
5375 {
5376 print_indent = 0;
5377 print_die (comp_unit_die (), stderr);
5378 }
5379 \f
5380 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5381 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5382 DIE that marks the start of the DIEs for this include file. */
5383
5384 static dw_die_ref
5385 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5386 {
5387 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5388 dw_die_ref new_unit = gen_compile_unit_die (filename);
5389
5390 new_unit->die_sib = old_unit;
5391 return new_unit;
5392 }
5393
5394 /* Close an include-file CU and reopen the enclosing one. */
5395
5396 static dw_die_ref
5397 pop_compile_unit (dw_die_ref old_unit)
5398 {
5399 dw_die_ref new_unit = old_unit->die_sib;
5400
5401 old_unit->die_sib = NULL;
5402 return new_unit;
5403 }
5404
5405 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5406 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5407
5408 /* Calculate the checksum of a location expression. */
5409
5410 static inline void
5411 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5412 {
5413 int tem;
5414
5415 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5416 CHECKSUM (tem);
5417 CHECKSUM (loc->dw_loc_oprnd1);
5418 CHECKSUM (loc->dw_loc_oprnd2);
5419 }
5420
5421 /* Calculate the checksum of an attribute. */
5422
5423 static void
5424 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5425 {
5426 dw_loc_descr_ref loc;
5427 rtx r;
5428
5429 CHECKSUM (at->dw_attr);
5430
5431 /* We don't care that this was compiled with a different compiler
5432 snapshot; if the output is the same, that's what matters. */
5433 if (at->dw_attr == DW_AT_producer)
5434 return;
5435
5436 switch (AT_class (at))
5437 {
5438 case dw_val_class_const:
5439 CHECKSUM (at->dw_attr_val.v.val_int);
5440 break;
5441 case dw_val_class_unsigned_const:
5442 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5443 break;
5444 case dw_val_class_const_double:
5445 CHECKSUM (at->dw_attr_val.v.val_double);
5446 break;
5447 case dw_val_class_vec:
5448 CHECKSUM (at->dw_attr_val.v.val_vec);
5449 break;
5450 case dw_val_class_flag:
5451 CHECKSUM (at->dw_attr_val.v.val_flag);
5452 break;
5453 case dw_val_class_str:
5454 CHECKSUM_STRING (AT_string (at));
5455 break;
5456
5457 case dw_val_class_addr:
5458 r = AT_addr (at);
5459 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5460 CHECKSUM_STRING (XSTR (r, 0));
5461 break;
5462
5463 case dw_val_class_offset:
5464 CHECKSUM (at->dw_attr_val.v.val_offset);
5465 break;
5466
5467 case dw_val_class_loc:
5468 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5469 loc_checksum (loc, ctx);
5470 break;
5471
5472 case dw_val_class_die_ref:
5473 die_checksum (AT_ref (at), ctx, mark);
5474 break;
5475
5476 case dw_val_class_fde_ref:
5477 case dw_val_class_vms_delta:
5478 case dw_val_class_lbl_id:
5479 case dw_val_class_lineptr:
5480 case dw_val_class_macptr:
5481 case dw_val_class_high_pc:
5482 break;
5483
5484 case dw_val_class_file:
5485 CHECKSUM_STRING (AT_file (at)->filename);
5486 break;
5487
5488 case dw_val_class_data8:
5489 CHECKSUM (at->dw_attr_val.v.val_data8);
5490 break;
5491
5492 default:
5493 break;
5494 }
5495 }
5496
5497 /* Calculate the checksum of a DIE. */
5498
5499 static void
5500 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5501 {
5502 dw_die_ref c;
5503 dw_attr_ref a;
5504 unsigned ix;
5505
5506 /* To avoid infinite recursion. */
5507 if (die->die_mark)
5508 {
5509 CHECKSUM (die->die_mark);
5510 return;
5511 }
5512 die->die_mark = ++(*mark);
5513
5514 CHECKSUM (die->die_tag);
5515
5516 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5517 attr_checksum (a, ctx, mark);
5518
5519 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5520 }
5521
5522 #undef CHECKSUM
5523 #undef CHECKSUM_STRING
5524
5525 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5526 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5527 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5528 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5529 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5530 #define CHECKSUM_ATTR(FOO) \
5531 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5532
5533 /* Calculate the checksum of a number in signed LEB128 format. */
5534
5535 static void
5536 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5537 {
5538 unsigned char byte;
5539 bool more;
5540
5541 while (1)
5542 {
5543 byte = (value & 0x7f);
5544 value >>= 7;
5545 more = !((value == 0 && (byte & 0x40) == 0)
5546 || (value == -1 && (byte & 0x40) != 0));
5547 if (more)
5548 byte |= 0x80;
5549 CHECKSUM (byte);
5550 if (!more)
5551 break;
5552 }
5553 }
5554
5555 /* Calculate the checksum of a number in unsigned LEB128 format. */
5556
5557 static void
5558 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5559 {
5560 while (1)
5561 {
5562 unsigned char byte = (value & 0x7f);
5563 value >>= 7;
5564 if (value != 0)
5565 /* More bytes to follow. */
5566 byte |= 0x80;
5567 CHECKSUM (byte);
5568 if (value == 0)
5569 break;
5570 }
5571 }
5572
5573 /* Checksum the context of the DIE. This adds the names of any
5574 surrounding namespaces or structures to the checksum. */
5575
5576 static void
5577 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5578 {
5579 const char *name;
5580 dw_die_ref spec;
5581 int tag = die->die_tag;
5582
5583 if (tag != DW_TAG_namespace
5584 && tag != DW_TAG_structure_type
5585 && tag != DW_TAG_class_type)
5586 return;
5587
5588 name = get_AT_string (die, DW_AT_name);
5589
5590 spec = get_AT_ref (die, DW_AT_specification);
5591 if (spec != NULL)
5592 die = spec;
5593
5594 if (die->die_parent != NULL)
5595 checksum_die_context (die->die_parent, ctx);
5596
5597 CHECKSUM_ULEB128 ('C');
5598 CHECKSUM_ULEB128 (tag);
5599 if (name != NULL)
5600 CHECKSUM_STRING (name);
5601 }
5602
5603 /* Calculate the checksum of a location expression. */
5604
5605 static inline void
5606 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5607 {
5608 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5609 were emitted as a DW_FORM_sdata instead of a location expression. */
5610 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5611 {
5612 CHECKSUM_ULEB128 (DW_FORM_sdata);
5613 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5614 return;
5615 }
5616
5617 /* Otherwise, just checksum the raw location expression. */
5618 while (loc != NULL)
5619 {
5620 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5621 CHECKSUM (loc->dw_loc_oprnd1);
5622 CHECKSUM (loc->dw_loc_oprnd2);
5623 loc = loc->dw_loc_next;
5624 }
5625 }
5626
5627 /* Calculate the checksum of an attribute. */
5628
5629 static void
5630 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5631 struct md5_ctx *ctx, int *mark)
5632 {
5633 dw_loc_descr_ref loc;
5634 rtx r;
5635
5636 if (AT_class (at) == dw_val_class_die_ref)
5637 {
5638 dw_die_ref target_die = AT_ref (at);
5639
5640 /* For pointer and reference types, we checksum only the (qualified)
5641 name of the target type (if there is a name). For friend entries,
5642 we checksum only the (qualified) name of the target type or function.
5643 This allows the checksum to remain the same whether the target type
5644 is complete or not. */
5645 if ((at->dw_attr == DW_AT_type
5646 && (tag == DW_TAG_pointer_type
5647 || tag == DW_TAG_reference_type
5648 || tag == DW_TAG_rvalue_reference_type
5649 || tag == DW_TAG_ptr_to_member_type))
5650 || (at->dw_attr == DW_AT_friend
5651 && tag == DW_TAG_friend))
5652 {
5653 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5654
5655 if (name_attr != NULL)
5656 {
5657 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5658
5659 if (decl == NULL)
5660 decl = target_die;
5661 CHECKSUM_ULEB128 ('N');
5662 CHECKSUM_ULEB128 (at->dw_attr);
5663 if (decl->die_parent != NULL)
5664 checksum_die_context (decl->die_parent, ctx);
5665 CHECKSUM_ULEB128 ('E');
5666 CHECKSUM_STRING (AT_string (name_attr));
5667 return;
5668 }
5669 }
5670
5671 /* For all other references to another DIE, we check to see if the
5672 target DIE has already been visited. If it has, we emit a
5673 backward reference; if not, we descend recursively. */
5674 if (target_die->die_mark > 0)
5675 {
5676 CHECKSUM_ULEB128 ('R');
5677 CHECKSUM_ULEB128 (at->dw_attr);
5678 CHECKSUM_ULEB128 (target_die->die_mark);
5679 }
5680 else
5681 {
5682 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5683
5684 if (decl == NULL)
5685 decl = target_die;
5686 target_die->die_mark = ++(*mark);
5687 CHECKSUM_ULEB128 ('T');
5688 CHECKSUM_ULEB128 (at->dw_attr);
5689 if (decl->die_parent != NULL)
5690 checksum_die_context (decl->die_parent, ctx);
5691 die_checksum_ordered (target_die, ctx, mark);
5692 }
5693 return;
5694 }
5695
5696 CHECKSUM_ULEB128 ('A');
5697 CHECKSUM_ULEB128 (at->dw_attr);
5698
5699 switch (AT_class (at))
5700 {
5701 case dw_val_class_const:
5702 CHECKSUM_ULEB128 (DW_FORM_sdata);
5703 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5704 break;
5705
5706 case dw_val_class_unsigned_const:
5707 CHECKSUM_ULEB128 (DW_FORM_sdata);
5708 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5709 break;
5710
5711 case dw_val_class_const_double:
5712 CHECKSUM_ULEB128 (DW_FORM_block);
5713 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5714 CHECKSUM (at->dw_attr_val.v.val_double);
5715 break;
5716
5717 case dw_val_class_vec:
5718 CHECKSUM_ULEB128 (DW_FORM_block);
5719 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5720 CHECKSUM (at->dw_attr_val.v.val_vec);
5721 break;
5722
5723 case dw_val_class_flag:
5724 CHECKSUM_ULEB128 (DW_FORM_flag);
5725 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5726 break;
5727
5728 case dw_val_class_str:
5729 CHECKSUM_ULEB128 (DW_FORM_string);
5730 CHECKSUM_STRING (AT_string (at));
5731 break;
5732
5733 case dw_val_class_addr:
5734 r = AT_addr (at);
5735 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5736 CHECKSUM_ULEB128 (DW_FORM_string);
5737 CHECKSUM_STRING (XSTR (r, 0));
5738 break;
5739
5740 case dw_val_class_offset:
5741 CHECKSUM_ULEB128 (DW_FORM_sdata);
5742 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5743 break;
5744
5745 case dw_val_class_loc:
5746 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5747 loc_checksum_ordered (loc, ctx);
5748 break;
5749
5750 case dw_val_class_fde_ref:
5751 case dw_val_class_lbl_id:
5752 case dw_val_class_lineptr:
5753 case dw_val_class_macptr:
5754 case dw_val_class_high_pc:
5755 break;
5756
5757 case dw_val_class_file:
5758 CHECKSUM_ULEB128 (DW_FORM_string);
5759 CHECKSUM_STRING (AT_file (at)->filename);
5760 break;
5761
5762 case dw_val_class_data8:
5763 CHECKSUM (at->dw_attr_val.v.val_data8);
5764 break;
5765
5766 default:
5767 break;
5768 }
5769 }
5770
5771 struct checksum_attributes
5772 {
5773 dw_attr_ref at_name;
5774 dw_attr_ref at_type;
5775 dw_attr_ref at_friend;
5776 dw_attr_ref at_accessibility;
5777 dw_attr_ref at_address_class;
5778 dw_attr_ref at_allocated;
5779 dw_attr_ref at_artificial;
5780 dw_attr_ref at_associated;
5781 dw_attr_ref at_binary_scale;
5782 dw_attr_ref at_bit_offset;
5783 dw_attr_ref at_bit_size;
5784 dw_attr_ref at_bit_stride;
5785 dw_attr_ref at_byte_size;
5786 dw_attr_ref at_byte_stride;
5787 dw_attr_ref at_const_value;
5788 dw_attr_ref at_containing_type;
5789 dw_attr_ref at_count;
5790 dw_attr_ref at_data_location;
5791 dw_attr_ref at_data_member_location;
5792 dw_attr_ref at_decimal_scale;
5793 dw_attr_ref at_decimal_sign;
5794 dw_attr_ref at_default_value;
5795 dw_attr_ref at_digit_count;
5796 dw_attr_ref at_discr;
5797 dw_attr_ref at_discr_list;
5798 dw_attr_ref at_discr_value;
5799 dw_attr_ref at_encoding;
5800 dw_attr_ref at_endianity;
5801 dw_attr_ref at_explicit;
5802 dw_attr_ref at_is_optional;
5803 dw_attr_ref at_location;
5804 dw_attr_ref at_lower_bound;
5805 dw_attr_ref at_mutable;
5806 dw_attr_ref at_ordering;
5807 dw_attr_ref at_picture_string;
5808 dw_attr_ref at_prototyped;
5809 dw_attr_ref at_small;
5810 dw_attr_ref at_segment;
5811 dw_attr_ref at_string_length;
5812 dw_attr_ref at_threads_scaled;
5813 dw_attr_ref at_upper_bound;
5814 dw_attr_ref at_use_location;
5815 dw_attr_ref at_use_UTF8;
5816 dw_attr_ref at_variable_parameter;
5817 dw_attr_ref at_virtuality;
5818 dw_attr_ref at_visibility;
5819 dw_attr_ref at_vtable_elem_location;
5820 };
5821
5822 /* Collect the attributes that we will want to use for the checksum. */
5823
5824 static void
5825 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5826 {
5827 dw_attr_ref a;
5828 unsigned ix;
5829
5830 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5831 {
5832 switch (a->dw_attr)
5833 {
5834 case DW_AT_name:
5835 attrs->at_name = a;
5836 break;
5837 case DW_AT_type:
5838 attrs->at_type = a;
5839 break;
5840 case DW_AT_friend:
5841 attrs->at_friend = a;
5842 break;
5843 case DW_AT_accessibility:
5844 attrs->at_accessibility = a;
5845 break;
5846 case DW_AT_address_class:
5847 attrs->at_address_class = a;
5848 break;
5849 case DW_AT_allocated:
5850 attrs->at_allocated = a;
5851 break;
5852 case DW_AT_artificial:
5853 attrs->at_artificial = a;
5854 break;
5855 case DW_AT_associated:
5856 attrs->at_associated = a;
5857 break;
5858 case DW_AT_binary_scale:
5859 attrs->at_binary_scale = a;
5860 break;
5861 case DW_AT_bit_offset:
5862 attrs->at_bit_offset = a;
5863 break;
5864 case DW_AT_bit_size:
5865 attrs->at_bit_size = a;
5866 break;
5867 case DW_AT_bit_stride:
5868 attrs->at_bit_stride = a;
5869 break;
5870 case DW_AT_byte_size:
5871 attrs->at_byte_size = a;
5872 break;
5873 case DW_AT_byte_stride:
5874 attrs->at_byte_stride = a;
5875 break;
5876 case DW_AT_const_value:
5877 attrs->at_const_value = a;
5878 break;
5879 case DW_AT_containing_type:
5880 attrs->at_containing_type = a;
5881 break;
5882 case DW_AT_count:
5883 attrs->at_count = a;
5884 break;
5885 case DW_AT_data_location:
5886 attrs->at_data_location = a;
5887 break;
5888 case DW_AT_data_member_location:
5889 attrs->at_data_member_location = a;
5890 break;
5891 case DW_AT_decimal_scale:
5892 attrs->at_decimal_scale = a;
5893 break;
5894 case DW_AT_decimal_sign:
5895 attrs->at_decimal_sign = a;
5896 break;
5897 case DW_AT_default_value:
5898 attrs->at_default_value = a;
5899 break;
5900 case DW_AT_digit_count:
5901 attrs->at_digit_count = a;
5902 break;
5903 case DW_AT_discr:
5904 attrs->at_discr = a;
5905 break;
5906 case DW_AT_discr_list:
5907 attrs->at_discr_list = a;
5908 break;
5909 case DW_AT_discr_value:
5910 attrs->at_discr_value = a;
5911 break;
5912 case DW_AT_encoding:
5913 attrs->at_encoding = a;
5914 break;
5915 case DW_AT_endianity:
5916 attrs->at_endianity = a;
5917 break;
5918 case DW_AT_explicit:
5919 attrs->at_explicit = a;
5920 break;
5921 case DW_AT_is_optional:
5922 attrs->at_is_optional = a;
5923 break;
5924 case DW_AT_location:
5925 attrs->at_location = a;
5926 break;
5927 case DW_AT_lower_bound:
5928 attrs->at_lower_bound = a;
5929 break;
5930 case DW_AT_mutable:
5931 attrs->at_mutable = a;
5932 break;
5933 case DW_AT_ordering:
5934 attrs->at_ordering = a;
5935 break;
5936 case DW_AT_picture_string:
5937 attrs->at_picture_string = a;
5938 break;
5939 case DW_AT_prototyped:
5940 attrs->at_prototyped = a;
5941 break;
5942 case DW_AT_small:
5943 attrs->at_small = a;
5944 break;
5945 case DW_AT_segment:
5946 attrs->at_segment = a;
5947 break;
5948 case DW_AT_string_length:
5949 attrs->at_string_length = a;
5950 break;
5951 case DW_AT_threads_scaled:
5952 attrs->at_threads_scaled = a;
5953 break;
5954 case DW_AT_upper_bound:
5955 attrs->at_upper_bound = a;
5956 break;
5957 case DW_AT_use_location:
5958 attrs->at_use_location = a;
5959 break;
5960 case DW_AT_use_UTF8:
5961 attrs->at_use_UTF8 = a;
5962 break;
5963 case DW_AT_variable_parameter:
5964 attrs->at_variable_parameter = a;
5965 break;
5966 case DW_AT_virtuality:
5967 attrs->at_virtuality = a;
5968 break;
5969 case DW_AT_visibility:
5970 attrs->at_visibility = a;
5971 break;
5972 case DW_AT_vtable_elem_location:
5973 attrs->at_vtable_elem_location = a;
5974 break;
5975 default:
5976 break;
5977 }
5978 }
5979 }
5980
5981 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
5982
5983 static void
5984 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5985 {
5986 dw_die_ref c;
5987 dw_die_ref decl;
5988 struct checksum_attributes attrs;
5989
5990 CHECKSUM_ULEB128 ('D');
5991 CHECKSUM_ULEB128 (die->die_tag);
5992
5993 memset (&attrs, 0, sizeof (attrs));
5994
5995 decl = get_AT_ref (die, DW_AT_specification);
5996 if (decl != NULL)
5997 collect_checksum_attributes (&attrs, decl);
5998 collect_checksum_attributes (&attrs, die);
5999
6000 CHECKSUM_ATTR (attrs.at_name);
6001 CHECKSUM_ATTR (attrs.at_accessibility);
6002 CHECKSUM_ATTR (attrs.at_address_class);
6003 CHECKSUM_ATTR (attrs.at_allocated);
6004 CHECKSUM_ATTR (attrs.at_artificial);
6005 CHECKSUM_ATTR (attrs.at_associated);
6006 CHECKSUM_ATTR (attrs.at_binary_scale);
6007 CHECKSUM_ATTR (attrs.at_bit_offset);
6008 CHECKSUM_ATTR (attrs.at_bit_size);
6009 CHECKSUM_ATTR (attrs.at_bit_stride);
6010 CHECKSUM_ATTR (attrs.at_byte_size);
6011 CHECKSUM_ATTR (attrs.at_byte_stride);
6012 CHECKSUM_ATTR (attrs.at_const_value);
6013 CHECKSUM_ATTR (attrs.at_containing_type);
6014 CHECKSUM_ATTR (attrs.at_count);
6015 CHECKSUM_ATTR (attrs.at_data_location);
6016 CHECKSUM_ATTR (attrs.at_data_member_location);
6017 CHECKSUM_ATTR (attrs.at_decimal_scale);
6018 CHECKSUM_ATTR (attrs.at_decimal_sign);
6019 CHECKSUM_ATTR (attrs.at_default_value);
6020 CHECKSUM_ATTR (attrs.at_digit_count);
6021 CHECKSUM_ATTR (attrs.at_discr);
6022 CHECKSUM_ATTR (attrs.at_discr_list);
6023 CHECKSUM_ATTR (attrs.at_discr_value);
6024 CHECKSUM_ATTR (attrs.at_encoding);
6025 CHECKSUM_ATTR (attrs.at_endianity);
6026 CHECKSUM_ATTR (attrs.at_explicit);
6027 CHECKSUM_ATTR (attrs.at_is_optional);
6028 CHECKSUM_ATTR (attrs.at_location);
6029 CHECKSUM_ATTR (attrs.at_lower_bound);
6030 CHECKSUM_ATTR (attrs.at_mutable);
6031 CHECKSUM_ATTR (attrs.at_ordering);
6032 CHECKSUM_ATTR (attrs.at_picture_string);
6033 CHECKSUM_ATTR (attrs.at_prototyped);
6034 CHECKSUM_ATTR (attrs.at_small);
6035 CHECKSUM_ATTR (attrs.at_segment);
6036 CHECKSUM_ATTR (attrs.at_string_length);
6037 CHECKSUM_ATTR (attrs.at_threads_scaled);
6038 CHECKSUM_ATTR (attrs.at_upper_bound);
6039 CHECKSUM_ATTR (attrs.at_use_location);
6040 CHECKSUM_ATTR (attrs.at_use_UTF8);
6041 CHECKSUM_ATTR (attrs.at_variable_parameter);
6042 CHECKSUM_ATTR (attrs.at_virtuality);
6043 CHECKSUM_ATTR (attrs.at_visibility);
6044 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6045 CHECKSUM_ATTR (attrs.at_type);
6046 CHECKSUM_ATTR (attrs.at_friend);
6047
6048 /* Checksum the child DIEs, except for nested types and member functions. */
6049 c = die->die_child;
6050 if (c) do {
6051 dw_attr_ref name_attr;
6052
6053 c = c->die_sib;
6054 name_attr = get_AT (c, DW_AT_name);
6055 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6056 && name_attr != NULL)
6057 {
6058 CHECKSUM_ULEB128 ('S');
6059 CHECKSUM_ULEB128 (c->die_tag);
6060 CHECKSUM_STRING (AT_string (name_attr));
6061 }
6062 else
6063 {
6064 /* Mark this DIE so it gets processed when unmarking. */
6065 if (c->die_mark == 0)
6066 c->die_mark = -1;
6067 die_checksum_ordered (c, ctx, mark);
6068 }
6069 } while (c != die->die_child);
6070
6071 CHECKSUM_ULEB128 (0);
6072 }
6073
6074 #undef CHECKSUM
6075 #undef CHECKSUM_STRING
6076 #undef CHECKSUM_ATTR
6077 #undef CHECKSUM_LEB128
6078 #undef CHECKSUM_ULEB128
6079
6080 /* Generate the type signature for DIE. This is computed by generating an
6081 MD5 checksum over the DIE's tag, its relevant attributes, and its
6082 children. Attributes that are references to other DIEs are processed
6083 by recursion, using the MARK field to prevent infinite recursion.
6084 If the DIE is nested inside a namespace or another type, we also
6085 need to include that context in the signature. The lower 64 bits
6086 of the resulting MD5 checksum comprise the signature. */
6087
6088 static void
6089 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6090 {
6091 int mark;
6092 const char *name;
6093 unsigned char checksum[16];
6094 struct md5_ctx ctx;
6095 dw_die_ref decl;
6096 dw_die_ref parent;
6097
6098 name = get_AT_string (die, DW_AT_name);
6099 decl = get_AT_ref (die, DW_AT_specification);
6100 parent = get_die_parent (die);
6101
6102 /* First, compute a signature for just the type name (and its surrounding
6103 context, if any. This is stored in the type unit DIE for link-time
6104 ODR (one-definition rule) checking. */
6105
6106 if (is_cxx() && name != NULL)
6107 {
6108 md5_init_ctx (&ctx);
6109
6110 /* Checksum the names of surrounding namespaces and structures. */
6111 if (parent != NULL)
6112 checksum_die_context (parent, &ctx);
6113
6114 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6115 md5_process_bytes (name, strlen (name) + 1, &ctx);
6116 md5_finish_ctx (&ctx, checksum);
6117
6118 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6119 }
6120
6121 /* Next, compute the complete type signature. */
6122
6123 md5_init_ctx (&ctx);
6124 mark = 1;
6125 die->die_mark = mark;
6126
6127 /* Checksum the names of surrounding namespaces and structures. */
6128 if (parent != NULL)
6129 checksum_die_context (parent, &ctx);
6130
6131 /* Checksum the DIE and its children. */
6132 die_checksum_ordered (die, &ctx, &mark);
6133 unmark_all_dies (die);
6134 md5_finish_ctx (&ctx, checksum);
6135
6136 /* Store the signature in the type node and link the type DIE and the
6137 type node together. */
6138 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6139 DWARF_TYPE_SIGNATURE_SIZE);
6140 die->comdat_type_p = true;
6141 die->die_id.die_type_node = type_node;
6142 type_node->type_die = die;
6143
6144 /* If the DIE is a specification, link its declaration to the type node
6145 as well. */
6146 if (decl != NULL)
6147 {
6148 decl->comdat_type_p = true;
6149 decl->die_id.die_type_node = type_node;
6150 }
6151 }
6152
6153 /* Do the location expressions look same? */
6154 static inline int
6155 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6156 {
6157 return loc1->dw_loc_opc == loc2->dw_loc_opc
6158 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6159 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6160 }
6161
6162 /* Do the values look the same? */
6163 static int
6164 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6165 {
6166 dw_loc_descr_ref loc1, loc2;
6167 rtx r1, r2;
6168
6169 if (v1->val_class != v2->val_class)
6170 return 0;
6171
6172 switch (v1->val_class)
6173 {
6174 case dw_val_class_const:
6175 return v1->v.val_int == v2->v.val_int;
6176 case dw_val_class_unsigned_const:
6177 return v1->v.val_unsigned == v2->v.val_unsigned;
6178 case dw_val_class_const_double:
6179 return v1->v.val_double.high == v2->v.val_double.high
6180 && v1->v.val_double.low == v2->v.val_double.low;
6181 case dw_val_class_vec:
6182 if (v1->v.val_vec.length != v2->v.val_vec.length
6183 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6184 return 0;
6185 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6186 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6187 return 0;
6188 return 1;
6189 case dw_val_class_flag:
6190 return v1->v.val_flag == v2->v.val_flag;
6191 case dw_val_class_str:
6192 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6193
6194 case dw_val_class_addr:
6195 r1 = v1->v.val_addr;
6196 r2 = v2->v.val_addr;
6197 if (GET_CODE (r1) != GET_CODE (r2))
6198 return 0;
6199 return !rtx_equal_p (r1, r2);
6200
6201 case dw_val_class_offset:
6202 return v1->v.val_offset == v2->v.val_offset;
6203
6204 case dw_val_class_loc:
6205 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6206 loc1 && loc2;
6207 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6208 if (!same_loc_p (loc1, loc2, mark))
6209 return 0;
6210 return !loc1 && !loc2;
6211
6212 case dw_val_class_die_ref:
6213 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6214
6215 case dw_val_class_fde_ref:
6216 case dw_val_class_vms_delta:
6217 case dw_val_class_lbl_id:
6218 case dw_val_class_lineptr:
6219 case dw_val_class_macptr:
6220 case dw_val_class_high_pc:
6221 return 1;
6222
6223 case dw_val_class_file:
6224 return v1->v.val_file == v2->v.val_file;
6225
6226 case dw_val_class_data8:
6227 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6228
6229 default:
6230 return 1;
6231 }
6232 }
6233
6234 /* Do the attributes look the same? */
6235
6236 static int
6237 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6238 {
6239 if (at1->dw_attr != at2->dw_attr)
6240 return 0;
6241
6242 /* We don't care that this was compiled with a different compiler
6243 snapshot; if the output is the same, that's what matters. */
6244 if (at1->dw_attr == DW_AT_producer)
6245 return 1;
6246
6247 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6248 }
6249
6250 /* Do the dies look the same? */
6251
6252 static int
6253 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6254 {
6255 dw_die_ref c1, c2;
6256 dw_attr_ref a1;
6257 unsigned ix;
6258
6259 /* To avoid infinite recursion. */
6260 if (die1->die_mark)
6261 return die1->die_mark == die2->die_mark;
6262 die1->die_mark = die2->die_mark = ++(*mark);
6263
6264 if (die1->die_tag != die2->die_tag)
6265 return 0;
6266
6267 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6268 return 0;
6269
6270 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6271 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6272 return 0;
6273
6274 c1 = die1->die_child;
6275 c2 = die2->die_child;
6276 if (! c1)
6277 {
6278 if (c2)
6279 return 0;
6280 }
6281 else
6282 for (;;)
6283 {
6284 if (!same_die_p (c1, c2, mark))
6285 return 0;
6286 c1 = c1->die_sib;
6287 c2 = c2->die_sib;
6288 if (c1 == die1->die_child)
6289 {
6290 if (c2 == die2->die_child)
6291 break;
6292 else
6293 return 0;
6294 }
6295 }
6296
6297 return 1;
6298 }
6299
6300 /* Do the dies look the same? Wrapper around same_die_p. */
6301
6302 static int
6303 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6304 {
6305 int mark = 0;
6306 int ret = same_die_p (die1, die2, &mark);
6307
6308 unmark_all_dies (die1);
6309 unmark_all_dies (die2);
6310
6311 return ret;
6312 }
6313
6314 /* The prefix to attach to symbols on DIEs in the current comdat debug
6315 info section. */
6316 static const char *comdat_symbol_id;
6317
6318 /* The index of the current symbol within the current comdat CU. */
6319 static unsigned int comdat_symbol_number;
6320
6321 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6322 children, and set comdat_symbol_id accordingly. */
6323
6324 static void
6325 compute_section_prefix (dw_die_ref unit_die)
6326 {
6327 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6328 const char *base = die_name ? lbasename (die_name) : "anonymous";
6329 char *name = XALLOCAVEC (char, strlen (base) + 64);
6330 char *p;
6331 int i, mark;
6332 unsigned char checksum[16];
6333 struct md5_ctx ctx;
6334
6335 /* Compute the checksum of the DIE, then append part of it as hex digits to
6336 the name filename of the unit. */
6337
6338 md5_init_ctx (&ctx);
6339 mark = 0;
6340 die_checksum (unit_die, &ctx, &mark);
6341 unmark_all_dies (unit_die);
6342 md5_finish_ctx (&ctx, checksum);
6343
6344 sprintf (name, "%s.", base);
6345 clean_symbol_name (name);
6346
6347 p = name + strlen (name);
6348 for (i = 0; i < 4; i++)
6349 {
6350 sprintf (p, "%.2x", checksum[i]);
6351 p += 2;
6352 }
6353
6354 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6355 comdat_symbol_number = 0;
6356 }
6357
6358 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6359
6360 static int
6361 is_type_die (dw_die_ref die)
6362 {
6363 switch (die->die_tag)
6364 {
6365 case DW_TAG_array_type:
6366 case DW_TAG_class_type:
6367 case DW_TAG_interface_type:
6368 case DW_TAG_enumeration_type:
6369 case DW_TAG_pointer_type:
6370 case DW_TAG_reference_type:
6371 case DW_TAG_rvalue_reference_type:
6372 case DW_TAG_string_type:
6373 case DW_TAG_structure_type:
6374 case DW_TAG_subroutine_type:
6375 case DW_TAG_union_type:
6376 case DW_TAG_ptr_to_member_type:
6377 case DW_TAG_set_type:
6378 case DW_TAG_subrange_type:
6379 case DW_TAG_base_type:
6380 case DW_TAG_const_type:
6381 case DW_TAG_file_type:
6382 case DW_TAG_packed_type:
6383 case DW_TAG_volatile_type:
6384 case DW_TAG_typedef:
6385 return 1;
6386 default:
6387 return 0;
6388 }
6389 }
6390
6391 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6392 Basically, we want to choose the bits that are likely to be shared between
6393 compilations (types) and leave out the bits that are specific to individual
6394 compilations (functions). */
6395
6396 static int
6397 is_comdat_die (dw_die_ref c)
6398 {
6399 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6400 we do for stabs. The advantage is a greater likelihood of sharing between
6401 objects that don't include headers in the same order (and therefore would
6402 put the base types in a different comdat). jason 8/28/00 */
6403
6404 if (c->die_tag == DW_TAG_base_type)
6405 return 0;
6406
6407 if (c->die_tag == DW_TAG_pointer_type
6408 || c->die_tag == DW_TAG_reference_type
6409 || c->die_tag == DW_TAG_rvalue_reference_type
6410 || c->die_tag == DW_TAG_const_type
6411 || c->die_tag == DW_TAG_volatile_type)
6412 {
6413 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6414
6415 return t ? is_comdat_die (t) : 0;
6416 }
6417
6418 return is_type_die (c);
6419 }
6420
6421 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6422 compilation unit. */
6423
6424 static int
6425 is_symbol_die (dw_die_ref c)
6426 {
6427 return (is_type_die (c)
6428 || is_declaration_die (c)
6429 || c->die_tag == DW_TAG_namespace
6430 || c->die_tag == DW_TAG_module);
6431 }
6432
6433 /* Returns true iff C is a compile-unit DIE. */
6434
6435 static inline bool
6436 is_cu_die (dw_die_ref c)
6437 {
6438 return c && c->die_tag == DW_TAG_compile_unit;
6439 }
6440
6441 /* Returns true iff C is a unit DIE of some sort. */
6442
6443 static inline bool
6444 is_unit_die (dw_die_ref c)
6445 {
6446 return c && (c->die_tag == DW_TAG_compile_unit
6447 || c->die_tag == DW_TAG_partial_unit
6448 || c->die_tag == DW_TAG_type_unit);
6449 }
6450
6451 /* Returns true iff C is a namespace DIE. */
6452
6453 static inline bool
6454 is_namespace_die (dw_die_ref c)
6455 {
6456 return c && c->die_tag == DW_TAG_namespace;
6457 }
6458
6459 /* Returns true iff C is a class or structure DIE. */
6460
6461 static inline bool
6462 is_class_die (dw_die_ref c)
6463 {
6464 return c && (c->die_tag == DW_TAG_class_type
6465 || c->die_tag == DW_TAG_structure_type);
6466 }
6467
6468 static char *
6469 gen_internal_sym (const char *prefix)
6470 {
6471 char buf[256];
6472
6473 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6474 return xstrdup (buf);
6475 }
6476
6477 /* Assign symbols to all worthy DIEs under DIE. */
6478
6479 static void
6480 assign_symbol_names (dw_die_ref die)
6481 {
6482 dw_die_ref c;
6483
6484 if (is_symbol_die (die) && !die->comdat_type_p)
6485 {
6486 if (comdat_symbol_id)
6487 {
6488 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6489
6490 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6491 comdat_symbol_id, comdat_symbol_number++);
6492 die->die_id.die_symbol = xstrdup (p);
6493 }
6494 else
6495 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6496 }
6497
6498 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6499 }
6500
6501 struct cu_hash_table_entry
6502 {
6503 dw_die_ref cu;
6504 unsigned min_comdat_num, max_comdat_num;
6505 struct cu_hash_table_entry *next;
6506 };
6507
6508 /* Routines to manipulate hash table of CUs. */
6509 static hashval_t
6510 htab_cu_hash (const void *of)
6511 {
6512 const struct cu_hash_table_entry *const entry =
6513 (const struct cu_hash_table_entry *) of;
6514
6515 return htab_hash_string (entry->cu->die_id.die_symbol);
6516 }
6517
6518 static int
6519 htab_cu_eq (const void *of1, const void *of2)
6520 {
6521 const struct cu_hash_table_entry *const entry1 =
6522 (const struct cu_hash_table_entry *) of1;
6523 const struct die_struct *const entry2 = (const struct die_struct *) of2;
6524
6525 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6526 }
6527
6528 static void
6529 htab_cu_del (void *what)
6530 {
6531 struct cu_hash_table_entry *next,
6532 *entry = (struct cu_hash_table_entry *) what;
6533
6534 while (entry)
6535 {
6536 next = entry->next;
6537 free (entry);
6538 entry = next;
6539 }
6540 }
6541
6542 /* Check whether we have already seen this CU and set up SYM_NUM
6543 accordingly. */
6544 static int
6545 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6546 {
6547 struct cu_hash_table_entry dummy;
6548 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6549
6550 dummy.max_comdat_num = 0;
6551
6552 slot = (struct cu_hash_table_entry **)
6553 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6554 INSERT);
6555 entry = *slot;
6556
6557 for (; entry; last = entry, entry = entry->next)
6558 {
6559 if (same_die_p_wrap (cu, entry->cu))
6560 break;
6561 }
6562
6563 if (entry)
6564 {
6565 *sym_num = entry->min_comdat_num;
6566 return 1;
6567 }
6568
6569 entry = XCNEW (struct cu_hash_table_entry);
6570 entry->cu = cu;
6571 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6572 entry->next = *slot;
6573 *slot = entry;
6574
6575 return 0;
6576 }
6577
6578 /* Record SYM_NUM to record of CU in HTABLE. */
6579 static void
6580 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6581 {
6582 struct cu_hash_table_entry **slot, *entry;
6583
6584 slot = (struct cu_hash_table_entry **)
6585 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6586 NO_INSERT);
6587 entry = *slot;
6588
6589 entry->max_comdat_num = sym_num;
6590 }
6591
6592 /* Traverse the DIE (which is always comp_unit_die), and set up
6593 additional compilation units for each of the include files we see
6594 bracketed by BINCL/EINCL. */
6595
6596 static void
6597 break_out_includes (dw_die_ref die)
6598 {
6599 dw_die_ref c;
6600 dw_die_ref unit = NULL;
6601 limbo_die_node *node, **pnode;
6602 htab_t cu_hash_table;
6603
6604 c = die->die_child;
6605 if (c) do {
6606 dw_die_ref prev = c;
6607 c = c->die_sib;
6608 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6609 || (unit && is_comdat_die (c)))
6610 {
6611 dw_die_ref next = c->die_sib;
6612
6613 /* This DIE is for a secondary CU; remove it from the main one. */
6614 remove_child_with_prev (c, prev);
6615
6616 if (c->die_tag == DW_TAG_GNU_BINCL)
6617 unit = push_new_compile_unit (unit, c);
6618 else if (c->die_tag == DW_TAG_GNU_EINCL)
6619 unit = pop_compile_unit (unit);
6620 else
6621 add_child_die (unit, c);
6622 c = next;
6623 if (c == die->die_child)
6624 break;
6625 }
6626 } while (c != die->die_child);
6627
6628 #if 0
6629 /* We can only use this in debugging, since the frontend doesn't check
6630 to make sure that we leave every include file we enter. */
6631 gcc_assert (!unit);
6632 #endif
6633
6634 assign_symbol_names (die);
6635 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6636 for (node = limbo_die_list, pnode = &limbo_die_list;
6637 node;
6638 node = node->next)
6639 {
6640 int is_dupl;
6641
6642 compute_section_prefix (node->die);
6643 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6644 &comdat_symbol_number);
6645 assign_symbol_names (node->die);
6646 if (is_dupl)
6647 *pnode = node->next;
6648 else
6649 {
6650 pnode = &node->next;
6651 record_comdat_symbol_number (node->die, cu_hash_table,
6652 comdat_symbol_number);
6653 }
6654 }
6655 htab_delete (cu_hash_table);
6656 }
6657
6658 /* Return non-zero if this DIE is a declaration. */
6659
6660 static int
6661 is_declaration_die (dw_die_ref die)
6662 {
6663 dw_attr_ref a;
6664 unsigned ix;
6665
6666 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6667 if (a->dw_attr == DW_AT_declaration)
6668 return 1;
6669
6670 return 0;
6671 }
6672
6673 /* Return non-zero if this DIE is nested inside a subprogram. */
6674
6675 static int
6676 is_nested_in_subprogram (dw_die_ref die)
6677 {
6678 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6679
6680 if (decl == NULL)
6681 decl = die;
6682 return local_scope_p (decl);
6683 }
6684
6685 /* Return non-zero if this DIE contains a defining declaration of a
6686 subprogram. */
6687
6688 static int
6689 contains_subprogram_definition (dw_die_ref die)
6690 {
6691 dw_die_ref c;
6692
6693 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6694 return 1;
6695 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6696 return 0;
6697 }
6698
6699 /* Return non-zero if this is a type DIE that should be moved to a
6700 COMDAT .debug_types section. */
6701
6702 static int
6703 should_move_die_to_comdat (dw_die_ref die)
6704 {
6705 switch (die->die_tag)
6706 {
6707 case DW_TAG_class_type:
6708 case DW_TAG_structure_type:
6709 case DW_TAG_enumeration_type:
6710 case DW_TAG_union_type:
6711 /* Don't move declarations, inlined instances, or types nested in a
6712 subprogram. */
6713 if (is_declaration_die (die)
6714 || get_AT (die, DW_AT_abstract_origin)
6715 || is_nested_in_subprogram (die))
6716 return 0;
6717 /* A type definition should never contain a subprogram definition. */
6718 gcc_assert (!contains_subprogram_definition (die));
6719 return 1;
6720 case DW_TAG_array_type:
6721 case DW_TAG_interface_type:
6722 case DW_TAG_pointer_type:
6723 case DW_TAG_reference_type:
6724 case DW_TAG_rvalue_reference_type:
6725 case DW_TAG_string_type:
6726 case DW_TAG_subroutine_type:
6727 case DW_TAG_ptr_to_member_type:
6728 case DW_TAG_set_type:
6729 case DW_TAG_subrange_type:
6730 case DW_TAG_base_type:
6731 case DW_TAG_const_type:
6732 case DW_TAG_file_type:
6733 case DW_TAG_packed_type:
6734 case DW_TAG_volatile_type:
6735 case DW_TAG_typedef:
6736 default:
6737 return 0;
6738 }
6739 }
6740
6741 /* Make a clone of DIE. */
6742
6743 static dw_die_ref
6744 clone_die (dw_die_ref die)
6745 {
6746 dw_die_ref clone;
6747 dw_attr_ref a;
6748 unsigned ix;
6749
6750 clone = ggc_alloc_cleared_die_node ();
6751 clone->die_tag = die->die_tag;
6752
6753 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6754 add_dwarf_attr (clone, a);
6755
6756 return clone;
6757 }
6758
6759 /* Make a clone of the tree rooted at DIE. */
6760
6761 static dw_die_ref
6762 clone_tree (dw_die_ref die)
6763 {
6764 dw_die_ref c;
6765 dw_die_ref clone = clone_die (die);
6766
6767 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6768
6769 return clone;
6770 }
6771
6772 /* Make a clone of DIE as a declaration. */
6773
6774 static dw_die_ref
6775 clone_as_declaration (dw_die_ref die)
6776 {
6777 dw_die_ref clone;
6778 dw_die_ref decl;
6779 dw_attr_ref a;
6780 unsigned ix;
6781
6782 /* If the DIE is already a declaration, just clone it. */
6783 if (is_declaration_die (die))
6784 return clone_die (die);
6785
6786 /* If the DIE is a specification, just clone its declaration DIE. */
6787 decl = get_AT_ref (die, DW_AT_specification);
6788 if (decl != NULL)
6789 {
6790 clone = clone_die (decl);
6791 if (die->comdat_type_p)
6792 add_AT_die_ref (clone, DW_AT_signature, die);
6793 return clone;
6794 }
6795
6796 clone = ggc_alloc_cleared_die_node ();
6797 clone->die_tag = die->die_tag;
6798
6799 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6800 {
6801 /* We don't want to copy over all attributes.
6802 For example we don't want DW_AT_byte_size because otherwise we will no
6803 longer have a declaration and GDB will treat it as a definition. */
6804
6805 switch (a->dw_attr)
6806 {
6807 case DW_AT_artificial:
6808 case DW_AT_containing_type:
6809 case DW_AT_external:
6810 case DW_AT_name:
6811 case DW_AT_type:
6812 case DW_AT_virtuality:
6813 case DW_AT_linkage_name:
6814 case DW_AT_MIPS_linkage_name:
6815 add_dwarf_attr (clone, a);
6816 break;
6817 case DW_AT_byte_size:
6818 default:
6819 break;
6820 }
6821 }
6822
6823 if (die->comdat_type_p)
6824 add_AT_die_ref (clone, DW_AT_signature, die);
6825
6826 add_AT_flag (clone, DW_AT_declaration, 1);
6827 return clone;
6828 }
6829
6830 /* Copy the declaration context to the new type unit DIE. This includes
6831 any surrounding namespace or type declarations. If the DIE has an
6832 AT_specification attribute, it also includes attributes and children
6833 attached to the specification, and returns a pointer to the original
6834 parent of the declaration DIE. Returns NULL otherwise. */
6835
6836 static dw_die_ref
6837 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6838 {
6839 dw_die_ref decl;
6840 dw_die_ref new_decl;
6841 dw_die_ref orig_parent = NULL;
6842
6843 decl = get_AT_ref (die, DW_AT_specification);
6844 if (decl == NULL)
6845 decl = die;
6846 else
6847 {
6848 unsigned ix;
6849 dw_die_ref c;
6850 dw_attr_ref a;
6851
6852 /* The original DIE will be changed to a declaration, and must
6853 be moved to be a child of the original declaration DIE. */
6854 orig_parent = decl->die_parent;
6855
6856 /* Copy the type node pointer from the new DIE to the original
6857 declaration DIE so we can forward references later. */
6858 decl->comdat_type_p = true;
6859 decl->die_id.die_type_node = die->die_id.die_type_node;
6860
6861 remove_AT (die, DW_AT_specification);
6862
6863 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
6864 {
6865 if (a->dw_attr != DW_AT_name
6866 && a->dw_attr != DW_AT_declaration
6867 && a->dw_attr != DW_AT_external)
6868 add_dwarf_attr (die, a);
6869 }
6870
6871 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
6872 }
6873
6874 if (decl->die_parent != NULL
6875 && !is_unit_die (decl->die_parent))
6876 {
6877 new_decl = copy_ancestor_tree (unit, decl, NULL);
6878 if (new_decl != NULL)
6879 {
6880 remove_AT (new_decl, DW_AT_signature);
6881 add_AT_specification (die, new_decl);
6882 }
6883 }
6884
6885 return orig_parent;
6886 }
6887
6888 /* Generate the skeleton ancestor tree for the given NODE, then clone
6889 the DIE and add the clone into the tree. */
6890
6891 static void
6892 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
6893 {
6894 if (node->new_die != NULL)
6895 return;
6896
6897 node->new_die = clone_as_declaration (node->old_die);
6898
6899 if (node->parent != NULL)
6900 {
6901 generate_skeleton_ancestor_tree (node->parent);
6902 add_child_die (node->parent->new_die, node->new_die);
6903 }
6904 }
6905
6906 /* Generate a skeleton tree of DIEs containing any declarations that are
6907 found in the original tree. We traverse the tree looking for declaration
6908 DIEs, and construct the skeleton from the bottom up whenever we find one. */
6909
6910 static void
6911 generate_skeleton_bottom_up (skeleton_chain_node *parent)
6912 {
6913 skeleton_chain_node node;
6914 dw_die_ref c;
6915 dw_die_ref first;
6916 dw_die_ref prev = NULL;
6917 dw_die_ref next = NULL;
6918
6919 node.parent = parent;
6920
6921 first = c = parent->old_die->die_child;
6922 if (c)
6923 next = c->die_sib;
6924 if (c) do {
6925 if (prev == NULL || prev->die_sib == c)
6926 prev = c;
6927 c = next;
6928 next = (c == first ? NULL : c->die_sib);
6929 node.old_die = c;
6930 node.new_die = NULL;
6931 if (is_declaration_die (c))
6932 {
6933 /* Clone the existing DIE, move the original to the skeleton
6934 tree (which is in the main CU), and put the clone, with
6935 all the original's children, where the original came from. */
6936 dw_die_ref clone = clone_die (c);
6937 move_all_children (c, clone);
6938
6939 replace_child (c, clone, prev);
6940 generate_skeleton_ancestor_tree (parent);
6941 add_child_die (parent->new_die, c);
6942 node.new_die = c;
6943 c = clone;
6944 }
6945 generate_skeleton_bottom_up (&node);
6946 } while (next != NULL);
6947 }
6948
6949 /* Wrapper function for generate_skeleton_bottom_up. */
6950
6951 static dw_die_ref
6952 generate_skeleton (dw_die_ref die)
6953 {
6954 skeleton_chain_node node;
6955
6956 node.old_die = die;
6957 node.new_die = NULL;
6958 node.parent = NULL;
6959
6960 /* If this type definition is nested inside another type,
6961 always leave at least a declaration in its place. */
6962 if (die->die_parent != NULL && is_type_die (die->die_parent))
6963 node.new_die = clone_as_declaration (die);
6964
6965 generate_skeleton_bottom_up (&node);
6966 return node.new_die;
6967 }
6968
6969 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
6970 declaration. The original DIE is moved to a new compile unit so that
6971 existing references to it follow it to the new location. If any of the
6972 original DIE's descendants is a declaration, we need to replace the
6973 original DIE with a skeleton tree and move the declarations back into the
6974 skeleton tree. */
6975
6976 static dw_die_ref
6977 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
6978 dw_die_ref prev)
6979 {
6980 dw_die_ref skeleton, orig_parent;
6981
6982 /* Copy the declaration context to the type unit DIE. If the returned
6983 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
6984 that DIE. */
6985 orig_parent = copy_declaration_context (unit, child);
6986
6987 skeleton = generate_skeleton (child);
6988 if (skeleton == NULL)
6989 remove_child_with_prev (child, prev);
6990 else
6991 {
6992 skeleton->comdat_type_p = true;
6993 skeleton->die_id.die_type_node = child->die_id.die_type_node;
6994
6995 /* If the original DIE was a specification, we need to put
6996 the skeleton under the parent DIE of the declaration.
6997 This leaves the original declaration in the tree, but
6998 it will be pruned later since there are no longer any
6999 references to it. */
7000 if (orig_parent != NULL)
7001 {
7002 remove_child_with_prev (child, prev);
7003 add_child_die (orig_parent, skeleton);
7004 }
7005 else
7006 replace_child (child, skeleton, prev);
7007 }
7008
7009 return skeleton;
7010 }
7011
7012 /* Traverse the DIE and set up additional .debug_types sections for each
7013 type worthy of being placed in a COMDAT section. */
7014
7015 static void
7016 break_out_comdat_types (dw_die_ref die)
7017 {
7018 dw_die_ref c;
7019 dw_die_ref first;
7020 dw_die_ref prev = NULL;
7021 dw_die_ref next = NULL;
7022 dw_die_ref unit = NULL;
7023
7024 first = c = die->die_child;
7025 if (c)
7026 next = c->die_sib;
7027 if (c) do {
7028 if (prev == NULL || prev->die_sib == c)
7029 prev = c;
7030 c = next;
7031 next = (c == first ? NULL : c->die_sib);
7032 if (should_move_die_to_comdat (c))
7033 {
7034 dw_die_ref replacement;
7035 comdat_type_node_ref type_node;
7036
7037 /* Create a new type unit DIE as the root for the new tree, and
7038 add it to the list of comdat types. */
7039 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7040 add_AT_unsigned (unit, DW_AT_language,
7041 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7042 type_node = ggc_alloc_cleared_comdat_type_node ();
7043 type_node->root_die = unit;
7044 type_node->next = comdat_type_list;
7045 comdat_type_list = type_node;
7046
7047 /* Generate the type signature. */
7048 generate_type_signature (c, type_node);
7049
7050 /* Copy the declaration context, attributes, and children of the
7051 declaration into the new type unit DIE, then remove this DIE
7052 from the main CU (or replace it with a skeleton if necessary). */
7053 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7054 type_node->skeleton_die = replacement;
7055
7056 /* Break out nested types into their own type units. */
7057 break_out_comdat_types (c);
7058
7059 /* Add the DIE to the new compunit. */
7060 add_child_die (unit, c);
7061
7062 if (replacement != NULL)
7063 c = replacement;
7064 }
7065 else if (c->die_tag == DW_TAG_namespace
7066 || c->die_tag == DW_TAG_class_type
7067 || c->die_tag == DW_TAG_structure_type
7068 || c->die_tag == DW_TAG_union_type)
7069 {
7070 /* Look for nested types that can be broken out. */
7071 break_out_comdat_types (c);
7072 }
7073 } while (next != NULL);
7074 }
7075
7076 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7077
7078 struct decl_table_entry
7079 {
7080 dw_die_ref orig;
7081 dw_die_ref copy;
7082 };
7083
7084 /* Routines to manipulate hash table of copied declarations. */
7085
7086 static hashval_t
7087 htab_decl_hash (const void *of)
7088 {
7089 const struct decl_table_entry *const entry =
7090 (const struct decl_table_entry *) of;
7091
7092 return htab_hash_pointer (entry->orig);
7093 }
7094
7095 static int
7096 htab_decl_eq (const void *of1, const void *of2)
7097 {
7098 const struct decl_table_entry *const entry1 =
7099 (const struct decl_table_entry *) of1;
7100 const struct die_struct *const entry2 = (const struct die_struct *) of2;
7101
7102 return entry1->orig == entry2;
7103 }
7104
7105 static void
7106 htab_decl_del (void *what)
7107 {
7108 struct decl_table_entry *entry = (struct decl_table_entry *) what;
7109
7110 free (entry);
7111 }
7112
7113 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7114 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7115 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7116 to check if the ancestor has already been copied into UNIT. */
7117
7118 static dw_die_ref
7119 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7120 {
7121 dw_die_ref parent = die->die_parent;
7122 dw_die_ref new_parent = unit;
7123 dw_die_ref copy;
7124 void **slot = NULL;
7125 struct decl_table_entry *entry = NULL;
7126
7127 if (decl_table)
7128 {
7129 /* Check if the entry has already been copied to UNIT. */
7130 slot = htab_find_slot_with_hash (decl_table, die,
7131 htab_hash_pointer (die), INSERT);
7132 if (*slot != HTAB_EMPTY_ENTRY)
7133 {
7134 entry = (struct decl_table_entry *) *slot;
7135 return entry->copy;
7136 }
7137
7138 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7139 entry = XCNEW (struct decl_table_entry);
7140 entry->orig = die;
7141 entry->copy = NULL;
7142 *slot = entry;
7143 }
7144
7145 if (parent != NULL)
7146 {
7147 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7148 if (spec != NULL)
7149 parent = spec;
7150 if (!is_unit_die (parent))
7151 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7152 }
7153
7154 copy = clone_as_declaration (die);
7155 add_child_die (new_parent, copy);
7156
7157 if (decl_table != NULL)
7158 {
7159 /* Record the pointer to the copy. */
7160 entry->copy = copy;
7161 }
7162
7163 return copy;
7164 }
7165
7166 /* Like clone_tree, but additionally enter all the children into
7167 the hash table decl_table. */
7168
7169 static dw_die_ref
7170 clone_tree_hash (dw_die_ref die, htab_t decl_table)
7171 {
7172 dw_die_ref c;
7173 dw_die_ref clone = clone_die (die);
7174 struct decl_table_entry *entry;
7175 void **slot = htab_find_slot_with_hash (decl_table, die,
7176 htab_hash_pointer (die), INSERT);
7177 /* Assert that DIE isn't in the hash table yet. If it would be there
7178 before, the ancestors would be necessarily there as well, therefore
7179 clone_tree_hash wouldn't be called. */
7180 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7181 entry = XCNEW (struct decl_table_entry);
7182 entry->orig = die;
7183 entry->copy = clone;
7184 *slot = entry;
7185
7186 FOR_EACH_CHILD (die, c,
7187 add_child_die (clone, clone_tree_hash (c, decl_table)));
7188
7189 return clone;
7190 }
7191
7192 /* Walk the DIE and its children, looking for references to incomplete
7193 or trivial types that are unmarked (i.e., that are not in the current
7194 type_unit). */
7195
7196 static void
7197 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7198 {
7199 dw_die_ref c;
7200 dw_attr_ref a;
7201 unsigned ix;
7202
7203 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7204 {
7205 if (AT_class (a) == dw_val_class_die_ref)
7206 {
7207 dw_die_ref targ = AT_ref (a);
7208 void **slot;
7209 struct decl_table_entry *entry;
7210
7211 if (targ->die_mark != 0 || targ->comdat_type_p)
7212 continue;
7213
7214 slot = htab_find_slot_with_hash (decl_table, targ,
7215 htab_hash_pointer (targ), INSERT);
7216
7217 if (*slot != HTAB_EMPTY_ENTRY)
7218 {
7219 /* TARG has already been copied, so we just need to
7220 modify the reference to point to the copy. */
7221 entry = (struct decl_table_entry *) *slot;
7222 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7223 }
7224 else
7225 {
7226 dw_die_ref parent = unit;
7227 dw_die_ref copy = clone_die (targ);
7228
7229 /* Record in DECL_TABLE that TARG has been copied.
7230 Need to do this now, before the recursive call,
7231 because DECL_TABLE may be expanded and SLOT
7232 would no longer be a valid pointer. */
7233 entry = XCNEW (struct decl_table_entry);
7234 entry->orig = targ;
7235 entry->copy = copy;
7236 *slot = entry;
7237
7238 FOR_EACH_CHILD (targ, c,
7239 add_child_die (copy,
7240 clone_tree_hash (c, decl_table)));
7241
7242 /* Make sure the cloned tree is marked as part of the
7243 type unit. */
7244 mark_dies (copy);
7245
7246 /* If TARG has surrounding context, copy its ancestor tree
7247 into the new type unit. */
7248 if (targ->die_parent != NULL
7249 && !is_unit_die (targ->die_parent))
7250 parent = copy_ancestor_tree (unit, targ->die_parent,
7251 decl_table);
7252
7253 add_child_die (parent, copy);
7254 a->dw_attr_val.v.val_die_ref.die = copy;
7255
7256 /* Make sure the newly-copied DIE is walked. If it was
7257 installed in a previously-added context, it won't
7258 get visited otherwise. */
7259 if (parent != unit)
7260 {
7261 /* Find the highest point of the newly-added tree,
7262 mark each node along the way, and walk from there. */
7263 parent->die_mark = 1;
7264 while (parent->die_parent
7265 && parent->die_parent->die_mark == 0)
7266 {
7267 parent = parent->die_parent;
7268 parent->die_mark = 1;
7269 }
7270 copy_decls_walk (unit, parent, decl_table);
7271 }
7272 }
7273 }
7274 }
7275
7276 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7277 }
7278
7279 /* Copy declarations for "unworthy" types into the new comdat section.
7280 Incomplete types, modified types, and certain other types aren't broken
7281 out into comdat sections of their own, so they don't have a signature,
7282 and we need to copy the declaration into the same section so that we
7283 don't have an external reference. */
7284
7285 static void
7286 copy_decls_for_unworthy_types (dw_die_ref unit)
7287 {
7288 htab_t decl_table;
7289
7290 mark_dies (unit);
7291 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7292 copy_decls_walk (unit, unit, decl_table);
7293 htab_delete (decl_table);
7294 unmark_dies (unit);
7295 }
7296
7297 /* Traverse the DIE and add a sibling attribute if it may have the
7298 effect of speeding up access to siblings. To save some space,
7299 avoid generating sibling attributes for DIE's without children. */
7300
7301 static void
7302 add_sibling_attributes (dw_die_ref die)
7303 {
7304 dw_die_ref c;
7305
7306 if (! die->die_child)
7307 return;
7308
7309 if (die->die_parent && die != die->die_parent->die_child)
7310 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7311
7312 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7313 }
7314
7315 /* Output all location lists for the DIE and its children. */
7316
7317 static void
7318 output_location_lists (dw_die_ref die)
7319 {
7320 dw_die_ref c;
7321 dw_attr_ref a;
7322 unsigned ix;
7323
7324 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7325 if (AT_class (a) == dw_val_class_loc_list)
7326 output_loc_list (AT_loc_list (a));
7327
7328 FOR_EACH_CHILD (die, c, output_location_lists (c));
7329 }
7330
7331 /* We want to limit the number of external references, because they are
7332 larger than local references: a relocation takes multiple words, and
7333 even a sig8 reference is always eight bytes, whereas a local reference
7334 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7335 So if we encounter multiple external references to the same type DIE, we
7336 make a local typedef stub for it and redirect all references there.
7337
7338 This is the element of the hash table for keeping track of these
7339 references. */
7340
7341 struct external_ref
7342 {
7343 dw_die_ref type;
7344 dw_die_ref stub;
7345 unsigned n_refs;
7346 };
7347
7348 /* Hash an external_ref. */
7349
7350 static hashval_t
7351 hash_external_ref (const void *p)
7352 {
7353 const struct external_ref *r = (const struct external_ref *)p;
7354 return htab_hash_pointer (r->type);
7355 }
7356
7357 /* Compare external_refs. */
7358
7359 static int
7360 external_ref_eq (const void *p1, const void *p2)
7361 {
7362 const struct external_ref *r1 = (const struct external_ref *)p1;
7363 const struct external_ref *r2 = (const struct external_ref *)p2;
7364 return r1->type == r2->type;
7365 }
7366
7367 /* Return a pointer to the external_ref for references to DIE. */
7368
7369 static struct external_ref *
7370 lookup_external_ref (htab_t map, dw_die_ref die)
7371 {
7372 struct external_ref ref, *ref_p;
7373 void ** slot;
7374
7375 ref.type = die;
7376 slot = htab_find_slot (map, &ref, INSERT);
7377 if (*slot != HTAB_EMPTY_ENTRY)
7378 return (struct external_ref *) *slot;
7379
7380 ref_p = XCNEW (struct external_ref);
7381 ref_p->type = die;
7382 *slot = ref_p;
7383 return ref_p;
7384 }
7385
7386 /* Subroutine of optimize_external_refs, below.
7387
7388 If we see a type skeleton, record it as our stub. If we see external
7389 references, remember how many we've seen. */
7390
7391 static void
7392 optimize_external_refs_1 (dw_die_ref die, htab_t map)
7393 {
7394 dw_die_ref c;
7395 dw_attr_ref a;
7396 unsigned ix;
7397 struct external_ref *ref_p;
7398
7399 if (is_type_die (die)
7400 && (c = get_AT_ref (die, DW_AT_signature)))
7401 {
7402 /* This is a local skeleton; use it for local references. */
7403 ref_p = lookup_external_ref (map, c);
7404 ref_p->stub = die;
7405 }
7406
7407 /* Scan the DIE references, and remember any that refer to DIEs from
7408 other CUs (i.e. those which are not marked). */
7409 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7410 if (AT_class (a) == dw_val_class_die_ref
7411 && (c = AT_ref (a))->die_mark == 0
7412 && is_type_die (c))
7413 {
7414 ref_p = lookup_external_ref (map, c);
7415 ref_p->n_refs++;
7416 }
7417
7418 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7419 }
7420
7421 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7422 points to an external_ref, DATA is the CU we're processing. If we don't
7423 already have a local stub, and we have multiple refs, build a stub. */
7424
7425 static int
7426 build_local_stub (void **slot, void *data)
7427 {
7428 struct external_ref *ref_p = (struct external_ref *)*slot;
7429
7430 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7431 {
7432 /* We have multiple references to this type, so build a small stub.
7433 Both of these forms are a bit dodgy from the perspective of the
7434 DWARF standard, since technically they should have names. */
7435 dw_die_ref cu = (dw_die_ref) data;
7436 dw_die_ref type = ref_p->type;
7437 dw_die_ref stub = NULL;
7438
7439 if (type->comdat_type_p)
7440 {
7441 /* If we refer to this type via sig8, use AT_signature. */
7442 stub = new_die (type->die_tag, cu, NULL_TREE);
7443 add_AT_die_ref (stub, DW_AT_signature, type);
7444 }
7445 else
7446 {
7447 /* Otherwise, use a typedef with no name. */
7448 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7449 add_AT_die_ref (stub, DW_AT_type, type);
7450 }
7451
7452 stub->die_mark++;
7453 ref_p->stub = stub;
7454 }
7455 return 1;
7456 }
7457
7458 /* DIE is a unit; look through all the DIE references to see if there are
7459 any external references to types, and if so, create local stubs for
7460 them which will be applied in build_abbrev_table. This is useful because
7461 references to local DIEs are smaller. */
7462
7463 static htab_t
7464 optimize_external_refs (dw_die_ref die)
7465 {
7466 htab_t map = htab_create (10, hash_external_ref, external_ref_eq, free);
7467 optimize_external_refs_1 (die, map);
7468 htab_traverse (map, build_local_stub, die);
7469 return map;
7470 }
7471
7472 /* The format of each DIE (and its attribute value pairs) is encoded in an
7473 abbreviation table. This routine builds the abbreviation table and assigns
7474 a unique abbreviation id for each abbreviation entry. The children of each
7475 die are visited recursively. */
7476
7477 static void
7478 build_abbrev_table (dw_die_ref die, htab_t extern_map)
7479 {
7480 unsigned long abbrev_id;
7481 unsigned int n_alloc;
7482 dw_die_ref c;
7483 dw_attr_ref a;
7484 unsigned ix;
7485
7486 /* Scan the DIE references, and replace any that refer to
7487 DIEs from other CUs (i.e. those which are not marked) with
7488 the local stubs we built in optimize_external_refs. */
7489 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7490 if (AT_class (a) == dw_val_class_die_ref
7491 && (c = AT_ref (a))->die_mark == 0)
7492 {
7493 struct external_ref *ref_p;
7494 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7495
7496 ref_p = lookup_external_ref (extern_map, c);
7497 if (ref_p->stub && ref_p->stub != die)
7498 change_AT_die_ref (a, ref_p->stub);
7499 else
7500 /* We aren't changing this reference, so mark it external. */
7501 set_AT_ref_external (a, 1);
7502 }
7503
7504 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7505 {
7506 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7507 dw_attr_ref die_a, abbrev_a;
7508 unsigned ix;
7509 bool ok = true;
7510
7511 if (abbrev->die_tag != die->die_tag)
7512 continue;
7513 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7514 continue;
7515
7516 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7517 continue;
7518
7519 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7520 {
7521 abbrev_a = &(*abbrev->die_attr)[ix];
7522 if ((abbrev_a->dw_attr != die_a->dw_attr)
7523 || (value_format (abbrev_a) != value_format (die_a)))
7524 {
7525 ok = false;
7526 break;
7527 }
7528 }
7529 if (ok)
7530 break;
7531 }
7532
7533 if (abbrev_id >= abbrev_die_table_in_use)
7534 {
7535 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7536 {
7537 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7538 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7539 n_alloc);
7540
7541 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7542 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7543 abbrev_die_table_allocated = n_alloc;
7544 }
7545
7546 ++abbrev_die_table_in_use;
7547 abbrev_die_table[abbrev_id] = die;
7548 }
7549
7550 die->die_abbrev = abbrev_id;
7551 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7552 }
7553 \f
7554 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7555
7556 static int
7557 constant_size (unsigned HOST_WIDE_INT value)
7558 {
7559 int log;
7560
7561 if (value == 0)
7562 log = 0;
7563 else
7564 log = floor_log2 (value);
7565
7566 log = log / 8;
7567 log = 1 << (floor_log2 (log) + 1);
7568
7569 return log;
7570 }
7571
7572 /* Return the size of a DIE as it is represented in the
7573 .debug_info section. */
7574
7575 static unsigned long
7576 size_of_die (dw_die_ref die)
7577 {
7578 unsigned long size = 0;
7579 dw_attr_ref a;
7580 unsigned ix;
7581 enum dwarf_form form;
7582
7583 size += size_of_uleb128 (die->die_abbrev);
7584 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7585 {
7586 switch (AT_class (a))
7587 {
7588 case dw_val_class_addr:
7589 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7590 {
7591 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7592 size += size_of_uleb128 (AT_index (a));
7593 }
7594 else
7595 size += DWARF2_ADDR_SIZE;
7596 break;
7597 case dw_val_class_offset:
7598 size += DWARF_OFFSET_SIZE;
7599 break;
7600 case dw_val_class_loc:
7601 {
7602 unsigned long lsize = size_of_locs (AT_loc (a));
7603
7604 /* Block length. */
7605 if (dwarf_version >= 4)
7606 size += size_of_uleb128 (lsize);
7607 else
7608 size += constant_size (lsize);
7609 size += lsize;
7610 }
7611 break;
7612 case dw_val_class_loc_list:
7613 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7614 {
7615 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7616 size += size_of_uleb128 (AT_index (a));
7617 }
7618 else
7619 size += DWARF_OFFSET_SIZE;
7620 break;
7621 case dw_val_class_range_list:
7622 size += DWARF_OFFSET_SIZE;
7623 break;
7624 case dw_val_class_const:
7625 size += size_of_sleb128 (AT_int (a));
7626 break;
7627 case dw_val_class_unsigned_const:
7628 {
7629 int csize = constant_size (AT_unsigned (a));
7630 if (dwarf_version == 3
7631 && a->dw_attr == DW_AT_data_member_location
7632 && csize >= 4)
7633 size += size_of_uleb128 (AT_unsigned (a));
7634 else
7635 size += csize;
7636 }
7637 break;
7638 case dw_val_class_const_double:
7639 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7640 if (HOST_BITS_PER_WIDE_INT >= 64)
7641 size++; /* block */
7642 break;
7643 case dw_val_class_vec:
7644 size += constant_size (a->dw_attr_val.v.val_vec.length
7645 * a->dw_attr_val.v.val_vec.elt_size)
7646 + a->dw_attr_val.v.val_vec.length
7647 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7648 break;
7649 case dw_val_class_flag:
7650 if (dwarf_version >= 4)
7651 /* Currently all add_AT_flag calls pass in 1 as last argument,
7652 so DW_FORM_flag_present can be used. If that ever changes,
7653 we'll need to use DW_FORM_flag and have some optimization
7654 in build_abbrev_table that will change those to
7655 DW_FORM_flag_present if it is set to 1 in all DIEs using
7656 the same abbrev entry. */
7657 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7658 else
7659 size += 1;
7660 break;
7661 case dw_val_class_die_ref:
7662 if (AT_ref_external (a))
7663 {
7664 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7665 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7666 is sized by target address length, whereas in DWARF3
7667 it's always sized as an offset. */
7668 if (use_debug_types)
7669 size += DWARF_TYPE_SIGNATURE_SIZE;
7670 else if (dwarf_version == 2)
7671 size += DWARF2_ADDR_SIZE;
7672 else
7673 size += DWARF_OFFSET_SIZE;
7674 }
7675 else
7676 size += DWARF_OFFSET_SIZE;
7677 break;
7678 case dw_val_class_fde_ref:
7679 size += DWARF_OFFSET_SIZE;
7680 break;
7681 case dw_val_class_lbl_id:
7682 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7683 {
7684 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7685 size += size_of_uleb128 (AT_index (a));
7686 }
7687 else
7688 size += DWARF2_ADDR_SIZE;
7689 break;
7690 case dw_val_class_lineptr:
7691 case dw_val_class_macptr:
7692 size += DWARF_OFFSET_SIZE;
7693 break;
7694 case dw_val_class_str:
7695 form = AT_string_form (a);
7696 if (form == DW_FORM_strp)
7697 size += DWARF_OFFSET_SIZE;
7698 else if (form == DW_FORM_GNU_str_index)
7699 size += size_of_uleb128 (AT_index (a));
7700 else
7701 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7702 break;
7703 case dw_val_class_file:
7704 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7705 break;
7706 case dw_val_class_data8:
7707 size += 8;
7708 break;
7709 case dw_val_class_vms_delta:
7710 size += DWARF_OFFSET_SIZE;
7711 break;
7712 case dw_val_class_high_pc:
7713 size += DWARF2_ADDR_SIZE;
7714 break;
7715 default:
7716 gcc_unreachable ();
7717 }
7718 }
7719
7720 return size;
7721 }
7722
7723 /* Size the debugging information associated with a given DIE. Visits the
7724 DIE's children recursively. Updates the global variable next_die_offset, on
7725 each time through. Uses the current value of next_die_offset to update the
7726 die_offset field in each DIE. */
7727
7728 static void
7729 calc_die_sizes (dw_die_ref die)
7730 {
7731 dw_die_ref c;
7732
7733 gcc_assert (die->die_offset == 0
7734 || (unsigned long int) die->die_offset == next_die_offset);
7735 die->die_offset = next_die_offset;
7736 next_die_offset += size_of_die (die);
7737
7738 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7739
7740 if (die->die_child != NULL)
7741 /* Count the null byte used to terminate sibling lists. */
7742 next_die_offset += 1;
7743 }
7744
7745 /* Size just the base type children at the start of the CU.
7746 This is needed because build_abbrev needs to size locs
7747 and sizing of type based stack ops needs to know die_offset
7748 values for the base types. */
7749
7750 static void
7751 calc_base_type_die_sizes (void)
7752 {
7753 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7754 unsigned int i;
7755 dw_die_ref base_type;
7756 #if ENABLE_ASSERT_CHECKING
7757 dw_die_ref prev = comp_unit_die ()->die_child;
7758 #endif
7759
7760 die_offset += size_of_die (comp_unit_die ());
7761 for (i = 0; base_types.iterate (i, &base_type); i++)
7762 {
7763 #if ENABLE_ASSERT_CHECKING
7764 gcc_assert (base_type->die_offset == 0
7765 && prev->die_sib == base_type
7766 && base_type->die_child == NULL
7767 && base_type->die_abbrev);
7768 prev = base_type;
7769 #endif
7770 base_type->die_offset = die_offset;
7771 die_offset += size_of_die (base_type);
7772 }
7773 }
7774
7775 /* Set the marks for a die and its children. We do this so
7776 that we know whether or not a reference needs to use FORM_ref_addr; only
7777 DIEs in the same CU will be marked. We used to clear out the offset
7778 and use that as the flag, but ran into ordering problems. */
7779
7780 static void
7781 mark_dies (dw_die_ref die)
7782 {
7783 dw_die_ref c;
7784
7785 gcc_assert (!die->die_mark);
7786
7787 die->die_mark = 1;
7788 FOR_EACH_CHILD (die, c, mark_dies (c));
7789 }
7790
7791 /* Clear the marks for a die and its children. */
7792
7793 static void
7794 unmark_dies (dw_die_ref die)
7795 {
7796 dw_die_ref c;
7797
7798 if (! use_debug_types)
7799 gcc_assert (die->die_mark);
7800
7801 die->die_mark = 0;
7802 FOR_EACH_CHILD (die, c, unmark_dies (c));
7803 }
7804
7805 /* Clear the marks for a die, its children and referred dies. */
7806
7807 static void
7808 unmark_all_dies (dw_die_ref die)
7809 {
7810 dw_die_ref c;
7811 dw_attr_ref a;
7812 unsigned ix;
7813
7814 if (!die->die_mark)
7815 return;
7816 die->die_mark = 0;
7817
7818 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7819
7820 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7821 if (AT_class (a) == dw_val_class_die_ref)
7822 unmark_all_dies (AT_ref (a));
7823 }
7824
7825 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7826 generated for the compilation unit. */
7827
7828 static unsigned long
7829 size_of_pubnames (vec<pubname_entry, va_gc> *names)
7830 {
7831 unsigned long size;
7832 unsigned i;
7833 pubname_ref p;
7834
7835 size = DWARF_PUBNAMES_HEADER_SIZE;
7836 FOR_EACH_VEC_ELT (*names, i, p)
7837 if (names != pubtype_table
7838 || p->die->die_offset != 0
7839 || !flag_eliminate_unused_debug_types)
7840 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7841
7842 size += DWARF_OFFSET_SIZE;
7843 return size;
7844 }
7845
7846 /* Return the size of the information in the .debug_aranges section. */
7847
7848 static unsigned long
7849 size_of_aranges (void)
7850 {
7851 unsigned long size;
7852
7853 size = DWARF_ARANGES_HEADER_SIZE;
7854
7855 /* Count the address/length pair for this compilation unit. */
7856 if (text_section_used)
7857 size += 2 * DWARF2_ADDR_SIZE;
7858 if (cold_text_section_used)
7859 size += 2 * DWARF2_ADDR_SIZE;
7860 if (have_multiple_function_sections)
7861 {
7862 unsigned fde_idx;
7863 dw_fde_ref fde;
7864
7865 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
7866 {
7867 if (DECL_IGNORED_P (fde->decl))
7868 continue;
7869 if (!fde->in_std_section)
7870 size += 2 * DWARF2_ADDR_SIZE;
7871 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7872 size += 2 * DWARF2_ADDR_SIZE;
7873 }
7874 }
7875
7876 /* Count the two zero words used to terminated the address range table. */
7877 size += 2 * DWARF2_ADDR_SIZE;
7878 return size;
7879 }
7880 \f
7881 /* Select the encoding of an attribute value. */
7882
7883 static enum dwarf_form
7884 value_format (dw_attr_ref a)
7885 {
7886 switch (AT_class (a))
7887 {
7888 case dw_val_class_addr:
7889 /* Only very few attributes allow DW_FORM_addr. */
7890 switch (a->dw_attr)
7891 {
7892 case DW_AT_low_pc:
7893 case DW_AT_high_pc:
7894 case DW_AT_entry_pc:
7895 case DW_AT_trampoline:
7896 return (AT_index (a) == NOT_INDEXED
7897 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
7898 default:
7899 break;
7900 }
7901 switch (DWARF2_ADDR_SIZE)
7902 {
7903 case 1:
7904 return DW_FORM_data1;
7905 case 2:
7906 return DW_FORM_data2;
7907 case 4:
7908 return DW_FORM_data4;
7909 case 8:
7910 return DW_FORM_data8;
7911 default:
7912 gcc_unreachable ();
7913 }
7914 case dw_val_class_range_list:
7915 case dw_val_class_loc_list:
7916 if (dwarf_version >= 4)
7917 return DW_FORM_sec_offset;
7918 /* FALLTHRU */
7919 case dw_val_class_vms_delta:
7920 case dw_val_class_offset:
7921 switch (DWARF_OFFSET_SIZE)
7922 {
7923 case 4:
7924 return DW_FORM_data4;
7925 case 8:
7926 return DW_FORM_data8;
7927 default:
7928 gcc_unreachable ();
7929 }
7930 case dw_val_class_loc:
7931 if (dwarf_version >= 4)
7932 return DW_FORM_exprloc;
7933 switch (constant_size (size_of_locs (AT_loc (a))))
7934 {
7935 case 1:
7936 return DW_FORM_block1;
7937 case 2:
7938 return DW_FORM_block2;
7939 case 4:
7940 return DW_FORM_block4;
7941 default:
7942 gcc_unreachable ();
7943 }
7944 case dw_val_class_const:
7945 return DW_FORM_sdata;
7946 case dw_val_class_unsigned_const:
7947 switch (constant_size (AT_unsigned (a)))
7948 {
7949 case 1:
7950 return DW_FORM_data1;
7951 case 2:
7952 return DW_FORM_data2;
7953 case 4:
7954 /* In DWARF3 DW_AT_data_member_location with
7955 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7956 constant, so we need to use DW_FORM_udata if we need
7957 a large constant. */
7958 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7959 return DW_FORM_udata;
7960 return DW_FORM_data4;
7961 case 8:
7962 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7963 return DW_FORM_udata;
7964 return DW_FORM_data8;
7965 default:
7966 gcc_unreachable ();
7967 }
7968 case dw_val_class_const_double:
7969 switch (HOST_BITS_PER_WIDE_INT)
7970 {
7971 case 8:
7972 return DW_FORM_data2;
7973 case 16:
7974 return DW_FORM_data4;
7975 case 32:
7976 return DW_FORM_data8;
7977 case 64:
7978 default:
7979 return DW_FORM_block1;
7980 }
7981 case dw_val_class_vec:
7982 switch (constant_size (a->dw_attr_val.v.val_vec.length
7983 * a->dw_attr_val.v.val_vec.elt_size))
7984 {
7985 case 1:
7986 return DW_FORM_block1;
7987 case 2:
7988 return DW_FORM_block2;
7989 case 4:
7990 return DW_FORM_block4;
7991 default:
7992 gcc_unreachable ();
7993 }
7994 case dw_val_class_flag:
7995 if (dwarf_version >= 4)
7996 {
7997 /* Currently all add_AT_flag calls pass in 1 as last argument,
7998 so DW_FORM_flag_present can be used. If that ever changes,
7999 we'll need to use DW_FORM_flag and have some optimization
8000 in build_abbrev_table that will change those to
8001 DW_FORM_flag_present if it is set to 1 in all DIEs using
8002 the same abbrev entry. */
8003 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8004 return DW_FORM_flag_present;
8005 }
8006 return DW_FORM_flag;
8007 case dw_val_class_die_ref:
8008 if (AT_ref_external (a))
8009 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8010 else
8011 return DW_FORM_ref;
8012 case dw_val_class_fde_ref:
8013 return DW_FORM_data;
8014 case dw_val_class_lbl_id:
8015 return (AT_index (a) == NOT_INDEXED
8016 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8017 case dw_val_class_lineptr:
8018 case dw_val_class_macptr:
8019 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8020 case dw_val_class_str:
8021 return AT_string_form (a);
8022 case dw_val_class_file:
8023 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8024 {
8025 case 1:
8026 return DW_FORM_data1;
8027 case 2:
8028 return DW_FORM_data2;
8029 case 4:
8030 return DW_FORM_data4;
8031 default:
8032 gcc_unreachable ();
8033 }
8034
8035 case dw_val_class_data8:
8036 return DW_FORM_data8;
8037
8038 case dw_val_class_high_pc:
8039 switch (DWARF2_ADDR_SIZE)
8040 {
8041 case 1:
8042 return DW_FORM_data1;
8043 case 2:
8044 return DW_FORM_data2;
8045 case 4:
8046 return DW_FORM_data4;
8047 case 8:
8048 return DW_FORM_data8;
8049 default:
8050 gcc_unreachable ();
8051 }
8052
8053 default:
8054 gcc_unreachable ();
8055 }
8056 }
8057
8058 /* Output the encoding of an attribute value. */
8059
8060 static void
8061 output_value_format (dw_attr_ref a)
8062 {
8063 enum dwarf_form form = value_format (a);
8064
8065 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8066 }
8067
8068 /* Given a die and id, produce the appropriate abbreviations. */
8069
8070 static void
8071 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8072 {
8073 unsigned ix;
8074 dw_attr_ref a_attr;
8075
8076 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8077 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8078 dwarf_tag_name (abbrev->die_tag));
8079
8080 if (abbrev->die_child != NULL)
8081 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8082 else
8083 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8084
8085 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8086 {
8087 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8088 dwarf_attr_name (a_attr->dw_attr));
8089 output_value_format (a_attr);
8090 }
8091
8092 dw2_asm_output_data (1, 0, NULL);
8093 dw2_asm_output_data (1, 0, NULL);
8094 }
8095
8096
8097 /* Output the .debug_abbrev section which defines the DIE abbreviation
8098 table. */
8099
8100 static void
8101 output_abbrev_section (void)
8102 {
8103 unsigned long abbrev_id;
8104
8105 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8106 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8107
8108 /* Terminate the table. */
8109 dw2_asm_output_data (1, 0, NULL);
8110 }
8111
8112 /* Output a symbol we can use to refer to this DIE from another CU. */
8113
8114 static inline void
8115 output_die_symbol (dw_die_ref die)
8116 {
8117 const char *sym = die->die_id.die_symbol;
8118
8119 gcc_assert (!die->comdat_type_p);
8120
8121 if (sym == 0)
8122 return;
8123
8124 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8125 /* We make these global, not weak; if the target doesn't support
8126 .linkonce, it doesn't support combining the sections, so debugging
8127 will break. */
8128 targetm.asm_out.globalize_label (asm_out_file, sym);
8129
8130 ASM_OUTPUT_LABEL (asm_out_file, sym);
8131 }
8132
8133 /* Return a new location list, given the begin and end range, and the
8134 expression. */
8135
8136 static inline dw_loc_list_ref
8137 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8138 const char *section)
8139 {
8140 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8141
8142 retlist->begin = begin;
8143 retlist->begin_entry = NULL;
8144 retlist->end = end;
8145 retlist->expr = expr;
8146 retlist->section = section;
8147
8148 return retlist;
8149 }
8150
8151 /* Generate a new internal symbol for this location list node, if it
8152 hasn't got one yet. */
8153
8154 static inline void
8155 gen_llsym (dw_loc_list_ref list)
8156 {
8157 gcc_assert (!list->ll_symbol);
8158 list->ll_symbol = gen_internal_sym ("LLST");
8159 }
8160
8161 /* Output the location list given to us. */
8162
8163 static void
8164 output_loc_list (dw_loc_list_ref list_head)
8165 {
8166 dw_loc_list_ref curr = list_head;
8167
8168 if (list_head->emitted)
8169 return;
8170 list_head->emitted = true;
8171
8172 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8173
8174 /* Walk the location list, and output each range + expression. */
8175 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8176 {
8177 unsigned long size;
8178 /* Don't output an entry that starts and ends at the same address. */
8179 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8180 continue;
8181 size = size_of_locs (curr->expr);
8182 /* If the expression is too large, drop it on the floor. We could
8183 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8184 in the expression, but >= 64KB expressions for a single value
8185 in a single range are unlikely very useful. */
8186 if (size > 0xffff)
8187 continue;
8188 if (dwarf_split_debug_info)
8189 {
8190 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8191 "Location list start/length entry (%s)",
8192 list_head->ll_symbol);
8193 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8194 "Location list range start index (%s)",
8195 curr->begin);
8196 /* The length field is 4 bytes. If we ever need to support
8197 an 8-byte length, we can add a new DW_LLE code or fall back
8198 to DW_LLE_GNU_start_end_entry. */
8199 dw2_asm_output_delta (4, curr->end, curr->begin,
8200 "Location list range length (%s)",
8201 list_head->ll_symbol);
8202 }
8203 else if (!have_multiple_function_sections)
8204 {
8205 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8206 "Location list begin address (%s)",
8207 list_head->ll_symbol);
8208 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8209 "Location list end address (%s)",
8210 list_head->ll_symbol);
8211 }
8212 else
8213 {
8214 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8215 "Location list begin address (%s)",
8216 list_head->ll_symbol);
8217 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8218 "Location list end address (%s)",
8219 list_head->ll_symbol);
8220 }
8221
8222 /* Output the block length for this list of location operations. */
8223 gcc_assert (size <= 0xffff);
8224 dw2_asm_output_data (2, size, "%s", "Location expression size");
8225
8226 output_loc_sequence (curr->expr, -1);
8227 }
8228
8229 if (dwarf_split_debug_info)
8230 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8231 "Location list terminator (%s)",
8232 list_head->ll_symbol);
8233 else
8234 {
8235 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8236 "Location list terminator begin (%s)",
8237 list_head->ll_symbol);
8238 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8239 "Location list terminator end (%s)",
8240 list_head->ll_symbol);
8241 }
8242 }
8243
8244 /* Output a range_list offset into the debug_range section. Emit a
8245 relocated reference if val_entry is NULL, otherwise, emit an
8246 indirect reference. */
8247
8248 static void
8249 output_range_list_offset (dw_attr_ref a)
8250 {
8251 const char *name = dwarf_attr_name (a->dw_attr);
8252
8253 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8254 {
8255 char *p = strchr (ranges_section_label, '\0');
8256 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8257 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8258 debug_ranges_section, "%s", name);
8259 *p = '\0';
8260 }
8261 else
8262 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8263 "%s (offset from %s)", name, ranges_section_label);
8264 }
8265
8266 /* Output the offset into the debug_loc section. */
8267
8268 static void
8269 output_loc_list_offset (dw_attr_ref a)
8270 {
8271 char *sym = AT_loc_list (a)->ll_symbol;
8272
8273 gcc_assert (sym);
8274 if (dwarf_split_debug_info)
8275 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8276 "%s", dwarf_attr_name (a->dw_attr));
8277 else
8278 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8279 "%s", dwarf_attr_name (a->dw_attr));
8280 }
8281
8282 /* Output an attribute's index or value appropriately. */
8283
8284 static void
8285 output_attr_index_or_value (dw_attr_ref a)
8286 {
8287 const char *name = dwarf_attr_name (a->dw_attr);
8288
8289 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8290 {
8291 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8292 return;
8293 }
8294 switch (AT_class (a))
8295 {
8296 case dw_val_class_addr:
8297 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8298 break;
8299 case dw_val_class_high_pc:
8300 case dw_val_class_lbl_id:
8301 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8302 break;
8303 case dw_val_class_loc_list:
8304 output_loc_list_offset (a);
8305 break;
8306 default:
8307 gcc_unreachable ();
8308 }
8309 }
8310
8311 /* Output a type signature. */
8312
8313 static inline void
8314 output_signature (const char *sig, const char *name)
8315 {
8316 int i;
8317
8318 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8319 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8320 }
8321
8322 /* Output the DIE and its attributes. Called recursively to generate
8323 the definitions of each child DIE. */
8324
8325 static void
8326 output_die (dw_die_ref die)
8327 {
8328 dw_attr_ref a;
8329 dw_die_ref c;
8330 unsigned long size;
8331 unsigned ix;
8332
8333 /* If someone in another CU might refer to us, set up a symbol for
8334 them to point to. */
8335 if (! die->comdat_type_p && die->die_id.die_symbol)
8336 output_die_symbol (die);
8337
8338 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8339 (unsigned long)die->die_offset,
8340 dwarf_tag_name (die->die_tag));
8341
8342 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8343 {
8344 const char *name = dwarf_attr_name (a->dw_attr);
8345
8346 switch (AT_class (a))
8347 {
8348 case dw_val_class_addr:
8349 output_attr_index_or_value (a);
8350 break;
8351
8352 case dw_val_class_offset:
8353 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8354 "%s", name);
8355 break;
8356
8357 case dw_val_class_range_list:
8358 output_range_list_offset (a);
8359 break;
8360
8361 case dw_val_class_loc:
8362 size = size_of_locs (AT_loc (a));
8363
8364 /* Output the block length for this list of location operations. */
8365 if (dwarf_version >= 4)
8366 dw2_asm_output_data_uleb128 (size, "%s", name);
8367 else
8368 dw2_asm_output_data (constant_size (size), size, "%s", name);
8369
8370 output_loc_sequence (AT_loc (a), -1);
8371 break;
8372
8373 case dw_val_class_const:
8374 /* ??? It would be slightly more efficient to use a scheme like is
8375 used for unsigned constants below, but gdb 4.x does not sign
8376 extend. Gdb 5.x does sign extend. */
8377 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8378 break;
8379
8380 case dw_val_class_unsigned_const:
8381 {
8382 int csize = constant_size (AT_unsigned (a));
8383 if (dwarf_version == 3
8384 && a->dw_attr == DW_AT_data_member_location
8385 && csize >= 4)
8386 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8387 else
8388 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8389 }
8390 break;
8391
8392 case dw_val_class_const_double:
8393 {
8394 unsigned HOST_WIDE_INT first, second;
8395
8396 if (HOST_BITS_PER_WIDE_INT >= 64)
8397 dw2_asm_output_data (1,
8398 HOST_BITS_PER_DOUBLE_INT
8399 / HOST_BITS_PER_CHAR,
8400 NULL);
8401
8402 if (WORDS_BIG_ENDIAN)
8403 {
8404 first = a->dw_attr_val.v.val_double.high;
8405 second = a->dw_attr_val.v.val_double.low;
8406 }
8407 else
8408 {
8409 first = a->dw_attr_val.v.val_double.low;
8410 second = a->dw_attr_val.v.val_double.high;
8411 }
8412
8413 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8414 first, "%s", name);
8415 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8416 second, NULL);
8417 }
8418 break;
8419
8420 case dw_val_class_vec:
8421 {
8422 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8423 unsigned int len = a->dw_attr_val.v.val_vec.length;
8424 unsigned int i;
8425 unsigned char *p;
8426
8427 dw2_asm_output_data (constant_size (len * elt_size),
8428 len * elt_size, "%s", name);
8429 if (elt_size > sizeof (HOST_WIDE_INT))
8430 {
8431 elt_size /= 2;
8432 len *= 2;
8433 }
8434 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8435 i < len;
8436 i++, p += elt_size)
8437 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8438 "fp or vector constant word %u", i);
8439 break;
8440 }
8441
8442 case dw_val_class_flag:
8443 if (dwarf_version >= 4)
8444 {
8445 /* Currently all add_AT_flag calls pass in 1 as last argument,
8446 so DW_FORM_flag_present can be used. If that ever changes,
8447 we'll need to use DW_FORM_flag and have some optimization
8448 in build_abbrev_table that will change those to
8449 DW_FORM_flag_present if it is set to 1 in all DIEs using
8450 the same abbrev entry. */
8451 gcc_assert (AT_flag (a) == 1);
8452 if (flag_debug_asm)
8453 fprintf (asm_out_file, "\t\t\t%s %s\n",
8454 ASM_COMMENT_START, name);
8455 break;
8456 }
8457 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8458 break;
8459
8460 case dw_val_class_loc_list:
8461 output_attr_index_or_value (a);
8462 break;
8463
8464 case dw_val_class_die_ref:
8465 if (AT_ref_external (a))
8466 {
8467 if (AT_ref (a)->comdat_type_p)
8468 {
8469 comdat_type_node_ref type_node =
8470 AT_ref (a)->die_id.die_type_node;
8471
8472 gcc_assert (type_node);
8473 output_signature (type_node->signature, name);
8474 }
8475 else
8476 {
8477 const char *sym = AT_ref (a)->die_id.die_symbol;
8478 int size;
8479
8480 gcc_assert (sym);
8481 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8482 length, whereas in DWARF3 it's always sized as an
8483 offset. */
8484 if (dwarf_version == 2)
8485 size = DWARF2_ADDR_SIZE;
8486 else
8487 size = DWARF_OFFSET_SIZE;
8488 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8489 name);
8490 }
8491 }
8492 else
8493 {
8494 gcc_assert (AT_ref (a)->die_offset);
8495 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8496 "%s", name);
8497 }
8498 break;
8499
8500 case dw_val_class_fde_ref:
8501 {
8502 char l1[20];
8503
8504 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8505 a->dw_attr_val.v.val_fde_index * 2);
8506 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8507 "%s", name);
8508 }
8509 break;
8510
8511 case dw_val_class_vms_delta:
8512 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8513 AT_vms_delta2 (a), AT_vms_delta1 (a),
8514 "%s", name);
8515 break;
8516
8517 case dw_val_class_lbl_id:
8518 output_attr_index_or_value (a);
8519 break;
8520
8521 case dw_val_class_lineptr:
8522 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8523 debug_line_section, "%s", name);
8524 break;
8525
8526 case dw_val_class_macptr:
8527 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8528 debug_macinfo_section, "%s", name);
8529 break;
8530
8531 case dw_val_class_str:
8532 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8533 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8534 a->dw_attr_val.v.val_str->label,
8535 debug_str_section,
8536 "%s: \"%s\"", name, AT_string (a));
8537 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8538 dw2_asm_output_data_uleb128 (AT_index (a),
8539 "%s: \"%s\"", name, AT_string (a));
8540 else
8541 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8542 break;
8543
8544 case dw_val_class_file:
8545 {
8546 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8547
8548 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8549 a->dw_attr_val.v.val_file->filename);
8550 break;
8551 }
8552
8553 case dw_val_class_data8:
8554 {
8555 int i;
8556
8557 for (i = 0; i < 8; i++)
8558 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8559 i == 0 ? "%s" : NULL, name);
8560 break;
8561 }
8562
8563 case dw_val_class_high_pc:
8564 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8565 get_AT_low_pc (die), "DW_AT_high_pc");
8566 break;
8567
8568 default:
8569 gcc_unreachable ();
8570 }
8571 }
8572
8573 FOR_EACH_CHILD (die, c, output_die (c));
8574
8575 /* Add null byte to terminate sibling list. */
8576 if (die->die_child != NULL)
8577 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8578 (unsigned long) die->die_offset);
8579 }
8580
8581 /* Output the compilation unit that appears at the beginning of the
8582 .debug_info section, and precedes the DIE descriptions. */
8583
8584 static void
8585 output_compilation_unit_header (void)
8586 {
8587 int ver = dwarf_version;
8588
8589 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8590 dw2_asm_output_data (4, 0xffffffff,
8591 "Initial length escape value indicating 64-bit DWARF extension");
8592 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8593 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8594 "Length of Compilation Unit Info");
8595 dw2_asm_output_data (2, ver, "DWARF version number");
8596 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8597 debug_abbrev_section,
8598 "Offset Into Abbrev. Section");
8599 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8600 }
8601
8602 /* Output the compilation unit DIE and its children. */
8603
8604 static void
8605 output_comp_unit (dw_die_ref die, int output_if_empty)
8606 {
8607 const char *secname, *oldsym;
8608 char *tmp;
8609 htab_t extern_map;
8610
8611 /* Unless we are outputting main CU, we may throw away empty ones. */
8612 if (!output_if_empty && die->die_child == NULL)
8613 return;
8614
8615 /* Even if there are no children of this DIE, we must output the information
8616 about the compilation unit. Otherwise, on an empty translation unit, we
8617 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8618 will then complain when examining the file. First mark all the DIEs in
8619 this CU so we know which get local refs. */
8620 mark_dies (die);
8621
8622 extern_map = optimize_external_refs (die);
8623
8624 build_abbrev_table (die, extern_map);
8625
8626 htab_delete (extern_map);
8627
8628 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8629 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8630 calc_die_sizes (die);
8631
8632 oldsym = die->die_id.die_symbol;
8633 if (oldsym)
8634 {
8635 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8636
8637 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8638 secname = tmp;
8639 die->die_id.die_symbol = NULL;
8640 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8641 }
8642 else
8643 {
8644 switch_to_section (debug_info_section);
8645 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8646 info_section_emitted = true;
8647 }
8648
8649 /* Output debugging information. */
8650 output_compilation_unit_header ();
8651 output_die (die);
8652
8653 /* Leave the marks on the main CU, so we can check them in
8654 output_pubnames. */
8655 if (oldsym)
8656 {
8657 unmark_dies (die);
8658 die->die_id.die_symbol = oldsym;
8659 }
8660 }
8661
8662 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8663 and .debug_pubtypes. This is configured per-target, but can be
8664 overridden by the -gpubnames or -gno-pubnames options. */
8665
8666 static inline bool
8667 want_pubnames (void)
8668 {
8669 return (debug_generate_pub_sections != -1
8670 ? debug_generate_pub_sections
8671 : targetm.want_debug_pub_sections);
8672 }
8673
8674 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8675
8676 static void
8677 add_AT_pubnames (dw_die_ref die)
8678 {
8679 if (want_pubnames ())
8680 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8681 }
8682
8683 /* Helper function to generate top-level dies for skeleton debug_info and
8684 debug_types. */
8685
8686 static void
8687 add_top_level_skeleton_die_attrs (dw_die_ref die)
8688 {
8689 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8690 dw_attr_ref attr;
8691
8692 add_comp_dir_attribute (die);
8693 add_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8694 /* The specification suggests that these attributes be inline to avoid
8695 having a .debug_str section. We know that they exist in the die because
8696 we just added them. */
8697 attr = get_AT (die, DW_AT_GNU_dwo_name);
8698 attr->dw_attr_val.v.val_str->form = DW_FORM_string;
8699 attr = get_AT (die, DW_AT_comp_dir);
8700 attr->dw_attr_val.v.val_str->form = DW_FORM_string;
8701
8702 add_AT_pubnames (die);
8703 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8704 }
8705
8706 /* Return the single type-unit die for skeleton type units. */
8707
8708 static dw_die_ref
8709 get_skeleton_type_unit (void)
8710 {
8711 /* For dwarf_split_debug_sections with use_type info, all type units in the
8712 skeleton sections have identical dies (but different headers). This
8713 single die will be output many times. */
8714
8715 static dw_die_ref skeleton_type_unit = NULL;
8716
8717 if (skeleton_type_unit == NULL)
8718 {
8719 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8720 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8721 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8722 }
8723 return skeleton_type_unit;
8724 }
8725
8726 /* Output skeleton debug sections that point to the dwo file. */
8727
8728 static void
8729 output_skeleton_debug_sections (dw_die_ref comp_unit)
8730 {
8731 /* These attributes will be found in the full debug_info section. */
8732 remove_AT (comp_unit, DW_AT_producer);
8733 remove_AT (comp_unit, DW_AT_language);
8734
8735 /* Add attributes common to skeleton compile_units and type_units. */
8736 add_top_level_skeleton_die_attrs (comp_unit);
8737
8738 switch_to_section (debug_skeleton_info_section);
8739 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8740
8741 /* Produce the skeleton compilation-unit header. This one differs enough from
8742 a normal CU header that it's better not to call output_compilation_unit
8743 header. */
8744 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8745 dw2_asm_output_data (4, 0xffffffff,
8746 "Initial length escape value indicating 64-bit DWARF extension");
8747
8748 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8749 DWARF_COMPILE_UNIT_HEADER_SIZE
8750 - DWARF_INITIAL_LENGTH_SIZE
8751 + size_of_die (comp_unit),
8752 "Length of Compilation Unit Info");
8753 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8754 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8755 debug_abbrev_section,
8756 "Offset Into Abbrev. Section");
8757 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8758
8759 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8760 output_die (comp_unit);
8761
8762 /* Build the skeleton debug_abbrev section. */
8763 switch_to_section (debug_skeleton_abbrev_section);
8764 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8765
8766 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8767 if (use_debug_types)
8768 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8769
8770 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8771 }
8772
8773 /* Output a comdat type unit DIE and its children. */
8774
8775 static void
8776 output_comdat_type_unit (comdat_type_node *node)
8777 {
8778 const char *secname;
8779 char *tmp;
8780 int i;
8781 #if defined (OBJECT_FORMAT_ELF)
8782 tree comdat_key;
8783 #endif
8784 htab_t extern_map;
8785
8786 /* First mark all the DIEs in this CU so we know which get local refs. */
8787 mark_dies (node->root_die);
8788
8789 extern_map = optimize_external_refs (node->root_die);
8790
8791 build_abbrev_table (node->root_die, extern_map);
8792
8793 htab_delete (extern_map);
8794
8795 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8796 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8797 calc_die_sizes (node->root_die);
8798
8799 #if defined (OBJECT_FORMAT_ELF)
8800 if (!dwarf_split_debug_info)
8801 secname = ".debug_types";
8802 else
8803 secname = ".debug_types.dwo";
8804
8805 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8806 sprintf (tmp, "wt.");
8807 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8808 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8809 comdat_key = get_identifier (tmp);
8810 targetm.asm_out.named_section (secname,
8811 SECTION_DEBUG | SECTION_LINKONCE,
8812 comdat_key);
8813 #else
8814 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8815 sprintf (tmp, ".gnu.linkonce.wt.");
8816 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8817 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8818 secname = tmp;
8819 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8820 #endif
8821
8822 /* Output debugging information. */
8823 output_compilation_unit_header ();
8824 output_signature (node->signature, "Type Signature");
8825 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8826 "Offset to Type DIE");
8827 output_die (node->root_die);
8828
8829 unmark_dies (node->root_die);
8830
8831 #if defined (OBJECT_FORMAT_ELF)
8832 if (dwarf_split_debug_info)
8833 {
8834 /* Produce the skeleton type-unit header. */
8835 const char *secname = ".debug_types";
8836
8837 targetm.asm_out.named_section (secname,
8838 SECTION_DEBUG | SECTION_LINKONCE,
8839 comdat_key);
8840 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8841 dw2_asm_output_data (4, 0xffffffff,
8842 "Initial length escape value indicating 64-bit DWARF extension");
8843
8844 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8845 DWARF_COMPILE_UNIT_HEADER_SIZE
8846 - DWARF_INITIAL_LENGTH_SIZE
8847 + size_of_die (get_skeleton_type_unit ())
8848 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
8849 "Length of Type Unit Info");
8850 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8851 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8852 debug_skeleton_abbrev_section_label,
8853 debug_abbrev_section,
8854 "Offset Into Abbrev. Section");
8855 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8856 output_signature (node->signature, "Type Signature");
8857 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
8858
8859 output_die (get_skeleton_type_unit ());
8860 }
8861 #endif
8862 }
8863
8864 /* Return the DWARF2/3 pubname associated with a decl. */
8865
8866 static const char *
8867 dwarf2_name (tree decl, int scope)
8868 {
8869 if (DECL_NAMELESS (decl))
8870 return NULL;
8871 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8872 }
8873
8874 /* Add a new entry to .debug_pubnames if appropriate. */
8875
8876 static void
8877 add_pubname_string (const char *str, dw_die_ref die)
8878 {
8879 pubname_entry e;
8880
8881 e.die = die;
8882 e.name = xstrdup (str);
8883 vec_safe_push (pubname_table, e);
8884 }
8885
8886 static void
8887 add_pubname (tree decl, dw_die_ref die)
8888 {
8889 if (!want_pubnames ())
8890 return;
8891
8892 /* Don't add items to the table when we expect that the consumer will have
8893 just read the enclosing die. For example, if the consumer is looking at a
8894 class_member, it will either be inside the class already, or will have just
8895 looked up the class to find the member. Either way, searching the class is
8896 faster than searching the index. */
8897 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
8898 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8899 {
8900 const char *name = dwarf2_name (decl, 1);
8901
8902 if (name)
8903 add_pubname_string (name, die);
8904 }
8905 }
8906
8907 /* Add an enumerator to the pubnames section. */
8908
8909 static void
8910 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8911 {
8912 pubname_entry e;
8913
8914 gcc_assert (scope_name);
8915 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8916 e.die = die;
8917 vec_safe_push (pubname_table, e);
8918 }
8919
8920 /* Add a new entry to .debug_pubtypes if appropriate. */
8921
8922 static void
8923 add_pubtype (tree decl, dw_die_ref die)
8924 {
8925 pubname_entry e;
8926
8927 if (!want_pubnames ())
8928 return;
8929
8930 if ((TREE_PUBLIC (decl)
8931 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8932 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8933 {
8934 tree scope = NULL;
8935 const char *scope_name = "";
8936 const char *sep = is_cxx () ? "::" : ".";
8937 const char *name;
8938
8939 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
8940 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
8941 {
8942 scope_name = lang_hooks.dwarf_name (scope, 1);
8943 if (scope_name != NULL && scope_name[0] != '\0')
8944 scope_name = concat (scope_name, sep, NULL);
8945 else
8946 scope_name = "";
8947 }
8948
8949 if (TYPE_P (decl))
8950 name = type_tag (decl);
8951 else
8952 name = lang_hooks.dwarf_name (decl, 1);
8953
8954 /* If we don't have a name for the type, there's no point in adding
8955 it to the table. */
8956 if (name != NULL && name[0] != '\0')
8957 {
8958 e.die = die;
8959 e.name = concat (scope_name, name, NULL);
8960 vec_safe_push (pubtype_table, e);
8961 }
8962
8963 /* Although it might be more consistent to add the pubinfo for the
8964 enumerators as their dies are created, they should only be added if the
8965 enum type meets the criteria above. So rather than re-check the parent
8966 enum type whenever an enumerator die is created, just output them all
8967 here. This isn't protected by the name conditional because anonymous
8968 enums don't have names. */
8969 if (die->die_tag == DW_TAG_enumeration_type)
8970 {
8971 dw_die_ref c;
8972
8973 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
8974 }
8975 }
8976 }
8977
8978 /* Output the public names table used to speed up access to externally
8979 visible names; or the public types table used to find type definitions. */
8980
8981 static void
8982 output_pubnames (vec<pubname_entry, va_gc> *names)
8983 {
8984 unsigned i;
8985 unsigned long pubnames_length = size_of_pubnames (names);
8986 pubname_ref pub;
8987
8988 if (!want_pubnames () || !info_section_emitted)
8989 return;
8990 if (names == pubname_table)
8991 switch_to_section (debug_pubnames_section);
8992 else
8993 switch_to_section (debug_pubtypes_section);
8994 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8995 dw2_asm_output_data (4, 0xffffffff,
8996 "Initial length escape value indicating 64-bit DWARF extension");
8997 if (names == pubname_table)
8998 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8999 "Length of Public Names Info");
9000 else
9001 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9002 "Length of Public Type Names Info");
9003 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
9004 dw2_asm_output_data (2, 2, "DWARF Version");
9005 if (dwarf_split_debug_info)
9006 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9007 debug_skeleton_info_section,
9008 "Offset of Compilation Unit Info");
9009 else
9010 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9011 debug_info_section,
9012 "Offset of Compilation Unit Info");
9013 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9014 "Compilation Unit Length");
9015
9016 FOR_EACH_VEC_ELT (*names, i, pub)
9017 {
9018 /* Enumerator names are part of the pubname table, but the parent
9019 DW_TAG_enumeration_type die may have been pruned. Don't output
9020 them if that is the case. */
9021 if (pub->die->die_tag == DW_TAG_enumerator && !pub->die->die_mark)
9022 continue;
9023
9024 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9025 if (names == pubname_table)
9026 gcc_assert (pub->die->die_mark);
9027
9028 if (names != pubtype_table
9029 || pub->die->die_offset != 0
9030 || !flag_eliminate_unused_debug_types)
9031 {
9032 dw_offset die_offset = pub->die->die_offset;
9033
9034 /* If we're putting types in their own .debug_types sections,
9035 the .debug_pubtypes table will still point to the compile
9036 unit (not the type unit), so we want to use the offset of
9037 the skeleton DIE (if there is one). */
9038 if (pub->die->comdat_type_p && names == pubtype_table)
9039 {
9040 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9041
9042 if (type_node != NULL)
9043 die_offset = (type_node->skeleton_die != NULL
9044 ? type_node->skeleton_die->die_offset
9045 : 0);
9046 }
9047
9048 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9049
9050 dw2_asm_output_nstring (pub->name, -1, "external name");
9051 }
9052 }
9053
9054 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9055 }
9056
9057 /* Output the information that goes into the .debug_aranges table.
9058 Namely, define the beginning and ending address range of the
9059 text section generated for this compilation unit. */
9060
9061 static void
9062 output_aranges (unsigned long aranges_length)
9063 {
9064 unsigned i;
9065
9066 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9067 dw2_asm_output_data (4, 0xffffffff,
9068 "Initial length escape value indicating 64-bit DWARF extension");
9069 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9070 "Length of Address Ranges Info");
9071 /* Version number for aranges is still 2, even in DWARF3. */
9072 dw2_asm_output_data (2, 2, "DWARF Version");
9073 if (dwarf_split_debug_info)
9074 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9075 debug_skeleton_info_section,
9076 "Offset of Compilation Unit Info");
9077 else
9078 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9079 debug_info_section,
9080 "Offset of Compilation Unit Info");
9081 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9082 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9083
9084 /* We need to align to twice the pointer size here. */
9085 if (DWARF_ARANGES_PAD_SIZE)
9086 {
9087 /* Pad using a 2 byte words so that padding is correct for any
9088 pointer size. */
9089 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9090 2 * DWARF2_ADDR_SIZE);
9091 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9092 dw2_asm_output_data (2, 0, NULL);
9093 }
9094
9095 /* It is necessary not to output these entries if the sections were
9096 not used; if the sections were not used, the length will be 0 and
9097 the address may end up as 0 if the section is discarded by ld
9098 --gc-sections, leaving an invalid (0, 0) entry that can be
9099 confused with the terminator. */
9100 if (text_section_used)
9101 {
9102 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9103 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9104 text_section_label, "Length");
9105 }
9106 if (cold_text_section_used)
9107 {
9108 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9109 "Address");
9110 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9111 cold_text_section_label, "Length");
9112 }
9113
9114 if (have_multiple_function_sections)
9115 {
9116 unsigned fde_idx;
9117 dw_fde_ref fde;
9118
9119 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9120 {
9121 if (DECL_IGNORED_P (fde->decl))
9122 continue;
9123 if (!fde->in_std_section)
9124 {
9125 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9126 "Address");
9127 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9128 fde->dw_fde_begin, "Length");
9129 }
9130 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9131 {
9132 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9133 "Address");
9134 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9135 fde->dw_fde_second_begin, "Length");
9136 }
9137 }
9138 }
9139
9140 /* Output the terminator words. */
9141 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9142 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9143 }
9144
9145 /* Add a new entry to .debug_ranges. Return the offset at which it
9146 was placed. */
9147
9148 static unsigned int
9149 add_ranges_num (int num)
9150 {
9151 unsigned int in_use = ranges_table_in_use;
9152
9153 if (in_use == ranges_table_allocated)
9154 {
9155 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9156 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9157 ranges_table_allocated);
9158 memset (ranges_table + ranges_table_in_use, 0,
9159 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9160 }
9161
9162 ranges_table[in_use].num = num;
9163 ranges_table_in_use = in_use + 1;
9164
9165 return in_use * 2 * DWARF2_ADDR_SIZE;
9166 }
9167
9168 /* Add a new entry to .debug_ranges corresponding to a block, or a
9169 range terminator if BLOCK is NULL. */
9170
9171 static unsigned int
9172 add_ranges (const_tree block)
9173 {
9174 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9175 }
9176
9177 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9178 When using dwarf_split_debug_info, address attributes in dies destined
9179 for the final executable should be direct references--setting the
9180 parameter force_direct ensures this behavior. */
9181
9182 static void
9183 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9184 bool *added, bool force_direct)
9185 {
9186 unsigned int in_use = ranges_by_label_in_use;
9187 unsigned int offset;
9188
9189 if (in_use == ranges_by_label_allocated)
9190 {
9191 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9192 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9193 ranges_by_label,
9194 ranges_by_label_allocated);
9195 memset (ranges_by_label + ranges_by_label_in_use, 0,
9196 RANGES_TABLE_INCREMENT
9197 * sizeof (struct dw_ranges_by_label_struct));
9198 }
9199
9200 ranges_by_label[in_use].begin = begin;
9201 ranges_by_label[in_use].end = end;
9202 ranges_by_label_in_use = in_use + 1;
9203
9204 offset = add_ranges_num (-(int)in_use - 1);
9205 if (!*added)
9206 {
9207 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9208 *added = true;
9209 }
9210 }
9211
9212 static void
9213 output_ranges (void)
9214 {
9215 unsigned i;
9216 static const char *const start_fmt = "Offset %#x";
9217 const char *fmt = start_fmt;
9218
9219 for (i = 0; i < ranges_table_in_use; i++)
9220 {
9221 int block_num = ranges_table[i].num;
9222
9223 if (block_num > 0)
9224 {
9225 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9226 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9227
9228 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9229 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9230
9231 /* If all code is in the text section, then the compilation
9232 unit base address defaults to DW_AT_low_pc, which is the
9233 base of the text section. */
9234 if (!have_multiple_function_sections)
9235 {
9236 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9237 text_section_label,
9238 fmt, i * 2 * DWARF2_ADDR_SIZE);
9239 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9240 text_section_label, NULL);
9241 }
9242
9243 /* Otherwise, the compilation unit base address is zero,
9244 which allows us to use absolute addresses, and not worry
9245 about whether the target supports cross-section
9246 arithmetic. */
9247 else
9248 {
9249 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9250 fmt, i * 2 * DWARF2_ADDR_SIZE);
9251 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9252 }
9253
9254 fmt = NULL;
9255 }
9256
9257 /* Negative block_num stands for an index into ranges_by_label. */
9258 else if (block_num < 0)
9259 {
9260 int lab_idx = - block_num - 1;
9261
9262 if (!have_multiple_function_sections)
9263 {
9264 gcc_unreachable ();
9265 #if 0
9266 /* If we ever use add_ranges_by_labels () for a single
9267 function section, all we have to do is to take out
9268 the #if 0 above. */
9269 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9270 ranges_by_label[lab_idx].begin,
9271 text_section_label,
9272 fmt, i * 2 * DWARF2_ADDR_SIZE);
9273 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9274 ranges_by_label[lab_idx].end,
9275 text_section_label, NULL);
9276 #endif
9277 }
9278 else
9279 {
9280 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9281 ranges_by_label[lab_idx].begin,
9282 fmt, i * 2 * DWARF2_ADDR_SIZE);
9283 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9284 ranges_by_label[lab_idx].end,
9285 NULL);
9286 }
9287 }
9288 else
9289 {
9290 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9291 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9292 fmt = start_fmt;
9293 }
9294 }
9295 }
9296
9297 /* Data structure containing information about input files. */
9298 struct file_info
9299 {
9300 const char *path; /* Complete file name. */
9301 const char *fname; /* File name part. */
9302 int length; /* Length of entire string. */
9303 struct dwarf_file_data * file_idx; /* Index in input file table. */
9304 int dir_idx; /* Index in directory table. */
9305 };
9306
9307 /* Data structure containing information about directories with source
9308 files. */
9309 struct dir_info
9310 {
9311 const char *path; /* Path including directory name. */
9312 int length; /* Path length. */
9313 int prefix; /* Index of directory entry which is a prefix. */
9314 int count; /* Number of files in this directory. */
9315 int dir_idx; /* Index of directory used as base. */
9316 };
9317
9318 /* Callback function for file_info comparison. We sort by looking at
9319 the directories in the path. */
9320
9321 static int
9322 file_info_cmp (const void *p1, const void *p2)
9323 {
9324 const struct file_info *const s1 = (const struct file_info *) p1;
9325 const struct file_info *const s2 = (const struct file_info *) p2;
9326 const unsigned char *cp1;
9327 const unsigned char *cp2;
9328
9329 /* Take care of file names without directories. We need to make sure that
9330 we return consistent values to qsort since some will get confused if
9331 we return the same value when identical operands are passed in opposite
9332 orders. So if neither has a directory, return 0 and otherwise return
9333 1 or -1 depending on which one has the directory. */
9334 if ((s1->path == s1->fname || s2->path == s2->fname))
9335 return (s2->path == s2->fname) - (s1->path == s1->fname);
9336
9337 cp1 = (const unsigned char *) s1->path;
9338 cp2 = (const unsigned char *) s2->path;
9339
9340 while (1)
9341 {
9342 ++cp1;
9343 ++cp2;
9344 /* Reached the end of the first path? If so, handle like above. */
9345 if ((cp1 == (const unsigned char *) s1->fname)
9346 || (cp2 == (const unsigned char *) s2->fname))
9347 return ((cp2 == (const unsigned char *) s2->fname)
9348 - (cp1 == (const unsigned char *) s1->fname));
9349
9350 /* Character of current path component the same? */
9351 else if (*cp1 != *cp2)
9352 return *cp1 - *cp2;
9353 }
9354 }
9355
9356 struct file_name_acquire_data
9357 {
9358 struct file_info *files;
9359 int used_files;
9360 int max_files;
9361 };
9362
9363 /* Traversal function for the hash table. */
9364
9365 static int
9366 file_name_acquire (void ** slot, void *data)
9367 {
9368 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9369 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9370 struct file_info *fi;
9371 const char *f;
9372
9373 gcc_assert (fnad->max_files >= d->emitted_number);
9374
9375 if (! d->emitted_number)
9376 return 1;
9377
9378 gcc_assert (fnad->max_files != fnad->used_files);
9379
9380 fi = fnad->files + fnad->used_files++;
9381
9382 /* Skip all leading "./". */
9383 f = d->filename;
9384 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9385 f += 2;
9386
9387 /* Create a new array entry. */
9388 fi->path = f;
9389 fi->length = strlen (f);
9390 fi->file_idx = d;
9391
9392 /* Search for the file name part. */
9393 f = strrchr (f, DIR_SEPARATOR);
9394 #if defined (DIR_SEPARATOR_2)
9395 {
9396 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9397
9398 if (g != NULL)
9399 {
9400 if (f == NULL || f < g)
9401 f = g;
9402 }
9403 }
9404 #endif
9405
9406 fi->fname = f == NULL ? fi->path : f + 1;
9407 return 1;
9408 }
9409
9410 /* Output the directory table and the file name table. We try to minimize
9411 the total amount of memory needed. A heuristic is used to avoid large
9412 slowdowns with many input files. */
9413
9414 static void
9415 output_file_names (void)
9416 {
9417 struct file_name_acquire_data fnad;
9418 int numfiles;
9419 struct file_info *files;
9420 struct dir_info *dirs;
9421 int *saved;
9422 int *savehere;
9423 int *backmap;
9424 int ndirs;
9425 int idx_offset;
9426 int i;
9427
9428 if (!last_emitted_file)
9429 {
9430 dw2_asm_output_data (1, 0, "End directory table");
9431 dw2_asm_output_data (1, 0, "End file name table");
9432 return;
9433 }
9434
9435 numfiles = last_emitted_file->emitted_number;
9436
9437 /* Allocate the various arrays we need. */
9438 files = XALLOCAVEC (struct file_info, numfiles);
9439 dirs = XALLOCAVEC (struct dir_info, numfiles);
9440
9441 fnad.files = files;
9442 fnad.used_files = 0;
9443 fnad.max_files = numfiles;
9444 htab_traverse (file_table, file_name_acquire, &fnad);
9445 gcc_assert (fnad.used_files == fnad.max_files);
9446
9447 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9448
9449 /* Find all the different directories used. */
9450 dirs[0].path = files[0].path;
9451 dirs[0].length = files[0].fname - files[0].path;
9452 dirs[0].prefix = -1;
9453 dirs[0].count = 1;
9454 dirs[0].dir_idx = 0;
9455 files[0].dir_idx = 0;
9456 ndirs = 1;
9457
9458 for (i = 1; i < numfiles; i++)
9459 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9460 && memcmp (dirs[ndirs - 1].path, files[i].path,
9461 dirs[ndirs - 1].length) == 0)
9462 {
9463 /* Same directory as last entry. */
9464 files[i].dir_idx = ndirs - 1;
9465 ++dirs[ndirs - 1].count;
9466 }
9467 else
9468 {
9469 int j;
9470
9471 /* This is a new directory. */
9472 dirs[ndirs].path = files[i].path;
9473 dirs[ndirs].length = files[i].fname - files[i].path;
9474 dirs[ndirs].count = 1;
9475 dirs[ndirs].dir_idx = ndirs;
9476 files[i].dir_idx = ndirs;
9477
9478 /* Search for a prefix. */
9479 dirs[ndirs].prefix = -1;
9480 for (j = 0; j < ndirs; j++)
9481 if (dirs[j].length < dirs[ndirs].length
9482 && dirs[j].length > 1
9483 && (dirs[ndirs].prefix == -1
9484 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9485 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9486 dirs[ndirs].prefix = j;
9487
9488 ++ndirs;
9489 }
9490
9491 /* Now to the actual work. We have to find a subset of the directories which
9492 allow expressing the file name using references to the directory table
9493 with the least amount of characters. We do not do an exhaustive search
9494 where we would have to check out every combination of every single
9495 possible prefix. Instead we use a heuristic which provides nearly optimal
9496 results in most cases and never is much off. */
9497 saved = XALLOCAVEC (int, ndirs);
9498 savehere = XALLOCAVEC (int, ndirs);
9499
9500 memset (saved, '\0', ndirs * sizeof (saved[0]));
9501 for (i = 0; i < ndirs; i++)
9502 {
9503 int j;
9504 int total;
9505
9506 /* We can always save some space for the current directory. But this
9507 does not mean it will be enough to justify adding the directory. */
9508 savehere[i] = dirs[i].length;
9509 total = (savehere[i] - saved[i]) * dirs[i].count;
9510
9511 for (j = i + 1; j < ndirs; j++)
9512 {
9513 savehere[j] = 0;
9514 if (saved[j] < dirs[i].length)
9515 {
9516 /* Determine whether the dirs[i] path is a prefix of the
9517 dirs[j] path. */
9518 int k;
9519
9520 k = dirs[j].prefix;
9521 while (k != -1 && k != (int) i)
9522 k = dirs[k].prefix;
9523
9524 if (k == (int) i)
9525 {
9526 /* Yes it is. We can possibly save some memory by
9527 writing the filenames in dirs[j] relative to
9528 dirs[i]. */
9529 savehere[j] = dirs[i].length;
9530 total += (savehere[j] - saved[j]) * dirs[j].count;
9531 }
9532 }
9533 }
9534
9535 /* Check whether we can save enough to justify adding the dirs[i]
9536 directory. */
9537 if (total > dirs[i].length + 1)
9538 {
9539 /* It's worthwhile adding. */
9540 for (j = i; j < ndirs; j++)
9541 if (savehere[j] > 0)
9542 {
9543 /* Remember how much we saved for this directory so far. */
9544 saved[j] = savehere[j];
9545
9546 /* Remember the prefix directory. */
9547 dirs[j].dir_idx = i;
9548 }
9549 }
9550 }
9551
9552 /* Emit the directory name table. */
9553 idx_offset = dirs[0].length > 0 ? 1 : 0;
9554 for (i = 1 - idx_offset; i < ndirs; i++)
9555 dw2_asm_output_nstring (dirs[i].path,
9556 dirs[i].length
9557 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9558 "Directory Entry: %#x", i + idx_offset);
9559
9560 dw2_asm_output_data (1, 0, "End directory table");
9561
9562 /* We have to emit them in the order of emitted_number since that's
9563 used in the debug info generation. To do this efficiently we
9564 generate a back-mapping of the indices first. */
9565 backmap = XALLOCAVEC (int, numfiles);
9566 for (i = 0; i < numfiles; i++)
9567 backmap[files[i].file_idx->emitted_number - 1] = i;
9568
9569 /* Now write all the file names. */
9570 for (i = 0; i < numfiles; i++)
9571 {
9572 int file_idx = backmap[i];
9573 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9574
9575 #ifdef VMS_DEBUGGING_INFO
9576 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9577
9578 /* Setting these fields can lead to debugger miscomparisons,
9579 but VMS Debug requires them to be set correctly. */
9580
9581 int ver;
9582 long long cdt;
9583 long siz;
9584 int maxfilelen = strlen (files[file_idx].path)
9585 + dirs[dir_idx].length
9586 + MAX_VMS_VERSION_LEN + 1;
9587 char *filebuf = XALLOCAVEC (char, maxfilelen);
9588
9589 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9590 snprintf (filebuf, maxfilelen, "%s;%d",
9591 files[file_idx].path + dirs[dir_idx].length, ver);
9592
9593 dw2_asm_output_nstring
9594 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9595
9596 /* Include directory index. */
9597 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9598
9599 /* Modification time. */
9600 dw2_asm_output_data_uleb128
9601 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9602 ? cdt : 0,
9603 NULL);
9604
9605 /* File length in bytes. */
9606 dw2_asm_output_data_uleb128
9607 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9608 ? siz : 0,
9609 NULL);
9610 #else
9611 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9612 "File Entry: %#x", (unsigned) i + 1);
9613
9614 /* Include directory index. */
9615 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9616
9617 /* Modification time. */
9618 dw2_asm_output_data_uleb128 (0, NULL);
9619
9620 /* File length in bytes. */
9621 dw2_asm_output_data_uleb128 (0, NULL);
9622 #endif /* VMS_DEBUGGING_INFO */
9623 }
9624
9625 dw2_asm_output_data (1, 0, "End file name table");
9626 }
9627
9628
9629 /* Output one line number table into the .debug_line section. */
9630
9631 static void
9632 output_one_line_info_table (dw_line_info_table *table)
9633 {
9634 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9635 unsigned int current_line = 1;
9636 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9637 dw_line_info_entry *ent;
9638 size_t i;
9639
9640 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9641 {
9642 switch (ent->opcode)
9643 {
9644 case LI_set_address:
9645 /* ??? Unfortunately, we have little choice here currently, and
9646 must always use the most general form. GCC does not know the
9647 address delta itself, so we can't use DW_LNS_advance_pc. Many
9648 ports do have length attributes which will give an upper bound
9649 on the address range. We could perhaps use length attributes
9650 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9651 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9652
9653 /* This can handle any delta. This takes
9654 4+DWARF2_ADDR_SIZE bytes. */
9655 dw2_asm_output_data (1, 0, "set address %s", line_label);
9656 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9657 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9658 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9659 break;
9660
9661 case LI_set_line:
9662 if (ent->val == current_line)
9663 {
9664 /* We still need to start a new row, so output a copy insn. */
9665 dw2_asm_output_data (1, DW_LNS_copy,
9666 "copy line %u", current_line);
9667 }
9668 else
9669 {
9670 int line_offset = ent->val - current_line;
9671 int line_delta = line_offset - DWARF_LINE_BASE;
9672
9673 current_line = ent->val;
9674 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9675 {
9676 /* This can handle deltas from -10 to 234, using the current
9677 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9678 This takes 1 byte. */
9679 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9680 "line %u", current_line);
9681 }
9682 else
9683 {
9684 /* This can handle any delta. This takes at least 4 bytes,
9685 depending on the value being encoded. */
9686 dw2_asm_output_data (1, DW_LNS_advance_line,
9687 "advance to line %u", current_line);
9688 dw2_asm_output_data_sleb128 (line_offset, NULL);
9689 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9690 }
9691 }
9692 break;
9693
9694 case LI_set_file:
9695 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9696 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9697 break;
9698
9699 case LI_set_column:
9700 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9701 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9702 break;
9703
9704 case LI_negate_stmt:
9705 current_is_stmt = !current_is_stmt;
9706 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9707 "is_stmt %d", current_is_stmt);
9708 break;
9709
9710 case LI_set_prologue_end:
9711 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9712 "set prologue end");
9713 break;
9714
9715 case LI_set_epilogue_begin:
9716 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9717 "set epilogue begin");
9718 break;
9719
9720 case LI_set_discriminator:
9721 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9722 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9723 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9724 dw2_asm_output_data_uleb128 (ent->val, NULL);
9725 break;
9726 }
9727 }
9728
9729 /* Emit debug info for the address of the end of the table. */
9730 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9731 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9732 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9733 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9734
9735 dw2_asm_output_data (1, 0, "end sequence");
9736 dw2_asm_output_data_uleb128 (1, NULL);
9737 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9738 }
9739
9740 /* Output the source line number correspondence information. This
9741 information goes into the .debug_line section. */
9742
9743 static void
9744 output_line_info (bool prologue_only)
9745 {
9746 char l1[20], l2[20], p1[20], p2[20];
9747 int ver = dwarf_version;
9748 bool saw_one = false;
9749 int opc;
9750
9751 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9752 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9753 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9754 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9755
9756 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9757 dw2_asm_output_data (4, 0xffffffff,
9758 "Initial length escape value indicating 64-bit DWARF extension");
9759 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9760 "Length of Source Line Info");
9761 ASM_OUTPUT_LABEL (asm_out_file, l1);
9762
9763 dw2_asm_output_data (2, ver, "DWARF Version");
9764 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9765 ASM_OUTPUT_LABEL (asm_out_file, p1);
9766
9767 /* Define the architecture-dependent minimum instruction length (in bytes).
9768 In this implementation of DWARF, this field is used for information
9769 purposes only. Since GCC generates assembly language, we have no
9770 a priori knowledge of how many instruction bytes are generated for each
9771 source line, and therefore can use only the DW_LNE_set_address and
9772 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9773 this as '1', which is "correct enough" for all architectures,
9774 and don't let the target override. */
9775 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9776
9777 if (ver >= 4)
9778 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9779 "Maximum Operations Per Instruction");
9780 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9781 "Default is_stmt_start flag");
9782 dw2_asm_output_data (1, DWARF_LINE_BASE,
9783 "Line Base Value (Special Opcodes)");
9784 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9785 "Line Range Value (Special Opcodes)");
9786 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9787 "Special Opcode Base");
9788
9789 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9790 {
9791 int n_op_args;
9792 switch (opc)
9793 {
9794 case DW_LNS_advance_pc:
9795 case DW_LNS_advance_line:
9796 case DW_LNS_set_file:
9797 case DW_LNS_set_column:
9798 case DW_LNS_fixed_advance_pc:
9799 case DW_LNS_set_isa:
9800 n_op_args = 1;
9801 break;
9802 default:
9803 n_op_args = 0;
9804 break;
9805 }
9806
9807 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9808 opc, n_op_args);
9809 }
9810
9811 /* Write out the information about the files we use. */
9812 output_file_names ();
9813 ASM_OUTPUT_LABEL (asm_out_file, p2);
9814 if (prologue_only)
9815 {
9816 /* Output the marker for the end of the line number info. */
9817 ASM_OUTPUT_LABEL (asm_out_file, l2);
9818 return;
9819 }
9820
9821 if (separate_line_info)
9822 {
9823 dw_line_info_table *table;
9824 size_t i;
9825
9826 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
9827 if (table->in_use)
9828 {
9829 output_one_line_info_table (table);
9830 saw_one = true;
9831 }
9832 }
9833 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9834 {
9835 output_one_line_info_table (cold_text_section_line_info);
9836 saw_one = true;
9837 }
9838
9839 /* ??? Some Darwin linkers crash on a .debug_line section with no
9840 sequences. Further, merely a DW_LNE_end_sequence entry is not
9841 sufficient -- the address column must also be initialized.
9842 Make sure to output at least one set_address/end_sequence pair,
9843 choosing .text since that section is always present. */
9844 if (text_section_line_info->in_use || !saw_one)
9845 output_one_line_info_table (text_section_line_info);
9846
9847 /* Output the marker for the end of the line number info. */
9848 ASM_OUTPUT_LABEL (asm_out_file, l2);
9849 }
9850 \f
9851 /* Given a pointer to a tree node for some base type, return a pointer to
9852 a DIE that describes the given type.
9853
9854 This routine must only be called for GCC type nodes that correspond to
9855 Dwarf base (fundamental) types. */
9856
9857 static dw_die_ref
9858 base_type_die (tree type)
9859 {
9860 dw_die_ref base_type_result;
9861 enum dwarf_type encoding;
9862
9863 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9864 return 0;
9865
9866 /* If this is a subtype that should not be emitted as a subrange type,
9867 use the base type. See subrange_type_for_debug_p. */
9868 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9869 type = TREE_TYPE (type);
9870
9871 switch (TREE_CODE (type))
9872 {
9873 case INTEGER_TYPE:
9874 if ((dwarf_version >= 4 || !dwarf_strict)
9875 && TYPE_NAME (type)
9876 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9877 && DECL_IS_BUILTIN (TYPE_NAME (type))
9878 && DECL_NAME (TYPE_NAME (type)))
9879 {
9880 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9881 if (strcmp (name, "char16_t") == 0
9882 || strcmp (name, "char32_t") == 0)
9883 {
9884 encoding = DW_ATE_UTF;
9885 break;
9886 }
9887 }
9888 if (TYPE_STRING_FLAG (type))
9889 {
9890 if (TYPE_UNSIGNED (type))
9891 encoding = DW_ATE_unsigned_char;
9892 else
9893 encoding = DW_ATE_signed_char;
9894 }
9895 else if (TYPE_UNSIGNED (type))
9896 encoding = DW_ATE_unsigned;
9897 else
9898 encoding = DW_ATE_signed;
9899 break;
9900
9901 case REAL_TYPE:
9902 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9903 {
9904 if (dwarf_version >= 3 || !dwarf_strict)
9905 encoding = DW_ATE_decimal_float;
9906 else
9907 encoding = DW_ATE_lo_user;
9908 }
9909 else
9910 encoding = DW_ATE_float;
9911 break;
9912
9913 case FIXED_POINT_TYPE:
9914 if (!(dwarf_version >= 3 || !dwarf_strict))
9915 encoding = DW_ATE_lo_user;
9916 else if (TYPE_UNSIGNED (type))
9917 encoding = DW_ATE_unsigned_fixed;
9918 else
9919 encoding = DW_ATE_signed_fixed;
9920 break;
9921
9922 /* Dwarf2 doesn't know anything about complex ints, so use
9923 a user defined type for it. */
9924 case COMPLEX_TYPE:
9925 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9926 encoding = DW_ATE_complex_float;
9927 else
9928 encoding = DW_ATE_lo_user;
9929 break;
9930
9931 case BOOLEAN_TYPE:
9932 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
9933 encoding = DW_ATE_boolean;
9934 break;
9935
9936 default:
9937 /* No other TREE_CODEs are Dwarf fundamental types. */
9938 gcc_unreachable ();
9939 }
9940
9941 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9942
9943 add_AT_unsigned (base_type_result, DW_AT_byte_size,
9944 int_size_in_bytes (type));
9945 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9946 add_pubtype (type, base_type_result);
9947
9948 return base_type_result;
9949 }
9950
9951 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9952 given input type is a Dwarf "fundamental" type. Otherwise return null. */
9953
9954 static inline int
9955 is_base_type (tree type)
9956 {
9957 switch (TREE_CODE (type))
9958 {
9959 case ERROR_MARK:
9960 case VOID_TYPE:
9961 case INTEGER_TYPE:
9962 case REAL_TYPE:
9963 case FIXED_POINT_TYPE:
9964 case COMPLEX_TYPE:
9965 case BOOLEAN_TYPE:
9966 return 1;
9967
9968 case ARRAY_TYPE:
9969 case RECORD_TYPE:
9970 case UNION_TYPE:
9971 case QUAL_UNION_TYPE:
9972 case ENUMERAL_TYPE:
9973 case FUNCTION_TYPE:
9974 case METHOD_TYPE:
9975 case POINTER_TYPE:
9976 case REFERENCE_TYPE:
9977 case NULLPTR_TYPE:
9978 case OFFSET_TYPE:
9979 case LANG_TYPE:
9980 case VECTOR_TYPE:
9981 return 0;
9982
9983 default:
9984 gcc_unreachable ();
9985 }
9986
9987 return 0;
9988 }
9989
9990 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9991 node, return the size in bits for the type if it is a constant, or else
9992 return the alignment for the type if the type's size is not constant, or
9993 else return BITS_PER_WORD if the type actually turns out to be an
9994 ERROR_MARK node. */
9995
9996 static inline unsigned HOST_WIDE_INT
9997 simple_type_size_in_bits (const_tree type)
9998 {
9999 if (TREE_CODE (type) == ERROR_MARK)
10000 return BITS_PER_WORD;
10001 else if (TYPE_SIZE (type) == NULL_TREE)
10002 return 0;
10003 else if (host_integerp (TYPE_SIZE (type), 1))
10004 return tree_low_cst (TYPE_SIZE (type), 1);
10005 else
10006 return TYPE_ALIGN (type);
10007 }
10008
10009 /* Similarly, but return a double_int instead of UHWI. */
10010
10011 static inline double_int
10012 double_int_type_size_in_bits (const_tree type)
10013 {
10014 if (TREE_CODE (type) == ERROR_MARK)
10015 return double_int::from_uhwi (BITS_PER_WORD);
10016 else if (TYPE_SIZE (type) == NULL_TREE)
10017 return double_int_zero;
10018 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10019 return tree_to_double_int (TYPE_SIZE (type));
10020 else
10021 return double_int::from_uhwi (TYPE_ALIGN (type));
10022 }
10023
10024 /* Given a pointer to a tree node for a subrange type, return a pointer
10025 to a DIE that describes the given type. */
10026
10027 static dw_die_ref
10028 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10029 {
10030 dw_die_ref subrange_die;
10031 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10032
10033 if (context_die == NULL)
10034 context_die = comp_unit_die ();
10035
10036 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10037
10038 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10039 {
10040 /* The size of the subrange type and its base type do not match,
10041 so we need to generate a size attribute for the subrange type. */
10042 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10043 }
10044
10045 if (low)
10046 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10047 if (high)
10048 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10049
10050 return subrange_die;
10051 }
10052
10053 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10054 entry that chains various modifiers in front of the given type. */
10055
10056 static dw_die_ref
10057 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10058 dw_die_ref context_die)
10059 {
10060 enum tree_code code = TREE_CODE (type);
10061 dw_die_ref mod_type_die;
10062 dw_die_ref sub_die = NULL;
10063 tree item_type = NULL;
10064 tree qualified_type;
10065 tree name, low, high;
10066 dw_die_ref mod_scope;
10067
10068 if (code == ERROR_MARK)
10069 return NULL;
10070
10071 /* See if we already have the appropriately qualified variant of
10072 this type. */
10073 qualified_type
10074 = get_qualified_type (type,
10075 ((is_const_type ? TYPE_QUAL_CONST : 0)
10076 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10077
10078 if (qualified_type == sizetype
10079 && TYPE_NAME (qualified_type)
10080 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10081 {
10082 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10083
10084 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10085 && TYPE_PRECISION (t)
10086 == TYPE_PRECISION (qualified_type)
10087 && TYPE_UNSIGNED (t)
10088 == TYPE_UNSIGNED (qualified_type));
10089 qualified_type = t;
10090 }
10091
10092 /* If we do, then we can just use its DIE, if it exists. */
10093 if (qualified_type)
10094 {
10095 mod_type_die = lookup_type_die (qualified_type);
10096 if (mod_type_die)
10097 return mod_type_die;
10098 }
10099
10100 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10101
10102 /* Handle C typedef types. */
10103 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10104 && !DECL_ARTIFICIAL (name))
10105 {
10106 tree dtype = TREE_TYPE (name);
10107
10108 if (qualified_type == dtype)
10109 {
10110 /* For a named type, use the typedef. */
10111 gen_type_die (qualified_type, context_die);
10112 return lookup_type_die (qualified_type);
10113 }
10114 else if (is_const_type < TYPE_READONLY (dtype)
10115 || is_volatile_type < TYPE_VOLATILE (dtype)
10116 || (is_const_type <= TYPE_READONLY (dtype)
10117 && is_volatile_type <= TYPE_VOLATILE (dtype)
10118 && DECL_ORIGINAL_TYPE (name) != type))
10119 /* cv-unqualified version of named type. Just use the unnamed
10120 type to which it refers. */
10121 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10122 is_const_type, is_volatile_type,
10123 context_die);
10124 /* Else cv-qualified version of named type; fall through. */
10125 }
10126
10127 mod_scope = scope_die_for (type, context_die);
10128
10129 if (is_const_type
10130 /* If both is_const_type and is_volatile_type, prefer the path
10131 which leads to a qualified type. */
10132 && (!is_volatile_type
10133 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10134 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10135 {
10136 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10137 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10138 }
10139 else if (is_volatile_type)
10140 {
10141 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10142 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10143 }
10144 else if (code == POINTER_TYPE)
10145 {
10146 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10147 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10148 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10149 item_type = TREE_TYPE (type);
10150 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10151 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10152 TYPE_ADDR_SPACE (item_type));
10153 }
10154 else if (code == REFERENCE_TYPE)
10155 {
10156 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10157 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10158 type);
10159 else
10160 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10161 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10162 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10163 item_type = TREE_TYPE (type);
10164 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10165 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10166 TYPE_ADDR_SPACE (item_type));
10167 }
10168 else if (code == INTEGER_TYPE
10169 && TREE_TYPE (type) != NULL_TREE
10170 && subrange_type_for_debug_p (type, &low, &high))
10171 {
10172 mod_type_die = subrange_type_die (type, low, high, context_die);
10173 item_type = TREE_TYPE (type);
10174 }
10175 else if (is_base_type (type))
10176 mod_type_die = base_type_die (type);
10177 else
10178 {
10179 gen_type_die (type, context_die);
10180
10181 /* We have to get the type_main_variant here (and pass that to the
10182 `lookup_type_die' routine) because the ..._TYPE node we have
10183 might simply be a *copy* of some original type node (where the
10184 copy was created to help us keep track of typedef names) and
10185 that copy might have a different TYPE_UID from the original
10186 ..._TYPE node. */
10187 if (TREE_CODE (type) != VECTOR_TYPE)
10188 return lookup_type_die (type_main_variant (type));
10189 else
10190 /* Vectors have the debugging information in the type,
10191 not the main variant. */
10192 return lookup_type_die (type);
10193 }
10194
10195 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10196 don't output a DW_TAG_typedef, since there isn't one in the
10197 user's program; just attach a DW_AT_name to the type.
10198 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10199 if the base type already has the same name. */
10200 if (name
10201 && ((TREE_CODE (name) != TYPE_DECL
10202 && (qualified_type == TYPE_MAIN_VARIANT (type)
10203 || (!is_const_type && !is_volatile_type)))
10204 || (TREE_CODE (name) == TYPE_DECL
10205 && TREE_TYPE (name) == qualified_type
10206 && DECL_NAME (name))))
10207 {
10208 if (TREE_CODE (name) == TYPE_DECL)
10209 /* Could just call add_name_and_src_coords_attributes here,
10210 but since this is a builtin type it doesn't have any
10211 useful source coordinates anyway. */
10212 name = DECL_NAME (name);
10213 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10214 }
10215 /* This probably indicates a bug. */
10216 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10217 {
10218 name = TYPE_NAME (type);
10219 if (name
10220 && TREE_CODE (name) == TYPE_DECL)
10221 name = DECL_NAME (name);
10222 add_name_attribute (mod_type_die,
10223 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10224 }
10225
10226 if (qualified_type)
10227 equate_type_number_to_die (qualified_type, mod_type_die);
10228
10229 if (item_type)
10230 /* We must do this after the equate_type_number_to_die call, in case
10231 this is a recursive type. This ensures that the modified_type_die
10232 recursion will terminate even if the type is recursive. Recursive
10233 types are possible in Ada. */
10234 sub_die = modified_type_die (item_type,
10235 TYPE_READONLY (item_type),
10236 TYPE_VOLATILE (item_type),
10237 context_die);
10238
10239 if (sub_die != NULL)
10240 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10241
10242 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10243 if (TYPE_ARTIFICIAL (type))
10244 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10245
10246 return mod_type_die;
10247 }
10248
10249 /* Generate DIEs for the generic parameters of T.
10250 T must be either a generic type or a generic function.
10251 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10252
10253 static void
10254 gen_generic_params_dies (tree t)
10255 {
10256 tree parms, args;
10257 int parms_num, i;
10258 dw_die_ref die = NULL;
10259 int non_default;
10260
10261 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10262 return;
10263
10264 if (TYPE_P (t))
10265 die = lookup_type_die (t);
10266 else if (DECL_P (t))
10267 die = lookup_decl_die (t);
10268
10269 gcc_assert (die);
10270
10271 parms = lang_hooks.get_innermost_generic_parms (t);
10272 if (!parms)
10273 /* T has no generic parameter. It means T is neither a generic type
10274 or function. End of story. */
10275 return;
10276
10277 parms_num = TREE_VEC_LENGTH (parms);
10278 args = lang_hooks.get_innermost_generic_args (t);
10279 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10280 non_default = int_cst_value (TREE_CHAIN (args));
10281 else
10282 non_default = TREE_VEC_LENGTH (args);
10283 for (i = 0; i < parms_num; i++)
10284 {
10285 tree parm, arg, arg_pack_elems;
10286 dw_die_ref parm_die;
10287
10288 parm = TREE_VEC_ELT (parms, i);
10289 arg = TREE_VEC_ELT (args, i);
10290 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10291 gcc_assert (parm && TREE_VALUE (parm) && arg);
10292
10293 if (parm && TREE_VALUE (parm) && arg)
10294 {
10295 /* If PARM represents a template parameter pack,
10296 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10297 by DW_TAG_template_*_parameter DIEs for the argument
10298 pack elements of ARG. Note that ARG would then be
10299 an argument pack. */
10300 if (arg_pack_elems)
10301 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10302 arg_pack_elems,
10303 die);
10304 else
10305 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10306 true /* emit name */, die);
10307 if (i >= non_default)
10308 add_AT_flag (parm_die, DW_AT_default_value, 1);
10309 }
10310 }
10311 }
10312
10313 /* Create and return a DIE for PARM which should be
10314 the representation of a generic type parameter.
10315 For instance, in the C++ front end, PARM would be a template parameter.
10316 ARG is the argument to PARM.
10317 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10318 name of the PARM.
10319 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10320 as a child node. */
10321
10322 static dw_die_ref
10323 generic_parameter_die (tree parm, tree arg,
10324 bool emit_name_p,
10325 dw_die_ref parent_die)
10326 {
10327 dw_die_ref tmpl_die = NULL;
10328 const char *name = NULL;
10329
10330 if (!parm || !DECL_NAME (parm) || !arg)
10331 return NULL;
10332
10333 /* We support non-type generic parameters and arguments,
10334 type generic parameters and arguments, as well as
10335 generic generic parameters (a.k.a. template template parameters in C++)
10336 and arguments. */
10337 if (TREE_CODE (parm) == PARM_DECL)
10338 /* PARM is a nontype generic parameter */
10339 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10340 else if (TREE_CODE (parm) == TYPE_DECL)
10341 /* PARM is a type generic parameter. */
10342 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10343 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10344 /* PARM is a generic generic parameter.
10345 Its DIE is a GNU extension. It shall have a
10346 DW_AT_name attribute to represent the name of the template template
10347 parameter, and a DW_AT_GNU_template_name attribute to represent the
10348 name of the template template argument. */
10349 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10350 parent_die, parm);
10351 else
10352 gcc_unreachable ();
10353
10354 if (tmpl_die)
10355 {
10356 tree tmpl_type;
10357
10358 /* If PARM is a generic parameter pack, it means we are
10359 emitting debug info for a template argument pack element.
10360 In other terms, ARG is a template argument pack element.
10361 In that case, we don't emit any DW_AT_name attribute for
10362 the die. */
10363 if (emit_name_p)
10364 {
10365 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10366 gcc_assert (name);
10367 add_AT_string (tmpl_die, DW_AT_name, name);
10368 }
10369
10370 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10371 {
10372 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10373 TMPL_DIE should have a child DW_AT_type attribute that is set
10374 to the type of the argument to PARM, which is ARG.
10375 If PARM is a type generic parameter, TMPL_DIE should have a
10376 child DW_AT_type that is set to ARG. */
10377 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10378 add_type_attribute (tmpl_die, tmpl_type, 0,
10379 TREE_THIS_VOLATILE (tmpl_type),
10380 parent_die);
10381 }
10382 else
10383 {
10384 /* So TMPL_DIE is a DIE representing a
10385 a generic generic template parameter, a.k.a template template
10386 parameter in C++ and arg is a template. */
10387
10388 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10389 to the name of the argument. */
10390 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10391 if (name)
10392 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10393 }
10394
10395 if (TREE_CODE (parm) == PARM_DECL)
10396 /* So PARM is a non-type generic parameter.
10397 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10398 attribute of TMPL_DIE which value represents the value
10399 of ARG.
10400 We must be careful here:
10401 The value of ARG might reference some function decls.
10402 We might currently be emitting debug info for a generic
10403 type and types are emitted before function decls, we don't
10404 know if the function decls referenced by ARG will actually be
10405 emitted after cgraph computations.
10406 So must defer the generation of the DW_AT_const_value to
10407 after cgraph is ready. */
10408 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10409 }
10410
10411 return tmpl_die;
10412 }
10413
10414 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10415 PARM_PACK must be a template parameter pack. The returned DIE
10416 will be child DIE of PARENT_DIE. */
10417
10418 static dw_die_ref
10419 template_parameter_pack_die (tree parm_pack,
10420 tree parm_pack_args,
10421 dw_die_ref parent_die)
10422 {
10423 dw_die_ref die;
10424 int j;
10425
10426 gcc_assert (parent_die && parm_pack);
10427
10428 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10429 add_name_and_src_coords_attributes (die, parm_pack);
10430 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10431 generic_parameter_die (parm_pack,
10432 TREE_VEC_ELT (parm_pack_args, j),
10433 false /* Don't emit DW_AT_name */,
10434 die);
10435 return die;
10436 }
10437
10438 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10439 an enumerated type. */
10440
10441 static inline int
10442 type_is_enum (const_tree type)
10443 {
10444 return TREE_CODE (type) == ENUMERAL_TYPE;
10445 }
10446
10447 /* Return the DBX register number described by a given RTL node. */
10448
10449 static unsigned int
10450 dbx_reg_number (const_rtx rtl)
10451 {
10452 unsigned regno = REGNO (rtl);
10453
10454 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10455
10456 #ifdef LEAF_REG_REMAP
10457 if (crtl->uses_only_leaf_regs)
10458 {
10459 int leaf_reg = LEAF_REG_REMAP (regno);
10460 if (leaf_reg != -1)
10461 regno = (unsigned) leaf_reg;
10462 }
10463 #endif
10464
10465 regno = DBX_REGISTER_NUMBER (regno);
10466 gcc_assert (regno != INVALID_REGNUM);
10467 return regno;
10468 }
10469
10470 /* Optionally add a DW_OP_piece term to a location description expression.
10471 DW_OP_piece is only added if the location description expression already
10472 doesn't end with DW_OP_piece. */
10473
10474 static void
10475 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10476 {
10477 dw_loc_descr_ref loc;
10478
10479 if (*list_head != NULL)
10480 {
10481 /* Find the end of the chain. */
10482 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10483 ;
10484
10485 if (loc->dw_loc_opc != DW_OP_piece)
10486 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10487 }
10488 }
10489
10490 /* Return a location descriptor that designates a machine register or
10491 zero if there is none. */
10492
10493 static dw_loc_descr_ref
10494 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10495 {
10496 rtx regs;
10497
10498 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10499 return 0;
10500
10501 /* We only use "frame base" when we're sure we're talking about the
10502 post-prologue local stack frame. We do this by *not* running
10503 register elimination until this point, and recognizing the special
10504 argument pointer and soft frame pointer rtx's.
10505 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10506 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10507 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10508 {
10509 dw_loc_descr_ref result = NULL;
10510
10511 if (dwarf_version >= 4 || !dwarf_strict)
10512 {
10513 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10514 initialized);
10515 if (result)
10516 add_loc_descr (&result,
10517 new_loc_descr (DW_OP_stack_value, 0, 0));
10518 }
10519 return result;
10520 }
10521
10522 regs = targetm.dwarf_register_span (rtl);
10523
10524 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10525 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10526 else
10527 {
10528 unsigned int dbx_regnum = dbx_reg_number (rtl);
10529 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10530 return 0;
10531 return one_reg_loc_descriptor (dbx_regnum, initialized);
10532 }
10533 }
10534
10535 /* Return a location descriptor that designates a machine register for
10536 a given hard register number. */
10537
10538 static dw_loc_descr_ref
10539 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10540 {
10541 dw_loc_descr_ref reg_loc_descr;
10542
10543 if (regno <= 31)
10544 reg_loc_descr
10545 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10546 else
10547 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10548
10549 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10550 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10551
10552 return reg_loc_descr;
10553 }
10554
10555 /* Given an RTL of a register, return a location descriptor that
10556 designates a value that spans more than one register. */
10557
10558 static dw_loc_descr_ref
10559 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10560 enum var_init_status initialized)
10561 {
10562 int nregs, size, i;
10563 unsigned reg;
10564 dw_loc_descr_ref loc_result = NULL;
10565
10566 reg = REGNO (rtl);
10567 #ifdef LEAF_REG_REMAP
10568 if (crtl->uses_only_leaf_regs)
10569 {
10570 int leaf_reg = LEAF_REG_REMAP (reg);
10571 if (leaf_reg != -1)
10572 reg = (unsigned) leaf_reg;
10573 }
10574 #endif
10575 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10576 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10577
10578 /* Simple, contiguous registers. */
10579 if (regs == NULL_RTX)
10580 {
10581 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10582
10583 loc_result = NULL;
10584 while (nregs--)
10585 {
10586 dw_loc_descr_ref t;
10587
10588 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10589 VAR_INIT_STATUS_INITIALIZED);
10590 add_loc_descr (&loc_result, t);
10591 add_loc_descr_op_piece (&loc_result, size);
10592 ++reg;
10593 }
10594 return loc_result;
10595 }
10596
10597 /* Now onto stupid register sets in non contiguous locations. */
10598
10599 gcc_assert (GET_CODE (regs) == PARALLEL);
10600
10601 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10602 loc_result = NULL;
10603
10604 for (i = 0; i < XVECLEN (regs, 0); ++i)
10605 {
10606 dw_loc_descr_ref t;
10607
10608 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10609 VAR_INIT_STATUS_INITIALIZED);
10610 add_loc_descr (&loc_result, t);
10611 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10612 add_loc_descr_op_piece (&loc_result, size);
10613 }
10614
10615 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10616 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10617 return loc_result;
10618 }
10619
10620 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10621
10622 /* Return a location descriptor that designates a constant i,
10623 as a compound operation from constant (i >> shift), constant shift
10624 and DW_OP_shl. */
10625
10626 static dw_loc_descr_ref
10627 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10628 {
10629 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10630 add_loc_descr (&ret, int_loc_descriptor (shift));
10631 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10632 return ret;
10633 }
10634
10635 /* Return a location descriptor that designates a constant. */
10636
10637 static dw_loc_descr_ref
10638 int_loc_descriptor (HOST_WIDE_INT i)
10639 {
10640 enum dwarf_location_atom op;
10641
10642 /* Pick the smallest representation of a constant, rather than just
10643 defaulting to the LEB encoding. */
10644 if (i >= 0)
10645 {
10646 int clz = clz_hwi (i);
10647 int ctz = ctz_hwi (i);
10648 if (i <= 31)
10649 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10650 else if (i <= 0xff)
10651 op = DW_OP_const1u;
10652 else if (i <= 0xffff)
10653 op = DW_OP_const2u;
10654 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10655 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10656 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10657 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10658 while DW_OP_const4u is 5 bytes. */
10659 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10660 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10661 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10662 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10663 while DW_OP_const4u is 5 bytes. */
10664 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10665 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10666 op = DW_OP_const4u;
10667 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10668 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10669 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10670 while DW_OP_constu of constant >= 0x100000000 takes at least
10671 6 bytes. */
10672 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10673 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10674 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10675 >= HOST_BITS_PER_WIDE_INT)
10676 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10677 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10678 while DW_OP_constu takes in this case at least 6 bytes. */
10679 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10680 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10681 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10682 && size_of_uleb128 (i) > 6)
10683 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10684 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10685 else
10686 op = DW_OP_constu;
10687 }
10688 else
10689 {
10690 if (i >= -0x80)
10691 op = DW_OP_const1s;
10692 else if (i >= -0x8000)
10693 op = DW_OP_const2s;
10694 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10695 {
10696 if (size_of_int_loc_descriptor (i) < 5)
10697 {
10698 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10699 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10700 return ret;
10701 }
10702 op = DW_OP_const4s;
10703 }
10704 else
10705 {
10706 if (size_of_int_loc_descriptor (i)
10707 < (unsigned long) 1 + size_of_sleb128 (i))
10708 {
10709 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10710 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10711 return ret;
10712 }
10713 op = DW_OP_consts;
10714 }
10715 }
10716
10717 return new_loc_descr (op, i, 0);
10718 }
10719
10720 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10721 without actually allocating it. */
10722
10723 static unsigned long
10724 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10725 {
10726 return size_of_int_loc_descriptor (i >> shift)
10727 + size_of_int_loc_descriptor (shift)
10728 + 1;
10729 }
10730
10731 /* Return size_of_locs (int_loc_descriptor (i)) without
10732 actually allocating it. */
10733
10734 static unsigned long
10735 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10736 {
10737 unsigned long s;
10738
10739 if (i >= 0)
10740 {
10741 int clz, ctz;
10742 if (i <= 31)
10743 return 1;
10744 else if (i <= 0xff)
10745 return 2;
10746 else if (i <= 0xffff)
10747 return 3;
10748 clz = clz_hwi (i);
10749 ctz = ctz_hwi (i);
10750 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10751 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10752 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10753 - clz - 5);
10754 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10755 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10756 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10757 - clz - 8);
10758 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10759 return 5;
10760 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10761 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10762 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10763 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10764 - clz - 8);
10765 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10766 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10767 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10768 - clz - 16);
10769 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10770 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10771 && s > 6)
10772 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10773 - clz - 32);
10774 else
10775 return 1 + s;
10776 }
10777 else
10778 {
10779 if (i >= -0x80)
10780 return 2;
10781 else if (i >= -0x8000)
10782 return 3;
10783 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10784 {
10785 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10786 {
10787 s = size_of_int_loc_descriptor (-i) + 1;
10788 if (s < 5)
10789 return s;
10790 }
10791 return 5;
10792 }
10793 else
10794 {
10795 unsigned long r = 1 + size_of_sleb128 (i);
10796 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10797 {
10798 s = size_of_int_loc_descriptor (-i) + 1;
10799 if (s < r)
10800 return s;
10801 }
10802 return r;
10803 }
10804 }
10805 }
10806
10807 /* Return loc description representing "address" of integer value.
10808 This can appear only as toplevel expression. */
10809
10810 static dw_loc_descr_ref
10811 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10812 {
10813 int litsize;
10814 dw_loc_descr_ref loc_result = NULL;
10815
10816 if (!(dwarf_version >= 4 || !dwarf_strict))
10817 return NULL;
10818
10819 litsize = size_of_int_loc_descriptor (i);
10820 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10821 is more compact. For DW_OP_stack_value we need:
10822 litsize + 1 (DW_OP_stack_value)
10823 and for DW_OP_implicit_value:
10824 1 (DW_OP_implicit_value) + 1 (length) + size. */
10825 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10826 {
10827 loc_result = int_loc_descriptor (i);
10828 add_loc_descr (&loc_result,
10829 new_loc_descr (DW_OP_stack_value, 0, 0));
10830 return loc_result;
10831 }
10832
10833 loc_result = new_loc_descr (DW_OP_implicit_value,
10834 size, 0);
10835 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10836 loc_result->dw_loc_oprnd2.v.val_int = i;
10837 return loc_result;
10838 }
10839
10840 /* Return a location descriptor that designates a base+offset location. */
10841
10842 static dw_loc_descr_ref
10843 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10844 enum var_init_status initialized)
10845 {
10846 unsigned int regno;
10847 dw_loc_descr_ref result;
10848 dw_fde_ref fde = cfun->fde;
10849
10850 /* We only use "frame base" when we're sure we're talking about the
10851 post-prologue local stack frame. We do this by *not* running
10852 register elimination until this point, and recognizing the special
10853 argument pointer and soft frame pointer rtx's. */
10854 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10855 {
10856 rtx elim = (ira_use_lra_p
10857 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10858 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10859
10860 if (elim != reg)
10861 {
10862 if (GET_CODE (elim) == PLUS)
10863 {
10864 offset += INTVAL (XEXP (elim, 1));
10865 elim = XEXP (elim, 0);
10866 }
10867 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10868 && (elim == hard_frame_pointer_rtx
10869 || elim == stack_pointer_rtx))
10870 || elim == (frame_pointer_needed
10871 ? hard_frame_pointer_rtx
10872 : stack_pointer_rtx));
10873
10874 /* If drap register is used to align stack, use frame
10875 pointer + offset to access stack variables. If stack
10876 is aligned without drap, use stack pointer + offset to
10877 access stack variables. */
10878 if (crtl->stack_realign_tried
10879 && reg == frame_pointer_rtx)
10880 {
10881 int base_reg
10882 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10883 ? HARD_FRAME_POINTER_REGNUM
10884 : REGNO (elim));
10885 return new_reg_loc_descr (base_reg, offset);
10886 }
10887
10888 gcc_assert (frame_pointer_fb_offset_valid);
10889 offset += frame_pointer_fb_offset;
10890 return new_loc_descr (DW_OP_fbreg, offset, 0);
10891 }
10892 }
10893
10894 regno = REGNO (reg);
10895 #ifdef LEAF_REG_REMAP
10896 if (crtl->uses_only_leaf_regs)
10897 {
10898 int leaf_reg = LEAF_REG_REMAP (regno);
10899 if (leaf_reg != -1)
10900 regno = (unsigned) leaf_reg;
10901 }
10902 #endif
10903 regno = DWARF_FRAME_REGNUM (regno);
10904
10905 if (!optimize && fde
10906 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10907 {
10908 /* Use cfa+offset to represent the location of arguments passed
10909 on the stack when drap is used to align stack.
10910 Only do this when not optimizing, for optimized code var-tracking
10911 is supposed to track where the arguments live and the register
10912 used as vdrap or drap in some spot might be used for something
10913 else in other part of the routine. */
10914 return new_loc_descr (DW_OP_fbreg, offset, 0);
10915 }
10916
10917 if (regno <= 31)
10918 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10919 offset, 0);
10920 else
10921 result = new_loc_descr (DW_OP_bregx, regno, offset);
10922
10923 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10924 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10925
10926 return result;
10927 }
10928
10929 /* Return true if this RTL expression describes a base+offset calculation. */
10930
10931 static inline int
10932 is_based_loc (const_rtx rtl)
10933 {
10934 return (GET_CODE (rtl) == PLUS
10935 && ((REG_P (XEXP (rtl, 0))
10936 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10937 && CONST_INT_P (XEXP (rtl, 1)))));
10938 }
10939
10940 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10941 failed. */
10942
10943 static dw_loc_descr_ref
10944 tls_mem_loc_descriptor (rtx mem)
10945 {
10946 tree base;
10947 dw_loc_descr_ref loc_result;
10948
10949 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10950 return NULL;
10951
10952 base = get_base_address (MEM_EXPR (mem));
10953 if (base == NULL
10954 || TREE_CODE (base) != VAR_DECL
10955 || !DECL_THREAD_LOCAL_P (base))
10956 return NULL;
10957
10958 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10959 if (loc_result == NULL)
10960 return NULL;
10961
10962 if (MEM_OFFSET (mem))
10963 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10964
10965 return loc_result;
10966 }
10967
10968 /* Output debug info about reason why we failed to expand expression as dwarf
10969 expression. */
10970
10971 static void
10972 expansion_failed (tree expr, rtx rtl, char const *reason)
10973 {
10974 if (dump_file && (dump_flags & TDF_DETAILS))
10975 {
10976 fprintf (dump_file, "Failed to expand as dwarf: ");
10977 if (expr)
10978 print_generic_expr (dump_file, expr, dump_flags);
10979 if (rtl)
10980 {
10981 fprintf (dump_file, "\n");
10982 print_rtl (dump_file, rtl);
10983 }
10984 fprintf (dump_file, "\nReason: %s\n", reason);
10985 }
10986 }
10987
10988 /* Helper function for const_ok_for_output, called either directly
10989 or via for_each_rtx. */
10990
10991 static int
10992 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10993 {
10994 rtx rtl = *rtlp;
10995
10996 if (GET_CODE (rtl) == UNSPEC)
10997 {
10998 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10999 we can't express it in the debug info. */
11000 #ifdef ENABLE_CHECKING
11001 /* Don't complain about TLS UNSPECs, those are just too hard to
11002 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11003 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11004 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11005 if (XVECLEN (rtl, 0) == 0
11006 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11007 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11008 inform (current_function_decl
11009 ? DECL_SOURCE_LOCATION (current_function_decl)
11010 : UNKNOWN_LOCATION,
11011 #if NUM_UNSPEC_VALUES > 0
11012 "non-delegitimized UNSPEC %s (%d) found in variable location",
11013 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11014 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11015 XINT (rtl, 1));
11016 #else
11017 "non-delegitimized UNSPEC %d found in variable location",
11018 XINT (rtl, 1));
11019 #endif
11020 #endif
11021 expansion_failed (NULL_TREE, rtl,
11022 "UNSPEC hasn't been delegitimized.\n");
11023 return 1;
11024 }
11025
11026 if (targetm.const_not_ok_for_debug_p (rtl))
11027 {
11028 expansion_failed (NULL_TREE, rtl,
11029 "Expression rejected for debug by the backend.\n");
11030 return 1;
11031 }
11032
11033 if (GET_CODE (rtl) != SYMBOL_REF)
11034 return 0;
11035
11036 if (CONSTANT_POOL_ADDRESS_P (rtl))
11037 {
11038 bool marked;
11039 get_pool_constant_mark (rtl, &marked);
11040 /* If all references to this pool constant were optimized away,
11041 it was not output and thus we can't represent it. */
11042 if (!marked)
11043 {
11044 expansion_failed (NULL_TREE, rtl,
11045 "Constant was removed from constant pool.\n");
11046 return 1;
11047 }
11048 }
11049
11050 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11051 return 1;
11052
11053 /* Avoid references to external symbols in debug info, on several targets
11054 the linker might even refuse to link when linking a shared library,
11055 and in many other cases the relocations for .debug_info/.debug_loc are
11056 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11057 to be defined within the same shared library or executable are fine. */
11058 if (SYMBOL_REF_EXTERNAL_P (rtl))
11059 {
11060 tree decl = SYMBOL_REF_DECL (rtl);
11061
11062 if (decl == NULL || !targetm.binds_local_p (decl))
11063 {
11064 expansion_failed (NULL_TREE, rtl,
11065 "Symbol not defined in current TU.\n");
11066 return 1;
11067 }
11068 }
11069
11070 return 0;
11071 }
11072
11073 /* Return true if constant RTL can be emitted in DW_OP_addr or
11074 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11075 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11076
11077 static bool
11078 const_ok_for_output (rtx rtl)
11079 {
11080 if (GET_CODE (rtl) == SYMBOL_REF)
11081 return const_ok_for_output_1 (&rtl, NULL) == 0;
11082
11083 if (GET_CODE (rtl) == CONST)
11084 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11085
11086 return true;
11087 }
11088
11089 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11090 if possible, NULL otherwise. */
11091
11092 static dw_die_ref
11093 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11094 {
11095 dw_die_ref type_die;
11096 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11097
11098 if (type == NULL)
11099 return NULL;
11100 switch (TREE_CODE (type))
11101 {
11102 case INTEGER_TYPE:
11103 case REAL_TYPE:
11104 break;
11105 default:
11106 return NULL;
11107 }
11108 type_die = lookup_type_die (type);
11109 if (!type_die)
11110 type_die = modified_type_die (type, false, false, comp_unit_die ());
11111 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11112 return NULL;
11113 return type_die;
11114 }
11115
11116 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11117 type matching MODE, or, if MODE is narrower than or as wide as
11118 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11119 possible. */
11120
11121 static dw_loc_descr_ref
11122 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11123 {
11124 enum machine_mode outer_mode = mode;
11125 dw_die_ref type_die;
11126 dw_loc_descr_ref cvt;
11127
11128 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11129 {
11130 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11131 return op;
11132 }
11133 type_die = base_type_for_mode (outer_mode, 1);
11134 if (type_die == NULL)
11135 return NULL;
11136 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11137 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11138 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11139 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11140 add_loc_descr (&op, cvt);
11141 return op;
11142 }
11143
11144 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11145
11146 static dw_loc_descr_ref
11147 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11148 dw_loc_descr_ref op1)
11149 {
11150 dw_loc_descr_ref ret = op0;
11151 add_loc_descr (&ret, op1);
11152 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11153 if (STORE_FLAG_VALUE != 1)
11154 {
11155 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11156 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11157 }
11158 return ret;
11159 }
11160
11161 /* Return location descriptor for signed comparison OP RTL. */
11162
11163 static dw_loc_descr_ref
11164 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11165 enum machine_mode mem_mode)
11166 {
11167 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11168 dw_loc_descr_ref op0, op1;
11169 int shift;
11170
11171 if (op_mode == VOIDmode)
11172 op_mode = GET_MODE (XEXP (rtl, 1));
11173 if (op_mode == VOIDmode)
11174 return NULL;
11175
11176 if (dwarf_strict
11177 && (GET_MODE_CLASS (op_mode) != MODE_INT
11178 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11179 return NULL;
11180
11181 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11182 VAR_INIT_STATUS_INITIALIZED);
11183 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11184 VAR_INIT_STATUS_INITIALIZED);
11185
11186 if (op0 == NULL || op1 == NULL)
11187 return NULL;
11188
11189 if (GET_MODE_CLASS (op_mode) != MODE_INT
11190 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11191 return compare_loc_descriptor (op, op0, op1);
11192
11193 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11194 {
11195 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11196 dw_loc_descr_ref cvt;
11197
11198 if (type_die == NULL)
11199 return NULL;
11200 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11201 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11202 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11203 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11204 add_loc_descr (&op0, cvt);
11205 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11206 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11207 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11208 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11209 add_loc_descr (&op1, cvt);
11210 return compare_loc_descriptor (op, op0, op1);
11211 }
11212
11213 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11214 /* For eq/ne, if the operands are known to be zero-extended,
11215 there is no need to do the fancy shifting up. */
11216 if (op == DW_OP_eq || op == DW_OP_ne)
11217 {
11218 dw_loc_descr_ref last0, last1;
11219 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11220 ;
11221 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11222 ;
11223 /* deref_size zero extends, and for constants we can check
11224 whether they are zero extended or not. */
11225 if (((last0->dw_loc_opc == DW_OP_deref_size
11226 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11227 || (CONST_INT_P (XEXP (rtl, 0))
11228 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11229 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11230 && ((last1->dw_loc_opc == DW_OP_deref_size
11231 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11232 || (CONST_INT_P (XEXP (rtl, 1))
11233 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11234 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11235 return compare_loc_descriptor (op, op0, op1);
11236
11237 /* EQ/NE comparison against constant in narrower type than
11238 DWARF2_ADDR_SIZE can be performed either as
11239 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11240 DW_OP_{eq,ne}
11241 or
11242 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11243 DW_OP_{eq,ne}. Pick whatever is shorter. */
11244 if (CONST_INT_P (XEXP (rtl, 1))
11245 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11246 && (size_of_int_loc_descriptor (shift) + 1
11247 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11248 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11249 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11250 & GET_MODE_MASK (op_mode))))
11251 {
11252 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11253 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11254 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11255 & GET_MODE_MASK (op_mode));
11256 return compare_loc_descriptor (op, op0, op1);
11257 }
11258 }
11259 add_loc_descr (&op0, int_loc_descriptor (shift));
11260 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11261 if (CONST_INT_P (XEXP (rtl, 1)))
11262 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11263 else
11264 {
11265 add_loc_descr (&op1, int_loc_descriptor (shift));
11266 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11267 }
11268 return compare_loc_descriptor (op, op0, op1);
11269 }
11270
11271 /* Return location descriptor for unsigned comparison OP RTL. */
11272
11273 static dw_loc_descr_ref
11274 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11275 enum machine_mode mem_mode)
11276 {
11277 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11278 dw_loc_descr_ref op0, op1;
11279
11280 if (op_mode == VOIDmode)
11281 op_mode = GET_MODE (XEXP (rtl, 1));
11282 if (op_mode == VOIDmode)
11283 return NULL;
11284 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11285 return NULL;
11286
11287 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11288 return NULL;
11289
11290 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11291 VAR_INIT_STATUS_INITIALIZED);
11292 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11293 VAR_INIT_STATUS_INITIALIZED);
11294
11295 if (op0 == NULL || op1 == NULL)
11296 return NULL;
11297
11298 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11299 {
11300 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11301 dw_loc_descr_ref last0, last1;
11302 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11303 ;
11304 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11305 ;
11306 if (CONST_INT_P (XEXP (rtl, 0)))
11307 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11308 /* deref_size zero extends, so no need to mask it again. */
11309 else if (last0->dw_loc_opc != DW_OP_deref_size
11310 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11311 {
11312 add_loc_descr (&op0, int_loc_descriptor (mask));
11313 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11314 }
11315 if (CONST_INT_P (XEXP (rtl, 1)))
11316 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11317 /* deref_size zero extends, so no need to mask it again. */
11318 else if (last1->dw_loc_opc != DW_OP_deref_size
11319 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11320 {
11321 add_loc_descr (&op1, int_loc_descriptor (mask));
11322 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11323 }
11324 }
11325 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11326 {
11327 HOST_WIDE_INT bias = 1;
11328 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11329 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11330 if (CONST_INT_P (XEXP (rtl, 1)))
11331 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11332 + INTVAL (XEXP (rtl, 1)));
11333 else
11334 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11335 bias, 0));
11336 }
11337 return compare_loc_descriptor (op, op0, op1);
11338 }
11339
11340 /* Return location descriptor for {U,S}{MIN,MAX}. */
11341
11342 static dw_loc_descr_ref
11343 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11344 enum machine_mode mem_mode)
11345 {
11346 enum dwarf_location_atom op;
11347 dw_loc_descr_ref op0, op1, ret;
11348 dw_loc_descr_ref bra_node, drop_node;
11349
11350 if (dwarf_strict
11351 && (GET_MODE_CLASS (mode) != MODE_INT
11352 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11353 return NULL;
11354
11355 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11356 VAR_INIT_STATUS_INITIALIZED);
11357 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11358 VAR_INIT_STATUS_INITIALIZED);
11359
11360 if (op0 == NULL || op1 == NULL)
11361 return NULL;
11362
11363 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11364 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11365 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11366 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11367 {
11368 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11369 {
11370 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11371 add_loc_descr (&op0, int_loc_descriptor (mask));
11372 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11373 add_loc_descr (&op1, int_loc_descriptor (mask));
11374 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11375 }
11376 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11377 {
11378 HOST_WIDE_INT bias = 1;
11379 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11380 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11381 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11382 }
11383 }
11384 else if (GET_MODE_CLASS (mode) == MODE_INT
11385 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11386 {
11387 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11388 add_loc_descr (&op0, int_loc_descriptor (shift));
11389 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11390 add_loc_descr (&op1, int_loc_descriptor (shift));
11391 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11392 }
11393 else if (GET_MODE_CLASS (mode) == MODE_INT
11394 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11395 {
11396 dw_die_ref type_die = base_type_for_mode (mode, 0);
11397 dw_loc_descr_ref cvt;
11398 if (type_die == NULL)
11399 return NULL;
11400 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11401 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11402 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11403 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11404 add_loc_descr (&op0, cvt);
11405 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11406 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11407 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11408 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11409 add_loc_descr (&op1, cvt);
11410 }
11411
11412 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11413 op = DW_OP_lt;
11414 else
11415 op = DW_OP_gt;
11416 ret = op0;
11417 add_loc_descr (&ret, op1);
11418 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11419 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11420 add_loc_descr (&ret, bra_node);
11421 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11422 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11423 add_loc_descr (&ret, drop_node);
11424 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11425 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11426 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11427 && GET_MODE_CLASS (mode) == MODE_INT
11428 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11429 ret = convert_descriptor_to_mode (mode, ret);
11430 return ret;
11431 }
11432
11433 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11434 but after converting arguments to type_die, afterwards
11435 convert back to unsigned. */
11436
11437 static dw_loc_descr_ref
11438 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11439 enum machine_mode mode, enum machine_mode mem_mode)
11440 {
11441 dw_loc_descr_ref cvt, op0, op1;
11442
11443 if (type_die == NULL)
11444 return NULL;
11445 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11446 VAR_INIT_STATUS_INITIALIZED);
11447 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11448 VAR_INIT_STATUS_INITIALIZED);
11449 if (op0 == NULL || op1 == NULL)
11450 return NULL;
11451 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11452 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11453 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11454 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11455 add_loc_descr (&op0, cvt);
11456 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11457 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11458 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11459 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11460 add_loc_descr (&op1, cvt);
11461 add_loc_descr (&op0, op1);
11462 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11463 return convert_descriptor_to_mode (mode, op0);
11464 }
11465
11466 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11467 const0 is DW_OP_lit0 or corresponding typed constant,
11468 const1 is DW_OP_lit1 or corresponding typed constant
11469 and constMSB is constant with just the MSB bit set
11470 for the mode):
11471 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11472 L1: const0 DW_OP_swap
11473 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11474 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11475 L3: DW_OP_drop
11476 L4: DW_OP_nop
11477
11478 CTZ is similar:
11479 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11480 L1: const0 DW_OP_swap
11481 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11482 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11483 L3: DW_OP_drop
11484 L4: DW_OP_nop
11485
11486 FFS is similar:
11487 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11488 L1: const1 DW_OP_swap
11489 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11490 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11491 L3: DW_OP_drop
11492 L4: DW_OP_nop */
11493
11494 static dw_loc_descr_ref
11495 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11496 enum machine_mode mem_mode)
11497 {
11498 dw_loc_descr_ref op0, ret, tmp;
11499 HOST_WIDE_INT valv;
11500 dw_loc_descr_ref l1jump, l1label;
11501 dw_loc_descr_ref l2jump, l2label;
11502 dw_loc_descr_ref l3jump, l3label;
11503 dw_loc_descr_ref l4jump, l4label;
11504 rtx msb;
11505
11506 if (GET_MODE_CLASS (mode) != MODE_INT
11507 || GET_MODE (XEXP (rtl, 0)) != mode
11508 || (GET_CODE (rtl) == CLZ
11509 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11510 return NULL;
11511
11512 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11513 VAR_INIT_STATUS_INITIALIZED);
11514 if (op0 == NULL)
11515 return NULL;
11516 ret = op0;
11517 if (GET_CODE (rtl) == CLZ)
11518 {
11519 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11520 valv = GET_MODE_BITSIZE (mode);
11521 }
11522 else if (GET_CODE (rtl) == FFS)
11523 valv = 0;
11524 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11525 valv = GET_MODE_BITSIZE (mode);
11526 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11527 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11528 add_loc_descr (&ret, l1jump);
11529 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11530 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11531 VAR_INIT_STATUS_INITIALIZED);
11532 if (tmp == NULL)
11533 return NULL;
11534 add_loc_descr (&ret, tmp);
11535 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11536 add_loc_descr (&ret, l4jump);
11537 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11538 ? const1_rtx : const0_rtx,
11539 mode, mem_mode,
11540 VAR_INIT_STATUS_INITIALIZED);
11541 if (l1label == NULL)
11542 return NULL;
11543 add_loc_descr (&ret, l1label);
11544 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11545 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11546 add_loc_descr (&ret, l2label);
11547 if (GET_CODE (rtl) != CLZ)
11548 msb = const1_rtx;
11549 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11550 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11551 << (GET_MODE_BITSIZE (mode) - 1));
11552 else
11553 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11554 << (GET_MODE_BITSIZE (mode)
11555 - HOST_BITS_PER_WIDE_INT - 1), mode);
11556 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11557 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11558 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11559 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11560 else
11561 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11562 VAR_INIT_STATUS_INITIALIZED);
11563 if (tmp == NULL)
11564 return NULL;
11565 add_loc_descr (&ret, tmp);
11566 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11567 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11568 add_loc_descr (&ret, l3jump);
11569 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11570 VAR_INIT_STATUS_INITIALIZED);
11571 if (tmp == NULL)
11572 return NULL;
11573 add_loc_descr (&ret, tmp);
11574 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11575 ? DW_OP_shl : DW_OP_shr, 0, 0));
11576 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11577 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11578 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11579 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11580 add_loc_descr (&ret, l2jump);
11581 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11582 add_loc_descr (&ret, l3label);
11583 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11584 add_loc_descr (&ret, l4label);
11585 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11586 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11587 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11588 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11589 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11590 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11591 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11592 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11593 return ret;
11594 }
11595
11596 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11597 const1 is DW_OP_lit1 or corresponding typed constant):
11598 const0 DW_OP_swap
11599 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11600 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11601 L2: DW_OP_drop
11602
11603 PARITY is similar:
11604 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11605 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11606 L2: DW_OP_drop */
11607
11608 static dw_loc_descr_ref
11609 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11610 enum machine_mode mem_mode)
11611 {
11612 dw_loc_descr_ref op0, ret, tmp;
11613 dw_loc_descr_ref l1jump, l1label;
11614 dw_loc_descr_ref l2jump, l2label;
11615
11616 if (GET_MODE_CLASS (mode) != MODE_INT
11617 || GET_MODE (XEXP (rtl, 0)) != mode)
11618 return NULL;
11619
11620 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11621 VAR_INIT_STATUS_INITIALIZED);
11622 if (op0 == NULL)
11623 return NULL;
11624 ret = op0;
11625 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11626 VAR_INIT_STATUS_INITIALIZED);
11627 if (tmp == NULL)
11628 return NULL;
11629 add_loc_descr (&ret, tmp);
11630 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11631 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11632 add_loc_descr (&ret, l1label);
11633 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11634 add_loc_descr (&ret, l2jump);
11635 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11636 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11637 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11638 VAR_INIT_STATUS_INITIALIZED);
11639 if (tmp == NULL)
11640 return NULL;
11641 add_loc_descr (&ret, tmp);
11642 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11643 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11644 ? DW_OP_plus : DW_OP_xor, 0, 0));
11645 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11646 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11647 VAR_INIT_STATUS_INITIALIZED);
11648 add_loc_descr (&ret, tmp);
11649 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11650 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11651 add_loc_descr (&ret, l1jump);
11652 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11653 add_loc_descr (&ret, l2label);
11654 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11655 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11656 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11657 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11658 return ret;
11659 }
11660
11661 /* BSWAP (constS is initial shift count, either 56 or 24):
11662 constS const0
11663 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11664 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11665 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11666 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11667 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11668
11669 static dw_loc_descr_ref
11670 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11671 enum machine_mode mem_mode)
11672 {
11673 dw_loc_descr_ref op0, ret, tmp;
11674 dw_loc_descr_ref l1jump, l1label;
11675 dw_loc_descr_ref l2jump, l2label;
11676
11677 if (GET_MODE_CLASS (mode) != MODE_INT
11678 || BITS_PER_UNIT != 8
11679 || (GET_MODE_BITSIZE (mode) != 32
11680 && GET_MODE_BITSIZE (mode) != 64))
11681 return NULL;
11682
11683 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11684 VAR_INIT_STATUS_INITIALIZED);
11685 if (op0 == NULL)
11686 return NULL;
11687
11688 ret = op0;
11689 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11690 mode, mem_mode,
11691 VAR_INIT_STATUS_INITIALIZED);
11692 if (tmp == NULL)
11693 return NULL;
11694 add_loc_descr (&ret, tmp);
11695 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11696 VAR_INIT_STATUS_INITIALIZED);
11697 if (tmp == NULL)
11698 return NULL;
11699 add_loc_descr (&ret, tmp);
11700 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11701 add_loc_descr (&ret, l1label);
11702 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11703 mode, mem_mode,
11704 VAR_INIT_STATUS_INITIALIZED);
11705 add_loc_descr (&ret, tmp);
11706 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11707 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11708 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11709 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11710 VAR_INIT_STATUS_INITIALIZED);
11711 if (tmp == NULL)
11712 return NULL;
11713 add_loc_descr (&ret, tmp);
11714 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11715 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11716 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11717 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11718 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11719 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11720 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11721 VAR_INIT_STATUS_INITIALIZED);
11722 add_loc_descr (&ret, tmp);
11723 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11724 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11725 add_loc_descr (&ret, l2jump);
11726 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11727 VAR_INIT_STATUS_INITIALIZED);
11728 add_loc_descr (&ret, tmp);
11729 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11730 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11731 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11732 add_loc_descr (&ret, l1jump);
11733 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11734 add_loc_descr (&ret, l2label);
11735 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11736 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11737 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11738 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11739 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11740 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11741 return ret;
11742 }
11743
11744 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11745 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11746 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11747 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11748
11749 ROTATERT is similar:
11750 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11751 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11752 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11753
11754 static dw_loc_descr_ref
11755 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11756 enum machine_mode mem_mode)
11757 {
11758 rtx rtlop1 = XEXP (rtl, 1);
11759 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11760 int i;
11761
11762 if (GET_MODE_CLASS (mode) != MODE_INT)
11763 return NULL;
11764
11765 if (GET_MODE (rtlop1) != VOIDmode
11766 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11767 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11768 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11769 VAR_INIT_STATUS_INITIALIZED);
11770 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11771 VAR_INIT_STATUS_INITIALIZED);
11772 if (op0 == NULL || op1 == NULL)
11773 return NULL;
11774 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11775 for (i = 0; i < 2; i++)
11776 {
11777 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11778 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11779 mode, mem_mode,
11780 VAR_INIT_STATUS_INITIALIZED);
11781 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11782 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11783 ? DW_OP_const4u
11784 : HOST_BITS_PER_WIDE_INT == 64
11785 ? DW_OP_const8u : DW_OP_constu,
11786 GET_MODE_MASK (mode), 0);
11787 else
11788 mask[i] = NULL;
11789 if (mask[i] == NULL)
11790 return NULL;
11791 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11792 }
11793 ret = op0;
11794 add_loc_descr (&ret, op1);
11795 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11796 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11797 if (GET_CODE (rtl) == ROTATERT)
11798 {
11799 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11800 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11801 GET_MODE_BITSIZE (mode), 0));
11802 }
11803 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11804 if (mask[0] != NULL)
11805 add_loc_descr (&ret, mask[0]);
11806 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11807 if (mask[1] != NULL)
11808 {
11809 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11810 add_loc_descr (&ret, mask[1]);
11811 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11812 }
11813 if (GET_CODE (rtl) == ROTATE)
11814 {
11815 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11816 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11817 GET_MODE_BITSIZE (mode), 0));
11818 }
11819 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11820 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11821 return ret;
11822 }
11823
11824 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11825 for DEBUG_PARAMETER_REF RTL. */
11826
11827 static dw_loc_descr_ref
11828 parameter_ref_descriptor (rtx rtl)
11829 {
11830 dw_loc_descr_ref ret;
11831 dw_die_ref ref;
11832
11833 if (dwarf_strict)
11834 return NULL;
11835 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11836 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11837 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11838 if (ref)
11839 {
11840 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11841 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11842 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11843 }
11844 else
11845 {
11846 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11847 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11848 }
11849 return ret;
11850 }
11851
11852 /* The following routine converts the RTL for a variable or parameter
11853 (resident in memory) into an equivalent Dwarf representation of a
11854 mechanism for getting the address of that same variable onto the top of a
11855 hypothetical "address evaluation" stack.
11856
11857 When creating memory location descriptors, we are effectively transforming
11858 the RTL for a memory-resident object into its Dwarf postfix expression
11859 equivalent. This routine recursively descends an RTL tree, turning
11860 it into Dwarf postfix code as it goes.
11861
11862 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11863
11864 MEM_MODE is the mode of the memory reference, needed to handle some
11865 autoincrement addressing modes.
11866
11867 Return 0 if we can't represent the location. */
11868
11869 dw_loc_descr_ref
11870 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11871 enum machine_mode mem_mode,
11872 enum var_init_status initialized)
11873 {
11874 dw_loc_descr_ref mem_loc_result = NULL;
11875 enum dwarf_location_atom op;
11876 dw_loc_descr_ref op0, op1;
11877 rtx inner = NULL_RTX;
11878
11879 if (mode == VOIDmode)
11880 mode = GET_MODE (rtl);
11881
11882 /* Note that for a dynamically sized array, the location we will generate a
11883 description of here will be the lowest numbered location which is
11884 actually within the array. That's *not* necessarily the same as the
11885 zeroth element of the array. */
11886
11887 rtl = targetm.delegitimize_address (rtl);
11888
11889 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11890 return NULL;
11891
11892 switch (GET_CODE (rtl))
11893 {
11894 case POST_INC:
11895 case POST_DEC:
11896 case POST_MODIFY:
11897 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11898
11899 case SUBREG:
11900 /* The case of a subreg may arise when we have a local (register)
11901 variable or a formal (register) parameter which doesn't quite fill
11902 up an entire register. For now, just assume that it is
11903 legitimate to make the Dwarf info refer to the whole register which
11904 contains the given subreg. */
11905 if (!subreg_lowpart_p (rtl))
11906 break;
11907 inner = SUBREG_REG (rtl);
11908 case TRUNCATE:
11909 if (inner == NULL_RTX)
11910 inner = XEXP (rtl, 0);
11911 if (GET_MODE_CLASS (mode) == MODE_INT
11912 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
11913 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11914 #ifdef POINTERS_EXTEND_UNSIGNED
11915 || (mode == Pmode && mem_mode != VOIDmode)
11916 #endif
11917 )
11918 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
11919 {
11920 mem_loc_result = mem_loc_descriptor (inner,
11921 GET_MODE (inner),
11922 mem_mode, initialized);
11923 break;
11924 }
11925 if (dwarf_strict)
11926 break;
11927 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
11928 break;
11929 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
11930 && (GET_MODE_CLASS (mode) != MODE_INT
11931 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
11932 break;
11933 else
11934 {
11935 dw_die_ref type_die;
11936 dw_loc_descr_ref cvt;
11937
11938 mem_loc_result = mem_loc_descriptor (inner,
11939 GET_MODE (inner),
11940 mem_mode, initialized);
11941 if (mem_loc_result == NULL)
11942 break;
11943 type_die = base_type_for_mode (mode,
11944 GET_MODE_CLASS (mode) == MODE_INT);
11945 if (type_die == NULL)
11946 {
11947 mem_loc_result = NULL;
11948 break;
11949 }
11950 if (GET_MODE_SIZE (mode)
11951 != GET_MODE_SIZE (GET_MODE (inner)))
11952 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11953 else
11954 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11955 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11956 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11957 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11958 add_loc_descr (&mem_loc_result, cvt);
11959 }
11960 break;
11961
11962 case REG:
11963 if (GET_MODE_CLASS (mode) != MODE_INT
11964 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11965 && rtl != arg_pointer_rtx
11966 && rtl != frame_pointer_rtx
11967 #ifdef POINTERS_EXTEND_UNSIGNED
11968 && (mode != Pmode || mem_mode == VOIDmode)
11969 #endif
11970 ))
11971 {
11972 dw_die_ref type_die;
11973 unsigned int dbx_regnum;
11974
11975 if (dwarf_strict)
11976 break;
11977 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11978 break;
11979 type_die = base_type_for_mode (mode,
11980 GET_MODE_CLASS (mode) == MODE_INT);
11981 if (type_die == NULL)
11982 break;
11983
11984 dbx_regnum = dbx_reg_number (rtl);
11985 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11986 break;
11987 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11988 dbx_regnum, 0);
11989 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11990 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11991 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11992 break;
11993 }
11994 /* Whenever a register number forms a part of the description of the
11995 method for calculating the (dynamic) address of a memory resident
11996 object, DWARF rules require the register number be referred to as
11997 a "base register". This distinction is not based in any way upon
11998 what category of register the hardware believes the given register
11999 belongs to. This is strictly DWARF terminology we're dealing with
12000 here. Note that in cases where the location of a memory-resident
12001 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12002 OP_CONST (0)) the actual DWARF location descriptor that we generate
12003 may just be OP_BASEREG (basereg). This may look deceptively like
12004 the object in question was allocated to a register (rather than in
12005 memory) so DWARF consumers need to be aware of the subtle
12006 distinction between OP_REG and OP_BASEREG. */
12007 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12008 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12009 else if (stack_realign_drap
12010 && crtl->drap_reg
12011 && crtl->args.internal_arg_pointer == rtl
12012 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12013 {
12014 /* If RTL is internal_arg_pointer, which has been optimized
12015 out, use DRAP instead. */
12016 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12017 VAR_INIT_STATUS_INITIALIZED);
12018 }
12019 break;
12020
12021 case SIGN_EXTEND:
12022 case ZERO_EXTEND:
12023 if (GET_MODE_CLASS (mode) != MODE_INT)
12024 break;
12025 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12026 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12027 if (op0 == 0)
12028 break;
12029 else if (GET_CODE (rtl) == ZERO_EXTEND
12030 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12031 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12032 < HOST_BITS_PER_WIDE_INT
12033 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12034 to expand zero extend as two shifts instead of
12035 masking. */
12036 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12037 {
12038 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12039 mem_loc_result = op0;
12040 add_loc_descr (&mem_loc_result,
12041 int_loc_descriptor (GET_MODE_MASK (imode)));
12042 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12043 }
12044 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12045 {
12046 int shift = DWARF2_ADDR_SIZE
12047 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12048 shift *= BITS_PER_UNIT;
12049 if (GET_CODE (rtl) == SIGN_EXTEND)
12050 op = DW_OP_shra;
12051 else
12052 op = DW_OP_shr;
12053 mem_loc_result = op0;
12054 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12055 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12056 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12057 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12058 }
12059 else if (!dwarf_strict)
12060 {
12061 dw_die_ref type_die1, type_die2;
12062 dw_loc_descr_ref cvt;
12063
12064 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12065 GET_CODE (rtl) == ZERO_EXTEND);
12066 if (type_die1 == NULL)
12067 break;
12068 type_die2 = base_type_for_mode (mode, 1);
12069 if (type_die2 == NULL)
12070 break;
12071 mem_loc_result = op0;
12072 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12073 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12074 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12075 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12076 add_loc_descr (&mem_loc_result, cvt);
12077 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12078 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12079 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12080 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12081 add_loc_descr (&mem_loc_result, cvt);
12082 }
12083 break;
12084
12085 case MEM:
12086 {
12087 rtx new_rtl = avoid_constant_pool_reference (rtl);
12088 if (new_rtl != rtl)
12089 {
12090 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12091 initialized);
12092 if (mem_loc_result != NULL)
12093 return mem_loc_result;
12094 }
12095 }
12096 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12097 get_address_mode (rtl), mode,
12098 VAR_INIT_STATUS_INITIALIZED);
12099 if (mem_loc_result == NULL)
12100 mem_loc_result = tls_mem_loc_descriptor (rtl);
12101 if (mem_loc_result != NULL)
12102 {
12103 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12104 || GET_MODE_CLASS (mode) != MODE_INT)
12105 {
12106 dw_die_ref type_die;
12107 dw_loc_descr_ref deref;
12108
12109 if (dwarf_strict)
12110 return NULL;
12111 type_die
12112 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12113 if (type_die == NULL)
12114 return NULL;
12115 deref = new_loc_descr (DW_OP_GNU_deref_type,
12116 GET_MODE_SIZE (mode), 0);
12117 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12118 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12119 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12120 add_loc_descr (&mem_loc_result, deref);
12121 }
12122 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12123 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12124 else
12125 add_loc_descr (&mem_loc_result,
12126 new_loc_descr (DW_OP_deref_size,
12127 GET_MODE_SIZE (mode), 0));
12128 }
12129 break;
12130
12131 case LO_SUM:
12132 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12133
12134 case LABEL_REF:
12135 /* Some ports can transform a symbol ref into a label ref, because
12136 the symbol ref is too far away and has to be dumped into a constant
12137 pool. */
12138 case CONST:
12139 case SYMBOL_REF:
12140 if (GET_MODE_CLASS (mode) != MODE_INT
12141 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12142 #ifdef POINTERS_EXTEND_UNSIGNED
12143 && (mode != Pmode || mem_mode == VOIDmode)
12144 #endif
12145 ))
12146 break;
12147 if (GET_CODE (rtl) == SYMBOL_REF
12148 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12149 {
12150 dw_loc_descr_ref temp;
12151
12152 /* If this is not defined, we have no way to emit the data. */
12153 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12154 break;
12155
12156 temp = new_addr_loc_descr (rtl, dtprel_true);
12157
12158 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12159 add_loc_descr (&mem_loc_result, temp);
12160
12161 break;
12162 }
12163
12164 if (!const_ok_for_output (rtl))
12165 break;
12166
12167 symref:
12168 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12169 vec_safe_push (used_rtx_array, rtl);
12170 break;
12171
12172 case CONCAT:
12173 case CONCATN:
12174 case VAR_LOCATION:
12175 case DEBUG_IMPLICIT_PTR:
12176 expansion_failed (NULL_TREE, rtl,
12177 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12178 return 0;
12179
12180 case ENTRY_VALUE:
12181 if (dwarf_strict)
12182 return NULL;
12183 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12184 {
12185 if (GET_MODE_CLASS (mode) != MODE_INT
12186 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12187 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12188 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12189 else
12190 {
12191 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12192 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12193 return NULL;
12194 op0 = one_reg_loc_descriptor (dbx_regnum,
12195 VAR_INIT_STATUS_INITIALIZED);
12196 }
12197 }
12198 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12199 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12200 {
12201 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12202 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12203 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12204 return NULL;
12205 }
12206 else
12207 gcc_unreachable ();
12208 if (op0 == NULL)
12209 return NULL;
12210 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12211 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12212 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12213 break;
12214
12215 case DEBUG_PARAMETER_REF:
12216 mem_loc_result = parameter_ref_descriptor (rtl);
12217 break;
12218
12219 case PRE_MODIFY:
12220 /* Extract the PLUS expression nested inside and fall into
12221 PLUS code below. */
12222 rtl = XEXP (rtl, 1);
12223 goto plus;
12224
12225 case PRE_INC:
12226 case PRE_DEC:
12227 /* Turn these into a PLUS expression and fall into the PLUS code
12228 below. */
12229 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12230 GEN_INT (GET_CODE (rtl) == PRE_INC
12231 ? GET_MODE_UNIT_SIZE (mem_mode)
12232 : -GET_MODE_UNIT_SIZE (mem_mode)));
12233
12234 /* ... fall through ... */
12235
12236 case PLUS:
12237 plus:
12238 if (is_based_loc (rtl)
12239 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12240 || XEXP (rtl, 0) == arg_pointer_rtx
12241 || XEXP (rtl, 0) == frame_pointer_rtx)
12242 && GET_MODE_CLASS (mode) == MODE_INT)
12243 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12244 INTVAL (XEXP (rtl, 1)),
12245 VAR_INIT_STATUS_INITIALIZED);
12246 else
12247 {
12248 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12249 VAR_INIT_STATUS_INITIALIZED);
12250 if (mem_loc_result == 0)
12251 break;
12252
12253 if (CONST_INT_P (XEXP (rtl, 1))
12254 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12255 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12256 else
12257 {
12258 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12259 VAR_INIT_STATUS_INITIALIZED);
12260 if (op1 == 0)
12261 break;
12262 add_loc_descr (&mem_loc_result, op1);
12263 add_loc_descr (&mem_loc_result,
12264 new_loc_descr (DW_OP_plus, 0, 0));
12265 }
12266 }
12267 break;
12268
12269 /* If a pseudo-reg is optimized away, it is possible for it to
12270 be replaced with a MEM containing a multiply or shift. */
12271 case MINUS:
12272 op = DW_OP_minus;
12273 goto do_binop;
12274
12275 case MULT:
12276 op = DW_OP_mul;
12277 goto do_binop;
12278
12279 case DIV:
12280 if (!dwarf_strict
12281 && GET_MODE_CLASS (mode) == MODE_INT
12282 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12283 {
12284 mem_loc_result = typed_binop (DW_OP_div, rtl,
12285 base_type_for_mode (mode, 0),
12286 mode, mem_mode);
12287 break;
12288 }
12289 op = DW_OP_div;
12290 goto do_binop;
12291
12292 case UMOD:
12293 op = DW_OP_mod;
12294 goto do_binop;
12295
12296 case ASHIFT:
12297 op = DW_OP_shl;
12298 goto do_shift;
12299
12300 case ASHIFTRT:
12301 op = DW_OP_shra;
12302 goto do_shift;
12303
12304 case LSHIFTRT:
12305 op = DW_OP_shr;
12306 goto do_shift;
12307
12308 do_shift:
12309 if (GET_MODE_CLASS (mode) != MODE_INT)
12310 break;
12311 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12312 VAR_INIT_STATUS_INITIALIZED);
12313 {
12314 rtx rtlop1 = XEXP (rtl, 1);
12315 if (GET_MODE (rtlop1) != VOIDmode
12316 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12317 < GET_MODE_BITSIZE (mode))
12318 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12319 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12320 VAR_INIT_STATUS_INITIALIZED);
12321 }
12322
12323 if (op0 == 0 || op1 == 0)
12324 break;
12325
12326 mem_loc_result = op0;
12327 add_loc_descr (&mem_loc_result, op1);
12328 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12329 break;
12330
12331 case AND:
12332 op = DW_OP_and;
12333 goto do_binop;
12334
12335 case IOR:
12336 op = DW_OP_or;
12337 goto do_binop;
12338
12339 case XOR:
12340 op = DW_OP_xor;
12341 goto do_binop;
12342
12343 do_binop:
12344 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12345 VAR_INIT_STATUS_INITIALIZED);
12346 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12347 VAR_INIT_STATUS_INITIALIZED);
12348
12349 if (op0 == 0 || op1 == 0)
12350 break;
12351
12352 mem_loc_result = op0;
12353 add_loc_descr (&mem_loc_result, op1);
12354 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12355 break;
12356
12357 case MOD:
12358 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12359 {
12360 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12361 base_type_for_mode (mode, 0),
12362 mode, mem_mode);
12363 break;
12364 }
12365
12366 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12367 VAR_INIT_STATUS_INITIALIZED);
12368 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12369 VAR_INIT_STATUS_INITIALIZED);
12370
12371 if (op0 == 0 || op1 == 0)
12372 break;
12373
12374 mem_loc_result = op0;
12375 add_loc_descr (&mem_loc_result, op1);
12376 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12377 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12378 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12379 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12380 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12381 break;
12382
12383 case UDIV:
12384 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12385 {
12386 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12387 {
12388 op = DW_OP_div;
12389 goto do_binop;
12390 }
12391 mem_loc_result = typed_binop (DW_OP_div, rtl,
12392 base_type_for_mode (mode, 1),
12393 mode, mem_mode);
12394 }
12395 break;
12396
12397 case NOT:
12398 op = DW_OP_not;
12399 goto do_unop;
12400
12401 case ABS:
12402 op = DW_OP_abs;
12403 goto do_unop;
12404
12405 case NEG:
12406 op = DW_OP_neg;
12407 goto do_unop;
12408
12409 do_unop:
12410 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12411 VAR_INIT_STATUS_INITIALIZED);
12412
12413 if (op0 == 0)
12414 break;
12415
12416 mem_loc_result = op0;
12417 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12418 break;
12419
12420 case CONST_INT:
12421 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12422 #ifdef POINTERS_EXTEND_UNSIGNED
12423 || (mode == Pmode
12424 && mem_mode != VOIDmode
12425 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12426 #endif
12427 )
12428 {
12429 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12430 break;
12431 }
12432 if (!dwarf_strict
12433 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12434 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12435 {
12436 dw_die_ref type_die = base_type_for_mode (mode, 1);
12437 enum machine_mode amode;
12438 if (type_die == NULL)
12439 return NULL;
12440 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12441 MODE_INT, 0);
12442 if (INTVAL (rtl) >= 0
12443 && amode != BLKmode
12444 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12445 /* const DW_OP_GNU_convert <XXX> vs.
12446 DW_OP_GNU_const_type <XXX, 1, const>. */
12447 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12448 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12449 {
12450 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12451 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12452 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12453 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12454 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12455 add_loc_descr (&mem_loc_result, op0);
12456 return mem_loc_result;
12457 }
12458 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12459 INTVAL (rtl));
12460 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12461 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12462 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12463 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12464 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12465 else
12466 {
12467 mem_loc_result->dw_loc_oprnd2.val_class
12468 = dw_val_class_const_double;
12469 mem_loc_result->dw_loc_oprnd2.v.val_double
12470 = double_int::from_shwi (INTVAL (rtl));
12471 }
12472 }
12473 break;
12474
12475 case CONST_DOUBLE:
12476 if (!dwarf_strict)
12477 {
12478 dw_die_ref type_die;
12479
12480 /* Note that a CONST_DOUBLE rtx could represent either an integer
12481 or a floating-point constant. A CONST_DOUBLE is used whenever
12482 the constant requires more than one word in order to be
12483 adequately represented. We output CONST_DOUBLEs as blocks. */
12484 if (mode == VOIDmode
12485 || (GET_MODE (rtl) == VOIDmode
12486 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12487 break;
12488 type_die = base_type_for_mode (mode,
12489 GET_MODE_CLASS (mode) == MODE_INT);
12490 if (type_die == NULL)
12491 return NULL;
12492 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12493 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12494 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12495 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12496 if (SCALAR_FLOAT_MODE_P (mode))
12497 {
12498 unsigned int length = GET_MODE_SIZE (mode);
12499 unsigned char *array
12500 = (unsigned char*) ggc_alloc_atomic (length);
12501
12502 insert_float (rtl, array);
12503 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12504 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12505 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12506 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12507 }
12508 else
12509 {
12510 mem_loc_result->dw_loc_oprnd2.val_class
12511 = dw_val_class_const_double;
12512 mem_loc_result->dw_loc_oprnd2.v.val_double
12513 = rtx_to_double_int (rtl);
12514 }
12515 }
12516 break;
12517
12518 case EQ:
12519 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12520 break;
12521
12522 case GE:
12523 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12524 break;
12525
12526 case GT:
12527 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12528 break;
12529
12530 case LE:
12531 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12532 break;
12533
12534 case LT:
12535 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12536 break;
12537
12538 case NE:
12539 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12540 break;
12541
12542 case GEU:
12543 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12544 break;
12545
12546 case GTU:
12547 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12548 break;
12549
12550 case LEU:
12551 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12552 break;
12553
12554 case LTU:
12555 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12556 break;
12557
12558 case UMIN:
12559 case UMAX:
12560 if (GET_MODE_CLASS (mode) != MODE_INT)
12561 break;
12562 /* FALLTHRU */
12563 case SMIN:
12564 case SMAX:
12565 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12566 break;
12567
12568 case ZERO_EXTRACT:
12569 case SIGN_EXTRACT:
12570 if (CONST_INT_P (XEXP (rtl, 1))
12571 && CONST_INT_P (XEXP (rtl, 2))
12572 && ((unsigned) INTVAL (XEXP (rtl, 1))
12573 + (unsigned) INTVAL (XEXP (rtl, 2))
12574 <= GET_MODE_BITSIZE (mode))
12575 && GET_MODE_CLASS (mode) == MODE_INT
12576 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12577 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12578 {
12579 int shift, size;
12580 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12581 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12582 if (op0 == 0)
12583 break;
12584 if (GET_CODE (rtl) == SIGN_EXTRACT)
12585 op = DW_OP_shra;
12586 else
12587 op = DW_OP_shr;
12588 mem_loc_result = op0;
12589 size = INTVAL (XEXP (rtl, 1));
12590 shift = INTVAL (XEXP (rtl, 2));
12591 if (BITS_BIG_ENDIAN)
12592 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12593 - shift - size;
12594 if (shift + size != (int) DWARF2_ADDR_SIZE)
12595 {
12596 add_loc_descr (&mem_loc_result,
12597 int_loc_descriptor (DWARF2_ADDR_SIZE
12598 - shift - size));
12599 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12600 }
12601 if (size != (int) DWARF2_ADDR_SIZE)
12602 {
12603 add_loc_descr (&mem_loc_result,
12604 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12605 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12606 }
12607 }
12608 break;
12609
12610 case IF_THEN_ELSE:
12611 {
12612 dw_loc_descr_ref op2, bra_node, drop_node;
12613 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12614 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12615 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12616 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12617 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12618 VAR_INIT_STATUS_INITIALIZED);
12619 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12620 VAR_INIT_STATUS_INITIALIZED);
12621 if (op0 == NULL || op1 == NULL || op2 == NULL)
12622 break;
12623
12624 mem_loc_result = op1;
12625 add_loc_descr (&mem_loc_result, op2);
12626 add_loc_descr (&mem_loc_result, op0);
12627 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12628 add_loc_descr (&mem_loc_result, bra_node);
12629 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12630 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12631 add_loc_descr (&mem_loc_result, drop_node);
12632 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12633 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12634 }
12635 break;
12636
12637 case FLOAT_EXTEND:
12638 case FLOAT_TRUNCATE:
12639 case FLOAT:
12640 case UNSIGNED_FLOAT:
12641 case FIX:
12642 case UNSIGNED_FIX:
12643 if (!dwarf_strict)
12644 {
12645 dw_die_ref type_die;
12646 dw_loc_descr_ref cvt;
12647
12648 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12649 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12650 if (op0 == NULL)
12651 break;
12652 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12653 && (GET_CODE (rtl) == FLOAT
12654 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12655 <= DWARF2_ADDR_SIZE))
12656 {
12657 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12658 GET_CODE (rtl) == UNSIGNED_FLOAT);
12659 if (type_die == NULL)
12660 break;
12661 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12662 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12663 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12664 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12665 add_loc_descr (&op0, cvt);
12666 }
12667 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12668 if (type_die == NULL)
12669 break;
12670 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12671 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12672 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12673 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12674 add_loc_descr (&op0, cvt);
12675 if (GET_MODE_CLASS (mode) == MODE_INT
12676 && (GET_CODE (rtl) == FIX
12677 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12678 {
12679 op0 = convert_descriptor_to_mode (mode, op0);
12680 if (op0 == NULL)
12681 break;
12682 }
12683 mem_loc_result = op0;
12684 }
12685 break;
12686
12687 case CLZ:
12688 case CTZ:
12689 case FFS:
12690 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12691 break;
12692
12693 case POPCOUNT:
12694 case PARITY:
12695 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12696 break;
12697
12698 case BSWAP:
12699 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12700 break;
12701
12702 case ROTATE:
12703 case ROTATERT:
12704 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12705 break;
12706
12707 case COMPARE:
12708 /* In theory, we could implement the above. */
12709 /* DWARF cannot represent the unsigned compare operations
12710 natively. */
12711 case SS_MULT:
12712 case US_MULT:
12713 case SS_DIV:
12714 case US_DIV:
12715 case SS_PLUS:
12716 case US_PLUS:
12717 case SS_MINUS:
12718 case US_MINUS:
12719 case SS_NEG:
12720 case US_NEG:
12721 case SS_ABS:
12722 case SS_ASHIFT:
12723 case US_ASHIFT:
12724 case SS_TRUNCATE:
12725 case US_TRUNCATE:
12726 case UNORDERED:
12727 case ORDERED:
12728 case UNEQ:
12729 case UNGE:
12730 case UNGT:
12731 case UNLE:
12732 case UNLT:
12733 case LTGT:
12734 case FRACT_CONVERT:
12735 case UNSIGNED_FRACT_CONVERT:
12736 case SAT_FRACT:
12737 case UNSIGNED_SAT_FRACT:
12738 case SQRT:
12739 case ASM_OPERANDS:
12740 case VEC_MERGE:
12741 case VEC_SELECT:
12742 case VEC_CONCAT:
12743 case VEC_DUPLICATE:
12744 case UNSPEC:
12745 case HIGH:
12746 case FMA:
12747 case STRICT_LOW_PART:
12748 case CONST_VECTOR:
12749 case CONST_FIXED:
12750 case CLRSB:
12751 case CLOBBER:
12752 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12753 can't express it in the debug info. This can happen e.g. with some
12754 TLS UNSPECs. */
12755 break;
12756
12757 case CONST_STRING:
12758 resolve_one_addr (&rtl, NULL);
12759 goto symref;
12760
12761 default:
12762 #ifdef ENABLE_CHECKING
12763 print_rtl (stderr, rtl);
12764 gcc_unreachable ();
12765 #else
12766 break;
12767 #endif
12768 }
12769
12770 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12771 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12772
12773 return mem_loc_result;
12774 }
12775
12776 /* Return a descriptor that describes the concatenation of two locations.
12777 This is typically a complex variable. */
12778
12779 static dw_loc_descr_ref
12780 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12781 {
12782 dw_loc_descr_ref cc_loc_result = NULL;
12783 dw_loc_descr_ref x0_ref
12784 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12785 dw_loc_descr_ref x1_ref
12786 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12787
12788 if (x0_ref == 0 || x1_ref == 0)
12789 return 0;
12790
12791 cc_loc_result = x0_ref;
12792 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12793
12794 add_loc_descr (&cc_loc_result, x1_ref);
12795 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12796
12797 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12798 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12799
12800 return cc_loc_result;
12801 }
12802
12803 /* Return a descriptor that describes the concatenation of N
12804 locations. */
12805
12806 static dw_loc_descr_ref
12807 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12808 {
12809 unsigned int i;
12810 dw_loc_descr_ref cc_loc_result = NULL;
12811 unsigned int n = XVECLEN (concatn, 0);
12812
12813 for (i = 0; i < n; ++i)
12814 {
12815 dw_loc_descr_ref ref;
12816 rtx x = XVECEXP (concatn, 0, i);
12817
12818 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12819 if (ref == NULL)
12820 return NULL;
12821
12822 add_loc_descr (&cc_loc_result, ref);
12823 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12824 }
12825
12826 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12827 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12828
12829 return cc_loc_result;
12830 }
12831
12832 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12833 for DEBUG_IMPLICIT_PTR RTL. */
12834
12835 static dw_loc_descr_ref
12836 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12837 {
12838 dw_loc_descr_ref ret;
12839 dw_die_ref ref;
12840
12841 if (dwarf_strict)
12842 return NULL;
12843 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12844 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12845 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12846 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12847 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12848 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12849 if (ref)
12850 {
12851 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12852 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12853 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12854 }
12855 else
12856 {
12857 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12858 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12859 }
12860 return ret;
12861 }
12862
12863 /* Output a proper Dwarf location descriptor for a variable or parameter
12864 which is either allocated in a register or in a memory location. For a
12865 register, we just generate an OP_REG and the register number. For a
12866 memory location we provide a Dwarf postfix expression describing how to
12867 generate the (dynamic) address of the object onto the address stack.
12868
12869 MODE is mode of the decl if this loc_descriptor is going to be used in
12870 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12871 allowed, VOIDmode otherwise.
12872
12873 If we don't know how to describe it, return 0. */
12874
12875 static dw_loc_descr_ref
12876 loc_descriptor (rtx rtl, enum machine_mode mode,
12877 enum var_init_status initialized)
12878 {
12879 dw_loc_descr_ref loc_result = NULL;
12880
12881 switch (GET_CODE (rtl))
12882 {
12883 case SUBREG:
12884 /* The case of a subreg may arise when we have a local (register)
12885 variable or a formal (register) parameter which doesn't quite fill
12886 up an entire register. For now, just assume that it is
12887 legitimate to make the Dwarf info refer to the whole register which
12888 contains the given subreg. */
12889 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12890 loc_result = loc_descriptor (SUBREG_REG (rtl),
12891 GET_MODE (SUBREG_REG (rtl)), initialized);
12892 else
12893 goto do_default;
12894 break;
12895
12896 case REG:
12897 loc_result = reg_loc_descriptor (rtl, initialized);
12898 break;
12899
12900 case MEM:
12901 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12902 GET_MODE (rtl), initialized);
12903 if (loc_result == NULL)
12904 loc_result = tls_mem_loc_descriptor (rtl);
12905 if (loc_result == NULL)
12906 {
12907 rtx new_rtl = avoid_constant_pool_reference (rtl);
12908 if (new_rtl != rtl)
12909 loc_result = loc_descriptor (new_rtl, mode, initialized);
12910 }
12911 break;
12912
12913 case CONCAT:
12914 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12915 initialized);
12916 break;
12917
12918 case CONCATN:
12919 loc_result = concatn_loc_descriptor (rtl, initialized);
12920 break;
12921
12922 case VAR_LOCATION:
12923 /* Single part. */
12924 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12925 {
12926 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12927 if (GET_CODE (loc) == EXPR_LIST)
12928 loc = XEXP (loc, 0);
12929 loc_result = loc_descriptor (loc, mode, initialized);
12930 break;
12931 }
12932
12933 rtl = XEXP (rtl, 1);
12934 /* FALLTHRU */
12935
12936 case PARALLEL:
12937 {
12938 rtvec par_elems = XVEC (rtl, 0);
12939 int num_elem = GET_NUM_ELEM (par_elems);
12940 enum machine_mode mode;
12941 int i;
12942
12943 /* Create the first one, so we have something to add to. */
12944 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12945 VOIDmode, initialized);
12946 if (loc_result == NULL)
12947 return NULL;
12948 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12949 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12950 for (i = 1; i < num_elem; i++)
12951 {
12952 dw_loc_descr_ref temp;
12953
12954 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12955 VOIDmode, initialized);
12956 if (temp == NULL)
12957 return NULL;
12958 add_loc_descr (&loc_result, temp);
12959 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12960 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12961 }
12962 }
12963 break;
12964
12965 case CONST_INT:
12966 if (mode != VOIDmode && mode != BLKmode)
12967 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12968 INTVAL (rtl));
12969 break;
12970
12971 case CONST_DOUBLE:
12972 if (mode == VOIDmode)
12973 mode = GET_MODE (rtl);
12974
12975 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12976 {
12977 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12978
12979 /* Note that a CONST_DOUBLE rtx could represent either an integer
12980 or a floating-point constant. A CONST_DOUBLE is used whenever
12981 the constant requires more than one word in order to be
12982 adequately represented. We output CONST_DOUBLEs as blocks. */
12983 loc_result = new_loc_descr (DW_OP_implicit_value,
12984 GET_MODE_SIZE (mode), 0);
12985 if (SCALAR_FLOAT_MODE_P (mode))
12986 {
12987 unsigned int length = GET_MODE_SIZE (mode);
12988 unsigned char *array
12989 = (unsigned char*) ggc_alloc_atomic (length);
12990
12991 insert_float (rtl, array);
12992 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12993 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12994 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12995 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12996 }
12997 else
12998 {
12999 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13000 loc_result->dw_loc_oprnd2.v.val_double
13001 = rtx_to_double_int (rtl);
13002 }
13003 }
13004 break;
13005
13006 case CONST_VECTOR:
13007 if (mode == VOIDmode)
13008 mode = GET_MODE (rtl);
13009
13010 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13011 {
13012 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13013 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13014 unsigned char *array = (unsigned char *)
13015 ggc_alloc_atomic (length * elt_size);
13016 unsigned int i;
13017 unsigned char *p;
13018
13019 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13020 switch (GET_MODE_CLASS (mode))
13021 {
13022 case MODE_VECTOR_INT:
13023 for (i = 0, p = array; i < length; i++, p += elt_size)
13024 {
13025 rtx elt = CONST_VECTOR_ELT (rtl, i);
13026 double_int val = rtx_to_double_int (elt);
13027
13028 if (elt_size <= sizeof (HOST_WIDE_INT))
13029 insert_int (val.to_shwi (), elt_size, p);
13030 else
13031 {
13032 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13033 insert_double (val, p);
13034 }
13035 }
13036 break;
13037
13038 case MODE_VECTOR_FLOAT:
13039 for (i = 0, p = array; i < length; i++, p += elt_size)
13040 {
13041 rtx elt = CONST_VECTOR_ELT (rtl, i);
13042 insert_float (elt, p);
13043 }
13044 break;
13045
13046 default:
13047 gcc_unreachable ();
13048 }
13049
13050 loc_result = new_loc_descr (DW_OP_implicit_value,
13051 length * elt_size, 0);
13052 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13053 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13054 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13055 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13056 }
13057 break;
13058
13059 case CONST:
13060 if (mode == VOIDmode
13061 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13062 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13063 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13064 {
13065 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13066 break;
13067 }
13068 /* FALLTHROUGH */
13069 case SYMBOL_REF:
13070 if (!const_ok_for_output (rtl))
13071 break;
13072 case LABEL_REF:
13073 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13074 && (dwarf_version >= 4 || !dwarf_strict))
13075 {
13076 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13077 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13078 vec_safe_push (used_rtx_array, rtl);
13079 }
13080 break;
13081
13082 case DEBUG_IMPLICIT_PTR:
13083 loc_result = implicit_ptr_descriptor (rtl, 0);
13084 break;
13085
13086 case PLUS:
13087 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13088 && CONST_INT_P (XEXP (rtl, 1)))
13089 {
13090 loc_result
13091 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13092 break;
13093 }
13094 /* FALLTHRU */
13095 do_default:
13096 default:
13097 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13098 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13099 && dwarf_version >= 4)
13100 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13101 {
13102 /* Value expression. */
13103 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13104 if (loc_result)
13105 add_loc_descr (&loc_result,
13106 new_loc_descr (DW_OP_stack_value, 0, 0));
13107 }
13108 break;
13109 }
13110
13111 return loc_result;
13112 }
13113
13114 /* We need to figure out what section we should use as the base for the
13115 address ranges where a given location is valid.
13116 1. If this particular DECL has a section associated with it, use that.
13117 2. If this function has a section associated with it, use that.
13118 3. Otherwise, use the text section.
13119 XXX: If you split a variable across multiple sections, we won't notice. */
13120
13121 static const char *
13122 secname_for_decl (const_tree decl)
13123 {
13124 const char *secname;
13125
13126 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13127 {
13128 tree sectree = DECL_SECTION_NAME (decl);
13129 secname = TREE_STRING_POINTER (sectree);
13130 }
13131 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13132 {
13133 tree sectree = DECL_SECTION_NAME (current_function_decl);
13134 secname = TREE_STRING_POINTER (sectree);
13135 }
13136 else if (cfun && in_cold_section_p)
13137 secname = crtl->subsections.cold_section_label;
13138 else
13139 secname = text_section_label;
13140
13141 return secname;
13142 }
13143
13144 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13145
13146 static bool
13147 decl_by_reference_p (tree decl)
13148 {
13149 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13150 || TREE_CODE (decl) == VAR_DECL)
13151 && DECL_BY_REFERENCE (decl));
13152 }
13153
13154 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13155 for VARLOC. */
13156
13157 static dw_loc_descr_ref
13158 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13159 enum var_init_status initialized)
13160 {
13161 int have_address = 0;
13162 dw_loc_descr_ref descr;
13163 enum machine_mode mode;
13164
13165 if (want_address != 2)
13166 {
13167 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13168 /* Single part. */
13169 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13170 {
13171 varloc = PAT_VAR_LOCATION_LOC (varloc);
13172 if (GET_CODE (varloc) == EXPR_LIST)
13173 varloc = XEXP (varloc, 0);
13174 mode = GET_MODE (varloc);
13175 if (MEM_P (varloc))
13176 {
13177 rtx addr = XEXP (varloc, 0);
13178 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13179 mode, initialized);
13180 if (descr)
13181 have_address = 1;
13182 else
13183 {
13184 rtx x = avoid_constant_pool_reference (varloc);
13185 if (x != varloc)
13186 descr = mem_loc_descriptor (x, mode, VOIDmode,
13187 initialized);
13188 }
13189 }
13190 else
13191 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13192 }
13193 else
13194 return 0;
13195 }
13196 else
13197 {
13198 if (GET_CODE (varloc) == VAR_LOCATION)
13199 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13200 else
13201 mode = DECL_MODE (loc);
13202 descr = loc_descriptor (varloc, mode, initialized);
13203 have_address = 1;
13204 }
13205
13206 if (!descr)
13207 return 0;
13208
13209 if (want_address == 2 && !have_address
13210 && (dwarf_version >= 4 || !dwarf_strict))
13211 {
13212 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13213 {
13214 expansion_failed (loc, NULL_RTX,
13215 "DWARF address size mismatch");
13216 return 0;
13217 }
13218 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13219 have_address = 1;
13220 }
13221 /* Show if we can't fill the request for an address. */
13222 if (want_address && !have_address)
13223 {
13224 expansion_failed (loc, NULL_RTX,
13225 "Want address and only have value");
13226 return 0;
13227 }
13228
13229 /* If we've got an address and don't want one, dereference. */
13230 if (!want_address && have_address)
13231 {
13232 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13233 enum dwarf_location_atom op;
13234
13235 if (size > DWARF2_ADDR_SIZE || size == -1)
13236 {
13237 expansion_failed (loc, NULL_RTX,
13238 "DWARF address size mismatch");
13239 return 0;
13240 }
13241 else if (size == DWARF2_ADDR_SIZE)
13242 op = DW_OP_deref;
13243 else
13244 op = DW_OP_deref_size;
13245
13246 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13247 }
13248
13249 return descr;
13250 }
13251
13252 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13253 if it is not possible. */
13254
13255 static dw_loc_descr_ref
13256 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13257 {
13258 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13259 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13260 else if (dwarf_version >= 3 || !dwarf_strict)
13261 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13262 else
13263 return NULL;
13264 }
13265
13266 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13267 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13268
13269 static dw_loc_descr_ref
13270 dw_sra_loc_expr (tree decl, rtx loc)
13271 {
13272 rtx p;
13273 unsigned int padsize = 0;
13274 dw_loc_descr_ref descr, *descr_tail;
13275 unsigned HOST_WIDE_INT decl_size;
13276 rtx varloc;
13277 enum var_init_status initialized;
13278
13279 if (DECL_SIZE (decl) == NULL
13280 || !host_integerp (DECL_SIZE (decl), 1))
13281 return NULL;
13282
13283 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13284 descr = NULL;
13285 descr_tail = &descr;
13286
13287 for (p = loc; p; p = XEXP (p, 1))
13288 {
13289 unsigned int bitsize = decl_piece_bitsize (p);
13290 rtx loc_note = *decl_piece_varloc_ptr (p);
13291 dw_loc_descr_ref cur_descr;
13292 dw_loc_descr_ref *tail, last = NULL;
13293 unsigned int opsize = 0;
13294
13295 if (loc_note == NULL_RTX
13296 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13297 {
13298 padsize += bitsize;
13299 continue;
13300 }
13301 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13302 varloc = NOTE_VAR_LOCATION (loc_note);
13303 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13304 if (cur_descr == NULL)
13305 {
13306 padsize += bitsize;
13307 continue;
13308 }
13309
13310 /* Check that cur_descr either doesn't use
13311 DW_OP_*piece operations, or their sum is equal
13312 to bitsize. Otherwise we can't embed it. */
13313 for (tail = &cur_descr; *tail != NULL;
13314 tail = &(*tail)->dw_loc_next)
13315 if ((*tail)->dw_loc_opc == DW_OP_piece)
13316 {
13317 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13318 * BITS_PER_UNIT;
13319 last = *tail;
13320 }
13321 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13322 {
13323 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13324 last = *tail;
13325 }
13326
13327 if (last != NULL && opsize != bitsize)
13328 {
13329 padsize += bitsize;
13330 continue;
13331 }
13332
13333 /* If there is a hole, add DW_OP_*piece after empty DWARF
13334 expression, which means that those bits are optimized out. */
13335 if (padsize)
13336 {
13337 if (padsize > decl_size)
13338 return NULL;
13339 decl_size -= padsize;
13340 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13341 if (*descr_tail == NULL)
13342 return NULL;
13343 descr_tail = &(*descr_tail)->dw_loc_next;
13344 padsize = 0;
13345 }
13346 *descr_tail = cur_descr;
13347 descr_tail = tail;
13348 if (bitsize > decl_size)
13349 return NULL;
13350 decl_size -= bitsize;
13351 if (last == NULL)
13352 {
13353 HOST_WIDE_INT offset = 0;
13354 if (GET_CODE (varloc) == VAR_LOCATION
13355 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13356 {
13357 varloc = PAT_VAR_LOCATION_LOC (varloc);
13358 if (GET_CODE (varloc) == EXPR_LIST)
13359 varloc = XEXP (varloc, 0);
13360 }
13361 do
13362 {
13363 if (GET_CODE (varloc) == CONST
13364 || GET_CODE (varloc) == SIGN_EXTEND
13365 || GET_CODE (varloc) == ZERO_EXTEND)
13366 varloc = XEXP (varloc, 0);
13367 else if (GET_CODE (varloc) == SUBREG)
13368 varloc = SUBREG_REG (varloc);
13369 else
13370 break;
13371 }
13372 while (1);
13373 /* DW_OP_bit_size offset should be zero for register
13374 or implicit location descriptions and empty location
13375 descriptions, but for memory addresses needs big endian
13376 adjustment. */
13377 if (MEM_P (varloc))
13378 {
13379 unsigned HOST_WIDE_INT memsize
13380 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13381 if (memsize != bitsize)
13382 {
13383 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13384 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13385 return NULL;
13386 if (memsize < bitsize)
13387 return NULL;
13388 if (BITS_BIG_ENDIAN)
13389 offset = memsize - bitsize;
13390 }
13391 }
13392
13393 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13394 if (*descr_tail == NULL)
13395 return NULL;
13396 descr_tail = &(*descr_tail)->dw_loc_next;
13397 }
13398 }
13399
13400 /* If there were any non-empty expressions, add padding till the end of
13401 the decl. */
13402 if (descr != NULL && decl_size != 0)
13403 {
13404 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13405 if (*descr_tail == NULL)
13406 return NULL;
13407 }
13408 return descr;
13409 }
13410
13411 /* Return the dwarf representation of the location list LOC_LIST of
13412 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13413 function. */
13414
13415 static dw_loc_list_ref
13416 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13417 {
13418 const char *endname, *secname;
13419 rtx varloc;
13420 enum var_init_status initialized;
13421 struct var_loc_node *node;
13422 dw_loc_descr_ref descr;
13423 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13424 dw_loc_list_ref list = NULL;
13425 dw_loc_list_ref *listp = &list;
13426
13427 /* Now that we know what section we are using for a base,
13428 actually construct the list of locations.
13429 The first location information is what is passed to the
13430 function that creates the location list, and the remaining
13431 locations just get added on to that list.
13432 Note that we only know the start address for a location
13433 (IE location changes), so to build the range, we use
13434 the range [current location start, next location start].
13435 This means we have to special case the last node, and generate
13436 a range of [last location start, end of function label]. */
13437
13438 secname = secname_for_decl (decl);
13439
13440 for (node = loc_list->first; node; node = node->next)
13441 if (GET_CODE (node->loc) == EXPR_LIST
13442 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13443 {
13444 if (GET_CODE (node->loc) == EXPR_LIST)
13445 {
13446 /* This requires DW_OP_{,bit_}piece, which is not usable
13447 inside DWARF expressions. */
13448 if (want_address != 2)
13449 continue;
13450 descr = dw_sra_loc_expr (decl, node->loc);
13451 if (descr == NULL)
13452 continue;
13453 }
13454 else
13455 {
13456 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13457 varloc = NOTE_VAR_LOCATION (node->loc);
13458 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13459 }
13460 if (descr)
13461 {
13462 bool range_across_switch = false;
13463 /* If section switch happens in between node->label
13464 and node->next->label (or end of function) and
13465 we can't emit it as a single entry list,
13466 emit two ranges, first one ending at the end
13467 of first partition and second one starting at the
13468 beginning of second partition. */
13469 if (node == loc_list->last_before_switch
13470 && (node != loc_list->first || loc_list->first->next)
13471 && current_function_decl)
13472 {
13473 endname = cfun->fde->dw_fde_end;
13474 range_across_switch = true;
13475 }
13476 /* The variable has a location between NODE->LABEL and
13477 NODE->NEXT->LABEL. */
13478 else if (node->next)
13479 endname = node->next->label;
13480 /* If the variable has a location at the last label
13481 it keeps its location until the end of function. */
13482 else if (!current_function_decl)
13483 endname = text_end_label;
13484 else
13485 {
13486 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13487 current_function_funcdef_no);
13488 endname = ggc_strdup (label_id);
13489 }
13490
13491 *listp = new_loc_list (descr, node->label, endname, secname);
13492 if (TREE_CODE (decl) == PARM_DECL
13493 && node == loc_list->first
13494 && NOTE_P (node->loc)
13495 && strcmp (node->label, endname) == 0)
13496 (*listp)->force = true;
13497 listp = &(*listp)->dw_loc_next;
13498
13499 if (range_across_switch)
13500 {
13501 if (GET_CODE (node->loc) == EXPR_LIST)
13502 descr = dw_sra_loc_expr (decl, node->loc);
13503 else
13504 {
13505 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13506 varloc = NOTE_VAR_LOCATION (node->loc);
13507 descr = dw_loc_list_1 (decl, varloc, want_address,
13508 initialized);
13509 }
13510 gcc_assert (descr);
13511 /* The variable has a location between NODE->LABEL and
13512 NODE->NEXT->LABEL. */
13513 if (node->next)
13514 endname = node->next->label;
13515 else
13516 endname = cfun->fde->dw_fde_second_end;
13517 *listp = new_loc_list (descr,
13518 cfun->fde->dw_fde_second_begin,
13519 endname, secname);
13520 listp = &(*listp)->dw_loc_next;
13521 }
13522 }
13523 }
13524
13525 /* Try to avoid the overhead of a location list emitting a location
13526 expression instead, but only if we didn't have more than one
13527 location entry in the first place. If some entries were not
13528 representable, we don't want to pretend a single entry that was
13529 applies to the entire scope in which the variable is
13530 available. */
13531 if (list && loc_list->first->next)
13532 gen_llsym (list);
13533
13534 return list;
13535 }
13536
13537 /* Return if the loc_list has only single element and thus can be represented
13538 as location description. */
13539
13540 static bool
13541 single_element_loc_list_p (dw_loc_list_ref list)
13542 {
13543 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13544 return !list->ll_symbol;
13545 }
13546
13547 /* To each location in list LIST add loc descr REF. */
13548
13549 static void
13550 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13551 {
13552 dw_loc_descr_ref copy;
13553 add_loc_descr (&list->expr, ref);
13554 list = list->dw_loc_next;
13555 while (list)
13556 {
13557 copy = ggc_alloc_dw_loc_descr_node ();
13558 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13559 add_loc_descr (&list->expr, copy);
13560 while (copy->dw_loc_next)
13561 {
13562 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13563 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13564 copy->dw_loc_next = new_copy;
13565 copy = new_copy;
13566 }
13567 list = list->dw_loc_next;
13568 }
13569 }
13570
13571 /* Given two lists RET and LIST
13572 produce location list that is result of adding expression in LIST
13573 to expression in RET on each position in program.
13574 Might be destructive on both RET and LIST.
13575
13576 TODO: We handle only simple cases of RET or LIST having at most one
13577 element. General case would inolve sorting the lists in program order
13578 and merging them that will need some additional work.
13579 Adding that will improve quality of debug info especially for SRA-ed
13580 structures. */
13581
13582 static void
13583 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13584 {
13585 if (!list)
13586 return;
13587 if (!*ret)
13588 {
13589 *ret = list;
13590 return;
13591 }
13592 if (!list->dw_loc_next)
13593 {
13594 add_loc_descr_to_each (*ret, list->expr);
13595 return;
13596 }
13597 if (!(*ret)->dw_loc_next)
13598 {
13599 add_loc_descr_to_each (list, (*ret)->expr);
13600 *ret = list;
13601 return;
13602 }
13603 expansion_failed (NULL_TREE, NULL_RTX,
13604 "Don't know how to merge two non-trivial"
13605 " location lists.\n");
13606 *ret = NULL;
13607 return;
13608 }
13609
13610 /* LOC is constant expression. Try a luck, look it up in constant
13611 pool and return its loc_descr of its address. */
13612
13613 static dw_loc_descr_ref
13614 cst_pool_loc_descr (tree loc)
13615 {
13616 /* Get an RTL for this, if something has been emitted. */
13617 rtx rtl = lookup_constant_def (loc);
13618
13619 if (!rtl || !MEM_P (rtl))
13620 {
13621 gcc_assert (!rtl);
13622 return 0;
13623 }
13624 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13625
13626 /* TODO: We might get more coverage if we was actually delaying expansion
13627 of all expressions till end of compilation when constant pools are fully
13628 populated. */
13629 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13630 {
13631 expansion_failed (loc, NULL_RTX,
13632 "CST value in contant pool but not marked.");
13633 return 0;
13634 }
13635 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13636 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13637 }
13638
13639 /* Return dw_loc_list representing address of addr_expr LOC
13640 by looking for inner INDIRECT_REF expression and turning
13641 it into simple arithmetics. */
13642
13643 static dw_loc_list_ref
13644 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13645 {
13646 tree obj, offset;
13647 HOST_WIDE_INT bitsize, bitpos, bytepos;
13648 enum machine_mode mode;
13649 int unsignedp, volatilep = 0;
13650 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13651
13652 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13653 &bitsize, &bitpos, &offset, &mode,
13654 &unsignedp, &volatilep, false);
13655 STRIP_NOPS (obj);
13656 if (bitpos % BITS_PER_UNIT)
13657 {
13658 expansion_failed (loc, NULL_RTX, "bitfield access");
13659 return 0;
13660 }
13661 if (!INDIRECT_REF_P (obj))
13662 {
13663 expansion_failed (obj,
13664 NULL_RTX, "no indirect ref in inner refrence");
13665 return 0;
13666 }
13667 if (!offset && !bitpos)
13668 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13669 else if (toplev
13670 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13671 && (dwarf_version >= 4 || !dwarf_strict))
13672 {
13673 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13674 if (!list_ret)
13675 return 0;
13676 if (offset)
13677 {
13678 /* Variable offset. */
13679 list_ret1 = loc_list_from_tree (offset, 0);
13680 if (list_ret1 == 0)
13681 return 0;
13682 add_loc_list (&list_ret, list_ret1);
13683 if (!list_ret)
13684 return 0;
13685 add_loc_descr_to_each (list_ret,
13686 new_loc_descr (DW_OP_plus, 0, 0));
13687 }
13688 bytepos = bitpos / BITS_PER_UNIT;
13689 if (bytepos > 0)
13690 add_loc_descr_to_each (list_ret,
13691 new_loc_descr (DW_OP_plus_uconst,
13692 bytepos, 0));
13693 else if (bytepos < 0)
13694 loc_list_plus_const (list_ret, bytepos);
13695 add_loc_descr_to_each (list_ret,
13696 new_loc_descr (DW_OP_stack_value, 0, 0));
13697 }
13698 return list_ret;
13699 }
13700
13701
13702 /* Generate Dwarf location list representing LOC.
13703 If WANT_ADDRESS is false, expression computing LOC will be computed
13704 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13705 if WANT_ADDRESS is 2, expression computing address useable in location
13706 will be returned (i.e. DW_OP_reg can be used
13707 to refer to register values). */
13708
13709 static dw_loc_list_ref
13710 loc_list_from_tree (tree loc, int want_address)
13711 {
13712 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13713 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13714 int have_address = 0;
13715 enum dwarf_location_atom op;
13716
13717 /* ??? Most of the time we do not take proper care for sign/zero
13718 extending the values properly. Hopefully this won't be a real
13719 problem... */
13720
13721 switch (TREE_CODE (loc))
13722 {
13723 case ERROR_MARK:
13724 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13725 return 0;
13726
13727 case PLACEHOLDER_EXPR:
13728 /* This case involves extracting fields from an object to determine the
13729 position of other fields. We don't try to encode this here. The
13730 only user of this is Ada, which encodes the needed information using
13731 the names of types. */
13732 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13733 return 0;
13734
13735 case CALL_EXPR:
13736 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13737 /* There are no opcodes for these operations. */
13738 return 0;
13739
13740 case PREINCREMENT_EXPR:
13741 case PREDECREMENT_EXPR:
13742 case POSTINCREMENT_EXPR:
13743 case POSTDECREMENT_EXPR:
13744 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13745 /* There are no opcodes for these operations. */
13746 return 0;
13747
13748 case ADDR_EXPR:
13749 /* If we already want an address, see if there is INDIRECT_REF inside
13750 e.g. for &this->field. */
13751 if (want_address)
13752 {
13753 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13754 (loc, want_address == 2);
13755 if (list_ret)
13756 have_address = 1;
13757 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13758 && (ret = cst_pool_loc_descr (loc)))
13759 have_address = 1;
13760 }
13761 /* Otherwise, process the argument and look for the address. */
13762 if (!list_ret && !ret)
13763 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13764 else
13765 {
13766 if (want_address)
13767 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13768 return NULL;
13769 }
13770 break;
13771
13772 case VAR_DECL:
13773 if (DECL_THREAD_LOCAL_P (loc))
13774 {
13775 rtx rtl;
13776 enum dwarf_location_atom tls_op;
13777 enum dtprel_bool dtprel = dtprel_false;
13778
13779 if (targetm.have_tls)
13780 {
13781 /* If this is not defined, we have no way to emit the
13782 data. */
13783 if (!targetm.asm_out.output_dwarf_dtprel)
13784 return 0;
13785
13786 /* The way DW_OP_GNU_push_tls_address is specified, we
13787 can only look up addresses of objects in the current
13788 module. We used DW_OP_addr as first op, but that's
13789 wrong, because DW_OP_addr is relocated by the debug
13790 info consumer, while DW_OP_GNU_push_tls_address
13791 operand shouldn't be. */
13792 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13793 return 0;
13794 dtprel = dtprel_true;
13795 tls_op = DW_OP_GNU_push_tls_address;
13796 }
13797 else
13798 {
13799 if (!targetm.emutls.debug_form_tls_address
13800 || !(dwarf_version >= 3 || !dwarf_strict))
13801 return 0;
13802 /* We stuffed the control variable into the DECL_VALUE_EXPR
13803 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13804 no longer appear in gimple code. We used the control
13805 variable in specific so that we could pick it up here. */
13806 loc = DECL_VALUE_EXPR (loc);
13807 tls_op = DW_OP_form_tls_address;
13808 }
13809
13810 rtl = rtl_for_decl_location (loc);
13811 if (rtl == NULL_RTX)
13812 return 0;
13813
13814 if (!MEM_P (rtl))
13815 return 0;
13816 rtl = XEXP (rtl, 0);
13817 if (! CONSTANT_P (rtl))
13818 return 0;
13819
13820 ret = new_addr_loc_descr (rtl, dtprel);
13821 ret1 = new_loc_descr (tls_op, 0, 0);
13822 add_loc_descr (&ret, ret1);
13823
13824 have_address = 1;
13825 break;
13826 }
13827 /* FALLTHRU */
13828
13829 case PARM_DECL:
13830 case RESULT_DECL:
13831 if (DECL_HAS_VALUE_EXPR_P (loc))
13832 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13833 want_address);
13834 /* FALLTHRU */
13835
13836 case FUNCTION_DECL:
13837 {
13838 rtx rtl;
13839 var_loc_list *loc_list = lookup_decl_loc (loc);
13840
13841 if (loc_list && loc_list->first)
13842 {
13843 list_ret = dw_loc_list (loc_list, loc, want_address);
13844 have_address = want_address != 0;
13845 break;
13846 }
13847 rtl = rtl_for_decl_location (loc);
13848 if (rtl == NULL_RTX)
13849 {
13850 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13851 return 0;
13852 }
13853 else if (CONST_INT_P (rtl))
13854 {
13855 HOST_WIDE_INT val = INTVAL (rtl);
13856 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13857 val &= GET_MODE_MASK (DECL_MODE (loc));
13858 ret = int_loc_descriptor (val);
13859 }
13860 else if (GET_CODE (rtl) == CONST_STRING)
13861 {
13862 expansion_failed (loc, NULL_RTX, "CONST_STRING");
13863 return 0;
13864 }
13865 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13866 ret = new_addr_loc_descr (rtl, dtprel_false);
13867 else
13868 {
13869 enum machine_mode mode, mem_mode;
13870
13871 /* Certain constructs can only be represented at top-level. */
13872 if (want_address == 2)
13873 {
13874 ret = loc_descriptor (rtl, VOIDmode,
13875 VAR_INIT_STATUS_INITIALIZED);
13876 have_address = 1;
13877 }
13878 else
13879 {
13880 mode = GET_MODE (rtl);
13881 mem_mode = VOIDmode;
13882 if (MEM_P (rtl))
13883 {
13884 mem_mode = mode;
13885 mode = get_address_mode (rtl);
13886 rtl = XEXP (rtl, 0);
13887 have_address = 1;
13888 }
13889 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13890 VAR_INIT_STATUS_INITIALIZED);
13891 }
13892 if (!ret)
13893 expansion_failed (loc, rtl,
13894 "failed to produce loc descriptor for rtl");
13895 }
13896 }
13897 break;
13898
13899 case MEM_REF:
13900 /* ??? FIXME. */
13901 if (!integer_zerop (TREE_OPERAND (loc, 1)))
13902 return 0;
13903 /* Fallthru. */
13904 case INDIRECT_REF:
13905 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13906 have_address = 1;
13907 break;
13908
13909 case COMPOUND_EXPR:
13910 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13911
13912 CASE_CONVERT:
13913 case VIEW_CONVERT_EXPR:
13914 case SAVE_EXPR:
13915 case MODIFY_EXPR:
13916 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13917
13918 case COMPONENT_REF:
13919 case BIT_FIELD_REF:
13920 case ARRAY_REF:
13921 case ARRAY_RANGE_REF:
13922 case REALPART_EXPR:
13923 case IMAGPART_EXPR:
13924 {
13925 tree obj, offset;
13926 HOST_WIDE_INT bitsize, bitpos, bytepos;
13927 enum machine_mode mode;
13928 int unsignedp, volatilep = 0;
13929
13930 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13931 &unsignedp, &volatilep, false);
13932
13933 gcc_assert (obj != loc);
13934
13935 list_ret = loc_list_from_tree (obj,
13936 want_address == 2
13937 && !bitpos && !offset ? 2 : 1);
13938 /* TODO: We can extract value of the small expression via shifting even
13939 for nonzero bitpos. */
13940 if (list_ret == 0)
13941 return 0;
13942 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13943 {
13944 expansion_failed (loc, NULL_RTX,
13945 "bitfield access");
13946 return 0;
13947 }
13948
13949 if (offset != NULL_TREE)
13950 {
13951 /* Variable offset. */
13952 list_ret1 = loc_list_from_tree (offset, 0);
13953 if (list_ret1 == 0)
13954 return 0;
13955 add_loc_list (&list_ret, list_ret1);
13956 if (!list_ret)
13957 return 0;
13958 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13959 }
13960
13961 bytepos = bitpos / BITS_PER_UNIT;
13962 if (bytepos > 0)
13963 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13964 else if (bytepos < 0)
13965 loc_list_plus_const (list_ret, bytepos);
13966
13967 have_address = 1;
13968 break;
13969 }
13970
13971 case INTEGER_CST:
13972 if ((want_address || !host_integerp (loc, 0))
13973 && (ret = cst_pool_loc_descr (loc)))
13974 have_address = 1;
13975 else if (want_address == 2
13976 && host_integerp (loc, 0)
13977 && (ret = address_of_int_loc_descriptor
13978 (int_size_in_bytes (TREE_TYPE (loc)),
13979 tree_low_cst (loc, 0))))
13980 have_address = 1;
13981 else if (host_integerp (loc, 0))
13982 ret = int_loc_descriptor (tree_low_cst (loc, 0));
13983 else
13984 {
13985 expansion_failed (loc, NULL_RTX,
13986 "Integer operand is not host integer");
13987 return 0;
13988 }
13989 break;
13990
13991 case CONSTRUCTOR:
13992 case REAL_CST:
13993 case STRING_CST:
13994 case COMPLEX_CST:
13995 if ((ret = cst_pool_loc_descr (loc)))
13996 have_address = 1;
13997 else
13998 /* We can construct small constants here using int_loc_descriptor. */
13999 expansion_failed (loc, NULL_RTX,
14000 "constructor or constant not in constant pool");
14001 break;
14002
14003 case TRUTH_AND_EXPR:
14004 case TRUTH_ANDIF_EXPR:
14005 case BIT_AND_EXPR:
14006 op = DW_OP_and;
14007 goto do_binop;
14008
14009 case TRUTH_XOR_EXPR:
14010 case BIT_XOR_EXPR:
14011 op = DW_OP_xor;
14012 goto do_binop;
14013
14014 case TRUTH_OR_EXPR:
14015 case TRUTH_ORIF_EXPR:
14016 case BIT_IOR_EXPR:
14017 op = DW_OP_or;
14018 goto do_binop;
14019
14020 case FLOOR_DIV_EXPR:
14021 case CEIL_DIV_EXPR:
14022 case ROUND_DIV_EXPR:
14023 case TRUNC_DIV_EXPR:
14024 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14025 return 0;
14026 op = DW_OP_div;
14027 goto do_binop;
14028
14029 case MINUS_EXPR:
14030 op = DW_OP_minus;
14031 goto do_binop;
14032
14033 case FLOOR_MOD_EXPR:
14034 case CEIL_MOD_EXPR:
14035 case ROUND_MOD_EXPR:
14036 case TRUNC_MOD_EXPR:
14037 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14038 {
14039 op = DW_OP_mod;
14040 goto do_binop;
14041 }
14042 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14043 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14044 if (list_ret == 0 || list_ret1 == 0)
14045 return 0;
14046
14047 add_loc_list (&list_ret, list_ret1);
14048 if (list_ret == 0)
14049 return 0;
14050 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14051 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14052 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14053 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14054 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14055 break;
14056
14057 case MULT_EXPR:
14058 op = DW_OP_mul;
14059 goto do_binop;
14060
14061 case LSHIFT_EXPR:
14062 op = DW_OP_shl;
14063 goto do_binop;
14064
14065 case RSHIFT_EXPR:
14066 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14067 goto do_binop;
14068
14069 case POINTER_PLUS_EXPR:
14070 case PLUS_EXPR:
14071 if (host_integerp (TREE_OPERAND (loc, 1), 0))
14072 {
14073 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14074 if (list_ret == 0)
14075 return 0;
14076
14077 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14078 break;
14079 }
14080
14081 op = DW_OP_plus;
14082 goto do_binop;
14083
14084 case LE_EXPR:
14085 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14086 return 0;
14087
14088 op = DW_OP_le;
14089 goto do_binop;
14090
14091 case GE_EXPR:
14092 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14093 return 0;
14094
14095 op = DW_OP_ge;
14096 goto do_binop;
14097
14098 case LT_EXPR:
14099 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14100 return 0;
14101
14102 op = DW_OP_lt;
14103 goto do_binop;
14104
14105 case GT_EXPR:
14106 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14107 return 0;
14108
14109 op = DW_OP_gt;
14110 goto do_binop;
14111
14112 case EQ_EXPR:
14113 op = DW_OP_eq;
14114 goto do_binop;
14115
14116 case NE_EXPR:
14117 op = DW_OP_ne;
14118 goto do_binop;
14119
14120 do_binop:
14121 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14122 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14123 if (list_ret == 0 || list_ret1 == 0)
14124 return 0;
14125
14126 add_loc_list (&list_ret, list_ret1);
14127 if (list_ret == 0)
14128 return 0;
14129 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14130 break;
14131
14132 case TRUTH_NOT_EXPR:
14133 case BIT_NOT_EXPR:
14134 op = DW_OP_not;
14135 goto do_unop;
14136
14137 case ABS_EXPR:
14138 op = DW_OP_abs;
14139 goto do_unop;
14140
14141 case NEGATE_EXPR:
14142 op = DW_OP_neg;
14143 goto do_unop;
14144
14145 do_unop:
14146 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14147 if (list_ret == 0)
14148 return 0;
14149
14150 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14151 break;
14152
14153 case MIN_EXPR:
14154 case MAX_EXPR:
14155 {
14156 const enum tree_code code =
14157 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14158
14159 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14160 build2 (code, integer_type_node,
14161 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14162 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14163 }
14164
14165 /* ... fall through ... */
14166
14167 case COND_EXPR:
14168 {
14169 dw_loc_descr_ref lhs
14170 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14171 dw_loc_list_ref rhs
14172 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14173 dw_loc_descr_ref bra_node, jump_node, tmp;
14174
14175 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14176 if (list_ret == 0 || lhs == 0 || rhs == 0)
14177 return 0;
14178
14179 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14180 add_loc_descr_to_each (list_ret, bra_node);
14181
14182 add_loc_list (&list_ret, rhs);
14183 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14184 add_loc_descr_to_each (list_ret, jump_node);
14185
14186 add_loc_descr_to_each (list_ret, lhs);
14187 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14188 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14189
14190 /* ??? Need a node to point the skip at. Use a nop. */
14191 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14192 add_loc_descr_to_each (list_ret, tmp);
14193 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14194 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14195 }
14196 break;
14197
14198 case FIX_TRUNC_EXPR:
14199 return 0;
14200
14201 default:
14202 /* Leave front-end specific codes as simply unknown. This comes
14203 up, for instance, with the C STMT_EXPR. */
14204 if ((unsigned int) TREE_CODE (loc)
14205 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14206 {
14207 expansion_failed (loc, NULL_RTX,
14208 "language specific tree node");
14209 return 0;
14210 }
14211
14212 #ifdef ENABLE_CHECKING
14213 /* Otherwise this is a generic code; we should just lists all of
14214 these explicitly. We forgot one. */
14215 gcc_unreachable ();
14216 #else
14217 /* In a release build, we want to degrade gracefully: better to
14218 generate incomplete debugging information than to crash. */
14219 return NULL;
14220 #endif
14221 }
14222
14223 if (!ret && !list_ret)
14224 return 0;
14225
14226 if (want_address == 2 && !have_address
14227 && (dwarf_version >= 4 || !dwarf_strict))
14228 {
14229 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14230 {
14231 expansion_failed (loc, NULL_RTX,
14232 "DWARF address size mismatch");
14233 return 0;
14234 }
14235 if (ret)
14236 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14237 else
14238 add_loc_descr_to_each (list_ret,
14239 new_loc_descr (DW_OP_stack_value, 0, 0));
14240 have_address = 1;
14241 }
14242 /* Show if we can't fill the request for an address. */
14243 if (want_address && !have_address)
14244 {
14245 expansion_failed (loc, NULL_RTX,
14246 "Want address and only have value");
14247 return 0;
14248 }
14249
14250 gcc_assert (!ret || !list_ret);
14251
14252 /* If we've got an address and don't want one, dereference. */
14253 if (!want_address && have_address)
14254 {
14255 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14256
14257 if (size > DWARF2_ADDR_SIZE || size == -1)
14258 {
14259 expansion_failed (loc, NULL_RTX,
14260 "DWARF address size mismatch");
14261 return 0;
14262 }
14263 else if (size == DWARF2_ADDR_SIZE)
14264 op = DW_OP_deref;
14265 else
14266 op = DW_OP_deref_size;
14267
14268 if (ret)
14269 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14270 else
14271 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14272 }
14273 if (ret)
14274 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14275
14276 return list_ret;
14277 }
14278
14279 /* Same as above but return only single location expression. */
14280 static dw_loc_descr_ref
14281 loc_descriptor_from_tree (tree loc, int want_address)
14282 {
14283 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14284 if (!ret)
14285 return NULL;
14286 if (ret->dw_loc_next)
14287 {
14288 expansion_failed (loc, NULL_RTX,
14289 "Location list where only loc descriptor needed");
14290 return NULL;
14291 }
14292 return ret->expr;
14293 }
14294
14295 /* Given a value, round it up to the lowest multiple of `boundary'
14296 which is not less than the value itself. */
14297
14298 static inline HOST_WIDE_INT
14299 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14300 {
14301 return (((value + boundary - 1) / boundary) * boundary);
14302 }
14303
14304 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14305 pointer to the declared type for the relevant field variable, or return
14306 `integer_type_node' if the given node turns out to be an
14307 ERROR_MARK node. */
14308
14309 static inline tree
14310 field_type (const_tree decl)
14311 {
14312 tree type;
14313
14314 if (TREE_CODE (decl) == ERROR_MARK)
14315 return integer_type_node;
14316
14317 type = DECL_BIT_FIELD_TYPE (decl);
14318 if (type == NULL_TREE)
14319 type = TREE_TYPE (decl);
14320
14321 return type;
14322 }
14323
14324 /* Given a pointer to a tree node, return the alignment in bits for
14325 it, or else return BITS_PER_WORD if the node actually turns out to
14326 be an ERROR_MARK node. */
14327
14328 static inline unsigned
14329 simple_type_align_in_bits (const_tree type)
14330 {
14331 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14332 }
14333
14334 static inline unsigned
14335 simple_decl_align_in_bits (const_tree decl)
14336 {
14337 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14338 }
14339
14340 /* Return the result of rounding T up to ALIGN. */
14341
14342 static inline double_int
14343 round_up_to_align (double_int t, unsigned int align)
14344 {
14345 double_int alignd = double_int::from_uhwi (align);
14346 t += alignd;
14347 t += double_int_minus_one;
14348 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14349 t *= alignd;
14350 return t;
14351 }
14352
14353 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14354 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14355 or return 0 if we are unable to determine what that offset is, either
14356 because the argument turns out to be a pointer to an ERROR_MARK node, or
14357 because the offset is actually variable. (We can't handle the latter case
14358 just yet). */
14359
14360 static HOST_WIDE_INT
14361 field_byte_offset (const_tree decl)
14362 {
14363 double_int object_offset_in_bits;
14364 double_int object_offset_in_bytes;
14365 double_int bitpos_int;
14366
14367 if (TREE_CODE (decl) == ERROR_MARK)
14368 return 0;
14369
14370 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14371
14372 /* We cannot yet cope with fields whose positions are variable, so
14373 for now, when we see such things, we simply return 0. Someday, we may
14374 be able to handle such cases, but it will be damn difficult. */
14375 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14376 return 0;
14377
14378 bitpos_int = tree_to_double_int (bit_position (decl));
14379
14380 #ifdef PCC_BITFIELD_TYPE_MATTERS
14381 if (PCC_BITFIELD_TYPE_MATTERS)
14382 {
14383 tree type;
14384 tree field_size_tree;
14385 double_int deepest_bitpos;
14386 double_int field_size_in_bits;
14387 unsigned int type_align_in_bits;
14388 unsigned int decl_align_in_bits;
14389 double_int type_size_in_bits;
14390
14391 type = field_type (decl);
14392 type_size_in_bits = double_int_type_size_in_bits (type);
14393 type_align_in_bits = simple_type_align_in_bits (type);
14394
14395 field_size_tree = DECL_SIZE (decl);
14396
14397 /* The size could be unspecified if there was an error, or for
14398 a flexible array member. */
14399 if (!field_size_tree)
14400 field_size_tree = bitsize_zero_node;
14401
14402 /* If the size of the field is not constant, use the type size. */
14403 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14404 field_size_in_bits = tree_to_double_int (field_size_tree);
14405 else
14406 field_size_in_bits = type_size_in_bits;
14407
14408 decl_align_in_bits = simple_decl_align_in_bits (decl);
14409
14410 /* The GCC front-end doesn't make any attempt to keep track of the
14411 starting bit offset (relative to the start of the containing
14412 structure type) of the hypothetical "containing object" for a
14413 bit-field. Thus, when computing the byte offset value for the
14414 start of the "containing object" of a bit-field, we must deduce
14415 this information on our own. This can be rather tricky to do in
14416 some cases. For example, handling the following structure type
14417 definition when compiling for an i386/i486 target (which only
14418 aligns long long's to 32-bit boundaries) can be very tricky:
14419
14420 struct S { int field1; long long field2:31; };
14421
14422 Fortunately, there is a simple rule-of-thumb which can be used
14423 in such cases. When compiling for an i386/i486, GCC will
14424 allocate 8 bytes for the structure shown above. It decides to
14425 do this based upon one simple rule for bit-field allocation.
14426 GCC allocates each "containing object" for each bit-field at
14427 the first (i.e. lowest addressed) legitimate alignment boundary
14428 (based upon the required minimum alignment for the declared
14429 type of the field) which it can possibly use, subject to the
14430 condition that there is still enough available space remaining
14431 in the containing object (when allocated at the selected point)
14432 to fully accommodate all of the bits of the bit-field itself.
14433
14434 This simple rule makes it obvious why GCC allocates 8 bytes for
14435 each object of the structure type shown above. When looking
14436 for a place to allocate the "containing object" for `field2',
14437 the compiler simply tries to allocate a 64-bit "containing
14438 object" at each successive 32-bit boundary (starting at zero)
14439 until it finds a place to allocate that 64- bit field such that
14440 at least 31 contiguous (and previously unallocated) bits remain
14441 within that selected 64 bit field. (As it turns out, for the
14442 example above, the compiler finds it is OK to allocate the
14443 "containing object" 64-bit field at bit-offset zero within the
14444 structure type.)
14445
14446 Here we attempt to work backwards from the limited set of facts
14447 we're given, and we try to deduce from those facts, where GCC
14448 must have believed that the containing object started (within
14449 the structure type). The value we deduce is then used (by the
14450 callers of this routine) to generate DW_AT_location and
14451 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14452 the case of DW_AT_location, regular fields as well). */
14453
14454 /* Figure out the bit-distance from the start of the structure to
14455 the "deepest" bit of the bit-field. */
14456 deepest_bitpos = bitpos_int + field_size_in_bits;
14457
14458 /* This is the tricky part. Use some fancy footwork to deduce
14459 where the lowest addressed bit of the containing object must
14460 be. */
14461 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14462
14463 /* Round up to type_align by default. This works best for
14464 bitfields. */
14465 object_offset_in_bits
14466 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14467
14468 if (object_offset_in_bits.ugt (bitpos_int))
14469 {
14470 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14471
14472 /* Round up to decl_align instead. */
14473 object_offset_in_bits
14474 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14475 }
14476 }
14477 else
14478 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14479 object_offset_in_bits = bitpos_int;
14480
14481 object_offset_in_bytes
14482 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14483 true, TRUNC_DIV_EXPR);
14484 return object_offset_in_bytes.to_shwi ();
14485 }
14486 \f
14487 /* The following routines define various Dwarf attributes and any data
14488 associated with them. */
14489
14490 /* Add a location description attribute value to a DIE.
14491
14492 This emits location attributes suitable for whole variables and
14493 whole parameters. Note that the location attributes for struct fields are
14494 generated by the routine `data_member_location_attribute' below. */
14495
14496 static inline void
14497 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14498 dw_loc_list_ref descr)
14499 {
14500 if (descr == 0)
14501 return;
14502 if (single_element_loc_list_p (descr))
14503 add_AT_loc (die, attr_kind, descr->expr);
14504 else
14505 add_AT_loc_list (die, attr_kind, descr);
14506 }
14507
14508 /* Add DW_AT_accessibility attribute to DIE if needed. */
14509
14510 static void
14511 add_accessibility_attribute (dw_die_ref die, tree decl)
14512 {
14513 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14514 children, otherwise the default is DW_ACCESS_public. In DWARF2
14515 the default has always been DW_ACCESS_public. */
14516 if (TREE_PROTECTED (decl))
14517 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14518 else if (TREE_PRIVATE (decl))
14519 {
14520 if (dwarf_version == 2
14521 || die->die_parent == NULL
14522 || die->die_parent->die_tag != DW_TAG_class_type)
14523 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14524 }
14525 else if (dwarf_version > 2
14526 && die->die_parent
14527 && die->die_parent->die_tag == DW_TAG_class_type)
14528 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14529 }
14530
14531 /* Attach the specialized form of location attribute used for data members of
14532 struct and union types. In the special case of a FIELD_DECL node which
14533 represents a bit-field, the "offset" part of this special location
14534 descriptor must indicate the distance in bytes from the lowest-addressed
14535 byte of the containing struct or union type to the lowest-addressed byte of
14536 the "containing object" for the bit-field. (See the `field_byte_offset'
14537 function above).
14538
14539 For any given bit-field, the "containing object" is a hypothetical object
14540 (of some integral or enum type) within which the given bit-field lives. The
14541 type of this hypothetical "containing object" is always the same as the
14542 declared type of the individual bit-field itself (for GCC anyway... the
14543 DWARF spec doesn't actually mandate this). Note that it is the size (in
14544 bytes) of the hypothetical "containing object" which will be given in the
14545 DW_AT_byte_size attribute for this bit-field. (See the
14546 `byte_size_attribute' function below.) It is also used when calculating the
14547 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14548 function below.) */
14549
14550 static void
14551 add_data_member_location_attribute (dw_die_ref die, tree decl)
14552 {
14553 HOST_WIDE_INT offset;
14554 dw_loc_descr_ref loc_descr = 0;
14555
14556 if (TREE_CODE (decl) == TREE_BINFO)
14557 {
14558 /* We're working on the TAG_inheritance for a base class. */
14559 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14560 {
14561 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14562 aren't at a fixed offset from all (sub)objects of the same
14563 type. We need to extract the appropriate offset from our
14564 vtable. The following dwarf expression means
14565
14566 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14567
14568 This is specific to the V3 ABI, of course. */
14569
14570 dw_loc_descr_ref tmp;
14571
14572 /* Make a copy of the object address. */
14573 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14574 add_loc_descr (&loc_descr, tmp);
14575
14576 /* Extract the vtable address. */
14577 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14578 add_loc_descr (&loc_descr, tmp);
14579
14580 /* Calculate the address of the offset. */
14581 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14582 gcc_assert (offset < 0);
14583
14584 tmp = int_loc_descriptor (-offset);
14585 add_loc_descr (&loc_descr, tmp);
14586 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14587 add_loc_descr (&loc_descr, tmp);
14588
14589 /* Extract the offset. */
14590 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14591 add_loc_descr (&loc_descr, tmp);
14592
14593 /* Add it to the object address. */
14594 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14595 add_loc_descr (&loc_descr, tmp);
14596 }
14597 else
14598 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14599 }
14600 else
14601 offset = field_byte_offset (decl);
14602
14603 if (! loc_descr)
14604 {
14605 if (dwarf_version > 2)
14606 {
14607 /* Don't need to output a location expression, just the constant. */
14608 if (offset < 0)
14609 add_AT_int (die, DW_AT_data_member_location, offset);
14610 else
14611 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14612 return;
14613 }
14614 else
14615 {
14616 enum dwarf_location_atom op;
14617
14618 /* The DWARF2 standard says that we should assume that the structure
14619 address is already on the stack, so we can specify a structure
14620 field address by using DW_OP_plus_uconst. */
14621 op = DW_OP_plus_uconst;
14622 loc_descr = new_loc_descr (op, offset, 0);
14623 }
14624 }
14625
14626 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14627 }
14628
14629 /* Writes integer values to dw_vec_const array. */
14630
14631 static void
14632 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14633 {
14634 while (size != 0)
14635 {
14636 *dest++ = val & 0xff;
14637 val >>= 8;
14638 --size;
14639 }
14640 }
14641
14642 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14643
14644 static HOST_WIDE_INT
14645 extract_int (const unsigned char *src, unsigned int size)
14646 {
14647 HOST_WIDE_INT val = 0;
14648
14649 src += size;
14650 while (size != 0)
14651 {
14652 val <<= 8;
14653 val |= *--src & 0xff;
14654 --size;
14655 }
14656 return val;
14657 }
14658
14659 /* Writes double_int values to dw_vec_const array. */
14660
14661 static void
14662 insert_double (double_int val, unsigned char *dest)
14663 {
14664 unsigned char *p0 = dest;
14665 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14666
14667 if (WORDS_BIG_ENDIAN)
14668 {
14669 p0 = p1;
14670 p1 = dest;
14671 }
14672
14673 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14674 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14675 }
14676
14677 /* Writes floating point values to dw_vec_const array. */
14678
14679 static void
14680 insert_float (const_rtx rtl, unsigned char *array)
14681 {
14682 REAL_VALUE_TYPE rv;
14683 long val[4];
14684 int i;
14685
14686 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14687 real_to_target (val, &rv, GET_MODE (rtl));
14688
14689 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14690 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14691 {
14692 insert_int (val[i], 4, array);
14693 array += 4;
14694 }
14695 }
14696
14697 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14698 does not have a "location" either in memory or in a register. These
14699 things can arise in GNU C when a constant is passed as an actual parameter
14700 to an inlined function. They can also arise in C++ where declared
14701 constants do not necessarily get memory "homes". */
14702
14703 static bool
14704 add_const_value_attribute (dw_die_ref die, rtx rtl)
14705 {
14706 switch (GET_CODE (rtl))
14707 {
14708 case CONST_INT:
14709 {
14710 HOST_WIDE_INT val = INTVAL (rtl);
14711
14712 if (val < 0)
14713 add_AT_int (die, DW_AT_const_value, val);
14714 else
14715 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14716 }
14717 return true;
14718
14719 case CONST_DOUBLE:
14720 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14721 floating-point constant. A CONST_DOUBLE is used whenever the
14722 constant requires more than one word in order to be adequately
14723 represented. */
14724 {
14725 enum machine_mode mode = GET_MODE (rtl);
14726
14727 if (SCALAR_FLOAT_MODE_P (mode))
14728 {
14729 unsigned int length = GET_MODE_SIZE (mode);
14730 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14731
14732 insert_float (rtl, array);
14733 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14734 }
14735 else
14736 add_AT_double (die, DW_AT_const_value,
14737 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14738 }
14739 return true;
14740
14741 case CONST_VECTOR:
14742 {
14743 enum machine_mode mode = GET_MODE (rtl);
14744 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14745 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14746 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14747 (length * elt_size);
14748 unsigned int i;
14749 unsigned char *p;
14750
14751 switch (GET_MODE_CLASS (mode))
14752 {
14753 case MODE_VECTOR_INT:
14754 for (i = 0, p = array; i < length; i++, p += elt_size)
14755 {
14756 rtx elt = CONST_VECTOR_ELT (rtl, i);
14757 double_int val = rtx_to_double_int (elt);
14758
14759 if (elt_size <= sizeof (HOST_WIDE_INT))
14760 insert_int (val.to_shwi (), elt_size, p);
14761 else
14762 {
14763 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14764 insert_double (val, p);
14765 }
14766 }
14767 break;
14768
14769 case MODE_VECTOR_FLOAT:
14770 for (i = 0, p = array; i < length; i++, p += elt_size)
14771 {
14772 rtx elt = CONST_VECTOR_ELT (rtl, i);
14773 insert_float (elt, p);
14774 }
14775 break;
14776
14777 default:
14778 gcc_unreachable ();
14779 }
14780
14781 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14782 }
14783 return true;
14784
14785 case CONST_STRING:
14786 if (dwarf_version >= 4 || !dwarf_strict)
14787 {
14788 dw_loc_descr_ref loc_result;
14789 resolve_one_addr (&rtl, NULL);
14790 rtl_addr:
14791 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14792 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14793 add_AT_loc (die, DW_AT_location, loc_result);
14794 vec_safe_push (used_rtx_array, rtl);
14795 return true;
14796 }
14797 return false;
14798
14799 case CONST:
14800 if (CONSTANT_P (XEXP (rtl, 0)))
14801 return add_const_value_attribute (die, XEXP (rtl, 0));
14802 /* FALLTHROUGH */
14803 case SYMBOL_REF:
14804 if (!const_ok_for_output (rtl))
14805 return false;
14806 case LABEL_REF:
14807 if (dwarf_version >= 4 || !dwarf_strict)
14808 goto rtl_addr;
14809 return false;
14810
14811 case PLUS:
14812 /* In cases where an inlined instance of an inline function is passed
14813 the address of an `auto' variable (which is local to the caller) we
14814 can get a situation where the DECL_RTL of the artificial local
14815 variable (for the inlining) which acts as a stand-in for the
14816 corresponding formal parameter (of the inline function) will look
14817 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14818 exactly a compile-time constant expression, but it isn't the address
14819 of the (artificial) local variable either. Rather, it represents the
14820 *value* which the artificial local variable always has during its
14821 lifetime. We currently have no way to represent such quasi-constant
14822 values in Dwarf, so for now we just punt and generate nothing. */
14823 return false;
14824
14825 case HIGH:
14826 case CONST_FIXED:
14827 return false;
14828
14829 case MEM:
14830 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14831 && MEM_READONLY_P (rtl)
14832 && GET_MODE (rtl) == BLKmode)
14833 {
14834 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14835 return true;
14836 }
14837 return false;
14838
14839 default:
14840 /* No other kinds of rtx should be possible here. */
14841 gcc_unreachable ();
14842 }
14843 return false;
14844 }
14845
14846 /* Determine whether the evaluation of EXPR references any variables
14847 or functions which aren't otherwise used (and therefore may not be
14848 output). */
14849 static tree
14850 reference_to_unused (tree * tp, int * walk_subtrees,
14851 void * data ATTRIBUTE_UNUSED)
14852 {
14853 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14854 *walk_subtrees = 0;
14855
14856 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14857 && ! TREE_ASM_WRITTEN (*tp))
14858 return *tp;
14859 /* ??? The C++ FE emits debug information for using decls, so
14860 putting gcc_unreachable here falls over. See PR31899. For now
14861 be conservative. */
14862 else if (!cgraph_global_info_ready
14863 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14864 return *tp;
14865 else if (TREE_CODE (*tp) == VAR_DECL)
14866 {
14867 struct varpool_node *node = varpool_get_node (*tp);
14868 if (!node || !node->analyzed)
14869 return *tp;
14870 }
14871 else if (TREE_CODE (*tp) == FUNCTION_DECL
14872 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14873 {
14874 /* The call graph machinery must have finished analyzing,
14875 optimizing and gimplifying the CU by now.
14876 So if *TP has no call graph node associated
14877 to it, it means *TP will not be emitted. */
14878 if (!cgraph_get_node (*tp))
14879 return *tp;
14880 }
14881 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14882 return *tp;
14883
14884 return NULL_TREE;
14885 }
14886
14887 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14888 for use in a later add_const_value_attribute call. */
14889
14890 static rtx
14891 rtl_for_decl_init (tree init, tree type)
14892 {
14893 rtx rtl = NULL_RTX;
14894
14895 STRIP_NOPS (init);
14896
14897 /* If a variable is initialized with a string constant without embedded
14898 zeros, build CONST_STRING. */
14899 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14900 {
14901 tree enttype = TREE_TYPE (type);
14902 tree domain = TYPE_DOMAIN (type);
14903 enum machine_mode mode = TYPE_MODE (enttype);
14904
14905 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14906 && domain
14907 && integer_zerop (TYPE_MIN_VALUE (domain))
14908 && compare_tree_int (TYPE_MAX_VALUE (domain),
14909 TREE_STRING_LENGTH (init) - 1) == 0
14910 && ((size_t) TREE_STRING_LENGTH (init)
14911 == strlen (TREE_STRING_POINTER (init)) + 1))
14912 {
14913 rtl = gen_rtx_CONST_STRING (VOIDmode,
14914 ggc_strdup (TREE_STRING_POINTER (init)));
14915 rtl = gen_rtx_MEM (BLKmode, rtl);
14916 MEM_READONLY_P (rtl) = 1;
14917 }
14918 }
14919 /* Other aggregates, and complex values, could be represented using
14920 CONCAT: FIXME! */
14921 else if (AGGREGATE_TYPE_P (type)
14922 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14923 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14924 || TREE_CODE (type) == COMPLEX_TYPE)
14925 ;
14926 /* Vectors only work if their mode is supported by the target.
14927 FIXME: generic vectors ought to work too. */
14928 else if (TREE_CODE (type) == VECTOR_TYPE
14929 && !VECTOR_MODE_P (TYPE_MODE (type)))
14930 ;
14931 /* If the initializer is something that we know will expand into an
14932 immediate RTL constant, expand it now. We must be careful not to
14933 reference variables which won't be output. */
14934 else if (initializer_constant_valid_p (init, type)
14935 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14936 {
14937 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14938 possible. */
14939 if (TREE_CODE (type) == VECTOR_TYPE)
14940 switch (TREE_CODE (init))
14941 {
14942 case VECTOR_CST:
14943 break;
14944 case CONSTRUCTOR:
14945 if (TREE_CONSTANT (init))
14946 {
14947 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
14948 bool constant_p = true;
14949 tree value;
14950 unsigned HOST_WIDE_INT ix;
14951
14952 /* Even when ctor is constant, it might contain non-*_CST
14953 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14954 belong into VECTOR_CST nodes. */
14955 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14956 if (!CONSTANT_CLASS_P (value))
14957 {
14958 constant_p = false;
14959 break;
14960 }
14961
14962 if (constant_p)
14963 {
14964 init = build_vector_from_ctor (type, elts);
14965 break;
14966 }
14967 }
14968 /* FALLTHRU */
14969
14970 default:
14971 return NULL;
14972 }
14973
14974 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14975
14976 /* If expand_expr returns a MEM, it wasn't immediate. */
14977 gcc_assert (!rtl || !MEM_P (rtl));
14978 }
14979
14980 return rtl;
14981 }
14982
14983 /* Generate RTL for the variable DECL to represent its location. */
14984
14985 static rtx
14986 rtl_for_decl_location (tree decl)
14987 {
14988 rtx rtl;
14989
14990 /* Here we have to decide where we are going to say the parameter "lives"
14991 (as far as the debugger is concerned). We only have a couple of
14992 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14993
14994 DECL_RTL normally indicates where the parameter lives during most of the
14995 activation of the function. If optimization is enabled however, this
14996 could be either NULL or else a pseudo-reg. Both of those cases indicate
14997 that the parameter doesn't really live anywhere (as far as the code
14998 generation parts of GCC are concerned) during most of the function's
14999 activation. That will happen (for example) if the parameter is never
15000 referenced within the function.
15001
15002 We could just generate a location descriptor here for all non-NULL
15003 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15004 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15005 where DECL_RTL is NULL or is a pseudo-reg.
15006
15007 Note however that we can only get away with using DECL_INCOMING_RTL as
15008 a backup substitute for DECL_RTL in certain limited cases. In cases
15009 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15010 we can be sure that the parameter was passed using the same type as it is
15011 declared to have within the function, and that its DECL_INCOMING_RTL
15012 points us to a place where a value of that type is passed.
15013
15014 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15015 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15016 because in these cases DECL_INCOMING_RTL points us to a value of some
15017 type which is *different* from the type of the parameter itself. Thus,
15018 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15019 such cases, the debugger would end up (for example) trying to fetch a
15020 `float' from a place which actually contains the first part of a
15021 `double'. That would lead to really incorrect and confusing
15022 output at debug-time.
15023
15024 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15025 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15026 are a couple of exceptions however. On little-endian machines we can
15027 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15028 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15029 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15030 when (on a little-endian machine) a non-prototyped function has a
15031 parameter declared to be of type `short' or `char'. In such cases,
15032 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15033 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15034 passed `int' value. If the debugger then uses that address to fetch
15035 a `short' or a `char' (on a little-endian machine) the result will be
15036 the correct data, so we allow for such exceptional cases below.
15037
15038 Note that our goal here is to describe the place where the given formal
15039 parameter lives during most of the function's activation (i.e. between the
15040 end of the prologue and the start of the epilogue). We'll do that as best
15041 as we can. Note however that if the given formal parameter is modified
15042 sometime during the execution of the function, then a stack backtrace (at
15043 debug-time) will show the function as having been called with the *new*
15044 value rather than the value which was originally passed in. This happens
15045 rarely enough that it is not a major problem, but it *is* a problem, and
15046 I'd like to fix it.
15047
15048 A future version of dwarf2out.c may generate two additional attributes for
15049 any given DW_TAG_formal_parameter DIE which will describe the "passed
15050 type" and the "passed location" for the given formal parameter in addition
15051 to the attributes we now generate to indicate the "declared type" and the
15052 "active location" for each parameter. This additional set of attributes
15053 could be used by debuggers for stack backtraces. Separately, note that
15054 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15055 This happens (for example) for inlined-instances of inline function formal
15056 parameters which are never referenced. This really shouldn't be
15057 happening. All PARM_DECL nodes should get valid non-NULL
15058 DECL_INCOMING_RTL values. FIXME. */
15059
15060 /* Use DECL_RTL as the "location" unless we find something better. */
15061 rtl = DECL_RTL_IF_SET (decl);
15062
15063 /* When generating abstract instances, ignore everything except
15064 constants, symbols living in memory, and symbols living in
15065 fixed registers. */
15066 if (! reload_completed)
15067 {
15068 if (rtl
15069 && (CONSTANT_P (rtl)
15070 || (MEM_P (rtl)
15071 && CONSTANT_P (XEXP (rtl, 0)))
15072 || (REG_P (rtl)
15073 && TREE_CODE (decl) == VAR_DECL
15074 && TREE_STATIC (decl))))
15075 {
15076 rtl = targetm.delegitimize_address (rtl);
15077 return rtl;
15078 }
15079 rtl = NULL_RTX;
15080 }
15081 else if (TREE_CODE (decl) == PARM_DECL)
15082 {
15083 if (rtl == NULL_RTX
15084 || is_pseudo_reg (rtl)
15085 || (MEM_P (rtl)
15086 && is_pseudo_reg (XEXP (rtl, 0))
15087 && DECL_INCOMING_RTL (decl)
15088 && MEM_P (DECL_INCOMING_RTL (decl))
15089 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15090 {
15091 tree declared_type = TREE_TYPE (decl);
15092 tree passed_type = DECL_ARG_TYPE (decl);
15093 enum machine_mode dmode = TYPE_MODE (declared_type);
15094 enum machine_mode pmode = TYPE_MODE (passed_type);
15095
15096 /* This decl represents a formal parameter which was optimized out.
15097 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15098 all cases where (rtl == NULL_RTX) just below. */
15099 if (dmode == pmode)
15100 rtl = DECL_INCOMING_RTL (decl);
15101 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15102 && SCALAR_INT_MODE_P (dmode)
15103 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15104 && DECL_INCOMING_RTL (decl))
15105 {
15106 rtx inc = DECL_INCOMING_RTL (decl);
15107 if (REG_P (inc))
15108 rtl = inc;
15109 else if (MEM_P (inc))
15110 {
15111 if (BYTES_BIG_ENDIAN)
15112 rtl = adjust_address_nv (inc, dmode,
15113 GET_MODE_SIZE (pmode)
15114 - GET_MODE_SIZE (dmode));
15115 else
15116 rtl = inc;
15117 }
15118 }
15119 }
15120
15121 /* If the parm was passed in registers, but lives on the stack, then
15122 make a big endian correction if the mode of the type of the
15123 parameter is not the same as the mode of the rtl. */
15124 /* ??? This is the same series of checks that are made in dbxout.c before
15125 we reach the big endian correction code there. It isn't clear if all
15126 of these checks are necessary here, but keeping them all is the safe
15127 thing to do. */
15128 else if (MEM_P (rtl)
15129 && XEXP (rtl, 0) != const0_rtx
15130 && ! CONSTANT_P (XEXP (rtl, 0))
15131 /* Not passed in memory. */
15132 && !MEM_P (DECL_INCOMING_RTL (decl))
15133 /* Not passed by invisible reference. */
15134 && (!REG_P (XEXP (rtl, 0))
15135 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15136 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15137 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15138 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15139 #endif
15140 )
15141 /* Big endian correction check. */
15142 && BYTES_BIG_ENDIAN
15143 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15144 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15145 < UNITS_PER_WORD))
15146 {
15147 enum machine_mode addr_mode = get_address_mode (rtl);
15148 int offset = (UNITS_PER_WORD
15149 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15150
15151 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15152 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15153 }
15154 }
15155 else if (TREE_CODE (decl) == VAR_DECL
15156 && rtl
15157 && MEM_P (rtl)
15158 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15159 && BYTES_BIG_ENDIAN)
15160 {
15161 enum machine_mode addr_mode = get_address_mode (rtl);
15162 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15163 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15164
15165 /* If a variable is declared "register" yet is smaller than
15166 a register, then if we store the variable to memory, it
15167 looks like we're storing a register-sized value, when in
15168 fact we are not. We need to adjust the offset of the
15169 storage location to reflect the actual value's bytes,
15170 else gdb will not be able to display it. */
15171 if (rsize > dsize)
15172 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15173 plus_constant (addr_mode, XEXP (rtl, 0),
15174 rsize - dsize));
15175 }
15176
15177 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15178 and will have been substituted directly into all expressions that use it.
15179 C does not have such a concept, but C++ and other languages do. */
15180 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15181 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15182
15183 if (rtl)
15184 rtl = targetm.delegitimize_address (rtl);
15185
15186 /* If we don't look past the constant pool, we risk emitting a
15187 reference to a constant pool entry that isn't referenced from
15188 code, and thus is not emitted. */
15189 if (rtl)
15190 rtl = avoid_constant_pool_reference (rtl);
15191
15192 /* Try harder to get a rtl. If this symbol ends up not being emitted
15193 in the current CU, resolve_addr will remove the expression referencing
15194 it. */
15195 if (rtl == NULL_RTX
15196 && TREE_CODE (decl) == VAR_DECL
15197 && !DECL_EXTERNAL (decl)
15198 && TREE_STATIC (decl)
15199 && DECL_NAME (decl)
15200 && !DECL_HARD_REGISTER (decl)
15201 && DECL_MODE (decl) != VOIDmode)
15202 {
15203 rtl = make_decl_rtl_for_debug (decl);
15204 if (!MEM_P (rtl)
15205 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15206 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15207 rtl = NULL_RTX;
15208 }
15209
15210 return rtl;
15211 }
15212
15213 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15214 returned. If so, the decl for the COMMON block is returned, and the
15215 value is the offset into the common block for the symbol. */
15216
15217 static tree
15218 fortran_common (tree decl, HOST_WIDE_INT *value)
15219 {
15220 tree val_expr, cvar;
15221 enum machine_mode mode;
15222 HOST_WIDE_INT bitsize, bitpos;
15223 tree offset;
15224 int unsignedp, volatilep = 0;
15225
15226 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15227 it does not have a value (the offset into the common area), or if it
15228 is thread local (as opposed to global) then it isn't common, and shouldn't
15229 be handled as such. */
15230 if (TREE_CODE (decl) != VAR_DECL
15231 || !TREE_STATIC (decl)
15232 || !DECL_HAS_VALUE_EXPR_P (decl)
15233 || !is_fortran ())
15234 return NULL_TREE;
15235
15236 val_expr = DECL_VALUE_EXPR (decl);
15237 if (TREE_CODE (val_expr) != COMPONENT_REF)
15238 return NULL_TREE;
15239
15240 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15241 &mode, &unsignedp, &volatilep, true);
15242
15243 if (cvar == NULL_TREE
15244 || TREE_CODE (cvar) != VAR_DECL
15245 || DECL_ARTIFICIAL (cvar)
15246 || !TREE_PUBLIC (cvar))
15247 return NULL_TREE;
15248
15249 *value = 0;
15250 if (offset != NULL)
15251 {
15252 if (!host_integerp (offset, 0))
15253 return NULL_TREE;
15254 *value = tree_low_cst (offset, 0);
15255 }
15256 if (bitpos != 0)
15257 *value += bitpos / BITS_PER_UNIT;
15258
15259 return cvar;
15260 }
15261
15262 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15263 data attribute for a variable or a parameter. We generate the
15264 DW_AT_const_value attribute only in those cases where the given variable
15265 or parameter does not have a true "location" either in memory or in a
15266 register. This can happen (for example) when a constant is passed as an
15267 actual argument in a call to an inline function. (It's possible that
15268 these things can crop up in other ways also.) Note that one type of
15269 constant value which can be passed into an inlined function is a constant
15270 pointer. This can happen for example if an actual argument in an inlined
15271 function call evaluates to a compile-time constant address.
15272
15273 CACHE_P is true if it is worth caching the location list for DECL,
15274 so that future calls can reuse it rather than regenerate it from scratch.
15275 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15276 since we will need to refer to them each time the function is inlined. */
15277
15278 static bool
15279 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15280 enum dwarf_attribute attr)
15281 {
15282 rtx rtl;
15283 dw_loc_list_ref list;
15284 var_loc_list *loc_list;
15285 cached_dw_loc_list *cache;
15286 void **slot;
15287
15288 if (TREE_CODE (decl) == ERROR_MARK)
15289 return false;
15290
15291 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15292 || TREE_CODE (decl) == RESULT_DECL);
15293
15294 /* Try to get some constant RTL for this decl, and use that as the value of
15295 the location. */
15296
15297 rtl = rtl_for_decl_location (decl);
15298 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15299 && add_const_value_attribute (die, rtl))
15300 return true;
15301
15302 /* See if we have single element location list that is equivalent to
15303 a constant value. That way we are better to use add_const_value_attribute
15304 rather than expanding constant value equivalent. */
15305 loc_list = lookup_decl_loc (decl);
15306 if (loc_list
15307 && loc_list->first
15308 && loc_list->first->next == NULL
15309 && NOTE_P (loc_list->first->loc)
15310 && NOTE_VAR_LOCATION (loc_list->first->loc)
15311 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15312 {
15313 struct var_loc_node *node;
15314
15315 node = loc_list->first;
15316 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15317 if (GET_CODE (rtl) == EXPR_LIST)
15318 rtl = XEXP (rtl, 0);
15319 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15320 && add_const_value_attribute (die, rtl))
15321 return true;
15322 }
15323 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15324 list several times. See if we've already cached the contents. */
15325 list = NULL;
15326 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15327 cache_p = false;
15328 if (cache_p)
15329 {
15330 cache = (cached_dw_loc_list *)
15331 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15332 if (cache)
15333 list = cache->loc_list;
15334 }
15335 if (list == NULL)
15336 {
15337 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15338 /* It is usually worth caching this result if the decl is from
15339 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15340 if (cache_p && list && list->dw_loc_next)
15341 {
15342 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15343 DECL_UID (decl), INSERT);
15344 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15345 cache->decl_id = DECL_UID (decl);
15346 cache->loc_list = list;
15347 *slot = cache;
15348 }
15349 }
15350 if (list)
15351 {
15352 add_AT_location_description (die, attr, list);
15353 return true;
15354 }
15355 /* None of that worked, so it must not really have a location;
15356 try adding a constant value attribute from the DECL_INITIAL. */
15357 return tree_add_const_value_attribute_for_decl (die, decl);
15358 }
15359
15360 /* Add VARIABLE and DIE into deferred locations list. */
15361
15362 static void
15363 defer_location (tree variable, dw_die_ref die)
15364 {
15365 deferred_locations entry;
15366 entry.variable = variable;
15367 entry.die = die;
15368 vec_safe_push (deferred_locations_list, entry);
15369 }
15370
15371 /* Helper function for tree_add_const_value_attribute. Natively encode
15372 initializer INIT into an array. Return true if successful. */
15373
15374 static bool
15375 native_encode_initializer (tree init, unsigned char *array, int size)
15376 {
15377 tree type;
15378
15379 if (init == NULL_TREE)
15380 return false;
15381
15382 STRIP_NOPS (init);
15383 switch (TREE_CODE (init))
15384 {
15385 case STRING_CST:
15386 type = TREE_TYPE (init);
15387 if (TREE_CODE (type) == ARRAY_TYPE)
15388 {
15389 tree enttype = TREE_TYPE (type);
15390 enum machine_mode mode = TYPE_MODE (enttype);
15391
15392 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15393 return false;
15394 if (int_size_in_bytes (type) != size)
15395 return false;
15396 if (size > TREE_STRING_LENGTH (init))
15397 {
15398 memcpy (array, TREE_STRING_POINTER (init),
15399 TREE_STRING_LENGTH (init));
15400 memset (array + TREE_STRING_LENGTH (init),
15401 '\0', size - TREE_STRING_LENGTH (init));
15402 }
15403 else
15404 memcpy (array, TREE_STRING_POINTER (init), size);
15405 return true;
15406 }
15407 return false;
15408 case CONSTRUCTOR:
15409 type = TREE_TYPE (init);
15410 if (int_size_in_bytes (type) != size)
15411 return false;
15412 if (TREE_CODE (type) == ARRAY_TYPE)
15413 {
15414 HOST_WIDE_INT min_index;
15415 unsigned HOST_WIDE_INT cnt;
15416 int curpos = 0, fieldsize;
15417 constructor_elt *ce;
15418
15419 if (TYPE_DOMAIN (type) == NULL_TREE
15420 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15421 return false;
15422
15423 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15424 if (fieldsize <= 0)
15425 return false;
15426
15427 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15428 memset (array, '\0', size);
15429 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15430 {
15431 tree val = ce->value;
15432 tree index = ce->index;
15433 int pos = curpos;
15434 if (index && TREE_CODE (index) == RANGE_EXPR)
15435 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15436 * fieldsize;
15437 else if (index)
15438 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15439
15440 if (val)
15441 {
15442 STRIP_NOPS (val);
15443 if (!native_encode_initializer (val, array + pos, fieldsize))
15444 return false;
15445 }
15446 curpos = pos + fieldsize;
15447 if (index && TREE_CODE (index) == RANGE_EXPR)
15448 {
15449 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15450 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15451 while (count-- > 0)
15452 {
15453 if (val)
15454 memcpy (array + curpos, array + pos, fieldsize);
15455 curpos += fieldsize;
15456 }
15457 }
15458 gcc_assert (curpos <= size);
15459 }
15460 return true;
15461 }
15462 else if (TREE_CODE (type) == RECORD_TYPE
15463 || TREE_CODE (type) == UNION_TYPE)
15464 {
15465 tree field = NULL_TREE;
15466 unsigned HOST_WIDE_INT cnt;
15467 constructor_elt *ce;
15468
15469 if (int_size_in_bytes (type) != size)
15470 return false;
15471
15472 if (TREE_CODE (type) == RECORD_TYPE)
15473 field = TYPE_FIELDS (type);
15474
15475 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15476 {
15477 tree val = ce->value;
15478 int pos, fieldsize;
15479
15480 if (ce->index != 0)
15481 field = ce->index;
15482
15483 if (val)
15484 STRIP_NOPS (val);
15485
15486 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15487 return false;
15488
15489 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15490 && TYPE_DOMAIN (TREE_TYPE (field))
15491 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15492 return false;
15493 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15494 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15495 return false;
15496 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15497 pos = int_byte_position (field);
15498 gcc_assert (pos + fieldsize <= size);
15499 if (val
15500 && !native_encode_initializer (val, array + pos, fieldsize))
15501 return false;
15502 }
15503 return true;
15504 }
15505 return false;
15506 case VIEW_CONVERT_EXPR:
15507 case NON_LVALUE_EXPR:
15508 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15509 default:
15510 return native_encode_expr (init, array, size) == size;
15511 }
15512 }
15513
15514 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15515 attribute is the const value T. */
15516
15517 static bool
15518 tree_add_const_value_attribute (dw_die_ref die, tree t)
15519 {
15520 tree init;
15521 tree type = TREE_TYPE (t);
15522 rtx rtl;
15523
15524 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15525 return false;
15526
15527 init = t;
15528 gcc_assert (!DECL_P (init));
15529
15530 rtl = rtl_for_decl_init (init, type);
15531 if (rtl)
15532 return add_const_value_attribute (die, rtl);
15533 /* If the host and target are sane, try harder. */
15534 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15535 && initializer_constant_valid_p (init, type))
15536 {
15537 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15538 if (size > 0 && (int) size == size)
15539 {
15540 unsigned char *array = (unsigned char *)
15541 ggc_alloc_cleared_atomic (size);
15542
15543 if (native_encode_initializer (init, array, size))
15544 {
15545 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15546 return true;
15547 }
15548 ggc_free (array);
15549 }
15550 }
15551 return false;
15552 }
15553
15554 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15555 attribute is the const value of T, where T is an integral constant
15556 variable with static storage duration
15557 (so it can't be a PARM_DECL or a RESULT_DECL). */
15558
15559 static bool
15560 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15561 {
15562
15563 if (!decl
15564 || (TREE_CODE (decl) != VAR_DECL
15565 && TREE_CODE (decl) != CONST_DECL)
15566 || (TREE_CODE (decl) == VAR_DECL
15567 && !TREE_STATIC (decl)))
15568 return false;
15569
15570 if (TREE_READONLY (decl)
15571 && ! TREE_THIS_VOLATILE (decl)
15572 && DECL_INITIAL (decl))
15573 /* OK */;
15574 else
15575 return false;
15576
15577 /* Don't add DW_AT_const_value if abstract origin already has one. */
15578 if (get_AT (var_die, DW_AT_const_value))
15579 return false;
15580
15581 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15582 }
15583
15584 /* Convert the CFI instructions for the current function into a
15585 location list. This is used for DW_AT_frame_base when we targeting
15586 a dwarf2 consumer that does not support the dwarf3
15587 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15588 expressions. */
15589
15590 static dw_loc_list_ref
15591 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15592 {
15593 int ix;
15594 dw_fde_ref fde;
15595 dw_loc_list_ref list, *list_tail;
15596 dw_cfi_ref cfi;
15597 dw_cfa_location last_cfa, next_cfa;
15598 const char *start_label, *last_label, *section;
15599 dw_cfa_location remember;
15600
15601 fde = cfun->fde;
15602 gcc_assert (fde != NULL);
15603
15604 section = secname_for_decl (current_function_decl);
15605 list_tail = &list;
15606 list = NULL;
15607
15608 memset (&next_cfa, 0, sizeof (next_cfa));
15609 next_cfa.reg = INVALID_REGNUM;
15610 remember = next_cfa;
15611
15612 start_label = fde->dw_fde_begin;
15613
15614 /* ??? Bald assumption that the CIE opcode list does not contain
15615 advance opcodes. */
15616 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15617 lookup_cfa_1 (cfi, &next_cfa, &remember);
15618
15619 last_cfa = next_cfa;
15620 last_label = start_label;
15621
15622 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15623 {
15624 /* If the first partition contained no CFI adjustments, the
15625 CIE opcodes apply to the whole first partition. */
15626 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15627 fde->dw_fde_begin, fde->dw_fde_end, section);
15628 list_tail =&(*list_tail)->dw_loc_next;
15629 start_label = last_label = fde->dw_fde_second_begin;
15630 }
15631
15632 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15633 {
15634 switch (cfi->dw_cfi_opc)
15635 {
15636 case DW_CFA_set_loc:
15637 case DW_CFA_advance_loc1:
15638 case DW_CFA_advance_loc2:
15639 case DW_CFA_advance_loc4:
15640 if (!cfa_equal_p (&last_cfa, &next_cfa))
15641 {
15642 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15643 start_label, last_label, section);
15644
15645 list_tail = &(*list_tail)->dw_loc_next;
15646 last_cfa = next_cfa;
15647 start_label = last_label;
15648 }
15649 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15650 break;
15651
15652 case DW_CFA_advance_loc:
15653 /* The encoding is complex enough that we should never emit this. */
15654 gcc_unreachable ();
15655
15656 default:
15657 lookup_cfa_1 (cfi, &next_cfa, &remember);
15658 break;
15659 }
15660 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15661 {
15662 if (!cfa_equal_p (&last_cfa, &next_cfa))
15663 {
15664 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15665 start_label, last_label, section);
15666
15667 list_tail = &(*list_tail)->dw_loc_next;
15668 last_cfa = next_cfa;
15669 start_label = last_label;
15670 }
15671 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15672 start_label, fde->dw_fde_end, section);
15673 list_tail = &(*list_tail)->dw_loc_next;
15674 start_label = last_label = fde->dw_fde_second_begin;
15675 }
15676 }
15677
15678 if (!cfa_equal_p (&last_cfa, &next_cfa))
15679 {
15680 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15681 start_label, last_label, section);
15682 list_tail = &(*list_tail)->dw_loc_next;
15683 start_label = last_label;
15684 }
15685
15686 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15687 start_label,
15688 fde->dw_fde_second_begin
15689 ? fde->dw_fde_second_end : fde->dw_fde_end,
15690 section);
15691
15692 if (list && list->dw_loc_next)
15693 gen_llsym (list);
15694
15695 return list;
15696 }
15697
15698 /* Compute a displacement from the "steady-state frame pointer" to the
15699 frame base (often the same as the CFA), and store it in
15700 frame_pointer_fb_offset. OFFSET is added to the displacement
15701 before the latter is negated. */
15702
15703 static void
15704 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15705 {
15706 rtx reg, elim;
15707
15708 #ifdef FRAME_POINTER_CFA_OFFSET
15709 reg = frame_pointer_rtx;
15710 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15711 #else
15712 reg = arg_pointer_rtx;
15713 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15714 #endif
15715
15716 elim = (ira_use_lra_p
15717 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15718 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15719 if (GET_CODE (elim) == PLUS)
15720 {
15721 offset += INTVAL (XEXP (elim, 1));
15722 elim = XEXP (elim, 0);
15723 }
15724
15725 frame_pointer_fb_offset = -offset;
15726
15727 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15728 in which to eliminate. This is because it's stack pointer isn't
15729 directly accessible as a register within the ISA. To work around
15730 this, assume that while we cannot provide a proper value for
15731 frame_pointer_fb_offset, we won't need one either. */
15732 frame_pointer_fb_offset_valid
15733 = ((SUPPORTS_STACK_ALIGNMENT
15734 && (elim == hard_frame_pointer_rtx
15735 || elim == stack_pointer_rtx))
15736 || elim == (frame_pointer_needed
15737 ? hard_frame_pointer_rtx
15738 : stack_pointer_rtx));
15739 }
15740
15741 /* Generate a DW_AT_name attribute given some string value to be included as
15742 the value of the attribute. */
15743
15744 static void
15745 add_name_attribute (dw_die_ref die, const char *name_string)
15746 {
15747 if (name_string != NULL && *name_string != 0)
15748 {
15749 if (demangle_name_func)
15750 name_string = (*demangle_name_func) (name_string);
15751
15752 add_AT_string (die, DW_AT_name, name_string);
15753 }
15754 }
15755
15756 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15757 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15758 of TYPE accordingly.
15759
15760 ??? This is a temporary measure until after we're able to generate
15761 regular DWARF for the complex Ada type system. */
15762
15763 static void
15764 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15765 dw_die_ref context_die)
15766 {
15767 tree dtype;
15768 dw_die_ref dtype_die;
15769
15770 if (!lang_hooks.types.descriptive_type)
15771 return;
15772
15773 dtype = lang_hooks.types.descriptive_type (type);
15774 if (!dtype)
15775 return;
15776
15777 dtype_die = lookup_type_die (dtype);
15778 if (!dtype_die)
15779 {
15780 gen_type_die (dtype, context_die);
15781 dtype_die = lookup_type_die (dtype);
15782 gcc_assert (dtype_die);
15783 }
15784
15785 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15786 }
15787
15788 /* Generate a DW_AT_comp_dir attribute for DIE. */
15789
15790 static void
15791 add_comp_dir_attribute (dw_die_ref die)
15792 {
15793 const char *wd = get_src_pwd ();
15794 char *wd1;
15795
15796 if (wd == NULL)
15797 return;
15798
15799 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15800 {
15801 int wdlen;
15802
15803 wdlen = strlen (wd);
15804 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15805 strcpy (wd1, wd);
15806 wd1 [wdlen] = DIR_SEPARATOR;
15807 wd1 [wdlen + 1] = 0;
15808 wd = wd1;
15809 }
15810
15811 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15812 }
15813
15814 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15815 default. */
15816
15817 static int
15818 lower_bound_default (void)
15819 {
15820 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15821 {
15822 case DW_LANG_C:
15823 case DW_LANG_C89:
15824 case DW_LANG_C99:
15825 case DW_LANG_C_plus_plus:
15826 case DW_LANG_ObjC:
15827 case DW_LANG_ObjC_plus_plus:
15828 case DW_LANG_Java:
15829 return 0;
15830 case DW_LANG_Fortran77:
15831 case DW_LANG_Fortran90:
15832 case DW_LANG_Fortran95:
15833 return 1;
15834 case DW_LANG_UPC:
15835 case DW_LANG_D:
15836 case DW_LANG_Python:
15837 return dwarf_version >= 4 ? 0 : -1;
15838 case DW_LANG_Ada95:
15839 case DW_LANG_Ada83:
15840 case DW_LANG_Cobol74:
15841 case DW_LANG_Cobol85:
15842 case DW_LANG_Pascal83:
15843 case DW_LANG_Modula2:
15844 case DW_LANG_PLI:
15845 return dwarf_version >= 4 ? 1 : -1;
15846 default:
15847 return -1;
15848 }
15849 }
15850
15851 /* Given a tree node describing an array bound (either lower or upper) output
15852 a representation for that bound. */
15853
15854 static void
15855 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15856 {
15857 switch (TREE_CODE (bound))
15858 {
15859 case ERROR_MARK:
15860 return;
15861
15862 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15863 case INTEGER_CST:
15864 {
15865 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15866 int dflt;
15867
15868 /* Use the default if possible. */
15869 if (bound_attr == DW_AT_lower_bound
15870 && host_integerp (bound, 0)
15871 && (dflt = lower_bound_default ()) != -1
15872 && tree_low_cst (bound, 0) == dflt)
15873 ;
15874
15875 /* Otherwise represent the bound as an unsigned value with the
15876 precision of its type. The precision and signedness of the
15877 type will be necessary to re-interpret it unambiguously. */
15878 else if (prec < HOST_BITS_PER_WIDE_INT)
15879 {
15880 unsigned HOST_WIDE_INT mask
15881 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15882 add_AT_unsigned (subrange_die, bound_attr,
15883 TREE_INT_CST_LOW (bound) & mask);
15884 }
15885 else if (prec == HOST_BITS_PER_WIDE_INT
15886 || TREE_INT_CST_HIGH (bound) == 0)
15887 add_AT_unsigned (subrange_die, bound_attr,
15888 TREE_INT_CST_LOW (bound));
15889 else
15890 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15891 TREE_INT_CST_LOW (bound));
15892 }
15893 break;
15894
15895 CASE_CONVERT:
15896 case VIEW_CONVERT_EXPR:
15897 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15898 break;
15899
15900 case SAVE_EXPR:
15901 break;
15902
15903 case VAR_DECL:
15904 case PARM_DECL:
15905 case RESULT_DECL:
15906 {
15907 dw_die_ref decl_die = lookup_decl_die (bound);
15908
15909 /* ??? Can this happen, or should the variable have been bound
15910 first? Probably it can, since I imagine that we try to create
15911 the types of parameters in the order in which they exist in
15912 the list, and won't have created a forward reference to a
15913 later parameter. */
15914 if (decl_die != NULL)
15915 {
15916 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15917 break;
15918 }
15919 }
15920 /* FALLTHRU */
15921
15922 default:
15923 {
15924 /* Otherwise try to create a stack operation procedure to
15925 evaluate the value of the array bound. */
15926
15927 dw_die_ref ctx, decl_die;
15928 dw_loc_list_ref list;
15929
15930 list = loc_list_from_tree (bound, 2);
15931 if (list == NULL || single_element_loc_list_p (list))
15932 {
15933 /* If DW_AT_*bound is not a reference nor constant, it is
15934 a DWARF expression rather than location description.
15935 For that loc_list_from_tree (bound, 0) is needed.
15936 If that fails to give a single element list,
15937 fall back to outputting this as a reference anyway. */
15938 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15939 if (list2 && single_element_loc_list_p (list2))
15940 {
15941 add_AT_loc (subrange_die, bound_attr, list2->expr);
15942 break;
15943 }
15944 }
15945 if (list == NULL)
15946 break;
15947
15948 if (current_function_decl == 0)
15949 ctx = comp_unit_die ();
15950 else
15951 ctx = lookup_decl_die (current_function_decl);
15952
15953 decl_die = new_die (DW_TAG_variable, ctx, bound);
15954 add_AT_flag (decl_die, DW_AT_artificial, 1);
15955 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15956 add_AT_location_description (decl_die, DW_AT_location, list);
15957 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15958 break;
15959 }
15960 }
15961 }
15962
15963 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15964 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15965 Note that the block of subscript information for an array type also
15966 includes information about the element type of the given array type. */
15967
15968 static void
15969 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15970 {
15971 unsigned dimension_number;
15972 tree lower, upper;
15973 dw_die_ref subrange_die;
15974
15975 for (dimension_number = 0;
15976 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15977 type = TREE_TYPE (type), dimension_number++)
15978 {
15979 tree domain = TYPE_DOMAIN (type);
15980
15981 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15982 break;
15983
15984 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15985 and (in GNU C only) variable bounds. Handle all three forms
15986 here. */
15987 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15988 if (domain)
15989 {
15990 /* We have an array type with specified bounds. */
15991 lower = TYPE_MIN_VALUE (domain);
15992 upper = TYPE_MAX_VALUE (domain);
15993
15994 /* Define the index type. */
15995 if (TREE_TYPE (domain))
15996 {
15997 /* ??? This is probably an Ada unnamed subrange type. Ignore the
15998 TREE_TYPE field. We can't emit debug info for this
15999 because it is an unnamed integral type. */
16000 if (TREE_CODE (domain) == INTEGER_TYPE
16001 && TYPE_NAME (domain) == NULL_TREE
16002 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16003 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16004 ;
16005 else
16006 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16007 type_die);
16008 }
16009
16010 /* ??? If upper is NULL, the array has unspecified length,
16011 but it does have a lower bound. This happens with Fortran
16012 dimension arr(N:*)
16013 Since the debugger is definitely going to need to know N
16014 to produce useful results, go ahead and output the lower
16015 bound solo, and hope the debugger can cope. */
16016
16017 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16018 if (upper)
16019 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16020 }
16021
16022 /* Otherwise we have an array type with an unspecified length. The
16023 DWARF-2 spec does not say how to handle this; let's just leave out the
16024 bounds. */
16025 }
16026 }
16027
16028 static void
16029 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16030 {
16031 dw_die_ref decl_die;
16032 unsigned size;
16033
16034 switch (TREE_CODE (tree_node))
16035 {
16036 case ERROR_MARK:
16037 size = 0;
16038 break;
16039 case ENUMERAL_TYPE:
16040 case RECORD_TYPE:
16041 case UNION_TYPE:
16042 case QUAL_UNION_TYPE:
16043 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16044 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16045 {
16046 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16047 return;
16048 }
16049 size = int_size_in_bytes (tree_node);
16050 break;
16051 case FIELD_DECL:
16052 /* For a data member of a struct or union, the DW_AT_byte_size is
16053 generally given as the number of bytes normally allocated for an
16054 object of the *declared* type of the member itself. This is true
16055 even for bit-fields. */
16056 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16057 break;
16058 default:
16059 gcc_unreachable ();
16060 }
16061
16062 /* Note that `size' might be -1 when we get to this point. If it is, that
16063 indicates that the byte size of the entity in question is variable. We
16064 have no good way of expressing this fact in Dwarf at the present time,
16065 so just let the -1 pass on through. */
16066 add_AT_unsigned (die, DW_AT_byte_size, size);
16067 }
16068
16069 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16070 which specifies the distance in bits from the highest order bit of the
16071 "containing object" for the bit-field to the highest order bit of the
16072 bit-field itself.
16073
16074 For any given bit-field, the "containing object" is a hypothetical object
16075 (of some integral or enum type) within which the given bit-field lives. The
16076 type of this hypothetical "containing object" is always the same as the
16077 declared type of the individual bit-field itself. The determination of the
16078 exact location of the "containing object" for a bit-field is rather
16079 complicated. It's handled by the `field_byte_offset' function (above).
16080
16081 Note that it is the size (in bytes) of the hypothetical "containing object"
16082 which will be given in the DW_AT_byte_size attribute for this bit-field.
16083 (See `byte_size_attribute' above). */
16084
16085 static inline void
16086 add_bit_offset_attribute (dw_die_ref die, tree decl)
16087 {
16088 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16089 tree type = DECL_BIT_FIELD_TYPE (decl);
16090 HOST_WIDE_INT bitpos_int;
16091 HOST_WIDE_INT highest_order_object_bit_offset;
16092 HOST_WIDE_INT highest_order_field_bit_offset;
16093 HOST_WIDE_INT bit_offset;
16094
16095 /* Must be a field and a bit field. */
16096 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16097
16098 /* We can't yet handle bit-fields whose offsets are variable, so if we
16099 encounter such things, just return without generating any attribute
16100 whatsoever. Likewise for variable or too large size. */
16101 if (! host_integerp (bit_position (decl), 0)
16102 || ! host_integerp (DECL_SIZE (decl), 1))
16103 return;
16104
16105 bitpos_int = int_bit_position (decl);
16106
16107 /* Note that the bit offset is always the distance (in bits) from the
16108 highest-order bit of the "containing object" to the highest-order bit of
16109 the bit-field itself. Since the "high-order end" of any object or field
16110 is different on big-endian and little-endian machines, the computation
16111 below must take account of these differences. */
16112 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16113 highest_order_field_bit_offset = bitpos_int;
16114
16115 if (! BYTES_BIG_ENDIAN)
16116 {
16117 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16118 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16119 }
16120
16121 bit_offset
16122 = (! BYTES_BIG_ENDIAN
16123 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16124 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16125
16126 if (bit_offset < 0)
16127 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16128 else
16129 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16130 }
16131
16132 /* For a FIELD_DECL node which represents a bit field, output an attribute
16133 which specifies the length in bits of the given field. */
16134
16135 static inline void
16136 add_bit_size_attribute (dw_die_ref die, tree decl)
16137 {
16138 /* Must be a field and a bit field. */
16139 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16140 && DECL_BIT_FIELD_TYPE (decl));
16141
16142 if (host_integerp (DECL_SIZE (decl), 1))
16143 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16144 }
16145
16146 /* If the compiled language is ANSI C, then add a 'prototyped'
16147 attribute, if arg types are given for the parameters of a function. */
16148
16149 static inline void
16150 add_prototyped_attribute (dw_die_ref die, tree func_type)
16151 {
16152 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16153 && prototype_p (func_type))
16154 add_AT_flag (die, DW_AT_prototyped, 1);
16155 }
16156
16157 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16158 by looking in either the type declaration or object declaration
16159 equate table. */
16160
16161 static inline dw_die_ref
16162 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16163 {
16164 dw_die_ref origin_die = NULL;
16165
16166 if (TREE_CODE (origin) != FUNCTION_DECL)
16167 {
16168 /* We may have gotten separated from the block for the inlined
16169 function, if we're in an exception handler or some such; make
16170 sure that the abstract function has been written out.
16171
16172 Doing this for nested functions is wrong, however; functions are
16173 distinct units, and our context might not even be inline. */
16174 tree fn = origin;
16175
16176 if (TYPE_P (fn))
16177 fn = TYPE_STUB_DECL (fn);
16178
16179 fn = decl_function_context (fn);
16180 if (fn)
16181 dwarf2out_abstract_function (fn);
16182 }
16183
16184 if (DECL_P (origin))
16185 origin_die = lookup_decl_die (origin);
16186 else if (TYPE_P (origin))
16187 origin_die = lookup_type_die (origin);
16188
16189 /* XXX: Functions that are never lowered don't always have correct block
16190 trees (in the case of java, they simply have no block tree, in some other
16191 languages). For these functions, there is nothing we can really do to
16192 output correct debug info for inlined functions in all cases. Rather
16193 than die, we'll just produce deficient debug info now, in that we will
16194 have variables without a proper abstract origin. In the future, when all
16195 functions are lowered, we should re-add a gcc_assert (origin_die)
16196 here. */
16197
16198 if (origin_die)
16199 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16200 return origin_die;
16201 }
16202
16203 /* We do not currently support the pure_virtual attribute. */
16204
16205 static inline void
16206 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16207 {
16208 if (DECL_VINDEX (func_decl))
16209 {
16210 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16211
16212 if (host_integerp (DECL_VINDEX (func_decl), 0))
16213 add_AT_loc (die, DW_AT_vtable_elem_location,
16214 new_loc_descr (DW_OP_constu,
16215 tree_low_cst (DECL_VINDEX (func_decl), 0),
16216 0));
16217
16218 /* GNU extension: Record what type this method came from originally. */
16219 if (debug_info_level > DINFO_LEVEL_TERSE
16220 && DECL_CONTEXT (func_decl))
16221 add_AT_die_ref (die, DW_AT_containing_type,
16222 lookup_type_die (DECL_CONTEXT (func_decl)));
16223 }
16224 }
16225 \f
16226 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16227 given decl. This used to be a vendor extension until after DWARF 4
16228 standardized it. */
16229
16230 static void
16231 add_linkage_attr (dw_die_ref die, tree decl)
16232 {
16233 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16234
16235 /* Mimic what assemble_name_raw does with a leading '*'. */
16236 if (name[0] == '*')
16237 name = &name[1];
16238
16239 if (dwarf_version >= 4)
16240 add_AT_string (die, DW_AT_linkage_name, name);
16241 else
16242 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16243 }
16244
16245 /* Add source coordinate attributes for the given decl. */
16246
16247 static void
16248 add_src_coords_attributes (dw_die_ref die, tree decl)
16249 {
16250 expanded_location s;
16251
16252 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16253 return;
16254 s = expand_location (DECL_SOURCE_LOCATION (decl));
16255 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16256 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16257 }
16258
16259 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16260
16261 static void
16262 add_linkage_name (dw_die_ref die, tree decl)
16263 {
16264 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16265 && TREE_PUBLIC (decl)
16266 && !DECL_ABSTRACT (decl)
16267 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16268 && die->die_tag != DW_TAG_member)
16269 {
16270 /* Defer until we have an assembler name set. */
16271 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16272 {
16273 limbo_die_node *asm_name;
16274
16275 asm_name = ggc_alloc_cleared_limbo_die_node ();
16276 asm_name->die = die;
16277 asm_name->created_for = decl;
16278 asm_name->next = deferred_asm_name;
16279 deferred_asm_name = asm_name;
16280 }
16281 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16282 add_linkage_attr (die, decl);
16283 }
16284 }
16285
16286 /* Add a DW_AT_name attribute and source coordinate attribute for the
16287 given decl, but only if it actually has a name. */
16288
16289 static void
16290 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16291 {
16292 tree decl_name;
16293
16294 decl_name = DECL_NAME (decl);
16295 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16296 {
16297 const char *name = dwarf2_name (decl, 0);
16298 if (name)
16299 add_name_attribute (die, name);
16300 if (! DECL_ARTIFICIAL (decl))
16301 add_src_coords_attributes (die, decl);
16302
16303 add_linkage_name (die, decl);
16304 }
16305
16306 #ifdef VMS_DEBUGGING_INFO
16307 /* Get the function's name, as described by its RTL. This may be different
16308 from the DECL_NAME name used in the source file. */
16309 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16310 {
16311 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16312 XEXP (DECL_RTL (decl), 0), false);
16313 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16314 }
16315 #endif /* VMS_DEBUGGING_INFO */
16316 }
16317
16318 #ifdef VMS_DEBUGGING_INFO
16319 /* Output the debug main pointer die for VMS */
16320
16321 void
16322 dwarf2out_vms_debug_main_pointer (void)
16323 {
16324 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16325 dw_die_ref die;
16326
16327 /* Allocate the VMS debug main subprogram die. */
16328 die = ggc_alloc_cleared_die_node ();
16329 die->die_tag = DW_TAG_subprogram;
16330 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16331 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16332 current_function_funcdef_no);
16333 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16334
16335 /* Make it the first child of comp_unit_die (). */
16336 die->die_parent = comp_unit_die ();
16337 if (comp_unit_die ()->die_child)
16338 {
16339 die->die_sib = comp_unit_die ()->die_child->die_sib;
16340 comp_unit_die ()->die_child->die_sib = die;
16341 }
16342 else
16343 {
16344 die->die_sib = die;
16345 comp_unit_die ()->die_child = die;
16346 }
16347 }
16348 #endif /* VMS_DEBUGGING_INFO */
16349
16350 /* Push a new declaration scope. */
16351
16352 static void
16353 push_decl_scope (tree scope)
16354 {
16355 vec_safe_push (decl_scope_table, scope);
16356 }
16357
16358 /* Pop a declaration scope. */
16359
16360 static inline void
16361 pop_decl_scope (void)
16362 {
16363 decl_scope_table->pop ();
16364 }
16365
16366 /* walk_tree helper function for uses_local_type, below. */
16367
16368 static tree
16369 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16370 {
16371 if (!TYPE_P (*tp))
16372 *walk_subtrees = 0;
16373 else
16374 {
16375 tree name = TYPE_NAME (*tp);
16376 if (name && DECL_P (name) && decl_function_context (name))
16377 return *tp;
16378 }
16379 return NULL_TREE;
16380 }
16381
16382 /* If TYPE involves a function-local type (including a local typedef to a
16383 non-local type), returns that type; otherwise returns NULL_TREE. */
16384
16385 static tree
16386 uses_local_type (tree type)
16387 {
16388 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16389 return used;
16390 }
16391
16392 /* Return the DIE for the scope that immediately contains this type.
16393 Non-named types that do not involve a function-local type get global
16394 scope. Named types nested in namespaces or other types get their
16395 containing scope. All other types (i.e. function-local named types) get
16396 the current active scope. */
16397
16398 static dw_die_ref
16399 scope_die_for (tree t, dw_die_ref context_die)
16400 {
16401 dw_die_ref scope_die = NULL;
16402 tree containing_scope;
16403
16404 /* Non-types always go in the current scope. */
16405 gcc_assert (TYPE_P (t));
16406
16407 /* Use the scope of the typedef, rather than the scope of the type
16408 it refers to. */
16409 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16410 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16411 else
16412 containing_scope = TYPE_CONTEXT (t);
16413
16414 /* Use the containing namespace if there is one. */
16415 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16416 {
16417 if (context_die == lookup_decl_die (containing_scope))
16418 /* OK */;
16419 else if (debug_info_level > DINFO_LEVEL_TERSE)
16420 context_die = get_context_die (containing_scope);
16421 else
16422 containing_scope = NULL_TREE;
16423 }
16424
16425 /* Ignore function type "scopes" from the C frontend. They mean that
16426 a tagged type is local to a parmlist of a function declarator, but
16427 that isn't useful to DWARF. */
16428 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16429 containing_scope = NULL_TREE;
16430
16431 if (SCOPE_FILE_SCOPE_P (containing_scope))
16432 {
16433 /* If T uses a local type keep it local as well, to avoid references
16434 to function-local DIEs from outside the function. */
16435 if (current_function_decl && uses_local_type (t))
16436 scope_die = context_die;
16437 else
16438 scope_die = comp_unit_die ();
16439 }
16440 else if (TYPE_P (containing_scope))
16441 {
16442 /* For types, we can just look up the appropriate DIE. */
16443 if (debug_info_level > DINFO_LEVEL_TERSE)
16444 scope_die = get_context_die (containing_scope);
16445 else
16446 {
16447 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16448 if (scope_die == NULL)
16449 scope_die = comp_unit_die ();
16450 }
16451 }
16452 else
16453 scope_die = context_die;
16454
16455 return scope_die;
16456 }
16457
16458 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16459
16460 static inline int
16461 local_scope_p (dw_die_ref context_die)
16462 {
16463 for (; context_die; context_die = context_die->die_parent)
16464 if (context_die->die_tag == DW_TAG_inlined_subroutine
16465 || context_die->die_tag == DW_TAG_subprogram)
16466 return 1;
16467
16468 return 0;
16469 }
16470
16471 /* Returns nonzero if CONTEXT_DIE is a class. */
16472
16473 static inline int
16474 class_scope_p (dw_die_ref context_die)
16475 {
16476 return (context_die
16477 && (context_die->die_tag == DW_TAG_structure_type
16478 || context_die->die_tag == DW_TAG_class_type
16479 || context_die->die_tag == DW_TAG_interface_type
16480 || context_die->die_tag == DW_TAG_union_type));
16481 }
16482
16483 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16484 whether or not to treat a DIE in this context as a declaration. */
16485
16486 static inline int
16487 class_or_namespace_scope_p (dw_die_ref context_die)
16488 {
16489 return (class_scope_p (context_die)
16490 || (context_die && context_die->die_tag == DW_TAG_namespace));
16491 }
16492
16493 /* Many forms of DIEs require a "type description" attribute. This
16494 routine locates the proper "type descriptor" die for the type given
16495 by 'type', and adds a DW_AT_type attribute below the given die. */
16496
16497 static void
16498 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16499 int decl_volatile, dw_die_ref context_die)
16500 {
16501 enum tree_code code = TREE_CODE (type);
16502 dw_die_ref type_die = NULL;
16503
16504 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16505 or fixed-point type, use the inner type. This is because we have no
16506 support for unnamed types in base_type_die. This can happen if this is
16507 an Ada subrange type. Correct solution is emit a subrange type die. */
16508 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16509 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16510 type = TREE_TYPE (type), code = TREE_CODE (type);
16511
16512 if (code == ERROR_MARK
16513 /* Handle a special case. For functions whose return type is void, we
16514 generate *no* type attribute. (Note that no object may have type
16515 `void', so this only applies to function return types). */
16516 || code == VOID_TYPE)
16517 return;
16518
16519 type_die = modified_type_die (type,
16520 decl_const || TYPE_READONLY (type),
16521 decl_volatile || TYPE_VOLATILE (type),
16522 context_die);
16523
16524 if (type_die != NULL)
16525 add_AT_die_ref (object_die, DW_AT_type, type_die);
16526 }
16527
16528 /* Given an object die, add the calling convention attribute for the
16529 function call type. */
16530 static void
16531 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16532 {
16533 enum dwarf_calling_convention value = DW_CC_normal;
16534
16535 value = ((enum dwarf_calling_convention)
16536 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16537
16538 if (is_fortran ()
16539 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16540 {
16541 /* DWARF 2 doesn't provide a way to identify a program's source-level
16542 entry point. DW_AT_calling_convention attributes are only meant
16543 to describe functions' calling conventions. However, lacking a
16544 better way to signal the Fortran main program, we used this for
16545 a long time, following existing custom. Now, DWARF 4 has
16546 DW_AT_main_subprogram, which we add below, but some tools still
16547 rely on the old way, which we thus keep. */
16548 value = DW_CC_program;
16549
16550 if (dwarf_version >= 4 || !dwarf_strict)
16551 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16552 }
16553
16554 /* Only add the attribute if the backend requests it, and
16555 is not DW_CC_normal. */
16556 if (value && (value != DW_CC_normal))
16557 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16558 }
16559
16560 /* Given a tree pointer to a struct, class, union, or enum type node, return
16561 a pointer to the (string) tag name for the given type, or zero if the type
16562 was declared without a tag. */
16563
16564 static const char *
16565 type_tag (const_tree type)
16566 {
16567 const char *name = 0;
16568
16569 if (TYPE_NAME (type) != 0)
16570 {
16571 tree t = 0;
16572
16573 /* Find the IDENTIFIER_NODE for the type name. */
16574 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16575 && !TYPE_NAMELESS (type))
16576 t = TYPE_NAME (type);
16577
16578 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16579 a TYPE_DECL node, regardless of whether or not a `typedef' was
16580 involved. */
16581 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16582 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16583 {
16584 /* We want to be extra verbose. Don't call dwarf_name if
16585 DECL_NAME isn't set. The default hook for decl_printable_name
16586 doesn't like that, and in this context it's correct to return
16587 0, instead of "<anonymous>" or the like. */
16588 if (DECL_NAME (TYPE_NAME (type))
16589 && !DECL_NAMELESS (TYPE_NAME (type)))
16590 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16591 }
16592
16593 /* Now get the name as a string, or invent one. */
16594 if (!name && t != 0)
16595 name = IDENTIFIER_POINTER (t);
16596 }
16597
16598 return (name == 0 || *name == '\0') ? 0 : name;
16599 }
16600
16601 /* Return the type associated with a data member, make a special check
16602 for bit field types. */
16603
16604 static inline tree
16605 member_declared_type (const_tree member)
16606 {
16607 return (DECL_BIT_FIELD_TYPE (member)
16608 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16609 }
16610
16611 /* Get the decl's label, as described by its RTL. This may be different
16612 from the DECL_NAME name used in the source file. */
16613
16614 #if 0
16615 static const char *
16616 decl_start_label (tree decl)
16617 {
16618 rtx x;
16619 const char *fnname;
16620
16621 x = DECL_RTL (decl);
16622 gcc_assert (MEM_P (x));
16623
16624 x = XEXP (x, 0);
16625 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16626
16627 fnname = XSTR (x, 0);
16628 return fnname;
16629 }
16630 #endif
16631 \f
16632 /* These routines generate the internal representation of the DIE's for
16633 the compilation unit. Debugging information is collected by walking
16634 the declaration trees passed in from dwarf2out_decl(). */
16635
16636 static void
16637 gen_array_type_die (tree type, dw_die_ref context_die)
16638 {
16639 dw_die_ref scope_die = scope_die_for (type, context_die);
16640 dw_die_ref array_die;
16641
16642 /* GNU compilers represent multidimensional array types as sequences of one
16643 dimensional array types whose element types are themselves array types.
16644 We sometimes squish that down to a single array_type DIE with multiple
16645 subscripts in the Dwarf debugging info. The draft Dwarf specification
16646 say that we are allowed to do this kind of compression in C, because
16647 there is no difference between an array of arrays and a multidimensional
16648 array. We don't do this for Ada to remain as close as possible to the
16649 actual representation, which is especially important against the language
16650 flexibilty wrt arrays of variable size. */
16651
16652 bool collapse_nested_arrays = !is_ada ();
16653 tree element_type;
16654
16655 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16656 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16657 if (TYPE_STRING_FLAG (type)
16658 && TREE_CODE (type) == ARRAY_TYPE
16659 && is_fortran ()
16660 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16661 {
16662 HOST_WIDE_INT size;
16663
16664 array_die = new_die (DW_TAG_string_type, scope_die, type);
16665 add_name_attribute (array_die, type_tag (type));
16666 equate_type_number_to_die (type, array_die);
16667 size = int_size_in_bytes (type);
16668 if (size >= 0)
16669 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16670 else if (TYPE_DOMAIN (type) != NULL_TREE
16671 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16672 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16673 {
16674 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16675 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16676
16677 size = int_size_in_bytes (TREE_TYPE (szdecl));
16678 if (loc && size > 0)
16679 {
16680 add_AT_location_description (array_die, DW_AT_string_length, loc);
16681 if (size != DWARF2_ADDR_SIZE)
16682 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16683 }
16684 }
16685 return;
16686 }
16687
16688 array_die = new_die (DW_TAG_array_type, scope_die, type);
16689 add_name_attribute (array_die, type_tag (type));
16690 equate_type_number_to_die (type, array_die);
16691
16692 if (TREE_CODE (type) == VECTOR_TYPE)
16693 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16694
16695 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16696 if (is_fortran ()
16697 && TREE_CODE (type) == ARRAY_TYPE
16698 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16699 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16700 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16701
16702 #if 0
16703 /* We default the array ordering. SDB will probably do
16704 the right things even if DW_AT_ordering is not present. It's not even
16705 an issue until we start to get into multidimensional arrays anyway. If
16706 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16707 then we'll have to put the DW_AT_ordering attribute back in. (But if
16708 and when we find out that we need to put these in, we will only do so
16709 for multidimensional arrays. */
16710 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16711 #endif
16712
16713 if (TREE_CODE (type) == VECTOR_TYPE)
16714 {
16715 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16716 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16717 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16718 add_bound_info (subrange_die, DW_AT_upper_bound,
16719 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16720 }
16721 else
16722 add_subscript_info (array_die, type, collapse_nested_arrays);
16723
16724 /* Add representation of the type of the elements of this array type and
16725 emit the corresponding DIE if we haven't done it already. */
16726 element_type = TREE_TYPE (type);
16727 if (collapse_nested_arrays)
16728 while (TREE_CODE (element_type) == ARRAY_TYPE)
16729 {
16730 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16731 break;
16732 element_type = TREE_TYPE (element_type);
16733 }
16734
16735 add_type_attribute (array_die, element_type, 0, 0, context_die);
16736
16737 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16738 if (TYPE_ARTIFICIAL (type))
16739 add_AT_flag (array_die, DW_AT_artificial, 1);
16740
16741 if (get_AT (array_die, DW_AT_name))
16742 add_pubtype (type, array_die);
16743 }
16744
16745 static dw_loc_descr_ref
16746 descr_info_loc (tree val, tree base_decl)
16747 {
16748 HOST_WIDE_INT size;
16749 dw_loc_descr_ref loc, loc2;
16750 enum dwarf_location_atom op;
16751
16752 if (val == base_decl)
16753 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16754
16755 switch (TREE_CODE (val))
16756 {
16757 CASE_CONVERT:
16758 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16759 case VAR_DECL:
16760 return loc_descriptor_from_tree (val, 0);
16761 case INTEGER_CST:
16762 if (host_integerp (val, 0))
16763 return int_loc_descriptor (tree_low_cst (val, 0));
16764 break;
16765 case INDIRECT_REF:
16766 size = int_size_in_bytes (TREE_TYPE (val));
16767 if (size < 0)
16768 break;
16769 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16770 if (!loc)
16771 break;
16772 if (size == DWARF2_ADDR_SIZE)
16773 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16774 else
16775 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16776 return loc;
16777 case POINTER_PLUS_EXPR:
16778 case PLUS_EXPR:
16779 if (host_integerp (TREE_OPERAND (val, 1), 1)
16780 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16781 < 16384)
16782 {
16783 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16784 if (!loc)
16785 break;
16786 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16787 }
16788 else
16789 {
16790 op = DW_OP_plus;
16791 do_binop:
16792 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16793 if (!loc)
16794 break;
16795 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16796 if (!loc2)
16797 break;
16798 add_loc_descr (&loc, loc2);
16799 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16800 }
16801 return loc;
16802 case MINUS_EXPR:
16803 op = DW_OP_minus;
16804 goto do_binop;
16805 case MULT_EXPR:
16806 op = DW_OP_mul;
16807 goto do_binop;
16808 case EQ_EXPR:
16809 op = DW_OP_eq;
16810 goto do_binop;
16811 case NE_EXPR:
16812 op = DW_OP_ne;
16813 goto do_binop;
16814 default:
16815 break;
16816 }
16817 return NULL;
16818 }
16819
16820 static void
16821 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16822 tree val, tree base_decl)
16823 {
16824 dw_loc_descr_ref loc;
16825
16826 if (host_integerp (val, 0))
16827 {
16828 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16829 return;
16830 }
16831
16832 loc = descr_info_loc (val, base_decl);
16833 if (!loc)
16834 return;
16835
16836 add_AT_loc (die, attr, loc);
16837 }
16838
16839 /* This routine generates DIE for array with hidden descriptor, details
16840 are filled into *info by a langhook. */
16841
16842 static void
16843 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16844 dw_die_ref context_die)
16845 {
16846 dw_die_ref scope_die = scope_die_for (type, context_die);
16847 dw_die_ref array_die;
16848 int dim;
16849
16850 array_die = new_die (DW_TAG_array_type, scope_die, type);
16851 add_name_attribute (array_die, type_tag (type));
16852 equate_type_number_to_die (type, array_die);
16853
16854 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16855 if (is_fortran ()
16856 && info->ndimensions >= 2)
16857 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16858
16859 if (info->data_location)
16860 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16861 info->base_decl);
16862 if (info->associated)
16863 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16864 info->base_decl);
16865 if (info->allocated)
16866 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16867 info->base_decl);
16868
16869 for (dim = 0; dim < info->ndimensions; dim++)
16870 {
16871 dw_die_ref subrange_die
16872 = new_die (DW_TAG_subrange_type, array_die, NULL);
16873
16874 if (info->dimen[dim].lower_bound)
16875 {
16876 /* If it is the default value, omit it. */
16877 int dflt;
16878
16879 if (host_integerp (info->dimen[dim].lower_bound, 0)
16880 && (dflt = lower_bound_default ()) != -1
16881 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16882 ;
16883 else
16884 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16885 info->dimen[dim].lower_bound,
16886 info->base_decl);
16887 }
16888 if (info->dimen[dim].upper_bound)
16889 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16890 info->dimen[dim].upper_bound,
16891 info->base_decl);
16892 if (info->dimen[dim].stride)
16893 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16894 info->dimen[dim].stride,
16895 info->base_decl);
16896 }
16897
16898 gen_type_die (info->element_type, context_die);
16899 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16900
16901 if (get_AT (array_die, DW_AT_name))
16902 add_pubtype (type, array_die);
16903 }
16904
16905 #if 0
16906 static void
16907 gen_entry_point_die (tree decl, dw_die_ref context_die)
16908 {
16909 tree origin = decl_ultimate_origin (decl);
16910 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16911
16912 if (origin != NULL)
16913 add_abstract_origin_attribute (decl_die, origin);
16914 else
16915 {
16916 add_name_and_src_coords_attributes (decl_die, decl);
16917 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16918 0, 0, context_die);
16919 }
16920
16921 if (DECL_ABSTRACT (decl))
16922 equate_decl_number_to_die (decl, decl_die);
16923 else
16924 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16925 }
16926 #endif
16927
16928 /* Walk through the list of incomplete types again, trying once more to
16929 emit full debugging info for them. */
16930
16931 static void
16932 retry_incomplete_types (void)
16933 {
16934 int i;
16935
16936 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
16937 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
16938 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
16939 }
16940
16941 /* Determine what tag to use for a record type. */
16942
16943 static enum dwarf_tag
16944 record_type_tag (tree type)
16945 {
16946 if (! lang_hooks.types.classify_record)
16947 return DW_TAG_structure_type;
16948
16949 switch (lang_hooks.types.classify_record (type))
16950 {
16951 case RECORD_IS_STRUCT:
16952 return DW_TAG_structure_type;
16953
16954 case RECORD_IS_CLASS:
16955 return DW_TAG_class_type;
16956
16957 case RECORD_IS_INTERFACE:
16958 if (dwarf_version >= 3 || !dwarf_strict)
16959 return DW_TAG_interface_type;
16960 return DW_TAG_structure_type;
16961
16962 default:
16963 gcc_unreachable ();
16964 }
16965 }
16966
16967 /* Generate a DIE to represent an enumeration type. Note that these DIEs
16968 include all of the information about the enumeration values also. Each
16969 enumerated type name/value is listed as a child of the enumerated type
16970 DIE. */
16971
16972 static dw_die_ref
16973 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16974 {
16975 dw_die_ref type_die = lookup_type_die (type);
16976
16977 if (type_die == NULL)
16978 {
16979 type_die = new_die (DW_TAG_enumeration_type,
16980 scope_die_for (type, context_die), type);
16981 equate_type_number_to_die (type, type_die);
16982 add_name_attribute (type_die, type_tag (type));
16983 if (dwarf_version >= 4 || !dwarf_strict)
16984 {
16985 if (ENUM_IS_SCOPED (type))
16986 add_AT_flag (type_die, DW_AT_enum_class, 1);
16987 if (ENUM_IS_OPAQUE (type))
16988 add_AT_flag (type_die, DW_AT_declaration, 1);
16989 }
16990 }
16991 else if (! TYPE_SIZE (type))
16992 return type_die;
16993 else
16994 remove_AT (type_die, DW_AT_declaration);
16995
16996 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
16997 given enum type is incomplete, do not generate the DW_AT_byte_size
16998 attribute or the DW_AT_element_list attribute. */
16999 if (TYPE_SIZE (type))
17000 {
17001 tree link;
17002
17003 TREE_ASM_WRITTEN (type) = 1;
17004 add_byte_size_attribute (type_die, type);
17005 if (TYPE_STUB_DECL (type) != NULL_TREE)
17006 {
17007 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17008 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17009 }
17010
17011 /* If the first reference to this type was as the return type of an
17012 inline function, then it may not have a parent. Fix this now. */
17013 if (type_die->die_parent == NULL)
17014 add_child_die (scope_die_for (type, context_die), type_die);
17015
17016 for (link = TYPE_VALUES (type);
17017 link != NULL; link = TREE_CHAIN (link))
17018 {
17019 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17020 tree value = TREE_VALUE (link);
17021
17022 add_name_attribute (enum_die,
17023 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17024
17025 if (TREE_CODE (value) == CONST_DECL)
17026 value = DECL_INITIAL (value);
17027
17028 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
17029 /* DWARF2 does not provide a way of indicating whether or
17030 not enumeration constants are signed or unsigned. GDB
17031 always assumes the values are signed, so we output all
17032 values as if they were signed. That means that
17033 enumeration constants with very large unsigned values
17034 will appear to have negative values in the debugger. */
17035 add_AT_int (enum_die, DW_AT_const_value,
17036 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
17037 }
17038
17039 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17040 if (TYPE_ARTIFICIAL (type))
17041 add_AT_flag (type_die, DW_AT_artificial, 1);
17042 }
17043 else
17044 add_AT_flag (type_die, DW_AT_declaration, 1);
17045
17046 add_pubtype (type, type_die);
17047
17048 return type_die;
17049 }
17050
17051 /* Generate a DIE to represent either a real live formal parameter decl or to
17052 represent just the type of some formal parameter position in some function
17053 type.
17054
17055 Note that this routine is a bit unusual because its argument may be a
17056 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17057 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17058 node. If it's the former then this function is being called to output a
17059 DIE to represent a formal parameter object (or some inlining thereof). If
17060 it's the latter, then this function is only being called to output a
17061 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17062 argument type of some subprogram type.
17063 If EMIT_NAME_P is true, name and source coordinate attributes
17064 are emitted. */
17065
17066 static dw_die_ref
17067 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17068 dw_die_ref context_die)
17069 {
17070 tree node_or_origin = node ? node : origin;
17071 tree ultimate_origin;
17072 dw_die_ref parm_die
17073 = new_die (DW_TAG_formal_parameter, context_die, node);
17074
17075 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17076 {
17077 case tcc_declaration:
17078 ultimate_origin = decl_ultimate_origin (node_or_origin);
17079 if (node || ultimate_origin)
17080 origin = ultimate_origin;
17081 if (origin != NULL)
17082 add_abstract_origin_attribute (parm_die, origin);
17083 else if (emit_name_p)
17084 add_name_and_src_coords_attributes (parm_die, node);
17085 if (origin == NULL
17086 || (! DECL_ABSTRACT (node_or_origin)
17087 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17088 decl_function_context
17089 (node_or_origin))))
17090 {
17091 tree type = TREE_TYPE (node_or_origin);
17092 if (decl_by_reference_p (node_or_origin))
17093 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17094 context_die);
17095 else
17096 add_type_attribute (parm_die, type,
17097 TREE_READONLY (node_or_origin),
17098 TREE_THIS_VOLATILE (node_or_origin),
17099 context_die);
17100 }
17101 if (origin == NULL && DECL_ARTIFICIAL (node))
17102 add_AT_flag (parm_die, DW_AT_artificial, 1);
17103
17104 if (node && node != origin)
17105 equate_decl_number_to_die (node, parm_die);
17106 if (! DECL_ABSTRACT (node_or_origin))
17107 add_location_or_const_value_attribute (parm_die, node_or_origin,
17108 node == NULL, DW_AT_location);
17109
17110 break;
17111
17112 case tcc_type:
17113 /* We were called with some kind of a ..._TYPE node. */
17114 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17115 break;
17116
17117 default:
17118 gcc_unreachable ();
17119 }
17120
17121 return parm_die;
17122 }
17123
17124 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17125 children DW_TAG_formal_parameter DIEs representing the arguments of the
17126 parameter pack.
17127
17128 PARM_PACK must be a function parameter pack.
17129 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17130 must point to the subsequent arguments of the function PACK_ARG belongs to.
17131 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17132 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17133 following the last one for which a DIE was generated. */
17134
17135 static dw_die_ref
17136 gen_formal_parameter_pack_die (tree parm_pack,
17137 tree pack_arg,
17138 dw_die_ref subr_die,
17139 tree *next_arg)
17140 {
17141 tree arg;
17142 dw_die_ref parm_pack_die;
17143
17144 gcc_assert (parm_pack
17145 && lang_hooks.function_parameter_pack_p (parm_pack)
17146 && subr_die);
17147
17148 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17149 add_src_coords_attributes (parm_pack_die, parm_pack);
17150
17151 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17152 {
17153 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17154 parm_pack))
17155 break;
17156 gen_formal_parameter_die (arg, NULL,
17157 false /* Don't emit name attribute. */,
17158 parm_pack_die);
17159 }
17160 if (next_arg)
17161 *next_arg = arg;
17162 return parm_pack_die;
17163 }
17164
17165 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17166 at the end of an (ANSI prototyped) formal parameters list. */
17167
17168 static void
17169 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17170 {
17171 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17172 }
17173
17174 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17175 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17176 parameters as specified in some function type specification (except for
17177 those which appear as part of a function *definition*). */
17178
17179 static void
17180 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17181 {
17182 tree link;
17183 tree formal_type = NULL;
17184 tree first_parm_type;
17185 tree arg;
17186
17187 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17188 {
17189 arg = DECL_ARGUMENTS (function_or_method_type);
17190 function_or_method_type = TREE_TYPE (function_or_method_type);
17191 }
17192 else
17193 arg = NULL_TREE;
17194
17195 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17196
17197 /* Make our first pass over the list of formal parameter types and output a
17198 DW_TAG_formal_parameter DIE for each one. */
17199 for (link = first_parm_type; link; )
17200 {
17201 dw_die_ref parm_die;
17202
17203 formal_type = TREE_VALUE (link);
17204 if (formal_type == void_type_node)
17205 break;
17206
17207 /* Output a (nameless) DIE to represent the formal parameter itself. */
17208 parm_die = gen_formal_parameter_die (formal_type, NULL,
17209 true /* Emit name attribute. */,
17210 context_die);
17211 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17212 && link == first_parm_type)
17213 {
17214 add_AT_flag (parm_die, DW_AT_artificial, 1);
17215 if (dwarf_version >= 3 || !dwarf_strict)
17216 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17217 }
17218 else if (arg && DECL_ARTIFICIAL (arg))
17219 add_AT_flag (parm_die, DW_AT_artificial, 1);
17220
17221 link = TREE_CHAIN (link);
17222 if (arg)
17223 arg = DECL_CHAIN (arg);
17224 }
17225
17226 /* If this function type has an ellipsis, add a
17227 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17228 if (formal_type != void_type_node)
17229 gen_unspecified_parameters_die (function_or_method_type, context_die);
17230
17231 /* Make our second (and final) pass over the list of formal parameter types
17232 and output DIEs to represent those types (as necessary). */
17233 for (link = TYPE_ARG_TYPES (function_or_method_type);
17234 link && TREE_VALUE (link);
17235 link = TREE_CHAIN (link))
17236 gen_type_die (TREE_VALUE (link), context_die);
17237 }
17238
17239 /* We want to generate the DIE for TYPE so that we can generate the
17240 die for MEMBER, which has been defined; we will need to refer back
17241 to the member declaration nested within TYPE. If we're trying to
17242 generate minimal debug info for TYPE, processing TYPE won't do the
17243 trick; we need to attach the member declaration by hand. */
17244
17245 static void
17246 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17247 {
17248 gen_type_die (type, context_die);
17249
17250 /* If we're trying to avoid duplicate debug info, we may not have
17251 emitted the member decl for this function. Emit it now. */
17252 if (TYPE_STUB_DECL (type)
17253 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17254 && ! lookup_decl_die (member))
17255 {
17256 dw_die_ref type_die;
17257 gcc_assert (!decl_ultimate_origin (member));
17258
17259 push_decl_scope (type);
17260 type_die = lookup_type_die_strip_naming_typedef (type);
17261 if (TREE_CODE (member) == FUNCTION_DECL)
17262 gen_subprogram_die (member, type_die);
17263 else if (TREE_CODE (member) == FIELD_DECL)
17264 {
17265 /* Ignore the nameless fields that are used to skip bits but handle
17266 C++ anonymous unions and structs. */
17267 if (DECL_NAME (member) != NULL_TREE
17268 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17269 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17270 {
17271 gen_type_die (member_declared_type (member), type_die);
17272 gen_field_die (member, type_die);
17273 }
17274 }
17275 else
17276 gen_variable_die (member, NULL_TREE, type_die);
17277
17278 pop_decl_scope ();
17279 }
17280 }
17281 \f
17282 /* Forward declare these functions, because they are mutually recursive
17283 with their set_block_* pairing functions. */
17284 static void set_decl_origin_self (tree);
17285 static void set_decl_abstract_flags (tree, int);
17286
17287 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17288 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17289 that it points to the node itself, thus indicating that the node is its
17290 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17291 the given node is NULL, recursively descend the decl/block tree which
17292 it is the root of, and for each other ..._DECL or BLOCK node contained
17293 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17294 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17295 values to point to themselves. */
17296
17297 static void
17298 set_block_origin_self (tree stmt)
17299 {
17300 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17301 {
17302 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17303
17304 {
17305 tree local_decl;
17306
17307 for (local_decl = BLOCK_VARS (stmt);
17308 local_decl != NULL_TREE;
17309 local_decl = DECL_CHAIN (local_decl))
17310 if (! DECL_EXTERNAL (local_decl))
17311 set_decl_origin_self (local_decl); /* Potential recursion. */
17312 }
17313
17314 {
17315 tree subblock;
17316
17317 for (subblock = BLOCK_SUBBLOCKS (stmt);
17318 subblock != NULL_TREE;
17319 subblock = BLOCK_CHAIN (subblock))
17320 set_block_origin_self (subblock); /* Recurse. */
17321 }
17322 }
17323 }
17324
17325 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17326 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17327 node to so that it points to the node itself, thus indicating that the
17328 node represents its own (abstract) origin. Additionally, if the
17329 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17330 the decl/block tree of which the given node is the root of, and for
17331 each other ..._DECL or BLOCK node contained therein whose
17332 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17333 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17334 point to themselves. */
17335
17336 static void
17337 set_decl_origin_self (tree decl)
17338 {
17339 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17340 {
17341 DECL_ABSTRACT_ORIGIN (decl) = decl;
17342 if (TREE_CODE (decl) == FUNCTION_DECL)
17343 {
17344 tree arg;
17345
17346 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17347 DECL_ABSTRACT_ORIGIN (arg) = arg;
17348 if (DECL_INITIAL (decl) != NULL_TREE
17349 && DECL_INITIAL (decl) != error_mark_node)
17350 set_block_origin_self (DECL_INITIAL (decl));
17351 }
17352 }
17353 }
17354 \f
17355 /* Given a pointer to some BLOCK node, and a boolean value to set the
17356 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17357 the given block, and for all local decls and all local sub-blocks
17358 (recursively) which are contained therein. */
17359
17360 static void
17361 set_block_abstract_flags (tree stmt, int setting)
17362 {
17363 tree local_decl;
17364 tree subblock;
17365 unsigned int i;
17366
17367 BLOCK_ABSTRACT (stmt) = setting;
17368
17369 for (local_decl = BLOCK_VARS (stmt);
17370 local_decl != NULL_TREE;
17371 local_decl = DECL_CHAIN (local_decl))
17372 if (! DECL_EXTERNAL (local_decl))
17373 set_decl_abstract_flags (local_decl, setting);
17374
17375 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17376 {
17377 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17378 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17379 || TREE_CODE (local_decl) == PARM_DECL)
17380 set_decl_abstract_flags (local_decl, setting);
17381 }
17382
17383 for (subblock = BLOCK_SUBBLOCKS (stmt);
17384 subblock != NULL_TREE;
17385 subblock = BLOCK_CHAIN (subblock))
17386 set_block_abstract_flags (subblock, setting);
17387 }
17388
17389 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17390 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17391 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17392 set the abstract flags for all of the parameters, local vars, local
17393 blocks and sub-blocks (recursively) to the same setting. */
17394
17395 static void
17396 set_decl_abstract_flags (tree decl, int setting)
17397 {
17398 DECL_ABSTRACT (decl) = setting;
17399 if (TREE_CODE (decl) == FUNCTION_DECL)
17400 {
17401 tree arg;
17402
17403 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17404 DECL_ABSTRACT (arg) = setting;
17405 if (DECL_INITIAL (decl) != NULL_TREE
17406 && DECL_INITIAL (decl) != error_mark_node)
17407 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17408 }
17409 }
17410
17411 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17412 may later generate inlined and/or out-of-line instances of. */
17413
17414 static void
17415 dwarf2out_abstract_function (tree decl)
17416 {
17417 dw_die_ref old_die;
17418 tree save_fn;
17419 tree context;
17420 int was_abstract;
17421 htab_t old_decl_loc_table;
17422 htab_t old_cached_dw_loc_list_table;
17423 int old_call_site_count, old_tail_call_site_count;
17424 struct call_arg_loc_node *old_call_arg_locations;
17425
17426 /* Make sure we have the actual abstract inline, not a clone. */
17427 decl = DECL_ORIGIN (decl);
17428
17429 old_die = lookup_decl_die (decl);
17430 if (old_die && get_AT (old_die, DW_AT_inline))
17431 /* We've already generated the abstract instance. */
17432 return;
17433
17434 /* We can be called while recursively when seeing block defining inlined subroutine
17435 DIE. Be sure to not clobber the outer location table nor use it or we would
17436 get locations in abstract instantces. */
17437 old_decl_loc_table = decl_loc_table;
17438 decl_loc_table = NULL;
17439 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17440 cached_dw_loc_list_table = NULL;
17441 old_call_arg_locations = call_arg_locations;
17442 call_arg_locations = NULL;
17443 old_call_site_count = call_site_count;
17444 call_site_count = -1;
17445 old_tail_call_site_count = tail_call_site_count;
17446 tail_call_site_count = -1;
17447
17448 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17449 we don't get confused by DECL_ABSTRACT. */
17450 if (debug_info_level > DINFO_LEVEL_TERSE)
17451 {
17452 context = decl_class_context (decl);
17453 if (context)
17454 gen_type_die_for_member
17455 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17456 }
17457
17458 /* Pretend we've just finished compiling this function. */
17459 save_fn = current_function_decl;
17460 current_function_decl = decl;
17461
17462 was_abstract = DECL_ABSTRACT (decl);
17463 set_decl_abstract_flags (decl, 1);
17464 dwarf2out_decl (decl);
17465 if (! was_abstract)
17466 set_decl_abstract_flags (decl, 0);
17467
17468 current_function_decl = save_fn;
17469 decl_loc_table = old_decl_loc_table;
17470 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17471 call_arg_locations = old_call_arg_locations;
17472 call_site_count = old_call_site_count;
17473 tail_call_site_count = old_tail_call_site_count;
17474 }
17475
17476 /* Helper function of premark_used_types() which gets called through
17477 htab_traverse.
17478
17479 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17480 marked as unused by prune_unused_types. */
17481
17482 static int
17483 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17484 {
17485 tree type;
17486 dw_die_ref die;
17487
17488 type = (tree) *slot;
17489 die = lookup_type_die (type);
17490 if (die != NULL)
17491 die->die_perennial_p = 1;
17492 return 1;
17493 }
17494
17495 /* Helper function of premark_types_used_by_global_vars which gets called
17496 through htab_traverse.
17497
17498 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17499 marked as unused by prune_unused_types. The DIE of the type is marked
17500 only if the global variable using the type will actually be emitted. */
17501
17502 static int
17503 premark_types_used_by_global_vars_helper (void **slot,
17504 void *data ATTRIBUTE_UNUSED)
17505 {
17506 struct types_used_by_vars_entry *entry;
17507 dw_die_ref die;
17508
17509 entry = (struct types_used_by_vars_entry *) *slot;
17510 gcc_assert (entry->type != NULL
17511 && entry->var_decl != NULL);
17512 die = lookup_type_die (entry->type);
17513 if (die)
17514 {
17515 /* Ask cgraph if the global variable really is to be emitted.
17516 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17517 struct varpool_node *node = varpool_get_node (entry->var_decl);
17518 if (node && node->analyzed)
17519 {
17520 die->die_perennial_p = 1;
17521 /* Keep the parent DIEs as well. */
17522 while ((die = die->die_parent) && die->die_perennial_p == 0)
17523 die->die_perennial_p = 1;
17524 }
17525 }
17526 return 1;
17527 }
17528
17529 /* Mark all members of used_types_hash as perennial. */
17530
17531 static void
17532 premark_used_types (struct function *fun)
17533 {
17534 if (fun && fun->used_types_hash)
17535 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17536 }
17537
17538 /* Mark all members of types_used_by_vars_entry as perennial. */
17539
17540 static void
17541 premark_types_used_by_global_vars (void)
17542 {
17543 if (types_used_by_vars_hash)
17544 htab_traverse (types_used_by_vars_hash,
17545 premark_types_used_by_global_vars_helper, NULL);
17546 }
17547
17548 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17549 for CA_LOC call arg loc node. */
17550
17551 static dw_die_ref
17552 gen_call_site_die (tree decl, dw_die_ref subr_die,
17553 struct call_arg_loc_node *ca_loc)
17554 {
17555 dw_die_ref stmt_die = NULL, die;
17556 tree block = ca_loc->block;
17557
17558 while (block
17559 && block != DECL_INITIAL (decl)
17560 && TREE_CODE (block) == BLOCK)
17561 {
17562 if (block_map.length () > BLOCK_NUMBER (block))
17563 stmt_die = block_map[BLOCK_NUMBER (block)];
17564 if (stmt_die)
17565 break;
17566 block = BLOCK_SUPERCONTEXT (block);
17567 }
17568 if (stmt_die == NULL)
17569 stmt_die = subr_die;
17570 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17571 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17572 if (ca_loc->tail_call_p)
17573 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17574 if (ca_loc->symbol_ref)
17575 {
17576 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17577 if (tdie)
17578 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17579 else
17580 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17581 }
17582 return die;
17583 }
17584
17585 /* Generate a DIE to represent a declared function (either file-scope or
17586 block-local). */
17587
17588 static void
17589 gen_subprogram_die (tree decl, dw_die_ref context_die)
17590 {
17591 tree origin = decl_ultimate_origin (decl);
17592 dw_die_ref subr_die;
17593 tree outer_scope;
17594 dw_die_ref old_die = lookup_decl_die (decl);
17595 int declaration = (current_function_decl != decl
17596 || class_or_namespace_scope_p (context_die));
17597
17598 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17599
17600 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17601 started to generate the abstract instance of an inline, decided to output
17602 its containing class, and proceeded to emit the declaration of the inline
17603 from the member list for the class. If so, DECLARATION takes priority;
17604 we'll get back to the abstract instance when done with the class. */
17605
17606 /* The class-scope declaration DIE must be the primary DIE. */
17607 if (origin && declaration && class_or_namespace_scope_p (context_die))
17608 {
17609 origin = NULL;
17610 gcc_assert (!old_die);
17611 }
17612
17613 /* Now that the C++ front end lazily declares artificial member fns, we
17614 might need to retrofit the declaration into its class. */
17615 if (!declaration && !origin && !old_die
17616 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17617 && !class_or_namespace_scope_p (context_die)
17618 && debug_info_level > DINFO_LEVEL_TERSE)
17619 old_die = force_decl_die (decl);
17620
17621 if (origin != NULL)
17622 {
17623 gcc_assert (!declaration || local_scope_p (context_die));
17624
17625 /* Fixup die_parent for the abstract instance of a nested
17626 inline function. */
17627 if (old_die && old_die->die_parent == NULL)
17628 add_child_die (context_die, old_die);
17629
17630 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17631 add_abstract_origin_attribute (subr_die, origin);
17632 /* This is where the actual code for a cloned function is.
17633 Let's emit linkage name attribute for it. This helps
17634 debuggers to e.g, set breakpoints into
17635 constructors/destructors when the user asks "break
17636 K::K". */
17637 add_linkage_name (subr_die, decl);
17638 }
17639 else if (old_die)
17640 {
17641 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17642 struct dwarf_file_data * file_index = lookup_filename (s.file);
17643
17644 if (!get_AT_flag (old_die, DW_AT_declaration)
17645 /* We can have a normal definition following an inline one in the
17646 case of redefinition of GNU C extern inlines.
17647 It seems reasonable to use AT_specification in this case. */
17648 && !get_AT (old_die, DW_AT_inline))
17649 {
17650 /* Detect and ignore this case, where we are trying to output
17651 something we have already output. */
17652 return;
17653 }
17654
17655 /* If the definition comes from the same place as the declaration,
17656 maybe use the old DIE. We always want the DIE for this function
17657 that has the *_pc attributes to be under comp_unit_die so the
17658 debugger can find it. We also need to do this for abstract
17659 instances of inlines, since the spec requires the out-of-line copy
17660 to have the same parent. For local class methods, this doesn't
17661 apply; we just use the old DIE. */
17662 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17663 && (DECL_ARTIFICIAL (decl)
17664 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17665 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17666 == (unsigned) s.line))))
17667 {
17668 subr_die = old_die;
17669
17670 /* Clear out the declaration attribute and the formal parameters.
17671 Do not remove all children, because it is possible that this
17672 declaration die was forced using force_decl_die(). In such
17673 cases die that forced declaration die (e.g. TAG_imported_module)
17674 is one of the children that we do not want to remove. */
17675 remove_AT (subr_die, DW_AT_declaration);
17676 remove_AT (subr_die, DW_AT_object_pointer);
17677 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17678 }
17679 else
17680 {
17681 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17682 add_AT_specification (subr_die, old_die);
17683 add_pubname (decl, subr_die);
17684 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17685 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17686 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17687 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17688 }
17689 }
17690 else
17691 {
17692 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17693
17694 if (TREE_PUBLIC (decl))
17695 add_AT_flag (subr_die, DW_AT_external, 1);
17696
17697 add_name_and_src_coords_attributes (subr_die, decl);
17698 add_pubname (decl, subr_die);
17699 if (debug_info_level > DINFO_LEVEL_TERSE)
17700 {
17701 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17702 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17703 0, 0, context_die);
17704 }
17705
17706 add_pure_or_virtual_attribute (subr_die, decl);
17707 if (DECL_ARTIFICIAL (decl))
17708 add_AT_flag (subr_die, DW_AT_artificial, 1);
17709
17710 add_accessibility_attribute (subr_die, decl);
17711 }
17712
17713 if (declaration)
17714 {
17715 if (!old_die || !get_AT (old_die, DW_AT_inline))
17716 {
17717 add_AT_flag (subr_die, DW_AT_declaration, 1);
17718
17719 /* If this is an explicit function declaration then generate
17720 a DW_AT_explicit attribute. */
17721 if (lang_hooks.decls.function_decl_explicit_p (decl)
17722 && (dwarf_version >= 3 || !dwarf_strict))
17723 add_AT_flag (subr_die, DW_AT_explicit, 1);
17724
17725 /* The first time we see a member function, it is in the context of
17726 the class to which it belongs. We make sure of this by emitting
17727 the class first. The next time is the definition, which is
17728 handled above. The two may come from the same source text.
17729
17730 Note that force_decl_die() forces function declaration die. It is
17731 later reused to represent definition. */
17732 equate_decl_number_to_die (decl, subr_die);
17733 }
17734 }
17735 else if (DECL_ABSTRACT (decl))
17736 {
17737 if (DECL_DECLARED_INLINE_P (decl))
17738 {
17739 if (cgraph_function_possibly_inlined_p (decl))
17740 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17741 else
17742 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17743 }
17744 else
17745 {
17746 if (cgraph_function_possibly_inlined_p (decl))
17747 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17748 else
17749 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17750 }
17751
17752 if (DECL_DECLARED_INLINE_P (decl)
17753 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17754 add_AT_flag (subr_die, DW_AT_artificial, 1);
17755
17756 equate_decl_number_to_die (decl, subr_die);
17757 }
17758 else if (!DECL_EXTERNAL (decl))
17759 {
17760 HOST_WIDE_INT cfa_fb_offset;
17761 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17762
17763 if (!old_die || !get_AT (old_die, DW_AT_inline))
17764 equate_decl_number_to_die (decl, subr_die);
17765
17766 gcc_checking_assert (fun);
17767 if (!flag_reorder_blocks_and_partition)
17768 {
17769 dw_fde_ref fde = fun->fde;
17770 if (fde->dw_fde_begin)
17771 {
17772 /* We have already generated the labels. */
17773 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17774 fde->dw_fde_end, false);
17775 }
17776 else
17777 {
17778 /* Create start/end labels and add the range. */
17779 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17780 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17781 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17782 current_function_funcdef_no);
17783 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17784 current_function_funcdef_no);
17785 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
17786 false);
17787 }
17788
17789 #if VMS_DEBUGGING_INFO
17790 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17791 Section 2.3 Prologue and Epilogue Attributes:
17792 When a breakpoint is set on entry to a function, it is generally
17793 desirable for execution to be suspended, not on the very first
17794 instruction of the function, but rather at a point after the
17795 function's frame has been set up, after any language defined local
17796 declaration processing has been completed, and before execution of
17797 the first statement of the function begins. Debuggers generally
17798 cannot properly determine where this point is. Similarly for a
17799 breakpoint set on exit from a function. The prologue and epilogue
17800 attributes allow a compiler to communicate the location(s) to use. */
17801
17802 {
17803 if (fde->dw_fde_vms_end_prologue)
17804 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17805 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17806
17807 if (fde->dw_fde_vms_begin_epilogue)
17808 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17809 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17810 }
17811 #endif
17812
17813 }
17814 else
17815 {
17816 /* Generate pubnames entries for the split function code ranges. */
17817 dw_fde_ref fde = fun->fde;
17818
17819 if (fde->dw_fde_second_begin)
17820 {
17821 if (dwarf_version >= 3 || !dwarf_strict)
17822 {
17823 /* We should use ranges for non-contiguous code section
17824 addresses. Use the actual code range for the initial
17825 section, since the HOT/COLD labels might precede an
17826 alignment offset. */
17827 bool range_list_added = false;
17828 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17829 fde->dw_fde_end, &range_list_added,
17830 false);
17831 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17832 fde->dw_fde_second_end,
17833 &range_list_added, false);
17834 if (range_list_added)
17835 add_ranges (NULL);
17836 }
17837 else
17838 {
17839 /* There is no real support in DW2 for this .. so we make
17840 a work-around. First, emit the pub name for the segment
17841 containing the function label. Then make and emit a
17842 simplified subprogram DIE for the second segment with the
17843 name pre-fixed by __hot/cold_sect_of_. We use the same
17844 linkage name for the second die so that gdb will find both
17845 sections when given "b foo". */
17846 const char *name = NULL;
17847 tree decl_name = DECL_NAME (decl);
17848 dw_die_ref seg_die;
17849
17850 /* Do the 'primary' section. */
17851 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17852 fde->dw_fde_end, false);
17853
17854 /* Build a minimal DIE for the secondary section. */
17855 seg_die = new_die (DW_TAG_subprogram,
17856 subr_die->die_parent, decl);
17857
17858 if (TREE_PUBLIC (decl))
17859 add_AT_flag (seg_die, DW_AT_external, 1);
17860
17861 if (decl_name != NULL
17862 && IDENTIFIER_POINTER (decl_name) != NULL)
17863 {
17864 name = dwarf2_name (decl, 1);
17865 if (! DECL_ARTIFICIAL (decl))
17866 add_src_coords_attributes (seg_die, decl);
17867
17868 add_linkage_name (seg_die, decl);
17869 }
17870 gcc_assert (name != NULL);
17871 add_pure_or_virtual_attribute (seg_die, decl);
17872 if (DECL_ARTIFICIAL (decl))
17873 add_AT_flag (seg_die, DW_AT_artificial, 1);
17874
17875 name = concat ("__second_sect_of_", name, NULL);
17876 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17877 fde->dw_fde_second_end, false);
17878 add_name_attribute (seg_die, name);
17879 if (want_pubnames ())
17880 add_pubname_string (name, seg_die);
17881 }
17882 }
17883 else
17884 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
17885 false);
17886 }
17887
17888 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17889
17890 /* We define the "frame base" as the function's CFA. This is more
17891 convenient for several reasons: (1) It's stable across the prologue
17892 and epilogue, which makes it better than just a frame pointer,
17893 (2) With dwarf3, there exists a one-byte encoding that allows us
17894 to reference the .debug_frame data by proxy, but failing that,
17895 (3) We can at least reuse the code inspection and interpretation
17896 code that determines the CFA position at various points in the
17897 function. */
17898 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17899 {
17900 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17901 add_AT_loc (subr_die, DW_AT_frame_base, op);
17902 }
17903 else
17904 {
17905 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17906 if (list->dw_loc_next)
17907 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17908 else
17909 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17910 }
17911
17912 /* Compute a displacement from the "steady-state frame pointer" to
17913 the CFA. The former is what all stack slots and argument slots
17914 will reference in the rtl; the latter is what we've told the
17915 debugger about. We'll need to adjust all frame_base references
17916 by this displacement. */
17917 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17918
17919 if (fun->static_chain_decl)
17920 add_AT_location_description (subr_die, DW_AT_static_link,
17921 loc_list_from_tree (fun->static_chain_decl, 2));
17922 }
17923
17924 /* Generate child dies for template paramaters. */
17925 if (debug_info_level > DINFO_LEVEL_TERSE)
17926 gen_generic_params_dies (decl);
17927
17928 /* Now output descriptions of the arguments for this function. This gets
17929 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17930 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17931 `...' at the end of the formal parameter list. In order to find out if
17932 there was a trailing ellipsis or not, we must instead look at the type
17933 associated with the FUNCTION_DECL. This will be a node of type
17934 FUNCTION_TYPE. If the chain of type nodes hanging off of this
17935 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17936 an ellipsis at the end. */
17937
17938 /* In the case where we are describing a mere function declaration, all we
17939 need to do here (and all we *can* do here) is to describe the *types* of
17940 its formal parameters. */
17941 if (debug_info_level <= DINFO_LEVEL_TERSE)
17942 ;
17943 else if (declaration)
17944 gen_formal_types_die (decl, subr_die);
17945 else
17946 {
17947 /* Generate DIEs to represent all known formal parameters. */
17948 tree parm = DECL_ARGUMENTS (decl);
17949 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17950 tree generic_decl_parm = generic_decl
17951 ? DECL_ARGUMENTS (generic_decl)
17952 : NULL;
17953
17954 /* Now we want to walk the list of parameters of the function and
17955 emit their relevant DIEs.
17956
17957 We consider the case of DECL being an instance of a generic function
17958 as well as it being a normal function.
17959
17960 If DECL is an instance of a generic function we walk the
17961 parameters of the generic function declaration _and_ the parameters of
17962 DECL itself. This is useful because we want to emit specific DIEs for
17963 function parameter packs and those are declared as part of the
17964 generic function declaration. In that particular case,
17965 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17966 That DIE has children DIEs representing the set of arguments
17967 of the pack. Note that the set of pack arguments can be empty.
17968 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17969 children DIE.
17970
17971 Otherwise, we just consider the parameters of DECL. */
17972 while (generic_decl_parm || parm)
17973 {
17974 if (generic_decl_parm
17975 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17976 gen_formal_parameter_pack_die (generic_decl_parm,
17977 parm, subr_die,
17978 &parm);
17979 else if (parm)
17980 {
17981 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17982
17983 if (parm == DECL_ARGUMENTS (decl)
17984 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17985 && parm_die
17986 && (dwarf_version >= 3 || !dwarf_strict))
17987 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17988
17989 parm = DECL_CHAIN (parm);
17990 }
17991
17992 if (generic_decl_parm)
17993 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17994 }
17995
17996 /* Decide whether we need an unspecified_parameters DIE at the end.
17997 There are 2 more cases to do this for: 1) the ansi ... declaration -
17998 this is detectable when the end of the arg list is not a
17999 void_type_node 2) an unprototyped function declaration (not a
18000 definition). This just means that we have no info about the
18001 parameters at all. */
18002 if (prototype_p (TREE_TYPE (decl)))
18003 {
18004 /* This is the prototyped case, check for.... */
18005 if (stdarg_p (TREE_TYPE (decl)))
18006 gen_unspecified_parameters_die (decl, subr_die);
18007 }
18008 else if (DECL_INITIAL (decl) == NULL_TREE)
18009 gen_unspecified_parameters_die (decl, subr_die);
18010 }
18011
18012 /* Output Dwarf info for all of the stuff within the body of the function
18013 (if it has one - it may be just a declaration). */
18014 outer_scope = DECL_INITIAL (decl);
18015
18016 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18017 a function. This BLOCK actually represents the outermost binding contour
18018 for the function, i.e. the contour in which the function's formal
18019 parameters and labels get declared. Curiously, it appears that the front
18020 end doesn't actually put the PARM_DECL nodes for the current function onto
18021 the BLOCK_VARS list for this outer scope, but are strung off of the
18022 DECL_ARGUMENTS list for the function instead.
18023
18024 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18025 the LABEL_DECL nodes for the function however, and we output DWARF info
18026 for those in decls_for_scope. Just within the `outer_scope' there will be
18027 a BLOCK node representing the function's outermost pair of curly braces,
18028 and any blocks used for the base and member initializers of a C++
18029 constructor function. */
18030 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18031 {
18032 int call_site_note_count = 0;
18033 int tail_call_site_note_count = 0;
18034
18035 /* Emit a DW_TAG_variable DIE for a named return value. */
18036 if (DECL_NAME (DECL_RESULT (decl)))
18037 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18038
18039 current_function_has_inlines = 0;
18040 decls_for_scope (outer_scope, subr_die, 0);
18041
18042 if (call_arg_locations && !dwarf_strict)
18043 {
18044 struct call_arg_loc_node *ca_loc;
18045 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18046 {
18047 dw_die_ref die = NULL;
18048 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18049 rtx arg, next_arg;
18050
18051 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18052 arg; arg = next_arg)
18053 {
18054 dw_loc_descr_ref reg, val;
18055 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18056 dw_die_ref cdie, tdie = NULL;
18057
18058 next_arg = XEXP (arg, 1);
18059 if (REG_P (XEXP (XEXP (arg, 0), 0))
18060 && next_arg
18061 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18062 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18063 && REGNO (XEXP (XEXP (arg, 0), 0))
18064 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18065 next_arg = XEXP (next_arg, 1);
18066 if (mode == VOIDmode)
18067 {
18068 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18069 if (mode == VOIDmode)
18070 mode = GET_MODE (XEXP (arg, 0));
18071 }
18072 if (mode == VOIDmode || mode == BLKmode)
18073 continue;
18074 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18075 {
18076 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18077 tloc = XEXP (XEXP (arg, 0), 1);
18078 continue;
18079 }
18080 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18081 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18082 {
18083 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18084 tlocc = XEXP (XEXP (arg, 0), 1);
18085 continue;
18086 }
18087 reg = NULL;
18088 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18089 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18090 VAR_INIT_STATUS_INITIALIZED);
18091 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18092 {
18093 rtx mem = XEXP (XEXP (arg, 0), 0);
18094 reg = mem_loc_descriptor (XEXP (mem, 0),
18095 get_address_mode (mem),
18096 GET_MODE (mem),
18097 VAR_INIT_STATUS_INITIALIZED);
18098 }
18099 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18100 == DEBUG_PARAMETER_REF)
18101 {
18102 tree tdecl
18103 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18104 tdie = lookup_decl_die (tdecl);
18105 if (tdie == NULL)
18106 continue;
18107 }
18108 else
18109 continue;
18110 if (reg == NULL
18111 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18112 != DEBUG_PARAMETER_REF)
18113 continue;
18114 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18115 VOIDmode,
18116 VAR_INIT_STATUS_INITIALIZED);
18117 if (val == NULL)
18118 continue;
18119 if (die == NULL)
18120 die = gen_call_site_die (decl, subr_die, ca_loc);
18121 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18122 NULL_TREE);
18123 if (reg != NULL)
18124 add_AT_loc (cdie, DW_AT_location, reg);
18125 else if (tdie != NULL)
18126 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18127 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18128 if (next_arg != XEXP (arg, 1))
18129 {
18130 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18131 if (mode == VOIDmode)
18132 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18133 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18134 0), 1),
18135 mode, VOIDmode,
18136 VAR_INIT_STATUS_INITIALIZED);
18137 if (val != NULL)
18138 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18139 }
18140 }
18141 if (die == NULL
18142 && (ca_loc->symbol_ref || tloc))
18143 die = gen_call_site_die (decl, subr_die, ca_loc);
18144 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18145 {
18146 dw_loc_descr_ref tval = NULL;
18147
18148 if (tloc != NULL_RTX)
18149 tval = mem_loc_descriptor (tloc,
18150 GET_MODE (tloc) == VOIDmode
18151 ? Pmode : GET_MODE (tloc),
18152 VOIDmode,
18153 VAR_INIT_STATUS_INITIALIZED);
18154 if (tval)
18155 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18156 else if (tlocc != NULL_RTX)
18157 {
18158 tval = mem_loc_descriptor (tlocc,
18159 GET_MODE (tlocc) == VOIDmode
18160 ? Pmode : GET_MODE (tlocc),
18161 VOIDmode,
18162 VAR_INIT_STATUS_INITIALIZED);
18163 if (tval)
18164 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18165 tval);
18166 }
18167 }
18168 if (die != NULL)
18169 {
18170 call_site_note_count++;
18171 if (ca_loc->tail_call_p)
18172 tail_call_site_note_count++;
18173 }
18174 }
18175 }
18176 call_arg_locations = NULL;
18177 call_arg_loc_last = NULL;
18178 if (tail_call_site_count >= 0
18179 && tail_call_site_count == tail_call_site_note_count
18180 && !dwarf_strict)
18181 {
18182 if (call_site_count >= 0
18183 && call_site_count == call_site_note_count)
18184 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18185 else
18186 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18187 }
18188 call_site_count = -1;
18189 tail_call_site_count = -1;
18190 }
18191 /* Add the calling convention attribute if requested. */
18192 add_calling_convention_attribute (subr_die, decl);
18193
18194 }
18195
18196 /* Returns a hash value for X (which really is a die_struct). */
18197
18198 static hashval_t
18199 common_block_die_table_hash (const void *x)
18200 {
18201 const_dw_die_ref d = (const_dw_die_ref) x;
18202 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18203 }
18204
18205 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18206 as decl_id and die_parent of die_struct Y. */
18207
18208 static int
18209 common_block_die_table_eq (const void *x, const void *y)
18210 {
18211 const_dw_die_ref d = (const_dw_die_ref) x;
18212 const_dw_die_ref e = (const_dw_die_ref) y;
18213 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18214 }
18215
18216 /* Generate a DIE to represent a declared data object.
18217 Either DECL or ORIGIN must be non-null. */
18218
18219 static void
18220 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18221 {
18222 HOST_WIDE_INT off = 0;
18223 tree com_decl;
18224 tree decl_or_origin = decl ? decl : origin;
18225 tree ultimate_origin;
18226 dw_die_ref var_die;
18227 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18228 dw_die_ref origin_die;
18229 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18230 || class_or_namespace_scope_p (context_die));
18231 bool specialization_p = false;
18232
18233 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18234 if (decl || ultimate_origin)
18235 origin = ultimate_origin;
18236 com_decl = fortran_common (decl_or_origin, &off);
18237
18238 /* Symbol in common gets emitted as a child of the common block, in the form
18239 of a data member. */
18240 if (com_decl)
18241 {
18242 dw_die_ref com_die;
18243 dw_loc_list_ref loc;
18244 die_node com_die_arg;
18245
18246 var_die = lookup_decl_die (decl_or_origin);
18247 if (var_die)
18248 {
18249 if (get_AT (var_die, DW_AT_location) == NULL)
18250 {
18251 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18252 if (loc)
18253 {
18254 if (off)
18255 {
18256 /* Optimize the common case. */
18257 if (single_element_loc_list_p (loc)
18258 && loc->expr->dw_loc_opc == DW_OP_addr
18259 && loc->expr->dw_loc_next == NULL
18260 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18261 == SYMBOL_REF)
18262 {
18263 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18264 loc->expr->dw_loc_oprnd1.v.val_addr
18265 = plus_constant (GET_MODE (x), x , off);
18266 }
18267 else
18268 loc_list_plus_const (loc, off);
18269 }
18270 add_AT_location_description (var_die, DW_AT_location, loc);
18271 remove_AT (var_die, DW_AT_declaration);
18272 }
18273 }
18274 return;
18275 }
18276
18277 if (common_block_die_table == NULL)
18278 common_block_die_table
18279 = htab_create_ggc (10, common_block_die_table_hash,
18280 common_block_die_table_eq, NULL);
18281
18282 com_die_arg.decl_id = DECL_UID (com_decl);
18283 com_die_arg.die_parent = context_die;
18284 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18285 loc = loc_list_from_tree (com_decl, 2);
18286 if (com_die == NULL)
18287 {
18288 const char *cnam
18289 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18290 void **slot;
18291
18292 com_die = new_die (DW_TAG_common_block, context_die, decl);
18293 add_name_and_src_coords_attributes (com_die, com_decl);
18294 if (loc)
18295 {
18296 add_AT_location_description (com_die, DW_AT_location, loc);
18297 /* Avoid sharing the same loc descriptor between
18298 DW_TAG_common_block and DW_TAG_variable. */
18299 loc = loc_list_from_tree (com_decl, 2);
18300 }
18301 else if (DECL_EXTERNAL (decl))
18302 add_AT_flag (com_die, DW_AT_declaration, 1);
18303 if (want_pubnames ())
18304 add_pubname_string (cnam, com_die); /* ??? needed? */
18305 com_die->decl_id = DECL_UID (com_decl);
18306 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18307 *slot = (void *) com_die;
18308 }
18309 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18310 {
18311 add_AT_location_description (com_die, DW_AT_location, loc);
18312 loc = loc_list_from_tree (com_decl, 2);
18313 remove_AT (com_die, DW_AT_declaration);
18314 }
18315 var_die = new_die (DW_TAG_variable, com_die, decl);
18316 add_name_and_src_coords_attributes (var_die, decl);
18317 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18318 TREE_THIS_VOLATILE (decl), context_die);
18319 add_AT_flag (var_die, DW_AT_external, 1);
18320 if (loc)
18321 {
18322 if (off)
18323 {
18324 /* Optimize the common case. */
18325 if (single_element_loc_list_p (loc)
18326 && loc->expr->dw_loc_opc == DW_OP_addr
18327 && loc->expr->dw_loc_next == NULL
18328 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18329 {
18330 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18331 loc->expr->dw_loc_oprnd1.v.val_addr
18332 = plus_constant (GET_MODE (x), x, off);
18333 }
18334 else
18335 loc_list_plus_const (loc, off);
18336 }
18337 add_AT_location_description (var_die, DW_AT_location, loc);
18338 }
18339 else if (DECL_EXTERNAL (decl))
18340 add_AT_flag (var_die, DW_AT_declaration, 1);
18341 equate_decl_number_to_die (decl, var_die);
18342 return;
18343 }
18344
18345 /* If the compiler emitted a definition for the DECL declaration
18346 and if we already emitted a DIE for it, don't emit a second
18347 DIE for it again. Allow re-declarations of DECLs that are
18348 inside functions, though. */
18349 if (old_die && declaration && !local_scope_p (context_die))
18350 return;
18351
18352 /* For static data members, the declaration in the class is supposed
18353 to have DW_TAG_member tag; the specification should still be
18354 DW_TAG_variable referencing the DW_TAG_member DIE. */
18355 if (declaration && class_scope_p (context_die))
18356 var_die = new_die (DW_TAG_member, context_die, decl);
18357 else
18358 var_die = new_die (DW_TAG_variable, context_die, decl);
18359
18360 origin_die = NULL;
18361 if (origin != NULL)
18362 origin_die = add_abstract_origin_attribute (var_die, origin);
18363
18364 /* Loop unrolling can create multiple blocks that refer to the same
18365 static variable, so we must test for the DW_AT_declaration flag.
18366
18367 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18368 copy decls and set the DECL_ABSTRACT flag on them instead of
18369 sharing them.
18370
18371 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18372
18373 ??? The declare_in_namespace support causes us to get two DIEs for one
18374 variable, both of which are declarations. We want to avoid considering
18375 one to be a specification, so we must test that this DIE is not a
18376 declaration. */
18377 else if (old_die && TREE_STATIC (decl) && ! declaration
18378 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18379 {
18380 /* This is a definition of a C++ class level static. */
18381 add_AT_specification (var_die, old_die);
18382 specialization_p = true;
18383 if (DECL_NAME (decl))
18384 {
18385 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18386 struct dwarf_file_data * file_index = lookup_filename (s.file);
18387
18388 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18389 add_AT_file (var_die, DW_AT_decl_file, file_index);
18390
18391 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18392 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18393
18394 if (old_die->die_tag == DW_TAG_member)
18395 add_linkage_name (var_die, decl);
18396 }
18397 }
18398 else
18399 add_name_and_src_coords_attributes (var_die, decl);
18400
18401 if ((origin == NULL && !specialization_p)
18402 || (origin != NULL
18403 && !DECL_ABSTRACT (decl_or_origin)
18404 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18405 decl_function_context
18406 (decl_or_origin))))
18407 {
18408 tree type = TREE_TYPE (decl_or_origin);
18409
18410 if (decl_by_reference_p (decl_or_origin))
18411 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18412 else
18413 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18414 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18415 }
18416
18417 if (origin == NULL && !specialization_p)
18418 {
18419 if (TREE_PUBLIC (decl))
18420 add_AT_flag (var_die, DW_AT_external, 1);
18421
18422 if (DECL_ARTIFICIAL (decl))
18423 add_AT_flag (var_die, DW_AT_artificial, 1);
18424
18425 add_accessibility_attribute (var_die, decl);
18426 }
18427
18428 if (declaration)
18429 add_AT_flag (var_die, DW_AT_declaration, 1);
18430
18431 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18432 equate_decl_number_to_die (decl, var_die);
18433
18434 if (! declaration
18435 && (! DECL_ABSTRACT (decl_or_origin)
18436 /* Local static vars are shared between all clones/inlines,
18437 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18438 already set. */
18439 || (TREE_CODE (decl_or_origin) == VAR_DECL
18440 && TREE_STATIC (decl_or_origin)
18441 && DECL_RTL_SET_P (decl_or_origin)))
18442 /* When abstract origin already has DW_AT_location attribute, no need
18443 to add it again. */
18444 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18445 {
18446 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18447 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18448 defer_location (decl_or_origin, var_die);
18449 else
18450 add_location_or_const_value_attribute (var_die, decl_or_origin,
18451 decl == NULL, DW_AT_location);
18452 add_pubname (decl_or_origin, var_die);
18453 }
18454 else
18455 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18456 }
18457
18458 /* Generate a DIE to represent a named constant. */
18459
18460 static void
18461 gen_const_die (tree decl, dw_die_ref context_die)
18462 {
18463 dw_die_ref const_die;
18464 tree type = TREE_TYPE (decl);
18465
18466 const_die = new_die (DW_TAG_constant, context_die, decl);
18467 add_name_and_src_coords_attributes (const_die, decl);
18468 add_type_attribute (const_die, type, 1, 0, context_die);
18469 if (TREE_PUBLIC (decl))
18470 add_AT_flag (const_die, DW_AT_external, 1);
18471 if (DECL_ARTIFICIAL (decl))
18472 add_AT_flag (const_die, DW_AT_artificial, 1);
18473 tree_add_const_value_attribute_for_decl (const_die, decl);
18474 }
18475
18476 /* Generate a DIE to represent a label identifier. */
18477
18478 static void
18479 gen_label_die (tree decl, dw_die_ref context_die)
18480 {
18481 tree origin = decl_ultimate_origin (decl);
18482 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18483 rtx insn;
18484 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18485
18486 if (origin != NULL)
18487 add_abstract_origin_attribute (lbl_die, origin);
18488 else
18489 add_name_and_src_coords_attributes (lbl_die, decl);
18490
18491 if (DECL_ABSTRACT (decl))
18492 equate_decl_number_to_die (decl, lbl_die);
18493 else
18494 {
18495 insn = DECL_RTL_IF_SET (decl);
18496
18497 /* Deleted labels are programmer specified labels which have been
18498 eliminated because of various optimizations. We still emit them
18499 here so that it is possible to put breakpoints on them. */
18500 if (insn
18501 && (LABEL_P (insn)
18502 || ((NOTE_P (insn)
18503 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18504 {
18505 /* When optimization is enabled (via -O) some parts of the compiler
18506 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18507 represent source-level labels which were explicitly declared by
18508 the user. This really shouldn't be happening though, so catch
18509 it if it ever does happen. */
18510 gcc_assert (!INSN_DELETED_P (insn));
18511
18512 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18513 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18514 }
18515 else if (insn
18516 && NOTE_P (insn)
18517 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18518 && CODE_LABEL_NUMBER (insn) != -1)
18519 {
18520 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18521 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18522 }
18523 }
18524 }
18525
18526 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18527 attributes to the DIE for a block STMT, to describe where the inlined
18528 function was called from. This is similar to add_src_coords_attributes. */
18529
18530 static inline void
18531 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18532 {
18533 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18534
18535 if (dwarf_version >= 3 || !dwarf_strict)
18536 {
18537 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18538 add_AT_unsigned (die, DW_AT_call_line, s.line);
18539 }
18540 }
18541
18542
18543 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18544 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18545
18546 static inline void
18547 add_high_low_attributes (tree stmt, dw_die_ref die)
18548 {
18549 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18550
18551 if (BLOCK_FRAGMENT_CHAIN (stmt)
18552 && (dwarf_version >= 3 || !dwarf_strict))
18553 {
18554 tree chain, superblock = NULL_TREE;
18555 dw_die_ref pdie;
18556 dw_attr_ref attr = NULL;
18557
18558 if (inlined_function_outer_scope_p (stmt))
18559 {
18560 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18561 BLOCK_NUMBER (stmt));
18562 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18563 }
18564
18565 /* Optimize duplicate .debug_ranges lists or even tails of
18566 lists. If this BLOCK has same ranges as its supercontext,
18567 lookup DW_AT_ranges attribute in the supercontext (and
18568 recursively so), verify that the ranges_table contains the
18569 right values and use it instead of adding a new .debug_range. */
18570 for (chain = stmt, pdie = die;
18571 BLOCK_SAME_RANGE (chain);
18572 chain = BLOCK_SUPERCONTEXT (chain))
18573 {
18574 dw_attr_ref new_attr;
18575
18576 pdie = pdie->die_parent;
18577 if (pdie == NULL)
18578 break;
18579 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18580 break;
18581 new_attr = get_AT (pdie, DW_AT_ranges);
18582 if (new_attr == NULL
18583 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18584 break;
18585 attr = new_attr;
18586 superblock = BLOCK_SUPERCONTEXT (chain);
18587 }
18588 if (attr != NULL
18589 && (ranges_table[attr->dw_attr_val.v.val_offset
18590 / 2 / DWARF2_ADDR_SIZE].num
18591 == BLOCK_NUMBER (superblock))
18592 && BLOCK_FRAGMENT_CHAIN (superblock))
18593 {
18594 unsigned long off = attr->dw_attr_val.v.val_offset
18595 / 2 / DWARF2_ADDR_SIZE;
18596 unsigned long supercnt = 0, thiscnt = 0;
18597 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18598 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18599 {
18600 ++supercnt;
18601 gcc_checking_assert (ranges_table[off + supercnt].num
18602 == BLOCK_NUMBER (chain));
18603 }
18604 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18605 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18606 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18607 ++thiscnt;
18608 gcc_assert (supercnt >= thiscnt);
18609 add_AT_range_list (die, DW_AT_ranges,
18610 ((off + supercnt - thiscnt)
18611 * 2 * DWARF2_ADDR_SIZE),
18612 false);
18613 return;
18614 }
18615
18616 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18617
18618 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18619 do
18620 {
18621 add_ranges (chain);
18622 chain = BLOCK_FRAGMENT_CHAIN (chain);
18623 }
18624 while (chain);
18625 add_ranges (NULL);
18626 }
18627 else
18628 {
18629 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18630 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18631 BLOCK_NUMBER (stmt));
18632 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18633 BLOCK_NUMBER (stmt));
18634 add_AT_low_high_pc (die, label, label_high, false);
18635 }
18636 }
18637
18638 /* Generate a DIE for a lexical block. */
18639
18640 static void
18641 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18642 {
18643 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18644
18645 if (call_arg_locations)
18646 {
18647 if (block_map.length () <= BLOCK_NUMBER (stmt))
18648 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18649 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18650 }
18651
18652 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18653 add_high_low_attributes (stmt, stmt_die);
18654
18655 decls_for_scope (stmt, stmt_die, depth);
18656 }
18657
18658 /* Generate a DIE for an inlined subprogram. */
18659
18660 static void
18661 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18662 {
18663 tree decl;
18664
18665 /* The instance of function that is effectively being inlined shall not
18666 be abstract. */
18667 gcc_assert (! BLOCK_ABSTRACT (stmt));
18668
18669 decl = block_ultimate_origin (stmt);
18670
18671 /* Emit info for the abstract instance first, if we haven't yet. We
18672 must emit this even if the block is abstract, otherwise when we
18673 emit the block below (or elsewhere), we may end up trying to emit
18674 a die whose origin die hasn't been emitted, and crashing. */
18675 dwarf2out_abstract_function (decl);
18676
18677 if (! BLOCK_ABSTRACT (stmt))
18678 {
18679 dw_die_ref subr_die
18680 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18681
18682 if (call_arg_locations)
18683 {
18684 if (block_map.length () <= BLOCK_NUMBER (stmt))
18685 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18686 block_map[BLOCK_NUMBER (stmt)] = subr_die;
18687 }
18688 add_abstract_origin_attribute (subr_die, decl);
18689 if (TREE_ASM_WRITTEN (stmt))
18690 add_high_low_attributes (stmt, subr_die);
18691 add_call_src_coords_attributes (stmt, subr_die);
18692
18693 decls_for_scope (stmt, subr_die, depth);
18694 current_function_has_inlines = 1;
18695 }
18696 }
18697
18698 /* Generate a DIE for a field in a record, or structure. */
18699
18700 static void
18701 gen_field_die (tree decl, dw_die_ref context_die)
18702 {
18703 dw_die_ref decl_die;
18704
18705 if (TREE_TYPE (decl) == error_mark_node)
18706 return;
18707
18708 decl_die = new_die (DW_TAG_member, context_die, decl);
18709 add_name_and_src_coords_attributes (decl_die, decl);
18710 add_type_attribute (decl_die, member_declared_type (decl),
18711 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18712 context_die);
18713
18714 if (DECL_BIT_FIELD_TYPE (decl))
18715 {
18716 add_byte_size_attribute (decl_die, decl);
18717 add_bit_size_attribute (decl_die, decl);
18718 add_bit_offset_attribute (decl_die, decl);
18719 }
18720
18721 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18722 add_data_member_location_attribute (decl_die, decl);
18723
18724 if (DECL_ARTIFICIAL (decl))
18725 add_AT_flag (decl_die, DW_AT_artificial, 1);
18726
18727 add_accessibility_attribute (decl_die, decl);
18728
18729 /* Equate decl number to die, so that we can look up this decl later on. */
18730 equate_decl_number_to_die (decl, decl_die);
18731 }
18732
18733 #if 0
18734 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18735 Use modified_type_die instead.
18736 We keep this code here just in case these types of DIEs may be needed to
18737 represent certain things in other languages (e.g. Pascal) someday. */
18738
18739 static void
18740 gen_pointer_type_die (tree type, dw_die_ref context_die)
18741 {
18742 dw_die_ref ptr_die
18743 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18744
18745 equate_type_number_to_die (type, ptr_die);
18746 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18747 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18748 }
18749
18750 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18751 Use modified_type_die instead.
18752 We keep this code here just in case these types of DIEs may be needed to
18753 represent certain things in other languages (e.g. Pascal) someday. */
18754
18755 static void
18756 gen_reference_type_die (tree type, dw_die_ref context_die)
18757 {
18758 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18759
18760 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18761 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18762 else
18763 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18764
18765 equate_type_number_to_die (type, ref_die);
18766 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18767 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18768 }
18769 #endif
18770
18771 /* Generate a DIE for a pointer to a member type. */
18772
18773 static void
18774 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18775 {
18776 dw_die_ref ptr_die
18777 = new_die (DW_TAG_ptr_to_member_type,
18778 scope_die_for (type, context_die), type);
18779
18780 equate_type_number_to_die (type, ptr_die);
18781 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18782 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18783 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18784 }
18785
18786 typedef const char *dchar_p; /* For DEF_VEC_P. */
18787
18788 static char *producer_string;
18789
18790 /* Return a heap allocated producer string including command line options
18791 if -grecord-gcc-switches. */
18792
18793 static char *
18794 gen_producer_string (void)
18795 {
18796 size_t j;
18797 vec<dchar_p> switches = vNULL;
18798 const char *language_string = lang_hooks.name;
18799 char *producer, *tail;
18800 const char *p;
18801 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18802 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18803
18804 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18805 switch (save_decoded_options[j].opt_index)
18806 {
18807 case OPT_o:
18808 case OPT_d:
18809 case OPT_dumpbase:
18810 case OPT_dumpdir:
18811 case OPT_auxbase:
18812 case OPT_auxbase_strip:
18813 case OPT_quiet:
18814 case OPT_version:
18815 case OPT_v:
18816 case OPT_w:
18817 case OPT_L:
18818 case OPT_D:
18819 case OPT_I:
18820 case OPT_U:
18821 case OPT_SPECIAL_unknown:
18822 case OPT_SPECIAL_ignore:
18823 case OPT_SPECIAL_program_name:
18824 case OPT_SPECIAL_input_file:
18825 case OPT_grecord_gcc_switches:
18826 case OPT_gno_record_gcc_switches:
18827 case OPT__output_pch_:
18828 case OPT_fdiagnostics_show_location_:
18829 case OPT_fdiagnostics_show_option:
18830 case OPT_fdiagnostics_show_caret:
18831 case OPT_fverbose_asm:
18832 case OPT____:
18833 case OPT__sysroot_:
18834 case OPT_nostdinc:
18835 case OPT_nostdinc__:
18836 /* Ignore these. */
18837 continue;
18838 default:
18839 if (cl_options[save_decoded_options[j].opt_index].flags
18840 & CL_NO_DWARF_RECORD)
18841 continue;
18842 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18843 == '-');
18844 switch (save_decoded_options[j].canonical_option[0][1])
18845 {
18846 case 'M':
18847 case 'i':
18848 case 'W':
18849 continue;
18850 case 'f':
18851 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18852 "dump", 4) == 0)
18853 continue;
18854 break;
18855 default:
18856 break;
18857 }
18858 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
18859 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18860 break;
18861 }
18862
18863 producer = XNEWVEC (char, plen + 1 + len + 1);
18864 tail = producer;
18865 sprintf (tail, "%s %s", language_string, version_string);
18866 tail += plen;
18867
18868 FOR_EACH_VEC_ELT (switches, j, p)
18869 {
18870 len = strlen (p);
18871 *tail = ' ';
18872 memcpy (tail + 1, p, len);
18873 tail += len + 1;
18874 }
18875
18876 *tail = '\0';
18877 switches.release ();
18878 return producer;
18879 }
18880
18881 /* Generate the DIE for the compilation unit. */
18882
18883 static dw_die_ref
18884 gen_compile_unit_die (const char *filename)
18885 {
18886 dw_die_ref die;
18887 const char *language_string = lang_hooks.name;
18888 int language;
18889
18890 die = new_die (DW_TAG_compile_unit, NULL, NULL);
18891
18892 if (filename)
18893 {
18894 add_name_attribute (die, filename);
18895 /* Don't add cwd for <built-in>. */
18896 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18897 add_comp_dir_attribute (die);
18898 }
18899
18900 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
18901
18902 /* If our producer is LTO try to figure out a common language to use
18903 from the global list of translation units. */
18904 if (strcmp (language_string, "GNU GIMPLE") == 0)
18905 {
18906 unsigned i;
18907 tree t;
18908 const char *common_lang = NULL;
18909
18910 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
18911 {
18912 if (!TRANSLATION_UNIT_LANGUAGE (t))
18913 continue;
18914 if (!common_lang)
18915 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18916 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18917 ;
18918 else if (strncmp (common_lang, "GNU C", 5) == 0
18919 && strncmp(TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18920 /* Mixing C and C++ is ok, use C++ in that case. */
18921 common_lang = "GNU C++";
18922 else
18923 {
18924 /* Fall back to C. */
18925 common_lang = NULL;
18926 break;
18927 }
18928 }
18929
18930 if (common_lang)
18931 language_string = common_lang;
18932 }
18933
18934 language = DW_LANG_C89;
18935 if (strcmp (language_string, "GNU C++") == 0)
18936 language = DW_LANG_C_plus_plus;
18937 else if (strcmp (language_string, "GNU F77") == 0)
18938 language = DW_LANG_Fortran77;
18939 else if (strcmp (language_string, "GNU Pascal") == 0)
18940 language = DW_LANG_Pascal83;
18941 else if (dwarf_version >= 3 || !dwarf_strict)
18942 {
18943 if (strcmp (language_string, "GNU Ada") == 0)
18944 language = DW_LANG_Ada95;
18945 else if (strcmp (language_string, "GNU Fortran") == 0)
18946 language = DW_LANG_Fortran95;
18947 else if (strcmp (language_string, "GNU Java") == 0)
18948 language = DW_LANG_Java;
18949 else if (strcmp (language_string, "GNU Objective-C") == 0)
18950 language = DW_LANG_ObjC;
18951 else if (strcmp (language_string, "GNU Objective-C++") == 0)
18952 language = DW_LANG_ObjC_plus_plus;
18953 else if (dwarf_version >= 5 || !dwarf_strict)
18954 {
18955 if (strcmp (language_string, "GNU Go") == 0)
18956 language = DW_LANG_Go;
18957 }
18958 }
18959 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
18960 else if (strcmp (language_string, "GNU Fortran") == 0)
18961 language = DW_LANG_Fortran90;
18962
18963 add_AT_unsigned (die, DW_AT_language, language);
18964
18965 switch (language)
18966 {
18967 case DW_LANG_Fortran77:
18968 case DW_LANG_Fortran90:
18969 case DW_LANG_Fortran95:
18970 /* Fortran has case insensitive identifiers and the front-end
18971 lowercases everything. */
18972 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18973 break;
18974 default:
18975 /* The default DW_ID_case_sensitive doesn't need to be specified. */
18976 break;
18977 }
18978 return die;
18979 }
18980
18981 /* Generate the DIE for a base class. */
18982
18983 static void
18984 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18985 {
18986 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18987
18988 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18989 add_data_member_location_attribute (die, binfo);
18990
18991 if (BINFO_VIRTUAL_P (binfo))
18992 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18993
18994 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18995 children, otherwise the default is DW_ACCESS_public. In DWARF2
18996 the default has always been DW_ACCESS_private. */
18997 if (access == access_public_node)
18998 {
18999 if (dwarf_version == 2
19000 || context_die->die_tag == DW_TAG_class_type)
19001 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19002 }
19003 else if (access == access_protected_node)
19004 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19005 else if (dwarf_version > 2
19006 && context_die->die_tag != DW_TAG_class_type)
19007 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19008 }
19009
19010 /* Generate a DIE for a class member. */
19011
19012 static void
19013 gen_member_die (tree type, dw_die_ref context_die)
19014 {
19015 tree member;
19016 tree binfo = TYPE_BINFO (type);
19017 dw_die_ref child;
19018
19019 /* If this is not an incomplete type, output descriptions of each of its
19020 members. Note that as we output the DIEs necessary to represent the
19021 members of this record or union type, we will also be trying to output
19022 DIEs to represent the *types* of those members. However the `type'
19023 function (above) will specifically avoid generating type DIEs for member
19024 types *within* the list of member DIEs for this (containing) type except
19025 for those types (of members) which are explicitly marked as also being
19026 members of this (containing) type themselves. The g++ front- end can
19027 force any given type to be treated as a member of some other (containing)
19028 type by setting the TYPE_CONTEXT of the given (member) type to point to
19029 the TREE node representing the appropriate (containing) type. */
19030
19031 /* First output info about the base classes. */
19032 if (binfo)
19033 {
19034 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19035 int i;
19036 tree base;
19037
19038 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19039 gen_inheritance_die (base,
19040 (accesses ? (*accesses)[i] : access_public_node),
19041 context_die);
19042 }
19043
19044 /* Now output info about the data members and type members. */
19045 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19046 {
19047 /* If we thought we were generating minimal debug info for TYPE
19048 and then changed our minds, some of the member declarations
19049 may have already been defined. Don't define them again, but
19050 do put them in the right order. */
19051
19052 child = lookup_decl_die (member);
19053 if (child)
19054 splice_child_die (context_die, child);
19055 else
19056 gen_decl_die (member, NULL, context_die);
19057 }
19058
19059 /* Now output info about the function members (if any). */
19060 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19061 {
19062 /* Don't include clones in the member list. */
19063 if (DECL_ABSTRACT_ORIGIN (member))
19064 continue;
19065
19066 child = lookup_decl_die (member);
19067 if (child)
19068 splice_child_die (context_die, child);
19069 else
19070 gen_decl_die (member, NULL, context_die);
19071 }
19072 }
19073
19074 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19075 is set, we pretend that the type was never defined, so we only get the
19076 member DIEs needed by later specification DIEs. */
19077
19078 static void
19079 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19080 enum debug_info_usage usage)
19081 {
19082 dw_die_ref type_die = lookup_type_die (type);
19083 dw_die_ref scope_die = 0;
19084 int nested = 0;
19085 int complete = (TYPE_SIZE (type)
19086 && (! TYPE_STUB_DECL (type)
19087 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19088 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19089 complete = complete && should_emit_struct_debug (type, usage);
19090
19091 if (type_die && ! complete)
19092 return;
19093
19094 if (TYPE_CONTEXT (type) != NULL_TREE
19095 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19096 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19097 nested = 1;
19098
19099 scope_die = scope_die_for (type, context_die);
19100
19101 /* Generate child dies for template paramaters. */
19102 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19103 schedule_generic_params_dies_gen (type);
19104
19105 if (! type_die || (nested && is_cu_die (scope_die)))
19106 /* First occurrence of type or toplevel definition of nested class. */
19107 {
19108 dw_die_ref old_die = type_die;
19109
19110 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19111 ? record_type_tag (type) : DW_TAG_union_type,
19112 scope_die, type);
19113 equate_type_number_to_die (type, type_die);
19114 if (old_die)
19115 add_AT_specification (type_die, old_die);
19116 else
19117 add_name_attribute (type_die, type_tag (type));
19118 }
19119 else
19120 remove_AT (type_die, DW_AT_declaration);
19121
19122 /* If this type has been completed, then give it a byte_size attribute and
19123 then give a list of members. */
19124 if (complete && !ns_decl)
19125 {
19126 /* Prevent infinite recursion in cases where the type of some member of
19127 this type is expressed in terms of this type itself. */
19128 TREE_ASM_WRITTEN (type) = 1;
19129 add_byte_size_attribute (type_die, type);
19130 if (TYPE_STUB_DECL (type) != NULL_TREE)
19131 {
19132 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19133 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19134 }
19135
19136 /* If the first reference to this type was as the return type of an
19137 inline function, then it may not have a parent. Fix this now. */
19138 if (type_die->die_parent == NULL)
19139 add_child_die (scope_die, type_die);
19140
19141 push_decl_scope (type);
19142 gen_member_die (type, type_die);
19143 pop_decl_scope ();
19144
19145 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19146 if (TYPE_ARTIFICIAL (type))
19147 add_AT_flag (type_die, DW_AT_artificial, 1);
19148
19149 /* GNU extension: Record what type our vtable lives in. */
19150 if (TYPE_VFIELD (type))
19151 {
19152 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19153
19154 gen_type_die (vtype, context_die);
19155 add_AT_die_ref (type_die, DW_AT_containing_type,
19156 lookup_type_die (vtype));
19157 }
19158 }
19159 else
19160 {
19161 add_AT_flag (type_die, DW_AT_declaration, 1);
19162
19163 /* We don't need to do this for function-local types. */
19164 if (TYPE_STUB_DECL (type)
19165 && ! decl_function_context (TYPE_STUB_DECL (type)))
19166 vec_safe_push (incomplete_types, type);
19167 }
19168
19169 if (get_AT (type_die, DW_AT_name))
19170 add_pubtype (type, type_die);
19171 }
19172
19173 /* Generate a DIE for a subroutine _type_. */
19174
19175 static void
19176 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19177 {
19178 tree return_type = TREE_TYPE (type);
19179 dw_die_ref subr_die
19180 = new_die (DW_TAG_subroutine_type,
19181 scope_die_for (type, context_die), type);
19182
19183 equate_type_number_to_die (type, subr_die);
19184 add_prototyped_attribute (subr_die, type);
19185 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19186 gen_formal_types_die (type, subr_die);
19187
19188 if (get_AT (subr_die, DW_AT_name))
19189 add_pubtype (type, subr_die);
19190 }
19191
19192 /* Generate a DIE for a type definition. */
19193
19194 static void
19195 gen_typedef_die (tree decl, dw_die_ref context_die)
19196 {
19197 dw_die_ref type_die;
19198 tree origin;
19199
19200 if (TREE_ASM_WRITTEN (decl))
19201 return;
19202
19203 TREE_ASM_WRITTEN (decl) = 1;
19204 type_die = new_die (DW_TAG_typedef, context_die, decl);
19205 origin = decl_ultimate_origin (decl);
19206 if (origin != NULL)
19207 add_abstract_origin_attribute (type_die, origin);
19208 else
19209 {
19210 tree type;
19211
19212 add_name_and_src_coords_attributes (type_die, decl);
19213 if (DECL_ORIGINAL_TYPE (decl))
19214 {
19215 type = DECL_ORIGINAL_TYPE (decl);
19216
19217 gcc_assert (type != TREE_TYPE (decl));
19218 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19219 }
19220 else
19221 {
19222 type = TREE_TYPE (decl);
19223
19224 if (is_naming_typedef_decl (TYPE_NAME (type)))
19225 {
19226 /* Here, we are in the case of decl being a typedef naming
19227 an anonymous type, e.g:
19228 typedef struct {...} foo;
19229 In that case TREE_TYPE (decl) is not a typedef variant
19230 type and TYPE_NAME of the anonymous type is set to the
19231 TYPE_DECL of the typedef. This construct is emitted by
19232 the C++ FE.
19233
19234 TYPE is the anonymous struct named by the typedef
19235 DECL. As we need the DW_AT_type attribute of the
19236 DW_TAG_typedef to point to the DIE of TYPE, let's
19237 generate that DIE right away. add_type_attribute
19238 called below will then pick (via lookup_type_die) that
19239 anonymous struct DIE. */
19240 if (!TREE_ASM_WRITTEN (type))
19241 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19242
19243 /* This is a GNU Extension. We are adding a
19244 DW_AT_linkage_name attribute to the DIE of the
19245 anonymous struct TYPE. The value of that attribute
19246 is the name of the typedef decl naming the anonymous
19247 struct. This greatly eases the work of consumers of
19248 this debug info. */
19249 add_linkage_attr (lookup_type_die (type), decl);
19250 }
19251 }
19252
19253 add_type_attribute (type_die, type, TREE_READONLY (decl),
19254 TREE_THIS_VOLATILE (decl), context_die);
19255
19256 if (is_naming_typedef_decl (decl))
19257 /* We want that all subsequent calls to lookup_type_die with
19258 TYPE in argument yield the DW_TAG_typedef we have just
19259 created. */
19260 equate_type_number_to_die (type, type_die);
19261
19262 add_accessibility_attribute (type_die, decl);
19263 }
19264
19265 if (DECL_ABSTRACT (decl))
19266 equate_decl_number_to_die (decl, type_die);
19267
19268 if (get_AT (type_die, DW_AT_name))
19269 add_pubtype (decl, type_die);
19270 }
19271
19272 /* Generate a DIE for a struct, class, enum or union type. */
19273
19274 static void
19275 gen_tagged_type_die (tree type,
19276 dw_die_ref context_die,
19277 enum debug_info_usage usage)
19278 {
19279 int need_pop;
19280
19281 if (type == NULL_TREE
19282 || !is_tagged_type (type))
19283 return;
19284
19285 /* If this is a nested type whose containing class hasn't been written
19286 out yet, writing it out will cover this one, too. This does not apply
19287 to instantiations of member class templates; they need to be added to
19288 the containing class as they are generated. FIXME: This hurts the
19289 idea of combining type decls from multiple TUs, since we can't predict
19290 what set of template instantiations we'll get. */
19291 if (TYPE_CONTEXT (type)
19292 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19293 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19294 {
19295 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19296
19297 if (TREE_ASM_WRITTEN (type))
19298 return;
19299
19300 /* If that failed, attach ourselves to the stub. */
19301 push_decl_scope (TYPE_CONTEXT (type));
19302 context_die = lookup_type_die (TYPE_CONTEXT (type));
19303 need_pop = 1;
19304 }
19305 else if (TYPE_CONTEXT (type) != NULL_TREE
19306 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19307 {
19308 /* If this type is local to a function that hasn't been written
19309 out yet, use a NULL context for now; it will be fixed up in
19310 decls_for_scope. */
19311 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19312 /* A declaration DIE doesn't count; nested types need to go in the
19313 specification. */
19314 if (context_die && is_declaration_die (context_die))
19315 context_die = NULL;
19316 need_pop = 0;
19317 }
19318 else
19319 {
19320 context_die = declare_in_namespace (type, context_die);
19321 need_pop = 0;
19322 }
19323
19324 if (TREE_CODE (type) == ENUMERAL_TYPE)
19325 {
19326 /* This might have been written out by the call to
19327 declare_in_namespace. */
19328 if (!TREE_ASM_WRITTEN (type))
19329 gen_enumeration_type_die (type, context_die);
19330 }
19331 else
19332 gen_struct_or_union_type_die (type, context_die, usage);
19333
19334 if (need_pop)
19335 pop_decl_scope ();
19336
19337 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19338 it up if it is ever completed. gen_*_type_die will set it for us
19339 when appropriate. */
19340 }
19341
19342 /* Generate a type description DIE. */
19343
19344 static void
19345 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19346 enum debug_info_usage usage)
19347 {
19348 struct array_descr_info info;
19349
19350 if (type == NULL_TREE || type == error_mark_node)
19351 return;
19352
19353 if (TYPE_NAME (type) != NULL_TREE
19354 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19355 && is_redundant_typedef (TYPE_NAME (type))
19356 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19357 /* The DECL of this type is a typedef we don't want to emit debug
19358 info for but we want debug info for its underlying typedef.
19359 This can happen for e.g, the injected-class-name of a C++
19360 type. */
19361 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19362
19363 /* If TYPE is a typedef type variant, let's generate debug info
19364 for the parent typedef which TYPE is a type of. */
19365 if (typedef_variant_p (type))
19366 {
19367 if (TREE_ASM_WRITTEN (type))
19368 return;
19369
19370 /* Prevent broken recursion; we can't hand off to the same type. */
19371 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19372
19373 /* Give typedefs the right scope. */
19374 context_die = scope_die_for (type, context_die);
19375
19376 TREE_ASM_WRITTEN (type) = 1;
19377
19378 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19379 return;
19380 }
19381
19382 /* If type is an anonymous tagged type named by a typedef, let's
19383 generate debug info for the typedef. */
19384 if (is_naming_typedef_decl (TYPE_NAME (type)))
19385 {
19386 /* Use the DIE of the containing namespace as the parent DIE of
19387 the type description DIE we want to generate. */
19388 if (DECL_CONTEXT (TYPE_NAME (type))
19389 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19390 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19391
19392 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19393 return;
19394 }
19395
19396 /* If this is an array type with hidden descriptor, handle it first. */
19397 if (!TREE_ASM_WRITTEN (type)
19398 && lang_hooks.types.get_array_descr_info
19399 && lang_hooks.types.get_array_descr_info (type, &info)
19400 && (dwarf_version >= 3 || !dwarf_strict))
19401 {
19402 gen_descr_array_type_die (type, &info, context_die);
19403 TREE_ASM_WRITTEN (type) = 1;
19404 return;
19405 }
19406
19407 /* We are going to output a DIE to represent the unqualified version
19408 of this type (i.e. without any const or volatile qualifiers) so
19409 get the main variant (i.e. the unqualified version) of this type
19410 now. (Vectors are special because the debugging info is in the
19411 cloned type itself). */
19412 if (TREE_CODE (type) != VECTOR_TYPE)
19413 type = type_main_variant (type);
19414
19415 if (TREE_ASM_WRITTEN (type))
19416 return;
19417
19418 switch (TREE_CODE (type))
19419 {
19420 case ERROR_MARK:
19421 break;
19422
19423 case POINTER_TYPE:
19424 case REFERENCE_TYPE:
19425 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19426 ensures that the gen_type_die recursion will terminate even if the
19427 type is recursive. Recursive types are possible in Ada. */
19428 /* ??? We could perhaps do this for all types before the switch
19429 statement. */
19430 TREE_ASM_WRITTEN (type) = 1;
19431
19432 /* For these types, all that is required is that we output a DIE (or a
19433 set of DIEs) to represent the "basis" type. */
19434 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19435 DINFO_USAGE_IND_USE);
19436 break;
19437
19438 case OFFSET_TYPE:
19439 /* This code is used for C++ pointer-to-data-member types.
19440 Output a description of the relevant class type. */
19441 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19442 DINFO_USAGE_IND_USE);
19443
19444 /* Output a description of the type of the object pointed to. */
19445 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19446 DINFO_USAGE_IND_USE);
19447
19448 /* Now output a DIE to represent this pointer-to-data-member type
19449 itself. */
19450 gen_ptr_to_mbr_type_die (type, context_die);
19451 break;
19452
19453 case FUNCTION_TYPE:
19454 /* Force out return type (in case it wasn't forced out already). */
19455 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19456 DINFO_USAGE_DIR_USE);
19457 gen_subroutine_type_die (type, context_die);
19458 break;
19459
19460 case METHOD_TYPE:
19461 /* Force out return type (in case it wasn't forced out already). */
19462 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19463 DINFO_USAGE_DIR_USE);
19464 gen_subroutine_type_die (type, context_die);
19465 break;
19466
19467 case ARRAY_TYPE:
19468 gen_array_type_die (type, context_die);
19469 break;
19470
19471 case VECTOR_TYPE:
19472 gen_array_type_die (type, context_die);
19473 break;
19474
19475 case ENUMERAL_TYPE:
19476 case RECORD_TYPE:
19477 case UNION_TYPE:
19478 case QUAL_UNION_TYPE:
19479 gen_tagged_type_die (type, context_die, usage);
19480 return;
19481
19482 case VOID_TYPE:
19483 case INTEGER_TYPE:
19484 case REAL_TYPE:
19485 case FIXED_POINT_TYPE:
19486 case COMPLEX_TYPE:
19487 case BOOLEAN_TYPE:
19488 /* No DIEs needed for fundamental types. */
19489 break;
19490
19491 case NULLPTR_TYPE:
19492 case LANG_TYPE:
19493 /* Just use DW_TAG_unspecified_type. */
19494 {
19495 dw_die_ref type_die = lookup_type_die (type);
19496 if (type_die == NULL)
19497 {
19498 tree name = TYPE_NAME (type);
19499 if (TREE_CODE (name) == TYPE_DECL)
19500 name = DECL_NAME (name);
19501 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19502 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19503 equate_type_number_to_die (type, type_die);
19504 }
19505 }
19506 break;
19507
19508 default:
19509 gcc_unreachable ();
19510 }
19511
19512 TREE_ASM_WRITTEN (type) = 1;
19513 }
19514
19515 static void
19516 gen_type_die (tree type, dw_die_ref context_die)
19517 {
19518 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19519 }
19520
19521 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19522 things which are local to the given block. */
19523
19524 static void
19525 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19526 {
19527 int must_output_die = 0;
19528 bool inlined_func;
19529
19530 /* Ignore blocks that are NULL. */
19531 if (stmt == NULL_TREE)
19532 return;
19533
19534 inlined_func = inlined_function_outer_scope_p (stmt);
19535
19536 /* If the block is one fragment of a non-contiguous block, do not
19537 process the variables, since they will have been done by the
19538 origin block. Do process subblocks. */
19539 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19540 {
19541 tree sub;
19542
19543 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19544 gen_block_die (sub, context_die, depth + 1);
19545
19546 return;
19547 }
19548
19549 /* Determine if we need to output any Dwarf DIEs at all to represent this
19550 block. */
19551 if (inlined_func)
19552 /* The outer scopes for inlinings *must* always be represented. We
19553 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19554 must_output_die = 1;
19555 else
19556 {
19557 /* Determine if this block directly contains any "significant"
19558 local declarations which we will need to output DIEs for. */
19559 if (debug_info_level > DINFO_LEVEL_TERSE)
19560 /* We are not in terse mode so *any* local declaration counts
19561 as being a "significant" one. */
19562 must_output_die = ((BLOCK_VARS (stmt) != NULL
19563 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19564 && (TREE_USED (stmt)
19565 || TREE_ASM_WRITTEN (stmt)
19566 || BLOCK_ABSTRACT (stmt)));
19567 else if ((TREE_USED (stmt)
19568 || TREE_ASM_WRITTEN (stmt)
19569 || BLOCK_ABSTRACT (stmt))
19570 && !dwarf2out_ignore_block (stmt))
19571 must_output_die = 1;
19572 }
19573
19574 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19575 DIE for any block which contains no significant local declarations at
19576 all. Rather, in such cases we just call `decls_for_scope' so that any
19577 needed Dwarf info for any sub-blocks will get properly generated. Note
19578 that in terse mode, our definition of what constitutes a "significant"
19579 local declaration gets restricted to include only inlined function
19580 instances and local (nested) function definitions. */
19581 if (must_output_die)
19582 {
19583 if (inlined_func)
19584 {
19585 /* If STMT block is abstract, that means we have been called
19586 indirectly from dwarf2out_abstract_function.
19587 That function rightfully marks the descendent blocks (of
19588 the abstract function it is dealing with) as being abstract,
19589 precisely to prevent us from emitting any
19590 DW_TAG_inlined_subroutine DIE as a descendent
19591 of an abstract function instance. So in that case, we should
19592 not call gen_inlined_subroutine_die.
19593
19594 Later though, when cgraph asks dwarf2out to emit info
19595 for the concrete instance of the function decl into which
19596 the concrete instance of STMT got inlined, the later will lead
19597 to the generation of a DW_TAG_inlined_subroutine DIE. */
19598 if (! BLOCK_ABSTRACT (stmt))
19599 gen_inlined_subroutine_die (stmt, context_die, depth);
19600 }
19601 else
19602 gen_lexical_block_die (stmt, context_die, depth);
19603 }
19604 else
19605 decls_for_scope (stmt, context_die, depth);
19606 }
19607
19608 /* Process variable DECL (or variable with origin ORIGIN) within
19609 block STMT and add it to CONTEXT_DIE. */
19610 static void
19611 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19612 {
19613 dw_die_ref die;
19614 tree decl_or_origin = decl ? decl : origin;
19615
19616 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19617 die = lookup_decl_die (decl_or_origin);
19618 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19619 && TYPE_DECL_IS_STUB (decl_or_origin))
19620 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19621 else
19622 die = NULL;
19623
19624 if (die != NULL && die->die_parent == NULL)
19625 add_child_die (context_die, die);
19626 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19627 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19628 stmt, context_die);
19629 else
19630 gen_decl_die (decl, origin, context_die);
19631 }
19632
19633 /* Generate all of the decls declared within a given scope and (recursively)
19634 all of its sub-blocks. */
19635
19636 static void
19637 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19638 {
19639 tree decl;
19640 unsigned int i;
19641 tree subblocks;
19642
19643 /* Ignore NULL blocks. */
19644 if (stmt == NULL_TREE)
19645 return;
19646
19647 /* Output the DIEs to represent all of the data objects and typedefs
19648 declared directly within this block but not within any nested
19649 sub-blocks. Also, nested function and tag DIEs have been
19650 generated with a parent of NULL; fix that up now. */
19651 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19652 process_scope_var (stmt, decl, NULL_TREE, context_die);
19653 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19654 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19655 context_die);
19656
19657 /* If we're at -g1, we're not interested in subblocks. */
19658 if (debug_info_level <= DINFO_LEVEL_TERSE)
19659 return;
19660
19661 /* Output the DIEs to represent all sub-blocks (and the items declared
19662 therein) of this block. */
19663 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19664 subblocks != NULL;
19665 subblocks = BLOCK_CHAIN (subblocks))
19666 gen_block_die (subblocks, context_die, depth + 1);
19667 }
19668
19669 /* Is this a typedef we can avoid emitting? */
19670
19671 static inline int
19672 is_redundant_typedef (const_tree decl)
19673 {
19674 if (TYPE_DECL_IS_STUB (decl))
19675 return 1;
19676
19677 if (DECL_ARTIFICIAL (decl)
19678 && DECL_CONTEXT (decl)
19679 && is_tagged_type (DECL_CONTEXT (decl))
19680 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19681 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19682 /* Also ignore the artificial member typedef for the class name. */
19683 return 1;
19684
19685 return 0;
19686 }
19687
19688 /* Return TRUE if TYPE is a typedef that names a type for linkage
19689 purposes. This kind of typedefs is produced by the C++ FE for
19690 constructs like:
19691
19692 typedef struct {...} foo;
19693
19694 In that case, there is no typedef variant type produced for foo.
19695 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19696 struct type. */
19697
19698 static bool
19699 is_naming_typedef_decl (const_tree decl)
19700 {
19701 if (decl == NULL_TREE
19702 || TREE_CODE (decl) != TYPE_DECL
19703 || !is_tagged_type (TREE_TYPE (decl))
19704 || DECL_IS_BUILTIN (decl)
19705 || is_redundant_typedef (decl)
19706 /* It looks like Ada produces TYPE_DECLs that are very similar
19707 to C++ naming typedefs but that have different
19708 semantics. Let's be specific to c++ for now. */
19709 || !is_cxx ())
19710 return FALSE;
19711
19712 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19713 && TYPE_NAME (TREE_TYPE (decl)) == decl
19714 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19715 != TYPE_NAME (TREE_TYPE (decl))));
19716 }
19717
19718 /* Returns the DIE for a context. */
19719
19720 static inline dw_die_ref
19721 get_context_die (tree context)
19722 {
19723 if (context)
19724 {
19725 /* Find die that represents this context. */
19726 if (TYPE_P (context))
19727 {
19728 context = TYPE_MAIN_VARIANT (context);
19729 return strip_naming_typedef (context, force_type_die (context));
19730 }
19731 else
19732 return force_decl_die (context);
19733 }
19734 return comp_unit_die ();
19735 }
19736
19737 /* Returns the DIE for decl. A DIE will always be returned. */
19738
19739 static dw_die_ref
19740 force_decl_die (tree decl)
19741 {
19742 dw_die_ref decl_die;
19743 unsigned saved_external_flag;
19744 tree save_fn = NULL_TREE;
19745 decl_die = lookup_decl_die (decl);
19746 if (!decl_die)
19747 {
19748 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19749
19750 decl_die = lookup_decl_die (decl);
19751 if (decl_die)
19752 return decl_die;
19753
19754 switch (TREE_CODE (decl))
19755 {
19756 case FUNCTION_DECL:
19757 /* Clear current_function_decl, so that gen_subprogram_die thinks
19758 that this is a declaration. At this point, we just want to force
19759 declaration die. */
19760 save_fn = current_function_decl;
19761 current_function_decl = NULL_TREE;
19762 gen_subprogram_die (decl, context_die);
19763 current_function_decl = save_fn;
19764 break;
19765
19766 case VAR_DECL:
19767 /* Set external flag to force declaration die. Restore it after
19768 gen_decl_die() call. */
19769 saved_external_flag = DECL_EXTERNAL (decl);
19770 DECL_EXTERNAL (decl) = 1;
19771 gen_decl_die (decl, NULL, context_die);
19772 DECL_EXTERNAL (decl) = saved_external_flag;
19773 break;
19774
19775 case NAMESPACE_DECL:
19776 if (dwarf_version >= 3 || !dwarf_strict)
19777 dwarf2out_decl (decl);
19778 else
19779 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19780 decl_die = comp_unit_die ();
19781 break;
19782
19783 case TRANSLATION_UNIT_DECL:
19784 decl_die = comp_unit_die ();
19785 break;
19786
19787 default:
19788 gcc_unreachable ();
19789 }
19790
19791 /* We should be able to find the DIE now. */
19792 if (!decl_die)
19793 decl_die = lookup_decl_die (decl);
19794 gcc_assert (decl_die);
19795 }
19796
19797 return decl_die;
19798 }
19799
19800 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19801 always returned. */
19802
19803 static dw_die_ref
19804 force_type_die (tree type)
19805 {
19806 dw_die_ref type_die;
19807
19808 type_die = lookup_type_die (type);
19809 if (!type_die)
19810 {
19811 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19812
19813 type_die = modified_type_die (type, TYPE_READONLY (type),
19814 TYPE_VOLATILE (type), context_die);
19815 gcc_assert (type_die);
19816 }
19817 return type_die;
19818 }
19819
19820 /* Force out any required namespaces to be able to output DECL,
19821 and return the new context_die for it, if it's changed. */
19822
19823 static dw_die_ref
19824 setup_namespace_context (tree thing, dw_die_ref context_die)
19825 {
19826 tree context = (DECL_P (thing)
19827 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19828 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19829 /* Force out the namespace. */
19830 context_die = force_decl_die (context);
19831
19832 return context_die;
19833 }
19834
19835 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19836 type) within its namespace, if appropriate.
19837
19838 For compatibility with older debuggers, namespace DIEs only contain
19839 declarations; all definitions are emitted at CU scope. */
19840
19841 static dw_die_ref
19842 declare_in_namespace (tree thing, dw_die_ref context_die)
19843 {
19844 dw_die_ref ns_context;
19845
19846 if (debug_info_level <= DINFO_LEVEL_TERSE)
19847 return context_die;
19848
19849 /* If this decl is from an inlined function, then don't try to emit it in its
19850 namespace, as we will get confused. It would have already been emitted
19851 when the abstract instance of the inline function was emitted anyways. */
19852 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19853 return context_die;
19854
19855 ns_context = setup_namespace_context (thing, context_die);
19856
19857 if (ns_context != context_die)
19858 {
19859 if (is_fortran ())
19860 return ns_context;
19861 if (DECL_P (thing))
19862 gen_decl_die (thing, NULL, ns_context);
19863 else
19864 gen_type_die (thing, ns_context);
19865 }
19866 return context_die;
19867 }
19868
19869 /* Generate a DIE for a namespace or namespace alias. */
19870
19871 static void
19872 gen_namespace_die (tree decl, dw_die_ref context_die)
19873 {
19874 dw_die_ref namespace_die;
19875
19876 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19877 they are an alias of. */
19878 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19879 {
19880 /* Output a real namespace or module. */
19881 context_die = setup_namespace_context (decl, comp_unit_die ());
19882 namespace_die = new_die (is_fortran ()
19883 ? DW_TAG_module : DW_TAG_namespace,
19884 context_die, decl);
19885 /* For Fortran modules defined in different CU don't add src coords. */
19886 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19887 {
19888 const char *name = dwarf2_name (decl, 0);
19889 if (name)
19890 add_name_attribute (namespace_die, name);
19891 }
19892 else
19893 add_name_and_src_coords_attributes (namespace_die, decl);
19894 if (DECL_EXTERNAL (decl))
19895 add_AT_flag (namespace_die, DW_AT_declaration, 1);
19896 equate_decl_number_to_die (decl, namespace_die);
19897 }
19898 else
19899 {
19900 /* Output a namespace alias. */
19901
19902 /* Force out the namespace we are an alias of, if necessary. */
19903 dw_die_ref origin_die
19904 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19905
19906 if (DECL_FILE_SCOPE_P (decl)
19907 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19908 context_die = setup_namespace_context (decl, comp_unit_die ());
19909 /* Now create the namespace alias DIE. */
19910 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19911 add_name_and_src_coords_attributes (namespace_die, decl);
19912 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19913 equate_decl_number_to_die (decl, namespace_die);
19914 }
19915 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
19916 if (want_pubnames ())
19917 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
19918 }
19919
19920 /* Generate Dwarf debug information for a decl described by DECL.
19921 The return value is currently only meaningful for PARM_DECLs,
19922 for all other decls it returns NULL. */
19923
19924 static dw_die_ref
19925 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19926 {
19927 tree decl_or_origin = decl ? decl : origin;
19928 tree class_origin = NULL, ultimate_origin;
19929
19930 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19931 return NULL;
19932
19933 switch (TREE_CODE (decl_or_origin))
19934 {
19935 case ERROR_MARK:
19936 break;
19937
19938 case CONST_DECL:
19939 if (!is_fortran () && !is_ada ())
19940 {
19941 /* The individual enumerators of an enum type get output when we output
19942 the Dwarf representation of the relevant enum type itself. */
19943 break;
19944 }
19945
19946 /* Emit its type. */
19947 gen_type_die (TREE_TYPE (decl), context_die);
19948
19949 /* And its containing namespace. */
19950 context_die = declare_in_namespace (decl, context_die);
19951
19952 gen_const_die (decl, context_die);
19953 break;
19954
19955 case FUNCTION_DECL:
19956 /* Don't output any DIEs to represent mere function declarations,
19957 unless they are class members or explicit block externs. */
19958 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19959 && DECL_FILE_SCOPE_P (decl_or_origin)
19960 && (current_function_decl == NULL_TREE
19961 || DECL_ARTIFICIAL (decl_or_origin)))
19962 break;
19963
19964 #if 0
19965 /* FIXME */
19966 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19967 on local redeclarations of global functions. That seems broken. */
19968 if (current_function_decl != decl)
19969 /* This is only a declaration. */;
19970 #endif
19971
19972 /* If we're emitting a clone, emit info for the abstract instance. */
19973 if (origin || DECL_ORIGIN (decl) != decl)
19974 dwarf2out_abstract_function (origin
19975 ? DECL_ORIGIN (origin)
19976 : DECL_ABSTRACT_ORIGIN (decl));
19977
19978 /* If we're emitting an out-of-line copy of an inline function,
19979 emit info for the abstract instance and set up to refer to it. */
19980 else if (cgraph_function_possibly_inlined_p (decl)
19981 && ! DECL_ABSTRACT (decl)
19982 && ! class_or_namespace_scope_p (context_die)
19983 /* dwarf2out_abstract_function won't emit a die if this is just
19984 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
19985 that case, because that works only if we have a die. */
19986 && DECL_INITIAL (decl) != NULL_TREE)
19987 {
19988 dwarf2out_abstract_function (decl);
19989 set_decl_origin_self (decl);
19990 }
19991
19992 /* Otherwise we're emitting the primary DIE for this decl. */
19993 else if (debug_info_level > DINFO_LEVEL_TERSE)
19994 {
19995 /* Before we describe the FUNCTION_DECL itself, make sure that we
19996 have its containing type. */
19997 if (!origin)
19998 origin = decl_class_context (decl);
19999 if (origin != NULL_TREE)
20000 gen_type_die (origin, context_die);
20001
20002 /* And its return type. */
20003 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20004
20005 /* And its virtual context. */
20006 if (DECL_VINDEX (decl) != NULL_TREE)
20007 gen_type_die (DECL_CONTEXT (decl), context_die);
20008
20009 /* Make sure we have a member DIE for decl. */
20010 if (origin != NULL_TREE)
20011 gen_type_die_for_member (origin, decl, context_die);
20012
20013 /* And its containing namespace. */
20014 context_die = declare_in_namespace (decl, context_die);
20015 }
20016
20017 /* Now output a DIE to represent the function itself. */
20018 if (decl)
20019 gen_subprogram_die (decl, context_die);
20020 break;
20021
20022 case TYPE_DECL:
20023 /* If we are in terse mode, don't generate any DIEs to represent any
20024 actual typedefs. */
20025 if (debug_info_level <= DINFO_LEVEL_TERSE)
20026 break;
20027
20028 /* In the special case of a TYPE_DECL node representing the declaration
20029 of some type tag, if the given TYPE_DECL is marked as having been
20030 instantiated from some other (original) TYPE_DECL node (e.g. one which
20031 was generated within the original definition of an inline function) we
20032 used to generate a special (abbreviated) DW_TAG_structure_type,
20033 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20034 should be actually referencing those DIEs, as variable DIEs with that
20035 type would be emitted already in the abstract origin, so it was always
20036 removed during unused type prunning. Don't add anything in this
20037 case. */
20038 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20039 break;
20040
20041 if (is_redundant_typedef (decl))
20042 gen_type_die (TREE_TYPE (decl), context_die);
20043 else
20044 /* Output a DIE to represent the typedef itself. */
20045 gen_typedef_die (decl, context_die);
20046 break;
20047
20048 case LABEL_DECL:
20049 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20050 gen_label_die (decl, context_die);
20051 break;
20052
20053 case VAR_DECL:
20054 case RESULT_DECL:
20055 /* If we are in terse mode, don't generate any DIEs to represent any
20056 variable declarations or definitions. */
20057 if (debug_info_level <= DINFO_LEVEL_TERSE)
20058 break;
20059
20060 /* Output any DIEs that are needed to specify the type of this data
20061 object. */
20062 if (decl_by_reference_p (decl_or_origin))
20063 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20064 else
20065 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20066
20067 /* And its containing type. */
20068 class_origin = decl_class_context (decl_or_origin);
20069 if (class_origin != NULL_TREE)
20070 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20071
20072 /* And its containing namespace. */
20073 context_die = declare_in_namespace (decl_or_origin, context_die);
20074
20075 /* Now output the DIE to represent the data object itself. This gets
20076 complicated because of the possibility that the VAR_DECL really
20077 represents an inlined instance of a formal parameter for an inline
20078 function. */
20079 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20080 if (ultimate_origin != NULL_TREE
20081 && TREE_CODE (ultimate_origin) == PARM_DECL)
20082 gen_formal_parameter_die (decl, origin,
20083 true /* Emit name attribute. */,
20084 context_die);
20085 else
20086 gen_variable_die (decl, origin, context_die);
20087 break;
20088
20089 case FIELD_DECL:
20090 /* Ignore the nameless fields that are used to skip bits but handle C++
20091 anonymous unions and structs. */
20092 if (DECL_NAME (decl) != NULL_TREE
20093 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20094 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20095 {
20096 gen_type_die (member_declared_type (decl), context_die);
20097 gen_field_die (decl, context_die);
20098 }
20099 break;
20100
20101 case PARM_DECL:
20102 if (DECL_BY_REFERENCE (decl_or_origin))
20103 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20104 else
20105 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20106 return gen_formal_parameter_die (decl, origin,
20107 true /* Emit name attribute. */,
20108 context_die);
20109
20110 case NAMESPACE_DECL:
20111 case IMPORTED_DECL:
20112 if (dwarf_version >= 3 || !dwarf_strict)
20113 gen_namespace_die (decl, context_die);
20114 break;
20115
20116 default:
20117 /* Probably some frontend-internal decl. Assume we don't care. */
20118 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20119 break;
20120 }
20121
20122 return NULL;
20123 }
20124 \f
20125 /* Output debug information for global decl DECL. Called from toplev.c after
20126 compilation proper has finished. */
20127
20128 static void
20129 dwarf2out_global_decl (tree decl)
20130 {
20131 /* Output DWARF2 information for file-scope tentative data object
20132 declarations, file-scope (extern) function declarations (which
20133 had no corresponding body) and file-scope tagged type declarations
20134 and definitions which have not yet been forced out. */
20135 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20136 dwarf2out_decl (decl);
20137 }
20138
20139 /* Output debug information for type decl DECL. Called from toplev.c
20140 and from language front ends (to record built-in types). */
20141 static void
20142 dwarf2out_type_decl (tree decl, int local)
20143 {
20144 if (!local)
20145 dwarf2out_decl (decl);
20146 }
20147
20148 /* Output debug information for imported module or decl DECL.
20149 NAME is non-NULL name in the lexical block if the decl has been renamed.
20150 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20151 that DECL belongs to.
20152 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20153 static void
20154 dwarf2out_imported_module_or_decl_1 (tree decl,
20155 tree name,
20156 tree lexical_block,
20157 dw_die_ref lexical_block_die)
20158 {
20159 expanded_location xloc;
20160 dw_die_ref imported_die = NULL;
20161 dw_die_ref at_import_die;
20162
20163 if (TREE_CODE (decl) == IMPORTED_DECL)
20164 {
20165 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20166 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20167 gcc_assert (decl);
20168 }
20169 else
20170 xloc = expand_location (input_location);
20171
20172 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20173 {
20174 at_import_die = force_type_die (TREE_TYPE (decl));
20175 /* For namespace N { typedef void T; } using N::T; base_type_die
20176 returns NULL, but DW_TAG_imported_declaration requires
20177 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20178 if (!at_import_die)
20179 {
20180 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20181 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20182 at_import_die = lookup_type_die (TREE_TYPE (decl));
20183 gcc_assert (at_import_die);
20184 }
20185 }
20186 else
20187 {
20188 at_import_die = lookup_decl_die (decl);
20189 if (!at_import_die)
20190 {
20191 /* If we're trying to avoid duplicate debug info, we may not have
20192 emitted the member decl for this field. Emit it now. */
20193 if (TREE_CODE (decl) == FIELD_DECL)
20194 {
20195 tree type = DECL_CONTEXT (decl);
20196
20197 if (TYPE_CONTEXT (type)
20198 && TYPE_P (TYPE_CONTEXT (type))
20199 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20200 DINFO_USAGE_DIR_USE))
20201 return;
20202 gen_type_die_for_member (type, decl,
20203 get_context_die (TYPE_CONTEXT (type)));
20204 }
20205 at_import_die = force_decl_die (decl);
20206 }
20207 }
20208
20209 if (TREE_CODE (decl) == NAMESPACE_DECL)
20210 {
20211 if (dwarf_version >= 3 || !dwarf_strict)
20212 imported_die = new_die (DW_TAG_imported_module,
20213 lexical_block_die,
20214 lexical_block);
20215 else
20216 return;
20217 }
20218 else
20219 imported_die = new_die (DW_TAG_imported_declaration,
20220 lexical_block_die,
20221 lexical_block);
20222
20223 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20224 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20225 if (name)
20226 add_AT_string (imported_die, DW_AT_name,
20227 IDENTIFIER_POINTER (name));
20228 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20229 }
20230
20231 /* Output debug information for imported module or decl DECL.
20232 NAME is non-NULL name in context if the decl has been renamed.
20233 CHILD is true if decl is one of the renamed decls as part of
20234 importing whole module. */
20235
20236 static void
20237 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20238 bool child)
20239 {
20240 /* dw_die_ref at_import_die; */
20241 dw_die_ref scope_die;
20242
20243 if (debug_info_level <= DINFO_LEVEL_TERSE)
20244 return;
20245
20246 gcc_assert (decl);
20247
20248 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20249 We need decl DIE for reference and scope die. First, get DIE for the decl
20250 itself. */
20251
20252 /* Get the scope die for decl context. Use comp_unit_die for global module
20253 or decl. If die is not found for non globals, force new die. */
20254 if (context
20255 && TYPE_P (context)
20256 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20257 return;
20258
20259 if (!(dwarf_version >= 3 || !dwarf_strict))
20260 return;
20261
20262 scope_die = get_context_die (context);
20263
20264 if (child)
20265 {
20266 gcc_assert (scope_die->die_child);
20267 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20268 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20269 scope_die = scope_die->die_child;
20270 }
20271
20272 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20273 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20274
20275 }
20276
20277 /* Write the debugging output for DECL. */
20278
20279 void
20280 dwarf2out_decl (tree decl)
20281 {
20282 dw_die_ref context_die = comp_unit_die ();
20283
20284 switch (TREE_CODE (decl))
20285 {
20286 case ERROR_MARK:
20287 return;
20288
20289 case FUNCTION_DECL:
20290 /* What we would really like to do here is to filter out all mere
20291 file-scope declarations of file-scope functions which are never
20292 referenced later within this translation unit (and keep all of ones
20293 that *are* referenced later on) but we aren't clairvoyant, so we have
20294 no idea which functions will be referenced in the future (i.e. later
20295 on within the current translation unit). So here we just ignore all
20296 file-scope function declarations which are not also definitions. If
20297 and when the debugger needs to know something about these functions,
20298 it will have to hunt around and find the DWARF information associated
20299 with the definition of the function.
20300
20301 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20302 nodes represent definitions and which ones represent mere
20303 declarations. We have to check DECL_INITIAL instead. That's because
20304 the C front-end supports some weird semantics for "extern inline"
20305 function definitions. These can get inlined within the current
20306 translation unit (and thus, we need to generate Dwarf info for their
20307 abstract instances so that the Dwarf info for the concrete inlined
20308 instances can have something to refer to) but the compiler never
20309 generates any out-of-lines instances of such things (despite the fact
20310 that they *are* definitions).
20311
20312 The important point is that the C front-end marks these "extern
20313 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20314 them anyway. Note that the C++ front-end also plays some similar games
20315 for inline function definitions appearing within include files which
20316 also contain `#pragma interface' pragmas.
20317
20318 If we are called from dwarf2out_abstract_function output a DIE
20319 anyway. We can end up here this way with early inlining and LTO
20320 where the inlined function is output in a different LTRANS unit
20321 or not at all. */
20322 if (DECL_INITIAL (decl) == NULL_TREE
20323 && ! DECL_ABSTRACT (decl))
20324 return;
20325
20326 /* If we're a nested function, initially use a parent of NULL; if we're
20327 a plain function, this will be fixed up in decls_for_scope. If
20328 we're a method, it will be ignored, since we already have a DIE. */
20329 if (decl_function_context (decl)
20330 /* But if we're in terse mode, we don't care about scope. */
20331 && debug_info_level > DINFO_LEVEL_TERSE)
20332 context_die = NULL;
20333 break;
20334
20335 case VAR_DECL:
20336 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20337 declaration and if the declaration was never even referenced from
20338 within this entire compilation unit. We suppress these DIEs in
20339 order to save space in the .debug section (by eliminating entries
20340 which are probably useless). Note that we must not suppress
20341 block-local extern declarations (whether used or not) because that
20342 would screw-up the debugger's name lookup mechanism and cause it to
20343 miss things which really ought to be in scope at a given point. */
20344 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20345 return;
20346
20347 /* For local statics lookup proper context die. */
20348 if (TREE_STATIC (decl)
20349 && DECL_CONTEXT (decl)
20350 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20351 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20352
20353 /* If we are in terse mode, don't generate any DIEs to represent any
20354 variable declarations or definitions. */
20355 if (debug_info_level <= DINFO_LEVEL_TERSE)
20356 return;
20357 break;
20358
20359 case CONST_DECL:
20360 if (debug_info_level <= DINFO_LEVEL_TERSE)
20361 return;
20362 if (!is_fortran () && !is_ada ())
20363 return;
20364 if (TREE_STATIC (decl) && decl_function_context (decl))
20365 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20366 break;
20367
20368 case NAMESPACE_DECL:
20369 case IMPORTED_DECL:
20370 if (debug_info_level <= DINFO_LEVEL_TERSE)
20371 return;
20372 if (lookup_decl_die (decl) != NULL)
20373 return;
20374 break;
20375
20376 case TYPE_DECL:
20377 /* Don't emit stubs for types unless they are needed by other DIEs. */
20378 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20379 return;
20380
20381 /* Don't bother trying to generate any DIEs to represent any of the
20382 normal built-in types for the language we are compiling. */
20383 if (DECL_IS_BUILTIN (decl))
20384 return;
20385
20386 /* If we are in terse mode, don't generate any DIEs for types. */
20387 if (debug_info_level <= DINFO_LEVEL_TERSE)
20388 return;
20389
20390 /* If we're a function-scope tag, initially use a parent of NULL;
20391 this will be fixed up in decls_for_scope. */
20392 if (decl_function_context (decl))
20393 context_die = NULL;
20394
20395 break;
20396
20397 default:
20398 return;
20399 }
20400
20401 gen_decl_die (decl, NULL, context_die);
20402 }
20403
20404 /* Write the debugging output for DECL. */
20405
20406 static void
20407 dwarf2out_function_decl (tree decl)
20408 {
20409 dwarf2out_decl (decl);
20410 call_arg_locations = NULL;
20411 call_arg_loc_last = NULL;
20412 call_site_count = -1;
20413 tail_call_site_count = -1;
20414 block_map.release ();
20415 htab_empty (decl_loc_table);
20416 htab_empty (cached_dw_loc_list_table);
20417 }
20418
20419 /* Output a marker (i.e. a label) for the beginning of the generated code for
20420 a lexical block. */
20421
20422 static void
20423 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20424 unsigned int blocknum)
20425 {
20426 switch_to_section (current_function_section ());
20427 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20428 }
20429
20430 /* Output a marker (i.e. a label) for the end of the generated code for a
20431 lexical block. */
20432
20433 static void
20434 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20435 {
20436 switch_to_section (current_function_section ());
20437 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20438 }
20439
20440 /* Returns nonzero if it is appropriate not to emit any debugging
20441 information for BLOCK, because it doesn't contain any instructions.
20442
20443 Don't allow this for blocks with nested functions or local classes
20444 as we would end up with orphans, and in the presence of scheduling
20445 we may end up calling them anyway. */
20446
20447 static bool
20448 dwarf2out_ignore_block (const_tree block)
20449 {
20450 tree decl;
20451 unsigned int i;
20452
20453 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20454 if (TREE_CODE (decl) == FUNCTION_DECL
20455 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20456 return 0;
20457 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20458 {
20459 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20460 if (TREE_CODE (decl) == FUNCTION_DECL
20461 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20462 return 0;
20463 }
20464
20465 return 1;
20466 }
20467
20468 /* Hash table routines for file_hash. */
20469
20470 static int
20471 file_table_eq (const void *p1_p, const void *p2_p)
20472 {
20473 const struct dwarf_file_data *const p1 =
20474 (const struct dwarf_file_data *) p1_p;
20475 const char *const p2 = (const char *) p2_p;
20476 return filename_cmp (p1->filename, p2) == 0;
20477 }
20478
20479 static hashval_t
20480 file_table_hash (const void *p_p)
20481 {
20482 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20483 return htab_hash_string (p->filename);
20484 }
20485
20486 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20487 dwarf2out.c) and return its "index". The index of each (known) filename is
20488 just a unique number which is associated with only that one filename. We
20489 need such numbers for the sake of generating labels (in the .debug_sfnames
20490 section) and references to those files numbers (in the .debug_srcinfo
20491 and.debug_macinfo sections). If the filename given as an argument is not
20492 found in our current list, add it to the list and assign it the next
20493 available unique index number. In order to speed up searches, we remember
20494 the index of the filename was looked up last. This handles the majority of
20495 all searches. */
20496
20497 static struct dwarf_file_data *
20498 lookup_filename (const char *file_name)
20499 {
20500 void ** slot;
20501 struct dwarf_file_data * created;
20502
20503 /* Check to see if the file name that was searched on the previous
20504 call matches this file name. If so, return the index. */
20505 if (file_table_last_lookup
20506 && (file_name == file_table_last_lookup->filename
20507 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20508 return file_table_last_lookup;
20509
20510 /* Didn't match the previous lookup, search the table. */
20511 slot = htab_find_slot_with_hash (file_table, file_name,
20512 htab_hash_string (file_name), INSERT);
20513 if (*slot)
20514 return (struct dwarf_file_data *) *slot;
20515
20516 created = ggc_alloc_dwarf_file_data ();
20517 created->filename = file_name;
20518 created->emitted_number = 0;
20519 *slot = created;
20520 return created;
20521 }
20522
20523 /* If the assembler will construct the file table, then translate the compiler
20524 internal file table number into the assembler file table number, and emit
20525 a .file directive if we haven't already emitted one yet. The file table
20526 numbers are different because we prune debug info for unused variables and
20527 types, which may include filenames. */
20528
20529 static int
20530 maybe_emit_file (struct dwarf_file_data * fd)
20531 {
20532 if (! fd->emitted_number)
20533 {
20534 if (last_emitted_file)
20535 fd->emitted_number = last_emitted_file->emitted_number + 1;
20536 else
20537 fd->emitted_number = 1;
20538 last_emitted_file = fd;
20539
20540 if (DWARF2_ASM_LINE_DEBUG_INFO)
20541 {
20542 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20543 output_quoted_string (asm_out_file,
20544 remap_debug_filename (fd->filename));
20545 fputc ('\n', asm_out_file);
20546 }
20547 }
20548
20549 return fd->emitted_number;
20550 }
20551
20552 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20553 That generation should happen after function debug info has been
20554 generated. The value of the attribute is the constant value of ARG. */
20555
20556 static void
20557 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20558 {
20559 die_arg_entry entry;
20560
20561 if (!die || !arg)
20562 return;
20563
20564 if (!tmpl_value_parm_die_table)
20565 vec_alloc (tmpl_value_parm_die_table, 32);
20566
20567 entry.die = die;
20568 entry.arg = arg;
20569 vec_safe_push (tmpl_value_parm_die_table, entry);
20570 }
20571
20572 /* Return TRUE if T is an instance of generic type, FALSE
20573 otherwise. */
20574
20575 static bool
20576 generic_type_p (tree t)
20577 {
20578 if (t == NULL_TREE || !TYPE_P (t))
20579 return false;
20580 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20581 }
20582
20583 /* Schedule the generation of the generic parameter dies for the
20584 instance of generic type T. The proper generation itself is later
20585 done by gen_scheduled_generic_parms_dies. */
20586
20587 static void
20588 schedule_generic_params_dies_gen (tree t)
20589 {
20590 if (!generic_type_p (t))
20591 return;
20592
20593 if (!generic_type_instances)
20594 vec_alloc (generic_type_instances, 256);
20595
20596 vec_safe_push (generic_type_instances, t);
20597 }
20598
20599 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20600 by append_entry_to_tmpl_value_parm_die_table. This function must
20601 be called after function DIEs have been generated. */
20602
20603 static void
20604 gen_remaining_tmpl_value_param_die_attribute (void)
20605 {
20606 if (tmpl_value_parm_die_table)
20607 {
20608 unsigned i;
20609 die_arg_entry *e;
20610
20611 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20612 tree_add_const_value_attribute (e->die, e->arg);
20613 }
20614 }
20615
20616 /* Generate generic parameters DIEs for instances of generic types
20617 that have been previously scheduled by
20618 schedule_generic_params_dies_gen. This function must be called
20619 after all the types of the CU have been laid out. */
20620
20621 static void
20622 gen_scheduled_generic_parms_dies (void)
20623 {
20624 unsigned i;
20625 tree t;
20626
20627 if (!generic_type_instances)
20628 return;
20629
20630 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20631 if (COMPLETE_TYPE_P (t))
20632 gen_generic_params_dies (t);
20633 }
20634
20635
20636 /* Replace DW_AT_name for the decl with name. */
20637
20638 static void
20639 dwarf2out_set_name (tree decl, tree name)
20640 {
20641 dw_die_ref die;
20642 dw_attr_ref attr;
20643 const char *dname;
20644
20645 die = TYPE_SYMTAB_DIE (decl);
20646 if (!die)
20647 return;
20648
20649 dname = dwarf2_name (name, 0);
20650 if (!dname)
20651 return;
20652
20653 attr = get_AT (die, DW_AT_name);
20654 if (attr)
20655 {
20656 struct indirect_string_node *node;
20657
20658 node = find_AT_string (dname);
20659 /* replace the string. */
20660 attr->dw_attr_val.v.val_str = node;
20661 }
20662
20663 else
20664 add_name_attribute (die, dname);
20665 }
20666
20667 /* True if before or during processing of the first function being emitted. */
20668 static bool in_first_function_p = true;
20669 /* True if loc_note during dwarf2out_var_location call might still be
20670 before first real instruction at address equal to .Ltext0. */
20671 static bool maybe_at_text_label_p = true;
20672 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
20673 static unsigned int first_loclabel_num_not_at_text_label;
20674
20675 /* Called by the final INSN scan whenever we see a var location. We
20676 use it to drop labels in the right places, and throw the location in
20677 our lookup table. */
20678
20679 static void
20680 dwarf2out_var_location (rtx loc_note)
20681 {
20682 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20683 struct var_loc_node *newloc;
20684 rtx next_real, next_note;
20685 static const char *last_label;
20686 static const char *last_postcall_label;
20687 static bool last_in_cold_section_p;
20688 static rtx expected_next_loc_note;
20689 tree decl;
20690 bool var_loc_p;
20691
20692 if (!NOTE_P (loc_note))
20693 {
20694 if (CALL_P (loc_note))
20695 {
20696 call_site_count++;
20697 if (SIBLING_CALL_P (loc_note))
20698 tail_call_site_count++;
20699 }
20700 return;
20701 }
20702
20703 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20704 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20705 return;
20706
20707 /* Optimize processing a large consecutive sequence of location
20708 notes so we don't spend too much time in next_real_insn. If the
20709 next insn is another location note, remember the next_real_insn
20710 calculation for next time. */
20711 next_real = cached_next_real_insn;
20712 if (next_real)
20713 {
20714 if (expected_next_loc_note != loc_note)
20715 next_real = NULL_RTX;
20716 }
20717
20718 next_note = NEXT_INSN (loc_note);
20719 if (! next_note
20720 || INSN_DELETED_P (next_note)
20721 || ! NOTE_P (next_note)
20722 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20723 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20724 next_note = NULL_RTX;
20725
20726 if (! next_real)
20727 next_real = next_real_insn (loc_note);
20728
20729 if (next_note)
20730 {
20731 expected_next_loc_note = next_note;
20732 cached_next_real_insn = next_real;
20733 }
20734 else
20735 cached_next_real_insn = NULL_RTX;
20736
20737 /* If there are no instructions which would be affected by this note,
20738 don't do anything. */
20739 if (var_loc_p
20740 && next_real == NULL_RTX
20741 && !NOTE_DURING_CALL_P (loc_note))
20742 return;
20743
20744 if (next_real == NULL_RTX)
20745 next_real = get_last_insn ();
20746
20747 /* If there were any real insns between note we processed last time
20748 and this note (or if it is the first note), clear
20749 last_{,postcall_}label so that they are not reused this time. */
20750 if (last_var_location_insn == NULL_RTX
20751 || last_var_location_insn != next_real
20752 || last_in_cold_section_p != in_cold_section_p)
20753 {
20754 last_label = NULL;
20755 last_postcall_label = NULL;
20756 }
20757
20758 if (var_loc_p)
20759 {
20760 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20761 newloc = add_var_loc_to_decl (decl, loc_note,
20762 NOTE_DURING_CALL_P (loc_note)
20763 ? last_postcall_label : last_label);
20764 if (newloc == NULL)
20765 return;
20766 }
20767 else
20768 {
20769 decl = NULL_TREE;
20770 newloc = NULL;
20771 }
20772
20773 /* If there were no real insns between note we processed last time
20774 and this note, use the label we emitted last time. Otherwise
20775 create a new label and emit it. */
20776 if (last_label == NULL)
20777 {
20778 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20779 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20780 loclabel_num++;
20781 last_label = ggc_strdup (loclabel);
20782 /* See if loclabel might be equal to .Ltext0. If yes,
20783 bump first_loclabel_num_not_at_text_label. */
20784 if (!have_multiple_function_sections
20785 && in_first_function_p
20786 && maybe_at_text_label_p)
20787 {
20788 static rtx last_start;
20789 rtx insn;
20790 for (insn = loc_note; insn; insn = previous_insn (insn))
20791 if (insn == last_start)
20792 break;
20793 else if (!NONDEBUG_INSN_P (insn))
20794 continue;
20795 else
20796 {
20797 rtx body = PATTERN (insn);
20798 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
20799 continue;
20800 /* Inline asm could occupy zero bytes. */
20801 else if (GET_CODE (body) == ASM_INPUT
20802 || asm_noperands (body) >= 0)
20803 continue;
20804 #ifdef HAVE_attr_length
20805 else if (get_attr_min_length (insn) == 0)
20806 continue;
20807 #endif
20808 else
20809 {
20810 /* Assume insn has non-zero length. */
20811 maybe_at_text_label_p = false;
20812 break;
20813 }
20814 }
20815 if (maybe_at_text_label_p)
20816 {
20817 last_start = loc_note;
20818 first_loclabel_num_not_at_text_label = loclabel_num;
20819 }
20820 }
20821 }
20822
20823 if (!var_loc_p)
20824 {
20825 struct call_arg_loc_node *ca_loc
20826 = ggc_alloc_cleared_call_arg_loc_node ();
20827 rtx prev = prev_real_insn (loc_note), x;
20828 ca_loc->call_arg_loc_note = loc_note;
20829 ca_loc->next = NULL;
20830 ca_loc->label = last_label;
20831 gcc_assert (prev
20832 && (CALL_P (prev)
20833 || (NONJUMP_INSN_P (prev)
20834 && GET_CODE (PATTERN (prev)) == SEQUENCE
20835 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20836 if (!CALL_P (prev))
20837 prev = XVECEXP (PATTERN (prev), 0, 0);
20838 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20839 x = get_call_rtx_from (PATTERN (prev));
20840 if (x)
20841 {
20842 x = XEXP (XEXP (x, 0), 0);
20843 if (GET_CODE (x) == SYMBOL_REF
20844 && SYMBOL_REF_DECL (x)
20845 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20846 ca_loc->symbol_ref = x;
20847 }
20848 ca_loc->block = insn_scope (prev);
20849 if (call_arg_locations)
20850 call_arg_loc_last->next = ca_loc;
20851 else
20852 call_arg_locations = ca_loc;
20853 call_arg_loc_last = ca_loc;
20854 }
20855 else if (!NOTE_DURING_CALL_P (loc_note))
20856 newloc->label = last_label;
20857 else
20858 {
20859 if (!last_postcall_label)
20860 {
20861 sprintf (loclabel, "%s-1", last_label);
20862 last_postcall_label = ggc_strdup (loclabel);
20863 }
20864 newloc->label = last_postcall_label;
20865 }
20866
20867 last_var_location_insn = next_real;
20868 last_in_cold_section_p = in_cold_section_p;
20869 }
20870
20871 /* Note in one location list that text section has changed. */
20872
20873 static int
20874 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20875 {
20876 var_loc_list *list = (var_loc_list *) *slot;
20877 if (list->first)
20878 list->last_before_switch
20879 = list->last->next ? list->last->next : list->last;
20880 return 1;
20881 }
20882
20883 /* Note in all location lists that text section has changed. */
20884
20885 static void
20886 var_location_switch_text_section (void)
20887 {
20888 if (decl_loc_table == NULL)
20889 return;
20890
20891 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20892 }
20893
20894 /* Create a new line number table. */
20895
20896 static dw_line_info_table *
20897 new_line_info_table (void)
20898 {
20899 dw_line_info_table *table;
20900
20901 table = ggc_alloc_cleared_dw_line_info_table_struct ();
20902 table->file_num = 1;
20903 table->line_num = 1;
20904 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20905
20906 return table;
20907 }
20908
20909 /* Lookup the "current" table into which we emit line info, so
20910 that we don't have to do it for every source line. */
20911
20912 static void
20913 set_cur_line_info_table (section *sec)
20914 {
20915 dw_line_info_table *table;
20916
20917 if (sec == text_section)
20918 table = text_section_line_info;
20919 else if (sec == cold_text_section)
20920 {
20921 table = cold_text_section_line_info;
20922 if (!table)
20923 {
20924 cold_text_section_line_info = table = new_line_info_table ();
20925 table->end_label = cold_end_label;
20926 }
20927 }
20928 else
20929 {
20930 const char *end_label;
20931
20932 if (flag_reorder_blocks_and_partition)
20933 {
20934 if (in_cold_section_p)
20935 end_label = crtl->subsections.cold_section_end_label;
20936 else
20937 end_label = crtl->subsections.hot_section_end_label;
20938 }
20939 else
20940 {
20941 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20942 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20943 current_function_funcdef_no);
20944 end_label = ggc_strdup (label);
20945 }
20946
20947 table = new_line_info_table ();
20948 table->end_label = end_label;
20949
20950 vec_safe_push (separate_line_info, table);
20951 }
20952
20953 if (DWARF2_ASM_LINE_DEBUG_INFO)
20954 table->is_stmt = (cur_line_info_table
20955 ? cur_line_info_table->is_stmt
20956 : DWARF_LINE_DEFAULT_IS_STMT_START);
20957 cur_line_info_table = table;
20958 }
20959
20960
20961 /* We need to reset the locations at the beginning of each
20962 function. We can't do this in the end_function hook, because the
20963 declarations that use the locations won't have been output when
20964 that hook is called. Also compute have_multiple_function_sections here. */
20965
20966 static void
20967 dwarf2out_begin_function (tree fun)
20968 {
20969 section *sec = function_section (fun);
20970
20971 if (sec != text_section)
20972 have_multiple_function_sections = true;
20973
20974 if (flag_reorder_blocks_and_partition && !cold_text_section)
20975 {
20976 gcc_assert (current_function_decl == fun);
20977 cold_text_section = unlikely_text_section ();
20978 switch_to_section (cold_text_section);
20979 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20980 switch_to_section (sec);
20981 }
20982
20983 dwarf2out_note_section_used ();
20984 call_site_count = 0;
20985 tail_call_site_count = 0;
20986
20987 set_cur_line_info_table (sec);
20988 }
20989
20990 /* Helper function of dwarf2out_end_function, called only after emitting
20991 the very first function into assembly. Check if some .debug_loc range
20992 might end with a .LVL* label that could be equal to .Ltext0.
20993 In that case we must force using absolute addresses in .debug_loc ranges,
20994 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
20995 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
20996 list terminator.
20997 Set have_multiple_function_sections to true in that case and
20998 terminate htab traversal. */
20999
21000 static int
21001 find_empty_loc_ranges_at_text_label (void **slot, void *)
21002 {
21003 var_loc_list *entry;
21004 struct var_loc_node *node;
21005
21006 entry = (var_loc_list *) *slot;
21007 node = entry->first;
21008 if (node && node->next && node->next->label)
21009 {
21010 unsigned int i;
21011 const char *label = node->next->label;
21012 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21013
21014 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21015 {
21016 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21017 if (strcmp (label, loclabel) == 0)
21018 {
21019 have_multiple_function_sections = true;
21020 return 0;
21021 }
21022 }
21023 }
21024 return 1;
21025 }
21026
21027 /* Hook called after emitting a function into assembly.
21028 This does something only for the very first function emitted. */
21029
21030 static void
21031 dwarf2out_end_function (unsigned int)
21032 {
21033 if (in_first_function_p
21034 && !have_multiple_function_sections
21035 && first_loclabel_num_not_at_text_label
21036 && decl_loc_table)
21037 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21038 NULL);
21039 in_first_function_p = false;
21040 maybe_at_text_label_p = false;
21041 }
21042
21043 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21044
21045 static void
21046 push_dw_line_info_entry (dw_line_info_table *table,
21047 enum dw_line_info_opcode opcode, unsigned int val)
21048 {
21049 dw_line_info_entry e;
21050 e.opcode = opcode;
21051 e.val = val;
21052 vec_safe_push (table->entries, e);
21053 }
21054
21055 /* Output a label to mark the beginning of a source code line entry
21056 and record information relating to this source line, in
21057 'line_info_table' for later output of the .debug_line section. */
21058 /* ??? The discriminator parameter ought to be unsigned. */
21059
21060 static void
21061 dwarf2out_source_line (unsigned int line, const char *filename,
21062 int discriminator, bool is_stmt)
21063 {
21064 unsigned int file_num;
21065 dw_line_info_table *table;
21066
21067 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
21068 return;
21069
21070 /* The discriminator column was added in dwarf4. Simplify the below
21071 by simply removing it if we're not supposed to output it. */
21072 if (dwarf_version < 4 && dwarf_strict)
21073 discriminator = 0;
21074
21075 table = cur_line_info_table;
21076 file_num = maybe_emit_file (lookup_filename (filename));
21077
21078 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21079 the debugger has used the second (possibly duplicate) line number
21080 at the beginning of the function to mark the end of the prologue.
21081 We could eliminate any other duplicates within the function. For
21082 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21083 that second line number entry. */
21084 /* Recall that this end-of-prologue indication is *not* the same thing
21085 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21086 to which the hook corresponds, follows the last insn that was
21087 emitted by gen_prologue. What we need is to precede the first insn
21088 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21089 insn that corresponds to something the user wrote. These may be
21090 very different locations once scheduling is enabled. */
21091
21092 if (0 && file_num == table->file_num
21093 && line == table->line_num
21094 && discriminator == table->discrim_num
21095 && is_stmt == table->is_stmt)
21096 return;
21097
21098 switch_to_section (current_function_section ());
21099
21100 /* If requested, emit something human-readable. */
21101 if (flag_debug_asm)
21102 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21103
21104 if (DWARF2_ASM_LINE_DEBUG_INFO)
21105 {
21106 /* Emit the .loc directive understood by GNU as. */
21107 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21108 file_num, line, is_stmt, discriminator */
21109 fputs ("\t.loc ", asm_out_file);
21110 fprint_ul (asm_out_file, file_num);
21111 putc (' ', asm_out_file);
21112 fprint_ul (asm_out_file, line);
21113 putc (' ', asm_out_file);
21114 putc ('0', asm_out_file);
21115
21116 if (is_stmt != table->is_stmt)
21117 {
21118 fputs (" is_stmt ", asm_out_file);
21119 putc (is_stmt ? '1' : '0', asm_out_file);
21120 }
21121 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21122 {
21123 gcc_assert (discriminator > 0);
21124 fputs (" discriminator ", asm_out_file);
21125 fprint_ul (asm_out_file, (unsigned long) discriminator);
21126 }
21127 putc ('\n', asm_out_file);
21128 }
21129 else
21130 {
21131 unsigned int label_num = ++line_info_label_num;
21132
21133 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21134
21135 push_dw_line_info_entry (table, LI_set_address, label_num);
21136 if (file_num != table->file_num)
21137 push_dw_line_info_entry (table, LI_set_file, file_num);
21138 if (discriminator != table->discrim_num)
21139 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21140 if (is_stmt != table->is_stmt)
21141 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21142 push_dw_line_info_entry (table, LI_set_line, line);
21143 }
21144
21145 table->file_num = file_num;
21146 table->line_num = line;
21147 table->discrim_num = discriminator;
21148 table->is_stmt = is_stmt;
21149 table->in_use = true;
21150 }
21151
21152 /* Record the beginning of a new source file. */
21153
21154 static void
21155 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21156 {
21157 if (flag_eliminate_dwarf2_dups)
21158 {
21159 /* Record the beginning of the file for break_out_includes. */
21160 dw_die_ref bincl_die;
21161
21162 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21163 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21164 }
21165
21166 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21167 {
21168 macinfo_entry e;
21169 e.code = DW_MACINFO_start_file;
21170 e.lineno = lineno;
21171 e.info = ggc_strdup (filename);
21172 vec_safe_push (macinfo_table, e);
21173 }
21174 }
21175
21176 /* Record the end of a source file. */
21177
21178 static void
21179 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21180 {
21181 if (flag_eliminate_dwarf2_dups)
21182 /* Record the end of the file for break_out_includes. */
21183 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21184
21185 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21186 {
21187 macinfo_entry e;
21188 e.code = DW_MACINFO_end_file;
21189 e.lineno = lineno;
21190 e.info = NULL;
21191 vec_safe_push (macinfo_table, e);
21192 }
21193 }
21194
21195 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21196 the tail part of the directive line, i.e. the part which is past the
21197 initial whitespace, #, whitespace, directive-name, whitespace part. */
21198
21199 static void
21200 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21201 const char *buffer ATTRIBUTE_UNUSED)
21202 {
21203 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21204 {
21205 macinfo_entry e;
21206 /* Insert a dummy first entry to be able to optimize the whole
21207 predefined macro block using DW_MACRO_GNU_transparent_include. */
21208 if (macinfo_table->is_empty () && lineno <= 1)
21209 {
21210 e.code = 0;
21211 e.lineno = 0;
21212 e.info = NULL;
21213 vec_safe_push (macinfo_table, e);
21214 }
21215 e.code = DW_MACINFO_define;
21216 e.lineno = lineno;
21217 e.info = ggc_strdup (buffer);
21218 vec_safe_push (macinfo_table, e);
21219 }
21220 }
21221
21222 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21223 the tail part of the directive line, i.e. the part which is past the
21224 initial whitespace, #, whitespace, directive-name, whitespace part. */
21225
21226 static void
21227 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21228 const char *buffer ATTRIBUTE_UNUSED)
21229 {
21230 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21231 {
21232 macinfo_entry e;
21233 /* Insert a dummy first entry to be able to optimize the whole
21234 predefined macro block using DW_MACRO_GNU_transparent_include. */
21235 if (macinfo_table->is_empty () && lineno <= 1)
21236 {
21237 e.code = 0;
21238 e.lineno = 0;
21239 e.info = NULL;
21240 vec_safe_push (macinfo_table, e);
21241 }
21242 e.code = DW_MACINFO_undef;
21243 e.lineno = lineno;
21244 e.info = ggc_strdup (buffer);
21245 vec_safe_push (macinfo_table, e);
21246 }
21247 }
21248
21249 /* Routines to manipulate hash table of CUs. */
21250
21251 static hashval_t
21252 htab_macinfo_hash (const void *of)
21253 {
21254 const macinfo_entry *const entry =
21255 (const macinfo_entry *) of;
21256
21257 return htab_hash_string (entry->info);
21258 }
21259
21260 static int
21261 htab_macinfo_eq (const void *of1, const void *of2)
21262 {
21263 const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
21264 const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
21265
21266 return !strcmp (entry1->info, entry2->info);
21267 }
21268
21269 /* Output a single .debug_macinfo entry. */
21270
21271 static void
21272 output_macinfo_op (macinfo_entry *ref)
21273 {
21274 int file_num;
21275 size_t len;
21276 struct indirect_string_node *node;
21277 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21278 struct dwarf_file_data *fd;
21279
21280 switch (ref->code)
21281 {
21282 case DW_MACINFO_start_file:
21283 fd = lookup_filename (ref->info);
21284 file_num = maybe_emit_file (fd);
21285 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21286 dw2_asm_output_data_uleb128 (ref->lineno,
21287 "Included from line number %lu",
21288 (unsigned long) ref->lineno);
21289 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21290 break;
21291 case DW_MACINFO_end_file:
21292 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21293 break;
21294 case DW_MACINFO_define:
21295 case DW_MACINFO_undef:
21296 len = strlen (ref->info) + 1;
21297 if (!dwarf_strict
21298 && len > DWARF_OFFSET_SIZE
21299 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21300 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21301 {
21302 ref->code = ref->code == DW_MACINFO_define
21303 ? DW_MACRO_GNU_define_indirect
21304 : DW_MACRO_GNU_undef_indirect;
21305 output_macinfo_op (ref);
21306 return;
21307 }
21308 dw2_asm_output_data (1, ref->code,
21309 ref->code == DW_MACINFO_define
21310 ? "Define macro" : "Undefine macro");
21311 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21312 (unsigned long) ref->lineno);
21313 dw2_asm_output_nstring (ref->info, -1, "The macro");
21314 break;
21315 case DW_MACRO_GNU_define_indirect:
21316 case DW_MACRO_GNU_undef_indirect:
21317 node = find_AT_string (ref->info);
21318 gcc_assert (node
21319 && ((node->form == DW_FORM_strp)
21320 || (node->form == DW_FORM_GNU_str_index)));
21321 dw2_asm_output_data (1, ref->code,
21322 ref->code == DW_MACRO_GNU_define_indirect
21323 ? "Define macro indirect"
21324 : "Undefine macro indirect");
21325 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21326 (unsigned long) ref->lineno);
21327 if (node->form == DW_FORM_strp)
21328 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21329 debug_str_section, "The macro: \"%s\"",
21330 ref->info);
21331 else
21332 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21333 ref->info);
21334 break;
21335 case DW_MACRO_GNU_transparent_include:
21336 dw2_asm_output_data (1, ref->code, "Transparent include");
21337 ASM_GENERATE_INTERNAL_LABEL (label,
21338 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21339 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21340 break;
21341 default:
21342 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21343 ASM_COMMENT_START, (unsigned long) ref->code);
21344 break;
21345 }
21346 }
21347
21348 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21349 other compilation unit .debug_macinfo sections. IDX is the first
21350 index of a define/undef, return the number of ops that should be
21351 emitted in a comdat .debug_macinfo section and emit
21352 a DW_MACRO_GNU_transparent_include entry referencing it.
21353 If the define/undef entry should be emitted normally, return 0. */
21354
21355 static unsigned
21356 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21357 htab_t *macinfo_htab)
21358 {
21359 macinfo_entry *first, *second, *cur, *inc;
21360 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21361 unsigned char checksum[16];
21362 struct md5_ctx ctx;
21363 char *grp_name, *tail;
21364 const char *base;
21365 unsigned int i, count, encoded_filename_len, linebuf_len;
21366 void **slot;
21367
21368 first = &(*macinfo_table)[idx];
21369 second = &(*macinfo_table)[idx + 1];
21370
21371 /* Optimize only if there are at least two consecutive define/undef ops,
21372 and either all of them are before first DW_MACINFO_start_file
21373 with lineno {0,1} (i.e. predefined macro block), or all of them are
21374 in some included header file. */
21375 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21376 return 0;
21377 if (vec_safe_is_empty (files))
21378 {
21379 if (first->lineno > 1 || second->lineno > 1)
21380 return 0;
21381 }
21382 else if (first->lineno == 0)
21383 return 0;
21384
21385 /* Find the last define/undef entry that can be grouped together
21386 with first and at the same time compute md5 checksum of their
21387 codes, linenumbers and strings. */
21388 md5_init_ctx (&ctx);
21389 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21390 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21391 break;
21392 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21393 break;
21394 else
21395 {
21396 unsigned char code = cur->code;
21397 md5_process_bytes (&code, 1, &ctx);
21398 checksum_uleb128 (cur->lineno, &ctx);
21399 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21400 }
21401 md5_finish_ctx (&ctx, checksum);
21402 count = i - idx;
21403
21404 /* From the containing include filename (if any) pick up just
21405 usable characters from its basename. */
21406 if (vec_safe_is_empty (files))
21407 base = "";
21408 else
21409 base = lbasename (files->last ().info);
21410 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21411 if (ISIDNUM (base[i]) || base[i] == '.')
21412 encoded_filename_len++;
21413 /* Count . at the end. */
21414 if (encoded_filename_len)
21415 encoded_filename_len++;
21416
21417 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21418 linebuf_len = strlen (linebuf);
21419
21420 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21421 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21422 + 16 * 2 + 1);
21423 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21424 tail = grp_name + 4;
21425 if (encoded_filename_len)
21426 {
21427 for (i = 0; base[i]; i++)
21428 if (ISIDNUM (base[i]) || base[i] == '.')
21429 *tail++ = base[i];
21430 *tail++ = '.';
21431 }
21432 memcpy (tail, linebuf, linebuf_len);
21433 tail += linebuf_len;
21434 *tail++ = '.';
21435 for (i = 0; i < 16; i++)
21436 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21437
21438 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21439 in the empty vector entry before the first define/undef. */
21440 inc = &(*macinfo_table)[idx - 1];
21441 inc->code = DW_MACRO_GNU_transparent_include;
21442 inc->lineno = 0;
21443 inc->info = ggc_strdup (grp_name);
21444 if (*macinfo_htab == NULL)
21445 *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
21446 /* Avoid emitting duplicates. */
21447 slot = htab_find_slot (*macinfo_htab, inc, INSERT);
21448 if (*slot != NULL)
21449 {
21450 inc->code = 0;
21451 inc->info = NULL;
21452 /* If such an entry has been used before, just emit
21453 a DW_MACRO_GNU_transparent_include op. */
21454 inc = (macinfo_entry *) *slot;
21455 output_macinfo_op (inc);
21456 /* And clear all macinfo_entry in the range to avoid emitting them
21457 in the second pass. */
21458 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21459 {
21460 cur->code = 0;
21461 cur->info = NULL;
21462 }
21463 }
21464 else
21465 {
21466 *slot = inc;
21467 inc->lineno = htab_elements (*macinfo_htab);
21468 output_macinfo_op (inc);
21469 }
21470 return count;
21471 }
21472
21473 /* Save any strings needed by the macinfo table in the debug str
21474 table. All strings must be collected into the table by the time
21475 index_string is called. */
21476
21477 static void
21478 save_macinfo_strings (void)
21479 {
21480 unsigned len;
21481 unsigned i;
21482 macinfo_entry *ref;
21483
21484 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21485 {
21486 switch (ref->code)
21487 {
21488 /* Match the logic in output_macinfo_op to decide on
21489 indirect strings. */
21490 case DW_MACINFO_define:
21491 case DW_MACINFO_undef:
21492 len = strlen (ref->info) + 1;
21493 if (!dwarf_strict
21494 && len > DWARF_OFFSET_SIZE
21495 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21496 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21497 set_indirect_string (find_AT_string (ref->info));
21498 break;
21499 case DW_MACRO_GNU_define_indirect:
21500 case DW_MACRO_GNU_undef_indirect:
21501 set_indirect_string (find_AT_string (ref->info));
21502 break;
21503 default:
21504 break;
21505 }
21506 }
21507 }
21508
21509 /* Output macinfo section(s). */
21510
21511 static void
21512 output_macinfo (void)
21513 {
21514 unsigned i;
21515 unsigned long length = vec_safe_length (macinfo_table);
21516 macinfo_entry *ref;
21517 vec<macinfo_entry, va_gc> *files = NULL;
21518 htab_t macinfo_htab = NULL;
21519
21520 if (! length)
21521 return;
21522
21523 /* output_macinfo* uses these interchangeably. */
21524 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21525 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21526 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21527 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21528
21529 /* For .debug_macro emit the section header. */
21530 if (!dwarf_strict)
21531 {
21532 dw2_asm_output_data (2, 4, "DWARF macro version number");
21533 if (DWARF_OFFSET_SIZE == 8)
21534 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21535 else
21536 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21537 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21538 (!dwarf_split_debug_info ? debug_line_section_label
21539 : debug_skeleton_line_section_label),
21540 debug_line_section, NULL);
21541 }
21542
21543 /* In the first loop, it emits the primary .debug_macinfo section
21544 and after each emitted op the macinfo_entry is cleared.
21545 If a longer range of define/undef ops can be optimized using
21546 DW_MACRO_GNU_transparent_include, the
21547 DW_MACRO_GNU_transparent_include op is emitted and kept in
21548 the vector before the first define/undef in the range and the
21549 whole range of define/undef ops is not emitted and kept. */
21550 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21551 {
21552 switch (ref->code)
21553 {
21554 case DW_MACINFO_start_file:
21555 vec_safe_push (files, *ref);
21556 break;
21557 case DW_MACINFO_end_file:
21558 if (!vec_safe_is_empty (files))
21559 files->pop ();
21560 break;
21561 case DW_MACINFO_define:
21562 case DW_MACINFO_undef:
21563 if (!dwarf_strict
21564 && HAVE_COMDAT_GROUP
21565 && vec_safe_length (files) != 1
21566 && i > 0
21567 && i + 1 < length
21568 && (*macinfo_table)[i - 1].code == 0)
21569 {
21570 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21571 if (count)
21572 {
21573 i += count - 1;
21574 continue;
21575 }
21576 }
21577 break;
21578 case 0:
21579 /* A dummy entry may be inserted at the beginning to be able
21580 to optimize the whole block of predefined macros. */
21581 if (i == 0)
21582 continue;
21583 default:
21584 break;
21585 }
21586 output_macinfo_op (ref);
21587 ref->info = NULL;
21588 ref->code = 0;
21589 }
21590
21591 if (macinfo_htab == NULL)
21592 return;
21593
21594 htab_delete (macinfo_htab);
21595
21596 /* If any DW_MACRO_GNU_transparent_include were used, on those
21597 DW_MACRO_GNU_transparent_include entries terminate the
21598 current chain and switch to a new comdat .debug_macinfo
21599 section and emit the define/undef entries within it. */
21600 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21601 switch (ref->code)
21602 {
21603 case 0:
21604 continue;
21605 case DW_MACRO_GNU_transparent_include:
21606 {
21607 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21608 tree comdat_key = get_identifier (ref->info);
21609 /* Terminate the previous .debug_macinfo section. */
21610 dw2_asm_output_data (1, 0, "End compilation unit");
21611 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21612 SECTION_DEBUG
21613 | SECTION_LINKONCE,
21614 comdat_key);
21615 ASM_GENERATE_INTERNAL_LABEL (label,
21616 DEBUG_MACRO_SECTION_LABEL,
21617 ref->lineno);
21618 ASM_OUTPUT_LABEL (asm_out_file, label);
21619 ref->code = 0;
21620 ref->info = NULL;
21621 dw2_asm_output_data (2, 4, "DWARF macro version number");
21622 if (DWARF_OFFSET_SIZE == 8)
21623 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21624 else
21625 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21626 }
21627 break;
21628 case DW_MACINFO_define:
21629 case DW_MACINFO_undef:
21630 output_macinfo_op (ref);
21631 ref->code = 0;
21632 ref->info = NULL;
21633 break;
21634 default:
21635 gcc_unreachable ();
21636 }
21637 }
21638
21639 /* Set up for Dwarf output at the start of compilation. */
21640
21641 static void
21642 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21643 {
21644 /* Allocate the file_table. */
21645 file_table = htab_create_ggc (50, file_table_hash,
21646 file_table_eq, NULL);
21647
21648 /* Allocate the decl_die_table. */
21649 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21650 decl_die_table_eq, NULL);
21651
21652 /* Allocate the decl_loc_table. */
21653 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21654 decl_loc_table_eq, NULL);
21655
21656 /* Allocate the cached_dw_loc_list_table. */
21657 cached_dw_loc_list_table
21658 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21659 cached_dw_loc_list_table_eq, NULL);
21660
21661 /* Allocate the initial hunk of the decl_scope_table. */
21662 vec_alloc (decl_scope_table, 256);
21663
21664 /* Allocate the initial hunk of the abbrev_die_table. */
21665 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21666 (ABBREV_DIE_TABLE_INCREMENT);
21667 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21668 /* Zero-th entry is allocated, but unused. */
21669 abbrev_die_table_in_use = 1;
21670
21671 /* Allocate the pubtypes and pubnames vectors. */
21672 vec_alloc (pubname_table, 32);
21673 vec_alloc (pubtype_table, 32);
21674
21675 vec_alloc (incomplete_types, 64);
21676
21677 vec_alloc (used_rtx_array, 32);
21678
21679 if (!dwarf_split_debug_info)
21680 {
21681 debug_info_section = get_section (DEBUG_INFO_SECTION,
21682 SECTION_DEBUG, NULL);
21683 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21684 SECTION_DEBUG, NULL);
21685 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21686 SECTION_DEBUG, NULL);
21687 }
21688 else
21689 {
21690 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
21691 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21692 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
21693 SECTION_DEBUG | SECTION_EXCLUDE,
21694 NULL);
21695 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
21696 SECTION_DEBUG, NULL);
21697 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
21698 SECTION_DEBUG, NULL);
21699 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21700 SECTION_DEBUG, NULL);
21701 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
21702 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
21703
21704 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
21705 the main .o, but the skeleton_line goes into the split off dwo. */
21706 debug_skeleton_line_section
21707 = get_section (DEBUG_DWO_LINE_SECTION,
21708 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21709 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
21710 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
21711 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
21712 SECTION_DEBUG | SECTION_EXCLUDE,
21713 NULL);
21714 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
21715 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
21716 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
21717 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21718 }
21719 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21720 SECTION_DEBUG, NULL);
21721 debug_macinfo_section = get_section (dwarf_strict
21722 ? DEBUG_MACINFO_SECTION
21723 : DEBUG_MACRO_SECTION,
21724 DEBUG_MACRO_SECTION_FLAGS, NULL);
21725 debug_line_section = get_section (DEBUG_LINE_SECTION,
21726 SECTION_DEBUG, NULL);
21727 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21728 SECTION_DEBUG, NULL);
21729 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21730 SECTION_DEBUG, NULL);
21731 debug_str_section = get_section (DEBUG_STR_SECTION,
21732 DEBUG_STR_SECTION_FLAGS, NULL);
21733 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21734 SECTION_DEBUG, NULL);
21735 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21736 SECTION_DEBUG, NULL);
21737
21738 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21739 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21740 DEBUG_ABBREV_SECTION_LABEL, 0);
21741 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21742 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21743 COLD_TEXT_SECTION_LABEL, 0);
21744 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21745
21746 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21747 DEBUG_INFO_SECTION_LABEL, 0);
21748 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21749 DEBUG_LINE_SECTION_LABEL, 0);
21750 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21751 DEBUG_RANGES_SECTION_LABEL, 0);
21752 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
21753 DEBUG_ADDR_SECTION_LABEL, 0);
21754 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21755 dwarf_strict
21756 ? DEBUG_MACINFO_SECTION_LABEL
21757 : DEBUG_MACRO_SECTION_LABEL, 0);
21758 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
21759
21760 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21761 vec_alloc (macinfo_table, 64);
21762
21763 switch_to_section (text_section);
21764 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21765
21766 /* Make sure the line number table for .text always exists. */
21767 text_section_line_info = new_line_info_table ();
21768 text_section_line_info->end_label = text_end_label;
21769 }
21770
21771 /* Called before compile () starts outputtting functions, variables
21772 and toplevel asms into assembly. */
21773
21774 static void
21775 dwarf2out_assembly_start (void)
21776 {
21777 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21778 && dwarf2out_do_cfi_asm ()
21779 && (!(flag_unwind_tables || flag_exceptions)
21780 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21781 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21782 }
21783
21784 /* A helper function for dwarf2out_finish called through
21785 htab_traverse. Assign a string its index. All strings must be
21786 collected into the table by the time index_string is called,
21787 because the indexing code relies on htab_traverse to traverse nodes
21788 in the same order for each run. */
21789
21790 static int
21791 index_string (void **h, void *v)
21792 {
21793 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21794 unsigned int *index = (unsigned int *) v;
21795
21796 find_string_form (node);
21797 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21798 {
21799 gcc_assert(node->index == NO_INDEX_ASSIGNED);
21800 node->index = *index;
21801 *index += 1;
21802 }
21803 return 1;
21804 }
21805
21806 /* A helper function for output_indirect_strings called through
21807 htab_traverse. Output the offset to a string and update the
21808 current offset. */
21809
21810 static int
21811 output_index_string_offset (void **h, void *v)
21812 {
21813 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21814 unsigned int *offset = (unsigned int *) v;
21815
21816 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21817 {
21818 /* Assert that this node has been assigned an index. */
21819 gcc_assert (node->index != NO_INDEX_ASSIGNED
21820 && node->index != NOT_INDEXED);
21821 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
21822 "indexed string 0x%x: %s", node->index, node->str);
21823 *offset += strlen (node->str) + 1;
21824 }
21825 return 1;
21826 }
21827
21828 /* A helper function for dwarf2out_finish called through
21829 htab_traverse. Output the indexed string. */
21830
21831 static int
21832 output_index_string (void **h, void *v)
21833 {
21834 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21835 unsigned int *cur_idx = (unsigned int *) v;
21836
21837 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21838 {
21839 /* Assert that the strings are output in the same order as their
21840 indexes were assigned. */
21841 gcc_assert (*cur_idx == node->index);
21842 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21843 assemble_string (node->str, strlen (node->str) + 1);
21844 *cur_idx += 1;
21845 }
21846 return 1;
21847 }
21848
21849 /* A helper function for dwarf2out_finish called through
21850 htab_traverse. Emit one queued .debug_str string. */
21851
21852 static int
21853 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21854 {
21855 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21856
21857 if (node->form == DW_FORM_strp && node->refcount > 0)
21858 {
21859 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21860 assemble_string (node->str, strlen (node->str) + 1);
21861 }
21862
21863 return 1;
21864 }
21865
21866 /* Output the indexed string table. */
21867
21868 static void
21869 output_indirect_strings (void)
21870 {
21871 if (!dwarf_split_debug_info)
21872 {
21873 switch_to_section (debug_str_section);
21874 htab_traverse (debug_str_hash, output_indirect_string, NULL);
21875 }
21876 else
21877 {
21878 unsigned int offset = 0;
21879 unsigned int cur_idx = 0;
21880
21881 switch_to_section (debug_str_offsets_section);
21882 htab_traverse_noresize (debug_str_hash,
21883 output_index_string_offset,
21884 &offset);
21885 switch_to_section (debug_str_section);
21886 htab_traverse_noresize (debug_str_hash,
21887 output_index_string,
21888 &cur_idx);
21889 }
21890 }
21891
21892 /* Callback for htab_traverse to assign an index to an entry in the
21893 table, and to write that entry to the .debug_addr section. */
21894
21895 static int
21896 output_addr_table_entry (void **slot, void *data)
21897 {
21898 addr_table_entry *entry = (addr_table_entry *) *slot;
21899 unsigned int *cur_index = (unsigned int *)data;
21900
21901 if (entry->refcount == 0)
21902 {
21903 gcc_assert (entry->index == NO_INDEX_ASSIGNED
21904 || entry->index == NOT_INDEXED);
21905 return 1;
21906 }
21907
21908 gcc_assert (entry->index == *cur_index);
21909 (*cur_index)++;
21910
21911 switch (entry->kind)
21912 {
21913 case ate_kind_rtx:
21914 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
21915 "0x%x", entry->index);
21916 break;
21917 case ate_kind_rtx_dtprel:
21918 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
21919 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
21920 DWARF2_ADDR_SIZE,
21921 entry->addr.rtl);
21922 fputc ('\n', asm_out_file);
21923 break;
21924 case ate_kind_label:
21925 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
21926 "0x%x", entry->index);
21927 break;
21928 default:
21929 gcc_unreachable ();
21930 }
21931 return 1;
21932 }
21933
21934 /* Produce the .debug_addr section. */
21935
21936 static void
21937 output_addr_table (void)
21938 {
21939 unsigned int index = 0;
21940 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
21941 return;
21942
21943 switch_to_section (debug_addr_section);
21944 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
21945 }
21946
21947 #if ENABLE_ASSERT_CHECKING
21948 /* Verify that all marks are clear. */
21949
21950 static void
21951 verify_marks_clear (dw_die_ref die)
21952 {
21953 dw_die_ref c;
21954
21955 gcc_assert (! die->die_mark);
21956 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21957 }
21958 #endif /* ENABLE_ASSERT_CHECKING */
21959
21960 /* Clear the marks for a die and its children.
21961 Be cool if the mark isn't set. */
21962
21963 static void
21964 prune_unmark_dies (dw_die_ref die)
21965 {
21966 dw_die_ref c;
21967
21968 if (die->die_mark)
21969 die->die_mark = 0;
21970 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21971 }
21972
21973 /* Given DIE that we're marking as used, find any other dies
21974 it references as attributes and mark them as used. */
21975
21976 static void
21977 prune_unused_types_walk_attribs (dw_die_ref die)
21978 {
21979 dw_attr_ref a;
21980 unsigned ix;
21981
21982 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
21983 {
21984 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21985 {
21986 /* A reference to another DIE.
21987 Make sure that it will get emitted.
21988 If it was broken out into a comdat group, don't follow it. */
21989 if (! AT_ref (a)->comdat_type_p
21990 || a->dw_attr == DW_AT_specification)
21991 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21992 }
21993 /* Set the string's refcount to 0 so that prune_unused_types_mark
21994 accounts properly for it. */
21995 if (AT_class (a) == dw_val_class_str)
21996 a->dw_attr_val.v.val_str->refcount = 0;
21997 }
21998 }
21999
22000 /* Mark the generic parameters and arguments children DIEs of DIE. */
22001
22002 static void
22003 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22004 {
22005 dw_die_ref c;
22006
22007 if (die == NULL || die->die_child == NULL)
22008 return;
22009 c = die->die_child;
22010 do
22011 {
22012 switch (c->die_tag)
22013 {
22014 case DW_TAG_template_type_param:
22015 case DW_TAG_template_value_param:
22016 case DW_TAG_GNU_template_template_param:
22017 case DW_TAG_GNU_template_parameter_pack:
22018 prune_unused_types_mark (c, 1);
22019 break;
22020 default:
22021 break;
22022 }
22023 c = c->die_sib;
22024 } while (c && c != die->die_child);
22025 }
22026
22027 /* Mark DIE as being used. If DOKIDS is true, then walk down
22028 to DIE's children. */
22029
22030 static void
22031 prune_unused_types_mark (dw_die_ref die, int dokids)
22032 {
22033 dw_die_ref c;
22034
22035 if (die->die_mark == 0)
22036 {
22037 /* We haven't done this node yet. Mark it as used. */
22038 die->die_mark = 1;
22039 /* If this is the DIE of a generic type instantiation,
22040 mark the children DIEs that describe its generic parms and
22041 args. */
22042 prune_unused_types_mark_generic_parms_dies (die);
22043
22044 /* We also have to mark its parents as used.
22045 (But we don't want to mark our parent's kids due to this,
22046 unless it is a class.) */
22047 if (die->die_parent)
22048 prune_unused_types_mark (die->die_parent,
22049 class_scope_p (die->die_parent));
22050
22051 /* Mark any referenced nodes. */
22052 prune_unused_types_walk_attribs (die);
22053
22054 /* If this node is a specification,
22055 also mark the definition, if it exists. */
22056 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22057 prune_unused_types_mark (die->die_definition, 1);
22058 }
22059
22060 if (dokids && die->die_mark != 2)
22061 {
22062 /* We need to walk the children, but haven't done so yet.
22063 Remember that we've walked the kids. */
22064 die->die_mark = 2;
22065
22066 /* If this is an array type, we need to make sure our
22067 kids get marked, even if they're types. If we're
22068 breaking out types into comdat sections, do this
22069 for all type definitions. */
22070 if (die->die_tag == DW_TAG_array_type
22071 || (use_debug_types
22072 && is_type_die (die) && ! is_declaration_die (die)))
22073 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22074 else
22075 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22076 }
22077 }
22078
22079 /* For local classes, look if any static member functions were emitted
22080 and if so, mark them. */
22081
22082 static void
22083 prune_unused_types_walk_local_classes (dw_die_ref die)
22084 {
22085 dw_die_ref c;
22086
22087 if (die->die_mark == 2)
22088 return;
22089
22090 switch (die->die_tag)
22091 {
22092 case DW_TAG_structure_type:
22093 case DW_TAG_union_type:
22094 case DW_TAG_class_type:
22095 break;
22096
22097 case DW_TAG_subprogram:
22098 if (!get_AT_flag (die, DW_AT_declaration)
22099 || die->die_definition != NULL)
22100 prune_unused_types_mark (die, 1);
22101 return;
22102
22103 default:
22104 return;
22105 }
22106
22107 /* Mark children. */
22108 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22109 }
22110
22111 /* Walk the tree DIE and mark types that we actually use. */
22112
22113 static void
22114 prune_unused_types_walk (dw_die_ref die)
22115 {
22116 dw_die_ref c;
22117
22118 /* Don't do anything if this node is already marked and
22119 children have been marked as well. */
22120 if (die->die_mark == 2)
22121 return;
22122
22123 switch (die->die_tag)
22124 {
22125 case DW_TAG_structure_type:
22126 case DW_TAG_union_type:
22127 case DW_TAG_class_type:
22128 if (die->die_perennial_p)
22129 break;
22130
22131 for (c = die->die_parent; c; c = c->die_parent)
22132 if (c->die_tag == DW_TAG_subprogram)
22133 break;
22134
22135 /* Finding used static member functions inside of classes
22136 is needed just for local classes, because for other classes
22137 static member function DIEs with DW_AT_specification
22138 are emitted outside of the DW_TAG_*_type. If we ever change
22139 it, we'd need to call this even for non-local classes. */
22140 if (c)
22141 prune_unused_types_walk_local_classes (die);
22142
22143 /* It's a type node --- don't mark it. */
22144 return;
22145
22146 case DW_TAG_const_type:
22147 case DW_TAG_packed_type:
22148 case DW_TAG_pointer_type:
22149 case DW_TAG_reference_type:
22150 case DW_TAG_rvalue_reference_type:
22151 case DW_TAG_volatile_type:
22152 case DW_TAG_typedef:
22153 case DW_TAG_array_type:
22154 case DW_TAG_interface_type:
22155 case DW_TAG_friend:
22156 case DW_TAG_variant_part:
22157 case DW_TAG_enumeration_type:
22158 case DW_TAG_subroutine_type:
22159 case DW_TAG_string_type:
22160 case DW_TAG_set_type:
22161 case DW_TAG_subrange_type:
22162 case DW_TAG_ptr_to_member_type:
22163 case DW_TAG_file_type:
22164 if (die->die_perennial_p)
22165 break;
22166
22167 /* It's a type node --- don't mark it. */
22168 return;
22169
22170 default:
22171 /* Mark everything else. */
22172 break;
22173 }
22174
22175 if (die->die_mark == 0)
22176 {
22177 die->die_mark = 1;
22178
22179 /* Now, mark any dies referenced from here. */
22180 prune_unused_types_walk_attribs (die);
22181 }
22182
22183 die->die_mark = 2;
22184
22185 /* Mark children. */
22186 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22187 }
22188
22189 /* Increment the string counts on strings referred to from DIE's
22190 attributes. */
22191
22192 static void
22193 prune_unused_types_update_strings (dw_die_ref die)
22194 {
22195 dw_attr_ref a;
22196 unsigned ix;
22197
22198 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22199 if (AT_class (a) == dw_val_class_str)
22200 {
22201 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22202 s->refcount++;
22203 /* Avoid unnecessarily putting strings that are used less than
22204 twice in the hash table. */
22205 if (s->refcount
22206 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22207 {
22208 void ** slot;
22209 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22210 htab_hash_string (s->str),
22211 INSERT);
22212 gcc_assert (*slot == NULL);
22213 *slot = s;
22214 }
22215 }
22216 }
22217
22218 /* Remove from the tree DIE any dies that aren't marked. */
22219
22220 static void
22221 prune_unused_types_prune (dw_die_ref die)
22222 {
22223 dw_die_ref c;
22224
22225 gcc_assert (die->die_mark);
22226 prune_unused_types_update_strings (die);
22227
22228 if (! die->die_child)
22229 return;
22230
22231 c = die->die_child;
22232 do {
22233 dw_die_ref prev = c;
22234 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22235 if (c == die->die_child)
22236 {
22237 /* No marked children between 'prev' and the end of the list. */
22238 if (prev == c)
22239 /* No marked children at all. */
22240 die->die_child = NULL;
22241 else
22242 {
22243 prev->die_sib = c->die_sib;
22244 die->die_child = prev;
22245 }
22246 return;
22247 }
22248
22249 if (c != prev->die_sib)
22250 prev->die_sib = c;
22251 prune_unused_types_prune (c);
22252 } while (c != die->die_child);
22253 }
22254
22255 /* Remove dies representing declarations that we never use. */
22256
22257 static void
22258 prune_unused_types (void)
22259 {
22260 unsigned int i;
22261 limbo_die_node *node;
22262 comdat_type_node *ctnode;
22263 pubname_ref pub;
22264 dw_die_ref base_type;
22265
22266 #if ENABLE_ASSERT_CHECKING
22267 /* All the marks should already be clear. */
22268 verify_marks_clear (comp_unit_die ());
22269 for (node = limbo_die_list; node; node = node->next)
22270 verify_marks_clear (node->die);
22271 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22272 verify_marks_clear (ctnode->root_die);
22273 #endif /* ENABLE_ASSERT_CHECKING */
22274
22275 /* Mark types that are used in global variables. */
22276 premark_types_used_by_global_vars ();
22277
22278 /* Set the mark on nodes that are actually used. */
22279 prune_unused_types_walk (comp_unit_die ());
22280 for (node = limbo_die_list; node; node = node->next)
22281 prune_unused_types_walk (node->die);
22282 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22283 {
22284 prune_unused_types_walk (ctnode->root_die);
22285 prune_unused_types_mark (ctnode->type_die, 1);
22286 }
22287
22288 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22289 are unusual in that they are pubnames that are the children of pubtypes.
22290 They should only be marked via their parent DW_TAG_enumeration_type die,
22291 not as roots in themselves. */
22292 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22293 if (pub->die->die_tag != DW_TAG_enumerator)
22294 prune_unused_types_mark (pub->die, 1);
22295 for (i = 0; base_types.iterate (i, &base_type); i++)
22296 prune_unused_types_mark (base_type, 1);
22297
22298 if (debug_str_hash)
22299 htab_empty (debug_str_hash);
22300 prune_unused_types_prune (comp_unit_die ());
22301 for (node = limbo_die_list; node; node = node->next)
22302 prune_unused_types_prune (node->die);
22303 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22304 prune_unused_types_prune (ctnode->root_die);
22305
22306 /* Leave the marks clear. */
22307 prune_unmark_dies (comp_unit_die ());
22308 for (node = limbo_die_list; node; node = node->next)
22309 prune_unmark_dies (node->die);
22310 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22311 prune_unmark_dies (ctnode->root_die);
22312 }
22313
22314 /* Set the parameter to true if there are any relative pathnames in
22315 the file table. */
22316 static int
22317 file_table_relative_p (void ** slot, void *param)
22318 {
22319 bool *p = (bool *) param;
22320 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22321 if (!IS_ABSOLUTE_PATH (d->filename))
22322 {
22323 *p = true;
22324 return 0;
22325 }
22326 return 1;
22327 }
22328
22329 /* Routines to manipulate hash table of comdat type units. */
22330
22331 static hashval_t
22332 htab_ct_hash (const void *of)
22333 {
22334 hashval_t h;
22335 const comdat_type_node *const type_node = (const comdat_type_node *) of;
22336
22337 memcpy (&h, type_node->signature, sizeof (h));
22338 return h;
22339 }
22340
22341 static int
22342 htab_ct_eq (const void *of1, const void *of2)
22343 {
22344 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22345 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22346
22347 return (! memcmp (type_node_1->signature, type_node_2->signature,
22348 DWARF_TYPE_SIGNATURE_SIZE));
22349 }
22350
22351 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22352 to the location it would have been added, should we know its
22353 DECL_ASSEMBLER_NAME when we added other attributes. This will
22354 probably improve compactness of debug info, removing equivalent
22355 abbrevs, and hide any differences caused by deferring the
22356 computation of the assembler name, triggered by e.g. PCH. */
22357
22358 static inline void
22359 move_linkage_attr (dw_die_ref die)
22360 {
22361 unsigned ix = vec_safe_length (die->die_attr);
22362 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22363
22364 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22365 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22366
22367 while (--ix > 0)
22368 {
22369 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22370
22371 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22372 break;
22373 }
22374
22375 if (ix != vec_safe_length (die->die_attr) - 1)
22376 {
22377 die->die_attr->pop ();
22378 die->die_attr->quick_insert (ix, linkage);
22379 }
22380 }
22381
22382 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22383 referenced from typed stack ops and count how often they are used. */
22384
22385 static void
22386 mark_base_types (dw_loc_descr_ref loc)
22387 {
22388 dw_die_ref base_type = NULL;
22389
22390 for (; loc; loc = loc->dw_loc_next)
22391 {
22392 switch (loc->dw_loc_opc)
22393 {
22394 case DW_OP_GNU_regval_type:
22395 case DW_OP_GNU_deref_type:
22396 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22397 break;
22398 case DW_OP_GNU_convert:
22399 case DW_OP_GNU_reinterpret:
22400 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22401 continue;
22402 /* FALLTHRU */
22403 case DW_OP_GNU_const_type:
22404 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22405 break;
22406 case DW_OP_GNU_entry_value:
22407 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22408 continue;
22409 default:
22410 continue;
22411 }
22412 gcc_assert (base_type->die_parent == comp_unit_die ());
22413 if (base_type->die_mark)
22414 base_type->die_mark++;
22415 else
22416 {
22417 base_types.safe_push (base_type);
22418 base_type->die_mark = 1;
22419 }
22420 }
22421 }
22422
22423 /* Comparison function for sorting marked base types. */
22424
22425 static int
22426 base_type_cmp (const void *x, const void *y)
22427 {
22428 dw_die_ref dx = *(const dw_die_ref *) x;
22429 dw_die_ref dy = *(const dw_die_ref *) y;
22430 unsigned int byte_size1, byte_size2;
22431 unsigned int encoding1, encoding2;
22432 if (dx->die_mark > dy->die_mark)
22433 return -1;
22434 if (dx->die_mark < dy->die_mark)
22435 return 1;
22436 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22437 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22438 if (byte_size1 < byte_size2)
22439 return 1;
22440 if (byte_size1 > byte_size2)
22441 return -1;
22442 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22443 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22444 if (encoding1 < encoding2)
22445 return 1;
22446 if (encoding1 > encoding2)
22447 return -1;
22448 return 0;
22449 }
22450
22451 /* Move base types marked by mark_base_types as early as possible
22452 in the CU, sorted by decreasing usage count both to make the
22453 uleb128 references as small as possible and to make sure they
22454 will have die_offset already computed by calc_die_sizes when
22455 sizes of typed stack loc ops is computed. */
22456
22457 static void
22458 move_marked_base_types (void)
22459 {
22460 unsigned int i;
22461 dw_die_ref base_type, die, c;
22462
22463 if (base_types.is_empty ())
22464 return;
22465
22466 /* Sort by decreasing usage count, they will be added again in that
22467 order later on. */
22468 base_types.qsort (base_type_cmp);
22469 die = comp_unit_die ();
22470 c = die->die_child;
22471 do
22472 {
22473 dw_die_ref prev = c;
22474 c = c->die_sib;
22475 while (c->die_mark)
22476 {
22477 remove_child_with_prev (c, prev);
22478 /* As base types got marked, there must be at least
22479 one node other than DW_TAG_base_type. */
22480 gcc_assert (c != c->die_sib);
22481 c = c->die_sib;
22482 }
22483 }
22484 while (c != die->die_child);
22485 gcc_assert (die->die_child);
22486 c = die->die_child;
22487 for (i = 0; base_types.iterate (i, &base_type); i++)
22488 {
22489 base_type->die_mark = 0;
22490 base_type->die_sib = c->die_sib;
22491 c->die_sib = base_type;
22492 c = base_type;
22493 }
22494 }
22495
22496 /* Helper function for resolve_addr, attempt to resolve
22497 one CONST_STRING, return non-zero if not successful. Similarly verify that
22498 SYMBOL_REFs refer to variables emitted in the current CU. */
22499
22500 static int
22501 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22502 {
22503 rtx rtl = *addr;
22504
22505 if (GET_CODE (rtl) == CONST_STRING)
22506 {
22507 size_t len = strlen (XSTR (rtl, 0)) + 1;
22508 tree t = build_string (len, XSTR (rtl, 0));
22509 tree tlen = size_int (len - 1);
22510 TREE_TYPE (t)
22511 = build_array_type (char_type_node, build_index_type (tlen));
22512 rtl = lookup_constant_def (t);
22513 if (!rtl || !MEM_P (rtl))
22514 return 1;
22515 rtl = XEXP (rtl, 0);
22516 if (GET_CODE (rtl) == SYMBOL_REF
22517 && SYMBOL_REF_DECL (rtl)
22518 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22519 return 1;
22520 vec_safe_push (used_rtx_array, rtl);
22521 *addr = rtl;
22522 return 0;
22523 }
22524
22525 if (GET_CODE (rtl) == SYMBOL_REF
22526 && SYMBOL_REF_DECL (rtl))
22527 {
22528 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22529 {
22530 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22531 return 1;
22532 }
22533 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22534 return 1;
22535 }
22536
22537 if (GET_CODE (rtl) == CONST
22538 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22539 return 1;
22540
22541 return 0;
22542 }
22543
22544 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22545 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22546 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22547
22548 static rtx
22549 string_cst_pool_decl (tree t)
22550 {
22551 rtx rtl = output_constant_def (t, 1);
22552 unsigned char *array;
22553 dw_loc_descr_ref l;
22554 tree decl;
22555 size_t len;
22556 dw_die_ref ref;
22557
22558 if (!rtl || !MEM_P (rtl))
22559 return NULL_RTX;
22560 rtl = XEXP (rtl, 0);
22561 if (GET_CODE (rtl) != SYMBOL_REF
22562 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22563 return NULL_RTX;
22564
22565 decl = SYMBOL_REF_DECL (rtl);
22566 if (!lookup_decl_die (decl))
22567 {
22568 len = TREE_STRING_LENGTH (t);
22569 vec_safe_push (used_rtx_array, rtl);
22570 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
22571 array = (unsigned char *) ggc_alloc_atomic (len);
22572 memcpy (array, TREE_STRING_POINTER (t), len);
22573 l = new_loc_descr (DW_OP_implicit_value, len, 0);
22574 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
22575 l->dw_loc_oprnd2.v.val_vec.length = len;
22576 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
22577 l->dw_loc_oprnd2.v.val_vec.array = array;
22578 add_AT_loc (ref, DW_AT_location, l);
22579 equate_decl_number_to_die (decl, ref);
22580 }
22581 return rtl;
22582 }
22583
22584 /* Helper function of resolve_addr_in_expr. LOC is
22585 a DW_OP_addr followed by DW_OP_stack_value, either at the start
22586 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
22587 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
22588 with DW_OP_GNU_implicit_pointer if possible
22589 and return true, if unsuccesful, return false. */
22590
22591 static bool
22592 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
22593 {
22594 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
22595 HOST_WIDE_INT offset = 0;
22596 dw_die_ref ref = NULL;
22597 tree decl;
22598
22599 if (GET_CODE (rtl) == CONST
22600 && GET_CODE (XEXP (rtl, 0)) == PLUS
22601 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
22602 {
22603 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
22604 rtl = XEXP (XEXP (rtl, 0), 0);
22605 }
22606 if (GET_CODE (rtl) == CONST_STRING)
22607 {
22608 size_t len = strlen (XSTR (rtl, 0)) + 1;
22609 tree t = build_string (len, XSTR (rtl, 0));
22610 tree tlen = size_int (len - 1);
22611
22612 TREE_TYPE (t)
22613 = build_array_type (char_type_node, build_index_type (tlen));
22614 rtl = string_cst_pool_decl (t);
22615 if (!rtl)
22616 return false;
22617 }
22618 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
22619 {
22620 decl = SYMBOL_REF_DECL (rtl);
22621 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
22622 {
22623 ref = lookup_decl_die (decl);
22624 if (ref && (get_AT (ref, DW_AT_location)
22625 || get_AT (ref, DW_AT_const_value)))
22626 {
22627 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
22628 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22629 loc->dw_loc_oprnd1.val_entry = NULL;
22630 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22631 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22632 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22633 loc->dw_loc_oprnd2.v.val_int = offset;
22634 return true;
22635 }
22636 }
22637 }
22638 return false;
22639 }
22640
22641 /* Helper function for resolve_addr, handle one location
22642 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22643 the location list couldn't be resolved. */
22644
22645 static bool
22646 resolve_addr_in_expr (dw_loc_descr_ref loc)
22647 {
22648 dw_loc_descr_ref keep = NULL;
22649 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
22650 switch (loc->dw_loc_opc)
22651 {
22652 case DW_OP_addr:
22653 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22654 {
22655 if ((prev == NULL
22656 || prev->dw_loc_opc == DW_OP_piece
22657 || prev->dw_loc_opc == DW_OP_bit_piece)
22658 && loc->dw_loc_next
22659 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
22660 && !dwarf_strict
22661 && optimize_one_addr_into_implicit_ptr (loc))
22662 break;
22663 return false;
22664 }
22665 break;
22666 case DW_OP_GNU_addr_index:
22667 case DW_OP_GNU_const_index:
22668 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22669 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22670 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22671 NULL))
22672 return false;
22673 break;
22674 case DW_OP_const4u:
22675 case DW_OP_const8u:
22676 if (loc->dtprel
22677 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22678 return false;
22679 break;
22680 case DW_OP_plus_uconst:
22681 if (size_of_loc_descr (loc)
22682 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22683 + 1
22684 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22685 {
22686 dw_loc_descr_ref repl
22687 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
22688 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
22689 add_loc_descr (&repl, loc->dw_loc_next);
22690 *loc = *repl;
22691 }
22692 break;
22693 case DW_OP_implicit_value:
22694 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22695 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
22696 return false;
22697 break;
22698 case DW_OP_GNU_implicit_pointer:
22699 case DW_OP_GNU_parameter_ref:
22700 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22701 {
22702 dw_die_ref ref
22703 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22704 if (ref == NULL)
22705 return false;
22706 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22707 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22708 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22709 }
22710 break;
22711 case DW_OP_GNU_const_type:
22712 case DW_OP_GNU_regval_type:
22713 case DW_OP_GNU_deref_type:
22714 case DW_OP_GNU_convert:
22715 case DW_OP_GNU_reinterpret:
22716 while (loc->dw_loc_next
22717 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
22718 {
22719 dw_die_ref base1, base2;
22720 unsigned enc1, enc2, size1, size2;
22721 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22722 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22723 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
22724 else if (loc->dw_loc_oprnd1.val_class
22725 == dw_val_class_unsigned_const)
22726 break;
22727 else
22728 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
22729 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
22730 == dw_val_class_unsigned_const)
22731 break;
22732 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
22733 gcc_assert (base1->die_tag == DW_TAG_base_type
22734 && base2->die_tag == DW_TAG_base_type);
22735 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
22736 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
22737 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
22738 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
22739 if (size1 == size2
22740 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
22741 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
22742 && loc != keep)
22743 || enc1 == enc2))
22744 {
22745 /* Optimize away next DW_OP_GNU_convert after
22746 adjusting LOC's base type die reference. */
22747 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22748 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22749 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
22750 else
22751 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
22752 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22753 continue;
22754 }
22755 /* Don't change integer DW_OP_GNU_convert after e.g. floating
22756 point typed stack entry. */
22757 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
22758 keep = loc->dw_loc_next;
22759 break;
22760 }
22761 break;
22762 default:
22763 break;
22764 }
22765 return true;
22766 }
22767
22768 /* Helper function of resolve_addr. DIE had DW_AT_location of
22769 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
22770 and DW_OP_addr couldn't be resolved. resolve_addr has already
22771 removed the DW_AT_location attribute. This function attempts to
22772 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
22773 to it or DW_AT_const_value attribute, if possible. */
22774
22775 static void
22776 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
22777 {
22778 if (TREE_CODE (decl) != VAR_DECL
22779 || lookup_decl_die (decl) != die
22780 || DECL_EXTERNAL (decl)
22781 || !TREE_STATIC (decl)
22782 || DECL_INITIAL (decl) == NULL_TREE
22783 || DECL_P (DECL_INITIAL (decl))
22784 || get_AT (die, DW_AT_const_value))
22785 return;
22786
22787 tree init = DECL_INITIAL (decl);
22788 HOST_WIDE_INT offset = 0;
22789 /* For variables that have been optimized away and thus
22790 don't have a memory location, see if we can emit
22791 DW_AT_const_value instead. */
22792 if (tree_add_const_value_attribute (die, init))
22793 return;
22794 if (dwarf_strict)
22795 return;
22796 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
22797 and ADDR_EXPR refers to a decl that has DW_AT_location or
22798 DW_AT_const_value (but isn't addressable, otherwise
22799 resolving the original DW_OP_addr wouldn't fail), see if
22800 we can add DW_OP_GNU_implicit_pointer. */
22801 STRIP_NOPS (init);
22802 if (TREE_CODE (init) == POINTER_PLUS_EXPR
22803 && host_integerp (TREE_OPERAND (init, 1), 0))
22804 {
22805 offset = tree_low_cst (TREE_OPERAND (init, 1), 0);
22806 init = TREE_OPERAND (init, 0);
22807 STRIP_NOPS (init);
22808 }
22809 if (TREE_CODE (init) != ADDR_EXPR)
22810 return;
22811 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
22812 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
22813 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
22814 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
22815 && TREE_OPERAND (init, 0) != decl))
22816 {
22817 dw_die_ref ref;
22818 dw_loc_descr_ref l;
22819
22820 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
22821 {
22822 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
22823 if (!rtl)
22824 return;
22825 decl = SYMBOL_REF_DECL (rtl);
22826 }
22827 else
22828 decl = TREE_OPERAND (init, 0);
22829 ref = lookup_decl_die (decl);
22830 if (ref == NULL
22831 || (!get_AT (ref, DW_AT_location)
22832 && !get_AT (ref, DW_AT_const_value)))
22833 return;
22834 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
22835 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22836 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
22837 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
22838 add_AT_loc (die, DW_AT_location, l);
22839 }
22840 }
22841
22842 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22843 an address in .rodata section if the string literal is emitted there,
22844 or remove the containing location list or replace DW_AT_const_value
22845 with DW_AT_location and empty location expression, if it isn't found
22846 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22847 to something that has been emitted in the current CU. */
22848
22849 static void
22850 resolve_addr (dw_die_ref die)
22851 {
22852 dw_die_ref c;
22853 dw_attr_ref a;
22854 dw_loc_list_ref *curr, *start, loc;
22855 unsigned ix;
22856
22857 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22858 switch (AT_class (a))
22859 {
22860 case dw_val_class_loc_list:
22861 start = curr = AT_loc_list_ptr (a);
22862 loc = *curr;
22863 gcc_assert (loc);
22864 /* The same list can be referenced more than once. See if we have
22865 already recorded the result from a previous pass. */
22866 if (loc->replaced)
22867 *curr = loc->dw_loc_next;
22868 else if (!loc->resolved_addr)
22869 {
22870 /* As things stand, we do not expect or allow one die to
22871 reference a suffix of another die's location list chain.
22872 References must be identical or completely separate.
22873 There is therefore no need to cache the result of this
22874 pass on any list other than the first; doing so
22875 would lead to unnecessary writes. */
22876 while (*curr)
22877 {
22878 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
22879 if (!resolve_addr_in_expr ((*curr)->expr))
22880 {
22881 dw_loc_list_ref next = (*curr)->dw_loc_next;
22882 dw_loc_descr_ref l = (*curr)->expr;
22883
22884 if (next && (*curr)->ll_symbol)
22885 {
22886 gcc_assert (!next->ll_symbol);
22887 next->ll_symbol = (*curr)->ll_symbol;
22888 }
22889 if (dwarf_split_debug_info)
22890 remove_loc_list_addr_table_entries (l);
22891 *curr = next;
22892 }
22893 else
22894 {
22895 mark_base_types ((*curr)->expr);
22896 curr = &(*curr)->dw_loc_next;
22897 }
22898 }
22899 if (loc == *start)
22900 loc->resolved_addr = 1;
22901 else
22902 {
22903 loc->replaced = 1;
22904 loc->dw_loc_next = *start;
22905 }
22906 }
22907 if (!*start)
22908 {
22909 remove_AT (die, a->dw_attr);
22910 ix--;
22911 }
22912 break;
22913 case dw_val_class_loc:
22914 {
22915 dw_loc_descr_ref l = AT_loc (a);
22916 /* For -gdwarf-2 don't attempt to optimize
22917 DW_AT_data_member_location containing
22918 DW_OP_plus_uconst - older consumers might
22919 rely on it being that op instead of a more complex,
22920 but shorter, location description. */
22921 if ((dwarf_version > 2
22922 || a->dw_attr != DW_AT_data_member_location
22923 || l == NULL
22924 || l->dw_loc_opc != DW_OP_plus_uconst
22925 || l->dw_loc_next != NULL)
22926 && !resolve_addr_in_expr (l))
22927 {
22928 if (dwarf_split_debug_info)
22929 remove_loc_list_addr_table_entries (l);
22930 if (l != NULL
22931 && l->dw_loc_next == NULL
22932 && l->dw_loc_opc == DW_OP_addr
22933 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
22934 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
22935 && a->dw_attr == DW_AT_location)
22936 {
22937 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
22938 remove_AT (die, a->dw_attr);
22939 ix--;
22940 optimize_location_into_implicit_ptr (die, decl);
22941 break;
22942 }
22943 remove_AT (die, a->dw_attr);
22944 ix--;
22945 }
22946 else
22947 mark_base_types (l);
22948 }
22949 break;
22950 case dw_val_class_addr:
22951 if (a->dw_attr == DW_AT_const_value
22952 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22953 {
22954 if (AT_index (a) != NOT_INDEXED)
22955 remove_addr_table_entry (a->dw_attr_val.val_entry);
22956 remove_AT (die, a->dw_attr);
22957 ix--;
22958 }
22959 if (die->die_tag == DW_TAG_GNU_call_site
22960 && a->dw_attr == DW_AT_abstract_origin)
22961 {
22962 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
22963 dw_die_ref tdie = lookup_decl_die (tdecl);
22964 if (tdie == NULL
22965 && DECL_EXTERNAL (tdecl)
22966 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
22967 {
22968 force_decl_die (tdecl);
22969 tdie = lookup_decl_die (tdecl);
22970 }
22971 if (tdie)
22972 {
22973 a->dw_attr_val.val_class = dw_val_class_die_ref;
22974 a->dw_attr_val.v.val_die_ref.die = tdie;
22975 a->dw_attr_val.v.val_die_ref.external = 0;
22976 }
22977 else
22978 {
22979 if (AT_index (a) != NOT_INDEXED)
22980 remove_addr_table_entry (a->dw_attr_val.val_entry);
22981 remove_AT (die, a->dw_attr);
22982 ix--;
22983 }
22984 }
22985 break;
22986 default:
22987 break;
22988 }
22989
22990 FOR_EACH_CHILD (die, c, resolve_addr (c));
22991 }
22992 \f
22993 /* Helper routines for optimize_location_lists.
22994 This pass tries to share identical local lists in .debug_loc
22995 section. */
22996
22997 /* Iteratively hash operands of LOC opcode. */
22998
22999 static inline hashval_t
23000 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23001 {
23002 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23003 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23004
23005 switch (loc->dw_loc_opc)
23006 {
23007 case DW_OP_const4u:
23008 case DW_OP_const8u:
23009 if (loc->dtprel)
23010 goto hash_addr;
23011 /* FALLTHRU */
23012 case DW_OP_const1u:
23013 case DW_OP_const1s:
23014 case DW_OP_const2u:
23015 case DW_OP_const2s:
23016 case DW_OP_const4s:
23017 case DW_OP_const8s:
23018 case DW_OP_constu:
23019 case DW_OP_consts:
23020 case DW_OP_pick:
23021 case DW_OP_plus_uconst:
23022 case DW_OP_breg0:
23023 case DW_OP_breg1:
23024 case DW_OP_breg2:
23025 case DW_OP_breg3:
23026 case DW_OP_breg4:
23027 case DW_OP_breg5:
23028 case DW_OP_breg6:
23029 case DW_OP_breg7:
23030 case DW_OP_breg8:
23031 case DW_OP_breg9:
23032 case DW_OP_breg10:
23033 case DW_OP_breg11:
23034 case DW_OP_breg12:
23035 case DW_OP_breg13:
23036 case DW_OP_breg14:
23037 case DW_OP_breg15:
23038 case DW_OP_breg16:
23039 case DW_OP_breg17:
23040 case DW_OP_breg18:
23041 case DW_OP_breg19:
23042 case DW_OP_breg20:
23043 case DW_OP_breg21:
23044 case DW_OP_breg22:
23045 case DW_OP_breg23:
23046 case DW_OP_breg24:
23047 case DW_OP_breg25:
23048 case DW_OP_breg26:
23049 case DW_OP_breg27:
23050 case DW_OP_breg28:
23051 case DW_OP_breg29:
23052 case DW_OP_breg30:
23053 case DW_OP_breg31:
23054 case DW_OP_regx:
23055 case DW_OP_fbreg:
23056 case DW_OP_piece:
23057 case DW_OP_deref_size:
23058 case DW_OP_xderef_size:
23059 hash = iterative_hash_object (val1->v.val_int, hash);
23060 break;
23061 case DW_OP_skip:
23062 case DW_OP_bra:
23063 {
23064 int offset;
23065
23066 gcc_assert (val1->val_class == dw_val_class_loc);
23067 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23068 hash = iterative_hash_object (offset, hash);
23069 }
23070 break;
23071 case DW_OP_implicit_value:
23072 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23073 switch (val2->val_class)
23074 {
23075 case dw_val_class_const:
23076 hash = iterative_hash_object (val2->v.val_int, hash);
23077 break;
23078 case dw_val_class_vec:
23079 {
23080 unsigned int elt_size = val2->v.val_vec.elt_size;
23081 unsigned int len = val2->v.val_vec.length;
23082
23083 hash = iterative_hash_object (elt_size, hash);
23084 hash = iterative_hash_object (len, hash);
23085 hash = iterative_hash (val2->v.val_vec.array,
23086 len * elt_size, hash);
23087 }
23088 break;
23089 case dw_val_class_const_double:
23090 hash = iterative_hash_object (val2->v.val_double.low, hash);
23091 hash = iterative_hash_object (val2->v.val_double.high, hash);
23092 break;
23093 case dw_val_class_addr:
23094 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23095 break;
23096 default:
23097 gcc_unreachable ();
23098 }
23099 break;
23100 case DW_OP_bregx:
23101 case DW_OP_bit_piece:
23102 hash = iterative_hash_object (val1->v.val_int, hash);
23103 hash = iterative_hash_object (val2->v.val_int, hash);
23104 break;
23105 case DW_OP_addr:
23106 hash_addr:
23107 if (loc->dtprel)
23108 {
23109 unsigned char dtprel = 0xd1;
23110 hash = iterative_hash_object (dtprel, hash);
23111 }
23112 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23113 break;
23114 case DW_OP_GNU_addr_index:
23115 case DW_OP_GNU_const_index:
23116 {
23117 if (loc->dtprel)
23118 {
23119 unsigned char dtprel = 0xd1;
23120 hash = iterative_hash_object (dtprel, hash);
23121 }
23122 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23123 }
23124 break;
23125 case DW_OP_GNU_implicit_pointer:
23126 hash = iterative_hash_object (val2->v.val_int, hash);
23127 break;
23128 case DW_OP_GNU_entry_value:
23129 hash = hash_loc_operands (val1->v.val_loc, hash);
23130 break;
23131 case DW_OP_GNU_regval_type:
23132 case DW_OP_GNU_deref_type:
23133 {
23134 unsigned int byte_size
23135 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23136 unsigned int encoding
23137 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23138 hash = iterative_hash_object (val1->v.val_int, hash);
23139 hash = iterative_hash_object (byte_size, hash);
23140 hash = iterative_hash_object (encoding, hash);
23141 }
23142 break;
23143 case DW_OP_GNU_convert:
23144 case DW_OP_GNU_reinterpret:
23145 if (val1->val_class == dw_val_class_unsigned_const)
23146 {
23147 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23148 break;
23149 }
23150 /* FALLTHRU */
23151 case DW_OP_GNU_const_type:
23152 {
23153 unsigned int byte_size
23154 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23155 unsigned int encoding
23156 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23157 hash = iterative_hash_object (byte_size, hash);
23158 hash = iterative_hash_object (encoding, hash);
23159 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23160 break;
23161 hash = iterative_hash_object (val2->val_class, hash);
23162 switch (val2->val_class)
23163 {
23164 case dw_val_class_const:
23165 hash = iterative_hash_object (val2->v.val_int, hash);
23166 break;
23167 case dw_val_class_vec:
23168 {
23169 unsigned int elt_size = val2->v.val_vec.elt_size;
23170 unsigned int len = val2->v.val_vec.length;
23171
23172 hash = iterative_hash_object (elt_size, hash);
23173 hash = iterative_hash_object (len, hash);
23174 hash = iterative_hash (val2->v.val_vec.array,
23175 len * elt_size, hash);
23176 }
23177 break;
23178 case dw_val_class_const_double:
23179 hash = iterative_hash_object (val2->v.val_double.low, hash);
23180 hash = iterative_hash_object (val2->v.val_double.high, hash);
23181 break;
23182 default:
23183 gcc_unreachable ();
23184 }
23185 }
23186 break;
23187
23188 default:
23189 /* Other codes have no operands. */
23190 break;
23191 }
23192 return hash;
23193 }
23194
23195 /* Iteratively hash the whole DWARF location expression LOC. */
23196
23197 static inline hashval_t
23198 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23199 {
23200 dw_loc_descr_ref l;
23201 bool sizes_computed = false;
23202 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23203 size_of_locs (loc);
23204
23205 for (l = loc; l != NULL; l = l->dw_loc_next)
23206 {
23207 enum dwarf_location_atom opc = l->dw_loc_opc;
23208 hash = iterative_hash_object (opc, hash);
23209 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23210 {
23211 size_of_locs (loc);
23212 sizes_computed = true;
23213 }
23214 hash = hash_loc_operands (l, hash);
23215 }
23216 return hash;
23217 }
23218
23219 /* Compute hash of the whole location list LIST_HEAD. */
23220
23221 static inline void
23222 hash_loc_list (dw_loc_list_ref list_head)
23223 {
23224 dw_loc_list_ref curr = list_head;
23225 hashval_t hash = 0;
23226
23227 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23228 {
23229 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23230 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23231 if (curr->section)
23232 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23233 hash);
23234 hash = hash_locs (curr->expr, hash);
23235 }
23236 list_head->hash = hash;
23237 }
23238
23239 /* Return true if X and Y opcodes have the same operands. */
23240
23241 static inline bool
23242 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23243 {
23244 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23245 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23246 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23247 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23248
23249 switch (x->dw_loc_opc)
23250 {
23251 case DW_OP_const4u:
23252 case DW_OP_const8u:
23253 if (x->dtprel)
23254 goto hash_addr;
23255 /* FALLTHRU */
23256 case DW_OP_const1u:
23257 case DW_OP_const1s:
23258 case DW_OP_const2u:
23259 case DW_OP_const2s:
23260 case DW_OP_const4s:
23261 case DW_OP_const8s:
23262 case DW_OP_constu:
23263 case DW_OP_consts:
23264 case DW_OP_pick:
23265 case DW_OP_plus_uconst:
23266 case DW_OP_breg0:
23267 case DW_OP_breg1:
23268 case DW_OP_breg2:
23269 case DW_OP_breg3:
23270 case DW_OP_breg4:
23271 case DW_OP_breg5:
23272 case DW_OP_breg6:
23273 case DW_OP_breg7:
23274 case DW_OP_breg8:
23275 case DW_OP_breg9:
23276 case DW_OP_breg10:
23277 case DW_OP_breg11:
23278 case DW_OP_breg12:
23279 case DW_OP_breg13:
23280 case DW_OP_breg14:
23281 case DW_OP_breg15:
23282 case DW_OP_breg16:
23283 case DW_OP_breg17:
23284 case DW_OP_breg18:
23285 case DW_OP_breg19:
23286 case DW_OP_breg20:
23287 case DW_OP_breg21:
23288 case DW_OP_breg22:
23289 case DW_OP_breg23:
23290 case DW_OP_breg24:
23291 case DW_OP_breg25:
23292 case DW_OP_breg26:
23293 case DW_OP_breg27:
23294 case DW_OP_breg28:
23295 case DW_OP_breg29:
23296 case DW_OP_breg30:
23297 case DW_OP_breg31:
23298 case DW_OP_regx:
23299 case DW_OP_fbreg:
23300 case DW_OP_piece:
23301 case DW_OP_deref_size:
23302 case DW_OP_xderef_size:
23303 return valx1->v.val_int == valy1->v.val_int;
23304 case DW_OP_skip:
23305 case DW_OP_bra:
23306 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23307 can cause irrelevant differences in dw_loc_addr. */
23308 gcc_assert (valx1->val_class == dw_val_class_loc
23309 && valy1->val_class == dw_val_class_loc
23310 && (dwarf_split_debug_info
23311 || x->dw_loc_addr == y->dw_loc_addr));
23312 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23313 case DW_OP_implicit_value:
23314 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23315 || valx2->val_class != valy2->val_class)
23316 return false;
23317 switch (valx2->val_class)
23318 {
23319 case dw_val_class_const:
23320 return valx2->v.val_int == valy2->v.val_int;
23321 case dw_val_class_vec:
23322 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23323 && valx2->v.val_vec.length == valy2->v.val_vec.length
23324 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23325 valx2->v.val_vec.elt_size
23326 * valx2->v.val_vec.length) == 0;
23327 case dw_val_class_const_double:
23328 return valx2->v.val_double.low == valy2->v.val_double.low
23329 && valx2->v.val_double.high == valy2->v.val_double.high;
23330 case dw_val_class_addr:
23331 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23332 default:
23333 gcc_unreachable ();
23334 }
23335 case DW_OP_bregx:
23336 case DW_OP_bit_piece:
23337 return valx1->v.val_int == valy1->v.val_int
23338 && valx2->v.val_int == valy2->v.val_int;
23339 case DW_OP_addr:
23340 hash_addr:
23341 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23342 case DW_OP_GNU_addr_index:
23343 case DW_OP_GNU_const_index:
23344 {
23345 rtx ax1 = valx1->val_entry->addr.rtl;
23346 rtx ay1 = valy1->val_entry->addr.rtl;
23347 return rtx_equal_p (ax1, ay1);
23348 }
23349 case DW_OP_GNU_implicit_pointer:
23350 return valx1->val_class == dw_val_class_die_ref
23351 && valx1->val_class == valy1->val_class
23352 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23353 && valx2->v.val_int == valy2->v.val_int;
23354 case DW_OP_GNU_entry_value:
23355 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23356 case DW_OP_GNU_const_type:
23357 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23358 || valx2->val_class != valy2->val_class)
23359 return false;
23360 switch (valx2->val_class)
23361 {
23362 case dw_val_class_const:
23363 return valx2->v.val_int == valy2->v.val_int;
23364 case dw_val_class_vec:
23365 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23366 && valx2->v.val_vec.length == valy2->v.val_vec.length
23367 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23368 valx2->v.val_vec.elt_size
23369 * valx2->v.val_vec.length) == 0;
23370 case dw_val_class_const_double:
23371 return valx2->v.val_double.low == valy2->v.val_double.low
23372 && valx2->v.val_double.high == valy2->v.val_double.high;
23373 default:
23374 gcc_unreachable ();
23375 }
23376 case DW_OP_GNU_regval_type:
23377 case DW_OP_GNU_deref_type:
23378 return valx1->v.val_int == valy1->v.val_int
23379 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23380 case DW_OP_GNU_convert:
23381 case DW_OP_GNU_reinterpret:
23382 if (valx1->val_class != valy1->val_class)
23383 return false;
23384 if (valx1->val_class == dw_val_class_unsigned_const)
23385 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23386 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23387 case DW_OP_GNU_parameter_ref:
23388 return valx1->val_class == dw_val_class_die_ref
23389 && valx1->val_class == valy1->val_class
23390 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23391 default:
23392 /* Other codes have no operands. */
23393 return true;
23394 }
23395 }
23396
23397 /* Return true if DWARF location expressions X and Y are the same. */
23398
23399 static inline bool
23400 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23401 {
23402 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23403 if (x->dw_loc_opc != y->dw_loc_opc
23404 || x->dtprel != y->dtprel
23405 || !compare_loc_operands (x, y))
23406 break;
23407 return x == NULL && y == NULL;
23408 }
23409
23410 /* Return precomputed hash of location list X. */
23411
23412 static hashval_t
23413 loc_list_hash (const void *x)
23414 {
23415 return ((const struct dw_loc_list_struct *) x)->hash;
23416 }
23417
23418 /* Return 1 if location lists X and Y are the same. */
23419
23420 static int
23421 loc_list_eq (const void *x, const void *y)
23422 {
23423 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23424 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23425 if (a == b)
23426 return 1;
23427 if (a->hash != b->hash)
23428 return 0;
23429 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23430 if (strcmp (a->begin, b->begin) != 0
23431 || strcmp (a->end, b->end) != 0
23432 || (a->section == NULL) != (b->section == NULL)
23433 || (a->section && strcmp (a->section, b->section) != 0)
23434 || !compare_locs (a->expr, b->expr))
23435 break;
23436 return a == NULL && b == NULL;
23437 }
23438
23439 /* Recursively optimize location lists referenced from DIE
23440 children and share them whenever possible. */
23441
23442 static void
23443 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23444 {
23445 dw_die_ref c;
23446 dw_attr_ref a;
23447 unsigned ix;
23448 void **slot;
23449
23450 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23451 if (AT_class (a) == dw_val_class_loc_list)
23452 {
23453 dw_loc_list_ref list = AT_loc_list (a);
23454 /* TODO: perform some optimizations here, before hashing
23455 it and storing into the hash table. */
23456 hash_loc_list (list);
23457 slot = htab_find_slot_with_hash (htab, list, list->hash,
23458 INSERT);
23459 if (*slot == NULL)
23460 *slot = (void *) list;
23461 else
23462 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23463 }
23464
23465 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23466 }
23467
23468
23469 /* Recursively assign each location list a unique index into the debug_addr
23470 section. */
23471
23472 static void
23473 index_location_lists (dw_die_ref die)
23474 {
23475 dw_die_ref c;
23476 dw_attr_ref a;
23477 unsigned ix;
23478
23479 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23480 if (AT_class (a) == dw_val_class_loc_list)
23481 {
23482 dw_loc_list_ref list = AT_loc_list (a);
23483 dw_loc_list_ref curr;
23484 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23485 {
23486 /* Don't index an entry that has already been indexed
23487 or won't be output. */
23488 if (curr->begin_entry != NULL
23489 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23490 continue;
23491
23492 curr->begin_entry
23493 = add_addr_table_entry (xstrdup (curr->begin),
23494 ate_kind_label);
23495 }
23496 }
23497
23498 FOR_EACH_CHILD (die, c, index_location_lists (c));
23499 }
23500
23501 /* Optimize location lists referenced from DIE
23502 children and share them whenever possible. */
23503
23504 static void
23505 optimize_location_lists (dw_die_ref die)
23506 {
23507 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23508 optimize_location_lists_1 (die, htab);
23509 htab_delete (htab);
23510 }
23511 \f
23512 /* Output stuff that dwarf requires at the end of every file,
23513 and generate the DWARF-2 debugging info. */
23514
23515 static void
23516 dwarf2out_finish (const char *filename)
23517 {
23518 limbo_die_node *node, *next_node;
23519 comdat_type_node *ctnode;
23520 htab_t comdat_type_table;
23521 unsigned int i;
23522 dw_die_ref main_comp_unit_die;
23523
23524 /* PCH might result in DW_AT_producer string being restored from the
23525 header compilation, so always fill it with empty string initially
23526 and overwrite only here. */
23527 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23528 producer_string = gen_producer_string ();
23529 producer->dw_attr_val.v.val_str->refcount--;
23530 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23531
23532 gen_scheduled_generic_parms_dies ();
23533 gen_remaining_tmpl_value_param_die_attribute ();
23534
23535 /* Add the name for the main input file now. We delayed this from
23536 dwarf2out_init to avoid complications with PCH. */
23537 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23538 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23539 add_comp_dir_attribute (comp_unit_die ());
23540 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23541 {
23542 bool p = false;
23543 htab_traverse (file_table, file_table_relative_p, &p);
23544 if (p)
23545 add_comp_dir_attribute (comp_unit_die ());
23546 }
23547
23548 if (deferred_locations_list)
23549 for (i = 0; i < deferred_locations_list->length (); i++)
23550 {
23551 add_location_or_const_value_attribute (
23552 (*deferred_locations_list)[i].die,
23553 (*deferred_locations_list)[i].variable,
23554 false,
23555 DW_AT_location);
23556 }
23557
23558 /* Traverse the limbo die list, and add parent/child links. The only
23559 dies without parents that should be here are concrete instances of
23560 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23561 For concrete instances, we can get the parent die from the abstract
23562 instance. */
23563 for (node = limbo_die_list; node; node = next_node)
23564 {
23565 dw_die_ref die = node->die;
23566 next_node = node->next;
23567
23568 if (die->die_parent == NULL)
23569 {
23570 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23571
23572 if (origin && origin->die_parent)
23573 add_child_die (origin->die_parent, die);
23574 else if (is_cu_die (die))
23575 ;
23576 else if (seen_error ())
23577 /* It's OK to be confused by errors in the input. */
23578 add_child_die (comp_unit_die (), die);
23579 else
23580 {
23581 /* In certain situations, the lexical block containing a
23582 nested function can be optimized away, which results
23583 in the nested function die being orphaned. Likewise
23584 with the return type of that nested function. Force
23585 this to be a child of the containing function.
23586
23587 It may happen that even the containing function got fully
23588 inlined and optimized out. In that case we are lost and
23589 assign the empty child. This should not be big issue as
23590 the function is likely unreachable too. */
23591 gcc_assert (node->created_for);
23592
23593 if (DECL_P (node->created_for))
23594 origin = get_context_die (DECL_CONTEXT (node->created_for));
23595 else if (TYPE_P (node->created_for))
23596 origin = scope_die_for (node->created_for, comp_unit_die ());
23597 else
23598 origin = comp_unit_die ();
23599
23600 add_child_die (origin, die);
23601 }
23602 }
23603 }
23604
23605 limbo_die_list = NULL;
23606
23607 #if ENABLE_ASSERT_CHECKING
23608 {
23609 dw_die_ref die = comp_unit_die (), c;
23610 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23611 }
23612 #endif
23613 resolve_addr (comp_unit_die ());
23614 move_marked_base_types ();
23615
23616 for (node = deferred_asm_name; node; node = node->next)
23617 {
23618 tree decl = node->created_for;
23619 /* When generating LTO bytecode we can not generate new assembler
23620 names at this point and all important decls got theirs via
23621 free-lang-data. */
23622 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23623 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23624 {
23625 add_linkage_attr (node->die, decl);
23626 move_linkage_attr (node->die);
23627 }
23628 }
23629
23630 deferred_asm_name = NULL;
23631
23632 /* Walk through the list of incomplete types again, trying once more to
23633 emit full debugging info for them. */
23634 retry_incomplete_types ();
23635
23636 if (flag_eliminate_unused_debug_types)
23637 prune_unused_types ();
23638
23639 /* Generate separate COMDAT sections for type DIEs. */
23640 if (use_debug_types)
23641 {
23642 break_out_comdat_types (comp_unit_die ());
23643
23644 /* Each new type_unit DIE was added to the limbo die list when created.
23645 Since these have all been added to comdat_type_list, clear the
23646 limbo die list. */
23647 limbo_die_list = NULL;
23648
23649 /* For each new comdat type unit, copy declarations for incomplete
23650 types to make the new unit self-contained (i.e., no direct
23651 references to the main compile unit). */
23652 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23653 copy_decls_for_unworthy_types (ctnode->root_die);
23654 copy_decls_for_unworthy_types (comp_unit_die ());
23655
23656 /* In the process of copying declarations from one unit to another,
23657 we may have left some declarations behind that are no longer
23658 referenced. Prune them. */
23659 prune_unused_types ();
23660 }
23661
23662 /* Generate separate CUs for each of the include files we've seen.
23663 They will go into limbo_die_list. */
23664 if (flag_eliminate_dwarf2_dups)
23665 break_out_includes (comp_unit_die ());
23666
23667 /* Traverse the DIE's and add add sibling attributes to those DIE's
23668 that have children. */
23669 add_sibling_attributes (comp_unit_die ());
23670 for (node = limbo_die_list; node; node = node->next)
23671 add_sibling_attributes (node->die);
23672 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23673 add_sibling_attributes (ctnode->root_die);
23674
23675 /* When splitting DWARF info, we put some attributes in the
23676 skeleton compile_unit DIE that remains in the .o, while
23677 most attributes go in the DWO compile_unit_die. */
23678 if (dwarf_split_debug_info)
23679 main_comp_unit_die = gen_compile_unit_die (NULL);
23680 else
23681 main_comp_unit_die = comp_unit_die ();
23682
23683 /* Output a terminator label for the .text section. */
23684 switch_to_section (text_section);
23685 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23686 if (cold_text_section)
23687 {
23688 switch_to_section (cold_text_section);
23689 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23690 }
23691
23692 /* We can only use the low/high_pc attributes if all of the code was
23693 in .text. */
23694 if (!have_multiple_function_sections
23695 || (dwarf_version < 3 && dwarf_strict))
23696 {
23697 /* Don't add if the CU has no associated code. */
23698 if (text_section_used)
23699 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
23700 text_end_label, true);
23701 }
23702 else
23703 {
23704 unsigned fde_idx;
23705 dw_fde_ref fde;
23706 bool range_list_added = false;
23707
23708 if (text_section_used)
23709 add_ranges_by_labels (main_comp_unit_die, text_section_label,
23710 text_end_label, &range_list_added, true);
23711 if (cold_text_section_used)
23712 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
23713 cold_end_label, &range_list_added, true);
23714
23715 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
23716 {
23717 if (DECL_IGNORED_P (fde->decl))
23718 continue;
23719 if (!fde->in_std_section)
23720 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
23721 fde->dw_fde_end, &range_list_added,
23722 true);
23723 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23724 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
23725 fde->dw_fde_second_end, &range_list_added,
23726 true);
23727 }
23728
23729 if (range_list_added)
23730 {
23731 /* We need to give .debug_loc and .debug_ranges an appropriate
23732 "base address". Use zero so that these addresses become
23733 absolute. Historically, we've emitted the unexpected
23734 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23735 Emit both to give time for other tools to adapt. */
23736 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
23737 if (! dwarf_strict && dwarf_version < 4)
23738 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
23739
23740 add_ranges (NULL);
23741 }
23742 }
23743
23744 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23745 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
23746 debug_line_section_label);
23747
23748 if (have_macinfo)
23749 add_AT_macptr (comp_unit_die (),
23750 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
23751 macinfo_section_label);
23752
23753 if (dwarf_split_debug_info && addr_index_table != NULL)
23754 {
23755 /* optimize_location_lists calculates the size of the lists,
23756 so index them first, and assign indices to the entries.
23757 Although optimize_location_lists will remove entries from
23758 the table, it only does so for duplicates, and therefore
23759 only reduces ref_counts to 1. */
23760 unsigned int index = 0;
23761 index_location_lists (comp_unit_die ());
23762 htab_traverse_noresize (addr_index_table,
23763 index_addr_table_entry, &index);
23764 }
23765 if (have_location_lists)
23766 optimize_location_lists (comp_unit_die ());
23767
23768 save_macinfo_strings ();
23769 if (dwarf_split_debug_info)
23770 {
23771 unsigned int index = 0;
23772 htab_traverse_noresize (debug_str_hash, index_string, &index);
23773 }
23774
23775 /* Output all of the compilation units. We put the main one last so that
23776 the offsets are available to output_pubnames. */
23777 for (node = limbo_die_list; node; node = node->next)
23778 output_comp_unit (node->die, 0);
23779
23780 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23781 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23782 {
23783 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23784
23785 /* Don't output duplicate types. */
23786 if (*slot != HTAB_EMPTY_ENTRY)
23787 continue;
23788
23789 /* Add a pointer to the line table for the main compilation unit
23790 so that the debugger can make sense of DW_AT_decl_file
23791 attributes. */
23792 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23793 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23794 (!dwarf_split_debug_info
23795 ? debug_line_section_label
23796 : debug_skeleton_line_section_label));
23797
23798 output_comdat_type_unit (ctnode);
23799 *slot = ctnode;
23800 }
23801 htab_delete (comdat_type_table);
23802
23803 /* The AT_pubnames attribute needs to go in all skeleton dies, including
23804 both the main_cu and all skeleton TUs. Making this call unconditional
23805 would end up either adding a second copy of the AT_pubnames attribute, or
23806 requiring a special case in add_top_level_skeleton_die_attrs. */
23807 if (!dwarf_split_debug_info)
23808 add_AT_pubnames (comp_unit_die ());
23809
23810 if (dwarf_split_debug_info)
23811 {
23812 int mark;
23813 unsigned char checksum[16];
23814 struct md5_ctx ctx;
23815
23816 /* Compute a checksum of the comp_unit to use as the dwo_id. */
23817 md5_init_ctx (&ctx);
23818 mark = 0;
23819 die_checksum (comp_unit_die (), &ctx, &mark);
23820 unmark_all_dies (comp_unit_die ());
23821 md5_finish_ctx (&ctx, checksum);
23822
23823 /* Use the first 8 bytes of the checksum as the dwo_id,
23824 and add it to both comp-unit DIEs. */
23825 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
23826 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
23827
23828 /* Add the base offset of the ranges table to the skeleton
23829 comp-unit DIE. */
23830 if (ranges_table_in_use)
23831 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
23832 ranges_section_label);
23833
23834 switch_to_section (debug_addr_section);
23835 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
23836 output_addr_table ();
23837 }
23838
23839 /* Output the main compilation unit if non-empty or if .debug_macinfo
23840 or .debug_macro will be emitted. */
23841 output_comp_unit (comp_unit_die (), have_macinfo);
23842
23843 if (dwarf_split_debug_info && info_section_emitted)
23844 output_skeleton_debug_sections (main_comp_unit_die);
23845
23846 /* Output the abbreviation table. */
23847 if (abbrev_die_table_in_use != 1)
23848 {
23849 switch_to_section (debug_abbrev_section);
23850 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23851 output_abbrev_section ();
23852 }
23853
23854 /* Output location list section if necessary. */
23855 if (have_location_lists)
23856 {
23857 /* Output the location lists info. */
23858 switch_to_section (debug_loc_section);
23859 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23860 output_location_lists (comp_unit_die ());
23861 }
23862
23863 /* Output public names and types tables if necessary. */
23864 output_pubnames (pubname_table);
23865 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23866 It shouldn't hurt to emit it always, since pure DWARF2 consumers
23867 simply won't look for the section. */
23868 output_pubnames (pubtype_table);
23869
23870 /* Output the address range information if a CU (.debug_info section)
23871 was emitted. We output an empty table even if we had no functions
23872 to put in it. This because the consumer has no way to tell the
23873 difference between an empty table that we omitted and failure to
23874 generate a table that would have contained data. */
23875 if (info_section_emitted)
23876 {
23877 unsigned long aranges_length = size_of_aranges ();
23878
23879 switch_to_section (debug_aranges_section);
23880 output_aranges (aranges_length);
23881 }
23882
23883 /* Output ranges section if necessary. */
23884 if (ranges_table_in_use)
23885 {
23886 switch_to_section (debug_ranges_section);
23887 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23888 output_ranges ();
23889 }
23890
23891 /* Have to end the macro section. */
23892 if (have_macinfo)
23893 {
23894 switch_to_section (debug_macinfo_section);
23895 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23896 output_macinfo ();
23897 dw2_asm_output_data (1, 0, "End compilation unit");
23898 }
23899
23900 /* Output the source line correspondence table. We must do this
23901 even if there is no line information. Otherwise, on an empty
23902 translation unit, we will generate a present, but empty,
23903 .debug_info section. IRIX 6.5 `nm' will then complain when
23904 examining the file. This is done late so that any filenames
23905 used by the debug_info section are marked as 'used'. */
23906 switch_to_section (debug_line_section);
23907 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23908 if (! DWARF2_ASM_LINE_DEBUG_INFO)
23909 output_line_info (false);
23910
23911 if (dwarf_split_debug_info && info_section_emitted)
23912 {
23913 switch_to_section (debug_skeleton_line_section);
23914 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
23915 output_line_info (true);
23916 }
23917
23918 /* If we emitted any indirect strings, output the string table too. */
23919 if (debug_str_hash)
23920 output_indirect_strings ();
23921 }
23922
23923 #include "gt-dwarf2out.h"