]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/dwarf2out.c
dbxout.c (dbxout_symbol_location): Pass new argument to alter_subreg.
[thirdparty/gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Contributed by Gary Funck (gary@intrepid.com).
6 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7 Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26 the file numbers are used by .debug_info. Alternately, leave
27 out locations for types and decls.
28 Avoid talking about ctors and op= for PODs.
29 Factor out common prologue sequences into multiple CIEs. */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32 information, which is also used by the GCC efficient exception handling
33 mechanism. The second part, controlled only by an #ifdef
34 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35 information. */
36
37 /* DWARF2 Abbreviation Glossary:
38
39 CFA = Canonical Frame Address
40 a fixed address on the stack which identifies a call frame.
41 We define it to be the value of SP just before the call insn.
42 The CFA register and offset, which may change during the course
43 of the function, are used to calculate its value at runtime.
44
45 CFI = Call Frame Instruction
46 an instruction for the DWARF2 abstract machine
47
48 CIE = Common Information Entry
49 information describing information common to one or more FDEs
50
51 DIE = Debugging Information Entry
52
53 FDE = Frame Description Entry
54 information describing the stack call frame, in particular,
55 how to restore registers
56
57 DW_CFA_... = DWARF2 CFA call frame instruction
58 DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "except.h"
76 #include "dwarf2.h"
77 #include "dwarf2out.h"
78 #include "dwarf2asm.h"
79 #include "toplev.h"
80 #include "ggc.h"
81 #include "md5.h"
82 #include "tm_p.h"
83 #include "diagnostic.h"
84 #include "tree-pretty-print.h"
85 #include "debug.h"
86 #include "target.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "ira.h"
94 #include "lra.h"
95 #include "dumpfile.h"
96 #include "opts.h"
97
98 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
99 static rtx last_var_location_insn;
100 static rtx cached_next_real_insn;
101
102 #ifdef VMS_DEBUGGING_INFO
103 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
104
105 /* Define this macro to be a nonzero value if the directory specifications
106 which are output in the debug info should end with a separator. */
107 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
108 /* Define this macro to evaluate to a nonzero value if GCC should refrain
109 from generating indirect strings in DWARF2 debug information, for instance
110 if your target is stuck with an old version of GDB that is unable to
111 process them properly or uses VMS Debug. */
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
113 #else
114 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
116 #endif
117
118 /* ??? Poison these here until it can be done generically. They've been
119 totally replaced in this file; make sure it stays that way. */
120 #undef DWARF2_UNWIND_INFO
121 #undef DWARF2_FRAME_INFO
122 #if (GCC_VERSION >= 3000)
123 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
124 #endif
125
126 /* The size of the target's pointer type. */
127 #ifndef PTR_SIZE
128 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
129 #endif
130
131 /* Array of RTXes referenced by the debugging information, which therefore
132 must be kept around forever. */
133 static GTY(()) VEC(rtx,gc) *used_rtx_array;
134
135 /* A pointer to the base of a list of incomplete types which might be
136 completed at some later time. incomplete_types_list needs to be a
137 VEC(tree,gc) because we want to tell the garbage collector about
138 it. */
139 static GTY(()) VEC(tree,gc) *incomplete_types;
140
141 /* A pointer to the base of a table of references to declaration
142 scopes. This table is a display which tracks the nesting
143 of declaration scopes at the current scope and containing
144 scopes. This table is used to find the proper place to
145 define type declaration DIE's. */
146 static GTY(()) VEC(tree,gc) *decl_scope_table;
147
148 /* Pointers to various DWARF2 sections. */
149 static GTY(()) section *debug_info_section;
150 static GTY(()) section *debug_abbrev_section;
151 static GTY(()) section *debug_aranges_section;
152 static GTY(()) section *debug_macinfo_section;
153 static GTY(()) section *debug_line_section;
154 static GTY(()) section *debug_loc_section;
155 static GTY(()) section *debug_pubnames_section;
156 static GTY(()) section *debug_pubtypes_section;
157 static GTY(()) section *debug_str_section;
158 static GTY(()) section *debug_ranges_section;
159 static GTY(()) section *debug_frame_section;
160
161 /* Maximum size (in bytes) of an artificially generated label. */
162 #define MAX_ARTIFICIAL_LABEL_BYTES 30
163
164 /* According to the (draft) DWARF 3 specification, the initial length
165 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
166 bytes are 0xffffffff, followed by the length stored in the next 8
167 bytes.
168
169 However, the SGI/MIPS ABI uses an initial length which is equal to
170 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
171
172 #ifndef DWARF_INITIAL_LENGTH_SIZE
173 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
174 #endif
175
176 /* Round SIZE up to the nearest BOUNDARY. */
177 #define DWARF_ROUND(SIZE,BOUNDARY) \
178 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
179
180 /* CIE identifier. */
181 #if HOST_BITS_PER_WIDE_INT >= 64
182 #define DWARF_CIE_ID \
183 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
184 #else
185 #define DWARF_CIE_ID DW_CIE_ID
186 #endif
187
188 DEF_VEC_P (dw_fde_ref);
189 DEF_VEC_ALLOC_P (dw_fde_ref, gc);
190
191 /* A vector for a table that contains frame description
192 information for each routine. */
193 static GTY(()) VEC(dw_fde_ref, gc) *fde_vec;
194
195 struct GTY(()) indirect_string_node {
196 const char *str;
197 unsigned int refcount;
198 enum dwarf_form form;
199 char *label;
200 };
201
202 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
203
204 static GTY(()) int dw2_string_counter;
205
206 /* True if the compilation unit places functions in more than one section. */
207 static GTY(()) bool have_multiple_function_sections = false;
208
209 /* Whether the default text and cold text sections have been used at all. */
210
211 static GTY(()) bool text_section_used = false;
212 static GTY(()) bool cold_text_section_used = false;
213
214 /* The default cold text section. */
215 static GTY(()) section *cold_text_section;
216
217 /* Forward declarations for functions defined in this file. */
218
219 static char *stripattributes (const char *);
220 static void output_call_frame_info (int);
221 static void dwarf2out_note_section_used (void);
222
223 /* Personality decl of current unit. Used only when assembler does not support
224 personality CFI. */
225 static GTY(()) rtx current_unit_personality;
226
227 /* Data and reference forms for relocatable data. */
228 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
229 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
230
231 #ifndef DEBUG_FRAME_SECTION
232 #define DEBUG_FRAME_SECTION ".debug_frame"
233 #endif
234
235 #ifndef FUNC_BEGIN_LABEL
236 #define FUNC_BEGIN_LABEL "LFB"
237 #endif
238
239 #ifndef FUNC_END_LABEL
240 #define FUNC_END_LABEL "LFE"
241 #endif
242
243 #ifndef PROLOGUE_END_LABEL
244 #define PROLOGUE_END_LABEL "LPE"
245 #endif
246
247 #ifndef EPILOGUE_BEGIN_LABEL
248 #define EPILOGUE_BEGIN_LABEL "LEB"
249 #endif
250
251 #ifndef FRAME_BEGIN_LABEL
252 #define FRAME_BEGIN_LABEL "Lframe"
253 #endif
254 #define CIE_AFTER_SIZE_LABEL "LSCIE"
255 #define CIE_END_LABEL "LECIE"
256 #define FDE_LABEL "LSFDE"
257 #define FDE_AFTER_SIZE_LABEL "LASFDE"
258 #define FDE_END_LABEL "LEFDE"
259 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
260 #define LINE_NUMBER_END_LABEL "LELT"
261 #define LN_PROLOG_AS_LABEL "LASLTP"
262 #define LN_PROLOG_END_LABEL "LELTP"
263 #define DIE_LABEL_PREFIX "DW"
264 \f
265 /* Match the base name of a file to the base name of a compilation unit. */
266
267 static int
268 matches_main_base (const char *path)
269 {
270 /* Cache the last query. */
271 static const char *last_path = NULL;
272 static int last_match = 0;
273 if (path != last_path)
274 {
275 const char *base;
276 int length = base_of_path (path, &base);
277 last_path = path;
278 last_match = (length == main_input_baselength
279 && memcmp (base, main_input_basename, length) == 0);
280 }
281 return last_match;
282 }
283
284 #ifdef DEBUG_DEBUG_STRUCT
285
286 static int
287 dump_struct_debug (tree type, enum debug_info_usage usage,
288 enum debug_struct_file criterion, int generic,
289 int matches, int result)
290 {
291 /* Find the type name. */
292 tree type_decl = TYPE_STUB_DECL (type);
293 tree t = type_decl;
294 const char *name = 0;
295 if (TREE_CODE (t) == TYPE_DECL)
296 t = DECL_NAME (t);
297 if (t)
298 name = IDENTIFIER_POINTER (t);
299
300 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
301 criterion,
302 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
303 matches ? "bas" : "hdr",
304 generic ? "gen" : "ord",
305 usage == DINFO_USAGE_DFN ? ";" :
306 usage == DINFO_USAGE_DIR_USE ? "." : "*",
307 result,
308 (void*) type_decl, name);
309 return result;
310 }
311 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
312 dump_struct_debug (type, usage, criterion, generic, matches, result)
313
314 #else
315
316 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
317 (result)
318
319 #endif
320
321 static bool
322 should_emit_struct_debug (tree type, enum debug_info_usage usage)
323 {
324 enum debug_struct_file criterion;
325 tree type_decl;
326 bool generic = lang_hooks.types.generic_p (type);
327
328 if (generic)
329 criterion = debug_struct_generic[usage];
330 else
331 criterion = debug_struct_ordinary[usage];
332
333 if (criterion == DINFO_STRUCT_FILE_NONE)
334 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
335 if (criterion == DINFO_STRUCT_FILE_ANY)
336 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
337
338 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
339
340 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
341 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
342
343 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
344 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
345 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
346 }
347 \f
348 /* Return a pointer to a copy of the section string name S with all
349 attributes stripped off, and an asterisk prepended (for assemble_name). */
350
351 static inline char *
352 stripattributes (const char *s)
353 {
354 char *stripped = XNEWVEC (char, strlen (s) + 2);
355 char *p = stripped;
356
357 *p++ = '*';
358
359 while (*s && *s != ',')
360 *p++ = *s++;
361
362 *p = '\0';
363 return stripped;
364 }
365
366 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
367 switch to the data section instead, and write out a synthetic start label
368 for collect2 the first time around. */
369
370 static void
371 switch_to_eh_frame_section (bool back)
372 {
373 tree label;
374
375 #ifdef EH_FRAME_SECTION_NAME
376 if (eh_frame_section == 0)
377 {
378 int flags;
379
380 if (EH_TABLES_CAN_BE_READ_ONLY)
381 {
382 int fde_encoding;
383 int per_encoding;
384 int lsda_encoding;
385
386 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
387 /*global=*/0);
388 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
389 /*global=*/1);
390 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
391 /*global=*/0);
392 flags = ((! flag_pic
393 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
394 && (fde_encoding & 0x70) != DW_EH_PE_aligned
395 && (per_encoding & 0x70) != DW_EH_PE_absptr
396 && (per_encoding & 0x70) != DW_EH_PE_aligned
397 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
398 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
399 ? 0 : SECTION_WRITE);
400 }
401 else
402 flags = SECTION_WRITE;
403 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
404 }
405 #endif /* EH_FRAME_SECTION_NAME */
406
407 if (eh_frame_section)
408 switch_to_section (eh_frame_section);
409 else
410 {
411 /* We have no special eh_frame section. Put the information in
412 the data section and emit special labels to guide collect2. */
413 switch_to_section (data_section);
414
415 if (!back)
416 {
417 label = get_file_function_name ("F");
418 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
419 targetm.asm_out.globalize_label (asm_out_file,
420 IDENTIFIER_POINTER (label));
421 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
422 }
423 }
424 }
425
426 /* Switch [BACK] to the eh or debug frame table section, depending on
427 FOR_EH. */
428
429 static void
430 switch_to_frame_table_section (int for_eh, bool back)
431 {
432 if (for_eh)
433 switch_to_eh_frame_section (back);
434 else
435 {
436 if (!debug_frame_section)
437 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
438 SECTION_DEBUG, NULL);
439 switch_to_section (debug_frame_section);
440 }
441 }
442
443 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
444
445 enum dw_cfi_oprnd_type
446 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
447 {
448 switch (cfi)
449 {
450 case DW_CFA_nop:
451 case DW_CFA_GNU_window_save:
452 case DW_CFA_remember_state:
453 case DW_CFA_restore_state:
454 return dw_cfi_oprnd_unused;
455
456 case DW_CFA_set_loc:
457 case DW_CFA_advance_loc1:
458 case DW_CFA_advance_loc2:
459 case DW_CFA_advance_loc4:
460 case DW_CFA_MIPS_advance_loc8:
461 return dw_cfi_oprnd_addr;
462
463 case DW_CFA_offset:
464 case DW_CFA_offset_extended:
465 case DW_CFA_def_cfa:
466 case DW_CFA_offset_extended_sf:
467 case DW_CFA_def_cfa_sf:
468 case DW_CFA_restore:
469 case DW_CFA_restore_extended:
470 case DW_CFA_undefined:
471 case DW_CFA_same_value:
472 case DW_CFA_def_cfa_register:
473 case DW_CFA_register:
474 case DW_CFA_expression:
475 return dw_cfi_oprnd_reg_num;
476
477 case DW_CFA_def_cfa_offset:
478 case DW_CFA_GNU_args_size:
479 case DW_CFA_def_cfa_offset_sf:
480 return dw_cfi_oprnd_offset;
481
482 case DW_CFA_def_cfa_expression:
483 return dw_cfi_oprnd_loc;
484
485 default:
486 gcc_unreachable ();
487 }
488 }
489
490 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
491
492 enum dw_cfi_oprnd_type
493 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
494 {
495 switch (cfi)
496 {
497 case DW_CFA_def_cfa:
498 case DW_CFA_def_cfa_sf:
499 case DW_CFA_offset:
500 case DW_CFA_offset_extended_sf:
501 case DW_CFA_offset_extended:
502 return dw_cfi_oprnd_offset;
503
504 case DW_CFA_register:
505 return dw_cfi_oprnd_reg_num;
506
507 case DW_CFA_expression:
508 return dw_cfi_oprnd_loc;
509
510 default:
511 return dw_cfi_oprnd_unused;
512 }
513 }
514
515 /* Output one FDE. */
516
517 static void
518 output_fde (dw_fde_ref fde, bool for_eh, bool second,
519 char *section_start_label, int fde_encoding, char *augmentation,
520 bool any_lsda_needed, int lsda_encoding)
521 {
522 const char *begin, *end;
523 static unsigned int j;
524 char l1[20], l2[20];
525
526 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
527 /* empty */ 0);
528 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
529 for_eh + j);
530 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
531 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
532 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
533 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
534 " indicating 64-bit DWARF extension");
535 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
536 "FDE Length");
537 ASM_OUTPUT_LABEL (asm_out_file, l1);
538
539 if (for_eh)
540 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
541 else
542 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
543 debug_frame_section, "FDE CIE offset");
544
545 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
546 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
547
548 if (for_eh)
549 {
550 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
551 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
552 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
553 "FDE initial location");
554 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
555 end, begin, "FDE address range");
556 }
557 else
558 {
559 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
560 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
561 }
562
563 if (augmentation[0])
564 {
565 if (any_lsda_needed)
566 {
567 int size = size_of_encoded_value (lsda_encoding);
568
569 if (lsda_encoding == DW_EH_PE_aligned)
570 {
571 int offset = ( 4 /* Length */
572 + 4 /* CIE offset */
573 + 2 * size_of_encoded_value (fde_encoding)
574 + 1 /* Augmentation size */ );
575 int pad = -offset & (PTR_SIZE - 1);
576
577 size += pad;
578 gcc_assert (size_of_uleb128 (size) == 1);
579 }
580
581 dw2_asm_output_data_uleb128 (size, "Augmentation size");
582
583 if (fde->uses_eh_lsda)
584 {
585 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
586 fde->funcdef_number);
587 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
588 gen_rtx_SYMBOL_REF (Pmode, l1),
589 false,
590 "Language Specific Data Area");
591 }
592 else
593 {
594 if (lsda_encoding == DW_EH_PE_aligned)
595 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
596 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
597 "Language Specific Data Area (none)");
598 }
599 }
600 else
601 dw2_asm_output_data_uleb128 (0, "Augmentation size");
602 }
603
604 /* Loop through the Call Frame Instructions associated with this FDE. */
605 fde->dw_fde_current_label = begin;
606 {
607 size_t from, until, i;
608
609 from = 0;
610 until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
611
612 if (fde->dw_fde_second_begin == NULL)
613 ;
614 else if (!second)
615 until = fde->dw_fde_switch_cfi_index;
616 else
617 from = fde->dw_fde_switch_cfi_index;
618
619 for (i = from; i < until; i++)
620 output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i), fde, for_eh);
621 }
622
623 /* If we are to emit a ref/link from function bodies to their frame tables,
624 do it now. This is typically performed to make sure that tables
625 associated with functions are dragged with them and not discarded in
626 garbage collecting links. We need to do this on a per function basis to
627 cope with -ffunction-sections. */
628
629 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
630 /* Switch to the function section, emit the ref to the tables, and
631 switch *back* into the table section. */
632 switch_to_section (function_section (fde->decl));
633 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
634 switch_to_frame_table_section (for_eh, true);
635 #endif
636
637 /* Pad the FDE out to an address sized boundary. */
638 ASM_OUTPUT_ALIGN (asm_out_file,
639 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
640 ASM_OUTPUT_LABEL (asm_out_file, l2);
641
642 j += 2;
643 }
644
645 /* Return true if frame description entry FDE is needed for EH. */
646
647 static bool
648 fde_needed_for_eh_p (dw_fde_ref fde)
649 {
650 if (flag_asynchronous_unwind_tables)
651 return true;
652
653 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
654 return true;
655
656 if (fde->uses_eh_lsda)
657 return true;
658
659 /* If exceptions are enabled, we have collected nothrow info. */
660 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
661 return false;
662
663 return true;
664 }
665
666 /* Output the call frame information used to record information
667 that relates to calculating the frame pointer, and records the
668 location of saved registers. */
669
670 static void
671 output_call_frame_info (int for_eh)
672 {
673 unsigned int i;
674 dw_fde_ref fde;
675 dw_cfi_ref cfi;
676 char l1[20], l2[20], section_start_label[20];
677 bool any_lsda_needed = false;
678 char augmentation[6];
679 int augmentation_size;
680 int fde_encoding = DW_EH_PE_absptr;
681 int per_encoding = DW_EH_PE_absptr;
682 int lsda_encoding = DW_EH_PE_absptr;
683 int return_reg;
684 rtx personality = NULL;
685 int dw_cie_version;
686
687 /* Don't emit a CIE if there won't be any FDEs. */
688 if (fde_vec == NULL)
689 return;
690
691 /* Nothing to do if the assembler's doing it all. */
692 if (dwarf2out_do_cfi_asm ())
693 return;
694
695 /* If we don't have any functions we'll want to unwind out of, don't emit
696 any EH unwind information. If we make FDEs linkonce, we may have to
697 emit an empty label for an FDE that wouldn't otherwise be emitted. We
698 want to avoid having an FDE kept around when the function it refers to
699 is discarded. Example where this matters: a primary function template
700 in C++ requires EH information, an explicit specialization doesn't. */
701 if (for_eh)
702 {
703 bool any_eh_needed = false;
704
705 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
706 {
707 if (fde->uses_eh_lsda)
708 any_eh_needed = any_lsda_needed = true;
709 else if (fde_needed_for_eh_p (fde))
710 any_eh_needed = true;
711 else if (TARGET_USES_WEAK_UNWIND_INFO)
712 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
713 }
714
715 if (!any_eh_needed)
716 return;
717 }
718
719 /* We're going to be generating comments, so turn on app. */
720 if (flag_debug_asm)
721 app_enable ();
722
723 /* Switch to the proper frame section, first time. */
724 switch_to_frame_table_section (for_eh, false);
725
726 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
727 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
728
729 /* Output the CIE. */
730 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
731 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
732 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
733 dw2_asm_output_data (4, 0xffffffff,
734 "Initial length escape value indicating 64-bit DWARF extension");
735 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
736 "Length of Common Information Entry");
737 ASM_OUTPUT_LABEL (asm_out_file, l1);
738
739 /* Now that the CIE pointer is PC-relative for EH,
740 use 0 to identify the CIE. */
741 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
742 (for_eh ? 0 : DWARF_CIE_ID),
743 "CIE Identifier Tag");
744
745 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
746 use CIE version 1, unless that would produce incorrect results
747 due to overflowing the return register column. */
748 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
749 dw_cie_version = 1;
750 if (return_reg >= 256 || dwarf_version > 2)
751 dw_cie_version = 3;
752 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
753
754 augmentation[0] = 0;
755 augmentation_size = 0;
756
757 personality = current_unit_personality;
758 if (for_eh)
759 {
760 char *p;
761
762 /* Augmentation:
763 z Indicates that a uleb128 is present to size the
764 augmentation section.
765 L Indicates the encoding (and thus presence) of
766 an LSDA pointer in the FDE augmentation.
767 R Indicates a non-default pointer encoding for
768 FDE code pointers.
769 P Indicates the presence of an encoding + language
770 personality routine in the CIE augmentation. */
771
772 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
773 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
774 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
775
776 p = augmentation + 1;
777 if (personality)
778 {
779 *p++ = 'P';
780 augmentation_size += 1 + size_of_encoded_value (per_encoding);
781 assemble_external_libcall (personality);
782 }
783 if (any_lsda_needed)
784 {
785 *p++ = 'L';
786 augmentation_size += 1;
787 }
788 if (fde_encoding != DW_EH_PE_absptr)
789 {
790 *p++ = 'R';
791 augmentation_size += 1;
792 }
793 if (p > augmentation + 1)
794 {
795 augmentation[0] = 'z';
796 *p = '\0';
797 }
798
799 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
800 if (personality && per_encoding == DW_EH_PE_aligned)
801 {
802 int offset = ( 4 /* Length */
803 + 4 /* CIE Id */
804 + 1 /* CIE version */
805 + strlen (augmentation) + 1 /* Augmentation */
806 + size_of_uleb128 (1) /* Code alignment */
807 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
808 + 1 /* RA column */
809 + 1 /* Augmentation size */
810 + 1 /* Personality encoding */ );
811 int pad = -offset & (PTR_SIZE - 1);
812
813 augmentation_size += pad;
814
815 /* Augmentations should be small, so there's scarce need to
816 iterate for a solution. Die if we exceed one uleb128 byte. */
817 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
818 }
819 }
820
821 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
822 if (dw_cie_version >= 4)
823 {
824 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
825 dw2_asm_output_data (1, 0, "CIE Segment Size");
826 }
827 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
828 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
829 "CIE Data Alignment Factor");
830
831 if (dw_cie_version == 1)
832 dw2_asm_output_data (1, return_reg, "CIE RA Column");
833 else
834 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
835
836 if (augmentation[0])
837 {
838 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
839 if (personality)
840 {
841 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
842 eh_data_format_name (per_encoding));
843 dw2_asm_output_encoded_addr_rtx (per_encoding,
844 personality,
845 true, NULL);
846 }
847
848 if (any_lsda_needed)
849 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
850 eh_data_format_name (lsda_encoding));
851
852 if (fde_encoding != DW_EH_PE_absptr)
853 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
854 eh_data_format_name (fde_encoding));
855 }
856
857 FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
858 output_cfi (cfi, NULL, for_eh);
859
860 /* Pad the CIE out to an address sized boundary. */
861 ASM_OUTPUT_ALIGN (asm_out_file,
862 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
863 ASM_OUTPUT_LABEL (asm_out_file, l2);
864
865 /* Loop through all of the FDE's. */
866 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
867 {
868 unsigned int k;
869
870 /* Don't emit EH unwind info for leaf functions that don't need it. */
871 if (for_eh && !fde_needed_for_eh_p (fde))
872 continue;
873
874 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
875 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
876 augmentation, any_lsda_needed, lsda_encoding);
877 }
878
879 if (for_eh && targetm.terminate_dw2_eh_frame_info)
880 dw2_asm_output_data (4, 0, "End of Table");
881
882 /* Turn off app to make assembly quicker. */
883 if (flag_debug_asm)
884 app_disable ();
885 }
886
887 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
888
889 static void
890 dwarf2out_do_cfi_startproc (bool second)
891 {
892 int enc;
893 rtx ref;
894 rtx personality = get_personality_function (current_function_decl);
895
896 fprintf (asm_out_file, "\t.cfi_startproc\n");
897
898 if (personality)
899 {
900 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
901 ref = personality;
902
903 /* ??? The GAS support isn't entirely consistent. We have to
904 handle indirect support ourselves, but PC-relative is done
905 in the assembler. Further, the assembler can't handle any
906 of the weirder relocation types. */
907 if (enc & DW_EH_PE_indirect)
908 ref = dw2_force_const_mem (ref, true);
909
910 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
911 output_addr_const (asm_out_file, ref);
912 fputc ('\n', asm_out_file);
913 }
914
915 if (crtl->uses_eh_lsda)
916 {
917 char lab[20];
918
919 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
920 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
921 current_function_funcdef_no);
922 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
923 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
924
925 if (enc & DW_EH_PE_indirect)
926 ref = dw2_force_const_mem (ref, true);
927
928 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
929 output_addr_const (asm_out_file, ref);
930 fputc ('\n', asm_out_file);
931 }
932 }
933
934 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
935 this allocation may be done before pass_final. */
936
937 dw_fde_ref
938 dwarf2out_alloc_current_fde (void)
939 {
940 dw_fde_ref fde;
941
942 fde = ggc_alloc_cleared_dw_fde_node ();
943 fde->decl = current_function_decl;
944 fde->funcdef_number = current_function_funcdef_no;
945 fde->fde_index = VEC_length (dw_fde_ref, fde_vec);
946 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
947 fde->uses_eh_lsda = crtl->uses_eh_lsda;
948 fde->nothrow = crtl->nothrow;
949 fde->drap_reg = INVALID_REGNUM;
950 fde->vdrap_reg = INVALID_REGNUM;
951
952 /* Record the FDE associated with this function. */
953 cfun->fde = fde;
954 VEC_safe_push (dw_fde_ref, gc, fde_vec, fde);
955
956 return fde;
957 }
958
959 /* Output a marker (i.e. a label) for the beginning of a function, before
960 the prologue. */
961
962 void
963 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
964 const char *file ATTRIBUTE_UNUSED)
965 {
966 char label[MAX_ARTIFICIAL_LABEL_BYTES];
967 char * dup_label;
968 dw_fde_ref fde;
969 section *fnsec;
970 bool do_frame;
971
972 current_function_func_begin_label = NULL;
973
974 do_frame = dwarf2out_do_frame ();
975
976 /* ??? current_function_func_begin_label is also used by except.c for
977 call-site information. We must emit this label if it might be used. */
978 if (!do_frame
979 && (!flag_exceptions
980 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
981 return;
982
983 fnsec = function_section (current_function_decl);
984 switch_to_section (fnsec);
985 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
986 current_function_funcdef_no);
987 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
988 current_function_funcdef_no);
989 dup_label = xstrdup (label);
990 current_function_func_begin_label = dup_label;
991
992 /* We can elide the fde allocation if we're not emitting debug info. */
993 if (!do_frame)
994 return;
995
996 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
997 emit insns as rtx but bypass the bulk of rest_of_compilation, which
998 would include pass_dwarf2_frame. If we've not created the FDE yet,
999 do so now. */
1000 fde = cfun->fde;
1001 if (fde == NULL)
1002 fde = dwarf2out_alloc_current_fde ();
1003
1004 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1005 fde->dw_fde_begin = dup_label;
1006 fde->dw_fde_current_label = dup_label;
1007 fde->in_std_section = (fnsec == text_section
1008 || (cold_text_section && fnsec == cold_text_section));
1009
1010 /* We only want to output line number information for the genuine dwarf2
1011 prologue case, not the eh frame case. */
1012 #ifdef DWARF2_DEBUGGING_INFO
1013 if (file)
1014 dwarf2out_source_line (line, file, 0, true);
1015 #endif
1016
1017 if (dwarf2out_do_cfi_asm ())
1018 dwarf2out_do_cfi_startproc (false);
1019 else
1020 {
1021 rtx personality = get_personality_function (current_function_decl);
1022 if (!current_unit_personality)
1023 current_unit_personality = personality;
1024
1025 /* We cannot keep a current personality per function as without CFI
1026 asm, at the point where we emit the CFI data, there is no current
1027 function anymore. */
1028 if (personality && current_unit_personality != personality)
1029 sorry ("multiple EH personalities are supported only with assemblers "
1030 "supporting .cfi_personality directive");
1031 }
1032 }
1033
1034 /* Output a marker (i.e. a label) for the end of the generated code
1035 for a function prologue. This gets called *after* the prologue code has
1036 been generated. */
1037
1038 void
1039 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1040 const char *file ATTRIBUTE_UNUSED)
1041 {
1042 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1043
1044 /* Output a label to mark the endpoint of the code generated for this
1045 function. */
1046 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1047 current_function_funcdef_no);
1048 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1049 current_function_funcdef_no);
1050 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1051 }
1052
1053 /* Output a marker (i.e. a label) for the beginning of the generated code
1054 for a function epilogue. This gets called *before* the prologue code has
1055 been generated. */
1056
1057 void
1058 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1059 const char *file ATTRIBUTE_UNUSED)
1060 {
1061 dw_fde_ref fde = cfun->fde;
1062 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1063
1064 if (fde->dw_fde_vms_begin_epilogue)
1065 return;
1066
1067 /* Output a label to mark the endpoint of the code generated for this
1068 function. */
1069 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1070 current_function_funcdef_no);
1071 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1072 current_function_funcdef_no);
1073 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1074 }
1075
1076 /* Output a marker (i.e. a label) for the absolute end of the generated code
1077 for a function definition. This gets called *after* the epilogue code has
1078 been generated. */
1079
1080 void
1081 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1082 const char *file ATTRIBUTE_UNUSED)
1083 {
1084 dw_fde_ref fde;
1085 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1086
1087 last_var_location_insn = NULL_RTX;
1088 cached_next_real_insn = NULL_RTX;
1089
1090 if (dwarf2out_do_cfi_asm ())
1091 fprintf (asm_out_file, "\t.cfi_endproc\n");
1092
1093 /* Output a label to mark the endpoint of the code generated for this
1094 function. */
1095 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1096 current_function_funcdef_no);
1097 ASM_OUTPUT_LABEL (asm_out_file, label);
1098 fde = cfun->fde;
1099 gcc_assert (fde != NULL);
1100 if (fde->dw_fde_second_begin == NULL)
1101 fde->dw_fde_end = xstrdup (label);
1102 }
1103
1104 void
1105 dwarf2out_frame_finish (void)
1106 {
1107 /* Output call frame information. */
1108 if (targetm.debug_unwind_info () == UI_DWARF2)
1109 output_call_frame_info (0);
1110
1111 /* Output another copy for the unwinder. */
1112 if ((flag_unwind_tables || flag_exceptions)
1113 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1114 output_call_frame_info (1);
1115 }
1116
1117 /* Note that the current function section is being used for code. */
1118
1119 static void
1120 dwarf2out_note_section_used (void)
1121 {
1122 section *sec = current_function_section ();
1123 if (sec == text_section)
1124 text_section_used = true;
1125 else if (sec == cold_text_section)
1126 cold_text_section_used = true;
1127 }
1128
1129 static void var_location_switch_text_section (void);
1130 static void set_cur_line_info_table (section *);
1131
1132 void
1133 dwarf2out_switch_text_section (void)
1134 {
1135 section *sect;
1136 dw_fde_ref fde = cfun->fde;
1137
1138 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1139
1140 if (!in_cold_section_p)
1141 {
1142 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1143 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1144 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1145 }
1146 else
1147 {
1148 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1149 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1150 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1151 }
1152 have_multiple_function_sections = true;
1153
1154 /* There is no need to mark used sections when not debugging. */
1155 if (cold_text_section != NULL)
1156 dwarf2out_note_section_used ();
1157
1158 if (dwarf2out_do_cfi_asm ())
1159 fprintf (asm_out_file, "\t.cfi_endproc\n");
1160
1161 /* Now do the real section switch. */
1162 sect = current_function_section ();
1163 switch_to_section (sect);
1164
1165 fde->second_in_std_section
1166 = (sect == text_section
1167 || (cold_text_section && sect == cold_text_section));
1168
1169 if (dwarf2out_do_cfi_asm ())
1170 dwarf2out_do_cfi_startproc (true);
1171
1172 var_location_switch_text_section ();
1173
1174 if (cold_text_section != NULL)
1175 set_cur_line_info_table (sect);
1176 }
1177 \f
1178 /* And now, the subset of the debugging information support code necessary
1179 for emitting location expressions. */
1180
1181 /* Data about a single source file. */
1182 struct GTY(()) dwarf_file_data {
1183 const char * filename;
1184 int emitted_number;
1185 };
1186
1187 typedef struct GTY(()) deferred_locations_struct
1188 {
1189 tree variable;
1190 dw_die_ref die;
1191 } deferred_locations;
1192
1193 DEF_VEC_O(deferred_locations);
1194 DEF_VEC_ALLOC_O(deferred_locations,gc);
1195
1196 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
1197
1198 DEF_VEC_P(dw_die_ref);
1199 DEF_VEC_ALLOC_P(dw_die_ref,heap);
1200
1201 /* Location lists are ranges + location descriptions for that range,
1202 so you can track variables that are in different places over
1203 their entire life. */
1204 typedef struct GTY(()) dw_loc_list_struct {
1205 dw_loc_list_ref dw_loc_next;
1206 const char *begin; /* Label for begin address of range */
1207 const char *end; /* Label for end address of range */
1208 char *ll_symbol; /* Label for beginning of location list.
1209 Only on head of list */
1210 const char *section; /* Section this loclist is relative to */
1211 dw_loc_descr_ref expr;
1212 hashval_t hash;
1213 /* True if all addresses in this and subsequent lists are known to be
1214 resolved. */
1215 bool resolved_addr;
1216 /* True if this list has been replaced by dw_loc_next. */
1217 bool replaced;
1218 bool emitted;
1219 /* True if the range should be emitted even if begin and end
1220 are the same. */
1221 bool force;
1222 } dw_loc_list_node;
1223
1224 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1225
1226 /* Convert a DWARF stack opcode into its string name. */
1227
1228 static const char *
1229 dwarf_stack_op_name (unsigned int op)
1230 {
1231 const char *name = get_DW_OP_name (op);
1232
1233 if (name != NULL)
1234 return name;
1235
1236 return "OP_<unknown>";
1237 }
1238
1239 /* Return a pointer to a newly allocated location description. Location
1240 descriptions are simple expression terms that can be strung
1241 together to form more complicated location (address) descriptions. */
1242
1243 static inline dw_loc_descr_ref
1244 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1245 unsigned HOST_WIDE_INT oprnd2)
1246 {
1247 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1248
1249 descr->dw_loc_opc = op;
1250 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1251 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1252 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1253 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1254
1255 return descr;
1256 }
1257
1258 /* Return a pointer to a newly allocated location description for
1259 REG and OFFSET. */
1260
1261 static inline dw_loc_descr_ref
1262 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1263 {
1264 if (reg <= 31)
1265 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1266 offset, 0);
1267 else
1268 return new_loc_descr (DW_OP_bregx, reg, offset);
1269 }
1270
1271 /* Add a location description term to a location description expression. */
1272
1273 static inline void
1274 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1275 {
1276 dw_loc_descr_ref *d;
1277
1278 /* Find the end of the chain. */
1279 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1280 ;
1281
1282 *d = descr;
1283 }
1284
1285 /* Compare two location operands for exact equality. */
1286
1287 static bool
1288 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1289 {
1290 if (a->val_class != b->val_class)
1291 return false;
1292 switch (a->val_class)
1293 {
1294 case dw_val_class_none:
1295 return true;
1296 case dw_val_class_addr:
1297 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1298
1299 case dw_val_class_offset:
1300 case dw_val_class_unsigned_const:
1301 case dw_val_class_const:
1302 case dw_val_class_range_list:
1303 case dw_val_class_lineptr:
1304 case dw_val_class_macptr:
1305 /* These are all HOST_WIDE_INT, signed or unsigned. */
1306 return a->v.val_unsigned == b->v.val_unsigned;
1307
1308 case dw_val_class_loc:
1309 return a->v.val_loc == b->v.val_loc;
1310 case dw_val_class_loc_list:
1311 return a->v.val_loc_list == b->v.val_loc_list;
1312 case dw_val_class_die_ref:
1313 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1314 case dw_val_class_fde_ref:
1315 return a->v.val_fde_index == b->v.val_fde_index;
1316 case dw_val_class_lbl_id:
1317 case dw_val_class_high_pc:
1318 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1319 case dw_val_class_str:
1320 return a->v.val_str == b->v.val_str;
1321 case dw_val_class_flag:
1322 return a->v.val_flag == b->v.val_flag;
1323 case dw_val_class_file:
1324 return a->v.val_file == b->v.val_file;
1325 case dw_val_class_decl_ref:
1326 return a->v.val_decl_ref == b->v.val_decl_ref;
1327
1328 case dw_val_class_const_double:
1329 return (a->v.val_double.high == b->v.val_double.high
1330 && a->v.val_double.low == b->v.val_double.low);
1331
1332 case dw_val_class_vec:
1333 {
1334 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1335 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1336
1337 return (a_len == b_len
1338 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1339 }
1340
1341 case dw_val_class_data8:
1342 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1343
1344 case dw_val_class_vms_delta:
1345 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1346 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1347 }
1348 gcc_unreachable ();
1349 }
1350
1351 /* Compare two location atoms for exact equality. */
1352
1353 static bool
1354 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1355 {
1356 if (a->dw_loc_opc != b->dw_loc_opc)
1357 return false;
1358
1359 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1360 address size, but since we always allocate cleared storage it
1361 should be zero for other types of locations. */
1362 if (a->dtprel != b->dtprel)
1363 return false;
1364
1365 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1366 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1367 }
1368
1369 /* Compare two complete location expressions for exact equality. */
1370
1371 bool
1372 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1373 {
1374 while (1)
1375 {
1376 if (a == b)
1377 return true;
1378 if (a == NULL || b == NULL)
1379 return false;
1380 if (!loc_descr_equal_p_1 (a, b))
1381 return false;
1382
1383 a = a->dw_loc_next;
1384 b = b->dw_loc_next;
1385 }
1386 }
1387
1388
1389 /* Add a constant OFFSET to a location expression. */
1390
1391 static void
1392 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1393 {
1394 dw_loc_descr_ref loc;
1395 HOST_WIDE_INT *p;
1396
1397 gcc_assert (*list_head != NULL);
1398
1399 if (!offset)
1400 return;
1401
1402 /* Find the end of the chain. */
1403 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1404 ;
1405
1406 p = NULL;
1407 if (loc->dw_loc_opc == DW_OP_fbreg
1408 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1409 p = &loc->dw_loc_oprnd1.v.val_int;
1410 else if (loc->dw_loc_opc == DW_OP_bregx)
1411 p = &loc->dw_loc_oprnd2.v.val_int;
1412
1413 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1414 offset. Don't optimize if an signed integer overflow would happen. */
1415 if (p != NULL
1416 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1417 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1418 *p += offset;
1419
1420 else if (offset > 0)
1421 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1422
1423 else
1424 {
1425 loc->dw_loc_next = int_loc_descriptor (-offset);
1426 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1427 }
1428 }
1429
1430 /* Add a constant OFFSET to a location list. */
1431
1432 static void
1433 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1434 {
1435 dw_loc_list_ref d;
1436 for (d = list_head; d != NULL; d = d->dw_loc_next)
1437 loc_descr_plus_const (&d->expr, offset);
1438 }
1439
1440 #define DWARF_REF_SIZE \
1441 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1442
1443 static unsigned long int get_base_type_offset (dw_die_ref);
1444
1445 /* Return the size of a location descriptor. */
1446
1447 static unsigned long
1448 size_of_loc_descr (dw_loc_descr_ref loc)
1449 {
1450 unsigned long size = 1;
1451
1452 switch (loc->dw_loc_opc)
1453 {
1454 case DW_OP_addr:
1455 size += DWARF2_ADDR_SIZE;
1456 break;
1457 case DW_OP_const1u:
1458 case DW_OP_const1s:
1459 size += 1;
1460 break;
1461 case DW_OP_const2u:
1462 case DW_OP_const2s:
1463 size += 2;
1464 break;
1465 case DW_OP_const4u:
1466 case DW_OP_const4s:
1467 size += 4;
1468 break;
1469 case DW_OP_const8u:
1470 case DW_OP_const8s:
1471 size += 8;
1472 break;
1473 case DW_OP_constu:
1474 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1475 break;
1476 case DW_OP_consts:
1477 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1478 break;
1479 case DW_OP_pick:
1480 size += 1;
1481 break;
1482 case DW_OP_plus_uconst:
1483 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1484 break;
1485 case DW_OP_skip:
1486 case DW_OP_bra:
1487 size += 2;
1488 break;
1489 case DW_OP_breg0:
1490 case DW_OP_breg1:
1491 case DW_OP_breg2:
1492 case DW_OP_breg3:
1493 case DW_OP_breg4:
1494 case DW_OP_breg5:
1495 case DW_OP_breg6:
1496 case DW_OP_breg7:
1497 case DW_OP_breg8:
1498 case DW_OP_breg9:
1499 case DW_OP_breg10:
1500 case DW_OP_breg11:
1501 case DW_OP_breg12:
1502 case DW_OP_breg13:
1503 case DW_OP_breg14:
1504 case DW_OP_breg15:
1505 case DW_OP_breg16:
1506 case DW_OP_breg17:
1507 case DW_OP_breg18:
1508 case DW_OP_breg19:
1509 case DW_OP_breg20:
1510 case DW_OP_breg21:
1511 case DW_OP_breg22:
1512 case DW_OP_breg23:
1513 case DW_OP_breg24:
1514 case DW_OP_breg25:
1515 case DW_OP_breg26:
1516 case DW_OP_breg27:
1517 case DW_OP_breg28:
1518 case DW_OP_breg29:
1519 case DW_OP_breg30:
1520 case DW_OP_breg31:
1521 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1522 break;
1523 case DW_OP_regx:
1524 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1525 break;
1526 case DW_OP_fbreg:
1527 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1528 break;
1529 case DW_OP_bregx:
1530 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1531 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1532 break;
1533 case DW_OP_piece:
1534 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1535 break;
1536 case DW_OP_bit_piece:
1537 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1538 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1539 break;
1540 case DW_OP_deref_size:
1541 case DW_OP_xderef_size:
1542 size += 1;
1543 break;
1544 case DW_OP_call2:
1545 size += 2;
1546 break;
1547 case DW_OP_call4:
1548 size += 4;
1549 break;
1550 case DW_OP_call_ref:
1551 size += DWARF_REF_SIZE;
1552 break;
1553 case DW_OP_implicit_value:
1554 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1555 + loc->dw_loc_oprnd1.v.val_unsigned;
1556 break;
1557 case DW_OP_GNU_implicit_pointer:
1558 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1559 break;
1560 case DW_OP_GNU_entry_value:
1561 {
1562 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1563 size += size_of_uleb128 (op_size) + op_size;
1564 break;
1565 }
1566 case DW_OP_GNU_const_type:
1567 {
1568 unsigned long o
1569 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1570 size += size_of_uleb128 (o) + 1;
1571 switch (loc->dw_loc_oprnd2.val_class)
1572 {
1573 case dw_val_class_vec:
1574 size += loc->dw_loc_oprnd2.v.val_vec.length
1575 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1576 break;
1577 case dw_val_class_const:
1578 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1579 break;
1580 case dw_val_class_const_double:
1581 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1582 break;
1583 default:
1584 gcc_unreachable ();
1585 }
1586 break;
1587 }
1588 case DW_OP_GNU_regval_type:
1589 {
1590 unsigned long o
1591 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1592 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1593 + size_of_uleb128 (o);
1594 }
1595 break;
1596 case DW_OP_GNU_deref_type:
1597 {
1598 unsigned long o
1599 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1600 size += 1 + size_of_uleb128 (o);
1601 }
1602 break;
1603 case DW_OP_GNU_convert:
1604 case DW_OP_GNU_reinterpret:
1605 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1606 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1607 else
1608 {
1609 unsigned long o
1610 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1611 size += size_of_uleb128 (o);
1612 }
1613 break;
1614 case DW_OP_GNU_parameter_ref:
1615 size += 4;
1616 break;
1617 default:
1618 break;
1619 }
1620
1621 return size;
1622 }
1623
1624 /* Return the size of a series of location descriptors. */
1625
1626 unsigned long
1627 size_of_locs (dw_loc_descr_ref loc)
1628 {
1629 dw_loc_descr_ref l;
1630 unsigned long size;
1631
1632 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1633 field, to avoid writing to a PCH file. */
1634 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1635 {
1636 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1637 break;
1638 size += size_of_loc_descr (l);
1639 }
1640 if (! l)
1641 return size;
1642
1643 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1644 {
1645 l->dw_loc_addr = size;
1646 size += size_of_loc_descr (l);
1647 }
1648
1649 return size;
1650 }
1651
1652 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1653 static void get_ref_die_offset_label (char *, dw_die_ref);
1654 static unsigned long int get_ref_die_offset (dw_die_ref);
1655
1656 /* Output location description stack opcode's operands (if any).
1657 The for_eh_or_skip parameter controls whether register numbers are
1658 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1659 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1660 info). This should be suppressed for the cases that have not been converted
1661 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1662
1663 static void
1664 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1665 {
1666 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1667 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1668
1669 switch (loc->dw_loc_opc)
1670 {
1671 #ifdef DWARF2_DEBUGGING_INFO
1672 case DW_OP_const2u:
1673 case DW_OP_const2s:
1674 dw2_asm_output_data (2, val1->v.val_int, NULL);
1675 break;
1676 case DW_OP_const4u:
1677 if (loc->dtprel)
1678 {
1679 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1680 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1681 val1->v.val_addr);
1682 fputc ('\n', asm_out_file);
1683 break;
1684 }
1685 /* FALLTHRU */
1686 case DW_OP_const4s:
1687 dw2_asm_output_data (4, val1->v.val_int, NULL);
1688 break;
1689 case DW_OP_const8u:
1690 if (loc->dtprel)
1691 {
1692 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1693 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1694 val1->v.val_addr);
1695 fputc ('\n', asm_out_file);
1696 break;
1697 }
1698 /* FALLTHRU */
1699 case DW_OP_const8s:
1700 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1701 dw2_asm_output_data (8, val1->v.val_int, NULL);
1702 break;
1703 case DW_OP_skip:
1704 case DW_OP_bra:
1705 {
1706 int offset;
1707
1708 gcc_assert (val1->val_class == dw_val_class_loc);
1709 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1710
1711 dw2_asm_output_data (2, offset, NULL);
1712 }
1713 break;
1714 case DW_OP_implicit_value:
1715 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1716 switch (val2->val_class)
1717 {
1718 case dw_val_class_const:
1719 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1720 break;
1721 case dw_val_class_vec:
1722 {
1723 unsigned int elt_size = val2->v.val_vec.elt_size;
1724 unsigned int len = val2->v.val_vec.length;
1725 unsigned int i;
1726 unsigned char *p;
1727
1728 if (elt_size > sizeof (HOST_WIDE_INT))
1729 {
1730 elt_size /= 2;
1731 len *= 2;
1732 }
1733 for (i = 0, p = val2->v.val_vec.array;
1734 i < len;
1735 i++, p += elt_size)
1736 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1737 "fp or vector constant word %u", i);
1738 }
1739 break;
1740 case dw_val_class_const_double:
1741 {
1742 unsigned HOST_WIDE_INT first, second;
1743
1744 if (WORDS_BIG_ENDIAN)
1745 {
1746 first = val2->v.val_double.high;
1747 second = val2->v.val_double.low;
1748 }
1749 else
1750 {
1751 first = val2->v.val_double.low;
1752 second = val2->v.val_double.high;
1753 }
1754 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1755 first, NULL);
1756 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1757 second, NULL);
1758 }
1759 break;
1760 case dw_val_class_addr:
1761 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1762 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1763 break;
1764 default:
1765 gcc_unreachable ();
1766 }
1767 break;
1768 #else
1769 case DW_OP_const2u:
1770 case DW_OP_const2s:
1771 case DW_OP_const4u:
1772 case DW_OP_const4s:
1773 case DW_OP_const8u:
1774 case DW_OP_const8s:
1775 case DW_OP_skip:
1776 case DW_OP_bra:
1777 case DW_OP_implicit_value:
1778 /* We currently don't make any attempt to make sure these are
1779 aligned properly like we do for the main unwind info, so
1780 don't support emitting things larger than a byte if we're
1781 only doing unwinding. */
1782 gcc_unreachable ();
1783 #endif
1784 case DW_OP_const1u:
1785 case DW_OP_const1s:
1786 dw2_asm_output_data (1, val1->v.val_int, NULL);
1787 break;
1788 case DW_OP_constu:
1789 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1790 break;
1791 case DW_OP_consts:
1792 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1793 break;
1794 case DW_OP_pick:
1795 dw2_asm_output_data (1, val1->v.val_int, NULL);
1796 break;
1797 case DW_OP_plus_uconst:
1798 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1799 break;
1800 case DW_OP_breg0:
1801 case DW_OP_breg1:
1802 case DW_OP_breg2:
1803 case DW_OP_breg3:
1804 case DW_OP_breg4:
1805 case DW_OP_breg5:
1806 case DW_OP_breg6:
1807 case DW_OP_breg7:
1808 case DW_OP_breg8:
1809 case DW_OP_breg9:
1810 case DW_OP_breg10:
1811 case DW_OP_breg11:
1812 case DW_OP_breg12:
1813 case DW_OP_breg13:
1814 case DW_OP_breg14:
1815 case DW_OP_breg15:
1816 case DW_OP_breg16:
1817 case DW_OP_breg17:
1818 case DW_OP_breg18:
1819 case DW_OP_breg19:
1820 case DW_OP_breg20:
1821 case DW_OP_breg21:
1822 case DW_OP_breg22:
1823 case DW_OP_breg23:
1824 case DW_OP_breg24:
1825 case DW_OP_breg25:
1826 case DW_OP_breg26:
1827 case DW_OP_breg27:
1828 case DW_OP_breg28:
1829 case DW_OP_breg29:
1830 case DW_OP_breg30:
1831 case DW_OP_breg31:
1832 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1833 break;
1834 case DW_OP_regx:
1835 {
1836 unsigned r = val1->v.val_unsigned;
1837 if (for_eh_or_skip >= 0)
1838 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1839 gcc_assert (size_of_uleb128 (r)
1840 == size_of_uleb128 (val1->v.val_unsigned));
1841 dw2_asm_output_data_uleb128 (r, NULL);
1842 }
1843 break;
1844 case DW_OP_fbreg:
1845 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1846 break;
1847 case DW_OP_bregx:
1848 {
1849 unsigned r = val1->v.val_unsigned;
1850 if (for_eh_or_skip >= 0)
1851 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1852 gcc_assert (size_of_uleb128 (r)
1853 == size_of_uleb128 (val1->v.val_unsigned));
1854 dw2_asm_output_data_uleb128 (r, NULL);
1855 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1856 }
1857 break;
1858 case DW_OP_piece:
1859 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1860 break;
1861 case DW_OP_bit_piece:
1862 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1863 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1864 break;
1865 case DW_OP_deref_size:
1866 case DW_OP_xderef_size:
1867 dw2_asm_output_data (1, val1->v.val_int, NULL);
1868 break;
1869
1870 case DW_OP_addr:
1871 if (loc->dtprel)
1872 {
1873 if (targetm.asm_out.output_dwarf_dtprel)
1874 {
1875 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1876 DWARF2_ADDR_SIZE,
1877 val1->v.val_addr);
1878 fputc ('\n', asm_out_file);
1879 }
1880 else
1881 gcc_unreachable ();
1882 }
1883 else
1884 {
1885 #ifdef DWARF2_DEBUGGING_INFO
1886 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1887 #else
1888 gcc_unreachable ();
1889 #endif
1890 }
1891 break;
1892
1893 case DW_OP_GNU_implicit_pointer:
1894 {
1895 char label[MAX_ARTIFICIAL_LABEL_BYTES
1896 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1897 gcc_assert (val1->val_class == dw_val_class_die_ref);
1898 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1899 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1900 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1901 }
1902 break;
1903
1904 case DW_OP_GNU_entry_value:
1905 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1906 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1907 break;
1908
1909 case DW_OP_GNU_const_type:
1910 {
1911 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1912 gcc_assert (o);
1913 dw2_asm_output_data_uleb128 (o, NULL);
1914 switch (val2->val_class)
1915 {
1916 case dw_val_class_const:
1917 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1918 dw2_asm_output_data (1, l, NULL);
1919 dw2_asm_output_data (l, val2->v.val_int, NULL);
1920 break;
1921 case dw_val_class_vec:
1922 {
1923 unsigned int elt_size = val2->v.val_vec.elt_size;
1924 unsigned int len = val2->v.val_vec.length;
1925 unsigned int i;
1926 unsigned char *p;
1927
1928 l = len * elt_size;
1929 dw2_asm_output_data (1, l, NULL);
1930 if (elt_size > sizeof (HOST_WIDE_INT))
1931 {
1932 elt_size /= 2;
1933 len *= 2;
1934 }
1935 for (i = 0, p = val2->v.val_vec.array;
1936 i < len;
1937 i++, p += elt_size)
1938 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1939 "fp or vector constant word %u", i);
1940 }
1941 break;
1942 case dw_val_class_const_double:
1943 {
1944 unsigned HOST_WIDE_INT first, second;
1945 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1946
1947 dw2_asm_output_data (1, 2 * l, NULL);
1948 if (WORDS_BIG_ENDIAN)
1949 {
1950 first = val2->v.val_double.high;
1951 second = val2->v.val_double.low;
1952 }
1953 else
1954 {
1955 first = val2->v.val_double.low;
1956 second = val2->v.val_double.high;
1957 }
1958 dw2_asm_output_data (l, first, NULL);
1959 dw2_asm_output_data (l, second, NULL);
1960 }
1961 break;
1962 default:
1963 gcc_unreachable ();
1964 }
1965 }
1966 break;
1967 case DW_OP_GNU_regval_type:
1968 {
1969 unsigned r = val1->v.val_unsigned;
1970 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
1971 gcc_assert (o);
1972 if (for_eh_or_skip >= 0)
1973 {
1974 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1975 gcc_assert (size_of_uleb128 (r)
1976 == size_of_uleb128 (val1->v.val_unsigned));
1977 }
1978 dw2_asm_output_data_uleb128 (r, NULL);
1979 dw2_asm_output_data_uleb128 (o, NULL);
1980 }
1981 break;
1982 case DW_OP_GNU_deref_type:
1983 {
1984 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
1985 gcc_assert (o);
1986 dw2_asm_output_data (1, val1->v.val_int, NULL);
1987 dw2_asm_output_data_uleb128 (o, NULL);
1988 }
1989 break;
1990 case DW_OP_GNU_convert:
1991 case DW_OP_GNU_reinterpret:
1992 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1993 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1994 else
1995 {
1996 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
1997 gcc_assert (o);
1998 dw2_asm_output_data_uleb128 (o, NULL);
1999 }
2000 break;
2001
2002 case DW_OP_GNU_parameter_ref:
2003 {
2004 unsigned long o;
2005 gcc_assert (val1->val_class == dw_val_class_die_ref);
2006 o = get_ref_die_offset (val1->v.val_die_ref.die);
2007 dw2_asm_output_data (4, o, NULL);
2008 }
2009 break;
2010
2011 default:
2012 /* Other codes have no operands. */
2013 break;
2014 }
2015 }
2016
2017 /* Output a sequence of location operations.
2018 The for_eh_or_skip parameter controls whether register numbers are
2019 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2020 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2021 info). This should be suppressed for the cases that have not been converted
2022 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2023
2024 void
2025 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2026 {
2027 for (; loc != NULL; loc = loc->dw_loc_next)
2028 {
2029 enum dwarf_location_atom opc = loc->dw_loc_opc;
2030 /* Output the opcode. */
2031 if (for_eh_or_skip >= 0
2032 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2033 {
2034 unsigned r = (opc - DW_OP_breg0);
2035 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2036 gcc_assert (r <= 31);
2037 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2038 }
2039 else if (for_eh_or_skip >= 0
2040 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2041 {
2042 unsigned r = (opc - DW_OP_reg0);
2043 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2044 gcc_assert (r <= 31);
2045 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2046 }
2047
2048 dw2_asm_output_data (1, opc,
2049 "%s", dwarf_stack_op_name (opc));
2050
2051 /* Output the operand(s) (if any). */
2052 output_loc_operands (loc, for_eh_or_skip);
2053 }
2054 }
2055
2056 /* Output location description stack opcode's operands (if any).
2057 The output is single bytes on a line, suitable for .cfi_escape. */
2058
2059 static void
2060 output_loc_operands_raw (dw_loc_descr_ref loc)
2061 {
2062 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2063 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2064
2065 switch (loc->dw_loc_opc)
2066 {
2067 case DW_OP_addr:
2068 case DW_OP_implicit_value:
2069 /* We cannot output addresses in .cfi_escape, only bytes. */
2070 gcc_unreachable ();
2071
2072 case DW_OP_const1u:
2073 case DW_OP_const1s:
2074 case DW_OP_pick:
2075 case DW_OP_deref_size:
2076 case DW_OP_xderef_size:
2077 fputc (',', asm_out_file);
2078 dw2_asm_output_data_raw (1, val1->v.val_int);
2079 break;
2080
2081 case DW_OP_const2u:
2082 case DW_OP_const2s:
2083 fputc (',', asm_out_file);
2084 dw2_asm_output_data_raw (2, val1->v.val_int);
2085 break;
2086
2087 case DW_OP_const4u:
2088 case DW_OP_const4s:
2089 fputc (',', asm_out_file);
2090 dw2_asm_output_data_raw (4, val1->v.val_int);
2091 break;
2092
2093 case DW_OP_const8u:
2094 case DW_OP_const8s:
2095 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2096 fputc (',', asm_out_file);
2097 dw2_asm_output_data_raw (8, val1->v.val_int);
2098 break;
2099
2100 case DW_OP_skip:
2101 case DW_OP_bra:
2102 {
2103 int offset;
2104
2105 gcc_assert (val1->val_class == dw_val_class_loc);
2106 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2107
2108 fputc (',', asm_out_file);
2109 dw2_asm_output_data_raw (2, offset);
2110 }
2111 break;
2112
2113 case DW_OP_regx:
2114 {
2115 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2116 gcc_assert (size_of_uleb128 (r)
2117 == size_of_uleb128 (val1->v.val_unsigned));
2118 fputc (',', asm_out_file);
2119 dw2_asm_output_data_uleb128_raw (r);
2120 }
2121 break;
2122
2123 case DW_OP_constu:
2124 case DW_OP_plus_uconst:
2125 case DW_OP_piece:
2126 fputc (',', asm_out_file);
2127 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2128 break;
2129
2130 case DW_OP_bit_piece:
2131 fputc (',', asm_out_file);
2132 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2133 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2134 break;
2135
2136 case DW_OP_consts:
2137 case DW_OP_breg0:
2138 case DW_OP_breg1:
2139 case DW_OP_breg2:
2140 case DW_OP_breg3:
2141 case DW_OP_breg4:
2142 case DW_OP_breg5:
2143 case DW_OP_breg6:
2144 case DW_OP_breg7:
2145 case DW_OP_breg8:
2146 case DW_OP_breg9:
2147 case DW_OP_breg10:
2148 case DW_OP_breg11:
2149 case DW_OP_breg12:
2150 case DW_OP_breg13:
2151 case DW_OP_breg14:
2152 case DW_OP_breg15:
2153 case DW_OP_breg16:
2154 case DW_OP_breg17:
2155 case DW_OP_breg18:
2156 case DW_OP_breg19:
2157 case DW_OP_breg20:
2158 case DW_OP_breg21:
2159 case DW_OP_breg22:
2160 case DW_OP_breg23:
2161 case DW_OP_breg24:
2162 case DW_OP_breg25:
2163 case DW_OP_breg26:
2164 case DW_OP_breg27:
2165 case DW_OP_breg28:
2166 case DW_OP_breg29:
2167 case DW_OP_breg30:
2168 case DW_OP_breg31:
2169 case DW_OP_fbreg:
2170 fputc (',', asm_out_file);
2171 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2172 break;
2173
2174 case DW_OP_bregx:
2175 {
2176 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2177 gcc_assert (size_of_uleb128 (r)
2178 == size_of_uleb128 (val1->v.val_unsigned));
2179 fputc (',', asm_out_file);
2180 dw2_asm_output_data_uleb128_raw (r);
2181 fputc (',', asm_out_file);
2182 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2183 }
2184 break;
2185
2186 case DW_OP_GNU_implicit_pointer:
2187 case DW_OP_GNU_entry_value:
2188 case DW_OP_GNU_const_type:
2189 case DW_OP_GNU_regval_type:
2190 case DW_OP_GNU_deref_type:
2191 case DW_OP_GNU_convert:
2192 case DW_OP_GNU_reinterpret:
2193 case DW_OP_GNU_parameter_ref:
2194 gcc_unreachable ();
2195 break;
2196
2197 default:
2198 /* Other codes have no operands. */
2199 break;
2200 }
2201 }
2202
2203 void
2204 output_loc_sequence_raw (dw_loc_descr_ref loc)
2205 {
2206 while (1)
2207 {
2208 enum dwarf_location_atom opc = loc->dw_loc_opc;
2209 /* Output the opcode. */
2210 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2211 {
2212 unsigned r = (opc - DW_OP_breg0);
2213 r = DWARF2_FRAME_REG_OUT (r, 1);
2214 gcc_assert (r <= 31);
2215 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2216 }
2217 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2218 {
2219 unsigned r = (opc - DW_OP_reg0);
2220 r = DWARF2_FRAME_REG_OUT (r, 1);
2221 gcc_assert (r <= 31);
2222 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2223 }
2224 /* Output the opcode. */
2225 fprintf (asm_out_file, "%#x", opc);
2226 output_loc_operands_raw (loc);
2227
2228 if (!loc->dw_loc_next)
2229 break;
2230 loc = loc->dw_loc_next;
2231
2232 fputc (',', asm_out_file);
2233 }
2234 }
2235
2236 /* This function builds a dwarf location descriptor sequence from a
2237 dw_cfa_location, adding the given OFFSET to the result of the
2238 expression. */
2239
2240 struct dw_loc_descr_struct *
2241 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2242 {
2243 struct dw_loc_descr_struct *head, *tmp;
2244
2245 offset += cfa->offset;
2246
2247 if (cfa->indirect)
2248 {
2249 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2250 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2251 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2252 add_loc_descr (&head, tmp);
2253 if (offset != 0)
2254 {
2255 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2256 add_loc_descr (&head, tmp);
2257 }
2258 }
2259 else
2260 head = new_reg_loc_descr (cfa->reg, offset);
2261
2262 return head;
2263 }
2264
2265 /* This function builds a dwarf location descriptor sequence for
2266 the address at OFFSET from the CFA when stack is aligned to
2267 ALIGNMENT byte. */
2268
2269 struct dw_loc_descr_struct *
2270 build_cfa_aligned_loc (dw_cfa_location *cfa,
2271 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2272 {
2273 struct dw_loc_descr_struct *head;
2274 unsigned int dwarf_fp
2275 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2276
2277 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2278 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2279 {
2280 head = new_reg_loc_descr (dwarf_fp, 0);
2281 add_loc_descr (&head, int_loc_descriptor (alignment));
2282 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2283 loc_descr_plus_const (&head, offset);
2284 }
2285 else
2286 head = new_reg_loc_descr (dwarf_fp, offset);
2287 return head;
2288 }
2289 \f
2290 /* And now, the support for symbolic debugging information. */
2291
2292 /* .debug_str support. */
2293 static int output_indirect_string (void **, void *);
2294
2295 static void dwarf2out_init (const char *);
2296 static void dwarf2out_finish (const char *);
2297 static void dwarf2out_assembly_start (void);
2298 static void dwarf2out_define (unsigned int, const char *);
2299 static void dwarf2out_undef (unsigned int, const char *);
2300 static void dwarf2out_start_source_file (unsigned, const char *);
2301 static void dwarf2out_end_source_file (unsigned);
2302 static void dwarf2out_function_decl (tree);
2303 static void dwarf2out_begin_block (unsigned, unsigned);
2304 static void dwarf2out_end_block (unsigned, unsigned);
2305 static bool dwarf2out_ignore_block (const_tree);
2306 static void dwarf2out_global_decl (tree);
2307 static void dwarf2out_type_decl (tree, int);
2308 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2309 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2310 dw_die_ref);
2311 static void dwarf2out_abstract_function (tree);
2312 static void dwarf2out_var_location (rtx);
2313 static void dwarf2out_begin_function (tree);
2314 static void dwarf2out_set_name (tree, tree);
2315
2316 /* The debug hooks structure. */
2317
2318 const struct gcc_debug_hooks dwarf2_debug_hooks =
2319 {
2320 dwarf2out_init,
2321 dwarf2out_finish,
2322 dwarf2out_assembly_start,
2323 dwarf2out_define,
2324 dwarf2out_undef,
2325 dwarf2out_start_source_file,
2326 dwarf2out_end_source_file,
2327 dwarf2out_begin_block,
2328 dwarf2out_end_block,
2329 dwarf2out_ignore_block,
2330 dwarf2out_source_line,
2331 dwarf2out_begin_prologue,
2332 #if VMS_DEBUGGING_INFO
2333 dwarf2out_vms_end_prologue,
2334 dwarf2out_vms_begin_epilogue,
2335 #else
2336 debug_nothing_int_charstar,
2337 debug_nothing_int_charstar,
2338 #endif
2339 dwarf2out_end_epilogue,
2340 dwarf2out_begin_function,
2341 debug_nothing_int, /* end_function */
2342 dwarf2out_function_decl, /* function_decl */
2343 dwarf2out_global_decl,
2344 dwarf2out_type_decl, /* type_decl */
2345 dwarf2out_imported_module_or_decl,
2346 debug_nothing_tree, /* deferred_inline_function */
2347 /* The DWARF 2 backend tries to reduce debugging bloat by not
2348 emitting the abstract description of inline functions until
2349 something tries to reference them. */
2350 dwarf2out_abstract_function, /* outlining_inline_function */
2351 debug_nothing_rtx, /* label */
2352 debug_nothing_int, /* handle_pch */
2353 dwarf2out_var_location,
2354 dwarf2out_switch_text_section,
2355 dwarf2out_set_name,
2356 1, /* start_end_main_source_file */
2357 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2358 };
2359 \f
2360 /* NOTE: In the comments in this file, many references are made to
2361 "Debugging Information Entries". This term is abbreviated as `DIE'
2362 throughout the remainder of this file. */
2363
2364 /* An internal representation of the DWARF output is built, and then
2365 walked to generate the DWARF debugging info. The walk of the internal
2366 representation is done after the entire program has been compiled.
2367 The types below are used to describe the internal representation. */
2368
2369 /* Whether to put type DIEs into their own section .debug_types instead
2370 of making them part of the .debug_info section. Only supported for
2371 Dwarf V4 or higher and the user didn't disable them through
2372 -fno-debug-types-section. It is more efficient to put them in a
2373 separate comdat sections since the linker will then be able to
2374 remove duplicates. But not all tools support .debug_types sections
2375 yet. */
2376
2377 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2378
2379 /* Various DIE's use offsets relative to the beginning of the
2380 .debug_info section to refer to each other. */
2381
2382 typedef long int dw_offset;
2383
2384 /* Define typedefs here to avoid circular dependencies. */
2385
2386 typedef struct dw_attr_struct *dw_attr_ref;
2387 typedef struct dw_line_info_struct *dw_line_info_ref;
2388 typedef struct pubname_struct *pubname_ref;
2389 typedef struct dw_ranges_struct *dw_ranges_ref;
2390 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2391 typedef struct comdat_type_struct *comdat_type_node_ref;
2392
2393 /* The entries in the line_info table more-or-less mirror the opcodes
2394 that are used in the real dwarf line table. Arrays of these entries
2395 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2396 supported. */
2397
2398 enum dw_line_info_opcode {
2399 /* Emit DW_LNE_set_address; the operand is the label index. */
2400 LI_set_address,
2401
2402 /* Emit a row to the matrix with the given line. This may be done
2403 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2404 special opcodes. */
2405 LI_set_line,
2406
2407 /* Emit a DW_LNS_set_file. */
2408 LI_set_file,
2409
2410 /* Emit a DW_LNS_set_column. */
2411 LI_set_column,
2412
2413 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2414 LI_negate_stmt,
2415
2416 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2417 LI_set_prologue_end,
2418 LI_set_epilogue_begin,
2419
2420 /* Emit a DW_LNE_set_discriminator. */
2421 LI_set_discriminator
2422 };
2423
2424 typedef struct GTY(()) dw_line_info_struct {
2425 enum dw_line_info_opcode opcode;
2426 unsigned int val;
2427 } dw_line_info_entry;
2428
2429 DEF_VEC_O(dw_line_info_entry);
2430 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
2431
2432 typedef struct GTY(()) dw_line_info_table_struct {
2433 /* The label that marks the end of this section. */
2434 const char *end_label;
2435
2436 /* The values for the last row of the matrix, as collected in the table.
2437 These are used to minimize the changes to the next row. */
2438 unsigned int file_num;
2439 unsigned int line_num;
2440 unsigned int column_num;
2441 int discrim_num;
2442 bool is_stmt;
2443 bool in_use;
2444
2445 VEC(dw_line_info_entry, gc) *entries;
2446 } dw_line_info_table;
2447
2448 typedef dw_line_info_table *dw_line_info_table_p;
2449
2450 DEF_VEC_P(dw_line_info_table_p);
2451 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
2452
2453 /* Each DIE attribute has a field specifying the attribute kind,
2454 a link to the next attribute in the chain, and an attribute value.
2455 Attributes are typically linked below the DIE they modify. */
2456
2457 typedef struct GTY(()) dw_attr_struct {
2458 enum dwarf_attribute dw_attr;
2459 dw_val_node dw_attr_val;
2460 }
2461 dw_attr_node;
2462
2463 DEF_VEC_O(dw_attr_node);
2464 DEF_VEC_ALLOC_O(dw_attr_node,gc);
2465
2466 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2467 The children of each node form a circular list linked by
2468 die_sib. die_child points to the node *before* the "first" child node. */
2469
2470 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2471 union die_symbol_or_type_node
2472 {
2473 const char * GTY ((tag ("0"))) die_symbol;
2474 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2475 }
2476 GTY ((desc ("%0.comdat_type_p"))) die_id;
2477 VEC(dw_attr_node,gc) * die_attr;
2478 dw_die_ref die_parent;
2479 dw_die_ref die_child;
2480 dw_die_ref die_sib;
2481 dw_die_ref die_definition; /* ref from a specification to its definition */
2482 dw_offset die_offset;
2483 unsigned long die_abbrev;
2484 int die_mark;
2485 unsigned int decl_id;
2486 enum dwarf_tag die_tag;
2487 /* Die is used and must not be pruned as unused. */
2488 BOOL_BITFIELD die_perennial_p : 1;
2489 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2490 /* Lots of spare bits. */
2491 }
2492 die_node;
2493
2494 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2495 #define FOR_EACH_CHILD(die, c, expr) do { \
2496 c = die->die_child; \
2497 if (c) do { \
2498 c = c->die_sib; \
2499 expr; \
2500 } while (c != die->die_child); \
2501 } while (0)
2502
2503 /* The pubname structure */
2504
2505 typedef struct GTY(()) pubname_struct {
2506 dw_die_ref die;
2507 const char *name;
2508 }
2509 pubname_entry;
2510
2511 DEF_VEC_O(pubname_entry);
2512 DEF_VEC_ALLOC_O(pubname_entry, gc);
2513
2514 struct GTY(()) dw_ranges_struct {
2515 /* If this is positive, it's a block number, otherwise it's a
2516 bitwise-negated index into dw_ranges_by_label. */
2517 int num;
2518 };
2519
2520 /* A structure to hold a macinfo entry. */
2521
2522 typedef struct GTY(()) macinfo_struct {
2523 unsigned char code;
2524 unsigned HOST_WIDE_INT lineno;
2525 const char *info;
2526 }
2527 macinfo_entry;
2528
2529 DEF_VEC_O(macinfo_entry);
2530 DEF_VEC_ALLOC_O(macinfo_entry, gc);
2531
2532 struct GTY(()) dw_ranges_by_label_struct {
2533 const char *begin;
2534 const char *end;
2535 };
2536
2537 /* The comdat type node structure. */
2538 typedef struct GTY(()) comdat_type_struct
2539 {
2540 dw_die_ref root_die;
2541 dw_die_ref type_die;
2542 dw_die_ref skeleton_die;
2543 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2544 struct comdat_type_struct *next;
2545 }
2546 comdat_type_node;
2547
2548 /* The limbo die list structure. */
2549 typedef struct GTY(()) limbo_die_struct {
2550 dw_die_ref die;
2551 tree created_for;
2552 struct limbo_die_struct *next;
2553 }
2554 limbo_die_node;
2555
2556 typedef struct skeleton_chain_struct
2557 {
2558 dw_die_ref old_die;
2559 dw_die_ref new_die;
2560 struct skeleton_chain_struct *parent;
2561 }
2562 skeleton_chain_node;
2563
2564 /* Define a macro which returns nonzero for a TYPE_DECL which was
2565 implicitly generated for a type.
2566
2567 Note that, unlike the C front-end (which generates a NULL named
2568 TYPE_DECL node for each complete tagged type, each array type,
2569 and each function type node created) the C++ front-end generates
2570 a _named_ TYPE_DECL node for each tagged type node created.
2571 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2572 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2573 front-end, but for each type, tagged or not. */
2574
2575 #define TYPE_DECL_IS_STUB(decl) \
2576 (DECL_NAME (decl) == NULL_TREE \
2577 || (DECL_ARTIFICIAL (decl) \
2578 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2579 /* This is necessary for stub decls that \
2580 appear in nested inline functions. */ \
2581 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2582 && (decl_ultimate_origin (decl) \
2583 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2584
2585 /* Information concerning the compilation unit's programming
2586 language, and compiler version. */
2587
2588 /* Fixed size portion of the DWARF compilation unit header. */
2589 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2590 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2591
2592 /* Fixed size portion of the DWARF comdat type unit header. */
2593 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2594 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2595 + DWARF_OFFSET_SIZE)
2596
2597 /* Fixed size portion of public names info. */
2598 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2599
2600 /* Fixed size portion of the address range info. */
2601 #define DWARF_ARANGES_HEADER_SIZE \
2602 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2603 DWARF2_ADDR_SIZE * 2) \
2604 - DWARF_INITIAL_LENGTH_SIZE)
2605
2606 /* Size of padding portion in the address range info. It must be
2607 aligned to twice the pointer size. */
2608 #define DWARF_ARANGES_PAD_SIZE \
2609 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2610 DWARF2_ADDR_SIZE * 2) \
2611 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2612
2613 /* Use assembler line directives if available. */
2614 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2615 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2616 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2617 #else
2618 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2619 #endif
2620 #endif
2621
2622 /* Minimum line offset in a special line info. opcode.
2623 This value was chosen to give a reasonable range of values. */
2624 #define DWARF_LINE_BASE -10
2625
2626 /* First special line opcode - leave room for the standard opcodes. */
2627 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2628
2629 /* Range of line offsets in a special line info. opcode. */
2630 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2631
2632 /* Flag that indicates the initial value of the is_stmt_start flag.
2633 In the present implementation, we do not mark any lines as
2634 the beginning of a source statement, because that information
2635 is not made available by the GCC front-end. */
2636 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2637
2638 /* Maximum number of operations per instruction bundle. */
2639 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2640 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2641 #endif
2642
2643 /* This location is used by calc_die_sizes() to keep track
2644 the offset of each DIE within the .debug_info section. */
2645 static unsigned long next_die_offset;
2646
2647 /* Record the root of the DIE's built for the current compilation unit. */
2648 static GTY(()) dw_die_ref single_comp_unit_die;
2649
2650 /* A list of type DIEs that have been separated into comdat sections. */
2651 static GTY(()) comdat_type_node *comdat_type_list;
2652
2653 /* A list of DIEs with a NULL parent waiting to be relocated. */
2654 static GTY(()) limbo_die_node *limbo_die_list;
2655
2656 /* A list of DIEs for which we may have to generate
2657 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2658 static GTY(()) limbo_die_node *deferred_asm_name;
2659
2660 /* Filenames referenced by this compilation unit. */
2661 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2662
2663 /* A hash table of references to DIE's that describe declarations.
2664 The key is a DECL_UID() which is a unique number identifying each decl. */
2665 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2666
2667 /* A hash table of references to DIE's that describe COMMON blocks.
2668 The key is DECL_UID() ^ die_parent. */
2669 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2670
2671 typedef struct GTY(()) die_arg_entry_struct {
2672 dw_die_ref die;
2673 tree arg;
2674 } die_arg_entry;
2675
2676 DEF_VEC_O(die_arg_entry);
2677 DEF_VEC_ALLOC_O(die_arg_entry,gc);
2678
2679 /* Node of the variable location list. */
2680 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2681 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2682 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2683 in mode of the EXPR_LIST node and first EXPR_LIST operand
2684 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2685 location or NULL for padding. For larger bitsizes,
2686 mode is 0 and first operand is a CONCAT with bitsize
2687 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2688 NULL as second operand. */
2689 rtx GTY (()) loc;
2690 const char * GTY (()) label;
2691 struct var_loc_node * GTY (()) next;
2692 };
2693
2694 /* Variable location list. */
2695 struct GTY (()) var_loc_list_def {
2696 struct var_loc_node * GTY (()) first;
2697
2698 /* Pointer to the last but one or last element of the
2699 chained list. If the list is empty, both first and
2700 last are NULL, if the list contains just one node
2701 or the last node certainly is not redundant, it points
2702 to the last node, otherwise points to the last but one.
2703 Do not mark it for GC because it is marked through the chain. */
2704 struct var_loc_node * GTY ((skip ("%h"))) last;
2705
2706 /* Pointer to the last element before section switch,
2707 if NULL, either sections weren't switched or first
2708 is after section switch. */
2709 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2710
2711 /* DECL_UID of the variable decl. */
2712 unsigned int decl_id;
2713 };
2714 typedef struct var_loc_list_def var_loc_list;
2715
2716 /* Call argument location list. */
2717 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2718 rtx GTY (()) call_arg_loc_note;
2719 const char * GTY (()) label;
2720 tree GTY (()) block;
2721 bool tail_call_p;
2722 rtx GTY (()) symbol_ref;
2723 struct call_arg_loc_node * GTY (()) next;
2724 };
2725
2726
2727 /* Table of decl location linked lists. */
2728 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2729
2730 /* Head and tail of call_arg_loc chain. */
2731 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2732 static struct call_arg_loc_node *call_arg_loc_last;
2733
2734 /* Number of call sites in the current function. */
2735 static int call_site_count = -1;
2736 /* Number of tail call sites in the current function. */
2737 static int tail_call_site_count = -1;
2738
2739 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2740 DIEs. */
2741 static VEC (dw_die_ref, heap) *block_map;
2742
2743 /* A cached location list. */
2744 struct GTY (()) cached_dw_loc_list_def {
2745 /* The DECL_UID of the decl that this entry describes. */
2746 unsigned int decl_id;
2747
2748 /* The cached location list. */
2749 dw_loc_list_ref loc_list;
2750 };
2751 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2752
2753 /* Table of cached location lists. */
2754 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2755
2756 /* A pointer to the base of a list of references to DIE's that
2757 are uniquely identified by their tag, presence/absence of
2758 children DIE's, and list of attribute/value pairs. */
2759 static GTY((length ("abbrev_die_table_allocated")))
2760 dw_die_ref *abbrev_die_table;
2761
2762 /* Number of elements currently allocated for abbrev_die_table. */
2763 static GTY(()) unsigned abbrev_die_table_allocated;
2764
2765 /* Number of elements in type_die_table currently in use. */
2766 static GTY(()) unsigned abbrev_die_table_in_use;
2767
2768 /* Size (in elements) of increments by which we may expand the
2769 abbrev_die_table. */
2770 #define ABBREV_DIE_TABLE_INCREMENT 256
2771
2772 /* A global counter for generating labels for line number data. */
2773 static unsigned int line_info_label_num;
2774
2775 /* The current table to which we should emit line number information
2776 for the current function. This will be set up at the beginning of
2777 assembly for the function. */
2778 static dw_line_info_table *cur_line_info_table;
2779
2780 /* The two default tables of line number info. */
2781 static GTY(()) dw_line_info_table *text_section_line_info;
2782 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2783
2784 /* The set of all non-default tables of line number info. */
2785 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
2786
2787 /* A flag to tell pubnames/types export if there is an info section to
2788 refer to. */
2789 static bool info_section_emitted;
2790
2791 /* A pointer to the base of a table that contains a list of publicly
2792 accessible names. */
2793 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
2794
2795 /* A pointer to the base of a table that contains a list of publicly
2796 accessible types. */
2797 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
2798
2799 /* A pointer to the base of a table that contains a list of macro
2800 defines/undefines (and file start/end markers). */
2801 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
2802
2803 /* True if .debug_macinfo or .debug_macros section is going to be
2804 emitted. */
2805 #define have_macinfo \
2806 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2807 && !VEC_empty (macinfo_entry, macinfo_table))
2808
2809 /* Array of dies for which we should generate .debug_ranges info. */
2810 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2811
2812 /* Number of elements currently allocated for ranges_table. */
2813 static GTY(()) unsigned ranges_table_allocated;
2814
2815 /* Number of elements in ranges_table currently in use. */
2816 static GTY(()) unsigned ranges_table_in_use;
2817
2818 /* Array of pairs of labels referenced in ranges_table. */
2819 static GTY ((length ("ranges_by_label_allocated")))
2820 dw_ranges_by_label_ref ranges_by_label;
2821
2822 /* Number of elements currently allocated for ranges_by_label. */
2823 static GTY(()) unsigned ranges_by_label_allocated;
2824
2825 /* Number of elements in ranges_by_label currently in use. */
2826 static GTY(()) unsigned ranges_by_label_in_use;
2827
2828 /* Size (in elements) of increments by which we may expand the
2829 ranges_table. */
2830 #define RANGES_TABLE_INCREMENT 64
2831
2832 /* Whether we have location lists that need outputting */
2833 static GTY(()) bool have_location_lists;
2834
2835 /* Unique label counter. */
2836 static GTY(()) unsigned int loclabel_num;
2837
2838 /* Unique label counter for point-of-call tables. */
2839 static GTY(()) unsigned int poc_label_num;
2840
2841 /* Record whether the function being analyzed contains inlined functions. */
2842 static int current_function_has_inlines;
2843
2844 /* The last file entry emitted by maybe_emit_file(). */
2845 static GTY(()) struct dwarf_file_data * last_emitted_file;
2846
2847 /* Number of internal labels generated by gen_internal_sym(). */
2848 static GTY(()) int label_num;
2849
2850 /* Cached result of previous call to lookup_filename. */
2851 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2852
2853 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
2854
2855 /* Instances of generic types for which we need to generate debug
2856 info that describe their generic parameters and arguments. That
2857 generation needs to happen once all types are properly laid out so
2858 we do it at the end of compilation. */
2859 static GTY(()) VEC(tree,gc) *generic_type_instances;
2860
2861 /* Offset from the "steady-state frame pointer" to the frame base,
2862 within the current function. */
2863 static HOST_WIDE_INT frame_pointer_fb_offset;
2864 static bool frame_pointer_fb_offset_valid;
2865
2866 static VEC (dw_die_ref, heap) *base_types;
2867
2868 /* Forward declarations for functions defined in this file. */
2869
2870 static int is_pseudo_reg (const_rtx);
2871 static tree type_main_variant (tree);
2872 static int is_tagged_type (const_tree);
2873 static const char *dwarf_tag_name (unsigned);
2874 static const char *dwarf_attr_name (unsigned);
2875 static const char *dwarf_form_name (unsigned);
2876 static tree decl_ultimate_origin (const_tree);
2877 static tree decl_class_context (tree);
2878 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2879 static inline enum dw_val_class AT_class (dw_attr_ref);
2880 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2881 static inline unsigned AT_flag (dw_attr_ref);
2882 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2883 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2884 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2885 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2886 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2887 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2888 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2889 unsigned int, unsigned char *);
2890 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2891 static hashval_t debug_str_do_hash (const void *);
2892 static int debug_str_eq (const void *, const void *);
2893 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2894 static inline const char *AT_string (dw_attr_ref);
2895 static enum dwarf_form AT_string_form (dw_attr_ref);
2896 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2897 static void add_AT_specification (dw_die_ref, dw_die_ref);
2898 static inline dw_die_ref AT_ref (dw_attr_ref);
2899 static inline int AT_ref_external (dw_attr_ref);
2900 static inline void set_AT_ref_external (dw_attr_ref, int);
2901 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2902 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2903 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2904 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2905 dw_loc_list_ref);
2906 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2907 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
2908 static inline rtx AT_addr (dw_attr_ref);
2909 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2910 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2911 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2912 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2913 unsigned HOST_WIDE_INT);
2914 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2915 unsigned long);
2916 static inline const char *AT_lbl (dw_attr_ref);
2917 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2918 static const char *get_AT_low_pc (dw_die_ref);
2919 static const char *get_AT_hi_pc (dw_die_ref);
2920 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2921 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2922 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2923 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2924 static bool is_cxx (void);
2925 static bool is_fortran (void);
2926 static bool is_ada (void);
2927 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2928 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2929 static void add_child_die (dw_die_ref, dw_die_ref);
2930 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2931 static dw_die_ref lookup_type_die (tree);
2932 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2933 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2934 static void equate_type_number_to_die (tree, dw_die_ref);
2935 static hashval_t decl_die_table_hash (const void *);
2936 static int decl_die_table_eq (const void *, const void *);
2937 static dw_die_ref lookup_decl_die (tree);
2938 static hashval_t common_block_die_table_hash (const void *);
2939 static int common_block_die_table_eq (const void *, const void *);
2940 static hashval_t decl_loc_table_hash (const void *);
2941 static int decl_loc_table_eq (const void *, const void *);
2942 static var_loc_list *lookup_decl_loc (const_tree);
2943 static void equate_decl_number_to_die (tree, dw_die_ref);
2944 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
2945 static void print_spaces (FILE *);
2946 static void print_die (dw_die_ref, FILE *);
2947 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
2948 static dw_die_ref pop_compile_unit (dw_die_ref);
2949 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
2950 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
2951 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
2952 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
2953 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
2954 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
2955 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
2956 struct md5_ctx *, int *);
2957 struct checksum_attributes;
2958 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
2959 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
2960 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
2961 static void generate_type_signature (dw_die_ref, comdat_type_node *);
2962 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
2963 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
2964 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
2965 static int same_die_p (dw_die_ref, dw_die_ref, int *);
2966 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
2967 static void compute_section_prefix (dw_die_ref);
2968 static int is_type_die (dw_die_ref);
2969 static int is_comdat_die (dw_die_ref);
2970 static int is_symbol_die (dw_die_ref);
2971 static void assign_symbol_names (dw_die_ref);
2972 static void break_out_includes (dw_die_ref);
2973 static int is_declaration_die (dw_die_ref);
2974 static int should_move_die_to_comdat (dw_die_ref);
2975 static dw_die_ref clone_as_declaration (dw_die_ref);
2976 static dw_die_ref clone_die (dw_die_ref);
2977 static dw_die_ref clone_tree (dw_die_ref);
2978 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
2979 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
2980 static void generate_skeleton_bottom_up (skeleton_chain_node *);
2981 static dw_die_ref generate_skeleton (dw_die_ref);
2982 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
2983 dw_die_ref,
2984 dw_die_ref);
2985 static void break_out_comdat_types (dw_die_ref);
2986 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
2987 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
2988 static void copy_decls_for_unworthy_types (dw_die_ref);
2989
2990 static hashval_t htab_cu_hash (const void *);
2991 static int htab_cu_eq (const void *, const void *);
2992 static void htab_cu_del (void *);
2993 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
2994 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
2995 static void add_sibling_attributes (dw_die_ref);
2996 static void build_abbrev_table (dw_die_ref, htab_t);
2997 static void output_location_lists (dw_die_ref);
2998 static int constant_size (unsigned HOST_WIDE_INT);
2999 static unsigned long size_of_die (dw_die_ref);
3000 static void calc_die_sizes (dw_die_ref);
3001 static void calc_base_type_die_sizes (void);
3002 static void mark_dies (dw_die_ref);
3003 static void unmark_dies (dw_die_ref);
3004 static void unmark_all_dies (dw_die_ref);
3005 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
3006 static unsigned long size_of_aranges (void);
3007 static enum dwarf_form value_format (dw_attr_ref);
3008 static void output_value_format (dw_attr_ref);
3009 static void output_abbrev_section (void);
3010 static void output_die_symbol (dw_die_ref);
3011 static void output_die (dw_die_ref);
3012 static void output_compilation_unit_header (void);
3013 static void output_comp_unit (dw_die_ref, int);
3014 static void output_comdat_type_unit (comdat_type_node *);
3015 static const char *dwarf2_name (tree, int);
3016 static void add_pubname (tree, dw_die_ref);
3017 static void add_enumerator_pubname (const char *, dw_die_ref);
3018 static void add_pubname_string (const char *, dw_die_ref);
3019 static void add_pubtype (tree, dw_die_ref);
3020 static void output_pubnames (VEC (pubname_entry,gc) *);
3021 static void output_aranges (unsigned long);
3022 static unsigned int add_ranges_num (int);
3023 static unsigned int add_ranges (const_tree);
3024 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3025 bool *);
3026 static void output_ranges (void);
3027 static dw_line_info_table *new_line_info_table (void);
3028 static void output_line_info (void);
3029 static void output_file_names (void);
3030 static dw_die_ref base_type_die (tree);
3031 static int is_base_type (tree);
3032 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3033 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3034 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3035 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3036 static int type_is_enum (const_tree);
3037 static unsigned int dbx_reg_number (const_rtx);
3038 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3039 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3040 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3041 enum var_init_status);
3042 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3043 enum var_init_status);
3044 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3045 enum var_init_status);
3046 static int is_based_loc (const_rtx);
3047 static int resolve_one_addr (rtx *, void *);
3048 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3049 enum var_init_status);
3050 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3051 enum var_init_status);
3052 static dw_loc_list_ref loc_list_from_tree (tree, int);
3053 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3054 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3055 static tree field_type (const_tree);
3056 static unsigned int simple_type_align_in_bits (const_tree);
3057 static unsigned int simple_decl_align_in_bits (const_tree);
3058 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3059 static HOST_WIDE_INT field_byte_offset (const_tree);
3060 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3061 dw_loc_list_ref);
3062 static void add_data_member_location_attribute (dw_die_ref, tree);
3063 static bool add_const_value_attribute (dw_die_ref, rtx);
3064 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3065 static void insert_double (double_int, unsigned char *);
3066 static void insert_float (const_rtx, unsigned char *);
3067 static rtx rtl_for_decl_location (tree);
3068 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3069 enum dwarf_attribute);
3070 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3071 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3072 static void add_name_attribute (dw_die_ref, const char *);
3073 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3074 static void add_comp_dir_attribute (dw_die_ref);
3075 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3076 static void add_subscript_info (dw_die_ref, tree, bool);
3077 static void add_byte_size_attribute (dw_die_ref, tree);
3078 static void add_bit_offset_attribute (dw_die_ref, tree);
3079 static void add_bit_size_attribute (dw_die_ref, tree);
3080 static void add_prototyped_attribute (dw_die_ref, tree);
3081 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3082 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3083 static void add_src_coords_attributes (dw_die_ref, tree);
3084 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3085 static void push_decl_scope (tree);
3086 static void pop_decl_scope (void);
3087 static dw_die_ref scope_die_for (tree, dw_die_ref);
3088 static inline int local_scope_p (dw_die_ref);
3089 static inline int class_scope_p (dw_die_ref);
3090 static inline int class_or_namespace_scope_p (dw_die_ref);
3091 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3092 static void add_calling_convention_attribute (dw_die_ref, tree);
3093 static const char *type_tag (const_tree);
3094 static tree member_declared_type (const_tree);
3095 #if 0
3096 static const char *decl_start_label (tree);
3097 #endif
3098 static void gen_array_type_die (tree, dw_die_ref);
3099 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3100 #if 0
3101 static void gen_entry_point_die (tree, dw_die_ref);
3102 #endif
3103 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3104 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3105 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3106 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3107 static void gen_formal_types_die (tree, dw_die_ref);
3108 static void gen_subprogram_die (tree, dw_die_ref);
3109 static void gen_variable_die (tree, tree, dw_die_ref);
3110 static void gen_const_die (tree, dw_die_ref);
3111 static void gen_label_die (tree, dw_die_ref);
3112 static void gen_lexical_block_die (tree, dw_die_ref, int);
3113 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3114 static void gen_field_die (tree, dw_die_ref);
3115 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3116 static dw_die_ref gen_compile_unit_die (const char *);
3117 static void gen_inheritance_die (tree, tree, dw_die_ref);
3118 static void gen_member_die (tree, dw_die_ref);
3119 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3120 enum debug_info_usage);
3121 static void gen_subroutine_type_die (tree, dw_die_ref);
3122 static void gen_typedef_die (tree, dw_die_ref);
3123 static void gen_type_die (tree, dw_die_ref);
3124 static void gen_block_die (tree, dw_die_ref, int);
3125 static void decls_for_scope (tree, dw_die_ref, int);
3126 static inline int is_redundant_typedef (const_tree);
3127 static bool is_naming_typedef_decl (const_tree);
3128 static inline dw_die_ref get_context_die (tree);
3129 static void gen_namespace_die (tree, dw_die_ref);
3130 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3131 static dw_die_ref force_decl_die (tree);
3132 static dw_die_ref force_type_die (tree);
3133 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3134 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3135 static struct dwarf_file_data * lookup_filename (const char *);
3136 static void retry_incomplete_types (void);
3137 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3138 static void gen_generic_params_dies (tree);
3139 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3140 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3141 static void splice_child_die (dw_die_ref, dw_die_ref);
3142 static int file_info_cmp (const void *, const void *);
3143 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3144 const char *, const char *);
3145 static void output_loc_list (dw_loc_list_ref);
3146 static char *gen_internal_sym (const char *);
3147 static bool want_pubnames (void);
3148
3149 static void prune_unmark_dies (dw_die_ref);
3150 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3151 static void prune_unused_types_mark (dw_die_ref, int);
3152 static void prune_unused_types_walk (dw_die_ref);
3153 static void prune_unused_types_walk_attribs (dw_die_ref);
3154 static void prune_unused_types_prune (dw_die_ref);
3155 static void prune_unused_types (void);
3156 static int maybe_emit_file (struct dwarf_file_data *fd);
3157 static inline const char *AT_vms_delta1 (dw_attr_ref);
3158 static inline const char *AT_vms_delta2 (dw_attr_ref);
3159 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3160 const char *, const char *);
3161 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3162 static void gen_remaining_tmpl_value_param_die_attribute (void);
3163 static bool generic_type_p (tree);
3164 static void schedule_generic_params_dies_gen (tree t);
3165 static void gen_scheduled_generic_parms_dies (void);
3166
3167 /* Section names used to hold DWARF debugging information. */
3168 #ifndef DEBUG_INFO_SECTION
3169 #define DEBUG_INFO_SECTION ".debug_info"
3170 #endif
3171 #ifndef DEBUG_ABBREV_SECTION
3172 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3173 #endif
3174 #ifndef DEBUG_ARANGES_SECTION
3175 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3176 #endif
3177 #ifndef DEBUG_MACINFO_SECTION
3178 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3179 #endif
3180 #ifndef DEBUG_MACRO_SECTION
3181 #define DEBUG_MACRO_SECTION ".debug_macro"
3182 #endif
3183 #ifndef DEBUG_LINE_SECTION
3184 #define DEBUG_LINE_SECTION ".debug_line"
3185 #endif
3186 #ifndef DEBUG_LOC_SECTION
3187 #define DEBUG_LOC_SECTION ".debug_loc"
3188 #endif
3189 #ifndef DEBUG_PUBNAMES_SECTION
3190 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3191 #endif
3192 #ifndef DEBUG_PUBTYPES_SECTION
3193 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
3194 #endif
3195 #ifndef DEBUG_STR_SECTION
3196 #define DEBUG_STR_SECTION ".debug_str"
3197 #endif
3198 #ifndef DEBUG_RANGES_SECTION
3199 #define DEBUG_RANGES_SECTION ".debug_ranges"
3200 #endif
3201
3202 /* Standard ELF section names for compiled code and data. */
3203 #ifndef TEXT_SECTION_NAME
3204 #define TEXT_SECTION_NAME ".text"
3205 #endif
3206
3207 /* Section flags for .debug_str section. */
3208 #define DEBUG_STR_SECTION_FLAGS \
3209 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3210 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3211 : SECTION_DEBUG)
3212
3213 /* Labels we insert at beginning sections we can reference instead of
3214 the section names themselves. */
3215
3216 #ifndef TEXT_SECTION_LABEL
3217 #define TEXT_SECTION_LABEL "Ltext"
3218 #endif
3219 #ifndef COLD_TEXT_SECTION_LABEL
3220 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3221 #endif
3222 #ifndef DEBUG_LINE_SECTION_LABEL
3223 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3224 #endif
3225 #ifndef DEBUG_INFO_SECTION_LABEL
3226 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3227 #endif
3228 #ifndef DEBUG_ABBREV_SECTION_LABEL
3229 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3230 #endif
3231 #ifndef DEBUG_LOC_SECTION_LABEL
3232 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3233 #endif
3234 #ifndef DEBUG_RANGES_SECTION_LABEL
3235 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3236 #endif
3237 #ifndef DEBUG_MACINFO_SECTION_LABEL
3238 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3239 #endif
3240 #ifndef DEBUG_MACRO_SECTION_LABEL
3241 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3242 #endif
3243
3244
3245 /* Definitions of defaults for formats and names of various special
3246 (artificial) labels which may be generated within this file (when the -g
3247 options is used and DWARF2_DEBUGGING_INFO is in effect.
3248 If necessary, these may be overridden from within the tm.h file, but
3249 typically, overriding these defaults is unnecessary. */
3250
3251 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3252 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3253 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3254 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3255 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3256 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3257 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3258 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3259 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3260 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3261
3262 #ifndef TEXT_END_LABEL
3263 #define TEXT_END_LABEL "Letext"
3264 #endif
3265 #ifndef COLD_END_LABEL
3266 #define COLD_END_LABEL "Letext_cold"
3267 #endif
3268 #ifndef BLOCK_BEGIN_LABEL
3269 #define BLOCK_BEGIN_LABEL "LBB"
3270 #endif
3271 #ifndef BLOCK_END_LABEL
3272 #define BLOCK_END_LABEL "LBE"
3273 #endif
3274 #ifndef LINE_CODE_LABEL
3275 #define LINE_CODE_LABEL "LM"
3276 #endif
3277
3278 \f
3279 /* Return the root of the DIE's built for the current compilation unit. */
3280 static dw_die_ref
3281 comp_unit_die (void)
3282 {
3283 if (!single_comp_unit_die)
3284 single_comp_unit_die = gen_compile_unit_die (NULL);
3285 return single_comp_unit_die;
3286 }
3287
3288 /* We allow a language front-end to designate a function that is to be
3289 called to "demangle" any name before it is put into a DIE. */
3290
3291 static const char *(*demangle_name_func) (const char *);
3292
3293 void
3294 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3295 {
3296 demangle_name_func = func;
3297 }
3298
3299 /* Test if rtl node points to a pseudo register. */
3300
3301 static inline int
3302 is_pseudo_reg (const_rtx rtl)
3303 {
3304 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3305 || (GET_CODE (rtl) == SUBREG
3306 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3307 }
3308
3309 /* Return a reference to a type, with its const and volatile qualifiers
3310 removed. */
3311
3312 static inline tree
3313 type_main_variant (tree type)
3314 {
3315 type = TYPE_MAIN_VARIANT (type);
3316
3317 /* ??? There really should be only one main variant among any group of
3318 variants of a given type (and all of the MAIN_VARIANT values for all
3319 members of the group should point to that one type) but sometimes the C
3320 front-end messes this up for array types, so we work around that bug
3321 here. */
3322 if (TREE_CODE (type) == ARRAY_TYPE)
3323 while (type != TYPE_MAIN_VARIANT (type))
3324 type = TYPE_MAIN_VARIANT (type);
3325
3326 return type;
3327 }
3328
3329 /* Return nonzero if the given type node represents a tagged type. */
3330
3331 static inline int
3332 is_tagged_type (const_tree type)
3333 {
3334 enum tree_code code = TREE_CODE (type);
3335
3336 return (code == RECORD_TYPE || code == UNION_TYPE
3337 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3338 }
3339
3340 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3341
3342 static void
3343 get_ref_die_offset_label (char *label, dw_die_ref ref)
3344 {
3345 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3346 }
3347
3348 /* Return die_offset of a DIE reference to a base type. */
3349
3350 static unsigned long int
3351 get_base_type_offset (dw_die_ref ref)
3352 {
3353 if (ref->die_offset)
3354 return ref->die_offset;
3355 if (comp_unit_die ()->die_abbrev)
3356 {
3357 calc_base_type_die_sizes ();
3358 gcc_assert (ref->die_offset);
3359 }
3360 return ref->die_offset;
3361 }
3362
3363 /* Return die_offset of a DIE reference other than base type. */
3364
3365 static unsigned long int
3366 get_ref_die_offset (dw_die_ref ref)
3367 {
3368 gcc_assert (ref->die_offset);
3369 return ref->die_offset;
3370 }
3371
3372 /* Convert a DIE tag into its string name. */
3373
3374 static const char *
3375 dwarf_tag_name (unsigned int tag)
3376 {
3377 const char *name = get_DW_TAG_name (tag);
3378
3379 if (name != NULL)
3380 return name;
3381
3382 return "DW_TAG_<unknown>";
3383 }
3384
3385 /* Convert a DWARF attribute code into its string name. */
3386
3387 static const char *
3388 dwarf_attr_name (unsigned int attr)
3389 {
3390 const char *name;
3391
3392 switch (attr)
3393 {
3394 #if VMS_DEBUGGING_INFO
3395 case DW_AT_HP_prologue:
3396 return "DW_AT_HP_prologue";
3397 #else
3398 case DW_AT_MIPS_loop_unroll_factor:
3399 return "DW_AT_MIPS_loop_unroll_factor";
3400 #endif
3401
3402 #if VMS_DEBUGGING_INFO
3403 case DW_AT_HP_epilogue:
3404 return "DW_AT_HP_epilogue";
3405 #else
3406 case DW_AT_MIPS_stride:
3407 return "DW_AT_MIPS_stride";
3408 #endif
3409 }
3410
3411 name = get_DW_AT_name (attr);
3412
3413 if (name != NULL)
3414 return name;
3415
3416 return "DW_AT_<unknown>";
3417 }
3418
3419 /* Convert a DWARF value form code into its string name. */
3420
3421 static const char *
3422 dwarf_form_name (unsigned int form)
3423 {
3424 const char *name = get_DW_FORM_name (form);
3425
3426 if (name != NULL)
3427 return name;
3428
3429 return "DW_FORM_<unknown>";
3430 }
3431 \f
3432 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3433 instance of an inlined instance of a decl which is local to an inline
3434 function, so we have to trace all of the way back through the origin chain
3435 to find out what sort of node actually served as the original seed for the
3436 given block. */
3437
3438 static tree
3439 decl_ultimate_origin (const_tree decl)
3440 {
3441 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3442 return NULL_TREE;
3443
3444 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3445 nodes in the function to point to themselves; ignore that if
3446 we're trying to output the abstract instance of this function. */
3447 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3448 return NULL_TREE;
3449
3450 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3451 most distant ancestor, this should never happen. */
3452 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3453
3454 return DECL_ABSTRACT_ORIGIN (decl);
3455 }
3456
3457 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3458 of a virtual function may refer to a base class, so we check the 'this'
3459 parameter. */
3460
3461 static tree
3462 decl_class_context (tree decl)
3463 {
3464 tree context = NULL_TREE;
3465
3466 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3467 context = DECL_CONTEXT (decl);
3468 else
3469 context = TYPE_MAIN_VARIANT
3470 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3471
3472 if (context && !TYPE_P (context))
3473 context = NULL_TREE;
3474
3475 return context;
3476 }
3477 \f
3478 /* Add an attribute/value pair to a DIE. */
3479
3480 static inline void
3481 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3482 {
3483 /* Maybe this should be an assert? */
3484 if (die == NULL)
3485 return;
3486
3487 if (die->die_attr == NULL)
3488 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
3489 VEC_safe_push (dw_attr_node, gc, die->die_attr, *attr);
3490 }
3491
3492 static inline enum dw_val_class
3493 AT_class (dw_attr_ref a)
3494 {
3495 return a->dw_attr_val.val_class;
3496 }
3497
3498 /* Add a flag value attribute to a DIE. */
3499
3500 static inline void
3501 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3502 {
3503 dw_attr_node attr;
3504
3505 attr.dw_attr = attr_kind;
3506 attr.dw_attr_val.val_class = dw_val_class_flag;
3507 attr.dw_attr_val.v.val_flag = flag;
3508 add_dwarf_attr (die, &attr);
3509 }
3510
3511 static inline unsigned
3512 AT_flag (dw_attr_ref a)
3513 {
3514 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3515 return a->dw_attr_val.v.val_flag;
3516 }
3517
3518 /* Add a signed integer attribute value to a DIE. */
3519
3520 static inline void
3521 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3522 {
3523 dw_attr_node attr;
3524
3525 attr.dw_attr = attr_kind;
3526 attr.dw_attr_val.val_class = dw_val_class_const;
3527 attr.dw_attr_val.v.val_int = int_val;
3528 add_dwarf_attr (die, &attr);
3529 }
3530
3531 static inline HOST_WIDE_INT
3532 AT_int (dw_attr_ref a)
3533 {
3534 gcc_assert (a && AT_class (a) == dw_val_class_const);
3535 return a->dw_attr_val.v.val_int;
3536 }
3537
3538 /* Add an unsigned integer attribute value to a DIE. */
3539
3540 static inline void
3541 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3542 unsigned HOST_WIDE_INT unsigned_val)
3543 {
3544 dw_attr_node attr;
3545
3546 attr.dw_attr = attr_kind;
3547 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3548 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3549 add_dwarf_attr (die, &attr);
3550 }
3551
3552 static inline unsigned HOST_WIDE_INT
3553 AT_unsigned (dw_attr_ref a)
3554 {
3555 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3556 return a->dw_attr_val.v.val_unsigned;
3557 }
3558
3559 /* Add an unsigned double integer attribute value to a DIE. */
3560
3561 static inline void
3562 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3563 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3564 {
3565 dw_attr_node attr;
3566
3567 attr.dw_attr = attr_kind;
3568 attr.dw_attr_val.val_class = dw_val_class_const_double;
3569 attr.dw_attr_val.v.val_double.high = high;
3570 attr.dw_attr_val.v.val_double.low = low;
3571 add_dwarf_attr (die, &attr);
3572 }
3573
3574 /* Add a floating point attribute value to a DIE and return it. */
3575
3576 static inline void
3577 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3578 unsigned int length, unsigned int elt_size, unsigned char *array)
3579 {
3580 dw_attr_node attr;
3581
3582 attr.dw_attr = attr_kind;
3583 attr.dw_attr_val.val_class = dw_val_class_vec;
3584 attr.dw_attr_val.v.val_vec.length = length;
3585 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3586 attr.dw_attr_val.v.val_vec.array = array;
3587 add_dwarf_attr (die, &attr);
3588 }
3589
3590 /* Add an 8-byte data attribute value to a DIE. */
3591
3592 static inline void
3593 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3594 unsigned char data8[8])
3595 {
3596 dw_attr_node attr;
3597
3598 attr.dw_attr = attr_kind;
3599 attr.dw_attr_val.val_class = dw_val_class_data8;
3600 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3601 add_dwarf_attr (die, &attr);
3602 }
3603
3604 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. */
3605 static inline void
3606 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high)
3607 {
3608 dw_attr_node attr;
3609
3610 attr.dw_attr = DW_AT_low_pc;
3611 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3612 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_low);
3613 add_dwarf_attr (die, &attr);
3614
3615 attr.dw_attr = DW_AT_high_pc;
3616 if (dwarf_version < 4)
3617 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3618 else
3619 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3620 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_high);
3621 add_dwarf_attr (die, &attr);
3622 }
3623
3624 /* Hash and equality functions for debug_str_hash. */
3625
3626 static hashval_t
3627 debug_str_do_hash (const void *x)
3628 {
3629 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3630 }
3631
3632 static int
3633 debug_str_eq (const void *x1, const void *x2)
3634 {
3635 return strcmp ((((const struct indirect_string_node *)x1)->str),
3636 (const char *)x2) == 0;
3637 }
3638
3639 /* Add STR to the indirect string hash table. */
3640
3641 static struct indirect_string_node *
3642 find_AT_string (const char *str)
3643 {
3644 struct indirect_string_node *node;
3645 void **slot;
3646
3647 if (! debug_str_hash)
3648 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3649 debug_str_eq, NULL);
3650
3651 slot = htab_find_slot_with_hash (debug_str_hash, str,
3652 htab_hash_string (str), INSERT);
3653 if (*slot == NULL)
3654 {
3655 node = ggc_alloc_cleared_indirect_string_node ();
3656 node->str = ggc_strdup (str);
3657 *slot = node;
3658 }
3659 else
3660 node = (struct indirect_string_node *) *slot;
3661
3662 node->refcount++;
3663 return node;
3664 }
3665
3666 /* Add a string attribute value to a DIE. */
3667
3668 static inline void
3669 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3670 {
3671 dw_attr_node attr;
3672 struct indirect_string_node *node;
3673
3674 node = find_AT_string (str);
3675
3676 attr.dw_attr = attr_kind;
3677 attr.dw_attr_val.val_class = dw_val_class_str;
3678 attr.dw_attr_val.v.val_str = node;
3679 add_dwarf_attr (die, &attr);
3680 }
3681
3682 static inline const char *
3683 AT_string (dw_attr_ref a)
3684 {
3685 gcc_assert (a && AT_class (a) == dw_val_class_str);
3686 return a->dw_attr_val.v.val_str->str;
3687 }
3688
3689 /* Find out whether a string should be output inline in DIE
3690 or out-of-line in .debug_str section. */
3691
3692 static enum dwarf_form
3693 AT_string_form (dw_attr_ref a)
3694 {
3695 struct indirect_string_node *node;
3696 unsigned int len;
3697 char label[32];
3698
3699 gcc_assert (a && AT_class (a) == dw_val_class_str);
3700
3701 node = a->dw_attr_val.v.val_str;
3702 if (node->form)
3703 return node->form;
3704
3705 len = strlen (node->str) + 1;
3706
3707 /* If the string is shorter or equal to the size of the reference, it is
3708 always better to put it inline. */
3709 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3710 return node->form = DW_FORM_string;
3711
3712 /* If we cannot expect the linker to merge strings in .debug_str
3713 section, only put it into .debug_str if it is worth even in this
3714 single module. */
3715 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3716 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3717 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3718 return node->form = DW_FORM_string;
3719
3720 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3721 ++dw2_string_counter;
3722 node->label = xstrdup (label);
3723
3724 return node->form = DW_FORM_strp;
3725 }
3726
3727 /* Add a DIE reference attribute value to a DIE. */
3728
3729 static inline void
3730 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3731 {
3732 dw_attr_node attr;
3733
3734 #ifdef ENABLE_CHECKING
3735 gcc_assert (targ_die != NULL);
3736 #else
3737 /* With LTO we can end up trying to reference something we didn't create
3738 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3739 if (targ_die == NULL)
3740 return;
3741 #endif
3742
3743 attr.dw_attr = attr_kind;
3744 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3745 attr.dw_attr_val.v.val_die_ref.die = targ_die;
3746 attr.dw_attr_val.v.val_die_ref.external = 0;
3747 add_dwarf_attr (die, &attr);
3748 }
3749
3750 /* Change DIE reference REF to point to NEW_DIE instead. */
3751
3752 static inline void
3753 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
3754 {
3755 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
3756 ref->dw_attr_val.v.val_die_ref.die = new_die;
3757 ref->dw_attr_val.v.val_die_ref.external = 0;
3758 }
3759
3760 /* Add an AT_specification attribute to a DIE, and also make the back
3761 pointer from the specification to the definition. */
3762
3763 static inline void
3764 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
3765 {
3766 add_AT_die_ref (die, DW_AT_specification, targ_die);
3767 gcc_assert (!targ_die->die_definition);
3768 targ_die->die_definition = die;
3769 }
3770
3771 static inline dw_die_ref
3772 AT_ref (dw_attr_ref a)
3773 {
3774 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3775 return a->dw_attr_val.v.val_die_ref.die;
3776 }
3777
3778 static inline int
3779 AT_ref_external (dw_attr_ref a)
3780 {
3781 if (a && AT_class (a) == dw_val_class_die_ref)
3782 return a->dw_attr_val.v.val_die_ref.external;
3783
3784 return 0;
3785 }
3786
3787 static inline void
3788 set_AT_ref_external (dw_attr_ref a, int i)
3789 {
3790 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3791 a->dw_attr_val.v.val_die_ref.external = i;
3792 }
3793
3794 /* Add an FDE reference attribute value to a DIE. */
3795
3796 static inline void
3797 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
3798 {
3799 dw_attr_node attr;
3800
3801 attr.dw_attr = attr_kind;
3802 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
3803 attr.dw_attr_val.v.val_fde_index = targ_fde;
3804 add_dwarf_attr (die, &attr);
3805 }
3806
3807 /* Add a location description attribute value to a DIE. */
3808
3809 static inline void
3810 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
3811 {
3812 dw_attr_node attr;
3813
3814 attr.dw_attr = attr_kind;
3815 attr.dw_attr_val.val_class = dw_val_class_loc;
3816 attr.dw_attr_val.v.val_loc = loc;
3817 add_dwarf_attr (die, &attr);
3818 }
3819
3820 static inline dw_loc_descr_ref
3821 AT_loc (dw_attr_ref a)
3822 {
3823 gcc_assert (a && AT_class (a) == dw_val_class_loc);
3824 return a->dw_attr_val.v.val_loc;
3825 }
3826
3827 static inline void
3828 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
3829 {
3830 dw_attr_node attr;
3831
3832 attr.dw_attr = attr_kind;
3833 attr.dw_attr_val.val_class = dw_val_class_loc_list;
3834 attr.dw_attr_val.v.val_loc_list = loc_list;
3835 add_dwarf_attr (die, &attr);
3836 have_location_lists = true;
3837 }
3838
3839 static inline dw_loc_list_ref
3840 AT_loc_list (dw_attr_ref a)
3841 {
3842 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
3843 return a->dw_attr_val.v.val_loc_list;
3844 }
3845
3846 static inline dw_loc_list_ref *
3847 AT_loc_list_ptr (dw_attr_ref a)
3848 {
3849 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
3850 return &a->dw_attr_val.v.val_loc_list;
3851 }
3852
3853 /* Add an address constant attribute value to a DIE. */
3854
3855 static inline void
3856 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
3857 {
3858 dw_attr_node attr;
3859
3860 attr.dw_attr = attr_kind;
3861 attr.dw_attr_val.val_class = dw_val_class_addr;
3862 attr.dw_attr_val.v.val_addr = addr;
3863 add_dwarf_attr (die, &attr);
3864 }
3865
3866 /* Get the RTX from to an address DIE attribute. */
3867
3868 static inline rtx
3869 AT_addr (dw_attr_ref a)
3870 {
3871 gcc_assert (a && AT_class (a) == dw_val_class_addr);
3872 return a->dw_attr_val.v.val_addr;
3873 }
3874
3875 /* Add a file attribute value to a DIE. */
3876
3877 static inline void
3878 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
3879 struct dwarf_file_data *fd)
3880 {
3881 dw_attr_node attr;
3882
3883 attr.dw_attr = attr_kind;
3884 attr.dw_attr_val.val_class = dw_val_class_file;
3885 attr.dw_attr_val.v.val_file = fd;
3886 add_dwarf_attr (die, &attr);
3887 }
3888
3889 /* Get the dwarf_file_data from a file DIE attribute. */
3890
3891 static inline struct dwarf_file_data *
3892 AT_file (dw_attr_ref a)
3893 {
3894 gcc_assert (a && AT_class (a) == dw_val_class_file);
3895 return a->dw_attr_val.v.val_file;
3896 }
3897
3898 /* Add a vms delta attribute value to a DIE. */
3899
3900 static inline void
3901 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
3902 const char *lbl1, const char *lbl2)
3903 {
3904 dw_attr_node attr;
3905
3906 attr.dw_attr = attr_kind;
3907 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
3908 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
3909 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
3910 add_dwarf_attr (die, &attr);
3911 }
3912
3913 /* Add a label identifier attribute value to a DIE. */
3914
3915 static inline void
3916 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
3917 {
3918 dw_attr_node attr;
3919
3920 attr.dw_attr = attr_kind;
3921 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3922 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
3923 add_dwarf_attr (die, &attr);
3924 }
3925
3926 /* Add a section offset attribute value to a DIE, an offset into the
3927 debug_line section. */
3928
3929 static inline void
3930 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
3931 const char *label)
3932 {
3933 dw_attr_node attr;
3934
3935 attr.dw_attr = attr_kind;
3936 attr.dw_attr_val.val_class = dw_val_class_lineptr;
3937 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
3938 add_dwarf_attr (die, &attr);
3939 }
3940
3941 /* Add a section offset attribute value to a DIE, an offset into the
3942 debug_macinfo section. */
3943
3944 static inline void
3945 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
3946 const char *label)
3947 {
3948 dw_attr_node attr;
3949
3950 attr.dw_attr = attr_kind;
3951 attr.dw_attr_val.val_class = dw_val_class_macptr;
3952 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
3953 add_dwarf_attr (die, &attr);
3954 }
3955
3956 /* Add an offset attribute value to a DIE. */
3957
3958 static inline void
3959 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
3960 unsigned HOST_WIDE_INT offset)
3961 {
3962 dw_attr_node attr;
3963
3964 attr.dw_attr = attr_kind;
3965 attr.dw_attr_val.val_class = dw_val_class_offset;
3966 attr.dw_attr_val.v.val_offset = offset;
3967 add_dwarf_attr (die, &attr);
3968 }
3969
3970 /* Add an range_list attribute value to a DIE. */
3971
3972 static void
3973 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
3974 long unsigned int offset)
3975 {
3976 dw_attr_node attr;
3977
3978 attr.dw_attr = attr_kind;
3979 attr.dw_attr_val.val_class = dw_val_class_range_list;
3980 attr.dw_attr_val.v.val_offset = offset;
3981 add_dwarf_attr (die, &attr);
3982 }
3983
3984 /* Return the start label of a delta attribute. */
3985
3986 static inline const char *
3987 AT_vms_delta1 (dw_attr_ref a)
3988 {
3989 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
3990 return a->dw_attr_val.v.val_vms_delta.lbl1;
3991 }
3992
3993 /* Return the end label of a delta attribute. */
3994
3995 static inline const char *
3996 AT_vms_delta2 (dw_attr_ref a)
3997 {
3998 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
3999 return a->dw_attr_val.v.val_vms_delta.lbl2;
4000 }
4001
4002 static inline const char *
4003 AT_lbl (dw_attr_ref a)
4004 {
4005 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4006 || AT_class (a) == dw_val_class_lineptr
4007 || AT_class (a) == dw_val_class_macptr
4008 || AT_class (a) == dw_val_class_high_pc));
4009 return a->dw_attr_val.v.val_lbl_id;
4010 }
4011
4012 /* Get the attribute of type attr_kind. */
4013
4014 static dw_attr_ref
4015 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4016 {
4017 dw_attr_ref a;
4018 unsigned ix;
4019 dw_die_ref spec = NULL;
4020
4021 if (! die)
4022 return NULL;
4023
4024 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4025 if (a->dw_attr == attr_kind)
4026 return a;
4027 else if (a->dw_attr == DW_AT_specification
4028 || a->dw_attr == DW_AT_abstract_origin)
4029 spec = AT_ref (a);
4030
4031 if (spec)
4032 return get_AT (spec, attr_kind);
4033
4034 return NULL;
4035 }
4036
4037 /* Returns the parent of the declaration of DIE. */
4038
4039 static dw_die_ref
4040 get_die_parent (dw_die_ref die)
4041 {
4042 dw_die_ref t;
4043
4044 if (!die)
4045 return NULL;
4046
4047 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4048 || (t = get_AT_ref (die, DW_AT_specification)))
4049 die = t;
4050
4051 return die->die_parent;
4052 }
4053
4054 /* Return the "low pc" attribute value, typically associated with a subprogram
4055 DIE. Return null if the "low pc" attribute is either not present, or if it
4056 cannot be represented as an assembler label identifier. */
4057
4058 static inline const char *
4059 get_AT_low_pc (dw_die_ref die)
4060 {
4061 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4062
4063 return a ? AT_lbl (a) : NULL;
4064 }
4065
4066 /* Return the "high pc" attribute value, typically associated with a subprogram
4067 DIE. Return null if the "high pc" attribute is either not present, or if it
4068 cannot be represented as an assembler label identifier. */
4069
4070 static inline const char *
4071 get_AT_hi_pc (dw_die_ref die)
4072 {
4073 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4074
4075 return a ? AT_lbl (a) : NULL;
4076 }
4077
4078 /* Return the value of the string attribute designated by ATTR_KIND, or
4079 NULL if it is not present. */
4080
4081 static inline const char *
4082 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4083 {
4084 dw_attr_ref a = get_AT (die, attr_kind);
4085
4086 return a ? AT_string (a) : NULL;
4087 }
4088
4089 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4090 if it is not present. */
4091
4092 static inline int
4093 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4094 {
4095 dw_attr_ref a = get_AT (die, attr_kind);
4096
4097 return a ? AT_flag (a) : 0;
4098 }
4099
4100 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4101 if it is not present. */
4102
4103 static inline unsigned
4104 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4105 {
4106 dw_attr_ref a = get_AT (die, attr_kind);
4107
4108 return a ? AT_unsigned (a) : 0;
4109 }
4110
4111 static inline dw_die_ref
4112 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4113 {
4114 dw_attr_ref a = get_AT (die, attr_kind);
4115
4116 return a ? AT_ref (a) : NULL;
4117 }
4118
4119 static inline struct dwarf_file_data *
4120 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4121 {
4122 dw_attr_ref a = get_AT (die, attr_kind);
4123
4124 return a ? AT_file (a) : NULL;
4125 }
4126
4127 /* Return TRUE if the language is C++. */
4128
4129 static inline bool
4130 is_cxx (void)
4131 {
4132 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4133
4134 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4135 }
4136
4137 /* Return TRUE if the language is Fortran. */
4138
4139 static inline bool
4140 is_fortran (void)
4141 {
4142 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4143
4144 return (lang == DW_LANG_Fortran77
4145 || lang == DW_LANG_Fortran90
4146 || lang == DW_LANG_Fortran95);
4147 }
4148
4149 /* Return TRUE if the language is Ada. */
4150
4151 static inline bool
4152 is_ada (void)
4153 {
4154 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4155
4156 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4157 }
4158
4159 /* Remove the specified attribute if present. */
4160
4161 static void
4162 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4163 {
4164 dw_attr_ref a;
4165 unsigned ix;
4166
4167 if (! die)
4168 return;
4169
4170 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4171 if (a->dw_attr == attr_kind)
4172 {
4173 if (AT_class (a) == dw_val_class_str)
4174 if (a->dw_attr_val.v.val_str->refcount)
4175 a->dw_attr_val.v.val_str->refcount--;
4176
4177 /* VEC_ordered_remove should help reduce the number of abbrevs
4178 that are needed. */
4179 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
4180 return;
4181 }
4182 }
4183
4184 /* Remove CHILD from its parent. PREV must have the property that
4185 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4186
4187 static void
4188 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4189 {
4190 gcc_assert (child->die_parent == prev->die_parent);
4191 gcc_assert (prev->die_sib == child);
4192 if (prev == child)
4193 {
4194 gcc_assert (child->die_parent->die_child == child);
4195 prev = NULL;
4196 }
4197 else
4198 prev->die_sib = child->die_sib;
4199 if (child->die_parent->die_child == child)
4200 child->die_parent->die_child = prev;
4201 }
4202
4203 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4204 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4205
4206 static void
4207 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4208 {
4209 dw_die_ref parent = old_child->die_parent;
4210
4211 gcc_assert (parent == prev->die_parent);
4212 gcc_assert (prev->die_sib == old_child);
4213
4214 new_child->die_parent = parent;
4215 if (prev == old_child)
4216 {
4217 gcc_assert (parent->die_child == old_child);
4218 new_child->die_sib = new_child;
4219 }
4220 else
4221 {
4222 prev->die_sib = new_child;
4223 new_child->die_sib = old_child->die_sib;
4224 }
4225 if (old_child->die_parent->die_child == old_child)
4226 old_child->die_parent->die_child = new_child;
4227 }
4228
4229 /* Move all children from OLD_PARENT to NEW_PARENT. */
4230
4231 static void
4232 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4233 {
4234 dw_die_ref c;
4235 new_parent->die_child = old_parent->die_child;
4236 old_parent->die_child = NULL;
4237 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4238 }
4239
4240 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4241 matches TAG. */
4242
4243 static void
4244 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4245 {
4246 dw_die_ref c;
4247
4248 c = die->die_child;
4249 if (c) do {
4250 dw_die_ref prev = c;
4251 c = c->die_sib;
4252 while (c->die_tag == tag)
4253 {
4254 remove_child_with_prev (c, prev);
4255 /* Might have removed every child. */
4256 if (c == c->die_sib)
4257 return;
4258 c = c->die_sib;
4259 }
4260 } while (c != die->die_child);
4261 }
4262
4263 /* Add a CHILD_DIE as the last child of DIE. */
4264
4265 static void
4266 add_child_die (dw_die_ref die, dw_die_ref child_die)
4267 {
4268 /* FIXME this should probably be an assert. */
4269 if (! die || ! child_die)
4270 return;
4271 gcc_assert (die != child_die);
4272
4273 child_die->die_parent = die;
4274 if (die->die_child)
4275 {
4276 child_die->die_sib = die->die_child->die_sib;
4277 die->die_child->die_sib = child_die;
4278 }
4279 else
4280 child_die->die_sib = child_die;
4281 die->die_child = child_die;
4282 }
4283
4284 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4285 is the specification, to the end of PARENT's list of children.
4286 This is done by removing and re-adding it. */
4287
4288 static void
4289 splice_child_die (dw_die_ref parent, dw_die_ref child)
4290 {
4291 dw_die_ref p;
4292
4293 /* We want the declaration DIE from inside the class, not the
4294 specification DIE at toplevel. */
4295 if (child->die_parent != parent)
4296 {
4297 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4298
4299 if (tmp)
4300 child = tmp;
4301 }
4302
4303 gcc_assert (child->die_parent == parent
4304 || (child->die_parent
4305 == get_AT_ref (parent, DW_AT_specification)));
4306
4307 for (p = child->die_parent->die_child; ; p = p->die_sib)
4308 if (p->die_sib == child)
4309 {
4310 remove_child_with_prev (child, p);
4311 break;
4312 }
4313
4314 add_child_die (parent, child);
4315 }
4316
4317 /* Return a pointer to a newly created DIE node. */
4318
4319 static inline dw_die_ref
4320 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4321 {
4322 dw_die_ref die = ggc_alloc_cleared_die_node ();
4323
4324 die->die_tag = tag_value;
4325
4326 if (parent_die != NULL)
4327 add_child_die (parent_die, die);
4328 else
4329 {
4330 limbo_die_node *limbo_node;
4331
4332 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4333 limbo_node->die = die;
4334 limbo_node->created_for = t;
4335 limbo_node->next = limbo_die_list;
4336 limbo_die_list = limbo_node;
4337 }
4338
4339 return die;
4340 }
4341
4342 /* Return the DIE associated with the given type specifier. */
4343
4344 static inline dw_die_ref
4345 lookup_type_die (tree type)
4346 {
4347 return TYPE_SYMTAB_DIE (type);
4348 }
4349
4350 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4351 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4352 anonymous type instead the one of the naming typedef. */
4353
4354 static inline dw_die_ref
4355 strip_naming_typedef (tree type, dw_die_ref type_die)
4356 {
4357 if (type
4358 && TREE_CODE (type) == RECORD_TYPE
4359 && type_die
4360 && type_die->die_tag == DW_TAG_typedef
4361 && is_naming_typedef_decl (TYPE_NAME (type)))
4362 type_die = get_AT_ref (type_die, DW_AT_type);
4363 return type_die;
4364 }
4365
4366 /* Like lookup_type_die, but if type is an anonymous type named by a
4367 typedef[1], return the DIE of the anonymous type instead the one of
4368 the naming typedef. This is because in gen_typedef_die, we did
4369 equate the anonymous struct named by the typedef with the DIE of
4370 the naming typedef. So by default, lookup_type_die on an anonymous
4371 struct yields the DIE of the naming typedef.
4372
4373 [1]: Read the comment of is_naming_typedef_decl to learn about what
4374 a naming typedef is. */
4375
4376 static inline dw_die_ref
4377 lookup_type_die_strip_naming_typedef (tree type)
4378 {
4379 dw_die_ref die = lookup_type_die (type);
4380 return strip_naming_typedef (type, die);
4381 }
4382
4383 /* Equate a DIE to a given type specifier. */
4384
4385 static inline void
4386 equate_type_number_to_die (tree type, dw_die_ref type_die)
4387 {
4388 TYPE_SYMTAB_DIE (type) = type_die;
4389 }
4390
4391 /* Returns a hash value for X (which really is a die_struct). */
4392
4393 static hashval_t
4394 decl_die_table_hash (const void *x)
4395 {
4396 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4397 }
4398
4399 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4400
4401 static int
4402 decl_die_table_eq (const void *x, const void *y)
4403 {
4404 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4405 }
4406
4407 /* Return the DIE associated with a given declaration. */
4408
4409 static inline dw_die_ref
4410 lookup_decl_die (tree decl)
4411 {
4412 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4413 }
4414
4415 /* Returns a hash value for X (which really is a var_loc_list). */
4416
4417 static hashval_t
4418 decl_loc_table_hash (const void *x)
4419 {
4420 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4421 }
4422
4423 /* Return nonzero if decl_id of var_loc_list X is the same as
4424 UID of decl *Y. */
4425
4426 static int
4427 decl_loc_table_eq (const void *x, const void *y)
4428 {
4429 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4430 }
4431
4432 /* Return the var_loc list associated with a given declaration. */
4433
4434 static inline var_loc_list *
4435 lookup_decl_loc (const_tree decl)
4436 {
4437 if (!decl_loc_table)
4438 return NULL;
4439 return (var_loc_list *)
4440 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4441 }
4442
4443 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4444
4445 static hashval_t
4446 cached_dw_loc_list_table_hash (const void *x)
4447 {
4448 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4449 }
4450
4451 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4452 UID of decl *Y. */
4453
4454 static int
4455 cached_dw_loc_list_table_eq (const void *x, const void *y)
4456 {
4457 return (((const cached_dw_loc_list *) x)->decl_id
4458 == DECL_UID ((const_tree) y));
4459 }
4460
4461 /* Equate a DIE to a particular declaration. */
4462
4463 static void
4464 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4465 {
4466 unsigned int decl_id = DECL_UID (decl);
4467 void **slot;
4468
4469 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4470 *slot = decl_die;
4471 decl_die->decl_id = decl_id;
4472 }
4473
4474 /* Return how many bits covers PIECE EXPR_LIST. */
4475
4476 static int
4477 decl_piece_bitsize (rtx piece)
4478 {
4479 int ret = (int) GET_MODE (piece);
4480 if (ret)
4481 return ret;
4482 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4483 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4484 return INTVAL (XEXP (XEXP (piece, 0), 0));
4485 }
4486
4487 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4488
4489 static rtx *
4490 decl_piece_varloc_ptr (rtx piece)
4491 {
4492 if ((int) GET_MODE (piece))
4493 return &XEXP (piece, 0);
4494 else
4495 return &XEXP (XEXP (piece, 0), 1);
4496 }
4497
4498 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4499 Next is the chain of following piece nodes. */
4500
4501 static rtx
4502 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4503 {
4504 if (bitsize <= (int) MAX_MACHINE_MODE)
4505 return alloc_EXPR_LIST (bitsize, loc_note, next);
4506 else
4507 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4508 GEN_INT (bitsize),
4509 loc_note), next);
4510 }
4511
4512 /* Return rtx that should be stored into loc field for
4513 LOC_NOTE and BITPOS/BITSIZE. */
4514
4515 static rtx
4516 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4517 HOST_WIDE_INT bitsize)
4518 {
4519 if (bitsize != -1)
4520 {
4521 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4522 if (bitpos != 0)
4523 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4524 }
4525 return loc_note;
4526 }
4527
4528 /* This function either modifies location piece list *DEST in
4529 place (if SRC and INNER is NULL), or copies location piece list
4530 *SRC to *DEST while modifying it. Location BITPOS is modified
4531 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4532 not copied and if needed some padding around it is added.
4533 When modifying in place, DEST should point to EXPR_LIST where
4534 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4535 to the start of the whole list and INNER points to the EXPR_LIST
4536 where earlier pieces cover PIECE_BITPOS bits. */
4537
4538 static void
4539 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4540 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4541 HOST_WIDE_INT bitsize, rtx loc_note)
4542 {
4543 int diff;
4544 bool copy = inner != NULL;
4545
4546 if (copy)
4547 {
4548 /* First copy all nodes preceding the current bitpos. */
4549 while (src != inner)
4550 {
4551 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4552 decl_piece_bitsize (*src), NULL_RTX);
4553 dest = &XEXP (*dest, 1);
4554 src = &XEXP (*src, 1);
4555 }
4556 }
4557 /* Add padding if needed. */
4558 if (bitpos != piece_bitpos)
4559 {
4560 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4561 copy ? NULL_RTX : *dest);
4562 dest = &XEXP (*dest, 1);
4563 }
4564 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4565 {
4566 gcc_assert (!copy);
4567 /* A piece with correct bitpos and bitsize already exist,
4568 just update the location for it and return. */
4569 *decl_piece_varloc_ptr (*dest) = loc_note;
4570 return;
4571 }
4572 /* Add the piece that changed. */
4573 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
4574 dest = &XEXP (*dest, 1);
4575 /* Skip over pieces that overlap it. */
4576 diff = bitpos - piece_bitpos + bitsize;
4577 if (!copy)
4578 src = dest;
4579 while (diff > 0 && *src)
4580 {
4581 rtx piece = *src;
4582 diff -= decl_piece_bitsize (piece);
4583 if (copy)
4584 src = &XEXP (piece, 1);
4585 else
4586 {
4587 *src = XEXP (piece, 1);
4588 free_EXPR_LIST_node (piece);
4589 }
4590 }
4591 /* Add padding if needed. */
4592 if (diff < 0 && *src)
4593 {
4594 if (!copy)
4595 dest = src;
4596 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
4597 dest = &XEXP (*dest, 1);
4598 }
4599 if (!copy)
4600 return;
4601 /* Finally copy all nodes following it. */
4602 while (*src)
4603 {
4604 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4605 decl_piece_bitsize (*src), NULL_RTX);
4606 dest = &XEXP (*dest, 1);
4607 src = &XEXP (*src, 1);
4608 }
4609 }
4610
4611 /* Add a variable location node to the linked list for DECL. */
4612
4613 static struct var_loc_node *
4614 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
4615 {
4616 unsigned int decl_id;
4617 var_loc_list *temp;
4618 void **slot;
4619 struct var_loc_node *loc = NULL;
4620 HOST_WIDE_INT bitsize = -1, bitpos = -1;
4621
4622 if (DECL_DEBUG_EXPR_IS_FROM (decl))
4623 {
4624 tree realdecl = DECL_DEBUG_EXPR (decl);
4625 if (realdecl && handled_component_p (realdecl))
4626 {
4627 HOST_WIDE_INT maxsize;
4628 tree innerdecl;
4629 innerdecl
4630 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
4631 if (!DECL_P (innerdecl)
4632 || DECL_IGNORED_P (innerdecl)
4633 || TREE_STATIC (innerdecl)
4634 || bitsize <= 0
4635 || bitpos + bitsize > 256
4636 || bitsize != maxsize)
4637 return NULL;
4638 decl = innerdecl;
4639 }
4640 }
4641
4642 decl_id = DECL_UID (decl);
4643 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
4644 if (*slot == NULL)
4645 {
4646 temp = ggc_alloc_cleared_var_loc_list ();
4647 temp->decl_id = decl_id;
4648 *slot = temp;
4649 }
4650 else
4651 temp = (var_loc_list *) *slot;
4652
4653 /* For PARM_DECLs try to keep around the original incoming value,
4654 even if that means we'll emit a zero-range .debug_loc entry. */
4655 if (temp->last
4656 && temp->first == temp->last
4657 && TREE_CODE (decl) == PARM_DECL
4658 && GET_CODE (temp->first->loc) == NOTE
4659 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
4660 && DECL_INCOMING_RTL (decl)
4661 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
4662 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
4663 == GET_CODE (DECL_INCOMING_RTL (decl))
4664 && prev_real_insn (temp->first->loc) == NULL_RTX
4665 && (bitsize != -1
4666 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
4667 NOTE_VAR_LOCATION_LOC (loc_note))
4668 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
4669 != NOTE_VAR_LOCATION_STATUS (loc_note))))
4670 {
4671 loc = ggc_alloc_cleared_var_loc_node ();
4672 temp->first->next = loc;
4673 temp->last = loc;
4674 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
4675 }
4676 else if (temp->last)
4677 {
4678 struct var_loc_node *last = temp->last, *unused = NULL;
4679 rtx *piece_loc = NULL, last_loc_note;
4680 int piece_bitpos = 0;
4681 if (last->next)
4682 {
4683 last = last->next;
4684 gcc_assert (last->next == NULL);
4685 }
4686 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
4687 {
4688 piece_loc = &last->loc;
4689 do
4690 {
4691 int cur_bitsize = decl_piece_bitsize (*piece_loc);
4692 if (piece_bitpos + cur_bitsize > bitpos)
4693 break;
4694 piece_bitpos += cur_bitsize;
4695 piece_loc = &XEXP (*piece_loc, 1);
4696 }
4697 while (*piece_loc);
4698 }
4699 /* TEMP->LAST here is either pointer to the last but one or
4700 last element in the chained list, LAST is pointer to the
4701 last element. */
4702 if (label && strcmp (last->label, label) == 0)
4703 {
4704 /* For SRA optimized variables if there weren't any real
4705 insns since last note, just modify the last node. */
4706 if (piece_loc != NULL)
4707 {
4708 adjust_piece_list (piece_loc, NULL, NULL,
4709 bitpos, piece_bitpos, bitsize, loc_note);
4710 return NULL;
4711 }
4712 /* If the last note doesn't cover any instructions, remove it. */
4713 if (temp->last != last)
4714 {
4715 temp->last->next = NULL;
4716 unused = last;
4717 last = temp->last;
4718 gcc_assert (strcmp (last->label, label) != 0);
4719 }
4720 else
4721 {
4722 gcc_assert (temp->first == temp->last
4723 || (temp->first->next == temp->last
4724 && TREE_CODE (decl) == PARM_DECL));
4725 memset (temp->last, '\0', sizeof (*temp->last));
4726 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
4727 return temp->last;
4728 }
4729 }
4730 if (bitsize == -1 && NOTE_P (last->loc))
4731 last_loc_note = last->loc;
4732 else if (piece_loc != NULL
4733 && *piece_loc != NULL_RTX
4734 && piece_bitpos == bitpos
4735 && decl_piece_bitsize (*piece_loc) == bitsize)
4736 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
4737 else
4738 last_loc_note = NULL_RTX;
4739 /* If the current location is the same as the end of the list,
4740 and either both or neither of the locations is uninitialized,
4741 we have nothing to do. */
4742 if (last_loc_note == NULL_RTX
4743 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
4744 NOTE_VAR_LOCATION_LOC (loc_note)))
4745 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
4746 != NOTE_VAR_LOCATION_STATUS (loc_note))
4747 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
4748 == VAR_INIT_STATUS_UNINITIALIZED)
4749 || (NOTE_VAR_LOCATION_STATUS (loc_note)
4750 == VAR_INIT_STATUS_UNINITIALIZED))))
4751 {
4752 /* Add LOC to the end of list and update LAST. If the last
4753 element of the list has been removed above, reuse its
4754 memory for the new node, otherwise allocate a new one. */
4755 if (unused)
4756 {
4757 loc = unused;
4758 memset (loc, '\0', sizeof (*loc));
4759 }
4760 else
4761 loc = ggc_alloc_cleared_var_loc_node ();
4762 if (bitsize == -1 || piece_loc == NULL)
4763 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
4764 else
4765 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
4766 bitpos, piece_bitpos, bitsize, loc_note);
4767 last->next = loc;
4768 /* Ensure TEMP->LAST will point either to the new last but one
4769 element of the chain, or to the last element in it. */
4770 if (last != temp->last)
4771 temp->last = last;
4772 }
4773 else if (unused)
4774 ggc_free (unused);
4775 }
4776 else
4777 {
4778 loc = ggc_alloc_cleared_var_loc_node ();
4779 temp->first = loc;
4780 temp->last = loc;
4781 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
4782 }
4783 return loc;
4784 }
4785 \f
4786 /* Keep track of the number of spaces used to indent the
4787 output of the debugging routines that print the structure of
4788 the DIE internal representation. */
4789 static int print_indent;
4790
4791 /* Indent the line the number of spaces given by print_indent. */
4792
4793 static inline void
4794 print_spaces (FILE *outfile)
4795 {
4796 fprintf (outfile, "%*s", print_indent, "");
4797 }
4798
4799 /* Print a type signature in hex. */
4800
4801 static inline void
4802 print_signature (FILE *outfile, char *sig)
4803 {
4804 int i;
4805
4806 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
4807 fprintf (outfile, "%02x", sig[i] & 0xff);
4808 }
4809
4810 /* Print the information associated with a given DIE, and its children.
4811 This routine is a debugging aid only. */
4812
4813 static void
4814 print_die (dw_die_ref die, FILE *outfile)
4815 {
4816 dw_attr_ref a;
4817 dw_die_ref c;
4818 unsigned ix;
4819
4820 print_spaces (outfile);
4821 fprintf (outfile, "DIE %4ld: %s (%p)\n",
4822 die->die_offset, dwarf_tag_name (die->die_tag),
4823 (void*) die);
4824 print_spaces (outfile);
4825 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
4826 fprintf (outfile, " offset: %ld", die->die_offset);
4827 fprintf (outfile, " mark: %d\n", die->die_mark);
4828
4829 if (die->comdat_type_p)
4830 {
4831 print_spaces (outfile);
4832 fprintf (outfile, " signature: ");
4833 print_signature (outfile, die->die_id.die_type_node->signature);
4834 fprintf (outfile, "\n");
4835 }
4836
4837 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4838 {
4839 print_spaces (outfile);
4840 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
4841
4842 switch (AT_class (a))
4843 {
4844 case dw_val_class_addr:
4845 fprintf (outfile, "address");
4846 break;
4847 case dw_val_class_offset:
4848 fprintf (outfile, "offset");
4849 break;
4850 case dw_val_class_loc:
4851 fprintf (outfile, "location descriptor");
4852 break;
4853 case dw_val_class_loc_list:
4854 fprintf (outfile, "location list -> label:%s",
4855 AT_loc_list (a)->ll_symbol);
4856 break;
4857 case dw_val_class_range_list:
4858 fprintf (outfile, "range list");
4859 break;
4860 case dw_val_class_const:
4861 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
4862 break;
4863 case dw_val_class_unsigned_const:
4864 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
4865 break;
4866 case dw_val_class_const_double:
4867 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
4868 HOST_WIDE_INT_PRINT_UNSIGNED")",
4869 a->dw_attr_val.v.val_double.high,
4870 a->dw_attr_val.v.val_double.low);
4871 break;
4872 case dw_val_class_vec:
4873 fprintf (outfile, "floating-point or vector constant");
4874 break;
4875 case dw_val_class_flag:
4876 fprintf (outfile, "%u", AT_flag (a));
4877 break;
4878 case dw_val_class_die_ref:
4879 if (AT_ref (a) != NULL)
4880 {
4881 if (AT_ref (a)->comdat_type_p)
4882 {
4883 fprintf (outfile, "die -> signature: ");
4884 print_signature (outfile,
4885 AT_ref (a)->die_id.die_type_node->signature);
4886 }
4887 else if (AT_ref (a)->die_id.die_symbol)
4888 fprintf (outfile, "die -> label: %s",
4889 AT_ref (a)->die_id.die_symbol);
4890 else
4891 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
4892 fprintf (outfile, " (%p)", (void *) AT_ref (a));
4893 }
4894 else
4895 fprintf (outfile, "die -> <null>");
4896 break;
4897 case dw_val_class_vms_delta:
4898 fprintf (outfile, "delta: @slotcount(%s-%s)",
4899 AT_vms_delta2 (a), AT_vms_delta1 (a));
4900 break;
4901 case dw_val_class_lbl_id:
4902 case dw_val_class_lineptr:
4903 case dw_val_class_macptr:
4904 case dw_val_class_high_pc:
4905 fprintf (outfile, "label: %s", AT_lbl (a));
4906 break;
4907 case dw_val_class_str:
4908 if (AT_string (a) != NULL)
4909 fprintf (outfile, "\"%s\"", AT_string (a));
4910 else
4911 fprintf (outfile, "<null>");
4912 break;
4913 case dw_val_class_file:
4914 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
4915 AT_file (a)->emitted_number);
4916 break;
4917 case dw_val_class_data8:
4918 {
4919 int i;
4920
4921 for (i = 0; i < 8; i++)
4922 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
4923 break;
4924 }
4925 default:
4926 break;
4927 }
4928
4929 fprintf (outfile, "\n");
4930 }
4931
4932 if (die->die_child != NULL)
4933 {
4934 print_indent += 4;
4935 FOR_EACH_CHILD (die, c, print_die (c, outfile));
4936 print_indent -= 4;
4937 }
4938 if (print_indent == 0)
4939 fprintf (outfile, "\n");
4940 }
4941
4942 /* Print the information collected for a given DIE. */
4943
4944 DEBUG_FUNCTION void
4945 debug_dwarf_die (dw_die_ref die)
4946 {
4947 print_die (die, stderr);
4948 }
4949
4950 /* Print all DWARF information collected for the compilation unit.
4951 This routine is a debugging aid only. */
4952
4953 DEBUG_FUNCTION void
4954 debug_dwarf (void)
4955 {
4956 print_indent = 0;
4957 print_die (comp_unit_die (), stderr);
4958 }
4959 \f
4960 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
4961 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
4962 DIE that marks the start of the DIEs for this include file. */
4963
4964 static dw_die_ref
4965 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
4966 {
4967 const char *filename = get_AT_string (bincl_die, DW_AT_name);
4968 dw_die_ref new_unit = gen_compile_unit_die (filename);
4969
4970 new_unit->die_sib = old_unit;
4971 return new_unit;
4972 }
4973
4974 /* Close an include-file CU and reopen the enclosing one. */
4975
4976 static dw_die_ref
4977 pop_compile_unit (dw_die_ref old_unit)
4978 {
4979 dw_die_ref new_unit = old_unit->die_sib;
4980
4981 old_unit->die_sib = NULL;
4982 return new_unit;
4983 }
4984
4985 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
4986 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
4987
4988 /* Calculate the checksum of a location expression. */
4989
4990 static inline void
4991 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
4992 {
4993 int tem;
4994
4995 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
4996 CHECKSUM (tem);
4997 CHECKSUM (loc->dw_loc_oprnd1);
4998 CHECKSUM (loc->dw_loc_oprnd2);
4999 }
5000
5001 /* Calculate the checksum of an attribute. */
5002
5003 static void
5004 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5005 {
5006 dw_loc_descr_ref loc;
5007 rtx r;
5008
5009 CHECKSUM (at->dw_attr);
5010
5011 /* We don't care that this was compiled with a different compiler
5012 snapshot; if the output is the same, that's what matters. */
5013 if (at->dw_attr == DW_AT_producer)
5014 return;
5015
5016 switch (AT_class (at))
5017 {
5018 case dw_val_class_const:
5019 CHECKSUM (at->dw_attr_val.v.val_int);
5020 break;
5021 case dw_val_class_unsigned_const:
5022 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5023 break;
5024 case dw_val_class_const_double:
5025 CHECKSUM (at->dw_attr_val.v.val_double);
5026 break;
5027 case dw_val_class_vec:
5028 CHECKSUM (at->dw_attr_val.v.val_vec);
5029 break;
5030 case dw_val_class_flag:
5031 CHECKSUM (at->dw_attr_val.v.val_flag);
5032 break;
5033 case dw_val_class_str:
5034 CHECKSUM_STRING (AT_string (at));
5035 break;
5036
5037 case dw_val_class_addr:
5038 r = AT_addr (at);
5039 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5040 CHECKSUM_STRING (XSTR (r, 0));
5041 break;
5042
5043 case dw_val_class_offset:
5044 CHECKSUM (at->dw_attr_val.v.val_offset);
5045 break;
5046
5047 case dw_val_class_loc:
5048 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5049 loc_checksum (loc, ctx);
5050 break;
5051
5052 case dw_val_class_die_ref:
5053 die_checksum (AT_ref (at), ctx, mark);
5054 break;
5055
5056 case dw_val_class_fde_ref:
5057 case dw_val_class_vms_delta:
5058 case dw_val_class_lbl_id:
5059 case dw_val_class_lineptr:
5060 case dw_val_class_macptr:
5061 case dw_val_class_high_pc:
5062 break;
5063
5064 case dw_val_class_file:
5065 CHECKSUM_STRING (AT_file (at)->filename);
5066 break;
5067
5068 case dw_val_class_data8:
5069 CHECKSUM (at->dw_attr_val.v.val_data8);
5070 break;
5071
5072 default:
5073 break;
5074 }
5075 }
5076
5077 /* Calculate the checksum of a DIE. */
5078
5079 static void
5080 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5081 {
5082 dw_die_ref c;
5083 dw_attr_ref a;
5084 unsigned ix;
5085
5086 /* To avoid infinite recursion. */
5087 if (die->die_mark)
5088 {
5089 CHECKSUM (die->die_mark);
5090 return;
5091 }
5092 die->die_mark = ++(*mark);
5093
5094 CHECKSUM (die->die_tag);
5095
5096 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5097 attr_checksum (a, ctx, mark);
5098
5099 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5100 }
5101
5102 #undef CHECKSUM
5103 #undef CHECKSUM_STRING
5104
5105 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5106 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5107 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5108 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5109 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5110 #define CHECKSUM_ATTR(FOO) \
5111 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5112
5113 /* Calculate the checksum of a number in signed LEB128 format. */
5114
5115 static void
5116 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5117 {
5118 unsigned char byte;
5119 bool more;
5120
5121 while (1)
5122 {
5123 byte = (value & 0x7f);
5124 value >>= 7;
5125 more = !((value == 0 && (byte & 0x40) == 0)
5126 || (value == -1 && (byte & 0x40) != 0));
5127 if (more)
5128 byte |= 0x80;
5129 CHECKSUM (byte);
5130 if (!more)
5131 break;
5132 }
5133 }
5134
5135 /* Calculate the checksum of a number in unsigned LEB128 format. */
5136
5137 static void
5138 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5139 {
5140 while (1)
5141 {
5142 unsigned char byte = (value & 0x7f);
5143 value >>= 7;
5144 if (value != 0)
5145 /* More bytes to follow. */
5146 byte |= 0x80;
5147 CHECKSUM (byte);
5148 if (value == 0)
5149 break;
5150 }
5151 }
5152
5153 /* Checksum the context of the DIE. This adds the names of any
5154 surrounding namespaces or structures to the checksum. */
5155
5156 static void
5157 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5158 {
5159 const char *name;
5160 dw_die_ref spec;
5161 int tag = die->die_tag;
5162
5163 if (tag != DW_TAG_namespace
5164 && tag != DW_TAG_structure_type
5165 && tag != DW_TAG_class_type)
5166 return;
5167
5168 name = get_AT_string (die, DW_AT_name);
5169
5170 spec = get_AT_ref (die, DW_AT_specification);
5171 if (spec != NULL)
5172 die = spec;
5173
5174 if (die->die_parent != NULL)
5175 checksum_die_context (die->die_parent, ctx);
5176
5177 CHECKSUM_ULEB128 ('C');
5178 CHECKSUM_ULEB128 (tag);
5179 if (name != NULL)
5180 CHECKSUM_STRING (name);
5181 }
5182
5183 /* Calculate the checksum of a location expression. */
5184
5185 static inline void
5186 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5187 {
5188 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5189 were emitted as a DW_FORM_sdata instead of a location expression. */
5190 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5191 {
5192 CHECKSUM_ULEB128 (DW_FORM_sdata);
5193 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5194 return;
5195 }
5196
5197 /* Otherwise, just checksum the raw location expression. */
5198 while (loc != NULL)
5199 {
5200 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5201 CHECKSUM (loc->dw_loc_oprnd1);
5202 CHECKSUM (loc->dw_loc_oprnd2);
5203 loc = loc->dw_loc_next;
5204 }
5205 }
5206
5207 /* Calculate the checksum of an attribute. */
5208
5209 static void
5210 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5211 struct md5_ctx *ctx, int *mark)
5212 {
5213 dw_loc_descr_ref loc;
5214 rtx r;
5215
5216 if (AT_class (at) == dw_val_class_die_ref)
5217 {
5218 dw_die_ref target_die = AT_ref (at);
5219
5220 /* For pointer and reference types, we checksum only the (qualified)
5221 name of the target type (if there is a name). For friend entries,
5222 we checksum only the (qualified) name of the target type or function.
5223 This allows the checksum to remain the same whether the target type
5224 is complete or not. */
5225 if ((at->dw_attr == DW_AT_type
5226 && (tag == DW_TAG_pointer_type
5227 || tag == DW_TAG_reference_type
5228 || tag == DW_TAG_rvalue_reference_type
5229 || tag == DW_TAG_ptr_to_member_type))
5230 || (at->dw_attr == DW_AT_friend
5231 && tag == DW_TAG_friend))
5232 {
5233 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5234
5235 if (name_attr != NULL)
5236 {
5237 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5238
5239 if (decl == NULL)
5240 decl = target_die;
5241 CHECKSUM_ULEB128 ('N');
5242 CHECKSUM_ULEB128 (at->dw_attr);
5243 if (decl->die_parent != NULL)
5244 checksum_die_context (decl->die_parent, ctx);
5245 CHECKSUM_ULEB128 ('E');
5246 CHECKSUM_STRING (AT_string (name_attr));
5247 return;
5248 }
5249 }
5250
5251 /* For all other references to another DIE, we check to see if the
5252 target DIE has already been visited. If it has, we emit a
5253 backward reference; if not, we descend recursively. */
5254 if (target_die->die_mark > 0)
5255 {
5256 CHECKSUM_ULEB128 ('R');
5257 CHECKSUM_ULEB128 (at->dw_attr);
5258 CHECKSUM_ULEB128 (target_die->die_mark);
5259 }
5260 else
5261 {
5262 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5263
5264 if (decl == NULL)
5265 decl = target_die;
5266 target_die->die_mark = ++(*mark);
5267 CHECKSUM_ULEB128 ('T');
5268 CHECKSUM_ULEB128 (at->dw_attr);
5269 if (decl->die_parent != NULL)
5270 checksum_die_context (decl->die_parent, ctx);
5271 die_checksum_ordered (target_die, ctx, mark);
5272 }
5273 return;
5274 }
5275
5276 CHECKSUM_ULEB128 ('A');
5277 CHECKSUM_ULEB128 (at->dw_attr);
5278
5279 switch (AT_class (at))
5280 {
5281 case dw_val_class_const:
5282 CHECKSUM_ULEB128 (DW_FORM_sdata);
5283 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5284 break;
5285
5286 case dw_val_class_unsigned_const:
5287 CHECKSUM_ULEB128 (DW_FORM_sdata);
5288 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5289 break;
5290
5291 case dw_val_class_const_double:
5292 CHECKSUM_ULEB128 (DW_FORM_block);
5293 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5294 CHECKSUM (at->dw_attr_val.v.val_double);
5295 break;
5296
5297 case dw_val_class_vec:
5298 CHECKSUM_ULEB128 (DW_FORM_block);
5299 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5300 CHECKSUM (at->dw_attr_val.v.val_vec);
5301 break;
5302
5303 case dw_val_class_flag:
5304 CHECKSUM_ULEB128 (DW_FORM_flag);
5305 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5306 break;
5307
5308 case dw_val_class_str:
5309 CHECKSUM_ULEB128 (DW_FORM_string);
5310 CHECKSUM_STRING (AT_string (at));
5311 break;
5312
5313 case dw_val_class_addr:
5314 r = AT_addr (at);
5315 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5316 CHECKSUM_ULEB128 (DW_FORM_string);
5317 CHECKSUM_STRING (XSTR (r, 0));
5318 break;
5319
5320 case dw_val_class_offset:
5321 CHECKSUM_ULEB128 (DW_FORM_sdata);
5322 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5323 break;
5324
5325 case dw_val_class_loc:
5326 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5327 loc_checksum_ordered (loc, ctx);
5328 break;
5329
5330 case dw_val_class_fde_ref:
5331 case dw_val_class_lbl_id:
5332 case dw_val_class_lineptr:
5333 case dw_val_class_macptr:
5334 case dw_val_class_high_pc:
5335 break;
5336
5337 case dw_val_class_file:
5338 CHECKSUM_ULEB128 (DW_FORM_string);
5339 CHECKSUM_STRING (AT_file (at)->filename);
5340 break;
5341
5342 case dw_val_class_data8:
5343 CHECKSUM (at->dw_attr_val.v.val_data8);
5344 break;
5345
5346 default:
5347 break;
5348 }
5349 }
5350
5351 struct checksum_attributes
5352 {
5353 dw_attr_ref at_name;
5354 dw_attr_ref at_type;
5355 dw_attr_ref at_friend;
5356 dw_attr_ref at_accessibility;
5357 dw_attr_ref at_address_class;
5358 dw_attr_ref at_allocated;
5359 dw_attr_ref at_artificial;
5360 dw_attr_ref at_associated;
5361 dw_attr_ref at_binary_scale;
5362 dw_attr_ref at_bit_offset;
5363 dw_attr_ref at_bit_size;
5364 dw_attr_ref at_bit_stride;
5365 dw_attr_ref at_byte_size;
5366 dw_attr_ref at_byte_stride;
5367 dw_attr_ref at_const_value;
5368 dw_attr_ref at_containing_type;
5369 dw_attr_ref at_count;
5370 dw_attr_ref at_data_location;
5371 dw_attr_ref at_data_member_location;
5372 dw_attr_ref at_decimal_scale;
5373 dw_attr_ref at_decimal_sign;
5374 dw_attr_ref at_default_value;
5375 dw_attr_ref at_digit_count;
5376 dw_attr_ref at_discr;
5377 dw_attr_ref at_discr_list;
5378 dw_attr_ref at_discr_value;
5379 dw_attr_ref at_encoding;
5380 dw_attr_ref at_endianity;
5381 dw_attr_ref at_explicit;
5382 dw_attr_ref at_is_optional;
5383 dw_attr_ref at_location;
5384 dw_attr_ref at_lower_bound;
5385 dw_attr_ref at_mutable;
5386 dw_attr_ref at_ordering;
5387 dw_attr_ref at_picture_string;
5388 dw_attr_ref at_prototyped;
5389 dw_attr_ref at_small;
5390 dw_attr_ref at_segment;
5391 dw_attr_ref at_string_length;
5392 dw_attr_ref at_threads_scaled;
5393 dw_attr_ref at_upper_bound;
5394 dw_attr_ref at_use_location;
5395 dw_attr_ref at_use_UTF8;
5396 dw_attr_ref at_variable_parameter;
5397 dw_attr_ref at_virtuality;
5398 dw_attr_ref at_visibility;
5399 dw_attr_ref at_vtable_elem_location;
5400 };
5401
5402 /* Collect the attributes that we will want to use for the checksum. */
5403
5404 static void
5405 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5406 {
5407 dw_attr_ref a;
5408 unsigned ix;
5409
5410 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5411 {
5412 switch (a->dw_attr)
5413 {
5414 case DW_AT_name:
5415 attrs->at_name = a;
5416 break;
5417 case DW_AT_type:
5418 attrs->at_type = a;
5419 break;
5420 case DW_AT_friend:
5421 attrs->at_friend = a;
5422 break;
5423 case DW_AT_accessibility:
5424 attrs->at_accessibility = a;
5425 break;
5426 case DW_AT_address_class:
5427 attrs->at_address_class = a;
5428 break;
5429 case DW_AT_allocated:
5430 attrs->at_allocated = a;
5431 break;
5432 case DW_AT_artificial:
5433 attrs->at_artificial = a;
5434 break;
5435 case DW_AT_associated:
5436 attrs->at_associated = a;
5437 break;
5438 case DW_AT_binary_scale:
5439 attrs->at_binary_scale = a;
5440 break;
5441 case DW_AT_bit_offset:
5442 attrs->at_bit_offset = a;
5443 break;
5444 case DW_AT_bit_size:
5445 attrs->at_bit_size = a;
5446 break;
5447 case DW_AT_bit_stride:
5448 attrs->at_bit_stride = a;
5449 break;
5450 case DW_AT_byte_size:
5451 attrs->at_byte_size = a;
5452 break;
5453 case DW_AT_byte_stride:
5454 attrs->at_byte_stride = a;
5455 break;
5456 case DW_AT_const_value:
5457 attrs->at_const_value = a;
5458 break;
5459 case DW_AT_containing_type:
5460 attrs->at_containing_type = a;
5461 break;
5462 case DW_AT_count:
5463 attrs->at_count = a;
5464 break;
5465 case DW_AT_data_location:
5466 attrs->at_data_location = a;
5467 break;
5468 case DW_AT_data_member_location:
5469 attrs->at_data_member_location = a;
5470 break;
5471 case DW_AT_decimal_scale:
5472 attrs->at_decimal_scale = a;
5473 break;
5474 case DW_AT_decimal_sign:
5475 attrs->at_decimal_sign = a;
5476 break;
5477 case DW_AT_default_value:
5478 attrs->at_default_value = a;
5479 break;
5480 case DW_AT_digit_count:
5481 attrs->at_digit_count = a;
5482 break;
5483 case DW_AT_discr:
5484 attrs->at_discr = a;
5485 break;
5486 case DW_AT_discr_list:
5487 attrs->at_discr_list = a;
5488 break;
5489 case DW_AT_discr_value:
5490 attrs->at_discr_value = a;
5491 break;
5492 case DW_AT_encoding:
5493 attrs->at_encoding = a;
5494 break;
5495 case DW_AT_endianity:
5496 attrs->at_endianity = a;
5497 break;
5498 case DW_AT_explicit:
5499 attrs->at_explicit = a;
5500 break;
5501 case DW_AT_is_optional:
5502 attrs->at_is_optional = a;
5503 break;
5504 case DW_AT_location:
5505 attrs->at_location = a;
5506 break;
5507 case DW_AT_lower_bound:
5508 attrs->at_lower_bound = a;
5509 break;
5510 case DW_AT_mutable:
5511 attrs->at_mutable = a;
5512 break;
5513 case DW_AT_ordering:
5514 attrs->at_ordering = a;
5515 break;
5516 case DW_AT_picture_string:
5517 attrs->at_picture_string = a;
5518 break;
5519 case DW_AT_prototyped:
5520 attrs->at_prototyped = a;
5521 break;
5522 case DW_AT_small:
5523 attrs->at_small = a;
5524 break;
5525 case DW_AT_segment:
5526 attrs->at_segment = a;
5527 break;
5528 case DW_AT_string_length:
5529 attrs->at_string_length = a;
5530 break;
5531 case DW_AT_threads_scaled:
5532 attrs->at_threads_scaled = a;
5533 break;
5534 case DW_AT_upper_bound:
5535 attrs->at_upper_bound = a;
5536 break;
5537 case DW_AT_use_location:
5538 attrs->at_use_location = a;
5539 break;
5540 case DW_AT_use_UTF8:
5541 attrs->at_use_UTF8 = a;
5542 break;
5543 case DW_AT_variable_parameter:
5544 attrs->at_variable_parameter = a;
5545 break;
5546 case DW_AT_virtuality:
5547 attrs->at_virtuality = a;
5548 break;
5549 case DW_AT_visibility:
5550 attrs->at_visibility = a;
5551 break;
5552 case DW_AT_vtable_elem_location:
5553 attrs->at_vtable_elem_location = a;
5554 break;
5555 default:
5556 break;
5557 }
5558 }
5559 }
5560
5561 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
5562
5563 static void
5564 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5565 {
5566 dw_die_ref c;
5567 dw_die_ref decl;
5568 struct checksum_attributes attrs;
5569
5570 CHECKSUM_ULEB128 ('D');
5571 CHECKSUM_ULEB128 (die->die_tag);
5572
5573 memset (&attrs, 0, sizeof (attrs));
5574
5575 decl = get_AT_ref (die, DW_AT_specification);
5576 if (decl != NULL)
5577 collect_checksum_attributes (&attrs, decl);
5578 collect_checksum_attributes (&attrs, die);
5579
5580 CHECKSUM_ATTR (attrs.at_name);
5581 CHECKSUM_ATTR (attrs.at_accessibility);
5582 CHECKSUM_ATTR (attrs.at_address_class);
5583 CHECKSUM_ATTR (attrs.at_allocated);
5584 CHECKSUM_ATTR (attrs.at_artificial);
5585 CHECKSUM_ATTR (attrs.at_associated);
5586 CHECKSUM_ATTR (attrs.at_binary_scale);
5587 CHECKSUM_ATTR (attrs.at_bit_offset);
5588 CHECKSUM_ATTR (attrs.at_bit_size);
5589 CHECKSUM_ATTR (attrs.at_bit_stride);
5590 CHECKSUM_ATTR (attrs.at_byte_size);
5591 CHECKSUM_ATTR (attrs.at_byte_stride);
5592 CHECKSUM_ATTR (attrs.at_const_value);
5593 CHECKSUM_ATTR (attrs.at_containing_type);
5594 CHECKSUM_ATTR (attrs.at_count);
5595 CHECKSUM_ATTR (attrs.at_data_location);
5596 CHECKSUM_ATTR (attrs.at_data_member_location);
5597 CHECKSUM_ATTR (attrs.at_decimal_scale);
5598 CHECKSUM_ATTR (attrs.at_decimal_sign);
5599 CHECKSUM_ATTR (attrs.at_default_value);
5600 CHECKSUM_ATTR (attrs.at_digit_count);
5601 CHECKSUM_ATTR (attrs.at_discr);
5602 CHECKSUM_ATTR (attrs.at_discr_list);
5603 CHECKSUM_ATTR (attrs.at_discr_value);
5604 CHECKSUM_ATTR (attrs.at_encoding);
5605 CHECKSUM_ATTR (attrs.at_endianity);
5606 CHECKSUM_ATTR (attrs.at_explicit);
5607 CHECKSUM_ATTR (attrs.at_is_optional);
5608 CHECKSUM_ATTR (attrs.at_location);
5609 CHECKSUM_ATTR (attrs.at_lower_bound);
5610 CHECKSUM_ATTR (attrs.at_mutable);
5611 CHECKSUM_ATTR (attrs.at_ordering);
5612 CHECKSUM_ATTR (attrs.at_picture_string);
5613 CHECKSUM_ATTR (attrs.at_prototyped);
5614 CHECKSUM_ATTR (attrs.at_small);
5615 CHECKSUM_ATTR (attrs.at_segment);
5616 CHECKSUM_ATTR (attrs.at_string_length);
5617 CHECKSUM_ATTR (attrs.at_threads_scaled);
5618 CHECKSUM_ATTR (attrs.at_upper_bound);
5619 CHECKSUM_ATTR (attrs.at_use_location);
5620 CHECKSUM_ATTR (attrs.at_use_UTF8);
5621 CHECKSUM_ATTR (attrs.at_variable_parameter);
5622 CHECKSUM_ATTR (attrs.at_virtuality);
5623 CHECKSUM_ATTR (attrs.at_visibility);
5624 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
5625 CHECKSUM_ATTR (attrs.at_type);
5626 CHECKSUM_ATTR (attrs.at_friend);
5627
5628 /* Checksum the child DIEs, except for nested types and member functions. */
5629 c = die->die_child;
5630 if (c) do {
5631 dw_attr_ref name_attr;
5632
5633 c = c->die_sib;
5634 name_attr = get_AT (c, DW_AT_name);
5635 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
5636 && name_attr != NULL)
5637 {
5638 CHECKSUM_ULEB128 ('S');
5639 CHECKSUM_ULEB128 (c->die_tag);
5640 CHECKSUM_STRING (AT_string (name_attr));
5641 }
5642 else
5643 {
5644 /* Mark this DIE so it gets processed when unmarking. */
5645 if (c->die_mark == 0)
5646 c->die_mark = -1;
5647 die_checksum_ordered (c, ctx, mark);
5648 }
5649 } while (c != die->die_child);
5650
5651 CHECKSUM_ULEB128 (0);
5652 }
5653
5654 #undef CHECKSUM
5655 #undef CHECKSUM_STRING
5656 #undef CHECKSUM_ATTR
5657 #undef CHECKSUM_LEB128
5658 #undef CHECKSUM_ULEB128
5659
5660 /* Generate the type signature for DIE. This is computed by generating an
5661 MD5 checksum over the DIE's tag, its relevant attributes, and its
5662 children. Attributes that are references to other DIEs are processed
5663 by recursion, using the MARK field to prevent infinite recursion.
5664 If the DIE is nested inside a namespace or another type, we also
5665 need to include that context in the signature. The lower 64 bits
5666 of the resulting MD5 checksum comprise the signature. */
5667
5668 static void
5669 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
5670 {
5671 int mark;
5672 const char *name;
5673 unsigned char checksum[16];
5674 struct md5_ctx ctx;
5675 dw_die_ref decl;
5676 dw_die_ref parent;
5677
5678 name = get_AT_string (die, DW_AT_name);
5679 decl = get_AT_ref (die, DW_AT_specification);
5680 parent = get_die_parent (die);
5681
5682 /* First, compute a signature for just the type name (and its surrounding
5683 context, if any. This is stored in the type unit DIE for link-time
5684 ODR (one-definition rule) checking. */
5685
5686 if (is_cxx() && name != NULL)
5687 {
5688 md5_init_ctx (&ctx);
5689
5690 /* Checksum the names of surrounding namespaces and structures. */
5691 if (parent != NULL)
5692 checksum_die_context (parent, &ctx);
5693
5694 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
5695 md5_process_bytes (name, strlen (name) + 1, &ctx);
5696 md5_finish_ctx (&ctx, checksum);
5697
5698 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
5699 }
5700
5701 /* Next, compute the complete type signature. */
5702
5703 md5_init_ctx (&ctx);
5704 mark = 1;
5705 die->die_mark = mark;
5706
5707 /* Checksum the names of surrounding namespaces and structures. */
5708 if (parent != NULL)
5709 checksum_die_context (parent, &ctx);
5710
5711 /* Checksum the DIE and its children. */
5712 die_checksum_ordered (die, &ctx, &mark);
5713 unmark_all_dies (die);
5714 md5_finish_ctx (&ctx, checksum);
5715
5716 /* Store the signature in the type node and link the type DIE and the
5717 type node together. */
5718 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
5719 DWARF_TYPE_SIGNATURE_SIZE);
5720 die->comdat_type_p = true;
5721 die->die_id.die_type_node = type_node;
5722 type_node->type_die = die;
5723
5724 /* If the DIE is a specification, link its declaration to the type node
5725 as well. */
5726 if (decl != NULL)
5727 {
5728 decl->comdat_type_p = true;
5729 decl->die_id.die_type_node = type_node;
5730 }
5731 }
5732
5733 /* Do the location expressions look same? */
5734 static inline int
5735 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5736 {
5737 return loc1->dw_loc_opc == loc2->dw_loc_opc
5738 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5739 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5740 }
5741
5742 /* Do the values look the same? */
5743 static int
5744 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
5745 {
5746 dw_loc_descr_ref loc1, loc2;
5747 rtx r1, r2;
5748
5749 if (v1->val_class != v2->val_class)
5750 return 0;
5751
5752 switch (v1->val_class)
5753 {
5754 case dw_val_class_const:
5755 return v1->v.val_int == v2->v.val_int;
5756 case dw_val_class_unsigned_const:
5757 return v1->v.val_unsigned == v2->v.val_unsigned;
5758 case dw_val_class_const_double:
5759 return v1->v.val_double.high == v2->v.val_double.high
5760 && v1->v.val_double.low == v2->v.val_double.low;
5761 case dw_val_class_vec:
5762 if (v1->v.val_vec.length != v2->v.val_vec.length
5763 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
5764 return 0;
5765 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
5766 v1->v.val_vec.length * v1->v.val_vec.elt_size))
5767 return 0;
5768 return 1;
5769 case dw_val_class_flag:
5770 return v1->v.val_flag == v2->v.val_flag;
5771 case dw_val_class_str:
5772 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
5773
5774 case dw_val_class_addr:
5775 r1 = v1->v.val_addr;
5776 r2 = v2->v.val_addr;
5777 if (GET_CODE (r1) != GET_CODE (r2))
5778 return 0;
5779 return !rtx_equal_p (r1, r2);
5780
5781 case dw_val_class_offset:
5782 return v1->v.val_offset == v2->v.val_offset;
5783
5784 case dw_val_class_loc:
5785 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
5786 loc1 && loc2;
5787 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
5788 if (!same_loc_p (loc1, loc2, mark))
5789 return 0;
5790 return !loc1 && !loc2;
5791
5792 case dw_val_class_die_ref:
5793 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
5794
5795 case dw_val_class_fde_ref:
5796 case dw_val_class_vms_delta:
5797 case dw_val_class_lbl_id:
5798 case dw_val_class_lineptr:
5799 case dw_val_class_macptr:
5800 case dw_val_class_high_pc:
5801 return 1;
5802
5803 case dw_val_class_file:
5804 return v1->v.val_file == v2->v.val_file;
5805
5806 case dw_val_class_data8:
5807 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
5808
5809 default:
5810 return 1;
5811 }
5812 }
5813
5814 /* Do the attributes look the same? */
5815
5816 static int
5817 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
5818 {
5819 if (at1->dw_attr != at2->dw_attr)
5820 return 0;
5821
5822 /* We don't care that this was compiled with a different compiler
5823 snapshot; if the output is the same, that's what matters. */
5824 if (at1->dw_attr == DW_AT_producer)
5825 return 1;
5826
5827 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
5828 }
5829
5830 /* Do the dies look the same? */
5831
5832 static int
5833 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
5834 {
5835 dw_die_ref c1, c2;
5836 dw_attr_ref a1;
5837 unsigned ix;
5838
5839 /* To avoid infinite recursion. */
5840 if (die1->die_mark)
5841 return die1->die_mark == die2->die_mark;
5842 die1->die_mark = die2->die_mark = ++(*mark);
5843
5844 if (die1->die_tag != die2->die_tag)
5845 return 0;
5846
5847 if (VEC_length (dw_attr_node, die1->die_attr)
5848 != VEC_length (dw_attr_node, die2->die_attr))
5849 return 0;
5850
5851 FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
5852 if (!same_attr_p (a1, &VEC_index (dw_attr_node, die2->die_attr, ix), mark))
5853 return 0;
5854
5855 c1 = die1->die_child;
5856 c2 = die2->die_child;
5857 if (! c1)
5858 {
5859 if (c2)
5860 return 0;
5861 }
5862 else
5863 for (;;)
5864 {
5865 if (!same_die_p (c1, c2, mark))
5866 return 0;
5867 c1 = c1->die_sib;
5868 c2 = c2->die_sib;
5869 if (c1 == die1->die_child)
5870 {
5871 if (c2 == die2->die_child)
5872 break;
5873 else
5874 return 0;
5875 }
5876 }
5877
5878 return 1;
5879 }
5880
5881 /* Do the dies look the same? Wrapper around same_die_p. */
5882
5883 static int
5884 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
5885 {
5886 int mark = 0;
5887 int ret = same_die_p (die1, die2, &mark);
5888
5889 unmark_all_dies (die1);
5890 unmark_all_dies (die2);
5891
5892 return ret;
5893 }
5894
5895 /* The prefix to attach to symbols on DIEs in the current comdat debug
5896 info section. */
5897 static const char *comdat_symbol_id;
5898
5899 /* The index of the current symbol within the current comdat CU. */
5900 static unsigned int comdat_symbol_number;
5901
5902 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5903 children, and set comdat_symbol_id accordingly. */
5904
5905 static void
5906 compute_section_prefix (dw_die_ref unit_die)
5907 {
5908 const char *die_name = get_AT_string (unit_die, DW_AT_name);
5909 const char *base = die_name ? lbasename (die_name) : "anonymous";
5910 char *name = XALLOCAVEC (char, strlen (base) + 64);
5911 char *p;
5912 int i, mark;
5913 unsigned char checksum[16];
5914 struct md5_ctx ctx;
5915
5916 /* Compute the checksum of the DIE, then append part of it as hex digits to
5917 the name filename of the unit. */
5918
5919 md5_init_ctx (&ctx);
5920 mark = 0;
5921 die_checksum (unit_die, &ctx, &mark);
5922 unmark_all_dies (unit_die);
5923 md5_finish_ctx (&ctx, checksum);
5924
5925 sprintf (name, "%s.", base);
5926 clean_symbol_name (name);
5927
5928 p = name + strlen (name);
5929 for (i = 0; i < 4; i++)
5930 {
5931 sprintf (p, "%.2x", checksum[i]);
5932 p += 2;
5933 }
5934
5935 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
5936 comdat_symbol_number = 0;
5937 }
5938
5939 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
5940
5941 static int
5942 is_type_die (dw_die_ref die)
5943 {
5944 switch (die->die_tag)
5945 {
5946 case DW_TAG_array_type:
5947 case DW_TAG_class_type:
5948 case DW_TAG_interface_type:
5949 case DW_TAG_enumeration_type:
5950 case DW_TAG_pointer_type:
5951 case DW_TAG_reference_type:
5952 case DW_TAG_rvalue_reference_type:
5953 case DW_TAG_string_type:
5954 case DW_TAG_structure_type:
5955 case DW_TAG_subroutine_type:
5956 case DW_TAG_union_type:
5957 case DW_TAG_ptr_to_member_type:
5958 case DW_TAG_set_type:
5959 case DW_TAG_subrange_type:
5960 case DW_TAG_base_type:
5961 case DW_TAG_const_type:
5962 case DW_TAG_file_type:
5963 case DW_TAG_packed_type:
5964 case DW_TAG_volatile_type:
5965 case DW_TAG_typedef:
5966 return 1;
5967 default:
5968 return 0;
5969 }
5970 }
5971
5972 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
5973 Basically, we want to choose the bits that are likely to be shared between
5974 compilations (types) and leave out the bits that are specific to individual
5975 compilations (functions). */
5976
5977 static int
5978 is_comdat_die (dw_die_ref c)
5979 {
5980 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
5981 we do for stabs. The advantage is a greater likelihood of sharing between
5982 objects that don't include headers in the same order (and therefore would
5983 put the base types in a different comdat). jason 8/28/00 */
5984
5985 if (c->die_tag == DW_TAG_base_type)
5986 return 0;
5987
5988 if (c->die_tag == DW_TAG_pointer_type
5989 || c->die_tag == DW_TAG_reference_type
5990 || c->die_tag == DW_TAG_rvalue_reference_type
5991 || c->die_tag == DW_TAG_const_type
5992 || c->die_tag == DW_TAG_volatile_type)
5993 {
5994 dw_die_ref t = get_AT_ref (c, DW_AT_type);
5995
5996 return t ? is_comdat_die (t) : 0;
5997 }
5998
5999 return is_type_die (c);
6000 }
6001
6002 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6003 compilation unit. */
6004
6005 static int
6006 is_symbol_die (dw_die_ref c)
6007 {
6008 return (is_type_die (c)
6009 || is_declaration_die (c)
6010 || c->die_tag == DW_TAG_namespace
6011 || c->die_tag == DW_TAG_module);
6012 }
6013
6014 /* Returns true iff C is a compile-unit DIE. */
6015
6016 static inline bool
6017 is_cu_die (dw_die_ref c)
6018 {
6019 return c && c->die_tag == DW_TAG_compile_unit;
6020 }
6021
6022 /* Returns true iff C is a unit DIE of some sort. */
6023
6024 static inline bool
6025 is_unit_die (dw_die_ref c)
6026 {
6027 return c && (c->die_tag == DW_TAG_compile_unit
6028 || c->die_tag == DW_TAG_partial_unit
6029 || c->die_tag == DW_TAG_type_unit);
6030 }
6031
6032 /* Returns true iff C is a namespace DIE. */
6033
6034 static inline bool
6035 is_namespace_die (dw_die_ref c)
6036 {
6037 return c && c->die_tag == DW_TAG_namespace;
6038 }
6039
6040 /* Returns true iff C is a class or structure DIE. */
6041
6042 static inline bool
6043 is_class_die (dw_die_ref c)
6044 {
6045 return c && (c->die_tag == DW_TAG_class_type
6046 || c->die_tag == DW_TAG_structure_type);
6047 }
6048
6049 static char *
6050 gen_internal_sym (const char *prefix)
6051 {
6052 char buf[256];
6053
6054 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6055 return xstrdup (buf);
6056 }
6057
6058 /* Assign symbols to all worthy DIEs under DIE. */
6059
6060 static void
6061 assign_symbol_names (dw_die_ref die)
6062 {
6063 dw_die_ref c;
6064
6065 if (is_symbol_die (die) && !die->comdat_type_p)
6066 {
6067 if (comdat_symbol_id)
6068 {
6069 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6070
6071 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6072 comdat_symbol_id, comdat_symbol_number++);
6073 die->die_id.die_symbol = xstrdup (p);
6074 }
6075 else
6076 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6077 }
6078
6079 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6080 }
6081
6082 struct cu_hash_table_entry
6083 {
6084 dw_die_ref cu;
6085 unsigned min_comdat_num, max_comdat_num;
6086 struct cu_hash_table_entry *next;
6087 };
6088
6089 /* Routines to manipulate hash table of CUs. */
6090 static hashval_t
6091 htab_cu_hash (const void *of)
6092 {
6093 const struct cu_hash_table_entry *const entry =
6094 (const struct cu_hash_table_entry *) of;
6095
6096 return htab_hash_string (entry->cu->die_id.die_symbol);
6097 }
6098
6099 static int
6100 htab_cu_eq (const void *of1, const void *of2)
6101 {
6102 const struct cu_hash_table_entry *const entry1 =
6103 (const struct cu_hash_table_entry *) of1;
6104 const struct die_struct *const entry2 = (const struct die_struct *) of2;
6105
6106 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6107 }
6108
6109 static void
6110 htab_cu_del (void *what)
6111 {
6112 struct cu_hash_table_entry *next,
6113 *entry = (struct cu_hash_table_entry *) what;
6114
6115 while (entry)
6116 {
6117 next = entry->next;
6118 free (entry);
6119 entry = next;
6120 }
6121 }
6122
6123 /* Check whether we have already seen this CU and set up SYM_NUM
6124 accordingly. */
6125 static int
6126 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6127 {
6128 struct cu_hash_table_entry dummy;
6129 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6130
6131 dummy.max_comdat_num = 0;
6132
6133 slot = (struct cu_hash_table_entry **)
6134 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6135 INSERT);
6136 entry = *slot;
6137
6138 for (; entry; last = entry, entry = entry->next)
6139 {
6140 if (same_die_p_wrap (cu, entry->cu))
6141 break;
6142 }
6143
6144 if (entry)
6145 {
6146 *sym_num = entry->min_comdat_num;
6147 return 1;
6148 }
6149
6150 entry = XCNEW (struct cu_hash_table_entry);
6151 entry->cu = cu;
6152 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6153 entry->next = *slot;
6154 *slot = entry;
6155
6156 return 0;
6157 }
6158
6159 /* Record SYM_NUM to record of CU in HTABLE. */
6160 static void
6161 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6162 {
6163 struct cu_hash_table_entry **slot, *entry;
6164
6165 slot = (struct cu_hash_table_entry **)
6166 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6167 NO_INSERT);
6168 entry = *slot;
6169
6170 entry->max_comdat_num = sym_num;
6171 }
6172
6173 /* Traverse the DIE (which is always comp_unit_die), and set up
6174 additional compilation units for each of the include files we see
6175 bracketed by BINCL/EINCL. */
6176
6177 static void
6178 break_out_includes (dw_die_ref die)
6179 {
6180 dw_die_ref c;
6181 dw_die_ref unit = NULL;
6182 limbo_die_node *node, **pnode;
6183 htab_t cu_hash_table;
6184
6185 c = die->die_child;
6186 if (c) do {
6187 dw_die_ref prev = c;
6188 c = c->die_sib;
6189 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6190 || (unit && is_comdat_die (c)))
6191 {
6192 dw_die_ref next = c->die_sib;
6193
6194 /* This DIE is for a secondary CU; remove it from the main one. */
6195 remove_child_with_prev (c, prev);
6196
6197 if (c->die_tag == DW_TAG_GNU_BINCL)
6198 unit = push_new_compile_unit (unit, c);
6199 else if (c->die_tag == DW_TAG_GNU_EINCL)
6200 unit = pop_compile_unit (unit);
6201 else
6202 add_child_die (unit, c);
6203 c = next;
6204 if (c == die->die_child)
6205 break;
6206 }
6207 } while (c != die->die_child);
6208
6209 #if 0
6210 /* We can only use this in debugging, since the frontend doesn't check
6211 to make sure that we leave every include file we enter. */
6212 gcc_assert (!unit);
6213 #endif
6214
6215 assign_symbol_names (die);
6216 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6217 for (node = limbo_die_list, pnode = &limbo_die_list;
6218 node;
6219 node = node->next)
6220 {
6221 int is_dupl;
6222
6223 compute_section_prefix (node->die);
6224 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6225 &comdat_symbol_number);
6226 assign_symbol_names (node->die);
6227 if (is_dupl)
6228 *pnode = node->next;
6229 else
6230 {
6231 pnode = &node->next;
6232 record_comdat_symbol_number (node->die, cu_hash_table,
6233 comdat_symbol_number);
6234 }
6235 }
6236 htab_delete (cu_hash_table);
6237 }
6238
6239 /* Return non-zero if this DIE is a declaration. */
6240
6241 static int
6242 is_declaration_die (dw_die_ref die)
6243 {
6244 dw_attr_ref a;
6245 unsigned ix;
6246
6247 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6248 if (a->dw_attr == DW_AT_declaration)
6249 return 1;
6250
6251 return 0;
6252 }
6253
6254 /* Return non-zero if this DIE is nested inside a subprogram. */
6255
6256 static int
6257 is_nested_in_subprogram (dw_die_ref die)
6258 {
6259 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6260
6261 if (decl == NULL)
6262 decl = die;
6263 return local_scope_p (decl);
6264 }
6265
6266 /* Return non-zero if this DIE contains a defining declaration of a
6267 subprogram. */
6268
6269 static int
6270 contains_subprogram_definition (dw_die_ref die)
6271 {
6272 dw_die_ref c;
6273
6274 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6275 return 1;
6276 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6277 return 0;
6278 }
6279
6280 /* Return non-zero if this is a type DIE that should be moved to a
6281 COMDAT .debug_types section. */
6282
6283 static int
6284 should_move_die_to_comdat (dw_die_ref die)
6285 {
6286 switch (die->die_tag)
6287 {
6288 case DW_TAG_class_type:
6289 case DW_TAG_structure_type:
6290 case DW_TAG_enumeration_type:
6291 case DW_TAG_union_type:
6292 /* Don't move declarations, inlined instances, or types nested in a
6293 subprogram. */
6294 if (is_declaration_die (die)
6295 || get_AT (die, DW_AT_abstract_origin)
6296 || is_nested_in_subprogram (die))
6297 return 0;
6298 /* A type definition should never contain a subprogram definition. */
6299 gcc_assert (!contains_subprogram_definition (die));
6300 return 1;
6301 case DW_TAG_array_type:
6302 case DW_TAG_interface_type:
6303 case DW_TAG_pointer_type:
6304 case DW_TAG_reference_type:
6305 case DW_TAG_rvalue_reference_type:
6306 case DW_TAG_string_type:
6307 case DW_TAG_subroutine_type:
6308 case DW_TAG_ptr_to_member_type:
6309 case DW_TAG_set_type:
6310 case DW_TAG_subrange_type:
6311 case DW_TAG_base_type:
6312 case DW_TAG_const_type:
6313 case DW_TAG_file_type:
6314 case DW_TAG_packed_type:
6315 case DW_TAG_volatile_type:
6316 case DW_TAG_typedef:
6317 default:
6318 return 0;
6319 }
6320 }
6321
6322 /* Make a clone of DIE. */
6323
6324 static dw_die_ref
6325 clone_die (dw_die_ref die)
6326 {
6327 dw_die_ref clone;
6328 dw_attr_ref a;
6329 unsigned ix;
6330
6331 clone = ggc_alloc_cleared_die_node ();
6332 clone->die_tag = die->die_tag;
6333
6334 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6335 add_dwarf_attr (clone, a);
6336
6337 return clone;
6338 }
6339
6340 /* Make a clone of the tree rooted at DIE. */
6341
6342 static dw_die_ref
6343 clone_tree (dw_die_ref die)
6344 {
6345 dw_die_ref c;
6346 dw_die_ref clone = clone_die (die);
6347
6348 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6349
6350 return clone;
6351 }
6352
6353 /* Make a clone of DIE as a declaration. */
6354
6355 static dw_die_ref
6356 clone_as_declaration (dw_die_ref die)
6357 {
6358 dw_die_ref clone;
6359 dw_die_ref decl;
6360 dw_attr_ref a;
6361 unsigned ix;
6362
6363 /* If the DIE is already a declaration, just clone it. */
6364 if (is_declaration_die (die))
6365 return clone_die (die);
6366
6367 /* If the DIE is a specification, just clone its declaration DIE. */
6368 decl = get_AT_ref (die, DW_AT_specification);
6369 if (decl != NULL)
6370 {
6371 clone = clone_die (decl);
6372 if (die->comdat_type_p)
6373 add_AT_die_ref (clone, DW_AT_signature, die);
6374 return clone;
6375 }
6376
6377 clone = ggc_alloc_cleared_die_node ();
6378 clone->die_tag = die->die_tag;
6379
6380 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6381 {
6382 /* We don't want to copy over all attributes.
6383 For example we don't want DW_AT_byte_size because otherwise we will no
6384 longer have a declaration and GDB will treat it as a definition. */
6385
6386 switch (a->dw_attr)
6387 {
6388 case DW_AT_artificial:
6389 case DW_AT_containing_type:
6390 case DW_AT_external:
6391 case DW_AT_name:
6392 case DW_AT_type:
6393 case DW_AT_virtuality:
6394 case DW_AT_linkage_name:
6395 case DW_AT_MIPS_linkage_name:
6396 add_dwarf_attr (clone, a);
6397 break;
6398 case DW_AT_byte_size:
6399 default:
6400 break;
6401 }
6402 }
6403
6404 if (die->comdat_type_p)
6405 add_AT_die_ref (clone, DW_AT_signature, die);
6406
6407 add_AT_flag (clone, DW_AT_declaration, 1);
6408 return clone;
6409 }
6410
6411 /* Copy the declaration context to the new type unit DIE. This includes
6412 any surrounding namespace or type declarations. If the DIE has an
6413 AT_specification attribute, it also includes attributes and children
6414 attached to the specification, and returns a pointer to the original
6415 parent of the declaration DIE. Returns NULL otherwise. */
6416
6417 static dw_die_ref
6418 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6419 {
6420 dw_die_ref decl;
6421 dw_die_ref new_decl;
6422 dw_die_ref orig_parent = NULL;
6423
6424 decl = get_AT_ref (die, DW_AT_specification);
6425 if (decl == NULL)
6426 decl = die;
6427 else
6428 {
6429 unsigned ix;
6430 dw_die_ref c;
6431 dw_attr_ref a;
6432
6433 /* The original DIE will be changed to a declaration, and must
6434 be moved to be a child of the original declaration DIE. */
6435 orig_parent = decl->die_parent;
6436
6437 /* Copy the type node pointer from the new DIE to the original
6438 declaration DIE so we can forward references later. */
6439 decl->comdat_type_p = true;
6440 decl->die_id.die_type_node = die->die_id.die_type_node;
6441
6442 remove_AT (die, DW_AT_specification);
6443
6444 FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
6445 {
6446 if (a->dw_attr != DW_AT_name
6447 && a->dw_attr != DW_AT_declaration
6448 && a->dw_attr != DW_AT_external)
6449 add_dwarf_attr (die, a);
6450 }
6451
6452 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
6453 }
6454
6455 if (decl->die_parent != NULL
6456 && !is_unit_die (decl->die_parent))
6457 {
6458 new_decl = copy_ancestor_tree (unit, decl, NULL);
6459 if (new_decl != NULL)
6460 {
6461 remove_AT (new_decl, DW_AT_signature);
6462 add_AT_specification (die, new_decl);
6463 }
6464 }
6465
6466 return orig_parent;
6467 }
6468
6469 /* Generate the skeleton ancestor tree for the given NODE, then clone
6470 the DIE and add the clone into the tree. */
6471
6472 static void
6473 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
6474 {
6475 if (node->new_die != NULL)
6476 return;
6477
6478 node->new_die = clone_as_declaration (node->old_die);
6479
6480 if (node->parent != NULL)
6481 {
6482 generate_skeleton_ancestor_tree (node->parent);
6483 add_child_die (node->parent->new_die, node->new_die);
6484 }
6485 }
6486
6487 /* Generate a skeleton tree of DIEs containing any declarations that are
6488 found in the original tree. We traverse the tree looking for declaration
6489 DIEs, and construct the skeleton from the bottom up whenever we find one. */
6490
6491 static void
6492 generate_skeleton_bottom_up (skeleton_chain_node *parent)
6493 {
6494 skeleton_chain_node node;
6495 dw_die_ref c;
6496 dw_die_ref first;
6497 dw_die_ref prev = NULL;
6498 dw_die_ref next = NULL;
6499
6500 node.parent = parent;
6501
6502 first = c = parent->old_die->die_child;
6503 if (c)
6504 next = c->die_sib;
6505 if (c) do {
6506 if (prev == NULL || prev->die_sib == c)
6507 prev = c;
6508 c = next;
6509 next = (c == first ? NULL : c->die_sib);
6510 node.old_die = c;
6511 node.new_die = NULL;
6512 if (is_declaration_die (c))
6513 {
6514 /* Clone the existing DIE, move the original to the skeleton
6515 tree (which is in the main CU), and put the clone, with
6516 all the original's children, where the original came from. */
6517 dw_die_ref clone = clone_die (c);
6518 move_all_children (c, clone);
6519
6520 replace_child (c, clone, prev);
6521 generate_skeleton_ancestor_tree (parent);
6522 add_child_die (parent->new_die, c);
6523 node.new_die = c;
6524 c = clone;
6525 }
6526 generate_skeleton_bottom_up (&node);
6527 } while (next != NULL);
6528 }
6529
6530 /* Wrapper function for generate_skeleton_bottom_up. */
6531
6532 static dw_die_ref
6533 generate_skeleton (dw_die_ref die)
6534 {
6535 skeleton_chain_node node;
6536
6537 node.old_die = die;
6538 node.new_die = NULL;
6539 node.parent = NULL;
6540
6541 /* If this type definition is nested inside another type,
6542 always leave at least a declaration in its place. */
6543 if (die->die_parent != NULL && is_type_die (die->die_parent))
6544 node.new_die = clone_as_declaration (die);
6545
6546 generate_skeleton_bottom_up (&node);
6547 return node.new_die;
6548 }
6549
6550 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
6551 declaration. The original DIE is moved to a new compile unit so that
6552 existing references to it follow it to the new location. If any of the
6553 original DIE's descendants is a declaration, we need to replace the
6554 original DIE with a skeleton tree and move the declarations back into the
6555 skeleton tree. */
6556
6557 static dw_die_ref
6558 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
6559 dw_die_ref prev)
6560 {
6561 dw_die_ref skeleton, orig_parent;
6562
6563 /* Copy the declaration context to the type unit DIE. If the returned
6564 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
6565 that DIE. */
6566 orig_parent = copy_declaration_context (unit, child);
6567
6568 skeleton = generate_skeleton (child);
6569 if (skeleton == NULL)
6570 remove_child_with_prev (child, prev);
6571 else
6572 {
6573 skeleton->comdat_type_p = true;
6574 skeleton->die_id.die_type_node = child->die_id.die_type_node;
6575
6576 /* If the original DIE was a specification, we need to put
6577 the skeleton under the parent DIE of the declaration.
6578 This leaves the original declaration in the tree, but
6579 it will be pruned later since there are no longer any
6580 references to it. */
6581 if (orig_parent != NULL)
6582 {
6583 remove_child_with_prev (child, prev);
6584 add_child_die (orig_parent, skeleton);
6585 }
6586 else
6587 replace_child (child, skeleton, prev);
6588 }
6589
6590 return skeleton;
6591 }
6592
6593 /* Traverse the DIE and set up additional .debug_types sections for each
6594 type worthy of being placed in a COMDAT section. */
6595
6596 static void
6597 break_out_comdat_types (dw_die_ref die)
6598 {
6599 dw_die_ref c;
6600 dw_die_ref first;
6601 dw_die_ref prev = NULL;
6602 dw_die_ref next = NULL;
6603 dw_die_ref unit = NULL;
6604
6605 first = c = die->die_child;
6606 if (c)
6607 next = c->die_sib;
6608 if (c) do {
6609 if (prev == NULL || prev->die_sib == c)
6610 prev = c;
6611 c = next;
6612 next = (c == first ? NULL : c->die_sib);
6613 if (should_move_die_to_comdat (c))
6614 {
6615 dw_die_ref replacement;
6616 comdat_type_node_ref type_node;
6617
6618 /* Create a new type unit DIE as the root for the new tree, and
6619 add it to the list of comdat types. */
6620 unit = new_die (DW_TAG_type_unit, NULL, NULL);
6621 add_AT_unsigned (unit, DW_AT_language,
6622 get_AT_unsigned (comp_unit_die (), DW_AT_language));
6623 type_node = ggc_alloc_cleared_comdat_type_node ();
6624 type_node->root_die = unit;
6625 type_node->next = comdat_type_list;
6626 comdat_type_list = type_node;
6627
6628 /* Generate the type signature. */
6629 generate_type_signature (c, type_node);
6630
6631 /* Copy the declaration context, attributes, and children of the
6632 declaration into the new type unit DIE, then remove this DIE
6633 from the main CU (or replace it with a skeleton if necessary). */
6634 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
6635 type_node->skeleton_die = replacement;
6636
6637 /* Break out nested types into their own type units. */
6638 break_out_comdat_types (c);
6639
6640 /* Add the DIE to the new compunit. */
6641 add_child_die (unit, c);
6642
6643 if (replacement != NULL)
6644 c = replacement;
6645 }
6646 else if (c->die_tag == DW_TAG_namespace
6647 || c->die_tag == DW_TAG_class_type
6648 || c->die_tag == DW_TAG_structure_type
6649 || c->die_tag == DW_TAG_union_type)
6650 {
6651 /* Look for nested types that can be broken out. */
6652 break_out_comdat_types (c);
6653 }
6654 } while (next != NULL);
6655 }
6656
6657 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6658
6659 struct decl_table_entry
6660 {
6661 dw_die_ref orig;
6662 dw_die_ref copy;
6663 };
6664
6665 /* Routines to manipulate hash table of copied declarations. */
6666
6667 static hashval_t
6668 htab_decl_hash (const void *of)
6669 {
6670 const struct decl_table_entry *const entry =
6671 (const struct decl_table_entry *) of;
6672
6673 return htab_hash_pointer (entry->orig);
6674 }
6675
6676 static int
6677 htab_decl_eq (const void *of1, const void *of2)
6678 {
6679 const struct decl_table_entry *const entry1 =
6680 (const struct decl_table_entry *) of1;
6681 const struct die_struct *const entry2 = (const struct die_struct *) of2;
6682
6683 return entry1->orig == entry2;
6684 }
6685
6686 static void
6687 htab_decl_del (void *what)
6688 {
6689 struct decl_table_entry *entry = (struct decl_table_entry *) what;
6690
6691 free (entry);
6692 }
6693
6694 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6695 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6696 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6697 to check if the ancestor has already been copied into UNIT. */
6698
6699 static dw_die_ref
6700 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
6701 {
6702 dw_die_ref parent = die->die_parent;
6703 dw_die_ref new_parent = unit;
6704 dw_die_ref copy;
6705 void **slot = NULL;
6706 struct decl_table_entry *entry = NULL;
6707
6708 if (decl_table)
6709 {
6710 /* Check if the entry has already been copied to UNIT. */
6711 slot = htab_find_slot_with_hash (decl_table, die,
6712 htab_hash_pointer (die), INSERT);
6713 if (*slot != HTAB_EMPTY_ENTRY)
6714 {
6715 entry = (struct decl_table_entry *) *slot;
6716 return entry->copy;
6717 }
6718
6719 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
6720 entry = XCNEW (struct decl_table_entry);
6721 entry->orig = die;
6722 entry->copy = NULL;
6723 *slot = entry;
6724 }
6725
6726 if (parent != NULL)
6727 {
6728 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
6729 if (spec != NULL)
6730 parent = spec;
6731 if (!is_unit_die (parent))
6732 new_parent = copy_ancestor_tree (unit, parent, decl_table);
6733 }
6734
6735 copy = clone_as_declaration (die);
6736 add_child_die (new_parent, copy);
6737
6738 if (decl_table != NULL)
6739 {
6740 /* Record the pointer to the copy. */
6741 entry->copy = copy;
6742 }
6743
6744 return copy;
6745 }
6746
6747 /* Like clone_tree, but additionally enter all the children into
6748 the hash table decl_table. */
6749
6750 static dw_die_ref
6751 clone_tree_hash (dw_die_ref die, htab_t decl_table)
6752 {
6753 dw_die_ref c;
6754 dw_die_ref clone = clone_die (die);
6755 struct decl_table_entry *entry;
6756 void **slot = htab_find_slot_with_hash (decl_table, die,
6757 htab_hash_pointer (die), INSERT);
6758 /* Assert that DIE isn't in the hash table yet. If it would be there
6759 before, the ancestors would be necessarily there as well, therefore
6760 clone_tree_hash wouldn't be called. */
6761 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
6762 entry = XCNEW (struct decl_table_entry);
6763 entry->orig = die;
6764 entry->copy = clone;
6765 *slot = entry;
6766
6767 FOR_EACH_CHILD (die, c,
6768 add_child_die (clone, clone_tree_hash (c, decl_table)));
6769
6770 return clone;
6771 }
6772
6773 /* Walk the DIE and its children, looking for references to incomplete
6774 or trivial types that are unmarked (i.e., that are not in the current
6775 type_unit). */
6776
6777 static void
6778 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
6779 {
6780 dw_die_ref c;
6781 dw_attr_ref a;
6782 unsigned ix;
6783
6784 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6785 {
6786 if (AT_class (a) == dw_val_class_die_ref)
6787 {
6788 dw_die_ref targ = AT_ref (a);
6789 void **slot;
6790 struct decl_table_entry *entry;
6791
6792 if (targ->die_mark != 0 || targ->comdat_type_p)
6793 continue;
6794
6795 slot = htab_find_slot_with_hash (decl_table, targ,
6796 htab_hash_pointer (targ), INSERT);
6797
6798 if (*slot != HTAB_EMPTY_ENTRY)
6799 {
6800 /* TARG has already been copied, so we just need to
6801 modify the reference to point to the copy. */
6802 entry = (struct decl_table_entry *) *slot;
6803 a->dw_attr_val.v.val_die_ref.die = entry->copy;
6804 }
6805 else
6806 {
6807 dw_die_ref parent = unit;
6808 dw_die_ref copy = clone_die (targ);
6809
6810 /* Record in DECL_TABLE that TARG has been copied.
6811 Need to do this now, before the recursive call,
6812 because DECL_TABLE may be expanded and SLOT
6813 would no longer be a valid pointer. */
6814 entry = XCNEW (struct decl_table_entry);
6815 entry->orig = targ;
6816 entry->copy = copy;
6817 *slot = entry;
6818
6819 FOR_EACH_CHILD (targ, c,
6820 add_child_die (copy,
6821 clone_tree_hash (c, decl_table)));
6822
6823 /* Make sure the cloned tree is marked as part of the
6824 type unit. */
6825 mark_dies (copy);
6826
6827 /* If TARG has surrounding context, copy its ancestor tree
6828 into the new type unit. */
6829 if (targ->die_parent != NULL
6830 && !is_unit_die (targ->die_parent))
6831 parent = copy_ancestor_tree (unit, targ->die_parent,
6832 decl_table);
6833
6834 add_child_die (parent, copy);
6835 a->dw_attr_val.v.val_die_ref.die = copy;
6836
6837 /* Make sure the newly-copied DIE is walked. If it was
6838 installed in a previously-added context, it won't
6839 get visited otherwise. */
6840 if (parent != unit)
6841 {
6842 /* Find the highest point of the newly-added tree,
6843 mark each node along the way, and walk from there. */
6844 parent->die_mark = 1;
6845 while (parent->die_parent
6846 && parent->die_parent->die_mark == 0)
6847 {
6848 parent = parent->die_parent;
6849 parent->die_mark = 1;
6850 }
6851 copy_decls_walk (unit, parent, decl_table);
6852 }
6853 }
6854 }
6855 }
6856
6857 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
6858 }
6859
6860 /* Copy declarations for "unworthy" types into the new comdat section.
6861 Incomplete types, modified types, and certain other types aren't broken
6862 out into comdat sections of their own, so they don't have a signature,
6863 and we need to copy the declaration into the same section so that we
6864 don't have an external reference. */
6865
6866 static void
6867 copy_decls_for_unworthy_types (dw_die_ref unit)
6868 {
6869 htab_t decl_table;
6870
6871 mark_dies (unit);
6872 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
6873 copy_decls_walk (unit, unit, decl_table);
6874 htab_delete (decl_table);
6875 unmark_dies (unit);
6876 }
6877
6878 /* Traverse the DIE and add a sibling attribute if it may have the
6879 effect of speeding up access to siblings. To save some space,
6880 avoid generating sibling attributes for DIE's without children. */
6881
6882 static void
6883 add_sibling_attributes (dw_die_ref die)
6884 {
6885 dw_die_ref c;
6886
6887 if (! die->die_child)
6888 return;
6889
6890 if (die->die_parent && die != die->die_parent->die_child)
6891 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6892
6893 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6894 }
6895
6896 /* Output all location lists for the DIE and its children. */
6897
6898 static void
6899 output_location_lists (dw_die_ref die)
6900 {
6901 dw_die_ref c;
6902 dw_attr_ref a;
6903 unsigned ix;
6904
6905 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6906 if (AT_class (a) == dw_val_class_loc_list)
6907 output_loc_list (AT_loc_list (a));
6908
6909 FOR_EACH_CHILD (die, c, output_location_lists (c));
6910 }
6911
6912 /* We want to limit the number of external references, because they are
6913 larger than local references: a relocation takes multiple words, and
6914 even a sig8 reference is always eight bytes, whereas a local reference
6915 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
6916 So if we encounter multiple external references to the same type DIE, we
6917 make a local typedef stub for it and redirect all references there.
6918
6919 This is the element of the hash table for keeping track of these
6920 references. */
6921
6922 struct external_ref
6923 {
6924 dw_die_ref type;
6925 dw_die_ref stub;
6926 unsigned n_refs;
6927 };
6928
6929 /* Hash an external_ref. */
6930
6931 static hashval_t
6932 hash_external_ref (const void *p)
6933 {
6934 const struct external_ref *r = (const struct external_ref *)p;
6935 return htab_hash_pointer (r->type);
6936 }
6937
6938 /* Compare external_refs. */
6939
6940 static int
6941 external_ref_eq (const void *p1, const void *p2)
6942 {
6943 const struct external_ref *r1 = (const struct external_ref *)p1;
6944 const struct external_ref *r2 = (const struct external_ref *)p2;
6945 return r1->type == r2->type;
6946 }
6947
6948 /* Return a pointer to the external_ref for references to DIE. */
6949
6950 static struct external_ref *
6951 lookup_external_ref (htab_t map, dw_die_ref die)
6952 {
6953 struct external_ref ref, *ref_p;
6954 void ** slot;
6955
6956 ref.type = die;
6957 slot = htab_find_slot (map, &ref, INSERT);
6958 if (*slot != HTAB_EMPTY_ENTRY)
6959 return (struct external_ref *) *slot;
6960
6961 ref_p = XCNEW (struct external_ref);
6962 ref_p->type = die;
6963 *slot = ref_p;
6964 return ref_p;
6965 }
6966
6967 /* Subroutine of optimize_external_refs, below.
6968
6969 If we see a type skeleton, record it as our stub. If we see external
6970 references, remember how many we've seen. */
6971
6972 static void
6973 optimize_external_refs_1 (dw_die_ref die, htab_t map)
6974 {
6975 dw_die_ref c;
6976 dw_attr_ref a;
6977 unsigned ix;
6978 struct external_ref *ref_p;
6979
6980 if (is_type_die (die)
6981 && (c = get_AT_ref (die, DW_AT_signature)))
6982 {
6983 /* This is a local skeleton; use it for local references. */
6984 ref_p = lookup_external_ref (map, c);
6985 ref_p->stub = die;
6986 }
6987
6988 /* Scan the DIE references, and remember any that refer to DIEs from
6989 other CUs (i.e. those which are not marked). */
6990 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6991 if (AT_class (a) == dw_val_class_die_ref
6992 && (c = AT_ref (a))->die_mark == 0
6993 && is_type_die (c))
6994 {
6995 ref_p = lookup_external_ref (map, c);
6996 ref_p->n_refs++;
6997 }
6998
6999 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7000 }
7001
7002 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7003 points to an external_ref, DATA is the CU we're processing. If we don't
7004 already have a local stub, and we have multiple refs, build a stub. */
7005
7006 static int
7007 build_local_stub (void **slot, void *data)
7008 {
7009 struct external_ref *ref_p = (struct external_ref *)*slot;
7010
7011 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7012 {
7013 /* We have multiple references to this type, so build a small stub.
7014 Both of these forms are a bit dodgy from the perspective of the
7015 DWARF standard, since technically they should have names. */
7016 dw_die_ref cu = (dw_die_ref) data;
7017 dw_die_ref type = ref_p->type;
7018 dw_die_ref stub = NULL;
7019
7020 if (type->comdat_type_p)
7021 {
7022 /* If we refer to this type via sig8, use AT_signature. */
7023 stub = new_die (type->die_tag, cu, NULL_TREE);
7024 add_AT_die_ref (stub, DW_AT_signature, type);
7025 }
7026 else
7027 {
7028 /* Otherwise, use a typedef with no name. */
7029 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7030 add_AT_die_ref (stub, DW_AT_type, type);
7031 }
7032
7033 stub->die_mark++;
7034 ref_p->stub = stub;
7035 }
7036 return 1;
7037 }
7038
7039 /* DIE is a unit; look through all the DIE references to see if there are
7040 any external references to types, and if so, create local stubs for
7041 them which will be applied in build_abbrev_table. This is useful because
7042 references to local DIEs are smaller. */
7043
7044 static htab_t
7045 optimize_external_refs (dw_die_ref die)
7046 {
7047 htab_t map = htab_create (10, hash_external_ref, external_ref_eq, free);
7048 optimize_external_refs_1 (die, map);
7049 htab_traverse (map, build_local_stub, die);
7050 return map;
7051 }
7052
7053 /* The format of each DIE (and its attribute value pairs) is encoded in an
7054 abbreviation table. This routine builds the abbreviation table and assigns
7055 a unique abbreviation id for each abbreviation entry. The children of each
7056 die are visited recursively. */
7057
7058 static void
7059 build_abbrev_table (dw_die_ref die, htab_t extern_map)
7060 {
7061 unsigned long abbrev_id;
7062 unsigned int n_alloc;
7063 dw_die_ref c;
7064 dw_attr_ref a;
7065 unsigned ix;
7066
7067 /* Scan the DIE references, and replace any that refer to
7068 DIEs from other CUs (i.e. those which are not marked) with
7069 the local stubs we built in optimize_external_refs. */
7070 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7071 if (AT_class (a) == dw_val_class_die_ref
7072 && (c = AT_ref (a))->die_mark == 0)
7073 {
7074 struct external_ref *ref_p;
7075 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7076
7077 ref_p = lookup_external_ref (extern_map, c);
7078 if (ref_p->stub && ref_p->stub != die)
7079 change_AT_die_ref (a, ref_p->stub);
7080 else
7081 /* We aren't changing this reference, so mark it external. */
7082 set_AT_ref_external (a, 1);
7083 }
7084
7085 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7086 {
7087 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7088 dw_attr_ref die_a, abbrev_a;
7089 unsigned ix;
7090 bool ok = true;
7091
7092 if (abbrev->die_tag != die->die_tag)
7093 continue;
7094 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7095 continue;
7096
7097 if (VEC_length (dw_attr_node, abbrev->die_attr)
7098 != VEC_length (dw_attr_node, die->die_attr))
7099 continue;
7100
7101 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
7102 {
7103 abbrev_a = &VEC_index (dw_attr_node, abbrev->die_attr, ix);
7104 if ((abbrev_a->dw_attr != die_a->dw_attr)
7105 || (value_format (abbrev_a) != value_format (die_a)))
7106 {
7107 ok = false;
7108 break;
7109 }
7110 }
7111 if (ok)
7112 break;
7113 }
7114
7115 if (abbrev_id >= abbrev_die_table_in_use)
7116 {
7117 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7118 {
7119 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7120 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7121 n_alloc);
7122
7123 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7124 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7125 abbrev_die_table_allocated = n_alloc;
7126 }
7127
7128 ++abbrev_die_table_in_use;
7129 abbrev_die_table[abbrev_id] = die;
7130 }
7131
7132 die->die_abbrev = abbrev_id;
7133 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7134 }
7135 \f
7136 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7137
7138 static int
7139 constant_size (unsigned HOST_WIDE_INT value)
7140 {
7141 int log;
7142
7143 if (value == 0)
7144 log = 0;
7145 else
7146 log = floor_log2 (value);
7147
7148 log = log / 8;
7149 log = 1 << (floor_log2 (log) + 1);
7150
7151 return log;
7152 }
7153
7154 /* Return the size of a DIE as it is represented in the
7155 .debug_info section. */
7156
7157 static unsigned long
7158 size_of_die (dw_die_ref die)
7159 {
7160 unsigned long size = 0;
7161 dw_attr_ref a;
7162 unsigned ix;
7163
7164 size += size_of_uleb128 (die->die_abbrev);
7165 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7166 {
7167 switch (AT_class (a))
7168 {
7169 case dw_val_class_addr:
7170 size += DWARF2_ADDR_SIZE;
7171 break;
7172 case dw_val_class_offset:
7173 size += DWARF_OFFSET_SIZE;
7174 break;
7175 case dw_val_class_loc:
7176 {
7177 unsigned long lsize = size_of_locs (AT_loc (a));
7178
7179 /* Block length. */
7180 if (dwarf_version >= 4)
7181 size += size_of_uleb128 (lsize);
7182 else
7183 size += constant_size (lsize);
7184 size += lsize;
7185 }
7186 break;
7187 case dw_val_class_loc_list:
7188 size += DWARF_OFFSET_SIZE;
7189 break;
7190 case dw_val_class_range_list:
7191 size += DWARF_OFFSET_SIZE;
7192 break;
7193 case dw_val_class_const:
7194 size += size_of_sleb128 (AT_int (a));
7195 break;
7196 case dw_val_class_unsigned_const:
7197 {
7198 int csize = constant_size (AT_unsigned (a));
7199 if (dwarf_version == 3
7200 && a->dw_attr == DW_AT_data_member_location
7201 && csize >= 4)
7202 size += size_of_uleb128 (AT_unsigned (a));
7203 else
7204 size += csize;
7205 }
7206 break;
7207 case dw_val_class_const_double:
7208 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7209 if (HOST_BITS_PER_WIDE_INT >= 64)
7210 size++; /* block */
7211 break;
7212 case dw_val_class_vec:
7213 size += constant_size (a->dw_attr_val.v.val_vec.length
7214 * a->dw_attr_val.v.val_vec.elt_size)
7215 + a->dw_attr_val.v.val_vec.length
7216 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7217 break;
7218 case dw_val_class_flag:
7219 if (dwarf_version >= 4)
7220 /* Currently all add_AT_flag calls pass in 1 as last argument,
7221 so DW_FORM_flag_present can be used. If that ever changes,
7222 we'll need to use DW_FORM_flag and have some optimization
7223 in build_abbrev_table that will change those to
7224 DW_FORM_flag_present if it is set to 1 in all DIEs using
7225 the same abbrev entry. */
7226 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7227 else
7228 size += 1;
7229 break;
7230 case dw_val_class_die_ref:
7231 if (AT_ref_external (a))
7232 {
7233 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7234 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7235 is sized by target address length, whereas in DWARF3
7236 it's always sized as an offset. */
7237 if (use_debug_types)
7238 size += DWARF_TYPE_SIGNATURE_SIZE;
7239 else if (dwarf_version == 2)
7240 size += DWARF2_ADDR_SIZE;
7241 else
7242 size += DWARF_OFFSET_SIZE;
7243 }
7244 else
7245 size += DWARF_OFFSET_SIZE;
7246 break;
7247 case dw_val_class_fde_ref:
7248 size += DWARF_OFFSET_SIZE;
7249 break;
7250 case dw_val_class_lbl_id:
7251 size += DWARF2_ADDR_SIZE;
7252 break;
7253 case dw_val_class_lineptr:
7254 case dw_val_class_macptr:
7255 size += DWARF_OFFSET_SIZE;
7256 break;
7257 case dw_val_class_str:
7258 if (AT_string_form (a) == DW_FORM_strp)
7259 size += DWARF_OFFSET_SIZE;
7260 else
7261 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7262 break;
7263 case dw_val_class_file:
7264 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7265 break;
7266 case dw_val_class_data8:
7267 size += 8;
7268 break;
7269 case dw_val_class_vms_delta:
7270 size += DWARF_OFFSET_SIZE;
7271 break;
7272 case dw_val_class_high_pc:
7273 size += DWARF2_ADDR_SIZE;
7274 break;
7275 default:
7276 gcc_unreachable ();
7277 }
7278 }
7279
7280 return size;
7281 }
7282
7283 /* Size the debugging information associated with a given DIE. Visits the
7284 DIE's children recursively. Updates the global variable next_die_offset, on
7285 each time through. Uses the current value of next_die_offset to update the
7286 die_offset field in each DIE. */
7287
7288 static void
7289 calc_die_sizes (dw_die_ref die)
7290 {
7291 dw_die_ref c;
7292
7293 gcc_assert (die->die_offset == 0
7294 || (unsigned long int) die->die_offset == next_die_offset);
7295 die->die_offset = next_die_offset;
7296 next_die_offset += size_of_die (die);
7297
7298 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7299
7300 if (die->die_child != NULL)
7301 /* Count the null byte used to terminate sibling lists. */
7302 next_die_offset += 1;
7303 }
7304
7305 /* Size just the base type children at the start of the CU.
7306 This is needed because build_abbrev needs to size locs
7307 and sizing of type based stack ops needs to know die_offset
7308 values for the base types. */
7309
7310 static void
7311 calc_base_type_die_sizes (void)
7312 {
7313 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7314 unsigned int i;
7315 dw_die_ref base_type;
7316 #if ENABLE_ASSERT_CHECKING
7317 dw_die_ref prev = comp_unit_die ()->die_child;
7318 #endif
7319
7320 die_offset += size_of_die (comp_unit_die ());
7321 for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7322 {
7323 #if ENABLE_ASSERT_CHECKING
7324 gcc_assert (base_type->die_offset == 0
7325 && prev->die_sib == base_type
7326 && base_type->die_child == NULL
7327 && base_type->die_abbrev);
7328 prev = base_type;
7329 #endif
7330 base_type->die_offset = die_offset;
7331 die_offset += size_of_die (base_type);
7332 }
7333 }
7334
7335 /* Set the marks for a die and its children. We do this so
7336 that we know whether or not a reference needs to use FORM_ref_addr; only
7337 DIEs in the same CU will be marked. We used to clear out the offset
7338 and use that as the flag, but ran into ordering problems. */
7339
7340 static void
7341 mark_dies (dw_die_ref die)
7342 {
7343 dw_die_ref c;
7344
7345 gcc_assert (!die->die_mark);
7346
7347 die->die_mark = 1;
7348 FOR_EACH_CHILD (die, c, mark_dies (c));
7349 }
7350
7351 /* Clear the marks for a die and its children. */
7352
7353 static void
7354 unmark_dies (dw_die_ref die)
7355 {
7356 dw_die_ref c;
7357
7358 if (! use_debug_types)
7359 gcc_assert (die->die_mark);
7360
7361 die->die_mark = 0;
7362 FOR_EACH_CHILD (die, c, unmark_dies (c));
7363 }
7364
7365 /* Clear the marks for a die, its children and referred dies. */
7366
7367 static void
7368 unmark_all_dies (dw_die_ref die)
7369 {
7370 dw_die_ref c;
7371 dw_attr_ref a;
7372 unsigned ix;
7373
7374 if (!die->die_mark)
7375 return;
7376 die->die_mark = 0;
7377
7378 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7379
7380 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7381 if (AT_class (a) == dw_val_class_die_ref)
7382 unmark_all_dies (AT_ref (a));
7383 }
7384
7385 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7386 generated for the compilation unit. */
7387
7388 static unsigned long
7389 size_of_pubnames (VEC (pubname_entry, gc) * names)
7390 {
7391 unsigned long size;
7392 unsigned i;
7393 pubname_ref p;
7394
7395 size = DWARF_PUBNAMES_HEADER_SIZE;
7396 FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7397 if (names != pubtype_table
7398 || p->die->die_offset != 0
7399 || !flag_eliminate_unused_debug_types)
7400 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7401
7402 size += DWARF_OFFSET_SIZE;
7403 return size;
7404 }
7405
7406 /* Return the size of the information in the .debug_aranges section. */
7407
7408 static unsigned long
7409 size_of_aranges (void)
7410 {
7411 unsigned long size;
7412
7413 size = DWARF_ARANGES_HEADER_SIZE;
7414
7415 /* Count the address/length pair for this compilation unit. */
7416 if (text_section_used)
7417 size += 2 * DWARF2_ADDR_SIZE;
7418 if (cold_text_section_used)
7419 size += 2 * DWARF2_ADDR_SIZE;
7420 if (have_multiple_function_sections)
7421 {
7422 unsigned fde_idx;
7423 dw_fde_ref fde;
7424
7425 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7426 {
7427 if (!fde->in_std_section)
7428 size += 2 * DWARF2_ADDR_SIZE;
7429 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7430 size += 2 * DWARF2_ADDR_SIZE;
7431 }
7432 }
7433
7434 /* Count the two zero words used to terminated the address range table. */
7435 size += 2 * DWARF2_ADDR_SIZE;
7436 return size;
7437 }
7438 \f
7439 /* Select the encoding of an attribute value. */
7440
7441 static enum dwarf_form
7442 value_format (dw_attr_ref a)
7443 {
7444 switch (a->dw_attr_val.val_class)
7445 {
7446 case dw_val_class_addr:
7447 /* Only very few attributes allow DW_FORM_addr. */
7448 switch (a->dw_attr)
7449 {
7450 case DW_AT_low_pc:
7451 case DW_AT_high_pc:
7452 case DW_AT_entry_pc:
7453 case DW_AT_trampoline:
7454 return DW_FORM_addr;
7455 default:
7456 break;
7457 }
7458 switch (DWARF2_ADDR_SIZE)
7459 {
7460 case 1:
7461 return DW_FORM_data1;
7462 case 2:
7463 return DW_FORM_data2;
7464 case 4:
7465 return DW_FORM_data4;
7466 case 8:
7467 return DW_FORM_data8;
7468 default:
7469 gcc_unreachable ();
7470 }
7471 case dw_val_class_range_list:
7472 case dw_val_class_loc_list:
7473 if (dwarf_version >= 4)
7474 return DW_FORM_sec_offset;
7475 /* FALLTHRU */
7476 case dw_val_class_vms_delta:
7477 case dw_val_class_offset:
7478 switch (DWARF_OFFSET_SIZE)
7479 {
7480 case 4:
7481 return DW_FORM_data4;
7482 case 8:
7483 return DW_FORM_data8;
7484 default:
7485 gcc_unreachable ();
7486 }
7487 case dw_val_class_loc:
7488 if (dwarf_version >= 4)
7489 return DW_FORM_exprloc;
7490 switch (constant_size (size_of_locs (AT_loc (a))))
7491 {
7492 case 1:
7493 return DW_FORM_block1;
7494 case 2:
7495 return DW_FORM_block2;
7496 case 4:
7497 return DW_FORM_block4;
7498 default:
7499 gcc_unreachable ();
7500 }
7501 case dw_val_class_const:
7502 return DW_FORM_sdata;
7503 case dw_val_class_unsigned_const:
7504 switch (constant_size (AT_unsigned (a)))
7505 {
7506 case 1:
7507 return DW_FORM_data1;
7508 case 2:
7509 return DW_FORM_data2;
7510 case 4:
7511 /* In DWARF3 DW_AT_data_member_location with
7512 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7513 constant, so we need to use DW_FORM_udata if we need
7514 a large constant. */
7515 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7516 return DW_FORM_udata;
7517 return DW_FORM_data4;
7518 case 8:
7519 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7520 return DW_FORM_udata;
7521 return DW_FORM_data8;
7522 default:
7523 gcc_unreachable ();
7524 }
7525 case dw_val_class_const_double:
7526 switch (HOST_BITS_PER_WIDE_INT)
7527 {
7528 case 8:
7529 return DW_FORM_data2;
7530 case 16:
7531 return DW_FORM_data4;
7532 case 32:
7533 return DW_FORM_data8;
7534 case 64:
7535 default:
7536 return DW_FORM_block1;
7537 }
7538 case dw_val_class_vec:
7539 switch (constant_size (a->dw_attr_val.v.val_vec.length
7540 * a->dw_attr_val.v.val_vec.elt_size))
7541 {
7542 case 1:
7543 return DW_FORM_block1;
7544 case 2:
7545 return DW_FORM_block2;
7546 case 4:
7547 return DW_FORM_block4;
7548 default:
7549 gcc_unreachable ();
7550 }
7551 case dw_val_class_flag:
7552 if (dwarf_version >= 4)
7553 {
7554 /* Currently all add_AT_flag calls pass in 1 as last argument,
7555 so DW_FORM_flag_present can be used. If that ever changes,
7556 we'll need to use DW_FORM_flag and have some optimization
7557 in build_abbrev_table that will change those to
7558 DW_FORM_flag_present if it is set to 1 in all DIEs using
7559 the same abbrev entry. */
7560 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7561 return DW_FORM_flag_present;
7562 }
7563 return DW_FORM_flag;
7564 case dw_val_class_die_ref:
7565 if (AT_ref_external (a))
7566 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
7567 else
7568 return DW_FORM_ref;
7569 case dw_val_class_fde_ref:
7570 return DW_FORM_data;
7571 case dw_val_class_lbl_id:
7572 return DW_FORM_addr;
7573 case dw_val_class_lineptr:
7574 case dw_val_class_macptr:
7575 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
7576 case dw_val_class_str:
7577 return AT_string_form (a);
7578 case dw_val_class_file:
7579 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7580 {
7581 case 1:
7582 return DW_FORM_data1;
7583 case 2:
7584 return DW_FORM_data2;
7585 case 4:
7586 return DW_FORM_data4;
7587 default:
7588 gcc_unreachable ();
7589 }
7590
7591 case dw_val_class_data8:
7592 return DW_FORM_data8;
7593
7594 case dw_val_class_high_pc:
7595 switch (DWARF2_ADDR_SIZE)
7596 {
7597 case 4:
7598 return DW_FORM_data4;
7599 case 8:
7600 return DW_FORM_data8;
7601 default:
7602 gcc_unreachable ();
7603 }
7604
7605 default:
7606 gcc_unreachable ();
7607 }
7608 }
7609
7610 /* Output the encoding of an attribute value. */
7611
7612 static void
7613 output_value_format (dw_attr_ref a)
7614 {
7615 enum dwarf_form form = value_format (a);
7616
7617 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7618 }
7619
7620 /* Output the .debug_abbrev section which defines the DIE abbreviation
7621 table. */
7622
7623 static void
7624 output_abbrev_section (void)
7625 {
7626 unsigned long abbrev_id;
7627
7628 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7629 {
7630 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7631 unsigned ix;
7632 dw_attr_ref a_attr;
7633
7634 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7635 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7636 dwarf_tag_name (abbrev->die_tag));
7637
7638 if (abbrev->die_child != NULL)
7639 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7640 else
7641 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7642
7643 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7644 ix++)
7645 {
7646 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7647 dwarf_attr_name (a_attr->dw_attr));
7648 output_value_format (a_attr);
7649 }
7650
7651 dw2_asm_output_data (1, 0, NULL);
7652 dw2_asm_output_data (1, 0, NULL);
7653 }
7654
7655 /* Terminate the table. */
7656 dw2_asm_output_data (1, 0, NULL);
7657 }
7658
7659 /* Output a symbol we can use to refer to this DIE from another CU. */
7660
7661 static inline void
7662 output_die_symbol (dw_die_ref die)
7663 {
7664 const char *sym = die->die_id.die_symbol;
7665
7666 gcc_assert (!die->comdat_type_p);
7667
7668 if (sym == 0)
7669 return;
7670
7671 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7672 /* We make these global, not weak; if the target doesn't support
7673 .linkonce, it doesn't support combining the sections, so debugging
7674 will break. */
7675 targetm.asm_out.globalize_label (asm_out_file, sym);
7676
7677 ASM_OUTPUT_LABEL (asm_out_file, sym);
7678 }
7679
7680 /* Return a new location list, given the begin and end range, and the
7681 expression. */
7682
7683 static inline dw_loc_list_ref
7684 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7685 const char *section)
7686 {
7687 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
7688
7689 retlist->begin = begin;
7690 retlist->end = end;
7691 retlist->expr = expr;
7692 retlist->section = section;
7693
7694 return retlist;
7695 }
7696
7697 /* Generate a new internal symbol for this location list node, if it
7698 hasn't got one yet. */
7699
7700 static inline void
7701 gen_llsym (dw_loc_list_ref list)
7702 {
7703 gcc_assert (!list->ll_symbol);
7704 list->ll_symbol = gen_internal_sym ("LLST");
7705 }
7706
7707 /* Output the location list given to us. */
7708
7709 static void
7710 output_loc_list (dw_loc_list_ref list_head)
7711 {
7712 dw_loc_list_ref curr = list_head;
7713
7714 if (list_head->emitted)
7715 return;
7716 list_head->emitted = true;
7717
7718 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7719
7720 /* Walk the location list, and output each range + expression. */
7721 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7722 {
7723 unsigned long size;
7724 /* Don't output an entry that starts and ends at the same address. */
7725 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
7726 continue;
7727 size = size_of_locs (curr->expr);
7728 /* If the expression is too large, drop it on the floor. We could
7729 perhaps put it into DW_TAG_dwarf_procedure and refer to that
7730 in the expression, but >= 64KB expressions for a single value
7731 in a single range are unlikely very useful. */
7732 if (size > 0xffff)
7733 continue;
7734 if (!have_multiple_function_sections)
7735 {
7736 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7737 "Location list begin address (%s)",
7738 list_head->ll_symbol);
7739 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7740 "Location list end address (%s)",
7741 list_head->ll_symbol);
7742 }
7743 else
7744 {
7745 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7746 "Location list begin address (%s)",
7747 list_head->ll_symbol);
7748 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7749 "Location list end address (%s)",
7750 list_head->ll_symbol);
7751 }
7752
7753 /* Output the block length for this list of location operations. */
7754 gcc_assert (size <= 0xffff);
7755 dw2_asm_output_data (2, size, "%s", "Location expression size");
7756
7757 output_loc_sequence (curr->expr, -1);
7758 }
7759
7760 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7761 "Location list terminator begin (%s)",
7762 list_head->ll_symbol);
7763 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7764 "Location list terminator end (%s)",
7765 list_head->ll_symbol);
7766 }
7767
7768 /* Output a type signature. */
7769
7770 static inline void
7771 output_signature (const char *sig, const char *name)
7772 {
7773 int i;
7774
7775 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
7776 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
7777 }
7778
7779 /* Output the DIE and its attributes. Called recursively to generate
7780 the definitions of each child DIE. */
7781
7782 static void
7783 output_die (dw_die_ref die)
7784 {
7785 dw_attr_ref a;
7786 dw_die_ref c;
7787 unsigned long size;
7788 unsigned ix;
7789
7790 /* If someone in another CU might refer to us, set up a symbol for
7791 them to point to. */
7792 if (! die->comdat_type_p && die->die_id.die_symbol)
7793 output_die_symbol (die);
7794
7795 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
7796 (unsigned long)die->die_offset,
7797 dwarf_tag_name (die->die_tag));
7798
7799 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7800 {
7801 const char *name = dwarf_attr_name (a->dw_attr);
7802
7803 switch (AT_class (a))
7804 {
7805 case dw_val_class_addr:
7806 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7807 break;
7808
7809 case dw_val_class_offset:
7810 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7811 "%s", name);
7812 break;
7813
7814 case dw_val_class_range_list:
7815 {
7816 char *p = strchr (ranges_section_label, '\0');
7817
7818 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7819 a->dw_attr_val.v.val_offset);
7820 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7821 debug_ranges_section, "%s", name);
7822 *p = '\0';
7823 }
7824 break;
7825
7826 case dw_val_class_loc:
7827 size = size_of_locs (AT_loc (a));
7828
7829 /* Output the block length for this list of location operations. */
7830 if (dwarf_version >= 4)
7831 dw2_asm_output_data_uleb128 (size, "%s", name);
7832 else
7833 dw2_asm_output_data (constant_size (size), size, "%s", name);
7834
7835 output_loc_sequence (AT_loc (a), -1);
7836 break;
7837
7838 case dw_val_class_const:
7839 /* ??? It would be slightly more efficient to use a scheme like is
7840 used for unsigned constants below, but gdb 4.x does not sign
7841 extend. Gdb 5.x does sign extend. */
7842 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7843 break;
7844
7845 case dw_val_class_unsigned_const:
7846 {
7847 int csize = constant_size (AT_unsigned (a));
7848 if (dwarf_version == 3
7849 && a->dw_attr == DW_AT_data_member_location
7850 && csize >= 4)
7851 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
7852 else
7853 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
7854 }
7855 break;
7856
7857 case dw_val_class_const_double:
7858 {
7859 unsigned HOST_WIDE_INT first, second;
7860
7861 if (HOST_BITS_PER_WIDE_INT >= 64)
7862 dw2_asm_output_data (1,
7863 HOST_BITS_PER_DOUBLE_INT
7864 / HOST_BITS_PER_CHAR,
7865 NULL);
7866
7867 if (WORDS_BIG_ENDIAN)
7868 {
7869 first = a->dw_attr_val.v.val_double.high;
7870 second = a->dw_attr_val.v.val_double.low;
7871 }
7872 else
7873 {
7874 first = a->dw_attr_val.v.val_double.low;
7875 second = a->dw_attr_val.v.val_double.high;
7876 }
7877
7878 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
7879 first, name);
7880 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
7881 second, NULL);
7882 }
7883 break;
7884
7885 case dw_val_class_vec:
7886 {
7887 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7888 unsigned int len = a->dw_attr_val.v.val_vec.length;
7889 unsigned int i;
7890 unsigned char *p;
7891
7892 dw2_asm_output_data (constant_size (len * elt_size),
7893 len * elt_size, "%s", name);
7894 if (elt_size > sizeof (HOST_WIDE_INT))
7895 {
7896 elt_size /= 2;
7897 len *= 2;
7898 }
7899 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7900 i < len;
7901 i++, p += elt_size)
7902 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7903 "fp or vector constant word %u", i);
7904 break;
7905 }
7906
7907 case dw_val_class_flag:
7908 if (dwarf_version >= 4)
7909 {
7910 /* Currently all add_AT_flag calls pass in 1 as last argument,
7911 so DW_FORM_flag_present can be used. If that ever changes,
7912 we'll need to use DW_FORM_flag and have some optimization
7913 in build_abbrev_table that will change those to
7914 DW_FORM_flag_present if it is set to 1 in all DIEs using
7915 the same abbrev entry. */
7916 gcc_assert (AT_flag (a) == 1);
7917 if (flag_debug_asm)
7918 fprintf (asm_out_file, "\t\t\t%s %s\n",
7919 ASM_COMMENT_START, name);
7920 break;
7921 }
7922 dw2_asm_output_data (1, AT_flag (a), "%s", name);
7923 break;
7924
7925 case dw_val_class_loc_list:
7926 {
7927 char *sym = AT_loc_list (a)->ll_symbol;
7928
7929 gcc_assert (sym);
7930 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7931 "%s", name);
7932 }
7933 break;
7934
7935 case dw_val_class_die_ref:
7936 if (AT_ref_external (a))
7937 {
7938 if (AT_ref (a)->comdat_type_p)
7939 {
7940 comdat_type_node_ref type_node =
7941 AT_ref (a)->die_id.die_type_node;
7942
7943 gcc_assert (type_node);
7944 output_signature (type_node->signature, name);
7945 }
7946 else
7947 {
7948 const char *sym = AT_ref (a)->die_id.die_symbol;
7949 int size;
7950
7951 gcc_assert (sym);
7952 /* In DWARF2, DW_FORM_ref_addr is sized by target address
7953 length, whereas in DWARF3 it's always sized as an
7954 offset. */
7955 if (dwarf_version == 2)
7956 size = DWARF2_ADDR_SIZE;
7957 else
7958 size = DWARF_OFFSET_SIZE;
7959 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
7960 name);
7961 }
7962 }
7963 else
7964 {
7965 gcc_assert (AT_ref (a)->die_offset);
7966 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7967 "%s", name);
7968 }
7969 break;
7970
7971 case dw_val_class_fde_ref:
7972 {
7973 char l1[20];
7974
7975 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7976 a->dw_attr_val.v.val_fde_index * 2);
7977 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7978 "%s", name);
7979 }
7980 break;
7981
7982 case dw_val_class_vms_delta:
7983 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
7984 AT_vms_delta2 (a), AT_vms_delta1 (a),
7985 "%s", name);
7986 break;
7987
7988 case dw_val_class_lbl_id:
7989 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7990 break;
7991
7992 case dw_val_class_lineptr:
7993 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7994 debug_line_section, "%s", name);
7995 break;
7996
7997 case dw_val_class_macptr:
7998 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7999 debug_macinfo_section, "%s", name);
8000 break;
8001
8002 case dw_val_class_str:
8003 if (AT_string_form (a) == DW_FORM_strp)
8004 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8005 a->dw_attr_val.v.val_str->label,
8006 debug_str_section,
8007 "%s: \"%s\"", name, AT_string (a));
8008 else
8009 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8010 break;
8011
8012 case dw_val_class_file:
8013 {
8014 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8015
8016 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8017 a->dw_attr_val.v.val_file->filename);
8018 break;
8019 }
8020
8021 case dw_val_class_data8:
8022 {
8023 int i;
8024
8025 for (i = 0; i < 8; i++)
8026 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8027 i == 0 ? "%s" : NULL, name);
8028 break;
8029 }
8030
8031 case dw_val_class_high_pc:
8032 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8033 get_AT_low_pc (die), "DW_AT_high_pc");
8034 break;
8035
8036 default:
8037 gcc_unreachable ();
8038 }
8039 }
8040
8041 FOR_EACH_CHILD (die, c, output_die (c));
8042
8043 /* Add null byte to terminate sibling list. */
8044 if (die->die_child != NULL)
8045 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8046 (unsigned long) die->die_offset);
8047 }
8048
8049 /* Output the compilation unit that appears at the beginning of the
8050 .debug_info section, and precedes the DIE descriptions. */
8051
8052 static void
8053 output_compilation_unit_header (void)
8054 {
8055 int ver = dwarf_version;
8056
8057 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8058 dw2_asm_output_data (4, 0xffffffff,
8059 "Initial length escape value indicating 64-bit DWARF extension");
8060 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8061 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8062 "Length of Compilation Unit Info");
8063 dw2_asm_output_data (2, ver, "DWARF version number");
8064 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8065 debug_abbrev_section,
8066 "Offset Into Abbrev. Section");
8067 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8068 }
8069
8070 /* Output the compilation unit DIE and its children. */
8071
8072 static void
8073 output_comp_unit (dw_die_ref die, int output_if_empty)
8074 {
8075 const char *secname, *oldsym;
8076 char *tmp;
8077 htab_t extern_map;
8078
8079 /* Unless we are outputting main CU, we may throw away empty ones. */
8080 if (!output_if_empty && die->die_child == NULL)
8081 return;
8082
8083 /* Even if there are no children of this DIE, we must output the information
8084 about the compilation unit. Otherwise, on an empty translation unit, we
8085 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8086 will then complain when examining the file. First mark all the DIEs in
8087 this CU so we know which get local refs. */
8088 mark_dies (die);
8089
8090 extern_map = optimize_external_refs (die);
8091
8092 build_abbrev_table (die, extern_map);
8093
8094 htab_delete (extern_map);
8095
8096 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8097 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8098 calc_die_sizes (die);
8099
8100 oldsym = die->die_id.die_symbol;
8101 if (oldsym)
8102 {
8103 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8104
8105 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8106 secname = tmp;
8107 die->die_id.die_symbol = NULL;
8108 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8109 }
8110 else
8111 {
8112 switch_to_section (debug_info_section);
8113 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8114 info_section_emitted = true;
8115 }
8116
8117 /* Output debugging information. */
8118 output_compilation_unit_header ();
8119 output_die (die);
8120
8121 /* Leave the marks on the main CU, so we can check them in
8122 output_pubnames. */
8123 if (oldsym)
8124 {
8125 unmark_dies (die);
8126 die->die_id.die_symbol = oldsym;
8127 }
8128 }
8129
8130 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8131 and .debug_pubtypes. This is configured per-target, but can be
8132 overridden by the -gpubnames or -gno-pubnames options. */
8133
8134 static inline bool
8135 want_pubnames (void)
8136 {
8137 return (debug_generate_pub_sections != -1
8138 ? debug_generate_pub_sections
8139 : targetm.want_debug_pub_sections);
8140 }
8141
8142 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8143
8144 static void
8145 add_AT_pubnames (dw_die_ref die)
8146 {
8147 if (want_pubnames ())
8148 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8149 }
8150
8151 /* Output a comdat type unit DIE and its children. */
8152
8153 static void
8154 output_comdat_type_unit (comdat_type_node *node)
8155 {
8156 const char *secname;
8157 char *tmp;
8158 int i;
8159 #if defined (OBJECT_FORMAT_ELF)
8160 tree comdat_key;
8161 #endif
8162 htab_t extern_map;
8163
8164 /* First mark all the DIEs in this CU so we know which get local refs. */
8165 mark_dies (node->root_die);
8166
8167 extern_map = optimize_external_refs (node->root_die);
8168
8169 build_abbrev_table (node->root_die, extern_map);
8170
8171 htab_delete (extern_map);
8172
8173 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8174 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8175 calc_die_sizes (node->root_die);
8176
8177 #if defined (OBJECT_FORMAT_ELF)
8178 secname = ".debug_types";
8179 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8180 sprintf (tmp, "wt.");
8181 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8182 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8183 comdat_key = get_identifier (tmp);
8184 targetm.asm_out.named_section (secname,
8185 SECTION_DEBUG | SECTION_LINKONCE,
8186 comdat_key);
8187 #else
8188 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8189 sprintf (tmp, ".gnu.linkonce.wt.");
8190 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8191 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8192 secname = tmp;
8193 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8194 #endif
8195
8196 /* Output debugging information. */
8197 output_compilation_unit_header ();
8198 output_signature (node->signature, "Type Signature");
8199 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8200 "Offset to Type DIE");
8201 output_die (node->root_die);
8202
8203 unmark_dies (node->root_die);
8204 }
8205
8206 /* Return the DWARF2/3 pubname associated with a decl. */
8207
8208 static const char *
8209 dwarf2_name (tree decl, int scope)
8210 {
8211 if (DECL_NAMELESS (decl))
8212 return NULL;
8213 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8214 }
8215
8216 /* Add a new entry to .debug_pubnames if appropriate. */
8217
8218 static void
8219 add_pubname_string (const char *str, dw_die_ref die)
8220 {
8221 pubname_entry e;
8222
8223 e.die = die;
8224 e.name = xstrdup (str);
8225 VEC_safe_push (pubname_entry, gc, pubname_table, e);
8226 }
8227
8228 static void
8229 add_pubname (tree decl, dw_die_ref die)
8230 {
8231 if (!want_pubnames ())
8232 return;
8233
8234 /* Don't add items to the table when we expect that the consumer will have
8235 just read the enclosing die. For example, if the consumer is looking at a
8236 class_member, it will either be inside the class already, or will have just
8237 looked up the class to find the member. Either way, searching the class is
8238 faster than searching the index. */
8239 if ((TREE_PUBLIC (decl) && !is_class_die (die->die_parent))
8240 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8241 {
8242 const char *name = dwarf2_name (decl, 1);
8243
8244 if (name)
8245 add_pubname_string (name, die);
8246 }
8247 }
8248
8249 /* Add an enumerator to the pubnames section. */
8250
8251 static void
8252 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8253 {
8254 pubname_entry e;
8255
8256 gcc_assert (scope_name);
8257 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8258 e.die = die;
8259 VEC_safe_push (pubname_entry, gc, pubname_table, e);
8260 }
8261
8262 /* Add a new entry to .debug_pubtypes if appropriate. */
8263
8264 static void
8265 add_pubtype (tree decl, dw_die_ref die)
8266 {
8267 pubname_entry e;
8268
8269 if (!want_pubnames ())
8270 return;
8271
8272 if ((TREE_PUBLIC (decl)
8273 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8274 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8275 {
8276 tree scope = NULL;
8277 const char *scope_name = "";
8278 const char *sep = is_cxx () ? "::" : ".";
8279 const char *name;
8280
8281 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
8282 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
8283 {
8284 scope_name = lang_hooks.dwarf_name (scope, 1);
8285 if (scope_name != NULL && scope_name[0] != '\0')
8286 scope_name = concat (scope_name, sep, NULL);
8287 else
8288 scope_name = "";
8289 }
8290
8291 if (TYPE_P (decl))
8292 name = type_tag (decl);
8293 else
8294 name = lang_hooks.dwarf_name (decl, 1);
8295
8296 /* If we don't have a name for the type, there's no point in adding
8297 it to the table. */
8298 if (name != NULL && name[0] != '\0')
8299 {
8300 e.die = die;
8301 e.name = concat (scope_name, name, NULL);
8302 VEC_safe_push (pubname_entry, gc, pubtype_table, e);
8303 }
8304
8305 /* Although it might be more consistent to add the pubinfo for the
8306 enumerators as their dies are created, they should only be added if the
8307 enum type meets the criteria above. So rather than re-check the parent
8308 enum type whenever an enumerator die is created, just output them all
8309 here. This isn't protected by the name conditional because anonymous
8310 enums don't have names. */
8311 if (die->die_tag == DW_TAG_enumeration_type)
8312 {
8313 dw_die_ref c;
8314
8315 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
8316 }
8317 }
8318 }
8319
8320 /* Output the public names table used to speed up access to externally
8321 visible names; or the public types table used to find type definitions. */
8322
8323 static void
8324 output_pubnames (VEC (pubname_entry, gc) * names)
8325 {
8326 unsigned i;
8327 unsigned long pubnames_length = size_of_pubnames (names);
8328 pubname_ref pub;
8329
8330 if (!want_pubnames () || !info_section_emitted)
8331 return;
8332 if (names == pubname_table)
8333 switch_to_section (debug_pubnames_section);
8334 else
8335 switch_to_section (debug_pubtypes_section);
8336 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8337 dw2_asm_output_data (4, 0xffffffff,
8338 "Initial length escape value indicating 64-bit DWARF extension");
8339 if (names == pubname_table)
8340 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8341 "Length of Public Names Info");
8342 else
8343 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8344 "Length of Public Type Names Info");
8345 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
8346 dw2_asm_output_data (2, 2, "DWARF Version");
8347 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8348 debug_info_section,
8349 "Offset of Compilation Unit Info");
8350 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8351 "Compilation Unit Length");
8352
8353 FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8354 {
8355 /* Enumerator names are part of the pubname table, but the parent
8356 DW_TAG_enumeration_type die may have been pruned. Don't output
8357 them if that is the case. */
8358 if (pub->die->die_tag == DW_TAG_enumerator && !pub->die->die_mark)
8359 continue;
8360
8361 /* We shouldn't see pubnames for DIEs outside of the main CU. */
8362 if (names == pubname_table)
8363 gcc_assert (pub->die->die_mark);
8364
8365 if (names != pubtype_table
8366 || pub->die->die_offset != 0
8367 || !flag_eliminate_unused_debug_types)
8368 {
8369 dw_offset die_offset = pub->die->die_offset;
8370
8371 /* If we're putting types in their own .debug_types sections,
8372 the .debug_pubtypes table will still point to the compile
8373 unit (not the type unit), so we want to use the offset of
8374 the skeleton DIE (if there is one). */
8375 if (pub->die->comdat_type_p && names == pubtype_table)
8376 {
8377 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
8378
8379 if (type_node != NULL)
8380 die_offset = (type_node->skeleton_die != NULL
8381 ? type_node->skeleton_die->die_offset
8382 : 0);
8383 }
8384
8385 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
8386
8387 dw2_asm_output_nstring (pub->name, -1, "external name");
8388 }
8389 }
8390
8391 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8392 }
8393
8394 /* Output the information that goes into the .debug_aranges table.
8395 Namely, define the beginning and ending address range of the
8396 text section generated for this compilation unit. */
8397
8398 static void
8399 output_aranges (unsigned long aranges_length)
8400 {
8401 unsigned i;
8402
8403 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8404 dw2_asm_output_data (4, 0xffffffff,
8405 "Initial length escape value indicating 64-bit DWARF extension");
8406 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8407 "Length of Address Ranges Info");
8408 /* Version number for aranges is still 2, even in DWARF3. */
8409 dw2_asm_output_data (2, 2, "DWARF Version");
8410 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8411 debug_info_section,
8412 "Offset of Compilation Unit Info");
8413 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8414 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8415
8416 /* We need to align to twice the pointer size here. */
8417 if (DWARF_ARANGES_PAD_SIZE)
8418 {
8419 /* Pad using a 2 byte words so that padding is correct for any
8420 pointer size. */
8421 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8422 2 * DWARF2_ADDR_SIZE);
8423 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8424 dw2_asm_output_data (2, 0, NULL);
8425 }
8426
8427 /* It is necessary not to output these entries if the sections were
8428 not used; if the sections were not used, the length will be 0 and
8429 the address may end up as 0 if the section is discarded by ld
8430 --gc-sections, leaving an invalid (0, 0) entry that can be
8431 confused with the terminator. */
8432 if (text_section_used)
8433 {
8434 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8435 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8436 text_section_label, "Length");
8437 }
8438 if (cold_text_section_used)
8439 {
8440 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8441 "Address");
8442 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8443 cold_text_section_label, "Length");
8444 }
8445
8446 if (have_multiple_function_sections)
8447 {
8448 unsigned fde_idx;
8449 dw_fde_ref fde;
8450
8451 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8452 {
8453 if (DECL_IGNORED_P (fde->decl))
8454 continue;
8455 if (!fde->in_std_section)
8456 {
8457 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8458 "Address");
8459 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8460 fde->dw_fde_begin, "Length");
8461 }
8462 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8463 {
8464 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8465 "Address");
8466 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8467 fde->dw_fde_second_begin, "Length");
8468 }
8469 }
8470 }
8471
8472 /* Output the terminator words. */
8473 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8474 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8475 }
8476
8477 /* Add a new entry to .debug_ranges. Return the offset at which it
8478 was placed. */
8479
8480 static unsigned int
8481 add_ranges_num (int num)
8482 {
8483 unsigned int in_use = ranges_table_in_use;
8484
8485 if (in_use == ranges_table_allocated)
8486 {
8487 ranges_table_allocated += RANGES_TABLE_INCREMENT;
8488 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8489 ranges_table_allocated);
8490 memset (ranges_table + ranges_table_in_use, 0,
8491 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8492 }
8493
8494 ranges_table[in_use].num = num;
8495 ranges_table_in_use = in_use + 1;
8496
8497 return in_use * 2 * DWARF2_ADDR_SIZE;
8498 }
8499
8500 /* Add a new entry to .debug_ranges corresponding to a block, or a
8501 range terminator if BLOCK is NULL. */
8502
8503 static unsigned int
8504 add_ranges (const_tree block)
8505 {
8506 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8507 }
8508
8509 /* Add a new entry to .debug_ranges corresponding to a pair of
8510 labels. */
8511
8512 static void
8513 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8514 bool *added)
8515 {
8516 unsigned int in_use = ranges_by_label_in_use;
8517 unsigned int offset;
8518
8519 if (in_use == ranges_by_label_allocated)
8520 {
8521 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8522 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8523 ranges_by_label,
8524 ranges_by_label_allocated);
8525 memset (ranges_by_label + ranges_by_label_in_use, 0,
8526 RANGES_TABLE_INCREMENT
8527 * sizeof (struct dw_ranges_by_label_struct));
8528 }
8529
8530 ranges_by_label[in_use].begin = begin;
8531 ranges_by_label[in_use].end = end;
8532 ranges_by_label_in_use = in_use + 1;
8533
8534 offset = add_ranges_num (-(int)in_use - 1);
8535 if (!*added)
8536 {
8537 add_AT_range_list (die, DW_AT_ranges, offset);
8538 *added = true;
8539 }
8540 }
8541
8542 static void
8543 output_ranges (void)
8544 {
8545 unsigned i;
8546 static const char *const start_fmt = "Offset %#x";
8547 const char *fmt = start_fmt;
8548
8549 for (i = 0; i < ranges_table_in_use; i++)
8550 {
8551 int block_num = ranges_table[i].num;
8552
8553 if (block_num > 0)
8554 {
8555 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8556 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8557
8558 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8559 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8560
8561 /* If all code is in the text section, then the compilation
8562 unit base address defaults to DW_AT_low_pc, which is the
8563 base of the text section. */
8564 if (!have_multiple_function_sections)
8565 {
8566 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8567 text_section_label,
8568 fmt, i * 2 * DWARF2_ADDR_SIZE);
8569 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8570 text_section_label, NULL);
8571 }
8572
8573 /* Otherwise, the compilation unit base address is zero,
8574 which allows us to use absolute addresses, and not worry
8575 about whether the target supports cross-section
8576 arithmetic. */
8577 else
8578 {
8579 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8580 fmt, i * 2 * DWARF2_ADDR_SIZE);
8581 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8582 }
8583
8584 fmt = NULL;
8585 }
8586
8587 /* Negative block_num stands for an index into ranges_by_label. */
8588 else if (block_num < 0)
8589 {
8590 int lab_idx = - block_num - 1;
8591
8592 if (!have_multiple_function_sections)
8593 {
8594 gcc_unreachable ();
8595 #if 0
8596 /* If we ever use add_ranges_by_labels () for a single
8597 function section, all we have to do is to take out
8598 the #if 0 above. */
8599 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8600 ranges_by_label[lab_idx].begin,
8601 text_section_label,
8602 fmt, i * 2 * DWARF2_ADDR_SIZE);
8603 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8604 ranges_by_label[lab_idx].end,
8605 text_section_label, NULL);
8606 #endif
8607 }
8608 else
8609 {
8610 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8611 ranges_by_label[lab_idx].begin,
8612 fmt, i * 2 * DWARF2_ADDR_SIZE);
8613 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8614 ranges_by_label[lab_idx].end,
8615 NULL);
8616 }
8617 }
8618 else
8619 {
8620 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8621 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8622 fmt = start_fmt;
8623 }
8624 }
8625 }
8626
8627 /* Data structure containing information about input files. */
8628 struct file_info
8629 {
8630 const char *path; /* Complete file name. */
8631 const char *fname; /* File name part. */
8632 int length; /* Length of entire string. */
8633 struct dwarf_file_data * file_idx; /* Index in input file table. */
8634 int dir_idx; /* Index in directory table. */
8635 };
8636
8637 /* Data structure containing information about directories with source
8638 files. */
8639 struct dir_info
8640 {
8641 const char *path; /* Path including directory name. */
8642 int length; /* Path length. */
8643 int prefix; /* Index of directory entry which is a prefix. */
8644 int count; /* Number of files in this directory. */
8645 int dir_idx; /* Index of directory used as base. */
8646 };
8647
8648 /* Callback function for file_info comparison. We sort by looking at
8649 the directories in the path. */
8650
8651 static int
8652 file_info_cmp (const void *p1, const void *p2)
8653 {
8654 const struct file_info *const s1 = (const struct file_info *) p1;
8655 const struct file_info *const s2 = (const struct file_info *) p2;
8656 const unsigned char *cp1;
8657 const unsigned char *cp2;
8658
8659 /* Take care of file names without directories. We need to make sure that
8660 we return consistent values to qsort since some will get confused if
8661 we return the same value when identical operands are passed in opposite
8662 orders. So if neither has a directory, return 0 and otherwise return
8663 1 or -1 depending on which one has the directory. */
8664 if ((s1->path == s1->fname || s2->path == s2->fname))
8665 return (s2->path == s2->fname) - (s1->path == s1->fname);
8666
8667 cp1 = (const unsigned char *) s1->path;
8668 cp2 = (const unsigned char *) s2->path;
8669
8670 while (1)
8671 {
8672 ++cp1;
8673 ++cp2;
8674 /* Reached the end of the first path? If so, handle like above. */
8675 if ((cp1 == (const unsigned char *) s1->fname)
8676 || (cp2 == (const unsigned char *) s2->fname))
8677 return ((cp2 == (const unsigned char *) s2->fname)
8678 - (cp1 == (const unsigned char *) s1->fname));
8679
8680 /* Character of current path component the same? */
8681 else if (*cp1 != *cp2)
8682 return *cp1 - *cp2;
8683 }
8684 }
8685
8686 struct file_name_acquire_data
8687 {
8688 struct file_info *files;
8689 int used_files;
8690 int max_files;
8691 };
8692
8693 /* Traversal function for the hash table. */
8694
8695 static int
8696 file_name_acquire (void ** slot, void *data)
8697 {
8698 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8699 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8700 struct file_info *fi;
8701 const char *f;
8702
8703 gcc_assert (fnad->max_files >= d->emitted_number);
8704
8705 if (! d->emitted_number)
8706 return 1;
8707
8708 gcc_assert (fnad->max_files != fnad->used_files);
8709
8710 fi = fnad->files + fnad->used_files++;
8711
8712 /* Skip all leading "./". */
8713 f = d->filename;
8714 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8715 f += 2;
8716
8717 /* Create a new array entry. */
8718 fi->path = f;
8719 fi->length = strlen (f);
8720 fi->file_idx = d;
8721
8722 /* Search for the file name part. */
8723 f = strrchr (f, DIR_SEPARATOR);
8724 #if defined (DIR_SEPARATOR_2)
8725 {
8726 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8727
8728 if (g != NULL)
8729 {
8730 if (f == NULL || f < g)
8731 f = g;
8732 }
8733 }
8734 #endif
8735
8736 fi->fname = f == NULL ? fi->path : f + 1;
8737 return 1;
8738 }
8739
8740 /* Output the directory table and the file name table. We try to minimize
8741 the total amount of memory needed. A heuristic is used to avoid large
8742 slowdowns with many input files. */
8743
8744 static void
8745 output_file_names (void)
8746 {
8747 struct file_name_acquire_data fnad;
8748 int numfiles;
8749 struct file_info *files;
8750 struct dir_info *dirs;
8751 int *saved;
8752 int *savehere;
8753 int *backmap;
8754 int ndirs;
8755 int idx_offset;
8756 int i;
8757
8758 if (!last_emitted_file)
8759 {
8760 dw2_asm_output_data (1, 0, "End directory table");
8761 dw2_asm_output_data (1, 0, "End file name table");
8762 return;
8763 }
8764
8765 numfiles = last_emitted_file->emitted_number;
8766
8767 /* Allocate the various arrays we need. */
8768 files = XALLOCAVEC (struct file_info, numfiles);
8769 dirs = XALLOCAVEC (struct dir_info, numfiles);
8770
8771 fnad.files = files;
8772 fnad.used_files = 0;
8773 fnad.max_files = numfiles;
8774 htab_traverse (file_table, file_name_acquire, &fnad);
8775 gcc_assert (fnad.used_files == fnad.max_files);
8776
8777 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8778
8779 /* Find all the different directories used. */
8780 dirs[0].path = files[0].path;
8781 dirs[0].length = files[0].fname - files[0].path;
8782 dirs[0].prefix = -1;
8783 dirs[0].count = 1;
8784 dirs[0].dir_idx = 0;
8785 files[0].dir_idx = 0;
8786 ndirs = 1;
8787
8788 for (i = 1; i < numfiles; i++)
8789 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8790 && memcmp (dirs[ndirs - 1].path, files[i].path,
8791 dirs[ndirs - 1].length) == 0)
8792 {
8793 /* Same directory as last entry. */
8794 files[i].dir_idx = ndirs - 1;
8795 ++dirs[ndirs - 1].count;
8796 }
8797 else
8798 {
8799 int j;
8800
8801 /* This is a new directory. */
8802 dirs[ndirs].path = files[i].path;
8803 dirs[ndirs].length = files[i].fname - files[i].path;
8804 dirs[ndirs].count = 1;
8805 dirs[ndirs].dir_idx = ndirs;
8806 files[i].dir_idx = ndirs;
8807
8808 /* Search for a prefix. */
8809 dirs[ndirs].prefix = -1;
8810 for (j = 0; j < ndirs; j++)
8811 if (dirs[j].length < dirs[ndirs].length
8812 && dirs[j].length > 1
8813 && (dirs[ndirs].prefix == -1
8814 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8815 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8816 dirs[ndirs].prefix = j;
8817
8818 ++ndirs;
8819 }
8820
8821 /* Now to the actual work. We have to find a subset of the directories which
8822 allow expressing the file name using references to the directory table
8823 with the least amount of characters. We do not do an exhaustive search
8824 where we would have to check out every combination of every single
8825 possible prefix. Instead we use a heuristic which provides nearly optimal
8826 results in most cases and never is much off. */
8827 saved = XALLOCAVEC (int, ndirs);
8828 savehere = XALLOCAVEC (int, ndirs);
8829
8830 memset (saved, '\0', ndirs * sizeof (saved[0]));
8831 for (i = 0; i < ndirs; i++)
8832 {
8833 int j;
8834 int total;
8835
8836 /* We can always save some space for the current directory. But this
8837 does not mean it will be enough to justify adding the directory. */
8838 savehere[i] = dirs[i].length;
8839 total = (savehere[i] - saved[i]) * dirs[i].count;
8840
8841 for (j = i + 1; j < ndirs; j++)
8842 {
8843 savehere[j] = 0;
8844 if (saved[j] < dirs[i].length)
8845 {
8846 /* Determine whether the dirs[i] path is a prefix of the
8847 dirs[j] path. */
8848 int k;
8849
8850 k = dirs[j].prefix;
8851 while (k != -1 && k != (int) i)
8852 k = dirs[k].prefix;
8853
8854 if (k == (int) i)
8855 {
8856 /* Yes it is. We can possibly save some memory by
8857 writing the filenames in dirs[j] relative to
8858 dirs[i]. */
8859 savehere[j] = dirs[i].length;
8860 total += (savehere[j] - saved[j]) * dirs[j].count;
8861 }
8862 }
8863 }
8864
8865 /* Check whether we can save enough to justify adding the dirs[i]
8866 directory. */
8867 if (total > dirs[i].length + 1)
8868 {
8869 /* It's worthwhile adding. */
8870 for (j = i; j < ndirs; j++)
8871 if (savehere[j] > 0)
8872 {
8873 /* Remember how much we saved for this directory so far. */
8874 saved[j] = savehere[j];
8875
8876 /* Remember the prefix directory. */
8877 dirs[j].dir_idx = i;
8878 }
8879 }
8880 }
8881
8882 /* Emit the directory name table. */
8883 idx_offset = dirs[0].length > 0 ? 1 : 0;
8884 for (i = 1 - idx_offset; i < ndirs; i++)
8885 dw2_asm_output_nstring (dirs[i].path,
8886 dirs[i].length
8887 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
8888 "Directory Entry: %#x", i + idx_offset);
8889
8890 dw2_asm_output_data (1, 0, "End directory table");
8891
8892 /* We have to emit them in the order of emitted_number since that's
8893 used in the debug info generation. To do this efficiently we
8894 generate a back-mapping of the indices first. */
8895 backmap = XALLOCAVEC (int, numfiles);
8896 for (i = 0; i < numfiles; i++)
8897 backmap[files[i].file_idx->emitted_number - 1] = i;
8898
8899 /* Now write all the file names. */
8900 for (i = 0; i < numfiles; i++)
8901 {
8902 int file_idx = backmap[i];
8903 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8904
8905 #ifdef VMS_DEBUGGING_INFO
8906 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
8907
8908 /* Setting these fields can lead to debugger miscomparisons,
8909 but VMS Debug requires them to be set correctly. */
8910
8911 int ver;
8912 long long cdt;
8913 long siz;
8914 int maxfilelen = strlen (files[file_idx].path)
8915 + dirs[dir_idx].length
8916 + MAX_VMS_VERSION_LEN + 1;
8917 char *filebuf = XALLOCAVEC (char, maxfilelen);
8918
8919 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
8920 snprintf (filebuf, maxfilelen, "%s;%d",
8921 files[file_idx].path + dirs[dir_idx].length, ver);
8922
8923 dw2_asm_output_nstring
8924 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
8925
8926 /* Include directory index. */
8927 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8928
8929 /* Modification time. */
8930 dw2_asm_output_data_uleb128
8931 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
8932 ? cdt : 0,
8933 NULL);
8934
8935 /* File length in bytes. */
8936 dw2_asm_output_data_uleb128
8937 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
8938 ? siz : 0,
8939 NULL);
8940 #else
8941 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8942 "File Entry: %#x", (unsigned) i + 1);
8943
8944 /* Include directory index. */
8945 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8946
8947 /* Modification time. */
8948 dw2_asm_output_data_uleb128 (0, NULL);
8949
8950 /* File length in bytes. */
8951 dw2_asm_output_data_uleb128 (0, NULL);
8952 #endif /* VMS_DEBUGGING_INFO */
8953 }
8954
8955 dw2_asm_output_data (1, 0, "End file name table");
8956 }
8957
8958
8959 /* Output one line number table into the .debug_line section. */
8960
8961 static void
8962 output_one_line_info_table (dw_line_info_table *table)
8963 {
8964 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8965 unsigned int current_line = 1;
8966 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
8967 dw_line_info_entry *ent;
8968 size_t i;
8969
8970 FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
8971 {
8972 switch (ent->opcode)
8973 {
8974 case LI_set_address:
8975 /* ??? Unfortunately, we have little choice here currently, and
8976 must always use the most general form. GCC does not know the
8977 address delta itself, so we can't use DW_LNS_advance_pc. Many
8978 ports do have length attributes which will give an upper bound
8979 on the address range. We could perhaps use length attributes
8980 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
8981 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
8982
8983 /* This can handle any delta. This takes
8984 4+DWARF2_ADDR_SIZE bytes. */
8985 dw2_asm_output_data (1, 0, "set address %s", line_label);
8986 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8987 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8988 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8989 break;
8990
8991 case LI_set_line:
8992 if (ent->val == current_line)
8993 {
8994 /* We still need to start a new row, so output a copy insn. */
8995 dw2_asm_output_data (1, DW_LNS_copy,
8996 "copy line %u", current_line);
8997 }
8998 else
8999 {
9000 int line_offset = ent->val - current_line;
9001 int line_delta = line_offset - DWARF_LINE_BASE;
9002
9003 current_line = ent->val;
9004 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9005 {
9006 /* This can handle deltas from -10 to 234, using the current
9007 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9008 This takes 1 byte. */
9009 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9010 "line %u", current_line);
9011 }
9012 else
9013 {
9014 /* This can handle any delta. This takes at least 4 bytes,
9015 depending on the value being encoded. */
9016 dw2_asm_output_data (1, DW_LNS_advance_line,
9017 "advance to line %u", current_line);
9018 dw2_asm_output_data_sleb128 (line_offset, NULL);
9019 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9020 }
9021 }
9022 break;
9023
9024 case LI_set_file:
9025 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9026 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9027 break;
9028
9029 case LI_set_column:
9030 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9031 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9032 break;
9033
9034 case LI_negate_stmt:
9035 current_is_stmt = !current_is_stmt;
9036 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9037 "is_stmt %d", current_is_stmt);
9038 break;
9039
9040 case LI_set_prologue_end:
9041 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9042 "set prologue end");
9043 break;
9044
9045 case LI_set_epilogue_begin:
9046 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9047 "set epilogue begin");
9048 break;
9049
9050 case LI_set_discriminator:
9051 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9052 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9053 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9054 dw2_asm_output_data_uleb128 (ent->val, NULL);
9055 break;
9056 }
9057 }
9058
9059 /* Emit debug info for the address of the end of the table. */
9060 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9061 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9062 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9063 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9064
9065 dw2_asm_output_data (1, 0, "end sequence");
9066 dw2_asm_output_data_uleb128 (1, NULL);
9067 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9068 }
9069
9070 /* Output the source line number correspondence information. This
9071 information goes into the .debug_line section. */
9072
9073 static void
9074 output_line_info (void)
9075 {
9076 char l1[20], l2[20], p1[20], p2[20];
9077 int ver = dwarf_version;
9078 bool saw_one = false;
9079 int opc;
9080
9081 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9082 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9083 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9084 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9085
9086 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9087 dw2_asm_output_data (4, 0xffffffff,
9088 "Initial length escape value indicating 64-bit DWARF extension");
9089 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9090 "Length of Source Line Info");
9091 ASM_OUTPUT_LABEL (asm_out_file, l1);
9092
9093 dw2_asm_output_data (2, ver, "DWARF Version");
9094 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9095 ASM_OUTPUT_LABEL (asm_out_file, p1);
9096
9097 /* Define the architecture-dependent minimum instruction length (in bytes).
9098 In this implementation of DWARF, this field is used for information
9099 purposes only. Since GCC generates assembly language, we have no
9100 a priori knowledge of how many instruction bytes are generated for each
9101 source line, and therefore can use only the DW_LNE_set_address and
9102 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9103 this as '1', which is "correct enough" for all architectures,
9104 and don't let the target override. */
9105 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9106
9107 if (ver >= 4)
9108 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9109 "Maximum Operations Per Instruction");
9110 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9111 "Default is_stmt_start flag");
9112 dw2_asm_output_data (1, DWARF_LINE_BASE,
9113 "Line Base Value (Special Opcodes)");
9114 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9115 "Line Range Value (Special Opcodes)");
9116 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9117 "Special Opcode Base");
9118
9119 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9120 {
9121 int n_op_args;
9122 switch (opc)
9123 {
9124 case DW_LNS_advance_pc:
9125 case DW_LNS_advance_line:
9126 case DW_LNS_set_file:
9127 case DW_LNS_set_column:
9128 case DW_LNS_fixed_advance_pc:
9129 case DW_LNS_set_isa:
9130 n_op_args = 1;
9131 break;
9132 default:
9133 n_op_args = 0;
9134 break;
9135 }
9136
9137 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9138 opc, n_op_args);
9139 }
9140
9141 /* Write out the information about the files we use. */
9142 output_file_names ();
9143 ASM_OUTPUT_LABEL (asm_out_file, p2);
9144
9145 if (separate_line_info)
9146 {
9147 dw_line_info_table *table;
9148 size_t i;
9149
9150 FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9151 if (table->in_use)
9152 {
9153 output_one_line_info_table (table);
9154 saw_one = true;
9155 }
9156 }
9157 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9158 {
9159 output_one_line_info_table (cold_text_section_line_info);
9160 saw_one = true;
9161 }
9162
9163 /* ??? Some Darwin linkers crash on a .debug_line section with no
9164 sequences. Further, merely a DW_LNE_end_sequence entry is not
9165 sufficient -- the address column must also be initialized.
9166 Make sure to output at least one set_address/end_sequence pair,
9167 choosing .text since that section is always present. */
9168 if (text_section_line_info->in_use || !saw_one)
9169 output_one_line_info_table (text_section_line_info);
9170
9171 /* Output the marker for the end of the line number info. */
9172 ASM_OUTPUT_LABEL (asm_out_file, l2);
9173 }
9174 \f
9175 /* Given a pointer to a tree node for some base type, return a pointer to
9176 a DIE that describes the given type.
9177
9178 This routine must only be called for GCC type nodes that correspond to
9179 Dwarf base (fundamental) types. */
9180
9181 static dw_die_ref
9182 base_type_die (tree type)
9183 {
9184 dw_die_ref base_type_result;
9185 enum dwarf_type encoding;
9186
9187 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9188 return 0;
9189
9190 /* If this is a subtype that should not be emitted as a subrange type,
9191 use the base type. See subrange_type_for_debug_p. */
9192 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9193 type = TREE_TYPE (type);
9194
9195 switch (TREE_CODE (type))
9196 {
9197 case INTEGER_TYPE:
9198 if ((dwarf_version >= 4 || !dwarf_strict)
9199 && TYPE_NAME (type)
9200 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9201 && DECL_IS_BUILTIN (TYPE_NAME (type))
9202 && DECL_NAME (TYPE_NAME (type)))
9203 {
9204 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9205 if (strcmp (name, "char16_t") == 0
9206 || strcmp (name, "char32_t") == 0)
9207 {
9208 encoding = DW_ATE_UTF;
9209 break;
9210 }
9211 }
9212 if (TYPE_STRING_FLAG (type))
9213 {
9214 if (TYPE_UNSIGNED (type))
9215 encoding = DW_ATE_unsigned_char;
9216 else
9217 encoding = DW_ATE_signed_char;
9218 }
9219 else if (TYPE_UNSIGNED (type))
9220 encoding = DW_ATE_unsigned;
9221 else
9222 encoding = DW_ATE_signed;
9223 break;
9224
9225 case REAL_TYPE:
9226 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9227 {
9228 if (dwarf_version >= 3 || !dwarf_strict)
9229 encoding = DW_ATE_decimal_float;
9230 else
9231 encoding = DW_ATE_lo_user;
9232 }
9233 else
9234 encoding = DW_ATE_float;
9235 break;
9236
9237 case FIXED_POINT_TYPE:
9238 if (!(dwarf_version >= 3 || !dwarf_strict))
9239 encoding = DW_ATE_lo_user;
9240 else if (TYPE_UNSIGNED (type))
9241 encoding = DW_ATE_unsigned_fixed;
9242 else
9243 encoding = DW_ATE_signed_fixed;
9244 break;
9245
9246 /* Dwarf2 doesn't know anything about complex ints, so use
9247 a user defined type for it. */
9248 case COMPLEX_TYPE:
9249 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9250 encoding = DW_ATE_complex_float;
9251 else
9252 encoding = DW_ATE_lo_user;
9253 break;
9254
9255 case BOOLEAN_TYPE:
9256 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
9257 encoding = DW_ATE_boolean;
9258 break;
9259
9260 default:
9261 /* No other TREE_CODEs are Dwarf fundamental types. */
9262 gcc_unreachable ();
9263 }
9264
9265 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9266
9267 add_AT_unsigned (base_type_result, DW_AT_byte_size,
9268 int_size_in_bytes (type));
9269 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9270 add_pubtype (type, base_type_result);
9271
9272 return base_type_result;
9273 }
9274
9275 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9276 given input type is a Dwarf "fundamental" type. Otherwise return null. */
9277
9278 static inline int
9279 is_base_type (tree type)
9280 {
9281 switch (TREE_CODE (type))
9282 {
9283 case ERROR_MARK:
9284 case VOID_TYPE:
9285 case INTEGER_TYPE:
9286 case REAL_TYPE:
9287 case FIXED_POINT_TYPE:
9288 case COMPLEX_TYPE:
9289 case BOOLEAN_TYPE:
9290 return 1;
9291
9292 case ARRAY_TYPE:
9293 case RECORD_TYPE:
9294 case UNION_TYPE:
9295 case QUAL_UNION_TYPE:
9296 case ENUMERAL_TYPE:
9297 case FUNCTION_TYPE:
9298 case METHOD_TYPE:
9299 case POINTER_TYPE:
9300 case REFERENCE_TYPE:
9301 case NULLPTR_TYPE:
9302 case OFFSET_TYPE:
9303 case LANG_TYPE:
9304 case VECTOR_TYPE:
9305 return 0;
9306
9307 default:
9308 gcc_unreachable ();
9309 }
9310
9311 return 0;
9312 }
9313
9314 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9315 node, return the size in bits for the type if it is a constant, or else
9316 return the alignment for the type if the type's size is not constant, or
9317 else return BITS_PER_WORD if the type actually turns out to be an
9318 ERROR_MARK node. */
9319
9320 static inline unsigned HOST_WIDE_INT
9321 simple_type_size_in_bits (const_tree type)
9322 {
9323 if (TREE_CODE (type) == ERROR_MARK)
9324 return BITS_PER_WORD;
9325 else if (TYPE_SIZE (type) == NULL_TREE)
9326 return 0;
9327 else if (host_integerp (TYPE_SIZE (type), 1))
9328 return tree_low_cst (TYPE_SIZE (type), 1);
9329 else
9330 return TYPE_ALIGN (type);
9331 }
9332
9333 /* Similarly, but return a double_int instead of UHWI. */
9334
9335 static inline double_int
9336 double_int_type_size_in_bits (const_tree type)
9337 {
9338 if (TREE_CODE (type) == ERROR_MARK)
9339 return double_int::from_uhwi (BITS_PER_WORD);
9340 else if (TYPE_SIZE (type) == NULL_TREE)
9341 return double_int_zero;
9342 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9343 return tree_to_double_int (TYPE_SIZE (type));
9344 else
9345 return double_int::from_uhwi (TYPE_ALIGN (type));
9346 }
9347
9348 /* Given a pointer to a tree node for a subrange type, return a pointer
9349 to a DIE that describes the given type. */
9350
9351 static dw_die_ref
9352 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9353 {
9354 dw_die_ref subrange_die;
9355 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9356
9357 if (context_die == NULL)
9358 context_die = comp_unit_die ();
9359
9360 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9361
9362 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9363 {
9364 /* The size of the subrange type and its base type do not match,
9365 so we need to generate a size attribute for the subrange type. */
9366 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9367 }
9368
9369 if (low)
9370 add_bound_info (subrange_die, DW_AT_lower_bound, low);
9371 if (high)
9372 add_bound_info (subrange_die, DW_AT_upper_bound, high);
9373
9374 return subrange_die;
9375 }
9376
9377 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9378 entry that chains various modifiers in front of the given type. */
9379
9380 static dw_die_ref
9381 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9382 dw_die_ref context_die)
9383 {
9384 enum tree_code code = TREE_CODE (type);
9385 dw_die_ref mod_type_die;
9386 dw_die_ref sub_die = NULL;
9387 tree item_type = NULL;
9388 tree qualified_type;
9389 tree name, low, high;
9390 dw_die_ref mod_scope;
9391
9392 if (code == ERROR_MARK)
9393 return NULL;
9394
9395 /* See if we already have the appropriately qualified variant of
9396 this type. */
9397 qualified_type
9398 = get_qualified_type (type,
9399 ((is_const_type ? TYPE_QUAL_CONST : 0)
9400 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9401
9402 if (qualified_type == sizetype
9403 && TYPE_NAME (qualified_type)
9404 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9405 {
9406 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9407
9408 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9409 && TYPE_PRECISION (t)
9410 == TYPE_PRECISION (qualified_type)
9411 && TYPE_UNSIGNED (t)
9412 == TYPE_UNSIGNED (qualified_type));
9413 qualified_type = t;
9414 }
9415
9416 /* If we do, then we can just use its DIE, if it exists. */
9417 if (qualified_type)
9418 {
9419 mod_type_die = lookup_type_die (qualified_type);
9420 if (mod_type_die)
9421 return mod_type_die;
9422 }
9423
9424 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9425
9426 /* Handle C typedef types. */
9427 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9428 && !DECL_ARTIFICIAL (name))
9429 {
9430 tree dtype = TREE_TYPE (name);
9431
9432 if (qualified_type == dtype)
9433 {
9434 /* For a named type, use the typedef. */
9435 gen_type_die (qualified_type, context_die);
9436 return lookup_type_die (qualified_type);
9437 }
9438 else if (is_const_type < TYPE_READONLY (dtype)
9439 || is_volatile_type < TYPE_VOLATILE (dtype)
9440 || (is_const_type <= TYPE_READONLY (dtype)
9441 && is_volatile_type <= TYPE_VOLATILE (dtype)
9442 && DECL_ORIGINAL_TYPE (name) != type))
9443 /* cv-unqualified version of named type. Just use the unnamed
9444 type to which it refers. */
9445 return modified_type_die (DECL_ORIGINAL_TYPE (name),
9446 is_const_type, is_volatile_type,
9447 context_die);
9448 /* Else cv-qualified version of named type; fall through. */
9449 }
9450
9451 mod_scope = scope_die_for (type, context_die);
9452
9453 if (is_const_type
9454 /* If both is_const_type and is_volatile_type, prefer the path
9455 which leads to a qualified type. */
9456 && (!is_volatile_type
9457 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9458 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9459 {
9460 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
9461 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9462 }
9463 else if (is_volatile_type)
9464 {
9465 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
9466 sub_die = modified_type_die (type, is_const_type, 0, context_die);
9467 }
9468 else if (code == POINTER_TYPE)
9469 {
9470 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
9471 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9472 simple_type_size_in_bits (type) / BITS_PER_UNIT);
9473 item_type = TREE_TYPE (type);
9474 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9475 add_AT_unsigned (mod_type_die, DW_AT_address_class,
9476 TYPE_ADDR_SPACE (item_type));
9477 }
9478 else if (code == REFERENCE_TYPE)
9479 {
9480 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9481 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
9482 type);
9483 else
9484 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
9485 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9486 simple_type_size_in_bits (type) / BITS_PER_UNIT);
9487 item_type = TREE_TYPE (type);
9488 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9489 add_AT_unsigned (mod_type_die, DW_AT_address_class,
9490 TYPE_ADDR_SPACE (item_type));
9491 }
9492 else if (code == INTEGER_TYPE
9493 && TREE_TYPE (type) != NULL_TREE
9494 && subrange_type_for_debug_p (type, &low, &high))
9495 {
9496 mod_type_die = subrange_type_die (type, low, high, context_die);
9497 item_type = TREE_TYPE (type);
9498 }
9499 else if (is_base_type (type))
9500 mod_type_die = base_type_die (type);
9501 else
9502 {
9503 gen_type_die (type, context_die);
9504
9505 /* We have to get the type_main_variant here (and pass that to the
9506 `lookup_type_die' routine) because the ..._TYPE node we have
9507 might simply be a *copy* of some original type node (where the
9508 copy was created to help us keep track of typedef names) and
9509 that copy might have a different TYPE_UID from the original
9510 ..._TYPE node. */
9511 if (TREE_CODE (type) != VECTOR_TYPE)
9512 return lookup_type_die (type_main_variant (type));
9513 else
9514 /* Vectors have the debugging information in the type,
9515 not the main variant. */
9516 return lookup_type_die (type);
9517 }
9518
9519 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
9520 don't output a DW_TAG_typedef, since there isn't one in the
9521 user's program; just attach a DW_AT_name to the type.
9522 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9523 if the base type already has the same name. */
9524 if (name
9525 && ((TREE_CODE (name) != TYPE_DECL
9526 && (qualified_type == TYPE_MAIN_VARIANT (type)
9527 || (!is_const_type && !is_volatile_type)))
9528 || (TREE_CODE (name) == TYPE_DECL
9529 && TREE_TYPE (name) == qualified_type
9530 && DECL_NAME (name))))
9531 {
9532 if (TREE_CODE (name) == TYPE_DECL)
9533 /* Could just call add_name_and_src_coords_attributes here,
9534 but since this is a builtin type it doesn't have any
9535 useful source coordinates anyway. */
9536 name = DECL_NAME (name);
9537 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9538 }
9539 /* This probably indicates a bug. */
9540 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9541 {
9542 name = TYPE_NAME (type);
9543 if (name
9544 && TREE_CODE (name) == TYPE_DECL)
9545 name = DECL_NAME (name);
9546 add_name_attribute (mod_type_die,
9547 name ? IDENTIFIER_POINTER (name) : "__unknown__");
9548 }
9549
9550 if (qualified_type)
9551 equate_type_number_to_die (qualified_type, mod_type_die);
9552
9553 if (item_type)
9554 /* We must do this after the equate_type_number_to_die call, in case
9555 this is a recursive type. This ensures that the modified_type_die
9556 recursion will terminate even if the type is recursive. Recursive
9557 types are possible in Ada. */
9558 sub_die = modified_type_die (item_type,
9559 TYPE_READONLY (item_type),
9560 TYPE_VOLATILE (item_type),
9561 context_die);
9562
9563 if (sub_die != NULL)
9564 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9565
9566 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9567 if (TYPE_ARTIFICIAL (type))
9568 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9569
9570 return mod_type_die;
9571 }
9572
9573 /* Generate DIEs for the generic parameters of T.
9574 T must be either a generic type or a generic function.
9575 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
9576
9577 static void
9578 gen_generic_params_dies (tree t)
9579 {
9580 tree parms, args;
9581 int parms_num, i;
9582 dw_die_ref die = NULL;
9583
9584 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9585 return;
9586
9587 if (TYPE_P (t))
9588 die = lookup_type_die (t);
9589 else if (DECL_P (t))
9590 die = lookup_decl_die (t);
9591
9592 gcc_assert (die);
9593
9594 parms = lang_hooks.get_innermost_generic_parms (t);
9595 if (!parms)
9596 /* T has no generic parameter. It means T is neither a generic type
9597 or function. End of story. */
9598 return;
9599
9600 parms_num = TREE_VEC_LENGTH (parms);
9601 args = lang_hooks.get_innermost_generic_args (t);
9602 for (i = 0; i < parms_num; i++)
9603 {
9604 tree parm, arg, arg_pack_elems;
9605
9606 parm = TREE_VEC_ELT (parms, i);
9607 arg = TREE_VEC_ELT (args, i);
9608 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
9609 gcc_assert (parm && TREE_VALUE (parm) && arg);
9610
9611 if (parm && TREE_VALUE (parm) && arg)
9612 {
9613 /* If PARM represents a template parameter pack,
9614 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
9615 by DW_TAG_template_*_parameter DIEs for the argument
9616 pack elements of ARG. Note that ARG would then be
9617 an argument pack. */
9618 if (arg_pack_elems)
9619 template_parameter_pack_die (TREE_VALUE (parm),
9620 arg_pack_elems,
9621 die);
9622 else
9623 generic_parameter_die (TREE_VALUE (parm), arg,
9624 true /* Emit DW_AT_name */, die);
9625 }
9626 }
9627 }
9628
9629 /* Create and return a DIE for PARM which should be
9630 the representation of a generic type parameter.
9631 For instance, in the C++ front end, PARM would be a template parameter.
9632 ARG is the argument to PARM.
9633 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
9634 name of the PARM.
9635 PARENT_DIE is the parent DIE which the new created DIE should be added to,
9636 as a child node. */
9637
9638 static dw_die_ref
9639 generic_parameter_die (tree parm, tree arg,
9640 bool emit_name_p,
9641 dw_die_ref parent_die)
9642 {
9643 dw_die_ref tmpl_die = NULL;
9644 const char *name = NULL;
9645
9646 if (!parm || !DECL_NAME (parm) || !arg)
9647 return NULL;
9648
9649 /* We support non-type generic parameters and arguments,
9650 type generic parameters and arguments, as well as
9651 generic generic parameters (a.k.a. template template parameters in C++)
9652 and arguments. */
9653 if (TREE_CODE (parm) == PARM_DECL)
9654 /* PARM is a nontype generic parameter */
9655 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
9656 else if (TREE_CODE (parm) == TYPE_DECL)
9657 /* PARM is a type generic parameter. */
9658 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
9659 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9660 /* PARM is a generic generic parameter.
9661 Its DIE is a GNU extension. It shall have a
9662 DW_AT_name attribute to represent the name of the template template
9663 parameter, and a DW_AT_GNU_template_name attribute to represent the
9664 name of the template template argument. */
9665 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
9666 parent_die, parm);
9667 else
9668 gcc_unreachable ();
9669
9670 if (tmpl_die)
9671 {
9672 tree tmpl_type;
9673
9674 /* If PARM is a generic parameter pack, it means we are
9675 emitting debug info for a template argument pack element.
9676 In other terms, ARG is a template argument pack element.
9677 In that case, we don't emit any DW_AT_name attribute for
9678 the die. */
9679 if (emit_name_p)
9680 {
9681 name = IDENTIFIER_POINTER (DECL_NAME (parm));
9682 gcc_assert (name);
9683 add_AT_string (tmpl_die, DW_AT_name, name);
9684 }
9685
9686 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9687 {
9688 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
9689 TMPL_DIE should have a child DW_AT_type attribute that is set
9690 to the type of the argument to PARM, which is ARG.
9691 If PARM is a type generic parameter, TMPL_DIE should have a
9692 child DW_AT_type that is set to ARG. */
9693 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
9694 add_type_attribute (tmpl_die, tmpl_type, 0,
9695 TREE_THIS_VOLATILE (tmpl_type),
9696 parent_die);
9697 }
9698 else
9699 {
9700 /* So TMPL_DIE is a DIE representing a
9701 a generic generic template parameter, a.k.a template template
9702 parameter in C++ and arg is a template. */
9703
9704 /* The DW_AT_GNU_template_name attribute of the DIE must be set
9705 to the name of the argument. */
9706 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
9707 if (name)
9708 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
9709 }
9710
9711 if (TREE_CODE (parm) == PARM_DECL)
9712 /* So PARM is a non-type generic parameter.
9713 DWARF3 5.6.8 says we must set a DW_AT_const_value child
9714 attribute of TMPL_DIE which value represents the value
9715 of ARG.
9716 We must be careful here:
9717 The value of ARG might reference some function decls.
9718 We might currently be emitting debug info for a generic
9719 type and types are emitted before function decls, we don't
9720 know if the function decls referenced by ARG will actually be
9721 emitted after cgraph computations.
9722 So must defer the generation of the DW_AT_const_value to
9723 after cgraph is ready. */
9724 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
9725 }
9726
9727 return tmpl_die;
9728 }
9729
9730 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
9731 PARM_PACK must be a template parameter pack. The returned DIE
9732 will be child DIE of PARENT_DIE. */
9733
9734 static dw_die_ref
9735 template_parameter_pack_die (tree parm_pack,
9736 tree parm_pack_args,
9737 dw_die_ref parent_die)
9738 {
9739 dw_die_ref die;
9740 int j;
9741
9742 gcc_assert (parent_die && parm_pack);
9743
9744 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
9745 add_name_and_src_coords_attributes (die, parm_pack);
9746 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
9747 generic_parameter_die (parm_pack,
9748 TREE_VEC_ELT (parm_pack_args, j),
9749 false /* Don't emit DW_AT_name */,
9750 die);
9751 return die;
9752 }
9753
9754 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9755 an enumerated type. */
9756
9757 static inline int
9758 type_is_enum (const_tree type)
9759 {
9760 return TREE_CODE (type) == ENUMERAL_TYPE;
9761 }
9762
9763 /* Return the DBX register number described by a given RTL node. */
9764
9765 static unsigned int
9766 dbx_reg_number (const_rtx rtl)
9767 {
9768 unsigned regno = REGNO (rtl);
9769
9770 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9771
9772 #ifdef LEAF_REG_REMAP
9773 if (crtl->uses_only_leaf_regs)
9774 {
9775 int leaf_reg = LEAF_REG_REMAP (regno);
9776 if (leaf_reg != -1)
9777 regno = (unsigned) leaf_reg;
9778 }
9779 #endif
9780
9781 regno = DBX_REGISTER_NUMBER (regno);
9782 gcc_assert (regno != INVALID_REGNUM);
9783 return regno;
9784 }
9785
9786 /* Optionally add a DW_OP_piece term to a location description expression.
9787 DW_OP_piece is only added if the location description expression already
9788 doesn't end with DW_OP_piece. */
9789
9790 static void
9791 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9792 {
9793 dw_loc_descr_ref loc;
9794
9795 if (*list_head != NULL)
9796 {
9797 /* Find the end of the chain. */
9798 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9799 ;
9800
9801 if (loc->dw_loc_opc != DW_OP_piece)
9802 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9803 }
9804 }
9805
9806 /* Return a location descriptor that designates a machine register or
9807 zero if there is none. */
9808
9809 static dw_loc_descr_ref
9810 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9811 {
9812 rtx regs;
9813
9814 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9815 return 0;
9816
9817 /* We only use "frame base" when we're sure we're talking about the
9818 post-prologue local stack frame. We do this by *not* running
9819 register elimination until this point, and recognizing the special
9820 argument pointer and soft frame pointer rtx's.
9821 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
9822 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
9823 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
9824 {
9825 dw_loc_descr_ref result = NULL;
9826
9827 if (dwarf_version >= 4 || !dwarf_strict)
9828 {
9829 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
9830 initialized);
9831 if (result)
9832 add_loc_descr (&result,
9833 new_loc_descr (DW_OP_stack_value, 0, 0));
9834 }
9835 return result;
9836 }
9837
9838 regs = targetm.dwarf_register_span (rtl);
9839
9840 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9841 return multiple_reg_loc_descriptor (rtl, regs, initialized);
9842 else
9843 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9844 }
9845
9846 /* Return a location descriptor that designates a machine register for
9847 a given hard register number. */
9848
9849 static dw_loc_descr_ref
9850 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9851 {
9852 dw_loc_descr_ref reg_loc_descr;
9853
9854 if (regno <= 31)
9855 reg_loc_descr
9856 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
9857 else
9858 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
9859
9860 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9861 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9862
9863 return reg_loc_descr;
9864 }
9865
9866 /* Given an RTL of a register, return a location descriptor that
9867 designates a value that spans more than one register. */
9868
9869 static dw_loc_descr_ref
9870 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9871 enum var_init_status initialized)
9872 {
9873 int nregs, size, i;
9874 unsigned reg;
9875 dw_loc_descr_ref loc_result = NULL;
9876
9877 reg = REGNO (rtl);
9878 #ifdef LEAF_REG_REMAP
9879 if (crtl->uses_only_leaf_regs)
9880 {
9881 int leaf_reg = LEAF_REG_REMAP (reg);
9882 if (leaf_reg != -1)
9883 reg = (unsigned) leaf_reg;
9884 }
9885 #endif
9886 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9887 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9888
9889 /* Simple, contiguous registers. */
9890 if (regs == NULL_RTX)
9891 {
9892 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9893
9894 loc_result = NULL;
9895 while (nregs--)
9896 {
9897 dw_loc_descr_ref t;
9898
9899 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9900 VAR_INIT_STATUS_INITIALIZED);
9901 add_loc_descr (&loc_result, t);
9902 add_loc_descr_op_piece (&loc_result, size);
9903 ++reg;
9904 }
9905 return loc_result;
9906 }
9907
9908 /* Now onto stupid register sets in non contiguous locations. */
9909
9910 gcc_assert (GET_CODE (regs) == PARALLEL);
9911
9912 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9913 loc_result = NULL;
9914
9915 for (i = 0; i < XVECLEN (regs, 0); ++i)
9916 {
9917 dw_loc_descr_ref t;
9918
9919 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9920 VAR_INIT_STATUS_INITIALIZED);
9921 add_loc_descr (&loc_result, t);
9922 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9923 add_loc_descr_op_piece (&loc_result, size);
9924 }
9925
9926 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9927 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9928 return loc_result;
9929 }
9930
9931 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
9932
9933 /* Return a location descriptor that designates a constant i,
9934 as a compound operation from constant (i >> shift), constant shift
9935 and DW_OP_shl. */
9936
9937 static dw_loc_descr_ref
9938 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
9939 {
9940 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
9941 add_loc_descr (&ret, int_loc_descriptor (shift));
9942 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
9943 return ret;
9944 }
9945
9946 /* Return a location descriptor that designates a constant. */
9947
9948 static dw_loc_descr_ref
9949 int_loc_descriptor (HOST_WIDE_INT i)
9950 {
9951 enum dwarf_location_atom op;
9952
9953 /* Pick the smallest representation of a constant, rather than just
9954 defaulting to the LEB encoding. */
9955 if (i >= 0)
9956 {
9957 int clz = clz_hwi (i);
9958 int ctz = ctz_hwi (i);
9959 if (i <= 31)
9960 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
9961 else if (i <= 0xff)
9962 op = DW_OP_const1u;
9963 else if (i <= 0xffff)
9964 op = DW_OP_const2u;
9965 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
9966 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
9967 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
9968 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
9969 while DW_OP_const4u is 5 bytes. */
9970 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
9971 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
9972 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
9973 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
9974 while DW_OP_const4u is 5 bytes. */
9975 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
9976 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
9977 op = DW_OP_const4u;
9978 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
9979 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
9980 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
9981 while DW_OP_constu of constant >= 0x100000000 takes at least
9982 6 bytes. */
9983 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
9984 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
9985 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
9986 >= HOST_BITS_PER_WIDE_INT)
9987 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
9988 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
9989 while DW_OP_constu takes in this case at least 6 bytes. */
9990 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
9991 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
9992 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
9993 && size_of_uleb128 (i) > 6)
9994 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
9995 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
9996 else
9997 op = DW_OP_constu;
9998 }
9999 else
10000 {
10001 if (i >= -0x80)
10002 op = DW_OP_const1s;
10003 else if (i >= -0x8000)
10004 op = DW_OP_const2s;
10005 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10006 {
10007 if (size_of_int_loc_descriptor (i) < 5)
10008 {
10009 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10010 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10011 return ret;
10012 }
10013 op = DW_OP_const4s;
10014 }
10015 else
10016 {
10017 if (size_of_int_loc_descriptor (i)
10018 < (unsigned long) 1 + size_of_sleb128 (i))
10019 {
10020 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10021 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10022 return ret;
10023 }
10024 op = DW_OP_consts;
10025 }
10026 }
10027
10028 return new_loc_descr (op, i, 0);
10029 }
10030
10031 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10032 without actually allocating it. */
10033
10034 static unsigned long
10035 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10036 {
10037 return size_of_int_loc_descriptor (i >> shift)
10038 + size_of_int_loc_descriptor (shift)
10039 + 1;
10040 }
10041
10042 /* Return size_of_locs (int_loc_descriptor (i)) without
10043 actually allocating it. */
10044
10045 static unsigned long
10046 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10047 {
10048 unsigned long s;
10049
10050 if (i >= 0)
10051 {
10052 int clz, ctz;
10053 if (i <= 31)
10054 return 1;
10055 else if (i <= 0xff)
10056 return 2;
10057 else if (i <= 0xffff)
10058 return 3;
10059 clz = clz_hwi (i);
10060 ctz = ctz_hwi (i);
10061 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10062 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10063 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10064 - clz - 5);
10065 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10066 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10067 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10068 - clz - 8);
10069 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10070 return 5;
10071 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10072 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10073 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10074 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10075 - clz - 8);
10076 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10077 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10078 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10079 - clz - 16);
10080 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10081 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10082 && s > 6)
10083 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10084 - clz - 32);
10085 else
10086 return 1 + s;
10087 }
10088 else
10089 {
10090 if (i >= -0x80)
10091 return 2;
10092 else if (i >= -0x8000)
10093 return 3;
10094 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10095 {
10096 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10097 {
10098 s = size_of_int_loc_descriptor (-i) + 1;
10099 if (s < 5)
10100 return s;
10101 }
10102 return 5;
10103 }
10104 else
10105 {
10106 unsigned long r = 1 + size_of_sleb128 (i);
10107 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10108 {
10109 s = size_of_int_loc_descriptor (-i) + 1;
10110 if (s < r)
10111 return s;
10112 }
10113 return r;
10114 }
10115 }
10116 }
10117
10118 /* Return loc description representing "address" of integer value.
10119 This can appear only as toplevel expression. */
10120
10121 static dw_loc_descr_ref
10122 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10123 {
10124 int litsize;
10125 dw_loc_descr_ref loc_result = NULL;
10126
10127 if (!(dwarf_version >= 4 || !dwarf_strict))
10128 return NULL;
10129
10130 litsize = size_of_int_loc_descriptor (i);
10131 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10132 is more compact. For DW_OP_stack_value we need:
10133 litsize + 1 (DW_OP_stack_value)
10134 and for DW_OP_implicit_value:
10135 1 (DW_OP_implicit_value) + 1 (length) + size. */
10136 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10137 {
10138 loc_result = int_loc_descriptor (i);
10139 add_loc_descr (&loc_result,
10140 new_loc_descr (DW_OP_stack_value, 0, 0));
10141 return loc_result;
10142 }
10143
10144 loc_result = new_loc_descr (DW_OP_implicit_value,
10145 size, 0);
10146 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10147 loc_result->dw_loc_oprnd2.v.val_int = i;
10148 return loc_result;
10149 }
10150
10151 /* Return a location descriptor that designates a base+offset location. */
10152
10153 static dw_loc_descr_ref
10154 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10155 enum var_init_status initialized)
10156 {
10157 unsigned int regno;
10158 dw_loc_descr_ref result;
10159 dw_fde_ref fde = cfun->fde;
10160
10161 /* We only use "frame base" when we're sure we're talking about the
10162 post-prologue local stack frame. We do this by *not* running
10163 register elimination until this point, and recognizing the special
10164 argument pointer and soft frame pointer rtx's. */
10165 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10166 {
10167 rtx elim = (ira_use_lra_p
10168 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10169 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10170
10171 if (elim != reg)
10172 {
10173 if (GET_CODE (elim) == PLUS)
10174 {
10175 offset += INTVAL (XEXP (elim, 1));
10176 elim = XEXP (elim, 0);
10177 }
10178 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10179 && (elim == hard_frame_pointer_rtx
10180 || elim == stack_pointer_rtx))
10181 || elim == (frame_pointer_needed
10182 ? hard_frame_pointer_rtx
10183 : stack_pointer_rtx));
10184
10185 /* If drap register is used to align stack, use frame
10186 pointer + offset to access stack variables. If stack
10187 is aligned without drap, use stack pointer + offset to
10188 access stack variables. */
10189 if (crtl->stack_realign_tried
10190 && reg == frame_pointer_rtx)
10191 {
10192 int base_reg
10193 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10194 ? HARD_FRAME_POINTER_REGNUM
10195 : REGNO (elim));
10196 return new_reg_loc_descr (base_reg, offset);
10197 }
10198
10199 gcc_assert (frame_pointer_fb_offset_valid);
10200 offset += frame_pointer_fb_offset;
10201 return new_loc_descr (DW_OP_fbreg, offset, 0);
10202 }
10203 }
10204
10205 regno = DWARF_FRAME_REGNUM (REGNO (reg));
10206
10207 if (!optimize && fde
10208 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10209 {
10210 /* Use cfa+offset to represent the location of arguments passed
10211 on the stack when drap is used to align stack.
10212 Only do this when not optimizing, for optimized code var-tracking
10213 is supposed to track where the arguments live and the register
10214 used as vdrap or drap in some spot might be used for something
10215 else in other part of the routine. */
10216 return new_loc_descr (DW_OP_fbreg, offset, 0);
10217 }
10218
10219 if (regno <= 31)
10220 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10221 offset, 0);
10222 else
10223 result = new_loc_descr (DW_OP_bregx, regno, offset);
10224
10225 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10226 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10227
10228 return result;
10229 }
10230
10231 /* Return true if this RTL expression describes a base+offset calculation. */
10232
10233 static inline int
10234 is_based_loc (const_rtx rtl)
10235 {
10236 return (GET_CODE (rtl) == PLUS
10237 && ((REG_P (XEXP (rtl, 0))
10238 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10239 && CONST_INT_P (XEXP (rtl, 1)))));
10240 }
10241
10242 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10243 failed. */
10244
10245 static dw_loc_descr_ref
10246 tls_mem_loc_descriptor (rtx mem)
10247 {
10248 tree base;
10249 dw_loc_descr_ref loc_result;
10250
10251 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10252 return NULL;
10253
10254 base = get_base_address (MEM_EXPR (mem));
10255 if (base == NULL
10256 || TREE_CODE (base) != VAR_DECL
10257 || !DECL_THREAD_LOCAL_P (base))
10258 return NULL;
10259
10260 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10261 if (loc_result == NULL)
10262 return NULL;
10263
10264 if (MEM_OFFSET (mem))
10265 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10266
10267 return loc_result;
10268 }
10269
10270 /* Output debug info about reason why we failed to expand expression as dwarf
10271 expression. */
10272
10273 static void
10274 expansion_failed (tree expr, rtx rtl, char const *reason)
10275 {
10276 if (dump_file && (dump_flags & TDF_DETAILS))
10277 {
10278 fprintf (dump_file, "Failed to expand as dwarf: ");
10279 if (expr)
10280 print_generic_expr (dump_file, expr, dump_flags);
10281 if (rtl)
10282 {
10283 fprintf (dump_file, "\n");
10284 print_rtl (dump_file, rtl);
10285 }
10286 fprintf (dump_file, "\nReason: %s\n", reason);
10287 }
10288 }
10289
10290 /* Helper function for const_ok_for_output, called either directly
10291 or via for_each_rtx. */
10292
10293 static int
10294 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10295 {
10296 rtx rtl = *rtlp;
10297
10298 if (GET_CODE (rtl) == UNSPEC)
10299 {
10300 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10301 we can't express it in the debug info. */
10302 #ifdef ENABLE_CHECKING
10303 /* Don't complain about TLS UNSPECs, those are just too hard to
10304 delegitimize. Note this could be a non-decl SYMBOL_REF such as
10305 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
10306 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
10307 if (XVECLEN (rtl, 0) == 0
10308 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10309 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
10310 inform (current_function_decl
10311 ? DECL_SOURCE_LOCATION (current_function_decl)
10312 : UNKNOWN_LOCATION,
10313 #if NUM_UNSPEC_VALUES > 0
10314 "non-delegitimized UNSPEC %s (%d) found in variable location",
10315 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10316 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10317 XINT (rtl, 1));
10318 #else
10319 "non-delegitimized UNSPEC %d found in variable location",
10320 XINT (rtl, 1));
10321 #endif
10322 #endif
10323 expansion_failed (NULL_TREE, rtl,
10324 "UNSPEC hasn't been delegitimized.\n");
10325 return 1;
10326 }
10327
10328 if (targetm.const_not_ok_for_debug_p (rtl))
10329 {
10330 expansion_failed (NULL_TREE, rtl,
10331 "Expression rejected for debug by the backend.\n");
10332 return 1;
10333 }
10334
10335 if (GET_CODE (rtl) != SYMBOL_REF)
10336 return 0;
10337
10338 if (CONSTANT_POOL_ADDRESS_P (rtl))
10339 {
10340 bool marked;
10341 get_pool_constant_mark (rtl, &marked);
10342 /* If all references to this pool constant were optimized away,
10343 it was not output and thus we can't represent it. */
10344 if (!marked)
10345 {
10346 expansion_failed (NULL_TREE, rtl,
10347 "Constant was removed from constant pool.\n");
10348 return 1;
10349 }
10350 }
10351
10352 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10353 return 1;
10354
10355 /* Avoid references to external symbols in debug info, on several targets
10356 the linker might even refuse to link when linking a shared library,
10357 and in many other cases the relocations for .debug_info/.debug_loc are
10358 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
10359 to be defined within the same shared library or executable are fine. */
10360 if (SYMBOL_REF_EXTERNAL_P (rtl))
10361 {
10362 tree decl = SYMBOL_REF_DECL (rtl);
10363
10364 if (decl == NULL || !targetm.binds_local_p (decl))
10365 {
10366 expansion_failed (NULL_TREE, rtl,
10367 "Symbol not defined in current TU.\n");
10368 return 1;
10369 }
10370 }
10371
10372 return 0;
10373 }
10374
10375 /* Return true if constant RTL can be emitted in DW_OP_addr or
10376 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
10377 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
10378
10379 static bool
10380 const_ok_for_output (rtx rtl)
10381 {
10382 if (GET_CODE (rtl) == SYMBOL_REF)
10383 return const_ok_for_output_1 (&rtl, NULL) == 0;
10384
10385 if (GET_CODE (rtl) == CONST)
10386 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10387
10388 return true;
10389 }
10390
10391 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10392 if possible, NULL otherwise. */
10393
10394 static dw_die_ref
10395 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10396 {
10397 dw_die_ref type_die;
10398 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10399
10400 if (type == NULL)
10401 return NULL;
10402 switch (TREE_CODE (type))
10403 {
10404 case INTEGER_TYPE:
10405 case REAL_TYPE:
10406 break;
10407 default:
10408 return NULL;
10409 }
10410 type_die = lookup_type_die (type);
10411 if (!type_die)
10412 type_die = modified_type_die (type, false, false, comp_unit_die ());
10413 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10414 return NULL;
10415 return type_die;
10416 }
10417
10418 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10419 type matching MODE, or, if MODE is narrower than or as wide as
10420 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
10421 possible. */
10422
10423 static dw_loc_descr_ref
10424 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10425 {
10426 enum machine_mode outer_mode = mode;
10427 dw_die_ref type_die;
10428 dw_loc_descr_ref cvt;
10429
10430 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10431 {
10432 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10433 return op;
10434 }
10435 type_die = base_type_for_mode (outer_mode, 1);
10436 if (type_die == NULL)
10437 return NULL;
10438 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10439 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10440 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10441 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10442 add_loc_descr (&op, cvt);
10443 return op;
10444 }
10445
10446 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
10447
10448 static dw_loc_descr_ref
10449 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10450 dw_loc_descr_ref op1)
10451 {
10452 dw_loc_descr_ref ret = op0;
10453 add_loc_descr (&ret, op1);
10454 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10455 if (STORE_FLAG_VALUE != 1)
10456 {
10457 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10458 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10459 }
10460 return ret;
10461 }
10462
10463 /* Return location descriptor for signed comparison OP RTL. */
10464
10465 static dw_loc_descr_ref
10466 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10467 enum machine_mode mem_mode)
10468 {
10469 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10470 dw_loc_descr_ref op0, op1;
10471 int shift;
10472
10473 if (op_mode == VOIDmode)
10474 op_mode = GET_MODE (XEXP (rtl, 1));
10475 if (op_mode == VOIDmode)
10476 return NULL;
10477
10478 if (dwarf_strict
10479 && (GET_MODE_CLASS (op_mode) != MODE_INT
10480 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10481 return NULL;
10482
10483 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10484 VAR_INIT_STATUS_INITIALIZED);
10485 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10486 VAR_INIT_STATUS_INITIALIZED);
10487
10488 if (op0 == NULL || op1 == NULL)
10489 return NULL;
10490
10491 if (GET_MODE_CLASS (op_mode) != MODE_INT
10492 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10493 return compare_loc_descriptor (op, op0, op1);
10494
10495 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10496 {
10497 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10498 dw_loc_descr_ref cvt;
10499
10500 if (type_die == NULL)
10501 return NULL;
10502 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10503 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10504 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10505 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10506 add_loc_descr (&op0, cvt);
10507 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10508 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10509 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10510 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10511 add_loc_descr (&op1, cvt);
10512 return compare_loc_descriptor (op, op0, op1);
10513 }
10514
10515 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10516 /* For eq/ne, if the operands are known to be zero-extended,
10517 there is no need to do the fancy shifting up. */
10518 if (op == DW_OP_eq || op == DW_OP_ne)
10519 {
10520 dw_loc_descr_ref last0, last1;
10521 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10522 ;
10523 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10524 ;
10525 /* deref_size zero extends, and for constants we can check
10526 whether they are zero extended or not. */
10527 if (((last0->dw_loc_opc == DW_OP_deref_size
10528 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10529 || (CONST_INT_P (XEXP (rtl, 0))
10530 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10531 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10532 && ((last1->dw_loc_opc == DW_OP_deref_size
10533 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10534 || (CONST_INT_P (XEXP (rtl, 1))
10535 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10536 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10537 return compare_loc_descriptor (op, op0, op1);
10538
10539 /* EQ/NE comparison against constant in narrower type than
10540 DWARF2_ADDR_SIZE can be performed either as
10541 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10542 DW_OP_{eq,ne}
10543 or
10544 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10545 DW_OP_{eq,ne}. Pick whatever is shorter. */
10546 if (CONST_INT_P (XEXP (rtl, 1))
10547 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10548 && (size_of_int_loc_descriptor (shift) + 1
10549 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10550 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10551 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10552 & GET_MODE_MASK (op_mode))))
10553 {
10554 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10555 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10556 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10557 & GET_MODE_MASK (op_mode));
10558 return compare_loc_descriptor (op, op0, op1);
10559 }
10560 }
10561 add_loc_descr (&op0, int_loc_descriptor (shift));
10562 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10563 if (CONST_INT_P (XEXP (rtl, 1)))
10564 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10565 else
10566 {
10567 add_loc_descr (&op1, int_loc_descriptor (shift));
10568 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10569 }
10570 return compare_loc_descriptor (op, op0, op1);
10571 }
10572
10573 /* Return location descriptor for unsigned comparison OP RTL. */
10574
10575 static dw_loc_descr_ref
10576 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10577 enum machine_mode mem_mode)
10578 {
10579 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10580 dw_loc_descr_ref op0, op1;
10581
10582 if (op_mode == VOIDmode)
10583 op_mode = GET_MODE (XEXP (rtl, 1));
10584 if (op_mode == VOIDmode)
10585 return NULL;
10586 if (GET_MODE_CLASS (op_mode) != MODE_INT)
10587 return NULL;
10588
10589 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10590 return NULL;
10591
10592 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10593 VAR_INIT_STATUS_INITIALIZED);
10594 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10595 VAR_INIT_STATUS_INITIALIZED);
10596
10597 if (op0 == NULL || op1 == NULL)
10598 return NULL;
10599
10600 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
10601 {
10602 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
10603 dw_loc_descr_ref last0, last1;
10604 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10605 ;
10606 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10607 ;
10608 if (CONST_INT_P (XEXP (rtl, 0)))
10609 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
10610 /* deref_size zero extends, so no need to mask it again. */
10611 else if (last0->dw_loc_opc != DW_OP_deref_size
10612 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10613 {
10614 add_loc_descr (&op0, int_loc_descriptor (mask));
10615 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10616 }
10617 if (CONST_INT_P (XEXP (rtl, 1)))
10618 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
10619 /* deref_size zero extends, so no need to mask it again. */
10620 else if (last1->dw_loc_opc != DW_OP_deref_size
10621 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10622 {
10623 add_loc_descr (&op1, int_loc_descriptor (mask));
10624 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10625 }
10626 }
10627 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10628 {
10629 HOST_WIDE_INT bias = 1;
10630 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10631 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10632 if (CONST_INT_P (XEXP (rtl, 1)))
10633 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
10634 + INTVAL (XEXP (rtl, 1)));
10635 else
10636 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
10637 bias, 0));
10638 }
10639 return compare_loc_descriptor (op, op0, op1);
10640 }
10641
10642 /* Return location descriptor for {U,S}{MIN,MAX}. */
10643
10644 static dw_loc_descr_ref
10645 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
10646 enum machine_mode mem_mode)
10647 {
10648 enum dwarf_location_atom op;
10649 dw_loc_descr_ref op0, op1, ret;
10650 dw_loc_descr_ref bra_node, drop_node;
10651
10652 if (dwarf_strict
10653 && (GET_MODE_CLASS (mode) != MODE_INT
10654 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
10655 return NULL;
10656
10657 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10658 VAR_INIT_STATUS_INITIALIZED);
10659 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10660 VAR_INIT_STATUS_INITIALIZED);
10661
10662 if (op0 == NULL || op1 == NULL)
10663 return NULL;
10664
10665 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
10666 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
10667 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
10668 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
10669 {
10670 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10671 {
10672 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10673 add_loc_descr (&op0, int_loc_descriptor (mask));
10674 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10675 add_loc_descr (&op1, int_loc_descriptor (mask));
10676 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10677 }
10678 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
10679 {
10680 HOST_WIDE_INT bias = 1;
10681 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10682 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10683 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10684 }
10685 }
10686 else if (GET_MODE_CLASS (mode) == MODE_INT
10687 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10688 {
10689 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
10690 add_loc_descr (&op0, int_loc_descriptor (shift));
10691 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10692 add_loc_descr (&op1, int_loc_descriptor (shift));
10693 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10694 }
10695 else if (GET_MODE_CLASS (mode) == MODE_INT
10696 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10697 {
10698 dw_die_ref type_die = base_type_for_mode (mode, 0);
10699 dw_loc_descr_ref cvt;
10700 if (type_die == NULL)
10701 return NULL;
10702 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10703 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10704 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10705 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10706 add_loc_descr (&op0, cvt);
10707 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10708 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10709 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10710 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10711 add_loc_descr (&op1, cvt);
10712 }
10713
10714 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
10715 op = DW_OP_lt;
10716 else
10717 op = DW_OP_gt;
10718 ret = op0;
10719 add_loc_descr (&ret, op1);
10720 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10721 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10722 add_loc_descr (&ret, bra_node);
10723 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10724 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
10725 add_loc_descr (&ret, drop_node);
10726 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10727 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
10728 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
10729 && GET_MODE_CLASS (mode) == MODE_INT
10730 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10731 ret = convert_descriptor_to_mode (mode, ret);
10732 return ret;
10733 }
10734
10735 /* Helper function for mem_loc_descriptor. Perform OP binary op,
10736 but after converting arguments to type_die, afterwards
10737 convert back to unsigned. */
10738
10739 static dw_loc_descr_ref
10740 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
10741 enum machine_mode mode, enum machine_mode mem_mode)
10742 {
10743 dw_loc_descr_ref cvt, op0, op1;
10744
10745 if (type_die == NULL)
10746 return NULL;
10747 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10748 VAR_INIT_STATUS_INITIALIZED);
10749 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10750 VAR_INIT_STATUS_INITIALIZED);
10751 if (op0 == NULL || op1 == NULL)
10752 return NULL;
10753 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10754 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10755 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10756 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10757 add_loc_descr (&op0, cvt);
10758 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10759 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10760 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10761 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10762 add_loc_descr (&op1, cvt);
10763 add_loc_descr (&op0, op1);
10764 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
10765 return convert_descriptor_to_mode (mode, op0);
10766 }
10767
10768 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
10769 const0 is DW_OP_lit0 or corresponding typed constant,
10770 const1 is DW_OP_lit1 or corresponding typed constant
10771 and constMSB is constant with just the MSB bit set
10772 for the mode):
10773 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10774 L1: const0 DW_OP_swap
10775 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
10776 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10777 L3: DW_OP_drop
10778 L4: DW_OP_nop
10779
10780 CTZ is similar:
10781 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10782 L1: const0 DW_OP_swap
10783 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
10784 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10785 L3: DW_OP_drop
10786 L4: DW_OP_nop
10787
10788 FFS is similar:
10789 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
10790 L1: const1 DW_OP_swap
10791 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
10792 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10793 L3: DW_OP_drop
10794 L4: DW_OP_nop */
10795
10796 static dw_loc_descr_ref
10797 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
10798 enum machine_mode mem_mode)
10799 {
10800 dw_loc_descr_ref op0, ret, tmp;
10801 HOST_WIDE_INT valv;
10802 dw_loc_descr_ref l1jump, l1label;
10803 dw_loc_descr_ref l2jump, l2label;
10804 dw_loc_descr_ref l3jump, l3label;
10805 dw_loc_descr_ref l4jump, l4label;
10806 rtx msb;
10807
10808 if (GET_MODE_CLASS (mode) != MODE_INT
10809 || GET_MODE (XEXP (rtl, 0)) != mode
10810 || (GET_CODE (rtl) == CLZ
10811 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
10812 return NULL;
10813
10814 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10815 VAR_INIT_STATUS_INITIALIZED);
10816 if (op0 == NULL)
10817 return NULL;
10818 ret = op0;
10819 if (GET_CODE (rtl) == CLZ)
10820 {
10821 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
10822 valv = GET_MODE_BITSIZE (mode);
10823 }
10824 else if (GET_CODE (rtl) == FFS)
10825 valv = 0;
10826 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
10827 valv = GET_MODE_BITSIZE (mode);
10828 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
10829 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
10830 add_loc_descr (&ret, l1jump);
10831 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
10832 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
10833 VAR_INIT_STATUS_INITIALIZED);
10834 if (tmp == NULL)
10835 return NULL;
10836 add_loc_descr (&ret, tmp);
10837 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
10838 add_loc_descr (&ret, l4jump);
10839 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
10840 ? const1_rtx : const0_rtx,
10841 mode, mem_mode,
10842 VAR_INIT_STATUS_INITIALIZED);
10843 if (l1label == NULL)
10844 return NULL;
10845 add_loc_descr (&ret, l1label);
10846 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10847 l2label = new_loc_descr (DW_OP_dup, 0, 0);
10848 add_loc_descr (&ret, l2label);
10849 if (GET_CODE (rtl) != CLZ)
10850 msb = const1_rtx;
10851 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10852 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
10853 << (GET_MODE_BITSIZE (mode) - 1));
10854 else
10855 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
10856 << (GET_MODE_BITSIZE (mode)
10857 - HOST_BITS_PER_WIDE_INT - 1), mode);
10858 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
10859 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
10860 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
10861 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
10862 else
10863 tmp = mem_loc_descriptor (msb, mode, mem_mode,
10864 VAR_INIT_STATUS_INITIALIZED);
10865 if (tmp == NULL)
10866 return NULL;
10867 add_loc_descr (&ret, tmp);
10868 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
10869 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
10870 add_loc_descr (&ret, l3jump);
10871 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
10872 VAR_INIT_STATUS_INITIALIZED);
10873 if (tmp == NULL)
10874 return NULL;
10875 add_loc_descr (&ret, tmp);
10876 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
10877 ? DW_OP_shl : DW_OP_shr, 0, 0));
10878 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10879 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
10880 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10881 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
10882 add_loc_descr (&ret, l2jump);
10883 l3label = new_loc_descr (DW_OP_drop, 0, 0);
10884 add_loc_descr (&ret, l3label);
10885 l4label = new_loc_descr (DW_OP_nop, 0, 0);
10886 add_loc_descr (&ret, l4label);
10887 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10888 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
10889 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10890 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
10891 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10892 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
10893 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10894 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
10895 return ret;
10896 }
10897
10898 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
10899 const1 is DW_OP_lit1 or corresponding typed constant):
10900 const0 DW_OP_swap
10901 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
10902 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
10903 L2: DW_OP_drop
10904
10905 PARITY is similar:
10906 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
10907 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
10908 L2: DW_OP_drop */
10909
10910 static dw_loc_descr_ref
10911 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
10912 enum machine_mode mem_mode)
10913 {
10914 dw_loc_descr_ref op0, ret, tmp;
10915 dw_loc_descr_ref l1jump, l1label;
10916 dw_loc_descr_ref l2jump, l2label;
10917
10918 if (GET_MODE_CLASS (mode) != MODE_INT
10919 || GET_MODE (XEXP (rtl, 0)) != mode)
10920 return NULL;
10921
10922 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10923 VAR_INIT_STATUS_INITIALIZED);
10924 if (op0 == NULL)
10925 return NULL;
10926 ret = op0;
10927 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
10928 VAR_INIT_STATUS_INITIALIZED);
10929 if (tmp == NULL)
10930 return NULL;
10931 add_loc_descr (&ret, tmp);
10932 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10933 l1label = new_loc_descr (DW_OP_dup, 0, 0);
10934 add_loc_descr (&ret, l1label);
10935 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
10936 add_loc_descr (&ret, l2jump);
10937 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
10938 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
10939 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
10940 VAR_INIT_STATUS_INITIALIZED);
10941 if (tmp == NULL)
10942 return NULL;
10943 add_loc_descr (&ret, tmp);
10944 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
10945 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
10946 ? DW_OP_plus : DW_OP_xor, 0, 0));
10947 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10948 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
10949 VAR_INIT_STATUS_INITIALIZED);
10950 add_loc_descr (&ret, tmp);
10951 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
10952 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
10953 add_loc_descr (&ret, l1jump);
10954 l2label = new_loc_descr (DW_OP_drop, 0, 0);
10955 add_loc_descr (&ret, l2label);
10956 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10957 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
10958 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10959 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
10960 return ret;
10961 }
10962
10963 /* BSWAP (constS is initial shift count, either 56 or 24):
10964 constS const0
10965 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
10966 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
10967 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
10968 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
10969 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
10970
10971 static dw_loc_descr_ref
10972 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
10973 enum machine_mode mem_mode)
10974 {
10975 dw_loc_descr_ref op0, ret, tmp;
10976 dw_loc_descr_ref l1jump, l1label;
10977 dw_loc_descr_ref l2jump, l2label;
10978
10979 if (GET_MODE_CLASS (mode) != MODE_INT
10980 || BITS_PER_UNIT != 8
10981 || (GET_MODE_BITSIZE (mode) != 32
10982 && GET_MODE_BITSIZE (mode) != 64))
10983 return NULL;
10984
10985 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10986 VAR_INIT_STATUS_INITIALIZED);
10987 if (op0 == NULL)
10988 return NULL;
10989
10990 ret = op0;
10991 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
10992 mode, mem_mode,
10993 VAR_INIT_STATUS_INITIALIZED);
10994 if (tmp == NULL)
10995 return NULL;
10996 add_loc_descr (&ret, tmp);
10997 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
10998 VAR_INIT_STATUS_INITIALIZED);
10999 if (tmp == NULL)
11000 return NULL;
11001 add_loc_descr (&ret, tmp);
11002 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11003 add_loc_descr (&ret, l1label);
11004 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11005 mode, mem_mode,
11006 VAR_INIT_STATUS_INITIALIZED);
11007 add_loc_descr (&ret, tmp);
11008 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11009 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11010 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11011 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11012 VAR_INIT_STATUS_INITIALIZED);
11013 if (tmp == NULL)
11014 return NULL;
11015 add_loc_descr (&ret, tmp);
11016 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11017 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11018 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11019 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11020 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11021 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11022 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11023 VAR_INIT_STATUS_INITIALIZED);
11024 add_loc_descr (&ret, tmp);
11025 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11026 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11027 add_loc_descr (&ret, l2jump);
11028 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11029 VAR_INIT_STATUS_INITIALIZED);
11030 add_loc_descr (&ret, tmp);
11031 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11032 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11033 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11034 add_loc_descr (&ret, l1jump);
11035 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11036 add_loc_descr (&ret, l2label);
11037 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11038 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11039 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11040 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11041 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11042 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11043 return ret;
11044 }
11045
11046 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11047 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11048 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11049 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11050
11051 ROTATERT is similar:
11052 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11053 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11054 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11055
11056 static dw_loc_descr_ref
11057 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11058 enum machine_mode mem_mode)
11059 {
11060 rtx rtlop1 = XEXP (rtl, 1);
11061 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11062 int i;
11063
11064 if (GET_MODE_CLASS (mode) != MODE_INT)
11065 return NULL;
11066
11067 if (GET_MODE (rtlop1) != VOIDmode
11068 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11069 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11070 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11071 VAR_INIT_STATUS_INITIALIZED);
11072 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11073 VAR_INIT_STATUS_INITIALIZED);
11074 if (op0 == NULL || op1 == NULL)
11075 return NULL;
11076 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11077 for (i = 0; i < 2; i++)
11078 {
11079 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11080 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11081 mode, mem_mode,
11082 VAR_INIT_STATUS_INITIALIZED);
11083 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11084 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11085 ? DW_OP_const4u
11086 : HOST_BITS_PER_WIDE_INT == 64
11087 ? DW_OP_const8u : DW_OP_constu,
11088 GET_MODE_MASK (mode), 0);
11089 else
11090 mask[i] = NULL;
11091 if (mask[i] == NULL)
11092 return NULL;
11093 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11094 }
11095 ret = op0;
11096 add_loc_descr (&ret, op1);
11097 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11098 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11099 if (GET_CODE (rtl) == ROTATERT)
11100 {
11101 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11102 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11103 GET_MODE_BITSIZE (mode), 0));
11104 }
11105 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11106 if (mask[0] != NULL)
11107 add_loc_descr (&ret, mask[0]);
11108 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11109 if (mask[1] != NULL)
11110 {
11111 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11112 add_loc_descr (&ret, mask[1]);
11113 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11114 }
11115 if (GET_CODE (rtl) == ROTATE)
11116 {
11117 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11118 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11119 GET_MODE_BITSIZE (mode), 0));
11120 }
11121 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11122 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11123 return ret;
11124 }
11125
11126 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11127 for DEBUG_PARAMETER_REF RTL. */
11128
11129 static dw_loc_descr_ref
11130 parameter_ref_descriptor (rtx rtl)
11131 {
11132 dw_loc_descr_ref ret;
11133 dw_die_ref ref;
11134
11135 if (dwarf_strict)
11136 return NULL;
11137 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11138 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11139 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11140 if (ref)
11141 {
11142 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11143 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11144 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11145 }
11146 else
11147 {
11148 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11149 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11150 }
11151 return ret;
11152 }
11153
11154 /* The following routine converts the RTL for a variable or parameter
11155 (resident in memory) into an equivalent Dwarf representation of a
11156 mechanism for getting the address of that same variable onto the top of a
11157 hypothetical "address evaluation" stack.
11158
11159 When creating memory location descriptors, we are effectively transforming
11160 the RTL for a memory-resident object into its Dwarf postfix expression
11161 equivalent. This routine recursively descends an RTL tree, turning
11162 it into Dwarf postfix code as it goes.
11163
11164 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11165
11166 MEM_MODE is the mode of the memory reference, needed to handle some
11167 autoincrement addressing modes.
11168
11169 Return 0 if we can't represent the location. */
11170
11171 dw_loc_descr_ref
11172 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11173 enum machine_mode mem_mode,
11174 enum var_init_status initialized)
11175 {
11176 dw_loc_descr_ref mem_loc_result = NULL;
11177 enum dwarf_location_atom op;
11178 dw_loc_descr_ref op0, op1;
11179
11180 if (mode == VOIDmode)
11181 mode = GET_MODE (rtl);
11182
11183 /* Note that for a dynamically sized array, the location we will generate a
11184 description of here will be the lowest numbered location which is
11185 actually within the array. That's *not* necessarily the same as the
11186 zeroth element of the array. */
11187
11188 rtl = targetm.delegitimize_address (rtl);
11189
11190 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11191 return NULL;
11192
11193 switch (GET_CODE (rtl))
11194 {
11195 case POST_INC:
11196 case POST_DEC:
11197 case POST_MODIFY:
11198 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11199
11200 case SUBREG:
11201 /* The case of a subreg may arise when we have a local (register)
11202 variable or a formal (register) parameter which doesn't quite fill
11203 up an entire register. For now, just assume that it is
11204 legitimate to make the Dwarf info refer to the whole register which
11205 contains the given subreg. */
11206 if (!subreg_lowpart_p (rtl))
11207 break;
11208 if (GET_MODE_CLASS (mode) == MODE_INT
11209 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11210 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11211 #ifdef POINTERS_EXTEND_UNSIGNED
11212 || (mode == Pmode && mem_mode != VOIDmode)
11213 #endif
11214 )
11215 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11216 {
11217 mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11218 GET_MODE (SUBREG_REG (rtl)),
11219 mem_mode, initialized);
11220 break;
11221 }
11222 if (dwarf_strict)
11223 break;
11224 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11225 break;
11226 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11227 && (GET_MODE_CLASS (mode) != MODE_INT
11228 || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11229 break;
11230 else
11231 {
11232 dw_die_ref type_die;
11233 dw_loc_descr_ref cvt;
11234
11235 mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11236 GET_MODE (SUBREG_REG (rtl)),
11237 mem_mode, initialized);
11238 if (mem_loc_result == NULL)
11239 break;
11240 type_die = base_type_for_mode (mode,
11241 GET_MODE_CLASS (mode) == MODE_INT);
11242 if (type_die == NULL)
11243 {
11244 mem_loc_result = NULL;
11245 break;
11246 }
11247 if (GET_MODE_SIZE (mode)
11248 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11249 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11250 else
11251 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11252 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11253 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11254 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11255 add_loc_descr (&mem_loc_result, cvt);
11256 }
11257 break;
11258
11259 case REG:
11260 if (GET_MODE_CLASS (mode) != MODE_INT
11261 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11262 && rtl != arg_pointer_rtx
11263 && rtl != frame_pointer_rtx
11264 #ifdef POINTERS_EXTEND_UNSIGNED
11265 && (mode != Pmode || mem_mode == VOIDmode)
11266 #endif
11267 ))
11268 {
11269 dw_die_ref type_die;
11270
11271 if (dwarf_strict)
11272 break;
11273 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11274 break;
11275 type_die = base_type_for_mode (mode,
11276 GET_MODE_CLASS (mode) == MODE_INT);
11277 if (type_die == NULL)
11278 break;
11279 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11280 dbx_reg_number (rtl), 0);
11281 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11282 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11283 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11284 break;
11285 }
11286 /* Whenever a register number forms a part of the description of the
11287 method for calculating the (dynamic) address of a memory resident
11288 object, DWARF rules require the register number be referred to as
11289 a "base register". This distinction is not based in any way upon
11290 what category of register the hardware believes the given register
11291 belongs to. This is strictly DWARF terminology we're dealing with
11292 here. Note that in cases where the location of a memory-resident
11293 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11294 OP_CONST (0)) the actual DWARF location descriptor that we generate
11295 may just be OP_BASEREG (basereg). This may look deceptively like
11296 the object in question was allocated to a register (rather than in
11297 memory) so DWARF consumers need to be aware of the subtle
11298 distinction between OP_REG and OP_BASEREG. */
11299 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11300 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11301 else if (stack_realign_drap
11302 && crtl->drap_reg
11303 && crtl->args.internal_arg_pointer == rtl
11304 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11305 {
11306 /* If RTL is internal_arg_pointer, which has been optimized
11307 out, use DRAP instead. */
11308 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11309 VAR_INIT_STATUS_INITIALIZED);
11310 }
11311 break;
11312
11313 case SIGN_EXTEND:
11314 case ZERO_EXTEND:
11315 if (GET_MODE_CLASS (mode) != MODE_INT)
11316 break;
11317 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11318 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11319 if (op0 == 0)
11320 break;
11321 else if (GET_CODE (rtl) == ZERO_EXTEND
11322 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11323 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11324 < HOST_BITS_PER_WIDE_INT
11325 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11326 to expand zero extend as two shifts instead of
11327 masking. */
11328 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11329 {
11330 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11331 mem_loc_result = op0;
11332 add_loc_descr (&mem_loc_result,
11333 int_loc_descriptor (GET_MODE_MASK (imode)));
11334 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11335 }
11336 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11337 {
11338 int shift = DWARF2_ADDR_SIZE
11339 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11340 shift *= BITS_PER_UNIT;
11341 if (GET_CODE (rtl) == SIGN_EXTEND)
11342 op = DW_OP_shra;
11343 else
11344 op = DW_OP_shr;
11345 mem_loc_result = op0;
11346 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11347 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11348 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11349 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11350 }
11351 else if (!dwarf_strict)
11352 {
11353 dw_die_ref type_die1, type_die2;
11354 dw_loc_descr_ref cvt;
11355
11356 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11357 GET_CODE (rtl) == ZERO_EXTEND);
11358 if (type_die1 == NULL)
11359 break;
11360 type_die2 = base_type_for_mode (mode, 1);
11361 if (type_die2 == NULL)
11362 break;
11363 mem_loc_result = op0;
11364 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11365 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11366 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11367 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11368 add_loc_descr (&mem_loc_result, cvt);
11369 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11370 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11371 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11372 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11373 add_loc_descr (&mem_loc_result, cvt);
11374 }
11375 break;
11376
11377 case MEM:
11378 {
11379 rtx new_rtl = avoid_constant_pool_reference (rtl);
11380 if (new_rtl != rtl)
11381 {
11382 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
11383 initialized);
11384 if (mem_loc_result != NULL)
11385 return mem_loc_result;
11386 }
11387 }
11388 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11389 get_address_mode (rtl), mode,
11390 VAR_INIT_STATUS_INITIALIZED);
11391 if (mem_loc_result == NULL)
11392 mem_loc_result = tls_mem_loc_descriptor (rtl);
11393 if (mem_loc_result != NULL)
11394 {
11395 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11396 || GET_MODE_CLASS (mode) != MODE_INT)
11397 {
11398 dw_die_ref type_die;
11399 dw_loc_descr_ref deref;
11400
11401 if (dwarf_strict)
11402 return NULL;
11403 type_die
11404 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11405 if (type_die == NULL)
11406 return NULL;
11407 deref = new_loc_descr (DW_OP_GNU_deref_type,
11408 GET_MODE_SIZE (mode), 0);
11409 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11410 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11411 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11412 add_loc_descr (&mem_loc_result, deref);
11413 }
11414 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11415 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11416 else
11417 add_loc_descr (&mem_loc_result,
11418 new_loc_descr (DW_OP_deref_size,
11419 GET_MODE_SIZE (mode), 0));
11420 }
11421 break;
11422
11423 case LO_SUM:
11424 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11425
11426 case LABEL_REF:
11427 /* Some ports can transform a symbol ref into a label ref, because
11428 the symbol ref is too far away and has to be dumped into a constant
11429 pool. */
11430 case CONST:
11431 case SYMBOL_REF:
11432 if (GET_MODE_CLASS (mode) != MODE_INT
11433 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11434 #ifdef POINTERS_EXTEND_UNSIGNED
11435 && (mode != Pmode || mem_mode == VOIDmode)
11436 #endif
11437 ))
11438 break;
11439 if (GET_CODE (rtl) == SYMBOL_REF
11440 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11441 {
11442 dw_loc_descr_ref temp;
11443
11444 /* If this is not defined, we have no way to emit the data. */
11445 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11446 break;
11447
11448 /* We used to emit DW_OP_addr here, but that's wrong, since
11449 DW_OP_addr should be relocated by the debug info consumer,
11450 while DW_OP_GNU_push_tls_address operand should not. */
11451 temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11452 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11453 temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11454 temp->dw_loc_oprnd1.v.val_addr = rtl;
11455 temp->dtprel = true;
11456
11457 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11458 add_loc_descr (&mem_loc_result, temp);
11459
11460 break;
11461 }
11462
11463 if (!const_ok_for_output (rtl))
11464 break;
11465
11466 symref:
11467 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11468 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11469 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11470 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11471 break;
11472
11473 case CONCAT:
11474 case CONCATN:
11475 case VAR_LOCATION:
11476 case DEBUG_IMPLICIT_PTR:
11477 expansion_failed (NULL_TREE, rtl,
11478 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11479 return 0;
11480
11481 case ENTRY_VALUE:
11482 if (dwarf_strict)
11483 return NULL;
11484 if (REG_P (ENTRY_VALUE_EXP (rtl)))
11485 {
11486 if (GET_MODE_CLASS (mode) != MODE_INT
11487 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11488 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11489 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11490 else
11491 op0
11492 = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11493 VAR_INIT_STATUS_INITIALIZED);
11494 }
11495 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11496 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11497 {
11498 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11499 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11500 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11501 return NULL;
11502 }
11503 else
11504 gcc_unreachable ();
11505 if (op0 == NULL)
11506 return NULL;
11507 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11508 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11509 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11510 break;
11511
11512 case DEBUG_PARAMETER_REF:
11513 mem_loc_result = parameter_ref_descriptor (rtl);
11514 break;
11515
11516 case PRE_MODIFY:
11517 /* Extract the PLUS expression nested inside and fall into
11518 PLUS code below. */
11519 rtl = XEXP (rtl, 1);
11520 goto plus;
11521
11522 case PRE_INC:
11523 case PRE_DEC:
11524 /* Turn these into a PLUS expression and fall into the PLUS code
11525 below. */
11526 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11527 GEN_INT (GET_CODE (rtl) == PRE_INC
11528 ? GET_MODE_UNIT_SIZE (mem_mode)
11529 : -GET_MODE_UNIT_SIZE (mem_mode)));
11530
11531 /* ... fall through ... */
11532
11533 case PLUS:
11534 plus:
11535 if (is_based_loc (rtl)
11536 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11537 || XEXP (rtl, 0) == arg_pointer_rtx
11538 || XEXP (rtl, 0) == frame_pointer_rtx)
11539 && GET_MODE_CLASS (mode) == MODE_INT)
11540 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11541 INTVAL (XEXP (rtl, 1)),
11542 VAR_INIT_STATUS_INITIALIZED);
11543 else
11544 {
11545 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11546 VAR_INIT_STATUS_INITIALIZED);
11547 if (mem_loc_result == 0)
11548 break;
11549
11550 if (CONST_INT_P (XEXP (rtl, 1))
11551 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11552 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11553 else
11554 {
11555 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11556 VAR_INIT_STATUS_INITIALIZED);
11557 if (op1 == 0)
11558 break;
11559 add_loc_descr (&mem_loc_result, op1);
11560 add_loc_descr (&mem_loc_result,
11561 new_loc_descr (DW_OP_plus, 0, 0));
11562 }
11563 }
11564 break;
11565
11566 /* If a pseudo-reg is optimized away, it is possible for it to
11567 be replaced with a MEM containing a multiply or shift. */
11568 case MINUS:
11569 op = DW_OP_minus;
11570 goto do_binop;
11571
11572 case MULT:
11573 op = DW_OP_mul;
11574 goto do_binop;
11575
11576 case DIV:
11577 if (!dwarf_strict
11578 && GET_MODE_CLASS (mode) == MODE_INT
11579 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11580 {
11581 mem_loc_result = typed_binop (DW_OP_div, rtl,
11582 base_type_for_mode (mode, 0),
11583 mode, mem_mode);
11584 break;
11585 }
11586 op = DW_OP_div;
11587 goto do_binop;
11588
11589 case UMOD:
11590 op = DW_OP_mod;
11591 goto do_binop;
11592
11593 case ASHIFT:
11594 op = DW_OP_shl;
11595 goto do_shift;
11596
11597 case ASHIFTRT:
11598 op = DW_OP_shra;
11599 goto do_shift;
11600
11601 case LSHIFTRT:
11602 op = DW_OP_shr;
11603 goto do_shift;
11604
11605 do_shift:
11606 if (GET_MODE_CLASS (mode) != MODE_INT)
11607 break;
11608 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11609 VAR_INIT_STATUS_INITIALIZED);
11610 {
11611 rtx rtlop1 = XEXP (rtl, 1);
11612 if (GET_MODE (rtlop1) != VOIDmode
11613 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
11614 < GET_MODE_BITSIZE (mode))
11615 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11616 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11617 VAR_INIT_STATUS_INITIALIZED);
11618 }
11619
11620 if (op0 == 0 || op1 == 0)
11621 break;
11622
11623 mem_loc_result = op0;
11624 add_loc_descr (&mem_loc_result, op1);
11625 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11626 break;
11627
11628 case AND:
11629 op = DW_OP_and;
11630 goto do_binop;
11631
11632 case IOR:
11633 op = DW_OP_or;
11634 goto do_binop;
11635
11636 case XOR:
11637 op = DW_OP_xor;
11638 goto do_binop;
11639
11640 do_binop:
11641 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11642 VAR_INIT_STATUS_INITIALIZED);
11643 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11644 VAR_INIT_STATUS_INITIALIZED);
11645
11646 if (op0 == 0 || op1 == 0)
11647 break;
11648
11649 mem_loc_result = op0;
11650 add_loc_descr (&mem_loc_result, op1);
11651 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11652 break;
11653
11654 case MOD:
11655 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
11656 {
11657 mem_loc_result = typed_binop (DW_OP_mod, rtl,
11658 base_type_for_mode (mode, 0),
11659 mode, mem_mode);
11660 break;
11661 }
11662
11663 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11664 VAR_INIT_STATUS_INITIALIZED);
11665 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11666 VAR_INIT_STATUS_INITIALIZED);
11667
11668 if (op0 == 0 || op1 == 0)
11669 break;
11670
11671 mem_loc_result = op0;
11672 add_loc_descr (&mem_loc_result, op1);
11673 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11674 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11675 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
11676 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11677 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
11678 break;
11679
11680 case UDIV:
11681 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
11682 {
11683 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
11684 {
11685 op = DW_OP_div;
11686 goto do_binop;
11687 }
11688 mem_loc_result = typed_binop (DW_OP_div, rtl,
11689 base_type_for_mode (mode, 1),
11690 mode, mem_mode);
11691 }
11692 break;
11693
11694 case NOT:
11695 op = DW_OP_not;
11696 goto do_unop;
11697
11698 case ABS:
11699 op = DW_OP_abs;
11700 goto do_unop;
11701
11702 case NEG:
11703 op = DW_OP_neg;
11704 goto do_unop;
11705
11706 do_unop:
11707 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11708 VAR_INIT_STATUS_INITIALIZED);
11709
11710 if (op0 == 0)
11711 break;
11712
11713 mem_loc_result = op0;
11714 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11715 break;
11716
11717 case CONST_INT:
11718 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11719 #ifdef POINTERS_EXTEND_UNSIGNED
11720 || (mode == Pmode
11721 && mem_mode != VOIDmode
11722 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
11723 #endif
11724 )
11725 {
11726 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11727 break;
11728 }
11729 if (!dwarf_strict
11730 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
11731 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
11732 {
11733 dw_die_ref type_die = base_type_for_mode (mode, 1);
11734 enum machine_mode amode;
11735 if (type_die == NULL)
11736 return NULL;
11737 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
11738 MODE_INT, 0);
11739 if (INTVAL (rtl) >= 0
11740 && amode != BLKmode
11741 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
11742 /* const DW_OP_GNU_convert <XXX> vs.
11743 DW_OP_GNU_const_type <XXX, 1, const>. */
11744 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
11745 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
11746 {
11747 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11748 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11749 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11750 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11751 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
11752 add_loc_descr (&mem_loc_result, op0);
11753 return mem_loc_result;
11754 }
11755 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
11756 INTVAL (rtl));
11757 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11758 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11759 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11760 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11761 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11762 else
11763 {
11764 mem_loc_result->dw_loc_oprnd2.val_class
11765 = dw_val_class_const_double;
11766 mem_loc_result->dw_loc_oprnd2.v.val_double
11767 = double_int::from_shwi (INTVAL (rtl));
11768 }
11769 }
11770 break;
11771
11772 case CONST_DOUBLE:
11773 if (!dwarf_strict)
11774 {
11775 dw_die_ref type_die;
11776
11777 /* Note that a CONST_DOUBLE rtx could represent either an integer
11778 or a floating-point constant. A CONST_DOUBLE is used whenever
11779 the constant requires more than one word in order to be
11780 adequately represented. We output CONST_DOUBLEs as blocks. */
11781 if (mode == VOIDmode
11782 || (GET_MODE (rtl) == VOIDmode
11783 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
11784 break;
11785 type_die = base_type_for_mode (mode,
11786 GET_MODE_CLASS (mode) == MODE_INT);
11787 if (type_die == NULL)
11788 return NULL;
11789 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
11790 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11791 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11792 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11793 if (SCALAR_FLOAT_MODE_P (mode))
11794 {
11795 unsigned int length = GET_MODE_SIZE (mode);
11796 unsigned char *array
11797 = (unsigned char*) ggc_alloc_atomic (length);
11798
11799 insert_float (rtl, array);
11800 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
11801 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
11802 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
11803 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
11804 }
11805 else
11806 {
11807 mem_loc_result->dw_loc_oprnd2.val_class
11808 = dw_val_class_const_double;
11809 mem_loc_result->dw_loc_oprnd2.v.val_double
11810 = rtx_to_double_int (rtl);
11811 }
11812 }
11813 break;
11814
11815 case EQ:
11816 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
11817 break;
11818
11819 case GE:
11820 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
11821 break;
11822
11823 case GT:
11824 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
11825 break;
11826
11827 case LE:
11828 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
11829 break;
11830
11831 case LT:
11832 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
11833 break;
11834
11835 case NE:
11836 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
11837 break;
11838
11839 case GEU:
11840 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
11841 break;
11842
11843 case GTU:
11844 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
11845 break;
11846
11847 case LEU:
11848 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
11849 break;
11850
11851 case LTU:
11852 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
11853 break;
11854
11855 case UMIN:
11856 case UMAX:
11857 if (GET_MODE_CLASS (mode) != MODE_INT)
11858 break;
11859 /* FALLTHRU */
11860 case SMIN:
11861 case SMAX:
11862 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
11863 break;
11864
11865 case ZERO_EXTRACT:
11866 case SIGN_EXTRACT:
11867 if (CONST_INT_P (XEXP (rtl, 1))
11868 && CONST_INT_P (XEXP (rtl, 2))
11869 && ((unsigned) INTVAL (XEXP (rtl, 1))
11870 + (unsigned) INTVAL (XEXP (rtl, 2))
11871 <= GET_MODE_BITSIZE (mode))
11872 && GET_MODE_CLASS (mode) == MODE_INT
11873 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11874 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
11875 {
11876 int shift, size;
11877 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11878 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11879 if (op0 == 0)
11880 break;
11881 if (GET_CODE (rtl) == SIGN_EXTRACT)
11882 op = DW_OP_shra;
11883 else
11884 op = DW_OP_shr;
11885 mem_loc_result = op0;
11886 size = INTVAL (XEXP (rtl, 1));
11887 shift = INTVAL (XEXP (rtl, 2));
11888 if (BITS_BIG_ENDIAN)
11889 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11890 - shift - size;
11891 if (shift + size != (int) DWARF2_ADDR_SIZE)
11892 {
11893 add_loc_descr (&mem_loc_result,
11894 int_loc_descriptor (DWARF2_ADDR_SIZE
11895 - shift - size));
11896 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11897 }
11898 if (size != (int) DWARF2_ADDR_SIZE)
11899 {
11900 add_loc_descr (&mem_loc_result,
11901 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
11902 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11903 }
11904 }
11905 break;
11906
11907 case IF_THEN_ELSE:
11908 {
11909 dw_loc_descr_ref op2, bra_node, drop_node;
11910 op0 = mem_loc_descriptor (XEXP (rtl, 0),
11911 GET_MODE (XEXP (rtl, 0)) == VOIDmode
11912 ? word_mode : GET_MODE (XEXP (rtl, 0)),
11913 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11914 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11915 VAR_INIT_STATUS_INITIALIZED);
11916 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
11917 VAR_INIT_STATUS_INITIALIZED);
11918 if (op0 == NULL || op1 == NULL || op2 == NULL)
11919 break;
11920
11921 mem_loc_result = op1;
11922 add_loc_descr (&mem_loc_result, op2);
11923 add_loc_descr (&mem_loc_result, op0);
11924 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11925 add_loc_descr (&mem_loc_result, bra_node);
11926 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
11927 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11928 add_loc_descr (&mem_loc_result, drop_node);
11929 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11930 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11931 }
11932 break;
11933
11934 case FLOAT_EXTEND:
11935 case FLOAT_TRUNCATE:
11936 case FLOAT:
11937 case UNSIGNED_FLOAT:
11938 case FIX:
11939 case UNSIGNED_FIX:
11940 if (!dwarf_strict)
11941 {
11942 dw_die_ref type_die;
11943 dw_loc_descr_ref cvt;
11944
11945 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11946 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11947 if (op0 == NULL)
11948 break;
11949 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
11950 && (GET_CODE (rtl) == FLOAT
11951 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
11952 <= DWARF2_ADDR_SIZE))
11953 {
11954 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11955 GET_CODE (rtl) == UNSIGNED_FLOAT);
11956 if (type_die == NULL)
11957 break;
11958 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11959 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11960 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11961 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11962 add_loc_descr (&op0, cvt);
11963 }
11964 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
11965 if (type_die == NULL)
11966 break;
11967 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11968 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11969 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11970 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11971 add_loc_descr (&op0, cvt);
11972 if (GET_MODE_CLASS (mode) == MODE_INT
11973 && (GET_CODE (rtl) == FIX
11974 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
11975 {
11976 op0 = convert_descriptor_to_mode (mode, op0);
11977 if (op0 == NULL)
11978 break;
11979 }
11980 mem_loc_result = op0;
11981 }
11982 break;
11983
11984 case CLZ:
11985 case CTZ:
11986 case FFS:
11987 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
11988 break;
11989
11990 case POPCOUNT:
11991 case PARITY:
11992 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
11993 break;
11994
11995 case BSWAP:
11996 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
11997 break;
11998
11999 case ROTATE:
12000 case ROTATERT:
12001 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12002 break;
12003
12004 case COMPARE:
12005 case TRUNCATE:
12006 /* In theory, we could implement the above. */
12007 /* DWARF cannot represent the unsigned compare operations
12008 natively. */
12009 case SS_MULT:
12010 case US_MULT:
12011 case SS_DIV:
12012 case US_DIV:
12013 case SS_PLUS:
12014 case US_PLUS:
12015 case SS_MINUS:
12016 case US_MINUS:
12017 case SS_NEG:
12018 case US_NEG:
12019 case SS_ABS:
12020 case SS_ASHIFT:
12021 case US_ASHIFT:
12022 case SS_TRUNCATE:
12023 case US_TRUNCATE:
12024 case UNORDERED:
12025 case ORDERED:
12026 case UNEQ:
12027 case UNGE:
12028 case UNGT:
12029 case UNLE:
12030 case UNLT:
12031 case LTGT:
12032 case FRACT_CONVERT:
12033 case UNSIGNED_FRACT_CONVERT:
12034 case SAT_FRACT:
12035 case UNSIGNED_SAT_FRACT:
12036 case SQRT:
12037 case ASM_OPERANDS:
12038 case VEC_MERGE:
12039 case VEC_SELECT:
12040 case VEC_CONCAT:
12041 case VEC_DUPLICATE:
12042 case UNSPEC:
12043 case HIGH:
12044 case FMA:
12045 case STRICT_LOW_PART:
12046 case CONST_VECTOR:
12047 case CONST_FIXED:
12048 case CLRSB:
12049 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12050 can't express it in the debug info. This can happen e.g. with some
12051 TLS UNSPECs. */
12052 break;
12053
12054 case CONST_STRING:
12055 resolve_one_addr (&rtl, NULL);
12056 goto symref;
12057
12058 default:
12059 #ifdef ENABLE_CHECKING
12060 print_rtl (stderr, rtl);
12061 gcc_unreachable ();
12062 #else
12063 break;
12064 #endif
12065 }
12066
12067 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12068 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12069
12070 return mem_loc_result;
12071 }
12072
12073 /* Return a descriptor that describes the concatenation of two locations.
12074 This is typically a complex variable. */
12075
12076 static dw_loc_descr_ref
12077 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12078 {
12079 dw_loc_descr_ref cc_loc_result = NULL;
12080 dw_loc_descr_ref x0_ref
12081 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12082 dw_loc_descr_ref x1_ref
12083 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12084
12085 if (x0_ref == 0 || x1_ref == 0)
12086 return 0;
12087
12088 cc_loc_result = x0_ref;
12089 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12090
12091 add_loc_descr (&cc_loc_result, x1_ref);
12092 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12093
12094 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12095 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12096
12097 return cc_loc_result;
12098 }
12099
12100 /* Return a descriptor that describes the concatenation of N
12101 locations. */
12102
12103 static dw_loc_descr_ref
12104 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12105 {
12106 unsigned int i;
12107 dw_loc_descr_ref cc_loc_result = NULL;
12108 unsigned int n = XVECLEN (concatn, 0);
12109
12110 for (i = 0; i < n; ++i)
12111 {
12112 dw_loc_descr_ref ref;
12113 rtx x = XVECEXP (concatn, 0, i);
12114
12115 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12116 if (ref == NULL)
12117 return NULL;
12118
12119 add_loc_descr (&cc_loc_result, ref);
12120 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12121 }
12122
12123 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12124 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12125
12126 return cc_loc_result;
12127 }
12128
12129 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12130 for DEBUG_IMPLICIT_PTR RTL. */
12131
12132 static dw_loc_descr_ref
12133 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12134 {
12135 dw_loc_descr_ref ret;
12136 dw_die_ref ref;
12137
12138 if (dwarf_strict)
12139 return NULL;
12140 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12141 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12142 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12143 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12144 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12145 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12146 if (ref)
12147 {
12148 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12149 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12150 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12151 }
12152 else
12153 {
12154 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12155 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12156 }
12157 return ret;
12158 }
12159
12160 /* Output a proper Dwarf location descriptor for a variable or parameter
12161 which is either allocated in a register or in a memory location. For a
12162 register, we just generate an OP_REG and the register number. For a
12163 memory location we provide a Dwarf postfix expression describing how to
12164 generate the (dynamic) address of the object onto the address stack.
12165
12166 MODE is mode of the decl if this loc_descriptor is going to be used in
12167 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12168 allowed, VOIDmode otherwise.
12169
12170 If we don't know how to describe it, return 0. */
12171
12172 static dw_loc_descr_ref
12173 loc_descriptor (rtx rtl, enum machine_mode mode,
12174 enum var_init_status initialized)
12175 {
12176 dw_loc_descr_ref loc_result = NULL;
12177
12178 switch (GET_CODE (rtl))
12179 {
12180 case SUBREG:
12181 /* The case of a subreg may arise when we have a local (register)
12182 variable or a formal (register) parameter which doesn't quite fill
12183 up an entire register. For now, just assume that it is
12184 legitimate to make the Dwarf info refer to the whole register which
12185 contains the given subreg. */
12186 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12187 loc_result = loc_descriptor (SUBREG_REG (rtl),
12188 GET_MODE (SUBREG_REG (rtl)), initialized);
12189 else
12190 goto do_default;
12191 break;
12192
12193 case REG:
12194 loc_result = reg_loc_descriptor (rtl, initialized);
12195 break;
12196
12197 case MEM:
12198 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12199 GET_MODE (rtl), initialized);
12200 if (loc_result == NULL)
12201 loc_result = tls_mem_loc_descriptor (rtl);
12202 if (loc_result == NULL)
12203 {
12204 rtx new_rtl = avoid_constant_pool_reference (rtl);
12205 if (new_rtl != rtl)
12206 loc_result = loc_descriptor (new_rtl, mode, initialized);
12207 }
12208 break;
12209
12210 case CONCAT:
12211 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12212 initialized);
12213 break;
12214
12215 case CONCATN:
12216 loc_result = concatn_loc_descriptor (rtl, initialized);
12217 break;
12218
12219 case VAR_LOCATION:
12220 /* Single part. */
12221 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12222 {
12223 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12224 if (GET_CODE (loc) == EXPR_LIST)
12225 loc = XEXP (loc, 0);
12226 loc_result = loc_descriptor (loc, mode, initialized);
12227 break;
12228 }
12229
12230 rtl = XEXP (rtl, 1);
12231 /* FALLTHRU */
12232
12233 case PARALLEL:
12234 {
12235 rtvec par_elems = XVEC (rtl, 0);
12236 int num_elem = GET_NUM_ELEM (par_elems);
12237 enum machine_mode mode;
12238 int i;
12239
12240 /* Create the first one, so we have something to add to. */
12241 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12242 VOIDmode, initialized);
12243 if (loc_result == NULL)
12244 return NULL;
12245 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12246 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12247 for (i = 1; i < num_elem; i++)
12248 {
12249 dw_loc_descr_ref temp;
12250
12251 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12252 VOIDmode, initialized);
12253 if (temp == NULL)
12254 return NULL;
12255 add_loc_descr (&loc_result, temp);
12256 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12257 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12258 }
12259 }
12260 break;
12261
12262 case CONST_INT:
12263 if (mode != VOIDmode && mode != BLKmode)
12264 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12265 INTVAL (rtl));
12266 break;
12267
12268 case CONST_DOUBLE:
12269 if (mode == VOIDmode)
12270 mode = GET_MODE (rtl);
12271
12272 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12273 {
12274 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12275
12276 /* Note that a CONST_DOUBLE rtx could represent either an integer
12277 or a floating-point constant. A CONST_DOUBLE is used whenever
12278 the constant requires more than one word in order to be
12279 adequately represented. We output CONST_DOUBLEs as blocks. */
12280 loc_result = new_loc_descr (DW_OP_implicit_value,
12281 GET_MODE_SIZE (mode), 0);
12282 if (SCALAR_FLOAT_MODE_P (mode))
12283 {
12284 unsigned int length = GET_MODE_SIZE (mode);
12285 unsigned char *array
12286 = (unsigned char*) ggc_alloc_atomic (length);
12287
12288 insert_float (rtl, array);
12289 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12290 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12291 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12292 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12293 }
12294 else
12295 {
12296 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12297 loc_result->dw_loc_oprnd2.v.val_double
12298 = rtx_to_double_int (rtl);
12299 }
12300 }
12301 break;
12302
12303 case CONST_VECTOR:
12304 if (mode == VOIDmode)
12305 mode = GET_MODE (rtl);
12306
12307 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12308 {
12309 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12310 unsigned int length = CONST_VECTOR_NUNITS (rtl);
12311 unsigned char *array = (unsigned char *)
12312 ggc_alloc_atomic (length * elt_size);
12313 unsigned int i;
12314 unsigned char *p;
12315
12316 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12317 switch (GET_MODE_CLASS (mode))
12318 {
12319 case MODE_VECTOR_INT:
12320 for (i = 0, p = array; i < length; i++, p += elt_size)
12321 {
12322 rtx elt = CONST_VECTOR_ELT (rtl, i);
12323 double_int val = rtx_to_double_int (elt);
12324
12325 if (elt_size <= sizeof (HOST_WIDE_INT))
12326 insert_int (val.to_shwi (), elt_size, p);
12327 else
12328 {
12329 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12330 insert_double (val, p);
12331 }
12332 }
12333 break;
12334
12335 case MODE_VECTOR_FLOAT:
12336 for (i = 0, p = array; i < length; i++, p += elt_size)
12337 {
12338 rtx elt = CONST_VECTOR_ELT (rtl, i);
12339 insert_float (elt, p);
12340 }
12341 break;
12342
12343 default:
12344 gcc_unreachable ();
12345 }
12346
12347 loc_result = new_loc_descr (DW_OP_implicit_value,
12348 length * elt_size, 0);
12349 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12350 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12351 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12352 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12353 }
12354 break;
12355
12356 case CONST:
12357 if (mode == VOIDmode
12358 || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12359 || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12360 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12361 {
12362 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12363 break;
12364 }
12365 /* FALLTHROUGH */
12366 case SYMBOL_REF:
12367 if (!const_ok_for_output (rtl))
12368 break;
12369 case LABEL_REF:
12370 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12371 && (dwarf_version >= 4 || !dwarf_strict))
12372 {
12373 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12374 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12375 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12376 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12377 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12378 }
12379 break;
12380
12381 case DEBUG_IMPLICIT_PTR:
12382 loc_result = implicit_ptr_descriptor (rtl, 0);
12383 break;
12384
12385 case PLUS:
12386 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12387 && CONST_INT_P (XEXP (rtl, 1)))
12388 {
12389 loc_result
12390 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12391 break;
12392 }
12393 /* FALLTHRU */
12394 do_default:
12395 default:
12396 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12397 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12398 && dwarf_version >= 4)
12399 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12400 {
12401 /* Value expression. */
12402 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12403 if (loc_result)
12404 add_loc_descr (&loc_result,
12405 new_loc_descr (DW_OP_stack_value, 0, 0));
12406 }
12407 break;
12408 }
12409
12410 return loc_result;
12411 }
12412
12413 /* We need to figure out what section we should use as the base for the
12414 address ranges where a given location is valid.
12415 1. If this particular DECL has a section associated with it, use that.
12416 2. If this function has a section associated with it, use that.
12417 3. Otherwise, use the text section.
12418 XXX: If you split a variable across multiple sections, we won't notice. */
12419
12420 static const char *
12421 secname_for_decl (const_tree decl)
12422 {
12423 const char *secname;
12424
12425 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12426 {
12427 tree sectree = DECL_SECTION_NAME (decl);
12428 secname = TREE_STRING_POINTER (sectree);
12429 }
12430 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12431 {
12432 tree sectree = DECL_SECTION_NAME (current_function_decl);
12433 secname = TREE_STRING_POINTER (sectree);
12434 }
12435 else if (cfun && in_cold_section_p)
12436 secname = crtl->subsections.cold_section_label;
12437 else
12438 secname = text_section_label;
12439
12440 return secname;
12441 }
12442
12443 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
12444
12445 static bool
12446 decl_by_reference_p (tree decl)
12447 {
12448 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12449 || TREE_CODE (decl) == VAR_DECL)
12450 && DECL_BY_REFERENCE (decl));
12451 }
12452
12453 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
12454 for VARLOC. */
12455
12456 static dw_loc_descr_ref
12457 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12458 enum var_init_status initialized)
12459 {
12460 int have_address = 0;
12461 dw_loc_descr_ref descr;
12462 enum machine_mode mode;
12463
12464 if (want_address != 2)
12465 {
12466 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12467 /* Single part. */
12468 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12469 {
12470 varloc = PAT_VAR_LOCATION_LOC (varloc);
12471 if (GET_CODE (varloc) == EXPR_LIST)
12472 varloc = XEXP (varloc, 0);
12473 mode = GET_MODE (varloc);
12474 if (MEM_P (varloc))
12475 {
12476 rtx addr = XEXP (varloc, 0);
12477 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12478 mode, initialized);
12479 if (descr)
12480 have_address = 1;
12481 else
12482 {
12483 rtx x = avoid_constant_pool_reference (varloc);
12484 if (x != varloc)
12485 descr = mem_loc_descriptor (x, mode, VOIDmode,
12486 initialized);
12487 }
12488 }
12489 else
12490 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12491 }
12492 else
12493 return 0;
12494 }
12495 else
12496 {
12497 if (GET_CODE (varloc) == VAR_LOCATION)
12498 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12499 else
12500 mode = DECL_MODE (loc);
12501 descr = loc_descriptor (varloc, mode, initialized);
12502 have_address = 1;
12503 }
12504
12505 if (!descr)
12506 return 0;
12507
12508 if (want_address == 2 && !have_address
12509 && (dwarf_version >= 4 || !dwarf_strict))
12510 {
12511 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12512 {
12513 expansion_failed (loc, NULL_RTX,
12514 "DWARF address size mismatch");
12515 return 0;
12516 }
12517 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12518 have_address = 1;
12519 }
12520 /* Show if we can't fill the request for an address. */
12521 if (want_address && !have_address)
12522 {
12523 expansion_failed (loc, NULL_RTX,
12524 "Want address and only have value");
12525 return 0;
12526 }
12527
12528 /* If we've got an address and don't want one, dereference. */
12529 if (!want_address && have_address)
12530 {
12531 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12532 enum dwarf_location_atom op;
12533
12534 if (size > DWARF2_ADDR_SIZE || size == -1)
12535 {
12536 expansion_failed (loc, NULL_RTX,
12537 "DWARF address size mismatch");
12538 return 0;
12539 }
12540 else if (size == DWARF2_ADDR_SIZE)
12541 op = DW_OP_deref;
12542 else
12543 op = DW_OP_deref_size;
12544
12545 add_loc_descr (&descr, new_loc_descr (op, size, 0));
12546 }
12547
12548 return descr;
12549 }
12550
12551 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12552 if it is not possible. */
12553
12554 static dw_loc_descr_ref
12555 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12556 {
12557 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12558 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12559 else if (dwarf_version >= 3 || !dwarf_strict)
12560 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12561 else
12562 return NULL;
12563 }
12564
12565 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
12566 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
12567
12568 static dw_loc_descr_ref
12569 dw_sra_loc_expr (tree decl, rtx loc)
12570 {
12571 rtx p;
12572 unsigned int padsize = 0;
12573 dw_loc_descr_ref descr, *descr_tail;
12574 unsigned HOST_WIDE_INT decl_size;
12575 rtx varloc;
12576 enum var_init_status initialized;
12577
12578 if (DECL_SIZE (decl) == NULL
12579 || !host_integerp (DECL_SIZE (decl), 1))
12580 return NULL;
12581
12582 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
12583 descr = NULL;
12584 descr_tail = &descr;
12585
12586 for (p = loc; p; p = XEXP (p, 1))
12587 {
12588 unsigned int bitsize = decl_piece_bitsize (p);
12589 rtx loc_note = *decl_piece_varloc_ptr (p);
12590 dw_loc_descr_ref cur_descr;
12591 dw_loc_descr_ref *tail, last = NULL;
12592 unsigned int opsize = 0;
12593
12594 if (loc_note == NULL_RTX
12595 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
12596 {
12597 padsize += bitsize;
12598 continue;
12599 }
12600 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
12601 varloc = NOTE_VAR_LOCATION (loc_note);
12602 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
12603 if (cur_descr == NULL)
12604 {
12605 padsize += bitsize;
12606 continue;
12607 }
12608
12609 /* Check that cur_descr either doesn't use
12610 DW_OP_*piece operations, or their sum is equal
12611 to bitsize. Otherwise we can't embed it. */
12612 for (tail = &cur_descr; *tail != NULL;
12613 tail = &(*tail)->dw_loc_next)
12614 if ((*tail)->dw_loc_opc == DW_OP_piece)
12615 {
12616 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
12617 * BITS_PER_UNIT;
12618 last = *tail;
12619 }
12620 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
12621 {
12622 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
12623 last = *tail;
12624 }
12625
12626 if (last != NULL && opsize != bitsize)
12627 {
12628 padsize += bitsize;
12629 continue;
12630 }
12631
12632 /* If there is a hole, add DW_OP_*piece after empty DWARF
12633 expression, which means that those bits are optimized out. */
12634 if (padsize)
12635 {
12636 if (padsize > decl_size)
12637 return NULL;
12638 decl_size -= padsize;
12639 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
12640 if (*descr_tail == NULL)
12641 return NULL;
12642 descr_tail = &(*descr_tail)->dw_loc_next;
12643 padsize = 0;
12644 }
12645 *descr_tail = cur_descr;
12646 descr_tail = tail;
12647 if (bitsize > decl_size)
12648 return NULL;
12649 decl_size -= bitsize;
12650 if (last == NULL)
12651 {
12652 HOST_WIDE_INT offset = 0;
12653 if (GET_CODE (varloc) == VAR_LOCATION
12654 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12655 {
12656 varloc = PAT_VAR_LOCATION_LOC (varloc);
12657 if (GET_CODE (varloc) == EXPR_LIST)
12658 varloc = XEXP (varloc, 0);
12659 }
12660 do
12661 {
12662 if (GET_CODE (varloc) == CONST
12663 || GET_CODE (varloc) == SIGN_EXTEND
12664 || GET_CODE (varloc) == ZERO_EXTEND)
12665 varloc = XEXP (varloc, 0);
12666 else if (GET_CODE (varloc) == SUBREG)
12667 varloc = SUBREG_REG (varloc);
12668 else
12669 break;
12670 }
12671 while (1);
12672 /* DW_OP_bit_size offset should be zero for register
12673 or implicit location descriptions and empty location
12674 descriptions, but for memory addresses needs big endian
12675 adjustment. */
12676 if (MEM_P (varloc))
12677 {
12678 unsigned HOST_WIDE_INT memsize
12679 = MEM_SIZE (varloc) * BITS_PER_UNIT;
12680 if (memsize != bitsize)
12681 {
12682 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
12683 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
12684 return NULL;
12685 if (memsize < bitsize)
12686 return NULL;
12687 if (BITS_BIG_ENDIAN)
12688 offset = memsize - bitsize;
12689 }
12690 }
12691
12692 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
12693 if (*descr_tail == NULL)
12694 return NULL;
12695 descr_tail = &(*descr_tail)->dw_loc_next;
12696 }
12697 }
12698
12699 /* If there were any non-empty expressions, add padding till the end of
12700 the decl. */
12701 if (descr != NULL && decl_size != 0)
12702 {
12703 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
12704 if (*descr_tail == NULL)
12705 return NULL;
12706 }
12707 return descr;
12708 }
12709
12710 /* Return the dwarf representation of the location list LOC_LIST of
12711 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
12712 function. */
12713
12714 static dw_loc_list_ref
12715 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
12716 {
12717 const char *endname, *secname;
12718 rtx varloc;
12719 enum var_init_status initialized;
12720 struct var_loc_node *node;
12721 dw_loc_descr_ref descr;
12722 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12723 dw_loc_list_ref list = NULL;
12724 dw_loc_list_ref *listp = &list;
12725
12726 /* Now that we know what section we are using for a base,
12727 actually construct the list of locations.
12728 The first location information is what is passed to the
12729 function that creates the location list, and the remaining
12730 locations just get added on to that list.
12731 Note that we only know the start address for a location
12732 (IE location changes), so to build the range, we use
12733 the range [current location start, next location start].
12734 This means we have to special case the last node, and generate
12735 a range of [last location start, end of function label]. */
12736
12737 secname = secname_for_decl (decl);
12738
12739 for (node = loc_list->first; node; node = node->next)
12740 if (GET_CODE (node->loc) == EXPR_LIST
12741 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
12742 {
12743 if (GET_CODE (node->loc) == EXPR_LIST)
12744 {
12745 /* This requires DW_OP_{,bit_}piece, which is not usable
12746 inside DWARF expressions. */
12747 if (want_address != 2)
12748 continue;
12749 descr = dw_sra_loc_expr (decl, node->loc);
12750 if (descr == NULL)
12751 continue;
12752 }
12753 else
12754 {
12755 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
12756 varloc = NOTE_VAR_LOCATION (node->loc);
12757 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
12758 }
12759 if (descr)
12760 {
12761 bool range_across_switch = false;
12762 /* If section switch happens in between node->label
12763 and node->next->label (or end of function) and
12764 we can't emit it as a single entry list,
12765 emit two ranges, first one ending at the end
12766 of first partition and second one starting at the
12767 beginning of second partition. */
12768 if (node == loc_list->last_before_switch
12769 && (node != loc_list->first || loc_list->first->next)
12770 && current_function_decl)
12771 {
12772 endname = cfun->fde->dw_fde_end;
12773 range_across_switch = true;
12774 }
12775 /* The variable has a location between NODE->LABEL and
12776 NODE->NEXT->LABEL. */
12777 else if (node->next)
12778 endname = node->next->label;
12779 /* If the variable has a location at the last label
12780 it keeps its location until the end of function. */
12781 else if (!current_function_decl)
12782 endname = text_end_label;
12783 else
12784 {
12785 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12786 current_function_funcdef_no);
12787 endname = ggc_strdup (label_id);
12788 }
12789
12790 *listp = new_loc_list (descr, node->label, endname, secname);
12791 if (TREE_CODE (decl) == PARM_DECL
12792 && node == loc_list->first
12793 && GET_CODE (node->loc) == NOTE
12794 && strcmp (node->label, endname) == 0)
12795 (*listp)->force = true;
12796 listp = &(*listp)->dw_loc_next;
12797
12798 if (range_across_switch)
12799 {
12800 if (GET_CODE (node->loc) == EXPR_LIST)
12801 descr = dw_sra_loc_expr (decl, node->loc);
12802 else
12803 {
12804 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
12805 varloc = NOTE_VAR_LOCATION (node->loc);
12806 descr = dw_loc_list_1 (decl, varloc, want_address,
12807 initialized);
12808 }
12809 gcc_assert (descr);
12810 /* The variable has a location between NODE->LABEL and
12811 NODE->NEXT->LABEL. */
12812 if (node->next)
12813 endname = node->next->label;
12814 else
12815 endname = cfun->fde->dw_fde_second_end;
12816 *listp = new_loc_list (descr,
12817 cfun->fde->dw_fde_second_begin,
12818 endname, secname);
12819 listp = &(*listp)->dw_loc_next;
12820 }
12821 }
12822 }
12823
12824 /* Try to avoid the overhead of a location list emitting a location
12825 expression instead, but only if we didn't have more than one
12826 location entry in the first place. If some entries were not
12827 representable, we don't want to pretend a single entry that was
12828 applies to the entire scope in which the variable is
12829 available. */
12830 if (list && loc_list->first->next)
12831 gen_llsym (list);
12832
12833 return list;
12834 }
12835
12836 /* Return if the loc_list has only single element and thus can be represented
12837 as location description. */
12838
12839 static bool
12840 single_element_loc_list_p (dw_loc_list_ref list)
12841 {
12842 gcc_assert (!list->dw_loc_next || list->ll_symbol);
12843 return !list->ll_symbol;
12844 }
12845
12846 /* To each location in list LIST add loc descr REF. */
12847
12848 static void
12849 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
12850 {
12851 dw_loc_descr_ref copy;
12852 add_loc_descr (&list->expr, ref);
12853 list = list->dw_loc_next;
12854 while (list)
12855 {
12856 copy = ggc_alloc_dw_loc_descr_node ();
12857 memcpy (copy, ref, sizeof (dw_loc_descr_node));
12858 add_loc_descr (&list->expr, copy);
12859 while (copy->dw_loc_next)
12860 {
12861 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
12862 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
12863 copy->dw_loc_next = new_copy;
12864 copy = new_copy;
12865 }
12866 list = list->dw_loc_next;
12867 }
12868 }
12869
12870 /* Given two lists RET and LIST
12871 produce location list that is result of adding expression in LIST
12872 to expression in RET on each position in program.
12873 Might be destructive on both RET and LIST.
12874
12875 TODO: We handle only simple cases of RET or LIST having at most one
12876 element. General case would inolve sorting the lists in program order
12877 and merging them that will need some additional work.
12878 Adding that will improve quality of debug info especially for SRA-ed
12879 structures. */
12880
12881 static void
12882 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
12883 {
12884 if (!list)
12885 return;
12886 if (!*ret)
12887 {
12888 *ret = list;
12889 return;
12890 }
12891 if (!list->dw_loc_next)
12892 {
12893 add_loc_descr_to_each (*ret, list->expr);
12894 return;
12895 }
12896 if (!(*ret)->dw_loc_next)
12897 {
12898 add_loc_descr_to_each (list, (*ret)->expr);
12899 *ret = list;
12900 return;
12901 }
12902 expansion_failed (NULL_TREE, NULL_RTX,
12903 "Don't know how to merge two non-trivial"
12904 " location lists.\n");
12905 *ret = NULL;
12906 return;
12907 }
12908
12909 /* LOC is constant expression. Try a luck, look it up in constant
12910 pool and return its loc_descr of its address. */
12911
12912 static dw_loc_descr_ref
12913 cst_pool_loc_descr (tree loc)
12914 {
12915 /* Get an RTL for this, if something has been emitted. */
12916 rtx rtl = lookup_constant_def (loc);
12917
12918 if (!rtl || !MEM_P (rtl))
12919 {
12920 gcc_assert (!rtl);
12921 return 0;
12922 }
12923 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
12924
12925 /* TODO: We might get more coverage if we was actually delaying expansion
12926 of all expressions till end of compilation when constant pools are fully
12927 populated. */
12928 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
12929 {
12930 expansion_failed (loc, NULL_RTX,
12931 "CST value in contant pool but not marked.");
12932 return 0;
12933 }
12934 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12935 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
12936 }
12937
12938 /* Return dw_loc_list representing address of addr_expr LOC
12939 by looking for inner INDIRECT_REF expression and turning
12940 it into simple arithmetics. */
12941
12942 static dw_loc_list_ref
12943 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
12944 {
12945 tree obj, offset;
12946 HOST_WIDE_INT bitsize, bitpos, bytepos;
12947 enum machine_mode mode;
12948 int unsignedp, volatilep = 0;
12949 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
12950
12951 obj = get_inner_reference (TREE_OPERAND (loc, 0),
12952 &bitsize, &bitpos, &offset, &mode,
12953 &unsignedp, &volatilep, false);
12954 STRIP_NOPS (obj);
12955 if (bitpos % BITS_PER_UNIT)
12956 {
12957 expansion_failed (loc, NULL_RTX, "bitfield access");
12958 return 0;
12959 }
12960 if (!INDIRECT_REF_P (obj))
12961 {
12962 expansion_failed (obj,
12963 NULL_RTX, "no indirect ref in inner refrence");
12964 return 0;
12965 }
12966 if (!offset && !bitpos)
12967 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
12968 else if (toplev
12969 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
12970 && (dwarf_version >= 4 || !dwarf_strict))
12971 {
12972 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
12973 if (!list_ret)
12974 return 0;
12975 if (offset)
12976 {
12977 /* Variable offset. */
12978 list_ret1 = loc_list_from_tree (offset, 0);
12979 if (list_ret1 == 0)
12980 return 0;
12981 add_loc_list (&list_ret, list_ret1);
12982 if (!list_ret)
12983 return 0;
12984 add_loc_descr_to_each (list_ret,
12985 new_loc_descr (DW_OP_plus, 0, 0));
12986 }
12987 bytepos = bitpos / BITS_PER_UNIT;
12988 if (bytepos > 0)
12989 add_loc_descr_to_each (list_ret,
12990 new_loc_descr (DW_OP_plus_uconst,
12991 bytepos, 0));
12992 else if (bytepos < 0)
12993 loc_list_plus_const (list_ret, bytepos);
12994 add_loc_descr_to_each (list_ret,
12995 new_loc_descr (DW_OP_stack_value, 0, 0));
12996 }
12997 return list_ret;
12998 }
12999
13000
13001 /* Generate Dwarf location list representing LOC.
13002 If WANT_ADDRESS is false, expression computing LOC will be computed
13003 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13004 if WANT_ADDRESS is 2, expression computing address useable in location
13005 will be returned (i.e. DW_OP_reg can be used
13006 to refer to register values). */
13007
13008 static dw_loc_list_ref
13009 loc_list_from_tree (tree loc, int want_address)
13010 {
13011 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13012 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13013 int have_address = 0;
13014 enum dwarf_location_atom op;
13015
13016 /* ??? Most of the time we do not take proper care for sign/zero
13017 extending the values properly. Hopefully this won't be a real
13018 problem... */
13019
13020 switch (TREE_CODE (loc))
13021 {
13022 case ERROR_MARK:
13023 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13024 return 0;
13025
13026 case PLACEHOLDER_EXPR:
13027 /* This case involves extracting fields from an object to determine the
13028 position of other fields. We don't try to encode this here. The
13029 only user of this is Ada, which encodes the needed information using
13030 the names of types. */
13031 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13032 return 0;
13033
13034 case CALL_EXPR:
13035 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13036 /* There are no opcodes for these operations. */
13037 return 0;
13038
13039 case PREINCREMENT_EXPR:
13040 case PREDECREMENT_EXPR:
13041 case POSTINCREMENT_EXPR:
13042 case POSTDECREMENT_EXPR:
13043 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13044 /* There are no opcodes for these operations. */
13045 return 0;
13046
13047 case ADDR_EXPR:
13048 /* If we already want an address, see if there is INDIRECT_REF inside
13049 e.g. for &this->field. */
13050 if (want_address)
13051 {
13052 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13053 (loc, want_address == 2);
13054 if (list_ret)
13055 have_address = 1;
13056 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13057 && (ret = cst_pool_loc_descr (loc)))
13058 have_address = 1;
13059 }
13060 /* Otherwise, process the argument and look for the address. */
13061 if (!list_ret && !ret)
13062 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13063 else
13064 {
13065 if (want_address)
13066 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13067 return NULL;
13068 }
13069 break;
13070
13071 case VAR_DECL:
13072 if (DECL_THREAD_LOCAL_P (loc))
13073 {
13074 rtx rtl;
13075 enum dwarf_location_atom first_op;
13076 enum dwarf_location_atom second_op;
13077 bool dtprel = false;
13078
13079 if (targetm.have_tls)
13080 {
13081 /* If this is not defined, we have no way to emit the
13082 data. */
13083 if (!targetm.asm_out.output_dwarf_dtprel)
13084 return 0;
13085
13086 /* The way DW_OP_GNU_push_tls_address is specified, we
13087 can only look up addresses of objects in the current
13088 module. We used DW_OP_addr as first op, but that's
13089 wrong, because DW_OP_addr is relocated by the debug
13090 info consumer, while DW_OP_GNU_push_tls_address
13091 operand shouldn't be. */
13092 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13093 return 0;
13094 first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13095 dtprel = true;
13096 second_op = DW_OP_GNU_push_tls_address;
13097 }
13098 else
13099 {
13100 if (!targetm.emutls.debug_form_tls_address
13101 || !(dwarf_version >= 3 || !dwarf_strict))
13102 return 0;
13103 /* We stuffed the control variable into the DECL_VALUE_EXPR
13104 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13105 no longer appear in gimple code. We used the control
13106 variable in specific so that we could pick it up here. */
13107 loc = DECL_VALUE_EXPR (loc);
13108 first_op = DW_OP_addr;
13109 second_op = DW_OP_form_tls_address;
13110 }
13111
13112 rtl = rtl_for_decl_location (loc);
13113 if (rtl == NULL_RTX)
13114 return 0;
13115
13116 if (!MEM_P (rtl))
13117 return 0;
13118 rtl = XEXP (rtl, 0);
13119 if (! CONSTANT_P (rtl))
13120 return 0;
13121
13122 ret = new_loc_descr (first_op, 0, 0);
13123 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13124 ret->dw_loc_oprnd1.v.val_addr = rtl;
13125 ret->dtprel = dtprel;
13126
13127 ret1 = new_loc_descr (second_op, 0, 0);
13128 add_loc_descr (&ret, ret1);
13129
13130 have_address = 1;
13131 break;
13132 }
13133 /* FALLTHRU */
13134
13135 case PARM_DECL:
13136 case RESULT_DECL:
13137 if (DECL_HAS_VALUE_EXPR_P (loc))
13138 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13139 want_address);
13140 /* FALLTHRU */
13141
13142 case FUNCTION_DECL:
13143 {
13144 rtx rtl;
13145 var_loc_list *loc_list = lookup_decl_loc (loc);
13146
13147 if (loc_list && loc_list->first)
13148 {
13149 list_ret = dw_loc_list (loc_list, loc, want_address);
13150 have_address = want_address != 0;
13151 break;
13152 }
13153 rtl = rtl_for_decl_location (loc);
13154 if (rtl == NULL_RTX)
13155 {
13156 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13157 return 0;
13158 }
13159 else if (CONST_INT_P (rtl))
13160 {
13161 HOST_WIDE_INT val = INTVAL (rtl);
13162 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13163 val &= GET_MODE_MASK (DECL_MODE (loc));
13164 ret = int_loc_descriptor (val);
13165 }
13166 else if (GET_CODE (rtl) == CONST_STRING)
13167 {
13168 expansion_failed (loc, NULL_RTX, "CONST_STRING");
13169 return 0;
13170 }
13171 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13172 {
13173 ret = new_loc_descr (DW_OP_addr, 0, 0);
13174 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13175 ret->dw_loc_oprnd1.v.val_addr = rtl;
13176 }
13177 else
13178 {
13179 enum machine_mode mode, mem_mode;
13180
13181 /* Certain constructs can only be represented at top-level. */
13182 if (want_address == 2)
13183 {
13184 ret = loc_descriptor (rtl, VOIDmode,
13185 VAR_INIT_STATUS_INITIALIZED);
13186 have_address = 1;
13187 }
13188 else
13189 {
13190 mode = GET_MODE (rtl);
13191 mem_mode = VOIDmode;
13192 if (MEM_P (rtl))
13193 {
13194 mem_mode = mode;
13195 mode = get_address_mode (rtl);
13196 rtl = XEXP (rtl, 0);
13197 have_address = 1;
13198 }
13199 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13200 VAR_INIT_STATUS_INITIALIZED);
13201 }
13202 if (!ret)
13203 expansion_failed (loc, rtl,
13204 "failed to produce loc descriptor for rtl");
13205 }
13206 }
13207 break;
13208
13209 case MEM_REF:
13210 /* ??? FIXME. */
13211 if (!integer_zerop (TREE_OPERAND (loc, 1)))
13212 return 0;
13213 /* Fallthru. */
13214 case INDIRECT_REF:
13215 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13216 have_address = 1;
13217 break;
13218
13219 case COMPOUND_EXPR:
13220 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13221
13222 CASE_CONVERT:
13223 case VIEW_CONVERT_EXPR:
13224 case SAVE_EXPR:
13225 case MODIFY_EXPR:
13226 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13227
13228 case COMPONENT_REF:
13229 case BIT_FIELD_REF:
13230 case ARRAY_REF:
13231 case ARRAY_RANGE_REF:
13232 case REALPART_EXPR:
13233 case IMAGPART_EXPR:
13234 {
13235 tree obj, offset;
13236 HOST_WIDE_INT bitsize, bitpos, bytepos;
13237 enum machine_mode mode;
13238 int unsignedp, volatilep = 0;
13239
13240 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13241 &unsignedp, &volatilep, false);
13242
13243 gcc_assert (obj != loc);
13244
13245 list_ret = loc_list_from_tree (obj,
13246 want_address == 2
13247 && !bitpos && !offset ? 2 : 1);
13248 /* TODO: We can extract value of the small expression via shifting even
13249 for nonzero bitpos. */
13250 if (list_ret == 0)
13251 return 0;
13252 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13253 {
13254 expansion_failed (loc, NULL_RTX,
13255 "bitfield access");
13256 return 0;
13257 }
13258
13259 if (offset != NULL_TREE)
13260 {
13261 /* Variable offset. */
13262 list_ret1 = loc_list_from_tree (offset, 0);
13263 if (list_ret1 == 0)
13264 return 0;
13265 add_loc_list (&list_ret, list_ret1);
13266 if (!list_ret)
13267 return 0;
13268 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13269 }
13270
13271 bytepos = bitpos / BITS_PER_UNIT;
13272 if (bytepos > 0)
13273 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13274 else if (bytepos < 0)
13275 loc_list_plus_const (list_ret, bytepos);
13276
13277 have_address = 1;
13278 break;
13279 }
13280
13281 case INTEGER_CST:
13282 if ((want_address || !host_integerp (loc, 0))
13283 && (ret = cst_pool_loc_descr (loc)))
13284 have_address = 1;
13285 else if (want_address == 2
13286 && host_integerp (loc, 0)
13287 && (ret = address_of_int_loc_descriptor
13288 (int_size_in_bytes (TREE_TYPE (loc)),
13289 tree_low_cst (loc, 0))))
13290 have_address = 1;
13291 else if (host_integerp (loc, 0))
13292 ret = int_loc_descriptor (tree_low_cst (loc, 0));
13293 else
13294 {
13295 expansion_failed (loc, NULL_RTX,
13296 "Integer operand is not host integer");
13297 return 0;
13298 }
13299 break;
13300
13301 case CONSTRUCTOR:
13302 case REAL_CST:
13303 case STRING_CST:
13304 case COMPLEX_CST:
13305 if ((ret = cst_pool_loc_descr (loc)))
13306 have_address = 1;
13307 else
13308 /* We can construct small constants here using int_loc_descriptor. */
13309 expansion_failed (loc, NULL_RTX,
13310 "constructor or constant not in constant pool");
13311 break;
13312
13313 case TRUTH_AND_EXPR:
13314 case TRUTH_ANDIF_EXPR:
13315 case BIT_AND_EXPR:
13316 op = DW_OP_and;
13317 goto do_binop;
13318
13319 case TRUTH_XOR_EXPR:
13320 case BIT_XOR_EXPR:
13321 op = DW_OP_xor;
13322 goto do_binop;
13323
13324 case TRUTH_OR_EXPR:
13325 case TRUTH_ORIF_EXPR:
13326 case BIT_IOR_EXPR:
13327 op = DW_OP_or;
13328 goto do_binop;
13329
13330 case FLOOR_DIV_EXPR:
13331 case CEIL_DIV_EXPR:
13332 case ROUND_DIV_EXPR:
13333 case TRUNC_DIV_EXPR:
13334 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13335 return 0;
13336 op = DW_OP_div;
13337 goto do_binop;
13338
13339 case MINUS_EXPR:
13340 op = DW_OP_minus;
13341 goto do_binop;
13342
13343 case FLOOR_MOD_EXPR:
13344 case CEIL_MOD_EXPR:
13345 case ROUND_MOD_EXPR:
13346 case TRUNC_MOD_EXPR:
13347 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13348 {
13349 op = DW_OP_mod;
13350 goto do_binop;
13351 }
13352 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13353 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13354 if (list_ret == 0 || list_ret1 == 0)
13355 return 0;
13356
13357 add_loc_list (&list_ret, list_ret1);
13358 if (list_ret == 0)
13359 return 0;
13360 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13361 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13362 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13363 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13364 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13365 break;
13366
13367 case MULT_EXPR:
13368 op = DW_OP_mul;
13369 goto do_binop;
13370
13371 case LSHIFT_EXPR:
13372 op = DW_OP_shl;
13373 goto do_binop;
13374
13375 case RSHIFT_EXPR:
13376 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13377 goto do_binop;
13378
13379 case POINTER_PLUS_EXPR:
13380 case PLUS_EXPR:
13381 if (host_integerp (TREE_OPERAND (loc, 1), 0))
13382 {
13383 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13384 if (list_ret == 0)
13385 return 0;
13386
13387 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13388 break;
13389 }
13390
13391 op = DW_OP_plus;
13392 goto do_binop;
13393
13394 case LE_EXPR:
13395 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13396 return 0;
13397
13398 op = DW_OP_le;
13399 goto do_binop;
13400
13401 case GE_EXPR:
13402 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13403 return 0;
13404
13405 op = DW_OP_ge;
13406 goto do_binop;
13407
13408 case LT_EXPR:
13409 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13410 return 0;
13411
13412 op = DW_OP_lt;
13413 goto do_binop;
13414
13415 case GT_EXPR:
13416 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13417 return 0;
13418
13419 op = DW_OP_gt;
13420 goto do_binop;
13421
13422 case EQ_EXPR:
13423 op = DW_OP_eq;
13424 goto do_binop;
13425
13426 case NE_EXPR:
13427 op = DW_OP_ne;
13428 goto do_binop;
13429
13430 do_binop:
13431 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13432 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13433 if (list_ret == 0 || list_ret1 == 0)
13434 return 0;
13435
13436 add_loc_list (&list_ret, list_ret1);
13437 if (list_ret == 0)
13438 return 0;
13439 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13440 break;
13441
13442 case TRUTH_NOT_EXPR:
13443 case BIT_NOT_EXPR:
13444 op = DW_OP_not;
13445 goto do_unop;
13446
13447 case ABS_EXPR:
13448 op = DW_OP_abs;
13449 goto do_unop;
13450
13451 case NEGATE_EXPR:
13452 op = DW_OP_neg;
13453 goto do_unop;
13454
13455 do_unop:
13456 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13457 if (list_ret == 0)
13458 return 0;
13459
13460 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13461 break;
13462
13463 case MIN_EXPR:
13464 case MAX_EXPR:
13465 {
13466 const enum tree_code code =
13467 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13468
13469 loc = build3 (COND_EXPR, TREE_TYPE (loc),
13470 build2 (code, integer_type_node,
13471 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13472 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13473 }
13474
13475 /* ... fall through ... */
13476
13477 case COND_EXPR:
13478 {
13479 dw_loc_descr_ref lhs
13480 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13481 dw_loc_list_ref rhs
13482 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13483 dw_loc_descr_ref bra_node, jump_node, tmp;
13484
13485 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13486 if (list_ret == 0 || lhs == 0 || rhs == 0)
13487 return 0;
13488
13489 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13490 add_loc_descr_to_each (list_ret, bra_node);
13491
13492 add_loc_list (&list_ret, rhs);
13493 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13494 add_loc_descr_to_each (list_ret, jump_node);
13495
13496 add_loc_descr_to_each (list_ret, lhs);
13497 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13498 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13499
13500 /* ??? Need a node to point the skip at. Use a nop. */
13501 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13502 add_loc_descr_to_each (list_ret, tmp);
13503 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13504 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13505 }
13506 break;
13507
13508 case FIX_TRUNC_EXPR:
13509 return 0;
13510
13511 default:
13512 /* Leave front-end specific codes as simply unknown. This comes
13513 up, for instance, with the C STMT_EXPR. */
13514 if ((unsigned int) TREE_CODE (loc)
13515 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13516 {
13517 expansion_failed (loc, NULL_RTX,
13518 "language specific tree node");
13519 return 0;
13520 }
13521
13522 #ifdef ENABLE_CHECKING
13523 /* Otherwise this is a generic code; we should just lists all of
13524 these explicitly. We forgot one. */
13525 gcc_unreachable ();
13526 #else
13527 /* In a release build, we want to degrade gracefully: better to
13528 generate incomplete debugging information than to crash. */
13529 return NULL;
13530 #endif
13531 }
13532
13533 if (!ret && !list_ret)
13534 return 0;
13535
13536 if (want_address == 2 && !have_address
13537 && (dwarf_version >= 4 || !dwarf_strict))
13538 {
13539 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13540 {
13541 expansion_failed (loc, NULL_RTX,
13542 "DWARF address size mismatch");
13543 return 0;
13544 }
13545 if (ret)
13546 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13547 else
13548 add_loc_descr_to_each (list_ret,
13549 new_loc_descr (DW_OP_stack_value, 0, 0));
13550 have_address = 1;
13551 }
13552 /* Show if we can't fill the request for an address. */
13553 if (want_address && !have_address)
13554 {
13555 expansion_failed (loc, NULL_RTX,
13556 "Want address and only have value");
13557 return 0;
13558 }
13559
13560 gcc_assert (!ret || !list_ret);
13561
13562 /* If we've got an address and don't want one, dereference. */
13563 if (!want_address && have_address)
13564 {
13565 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13566
13567 if (size > DWARF2_ADDR_SIZE || size == -1)
13568 {
13569 expansion_failed (loc, NULL_RTX,
13570 "DWARF address size mismatch");
13571 return 0;
13572 }
13573 else if (size == DWARF2_ADDR_SIZE)
13574 op = DW_OP_deref;
13575 else
13576 op = DW_OP_deref_size;
13577
13578 if (ret)
13579 add_loc_descr (&ret, new_loc_descr (op, size, 0));
13580 else
13581 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13582 }
13583 if (ret)
13584 list_ret = new_loc_list (ret, NULL, NULL, NULL);
13585
13586 return list_ret;
13587 }
13588
13589 /* Same as above but return only single location expression. */
13590 static dw_loc_descr_ref
13591 loc_descriptor_from_tree (tree loc, int want_address)
13592 {
13593 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13594 if (!ret)
13595 return NULL;
13596 if (ret->dw_loc_next)
13597 {
13598 expansion_failed (loc, NULL_RTX,
13599 "Location list where only loc descriptor needed");
13600 return NULL;
13601 }
13602 return ret->expr;
13603 }
13604
13605 /* Given a value, round it up to the lowest multiple of `boundary'
13606 which is not less than the value itself. */
13607
13608 static inline HOST_WIDE_INT
13609 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13610 {
13611 return (((value + boundary - 1) / boundary) * boundary);
13612 }
13613
13614 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13615 pointer to the declared type for the relevant field variable, or return
13616 `integer_type_node' if the given node turns out to be an
13617 ERROR_MARK node. */
13618
13619 static inline tree
13620 field_type (const_tree decl)
13621 {
13622 tree type;
13623
13624 if (TREE_CODE (decl) == ERROR_MARK)
13625 return integer_type_node;
13626
13627 type = DECL_BIT_FIELD_TYPE (decl);
13628 if (type == NULL_TREE)
13629 type = TREE_TYPE (decl);
13630
13631 return type;
13632 }
13633
13634 /* Given a pointer to a tree node, return the alignment in bits for
13635 it, or else return BITS_PER_WORD if the node actually turns out to
13636 be an ERROR_MARK node. */
13637
13638 static inline unsigned
13639 simple_type_align_in_bits (const_tree type)
13640 {
13641 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
13642 }
13643
13644 static inline unsigned
13645 simple_decl_align_in_bits (const_tree decl)
13646 {
13647 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
13648 }
13649
13650 /* Return the result of rounding T up to ALIGN. */
13651
13652 static inline double_int
13653 round_up_to_align (double_int t, unsigned int align)
13654 {
13655 double_int alignd = double_int::from_uhwi (align);
13656 t += alignd;
13657 t += double_int_minus_one;
13658 t = t.div (alignd, true, TRUNC_DIV_EXPR);
13659 t *= alignd;
13660 return t;
13661 }
13662
13663 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
13664 lowest addressed byte of the "containing object" for the given FIELD_DECL,
13665 or return 0 if we are unable to determine what that offset is, either
13666 because the argument turns out to be a pointer to an ERROR_MARK node, or
13667 because the offset is actually variable. (We can't handle the latter case
13668 just yet). */
13669
13670 static HOST_WIDE_INT
13671 field_byte_offset (const_tree decl)
13672 {
13673 double_int object_offset_in_bits;
13674 double_int object_offset_in_bytes;
13675 double_int bitpos_int;
13676
13677 if (TREE_CODE (decl) == ERROR_MARK)
13678 return 0;
13679
13680 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
13681
13682 /* We cannot yet cope with fields whose positions are variable, so
13683 for now, when we see such things, we simply return 0. Someday, we may
13684 be able to handle such cases, but it will be damn difficult. */
13685 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
13686 return 0;
13687
13688 bitpos_int = tree_to_double_int (bit_position (decl));
13689
13690 #ifdef PCC_BITFIELD_TYPE_MATTERS
13691 if (PCC_BITFIELD_TYPE_MATTERS)
13692 {
13693 tree type;
13694 tree field_size_tree;
13695 double_int deepest_bitpos;
13696 double_int field_size_in_bits;
13697 unsigned int type_align_in_bits;
13698 unsigned int decl_align_in_bits;
13699 double_int type_size_in_bits;
13700
13701 type = field_type (decl);
13702 type_size_in_bits = double_int_type_size_in_bits (type);
13703 type_align_in_bits = simple_type_align_in_bits (type);
13704
13705 field_size_tree = DECL_SIZE (decl);
13706
13707 /* The size could be unspecified if there was an error, or for
13708 a flexible array member. */
13709 if (!field_size_tree)
13710 field_size_tree = bitsize_zero_node;
13711
13712 /* If the size of the field is not constant, use the type size. */
13713 if (TREE_CODE (field_size_tree) == INTEGER_CST)
13714 field_size_in_bits = tree_to_double_int (field_size_tree);
13715 else
13716 field_size_in_bits = type_size_in_bits;
13717
13718 decl_align_in_bits = simple_decl_align_in_bits (decl);
13719
13720 /* The GCC front-end doesn't make any attempt to keep track of the
13721 starting bit offset (relative to the start of the containing
13722 structure type) of the hypothetical "containing object" for a
13723 bit-field. Thus, when computing the byte offset value for the
13724 start of the "containing object" of a bit-field, we must deduce
13725 this information on our own. This can be rather tricky to do in
13726 some cases. For example, handling the following structure type
13727 definition when compiling for an i386/i486 target (which only
13728 aligns long long's to 32-bit boundaries) can be very tricky:
13729
13730 struct S { int field1; long long field2:31; };
13731
13732 Fortunately, there is a simple rule-of-thumb which can be used
13733 in such cases. When compiling for an i386/i486, GCC will
13734 allocate 8 bytes for the structure shown above. It decides to
13735 do this based upon one simple rule for bit-field allocation.
13736 GCC allocates each "containing object" for each bit-field at
13737 the first (i.e. lowest addressed) legitimate alignment boundary
13738 (based upon the required minimum alignment for the declared
13739 type of the field) which it can possibly use, subject to the
13740 condition that there is still enough available space remaining
13741 in the containing object (when allocated at the selected point)
13742 to fully accommodate all of the bits of the bit-field itself.
13743
13744 This simple rule makes it obvious why GCC allocates 8 bytes for
13745 each object of the structure type shown above. When looking
13746 for a place to allocate the "containing object" for `field2',
13747 the compiler simply tries to allocate a 64-bit "containing
13748 object" at each successive 32-bit boundary (starting at zero)
13749 until it finds a place to allocate that 64- bit field such that
13750 at least 31 contiguous (and previously unallocated) bits remain
13751 within that selected 64 bit field. (As it turns out, for the
13752 example above, the compiler finds it is OK to allocate the
13753 "containing object" 64-bit field at bit-offset zero within the
13754 structure type.)
13755
13756 Here we attempt to work backwards from the limited set of facts
13757 we're given, and we try to deduce from those facts, where GCC
13758 must have believed that the containing object started (within
13759 the structure type). The value we deduce is then used (by the
13760 callers of this routine) to generate DW_AT_location and
13761 DW_AT_bit_offset attributes for fields (both bit-fields and, in
13762 the case of DW_AT_location, regular fields as well). */
13763
13764 /* Figure out the bit-distance from the start of the structure to
13765 the "deepest" bit of the bit-field. */
13766 deepest_bitpos = bitpos_int + field_size_in_bits;
13767
13768 /* This is the tricky part. Use some fancy footwork to deduce
13769 where the lowest addressed bit of the containing object must
13770 be. */
13771 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
13772
13773 /* Round up to type_align by default. This works best for
13774 bitfields. */
13775 object_offset_in_bits
13776 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
13777
13778 if (object_offset_in_bits.ugt (bitpos_int))
13779 {
13780 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
13781
13782 /* Round up to decl_align instead. */
13783 object_offset_in_bits
13784 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
13785 }
13786 }
13787 else
13788 #endif /* PCC_BITFIELD_TYPE_MATTERS */
13789 object_offset_in_bits = bitpos_int;
13790
13791 object_offset_in_bytes
13792 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
13793 true, TRUNC_DIV_EXPR);
13794 return object_offset_in_bytes.to_shwi ();
13795 }
13796 \f
13797 /* The following routines define various Dwarf attributes and any data
13798 associated with them. */
13799
13800 /* Add a location description attribute value to a DIE.
13801
13802 This emits location attributes suitable for whole variables and
13803 whole parameters. Note that the location attributes for struct fields are
13804 generated by the routine `data_member_location_attribute' below. */
13805
13806 static inline void
13807 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
13808 dw_loc_list_ref descr)
13809 {
13810 if (descr == 0)
13811 return;
13812 if (single_element_loc_list_p (descr))
13813 add_AT_loc (die, attr_kind, descr->expr);
13814 else
13815 add_AT_loc_list (die, attr_kind, descr);
13816 }
13817
13818 /* Add DW_AT_accessibility attribute to DIE if needed. */
13819
13820 static void
13821 add_accessibility_attribute (dw_die_ref die, tree decl)
13822 {
13823 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
13824 children, otherwise the default is DW_ACCESS_public. In DWARF2
13825 the default has always been DW_ACCESS_public. */
13826 if (TREE_PROTECTED (decl))
13827 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13828 else if (TREE_PRIVATE (decl))
13829 {
13830 if (dwarf_version == 2
13831 || die->die_parent == NULL
13832 || die->die_parent->die_tag != DW_TAG_class_type)
13833 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
13834 }
13835 else if (dwarf_version > 2
13836 && die->die_parent
13837 && die->die_parent->die_tag == DW_TAG_class_type)
13838 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13839 }
13840
13841 /* Attach the specialized form of location attribute used for data members of
13842 struct and union types. In the special case of a FIELD_DECL node which
13843 represents a bit-field, the "offset" part of this special location
13844 descriptor must indicate the distance in bytes from the lowest-addressed
13845 byte of the containing struct or union type to the lowest-addressed byte of
13846 the "containing object" for the bit-field. (See the `field_byte_offset'
13847 function above).
13848
13849 For any given bit-field, the "containing object" is a hypothetical object
13850 (of some integral or enum type) within which the given bit-field lives. The
13851 type of this hypothetical "containing object" is always the same as the
13852 declared type of the individual bit-field itself (for GCC anyway... the
13853 DWARF spec doesn't actually mandate this). Note that it is the size (in
13854 bytes) of the hypothetical "containing object" which will be given in the
13855 DW_AT_byte_size attribute for this bit-field. (See the
13856 `byte_size_attribute' function below.) It is also used when calculating the
13857 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
13858 function below.) */
13859
13860 static void
13861 add_data_member_location_attribute (dw_die_ref die, tree decl)
13862 {
13863 HOST_WIDE_INT offset;
13864 dw_loc_descr_ref loc_descr = 0;
13865
13866 if (TREE_CODE (decl) == TREE_BINFO)
13867 {
13868 /* We're working on the TAG_inheritance for a base class. */
13869 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
13870 {
13871 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
13872 aren't at a fixed offset from all (sub)objects of the same
13873 type. We need to extract the appropriate offset from our
13874 vtable. The following dwarf expression means
13875
13876 BaseAddr = ObAddr + *((*ObAddr) - Offset)
13877
13878 This is specific to the V3 ABI, of course. */
13879
13880 dw_loc_descr_ref tmp;
13881
13882 /* Make a copy of the object address. */
13883 tmp = new_loc_descr (DW_OP_dup, 0, 0);
13884 add_loc_descr (&loc_descr, tmp);
13885
13886 /* Extract the vtable address. */
13887 tmp = new_loc_descr (DW_OP_deref, 0, 0);
13888 add_loc_descr (&loc_descr, tmp);
13889
13890 /* Calculate the address of the offset. */
13891 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
13892 gcc_assert (offset < 0);
13893
13894 tmp = int_loc_descriptor (-offset);
13895 add_loc_descr (&loc_descr, tmp);
13896 tmp = new_loc_descr (DW_OP_minus, 0, 0);
13897 add_loc_descr (&loc_descr, tmp);
13898
13899 /* Extract the offset. */
13900 tmp = new_loc_descr (DW_OP_deref, 0, 0);
13901 add_loc_descr (&loc_descr, tmp);
13902
13903 /* Add it to the object address. */
13904 tmp = new_loc_descr (DW_OP_plus, 0, 0);
13905 add_loc_descr (&loc_descr, tmp);
13906 }
13907 else
13908 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
13909 }
13910 else
13911 offset = field_byte_offset (decl);
13912
13913 if (! loc_descr)
13914 {
13915 if (dwarf_version > 2)
13916 {
13917 /* Don't need to output a location expression, just the constant. */
13918 if (offset < 0)
13919 add_AT_int (die, DW_AT_data_member_location, offset);
13920 else
13921 add_AT_unsigned (die, DW_AT_data_member_location, offset);
13922 return;
13923 }
13924 else
13925 {
13926 enum dwarf_location_atom op;
13927
13928 /* The DWARF2 standard says that we should assume that the structure
13929 address is already on the stack, so we can specify a structure
13930 field address by using DW_OP_plus_uconst. */
13931 op = DW_OP_plus_uconst;
13932 loc_descr = new_loc_descr (op, offset, 0);
13933 }
13934 }
13935
13936 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
13937 }
13938
13939 /* Writes integer values to dw_vec_const array. */
13940
13941 static void
13942 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
13943 {
13944 while (size != 0)
13945 {
13946 *dest++ = val & 0xff;
13947 val >>= 8;
13948 --size;
13949 }
13950 }
13951
13952 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
13953
13954 static HOST_WIDE_INT
13955 extract_int (const unsigned char *src, unsigned int size)
13956 {
13957 HOST_WIDE_INT val = 0;
13958
13959 src += size;
13960 while (size != 0)
13961 {
13962 val <<= 8;
13963 val |= *--src & 0xff;
13964 --size;
13965 }
13966 return val;
13967 }
13968
13969 /* Writes double_int values to dw_vec_const array. */
13970
13971 static void
13972 insert_double (double_int val, unsigned char *dest)
13973 {
13974 unsigned char *p0 = dest;
13975 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
13976
13977 if (WORDS_BIG_ENDIAN)
13978 {
13979 p0 = p1;
13980 p1 = dest;
13981 }
13982
13983 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
13984 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
13985 }
13986
13987 /* Writes floating point values to dw_vec_const array. */
13988
13989 static void
13990 insert_float (const_rtx rtl, unsigned char *array)
13991 {
13992 REAL_VALUE_TYPE rv;
13993 long val[4];
13994 int i;
13995
13996 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
13997 real_to_target (val, &rv, GET_MODE (rtl));
13998
13999 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14000 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14001 {
14002 insert_int (val[i], 4, array);
14003 array += 4;
14004 }
14005 }
14006
14007 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14008 does not have a "location" either in memory or in a register. These
14009 things can arise in GNU C when a constant is passed as an actual parameter
14010 to an inlined function. They can also arise in C++ where declared
14011 constants do not necessarily get memory "homes". */
14012
14013 static bool
14014 add_const_value_attribute (dw_die_ref die, rtx rtl)
14015 {
14016 switch (GET_CODE (rtl))
14017 {
14018 case CONST_INT:
14019 {
14020 HOST_WIDE_INT val = INTVAL (rtl);
14021
14022 if (val < 0)
14023 add_AT_int (die, DW_AT_const_value, val);
14024 else
14025 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14026 }
14027 return true;
14028
14029 case CONST_DOUBLE:
14030 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14031 floating-point constant. A CONST_DOUBLE is used whenever the
14032 constant requires more than one word in order to be adequately
14033 represented. */
14034 {
14035 enum machine_mode mode = GET_MODE (rtl);
14036
14037 if (SCALAR_FLOAT_MODE_P (mode))
14038 {
14039 unsigned int length = GET_MODE_SIZE (mode);
14040 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14041
14042 insert_float (rtl, array);
14043 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14044 }
14045 else
14046 add_AT_double (die, DW_AT_const_value,
14047 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14048 }
14049 return true;
14050
14051 case CONST_VECTOR:
14052 {
14053 enum machine_mode mode = GET_MODE (rtl);
14054 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14055 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14056 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14057 (length * elt_size);
14058 unsigned int i;
14059 unsigned char *p;
14060
14061 switch (GET_MODE_CLASS (mode))
14062 {
14063 case MODE_VECTOR_INT:
14064 for (i = 0, p = array; i < length; i++, p += elt_size)
14065 {
14066 rtx elt = CONST_VECTOR_ELT (rtl, i);
14067 double_int val = rtx_to_double_int (elt);
14068
14069 if (elt_size <= sizeof (HOST_WIDE_INT))
14070 insert_int (val.to_shwi (), elt_size, p);
14071 else
14072 {
14073 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14074 insert_double (val, p);
14075 }
14076 }
14077 break;
14078
14079 case MODE_VECTOR_FLOAT:
14080 for (i = 0, p = array; i < length; i++, p += elt_size)
14081 {
14082 rtx elt = CONST_VECTOR_ELT (rtl, i);
14083 insert_float (elt, p);
14084 }
14085 break;
14086
14087 default:
14088 gcc_unreachable ();
14089 }
14090
14091 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14092 }
14093 return true;
14094
14095 case CONST_STRING:
14096 if (dwarf_version >= 4 || !dwarf_strict)
14097 {
14098 dw_loc_descr_ref loc_result;
14099 resolve_one_addr (&rtl, NULL);
14100 rtl_addr:
14101 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14102 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14103 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14104 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14105 add_AT_loc (die, DW_AT_location, loc_result);
14106 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14107 return true;
14108 }
14109 return false;
14110
14111 case CONST:
14112 if (CONSTANT_P (XEXP (rtl, 0)))
14113 return add_const_value_attribute (die, XEXP (rtl, 0));
14114 /* FALLTHROUGH */
14115 case SYMBOL_REF:
14116 if (!const_ok_for_output (rtl))
14117 return false;
14118 case LABEL_REF:
14119 if (dwarf_version >= 4 || !dwarf_strict)
14120 goto rtl_addr;
14121 return false;
14122
14123 case PLUS:
14124 /* In cases where an inlined instance of an inline function is passed
14125 the address of an `auto' variable (which is local to the caller) we
14126 can get a situation where the DECL_RTL of the artificial local
14127 variable (for the inlining) which acts as a stand-in for the
14128 corresponding formal parameter (of the inline function) will look
14129 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14130 exactly a compile-time constant expression, but it isn't the address
14131 of the (artificial) local variable either. Rather, it represents the
14132 *value* which the artificial local variable always has during its
14133 lifetime. We currently have no way to represent such quasi-constant
14134 values in Dwarf, so for now we just punt and generate nothing. */
14135 return false;
14136
14137 case HIGH:
14138 case CONST_FIXED:
14139 return false;
14140
14141 case MEM:
14142 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14143 && MEM_READONLY_P (rtl)
14144 && GET_MODE (rtl) == BLKmode)
14145 {
14146 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14147 return true;
14148 }
14149 return false;
14150
14151 default:
14152 /* No other kinds of rtx should be possible here. */
14153 gcc_unreachable ();
14154 }
14155 return false;
14156 }
14157
14158 /* Determine whether the evaluation of EXPR references any variables
14159 or functions which aren't otherwise used (and therefore may not be
14160 output). */
14161 static tree
14162 reference_to_unused (tree * tp, int * walk_subtrees,
14163 void * data ATTRIBUTE_UNUSED)
14164 {
14165 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14166 *walk_subtrees = 0;
14167
14168 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14169 && ! TREE_ASM_WRITTEN (*tp))
14170 return *tp;
14171 /* ??? The C++ FE emits debug information for using decls, so
14172 putting gcc_unreachable here falls over. See PR31899. For now
14173 be conservative. */
14174 else if (!cgraph_global_info_ready
14175 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14176 return *tp;
14177 else if (TREE_CODE (*tp) == VAR_DECL)
14178 {
14179 struct varpool_node *node = varpool_get_node (*tp);
14180 if (!node || !node->analyzed)
14181 return *tp;
14182 }
14183 else if (TREE_CODE (*tp) == FUNCTION_DECL
14184 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14185 {
14186 /* The call graph machinery must have finished analyzing,
14187 optimizing and gimplifying the CU by now.
14188 So if *TP has no call graph node associated
14189 to it, it means *TP will not be emitted. */
14190 if (!cgraph_get_node (*tp))
14191 return *tp;
14192 }
14193 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14194 return *tp;
14195
14196 return NULL_TREE;
14197 }
14198
14199 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14200 for use in a later add_const_value_attribute call. */
14201
14202 static rtx
14203 rtl_for_decl_init (tree init, tree type)
14204 {
14205 rtx rtl = NULL_RTX;
14206
14207 STRIP_NOPS (init);
14208
14209 /* If a variable is initialized with a string constant without embedded
14210 zeros, build CONST_STRING. */
14211 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14212 {
14213 tree enttype = TREE_TYPE (type);
14214 tree domain = TYPE_DOMAIN (type);
14215 enum machine_mode mode = TYPE_MODE (enttype);
14216
14217 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14218 && domain
14219 && integer_zerop (TYPE_MIN_VALUE (domain))
14220 && compare_tree_int (TYPE_MAX_VALUE (domain),
14221 TREE_STRING_LENGTH (init) - 1) == 0
14222 && ((size_t) TREE_STRING_LENGTH (init)
14223 == strlen (TREE_STRING_POINTER (init)) + 1))
14224 {
14225 rtl = gen_rtx_CONST_STRING (VOIDmode,
14226 ggc_strdup (TREE_STRING_POINTER (init)));
14227 rtl = gen_rtx_MEM (BLKmode, rtl);
14228 MEM_READONLY_P (rtl) = 1;
14229 }
14230 }
14231 /* Other aggregates, and complex values, could be represented using
14232 CONCAT: FIXME! */
14233 else if (AGGREGATE_TYPE_P (type)
14234 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14235 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14236 || TREE_CODE (type) == COMPLEX_TYPE)
14237 ;
14238 /* Vectors only work if their mode is supported by the target.
14239 FIXME: generic vectors ought to work too. */
14240 else if (TREE_CODE (type) == VECTOR_TYPE
14241 && !VECTOR_MODE_P (TYPE_MODE (type)))
14242 ;
14243 /* If the initializer is something that we know will expand into an
14244 immediate RTL constant, expand it now. We must be careful not to
14245 reference variables which won't be output. */
14246 else if (initializer_constant_valid_p (init, type)
14247 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14248 {
14249 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14250 possible. */
14251 if (TREE_CODE (type) == VECTOR_TYPE)
14252 switch (TREE_CODE (init))
14253 {
14254 case VECTOR_CST:
14255 break;
14256 case CONSTRUCTOR:
14257 if (TREE_CONSTANT (init))
14258 {
14259 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14260 bool constant_p = true;
14261 tree value;
14262 unsigned HOST_WIDE_INT ix;
14263
14264 /* Even when ctor is constant, it might contain non-*_CST
14265 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14266 belong into VECTOR_CST nodes. */
14267 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14268 if (!CONSTANT_CLASS_P (value))
14269 {
14270 constant_p = false;
14271 break;
14272 }
14273
14274 if (constant_p)
14275 {
14276 init = build_vector_from_ctor (type, elts);
14277 break;
14278 }
14279 }
14280 /* FALLTHRU */
14281
14282 default:
14283 return NULL;
14284 }
14285
14286 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14287
14288 /* If expand_expr returns a MEM, it wasn't immediate. */
14289 gcc_assert (!rtl || !MEM_P (rtl));
14290 }
14291
14292 return rtl;
14293 }
14294
14295 /* Generate RTL for the variable DECL to represent its location. */
14296
14297 static rtx
14298 rtl_for_decl_location (tree decl)
14299 {
14300 rtx rtl;
14301
14302 /* Here we have to decide where we are going to say the parameter "lives"
14303 (as far as the debugger is concerned). We only have a couple of
14304 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14305
14306 DECL_RTL normally indicates where the parameter lives during most of the
14307 activation of the function. If optimization is enabled however, this
14308 could be either NULL or else a pseudo-reg. Both of those cases indicate
14309 that the parameter doesn't really live anywhere (as far as the code
14310 generation parts of GCC are concerned) during most of the function's
14311 activation. That will happen (for example) if the parameter is never
14312 referenced within the function.
14313
14314 We could just generate a location descriptor here for all non-NULL
14315 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14316 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14317 where DECL_RTL is NULL or is a pseudo-reg.
14318
14319 Note however that we can only get away with using DECL_INCOMING_RTL as
14320 a backup substitute for DECL_RTL in certain limited cases. In cases
14321 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14322 we can be sure that the parameter was passed using the same type as it is
14323 declared to have within the function, and that its DECL_INCOMING_RTL
14324 points us to a place where a value of that type is passed.
14325
14326 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14327 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14328 because in these cases DECL_INCOMING_RTL points us to a value of some
14329 type which is *different* from the type of the parameter itself. Thus,
14330 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14331 such cases, the debugger would end up (for example) trying to fetch a
14332 `float' from a place which actually contains the first part of a
14333 `double'. That would lead to really incorrect and confusing
14334 output at debug-time.
14335
14336 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14337 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
14338 are a couple of exceptions however. On little-endian machines we can
14339 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14340 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14341 an integral type that is smaller than TREE_TYPE (decl). These cases arise
14342 when (on a little-endian machine) a non-prototyped function has a
14343 parameter declared to be of type `short' or `char'. In such cases,
14344 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14345 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14346 passed `int' value. If the debugger then uses that address to fetch
14347 a `short' or a `char' (on a little-endian machine) the result will be
14348 the correct data, so we allow for such exceptional cases below.
14349
14350 Note that our goal here is to describe the place where the given formal
14351 parameter lives during most of the function's activation (i.e. between the
14352 end of the prologue and the start of the epilogue). We'll do that as best
14353 as we can. Note however that if the given formal parameter is modified
14354 sometime during the execution of the function, then a stack backtrace (at
14355 debug-time) will show the function as having been called with the *new*
14356 value rather than the value which was originally passed in. This happens
14357 rarely enough that it is not a major problem, but it *is* a problem, and
14358 I'd like to fix it.
14359
14360 A future version of dwarf2out.c may generate two additional attributes for
14361 any given DW_TAG_formal_parameter DIE which will describe the "passed
14362 type" and the "passed location" for the given formal parameter in addition
14363 to the attributes we now generate to indicate the "declared type" and the
14364 "active location" for each parameter. This additional set of attributes
14365 could be used by debuggers for stack backtraces. Separately, note that
14366 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14367 This happens (for example) for inlined-instances of inline function formal
14368 parameters which are never referenced. This really shouldn't be
14369 happening. All PARM_DECL nodes should get valid non-NULL
14370 DECL_INCOMING_RTL values. FIXME. */
14371
14372 /* Use DECL_RTL as the "location" unless we find something better. */
14373 rtl = DECL_RTL_IF_SET (decl);
14374
14375 /* When generating abstract instances, ignore everything except
14376 constants, symbols living in memory, and symbols living in
14377 fixed registers. */
14378 if (! reload_completed)
14379 {
14380 if (rtl
14381 && (CONSTANT_P (rtl)
14382 || (MEM_P (rtl)
14383 && CONSTANT_P (XEXP (rtl, 0)))
14384 || (REG_P (rtl)
14385 && TREE_CODE (decl) == VAR_DECL
14386 && TREE_STATIC (decl))))
14387 {
14388 rtl = targetm.delegitimize_address (rtl);
14389 return rtl;
14390 }
14391 rtl = NULL_RTX;
14392 }
14393 else if (TREE_CODE (decl) == PARM_DECL)
14394 {
14395 if (rtl == NULL_RTX
14396 || is_pseudo_reg (rtl)
14397 || (MEM_P (rtl)
14398 && is_pseudo_reg (XEXP (rtl, 0))
14399 && DECL_INCOMING_RTL (decl)
14400 && MEM_P (DECL_INCOMING_RTL (decl))
14401 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14402 {
14403 tree declared_type = TREE_TYPE (decl);
14404 tree passed_type = DECL_ARG_TYPE (decl);
14405 enum machine_mode dmode = TYPE_MODE (declared_type);
14406 enum machine_mode pmode = TYPE_MODE (passed_type);
14407
14408 /* This decl represents a formal parameter which was optimized out.
14409 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14410 all cases where (rtl == NULL_RTX) just below. */
14411 if (dmode == pmode)
14412 rtl = DECL_INCOMING_RTL (decl);
14413 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14414 && SCALAR_INT_MODE_P (dmode)
14415 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14416 && DECL_INCOMING_RTL (decl))
14417 {
14418 rtx inc = DECL_INCOMING_RTL (decl);
14419 if (REG_P (inc))
14420 rtl = inc;
14421 else if (MEM_P (inc))
14422 {
14423 if (BYTES_BIG_ENDIAN)
14424 rtl = adjust_address_nv (inc, dmode,
14425 GET_MODE_SIZE (pmode)
14426 - GET_MODE_SIZE (dmode));
14427 else
14428 rtl = inc;
14429 }
14430 }
14431 }
14432
14433 /* If the parm was passed in registers, but lives on the stack, then
14434 make a big endian correction if the mode of the type of the
14435 parameter is not the same as the mode of the rtl. */
14436 /* ??? This is the same series of checks that are made in dbxout.c before
14437 we reach the big endian correction code there. It isn't clear if all
14438 of these checks are necessary here, but keeping them all is the safe
14439 thing to do. */
14440 else if (MEM_P (rtl)
14441 && XEXP (rtl, 0) != const0_rtx
14442 && ! CONSTANT_P (XEXP (rtl, 0))
14443 /* Not passed in memory. */
14444 && !MEM_P (DECL_INCOMING_RTL (decl))
14445 /* Not passed by invisible reference. */
14446 && (!REG_P (XEXP (rtl, 0))
14447 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14448 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14449 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14450 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14451 #endif
14452 )
14453 /* Big endian correction check. */
14454 && BYTES_BIG_ENDIAN
14455 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14456 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14457 < UNITS_PER_WORD))
14458 {
14459 enum machine_mode addr_mode = get_address_mode (rtl);
14460 int offset = (UNITS_PER_WORD
14461 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14462
14463 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14464 plus_constant (addr_mode, XEXP (rtl, 0), offset));
14465 }
14466 }
14467 else if (TREE_CODE (decl) == VAR_DECL
14468 && rtl
14469 && MEM_P (rtl)
14470 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14471 && BYTES_BIG_ENDIAN)
14472 {
14473 enum machine_mode addr_mode = get_address_mode (rtl);
14474 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14475 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14476
14477 /* If a variable is declared "register" yet is smaller than
14478 a register, then if we store the variable to memory, it
14479 looks like we're storing a register-sized value, when in
14480 fact we are not. We need to adjust the offset of the
14481 storage location to reflect the actual value's bytes,
14482 else gdb will not be able to display it. */
14483 if (rsize > dsize)
14484 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14485 plus_constant (addr_mode, XEXP (rtl, 0),
14486 rsize - dsize));
14487 }
14488
14489 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14490 and will have been substituted directly into all expressions that use it.
14491 C does not have such a concept, but C++ and other languages do. */
14492 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14493 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14494
14495 if (rtl)
14496 rtl = targetm.delegitimize_address (rtl);
14497
14498 /* If we don't look past the constant pool, we risk emitting a
14499 reference to a constant pool entry that isn't referenced from
14500 code, and thus is not emitted. */
14501 if (rtl)
14502 rtl = avoid_constant_pool_reference (rtl);
14503
14504 /* Try harder to get a rtl. If this symbol ends up not being emitted
14505 in the current CU, resolve_addr will remove the expression referencing
14506 it. */
14507 if (rtl == NULL_RTX
14508 && TREE_CODE (decl) == VAR_DECL
14509 && !DECL_EXTERNAL (decl)
14510 && TREE_STATIC (decl)
14511 && DECL_NAME (decl)
14512 && !DECL_HARD_REGISTER (decl)
14513 && DECL_MODE (decl) != VOIDmode)
14514 {
14515 rtl = make_decl_rtl_for_debug (decl);
14516 if (!MEM_P (rtl)
14517 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14518 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14519 rtl = NULL_RTX;
14520 }
14521
14522 return rtl;
14523 }
14524
14525 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
14526 returned. If so, the decl for the COMMON block is returned, and the
14527 value is the offset into the common block for the symbol. */
14528
14529 static tree
14530 fortran_common (tree decl, HOST_WIDE_INT *value)
14531 {
14532 tree val_expr, cvar;
14533 enum machine_mode mode;
14534 HOST_WIDE_INT bitsize, bitpos;
14535 tree offset;
14536 int unsignedp, volatilep = 0;
14537
14538 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14539 it does not have a value (the offset into the common area), or if it
14540 is thread local (as opposed to global) then it isn't common, and shouldn't
14541 be handled as such. */
14542 if (TREE_CODE (decl) != VAR_DECL
14543 || !TREE_STATIC (decl)
14544 || !DECL_HAS_VALUE_EXPR_P (decl)
14545 || !is_fortran ())
14546 return NULL_TREE;
14547
14548 val_expr = DECL_VALUE_EXPR (decl);
14549 if (TREE_CODE (val_expr) != COMPONENT_REF)
14550 return NULL_TREE;
14551
14552 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14553 &mode, &unsignedp, &volatilep, true);
14554
14555 if (cvar == NULL_TREE
14556 || TREE_CODE (cvar) != VAR_DECL
14557 || DECL_ARTIFICIAL (cvar)
14558 || !TREE_PUBLIC (cvar))
14559 return NULL_TREE;
14560
14561 *value = 0;
14562 if (offset != NULL)
14563 {
14564 if (!host_integerp (offset, 0))
14565 return NULL_TREE;
14566 *value = tree_low_cst (offset, 0);
14567 }
14568 if (bitpos != 0)
14569 *value += bitpos / BITS_PER_UNIT;
14570
14571 return cvar;
14572 }
14573
14574 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14575 data attribute for a variable or a parameter. We generate the
14576 DW_AT_const_value attribute only in those cases where the given variable
14577 or parameter does not have a true "location" either in memory or in a
14578 register. This can happen (for example) when a constant is passed as an
14579 actual argument in a call to an inline function. (It's possible that
14580 these things can crop up in other ways also.) Note that one type of
14581 constant value which can be passed into an inlined function is a constant
14582 pointer. This can happen for example if an actual argument in an inlined
14583 function call evaluates to a compile-time constant address.
14584
14585 CACHE_P is true if it is worth caching the location list for DECL,
14586 so that future calls can reuse it rather than regenerate it from scratch.
14587 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14588 since we will need to refer to them each time the function is inlined. */
14589
14590 static bool
14591 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
14592 enum dwarf_attribute attr)
14593 {
14594 rtx rtl;
14595 dw_loc_list_ref list;
14596 var_loc_list *loc_list;
14597 cached_dw_loc_list *cache;
14598 void **slot;
14599
14600 if (TREE_CODE (decl) == ERROR_MARK)
14601 return false;
14602
14603 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14604 || TREE_CODE (decl) == RESULT_DECL);
14605
14606 /* Try to get some constant RTL for this decl, and use that as the value of
14607 the location. */
14608
14609 rtl = rtl_for_decl_location (decl);
14610 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14611 && add_const_value_attribute (die, rtl))
14612 return true;
14613
14614 /* See if we have single element location list that is equivalent to
14615 a constant value. That way we are better to use add_const_value_attribute
14616 rather than expanding constant value equivalent. */
14617 loc_list = lookup_decl_loc (decl);
14618 if (loc_list
14619 && loc_list->first
14620 && loc_list->first->next == NULL
14621 && NOTE_P (loc_list->first->loc)
14622 && NOTE_VAR_LOCATION (loc_list->first->loc)
14623 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
14624 {
14625 struct var_loc_node *node;
14626
14627 node = loc_list->first;
14628 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
14629 if (GET_CODE (rtl) == EXPR_LIST)
14630 rtl = XEXP (rtl, 0);
14631 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14632 && add_const_value_attribute (die, rtl))
14633 return true;
14634 }
14635 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
14636 list several times. See if we've already cached the contents. */
14637 list = NULL;
14638 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
14639 cache_p = false;
14640 if (cache_p)
14641 {
14642 cache = (cached_dw_loc_list *)
14643 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
14644 if (cache)
14645 list = cache->loc_list;
14646 }
14647 if (list == NULL)
14648 {
14649 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
14650 /* It is usually worth caching this result if the decl is from
14651 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
14652 if (cache_p && list && list->dw_loc_next)
14653 {
14654 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
14655 DECL_UID (decl), INSERT);
14656 cache = ggc_alloc_cleared_cached_dw_loc_list ();
14657 cache->decl_id = DECL_UID (decl);
14658 cache->loc_list = list;
14659 *slot = cache;
14660 }
14661 }
14662 if (list)
14663 {
14664 add_AT_location_description (die, attr, list);
14665 return true;
14666 }
14667 /* None of that worked, so it must not really have a location;
14668 try adding a constant value attribute from the DECL_INITIAL. */
14669 return tree_add_const_value_attribute_for_decl (die, decl);
14670 }
14671
14672 /* Add VARIABLE and DIE into deferred locations list. */
14673
14674 static void
14675 defer_location (tree variable, dw_die_ref die)
14676 {
14677 deferred_locations entry;
14678 entry.variable = variable;
14679 entry.die = die;
14680 VEC_safe_push (deferred_locations, gc, deferred_locations_list, entry);
14681 }
14682
14683 /* Helper function for tree_add_const_value_attribute. Natively encode
14684 initializer INIT into an array. Return true if successful. */
14685
14686 static bool
14687 native_encode_initializer (tree init, unsigned char *array, int size)
14688 {
14689 tree type;
14690
14691 if (init == NULL_TREE)
14692 return false;
14693
14694 STRIP_NOPS (init);
14695 switch (TREE_CODE (init))
14696 {
14697 case STRING_CST:
14698 type = TREE_TYPE (init);
14699 if (TREE_CODE (type) == ARRAY_TYPE)
14700 {
14701 tree enttype = TREE_TYPE (type);
14702 enum machine_mode mode = TYPE_MODE (enttype);
14703
14704 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
14705 return false;
14706 if (int_size_in_bytes (type) != size)
14707 return false;
14708 if (size > TREE_STRING_LENGTH (init))
14709 {
14710 memcpy (array, TREE_STRING_POINTER (init),
14711 TREE_STRING_LENGTH (init));
14712 memset (array + TREE_STRING_LENGTH (init),
14713 '\0', size - TREE_STRING_LENGTH (init));
14714 }
14715 else
14716 memcpy (array, TREE_STRING_POINTER (init), size);
14717 return true;
14718 }
14719 return false;
14720 case CONSTRUCTOR:
14721 type = TREE_TYPE (init);
14722 if (int_size_in_bytes (type) != size)
14723 return false;
14724 if (TREE_CODE (type) == ARRAY_TYPE)
14725 {
14726 HOST_WIDE_INT min_index;
14727 unsigned HOST_WIDE_INT cnt;
14728 int curpos = 0, fieldsize;
14729 constructor_elt *ce;
14730
14731 if (TYPE_DOMAIN (type) == NULL_TREE
14732 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
14733 return false;
14734
14735 fieldsize = int_size_in_bytes (TREE_TYPE (type));
14736 if (fieldsize <= 0)
14737 return false;
14738
14739 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
14740 memset (array, '\0', size);
14741 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14742 {
14743 tree val = ce->value;
14744 tree index = ce->index;
14745 int pos = curpos;
14746 if (index && TREE_CODE (index) == RANGE_EXPR)
14747 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
14748 * fieldsize;
14749 else if (index)
14750 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
14751
14752 if (val)
14753 {
14754 STRIP_NOPS (val);
14755 if (!native_encode_initializer (val, array + pos, fieldsize))
14756 return false;
14757 }
14758 curpos = pos + fieldsize;
14759 if (index && TREE_CODE (index) == RANGE_EXPR)
14760 {
14761 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
14762 - tree_low_cst (TREE_OPERAND (index, 0), 0);
14763 while (count-- > 0)
14764 {
14765 if (val)
14766 memcpy (array + curpos, array + pos, fieldsize);
14767 curpos += fieldsize;
14768 }
14769 }
14770 gcc_assert (curpos <= size);
14771 }
14772 return true;
14773 }
14774 else if (TREE_CODE (type) == RECORD_TYPE
14775 || TREE_CODE (type) == UNION_TYPE)
14776 {
14777 tree field = NULL_TREE;
14778 unsigned HOST_WIDE_INT cnt;
14779 constructor_elt *ce;
14780
14781 if (int_size_in_bytes (type) != size)
14782 return false;
14783
14784 if (TREE_CODE (type) == RECORD_TYPE)
14785 field = TYPE_FIELDS (type);
14786
14787 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14788 {
14789 tree val = ce->value;
14790 int pos, fieldsize;
14791
14792 if (ce->index != 0)
14793 field = ce->index;
14794
14795 if (val)
14796 STRIP_NOPS (val);
14797
14798 if (field == NULL_TREE || DECL_BIT_FIELD (field))
14799 return false;
14800
14801 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
14802 && TYPE_DOMAIN (TREE_TYPE (field))
14803 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
14804 return false;
14805 else if (DECL_SIZE_UNIT (field) == NULL_TREE
14806 || !host_integerp (DECL_SIZE_UNIT (field), 0))
14807 return false;
14808 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
14809 pos = int_byte_position (field);
14810 gcc_assert (pos + fieldsize <= size);
14811 if (val
14812 && !native_encode_initializer (val, array + pos, fieldsize))
14813 return false;
14814 }
14815 return true;
14816 }
14817 return false;
14818 case VIEW_CONVERT_EXPR:
14819 case NON_LVALUE_EXPR:
14820 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
14821 default:
14822 return native_encode_expr (init, array, size) == size;
14823 }
14824 }
14825
14826 /* Attach a DW_AT_const_value attribute to DIE. The value of the
14827 attribute is the const value T. */
14828
14829 static bool
14830 tree_add_const_value_attribute (dw_die_ref die, tree t)
14831 {
14832 tree init;
14833 tree type = TREE_TYPE (t);
14834 rtx rtl;
14835
14836 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
14837 return false;
14838
14839 init = t;
14840 gcc_assert (!DECL_P (init));
14841
14842 rtl = rtl_for_decl_init (init, type);
14843 if (rtl)
14844 return add_const_value_attribute (die, rtl);
14845 /* If the host and target are sane, try harder. */
14846 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
14847 && initializer_constant_valid_p (init, type))
14848 {
14849 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
14850 if (size > 0 && (int) size == size)
14851 {
14852 unsigned char *array = (unsigned char *)
14853 ggc_alloc_cleared_atomic (size);
14854
14855 if (native_encode_initializer (init, array, size))
14856 {
14857 add_AT_vec (die, DW_AT_const_value, size, 1, array);
14858 return true;
14859 }
14860 }
14861 }
14862 return false;
14863 }
14864
14865 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
14866 attribute is the const value of T, where T is an integral constant
14867 variable with static storage duration
14868 (so it can't be a PARM_DECL or a RESULT_DECL). */
14869
14870 static bool
14871 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
14872 {
14873
14874 if (!decl
14875 || (TREE_CODE (decl) != VAR_DECL
14876 && TREE_CODE (decl) != CONST_DECL)
14877 || (TREE_CODE (decl) == VAR_DECL
14878 && !TREE_STATIC (decl)))
14879 return false;
14880
14881 if (TREE_READONLY (decl)
14882 && ! TREE_THIS_VOLATILE (decl)
14883 && DECL_INITIAL (decl))
14884 /* OK */;
14885 else
14886 return false;
14887
14888 /* Don't add DW_AT_const_value if abstract origin already has one. */
14889 if (get_AT (var_die, DW_AT_const_value))
14890 return false;
14891
14892 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
14893 }
14894
14895 /* Convert the CFI instructions for the current function into a
14896 location list. This is used for DW_AT_frame_base when we targeting
14897 a dwarf2 consumer that does not support the dwarf3
14898 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
14899 expressions. */
14900
14901 static dw_loc_list_ref
14902 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
14903 {
14904 int ix;
14905 dw_fde_ref fde;
14906 dw_loc_list_ref list, *list_tail;
14907 dw_cfi_ref cfi;
14908 dw_cfa_location last_cfa, next_cfa;
14909 const char *start_label, *last_label, *section;
14910 dw_cfa_location remember;
14911
14912 fde = cfun->fde;
14913 gcc_assert (fde != NULL);
14914
14915 section = secname_for_decl (current_function_decl);
14916 list_tail = &list;
14917 list = NULL;
14918
14919 memset (&next_cfa, 0, sizeof (next_cfa));
14920 next_cfa.reg = INVALID_REGNUM;
14921 remember = next_cfa;
14922
14923 start_label = fde->dw_fde_begin;
14924
14925 /* ??? Bald assumption that the CIE opcode list does not contain
14926 advance opcodes. */
14927 FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
14928 lookup_cfa_1 (cfi, &next_cfa, &remember);
14929
14930 last_cfa = next_cfa;
14931 last_label = start_label;
14932
14933 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
14934 {
14935 /* If the first partition contained no CFI adjustments, the
14936 CIE opcodes apply to the whole first partition. */
14937 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14938 fde->dw_fde_begin, fde->dw_fde_end, section);
14939 list_tail =&(*list_tail)->dw_loc_next;
14940 start_label = last_label = fde->dw_fde_second_begin;
14941 }
14942
14943 FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
14944 {
14945 switch (cfi->dw_cfi_opc)
14946 {
14947 case DW_CFA_set_loc:
14948 case DW_CFA_advance_loc1:
14949 case DW_CFA_advance_loc2:
14950 case DW_CFA_advance_loc4:
14951 if (!cfa_equal_p (&last_cfa, &next_cfa))
14952 {
14953 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14954 start_label, last_label, section);
14955
14956 list_tail = &(*list_tail)->dw_loc_next;
14957 last_cfa = next_cfa;
14958 start_label = last_label;
14959 }
14960 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
14961 break;
14962
14963 case DW_CFA_advance_loc:
14964 /* The encoding is complex enough that we should never emit this. */
14965 gcc_unreachable ();
14966
14967 default:
14968 lookup_cfa_1 (cfi, &next_cfa, &remember);
14969 break;
14970 }
14971 if (ix + 1 == fde->dw_fde_switch_cfi_index)
14972 {
14973 if (!cfa_equal_p (&last_cfa, &next_cfa))
14974 {
14975 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14976 start_label, last_label, section);
14977
14978 list_tail = &(*list_tail)->dw_loc_next;
14979 last_cfa = next_cfa;
14980 start_label = last_label;
14981 }
14982 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14983 start_label, fde->dw_fde_end, section);
14984 list_tail = &(*list_tail)->dw_loc_next;
14985 start_label = last_label = fde->dw_fde_second_begin;
14986 }
14987 }
14988
14989 if (!cfa_equal_p (&last_cfa, &next_cfa))
14990 {
14991 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14992 start_label, last_label, section);
14993 list_tail = &(*list_tail)->dw_loc_next;
14994 start_label = last_label;
14995 }
14996
14997 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
14998 start_label,
14999 fde->dw_fde_second_begin
15000 ? fde->dw_fde_second_end : fde->dw_fde_end,
15001 section);
15002
15003 if (list && list->dw_loc_next)
15004 gen_llsym (list);
15005
15006 return list;
15007 }
15008
15009 /* Compute a displacement from the "steady-state frame pointer" to the
15010 frame base (often the same as the CFA), and store it in
15011 frame_pointer_fb_offset. OFFSET is added to the displacement
15012 before the latter is negated. */
15013
15014 static void
15015 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15016 {
15017 rtx reg, elim;
15018
15019 #ifdef FRAME_POINTER_CFA_OFFSET
15020 reg = frame_pointer_rtx;
15021 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15022 #else
15023 reg = arg_pointer_rtx;
15024 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15025 #endif
15026
15027 elim = (ira_use_lra_p
15028 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15029 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15030 if (GET_CODE (elim) == PLUS)
15031 {
15032 offset += INTVAL (XEXP (elim, 1));
15033 elim = XEXP (elim, 0);
15034 }
15035
15036 frame_pointer_fb_offset = -offset;
15037
15038 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15039 in which to eliminate. This is because it's stack pointer isn't
15040 directly accessible as a register within the ISA. To work around
15041 this, assume that while we cannot provide a proper value for
15042 frame_pointer_fb_offset, we won't need one either. */
15043 frame_pointer_fb_offset_valid
15044 = ((SUPPORTS_STACK_ALIGNMENT
15045 && (elim == hard_frame_pointer_rtx
15046 || elim == stack_pointer_rtx))
15047 || elim == (frame_pointer_needed
15048 ? hard_frame_pointer_rtx
15049 : stack_pointer_rtx));
15050 }
15051
15052 /* Generate a DW_AT_name attribute given some string value to be included as
15053 the value of the attribute. */
15054
15055 static void
15056 add_name_attribute (dw_die_ref die, const char *name_string)
15057 {
15058 if (name_string != NULL && *name_string != 0)
15059 {
15060 if (demangle_name_func)
15061 name_string = (*demangle_name_func) (name_string);
15062
15063 add_AT_string (die, DW_AT_name, name_string);
15064 }
15065 }
15066
15067 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15068 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15069 of TYPE accordingly.
15070
15071 ??? This is a temporary measure until after we're able to generate
15072 regular DWARF for the complex Ada type system. */
15073
15074 static void
15075 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15076 dw_die_ref context_die)
15077 {
15078 tree dtype;
15079 dw_die_ref dtype_die;
15080
15081 if (!lang_hooks.types.descriptive_type)
15082 return;
15083
15084 dtype = lang_hooks.types.descriptive_type (type);
15085 if (!dtype)
15086 return;
15087
15088 dtype_die = lookup_type_die (dtype);
15089 if (!dtype_die)
15090 {
15091 gen_type_die (dtype, context_die);
15092 dtype_die = lookup_type_die (dtype);
15093 gcc_assert (dtype_die);
15094 }
15095
15096 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15097 }
15098
15099 /* Generate a DW_AT_comp_dir attribute for DIE. */
15100
15101 static void
15102 add_comp_dir_attribute (dw_die_ref die)
15103 {
15104 const char *wd = get_src_pwd ();
15105 char *wd1;
15106
15107 if (wd == NULL)
15108 return;
15109
15110 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15111 {
15112 int wdlen;
15113
15114 wdlen = strlen (wd);
15115 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15116 strcpy (wd1, wd);
15117 wd1 [wdlen] = DIR_SEPARATOR;
15118 wd1 [wdlen + 1] = 0;
15119 wd = wd1;
15120 }
15121
15122 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15123 }
15124
15125 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15126 default. */
15127
15128 static int
15129 lower_bound_default (void)
15130 {
15131 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15132 {
15133 case DW_LANG_C:
15134 case DW_LANG_C89:
15135 case DW_LANG_C99:
15136 case DW_LANG_C_plus_plus:
15137 case DW_LANG_ObjC:
15138 case DW_LANG_ObjC_plus_plus:
15139 case DW_LANG_Java:
15140 return 0;
15141 case DW_LANG_Fortran77:
15142 case DW_LANG_Fortran90:
15143 case DW_LANG_Fortran95:
15144 return 1;
15145 case DW_LANG_UPC:
15146 case DW_LANG_D:
15147 case DW_LANG_Python:
15148 return dwarf_version >= 4 ? 0 : -1;
15149 case DW_LANG_Ada95:
15150 case DW_LANG_Ada83:
15151 case DW_LANG_Cobol74:
15152 case DW_LANG_Cobol85:
15153 case DW_LANG_Pascal83:
15154 case DW_LANG_Modula2:
15155 case DW_LANG_PLI:
15156 return dwarf_version >= 4 ? 1 : -1;
15157 default:
15158 return -1;
15159 }
15160 }
15161
15162 /* Given a tree node describing an array bound (either lower or upper) output
15163 a representation for that bound. */
15164
15165 static void
15166 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15167 {
15168 switch (TREE_CODE (bound))
15169 {
15170 case ERROR_MARK:
15171 return;
15172
15173 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15174 case INTEGER_CST:
15175 {
15176 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15177 int dflt;
15178
15179 /* Use the default if possible. */
15180 if (bound_attr == DW_AT_lower_bound
15181 && host_integerp (bound, 0)
15182 && (dflt = lower_bound_default ()) != -1
15183 && tree_low_cst (bound, 0) == dflt)
15184 ;
15185
15186 /* Otherwise represent the bound as an unsigned value with the
15187 precision of its type. The precision and signedness of the
15188 type will be necessary to re-interpret it unambiguously. */
15189 else if (prec < HOST_BITS_PER_WIDE_INT)
15190 {
15191 unsigned HOST_WIDE_INT mask
15192 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15193 add_AT_unsigned (subrange_die, bound_attr,
15194 TREE_INT_CST_LOW (bound) & mask);
15195 }
15196 else if (prec == HOST_BITS_PER_WIDE_INT
15197 || TREE_INT_CST_HIGH (bound) == 0)
15198 add_AT_unsigned (subrange_die, bound_attr,
15199 TREE_INT_CST_LOW (bound));
15200 else
15201 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15202 TREE_INT_CST_LOW (bound));
15203 }
15204 break;
15205
15206 CASE_CONVERT:
15207 case VIEW_CONVERT_EXPR:
15208 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15209 break;
15210
15211 case SAVE_EXPR:
15212 break;
15213
15214 case VAR_DECL:
15215 case PARM_DECL:
15216 case RESULT_DECL:
15217 {
15218 dw_die_ref decl_die = lookup_decl_die (bound);
15219
15220 /* ??? Can this happen, or should the variable have been bound
15221 first? Probably it can, since I imagine that we try to create
15222 the types of parameters in the order in which they exist in
15223 the list, and won't have created a forward reference to a
15224 later parameter. */
15225 if (decl_die != NULL)
15226 {
15227 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15228 break;
15229 }
15230 }
15231 /* FALLTHRU */
15232
15233 default:
15234 {
15235 /* Otherwise try to create a stack operation procedure to
15236 evaluate the value of the array bound. */
15237
15238 dw_die_ref ctx, decl_die;
15239 dw_loc_list_ref list;
15240
15241 list = loc_list_from_tree (bound, 2);
15242 if (list == NULL || single_element_loc_list_p (list))
15243 {
15244 /* If DW_AT_*bound is not a reference nor constant, it is
15245 a DWARF expression rather than location description.
15246 For that loc_list_from_tree (bound, 0) is needed.
15247 If that fails to give a single element list,
15248 fall back to outputting this as a reference anyway. */
15249 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15250 if (list2 && single_element_loc_list_p (list2))
15251 {
15252 add_AT_loc (subrange_die, bound_attr, list2->expr);
15253 break;
15254 }
15255 }
15256 if (list == NULL)
15257 break;
15258
15259 if (current_function_decl == 0)
15260 ctx = comp_unit_die ();
15261 else
15262 ctx = lookup_decl_die (current_function_decl);
15263
15264 decl_die = new_die (DW_TAG_variable, ctx, bound);
15265 add_AT_flag (decl_die, DW_AT_artificial, 1);
15266 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15267 add_AT_location_description (decl_die, DW_AT_location, list);
15268 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15269 break;
15270 }
15271 }
15272 }
15273
15274 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15275 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15276 Note that the block of subscript information for an array type also
15277 includes information about the element type of the given array type. */
15278
15279 static void
15280 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15281 {
15282 unsigned dimension_number;
15283 tree lower, upper;
15284 dw_die_ref subrange_die;
15285
15286 for (dimension_number = 0;
15287 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15288 type = TREE_TYPE (type), dimension_number++)
15289 {
15290 tree domain = TYPE_DOMAIN (type);
15291
15292 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15293 break;
15294
15295 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15296 and (in GNU C only) variable bounds. Handle all three forms
15297 here. */
15298 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15299 if (domain)
15300 {
15301 /* We have an array type with specified bounds. */
15302 lower = TYPE_MIN_VALUE (domain);
15303 upper = TYPE_MAX_VALUE (domain);
15304
15305 /* Define the index type. */
15306 if (TREE_TYPE (domain))
15307 {
15308 /* ??? This is probably an Ada unnamed subrange type. Ignore the
15309 TREE_TYPE field. We can't emit debug info for this
15310 because it is an unnamed integral type. */
15311 if (TREE_CODE (domain) == INTEGER_TYPE
15312 && TYPE_NAME (domain) == NULL_TREE
15313 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15314 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15315 ;
15316 else
15317 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15318 type_die);
15319 }
15320
15321 /* ??? If upper is NULL, the array has unspecified length,
15322 but it does have a lower bound. This happens with Fortran
15323 dimension arr(N:*)
15324 Since the debugger is definitely going to need to know N
15325 to produce useful results, go ahead and output the lower
15326 bound solo, and hope the debugger can cope. */
15327
15328 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15329 if (upper)
15330 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15331 }
15332
15333 /* Otherwise we have an array type with an unspecified length. The
15334 DWARF-2 spec does not say how to handle this; let's just leave out the
15335 bounds. */
15336 }
15337 }
15338
15339 static void
15340 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15341 {
15342 dw_die_ref decl_die;
15343 unsigned size;
15344
15345 switch (TREE_CODE (tree_node))
15346 {
15347 case ERROR_MARK:
15348 size = 0;
15349 break;
15350 case ENUMERAL_TYPE:
15351 case RECORD_TYPE:
15352 case UNION_TYPE:
15353 case QUAL_UNION_TYPE:
15354 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
15355 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
15356 {
15357 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
15358 return;
15359 }
15360 size = int_size_in_bytes (tree_node);
15361 break;
15362 case FIELD_DECL:
15363 /* For a data member of a struct or union, the DW_AT_byte_size is
15364 generally given as the number of bytes normally allocated for an
15365 object of the *declared* type of the member itself. This is true
15366 even for bit-fields. */
15367 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15368 break;
15369 default:
15370 gcc_unreachable ();
15371 }
15372
15373 /* Note that `size' might be -1 when we get to this point. If it is, that
15374 indicates that the byte size of the entity in question is variable. We
15375 have no good way of expressing this fact in Dwarf at the present time,
15376 so just let the -1 pass on through. */
15377 add_AT_unsigned (die, DW_AT_byte_size, size);
15378 }
15379
15380 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15381 which specifies the distance in bits from the highest order bit of the
15382 "containing object" for the bit-field to the highest order bit of the
15383 bit-field itself.
15384
15385 For any given bit-field, the "containing object" is a hypothetical object
15386 (of some integral or enum type) within which the given bit-field lives. The
15387 type of this hypothetical "containing object" is always the same as the
15388 declared type of the individual bit-field itself. The determination of the
15389 exact location of the "containing object" for a bit-field is rather
15390 complicated. It's handled by the `field_byte_offset' function (above).
15391
15392 Note that it is the size (in bytes) of the hypothetical "containing object"
15393 which will be given in the DW_AT_byte_size attribute for this bit-field.
15394 (See `byte_size_attribute' above). */
15395
15396 static inline void
15397 add_bit_offset_attribute (dw_die_ref die, tree decl)
15398 {
15399 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15400 tree type = DECL_BIT_FIELD_TYPE (decl);
15401 HOST_WIDE_INT bitpos_int;
15402 HOST_WIDE_INT highest_order_object_bit_offset;
15403 HOST_WIDE_INT highest_order_field_bit_offset;
15404 HOST_WIDE_INT bit_offset;
15405
15406 /* Must be a field and a bit field. */
15407 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15408
15409 /* We can't yet handle bit-fields whose offsets are variable, so if we
15410 encounter such things, just return without generating any attribute
15411 whatsoever. Likewise for variable or too large size. */
15412 if (! host_integerp (bit_position (decl), 0)
15413 || ! host_integerp (DECL_SIZE (decl), 1))
15414 return;
15415
15416 bitpos_int = int_bit_position (decl);
15417
15418 /* Note that the bit offset is always the distance (in bits) from the
15419 highest-order bit of the "containing object" to the highest-order bit of
15420 the bit-field itself. Since the "high-order end" of any object or field
15421 is different on big-endian and little-endian machines, the computation
15422 below must take account of these differences. */
15423 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15424 highest_order_field_bit_offset = bitpos_int;
15425
15426 if (! BYTES_BIG_ENDIAN)
15427 {
15428 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15429 highest_order_object_bit_offset += simple_type_size_in_bits (type);
15430 }
15431
15432 bit_offset
15433 = (! BYTES_BIG_ENDIAN
15434 ? highest_order_object_bit_offset - highest_order_field_bit_offset
15435 : highest_order_field_bit_offset - highest_order_object_bit_offset);
15436
15437 if (bit_offset < 0)
15438 add_AT_int (die, DW_AT_bit_offset, bit_offset);
15439 else
15440 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15441 }
15442
15443 /* For a FIELD_DECL node which represents a bit field, output an attribute
15444 which specifies the length in bits of the given field. */
15445
15446 static inline void
15447 add_bit_size_attribute (dw_die_ref die, tree decl)
15448 {
15449 /* Must be a field and a bit field. */
15450 gcc_assert (TREE_CODE (decl) == FIELD_DECL
15451 && DECL_BIT_FIELD_TYPE (decl));
15452
15453 if (host_integerp (DECL_SIZE (decl), 1))
15454 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15455 }
15456
15457 /* If the compiled language is ANSI C, then add a 'prototyped'
15458 attribute, if arg types are given for the parameters of a function. */
15459
15460 static inline void
15461 add_prototyped_attribute (dw_die_ref die, tree func_type)
15462 {
15463 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15464 && prototype_p (func_type))
15465 add_AT_flag (die, DW_AT_prototyped, 1);
15466 }
15467
15468 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
15469 by looking in either the type declaration or object declaration
15470 equate table. */
15471
15472 static inline dw_die_ref
15473 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15474 {
15475 dw_die_ref origin_die = NULL;
15476
15477 if (TREE_CODE (origin) != FUNCTION_DECL)
15478 {
15479 /* We may have gotten separated from the block for the inlined
15480 function, if we're in an exception handler or some such; make
15481 sure that the abstract function has been written out.
15482
15483 Doing this for nested functions is wrong, however; functions are
15484 distinct units, and our context might not even be inline. */
15485 tree fn = origin;
15486
15487 if (TYPE_P (fn))
15488 fn = TYPE_STUB_DECL (fn);
15489
15490 fn = decl_function_context (fn);
15491 if (fn)
15492 dwarf2out_abstract_function (fn);
15493 }
15494
15495 if (DECL_P (origin))
15496 origin_die = lookup_decl_die (origin);
15497 else if (TYPE_P (origin))
15498 origin_die = lookup_type_die (origin);
15499
15500 /* XXX: Functions that are never lowered don't always have correct block
15501 trees (in the case of java, they simply have no block tree, in some other
15502 languages). For these functions, there is nothing we can really do to
15503 output correct debug info for inlined functions in all cases. Rather
15504 than die, we'll just produce deficient debug info now, in that we will
15505 have variables without a proper abstract origin. In the future, when all
15506 functions are lowered, we should re-add a gcc_assert (origin_die)
15507 here. */
15508
15509 if (origin_die)
15510 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15511 return origin_die;
15512 }
15513
15514 /* We do not currently support the pure_virtual attribute. */
15515
15516 static inline void
15517 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15518 {
15519 if (DECL_VINDEX (func_decl))
15520 {
15521 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15522
15523 if (host_integerp (DECL_VINDEX (func_decl), 0))
15524 add_AT_loc (die, DW_AT_vtable_elem_location,
15525 new_loc_descr (DW_OP_constu,
15526 tree_low_cst (DECL_VINDEX (func_decl), 0),
15527 0));
15528
15529 /* GNU extension: Record what type this method came from originally. */
15530 if (debug_info_level > DINFO_LEVEL_TERSE
15531 && DECL_CONTEXT (func_decl))
15532 add_AT_die_ref (die, DW_AT_containing_type,
15533 lookup_type_die (DECL_CONTEXT (func_decl)));
15534 }
15535 }
15536 \f
15537 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15538 given decl. This used to be a vendor extension until after DWARF 4
15539 standardized it. */
15540
15541 static void
15542 add_linkage_attr (dw_die_ref die, tree decl)
15543 {
15544 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15545
15546 /* Mimic what assemble_name_raw does with a leading '*'. */
15547 if (name[0] == '*')
15548 name = &name[1];
15549
15550 if (dwarf_version >= 4)
15551 add_AT_string (die, DW_AT_linkage_name, name);
15552 else
15553 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15554 }
15555
15556 /* Add source coordinate attributes for the given decl. */
15557
15558 static void
15559 add_src_coords_attributes (dw_die_ref die, tree decl)
15560 {
15561 expanded_location s;
15562
15563 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
15564 return;
15565 s = expand_location (DECL_SOURCE_LOCATION (decl));
15566 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15567 add_AT_unsigned (die, DW_AT_decl_line, s.line);
15568 }
15569
15570 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
15571
15572 static void
15573 add_linkage_name (dw_die_ref die, tree decl)
15574 {
15575 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15576 && TREE_PUBLIC (decl)
15577 && !DECL_ABSTRACT (decl)
15578 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15579 && die->die_tag != DW_TAG_member)
15580 {
15581 /* Defer until we have an assembler name set. */
15582 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15583 {
15584 limbo_die_node *asm_name;
15585
15586 asm_name = ggc_alloc_cleared_limbo_die_node ();
15587 asm_name->die = die;
15588 asm_name->created_for = decl;
15589 asm_name->next = deferred_asm_name;
15590 deferred_asm_name = asm_name;
15591 }
15592 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15593 add_linkage_attr (die, decl);
15594 }
15595 }
15596
15597 /* Add a DW_AT_name attribute and source coordinate attribute for the
15598 given decl, but only if it actually has a name. */
15599
15600 static void
15601 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
15602 {
15603 tree decl_name;
15604
15605 decl_name = DECL_NAME (decl);
15606 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
15607 {
15608 const char *name = dwarf2_name (decl, 0);
15609 if (name)
15610 add_name_attribute (die, name);
15611 if (! DECL_ARTIFICIAL (decl))
15612 add_src_coords_attributes (die, decl);
15613
15614 add_linkage_name (die, decl);
15615 }
15616
15617 #ifdef VMS_DEBUGGING_INFO
15618 /* Get the function's name, as described by its RTL. This may be different
15619 from the DECL_NAME name used in the source file. */
15620 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
15621 {
15622 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
15623 XEXP (DECL_RTL (decl), 0));
15624 VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
15625 }
15626 #endif /* VMS_DEBUGGING_INFO */
15627 }
15628
15629 #ifdef VMS_DEBUGGING_INFO
15630 /* Output the debug main pointer die for VMS */
15631
15632 void
15633 dwarf2out_vms_debug_main_pointer (void)
15634 {
15635 char label[MAX_ARTIFICIAL_LABEL_BYTES];
15636 dw_die_ref die;
15637
15638 /* Allocate the VMS debug main subprogram die. */
15639 die = ggc_alloc_cleared_die_node ();
15640 die->die_tag = DW_TAG_subprogram;
15641 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
15642 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
15643 current_function_funcdef_no);
15644 add_AT_lbl_id (die, DW_AT_entry_pc, label);
15645
15646 /* Make it the first child of comp_unit_die (). */
15647 die->die_parent = comp_unit_die ();
15648 if (comp_unit_die ()->die_child)
15649 {
15650 die->die_sib = comp_unit_die ()->die_child->die_sib;
15651 comp_unit_die ()->die_child->die_sib = die;
15652 }
15653 else
15654 {
15655 die->die_sib = die;
15656 comp_unit_die ()->die_child = die;
15657 }
15658 }
15659 #endif /* VMS_DEBUGGING_INFO */
15660
15661 /* Push a new declaration scope. */
15662
15663 static void
15664 push_decl_scope (tree scope)
15665 {
15666 VEC_safe_push (tree, gc, decl_scope_table, scope);
15667 }
15668
15669 /* Pop a declaration scope. */
15670
15671 static inline void
15672 pop_decl_scope (void)
15673 {
15674 VEC_pop (tree, decl_scope_table);
15675 }
15676
15677 /* walk_tree helper function for uses_local_type, below. */
15678
15679 static tree
15680 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
15681 {
15682 if (!TYPE_P (*tp))
15683 *walk_subtrees = 0;
15684 else
15685 {
15686 tree name = TYPE_NAME (*tp);
15687 if (name && DECL_P (name) && decl_function_context (name))
15688 return *tp;
15689 }
15690 return NULL_TREE;
15691 }
15692
15693 /* If TYPE involves a function-local type (including a local typedef to a
15694 non-local type), returns that type; otherwise returns NULL_TREE. */
15695
15696 static tree
15697 uses_local_type (tree type)
15698 {
15699 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
15700 return used;
15701 }
15702
15703 /* Return the DIE for the scope that immediately contains this type.
15704 Non-named types that do not involve a function-local type get global
15705 scope. Named types nested in namespaces or other types get their
15706 containing scope. All other types (i.e. function-local named types) get
15707 the current active scope. */
15708
15709 static dw_die_ref
15710 scope_die_for (tree t, dw_die_ref context_die)
15711 {
15712 dw_die_ref scope_die = NULL;
15713 tree containing_scope;
15714
15715 /* Non-types always go in the current scope. */
15716 gcc_assert (TYPE_P (t));
15717
15718 /* Use the scope of the typedef, rather than the scope of the type
15719 it refers to. */
15720 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
15721 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
15722 else
15723 containing_scope = TYPE_CONTEXT (t);
15724
15725 /* Use the containing namespace if there is one. */
15726 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
15727 {
15728 if (context_die == lookup_decl_die (containing_scope))
15729 /* OK */;
15730 else if (debug_info_level > DINFO_LEVEL_TERSE)
15731 context_die = get_context_die (containing_scope);
15732 else
15733 containing_scope = NULL_TREE;
15734 }
15735
15736 /* Ignore function type "scopes" from the C frontend. They mean that
15737 a tagged type is local to a parmlist of a function declarator, but
15738 that isn't useful to DWARF. */
15739 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
15740 containing_scope = NULL_TREE;
15741
15742 if (SCOPE_FILE_SCOPE_P (containing_scope))
15743 {
15744 /* If T uses a local type keep it local as well, to avoid references
15745 to function-local DIEs from outside the function. */
15746 if (current_function_decl && uses_local_type (t))
15747 scope_die = context_die;
15748 else
15749 scope_die = comp_unit_die ();
15750 }
15751 else if (TYPE_P (containing_scope))
15752 {
15753 /* For types, we can just look up the appropriate DIE. */
15754 if (debug_info_level > DINFO_LEVEL_TERSE)
15755 scope_die = get_context_die (containing_scope);
15756 else
15757 {
15758 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
15759 if (scope_die == NULL)
15760 scope_die = comp_unit_die ();
15761 }
15762 }
15763 else
15764 scope_die = context_die;
15765
15766 return scope_die;
15767 }
15768
15769 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
15770
15771 static inline int
15772 local_scope_p (dw_die_ref context_die)
15773 {
15774 for (; context_die; context_die = context_die->die_parent)
15775 if (context_die->die_tag == DW_TAG_inlined_subroutine
15776 || context_die->die_tag == DW_TAG_subprogram)
15777 return 1;
15778
15779 return 0;
15780 }
15781
15782 /* Returns nonzero if CONTEXT_DIE is a class. */
15783
15784 static inline int
15785 class_scope_p (dw_die_ref context_die)
15786 {
15787 return (context_die
15788 && (context_die->die_tag == DW_TAG_structure_type
15789 || context_die->die_tag == DW_TAG_class_type
15790 || context_die->die_tag == DW_TAG_interface_type
15791 || context_die->die_tag == DW_TAG_union_type));
15792 }
15793
15794 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
15795 whether or not to treat a DIE in this context as a declaration. */
15796
15797 static inline int
15798 class_or_namespace_scope_p (dw_die_ref context_die)
15799 {
15800 return (class_scope_p (context_die)
15801 || (context_die && context_die->die_tag == DW_TAG_namespace));
15802 }
15803
15804 /* Many forms of DIEs require a "type description" attribute. This
15805 routine locates the proper "type descriptor" die for the type given
15806 by 'type', and adds a DW_AT_type attribute below the given die. */
15807
15808 static void
15809 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
15810 int decl_volatile, dw_die_ref context_die)
15811 {
15812 enum tree_code code = TREE_CODE (type);
15813 dw_die_ref type_die = NULL;
15814
15815 /* ??? If this type is an unnamed subrange type of an integral, floating-point
15816 or fixed-point type, use the inner type. This is because we have no
15817 support for unnamed types in base_type_die. This can happen if this is
15818 an Ada subrange type. Correct solution is emit a subrange type die. */
15819 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
15820 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
15821 type = TREE_TYPE (type), code = TREE_CODE (type);
15822
15823 if (code == ERROR_MARK
15824 /* Handle a special case. For functions whose return type is void, we
15825 generate *no* type attribute. (Note that no object may have type
15826 `void', so this only applies to function return types). */
15827 || code == VOID_TYPE)
15828 return;
15829
15830 type_die = modified_type_die (type,
15831 decl_const || TYPE_READONLY (type),
15832 decl_volatile || TYPE_VOLATILE (type),
15833 context_die);
15834
15835 if (type_die != NULL)
15836 add_AT_die_ref (object_die, DW_AT_type, type_die);
15837 }
15838
15839 /* Given an object die, add the calling convention attribute for the
15840 function call type. */
15841 static void
15842 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
15843 {
15844 enum dwarf_calling_convention value = DW_CC_normal;
15845
15846 value = ((enum dwarf_calling_convention)
15847 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
15848
15849 if (is_fortran ()
15850 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
15851 {
15852 /* DWARF 2 doesn't provide a way to identify a program's source-level
15853 entry point. DW_AT_calling_convention attributes are only meant
15854 to describe functions' calling conventions. However, lacking a
15855 better way to signal the Fortran main program, we used this for
15856 a long time, following existing custom. Now, DWARF 4 has
15857 DW_AT_main_subprogram, which we add below, but some tools still
15858 rely on the old way, which we thus keep. */
15859 value = DW_CC_program;
15860
15861 if (dwarf_version >= 4 || !dwarf_strict)
15862 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
15863 }
15864
15865 /* Only add the attribute if the backend requests it, and
15866 is not DW_CC_normal. */
15867 if (value && (value != DW_CC_normal))
15868 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
15869 }
15870
15871 /* Given a tree pointer to a struct, class, union, or enum type node, return
15872 a pointer to the (string) tag name for the given type, or zero if the type
15873 was declared without a tag. */
15874
15875 static const char *
15876 type_tag (const_tree type)
15877 {
15878 const char *name = 0;
15879
15880 if (TYPE_NAME (type) != 0)
15881 {
15882 tree t = 0;
15883
15884 /* Find the IDENTIFIER_NODE for the type name. */
15885 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
15886 && !TYPE_NAMELESS (type))
15887 t = TYPE_NAME (type);
15888
15889 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
15890 a TYPE_DECL node, regardless of whether or not a `typedef' was
15891 involved. */
15892 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15893 && ! DECL_IGNORED_P (TYPE_NAME (type)))
15894 {
15895 /* We want to be extra verbose. Don't call dwarf_name if
15896 DECL_NAME isn't set. The default hook for decl_printable_name
15897 doesn't like that, and in this context it's correct to return
15898 0, instead of "<anonymous>" or the like. */
15899 if (DECL_NAME (TYPE_NAME (type))
15900 && !DECL_NAMELESS (TYPE_NAME (type)))
15901 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
15902 }
15903
15904 /* Now get the name as a string, or invent one. */
15905 if (!name && t != 0)
15906 name = IDENTIFIER_POINTER (t);
15907 }
15908
15909 return (name == 0 || *name == '\0') ? 0 : name;
15910 }
15911
15912 /* Return the type associated with a data member, make a special check
15913 for bit field types. */
15914
15915 static inline tree
15916 member_declared_type (const_tree member)
15917 {
15918 return (DECL_BIT_FIELD_TYPE (member)
15919 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
15920 }
15921
15922 /* Get the decl's label, as described by its RTL. This may be different
15923 from the DECL_NAME name used in the source file. */
15924
15925 #if 0
15926 static const char *
15927 decl_start_label (tree decl)
15928 {
15929 rtx x;
15930 const char *fnname;
15931
15932 x = DECL_RTL (decl);
15933 gcc_assert (MEM_P (x));
15934
15935 x = XEXP (x, 0);
15936 gcc_assert (GET_CODE (x) == SYMBOL_REF);
15937
15938 fnname = XSTR (x, 0);
15939 return fnname;
15940 }
15941 #endif
15942 \f
15943 /* These routines generate the internal representation of the DIE's for
15944 the compilation unit. Debugging information is collected by walking
15945 the declaration trees passed in from dwarf2out_decl(). */
15946
15947 static void
15948 gen_array_type_die (tree type, dw_die_ref context_die)
15949 {
15950 dw_die_ref scope_die = scope_die_for (type, context_die);
15951 dw_die_ref array_die;
15952
15953 /* GNU compilers represent multidimensional array types as sequences of one
15954 dimensional array types whose element types are themselves array types.
15955 We sometimes squish that down to a single array_type DIE with multiple
15956 subscripts in the Dwarf debugging info. The draft Dwarf specification
15957 say that we are allowed to do this kind of compression in C, because
15958 there is no difference between an array of arrays and a multidimensional
15959 array. We don't do this for Ada to remain as close as possible to the
15960 actual representation, which is especially important against the language
15961 flexibilty wrt arrays of variable size. */
15962
15963 bool collapse_nested_arrays = !is_ada ();
15964 tree element_type;
15965
15966 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
15967 DW_TAG_string_type doesn't have DW_AT_type attribute). */
15968 if (TYPE_STRING_FLAG (type)
15969 && TREE_CODE (type) == ARRAY_TYPE
15970 && is_fortran ()
15971 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
15972 {
15973 HOST_WIDE_INT size;
15974
15975 array_die = new_die (DW_TAG_string_type, scope_die, type);
15976 add_name_attribute (array_die, type_tag (type));
15977 equate_type_number_to_die (type, array_die);
15978 size = int_size_in_bytes (type);
15979 if (size >= 0)
15980 add_AT_unsigned (array_die, DW_AT_byte_size, size);
15981 else if (TYPE_DOMAIN (type) != NULL_TREE
15982 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
15983 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
15984 {
15985 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
15986 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
15987
15988 size = int_size_in_bytes (TREE_TYPE (szdecl));
15989 if (loc && size > 0)
15990 {
15991 add_AT_location_description (array_die, DW_AT_string_length, loc);
15992 if (size != DWARF2_ADDR_SIZE)
15993 add_AT_unsigned (array_die, DW_AT_byte_size, size);
15994 }
15995 }
15996 return;
15997 }
15998
15999 array_die = new_die (DW_TAG_array_type, scope_die, type);
16000 add_name_attribute (array_die, type_tag (type));
16001 equate_type_number_to_die (type, array_die);
16002
16003 if (TREE_CODE (type) == VECTOR_TYPE)
16004 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16005
16006 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16007 if (is_fortran ()
16008 && TREE_CODE (type) == ARRAY_TYPE
16009 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16010 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16011 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16012
16013 #if 0
16014 /* We default the array ordering. SDB will probably do
16015 the right things even if DW_AT_ordering is not present. It's not even
16016 an issue until we start to get into multidimensional arrays anyway. If
16017 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16018 then we'll have to put the DW_AT_ordering attribute back in. (But if
16019 and when we find out that we need to put these in, we will only do so
16020 for multidimensional arrays. */
16021 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16022 #endif
16023
16024 if (TREE_CODE (type) == VECTOR_TYPE)
16025 {
16026 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16027 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16028 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16029 add_bound_info (subrange_die, DW_AT_upper_bound,
16030 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16031 }
16032 else
16033 add_subscript_info (array_die, type, collapse_nested_arrays);
16034
16035 /* Add representation of the type of the elements of this array type and
16036 emit the corresponding DIE if we haven't done it already. */
16037 element_type = TREE_TYPE (type);
16038 if (collapse_nested_arrays)
16039 while (TREE_CODE (element_type) == ARRAY_TYPE)
16040 {
16041 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16042 break;
16043 element_type = TREE_TYPE (element_type);
16044 }
16045
16046 add_type_attribute (array_die, element_type, 0, 0, context_die);
16047
16048 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16049 if (TYPE_ARTIFICIAL (type))
16050 add_AT_flag (array_die, DW_AT_artificial, 1);
16051
16052 if (get_AT (array_die, DW_AT_name))
16053 add_pubtype (type, array_die);
16054 }
16055
16056 static dw_loc_descr_ref
16057 descr_info_loc (tree val, tree base_decl)
16058 {
16059 HOST_WIDE_INT size;
16060 dw_loc_descr_ref loc, loc2;
16061 enum dwarf_location_atom op;
16062
16063 if (val == base_decl)
16064 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16065
16066 switch (TREE_CODE (val))
16067 {
16068 CASE_CONVERT:
16069 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16070 case VAR_DECL:
16071 return loc_descriptor_from_tree (val, 0);
16072 case INTEGER_CST:
16073 if (host_integerp (val, 0))
16074 return int_loc_descriptor (tree_low_cst (val, 0));
16075 break;
16076 case INDIRECT_REF:
16077 size = int_size_in_bytes (TREE_TYPE (val));
16078 if (size < 0)
16079 break;
16080 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16081 if (!loc)
16082 break;
16083 if (size == DWARF2_ADDR_SIZE)
16084 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16085 else
16086 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16087 return loc;
16088 case POINTER_PLUS_EXPR:
16089 case PLUS_EXPR:
16090 if (host_integerp (TREE_OPERAND (val, 1), 1)
16091 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16092 < 16384)
16093 {
16094 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16095 if (!loc)
16096 break;
16097 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16098 }
16099 else
16100 {
16101 op = DW_OP_plus;
16102 do_binop:
16103 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16104 if (!loc)
16105 break;
16106 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16107 if (!loc2)
16108 break;
16109 add_loc_descr (&loc, loc2);
16110 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16111 }
16112 return loc;
16113 case MINUS_EXPR:
16114 op = DW_OP_minus;
16115 goto do_binop;
16116 case MULT_EXPR:
16117 op = DW_OP_mul;
16118 goto do_binop;
16119 case EQ_EXPR:
16120 op = DW_OP_eq;
16121 goto do_binop;
16122 case NE_EXPR:
16123 op = DW_OP_ne;
16124 goto do_binop;
16125 default:
16126 break;
16127 }
16128 return NULL;
16129 }
16130
16131 static void
16132 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16133 tree val, tree base_decl)
16134 {
16135 dw_loc_descr_ref loc;
16136
16137 if (host_integerp (val, 0))
16138 {
16139 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16140 return;
16141 }
16142
16143 loc = descr_info_loc (val, base_decl);
16144 if (!loc)
16145 return;
16146
16147 add_AT_loc (die, attr, loc);
16148 }
16149
16150 /* This routine generates DIE for array with hidden descriptor, details
16151 are filled into *info by a langhook. */
16152
16153 static void
16154 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16155 dw_die_ref context_die)
16156 {
16157 dw_die_ref scope_die = scope_die_for (type, context_die);
16158 dw_die_ref array_die;
16159 int dim;
16160
16161 array_die = new_die (DW_TAG_array_type, scope_die, type);
16162 add_name_attribute (array_die, type_tag (type));
16163 equate_type_number_to_die (type, array_die);
16164
16165 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16166 if (is_fortran ()
16167 && info->ndimensions >= 2)
16168 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16169
16170 if (info->data_location)
16171 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16172 info->base_decl);
16173 if (info->associated)
16174 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16175 info->base_decl);
16176 if (info->allocated)
16177 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16178 info->base_decl);
16179
16180 for (dim = 0; dim < info->ndimensions; dim++)
16181 {
16182 dw_die_ref subrange_die
16183 = new_die (DW_TAG_subrange_type, array_die, NULL);
16184
16185 if (info->dimen[dim].lower_bound)
16186 {
16187 /* If it is the default value, omit it. */
16188 int dflt;
16189
16190 if (host_integerp (info->dimen[dim].lower_bound, 0)
16191 && (dflt = lower_bound_default ()) != -1
16192 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16193 ;
16194 else
16195 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16196 info->dimen[dim].lower_bound,
16197 info->base_decl);
16198 }
16199 if (info->dimen[dim].upper_bound)
16200 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16201 info->dimen[dim].upper_bound,
16202 info->base_decl);
16203 if (info->dimen[dim].stride)
16204 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16205 info->dimen[dim].stride,
16206 info->base_decl);
16207 }
16208
16209 gen_type_die (info->element_type, context_die);
16210 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16211
16212 if (get_AT (array_die, DW_AT_name))
16213 add_pubtype (type, array_die);
16214 }
16215
16216 #if 0
16217 static void
16218 gen_entry_point_die (tree decl, dw_die_ref context_die)
16219 {
16220 tree origin = decl_ultimate_origin (decl);
16221 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16222
16223 if (origin != NULL)
16224 add_abstract_origin_attribute (decl_die, origin);
16225 else
16226 {
16227 add_name_and_src_coords_attributes (decl_die, decl);
16228 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16229 0, 0, context_die);
16230 }
16231
16232 if (DECL_ABSTRACT (decl))
16233 equate_decl_number_to_die (decl, decl_die);
16234 else
16235 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16236 }
16237 #endif
16238
16239 /* Walk through the list of incomplete types again, trying once more to
16240 emit full debugging info for them. */
16241
16242 static void
16243 retry_incomplete_types (void)
16244 {
16245 int i;
16246
16247 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16248 if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16249 DINFO_USAGE_DIR_USE))
16250 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16251 }
16252
16253 /* Determine what tag to use for a record type. */
16254
16255 static enum dwarf_tag
16256 record_type_tag (tree type)
16257 {
16258 if (! lang_hooks.types.classify_record)
16259 return DW_TAG_structure_type;
16260
16261 switch (lang_hooks.types.classify_record (type))
16262 {
16263 case RECORD_IS_STRUCT:
16264 return DW_TAG_structure_type;
16265
16266 case RECORD_IS_CLASS:
16267 return DW_TAG_class_type;
16268
16269 case RECORD_IS_INTERFACE:
16270 if (dwarf_version >= 3 || !dwarf_strict)
16271 return DW_TAG_interface_type;
16272 return DW_TAG_structure_type;
16273
16274 default:
16275 gcc_unreachable ();
16276 }
16277 }
16278
16279 /* Generate a DIE to represent an enumeration type. Note that these DIEs
16280 include all of the information about the enumeration values also. Each
16281 enumerated type name/value is listed as a child of the enumerated type
16282 DIE. */
16283
16284 static dw_die_ref
16285 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16286 {
16287 dw_die_ref type_die = lookup_type_die (type);
16288
16289 if (type_die == NULL)
16290 {
16291 type_die = new_die (DW_TAG_enumeration_type,
16292 scope_die_for (type, context_die), type);
16293 equate_type_number_to_die (type, type_die);
16294 add_name_attribute (type_die, type_tag (type));
16295 if (dwarf_version >= 4 || !dwarf_strict)
16296 {
16297 if (ENUM_IS_SCOPED (type))
16298 add_AT_flag (type_die, DW_AT_enum_class, 1);
16299 if (ENUM_IS_OPAQUE (type))
16300 add_AT_flag (type_die, DW_AT_declaration, 1);
16301 }
16302 }
16303 else if (! TYPE_SIZE (type))
16304 return type_die;
16305 else
16306 remove_AT (type_die, DW_AT_declaration);
16307
16308 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
16309 given enum type is incomplete, do not generate the DW_AT_byte_size
16310 attribute or the DW_AT_element_list attribute. */
16311 if (TYPE_SIZE (type))
16312 {
16313 tree link;
16314
16315 TREE_ASM_WRITTEN (type) = 1;
16316 add_byte_size_attribute (type_die, type);
16317 if (TYPE_STUB_DECL (type) != NULL_TREE)
16318 {
16319 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16320 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16321 }
16322
16323 /* If the first reference to this type was as the return type of an
16324 inline function, then it may not have a parent. Fix this now. */
16325 if (type_die->die_parent == NULL)
16326 add_child_die (scope_die_for (type, context_die), type_die);
16327
16328 for (link = TYPE_VALUES (type);
16329 link != NULL; link = TREE_CHAIN (link))
16330 {
16331 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16332 tree value = TREE_VALUE (link);
16333
16334 add_name_attribute (enum_die,
16335 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16336
16337 if (TREE_CODE (value) == CONST_DECL)
16338 value = DECL_INITIAL (value);
16339
16340 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16341 /* DWARF2 does not provide a way of indicating whether or
16342 not enumeration constants are signed or unsigned. GDB
16343 always assumes the values are signed, so we output all
16344 values as if they were signed. That means that
16345 enumeration constants with very large unsigned values
16346 will appear to have negative values in the debugger. */
16347 add_AT_int (enum_die, DW_AT_const_value,
16348 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16349 }
16350
16351 add_gnat_descriptive_type_attribute (type_die, type, context_die);
16352 if (TYPE_ARTIFICIAL (type))
16353 add_AT_flag (type_die, DW_AT_artificial, 1);
16354 }
16355 else
16356 add_AT_flag (type_die, DW_AT_declaration, 1);
16357
16358 add_pubtype (type, type_die);
16359
16360 return type_die;
16361 }
16362
16363 /* Generate a DIE to represent either a real live formal parameter decl or to
16364 represent just the type of some formal parameter position in some function
16365 type.
16366
16367 Note that this routine is a bit unusual because its argument may be a
16368 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16369 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16370 node. If it's the former then this function is being called to output a
16371 DIE to represent a formal parameter object (or some inlining thereof). If
16372 it's the latter, then this function is only being called to output a
16373 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16374 argument type of some subprogram type.
16375 If EMIT_NAME_P is true, name and source coordinate attributes
16376 are emitted. */
16377
16378 static dw_die_ref
16379 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16380 dw_die_ref context_die)
16381 {
16382 tree node_or_origin = node ? node : origin;
16383 tree ultimate_origin;
16384 dw_die_ref parm_die
16385 = new_die (DW_TAG_formal_parameter, context_die, node);
16386
16387 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16388 {
16389 case tcc_declaration:
16390 ultimate_origin = decl_ultimate_origin (node_or_origin);
16391 if (node || ultimate_origin)
16392 origin = ultimate_origin;
16393 if (origin != NULL)
16394 add_abstract_origin_attribute (parm_die, origin);
16395 else if (emit_name_p)
16396 add_name_and_src_coords_attributes (parm_die, node);
16397 if (origin == NULL
16398 || (! DECL_ABSTRACT (node_or_origin)
16399 && variably_modified_type_p (TREE_TYPE (node_or_origin),
16400 decl_function_context
16401 (node_or_origin))))
16402 {
16403 tree type = TREE_TYPE (node_or_origin);
16404 if (decl_by_reference_p (node_or_origin))
16405 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16406 context_die);
16407 else
16408 add_type_attribute (parm_die, type,
16409 TREE_READONLY (node_or_origin),
16410 TREE_THIS_VOLATILE (node_or_origin),
16411 context_die);
16412 }
16413 if (origin == NULL && DECL_ARTIFICIAL (node))
16414 add_AT_flag (parm_die, DW_AT_artificial, 1);
16415
16416 if (node && node != origin)
16417 equate_decl_number_to_die (node, parm_die);
16418 if (! DECL_ABSTRACT (node_or_origin))
16419 add_location_or_const_value_attribute (parm_die, node_or_origin,
16420 node == NULL, DW_AT_location);
16421
16422 break;
16423
16424 case tcc_type:
16425 /* We were called with some kind of a ..._TYPE node. */
16426 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16427 break;
16428
16429 default:
16430 gcc_unreachable ();
16431 }
16432
16433 return parm_die;
16434 }
16435
16436 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16437 children DW_TAG_formal_parameter DIEs representing the arguments of the
16438 parameter pack.
16439
16440 PARM_PACK must be a function parameter pack.
16441 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16442 must point to the subsequent arguments of the function PACK_ARG belongs to.
16443 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16444 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16445 following the last one for which a DIE was generated. */
16446
16447 static dw_die_ref
16448 gen_formal_parameter_pack_die (tree parm_pack,
16449 tree pack_arg,
16450 dw_die_ref subr_die,
16451 tree *next_arg)
16452 {
16453 tree arg;
16454 dw_die_ref parm_pack_die;
16455
16456 gcc_assert (parm_pack
16457 && lang_hooks.function_parameter_pack_p (parm_pack)
16458 && subr_die);
16459
16460 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16461 add_src_coords_attributes (parm_pack_die, parm_pack);
16462
16463 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16464 {
16465 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16466 parm_pack))
16467 break;
16468 gen_formal_parameter_die (arg, NULL,
16469 false /* Don't emit name attribute. */,
16470 parm_pack_die);
16471 }
16472 if (next_arg)
16473 *next_arg = arg;
16474 return parm_pack_die;
16475 }
16476
16477 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16478 at the end of an (ANSI prototyped) formal parameters list. */
16479
16480 static void
16481 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16482 {
16483 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16484 }
16485
16486 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16487 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16488 parameters as specified in some function type specification (except for
16489 those which appear as part of a function *definition*). */
16490
16491 static void
16492 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16493 {
16494 tree link;
16495 tree formal_type = NULL;
16496 tree first_parm_type;
16497 tree arg;
16498
16499 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16500 {
16501 arg = DECL_ARGUMENTS (function_or_method_type);
16502 function_or_method_type = TREE_TYPE (function_or_method_type);
16503 }
16504 else
16505 arg = NULL_TREE;
16506
16507 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16508
16509 /* Make our first pass over the list of formal parameter types and output a
16510 DW_TAG_formal_parameter DIE for each one. */
16511 for (link = first_parm_type; link; )
16512 {
16513 dw_die_ref parm_die;
16514
16515 formal_type = TREE_VALUE (link);
16516 if (formal_type == void_type_node)
16517 break;
16518
16519 /* Output a (nameless) DIE to represent the formal parameter itself. */
16520 parm_die = gen_formal_parameter_die (formal_type, NULL,
16521 true /* Emit name attribute. */,
16522 context_die);
16523 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16524 && link == first_parm_type)
16525 {
16526 add_AT_flag (parm_die, DW_AT_artificial, 1);
16527 if (dwarf_version >= 3 || !dwarf_strict)
16528 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16529 }
16530 else if (arg && DECL_ARTIFICIAL (arg))
16531 add_AT_flag (parm_die, DW_AT_artificial, 1);
16532
16533 link = TREE_CHAIN (link);
16534 if (arg)
16535 arg = DECL_CHAIN (arg);
16536 }
16537
16538 /* If this function type has an ellipsis, add a
16539 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
16540 if (formal_type != void_type_node)
16541 gen_unspecified_parameters_die (function_or_method_type, context_die);
16542
16543 /* Make our second (and final) pass over the list of formal parameter types
16544 and output DIEs to represent those types (as necessary). */
16545 for (link = TYPE_ARG_TYPES (function_or_method_type);
16546 link && TREE_VALUE (link);
16547 link = TREE_CHAIN (link))
16548 gen_type_die (TREE_VALUE (link), context_die);
16549 }
16550
16551 /* We want to generate the DIE for TYPE so that we can generate the
16552 die for MEMBER, which has been defined; we will need to refer back
16553 to the member declaration nested within TYPE. If we're trying to
16554 generate minimal debug info for TYPE, processing TYPE won't do the
16555 trick; we need to attach the member declaration by hand. */
16556
16557 static void
16558 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16559 {
16560 gen_type_die (type, context_die);
16561
16562 /* If we're trying to avoid duplicate debug info, we may not have
16563 emitted the member decl for this function. Emit it now. */
16564 if (TYPE_STUB_DECL (type)
16565 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16566 && ! lookup_decl_die (member))
16567 {
16568 dw_die_ref type_die;
16569 gcc_assert (!decl_ultimate_origin (member));
16570
16571 push_decl_scope (type);
16572 type_die = lookup_type_die_strip_naming_typedef (type);
16573 if (TREE_CODE (member) == FUNCTION_DECL)
16574 gen_subprogram_die (member, type_die);
16575 else if (TREE_CODE (member) == FIELD_DECL)
16576 {
16577 /* Ignore the nameless fields that are used to skip bits but handle
16578 C++ anonymous unions and structs. */
16579 if (DECL_NAME (member) != NULL_TREE
16580 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16581 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16582 {
16583 gen_type_die (member_declared_type (member), type_die);
16584 gen_field_die (member, type_die);
16585 }
16586 }
16587 else
16588 gen_variable_die (member, NULL_TREE, type_die);
16589
16590 pop_decl_scope ();
16591 }
16592 }
16593 \f
16594 /* Forward declare these functions, because they are mutually recursive
16595 with their set_block_* pairing functions. */
16596 static void set_decl_origin_self (tree);
16597 static void set_decl_abstract_flags (tree, int);
16598
16599 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
16600 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
16601 that it points to the node itself, thus indicating that the node is its
16602 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
16603 the given node is NULL, recursively descend the decl/block tree which
16604 it is the root of, and for each other ..._DECL or BLOCK node contained
16605 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
16606 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
16607 values to point to themselves. */
16608
16609 static void
16610 set_block_origin_self (tree stmt)
16611 {
16612 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
16613 {
16614 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
16615
16616 {
16617 tree local_decl;
16618
16619 for (local_decl = BLOCK_VARS (stmt);
16620 local_decl != NULL_TREE;
16621 local_decl = DECL_CHAIN (local_decl))
16622 if (! DECL_EXTERNAL (local_decl))
16623 set_decl_origin_self (local_decl); /* Potential recursion. */
16624 }
16625
16626 {
16627 tree subblock;
16628
16629 for (subblock = BLOCK_SUBBLOCKS (stmt);
16630 subblock != NULL_TREE;
16631 subblock = BLOCK_CHAIN (subblock))
16632 set_block_origin_self (subblock); /* Recurse. */
16633 }
16634 }
16635 }
16636
16637 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
16638 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
16639 node to so that it points to the node itself, thus indicating that the
16640 node represents its own (abstract) origin. Additionally, if the
16641 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
16642 the decl/block tree of which the given node is the root of, and for
16643 each other ..._DECL or BLOCK node contained therein whose
16644 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
16645 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
16646 point to themselves. */
16647
16648 static void
16649 set_decl_origin_self (tree decl)
16650 {
16651 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
16652 {
16653 DECL_ABSTRACT_ORIGIN (decl) = decl;
16654 if (TREE_CODE (decl) == FUNCTION_DECL)
16655 {
16656 tree arg;
16657
16658 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
16659 DECL_ABSTRACT_ORIGIN (arg) = arg;
16660 if (DECL_INITIAL (decl) != NULL_TREE
16661 && DECL_INITIAL (decl) != error_mark_node)
16662 set_block_origin_self (DECL_INITIAL (decl));
16663 }
16664 }
16665 }
16666 \f
16667 /* Given a pointer to some BLOCK node, and a boolean value to set the
16668 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
16669 the given block, and for all local decls and all local sub-blocks
16670 (recursively) which are contained therein. */
16671
16672 static void
16673 set_block_abstract_flags (tree stmt, int setting)
16674 {
16675 tree local_decl;
16676 tree subblock;
16677 unsigned int i;
16678
16679 BLOCK_ABSTRACT (stmt) = setting;
16680
16681 for (local_decl = BLOCK_VARS (stmt);
16682 local_decl != NULL_TREE;
16683 local_decl = DECL_CHAIN (local_decl))
16684 if (! DECL_EXTERNAL (local_decl))
16685 set_decl_abstract_flags (local_decl, setting);
16686
16687 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
16688 {
16689 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
16690 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
16691 || TREE_CODE (local_decl) == PARM_DECL)
16692 set_decl_abstract_flags (local_decl, setting);
16693 }
16694
16695 for (subblock = BLOCK_SUBBLOCKS (stmt);
16696 subblock != NULL_TREE;
16697 subblock = BLOCK_CHAIN (subblock))
16698 set_block_abstract_flags (subblock, setting);
16699 }
16700
16701 /* Given a pointer to some ..._DECL node, and a boolean value to set the
16702 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
16703 given decl, and (in the case where the decl is a FUNCTION_DECL) also
16704 set the abstract flags for all of the parameters, local vars, local
16705 blocks and sub-blocks (recursively) to the same setting. */
16706
16707 static void
16708 set_decl_abstract_flags (tree decl, int setting)
16709 {
16710 DECL_ABSTRACT (decl) = setting;
16711 if (TREE_CODE (decl) == FUNCTION_DECL)
16712 {
16713 tree arg;
16714
16715 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
16716 DECL_ABSTRACT (arg) = setting;
16717 if (DECL_INITIAL (decl) != NULL_TREE
16718 && DECL_INITIAL (decl) != error_mark_node)
16719 set_block_abstract_flags (DECL_INITIAL (decl), setting);
16720 }
16721 }
16722
16723 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16724 may later generate inlined and/or out-of-line instances of. */
16725
16726 static void
16727 dwarf2out_abstract_function (tree decl)
16728 {
16729 dw_die_ref old_die;
16730 tree save_fn;
16731 tree context;
16732 int was_abstract;
16733 htab_t old_decl_loc_table;
16734 htab_t old_cached_dw_loc_list_table;
16735 int old_call_site_count, old_tail_call_site_count;
16736 struct call_arg_loc_node *old_call_arg_locations;
16737
16738 /* Make sure we have the actual abstract inline, not a clone. */
16739 decl = DECL_ORIGIN (decl);
16740
16741 old_die = lookup_decl_die (decl);
16742 if (old_die && get_AT (old_die, DW_AT_inline))
16743 /* We've already generated the abstract instance. */
16744 return;
16745
16746 /* We can be called while recursively when seeing block defining inlined subroutine
16747 DIE. Be sure to not clobber the outer location table nor use it or we would
16748 get locations in abstract instantces. */
16749 old_decl_loc_table = decl_loc_table;
16750 decl_loc_table = NULL;
16751 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
16752 cached_dw_loc_list_table = NULL;
16753 old_call_arg_locations = call_arg_locations;
16754 call_arg_locations = NULL;
16755 old_call_site_count = call_site_count;
16756 call_site_count = -1;
16757 old_tail_call_site_count = tail_call_site_count;
16758 tail_call_site_count = -1;
16759
16760 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
16761 we don't get confused by DECL_ABSTRACT. */
16762 if (debug_info_level > DINFO_LEVEL_TERSE)
16763 {
16764 context = decl_class_context (decl);
16765 if (context)
16766 gen_type_die_for_member
16767 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
16768 }
16769
16770 /* Pretend we've just finished compiling this function. */
16771 save_fn = current_function_decl;
16772 current_function_decl = decl;
16773
16774 was_abstract = DECL_ABSTRACT (decl);
16775 set_decl_abstract_flags (decl, 1);
16776 dwarf2out_decl (decl);
16777 if (! was_abstract)
16778 set_decl_abstract_flags (decl, 0);
16779
16780 current_function_decl = save_fn;
16781 decl_loc_table = old_decl_loc_table;
16782 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
16783 call_arg_locations = old_call_arg_locations;
16784 call_site_count = old_call_site_count;
16785 tail_call_site_count = old_tail_call_site_count;
16786 }
16787
16788 /* Helper function of premark_used_types() which gets called through
16789 htab_traverse.
16790
16791 Marks the DIE of a given type in *SLOT as perennial, so it never gets
16792 marked as unused by prune_unused_types. */
16793
16794 static int
16795 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
16796 {
16797 tree type;
16798 dw_die_ref die;
16799
16800 type = (tree) *slot;
16801 die = lookup_type_die (type);
16802 if (die != NULL)
16803 die->die_perennial_p = 1;
16804 return 1;
16805 }
16806
16807 /* Helper function of premark_types_used_by_global_vars which gets called
16808 through htab_traverse.
16809
16810 Marks the DIE of a given type in *SLOT as perennial, so it never gets
16811 marked as unused by prune_unused_types. The DIE of the type is marked
16812 only if the global variable using the type will actually be emitted. */
16813
16814 static int
16815 premark_types_used_by_global_vars_helper (void **slot,
16816 void *data ATTRIBUTE_UNUSED)
16817 {
16818 struct types_used_by_vars_entry *entry;
16819 dw_die_ref die;
16820
16821 entry = (struct types_used_by_vars_entry *) *slot;
16822 gcc_assert (entry->type != NULL
16823 && entry->var_decl != NULL);
16824 die = lookup_type_die (entry->type);
16825 if (die)
16826 {
16827 /* Ask cgraph if the global variable really is to be emitted.
16828 If yes, then we'll keep the DIE of ENTRY->TYPE. */
16829 struct varpool_node *node = varpool_get_node (entry->var_decl);
16830 if (node && node->analyzed)
16831 {
16832 die->die_perennial_p = 1;
16833 /* Keep the parent DIEs as well. */
16834 while ((die = die->die_parent) && die->die_perennial_p == 0)
16835 die->die_perennial_p = 1;
16836 }
16837 }
16838 return 1;
16839 }
16840
16841 /* Mark all members of used_types_hash as perennial. */
16842
16843 static void
16844 premark_used_types (struct function *fun)
16845 {
16846 if (fun && fun->used_types_hash)
16847 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
16848 }
16849
16850 /* Mark all members of types_used_by_vars_entry as perennial. */
16851
16852 static void
16853 premark_types_used_by_global_vars (void)
16854 {
16855 if (types_used_by_vars_hash)
16856 htab_traverse (types_used_by_vars_hash,
16857 premark_types_used_by_global_vars_helper, NULL);
16858 }
16859
16860 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
16861 for CA_LOC call arg loc node. */
16862
16863 static dw_die_ref
16864 gen_call_site_die (tree decl, dw_die_ref subr_die,
16865 struct call_arg_loc_node *ca_loc)
16866 {
16867 dw_die_ref stmt_die = NULL, die;
16868 tree block = ca_loc->block;
16869
16870 while (block
16871 && block != DECL_INITIAL (decl)
16872 && TREE_CODE (block) == BLOCK)
16873 {
16874 if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
16875 stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
16876 if (stmt_die)
16877 break;
16878 block = BLOCK_SUPERCONTEXT (block);
16879 }
16880 if (stmt_die == NULL)
16881 stmt_die = subr_die;
16882 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
16883 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
16884 if (ca_loc->tail_call_p)
16885 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
16886 if (ca_loc->symbol_ref)
16887 {
16888 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
16889 if (tdie)
16890 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
16891 else
16892 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
16893 }
16894 return die;
16895 }
16896
16897 /* Generate a DIE to represent a declared function (either file-scope or
16898 block-local). */
16899
16900 static void
16901 gen_subprogram_die (tree decl, dw_die_ref context_die)
16902 {
16903 tree origin = decl_ultimate_origin (decl);
16904 dw_die_ref subr_die;
16905 tree outer_scope;
16906 dw_die_ref old_die = lookup_decl_die (decl);
16907 int declaration = (current_function_decl != decl
16908 || class_or_namespace_scope_p (context_die));
16909
16910 premark_used_types (DECL_STRUCT_FUNCTION (decl));
16911
16912 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
16913 started to generate the abstract instance of an inline, decided to output
16914 its containing class, and proceeded to emit the declaration of the inline
16915 from the member list for the class. If so, DECLARATION takes priority;
16916 we'll get back to the abstract instance when done with the class. */
16917
16918 /* The class-scope declaration DIE must be the primary DIE. */
16919 if (origin && declaration && class_or_namespace_scope_p (context_die))
16920 {
16921 origin = NULL;
16922 gcc_assert (!old_die);
16923 }
16924
16925 /* Now that the C++ front end lazily declares artificial member fns, we
16926 might need to retrofit the declaration into its class. */
16927 if (!declaration && !origin && !old_die
16928 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
16929 && !class_or_namespace_scope_p (context_die)
16930 && debug_info_level > DINFO_LEVEL_TERSE)
16931 old_die = force_decl_die (decl);
16932
16933 if (origin != NULL)
16934 {
16935 gcc_assert (!declaration || local_scope_p (context_die));
16936
16937 /* Fixup die_parent for the abstract instance of a nested
16938 inline function. */
16939 if (old_die && old_die->die_parent == NULL)
16940 add_child_die (context_die, old_die);
16941
16942 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16943 add_abstract_origin_attribute (subr_die, origin);
16944 /* This is where the actual code for a cloned function is.
16945 Let's emit linkage name attribute for it. This helps
16946 debuggers to e.g, set breakpoints into
16947 constructors/destructors when the user asks "break
16948 K::K". */
16949 add_linkage_name (subr_die, decl);
16950 }
16951 else if (old_die)
16952 {
16953 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
16954 struct dwarf_file_data * file_index = lookup_filename (s.file);
16955
16956 if (!get_AT_flag (old_die, DW_AT_declaration)
16957 /* We can have a normal definition following an inline one in the
16958 case of redefinition of GNU C extern inlines.
16959 It seems reasonable to use AT_specification in this case. */
16960 && !get_AT (old_die, DW_AT_inline))
16961 {
16962 /* Detect and ignore this case, where we are trying to output
16963 something we have already output. */
16964 return;
16965 }
16966
16967 /* If the definition comes from the same place as the declaration,
16968 maybe use the old DIE. We always want the DIE for this function
16969 that has the *_pc attributes to be under comp_unit_die so the
16970 debugger can find it. We also need to do this for abstract
16971 instances of inlines, since the spec requires the out-of-line copy
16972 to have the same parent. For local class methods, this doesn't
16973 apply; we just use the old DIE. */
16974 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
16975 && (DECL_ARTIFICIAL (decl)
16976 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
16977 && (get_AT_unsigned (old_die, DW_AT_decl_line)
16978 == (unsigned) s.line))))
16979 {
16980 subr_die = old_die;
16981
16982 /* Clear out the declaration attribute and the formal parameters.
16983 Do not remove all children, because it is possible that this
16984 declaration die was forced using force_decl_die(). In such
16985 cases die that forced declaration die (e.g. TAG_imported_module)
16986 is one of the children that we do not want to remove. */
16987 remove_AT (subr_die, DW_AT_declaration);
16988 remove_AT (subr_die, DW_AT_object_pointer);
16989 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
16990 }
16991 else
16992 {
16993 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16994 add_AT_specification (subr_die, old_die);
16995 add_pubname (decl, subr_die);
16996 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
16997 add_AT_file (subr_die, DW_AT_decl_file, file_index);
16998 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
16999 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17000 }
17001 }
17002 else
17003 {
17004 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17005
17006 if (TREE_PUBLIC (decl))
17007 add_AT_flag (subr_die, DW_AT_external, 1);
17008
17009 add_name_and_src_coords_attributes (subr_die, decl);
17010 add_pubname (decl, subr_die);
17011 if (debug_info_level > DINFO_LEVEL_TERSE)
17012 {
17013 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17014 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17015 0, 0, context_die);
17016 }
17017
17018 add_pure_or_virtual_attribute (subr_die, decl);
17019 if (DECL_ARTIFICIAL (decl))
17020 add_AT_flag (subr_die, DW_AT_artificial, 1);
17021
17022 add_accessibility_attribute (subr_die, decl);
17023 }
17024
17025 if (declaration)
17026 {
17027 if (!old_die || !get_AT (old_die, DW_AT_inline))
17028 {
17029 add_AT_flag (subr_die, DW_AT_declaration, 1);
17030
17031 /* If this is an explicit function declaration then generate
17032 a DW_AT_explicit attribute. */
17033 if (lang_hooks.decls.function_decl_explicit_p (decl)
17034 && (dwarf_version >= 3 || !dwarf_strict))
17035 add_AT_flag (subr_die, DW_AT_explicit, 1);
17036
17037 /* The first time we see a member function, it is in the context of
17038 the class to which it belongs. We make sure of this by emitting
17039 the class first. The next time is the definition, which is
17040 handled above. The two may come from the same source text.
17041
17042 Note that force_decl_die() forces function declaration die. It is
17043 later reused to represent definition. */
17044 equate_decl_number_to_die (decl, subr_die);
17045 }
17046 }
17047 else if (DECL_ABSTRACT (decl))
17048 {
17049 if (DECL_DECLARED_INLINE_P (decl))
17050 {
17051 if (cgraph_function_possibly_inlined_p (decl))
17052 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17053 else
17054 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17055 }
17056 else
17057 {
17058 if (cgraph_function_possibly_inlined_p (decl))
17059 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17060 else
17061 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17062 }
17063
17064 if (DECL_DECLARED_INLINE_P (decl)
17065 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17066 add_AT_flag (subr_die, DW_AT_artificial, 1);
17067
17068 equate_decl_number_to_die (decl, subr_die);
17069 }
17070 else if (!DECL_EXTERNAL (decl))
17071 {
17072 HOST_WIDE_INT cfa_fb_offset;
17073 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17074
17075 if (!old_die || !get_AT (old_die, DW_AT_inline))
17076 equate_decl_number_to_die (decl, subr_die);
17077
17078 gcc_checking_assert (fun);
17079 if (!flag_reorder_blocks_and_partition)
17080 {
17081 dw_fde_ref fde = fun->fde;
17082 if (fde->dw_fde_begin)
17083 {
17084 /* We have already generated the labels. */
17085 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end);
17086 }
17087 else
17088 {
17089 /* Create start/end labels and add the range. */
17090 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17091 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17092 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17093 current_function_funcdef_no);
17094 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17095 current_function_funcdef_no);
17096 add_AT_low_high_pc (subr_die, label_id_low, label_id_high);
17097 }
17098
17099 #if VMS_DEBUGGING_INFO
17100 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17101 Section 2.3 Prologue and Epilogue Attributes:
17102 When a breakpoint is set on entry to a function, it is generally
17103 desirable for execution to be suspended, not on the very first
17104 instruction of the function, but rather at a point after the
17105 function's frame has been set up, after any language defined local
17106 declaration processing has been completed, and before execution of
17107 the first statement of the function begins. Debuggers generally
17108 cannot properly determine where this point is. Similarly for a
17109 breakpoint set on exit from a function. The prologue and epilogue
17110 attributes allow a compiler to communicate the location(s) to use. */
17111
17112 {
17113 if (fde->dw_fde_vms_end_prologue)
17114 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17115 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17116
17117 if (fde->dw_fde_vms_begin_epilogue)
17118 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17119 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17120 }
17121 #endif
17122
17123 }
17124 else
17125 {
17126 /* Generate pubnames entries for the split function code ranges. */
17127 dw_fde_ref fde = fun->fde;
17128
17129 if (fde->dw_fde_second_begin)
17130 {
17131 if (dwarf_version >= 3 || !dwarf_strict)
17132 {
17133 /* We should use ranges for non-contiguous code section
17134 addresses. Use the actual code range for the initial
17135 section, since the HOT/COLD labels might precede an
17136 alignment offset. */
17137 bool range_list_added = false;
17138 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17139 fde->dw_fde_end, &range_list_added);
17140 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17141 fde->dw_fde_second_end,
17142 &range_list_added);
17143 if (range_list_added)
17144 add_ranges (NULL);
17145 }
17146 else
17147 {
17148 /* There is no real support in DW2 for this .. so we make
17149 a work-around. First, emit the pub name for the segment
17150 containing the function label. Then make and emit a
17151 simplified subprogram DIE for the second segment with the
17152 name pre-fixed by __hot/cold_sect_of_. We use the same
17153 linkage name for the second die so that gdb will find both
17154 sections when given "b foo". */
17155 const char *name = NULL;
17156 tree decl_name = DECL_NAME (decl);
17157 dw_die_ref seg_die;
17158
17159 /* Do the 'primary' section. */
17160 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17161 fde->dw_fde_end);
17162
17163 /* Build a minimal DIE for the secondary section. */
17164 seg_die = new_die (DW_TAG_subprogram,
17165 subr_die->die_parent, decl);
17166
17167 if (TREE_PUBLIC (decl))
17168 add_AT_flag (seg_die, DW_AT_external, 1);
17169
17170 if (decl_name != NULL
17171 && IDENTIFIER_POINTER (decl_name) != NULL)
17172 {
17173 name = dwarf2_name (decl, 1);
17174 if (! DECL_ARTIFICIAL (decl))
17175 add_src_coords_attributes (seg_die, decl);
17176
17177 add_linkage_name (seg_die, decl);
17178 }
17179 gcc_assert (name != NULL);
17180 add_pure_or_virtual_attribute (seg_die, decl);
17181 if (DECL_ARTIFICIAL (decl))
17182 add_AT_flag (seg_die, DW_AT_artificial, 1);
17183
17184 name = concat ("__second_sect_of_", name, NULL);
17185 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17186 fde->dw_fde_second_end);
17187 add_name_attribute (seg_die, name);
17188 if (want_pubnames ())
17189 add_pubname_string (name, seg_die);
17190 }
17191 }
17192 else
17193 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end);
17194 }
17195
17196 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17197
17198 /* We define the "frame base" as the function's CFA. This is more
17199 convenient for several reasons: (1) It's stable across the prologue
17200 and epilogue, which makes it better than just a frame pointer,
17201 (2) With dwarf3, there exists a one-byte encoding that allows us
17202 to reference the .debug_frame data by proxy, but failing that,
17203 (3) We can at least reuse the code inspection and interpretation
17204 code that determines the CFA position at various points in the
17205 function. */
17206 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17207 {
17208 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17209 add_AT_loc (subr_die, DW_AT_frame_base, op);
17210 }
17211 else
17212 {
17213 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17214 if (list->dw_loc_next)
17215 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17216 else
17217 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17218 }
17219
17220 /* Compute a displacement from the "steady-state frame pointer" to
17221 the CFA. The former is what all stack slots and argument slots
17222 will reference in the rtl; the later is what we've told the
17223 debugger about. We'll need to adjust all frame_base references
17224 by this displacement. */
17225 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17226
17227 if (fun->static_chain_decl)
17228 add_AT_location_description (subr_die, DW_AT_static_link,
17229 loc_list_from_tree (fun->static_chain_decl, 2));
17230 }
17231
17232 /* Generate child dies for template paramaters. */
17233 if (debug_info_level > DINFO_LEVEL_TERSE)
17234 gen_generic_params_dies (decl);
17235
17236 /* Now output descriptions of the arguments for this function. This gets
17237 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17238 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17239 `...' at the end of the formal parameter list. In order to find out if
17240 there was a trailing ellipsis or not, we must instead look at the type
17241 associated with the FUNCTION_DECL. This will be a node of type
17242 FUNCTION_TYPE. If the chain of type nodes hanging off of this
17243 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17244 an ellipsis at the end. */
17245
17246 /* In the case where we are describing a mere function declaration, all we
17247 need to do here (and all we *can* do here) is to describe the *types* of
17248 its formal parameters. */
17249 if (debug_info_level <= DINFO_LEVEL_TERSE)
17250 ;
17251 else if (declaration)
17252 gen_formal_types_die (decl, subr_die);
17253 else
17254 {
17255 /* Generate DIEs to represent all known formal parameters. */
17256 tree parm = DECL_ARGUMENTS (decl);
17257 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17258 tree generic_decl_parm = generic_decl
17259 ? DECL_ARGUMENTS (generic_decl)
17260 : NULL;
17261
17262 /* Now we want to walk the list of parameters of the function and
17263 emit their relevant DIEs.
17264
17265 We consider the case of DECL being an instance of a generic function
17266 as well as it being a normal function.
17267
17268 If DECL is an instance of a generic function we walk the
17269 parameters of the generic function declaration _and_ the parameters of
17270 DECL itself. This is useful because we want to emit specific DIEs for
17271 function parameter packs and those are declared as part of the
17272 generic function declaration. In that particular case,
17273 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17274 That DIE has children DIEs representing the set of arguments
17275 of the pack. Note that the set of pack arguments can be empty.
17276 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17277 children DIE.
17278
17279 Otherwise, we just consider the parameters of DECL. */
17280 while (generic_decl_parm || parm)
17281 {
17282 if (generic_decl_parm
17283 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17284 gen_formal_parameter_pack_die (generic_decl_parm,
17285 parm, subr_die,
17286 &parm);
17287 else if (parm)
17288 {
17289 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17290
17291 if (parm == DECL_ARGUMENTS (decl)
17292 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17293 && parm_die
17294 && (dwarf_version >= 3 || !dwarf_strict))
17295 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17296
17297 parm = DECL_CHAIN (parm);
17298 }
17299
17300 if (generic_decl_parm)
17301 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17302 }
17303
17304 /* Decide whether we need an unspecified_parameters DIE at the end.
17305 There are 2 more cases to do this for: 1) the ansi ... declaration -
17306 this is detectable when the end of the arg list is not a
17307 void_type_node 2) an unprototyped function declaration (not a
17308 definition). This just means that we have no info about the
17309 parameters at all. */
17310 if (prototype_p (TREE_TYPE (decl)))
17311 {
17312 /* This is the prototyped case, check for.... */
17313 if (stdarg_p (TREE_TYPE (decl)))
17314 gen_unspecified_parameters_die (decl, subr_die);
17315 }
17316 else if (DECL_INITIAL (decl) == NULL_TREE)
17317 gen_unspecified_parameters_die (decl, subr_die);
17318 }
17319
17320 /* Output Dwarf info for all of the stuff within the body of the function
17321 (if it has one - it may be just a declaration). */
17322 outer_scope = DECL_INITIAL (decl);
17323
17324 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17325 a function. This BLOCK actually represents the outermost binding contour
17326 for the function, i.e. the contour in which the function's formal
17327 parameters and labels get declared. Curiously, it appears that the front
17328 end doesn't actually put the PARM_DECL nodes for the current function onto
17329 the BLOCK_VARS list for this outer scope, but are strung off of the
17330 DECL_ARGUMENTS list for the function instead.
17331
17332 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17333 the LABEL_DECL nodes for the function however, and we output DWARF info
17334 for those in decls_for_scope. Just within the `outer_scope' there will be
17335 a BLOCK node representing the function's outermost pair of curly braces,
17336 and any blocks used for the base and member initializers of a C++
17337 constructor function. */
17338 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
17339 {
17340 int call_site_note_count = 0;
17341 int tail_call_site_note_count = 0;
17342
17343 /* Emit a DW_TAG_variable DIE for a named return value. */
17344 if (DECL_NAME (DECL_RESULT (decl)))
17345 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17346
17347 current_function_has_inlines = 0;
17348 decls_for_scope (outer_scope, subr_die, 0);
17349
17350 if (call_arg_locations && !dwarf_strict)
17351 {
17352 struct call_arg_loc_node *ca_loc;
17353 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17354 {
17355 dw_die_ref die = NULL;
17356 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17357 rtx arg, next_arg;
17358
17359 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17360 arg; arg = next_arg)
17361 {
17362 dw_loc_descr_ref reg, val;
17363 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17364 dw_die_ref cdie, tdie = NULL;
17365
17366 next_arg = XEXP (arg, 1);
17367 if (REG_P (XEXP (XEXP (arg, 0), 0))
17368 && next_arg
17369 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17370 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17371 && REGNO (XEXP (XEXP (arg, 0), 0))
17372 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17373 next_arg = XEXP (next_arg, 1);
17374 if (mode == VOIDmode)
17375 {
17376 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17377 if (mode == VOIDmode)
17378 mode = GET_MODE (XEXP (arg, 0));
17379 }
17380 if (mode == VOIDmode || mode == BLKmode)
17381 continue;
17382 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17383 {
17384 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17385 tloc = XEXP (XEXP (arg, 0), 1);
17386 continue;
17387 }
17388 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17389 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17390 {
17391 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17392 tlocc = XEXP (XEXP (arg, 0), 1);
17393 continue;
17394 }
17395 reg = NULL;
17396 if (REG_P (XEXP (XEXP (arg, 0), 0)))
17397 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17398 VAR_INIT_STATUS_INITIALIZED);
17399 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17400 {
17401 rtx mem = XEXP (XEXP (arg, 0), 0);
17402 reg = mem_loc_descriptor (XEXP (mem, 0),
17403 get_address_mode (mem),
17404 GET_MODE (mem),
17405 VAR_INIT_STATUS_INITIALIZED);
17406 }
17407 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17408 == DEBUG_PARAMETER_REF)
17409 {
17410 tree tdecl
17411 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17412 tdie = lookup_decl_die (tdecl);
17413 if (tdie == NULL)
17414 continue;
17415 }
17416 else
17417 continue;
17418 if (reg == NULL
17419 && GET_CODE (XEXP (XEXP (arg, 0), 0))
17420 != DEBUG_PARAMETER_REF)
17421 continue;
17422 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17423 VOIDmode,
17424 VAR_INIT_STATUS_INITIALIZED);
17425 if (val == NULL)
17426 continue;
17427 if (die == NULL)
17428 die = gen_call_site_die (decl, subr_die, ca_loc);
17429 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17430 NULL_TREE);
17431 if (reg != NULL)
17432 add_AT_loc (cdie, DW_AT_location, reg);
17433 else if (tdie != NULL)
17434 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17435 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17436 if (next_arg != XEXP (arg, 1))
17437 {
17438 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17439 if (mode == VOIDmode)
17440 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17441 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17442 0), 1),
17443 mode, VOIDmode,
17444 VAR_INIT_STATUS_INITIALIZED);
17445 if (val != NULL)
17446 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17447 }
17448 }
17449 if (die == NULL
17450 && (ca_loc->symbol_ref || tloc))
17451 die = gen_call_site_die (decl, subr_die, ca_loc);
17452 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17453 {
17454 dw_loc_descr_ref tval = NULL;
17455
17456 if (tloc != NULL_RTX)
17457 tval = mem_loc_descriptor (tloc,
17458 GET_MODE (tloc) == VOIDmode
17459 ? Pmode : GET_MODE (tloc),
17460 VOIDmode,
17461 VAR_INIT_STATUS_INITIALIZED);
17462 if (tval)
17463 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17464 else if (tlocc != NULL_RTX)
17465 {
17466 tval = mem_loc_descriptor (tlocc,
17467 GET_MODE (tlocc) == VOIDmode
17468 ? Pmode : GET_MODE (tlocc),
17469 VOIDmode,
17470 VAR_INIT_STATUS_INITIALIZED);
17471 if (tval)
17472 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17473 tval);
17474 }
17475 }
17476 if (die != NULL)
17477 {
17478 call_site_note_count++;
17479 if (ca_loc->tail_call_p)
17480 tail_call_site_note_count++;
17481 }
17482 }
17483 }
17484 call_arg_locations = NULL;
17485 call_arg_loc_last = NULL;
17486 if (tail_call_site_count >= 0
17487 && tail_call_site_count == tail_call_site_note_count
17488 && !dwarf_strict)
17489 {
17490 if (call_site_count >= 0
17491 && call_site_count == call_site_note_count)
17492 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17493 else
17494 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17495 }
17496 call_site_count = -1;
17497 tail_call_site_count = -1;
17498 }
17499 /* Add the calling convention attribute if requested. */
17500 add_calling_convention_attribute (subr_die, decl);
17501
17502 }
17503
17504 /* Returns a hash value for X (which really is a die_struct). */
17505
17506 static hashval_t
17507 common_block_die_table_hash (const void *x)
17508 {
17509 const_dw_die_ref d = (const_dw_die_ref) x;
17510 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17511 }
17512
17513 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17514 as decl_id and die_parent of die_struct Y. */
17515
17516 static int
17517 common_block_die_table_eq (const void *x, const void *y)
17518 {
17519 const_dw_die_ref d = (const_dw_die_ref) x;
17520 const_dw_die_ref e = (const_dw_die_ref) y;
17521 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17522 }
17523
17524 /* Generate a DIE to represent a declared data object.
17525 Either DECL or ORIGIN must be non-null. */
17526
17527 static void
17528 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17529 {
17530 HOST_WIDE_INT off = 0;
17531 tree com_decl;
17532 tree decl_or_origin = decl ? decl : origin;
17533 tree ultimate_origin;
17534 dw_die_ref var_die;
17535 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17536 dw_die_ref origin_die;
17537 bool declaration = (DECL_EXTERNAL (decl_or_origin)
17538 || class_or_namespace_scope_p (context_die));
17539 bool specialization_p = false;
17540
17541 ultimate_origin = decl_ultimate_origin (decl_or_origin);
17542 if (decl || ultimate_origin)
17543 origin = ultimate_origin;
17544 com_decl = fortran_common (decl_or_origin, &off);
17545
17546 /* Symbol in common gets emitted as a child of the common block, in the form
17547 of a data member. */
17548 if (com_decl)
17549 {
17550 dw_die_ref com_die;
17551 dw_loc_list_ref loc;
17552 die_node com_die_arg;
17553
17554 var_die = lookup_decl_die (decl_or_origin);
17555 if (var_die)
17556 {
17557 if (get_AT (var_die, DW_AT_location) == NULL)
17558 {
17559 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17560 if (loc)
17561 {
17562 if (off)
17563 {
17564 /* Optimize the common case. */
17565 if (single_element_loc_list_p (loc)
17566 && loc->expr->dw_loc_opc == DW_OP_addr
17567 && loc->expr->dw_loc_next == NULL
17568 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17569 == SYMBOL_REF)
17570 {
17571 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
17572 loc->expr->dw_loc_oprnd1.v.val_addr
17573 = plus_constant (GET_MODE (x), x , off);
17574 }
17575 else
17576 loc_list_plus_const (loc, off);
17577 }
17578 add_AT_location_description (var_die, DW_AT_location, loc);
17579 remove_AT (var_die, DW_AT_declaration);
17580 }
17581 }
17582 return;
17583 }
17584
17585 if (common_block_die_table == NULL)
17586 common_block_die_table
17587 = htab_create_ggc (10, common_block_die_table_hash,
17588 common_block_die_table_eq, NULL);
17589
17590 com_die_arg.decl_id = DECL_UID (com_decl);
17591 com_die_arg.die_parent = context_die;
17592 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17593 loc = loc_list_from_tree (com_decl, 2);
17594 if (com_die == NULL)
17595 {
17596 const char *cnam
17597 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17598 void **slot;
17599
17600 com_die = new_die (DW_TAG_common_block, context_die, decl);
17601 add_name_and_src_coords_attributes (com_die, com_decl);
17602 if (loc)
17603 {
17604 add_AT_location_description (com_die, DW_AT_location, loc);
17605 /* Avoid sharing the same loc descriptor between
17606 DW_TAG_common_block and DW_TAG_variable. */
17607 loc = loc_list_from_tree (com_decl, 2);
17608 }
17609 else if (DECL_EXTERNAL (decl))
17610 add_AT_flag (com_die, DW_AT_declaration, 1);
17611 if (want_pubnames ())
17612 add_pubname_string (cnam, com_die); /* ??? needed? */
17613 com_die->decl_id = DECL_UID (com_decl);
17614 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17615 *slot = (void *) com_die;
17616 }
17617 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17618 {
17619 add_AT_location_description (com_die, DW_AT_location, loc);
17620 loc = loc_list_from_tree (com_decl, 2);
17621 remove_AT (com_die, DW_AT_declaration);
17622 }
17623 var_die = new_die (DW_TAG_variable, com_die, decl);
17624 add_name_and_src_coords_attributes (var_die, decl);
17625 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17626 TREE_THIS_VOLATILE (decl), context_die);
17627 add_AT_flag (var_die, DW_AT_external, 1);
17628 if (loc)
17629 {
17630 if (off)
17631 {
17632 /* Optimize the common case. */
17633 if (single_element_loc_list_p (loc)
17634 && loc->expr->dw_loc_opc == DW_OP_addr
17635 && loc->expr->dw_loc_next == NULL
17636 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17637 {
17638 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
17639 loc->expr->dw_loc_oprnd1.v.val_addr
17640 = plus_constant (GET_MODE (x), x, off);
17641 }
17642 else
17643 loc_list_plus_const (loc, off);
17644 }
17645 add_AT_location_description (var_die, DW_AT_location, loc);
17646 }
17647 else if (DECL_EXTERNAL (decl))
17648 add_AT_flag (var_die, DW_AT_declaration, 1);
17649 equate_decl_number_to_die (decl, var_die);
17650 return;
17651 }
17652
17653 /* If the compiler emitted a definition for the DECL declaration
17654 and if we already emitted a DIE for it, don't emit a second
17655 DIE for it again. Allow re-declarations of DECLs that are
17656 inside functions, though. */
17657 if (old_die && declaration && !local_scope_p (context_die))
17658 return;
17659
17660 /* For static data members, the declaration in the class is supposed
17661 to have DW_TAG_member tag; the specification should still be
17662 DW_TAG_variable referencing the DW_TAG_member DIE. */
17663 if (declaration && class_scope_p (context_die))
17664 var_die = new_die (DW_TAG_member, context_die, decl);
17665 else
17666 var_die = new_die (DW_TAG_variable, context_die, decl);
17667
17668 origin_die = NULL;
17669 if (origin != NULL)
17670 origin_die = add_abstract_origin_attribute (var_die, origin);
17671
17672 /* Loop unrolling can create multiple blocks that refer to the same
17673 static variable, so we must test for the DW_AT_declaration flag.
17674
17675 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17676 copy decls and set the DECL_ABSTRACT flag on them instead of
17677 sharing them.
17678
17679 ??? Duplicated blocks have been rewritten to use .debug_ranges.
17680
17681 ??? The declare_in_namespace support causes us to get two DIEs for one
17682 variable, both of which are declarations. We want to avoid considering
17683 one to be a specification, so we must test that this DIE is not a
17684 declaration. */
17685 else if (old_die && TREE_STATIC (decl) && ! declaration
17686 && get_AT_flag (old_die, DW_AT_declaration) == 1)
17687 {
17688 /* This is a definition of a C++ class level static. */
17689 add_AT_specification (var_die, old_die);
17690 specialization_p = true;
17691 if (DECL_NAME (decl))
17692 {
17693 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17694 struct dwarf_file_data * file_index = lookup_filename (s.file);
17695
17696 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17697 add_AT_file (var_die, DW_AT_decl_file, file_index);
17698
17699 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17700 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17701
17702 if (old_die->die_tag == DW_TAG_member)
17703 add_linkage_name (var_die, decl);
17704 }
17705 }
17706 else
17707 add_name_and_src_coords_attributes (var_die, decl);
17708
17709 if ((origin == NULL && !specialization_p)
17710 || (origin != NULL
17711 && !DECL_ABSTRACT (decl_or_origin)
17712 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17713 decl_function_context
17714 (decl_or_origin))))
17715 {
17716 tree type = TREE_TYPE (decl_or_origin);
17717
17718 if (decl_by_reference_p (decl_or_origin))
17719 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17720 else
17721 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17722 TREE_THIS_VOLATILE (decl_or_origin), context_die);
17723 }
17724
17725 if (origin == NULL && !specialization_p)
17726 {
17727 if (TREE_PUBLIC (decl))
17728 add_AT_flag (var_die, DW_AT_external, 1);
17729
17730 if (DECL_ARTIFICIAL (decl))
17731 add_AT_flag (var_die, DW_AT_artificial, 1);
17732
17733 add_accessibility_attribute (var_die, decl);
17734 }
17735
17736 if (declaration)
17737 add_AT_flag (var_die, DW_AT_declaration, 1);
17738
17739 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
17740 equate_decl_number_to_die (decl, var_die);
17741
17742 if (! declaration
17743 && (! DECL_ABSTRACT (decl_or_origin)
17744 /* Local static vars are shared between all clones/inlines,
17745 so emit DW_AT_location on the abstract DIE if DECL_RTL is
17746 already set. */
17747 || (TREE_CODE (decl_or_origin) == VAR_DECL
17748 && TREE_STATIC (decl_or_origin)
17749 && DECL_RTL_SET_P (decl_or_origin)))
17750 /* When abstract origin already has DW_AT_location attribute, no need
17751 to add it again. */
17752 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17753 {
17754 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
17755 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
17756 defer_location (decl_or_origin, var_die);
17757 else
17758 add_location_or_const_value_attribute (var_die, decl_or_origin,
17759 decl == NULL, DW_AT_location);
17760 add_pubname (decl_or_origin, var_die);
17761 }
17762 else
17763 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
17764 }
17765
17766 /* Generate a DIE to represent a named constant. */
17767
17768 static void
17769 gen_const_die (tree decl, dw_die_ref context_die)
17770 {
17771 dw_die_ref const_die;
17772 tree type = TREE_TYPE (decl);
17773
17774 const_die = new_die (DW_TAG_constant, context_die, decl);
17775 add_name_and_src_coords_attributes (const_die, decl);
17776 add_type_attribute (const_die, type, 1, 0, context_die);
17777 if (TREE_PUBLIC (decl))
17778 add_AT_flag (const_die, DW_AT_external, 1);
17779 if (DECL_ARTIFICIAL (decl))
17780 add_AT_flag (const_die, DW_AT_artificial, 1);
17781 tree_add_const_value_attribute_for_decl (const_die, decl);
17782 }
17783
17784 /* Generate a DIE to represent a label identifier. */
17785
17786 static void
17787 gen_label_die (tree decl, dw_die_ref context_die)
17788 {
17789 tree origin = decl_ultimate_origin (decl);
17790 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
17791 rtx insn;
17792 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17793
17794 if (origin != NULL)
17795 add_abstract_origin_attribute (lbl_die, origin);
17796 else
17797 add_name_and_src_coords_attributes (lbl_die, decl);
17798
17799 if (DECL_ABSTRACT (decl))
17800 equate_decl_number_to_die (decl, lbl_die);
17801 else
17802 {
17803 insn = DECL_RTL_IF_SET (decl);
17804
17805 /* Deleted labels are programmer specified labels which have been
17806 eliminated because of various optimizations. We still emit them
17807 here so that it is possible to put breakpoints on them. */
17808 if (insn
17809 && (LABEL_P (insn)
17810 || ((NOTE_P (insn)
17811 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
17812 {
17813 /* When optimization is enabled (via -O) some parts of the compiler
17814 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
17815 represent source-level labels which were explicitly declared by
17816 the user. This really shouldn't be happening though, so catch
17817 it if it ever does happen. */
17818 gcc_assert (!INSN_DELETED_P (insn));
17819
17820 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
17821 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17822 }
17823 else if (insn
17824 && NOTE_P (insn)
17825 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
17826 && CODE_LABEL_NUMBER (insn) != -1)
17827 {
17828 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
17829 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17830 }
17831 }
17832 }
17833
17834 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
17835 attributes to the DIE for a block STMT, to describe where the inlined
17836 function was called from. This is similar to add_src_coords_attributes. */
17837
17838 static inline void
17839 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
17840 {
17841 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
17842
17843 if (dwarf_version >= 3 || !dwarf_strict)
17844 {
17845 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
17846 add_AT_unsigned (die, DW_AT_call_line, s.line);
17847 }
17848 }
17849
17850
17851 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
17852 Add low_pc and high_pc attributes to the DIE for a block STMT. */
17853
17854 static inline void
17855 add_high_low_attributes (tree stmt, dw_die_ref die)
17856 {
17857 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17858
17859 if (BLOCK_FRAGMENT_CHAIN (stmt)
17860 && (dwarf_version >= 3 || !dwarf_strict))
17861 {
17862 tree chain, superblock = NULL_TREE;
17863 dw_die_ref pdie;
17864 dw_attr_ref attr = NULL;
17865
17866 if (inlined_function_outer_scope_p (stmt))
17867 {
17868 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17869 BLOCK_NUMBER (stmt));
17870 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17871 }
17872
17873 /* Optimize duplicate .debug_ranges lists or even tails of
17874 lists. If this BLOCK has same ranges as its supercontext,
17875 lookup DW_AT_ranges attribute in the supercontext (and
17876 recursively so), verify that the ranges_table contains the
17877 right values and use it instead of adding a new .debug_range. */
17878 for (chain = stmt, pdie = die;
17879 BLOCK_SAME_RANGE (chain);
17880 chain = BLOCK_SUPERCONTEXT (chain))
17881 {
17882 dw_attr_ref new_attr;
17883
17884 pdie = pdie->die_parent;
17885 if (pdie == NULL)
17886 break;
17887 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
17888 break;
17889 new_attr = get_AT (pdie, DW_AT_ranges);
17890 if (new_attr == NULL
17891 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
17892 break;
17893 attr = new_attr;
17894 superblock = BLOCK_SUPERCONTEXT (chain);
17895 }
17896 if (attr != NULL
17897 && (ranges_table[attr->dw_attr_val.v.val_offset
17898 / 2 / DWARF2_ADDR_SIZE].num
17899 == BLOCK_NUMBER (superblock))
17900 && BLOCK_FRAGMENT_CHAIN (superblock))
17901 {
17902 unsigned long off = attr->dw_attr_val.v.val_offset
17903 / 2 / DWARF2_ADDR_SIZE;
17904 unsigned long supercnt = 0, thiscnt = 0;
17905 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
17906 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
17907 {
17908 ++supercnt;
17909 gcc_checking_assert (ranges_table[off + supercnt].num
17910 == BLOCK_NUMBER (chain));
17911 }
17912 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
17913 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
17914 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
17915 ++thiscnt;
17916 gcc_assert (supercnt >= thiscnt);
17917 add_AT_range_list (die, DW_AT_ranges,
17918 (off + supercnt - thiscnt)
17919 * 2 * DWARF2_ADDR_SIZE);
17920 return;
17921 }
17922
17923 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
17924
17925 chain = BLOCK_FRAGMENT_CHAIN (stmt);
17926 do
17927 {
17928 add_ranges (chain);
17929 chain = BLOCK_FRAGMENT_CHAIN (chain);
17930 }
17931 while (chain);
17932 add_ranges (NULL);
17933 }
17934 else
17935 {
17936 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
17937 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17938 BLOCK_NUMBER (stmt));
17939 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
17940 BLOCK_NUMBER (stmt));
17941 add_AT_low_high_pc (die, label, label_high);
17942 }
17943 }
17944
17945 /* Generate a DIE for a lexical block. */
17946
17947 static void
17948 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
17949 {
17950 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
17951
17952 if (call_arg_locations)
17953 {
17954 if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17955 VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17956 BLOCK_NUMBER (stmt) + 1);
17957 VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
17958 }
17959
17960 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
17961 add_high_low_attributes (stmt, stmt_die);
17962
17963 decls_for_scope (stmt, stmt_die, depth);
17964 }
17965
17966 /* Generate a DIE for an inlined subprogram. */
17967
17968 static void
17969 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
17970 {
17971 tree decl;
17972
17973 /* The instance of function that is effectively being inlined shall not
17974 be abstract. */
17975 gcc_assert (! BLOCK_ABSTRACT (stmt));
17976
17977 decl = block_ultimate_origin (stmt);
17978
17979 /* Emit info for the abstract instance first, if we haven't yet. We
17980 must emit this even if the block is abstract, otherwise when we
17981 emit the block below (or elsewhere), we may end up trying to emit
17982 a die whose origin die hasn't been emitted, and crashing. */
17983 dwarf2out_abstract_function (decl);
17984
17985 if (! BLOCK_ABSTRACT (stmt))
17986 {
17987 dw_die_ref subr_die
17988 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
17989
17990 if (call_arg_locations)
17991 {
17992 if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17993 VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17994 BLOCK_NUMBER (stmt) + 1);
17995 VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
17996 }
17997 add_abstract_origin_attribute (subr_die, decl);
17998 if (TREE_ASM_WRITTEN (stmt))
17999 add_high_low_attributes (stmt, subr_die);
18000 add_call_src_coords_attributes (stmt, subr_die);
18001
18002 decls_for_scope (stmt, subr_die, depth);
18003 current_function_has_inlines = 1;
18004 }
18005 }
18006
18007 /* Generate a DIE for a field in a record, or structure. */
18008
18009 static void
18010 gen_field_die (tree decl, dw_die_ref context_die)
18011 {
18012 dw_die_ref decl_die;
18013
18014 if (TREE_TYPE (decl) == error_mark_node)
18015 return;
18016
18017 decl_die = new_die (DW_TAG_member, context_die, decl);
18018 add_name_and_src_coords_attributes (decl_die, decl);
18019 add_type_attribute (decl_die, member_declared_type (decl),
18020 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18021 context_die);
18022
18023 if (DECL_BIT_FIELD_TYPE (decl))
18024 {
18025 add_byte_size_attribute (decl_die, decl);
18026 add_bit_size_attribute (decl_die, decl);
18027 add_bit_offset_attribute (decl_die, decl);
18028 }
18029
18030 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18031 add_data_member_location_attribute (decl_die, decl);
18032
18033 if (DECL_ARTIFICIAL (decl))
18034 add_AT_flag (decl_die, DW_AT_artificial, 1);
18035
18036 add_accessibility_attribute (decl_die, decl);
18037
18038 /* Equate decl number to die, so that we can look up this decl later on. */
18039 equate_decl_number_to_die (decl, decl_die);
18040 }
18041
18042 #if 0
18043 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18044 Use modified_type_die instead.
18045 We keep this code here just in case these types of DIEs may be needed to
18046 represent certain things in other languages (e.g. Pascal) someday. */
18047
18048 static void
18049 gen_pointer_type_die (tree type, dw_die_ref context_die)
18050 {
18051 dw_die_ref ptr_die
18052 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18053
18054 equate_type_number_to_die (type, ptr_die);
18055 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18056 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18057 }
18058
18059 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18060 Use modified_type_die instead.
18061 We keep this code here just in case these types of DIEs may be needed to
18062 represent certain things in other languages (e.g. Pascal) someday. */
18063
18064 static void
18065 gen_reference_type_die (tree type, dw_die_ref context_die)
18066 {
18067 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18068
18069 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18070 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18071 else
18072 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18073
18074 equate_type_number_to_die (type, ref_die);
18075 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18076 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18077 }
18078 #endif
18079
18080 /* Generate a DIE for a pointer to a member type. */
18081
18082 static void
18083 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18084 {
18085 dw_die_ref ptr_die
18086 = new_die (DW_TAG_ptr_to_member_type,
18087 scope_die_for (type, context_die), type);
18088
18089 equate_type_number_to_die (type, ptr_die);
18090 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18091 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18092 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18093 }
18094
18095 typedef const char *dchar_p; /* For DEF_VEC_P. */
18096 DEF_VEC_P(dchar_p);
18097 DEF_VEC_ALLOC_P(dchar_p,heap);
18098
18099 static char *producer_string;
18100
18101 /* Return a heap allocated producer string including command line options
18102 if -grecord-gcc-switches. */
18103
18104 static char *
18105 gen_producer_string (void)
18106 {
18107 size_t j;
18108 VEC(dchar_p, heap) *switches = NULL;
18109 const char *language_string = lang_hooks.name;
18110 char *producer, *tail;
18111 const char *p;
18112 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18113 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18114
18115 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18116 switch (save_decoded_options[j].opt_index)
18117 {
18118 case OPT_o:
18119 case OPT_d:
18120 case OPT_dumpbase:
18121 case OPT_dumpdir:
18122 case OPT_auxbase:
18123 case OPT_auxbase_strip:
18124 case OPT_quiet:
18125 case OPT_version:
18126 case OPT_v:
18127 case OPT_w:
18128 case OPT_L:
18129 case OPT_D:
18130 case OPT_I:
18131 case OPT_U:
18132 case OPT_SPECIAL_unknown:
18133 case OPT_SPECIAL_ignore:
18134 case OPT_SPECIAL_program_name:
18135 case OPT_SPECIAL_input_file:
18136 case OPT_grecord_gcc_switches:
18137 case OPT_gno_record_gcc_switches:
18138 case OPT__output_pch_:
18139 case OPT_fdiagnostics_show_location_:
18140 case OPT_fdiagnostics_show_option:
18141 case OPT_fdiagnostics_show_caret:
18142 case OPT_fverbose_asm:
18143 case OPT____:
18144 case OPT__sysroot_:
18145 case OPT_nostdinc:
18146 case OPT_nostdinc__:
18147 /* Ignore these. */
18148 continue;
18149 default:
18150 if (cl_options[save_decoded_options[j].opt_index].flags
18151 & CL_NO_DWARF_RECORD)
18152 continue;
18153 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18154 == '-');
18155 switch (save_decoded_options[j].canonical_option[0][1])
18156 {
18157 case 'M':
18158 case 'i':
18159 case 'W':
18160 continue;
18161 case 'f':
18162 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18163 "dump", 4) == 0)
18164 continue;
18165 break;
18166 default:
18167 break;
18168 }
18169 VEC_safe_push (dchar_p, heap, switches,
18170 save_decoded_options[j].orig_option_with_args_text);
18171 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18172 break;
18173 }
18174
18175 producer = XNEWVEC (char, plen + 1 + len + 1);
18176 tail = producer;
18177 sprintf (tail, "%s %s", language_string, version_string);
18178 tail += plen;
18179
18180 FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
18181 {
18182 len = strlen (p);
18183 *tail = ' ';
18184 memcpy (tail + 1, p, len);
18185 tail += len + 1;
18186 }
18187
18188 *tail = '\0';
18189 VEC_free (dchar_p, heap, switches);
18190 return producer;
18191 }
18192
18193 /* Generate the DIE for the compilation unit. */
18194
18195 static dw_die_ref
18196 gen_compile_unit_die (const char *filename)
18197 {
18198 dw_die_ref die;
18199 const char *language_string = lang_hooks.name;
18200 int language;
18201
18202 die = new_die (DW_TAG_compile_unit, NULL, NULL);
18203
18204 if (filename)
18205 {
18206 add_name_attribute (die, filename);
18207 /* Don't add cwd for <built-in>. */
18208 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18209 add_comp_dir_attribute (die);
18210 }
18211
18212 if (producer_string == NULL)
18213 producer_string = gen_producer_string ();
18214 add_AT_string (die, DW_AT_producer, producer_string);
18215
18216 /* If our producer is LTO try to figure out a common language to use
18217 from the global list of translation units. */
18218 if (strcmp (language_string, "GNU GIMPLE") == 0)
18219 {
18220 unsigned i;
18221 tree t;
18222 const char *common_lang = NULL;
18223
18224 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18225 {
18226 if (!TRANSLATION_UNIT_LANGUAGE (t))
18227 continue;
18228 if (!common_lang)
18229 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18230 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18231 ;
18232 else if (strncmp (common_lang, "GNU C", 5) == 0
18233 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18234 /* Mixing C and C++ is ok, use C++ in that case. */
18235 common_lang = "GNU C++";
18236 else
18237 {
18238 /* Fall back to C. */
18239 common_lang = NULL;
18240 break;
18241 }
18242 }
18243
18244 if (common_lang)
18245 language_string = common_lang;
18246 }
18247
18248 language = DW_LANG_C89;
18249 if (strcmp (language_string, "GNU C++") == 0)
18250 language = DW_LANG_C_plus_plus;
18251 else if (strcmp (language_string, "GNU F77") == 0)
18252 language = DW_LANG_Fortran77;
18253 else if (strcmp (language_string, "GNU Pascal") == 0)
18254 language = DW_LANG_Pascal83;
18255 else if (dwarf_version >= 3 || !dwarf_strict)
18256 {
18257 if (strcmp (language_string, "GNU Ada") == 0)
18258 language = DW_LANG_Ada95;
18259 else if (strcmp (language_string, "GNU Fortran") == 0)
18260 language = DW_LANG_Fortran95;
18261 else if (strcmp (language_string, "GNU Java") == 0)
18262 language = DW_LANG_Java;
18263 else if (strcmp (language_string, "GNU Objective-C") == 0)
18264 language = DW_LANG_ObjC;
18265 else if (strcmp (language_string, "GNU Objective-C++") == 0)
18266 language = DW_LANG_ObjC_plus_plus;
18267 else if (dwarf_version >= 5 || !dwarf_strict)
18268 {
18269 if (strcmp (language_string, "GNU Go") == 0)
18270 language = DW_LANG_Go;
18271 }
18272 }
18273 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
18274 else if (strcmp (language_string, "GNU Fortran") == 0)
18275 language = DW_LANG_Fortran90;
18276
18277 add_AT_unsigned (die, DW_AT_language, language);
18278
18279 switch (language)
18280 {
18281 case DW_LANG_Fortran77:
18282 case DW_LANG_Fortran90:
18283 case DW_LANG_Fortran95:
18284 /* Fortran has case insensitive identifiers and the front-end
18285 lowercases everything. */
18286 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18287 break;
18288 default:
18289 /* The default DW_ID_case_sensitive doesn't need to be specified. */
18290 break;
18291 }
18292 return die;
18293 }
18294
18295 /* Generate the DIE for a base class. */
18296
18297 static void
18298 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18299 {
18300 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18301
18302 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18303 add_data_member_location_attribute (die, binfo);
18304
18305 if (BINFO_VIRTUAL_P (binfo))
18306 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18307
18308 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18309 children, otherwise the default is DW_ACCESS_public. In DWARF2
18310 the default has always been DW_ACCESS_private. */
18311 if (access == access_public_node)
18312 {
18313 if (dwarf_version == 2
18314 || context_die->die_tag == DW_TAG_class_type)
18315 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18316 }
18317 else if (access == access_protected_node)
18318 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18319 else if (dwarf_version > 2
18320 && context_die->die_tag != DW_TAG_class_type)
18321 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18322 }
18323
18324 /* Generate a DIE for a class member. */
18325
18326 static void
18327 gen_member_die (tree type, dw_die_ref context_die)
18328 {
18329 tree member;
18330 tree binfo = TYPE_BINFO (type);
18331 dw_die_ref child;
18332
18333 /* If this is not an incomplete type, output descriptions of each of its
18334 members. Note that as we output the DIEs necessary to represent the
18335 members of this record or union type, we will also be trying to output
18336 DIEs to represent the *types* of those members. However the `type'
18337 function (above) will specifically avoid generating type DIEs for member
18338 types *within* the list of member DIEs for this (containing) type except
18339 for those types (of members) which are explicitly marked as also being
18340 members of this (containing) type themselves. The g++ front- end can
18341 force any given type to be treated as a member of some other (containing)
18342 type by setting the TYPE_CONTEXT of the given (member) type to point to
18343 the TREE node representing the appropriate (containing) type. */
18344
18345 /* First output info about the base classes. */
18346 if (binfo)
18347 {
18348 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18349 int i;
18350 tree base;
18351
18352 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18353 gen_inheritance_die (base,
18354 (accesses ? VEC_index (tree, accesses, i)
18355 : access_public_node), context_die);
18356 }
18357
18358 /* Now output info about the data members and type members. */
18359 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18360 {
18361 /* If we thought we were generating minimal debug info for TYPE
18362 and then changed our minds, some of the member declarations
18363 may have already been defined. Don't define them again, but
18364 do put them in the right order. */
18365
18366 child = lookup_decl_die (member);
18367 if (child)
18368 splice_child_die (context_die, child);
18369 else
18370 gen_decl_die (member, NULL, context_die);
18371 }
18372
18373 /* Now output info about the function members (if any). */
18374 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18375 {
18376 /* Don't include clones in the member list. */
18377 if (DECL_ABSTRACT_ORIGIN (member))
18378 continue;
18379
18380 child = lookup_decl_die (member);
18381 if (child)
18382 splice_child_die (context_die, child);
18383 else
18384 gen_decl_die (member, NULL, context_die);
18385 }
18386 }
18387
18388 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
18389 is set, we pretend that the type was never defined, so we only get the
18390 member DIEs needed by later specification DIEs. */
18391
18392 static void
18393 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18394 enum debug_info_usage usage)
18395 {
18396 dw_die_ref type_die = lookup_type_die (type);
18397 dw_die_ref scope_die = 0;
18398 int nested = 0;
18399 int complete = (TYPE_SIZE (type)
18400 && (! TYPE_STUB_DECL (type)
18401 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18402 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18403 complete = complete && should_emit_struct_debug (type, usage);
18404
18405 if (type_die && ! complete)
18406 return;
18407
18408 if (TYPE_CONTEXT (type) != NULL_TREE
18409 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18410 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18411 nested = 1;
18412
18413 scope_die = scope_die_for (type, context_die);
18414
18415 if (! type_die || (nested && is_cu_die (scope_die)))
18416 /* First occurrence of type or toplevel definition of nested class. */
18417 {
18418 dw_die_ref old_die = type_die;
18419
18420 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18421 ? record_type_tag (type) : DW_TAG_union_type,
18422 scope_die, type);
18423 equate_type_number_to_die (type, type_die);
18424 if (old_die)
18425 add_AT_specification (type_die, old_die);
18426 else
18427 add_name_attribute (type_die, type_tag (type));
18428 }
18429 else
18430 remove_AT (type_die, DW_AT_declaration);
18431
18432 /* Generate child dies for template paramaters. */
18433 if (debug_info_level > DINFO_LEVEL_TERSE
18434 && COMPLETE_TYPE_P (type))
18435 schedule_generic_params_dies_gen (type);
18436
18437 /* If this type has been completed, then give it a byte_size attribute and
18438 then give a list of members. */
18439 if (complete && !ns_decl)
18440 {
18441 /* Prevent infinite recursion in cases where the type of some member of
18442 this type is expressed in terms of this type itself. */
18443 TREE_ASM_WRITTEN (type) = 1;
18444 add_byte_size_attribute (type_die, type);
18445 if (TYPE_STUB_DECL (type) != NULL_TREE)
18446 {
18447 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18448 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18449 }
18450
18451 /* If the first reference to this type was as the return type of an
18452 inline function, then it may not have a parent. Fix this now. */
18453 if (type_die->die_parent == NULL)
18454 add_child_die (scope_die, type_die);
18455
18456 push_decl_scope (type);
18457 gen_member_die (type, type_die);
18458 pop_decl_scope ();
18459
18460 add_gnat_descriptive_type_attribute (type_die, type, context_die);
18461 if (TYPE_ARTIFICIAL (type))
18462 add_AT_flag (type_die, DW_AT_artificial, 1);
18463
18464 /* GNU extension: Record what type our vtable lives in. */
18465 if (TYPE_VFIELD (type))
18466 {
18467 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18468
18469 gen_type_die (vtype, context_die);
18470 add_AT_die_ref (type_die, DW_AT_containing_type,
18471 lookup_type_die (vtype));
18472 }
18473 }
18474 else
18475 {
18476 add_AT_flag (type_die, DW_AT_declaration, 1);
18477
18478 /* We don't need to do this for function-local types. */
18479 if (TYPE_STUB_DECL (type)
18480 && ! decl_function_context (TYPE_STUB_DECL (type)))
18481 VEC_safe_push (tree, gc, incomplete_types, type);
18482 }
18483
18484 if (get_AT (type_die, DW_AT_name))
18485 add_pubtype (type, type_die);
18486 }
18487
18488 /* Generate a DIE for a subroutine _type_. */
18489
18490 static void
18491 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18492 {
18493 tree return_type = TREE_TYPE (type);
18494 dw_die_ref subr_die
18495 = new_die (DW_TAG_subroutine_type,
18496 scope_die_for (type, context_die), type);
18497
18498 equate_type_number_to_die (type, subr_die);
18499 add_prototyped_attribute (subr_die, type);
18500 add_type_attribute (subr_die, return_type, 0, 0, context_die);
18501 gen_formal_types_die (type, subr_die);
18502
18503 if (get_AT (subr_die, DW_AT_name))
18504 add_pubtype (type, subr_die);
18505 }
18506
18507 /* Generate a DIE for a type definition. */
18508
18509 static void
18510 gen_typedef_die (tree decl, dw_die_ref context_die)
18511 {
18512 dw_die_ref type_die;
18513 tree origin;
18514
18515 if (TREE_ASM_WRITTEN (decl))
18516 return;
18517
18518 TREE_ASM_WRITTEN (decl) = 1;
18519 type_die = new_die (DW_TAG_typedef, context_die, decl);
18520 origin = decl_ultimate_origin (decl);
18521 if (origin != NULL)
18522 add_abstract_origin_attribute (type_die, origin);
18523 else
18524 {
18525 tree type;
18526
18527 add_name_and_src_coords_attributes (type_die, decl);
18528 if (DECL_ORIGINAL_TYPE (decl))
18529 {
18530 type = DECL_ORIGINAL_TYPE (decl);
18531
18532 gcc_assert (type != TREE_TYPE (decl));
18533 equate_type_number_to_die (TREE_TYPE (decl), type_die);
18534 }
18535 else
18536 {
18537 type = TREE_TYPE (decl);
18538
18539 if (is_naming_typedef_decl (TYPE_NAME (type)))
18540 {
18541 /* Here, we are in the case of decl being a typedef naming
18542 an anonymous type, e.g:
18543 typedef struct {...} foo;
18544 In that case TREE_TYPE (decl) is not a typedef variant
18545 type and TYPE_NAME of the anonymous type is set to the
18546 TYPE_DECL of the typedef. This construct is emitted by
18547 the C++ FE.
18548
18549 TYPE is the anonymous struct named by the typedef
18550 DECL. As we need the DW_AT_type attribute of the
18551 DW_TAG_typedef to point to the DIE of TYPE, let's
18552 generate that DIE right away. add_type_attribute
18553 called below will then pick (via lookup_type_die) that
18554 anonymous struct DIE. */
18555 if (!TREE_ASM_WRITTEN (type))
18556 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18557
18558 /* This is a GNU Extension. We are adding a
18559 DW_AT_linkage_name attribute to the DIE of the
18560 anonymous struct TYPE. The value of that attribute
18561 is the name of the typedef decl naming the anonymous
18562 struct. This greatly eases the work of consumers of
18563 this debug info. */
18564 add_linkage_attr (lookup_type_die (type), decl);
18565 }
18566 }
18567
18568 add_type_attribute (type_die, type, TREE_READONLY (decl),
18569 TREE_THIS_VOLATILE (decl), context_die);
18570
18571 if (is_naming_typedef_decl (decl))
18572 /* We want that all subsequent calls to lookup_type_die with
18573 TYPE in argument yield the DW_TAG_typedef we have just
18574 created. */
18575 equate_type_number_to_die (type, type_die);
18576
18577 add_accessibility_attribute (type_die, decl);
18578 }
18579
18580 if (DECL_ABSTRACT (decl))
18581 equate_decl_number_to_die (decl, type_die);
18582
18583 if (get_AT (type_die, DW_AT_name))
18584 add_pubtype (decl, type_die);
18585 }
18586
18587 /* Generate a DIE for a struct, class, enum or union type. */
18588
18589 static void
18590 gen_tagged_type_die (tree type,
18591 dw_die_ref context_die,
18592 enum debug_info_usage usage)
18593 {
18594 int need_pop;
18595
18596 if (type == NULL_TREE
18597 || !is_tagged_type (type))
18598 return;
18599
18600 /* If this is a nested type whose containing class hasn't been written
18601 out yet, writing it out will cover this one, too. This does not apply
18602 to instantiations of member class templates; they need to be added to
18603 the containing class as they are generated. FIXME: This hurts the
18604 idea of combining type decls from multiple TUs, since we can't predict
18605 what set of template instantiations we'll get. */
18606 if (TYPE_CONTEXT (type)
18607 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18608 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18609 {
18610 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18611
18612 if (TREE_ASM_WRITTEN (type))
18613 return;
18614
18615 /* If that failed, attach ourselves to the stub. */
18616 push_decl_scope (TYPE_CONTEXT (type));
18617 context_die = lookup_type_die (TYPE_CONTEXT (type));
18618 need_pop = 1;
18619 }
18620 else if (TYPE_CONTEXT (type) != NULL_TREE
18621 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18622 {
18623 /* If this type is local to a function that hasn't been written
18624 out yet, use a NULL context for now; it will be fixed up in
18625 decls_for_scope. */
18626 context_die = lookup_decl_die (TYPE_CONTEXT (type));
18627 /* A declaration DIE doesn't count; nested types need to go in the
18628 specification. */
18629 if (context_die && is_declaration_die (context_die))
18630 context_die = NULL;
18631 need_pop = 0;
18632 }
18633 else
18634 {
18635 context_die = declare_in_namespace (type, context_die);
18636 need_pop = 0;
18637 }
18638
18639 if (TREE_CODE (type) == ENUMERAL_TYPE)
18640 {
18641 /* This might have been written out by the call to
18642 declare_in_namespace. */
18643 if (!TREE_ASM_WRITTEN (type))
18644 gen_enumeration_type_die (type, context_die);
18645 }
18646 else
18647 gen_struct_or_union_type_die (type, context_die, usage);
18648
18649 if (need_pop)
18650 pop_decl_scope ();
18651
18652 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18653 it up if it is ever completed. gen_*_type_die will set it for us
18654 when appropriate. */
18655 }
18656
18657 /* Generate a type description DIE. */
18658
18659 static void
18660 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18661 enum debug_info_usage usage)
18662 {
18663 struct array_descr_info info;
18664
18665 if (type == NULL_TREE || type == error_mark_node)
18666 return;
18667
18668 if (TYPE_NAME (type) != NULL_TREE
18669 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18670 && is_redundant_typedef (TYPE_NAME (type))
18671 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18672 /* The DECL of this type is a typedef we don't want to emit debug
18673 info for but we want debug info for its underlying typedef.
18674 This can happen for e.g, the injected-class-name of a C++
18675 type. */
18676 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18677
18678 /* If TYPE is a typedef type variant, let's generate debug info
18679 for the parent typedef which TYPE is a type of. */
18680 if (typedef_variant_p (type))
18681 {
18682 if (TREE_ASM_WRITTEN (type))
18683 return;
18684
18685 /* Prevent broken recursion; we can't hand off to the same type. */
18686 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18687
18688 /* Give typedefs the right scope. */
18689 context_die = scope_die_for (type, context_die);
18690
18691 TREE_ASM_WRITTEN (type) = 1;
18692
18693 gen_decl_die (TYPE_NAME (type), NULL, context_die);
18694 return;
18695 }
18696
18697 /* If type is an anonymous tagged type named by a typedef, let's
18698 generate debug info for the typedef. */
18699 if (is_naming_typedef_decl (TYPE_NAME (type)))
18700 {
18701 /* Use the DIE of the containing namespace as the parent DIE of
18702 the type description DIE we want to generate. */
18703 if (DECL_CONTEXT (TYPE_NAME (type))
18704 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18705 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18706
18707 gen_decl_die (TYPE_NAME (type), NULL, context_die);
18708 return;
18709 }
18710
18711 /* If this is an array type with hidden descriptor, handle it first. */
18712 if (!TREE_ASM_WRITTEN (type)
18713 && lang_hooks.types.get_array_descr_info
18714 && lang_hooks.types.get_array_descr_info (type, &info)
18715 && (dwarf_version >= 3 || !dwarf_strict))
18716 {
18717 gen_descr_array_type_die (type, &info, context_die);
18718 TREE_ASM_WRITTEN (type) = 1;
18719 return;
18720 }
18721
18722 /* We are going to output a DIE to represent the unqualified version
18723 of this type (i.e. without any const or volatile qualifiers) so
18724 get the main variant (i.e. the unqualified version) of this type
18725 now. (Vectors are special because the debugging info is in the
18726 cloned type itself). */
18727 if (TREE_CODE (type) != VECTOR_TYPE)
18728 type = type_main_variant (type);
18729
18730 if (TREE_ASM_WRITTEN (type))
18731 return;
18732
18733 switch (TREE_CODE (type))
18734 {
18735 case ERROR_MARK:
18736 break;
18737
18738 case POINTER_TYPE:
18739 case REFERENCE_TYPE:
18740 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
18741 ensures that the gen_type_die recursion will terminate even if the
18742 type is recursive. Recursive types are possible in Ada. */
18743 /* ??? We could perhaps do this for all types before the switch
18744 statement. */
18745 TREE_ASM_WRITTEN (type) = 1;
18746
18747 /* For these types, all that is required is that we output a DIE (or a
18748 set of DIEs) to represent the "basis" type. */
18749 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18750 DINFO_USAGE_IND_USE);
18751 break;
18752
18753 case OFFSET_TYPE:
18754 /* This code is used for C++ pointer-to-data-member types.
18755 Output a description of the relevant class type. */
18756 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18757 DINFO_USAGE_IND_USE);
18758
18759 /* Output a description of the type of the object pointed to. */
18760 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18761 DINFO_USAGE_IND_USE);
18762
18763 /* Now output a DIE to represent this pointer-to-data-member type
18764 itself. */
18765 gen_ptr_to_mbr_type_die (type, context_die);
18766 break;
18767
18768 case FUNCTION_TYPE:
18769 /* Force out return type (in case it wasn't forced out already). */
18770 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18771 DINFO_USAGE_DIR_USE);
18772 gen_subroutine_type_die (type, context_die);
18773 break;
18774
18775 case METHOD_TYPE:
18776 /* Force out return type (in case it wasn't forced out already). */
18777 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18778 DINFO_USAGE_DIR_USE);
18779 gen_subroutine_type_die (type, context_die);
18780 break;
18781
18782 case ARRAY_TYPE:
18783 gen_array_type_die (type, context_die);
18784 break;
18785
18786 case VECTOR_TYPE:
18787 gen_array_type_die (type, context_die);
18788 break;
18789
18790 case ENUMERAL_TYPE:
18791 case RECORD_TYPE:
18792 case UNION_TYPE:
18793 case QUAL_UNION_TYPE:
18794 gen_tagged_type_die (type, context_die, usage);
18795 return;
18796
18797 case VOID_TYPE:
18798 case INTEGER_TYPE:
18799 case REAL_TYPE:
18800 case FIXED_POINT_TYPE:
18801 case COMPLEX_TYPE:
18802 case BOOLEAN_TYPE:
18803 /* No DIEs needed for fundamental types. */
18804 break;
18805
18806 case NULLPTR_TYPE:
18807 case LANG_TYPE:
18808 /* Just use DW_TAG_unspecified_type. */
18809 {
18810 dw_die_ref type_die = lookup_type_die (type);
18811 if (type_die == NULL)
18812 {
18813 tree name = TYPE_NAME (type);
18814 if (TREE_CODE (name) == TYPE_DECL)
18815 name = DECL_NAME (name);
18816 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
18817 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
18818 equate_type_number_to_die (type, type_die);
18819 }
18820 }
18821 break;
18822
18823 default:
18824 gcc_unreachable ();
18825 }
18826
18827 TREE_ASM_WRITTEN (type) = 1;
18828 }
18829
18830 static void
18831 gen_type_die (tree type, dw_die_ref context_die)
18832 {
18833 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
18834 }
18835
18836 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
18837 things which are local to the given block. */
18838
18839 static void
18840 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
18841 {
18842 int must_output_die = 0;
18843 bool inlined_func;
18844
18845 /* Ignore blocks that are NULL. */
18846 if (stmt == NULL_TREE)
18847 return;
18848
18849 inlined_func = inlined_function_outer_scope_p (stmt);
18850
18851 /* If the block is one fragment of a non-contiguous block, do not
18852 process the variables, since they will have been done by the
18853 origin block. Do process subblocks. */
18854 if (BLOCK_FRAGMENT_ORIGIN (stmt))
18855 {
18856 tree sub;
18857
18858 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
18859 gen_block_die (sub, context_die, depth + 1);
18860
18861 return;
18862 }
18863
18864 /* Determine if we need to output any Dwarf DIEs at all to represent this
18865 block. */
18866 if (inlined_func)
18867 /* The outer scopes for inlinings *must* always be represented. We
18868 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
18869 must_output_die = 1;
18870 else
18871 {
18872 /* Determine if this block directly contains any "significant"
18873 local declarations which we will need to output DIEs for. */
18874 if (debug_info_level > DINFO_LEVEL_TERSE)
18875 /* We are not in terse mode so *any* local declaration counts
18876 as being a "significant" one. */
18877 must_output_die = ((BLOCK_VARS (stmt) != NULL
18878 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
18879 && (TREE_USED (stmt)
18880 || TREE_ASM_WRITTEN (stmt)
18881 || BLOCK_ABSTRACT (stmt)));
18882 else if ((TREE_USED (stmt)
18883 || TREE_ASM_WRITTEN (stmt)
18884 || BLOCK_ABSTRACT (stmt))
18885 && !dwarf2out_ignore_block (stmt))
18886 must_output_die = 1;
18887 }
18888
18889 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
18890 DIE for any block which contains no significant local declarations at
18891 all. Rather, in such cases we just call `decls_for_scope' so that any
18892 needed Dwarf info for any sub-blocks will get properly generated. Note
18893 that in terse mode, our definition of what constitutes a "significant"
18894 local declaration gets restricted to include only inlined function
18895 instances and local (nested) function definitions. */
18896 if (must_output_die)
18897 {
18898 if (inlined_func)
18899 {
18900 /* If STMT block is abstract, that means we have been called
18901 indirectly from dwarf2out_abstract_function.
18902 That function rightfully marks the descendent blocks (of
18903 the abstract function it is dealing with) as being abstract,
18904 precisely to prevent us from emitting any
18905 DW_TAG_inlined_subroutine DIE as a descendent
18906 of an abstract function instance. So in that case, we should
18907 not call gen_inlined_subroutine_die.
18908
18909 Later though, when cgraph asks dwarf2out to emit info
18910 for the concrete instance of the function decl into which
18911 the concrete instance of STMT got inlined, the later will lead
18912 to the generation of a DW_TAG_inlined_subroutine DIE. */
18913 if (! BLOCK_ABSTRACT (stmt))
18914 gen_inlined_subroutine_die (stmt, context_die, depth);
18915 }
18916 else
18917 gen_lexical_block_die (stmt, context_die, depth);
18918 }
18919 else
18920 decls_for_scope (stmt, context_die, depth);
18921 }
18922
18923 /* Process variable DECL (or variable with origin ORIGIN) within
18924 block STMT and add it to CONTEXT_DIE. */
18925 static void
18926 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
18927 {
18928 dw_die_ref die;
18929 tree decl_or_origin = decl ? decl : origin;
18930
18931 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
18932 die = lookup_decl_die (decl_or_origin);
18933 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
18934 && TYPE_DECL_IS_STUB (decl_or_origin))
18935 die = lookup_type_die (TREE_TYPE (decl_or_origin));
18936 else
18937 die = NULL;
18938
18939 if (die != NULL && die->die_parent == NULL)
18940 add_child_die (context_die, die);
18941 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
18942 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
18943 stmt, context_die);
18944 else
18945 gen_decl_die (decl, origin, context_die);
18946 }
18947
18948 /* Generate all of the decls declared within a given scope and (recursively)
18949 all of its sub-blocks. */
18950
18951 static void
18952 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
18953 {
18954 tree decl;
18955 unsigned int i;
18956 tree subblocks;
18957
18958 /* Ignore NULL blocks. */
18959 if (stmt == NULL_TREE)
18960 return;
18961
18962 /* Output the DIEs to represent all of the data objects and typedefs
18963 declared directly within this block but not within any nested
18964 sub-blocks. Also, nested function and tag DIEs have been
18965 generated with a parent of NULL; fix that up now. */
18966 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
18967 process_scope_var (stmt, decl, NULL_TREE, context_die);
18968 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18969 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
18970 context_die);
18971
18972 /* If we're at -g1, we're not interested in subblocks. */
18973 if (debug_info_level <= DINFO_LEVEL_TERSE)
18974 return;
18975
18976 /* Output the DIEs to represent all sub-blocks (and the items declared
18977 therein) of this block. */
18978 for (subblocks = BLOCK_SUBBLOCKS (stmt);
18979 subblocks != NULL;
18980 subblocks = BLOCK_CHAIN (subblocks))
18981 gen_block_die (subblocks, context_die, depth + 1);
18982 }
18983
18984 /* Is this a typedef we can avoid emitting? */
18985
18986 static inline int
18987 is_redundant_typedef (const_tree decl)
18988 {
18989 if (TYPE_DECL_IS_STUB (decl))
18990 return 1;
18991
18992 if (DECL_ARTIFICIAL (decl)
18993 && DECL_CONTEXT (decl)
18994 && is_tagged_type (DECL_CONTEXT (decl))
18995 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
18996 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
18997 /* Also ignore the artificial member typedef for the class name. */
18998 return 1;
18999
19000 return 0;
19001 }
19002
19003 /* Return TRUE if TYPE is a typedef that names a type for linkage
19004 purposes. This kind of typedefs is produced by the C++ FE for
19005 constructs like:
19006
19007 typedef struct {...} foo;
19008
19009 In that case, there is no typedef variant type produced for foo.
19010 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19011 struct type. */
19012
19013 static bool
19014 is_naming_typedef_decl (const_tree decl)
19015 {
19016 if (decl == NULL_TREE
19017 || TREE_CODE (decl) != TYPE_DECL
19018 || !is_tagged_type (TREE_TYPE (decl))
19019 || DECL_IS_BUILTIN (decl)
19020 || is_redundant_typedef (decl)
19021 /* It looks like Ada produces TYPE_DECLs that are very similar
19022 to C++ naming typedefs but that have different
19023 semantics. Let's be specific to c++ for now. */
19024 || !is_cxx ())
19025 return FALSE;
19026
19027 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19028 && TYPE_NAME (TREE_TYPE (decl)) == decl
19029 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19030 != TYPE_NAME (TREE_TYPE (decl))));
19031 }
19032
19033 /* Returns the DIE for a context. */
19034
19035 static inline dw_die_ref
19036 get_context_die (tree context)
19037 {
19038 if (context)
19039 {
19040 /* Find die that represents this context. */
19041 if (TYPE_P (context))
19042 {
19043 context = TYPE_MAIN_VARIANT (context);
19044 return strip_naming_typedef (context, force_type_die (context));
19045 }
19046 else
19047 return force_decl_die (context);
19048 }
19049 return comp_unit_die ();
19050 }
19051
19052 /* Returns the DIE for decl. A DIE will always be returned. */
19053
19054 static dw_die_ref
19055 force_decl_die (tree decl)
19056 {
19057 dw_die_ref decl_die;
19058 unsigned saved_external_flag;
19059 tree save_fn = NULL_TREE;
19060 decl_die = lookup_decl_die (decl);
19061 if (!decl_die)
19062 {
19063 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19064
19065 decl_die = lookup_decl_die (decl);
19066 if (decl_die)
19067 return decl_die;
19068
19069 switch (TREE_CODE (decl))
19070 {
19071 case FUNCTION_DECL:
19072 /* Clear current_function_decl, so that gen_subprogram_die thinks
19073 that this is a declaration. At this point, we just want to force
19074 declaration die. */
19075 save_fn = current_function_decl;
19076 current_function_decl = NULL_TREE;
19077 gen_subprogram_die (decl, context_die);
19078 current_function_decl = save_fn;
19079 break;
19080
19081 case VAR_DECL:
19082 /* Set external flag to force declaration die. Restore it after
19083 gen_decl_die() call. */
19084 saved_external_flag = DECL_EXTERNAL (decl);
19085 DECL_EXTERNAL (decl) = 1;
19086 gen_decl_die (decl, NULL, context_die);
19087 DECL_EXTERNAL (decl) = saved_external_flag;
19088 break;
19089
19090 case NAMESPACE_DECL:
19091 if (dwarf_version >= 3 || !dwarf_strict)
19092 dwarf2out_decl (decl);
19093 else
19094 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19095 decl_die = comp_unit_die ();
19096 break;
19097
19098 case TRANSLATION_UNIT_DECL:
19099 decl_die = comp_unit_die ();
19100 break;
19101
19102 default:
19103 gcc_unreachable ();
19104 }
19105
19106 /* We should be able to find the DIE now. */
19107 if (!decl_die)
19108 decl_die = lookup_decl_die (decl);
19109 gcc_assert (decl_die);
19110 }
19111
19112 return decl_die;
19113 }
19114
19115 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19116 always returned. */
19117
19118 static dw_die_ref
19119 force_type_die (tree type)
19120 {
19121 dw_die_ref type_die;
19122
19123 type_die = lookup_type_die (type);
19124 if (!type_die)
19125 {
19126 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19127
19128 type_die = modified_type_die (type, TYPE_READONLY (type),
19129 TYPE_VOLATILE (type), context_die);
19130 gcc_assert (type_die);
19131 }
19132 return type_die;
19133 }
19134
19135 /* Force out any required namespaces to be able to output DECL,
19136 and return the new context_die for it, if it's changed. */
19137
19138 static dw_die_ref
19139 setup_namespace_context (tree thing, dw_die_ref context_die)
19140 {
19141 tree context = (DECL_P (thing)
19142 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19143 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19144 /* Force out the namespace. */
19145 context_die = force_decl_die (context);
19146
19147 return context_die;
19148 }
19149
19150 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19151 type) within its namespace, if appropriate.
19152
19153 For compatibility with older debuggers, namespace DIEs only contain
19154 declarations; all definitions are emitted at CU scope. */
19155
19156 static dw_die_ref
19157 declare_in_namespace (tree thing, dw_die_ref context_die)
19158 {
19159 dw_die_ref ns_context;
19160
19161 if (debug_info_level <= DINFO_LEVEL_TERSE)
19162 return context_die;
19163
19164 /* If this decl is from an inlined function, then don't try to emit it in its
19165 namespace, as we will get confused. It would have already been emitted
19166 when the abstract instance of the inline function was emitted anyways. */
19167 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19168 return context_die;
19169
19170 ns_context = setup_namespace_context (thing, context_die);
19171
19172 if (ns_context != context_die)
19173 {
19174 if (is_fortran ())
19175 return ns_context;
19176 if (DECL_P (thing))
19177 gen_decl_die (thing, NULL, ns_context);
19178 else
19179 gen_type_die (thing, ns_context);
19180 }
19181 return context_die;
19182 }
19183
19184 /* Generate a DIE for a namespace or namespace alias. */
19185
19186 static void
19187 gen_namespace_die (tree decl, dw_die_ref context_die)
19188 {
19189 dw_die_ref namespace_die;
19190
19191 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19192 they are an alias of. */
19193 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19194 {
19195 /* Output a real namespace or module. */
19196 context_die = setup_namespace_context (decl, comp_unit_die ());
19197 namespace_die = new_die (is_fortran ()
19198 ? DW_TAG_module : DW_TAG_namespace,
19199 context_die, decl);
19200 /* For Fortran modules defined in different CU don't add src coords. */
19201 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19202 {
19203 const char *name = dwarf2_name (decl, 0);
19204 if (name)
19205 add_name_attribute (namespace_die, name);
19206 }
19207 else
19208 add_name_and_src_coords_attributes (namespace_die, decl);
19209 if (DECL_EXTERNAL (decl))
19210 add_AT_flag (namespace_die, DW_AT_declaration, 1);
19211 equate_decl_number_to_die (decl, namespace_die);
19212 }
19213 else
19214 {
19215 /* Output a namespace alias. */
19216
19217 /* Force out the namespace we are an alias of, if necessary. */
19218 dw_die_ref origin_die
19219 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19220
19221 if (DECL_FILE_SCOPE_P (decl)
19222 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19223 context_die = setup_namespace_context (decl, comp_unit_die ());
19224 /* Now create the namespace alias DIE. */
19225 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19226 add_name_and_src_coords_attributes (namespace_die, decl);
19227 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19228 equate_decl_number_to_die (decl, namespace_die);
19229 }
19230 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
19231 if (want_pubnames ())
19232 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
19233 }
19234
19235 /* Generate Dwarf debug information for a decl described by DECL.
19236 The return value is currently only meaningful for PARM_DECLs,
19237 for all other decls it returns NULL. */
19238
19239 static dw_die_ref
19240 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19241 {
19242 tree decl_or_origin = decl ? decl : origin;
19243 tree class_origin = NULL, ultimate_origin;
19244
19245 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19246 return NULL;
19247
19248 switch (TREE_CODE (decl_or_origin))
19249 {
19250 case ERROR_MARK:
19251 break;
19252
19253 case CONST_DECL:
19254 if (!is_fortran () && !is_ada ())
19255 {
19256 /* The individual enumerators of an enum type get output when we output
19257 the Dwarf representation of the relevant enum type itself. */
19258 break;
19259 }
19260
19261 /* Emit its type. */
19262 gen_type_die (TREE_TYPE (decl), context_die);
19263
19264 /* And its containing namespace. */
19265 context_die = declare_in_namespace (decl, context_die);
19266
19267 gen_const_die (decl, context_die);
19268 break;
19269
19270 case FUNCTION_DECL:
19271 /* Don't output any DIEs to represent mere function declarations,
19272 unless they are class members or explicit block externs. */
19273 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19274 && DECL_FILE_SCOPE_P (decl_or_origin)
19275 && (current_function_decl == NULL_TREE
19276 || DECL_ARTIFICIAL (decl_or_origin)))
19277 break;
19278
19279 #if 0
19280 /* FIXME */
19281 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19282 on local redeclarations of global functions. That seems broken. */
19283 if (current_function_decl != decl)
19284 /* This is only a declaration. */;
19285 #endif
19286
19287 /* If we're emitting a clone, emit info for the abstract instance. */
19288 if (origin || DECL_ORIGIN (decl) != decl)
19289 dwarf2out_abstract_function (origin
19290 ? DECL_ORIGIN (origin)
19291 : DECL_ABSTRACT_ORIGIN (decl));
19292
19293 /* If we're emitting an out-of-line copy of an inline function,
19294 emit info for the abstract instance and set up to refer to it. */
19295 else if (cgraph_function_possibly_inlined_p (decl)
19296 && ! DECL_ABSTRACT (decl)
19297 && ! class_or_namespace_scope_p (context_die)
19298 /* dwarf2out_abstract_function won't emit a die if this is just
19299 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
19300 that case, because that works only if we have a die. */
19301 && DECL_INITIAL (decl) != NULL_TREE)
19302 {
19303 dwarf2out_abstract_function (decl);
19304 set_decl_origin_self (decl);
19305 }
19306
19307 /* Otherwise we're emitting the primary DIE for this decl. */
19308 else if (debug_info_level > DINFO_LEVEL_TERSE)
19309 {
19310 /* Before we describe the FUNCTION_DECL itself, make sure that we
19311 have its containing type. */
19312 if (!origin)
19313 origin = decl_class_context (decl);
19314 if (origin != NULL_TREE)
19315 gen_type_die (origin, context_die);
19316
19317 /* And its return type. */
19318 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19319
19320 /* And its virtual context. */
19321 if (DECL_VINDEX (decl) != NULL_TREE)
19322 gen_type_die (DECL_CONTEXT (decl), context_die);
19323
19324 /* Make sure we have a member DIE for decl. */
19325 if (origin != NULL_TREE)
19326 gen_type_die_for_member (origin, decl, context_die);
19327
19328 /* And its containing namespace. */
19329 context_die = declare_in_namespace (decl, context_die);
19330 }
19331
19332 /* Now output a DIE to represent the function itself. */
19333 if (decl)
19334 gen_subprogram_die (decl, context_die);
19335 break;
19336
19337 case TYPE_DECL:
19338 /* If we are in terse mode, don't generate any DIEs to represent any
19339 actual typedefs. */
19340 if (debug_info_level <= DINFO_LEVEL_TERSE)
19341 break;
19342
19343 /* In the special case of a TYPE_DECL node representing the declaration
19344 of some type tag, if the given TYPE_DECL is marked as having been
19345 instantiated from some other (original) TYPE_DECL node (e.g. one which
19346 was generated within the original definition of an inline function) we
19347 used to generate a special (abbreviated) DW_TAG_structure_type,
19348 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
19349 should be actually referencing those DIEs, as variable DIEs with that
19350 type would be emitted already in the abstract origin, so it was always
19351 removed during unused type prunning. Don't add anything in this
19352 case. */
19353 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19354 break;
19355
19356 if (is_redundant_typedef (decl))
19357 gen_type_die (TREE_TYPE (decl), context_die);
19358 else
19359 /* Output a DIE to represent the typedef itself. */
19360 gen_typedef_die (decl, context_die);
19361 break;
19362
19363 case LABEL_DECL:
19364 if (debug_info_level >= DINFO_LEVEL_NORMAL)
19365 gen_label_die (decl, context_die);
19366 break;
19367
19368 case VAR_DECL:
19369 case RESULT_DECL:
19370 /* If we are in terse mode, don't generate any DIEs to represent any
19371 variable declarations or definitions. */
19372 if (debug_info_level <= DINFO_LEVEL_TERSE)
19373 break;
19374
19375 /* Output any DIEs that are needed to specify the type of this data
19376 object. */
19377 if (decl_by_reference_p (decl_or_origin))
19378 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19379 else
19380 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19381
19382 /* And its containing type. */
19383 class_origin = decl_class_context (decl_or_origin);
19384 if (class_origin != NULL_TREE)
19385 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19386
19387 /* And its containing namespace. */
19388 context_die = declare_in_namespace (decl_or_origin, context_die);
19389
19390 /* Now output the DIE to represent the data object itself. This gets
19391 complicated because of the possibility that the VAR_DECL really
19392 represents an inlined instance of a formal parameter for an inline
19393 function. */
19394 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19395 if (ultimate_origin != NULL_TREE
19396 && TREE_CODE (ultimate_origin) == PARM_DECL)
19397 gen_formal_parameter_die (decl, origin,
19398 true /* Emit name attribute. */,
19399 context_die);
19400 else
19401 gen_variable_die (decl, origin, context_die);
19402 break;
19403
19404 case FIELD_DECL:
19405 /* Ignore the nameless fields that are used to skip bits but handle C++
19406 anonymous unions and structs. */
19407 if (DECL_NAME (decl) != NULL_TREE
19408 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19409 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19410 {
19411 gen_type_die (member_declared_type (decl), context_die);
19412 gen_field_die (decl, context_die);
19413 }
19414 break;
19415
19416 case PARM_DECL:
19417 if (DECL_BY_REFERENCE (decl_or_origin))
19418 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19419 else
19420 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19421 return gen_formal_parameter_die (decl, origin,
19422 true /* Emit name attribute. */,
19423 context_die);
19424
19425 case NAMESPACE_DECL:
19426 case IMPORTED_DECL:
19427 if (dwarf_version >= 3 || !dwarf_strict)
19428 gen_namespace_die (decl, context_die);
19429 break;
19430
19431 default:
19432 /* Probably some frontend-internal decl. Assume we don't care. */
19433 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19434 break;
19435 }
19436
19437 return NULL;
19438 }
19439 \f
19440 /* Output debug information for global decl DECL. Called from toplev.c after
19441 compilation proper has finished. */
19442
19443 static void
19444 dwarf2out_global_decl (tree decl)
19445 {
19446 /* Output DWARF2 information for file-scope tentative data object
19447 declarations, file-scope (extern) function declarations (which
19448 had no corresponding body) and file-scope tagged type declarations
19449 and definitions which have not yet been forced out. */
19450 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19451 dwarf2out_decl (decl);
19452 }
19453
19454 /* Output debug information for type decl DECL. Called from toplev.c
19455 and from language front ends (to record built-in types). */
19456 static void
19457 dwarf2out_type_decl (tree decl, int local)
19458 {
19459 if (!local)
19460 dwarf2out_decl (decl);
19461 }
19462
19463 /* Output debug information for imported module or decl DECL.
19464 NAME is non-NULL name in the lexical block if the decl has been renamed.
19465 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19466 that DECL belongs to.
19467 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
19468 static void
19469 dwarf2out_imported_module_or_decl_1 (tree decl,
19470 tree name,
19471 tree lexical_block,
19472 dw_die_ref lexical_block_die)
19473 {
19474 expanded_location xloc;
19475 dw_die_ref imported_die = NULL;
19476 dw_die_ref at_import_die;
19477
19478 if (TREE_CODE (decl) == IMPORTED_DECL)
19479 {
19480 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19481 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19482 gcc_assert (decl);
19483 }
19484 else
19485 xloc = expand_location (input_location);
19486
19487 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19488 {
19489 at_import_die = force_type_die (TREE_TYPE (decl));
19490 /* For namespace N { typedef void T; } using N::T; base_type_die
19491 returns NULL, but DW_TAG_imported_declaration requires
19492 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
19493 if (!at_import_die)
19494 {
19495 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19496 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19497 at_import_die = lookup_type_die (TREE_TYPE (decl));
19498 gcc_assert (at_import_die);
19499 }
19500 }
19501 else
19502 {
19503 at_import_die = lookup_decl_die (decl);
19504 if (!at_import_die)
19505 {
19506 /* If we're trying to avoid duplicate debug info, we may not have
19507 emitted the member decl for this field. Emit it now. */
19508 if (TREE_CODE (decl) == FIELD_DECL)
19509 {
19510 tree type = DECL_CONTEXT (decl);
19511
19512 if (TYPE_CONTEXT (type)
19513 && TYPE_P (TYPE_CONTEXT (type))
19514 && !should_emit_struct_debug (TYPE_CONTEXT (type),
19515 DINFO_USAGE_DIR_USE))
19516 return;
19517 gen_type_die_for_member (type, decl,
19518 get_context_die (TYPE_CONTEXT (type)));
19519 }
19520 at_import_die = force_decl_die (decl);
19521 }
19522 }
19523
19524 if (TREE_CODE (decl) == NAMESPACE_DECL)
19525 {
19526 if (dwarf_version >= 3 || !dwarf_strict)
19527 imported_die = new_die (DW_TAG_imported_module,
19528 lexical_block_die,
19529 lexical_block);
19530 else
19531 return;
19532 }
19533 else
19534 imported_die = new_die (DW_TAG_imported_declaration,
19535 lexical_block_die,
19536 lexical_block);
19537
19538 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19539 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19540 if (name)
19541 add_AT_string (imported_die, DW_AT_name,
19542 IDENTIFIER_POINTER (name));
19543 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19544 }
19545
19546 /* Output debug information for imported module or decl DECL.
19547 NAME is non-NULL name in context if the decl has been renamed.
19548 CHILD is true if decl is one of the renamed decls as part of
19549 importing whole module. */
19550
19551 static void
19552 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19553 bool child)
19554 {
19555 /* dw_die_ref at_import_die; */
19556 dw_die_ref scope_die;
19557
19558 if (debug_info_level <= DINFO_LEVEL_TERSE)
19559 return;
19560
19561 gcc_assert (decl);
19562
19563 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19564 We need decl DIE for reference and scope die. First, get DIE for the decl
19565 itself. */
19566
19567 /* Get the scope die for decl context. Use comp_unit_die for global module
19568 or decl. If die is not found for non globals, force new die. */
19569 if (context
19570 && TYPE_P (context)
19571 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19572 return;
19573
19574 if (!(dwarf_version >= 3 || !dwarf_strict))
19575 return;
19576
19577 scope_die = get_context_die (context);
19578
19579 if (child)
19580 {
19581 gcc_assert (scope_die->die_child);
19582 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19583 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19584 scope_die = scope_die->die_child;
19585 }
19586
19587 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
19588 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19589
19590 }
19591
19592 /* Write the debugging output for DECL. */
19593
19594 void
19595 dwarf2out_decl (tree decl)
19596 {
19597 dw_die_ref context_die = comp_unit_die ();
19598
19599 switch (TREE_CODE (decl))
19600 {
19601 case ERROR_MARK:
19602 return;
19603
19604 case FUNCTION_DECL:
19605 /* What we would really like to do here is to filter out all mere
19606 file-scope declarations of file-scope functions which are never
19607 referenced later within this translation unit (and keep all of ones
19608 that *are* referenced later on) but we aren't clairvoyant, so we have
19609 no idea which functions will be referenced in the future (i.e. later
19610 on within the current translation unit). So here we just ignore all
19611 file-scope function declarations which are not also definitions. If
19612 and when the debugger needs to know something about these functions,
19613 it will have to hunt around and find the DWARF information associated
19614 with the definition of the function.
19615
19616 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19617 nodes represent definitions and which ones represent mere
19618 declarations. We have to check DECL_INITIAL instead. That's because
19619 the C front-end supports some weird semantics for "extern inline"
19620 function definitions. These can get inlined within the current
19621 translation unit (and thus, we need to generate Dwarf info for their
19622 abstract instances so that the Dwarf info for the concrete inlined
19623 instances can have something to refer to) but the compiler never
19624 generates any out-of-lines instances of such things (despite the fact
19625 that they *are* definitions).
19626
19627 The important point is that the C front-end marks these "extern
19628 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19629 them anyway. Note that the C++ front-end also plays some similar games
19630 for inline function definitions appearing within include files which
19631 also contain `#pragma interface' pragmas.
19632
19633 If we are called from dwarf2out_abstract_function output a DIE
19634 anyway. We can end up here this way with early inlining and LTO
19635 where the inlined function is output in a different LTRANS unit
19636 or not at all. */
19637 if (DECL_INITIAL (decl) == NULL_TREE
19638 && ! DECL_ABSTRACT (decl))
19639 return;
19640
19641 /* If we're a nested function, initially use a parent of NULL; if we're
19642 a plain function, this will be fixed up in decls_for_scope. If
19643 we're a method, it will be ignored, since we already have a DIE. */
19644 if (decl_function_context (decl)
19645 /* But if we're in terse mode, we don't care about scope. */
19646 && debug_info_level > DINFO_LEVEL_TERSE)
19647 context_die = NULL;
19648 break;
19649
19650 case VAR_DECL:
19651 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19652 declaration and if the declaration was never even referenced from
19653 within this entire compilation unit. We suppress these DIEs in
19654 order to save space in the .debug section (by eliminating entries
19655 which are probably useless). Note that we must not suppress
19656 block-local extern declarations (whether used or not) because that
19657 would screw-up the debugger's name lookup mechanism and cause it to
19658 miss things which really ought to be in scope at a given point. */
19659 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19660 return;
19661
19662 /* For local statics lookup proper context die. */
19663 if (TREE_STATIC (decl)
19664 && DECL_CONTEXT (decl)
19665 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
19666 context_die = lookup_decl_die (DECL_CONTEXT (decl));
19667
19668 /* If we are in terse mode, don't generate any DIEs to represent any
19669 variable declarations or definitions. */
19670 if (debug_info_level <= DINFO_LEVEL_TERSE)
19671 return;
19672 break;
19673
19674 case CONST_DECL:
19675 if (debug_info_level <= DINFO_LEVEL_TERSE)
19676 return;
19677 if (!is_fortran () && !is_ada ())
19678 return;
19679 if (TREE_STATIC (decl) && decl_function_context (decl))
19680 context_die = lookup_decl_die (DECL_CONTEXT (decl));
19681 break;
19682
19683 case NAMESPACE_DECL:
19684 case IMPORTED_DECL:
19685 if (debug_info_level <= DINFO_LEVEL_TERSE)
19686 return;
19687 if (lookup_decl_die (decl) != NULL)
19688 return;
19689 break;
19690
19691 case TYPE_DECL:
19692 /* Don't emit stubs for types unless they are needed by other DIEs. */
19693 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19694 return;
19695
19696 /* Don't bother trying to generate any DIEs to represent any of the
19697 normal built-in types for the language we are compiling. */
19698 if (DECL_IS_BUILTIN (decl))
19699 return;
19700
19701 /* If we are in terse mode, don't generate any DIEs for types. */
19702 if (debug_info_level <= DINFO_LEVEL_TERSE)
19703 return;
19704
19705 /* If we're a function-scope tag, initially use a parent of NULL;
19706 this will be fixed up in decls_for_scope. */
19707 if (decl_function_context (decl))
19708 context_die = NULL;
19709
19710 break;
19711
19712 default:
19713 return;
19714 }
19715
19716 gen_decl_die (decl, NULL, context_die);
19717 }
19718
19719 /* Write the debugging output for DECL. */
19720
19721 static void
19722 dwarf2out_function_decl (tree decl)
19723 {
19724 dwarf2out_decl (decl);
19725 call_arg_locations = NULL;
19726 call_arg_loc_last = NULL;
19727 call_site_count = -1;
19728 tail_call_site_count = -1;
19729 VEC_free (dw_die_ref, heap, block_map);
19730 htab_empty (decl_loc_table);
19731 htab_empty (cached_dw_loc_list_table);
19732 }
19733
19734 /* Output a marker (i.e. a label) for the beginning of the generated code for
19735 a lexical block. */
19736
19737 static void
19738 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19739 unsigned int blocknum)
19740 {
19741 switch_to_section (current_function_section ());
19742 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19743 }
19744
19745 /* Output a marker (i.e. a label) for the end of the generated code for a
19746 lexical block. */
19747
19748 static void
19749 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19750 {
19751 switch_to_section (current_function_section ());
19752 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19753 }
19754
19755 /* Returns nonzero if it is appropriate not to emit any debugging
19756 information for BLOCK, because it doesn't contain any instructions.
19757
19758 Don't allow this for blocks with nested functions or local classes
19759 as we would end up with orphans, and in the presence of scheduling
19760 we may end up calling them anyway. */
19761
19762 static bool
19763 dwarf2out_ignore_block (const_tree block)
19764 {
19765 tree decl;
19766 unsigned int i;
19767
19768 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19769 if (TREE_CODE (decl) == FUNCTION_DECL
19770 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19771 return 0;
19772 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19773 {
19774 decl = BLOCK_NONLOCALIZED_VAR (block, i);
19775 if (TREE_CODE (decl) == FUNCTION_DECL
19776 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19777 return 0;
19778 }
19779
19780 return 1;
19781 }
19782
19783 /* Hash table routines for file_hash. */
19784
19785 static int
19786 file_table_eq (const void *p1_p, const void *p2_p)
19787 {
19788 const struct dwarf_file_data *const p1 =
19789 (const struct dwarf_file_data *) p1_p;
19790 const char *const p2 = (const char *) p2_p;
19791 return filename_cmp (p1->filename, p2) == 0;
19792 }
19793
19794 static hashval_t
19795 file_table_hash (const void *p_p)
19796 {
19797 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19798 return htab_hash_string (p->filename);
19799 }
19800
19801 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19802 dwarf2out.c) and return its "index". The index of each (known) filename is
19803 just a unique number which is associated with only that one filename. We
19804 need such numbers for the sake of generating labels (in the .debug_sfnames
19805 section) and references to those files numbers (in the .debug_srcinfo
19806 and.debug_macinfo sections). If the filename given as an argument is not
19807 found in our current list, add it to the list and assign it the next
19808 available unique index number. In order to speed up searches, we remember
19809 the index of the filename was looked up last. This handles the majority of
19810 all searches. */
19811
19812 static struct dwarf_file_data *
19813 lookup_filename (const char *file_name)
19814 {
19815 void ** slot;
19816 struct dwarf_file_data * created;
19817
19818 /* Check to see if the file name that was searched on the previous
19819 call matches this file name. If so, return the index. */
19820 if (file_table_last_lookup
19821 && (file_name == file_table_last_lookup->filename
19822 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
19823 return file_table_last_lookup;
19824
19825 /* Didn't match the previous lookup, search the table. */
19826 slot = htab_find_slot_with_hash (file_table, file_name,
19827 htab_hash_string (file_name), INSERT);
19828 if (*slot)
19829 return (struct dwarf_file_data *) *slot;
19830
19831 created = ggc_alloc_dwarf_file_data ();
19832 created->filename = file_name;
19833 created->emitted_number = 0;
19834 *slot = created;
19835 return created;
19836 }
19837
19838 /* If the assembler will construct the file table, then translate the compiler
19839 internal file table number into the assembler file table number, and emit
19840 a .file directive if we haven't already emitted one yet. The file table
19841 numbers are different because we prune debug info for unused variables and
19842 types, which may include filenames. */
19843
19844 static int
19845 maybe_emit_file (struct dwarf_file_data * fd)
19846 {
19847 if (! fd->emitted_number)
19848 {
19849 if (last_emitted_file)
19850 fd->emitted_number = last_emitted_file->emitted_number + 1;
19851 else
19852 fd->emitted_number = 1;
19853 last_emitted_file = fd;
19854
19855 if (DWARF2_ASM_LINE_DEBUG_INFO)
19856 {
19857 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
19858 output_quoted_string (asm_out_file,
19859 remap_debug_filename (fd->filename));
19860 fputc ('\n', asm_out_file);
19861 }
19862 }
19863
19864 return fd->emitted_number;
19865 }
19866
19867 /* Schedule generation of a DW_AT_const_value attribute to DIE.
19868 That generation should happen after function debug info has been
19869 generated. The value of the attribute is the constant value of ARG. */
19870
19871 static void
19872 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
19873 {
19874 die_arg_entry entry;
19875
19876 if (!die || !arg)
19877 return;
19878
19879 if (!tmpl_value_parm_die_table)
19880 tmpl_value_parm_die_table
19881 = VEC_alloc (die_arg_entry, gc, 32);
19882
19883 entry.die = die;
19884 entry.arg = arg;
19885 VEC_safe_push (die_arg_entry, gc,
19886 tmpl_value_parm_die_table,
19887 entry);
19888 }
19889
19890 /* Return TRUE if T is an instance of generic type, FALSE
19891 otherwise. */
19892
19893 static bool
19894 generic_type_p (tree t)
19895 {
19896 if (t == NULL_TREE || !TYPE_P (t))
19897 return false;
19898 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
19899 }
19900
19901 /* Schedule the generation of the generic parameter dies for the
19902 instance of generic type T. The proper generation itself is later
19903 done by gen_scheduled_generic_parms_dies. */
19904
19905 static void
19906 schedule_generic_params_dies_gen (tree t)
19907 {
19908 if (!generic_type_p (t))
19909 return;
19910
19911 if (generic_type_instances == NULL)
19912 generic_type_instances = VEC_alloc (tree, gc, 256);
19913
19914 VEC_safe_push (tree, gc, generic_type_instances, t);
19915 }
19916
19917 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
19918 by append_entry_to_tmpl_value_parm_die_table. This function must
19919 be called after function DIEs have been generated. */
19920
19921 static void
19922 gen_remaining_tmpl_value_param_die_attribute (void)
19923 {
19924 if (tmpl_value_parm_die_table)
19925 {
19926 unsigned i;
19927 die_arg_entry *e;
19928
19929 FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
19930 tree_add_const_value_attribute (e->die, e->arg);
19931 }
19932 }
19933
19934 /* Generate generic parameters DIEs for instances of generic types
19935 that have been previously scheduled by
19936 schedule_generic_params_dies_gen. This function must be called
19937 after all the types of the CU have been laid out. */
19938
19939 static void
19940 gen_scheduled_generic_parms_dies (void)
19941 {
19942 unsigned i;
19943 tree t;
19944
19945 if (generic_type_instances == NULL)
19946 return;
19947
19948 FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
19949 gen_generic_params_dies (t);
19950 }
19951
19952
19953 /* Replace DW_AT_name for the decl with name. */
19954
19955 static void
19956 dwarf2out_set_name (tree decl, tree name)
19957 {
19958 dw_die_ref die;
19959 dw_attr_ref attr;
19960 const char *dname;
19961
19962 die = TYPE_SYMTAB_DIE (decl);
19963 if (!die)
19964 return;
19965
19966 dname = dwarf2_name (name, 0);
19967 if (!dname)
19968 return;
19969
19970 attr = get_AT (die, DW_AT_name);
19971 if (attr)
19972 {
19973 struct indirect_string_node *node;
19974
19975 node = find_AT_string (dname);
19976 /* replace the string. */
19977 attr->dw_attr_val.v.val_str = node;
19978 }
19979
19980 else
19981 add_name_attribute (die, dname);
19982 }
19983
19984 /* Called by the final INSN scan whenever we see a var location. We
19985 use it to drop labels in the right places, and throw the location in
19986 our lookup table. */
19987
19988 static void
19989 dwarf2out_var_location (rtx loc_note)
19990 {
19991 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
19992 struct var_loc_node *newloc;
19993 rtx next_real, next_note;
19994 static const char *last_label;
19995 static const char *last_postcall_label;
19996 static bool last_in_cold_section_p;
19997 static rtx expected_next_loc_note;
19998 tree decl;
19999 bool var_loc_p;
20000
20001 if (!NOTE_P (loc_note))
20002 {
20003 if (CALL_P (loc_note))
20004 {
20005 call_site_count++;
20006 if (SIBLING_CALL_P (loc_note))
20007 tail_call_site_count++;
20008 }
20009 return;
20010 }
20011
20012 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20013 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20014 return;
20015
20016 /* Optimize processing a large consecutive sequence of location
20017 notes so we don't spend too much time in next_real_insn. If the
20018 next insn is another location note, remember the next_real_insn
20019 calculation for next time. */
20020 next_real = cached_next_real_insn;
20021 if (next_real)
20022 {
20023 if (expected_next_loc_note != loc_note)
20024 next_real = NULL_RTX;
20025 }
20026
20027 next_note = NEXT_INSN (loc_note);
20028 if (! next_note
20029 || INSN_DELETED_P (next_note)
20030 || GET_CODE (next_note) != NOTE
20031 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20032 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20033 next_note = NULL_RTX;
20034
20035 if (! next_real)
20036 next_real = next_real_insn (loc_note);
20037
20038 if (next_note)
20039 {
20040 expected_next_loc_note = next_note;
20041 cached_next_real_insn = next_real;
20042 }
20043 else
20044 cached_next_real_insn = NULL_RTX;
20045
20046 /* If there are no instructions which would be affected by this note,
20047 don't do anything. */
20048 if (var_loc_p
20049 && next_real == NULL_RTX
20050 && !NOTE_DURING_CALL_P (loc_note))
20051 return;
20052
20053 if (next_real == NULL_RTX)
20054 next_real = get_last_insn ();
20055
20056 /* If there were any real insns between note we processed last time
20057 and this note (or if it is the first note), clear
20058 last_{,postcall_}label so that they are not reused this time. */
20059 if (last_var_location_insn == NULL_RTX
20060 || last_var_location_insn != next_real
20061 || last_in_cold_section_p != in_cold_section_p)
20062 {
20063 last_label = NULL;
20064 last_postcall_label = NULL;
20065 }
20066
20067 if (var_loc_p)
20068 {
20069 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20070 newloc = add_var_loc_to_decl (decl, loc_note,
20071 NOTE_DURING_CALL_P (loc_note)
20072 ? last_postcall_label : last_label);
20073 if (newloc == NULL)
20074 return;
20075 }
20076 else
20077 {
20078 decl = NULL_TREE;
20079 newloc = NULL;
20080 }
20081
20082 /* If there were no real insns between note we processed last time
20083 and this note, use the label we emitted last time. Otherwise
20084 create a new label and emit it. */
20085 if (last_label == NULL)
20086 {
20087 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20088 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20089 loclabel_num++;
20090 last_label = ggc_strdup (loclabel);
20091 }
20092
20093 if (!var_loc_p)
20094 {
20095 struct call_arg_loc_node *ca_loc
20096 = ggc_alloc_cleared_call_arg_loc_node ();
20097 rtx prev = prev_real_insn (loc_note), x;
20098 ca_loc->call_arg_loc_note = loc_note;
20099 ca_loc->next = NULL;
20100 ca_loc->label = last_label;
20101 gcc_assert (prev
20102 && (CALL_P (prev)
20103 || (NONJUMP_INSN_P (prev)
20104 && GET_CODE (PATTERN (prev)) == SEQUENCE
20105 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20106 if (!CALL_P (prev))
20107 prev = XVECEXP (PATTERN (prev), 0, 0);
20108 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20109 x = get_call_rtx_from (PATTERN (prev));
20110 if (x)
20111 {
20112 x = XEXP (XEXP (x, 0), 0);
20113 if (GET_CODE (x) == SYMBOL_REF
20114 && SYMBOL_REF_DECL (x)
20115 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20116 ca_loc->symbol_ref = x;
20117 }
20118 ca_loc->block = insn_scope (prev);
20119 if (call_arg_locations)
20120 call_arg_loc_last->next = ca_loc;
20121 else
20122 call_arg_locations = ca_loc;
20123 call_arg_loc_last = ca_loc;
20124 }
20125 else if (!NOTE_DURING_CALL_P (loc_note))
20126 newloc->label = last_label;
20127 else
20128 {
20129 if (!last_postcall_label)
20130 {
20131 sprintf (loclabel, "%s-1", last_label);
20132 last_postcall_label = ggc_strdup (loclabel);
20133 }
20134 newloc->label = last_postcall_label;
20135 }
20136
20137 last_var_location_insn = next_real;
20138 last_in_cold_section_p = in_cold_section_p;
20139 }
20140
20141 /* Note in one location list that text section has changed. */
20142
20143 static int
20144 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20145 {
20146 var_loc_list *list = (var_loc_list *) *slot;
20147 if (list->first)
20148 list->last_before_switch
20149 = list->last->next ? list->last->next : list->last;
20150 return 1;
20151 }
20152
20153 /* Note in all location lists that text section has changed. */
20154
20155 static void
20156 var_location_switch_text_section (void)
20157 {
20158 if (decl_loc_table == NULL)
20159 return;
20160
20161 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20162 }
20163
20164 /* Create a new line number table. */
20165
20166 static dw_line_info_table *
20167 new_line_info_table (void)
20168 {
20169 dw_line_info_table *table;
20170
20171 table = ggc_alloc_cleared_dw_line_info_table_struct ();
20172 table->file_num = 1;
20173 table->line_num = 1;
20174 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20175
20176 return table;
20177 }
20178
20179 /* Lookup the "current" table into which we emit line info, so
20180 that we don't have to do it for every source line. */
20181
20182 static void
20183 set_cur_line_info_table (section *sec)
20184 {
20185 dw_line_info_table *table;
20186
20187 if (sec == text_section)
20188 table = text_section_line_info;
20189 else if (sec == cold_text_section)
20190 {
20191 table = cold_text_section_line_info;
20192 if (!table)
20193 {
20194 cold_text_section_line_info = table = new_line_info_table ();
20195 table->end_label = cold_end_label;
20196 }
20197 }
20198 else
20199 {
20200 const char *end_label;
20201
20202 if (flag_reorder_blocks_and_partition)
20203 {
20204 if (in_cold_section_p)
20205 end_label = crtl->subsections.cold_section_end_label;
20206 else
20207 end_label = crtl->subsections.hot_section_end_label;
20208 }
20209 else
20210 {
20211 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20212 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20213 current_function_funcdef_no);
20214 end_label = ggc_strdup (label);
20215 }
20216
20217 table = new_line_info_table ();
20218 table->end_label = end_label;
20219
20220 VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20221 }
20222
20223 if (DWARF2_ASM_LINE_DEBUG_INFO)
20224 table->is_stmt = (cur_line_info_table
20225 ? cur_line_info_table->is_stmt
20226 : DWARF_LINE_DEFAULT_IS_STMT_START);
20227 cur_line_info_table = table;
20228 }
20229
20230
20231 /* We need to reset the locations at the beginning of each
20232 function. We can't do this in the end_function hook, because the
20233 declarations that use the locations won't have been output when
20234 that hook is called. Also compute have_multiple_function_sections here. */
20235
20236 static void
20237 dwarf2out_begin_function (tree fun)
20238 {
20239 section *sec = function_section (fun);
20240
20241 if (sec != text_section)
20242 have_multiple_function_sections = true;
20243
20244 if (flag_reorder_blocks_and_partition && !cold_text_section)
20245 {
20246 gcc_assert (current_function_decl == fun);
20247 cold_text_section = unlikely_text_section ();
20248 switch_to_section (cold_text_section);
20249 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20250 switch_to_section (sec);
20251 }
20252
20253 dwarf2out_note_section_used ();
20254 call_site_count = 0;
20255 tail_call_site_count = 0;
20256
20257 set_cur_line_info_table (sec);
20258 }
20259
20260 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
20261
20262 static void
20263 push_dw_line_info_entry (dw_line_info_table *table,
20264 enum dw_line_info_opcode opcode, unsigned int val)
20265 {
20266 dw_line_info_entry e;
20267 e.opcode = opcode;
20268 e.val = val;
20269 VEC_safe_push (dw_line_info_entry, gc, table->entries, e);
20270 }
20271
20272 /* Output a label to mark the beginning of a source code line entry
20273 and record information relating to this source line, in
20274 'line_info_table' for later output of the .debug_line section. */
20275 /* ??? The discriminator parameter ought to be unsigned. */
20276
20277 static void
20278 dwarf2out_source_line (unsigned int line, const char *filename,
20279 int discriminator, bool is_stmt)
20280 {
20281 unsigned int file_num;
20282 dw_line_info_table *table;
20283
20284 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20285 return;
20286
20287 /* The discriminator column was added in dwarf4. Simplify the below
20288 by simply removing it if we're not supposed to output it. */
20289 if (dwarf_version < 4 && dwarf_strict)
20290 discriminator = 0;
20291
20292 table = cur_line_info_table;
20293 file_num = maybe_emit_file (lookup_filename (filename));
20294
20295 /* ??? TODO: Elide duplicate line number entries. Traditionally,
20296 the debugger has used the second (possibly duplicate) line number
20297 at the beginning of the function to mark the end of the prologue.
20298 We could eliminate any other duplicates within the function. For
20299 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20300 that second line number entry. */
20301 /* Recall that this end-of-prologue indication is *not* the same thing
20302 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
20303 to which the hook corresponds, follows the last insn that was
20304 emitted by gen_prologue. What we need is to precede the first insn
20305 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20306 insn that corresponds to something the user wrote. These may be
20307 very different locations once scheduling is enabled. */
20308
20309 if (0 && file_num == table->file_num
20310 && line == table->line_num
20311 && discriminator == table->discrim_num
20312 && is_stmt == table->is_stmt)
20313 return;
20314
20315 switch_to_section (current_function_section ());
20316
20317 /* If requested, emit something human-readable. */
20318 if (flag_debug_asm)
20319 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20320
20321 if (DWARF2_ASM_LINE_DEBUG_INFO)
20322 {
20323 /* Emit the .loc directive understood by GNU as. */
20324 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
20325 file_num, line, is_stmt, discriminator */
20326 fputs ("\t.loc ", asm_out_file);
20327 fprint_ul (asm_out_file, file_num);
20328 putc (' ', asm_out_file);
20329 fprint_ul (asm_out_file, line);
20330 putc (' ', asm_out_file);
20331 putc ('0', asm_out_file);
20332
20333 if (is_stmt != table->is_stmt)
20334 {
20335 fputs (" is_stmt ", asm_out_file);
20336 putc (is_stmt ? '1' : '0', asm_out_file);
20337 }
20338 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20339 {
20340 gcc_assert (discriminator > 0);
20341 fputs (" discriminator ", asm_out_file);
20342 fprint_ul (asm_out_file, (unsigned long) discriminator);
20343 }
20344 putc ('\n', asm_out_file);
20345 }
20346 else
20347 {
20348 unsigned int label_num = ++line_info_label_num;
20349
20350 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20351
20352 push_dw_line_info_entry (table, LI_set_address, label_num);
20353 if (file_num != table->file_num)
20354 push_dw_line_info_entry (table, LI_set_file, file_num);
20355 if (discriminator != table->discrim_num)
20356 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20357 if (is_stmt != table->is_stmt)
20358 push_dw_line_info_entry (table, LI_negate_stmt, 0);
20359 push_dw_line_info_entry (table, LI_set_line, line);
20360 }
20361
20362 table->file_num = file_num;
20363 table->line_num = line;
20364 table->discrim_num = discriminator;
20365 table->is_stmt = is_stmt;
20366 table->in_use = true;
20367 }
20368
20369 /* Record the beginning of a new source file. */
20370
20371 static void
20372 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20373 {
20374 if (flag_eliminate_dwarf2_dups)
20375 {
20376 /* Record the beginning of the file for break_out_includes. */
20377 dw_die_ref bincl_die;
20378
20379 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20380 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20381 }
20382
20383 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20384 {
20385 macinfo_entry e;
20386 e.code = DW_MACINFO_start_file;
20387 e.lineno = lineno;
20388 e.info = ggc_strdup (filename);
20389 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20390 }
20391 }
20392
20393 /* Record the end of a source file. */
20394
20395 static void
20396 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20397 {
20398 if (flag_eliminate_dwarf2_dups)
20399 /* Record the end of the file for break_out_includes. */
20400 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20401
20402 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20403 {
20404 macinfo_entry e;
20405 e.code = DW_MACINFO_end_file;
20406 e.lineno = lineno;
20407 e.info = NULL;
20408 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20409 }
20410 }
20411
20412 /* Called from debug_define in toplev.c. The `buffer' parameter contains
20413 the tail part of the directive line, i.e. the part which is past the
20414 initial whitespace, #, whitespace, directive-name, whitespace part. */
20415
20416 static void
20417 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20418 const char *buffer ATTRIBUTE_UNUSED)
20419 {
20420 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20421 {
20422 macinfo_entry e;
20423 /* Insert a dummy first entry to be able to optimize the whole
20424 predefined macro block using DW_MACRO_GNU_transparent_include. */
20425 if (VEC_empty (macinfo_entry, macinfo_table) && lineno <= 1)
20426 {
20427 e.code = 0;
20428 e.lineno = 0;
20429 e.info = NULL;
20430 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20431 }
20432 e.code = DW_MACINFO_define;
20433 e.lineno = lineno;
20434 e.info = ggc_strdup (buffer);
20435 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20436 }
20437 }
20438
20439 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
20440 the tail part of the directive line, i.e. the part which is past the
20441 initial whitespace, #, whitespace, directive-name, whitespace part. */
20442
20443 static void
20444 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20445 const char *buffer ATTRIBUTE_UNUSED)
20446 {
20447 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20448 {
20449 macinfo_entry e;
20450 /* Insert a dummy first entry to be able to optimize the whole
20451 predefined macro block using DW_MACRO_GNU_transparent_include. */
20452 if (VEC_empty (macinfo_entry, macinfo_table) && lineno <= 1)
20453 {
20454 e.code = 0;
20455 e.lineno = 0;
20456 e.info = NULL;
20457 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20458 }
20459 e.code = DW_MACINFO_undef;
20460 e.lineno = lineno;
20461 e.info = ggc_strdup (buffer);
20462 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20463 }
20464 }
20465
20466 /* Routines to manipulate hash table of CUs. */
20467
20468 static hashval_t
20469 htab_macinfo_hash (const void *of)
20470 {
20471 const macinfo_entry *const entry =
20472 (const macinfo_entry *) of;
20473
20474 return htab_hash_string (entry->info);
20475 }
20476
20477 static int
20478 htab_macinfo_eq (const void *of1, const void *of2)
20479 {
20480 const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
20481 const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
20482
20483 return !strcmp (entry1->info, entry2->info);
20484 }
20485
20486 /* Output a single .debug_macinfo entry. */
20487
20488 static void
20489 output_macinfo_op (macinfo_entry *ref)
20490 {
20491 int file_num;
20492 size_t len;
20493 struct indirect_string_node *node;
20494 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20495 struct dwarf_file_data *fd;
20496
20497 switch (ref->code)
20498 {
20499 case DW_MACINFO_start_file:
20500 fd = lookup_filename (ref->info);
20501 file_num = maybe_emit_file (fd);
20502 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20503 dw2_asm_output_data_uleb128 (ref->lineno,
20504 "Included from line number %lu",
20505 (unsigned long) ref->lineno);
20506 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20507 break;
20508 case DW_MACINFO_end_file:
20509 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20510 break;
20511 case DW_MACINFO_define:
20512 case DW_MACINFO_undef:
20513 len = strlen (ref->info) + 1;
20514 if (!dwarf_strict
20515 && len > DWARF_OFFSET_SIZE
20516 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
20517 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
20518 {
20519 ref->code = ref->code == DW_MACINFO_define
20520 ? DW_MACRO_GNU_define_indirect
20521 : DW_MACRO_GNU_undef_indirect;
20522 output_macinfo_op (ref);
20523 return;
20524 }
20525 dw2_asm_output_data (1, ref->code,
20526 ref->code == DW_MACINFO_define
20527 ? "Define macro" : "Undefine macro");
20528 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20529 (unsigned long) ref->lineno);
20530 dw2_asm_output_nstring (ref->info, -1, "The macro");
20531 break;
20532 case DW_MACRO_GNU_define_indirect:
20533 case DW_MACRO_GNU_undef_indirect:
20534 node = find_AT_string (ref->info);
20535 if (node->form != DW_FORM_strp)
20536 {
20537 char label[32];
20538 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
20539 ++dw2_string_counter;
20540 node->label = xstrdup (label);
20541 node->form = DW_FORM_strp;
20542 }
20543 dw2_asm_output_data (1, ref->code,
20544 ref->code == DW_MACRO_GNU_define_indirect
20545 ? "Define macro indirect"
20546 : "Undefine macro indirect");
20547 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20548 (unsigned long) ref->lineno);
20549 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
20550 debug_str_section, "The macro: \"%s\"",
20551 ref->info);
20552 break;
20553 case DW_MACRO_GNU_transparent_include:
20554 dw2_asm_output_data (1, ref->code, "Transparent include");
20555 ASM_GENERATE_INTERNAL_LABEL (label,
20556 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
20557 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
20558 break;
20559 default:
20560 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20561 ASM_COMMENT_START, (unsigned long) ref->code);
20562 break;
20563 }
20564 }
20565
20566 /* Attempt to make a sequence of define/undef macinfo ops shareable with
20567 other compilation unit .debug_macinfo sections. IDX is the first
20568 index of a define/undef, return the number of ops that should be
20569 emitted in a comdat .debug_macinfo section and emit
20570 a DW_MACRO_GNU_transparent_include entry referencing it.
20571 If the define/undef entry should be emitted normally, return 0. */
20572
20573 static unsigned
20574 optimize_macinfo_range (unsigned int idx, VEC (macinfo_entry, gc) *files,
20575 htab_t *macinfo_htab)
20576 {
20577 macinfo_entry *first, *second, *cur, *inc;
20578 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
20579 unsigned char checksum[16];
20580 struct md5_ctx ctx;
20581 char *grp_name, *tail;
20582 const char *base;
20583 unsigned int i, count, encoded_filename_len, linebuf_len;
20584 void **slot;
20585
20586 first = &VEC_index (macinfo_entry, macinfo_table, idx);
20587 second = &VEC_index (macinfo_entry, macinfo_table, idx + 1);
20588
20589 /* Optimize only if there are at least two consecutive define/undef ops,
20590 and either all of them are before first DW_MACINFO_start_file
20591 with lineno {0,1} (i.e. predefined macro block), or all of them are
20592 in some included header file. */
20593 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
20594 return 0;
20595 if (VEC_empty (macinfo_entry, files))
20596 {
20597 if (first->lineno > 1 || second->lineno > 1)
20598 return 0;
20599 }
20600 else if (first->lineno == 0)
20601 return 0;
20602
20603 /* Find the last define/undef entry that can be grouped together
20604 with first and at the same time compute md5 checksum of their
20605 codes, linenumbers and strings. */
20606 md5_init_ctx (&ctx);
20607 for (i = idx; VEC_iterate (macinfo_entry, macinfo_table, i, cur); i++)
20608 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
20609 break;
20610 else if (VEC_empty (macinfo_entry, files) && cur->lineno > 1)
20611 break;
20612 else
20613 {
20614 unsigned char code = cur->code;
20615 md5_process_bytes (&code, 1, &ctx);
20616 checksum_uleb128 (cur->lineno, &ctx);
20617 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
20618 }
20619 md5_finish_ctx (&ctx, checksum);
20620 count = i - idx;
20621
20622 /* From the containing include filename (if any) pick up just
20623 usable characters from its basename. */
20624 if (VEC_empty (macinfo_entry, files))
20625 base = "";
20626 else
20627 base = lbasename (VEC_last (macinfo_entry, files).info);
20628 for (encoded_filename_len = 0, i = 0; base[i]; i++)
20629 if (ISIDNUM (base[i]) || base[i] == '.')
20630 encoded_filename_len++;
20631 /* Count . at the end. */
20632 if (encoded_filename_len)
20633 encoded_filename_len++;
20634
20635 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
20636 linebuf_len = strlen (linebuf);
20637
20638 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
20639 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
20640 + 16 * 2 + 1);
20641 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
20642 tail = grp_name + 4;
20643 if (encoded_filename_len)
20644 {
20645 for (i = 0; base[i]; i++)
20646 if (ISIDNUM (base[i]) || base[i] == '.')
20647 *tail++ = base[i];
20648 *tail++ = '.';
20649 }
20650 memcpy (tail, linebuf, linebuf_len);
20651 tail += linebuf_len;
20652 *tail++ = '.';
20653 for (i = 0; i < 16; i++)
20654 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
20655
20656 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
20657 in the empty vector entry before the first define/undef. */
20658 inc = &VEC_index (macinfo_entry, macinfo_table, idx - 1);
20659 inc->code = DW_MACRO_GNU_transparent_include;
20660 inc->lineno = 0;
20661 inc->info = ggc_strdup (grp_name);
20662 if (*macinfo_htab == NULL)
20663 *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
20664 /* Avoid emitting duplicates. */
20665 slot = htab_find_slot (*macinfo_htab, inc, INSERT);
20666 if (*slot != NULL)
20667 {
20668 inc->code = 0;
20669 inc->info = NULL;
20670 /* If such an entry has been used before, just emit
20671 a DW_MACRO_GNU_transparent_include op. */
20672 inc = (macinfo_entry *) *slot;
20673 output_macinfo_op (inc);
20674 /* And clear all macinfo_entry in the range to avoid emitting them
20675 in the second pass. */
20676 for (i = idx;
20677 VEC_iterate (macinfo_entry, macinfo_table, i, cur)
20678 && i < idx + count;
20679 i++)
20680 {
20681 cur->code = 0;
20682 cur->info = NULL;
20683 }
20684 }
20685 else
20686 {
20687 *slot = inc;
20688 inc->lineno = htab_elements (*macinfo_htab);
20689 output_macinfo_op (inc);
20690 }
20691 return count;
20692 }
20693
20694 /* Output macinfo section(s). */
20695
20696 static void
20697 output_macinfo (void)
20698 {
20699 unsigned i;
20700 unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20701 macinfo_entry *ref;
20702 VEC (macinfo_entry, gc) *files = NULL;
20703 htab_t macinfo_htab = NULL;
20704
20705 if (! length)
20706 return;
20707
20708 /* output_macinfo* uses these interchangeably. */
20709 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
20710 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
20711 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
20712 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
20713
20714 /* For .debug_macro emit the section header. */
20715 if (!dwarf_strict)
20716 {
20717 dw2_asm_output_data (2, 4, "DWARF macro version number");
20718 if (DWARF_OFFSET_SIZE == 8)
20719 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
20720 else
20721 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
20722 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_section_label,
20723 debug_line_section, NULL);
20724 }
20725
20726 /* In the first loop, it emits the primary .debug_macinfo section
20727 and after each emitted op the macinfo_entry is cleared.
20728 If a longer range of define/undef ops can be optimized using
20729 DW_MACRO_GNU_transparent_include, the
20730 DW_MACRO_GNU_transparent_include op is emitted and kept in
20731 the vector before the first define/undef in the range and the
20732 whole range of define/undef ops is not emitted and kept. */
20733 for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20734 {
20735 switch (ref->code)
20736 {
20737 case DW_MACINFO_start_file:
20738 VEC_safe_push (macinfo_entry, gc, files, *ref);
20739 break;
20740 case DW_MACINFO_end_file:
20741 if (!VEC_empty (macinfo_entry, files))
20742 VEC_pop (macinfo_entry, files);
20743 break;
20744 case DW_MACINFO_define:
20745 case DW_MACINFO_undef:
20746 if (!dwarf_strict
20747 && HAVE_COMDAT_GROUP
20748 && VEC_length (macinfo_entry, files) != 1
20749 && i > 0
20750 && i + 1 < length
20751 && VEC_index (macinfo_entry, macinfo_table, i - 1).code == 0)
20752 {
20753 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
20754 if (count)
20755 {
20756 i += count - 1;
20757 continue;
20758 }
20759 }
20760 break;
20761 case 0:
20762 /* A dummy entry may be inserted at the beginning to be able
20763 to optimize the whole block of predefined macros. */
20764 if (i == 0)
20765 continue;
20766 default:
20767 break;
20768 }
20769 output_macinfo_op (ref);
20770 ref->info = NULL;
20771 ref->code = 0;
20772 }
20773
20774 if (macinfo_htab == NULL)
20775 return;
20776
20777 htab_delete (macinfo_htab);
20778
20779 /* If any DW_MACRO_GNU_transparent_include were used, on those
20780 DW_MACRO_GNU_transparent_include entries terminate the
20781 current chain and switch to a new comdat .debug_macinfo
20782 section and emit the define/undef entries within it. */
20783 for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20784 switch (ref->code)
20785 {
20786 case 0:
20787 continue;
20788 case DW_MACRO_GNU_transparent_include:
20789 {
20790 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20791 tree comdat_key = get_identifier (ref->info);
20792 /* Terminate the previous .debug_macinfo section. */
20793 dw2_asm_output_data (1, 0, "End compilation unit");
20794 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
20795 SECTION_DEBUG
20796 | SECTION_LINKONCE,
20797 comdat_key);
20798 ASM_GENERATE_INTERNAL_LABEL (label,
20799 DEBUG_MACRO_SECTION_LABEL,
20800 ref->lineno);
20801 ASM_OUTPUT_LABEL (asm_out_file, label);
20802 ref->code = 0;
20803 ref->info = NULL;
20804 dw2_asm_output_data (2, 4, "DWARF macro version number");
20805 if (DWARF_OFFSET_SIZE == 8)
20806 dw2_asm_output_data (1, 1, "Flags: 64-bit");
20807 else
20808 dw2_asm_output_data (1, 0, "Flags: 32-bit");
20809 }
20810 break;
20811 case DW_MACINFO_define:
20812 case DW_MACINFO_undef:
20813 output_macinfo_op (ref);
20814 ref->code = 0;
20815 ref->info = NULL;
20816 break;
20817 default:
20818 gcc_unreachable ();
20819 }
20820 }
20821
20822 /* Set up for Dwarf output at the start of compilation. */
20823
20824 static void
20825 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20826 {
20827 /* Allocate the file_table. */
20828 file_table = htab_create_ggc (50, file_table_hash,
20829 file_table_eq, NULL);
20830
20831 /* Allocate the decl_die_table. */
20832 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20833 decl_die_table_eq, NULL);
20834
20835 /* Allocate the decl_loc_table. */
20836 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20837 decl_loc_table_eq, NULL);
20838
20839 /* Allocate the cached_dw_loc_list_table. */
20840 cached_dw_loc_list_table
20841 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
20842 cached_dw_loc_list_table_eq, NULL);
20843
20844 /* Allocate the initial hunk of the decl_scope_table. */
20845 decl_scope_table = VEC_alloc (tree, gc, 256);
20846
20847 /* Allocate the initial hunk of the abbrev_die_table. */
20848 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
20849 (ABBREV_DIE_TABLE_INCREMENT);
20850 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20851 /* Zero-th entry is allocated, but unused. */
20852 abbrev_die_table_in_use = 1;
20853
20854 /* Allocate the pubtypes and pubnames vectors. */
20855 pubname_table = VEC_alloc (pubname_entry, gc, 32);
20856 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20857
20858 incomplete_types = VEC_alloc (tree, gc, 64);
20859
20860 used_rtx_array = VEC_alloc (rtx, gc, 32);
20861
20862 debug_info_section = get_section (DEBUG_INFO_SECTION,
20863 SECTION_DEBUG, NULL);
20864 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20865 SECTION_DEBUG, NULL);
20866 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20867 SECTION_DEBUG, NULL);
20868 debug_macinfo_section = get_section (dwarf_strict
20869 ? DEBUG_MACINFO_SECTION
20870 : DEBUG_MACRO_SECTION,
20871 SECTION_DEBUG, NULL);
20872 debug_line_section = get_section (DEBUG_LINE_SECTION,
20873 SECTION_DEBUG, NULL);
20874 debug_loc_section = get_section (DEBUG_LOC_SECTION,
20875 SECTION_DEBUG, NULL);
20876 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20877 SECTION_DEBUG, NULL);
20878 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20879 SECTION_DEBUG, NULL);
20880 debug_str_section = get_section (DEBUG_STR_SECTION,
20881 DEBUG_STR_SECTION_FLAGS, NULL);
20882 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20883 SECTION_DEBUG, NULL);
20884 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20885 SECTION_DEBUG, NULL);
20886
20887 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20888 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20889 DEBUG_ABBREV_SECTION_LABEL, 0);
20890 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20891 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20892 COLD_TEXT_SECTION_LABEL, 0);
20893 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20894
20895 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20896 DEBUG_INFO_SECTION_LABEL, 0);
20897 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20898 DEBUG_LINE_SECTION_LABEL, 0);
20899 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20900 DEBUG_RANGES_SECTION_LABEL, 0);
20901 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20902 dwarf_strict
20903 ? DEBUG_MACINFO_SECTION_LABEL
20904 : DEBUG_MACRO_SECTION_LABEL, 0);
20905
20906 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20907 macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
20908
20909 switch_to_section (text_section);
20910 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20911
20912 /* Make sure the line number table for .text always exists. */
20913 text_section_line_info = new_line_info_table ();
20914 text_section_line_info->end_label = text_end_label;
20915 }
20916
20917 /* Called before compile () starts outputtting functions, variables
20918 and toplevel asms into assembly. */
20919
20920 static void
20921 dwarf2out_assembly_start (void)
20922 {
20923 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
20924 && dwarf2out_do_cfi_asm ()
20925 && (!(flag_unwind_tables || flag_exceptions)
20926 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
20927 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20928 }
20929
20930 /* A helper function for dwarf2out_finish called through
20931 htab_traverse. Emit one queued .debug_str string. */
20932
20933 static int
20934 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20935 {
20936 struct indirect_string_node *node = (struct indirect_string_node *) *h;
20937
20938 if (node->form == DW_FORM_strp)
20939 {
20940 switch_to_section (debug_str_section);
20941 ASM_OUTPUT_LABEL (asm_out_file, node->label);
20942 assemble_string (node->str, strlen (node->str) + 1);
20943 }
20944
20945 return 1;
20946 }
20947
20948 #if ENABLE_ASSERT_CHECKING
20949 /* Verify that all marks are clear. */
20950
20951 static void
20952 verify_marks_clear (dw_die_ref die)
20953 {
20954 dw_die_ref c;
20955
20956 gcc_assert (! die->die_mark);
20957 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
20958 }
20959 #endif /* ENABLE_ASSERT_CHECKING */
20960
20961 /* Clear the marks for a die and its children.
20962 Be cool if the mark isn't set. */
20963
20964 static void
20965 prune_unmark_dies (dw_die_ref die)
20966 {
20967 dw_die_ref c;
20968
20969 if (die->die_mark)
20970 die->die_mark = 0;
20971 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
20972 }
20973
20974 /* Given DIE that we're marking as used, find any other dies
20975 it references as attributes and mark them as used. */
20976
20977 static void
20978 prune_unused_types_walk_attribs (dw_die_ref die)
20979 {
20980 dw_attr_ref a;
20981 unsigned ix;
20982
20983 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
20984 {
20985 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
20986 {
20987 /* A reference to another DIE.
20988 Make sure that it will get emitted.
20989 If it was broken out into a comdat group, don't follow it. */
20990 if (! AT_ref (a)->comdat_type_p
20991 || a->dw_attr == DW_AT_specification)
20992 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
20993 }
20994 /* Set the string's refcount to 0 so that prune_unused_types_mark
20995 accounts properly for it. */
20996 if (AT_class (a) == dw_val_class_str)
20997 a->dw_attr_val.v.val_str->refcount = 0;
20998 }
20999 }
21000
21001 /* Mark the generic parameters and arguments children DIEs of DIE. */
21002
21003 static void
21004 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21005 {
21006 dw_die_ref c;
21007
21008 if (die == NULL || die->die_child == NULL)
21009 return;
21010 c = die->die_child;
21011 do
21012 {
21013 switch (c->die_tag)
21014 {
21015 case DW_TAG_template_type_param:
21016 case DW_TAG_template_value_param:
21017 case DW_TAG_GNU_template_template_param:
21018 case DW_TAG_GNU_template_parameter_pack:
21019 prune_unused_types_mark (c, 1);
21020 break;
21021 default:
21022 break;
21023 }
21024 c = c->die_sib;
21025 } while (c && c != die->die_child);
21026 }
21027
21028 /* Mark DIE as being used. If DOKIDS is true, then walk down
21029 to DIE's children. */
21030
21031 static void
21032 prune_unused_types_mark (dw_die_ref die, int dokids)
21033 {
21034 dw_die_ref c;
21035
21036 if (die->die_mark == 0)
21037 {
21038 /* We haven't done this node yet. Mark it as used. */
21039 die->die_mark = 1;
21040 /* If this is the DIE of a generic type instantiation,
21041 mark the children DIEs that describe its generic parms and
21042 args. */
21043 prune_unused_types_mark_generic_parms_dies (die);
21044
21045 /* We also have to mark its parents as used.
21046 (But we don't want to mark our parents' kids due to this.) */
21047 if (die->die_parent)
21048 prune_unused_types_mark (die->die_parent, 0);
21049
21050 /* Mark any referenced nodes. */
21051 prune_unused_types_walk_attribs (die);
21052
21053 /* If this node is a specification,
21054 also mark the definition, if it exists. */
21055 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21056 prune_unused_types_mark (die->die_definition, 1);
21057 }
21058
21059 if (dokids && die->die_mark != 2)
21060 {
21061 /* We need to walk the children, but haven't done so yet.
21062 Remember that we've walked the kids. */
21063 die->die_mark = 2;
21064
21065 /* If this is an array type, we need to make sure our
21066 kids get marked, even if they're types. If we're
21067 breaking out types into comdat sections, do this
21068 for all type definitions. */
21069 if (die->die_tag == DW_TAG_array_type
21070 || (use_debug_types
21071 && is_type_die (die) && ! is_declaration_die (die)))
21072 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21073 else
21074 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21075 }
21076 }
21077
21078 /* For local classes, look if any static member functions were emitted
21079 and if so, mark them. */
21080
21081 static void
21082 prune_unused_types_walk_local_classes (dw_die_ref die)
21083 {
21084 dw_die_ref c;
21085
21086 if (die->die_mark == 2)
21087 return;
21088
21089 switch (die->die_tag)
21090 {
21091 case DW_TAG_structure_type:
21092 case DW_TAG_union_type:
21093 case DW_TAG_class_type:
21094 break;
21095
21096 case DW_TAG_subprogram:
21097 if (!get_AT_flag (die, DW_AT_declaration)
21098 || die->die_definition != NULL)
21099 prune_unused_types_mark (die, 1);
21100 return;
21101
21102 default:
21103 return;
21104 }
21105
21106 /* Mark children. */
21107 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21108 }
21109
21110 /* Walk the tree DIE and mark types that we actually use. */
21111
21112 static void
21113 prune_unused_types_walk (dw_die_ref die)
21114 {
21115 dw_die_ref c;
21116
21117 /* Don't do anything if this node is already marked and
21118 children have been marked as well. */
21119 if (die->die_mark == 2)
21120 return;
21121
21122 switch (die->die_tag)
21123 {
21124 case DW_TAG_structure_type:
21125 case DW_TAG_union_type:
21126 case DW_TAG_class_type:
21127 if (die->die_perennial_p)
21128 break;
21129
21130 for (c = die->die_parent; c; c = c->die_parent)
21131 if (c->die_tag == DW_TAG_subprogram)
21132 break;
21133
21134 /* Finding used static member functions inside of classes
21135 is needed just for local classes, because for other classes
21136 static member function DIEs with DW_AT_specification
21137 are emitted outside of the DW_TAG_*_type. If we ever change
21138 it, we'd need to call this even for non-local classes. */
21139 if (c)
21140 prune_unused_types_walk_local_classes (die);
21141
21142 /* It's a type node --- don't mark it. */
21143 return;
21144
21145 case DW_TAG_const_type:
21146 case DW_TAG_packed_type:
21147 case DW_TAG_pointer_type:
21148 case DW_TAG_reference_type:
21149 case DW_TAG_rvalue_reference_type:
21150 case DW_TAG_volatile_type:
21151 case DW_TAG_typedef:
21152 case DW_TAG_array_type:
21153 case DW_TAG_interface_type:
21154 case DW_TAG_friend:
21155 case DW_TAG_variant_part:
21156 case DW_TAG_enumeration_type:
21157 case DW_TAG_subroutine_type:
21158 case DW_TAG_string_type:
21159 case DW_TAG_set_type:
21160 case DW_TAG_subrange_type:
21161 case DW_TAG_ptr_to_member_type:
21162 case DW_TAG_file_type:
21163 if (die->die_perennial_p)
21164 break;
21165
21166 /* It's a type node --- don't mark it. */
21167 return;
21168
21169 default:
21170 /* Mark everything else. */
21171 break;
21172 }
21173
21174 if (die->die_mark == 0)
21175 {
21176 die->die_mark = 1;
21177
21178 /* Now, mark any dies referenced from here. */
21179 prune_unused_types_walk_attribs (die);
21180 }
21181
21182 die->die_mark = 2;
21183
21184 /* Mark children. */
21185 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21186 }
21187
21188 /* Increment the string counts on strings referred to from DIE's
21189 attributes. */
21190
21191 static void
21192 prune_unused_types_update_strings (dw_die_ref die)
21193 {
21194 dw_attr_ref a;
21195 unsigned ix;
21196
21197 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21198 if (AT_class (a) == dw_val_class_str)
21199 {
21200 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21201 s->refcount++;
21202 /* Avoid unnecessarily putting strings that are used less than
21203 twice in the hash table. */
21204 if (s->refcount
21205 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21206 {
21207 void ** slot;
21208 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21209 htab_hash_string (s->str),
21210 INSERT);
21211 gcc_assert (*slot == NULL);
21212 *slot = s;
21213 }
21214 }
21215 }
21216
21217 /* Remove from the tree DIE any dies that aren't marked. */
21218
21219 static void
21220 prune_unused_types_prune (dw_die_ref die)
21221 {
21222 dw_die_ref c;
21223
21224 gcc_assert (die->die_mark);
21225 prune_unused_types_update_strings (die);
21226
21227 if (! die->die_child)
21228 return;
21229
21230 c = die->die_child;
21231 do {
21232 dw_die_ref prev = c;
21233 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21234 if (c == die->die_child)
21235 {
21236 /* No marked children between 'prev' and the end of the list. */
21237 if (prev == c)
21238 /* No marked children at all. */
21239 die->die_child = NULL;
21240 else
21241 {
21242 prev->die_sib = c->die_sib;
21243 die->die_child = prev;
21244 }
21245 return;
21246 }
21247
21248 if (c != prev->die_sib)
21249 prev->die_sib = c;
21250 prune_unused_types_prune (c);
21251 } while (c != die->die_child);
21252 }
21253
21254 /* Remove dies representing declarations that we never use. */
21255
21256 static void
21257 prune_unused_types (void)
21258 {
21259 unsigned int i;
21260 limbo_die_node *node;
21261 comdat_type_node *ctnode;
21262 pubname_ref pub;
21263 dw_die_ref base_type;
21264
21265 #if ENABLE_ASSERT_CHECKING
21266 /* All the marks should already be clear. */
21267 verify_marks_clear (comp_unit_die ());
21268 for (node = limbo_die_list; node; node = node->next)
21269 verify_marks_clear (node->die);
21270 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21271 verify_marks_clear (ctnode->root_die);
21272 #endif /* ENABLE_ASSERT_CHECKING */
21273
21274 /* Mark types that are used in global variables. */
21275 premark_types_used_by_global_vars ();
21276
21277 /* Set the mark on nodes that are actually used. */
21278 prune_unused_types_walk (comp_unit_die ());
21279 for (node = limbo_die_list; node; node = node->next)
21280 prune_unused_types_walk (node->die);
21281 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21282 {
21283 prune_unused_types_walk (ctnode->root_die);
21284 prune_unused_types_mark (ctnode->type_die, 1);
21285 }
21286
21287 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
21288 are unusual in that they are pubnames that are the children of pubtypes.
21289 They should only be marked via their parent DW_TAG_enumeration_type die,
21290 not as roots in themselves. */
21291 FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21292 if (pub->die->die_tag != DW_TAG_enumerator)
21293 prune_unused_types_mark (pub->die, 1);
21294 for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21295 prune_unused_types_mark (base_type, 1);
21296
21297 if (debug_str_hash)
21298 htab_empty (debug_str_hash);
21299 prune_unused_types_prune (comp_unit_die ());
21300 for (node = limbo_die_list; node; node = node->next)
21301 prune_unused_types_prune (node->die);
21302 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21303 prune_unused_types_prune (ctnode->root_die);
21304
21305 /* Leave the marks clear. */
21306 prune_unmark_dies (comp_unit_die ());
21307 for (node = limbo_die_list; node; node = node->next)
21308 prune_unmark_dies (node->die);
21309 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21310 prune_unmark_dies (ctnode->root_die);
21311 }
21312
21313 /* Set the parameter to true if there are any relative pathnames in
21314 the file table. */
21315 static int
21316 file_table_relative_p (void ** slot, void *param)
21317 {
21318 bool *p = (bool *) param;
21319 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21320 if (!IS_ABSOLUTE_PATH (d->filename))
21321 {
21322 *p = true;
21323 return 0;
21324 }
21325 return 1;
21326 }
21327
21328 /* Routines to manipulate hash table of comdat type units. */
21329
21330 static hashval_t
21331 htab_ct_hash (const void *of)
21332 {
21333 hashval_t h;
21334 const comdat_type_node *const type_node = (const comdat_type_node *) of;
21335
21336 memcpy (&h, type_node->signature, sizeof (h));
21337 return h;
21338 }
21339
21340 static int
21341 htab_ct_eq (const void *of1, const void *of2)
21342 {
21343 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21344 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21345
21346 return (! memcmp (type_node_1->signature, type_node_2->signature,
21347 DWARF_TYPE_SIGNATURE_SIZE));
21348 }
21349
21350 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21351 to the location it would have been added, should we know its
21352 DECL_ASSEMBLER_NAME when we added other attributes. This will
21353 probably improve compactness of debug info, removing equivalent
21354 abbrevs, and hide any differences caused by deferring the
21355 computation of the assembler name, triggered by e.g. PCH. */
21356
21357 static inline void
21358 move_linkage_attr (dw_die_ref die)
21359 {
21360 unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21361 dw_attr_node linkage = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21362
21363 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21364 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21365
21366 while (--ix > 0)
21367 {
21368 dw_attr_node *prev = &VEC_index (dw_attr_node, die->die_attr, ix - 1);
21369
21370 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21371 break;
21372 }
21373
21374 if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21375 {
21376 VEC_pop (dw_attr_node, die->die_attr);
21377 VEC_quick_insert (dw_attr_node, die->die_attr, ix, linkage);
21378 }
21379 }
21380
21381 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21382 referenced from typed stack ops and count how often they are used. */
21383
21384 static void
21385 mark_base_types (dw_loc_descr_ref loc)
21386 {
21387 dw_die_ref base_type = NULL;
21388
21389 for (; loc; loc = loc->dw_loc_next)
21390 {
21391 switch (loc->dw_loc_opc)
21392 {
21393 case DW_OP_GNU_regval_type:
21394 case DW_OP_GNU_deref_type:
21395 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21396 break;
21397 case DW_OP_GNU_convert:
21398 case DW_OP_GNU_reinterpret:
21399 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21400 continue;
21401 /* FALLTHRU */
21402 case DW_OP_GNU_const_type:
21403 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21404 break;
21405 case DW_OP_GNU_entry_value:
21406 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21407 continue;
21408 default:
21409 continue;
21410 }
21411 gcc_assert (base_type->die_parent == comp_unit_die ());
21412 if (base_type->die_mark)
21413 base_type->die_mark++;
21414 else
21415 {
21416 VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21417 base_type->die_mark = 1;
21418 }
21419 }
21420 }
21421
21422 /* Comparison function for sorting marked base types. */
21423
21424 static int
21425 base_type_cmp (const void *x, const void *y)
21426 {
21427 dw_die_ref dx = *(const dw_die_ref *) x;
21428 dw_die_ref dy = *(const dw_die_ref *) y;
21429 unsigned int byte_size1, byte_size2;
21430 unsigned int encoding1, encoding2;
21431 if (dx->die_mark > dy->die_mark)
21432 return -1;
21433 if (dx->die_mark < dy->die_mark)
21434 return 1;
21435 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21436 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21437 if (byte_size1 < byte_size2)
21438 return 1;
21439 if (byte_size1 > byte_size2)
21440 return -1;
21441 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21442 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21443 if (encoding1 < encoding2)
21444 return 1;
21445 if (encoding1 > encoding2)
21446 return -1;
21447 return 0;
21448 }
21449
21450 /* Move base types marked by mark_base_types as early as possible
21451 in the CU, sorted by decreasing usage count both to make the
21452 uleb128 references as small as possible and to make sure they
21453 will have die_offset already computed by calc_die_sizes when
21454 sizes of typed stack loc ops is computed. */
21455
21456 static void
21457 move_marked_base_types (void)
21458 {
21459 unsigned int i;
21460 dw_die_ref base_type, die, c;
21461
21462 if (VEC_empty (dw_die_ref, base_types))
21463 return;
21464
21465 /* Sort by decreasing usage count, they will be added again in that
21466 order later on. */
21467 VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21468 die = comp_unit_die ();
21469 c = die->die_child;
21470 do
21471 {
21472 dw_die_ref prev = c;
21473 c = c->die_sib;
21474 while (c->die_mark)
21475 {
21476 remove_child_with_prev (c, prev);
21477 /* As base types got marked, there must be at least
21478 one node other than DW_TAG_base_type. */
21479 gcc_assert (c != c->die_sib);
21480 c = c->die_sib;
21481 }
21482 }
21483 while (c != die->die_child);
21484 gcc_assert (die->die_child);
21485 c = die->die_child;
21486 for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21487 {
21488 base_type->die_mark = 0;
21489 base_type->die_sib = c->die_sib;
21490 c->die_sib = base_type;
21491 c = base_type;
21492 }
21493 }
21494
21495 /* Helper function for resolve_addr, attempt to resolve
21496 one CONST_STRING, return non-zero if not successful. Similarly verify that
21497 SYMBOL_REFs refer to variables emitted in the current CU. */
21498
21499 static int
21500 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21501 {
21502 rtx rtl = *addr;
21503
21504 if (GET_CODE (rtl) == CONST_STRING)
21505 {
21506 size_t len = strlen (XSTR (rtl, 0)) + 1;
21507 tree t = build_string (len, XSTR (rtl, 0));
21508 tree tlen = size_int (len - 1);
21509 TREE_TYPE (t)
21510 = build_array_type (char_type_node, build_index_type (tlen));
21511 rtl = lookup_constant_def (t);
21512 if (!rtl || !MEM_P (rtl))
21513 return 1;
21514 rtl = XEXP (rtl, 0);
21515 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21516 *addr = rtl;
21517 return 0;
21518 }
21519
21520 if (GET_CODE (rtl) == SYMBOL_REF
21521 && SYMBOL_REF_DECL (rtl))
21522 {
21523 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21524 {
21525 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21526 return 1;
21527 }
21528 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21529 return 1;
21530 }
21531
21532 if (GET_CODE (rtl) == CONST
21533 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21534 return 1;
21535
21536 return 0;
21537 }
21538
21539 /* Helper function for resolve_addr, handle one location
21540 expression, return false if at least one CONST_STRING or SYMBOL_REF in
21541 the location list couldn't be resolved. */
21542
21543 static bool
21544 resolve_addr_in_expr (dw_loc_descr_ref loc)
21545 {
21546 dw_loc_descr_ref keep = NULL;
21547 for (; loc; loc = loc->dw_loc_next)
21548 switch (loc->dw_loc_opc)
21549 {
21550 case DW_OP_addr:
21551 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21552 return false;
21553 break;
21554 case DW_OP_const4u:
21555 case DW_OP_const8u:
21556 if (loc->dtprel
21557 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21558 return false;
21559 break;
21560 case DW_OP_plus_uconst:
21561 if (size_of_loc_descr (loc)
21562 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21563 + 1
21564 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21565 {
21566 dw_loc_descr_ref repl
21567 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21568 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21569 add_loc_descr (&repl, loc->dw_loc_next);
21570 *loc = *repl;
21571 }
21572 break;
21573 case DW_OP_implicit_value:
21574 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21575 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21576 return false;
21577 break;
21578 case DW_OP_GNU_implicit_pointer:
21579 case DW_OP_GNU_parameter_ref:
21580 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21581 {
21582 dw_die_ref ref
21583 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21584 if (ref == NULL)
21585 return false;
21586 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21587 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21588 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21589 }
21590 break;
21591 case DW_OP_GNU_const_type:
21592 case DW_OP_GNU_regval_type:
21593 case DW_OP_GNU_deref_type:
21594 case DW_OP_GNU_convert:
21595 case DW_OP_GNU_reinterpret:
21596 while (loc->dw_loc_next
21597 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21598 {
21599 dw_die_ref base1, base2;
21600 unsigned enc1, enc2, size1, size2;
21601 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21602 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21603 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21604 else if (loc->dw_loc_oprnd1.val_class
21605 == dw_val_class_unsigned_const)
21606 break;
21607 else
21608 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21609 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21610 == dw_val_class_unsigned_const)
21611 break;
21612 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21613 gcc_assert (base1->die_tag == DW_TAG_base_type
21614 && base2->die_tag == DW_TAG_base_type);
21615 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21616 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21617 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21618 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21619 if (size1 == size2
21620 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21621 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21622 && loc != keep)
21623 || enc1 == enc2))
21624 {
21625 /* Optimize away next DW_OP_GNU_convert after
21626 adjusting LOC's base type die reference. */
21627 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21628 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21629 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21630 else
21631 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21632 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21633 continue;
21634 }
21635 /* Don't change integer DW_OP_GNU_convert after e.g. floating
21636 point typed stack entry. */
21637 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21638 keep = loc->dw_loc_next;
21639 break;
21640 }
21641 break;
21642 default:
21643 break;
21644 }
21645 return true;
21646 }
21647
21648 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21649 an address in .rodata section if the string literal is emitted there,
21650 or remove the containing location list or replace DW_AT_const_value
21651 with DW_AT_location and empty location expression, if it isn't found
21652 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
21653 to something that has been emitted in the current CU. */
21654
21655 static void
21656 resolve_addr (dw_die_ref die)
21657 {
21658 dw_die_ref c;
21659 dw_attr_ref a;
21660 dw_loc_list_ref *curr, *start, loc;
21661 unsigned ix;
21662
21663 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21664 switch (AT_class (a))
21665 {
21666 case dw_val_class_loc_list:
21667 start = curr = AT_loc_list_ptr (a);
21668 loc = *curr;
21669 gcc_assert (loc);
21670 /* The same list can be referenced more than once. See if we have
21671 already recorded the result from a previous pass. */
21672 if (loc->replaced)
21673 *curr = loc->dw_loc_next;
21674 else if (!loc->resolved_addr)
21675 {
21676 /* As things stand, we do not expect or allow one die to
21677 reference a suffix of another die's location list chain.
21678 References must be identical or completely separate.
21679 There is therefore no need to cache the result of this
21680 pass on any list other than the first; doing so
21681 would lead to unnecessary writes. */
21682 while (*curr)
21683 {
21684 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21685 if (!resolve_addr_in_expr ((*curr)->expr))
21686 {
21687 dw_loc_list_ref next = (*curr)->dw_loc_next;
21688 if (next && (*curr)->ll_symbol)
21689 {
21690 gcc_assert (!next->ll_symbol);
21691 next->ll_symbol = (*curr)->ll_symbol;
21692 }
21693 *curr = next;
21694 }
21695 else
21696 {
21697 mark_base_types ((*curr)->expr);
21698 curr = &(*curr)->dw_loc_next;
21699 }
21700 }
21701 if (loc == *start)
21702 loc->resolved_addr = 1;
21703 else
21704 {
21705 loc->replaced = 1;
21706 loc->dw_loc_next = *start;
21707 }
21708 }
21709 if (!*start)
21710 {
21711 remove_AT (die, a->dw_attr);
21712 ix--;
21713 }
21714 break;
21715 case dw_val_class_loc:
21716 {
21717 dw_loc_descr_ref l = AT_loc (a);
21718 /* For -gdwarf-2 don't attempt to optimize
21719 DW_AT_data_member_location containing
21720 DW_OP_plus_uconst - older consumers might
21721 rely on it being that op instead of a more complex,
21722 but shorter, location description. */
21723 if ((dwarf_version > 2
21724 || a->dw_attr != DW_AT_data_member_location
21725 || l == NULL
21726 || l->dw_loc_opc != DW_OP_plus_uconst
21727 || l->dw_loc_next != NULL)
21728 && !resolve_addr_in_expr (l))
21729 {
21730 remove_AT (die, a->dw_attr);
21731 ix--;
21732 }
21733 else
21734 mark_base_types (l);
21735 }
21736 break;
21737 case dw_val_class_addr:
21738 if (a->dw_attr == DW_AT_const_value
21739 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21740 {
21741 remove_AT (die, a->dw_attr);
21742 ix--;
21743 }
21744 if (die->die_tag == DW_TAG_GNU_call_site
21745 && a->dw_attr == DW_AT_abstract_origin)
21746 {
21747 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21748 dw_die_ref tdie = lookup_decl_die (tdecl);
21749 if (tdie == NULL
21750 && DECL_EXTERNAL (tdecl)
21751 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21752 {
21753 force_decl_die (tdecl);
21754 tdie = lookup_decl_die (tdecl);
21755 }
21756 if (tdie)
21757 {
21758 a->dw_attr_val.val_class = dw_val_class_die_ref;
21759 a->dw_attr_val.v.val_die_ref.die = tdie;
21760 a->dw_attr_val.v.val_die_ref.external = 0;
21761 }
21762 else
21763 {
21764 remove_AT (die, a->dw_attr);
21765 ix--;
21766 }
21767 }
21768 break;
21769 default:
21770 break;
21771 }
21772
21773 FOR_EACH_CHILD (die, c, resolve_addr (c));
21774 }
21775 \f
21776 /* Helper routines for optimize_location_lists.
21777 This pass tries to share identical local lists in .debug_loc
21778 section. */
21779
21780 /* Iteratively hash operands of LOC opcode. */
21781
21782 static inline hashval_t
21783 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21784 {
21785 dw_val_ref val1 = &loc->dw_loc_oprnd1;
21786 dw_val_ref val2 = &loc->dw_loc_oprnd2;
21787
21788 switch (loc->dw_loc_opc)
21789 {
21790 case DW_OP_const4u:
21791 case DW_OP_const8u:
21792 if (loc->dtprel)
21793 goto hash_addr;
21794 /* FALLTHRU */
21795 case DW_OP_const1u:
21796 case DW_OP_const1s:
21797 case DW_OP_const2u:
21798 case DW_OP_const2s:
21799 case DW_OP_const4s:
21800 case DW_OP_const8s:
21801 case DW_OP_constu:
21802 case DW_OP_consts:
21803 case DW_OP_pick:
21804 case DW_OP_plus_uconst:
21805 case DW_OP_breg0:
21806 case DW_OP_breg1:
21807 case DW_OP_breg2:
21808 case DW_OP_breg3:
21809 case DW_OP_breg4:
21810 case DW_OP_breg5:
21811 case DW_OP_breg6:
21812 case DW_OP_breg7:
21813 case DW_OP_breg8:
21814 case DW_OP_breg9:
21815 case DW_OP_breg10:
21816 case DW_OP_breg11:
21817 case DW_OP_breg12:
21818 case DW_OP_breg13:
21819 case DW_OP_breg14:
21820 case DW_OP_breg15:
21821 case DW_OP_breg16:
21822 case DW_OP_breg17:
21823 case DW_OP_breg18:
21824 case DW_OP_breg19:
21825 case DW_OP_breg20:
21826 case DW_OP_breg21:
21827 case DW_OP_breg22:
21828 case DW_OP_breg23:
21829 case DW_OP_breg24:
21830 case DW_OP_breg25:
21831 case DW_OP_breg26:
21832 case DW_OP_breg27:
21833 case DW_OP_breg28:
21834 case DW_OP_breg29:
21835 case DW_OP_breg30:
21836 case DW_OP_breg31:
21837 case DW_OP_regx:
21838 case DW_OP_fbreg:
21839 case DW_OP_piece:
21840 case DW_OP_deref_size:
21841 case DW_OP_xderef_size:
21842 hash = iterative_hash_object (val1->v.val_int, hash);
21843 break;
21844 case DW_OP_skip:
21845 case DW_OP_bra:
21846 {
21847 int offset;
21848
21849 gcc_assert (val1->val_class == dw_val_class_loc);
21850 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
21851 hash = iterative_hash_object (offset, hash);
21852 }
21853 break;
21854 case DW_OP_implicit_value:
21855 hash = iterative_hash_object (val1->v.val_unsigned, hash);
21856 switch (val2->val_class)
21857 {
21858 case dw_val_class_const:
21859 hash = iterative_hash_object (val2->v.val_int, hash);
21860 break;
21861 case dw_val_class_vec:
21862 {
21863 unsigned int elt_size = val2->v.val_vec.elt_size;
21864 unsigned int len = val2->v.val_vec.length;
21865
21866 hash = iterative_hash_object (elt_size, hash);
21867 hash = iterative_hash_object (len, hash);
21868 hash = iterative_hash (val2->v.val_vec.array,
21869 len * elt_size, hash);
21870 }
21871 break;
21872 case dw_val_class_const_double:
21873 hash = iterative_hash_object (val2->v.val_double.low, hash);
21874 hash = iterative_hash_object (val2->v.val_double.high, hash);
21875 break;
21876 case dw_val_class_addr:
21877 hash = iterative_hash_rtx (val2->v.val_addr, hash);
21878 break;
21879 default:
21880 gcc_unreachable ();
21881 }
21882 break;
21883 case DW_OP_bregx:
21884 case DW_OP_bit_piece:
21885 hash = iterative_hash_object (val1->v.val_int, hash);
21886 hash = iterative_hash_object (val2->v.val_int, hash);
21887 break;
21888 case DW_OP_addr:
21889 hash_addr:
21890 if (loc->dtprel)
21891 {
21892 unsigned char dtprel = 0xd1;
21893 hash = iterative_hash_object (dtprel, hash);
21894 }
21895 hash = iterative_hash_rtx (val1->v.val_addr, hash);
21896 break;
21897 case DW_OP_GNU_implicit_pointer:
21898 hash = iterative_hash_object (val2->v.val_int, hash);
21899 break;
21900 case DW_OP_GNU_entry_value:
21901 hash = hash_loc_operands (val1->v.val_loc, hash);
21902 break;
21903 case DW_OP_GNU_regval_type:
21904 case DW_OP_GNU_deref_type:
21905 {
21906 unsigned int byte_size
21907 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
21908 unsigned int encoding
21909 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
21910 hash = iterative_hash_object (val1->v.val_int, hash);
21911 hash = iterative_hash_object (byte_size, hash);
21912 hash = iterative_hash_object (encoding, hash);
21913 }
21914 break;
21915 case DW_OP_GNU_convert:
21916 case DW_OP_GNU_reinterpret:
21917 if (val1->val_class == dw_val_class_unsigned_const)
21918 {
21919 hash = iterative_hash_object (val1->v.val_unsigned, hash);
21920 break;
21921 }
21922 /* FALLTHRU */
21923 case DW_OP_GNU_const_type:
21924 {
21925 unsigned int byte_size
21926 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
21927 unsigned int encoding
21928 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
21929 hash = iterative_hash_object (byte_size, hash);
21930 hash = iterative_hash_object (encoding, hash);
21931 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
21932 break;
21933 hash = iterative_hash_object (val2->val_class, hash);
21934 switch (val2->val_class)
21935 {
21936 case dw_val_class_const:
21937 hash = iterative_hash_object (val2->v.val_int, hash);
21938 break;
21939 case dw_val_class_vec:
21940 {
21941 unsigned int elt_size = val2->v.val_vec.elt_size;
21942 unsigned int len = val2->v.val_vec.length;
21943
21944 hash = iterative_hash_object (elt_size, hash);
21945 hash = iterative_hash_object (len, hash);
21946 hash = iterative_hash (val2->v.val_vec.array,
21947 len * elt_size, hash);
21948 }
21949 break;
21950 case dw_val_class_const_double:
21951 hash = iterative_hash_object (val2->v.val_double.low, hash);
21952 hash = iterative_hash_object (val2->v.val_double.high, hash);
21953 break;
21954 default:
21955 gcc_unreachable ();
21956 }
21957 }
21958 break;
21959
21960 default:
21961 /* Other codes have no operands. */
21962 break;
21963 }
21964 return hash;
21965 }
21966
21967 /* Iteratively hash the whole DWARF location expression LOC. */
21968
21969 static inline hashval_t
21970 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
21971 {
21972 dw_loc_descr_ref l;
21973 bool sizes_computed = false;
21974 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
21975 size_of_locs (loc);
21976
21977 for (l = loc; l != NULL; l = l->dw_loc_next)
21978 {
21979 enum dwarf_location_atom opc = l->dw_loc_opc;
21980 hash = iterative_hash_object (opc, hash);
21981 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
21982 {
21983 size_of_locs (loc);
21984 sizes_computed = true;
21985 }
21986 hash = hash_loc_operands (l, hash);
21987 }
21988 return hash;
21989 }
21990
21991 /* Compute hash of the whole location list LIST_HEAD. */
21992
21993 static inline void
21994 hash_loc_list (dw_loc_list_ref list_head)
21995 {
21996 dw_loc_list_ref curr = list_head;
21997 hashval_t hash = 0;
21998
21999 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22000 {
22001 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22002 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22003 if (curr->section)
22004 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22005 hash);
22006 hash = hash_locs (curr->expr, hash);
22007 }
22008 list_head->hash = hash;
22009 }
22010
22011 /* Return true if X and Y opcodes have the same operands. */
22012
22013 static inline bool
22014 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22015 {
22016 dw_val_ref valx1 = &x->dw_loc_oprnd1;
22017 dw_val_ref valx2 = &x->dw_loc_oprnd2;
22018 dw_val_ref valy1 = &y->dw_loc_oprnd1;
22019 dw_val_ref valy2 = &y->dw_loc_oprnd2;
22020
22021 switch (x->dw_loc_opc)
22022 {
22023 case DW_OP_const4u:
22024 case DW_OP_const8u:
22025 if (x->dtprel)
22026 goto hash_addr;
22027 /* FALLTHRU */
22028 case DW_OP_const1u:
22029 case DW_OP_const1s:
22030 case DW_OP_const2u:
22031 case DW_OP_const2s:
22032 case DW_OP_const4s:
22033 case DW_OP_const8s:
22034 case DW_OP_constu:
22035 case DW_OP_consts:
22036 case DW_OP_pick:
22037 case DW_OP_plus_uconst:
22038 case DW_OP_breg0:
22039 case DW_OP_breg1:
22040 case DW_OP_breg2:
22041 case DW_OP_breg3:
22042 case DW_OP_breg4:
22043 case DW_OP_breg5:
22044 case DW_OP_breg6:
22045 case DW_OP_breg7:
22046 case DW_OP_breg8:
22047 case DW_OP_breg9:
22048 case DW_OP_breg10:
22049 case DW_OP_breg11:
22050 case DW_OP_breg12:
22051 case DW_OP_breg13:
22052 case DW_OP_breg14:
22053 case DW_OP_breg15:
22054 case DW_OP_breg16:
22055 case DW_OP_breg17:
22056 case DW_OP_breg18:
22057 case DW_OP_breg19:
22058 case DW_OP_breg20:
22059 case DW_OP_breg21:
22060 case DW_OP_breg22:
22061 case DW_OP_breg23:
22062 case DW_OP_breg24:
22063 case DW_OP_breg25:
22064 case DW_OP_breg26:
22065 case DW_OP_breg27:
22066 case DW_OP_breg28:
22067 case DW_OP_breg29:
22068 case DW_OP_breg30:
22069 case DW_OP_breg31:
22070 case DW_OP_regx:
22071 case DW_OP_fbreg:
22072 case DW_OP_piece:
22073 case DW_OP_deref_size:
22074 case DW_OP_xderef_size:
22075 return valx1->v.val_int == valy1->v.val_int;
22076 case DW_OP_skip:
22077 case DW_OP_bra:
22078 gcc_assert (valx1->val_class == dw_val_class_loc
22079 && valy1->val_class == dw_val_class_loc
22080 && x->dw_loc_addr == y->dw_loc_addr);
22081 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22082 case DW_OP_implicit_value:
22083 if (valx1->v.val_unsigned != valy1->v.val_unsigned
22084 || valx2->val_class != valy2->val_class)
22085 return false;
22086 switch (valx2->val_class)
22087 {
22088 case dw_val_class_const:
22089 return valx2->v.val_int == valy2->v.val_int;
22090 case dw_val_class_vec:
22091 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22092 && valx2->v.val_vec.length == valy2->v.val_vec.length
22093 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22094 valx2->v.val_vec.elt_size
22095 * valx2->v.val_vec.length) == 0;
22096 case dw_val_class_const_double:
22097 return valx2->v.val_double.low == valy2->v.val_double.low
22098 && valx2->v.val_double.high == valy2->v.val_double.high;
22099 case dw_val_class_addr:
22100 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22101 default:
22102 gcc_unreachable ();
22103 }
22104 case DW_OP_bregx:
22105 case DW_OP_bit_piece:
22106 return valx1->v.val_int == valy1->v.val_int
22107 && valx2->v.val_int == valy2->v.val_int;
22108 case DW_OP_addr:
22109 hash_addr:
22110 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22111 case DW_OP_GNU_implicit_pointer:
22112 return valx1->val_class == dw_val_class_die_ref
22113 && valx1->val_class == valy1->val_class
22114 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22115 && valx2->v.val_int == valy2->v.val_int;
22116 case DW_OP_GNU_entry_value:
22117 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22118 case DW_OP_GNU_const_type:
22119 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22120 || valx2->val_class != valy2->val_class)
22121 return false;
22122 switch (valx2->val_class)
22123 {
22124 case dw_val_class_const:
22125 return valx2->v.val_int == valy2->v.val_int;
22126 case dw_val_class_vec:
22127 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22128 && valx2->v.val_vec.length == valy2->v.val_vec.length
22129 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22130 valx2->v.val_vec.elt_size
22131 * valx2->v.val_vec.length) == 0;
22132 case dw_val_class_const_double:
22133 return valx2->v.val_double.low == valy2->v.val_double.low
22134 && valx2->v.val_double.high == valy2->v.val_double.high;
22135 default:
22136 gcc_unreachable ();
22137 }
22138 case DW_OP_GNU_regval_type:
22139 case DW_OP_GNU_deref_type:
22140 return valx1->v.val_int == valy1->v.val_int
22141 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22142 case DW_OP_GNU_convert:
22143 case DW_OP_GNU_reinterpret:
22144 if (valx1->val_class != valy1->val_class)
22145 return false;
22146 if (valx1->val_class == dw_val_class_unsigned_const)
22147 return valx1->v.val_unsigned == valy1->v.val_unsigned;
22148 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22149 case DW_OP_GNU_parameter_ref:
22150 return valx1->val_class == dw_val_class_die_ref
22151 && valx1->val_class == valy1->val_class
22152 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22153 default:
22154 /* Other codes have no operands. */
22155 return true;
22156 }
22157 }
22158
22159 /* Return true if DWARF location expressions X and Y are the same. */
22160
22161 static inline bool
22162 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22163 {
22164 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22165 if (x->dw_loc_opc != y->dw_loc_opc
22166 || x->dtprel != y->dtprel
22167 || !compare_loc_operands (x, y))
22168 break;
22169 return x == NULL && y == NULL;
22170 }
22171
22172 /* Return precomputed hash of location list X. */
22173
22174 static hashval_t
22175 loc_list_hash (const void *x)
22176 {
22177 return ((const struct dw_loc_list_struct *) x)->hash;
22178 }
22179
22180 /* Return 1 if location lists X and Y are the same. */
22181
22182 static int
22183 loc_list_eq (const void *x, const void *y)
22184 {
22185 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22186 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22187 if (a == b)
22188 return 1;
22189 if (a->hash != b->hash)
22190 return 0;
22191 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22192 if (strcmp (a->begin, b->begin) != 0
22193 || strcmp (a->end, b->end) != 0
22194 || (a->section == NULL) != (b->section == NULL)
22195 || (a->section && strcmp (a->section, b->section) != 0)
22196 || !compare_locs (a->expr, b->expr))
22197 break;
22198 return a == NULL && b == NULL;
22199 }
22200
22201 /* Recursively optimize location lists referenced from DIE
22202 children and share them whenever possible. */
22203
22204 static void
22205 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22206 {
22207 dw_die_ref c;
22208 dw_attr_ref a;
22209 unsigned ix;
22210 void **slot;
22211
22212 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22213 if (AT_class (a) == dw_val_class_loc_list)
22214 {
22215 dw_loc_list_ref list = AT_loc_list (a);
22216 /* TODO: perform some optimizations here, before hashing
22217 it and storing into the hash table. */
22218 hash_loc_list (list);
22219 slot = htab_find_slot_with_hash (htab, list, list->hash,
22220 INSERT);
22221 if (*slot == NULL)
22222 *slot = (void *) list;
22223 else
22224 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22225 }
22226
22227 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22228 }
22229
22230 /* Optimize location lists referenced from DIE
22231 children and share them whenever possible. */
22232
22233 static void
22234 optimize_location_lists (dw_die_ref die)
22235 {
22236 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22237 optimize_location_lists_1 (die, htab);
22238 htab_delete (htab);
22239 }
22240 \f
22241 /* Output stuff that dwarf requires at the end of every file,
22242 and generate the DWARF-2 debugging info. */
22243
22244 static void
22245 dwarf2out_finish (const char *filename)
22246 {
22247 limbo_die_node *node, *next_node;
22248 comdat_type_node *ctnode;
22249 htab_t comdat_type_table;
22250 unsigned int i;
22251
22252 /* PCH might result in DW_AT_producer string being restored from the
22253 header compilation, fix it up if needed. */
22254 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
22255 if (strcmp (AT_string (producer), producer_string) != 0)
22256 {
22257 struct indirect_string_node *node = find_AT_string (producer_string);
22258 producer->dw_attr_val.v.val_str = node;
22259 }
22260
22261 gen_scheduled_generic_parms_dies ();
22262 gen_remaining_tmpl_value_param_die_attribute ();
22263
22264 /* Add the name for the main input file now. We delayed this from
22265 dwarf2out_init to avoid complications with PCH. */
22266 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22267 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
22268 add_comp_dir_attribute (comp_unit_die ());
22269 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22270 {
22271 bool p = false;
22272 htab_traverse (file_table, file_table_relative_p, &p);
22273 if (p)
22274 add_comp_dir_attribute (comp_unit_die ());
22275 }
22276
22277 for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22278 {
22279 add_location_or_const_value_attribute (
22280 VEC_index (deferred_locations, deferred_locations_list, i).die,
22281 VEC_index (deferred_locations, deferred_locations_list, i).variable,
22282 false,
22283 DW_AT_location);
22284 }
22285
22286 /* Traverse the limbo die list, and add parent/child links. The only
22287 dies without parents that should be here are concrete instances of
22288 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
22289 For concrete instances, we can get the parent die from the abstract
22290 instance. */
22291 for (node = limbo_die_list; node; node = next_node)
22292 {
22293 dw_die_ref die = node->die;
22294 next_node = node->next;
22295
22296 if (die->die_parent == NULL)
22297 {
22298 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22299
22300 if (origin && origin->die_parent)
22301 add_child_die (origin->die_parent, die);
22302 else if (is_cu_die (die))
22303 ;
22304 else if (seen_error ())
22305 /* It's OK to be confused by errors in the input. */
22306 add_child_die (comp_unit_die (), die);
22307 else
22308 {
22309 /* In certain situations, the lexical block containing a
22310 nested function can be optimized away, which results
22311 in the nested function die being orphaned. Likewise
22312 with the return type of that nested function. Force
22313 this to be a child of the containing function.
22314
22315 It may happen that even the containing function got fully
22316 inlined and optimized out. In that case we are lost and
22317 assign the empty child. This should not be big issue as
22318 the function is likely unreachable too. */
22319 gcc_assert (node->created_for);
22320
22321 if (DECL_P (node->created_for))
22322 origin = get_context_die (DECL_CONTEXT (node->created_for));
22323 else if (TYPE_P (node->created_for))
22324 origin = scope_die_for (node->created_for, comp_unit_die ());
22325 else
22326 origin = comp_unit_die ();
22327
22328 add_child_die (origin, die);
22329 }
22330 }
22331 }
22332
22333 limbo_die_list = NULL;
22334
22335 #if ENABLE_ASSERT_CHECKING
22336 {
22337 dw_die_ref die = comp_unit_die (), c;
22338 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22339 }
22340 #endif
22341 resolve_addr (comp_unit_die ());
22342 move_marked_base_types ();
22343
22344 for (node = deferred_asm_name; node; node = node->next)
22345 {
22346 tree decl = node->created_for;
22347 /* When generating LTO bytecode we can not generate new assembler
22348 names at this point and all important decls got theirs via
22349 free-lang-data. */
22350 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
22351 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22352 {
22353 add_linkage_attr (node->die, decl);
22354 move_linkage_attr (node->die);
22355 }
22356 }
22357
22358 deferred_asm_name = NULL;
22359
22360 /* Walk through the list of incomplete types again, trying once more to
22361 emit full debugging info for them. */
22362 retry_incomplete_types ();
22363
22364 if (flag_eliminate_unused_debug_types)
22365 prune_unused_types ();
22366
22367 /* Generate separate COMDAT sections for type DIEs. */
22368 if (use_debug_types)
22369 {
22370 break_out_comdat_types (comp_unit_die ());
22371
22372 /* Each new type_unit DIE was added to the limbo die list when created.
22373 Since these have all been added to comdat_type_list, clear the
22374 limbo die list. */
22375 limbo_die_list = NULL;
22376
22377 /* For each new comdat type unit, copy declarations for incomplete
22378 types to make the new unit self-contained (i.e., no direct
22379 references to the main compile unit). */
22380 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22381 copy_decls_for_unworthy_types (ctnode->root_die);
22382 copy_decls_for_unworthy_types (comp_unit_die ());
22383
22384 /* In the process of copying declarations from one unit to another,
22385 we may have left some declarations behind that are no longer
22386 referenced. Prune them. */
22387 prune_unused_types ();
22388 }
22389
22390 /* Generate separate CUs for each of the include files we've seen.
22391 They will go into limbo_die_list. */
22392 if (flag_eliminate_dwarf2_dups)
22393 break_out_includes (comp_unit_die ());
22394
22395 /* Traverse the DIE's and add add sibling attributes to those DIE's
22396 that have children. */
22397 add_sibling_attributes (comp_unit_die ());
22398 for (node = limbo_die_list; node; node = node->next)
22399 add_sibling_attributes (node->die);
22400 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22401 add_sibling_attributes (ctnode->root_die);
22402
22403 /* Output a terminator label for the .text section. */
22404 switch_to_section (text_section);
22405 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22406 if (cold_text_section)
22407 {
22408 switch_to_section (cold_text_section);
22409 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22410 }
22411
22412 /* We can only use the low/high_pc attributes if all of the code was
22413 in .text. */
22414 if (!have_multiple_function_sections
22415 || (dwarf_version < 3 && dwarf_strict))
22416 {
22417 /* Don't add if the CU has no associated code. */
22418 if (text_section_used)
22419 add_AT_low_high_pc (comp_unit_die (), text_section_label,
22420 text_end_label);
22421 }
22422 else
22423 {
22424 unsigned fde_idx;
22425 dw_fde_ref fde;
22426 bool range_list_added = false;
22427
22428 if (text_section_used)
22429 add_ranges_by_labels (comp_unit_die (), text_section_label,
22430 text_end_label, &range_list_added);
22431 if (cold_text_section_used)
22432 add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22433 cold_end_label, &range_list_added);
22434
22435 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
22436 {
22437 if (DECL_IGNORED_P (fde->decl))
22438 continue;
22439 if (!fde->in_std_section)
22440 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22441 fde->dw_fde_end, &range_list_added);
22442 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22443 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22444 fde->dw_fde_second_end, &range_list_added);
22445 }
22446
22447 if (range_list_added)
22448 {
22449 /* We need to give .debug_loc and .debug_ranges an appropriate
22450 "base address". Use zero so that these addresses become
22451 absolute. Historically, we've emitted the unexpected
22452 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22453 Emit both to give time for other tools to adapt. */
22454 add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22455 if (! dwarf_strict && dwarf_version < 4)
22456 add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22457
22458 add_ranges (NULL);
22459 }
22460 }
22461
22462 if (debug_info_level >= DINFO_LEVEL_NORMAL)
22463 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22464 debug_line_section_label);
22465
22466 if (have_macinfo)
22467 add_AT_macptr (comp_unit_die (),
22468 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
22469 macinfo_section_label);
22470
22471 if (have_location_lists)
22472 optimize_location_lists (comp_unit_die ());
22473
22474 /* Output all of the compilation units. We put the main one last so that
22475 the offsets are available to output_pubnames. */
22476 for (node = limbo_die_list; node; node = node->next)
22477 output_comp_unit (node->die, 0);
22478
22479 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22480 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22481 {
22482 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22483
22484 /* Don't output duplicate types. */
22485 if (*slot != HTAB_EMPTY_ENTRY)
22486 continue;
22487
22488 /* Add a pointer to the line table for the main compilation unit
22489 so that the debugger can make sense of DW_AT_decl_file
22490 attributes. */
22491 if (debug_info_level >= DINFO_LEVEL_NORMAL)
22492 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22493 debug_line_section_label);
22494
22495 output_comdat_type_unit (ctnode);
22496 *slot = ctnode;
22497 }
22498 htab_delete (comdat_type_table);
22499
22500 add_AT_pubnames (comp_unit_die ());
22501
22502 /* Output the main compilation unit if non-empty or if .debug_macinfo
22503 or .debug_macro will be emitted. */
22504 output_comp_unit (comp_unit_die (), have_macinfo);
22505
22506 /* Output the abbreviation table. */
22507 if (abbrev_die_table_in_use != 1)
22508 {
22509 switch_to_section (debug_abbrev_section);
22510 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22511 output_abbrev_section ();
22512 }
22513
22514 /* Output location list section if necessary. */
22515 if (have_location_lists)
22516 {
22517 /* Output the location lists info. */
22518 switch_to_section (debug_loc_section);
22519 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22520 DEBUG_LOC_SECTION_LABEL, 0);
22521 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22522 output_location_lists (comp_unit_die ());
22523 }
22524
22525 /* Output public names and types tables if necessary. */
22526 output_pubnames (pubname_table);
22527 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22528 It shouldn't hurt to emit it always, since pure DWARF2 consumers
22529 simply won't look for the section. */
22530 output_pubnames (pubtype_table);
22531
22532 /* Output the address range information if a CU (.debug_info section)
22533 was emitted. We output an empty table even if we had no functions
22534 to put in it. This because the consumer has no way to tell the
22535 difference between an empty table that we omitted and failure to
22536 generate a table that would have contained data. */
22537 if (info_section_emitted)
22538 {
22539 unsigned long aranges_length = size_of_aranges ();
22540
22541 switch_to_section (debug_aranges_section);
22542 output_aranges (aranges_length);
22543 }
22544
22545 /* Output ranges section if necessary. */
22546 if (ranges_table_in_use)
22547 {
22548 switch_to_section (debug_ranges_section);
22549 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22550 output_ranges ();
22551 }
22552
22553 /* Have to end the macro section. */
22554 if (have_macinfo)
22555 {
22556 switch_to_section (debug_macinfo_section);
22557 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22558 output_macinfo ();
22559 dw2_asm_output_data (1, 0, "End compilation unit");
22560 }
22561
22562 /* Output the source line correspondence table. We must do this
22563 even if there is no line information. Otherwise, on an empty
22564 translation unit, we will generate a present, but empty,
22565 .debug_info section. IRIX 6.5 `nm' will then complain when
22566 examining the file. This is done late so that any filenames
22567 used by the debug_info section are marked as 'used'. */
22568 switch_to_section (debug_line_section);
22569 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22570 if (! DWARF2_ASM_LINE_DEBUG_INFO)
22571 output_line_info ();
22572
22573 /* If we emitted any DW_FORM_strp form attribute, output the string
22574 table too. */
22575 if (debug_str_hash)
22576 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22577 }
22578
22579 #include "gt-dwarf2out.h"