* dwarf2out.c (is_redundant_typedef): Add 'inline' to prototype.
[official-gcc.git] / gcc / dwarf2out.c
blobf3c4c0962a5aa883f4dc2458991e80b6d31f1664
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
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).
9 This file is part of GCC.
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.
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.
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/>. */
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. */
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. */
37 /* DWARF2 Abbreviation Glossary:
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.
45 CFI = Call Frame Instruction
46 an instruction for the DWARF2 abstract machine
48 CIE = Common Information Entry
49 information describing information common to one or more FDEs
51 DIE = Debugging Information Entry
53 FDE = Frame Description Entry
54 information describing the stack call frame, in particular,
55 how to restore registers
57 DW_CFA_... = DWARF2 CFA call frame instruction
58 DW_TAG_... = DWARF2 DIE tag */
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 "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "tree-pass.h"
94 #include "tree-flow.h"
95 #include "cfglayout.h"
97 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
98 static rtx last_var_location_insn;
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103 /* Define this macro to be a nonzero value if the directory specifications
104 which are output in the debug info should end with a separator. */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107 from generating indirect strings in DWARF2 debug information, for instance
108 if your target is stuck with an old version of GDB that is unable to
109 process them properly or uses VMS Debug. */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
116 /* ??? Poison these here until it can be done generically. They've been
117 totally replaced in this file; make sure it stays that way. */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
124 #ifndef INCOMING_RETURN_ADDR_RTX
125 #define INCOMING_RETURN_ADDR_RTX (gcc_unreachable (), NULL_RTX)
126 #endif
128 /* Map register numbers held in the call frame info that gcc has
129 collected using DWARF_FRAME_REGNUM to those that should be output in
130 .debug_frame and .eh_frame. */
131 #ifndef DWARF2_FRAME_REG_OUT
132 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
133 #endif
135 /* Save the result of dwarf2out_do_frame across PCH. */
136 static GTY(()) bool saved_do_cfi_asm = 0;
138 /* Decide whether we want to emit frame unwind information for the current
139 translation unit. */
142 dwarf2out_do_frame (void)
144 /* We want to emit correct CFA location expressions or lists, so we
145 have to return true if we're going to output debug info, even if
146 we're not going to output frame or unwind info. */
147 if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
148 return true;
150 if (saved_do_cfi_asm)
151 return true;
153 if (targetm.debug_unwind_info () == UI_DWARF2)
154 return true;
156 if ((flag_unwind_tables || flag_exceptions)
157 && targetm.except_unwind_info (&global_options) == UI_DWARF2)
158 return true;
160 return false;
163 /* Decide whether to emit frame unwind via assembler directives. */
166 dwarf2out_do_cfi_asm (void)
168 int enc;
170 #ifdef MIPS_DEBUGGING_INFO
171 return false;
172 #endif
173 if (saved_do_cfi_asm)
174 return true;
175 if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
176 return false;
177 if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
178 return false;
180 /* Make sure the personality encoding is one the assembler can support.
181 In particular, aligned addresses can't be handled. */
182 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
183 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
184 return false;
185 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
186 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
187 return false;
189 /* If we can't get the assembler to emit only .debug_frame, and we don't need
190 dwarf2 unwind info for exceptions, then emit .debug_frame by hand. */
191 if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
192 && !flag_unwind_tables && !flag_exceptions
193 && targetm.except_unwind_info (&global_options) != UI_DWARF2)
194 return false;
196 saved_do_cfi_asm = true;
197 return true;
200 /* The size of the target's pointer type. */
201 #ifndef PTR_SIZE
202 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
203 #endif
205 /* Array of RTXes referenced by the debugging information, which therefore
206 must be kept around forever. */
207 static GTY(()) VEC(rtx,gc) *used_rtx_array;
209 /* A pointer to the base of a list of incomplete types which might be
210 completed at some later time. incomplete_types_list needs to be a
211 VEC(tree,gc) because we want to tell the garbage collector about
212 it. */
213 static GTY(()) VEC(tree,gc) *incomplete_types;
215 /* A pointer to the base of a table of references to declaration
216 scopes. This table is a display which tracks the nesting
217 of declaration scopes at the current scope and containing
218 scopes. This table is used to find the proper place to
219 define type declaration DIE's. */
220 static GTY(()) VEC(tree,gc) *decl_scope_table;
222 /* Pointers to various DWARF2 sections. */
223 static GTY(()) section *debug_info_section;
224 static GTY(()) section *debug_abbrev_section;
225 static GTY(()) section *debug_aranges_section;
226 static GTY(()) section *debug_macinfo_section;
227 static GTY(()) section *debug_line_section;
228 static GTY(()) section *debug_loc_section;
229 static GTY(()) section *debug_pubnames_section;
230 static GTY(()) section *debug_pubtypes_section;
231 static GTY(()) section *debug_str_section;
232 static GTY(()) section *debug_ranges_section;
233 static GTY(()) section *debug_frame_section;
235 /* Personality decl of current unit. Used only when assembler does not support
236 personality CFI. */
237 static GTY(()) rtx current_unit_personality;
239 /* How to start an assembler comment. */
240 #ifndef ASM_COMMENT_START
241 #define ASM_COMMENT_START ";#"
242 #endif
244 typedef struct dw_cfi_struct *dw_cfi_ref;
245 typedef struct dw_fde_struct *dw_fde_ref;
246 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
248 /* Call frames are described using a sequence of Call Frame
249 Information instructions. The register number, offset
250 and address fields are provided as possible operands;
251 their use is selected by the opcode field. */
253 enum dw_cfi_oprnd_type {
254 dw_cfi_oprnd_unused,
255 dw_cfi_oprnd_reg_num,
256 dw_cfi_oprnd_offset,
257 dw_cfi_oprnd_addr,
258 dw_cfi_oprnd_loc
261 typedef union GTY(()) dw_cfi_oprnd_struct {
262 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
263 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
264 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
265 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
267 dw_cfi_oprnd;
269 typedef struct GTY(()) dw_cfi_struct {
270 enum dwarf_call_frame_info dw_cfi_opc;
271 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
272 dw_cfi_oprnd1;
273 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
274 dw_cfi_oprnd2;
276 dw_cfi_node;
278 DEF_VEC_P (dw_cfi_ref);
279 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
280 DEF_VEC_ALLOC_P (dw_cfi_ref, gc);
282 typedef VEC(dw_cfi_ref, gc) *cfi_vec;
284 /* This is how we define the location of the CFA. We use to handle it
285 as REG + OFFSET all the time, but now it can be more complex.
286 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
287 Instead of passing around REG and OFFSET, we pass a copy
288 of this structure. */
289 typedef struct cfa_loc {
290 HOST_WIDE_INT offset;
291 HOST_WIDE_INT base_offset;
292 unsigned int reg;
293 BOOL_BITFIELD indirect : 1; /* 1 if CFA is accessed via a dereference. */
294 BOOL_BITFIELD in_use : 1; /* 1 if a saved cfa is stored here. */
295 } dw_cfa_location;
297 /* All call frame descriptions (FDE's) in the GCC generated DWARF
298 refer to a single Common Information Entry (CIE), defined at
299 the beginning of the .debug_frame section. This use of a single
300 CIE obviates the need to keep track of multiple CIE's
301 in the DWARF generation routines below. */
303 typedef struct GTY(()) dw_fde_struct {
304 tree decl;
305 const char *dw_fde_begin;
306 const char *dw_fde_current_label;
307 const char *dw_fde_end;
308 const char *dw_fde_vms_end_prologue;
309 const char *dw_fde_vms_begin_epilogue;
310 const char *dw_fde_second_begin;
311 const char *dw_fde_second_end;
312 cfi_vec dw_fde_cfi;
313 int dw_fde_switch_cfi_index; /* Last CFI before switching sections. */
314 HOST_WIDE_INT stack_realignment;
315 unsigned funcdef_number;
316 /* Dynamic realign argument pointer register. */
317 unsigned int drap_reg;
318 /* Virtual dynamic realign argument pointer register. */
319 unsigned int vdrap_reg;
320 /* These 3 flags are copied from rtl_data in function.h. */
321 unsigned all_throwers_are_sibcalls : 1;
322 unsigned uses_eh_lsda : 1;
323 unsigned nothrow : 1;
324 /* Whether we did stack realign in this call frame. */
325 unsigned stack_realign : 1;
326 /* Whether dynamic realign argument pointer register has been saved. */
327 unsigned drap_reg_saved: 1;
328 /* True iff dw_fde_begin label is in text_section or cold_text_section. */
329 unsigned in_std_section : 1;
330 /* True iff dw_fde_second_begin label is in text_section or
331 cold_text_section. */
332 unsigned second_in_std_section : 1;
334 dw_fde_node;
336 /* Maximum size (in bytes) of an artificially generated label. */
337 #define MAX_ARTIFICIAL_LABEL_BYTES 30
339 /* The size of addresses as they appear in the Dwarf 2 data.
340 Some architectures use word addresses to refer to code locations,
341 but Dwarf 2 info always uses byte addresses. On such machines,
342 Dwarf 2 addresses need to be larger than the architecture's
343 pointers. */
344 #ifndef DWARF2_ADDR_SIZE
345 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
346 #endif
348 /* The size in bytes of a DWARF field indicating an offset or length
349 relative to a debug info section, specified to be 4 bytes in the
350 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
351 as PTR_SIZE. */
353 #ifndef DWARF_OFFSET_SIZE
354 #define DWARF_OFFSET_SIZE 4
355 #endif
357 /* The size in bytes of a DWARF 4 type signature. */
359 #ifndef DWARF_TYPE_SIGNATURE_SIZE
360 #define DWARF_TYPE_SIGNATURE_SIZE 8
361 #endif
363 /* According to the (draft) DWARF 3 specification, the initial length
364 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
365 bytes are 0xffffffff, followed by the length stored in the next 8
366 bytes.
368 However, the SGI/MIPS ABI uses an initial length which is equal to
369 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
371 #ifndef DWARF_INITIAL_LENGTH_SIZE
372 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
373 #endif
375 /* Round SIZE up to the nearest BOUNDARY. */
376 #define DWARF_ROUND(SIZE,BOUNDARY) \
377 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
379 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
380 #ifndef DWARF_CIE_DATA_ALIGNMENT
381 #ifdef STACK_GROWS_DOWNWARD
382 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
383 #else
384 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
385 #endif
386 #endif
388 /* CIE identifier. */
389 #if HOST_BITS_PER_WIDE_INT >= 64
390 #define DWARF_CIE_ID \
391 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
392 #else
393 #define DWARF_CIE_ID DW_CIE_ID
394 #endif
396 /* A pointer to the base of a table that contains frame description
397 information for each routine. */
398 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
400 /* Number of elements currently allocated for fde_table. */
401 static GTY(()) unsigned fde_table_allocated;
403 /* Number of elements in fde_table currently in use. */
404 static GTY(()) unsigned fde_table_in_use;
406 /* Size (in elements) of increments by which we may expand the
407 fde_table. */
408 #define FDE_TABLE_INCREMENT 256
410 /* Get the current fde_table entry we should use. */
412 static inline dw_fde_ref
413 current_fde (void)
415 return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
418 /* A vector of call frame insns for the CIE. */
419 static GTY(()) cfi_vec cie_cfi_vec;
421 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
422 attribute that accelerates the lookup of the FDE associated
423 with the subprogram. This variable holds the table index of the FDE
424 associated with the current function (body) definition. */
425 static unsigned current_funcdef_fde;
427 struct GTY(()) indirect_string_node {
428 const char *str;
429 unsigned int refcount;
430 enum dwarf_form form;
431 char *label;
434 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
436 /* True if the compilation unit has location entries that reference
437 debug strings. */
438 static GTY(()) bool debug_str_hash_forced = false;
440 static GTY(()) int dw2_string_counter;
441 static GTY(()) unsigned long dwarf2out_cfi_label_num;
443 /* True if the compilation unit places functions in more than one section. */
444 static GTY(()) bool have_multiple_function_sections = false;
446 /* Whether the default text and cold text sections have been used at all. */
448 static GTY(()) bool text_section_used = false;
449 static GTY(()) bool cold_text_section_used = false;
451 /* The default cold text section. */
452 static GTY(()) section *cold_text_section;
454 /* Forward declarations for functions defined in this file. */
456 static char *stripattributes (const char *);
457 static const char *dwarf_cfi_name (unsigned);
458 static dw_cfi_ref new_cfi (void);
459 static void add_cfi (cfi_vec *, dw_cfi_ref);
460 static void add_fde_cfi (const char *, dw_cfi_ref);
461 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
462 static void lookup_cfa (dw_cfa_location *);
463 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
464 static void initial_return_save (rtx);
465 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
466 HOST_WIDE_INT);
467 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
468 static void output_cfi_directive (dw_cfi_ref);
469 static void output_call_frame_info (int);
470 static void dwarf2out_note_section_used (void);
471 static bool clobbers_queued_reg_save (const_rtx);
472 static void dwarf2out_frame_debug_expr (rtx, const char *);
474 /* Support for complex CFA locations. */
475 static void output_cfa_loc (dw_cfi_ref, int);
476 static void output_cfa_loc_raw (dw_cfi_ref);
477 static void get_cfa_from_loc_descr (dw_cfa_location *,
478 struct dw_loc_descr_struct *);
479 static struct dw_loc_descr_struct *build_cfa_loc
480 (dw_cfa_location *, HOST_WIDE_INT);
481 static struct dw_loc_descr_struct *build_cfa_aligned_loc
482 (HOST_WIDE_INT, HOST_WIDE_INT);
483 static void def_cfa_1 (const char *, dw_cfa_location *);
484 static struct dw_loc_descr_struct *mem_loc_descriptor
485 (rtx, enum machine_mode mode, enum var_init_status);
487 /* How to start an assembler comment. */
488 #ifndef ASM_COMMENT_START
489 #define ASM_COMMENT_START ";#"
490 #endif
492 /* Data and reference forms for relocatable data. */
493 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
494 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
496 #ifndef DEBUG_FRAME_SECTION
497 #define DEBUG_FRAME_SECTION ".debug_frame"
498 #endif
500 #ifndef FUNC_BEGIN_LABEL
501 #define FUNC_BEGIN_LABEL "LFB"
502 #endif
504 #ifndef FUNC_END_LABEL
505 #define FUNC_END_LABEL "LFE"
506 #endif
508 #ifndef PROLOGUE_END_LABEL
509 #define PROLOGUE_END_LABEL "LPE"
510 #endif
512 #ifndef EPILOGUE_BEGIN_LABEL
513 #define EPILOGUE_BEGIN_LABEL "LEB"
514 #endif
516 #ifndef FRAME_BEGIN_LABEL
517 #define FRAME_BEGIN_LABEL "Lframe"
518 #endif
519 #define CIE_AFTER_SIZE_LABEL "LSCIE"
520 #define CIE_END_LABEL "LECIE"
521 #define FDE_LABEL "LSFDE"
522 #define FDE_AFTER_SIZE_LABEL "LASFDE"
523 #define FDE_END_LABEL "LEFDE"
524 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
525 #define LINE_NUMBER_END_LABEL "LELT"
526 #define LN_PROLOG_AS_LABEL "LASLTP"
527 #define LN_PROLOG_END_LABEL "LELTP"
528 #define DIE_LABEL_PREFIX "DW"
530 /* The DWARF 2 CFA column which tracks the return address. Normally this
531 is the column for PC, or the first column after all of the hard
532 registers. */
533 #ifndef DWARF_FRAME_RETURN_COLUMN
534 #ifdef PC_REGNUM
535 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
536 #else
537 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
538 #endif
539 #endif
541 /* The mapping from gcc register number to DWARF 2 CFA column number. By
542 default, we just provide columns for all registers. */
543 #ifndef DWARF_FRAME_REGNUM
544 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
545 #endif
547 /* Match the base name of a file to the base name of a compilation unit. */
549 static int
550 matches_main_base (const char *path)
552 /* Cache the last query. */
553 static const char *last_path = NULL;
554 static int last_match = 0;
555 if (path != last_path)
557 const char *base;
558 int length = base_of_path (path, &base);
559 last_path = path;
560 last_match = (length == main_input_baselength
561 && memcmp (base, main_input_basename, length) == 0);
563 return last_match;
566 #ifdef DEBUG_DEBUG_STRUCT
568 static int
569 dump_struct_debug (tree type, enum debug_info_usage usage,
570 enum debug_struct_file criterion, int generic,
571 int matches, int result)
573 /* Find the type name. */
574 tree type_decl = TYPE_STUB_DECL (type);
575 tree t = type_decl;
576 const char *name = 0;
577 if (TREE_CODE (t) == TYPE_DECL)
578 t = DECL_NAME (t);
579 if (t)
580 name = IDENTIFIER_POINTER (t);
582 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
583 criterion,
584 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
585 matches ? "bas" : "hdr",
586 generic ? "gen" : "ord",
587 usage == DINFO_USAGE_DFN ? ";" :
588 usage == DINFO_USAGE_DIR_USE ? "." : "*",
589 result,
590 (void*) type_decl, name);
591 return result;
593 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
594 dump_struct_debug (type, usage, criterion, generic, matches, result)
596 #else
598 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
599 (result)
601 #endif
603 static bool
604 should_emit_struct_debug (tree type, enum debug_info_usage usage)
606 enum debug_struct_file criterion;
607 tree type_decl;
608 bool generic = lang_hooks.types.generic_p (type);
610 if (generic)
611 criterion = debug_struct_generic[usage];
612 else
613 criterion = debug_struct_ordinary[usage];
615 if (criterion == DINFO_STRUCT_FILE_NONE)
616 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
617 if (criterion == DINFO_STRUCT_FILE_ANY)
618 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
620 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
622 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
623 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
625 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
626 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
627 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
630 /* Hook used by __throw. */
633 expand_builtin_dwarf_sp_column (void)
635 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
636 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
639 /* Return a pointer to a copy of the section string name S with all
640 attributes stripped off, and an asterisk prepended (for assemble_name). */
642 static inline char *
643 stripattributes (const char *s)
645 char *stripped = XNEWVEC (char, strlen (s) + 2);
646 char *p = stripped;
648 *p++ = '*';
650 while (*s && *s != ',')
651 *p++ = *s++;
653 *p = '\0';
654 return stripped;
657 /* MEM is a memory reference for the register size table, each element of
658 which has mode MODE. Initialize column C as a return address column. */
660 static void
661 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
663 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
664 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
665 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
668 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder. */
670 static inline HOST_WIDE_INT
671 div_data_align (HOST_WIDE_INT off)
673 HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
674 gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
675 return r;
678 /* Return true if we need a signed version of a given opcode
679 (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended). */
681 static inline bool
682 need_data_align_sf_opcode (HOST_WIDE_INT off)
684 return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
687 /* Generate code to initialize the register size table. */
689 void
690 expand_builtin_init_dwarf_reg_sizes (tree address)
692 unsigned int i;
693 enum machine_mode mode = TYPE_MODE (char_type_node);
694 rtx addr = expand_normal (address);
695 rtx mem = gen_rtx_MEM (BLKmode, addr);
696 bool wrote_return_column = false;
698 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
700 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
702 if (rnum < DWARF_FRAME_REGISTERS)
704 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
705 enum machine_mode save_mode = reg_raw_mode[i];
706 HOST_WIDE_INT size;
708 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
709 save_mode = choose_hard_reg_mode (i, 1, true);
710 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
712 if (save_mode == VOIDmode)
713 continue;
714 wrote_return_column = true;
716 size = GET_MODE_SIZE (save_mode);
717 if (offset < 0)
718 continue;
720 emit_move_insn (adjust_address (mem, mode, offset),
721 gen_int_mode (size, mode));
725 if (!wrote_return_column)
726 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
728 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
729 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
730 #endif
732 targetm.init_dwarf_reg_sizes_extra (address);
735 /* Convert a DWARF call frame info. operation to its string name */
737 static const char *
738 dwarf_cfi_name (unsigned int cfi_opc)
740 switch (cfi_opc)
742 case DW_CFA_advance_loc:
743 return "DW_CFA_advance_loc";
744 case DW_CFA_offset:
745 return "DW_CFA_offset";
746 case DW_CFA_restore:
747 return "DW_CFA_restore";
748 case DW_CFA_nop:
749 return "DW_CFA_nop";
750 case DW_CFA_set_loc:
751 return "DW_CFA_set_loc";
752 case DW_CFA_advance_loc1:
753 return "DW_CFA_advance_loc1";
754 case DW_CFA_advance_loc2:
755 return "DW_CFA_advance_loc2";
756 case DW_CFA_advance_loc4:
757 return "DW_CFA_advance_loc4";
758 case DW_CFA_offset_extended:
759 return "DW_CFA_offset_extended";
760 case DW_CFA_restore_extended:
761 return "DW_CFA_restore_extended";
762 case DW_CFA_undefined:
763 return "DW_CFA_undefined";
764 case DW_CFA_same_value:
765 return "DW_CFA_same_value";
766 case DW_CFA_register:
767 return "DW_CFA_register";
768 case DW_CFA_remember_state:
769 return "DW_CFA_remember_state";
770 case DW_CFA_restore_state:
771 return "DW_CFA_restore_state";
772 case DW_CFA_def_cfa:
773 return "DW_CFA_def_cfa";
774 case DW_CFA_def_cfa_register:
775 return "DW_CFA_def_cfa_register";
776 case DW_CFA_def_cfa_offset:
777 return "DW_CFA_def_cfa_offset";
779 /* DWARF 3 */
780 case DW_CFA_def_cfa_expression:
781 return "DW_CFA_def_cfa_expression";
782 case DW_CFA_expression:
783 return "DW_CFA_expression";
784 case DW_CFA_offset_extended_sf:
785 return "DW_CFA_offset_extended_sf";
786 case DW_CFA_def_cfa_sf:
787 return "DW_CFA_def_cfa_sf";
788 case DW_CFA_def_cfa_offset_sf:
789 return "DW_CFA_def_cfa_offset_sf";
791 /* SGI/MIPS specific */
792 case DW_CFA_MIPS_advance_loc8:
793 return "DW_CFA_MIPS_advance_loc8";
795 /* GNU extensions */
796 case DW_CFA_GNU_window_save:
797 return "DW_CFA_GNU_window_save";
798 case DW_CFA_GNU_args_size:
799 return "DW_CFA_GNU_args_size";
800 case DW_CFA_GNU_negative_offset_extended:
801 return "DW_CFA_GNU_negative_offset_extended";
803 default:
804 return "DW_CFA_<unknown>";
808 /* Return a pointer to a newly allocated Call Frame Instruction. */
810 static inline dw_cfi_ref
811 new_cfi (void)
813 dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
815 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
816 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
818 return cfi;
821 /* Add a Call Frame Instruction to list of instructions. */
823 static inline void
824 add_cfi (cfi_vec *vec, dw_cfi_ref cfi)
826 dw_fde_ref fde = current_fde ();
828 /* When DRAP is used, CFA is defined with an expression. Redefine
829 CFA may lead to a different CFA value. */
830 /* ??? Of course, this heuristic fails when we're annotating epilogues,
831 because of course we'll always want to redefine the CFA back to the
832 stack pointer on the way out. Where should we move this check? */
833 if (0 && fde && fde->drap_reg != INVALID_REGNUM)
834 switch (cfi->dw_cfi_opc)
836 case DW_CFA_def_cfa_register:
837 case DW_CFA_def_cfa_offset:
838 case DW_CFA_def_cfa_offset_sf:
839 case DW_CFA_def_cfa:
840 case DW_CFA_def_cfa_sf:
841 gcc_unreachable ();
843 default:
844 break;
847 VEC_safe_push (dw_cfi_ref, gc, *vec, cfi);
850 /* Generate a new label for the CFI info to refer to. FORCE is true
851 if a label needs to be output even when using .cfi_* directives. */
853 char *
854 dwarf2out_cfi_label (bool force)
856 static char label[20];
858 if (!force && dwarf2out_do_cfi_asm ())
860 /* In this case, we will be emitting the asm directive instead of
861 the label, so just return a placeholder to keep the rest of the
862 interfaces happy. */
863 strcpy (label, "<do not output>");
865 else
867 int num = dwarf2out_cfi_label_num++;
868 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
869 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
872 return label;
875 /* True if remember_state should be emitted before following CFI directive. */
876 static bool emit_cfa_remember;
878 /* True if any CFI directives were emitted at the current insn. */
879 static bool any_cfis_emitted;
881 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
882 or to the CIE if LABEL is NULL. */
884 static void
885 add_fde_cfi (const char *label, dw_cfi_ref cfi)
887 cfi_vec *vec;
889 if (cie_cfi_vec == NULL)
890 cie_cfi_vec = VEC_alloc (dw_cfi_ref, gc, 20);
892 vec = &cie_cfi_vec;
894 if (emit_cfa_remember)
896 dw_cfi_ref cfi_remember;
898 /* Emit the state save. */
899 emit_cfa_remember = false;
900 cfi_remember = new_cfi ();
901 cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
902 add_fde_cfi (label, cfi_remember);
905 if (dwarf2out_do_cfi_asm ())
907 if (label)
909 dw_fde_ref fde = current_fde ();
911 gcc_assert (fde != NULL);
913 /* We still have to add the cfi to the list so that lookup_cfa
914 works later on. When -g2 and above we even need to force
915 emitting of CFI labels and add to list a DW_CFA_set_loc for
916 convert_cfa_to_fb_loc_list purposes. If we're generating
917 DWARF3 output we use DW_OP_call_frame_cfa and so don't use
918 convert_cfa_to_fb_loc_list. */
919 if (dwarf_version == 2
920 && debug_info_level > DINFO_LEVEL_TERSE
921 && (write_symbols == DWARF2_DEBUG
922 || write_symbols == VMS_AND_DWARF2_DEBUG))
924 switch (cfi->dw_cfi_opc)
926 case DW_CFA_def_cfa_offset:
927 case DW_CFA_def_cfa_offset_sf:
928 case DW_CFA_def_cfa_register:
929 case DW_CFA_def_cfa:
930 case DW_CFA_def_cfa_sf:
931 case DW_CFA_def_cfa_expression:
932 case DW_CFA_restore_state:
933 if (*label == 0 || strcmp (label, "<do not output>") == 0)
934 label = dwarf2out_cfi_label (true);
936 if (fde->dw_fde_current_label == NULL
937 || strcmp (label, fde->dw_fde_current_label) != 0)
939 dw_cfi_ref xcfi;
941 label = xstrdup (label);
943 /* Set the location counter to the new label. */
944 xcfi = new_cfi ();
945 /* It doesn't metter whether DW_CFA_set_loc
946 or DW_CFA_advance_loc4 is added here, those aren't
947 emitted into assembly, only looked up by
948 convert_cfa_to_fb_loc_list. */
949 xcfi->dw_cfi_opc = DW_CFA_set_loc;
950 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
951 add_cfi (&fde->dw_fde_cfi, xcfi);
952 fde->dw_fde_current_label = label;
954 break;
955 default:
956 break;
960 output_cfi_directive (cfi);
962 vec = &fde->dw_fde_cfi;
963 any_cfis_emitted = true;
965 /* ??? If this is a CFI for the CIE, we don't emit. This
966 assumes that the standard CIE contents that the assembler
967 uses matches the standard CIE contents that the compiler
968 uses. This is probably a bad assumption. I'm not quite
969 sure how to address this for now. */
971 else if (label)
973 dw_fde_ref fde = current_fde ();
975 gcc_assert (fde != NULL);
977 if (*label == 0)
978 label = dwarf2out_cfi_label (false);
980 if (fde->dw_fde_current_label == NULL
981 || strcmp (label, fde->dw_fde_current_label) != 0)
983 dw_cfi_ref xcfi;
985 label = xstrdup (label);
987 /* Set the location counter to the new label. */
988 xcfi = new_cfi ();
989 /* If we have a current label, advance from there, otherwise
990 set the location directly using set_loc. */
991 xcfi->dw_cfi_opc = fde->dw_fde_current_label
992 ? DW_CFA_advance_loc4
993 : DW_CFA_set_loc;
994 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
995 add_cfi (&fde->dw_fde_cfi, xcfi);
997 fde->dw_fde_current_label = label;
1000 vec = &fde->dw_fde_cfi;
1001 any_cfis_emitted = true;
1004 add_cfi (vec, cfi);
1007 /* Subroutine of lookup_cfa. */
1009 static void
1010 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1012 switch (cfi->dw_cfi_opc)
1014 case DW_CFA_def_cfa_offset:
1015 case DW_CFA_def_cfa_offset_sf:
1016 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1017 break;
1018 case DW_CFA_def_cfa_register:
1019 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1020 break;
1021 case DW_CFA_def_cfa:
1022 case DW_CFA_def_cfa_sf:
1023 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1024 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1025 break;
1026 case DW_CFA_def_cfa_expression:
1027 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1028 break;
1030 case DW_CFA_remember_state:
1031 gcc_assert (!remember->in_use);
1032 *remember = *loc;
1033 remember->in_use = 1;
1034 break;
1035 case DW_CFA_restore_state:
1036 gcc_assert (remember->in_use);
1037 *loc = *remember;
1038 remember->in_use = 0;
1039 break;
1041 default:
1042 break;
1046 /* Find the previous value for the CFA. */
1048 static void
1049 lookup_cfa (dw_cfa_location *loc)
1051 int ix;
1052 dw_cfi_ref cfi;
1053 dw_fde_ref fde;
1054 dw_cfa_location remember;
1056 memset (loc, 0, sizeof (*loc));
1057 loc->reg = INVALID_REGNUM;
1058 remember = *loc;
1060 FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
1061 lookup_cfa_1 (cfi, loc, &remember);
1063 fde = current_fde ();
1064 if (fde)
1065 FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
1066 lookup_cfa_1 (cfi, loc, &remember);
1069 /* The current rule for calculating the DWARF2 canonical frame address. */
1070 static dw_cfa_location cfa;
1072 /* The register used for saving registers to the stack, and its offset
1073 from the CFA. */
1074 static dw_cfa_location cfa_store;
1076 /* The current save location around an epilogue. */
1077 static dw_cfa_location cfa_remember;
1079 /* The running total of the size of arguments pushed onto the stack. */
1080 static HOST_WIDE_INT args_size;
1082 /* The last args_size we actually output. */
1083 static HOST_WIDE_INT old_args_size;
1085 /* Entry point to update the canonical frame address (CFA).
1086 LABEL is passed to add_fde_cfi. The value of CFA is now to be
1087 calculated from REG+OFFSET. */
1089 void
1090 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1092 dw_cfa_location loc;
1093 loc.indirect = 0;
1094 loc.base_offset = 0;
1095 loc.reg = reg;
1096 loc.offset = offset;
1097 def_cfa_1 (label, &loc);
1100 /* Determine if two dw_cfa_location structures define the same data. */
1102 static bool
1103 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1105 return (loc1->reg == loc2->reg
1106 && loc1->offset == loc2->offset
1107 && loc1->indirect == loc2->indirect
1108 && (loc1->indirect == 0
1109 || loc1->base_offset == loc2->base_offset));
1112 /* This routine does the actual work. The CFA is now calculated from
1113 the dw_cfa_location structure. */
1115 static void
1116 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1118 dw_cfi_ref cfi;
1119 dw_cfa_location old_cfa, loc;
1121 cfa = *loc_p;
1122 loc = *loc_p;
1124 if (cfa_store.reg == loc.reg && loc.indirect == 0)
1125 cfa_store.offset = loc.offset;
1127 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1128 lookup_cfa (&old_cfa);
1130 /* If nothing changed, no need to issue any call frame instructions. */
1131 if (cfa_equal_p (&loc, &old_cfa))
1132 return;
1134 cfi = new_cfi ();
1136 if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1138 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1139 the CFA register did not change but the offset did. The data
1140 factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1141 in the assembler via the .cfi_def_cfa_offset directive. */
1142 if (loc.offset < 0)
1143 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1144 else
1145 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1146 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1149 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
1150 else if (loc.offset == old_cfa.offset
1151 && old_cfa.reg != INVALID_REGNUM
1152 && !loc.indirect
1153 && !old_cfa.indirect)
1155 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1156 indicating the CFA register has changed to <register> but the
1157 offset has not changed. */
1158 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1159 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1161 #endif
1163 else if (loc.indirect == 0)
1165 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1166 indicating the CFA register has changed to <register> with
1167 the specified offset. The data factoring for DW_CFA_def_cfa_sf
1168 happens in output_cfi, or in the assembler via the .cfi_def_cfa
1169 directive. */
1170 if (loc.offset < 0)
1171 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1172 else
1173 cfi->dw_cfi_opc = DW_CFA_def_cfa;
1174 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1175 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1177 else
1179 /* Construct a DW_CFA_def_cfa_expression instruction to
1180 calculate the CFA using a full location expression since no
1181 register-offset pair is available. */
1182 struct dw_loc_descr_struct *loc_list;
1184 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1185 loc_list = build_cfa_loc (&loc, 0);
1186 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1189 add_fde_cfi (label, cfi);
1192 /* Add the CFI for saving a register. REG is the CFA column number.
1193 LABEL is passed to add_fde_cfi.
1194 If SREG is -1, the register is saved at OFFSET from the CFA;
1195 otherwise it is saved in SREG. */
1197 static void
1198 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1200 dw_cfi_ref cfi = new_cfi ();
1201 dw_fde_ref fde = current_fde ();
1203 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1205 /* When stack is aligned, store REG using DW_CFA_expression with
1206 FP. */
1207 if (fde
1208 && fde->stack_realign
1209 && sreg == INVALID_REGNUM)
1211 cfi->dw_cfi_opc = DW_CFA_expression;
1212 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1213 cfi->dw_cfi_oprnd2.dw_cfi_loc
1214 = build_cfa_aligned_loc (offset, fde->stack_realignment);
1216 else if (sreg == INVALID_REGNUM)
1218 if (need_data_align_sf_opcode (offset))
1219 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1220 else if (reg & ~0x3f)
1221 cfi->dw_cfi_opc = DW_CFA_offset_extended;
1222 else
1223 cfi->dw_cfi_opc = DW_CFA_offset;
1224 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1226 else if (sreg == reg)
1227 cfi->dw_cfi_opc = DW_CFA_same_value;
1228 else
1230 cfi->dw_cfi_opc = DW_CFA_register;
1231 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1234 add_fde_cfi (label, cfi);
1237 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
1238 This CFI tells the unwinder that it needs to restore the window registers
1239 from the previous frame's window save area.
1241 ??? Perhaps we should note in the CIE where windows are saved (instead of
1242 assuming 0(cfa)) and what registers are in the window. */
1244 void
1245 dwarf2out_window_save (const char *label)
1247 dw_cfi_ref cfi = new_cfi ();
1249 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1250 add_fde_cfi (label, cfi);
1253 /* Entry point for saving a register to the stack. REG is the GCC register
1254 number. LABEL and OFFSET are passed to reg_save. */
1256 void
1257 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1259 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1262 /* Entry point for saving the return address in the stack.
1263 LABEL and OFFSET are passed to reg_save. */
1265 void
1266 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1268 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1271 /* Entry point for saving the return address in a register.
1272 LABEL and SREG are passed to reg_save. */
1274 void
1275 dwarf2out_return_reg (const char *label, unsigned int sreg)
1277 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1280 /* Record the initial position of the return address. RTL is
1281 INCOMING_RETURN_ADDR_RTX. */
1283 static void
1284 initial_return_save (rtx rtl)
1286 unsigned int reg = INVALID_REGNUM;
1287 HOST_WIDE_INT offset = 0;
1289 switch (GET_CODE (rtl))
1291 case REG:
1292 /* RA is in a register. */
1293 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1294 break;
1296 case MEM:
1297 /* RA is on the stack. */
1298 rtl = XEXP (rtl, 0);
1299 switch (GET_CODE (rtl))
1301 case REG:
1302 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1303 offset = 0;
1304 break;
1306 case PLUS:
1307 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1308 offset = INTVAL (XEXP (rtl, 1));
1309 break;
1311 case MINUS:
1312 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1313 offset = -INTVAL (XEXP (rtl, 1));
1314 break;
1316 default:
1317 gcc_unreachable ();
1320 break;
1322 case PLUS:
1323 /* The return address is at some offset from any value we can
1324 actually load. For instance, on the SPARC it is in %i7+8. Just
1325 ignore the offset for now; it doesn't matter for unwinding frames. */
1326 gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1327 initial_return_save (XEXP (rtl, 0));
1328 return;
1330 default:
1331 gcc_unreachable ();
1334 if (reg != DWARF_FRAME_RETURN_COLUMN)
1335 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1338 /* Given a SET, calculate the amount of stack adjustment it
1339 contains. */
1341 static HOST_WIDE_INT
1342 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1343 HOST_WIDE_INT cur_offset)
1345 const_rtx src = SET_SRC (pattern);
1346 const_rtx dest = SET_DEST (pattern);
1347 HOST_WIDE_INT offset = 0;
1348 enum rtx_code code;
1350 if (dest == stack_pointer_rtx)
1352 code = GET_CODE (src);
1354 /* Assume (set (reg sp) (reg whatever)) sets args_size
1355 level to 0. */
1356 if (code == REG && src != stack_pointer_rtx)
1358 offset = -cur_args_size;
1359 #ifndef STACK_GROWS_DOWNWARD
1360 offset = -offset;
1361 #endif
1362 return offset - cur_offset;
1365 if (! (code == PLUS || code == MINUS)
1366 || XEXP (src, 0) != stack_pointer_rtx
1367 || !CONST_INT_P (XEXP (src, 1)))
1368 return 0;
1370 /* (set (reg sp) (plus (reg sp) (const_int))) */
1371 offset = INTVAL (XEXP (src, 1));
1372 if (code == PLUS)
1373 offset = -offset;
1374 return offset;
1377 if (MEM_P (src) && !MEM_P (dest))
1378 dest = src;
1379 if (MEM_P (dest))
1381 /* (set (mem (pre_dec (reg sp))) (foo)) */
1382 src = XEXP (dest, 0);
1383 code = GET_CODE (src);
1385 switch (code)
1387 case PRE_MODIFY:
1388 case POST_MODIFY:
1389 if (XEXP (src, 0) == stack_pointer_rtx)
1391 rtx val = XEXP (XEXP (src, 1), 1);
1392 /* We handle only adjustments by constant amount. */
1393 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1394 && CONST_INT_P (val));
1395 offset = -INTVAL (val);
1396 break;
1398 return 0;
1400 case PRE_DEC:
1401 case POST_DEC:
1402 if (XEXP (src, 0) == stack_pointer_rtx)
1404 offset = GET_MODE_SIZE (GET_MODE (dest));
1405 break;
1407 return 0;
1409 case PRE_INC:
1410 case POST_INC:
1411 if (XEXP (src, 0) == stack_pointer_rtx)
1413 offset = -GET_MODE_SIZE (GET_MODE (dest));
1414 break;
1416 return 0;
1418 default:
1419 return 0;
1422 else
1423 return 0;
1425 return offset;
1428 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1429 indexed by INSN_UID. */
1431 static HOST_WIDE_INT *barrier_args_size;
1433 /* Helper function for compute_barrier_args_size. Handle one insn. */
1435 static HOST_WIDE_INT
1436 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1437 VEC (rtx, heap) **next)
1439 HOST_WIDE_INT offset = 0;
1440 int i;
1442 if (! RTX_FRAME_RELATED_P (insn))
1444 if (prologue_epilogue_contains (insn))
1445 /* Nothing */;
1446 else if (GET_CODE (PATTERN (insn)) == SET)
1447 offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1448 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1449 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1451 /* There may be stack adjustments inside compound insns. Search
1452 for them. */
1453 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1454 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1455 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1456 cur_args_size, offset);
1459 else
1461 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1463 if (expr)
1465 expr = XEXP (expr, 0);
1466 if (GET_CODE (expr) == PARALLEL
1467 || GET_CODE (expr) == SEQUENCE)
1468 for (i = 1; i < XVECLEN (expr, 0); i++)
1470 rtx elem = XVECEXP (expr, 0, i);
1472 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1473 offset += stack_adjust_offset (elem, cur_args_size, offset);
1478 #ifndef STACK_GROWS_DOWNWARD
1479 offset = -offset;
1480 #endif
1482 cur_args_size += offset;
1483 if (cur_args_size < 0)
1484 cur_args_size = 0;
1486 if (JUMP_P (insn))
1488 rtx dest = JUMP_LABEL (insn);
1490 if (dest)
1492 if (barrier_args_size [INSN_UID (dest)] < 0)
1494 barrier_args_size [INSN_UID (dest)] = cur_args_size;
1495 VEC_safe_push (rtx, heap, *next, dest);
1500 return cur_args_size;
1503 /* Walk the whole function and compute args_size on BARRIERs. */
1505 static void
1506 compute_barrier_args_size (void)
1508 int max_uid = get_max_uid (), i;
1509 rtx insn;
1510 VEC (rtx, heap) *worklist, *next, *tmp;
1512 barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1513 for (i = 0; i < max_uid; i++)
1514 barrier_args_size[i] = -1;
1516 worklist = VEC_alloc (rtx, heap, 20);
1517 next = VEC_alloc (rtx, heap, 20);
1518 insn = get_insns ();
1519 barrier_args_size[INSN_UID (insn)] = 0;
1520 VEC_quick_push (rtx, worklist, insn);
1521 for (;;)
1523 while (!VEC_empty (rtx, worklist))
1525 rtx prev, body, first_insn;
1526 HOST_WIDE_INT cur_args_size;
1528 first_insn = insn = VEC_pop (rtx, worklist);
1529 cur_args_size = barrier_args_size[INSN_UID (insn)];
1530 prev = prev_nonnote_insn (insn);
1531 if (prev && BARRIER_P (prev))
1532 barrier_args_size[INSN_UID (prev)] = cur_args_size;
1534 for (; insn; insn = NEXT_INSN (insn))
1536 if (INSN_DELETED_P (insn) || NOTE_P (insn))
1537 continue;
1538 if (BARRIER_P (insn))
1539 break;
1541 if (LABEL_P (insn))
1543 if (insn == first_insn)
1544 continue;
1545 else if (barrier_args_size[INSN_UID (insn)] < 0)
1547 barrier_args_size[INSN_UID (insn)] = cur_args_size;
1548 continue;
1550 else
1552 /* The insns starting with this label have been
1553 already scanned or are in the worklist. */
1554 break;
1558 body = PATTERN (insn);
1559 if (GET_CODE (body) == SEQUENCE)
1561 HOST_WIDE_INT dest_args_size = cur_args_size;
1562 for (i = 1; i < XVECLEN (body, 0); i++)
1563 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1564 && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1565 dest_args_size
1566 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1567 dest_args_size, &next);
1568 else
1569 cur_args_size
1570 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1571 cur_args_size, &next);
1573 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1574 compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1575 dest_args_size, &next);
1576 else
1577 cur_args_size
1578 = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1579 cur_args_size, &next);
1581 else
1582 cur_args_size
1583 = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1587 if (VEC_empty (rtx, next))
1588 break;
1590 /* Swap WORKLIST with NEXT and truncate NEXT for next iteration. */
1591 tmp = next;
1592 next = worklist;
1593 worklist = tmp;
1594 VEC_truncate (rtx, next, 0);
1597 VEC_free (rtx, heap, worklist);
1598 VEC_free (rtx, heap, next);
1601 /* Add a CFI to update the running total of the size of arguments
1602 pushed onto the stack. */
1604 static void
1605 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1607 dw_cfi_ref cfi;
1609 if (size == old_args_size)
1610 return;
1612 old_args_size = size;
1614 cfi = new_cfi ();
1615 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1616 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1617 add_fde_cfi (label, cfi);
1620 /* Record a stack adjustment of OFFSET bytes. */
1622 static void
1623 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1625 if (cfa.reg == STACK_POINTER_REGNUM)
1626 cfa.offset += offset;
1628 if (cfa_store.reg == STACK_POINTER_REGNUM)
1629 cfa_store.offset += offset;
1631 if (ACCUMULATE_OUTGOING_ARGS)
1632 return;
1634 #ifndef STACK_GROWS_DOWNWARD
1635 offset = -offset;
1636 #endif
1638 args_size += offset;
1639 if (args_size < 0)
1640 args_size = 0;
1642 def_cfa_1 (label, &cfa);
1643 if (flag_asynchronous_unwind_tables)
1644 dwarf2out_args_size (label, args_size);
1647 /* Check INSN to see if it looks like a push or a stack adjustment, and
1648 make a note of it if it does. EH uses this information to find out
1649 how much extra space it needs to pop off the stack. */
1651 static void
1652 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1654 HOST_WIDE_INT offset;
1655 const char *label;
1656 int i;
1658 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1659 with this function. Proper support would require all frame-related
1660 insns to be marked, and to be able to handle saving state around
1661 epilogues textually in the middle of the function. */
1662 if (prologue_epilogue_contains (insn))
1663 return;
1665 /* If INSN is an instruction from target of an annulled branch, the
1666 effects are for the target only and so current argument size
1667 shouldn't change at all. */
1668 if (final_sequence
1669 && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1670 && INSN_FROM_TARGET_P (insn))
1671 return;
1673 /* If only calls can throw, and we have a frame pointer,
1674 save up adjustments until we see the CALL_INSN. */
1675 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1677 if (CALL_P (insn) && !after_p)
1679 /* Extract the size of the args from the CALL rtx itself. */
1680 insn = PATTERN (insn);
1681 if (GET_CODE (insn) == PARALLEL)
1682 insn = XVECEXP (insn, 0, 0);
1683 if (GET_CODE (insn) == SET)
1684 insn = SET_SRC (insn);
1685 gcc_assert (GET_CODE (insn) == CALL);
1686 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1688 return;
1691 if (CALL_P (insn) && !after_p)
1693 if (!flag_asynchronous_unwind_tables)
1694 dwarf2out_args_size ("", args_size);
1695 return;
1697 else if (BARRIER_P (insn))
1699 /* Don't call compute_barrier_args_size () if the only
1700 BARRIER is at the end of function. */
1701 if (barrier_args_size == NULL && next_nonnote_insn (insn))
1702 compute_barrier_args_size ();
1703 if (barrier_args_size == NULL)
1704 offset = 0;
1705 else
1707 offset = barrier_args_size[INSN_UID (insn)];
1708 if (offset < 0)
1709 offset = 0;
1712 offset -= args_size;
1713 #ifndef STACK_GROWS_DOWNWARD
1714 offset = -offset;
1715 #endif
1717 else if (GET_CODE (PATTERN (insn)) == SET)
1718 offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1719 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1720 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1722 /* There may be stack adjustments inside compound insns. Search
1723 for them. */
1724 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1725 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1726 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1727 args_size, offset);
1729 else
1730 return;
1732 if (offset == 0)
1733 return;
1735 label = dwarf2out_cfi_label (false);
1736 dwarf2out_stack_adjust (offset, label);
1739 /* We delay emitting a register save until either (a) we reach the end
1740 of the prologue or (b) the register is clobbered. This clusters
1741 register saves so that there are fewer pc advances. */
1743 struct GTY(()) queued_reg_save {
1744 struct queued_reg_save *next;
1745 rtx reg;
1746 HOST_WIDE_INT cfa_offset;
1747 rtx saved_reg;
1750 static GTY(()) struct queued_reg_save *queued_reg_saves;
1752 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1753 struct GTY(()) reg_saved_in_data {
1754 rtx orig_reg;
1755 rtx saved_in_reg;
1758 /* A list of registers saved in other registers.
1759 The list intentionally has a small maximum capacity of 4; if your
1760 port needs more than that, you might consider implementing a
1761 more efficient data structure. */
1762 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1763 static GTY(()) size_t num_regs_saved_in_regs;
1765 static const char *last_reg_save_label;
1767 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1768 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1770 static void
1771 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1773 struct queued_reg_save *q;
1775 /* Duplicates waste space, but it's also necessary to remove them
1776 for correctness, since the queue gets output in reverse
1777 order. */
1778 for (q = queued_reg_saves; q != NULL; q = q->next)
1779 if (REGNO (q->reg) == REGNO (reg))
1780 break;
1782 if (q == NULL)
1784 q = ggc_alloc_queued_reg_save ();
1785 q->next = queued_reg_saves;
1786 queued_reg_saves = q;
1789 q->reg = reg;
1790 q->cfa_offset = offset;
1791 q->saved_reg = sreg;
1793 last_reg_save_label = label;
1796 /* Output all the entries in QUEUED_REG_SAVES. */
1798 void
1799 dwarf2out_flush_queued_reg_saves (void)
1801 struct queued_reg_save *q;
1803 for (q = queued_reg_saves; q; q = q->next)
1805 size_t i;
1806 unsigned int reg, sreg;
1808 for (i = 0; i < num_regs_saved_in_regs; i++)
1809 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1810 break;
1811 if (q->saved_reg && i == num_regs_saved_in_regs)
1813 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1814 num_regs_saved_in_regs++;
1816 if (i != num_regs_saved_in_regs)
1818 regs_saved_in_regs[i].orig_reg = q->reg;
1819 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1822 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1823 if (q->saved_reg)
1824 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1825 else
1826 sreg = INVALID_REGNUM;
1827 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1830 queued_reg_saves = NULL;
1831 last_reg_save_label = NULL;
1834 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1835 location for? Or, does it clobber a register which we've previously
1836 said that some other register is saved in, and for which we now
1837 have a new location for? */
1839 static bool
1840 clobbers_queued_reg_save (const_rtx insn)
1842 struct queued_reg_save *q;
1844 for (q = queued_reg_saves; q; q = q->next)
1846 size_t i;
1847 if (modified_in_p (q->reg, insn))
1848 return true;
1849 for (i = 0; i < num_regs_saved_in_regs; i++)
1850 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1851 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1852 return true;
1855 return false;
1858 /* Entry point for saving the first register into the second. */
1860 void
1861 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1863 size_t i;
1864 unsigned int regno, sregno;
1866 for (i = 0; i < num_regs_saved_in_regs; i++)
1867 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1868 break;
1869 if (i == num_regs_saved_in_regs)
1871 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1872 num_regs_saved_in_regs++;
1874 regs_saved_in_regs[i].orig_reg = reg;
1875 regs_saved_in_regs[i].saved_in_reg = sreg;
1877 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1878 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1879 reg_save (label, regno, sregno, 0);
1882 /* What register, if any, is currently saved in REG? */
1884 static rtx
1885 reg_saved_in (rtx reg)
1887 unsigned int regn = REGNO (reg);
1888 size_t i;
1889 struct queued_reg_save *q;
1891 for (q = queued_reg_saves; q; q = q->next)
1892 if (q->saved_reg && regn == REGNO (q->saved_reg))
1893 return q->reg;
1895 for (i = 0; i < num_regs_saved_in_regs; i++)
1896 if (regs_saved_in_regs[i].saved_in_reg
1897 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1898 return regs_saved_in_regs[i].orig_reg;
1900 return NULL_RTX;
1904 /* A temporary register holding an integral value used in adjusting SP
1905 or setting up the store_reg. The "offset" field holds the integer
1906 value, not an offset. */
1907 static dw_cfa_location cfa_temp;
1909 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note. */
1911 static void
1912 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1914 memset (&cfa, 0, sizeof (cfa));
1916 switch (GET_CODE (pat))
1918 case PLUS:
1919 cfa.reg = REGNO (XEXP (pat, 0));
1920 cfa.offset = INTVAL (XEXP (pat, 1));
1921 break;
1923 case REG:
1924 cfa.reg = REGNO (pat);
1925 break;
1927 case MEM:
1928 cfa.indirect = 1;
1929 pat = XEXP (pat, 0);
1930 if (GET_CODE (pat) == PLUS)
1932 cfa.base_offset = INTVAL (XEXP (pat, 1));
1933 pat = XEXP (pat, 0);
1935 cfa.reg = REGNO (pat);
1936 break;
1938 default:
1939 /* Recurse and define an expression. */
1940 gcc_unreachable ();
1943 def_cfa_1 (label, &cfa);
1946 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note. */
1948 static void
1949 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1951 rtx src, dest;
1953 gcc_assert (GET_CODE (pat) == SET);
1954 dest = XEXP (pat, 0);
1955 src = XEXP (pat, 1);
1957 switch (GET_CODE (src))
1959 case PLUS:
1960 gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1961 cfa.offset -= INTVAL (XEXP (src, 1));
1962 break;
1964 case REG:
1965 break;
1967 default:
1968 gcc_unreachable ();
1971 cfa.reg = REGNO (dest);
1972 gcc_assert (cfa.indirect == 0);
1974 def_cfa_1 (label, &cfa);
1977 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note. */
1979 static void
1980 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1982 HOST_WIDE_INT offset;
1983 rtx src, addr, span;
1985 src = XEXP (set, 1);
1986 addr = XEXP (set, 0);
1987 gcc_assert (MEM_P (addr));
1988 addr = XEXP (addr, 0);
1990 /* As documented, only consider extremely simple addresses. */
1991 switch (GET_CODE (addr))
1993 case REG:
1994 gcc_assert (REGNO (addr) == cfa.reg);
1995 offset = -cfa.offset;
1996 break;
1997 case PLUS:
1998 gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1999 offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
2000 break;
2001 default:
2002 gcc_unreachable ();
2005 span = targetm.dwarf_register_span (src);
2007 /* ??? We'd like to use queue_reg_save, but we need to come up with
2008 a different flushing heuristic for epilogues. */
2009 if (!span)
2010 reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
2011 else
2013 /* We have a PARALLEL describing where the contents of SRC live.
2014 Queue register saves for each piece of the PARALLEL. */
2015 int par_index;
2016 int limit;
2017 HOST_WIDE_INT span_offset = offset;
2019 gcc_assert (GET_CODE (span) == PARALLEL);
2021 limit = XVECLEN (span, 0);
2022 for (par_index = 0; par_index < limit; par_index++)
2024 rtx elem = XVECEXP (span, 0, par_index);
2026 reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2027 INVALID_REGNUM, span_offset);
2028 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2033 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note. */
2035 static void
2036 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2038 rtx src, dest;
2039 unsigned sregno, dregno;
2041 src = XEXP (set, 1);
2042 dest = XEXP (set, 0);
2044 if (src == pc_rtx)
2045 sregno = DWARF_FRAME_RETURN_COLUMN;
2046 else
2047 sregno = DWARF_FRAME_REGNUM (REGNO (src));
2049 dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2051 /* ??? We'd like to use queue_reg_save, but we need to come up with
2052 a different flushing heuristic for epilogues. */
2053 reg_save (label, sregno, dregno, 0);
2056 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2058 static void
2059 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2061 rtx src, dest, span;
2062 dw_cfi_ref cfi = new_cfi ();
2064 dest = SET_DEST (set);
2065 src = SET_SRC (set);
2067 gcc_assert (REG_P (src));
2068 gcc_assert (MEM_P (dest));
2070 span = targetm.dwarf_register_span (src);
2071 gcc_assert (!span);
2073 cfi->dw_cfi_opc = DW_CFA_expression;
2074 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2075 cfi->dw_cfi_oprnd2.dw_cfi_loc
2076 = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
2077 VAR_INIT_STATUS_INITIALIZED);
2079 /* ??? We'd like to use queue_reg_save, were the interface different,
2080 and, as above, we could manage flushing for epilogues. */
2081 add_fde_cfi (label, cfi);
2084 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note. */
2086 static void
2087 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2089 dw_cfi_ref cfi = new_cfi ();
2090 unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2092 cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2093 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2095 add_fde_cfi (label, cfi);
2098 /* Record call frame debugging information for an expression EXPR,
2099 which either sets SP or FP (adjusting how we calculate the frame
2100 address) or saves a register to the stack or another register.
2101 LABEL indicates the address of EXPR.
2103 This function encodes a state machine mapping rtxes to actions on
2104 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
2105 users need not read the source code.
2107 The High-Level Picture
2109 Changes in the register we use to calculate the CFA: Currently we
2110 assume that if you copy the CFA register into another register, we
2111 should take the other one as the new CFA register; this seems to
2112 work pretty well. If it's wrong for some target, it's simple
2113 enough not to set RTX_FRAME_RELATED_P on the insn in question.
2115 Changes in the register we use for saving registers to the stack:
2116 This is usually SP, but not always. Again, we deduce that if you
2117 copy SP into another register (and SP is not the CFA register),
2118 then the new register is the one we will be using for register
2119 saves. This also seems to work.
2121 Register saves: There's not much guesswork about this one; if
2122 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2123 register save, and the register used to calculate the destination
2124 had better be the one we think we're using for this purpose.
2125 It's also assumed that a copy from a call-saved register to another
2126 register is saving that register if RTX_FRAME_RELATED_P is set on
2127 that instruction. If the copy is from a call-saved register to
2128 the *same* register, that means that the register is now the same
2129 value as in the caller.
2131 Except: If the register being saved is the CFA register, and the
2132 offset is nonzero, we are saving the CFA, so we assume we have to
2133 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
2134 the intent is to save the value of SP from the previous frame.
2136 In addition, if a register has previously been saved to a different
2137 register,
2139 Invariants / Summaries of Rules
2141 cfa current rule for calculating the CFA. It usually
2142 consists of a register and an offset.
2143 cfa_store register used by prologue code to save things to the stack
2144 cfa_store.offset is the offset from the value of
2145 cfa_store.reg to the actual CFA
2146 cfa_temp register holding an integral value. cfa_temp.offset
2147 stores the value, which will be used to adjust the
2148 stack pointer. cfa_temp is also used like cfa_store,
2149 to track stores to the stack via fp or a temp reg.
2151 Rules 1- 4: Setting a register's value to cfa.reg or an expression
2152 with cfa.reg as the first operand changes the cfa.reg and its
2153 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
2154 cfa_temp.offset.
2156 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
2157 expression yielding a constant. This sets cfa_temp.reg
2158 and cfa_temp.offset.
2160 Rule 5: Create a new register cfa_store used to save items to the
2161 stack.
2163 Rules 10-14: Save a register to the stack. Define offset as the
2164 difference of the original location and cfa_store's
2165 location (or cfa_temp's location if cfa_temp is used).
2167 Rules 16-20: If AND operation happens on sp in prologue, we assume
2168 stack is realigned. We will use a group of DW_OP_XXX
2169 expressions to represent the location of the stored
2170 register instead of CFA+offset.
2172 The Rules
2174 "{a,b}" indicates a choice of a xor b.
2175 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2177 Rule 1:
2178 (set <reg1> <reg2>:cfa.reg)
2179 effects: cfa.reg = <reg1>
2180 cfa.offset unchanged
2181 cfa_temp.reg = <reg1>
2182 cfa_temp.offset = cfa.offset
2184 Rule 2:
2185 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2186 {<const_int>,<reg>:cfa_temp.reg}))
2187 effects: cfa.reg = sp if fp used
2188 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2189 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2190 if cfa_store.reg==sp
2192 Rule 3:
2193 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2194 effects: cfa.reg = fp
2195 cfa_offset += +/- <const_int>
2197 Rule 4:
2198 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2199 constraints: <reg1> != fp
2200 <reg1> != sp
2201 effects: cfa.reg = <reg1>
2202 cfa_temp.reg = <reg1>
2203 cfa_temp.offset = cfa.offset
2205 Rule 5:
2206 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2207 constraints: <reg1> != fp
2208 <reg1> != sp
2209 effects: cfa_store.reg = <reg1>
2210 cfa_store.offset = cfa.offset - cfa_temp.offset
2212 Rule 6:
2213 (set <reg> <const_int>)
2214 effects: cfa_temp.reg = <reg>
2215 cfa_temp.offset = <const_int>
2217 Rule 7:
2218 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2219 effects: cfa_temp.reg = <reg1>
2220 cfa_temp.offset |= <const_int>
2222 Rule 8:
2223 (set <reg> (high <exp>))
2224 effects: none
2226 Rule 9:
2227 (set <reg> (lo_sum <exp> <const_int>))
2228 effects: cfa_temp.reg = <reg>
2229 cfa_temp.offset = <const_int>
2231 Rule 10:
2232 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2233 effects: cfa_store.offset -= <const_int>
2234 cfa.offset = cfa_store.offset if cfa.reg == sp
2235 cfa.reg = sp
2236 cfa.base_offset = -cfa_store.offset
2238 Rule 11:
2239 (set (mem ({pre_inc,pre_dec,post_dec} sp:cfa_store.reg)) <reg>)
2240 effects: cfa_store.offset += -/+ mode_size(mem)
2241 cfa.offset = cfa_store.offset if cfa.reg == sp
2242 cfa.reg = sp
2243 cfa.base_offset = -cfa_store.offset
2245 Rule 12:
2246 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2248 <reg2>)
2249 effects: cfa.reg = <reg1>
2250 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2252 Rule 13:
2253 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2254 effects: cfa.reg = <reg1>
2255 cfa.base_offset = -{cfa_store,cfa_temp}.offset
2257 Rule 14:
2258 (set (mem (post_inc <reg1>:cfa_temp <const_int>)) <reg2>)
2259 effects: cfa.reg = <reg1>
2260 cfa.base_offset = -cfa_temp.offset
2261 cfa_temp.offset -= mode_size(mem)
2263 Rule 15:
2264 (set <reg> {unspec, unspec_volatile})
2265 effects: target-dependent
2267 Rule 16:
2268 (set sp (and: sp <const_int>))
2269 constraints: cfa_store.reg == sp
2270 effects: current_fde.stack_realign = 1
2271 cfa_store.offset = 0
2272 fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2274 Rule 17:
2275 (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2276 effects: cfa_store.offset += -/+ mode_size(mem)
2278 Rule 18:
2279 (set (mem ({pre_inc, pre_dec} sp)) fp)
2280 constraints: fde->stack_realign == 1
2281 effects: cfa_store.offset = 0
2282 cfa.reg != HARD_FRAME_POINTER_REGNUM
2284 Rule 19:
2285 (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2286 constraints: fde->stack_realign == 1
2287 && cfa.offset == 0
2288 && cfa.indirect == 0
2289 && cfa.reg != HARD_FRAME_POINTER_REGNUM
2290 effects: Use DW_CFA_def_cfa_expression to define cfa
2291 cfa.reg == fde->drap_reg */
2293 static void
2294 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2296 rtx src, dest, span;
2297 HOST_WIDE_INT offset;
2298 dw_fde_ref fde;
2300 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2301 the PARALLEL independently. The first element is always processed if
2302 it is a SET. This is for backward compatibility. Other elements
2303 are processed only if they are SETs and the RTX_FRAME_RELATED_P
2304 flag is set in them. */
2305 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2307 int par_index;
2308 int limit = XVECLEN (expr, 0);
2309 rtx elem;
2311 /* PARALLELs have strict read-modify-write semantics, so we
2312 ought to evaluate every rvalue before changing any lvalue.
2313 It's cumbersome to do that in general, but there's an
2314 easy approximation that is enough for all current users:
2315 handle register saves before register assignments. */
2316 if (GET_CODE (expr) == PARALLEL)
2317 for (par_index = 0; par_index < limit; par_index++)
2319 elem = XVECEXP (expr, 0, par_index);
2320 if (GET_CODE (elem) == SET
2321 && MEM_P (SET_DEST (elem))
2322 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2323 dwarf2out_frame_debug_expr (elem, label);
2326 for (par_index = 0; par_index < limit; par_index++)
2328 elem = XVECEXP (expr, 0, par_index);
2329 if (GET_CODE (elem) == SET
2330 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2331 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2332 dwarf2out_frame_debug_expr (elem, label);
2333 else if (GET_CODE (elem) == SET
2334 && par_index != 0
2335 && !RTX_FRAME_RELATED_P (elem))
2337 /* Stack adjustment combining might combine some post-prologue
2338 stack adjustment into a prologue stack adjustment. */
2339 HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2341 if (offset != 0)
2342 dwarf2out_stack_adjust (offset, label);
2345 return;
2348 gcc_assert (GET_CODE (expr) == SET);
2350 src = SET_SRC (expr);
2351 dest = SET_DEST (expr);
2353 if (REG_P (src))
2355 rtx rsi = reg_saved_in (src);
2356 if (rsi)
2357 src = rsi;
2360 fde = current_fde ();
2362 switch (GET_CODE (dest))
2364 case REG:
2365 switch (GET_CODE (src))
2367 /* Setting FP from SP. */
2368 case REG:
2369 if (cfa.reg == (unsigned) REGNO (src))
2371 /* Rule 1 */
2372 /* Update the CFA rule wrt SP or FP. Make sure src is
2373 relative to the current CFA register.
2375 We used to require that dest be either SP or FP, but the
2376 ARM copies SP to a temporary register, and from there to
2377 FP. So we just rely on the backends to only set
2378 RTX_FRAME_RELATED_P on appropriate insns. */
2379 cfa.reg = REGNO (dest);
2380 cfa_temp.reg = cfa.reg;
2381 cfa_temp.offset = cfa.offset;
2383 else
2385 /* Saving a register in a register. */
2386 gcc_assert (!fixed_regs [REGNO (dest)]
2387 /* For the SPARC and its register window. */
2388 || (DWARF_FRAME_REGNUM (REGNO (src))
2389 == DWARF_FRAME_RETURN_COLUMN));
2391 /* After stack is aligned, we can only save SP in FP
2392 if drap register is used. In this case, we have
2393 to restore stack pointer with the CFA value and we
2394 don't generate this DWARF information. */
2395 if (fde
2396 && fde->stack_realign
2397 && REGNO (src) == STACK_POINTER_REGNUM)
2398 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2399 && fde->drap_reg != INVALID_REGNUM
2400 && cfa.reg != REGNO (src));
2401 else
2402 queue_reg_save (label, src, dest, 0);
2404 break;
2406 case PLUS:
2407 case MINUS:
2408 case LO_SUM:
2409 if (dest == stack_pointer_rtx)
2411 /* Rule 2 */
2412 /* Adjusting SP. */
2413 switch (GET_CODE (XEXP (src, 1)))
2415 case CONST_INT:
2416 offset = INTVAL (XEXP (src, 1));
2417 break;
2418 case REG:
2419 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2420 == cfa_temp.reg);
2421 offset = cfa_temp.offset;
2422 break;
2423 default:
2424 gcc_unreachable ();
2427 if (XEXP (src, 0) == hard_frame_pointer_rtx)
2429 /* Restoring SP from FP in the epilogue. */
2430 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2431 cfa.reg = STACK_POINTER_REGNUM;
2433 else if (GET_CODE (src) == LO_SUM)
2434 /* Assume we've set the source reg of the LO_SUM from sp. */
2436 else
2437 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2439 if (GET_CODE (src) != MINUS)
2440 offset = -offset;
2441 if (cfa.reg == STACK_POINTER_REGNUM)
2442 cfa.offset += offset;
2443 if (cfa_store.reg == STACK_POINTER_REGNUM)
2444 cfa_store.offset += offset;
2446 else if (dest == hard_frame_pointer_rtx)
2448 /* Rule 3 */
2449 /* Either setting the FP from an offset of the SP,
2450 or adjusting the FP */
2451 gcc_assert (frame_pointer_needed);
2453 gcc_assert (REG_P (XEXP (src, 0))
2454 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2455 && CONST_INT_P (XEXP (src, 1)));
2456 offset = INTVAL (XEXP (src, 1));
2457 if (GET_CODE (src) != MINUS)
2458 offset = -offset;
2459 cfa.offset += offset;
2460 cfa.reg = HARD_FRAME_POINTER_REGNUM;
2462 else
2464 gcc_assert (GET_CODE (src) != MINUS);
2466 /* Rule 4 */
2467 if (REG_P (XEXP (src, 0))
2468 && REGNO (XEXP (src, 0)) == cfa.reg
2469 && CONST_INT_P (XEXP (src, 1)))
2471 /* Setting a temporary CFA register that will be copied
2472 into the FP later on. */
2473 offset = - INTVAL (XEXP (src, 1));
2474 cfa.offset += offset;
2475 cfa.reg = REGNO (dest);
2476 /* Or used to save regs to the stack. */
2477 cfa_temp.reg = cfa.reg;
2478 cfa_temp.offset = cfa.offset;
2481 /* Rule 5 */
2482 else if (REG_P (XEXP (src, 0))
2483 && REGNO (XEXP (src, 0)) == cfa_temp.reg
2484 && XEXP (src, 1) == stack_pointer_rtx)
2486 /* Setting a scratch register that we will use instead
2487 of SP for saving registers to the stack. */
2488 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2489 cfa_store.reg = REGNO (dest);
2490 cfa_store.offset = cfa.offset - cfa_temp.offset;
2493 /* Rule 9 */
2494 else if (GET_CODE (src) == LO_SUM
2495 && CONST_INT_P (XEXP (src, 1)))
2497 cfa_temp.reg = REGNO (dest);
2498 cfa_temp.offset = INTVAL (XEXP (src, 1));
2500 else
2501 gcc_unreachable ();
2503 break;
2505 /* Rule 6 */
2506 case CONST_INT:
2507 cfa_temp.reg = REGNO (dest);
2508 cfa_temp.offset = INTVAL (src);
2509 break;
2511 /* Rule 7 */
2512 case IOR:
2513 gcc_assert (REG_P (XEXP (src, 0))
2514 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2515 && CONST_INT_P (XEXP (src, 1)));
2517 if ((unsigned) REGNO (dest) != cfa_temp.reg)
2518 cfa_temp.reg = REGNO (dest);
2519 cfa_temp.offset |= INTVAL (XEXP (src, 1));
2520 break;
2522 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2523 which will fill in all of the bits. */
2524 /* Rule 8 */
2525 case HIGH:
2526 break;
2528 /* Rule 15 */
2529 case UNSPEC:
2530 case UNSPEC_VOLATILE:
2531 gcc_assert (targetm.dwarf_handle_frame_unspec);
2532 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2533 return;
2535 /* Rule 16 */
2536 case AND:
2537 /* If this AND operation happens on stack pointer in prologue,
2538 we assume the stack is realigned and we extract the
2539 alignment. */
2540 if (fde && XEXP (src, 0) == stack_pointer_rtx)
2542 /* We interpret reg_save differently with stack_realign set.
2543 Thus we must flush whatever we have queued first. */
2544 dwarf2out_flush_queued_reg_saves ();
2546 gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2547 fde->stack_realign = 1;
2548 fde->stack_realignment = INTVAL (XEXP (src, 1));
2549 cfa_store.offset = 0;
2551 if (cfa.reg != STACK_POINTER_REGNUM
2552 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2553 fde->drap_reg = cfa.reg;
2555 return;
2557 default:
2558 gcc_unreachable ();
2561 def_cfa_1 (label, &cfa);
2562 break;
2564 case MEM:
2566 /* Saving a register to the stack. Make sure dest is relative to the
2567 CFA register. */
2568 switch (GET_CODE (XEXP (dest, 0)))
2570 /* Rule 10 */
2571 /* With a push. */
2572 case PRE_MODIFY:
2573 /* We can't handle variable size modifications. */
2574 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2575 == CONST_INT);
2576 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2578 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2579 && cfa_store.reg == STACK_POINTER_REGNUM);
2581 cfa_store.offset += offset;
2582 if (cfa.reg == STACK_POINTER_REGNUM)
2583 cfa.offset = cfa_store.offset;
2585 offset = -cfa_store.offset;
2586 break;
2588 /* Rule 11 */
2589 case PRE_INC:
2590 case PRE_DEC:
2591 case POST_DEC:
2592 offset = GET_MODE_SIZE (GET_MODE (dest));
2593 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2594 offset = -offset;
2596 gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2597 == STACK_POINTER_REGNUM)
2598 && cfa_store.reg == STACK_POINTER_REGNUM);
2600 cfa_store.offset += offset;
2602 /* Rule 18: If stack is aligned, we will use FP as a
2603 reference to represent the address of the stored
2604 regiser. */
2605 if (fde
2606 && fde->stack_realign
2607 && src == hard_frame_pointer_rtx)
2609 gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2610 cfa_store.offset = 0;
2613 if (cfa.reg == STACK_POINTER_REGNUM)
2614 cfa.offset = cfa_store.offset;
2616 if (GET_CODE (XEXP (dest, 0)) == POST_DEC)
2617 offset += -cfa_store.offset;
2618 else
2619 offset = -cfa_store.offset;
2620 break;
2622 /* Rule 12 */
2623 /* With an offset. */
2624 case PLUS:
2625 case MINUS:
2626 case LO_SUM:
2628 int regno;
2630 gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2631 && REG_P (XEXP (XEXP (dest, 0), 0)));
2632 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2633 if (GET_CODE (XEXP (dest, 0)) == MINUS)
2634 offset = -offset;
2636 regno = REGNO (XEXP (XEXP (dest, 0), 0));
2638 if (cfa.reg == (unsigned) regno)
2639 offset -= cfa.offset;
2640 else if (cfa_store.reg == (unsigned) regno)
2641 offset -= cfa_store.offset;
2642 else
2644 gcc_assert (cfa_temp.reg == (unsigned) regno);
2645 offset -= cfa_temp.offset;
2648 break;
2650 /* Rule 13 */
2651 /* Without an offset. */
2652 case REG:
2654 int regno = REGNO (XEXP (dest, 0));
2656 if (cfa.reg == (unsigned) regno)
2657 offset = -cfa.offset;
2658 else if (cfa_store.reg == (unsigned) regno)
2659 offset = -cfa_store.offset;
2660 else
2662 gcc_assert (cfa_temp.reg == (unsigned) regno);
2663 offset = -cfa_temp.offset;
2666 break;
2668 /* Rule 14 */
2669 case POST_INC:
2670 gcc_assert (cfa_temp.reg
2671 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2672 offset = -cfa_temp.offset;
2673 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2674 break;
2676 default:
2677 gcc_unreachable ();
2680 /* Rule 17 */
2681 /* If the source operand of this MEM operation is not a
2682 register, basically the source is return address. Here
2683 we only care how much stack grew and we don't save it. */
2684 if (!REG_P (src))
2685 break;
2687 if (REGNO (src) != STACK_POINTER_REGNUM
2688 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2689 && (unsigned) REGNO (src) == cfa.reg)
2691 /* We're storing the current CFA reg into the stack. */
2693 if (cfa.offset == 0)
2695 /* Rule 19 */
2696 /* If stack is aligned, putting CFA reg into stack means
2697 we can no longer use reg + offset to represent CFA.
2698 Here we use DW_CFA_def_cfa_expression instead. The
2699 result of this expression equals to the original CFA
2700 value. */
2701 if (fde
2702 && fde->stack_realign
2703 && cfa.indirect == 0
2704 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2706 dw_cfa_location cfa_exp;
2708 gcc_assert (fde->drap_reg == cfa.reg);
2710 cfa_exp.indirect = 1;
2711 cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2712 cfa_exp.base_offset = offset;
2713 cfa_exp.offset = 0;
2715 fde->drap_reg_saved = 1;
2717 def_cfa_1 (label, &cfa_exp);
2718 break;
2721 /* If the source register is exactly the CFA, assume
2722 we're saving SP like any other register; this happens
2723 on the ARM. */
2724 def_cfa_1 (label, &cfa);
2725 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2726 break;
2728 else
2730 /* Otherwise, we'll need to look in the stack to
2731 calculate the CFA. */
2732 rtx x = XEXP (dest, 0);
2734 if (!REG_P (x))
2735 x = XEXP (x, 0);
2736 gcc_assert (REG_P (x));
2738 cfa.reg = REGNO (x);
2739 cfa.base_offset = offset;
2740 cfa.indirect = 1;
2741 def_cfa_1 (label, &cfa);
2742 break;
2746 def_cfa_1 (label, &cfa);
2748 span = targetm.dwarf_register_span (src);
2750 if (!span)
2751 queue_reg_save (label, src, NULL_RTX, offset);
2752 else
2754 /* We have a PARALLEL describing where the contents of SRC
2755 live. Queue register saves for each piece of the
2756 PARALLEL. */
2757 int par_index;
2758 int limit;
2759 HOST_WIDE_INT span_offset = offset;
2761 gcc_assert (GET_CODE (span) == PARALLEL);
2763 limit = XVECLEN (span, 0);
2764 for (par_index = 0; par_index < limit; par_index++)
2766 rtx elem = XVECEXP (span, 0, par_index);
2768 queue_reg_save (label, elem, NULL_RTX, span_offset);
2769 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2773 break;
2775 default:
2776 gcc_unreachable ();
2780 /* Record call frame debugging information for INSN, which either
2781 sets SP or FP (adjusting how we calculate the frame address) or saves a
2782 register to the stack. If INSN is NULL_RTX, initialize our state.
2784 If AFTER_P is false, we're being called before the insn is emitted,
2785 otherwise after. Call instructions get invoked twice. */
2787 void
2788 dwarf2out_frame_debug (rtx insn, bool after_p)
2790 const char *label;
2791 rtx note, n;
2792 bool handled_one = false;
2794 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2795 dwarf2out_flush_queued_reg_saves ();
2797 if (!RTX_FRAME_RELATED_P (insn))
2799 /* ??? This should be done unconditionally since stack adjustments
2800 matter if the stack pointer is not the CFA register anymore but
2801 is still used to save registers. */
2802 if (!ACCUMULATE_OUTGOING_ARGS)
2803 dwarf2out_notice_stack_adjust (insn, after_p);
2804 return;
2807 label = dwarf2out_cfi_label (false);
2808 any_cfis_emitted = false;
2810 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2811 switch (REG_NOTE_KIND (note))
2813 case REG_FRAME_RELATED_EXPR:
2814 insn = XEXP (note, 0);
2815 goto found;
2817 case REG_CFA_DEF_CFA:
2818 dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2819 handled_one = true;
2820 break;
2822 case REG_CFA_ADJUST_CFA:
2823 n = XEXP (note, 0);
2824 if (n == NULL)
2826 n = PATTERN (insn);
2827 if (GET_CODE (n) == PARALLEL)
2828 n = XVECEXP (n, 0, 0);
2830 dwarf2out_frame_debug_adjust_cfa (n, label);
2831 handled_one = true;
2832 break;
2834 case REG_CFA_OFFSET:
2835 n = XEXP (note, 0);
2836 if (n == NULL)
2837 n = single_set (insn);
2838 dwarf2out_frame_debug_cfa_offset (n, label);
2839 handled_one = true;
2840 break;
2842 case REG_CFA_REGISTER:
2843 n = XEXP (note, 0);
2844 if (n == NULL)
2846 n = PATTERN (insn);
2847 if (GET_CODE (n) == PARALLEL)
2848 n = XVECEXP (n, 0, 0);
2850 dwarf2out_frame_debug_cfa_register (n, label);
2851 handled_one = true;
2852 break;
2854 case REG_CFA_EXPRESSION:
2855 n = XEXP (note, 0);
2856 if (n == NULL)
2857 n = single_set (insn);
2858 dwarf2out_frame_debug_cfa_expression (n, label);
2859 handled_one = true;
2860 break;
2862 case REG_CFA_RESTORE:
2863 n = XEXP (note, 0);
2864 if (n == NULL)
2866 n = PATTERN (insn);
2867 if (GET_CODE (n) == PARALLEL)
2868 n = XVECEXP (n, 0, 0);
2869 n = XEXP (n, 0);
2871 dwarf2out_frame_debug_cfa_restore (n, label);
2872 handled_one = true;
2873 break;
2875 case REG_CFA_SET_VDRAP:
2876 n = XEXP (note, 0);
2877 if (REG_P (n))
2879 dw_fde_ref fde = current_fde ();
2880 if (fde)
2882 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2883 if (REG_P (n))
2884 fde->vdrap_reg = REGNO (n);
2887 handled_one = true;
2888 break;
2890 default:
2891 break;
2893 if (handled_one)
2895 if (any_cfis_emitted)
2896 dwarf2out_flush_queued_reg_saves ();
2897 return;
2900 insn = PATTERN (insn);
2901 found:
2902 dwarf2out_frame_debug_expr (insn, label);
2904 /* Check again. A parallel can save and update the same register.
2905 We could probably check just once, here, but this is safer than
2906 removing the check above. */
2907 if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2908 dwarf2out_flush_queued_reg_saves ();
2911 /* Called once at the start of final to initialize some data for the
2912 current function. */
2913 void
2914 dwarf2out_frame_debug_init (void)
2916 size_t i;
2918 /* Flush any queued register saves. */
2919 dwarf2out_flush_queued_reg_saves ();
2921 /* Set up state for generating call frame debug info. */
2922 lookup_cfa (&cfa);
2923 gcc_assert (cfa.reg
2924 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2926 cfa.reg = STACK_POINTER_REGNUM;
2927 cfa_store = cfa;
2928 cfa_temp.reg = -1;
2929 cfa_temp.offset = 0;
2931 for (i = 0; i < num_regs_saved_in_regs; i++)
2933 regs_saved_in_regs[i].orig_reg = NULL_RTX;
2934 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2936 num_regs_saved_in_regs = 0;
2938 if (barrier_args_size)
2940 XDELETEVEC (barrier_args_size);
2941 barrier_args_size = NULL;
2945 /* Determine if we need to save and restore CFI information around this
2946 epilogue. If SIBCALL is true, then this is a sibcall epilogue. If
2947 we do need to save/restore, then emit the save now, and insert a
2948 NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream. */
2950 void
2951 dwarf2out_cfi_begin_epilogue (rtx insn)
2953 bool saw_frp = false;
2954 rtx i;
2956 /* Scan forward to the return insn, noticing if there are possible
2957 frame related insns. */
2958 for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2960 if (!INSN_P (i))
2961 continue;
2963 /* Look for both regular and sibcalls to end the block. */
2964 if (returnjump_p (i))
2965 break;
2966 if (CALL_P (i) && SIBLING_CALL_P (i))
2967 break;
2969 if (GET_CODE (PATTERN (i)) == SEQUENCE)
2971 int idx;
2972 rtx seq = PATTERN (i);
2974 if (returnjump_p (XVECEXP (seq, 0, 0)))
2975 break;
2976 if (CALL_P (XVECEXP (seq, 0, 0))
2977 && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2978 break;
2980 for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2981 if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2982 saw_frp = true;
2985 if (RTX_FRAME_RELATED_P (i))
2986 saw_frp = true;
2989 /* If the port doesn't emit epilogue unwind info, we don't need a
2990 save/restore pair. */
2991 if (!saw_frp)
2992 return;
2994 /* Otherwise, search forward to see if the return insn was the last
2995 basic block of the function. If so, we don't need save/restore. */
2996 gcc_assert (i != NULL);
2997 i = next_real_insn (i);
2998 if (i == NULL)
2999 return;
3001 /* Insert the restore before that next real insn in the stream, and before
3002 a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
3003 properly nested. This should be after any label or alignment. This
3004 will be pushed into the CFI stream by the function below. */
3005 while (1)
3007 rtx p = PREV_INSN (i);
3008 if (!NOTE_P (p))
3009 break;
3010 if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3011 break;
3012 i = p;
3014 emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3016 emit_cfa_remember = true;
3018 /* And emulate the state save. */
3019 gcc_assert (!cfa_remember.in_use);
3020 cfa_remember = cfa;
3021 cfa_remember.in_use = 1;
3024 /* A "subroutine" of dwarf2out_cfi_begin_epilogue. Emit the restore
3025 required. */
3027 void
3028 dwarf2out_frame_debug_restore_state (void)
3030 dw_cfi_ref cfi = new_cfi ();
3031 const char *label = dwarf2out_cfi_label (false);
3033 cfi->dw_cfi_opc = DW_CFA_restore_state;
3034 add_fde_cfi (label, cfi);
3036 gcc_assert (cfa_remember.in_use);
3037 cfa = cfa_remember;
3038 cfa_remember.in_use = 0;
3041 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
3042 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3043 (enum dwarf_call_frame_info cfi);
3045 static enum dw_cfi_oprnd_type
3046 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3048 switch (cfi)
3050 case DW_CFA_nop:
3051 case DW_CFA_GNU_window_save:
3052 case DW_CFA_remember_state:
3053 case DW_CFA_restore_state:
3054 return dw_cfi_oprnd_unused;
3056 case DW_CFA_set_loc:
3057 case DW_CFA_advance_loc1:
3058 case DW_CFA_advance_loc2:
3059 case DW_CFA_advance_loc4:
3060 case DW_CFA_MIPS_advance_loc8:
3061 return dw_cfi_oprnd_addr;
3063 case DW_CFA_offset:
3064 case DW_CFA_offset_extended:
3065 case DW_CFA_def_cfa:
3066 case DW_CFA_offset_extended_sf:
3067 case DW_CFA_def_cfa_sf:
3068 case DW_CFA_restore:
3069 case DW_CFA_restore_extended:
3070 case DW_CFA_undefined:
3071 case DW_CFA_same_value:
3072 case DW_CFA_def_cfa_register:
3073 case DW_CFA_register:
3074 case DW_CFA_expression:
3075 return dw_cfi_oprnd_reg_num;
3077 case DW_CFA_def_cfa_offset:
3078 case DW_CFA_GNU_args_size:
3079 case DW_CFA_def_cfa_offset_sf:
3080 return dw_cfi_oprnd_offset;
3082 case DW_CFA_def_cfa_expression:
3083 return dw_cfi_oprnd_loc;
3085 default:
3086 gcc_unreachable ();
3090 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
3091 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3092 (enum dwarf_call_frame_info cfi);
3094 static enum dw_cfi_oprnd_type
3095 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3097 switch (cfi)
3099 case DW_CFA_def_cfa:
3100 case DW_CFA_def_cfa_sf:
3101 case DW_CFA_offset:
3102 case DW_CFA_offset_extended_sf:
3103 case DW_CFA_offset_extended:
3104 return dw_cfi_oprnd_offset;
3106 case DW_CFA_register:
3107 return dw_cfi_oprnd_reg_num;
3109 case DW_CFA_expression:
3110 return dw_cfi_oprnd_loc;
3112 default:
3113 return dw_cfi_oprnd_unused;
3117 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
3118 switch to the data section instead, and write out a synthetic start label
3119 for collect2 the first time around. */
3121 static void
3122 switch_to_eh_frame_section (bool back)
3124 tree label;
3126 #ifdef EH_FRAME_SECTION_NAME
3127 if (eh_frame_section == 0)
3129 int flags;
3131 if (EH_TABLES_CAN_BE_READ_ONLY)
3133 int fde_encoding;
3134 int per_encoding;
3135 int lsda_encoding;
3137 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3138 /*global=*/0);
3139 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3140 /*global=*/1);
3141 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3142 /*global=*/0);
3143 flags = ((! flag_pic
3144 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3145 && (fde_encoding & 0x70) != DW_EH_PE_aligned
3146 && (per_encoding & 0x70) != DW_EH_PE_absptr
3147 && (per_encoding & 0x70) != DW_EH_PE_aligned
3148 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3149 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3150 ? 0 : SECTION_WRITE);
3152 else
3153 flags = SECTION_WRITE;
3154 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3156 #endif /* EH_FRAME_SECTION_NAME */
3158 if (eh_frame_section)
3159 switch_to_section (eh_frame_section);
3160 else
3162 /* We have no special eh_frame section. Put the information in
3163 the data section and emit special labels to guide collect2. */
3164 switch_to_section (data_section);
3166 if (!back)
3168 label = get_file_function_name ("F");
3169 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3170 targetm.asm_out.globalize_label (asm_out_file,
3171 IDENTIFIER_POINTER (label));
3172 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3177 /* Switch [BACK] to the eh or debug frame table section, depending on
3178 FOR_EH. */
3180 static void
3181 switch_to_frame_table_section (int for_eh, bool back)
3183 if (for_eh)
3184 switch_to_eh_frame_section (back);
3185 else
3187 if (!debug_frame_section)
3188 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3189 SECTION_DEBUG, NULL);
3190 switch_to_section (debug_frame_section);
3194 /* Output a Call Frame Information opcode and its operand(s). */
3196 static void
3197 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3199 unsigned long r;
3200 HOST_WIDE_INT off;
3202 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3203 dw2_asm_output_data (1, (cfi->dw_cfi_opc
3204 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3205 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3206 ((unsigned HOST_WIDE_INT)
3207 cfi->dw_cfi_oprnd1.dw_cfi_offset));
3208 else if (cfi->dw_cfi_opc == DW_CFA_offset)
3210 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3211 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3212 "DW_CFA_offset, column %#lx", r);
3213 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3214 dw2_asm_output_data_uleb128 (off, NULL);
3216 else if (cfi->dw_cfi_opc == DW_CFA_restore)
3218 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3219 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3220 "DW_CFA_restore, column %#lx", r);
3222 else
3224 dw2_asm_output_data (1, cfi->dw_cfi_opc,
3225 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3227 switch (cfi->dw_cfi_opc)
3229 case DW_CFA_set_loc:
3230 if (for_eh)
3231 dw2_asm_output_encoded_addr_rtx (
3232 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3233 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3234 false, NULL);
3235 else
3236 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3237 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3238 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3239 break;
3241 case DW_CFA_advance_loc1:
3242 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3243 fde->dw_fde_current_label, NULL);
3244 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3245 break;
3247 case DW_CFA_advance_loc2:
3248 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3249 fde->dw_fde_current_label, NULL);
3250 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3251 break;
3253 case DW_CFA_advance_loc4:
3254 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3255 fde->dw_fde_current_label, NULL);
3256 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3257 break;
3259 case DW_CFA_MIPS_advance_loc8:
3260 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3261 fde->dw_fde_current_label, NULL);
3262 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3263 break;
3265 case DW_CFA_offset_extended:
3266 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3267 dw2_asm_output_data_uleb128 (r, NULL);
3268 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3269 dw2_asm_output_data_uleb128 (off, NULL);
3270 break;
3272 case DW_CFA_def_cfa:
3273 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3274 dw2_asm_output_data_uleb128 (r, NULL);
3275 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3276 break;
3278 case DW_CFA_offset_extended_sf:
3279 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3280 dw2_asm_output_data_uleb128 (r, NULL);
3281 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3282 dw2_asm_output_data_sleb128 (off, NULL);
3283 break;
3285 case DW_CFA_def_cfa_sf:
3286 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3287 dw2_asm_output_data_uleb128 (r, NULL);
3288 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3289 dw2_asm_output_data_sleb128 (off, NULL);
3290 break;
3292 case DW_CFA_restore_extended:
3293 case DW_CFA_undefined:
3294 case DW_CFA_same_value:
3295 case DW_CFA_def_cfa_register:
3296 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3297 dw2_asm_output_data_uleb128 (r, NULL);
3298 break;
3300 case DW_CFA_register:
3301 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3302 dw2_asm_output_data_uleb128 (r, NULL);
3303 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3304 dw2_asm_output_data_uleb128 (r, NULL);
3305 break;
3307 case DW_CFA_def_cfa_offset:
3308 case DW_CFA_GNU_args_size:
3309 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3310 break;
3312 case DW_CFA_def_cfa_offset_sf:
3313 off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3314 dw2_asm_output_data_sleb128 (off, NULL);
3315 break;
3317 case DW_CFA_GNU_window_save:
3318 break;
3320 case DW_CFA_def_cfa_expression:
3321 case DW_CFA_expression:
3322 output_cfa_loc (cfi, for_eh);
3323 break;
3325 case DW_CFA_GNU_negative_offset_extended:
3326 /* Obsoleted by DW_CFA_offset_extended_sf. */
3327 gcc_unreachable ();
3329 default:
3330 break;
3335 /* Similar, but do it via assembler directives instead. */
3337 static void
3338 output_cfi_directive (dw_cfi_ref cfi)
3340 unsigned long r, r2;
3342 switch (cfi->dw_cfi_opc)
3344 case DW_CFA_advance_loc:
3345 case DW_CFA_advance_loc1:
3346 case DW_CFA_advance_loc2:
3347 case DW_CFA_advance_loc4:
3348 case DW_CFA_MIPS_advance_loc8:
3349 case DW_CFA_set_loc:
3350 /* Should only be created by add_fde_cfi in a code path not
3351 followed when emitting via directives. The assembler is
3352 going to take care of this for us. */
3353 gcc_unreachable ();
3355 case DW_CFA_offset:
3356 case DW_CFA_offset_extended:
3357 case DW_CFA_offset_extended_sf:
3358 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3359 fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3360 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3361 break;
3363 case DW_CFA_restore:
3364 case DW_CFA_restore_extended:
3365 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3366 fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3367 break;
3369 case DW_CFA_undefined:
3370 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3371 fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3372 break;
3374 case DW_CFA_same_value:
3375 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3376 fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3377 break;
3379 case DW_CFA_def_cfa:
3380 case DW_CFA_def_cfa_sf:
3381 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3382 fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3383 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3384 break;
3386 case DW_CFA_def_cfa_register:
3387 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3388 fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3389 break;
3391 case DW_CFA_register:
3392 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3393 r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3394 fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3395 break;
3397 case DW_CFA_def_cfa_offset:
3398 case DW_CFA_def_cfa_offset_sf:
3399 fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3400 HOST_WIDE_INT_PRINT_DEC"\n",
3401 cfi->dw_cfi_oprnd1.dw_cfi_offset);
3402 break;
3404 case DW_CFA_remember_state:
3405 fprintf (asm_out_file, "\t.cfi_remember_state\n");
3406 break;
3407 case DW_CFA_restore_state:
3408 fprintf (asm_out_file, "\t.cfi_restore_state\n");
3409 break;
3411 case DW_CFA_GNU_args_size:
3412 fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3413 dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3414 if (flag_debug_asm)
3415 fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3416 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3417 fputc ('\n', asm_out_file);
3418 break;
3420 case DW_CFA_GNU_window_save:
3421 fprintf (asm_out_file, "\t.cfi_window_save\n");
3422 break;
3424 case DW_CFA_def_cfa_expression:
3425 case DW_CFA_expression:
3426 fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3427 output_cfa_loc_raw (cfi);
3428 fputc ('\n', asm_out_file);
3429 break;
3431 default:
3432 gcc_unreachable ();
3436 /* Output CFIs from VEC, up to index UPTO, to bring current FDE to the
3437 same state as after executing CFIs in CFI chain. DO_CFI_ASM is
3438 true if .cfi_* directives shall be emitted, false otherwise. If it
3439 is false, FDE and FOR_EH are the other arguments to pass to
3440 output_cfi. */
3442 static void
3443 output_cfis (cfi_vec vec, int upto, bool do_cfi_asm,
3444 dw_fde_ref fde, bool for_eh)
3446 int ix;
3447 struct dw_cfi_struct cfi_buf;
3448 dw_cfi_ref cfi2;
3449 dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3450 VEC(dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3451 unsigned int len, idx;
3453 for (ix = 0; ix < upto + 1; ix++)
3455 dw_cfi_ref cfi = ix < upto ? VEC_index (dw_cfi_ref, vec, ix) : NULL;
3456 switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3458 case DW_CFA_advance_loc:
3459 case DW_CFA_advance_loc1:
3460 case DW_CFA_advance_loc2:
3461 case DW_CFA_advance_loc4:
3462 case DW_CFA_MIPS_advance_loc8:
3463 case DW_CFA_set_loc:
3464 /* All advances should be ignored. */
3465 break;
3466 case DW_CFA_remember_state:
3468 dw_cfi_ref args_size = cfi_args_size;
3470 /* Skip everything between .cfi_remember_state and
3471 .cfi_restore_state. */
3472 ix++;
3473 if (ix == upto)
3474 goto flush_all;
3476 for (; ix < upto; ix++)
3478 cfi2 = VEC_index (dw_cfi_ref, vec, ix);
3479 if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3480 break;
3481 else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3482 args_size = cfi2;
3483 else
3484 gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3487 cfi_args_size = args_size;
3488 break;
3490 case DW_CFA_GNU_args_size:
3491 cfi_args_size = cfi;
3492 break;
3493 case DW_CFA_GNU_window_save:
3494 goto flush_all;
3495 case DW_CFA_offset:
3496 case DW_CFA_offset_extended:
3497 case DW_CFA_offset_extended_sf:
3498 case DW_CFA_restore:
3499 case DW_CFA_restore_extended:
3500 case DW_CFA_undefined:
3501 case DW_CFA_same_value:
3502 case DW_CFA_register:
3503 case DW_CFA_val_offset:
3504 case DW_CFA_val_offset_sf:
3505 case DW_CFA_expression:
3506 case DW_CFA_val_expression:
3507 case DW_CFA_GNU_negative_offset_extended:
3508 if (VEC_length (dw_cfi_ref, regs)
3509 <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3510 VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3511 cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3512 VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
3513 cfi);
3514 break;
3515 case DW_CFA_def_cfa:
3516 case DW_CFA_def_cfa_sf:
3517 case DW_CFA_def_cfa_expression:
3518 cfi_cfa = cfi;
3519 cfi_cfa_offset = cfi;
3520 break;
3521 case DW_CFA_def_cfa_register:
3522 cfi_cfa = cfi;
3523 break;
3524 case DW_CFA_def_cfa_offset:
3525 case DW_CFA_def_cfa_offset_sf:
3526 cfi_cfa_offset = cfi;
3527 break;
3528 case DW_CFA_nop:
3529 gcc_assert (cfi == NULL);
3530 flush_all:
3531 len = VEC_length (dw_cfi_ref, regs);
3532 for (idx = 0; idx < len; idx++)
3534 cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3535 if (cfi2 != NULL
3536 && cfi2->dw_cfi_opc != DW_CFA_restore
3537 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3539 if (do_cfi_asm)
3540 output_cfi_directive (cfi2);
3541 else
3542 output_cfi (cfi2, fde, for_eh);
3545 if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3547 gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3548 cfi_buf = *cfi_cfa;
3549 switch (cfi_cfa_offset->dw_cfi_opc)
3551 case DW_CFA_def_cfa_offset:
3552 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3553 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3554 break;
3555 case DW_CFA_def_cfa_offset_sf:
3556 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3557 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3558 break;
3559 case DW_CFA_def_cfa:
3560 case DW_CFA_def_cfa_sf:
3561 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3562 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3563 break;
3564 default:
3565 gcc_unreachable ();
3567 cfi_cfa = &cfi_buf;
3569 else if (cfi_cfa_offset)
3570 cfi_cfa = cfi_cfa_offset;
3571 if (cfi_cfa)
3573 if (do_cfi_asm)
3574 output_cfi_directive (cfi_cfa);
3575 else
3576 output_cfi (cfi_cfa, fde, for_eh);
3578 cfi_cfa = NULL;
3579 cfi_cfa_offset = NULL;
3580 if (cfi_args_size
3581 && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3583 if (do_cfi_asm)
3584 output_cfi_directive (cfi_args_size);
3585 else
3586 output_cfi (cfi_args_size, fde, for_eh);
3588 cfi_args_size = NULL;
3589 if (cfi == NULL)
3591 VEC_free (dw_cfi_ref, heap, regs);
3592 return;
3594 else if (do_cfi_asm)
3595 output_cfi_directive (cfi);
3596 else
3597 output_cfi (cfi, fde, for_eh);
3598 break;
3599 default:
3600 gcc_unreachable ();
3605 /* Like output_cfis, but emit all CFIs in the vector. */
3606 static void
3607 output_all_cfis (cfi_vec vec, bool do_cfi_asm,
3608 dw_fde_ref fde, bool for_eh)
3610 output_cfis (vec, VEC_length (dw_cfi_ref, vec), do_cfi_asm, fde, for_eh);
3613 /* Output one FDE. */
3615 static void
3616 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3617 char *section_start_label, int fde_encoding, char *augmentation,
3618 bool any_lsda_needed, int lsda_encoding)
3620 int ix;
3621 const char *begin, *end;
3622 static unsigned int j;
3623 char l1[20], l2[20];
3624 dw_cfi_ref cfi;
3626 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3627 /* empty */ 0);
3628 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3629 for_eh + j);
3630 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3631 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3632 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3633 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3634 " indicating 64-bit DWARF extension");
3635 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3636 "FDE Length");
3637 ASM_OUTPUT_LABEL (asm_out_file, l1);
3639 if (for_eh)
3640 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3641 else
3642 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3643 debug_frame_section, "FDE CIE offset");
3645 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
3646 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
3648 if (for_eh)
3650 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3651 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3652 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3653 "FDE initial location");
3654 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3655 end, begin, "FDE address range");
3657 else
3659 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3660 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3663 if (augmentation[0])
3665 if (any_lsda_needed)
3667 int size = size_of_encoded_value (lsda_encoding);
3669 if (lsda_encoding == DW_EH_PE_aligned)
3671 int offset = ( 4 /* Length */
3672 + 4 /* CIE offset */
3673 + 2 * size_of_encoded_value (fde_encoding)
3674 + 1 /* Augmentation size */ );
3675 int pad = -offset & (PTR_SIZE - 1);
3677 size += pad;
3678 gcc_assert (size_of_uleb128 (size) == 1);
3681 dw2_asm_output_data_uleb128 (size, "Augmentation size");
3683 if (fde->uses_eh_lsda)
3685 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3686 fde->funcdef_number);
3687 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3688 gen_rtx_SYMBOL_REF (Pmode, l1),
3689 false,
3690 "Language Specific Data Area");
3692 else
3694 if (lsda_encoding == DW_EH_PE_aligned)
3695 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3696 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3697 "Language Specific Data Area (none)");
3700 else
3701 dw2_asm_output_data_uleb128 (0, "Augmentation size");
3704 /* Loop through the Call Frame Instructions associated with
3705 this FDE. */
3706 fde->dw_fde_current_label = begin;
3707 if (fde->dw_fde_second_begin == NULL)
3708 FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
3709 output_cfi (cfi, fde, for_eh);
3710 else if (!second)
3712 if (fde->dw_fde_switch_cfi_index > 0)
3713 FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
3715 if (ix == fde->dw_fde_switch_cfi_index)
3716 break;
3717 output_cfi (cfi, fde, for_eh);
3720 else
3722 int i, from = 0;
3723 int until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
3725 if (fde->dw_fde_switch_cfi_index > 0)
3727 from = fde->dw_fde_switch_cfi_index;
3728 output_cfis (fde->dw_fde_cfi, from, false, fde, for_eh);
3730 for (i = from; i < until; i++)
3731 output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i),
3732 fde, for_eh);
3735 /* If we are to emit a ref/link from function bodies to their frame tables,
3736 do it now. This is typically performed to make sure that tables
3737 associated with functions are dragged with them and not discarded in
3738 garbage collecting links. We need to do this on a per function basis to
3739 cope with -ffunction-sections. */
3741 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3742 /* Switch to the function section, emit the ref to the tables, and
3743 switch *back* into the table section. */
3744 switch_to_section (function_section (fde->decl));
3745 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3746 switch_to_frame_table_section (for_eh, true);
3747 #endif
3749 /* Pad the FDE out to an address sized boundary. */
3750 ASM_OUTPUT_ALIGN (asm_out_file,
3751 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3752 ASM_OUTPUT_LABEL (asm_out_file, l2);
3754 j += 2;
3757 /* Return true if frame description entry FDE is needed for EH. */
3759 static bool
3760 fde_needed_for_eh_p (dw_fde_ref fde)
3762 if (flag_asynchronous_unwind_tables)
3763 return true;
3765 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3766 return true;
3768 if (fde->uses_eh_lsda)
3769 return true;
3771 /* If exceptions are enabled, we have collected nothrow info. */
3772 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3773 return false;
3775 return true;
3778 /* Output the call frame information used to record information
3779 that relates to calculating the frame pointer, and records the
3780 location of saved registers. */
3782 static void
3783 output_call_frame_info (int for_eh)
3785 unsigned int i;
3786 dw_fde_ref fde;
3787 dw_cfi_ref cfi;
3788 char l1[20], l2[20], section_start_label[20];
3789 bool any_lsda_needed = false;
3790 char augmentation[6];
3791 int augmentation_size;
3792 int fde_encoding = DW_EH_PE_absptr;
3793 int per_encoding = DW_EH_PE_absptr;
3794 int lsda_encoding = DW_EH_PE_absptr;
3795 int return_reg;
3796 rtx personality = NULL;
3797 int dw_cie_version;
3799 /* Don't emit a CIE if there won't be any FDEs. */
3800 if (fde_table_in_use == 0)
3801 return;
3803 /* Nothing to do if the assembler's doing it all. */
3804 if (dwarf2out_do_cfi_asm ())
3805 return;
3807 /* If we don't have any functions we'll want to unwind out of, don't emit
3808 any EH unwind information. If we make FDEs linkonce, we may have to
3809 emit an empty label for an FDE that wouldn't otherwise be emitted. We
3810 want to avoid having an FDE kept around when the function it refers to
3811 is discarded. Example where this matters: a primary function template
3812 in C++ requires EH information, an explicit specialization doesn't. */
3813 if (for_eh)
3815 bool any_eh_needed = false;
3817 for (i = 0; i < fde_table_in_use; i++)
3818 if (fde_table[i].uses_eh_lsda)
3819 any_eh_needed = any_lsda_needed = true;
3820 else if (fde_needed_for_eh_p (&fde_table[i]))
3821 any_eh_needed = true;
3822 else if (TARGET_USES_WEAK_UNWIND_INFO)
3823 targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3824 1, 1);
3826 if (!any_eh_needed)
3827 return;
3830 /* We're going to be generating comments, so turn on app. */
3831 if (flag_debug_asm)
3832 app_enable ();
3834 /* Switch to the proper frame section, first time. */
3835 switch_to_frame_table_section (for_eh, false);
3837 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3838 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3840 /* Output the CIE. */
3841 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3842 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3843 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3844 dw2_asm_output_data (4, 0xffffffff,
3845 "Initial length escape value indicating 64-bit DWARF extension");
3846 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3847 "Length of Common Information Entry");
3848 ASM_OUTPUT_LABEL (asm_out_file, l1);
3850 /* Now that the CIE pointer is PC-relative for EH,
3851 use 0 to identify the CIE. */
3852 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3853 (for_eh ? 0 : DWARF_CIE_ID),
3854 "CIE Identifier Tag");
3856 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3857 use CIE version 1, unless that would produce incorrect results
3858 due to overflowing the return register column. */
3859 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3860 dw_cie_version = 1;
3861 if (return_reg >= 256 || dwarf_version > 2)
3862 dw_cie_version = 3;
3863 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3865 augmentation[0] = 0;
3866 augmentation_size = 0;
3868 personality = current_unit_personality;
3869 if (for_eh)
3871 char *p;
3873 /* Augmentation:
3874 z Indicates that a uleb128 is present to size the
3875 augmentation section.
3876 L Indicates the encoding (and thus presence) of
3877 an LSDA pointer in the FDE augmentation.
3878 R Indicates a non-default pointer encoding for
3879 FDE code pointers.
3880 P Indicates the presence of an encoding + language
3881 personality routine in the CIE augmentation. */
3883 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3884 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3885 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3887 p = augmentation + 1;
3888 if (personality)
3890 *p++ = 'P';
3891 augmentation_size += 1 + size_of_encoded_value (per_encoding);
3892 assemble_external_libcall (personality);
3894 if (any_lsda_needed)
3896 *p++ = 'L';
3897 augmentation_size += 1;
3899 if (fde_encoding != DW_EH_PE_absptr)
3901 *p++ = 'R';
3902 augmentation_size += 1;
3904 if (p > augmentation + 1)
3906 augmentation[0] = 'z';
3907 *p = '\0';
3910 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
3911 if (personality && per_encoding == DW_EH_PE_aligned)
3913 int offset = ( 4 /* Length */
3914 + 4 /* CIE Id */
3915 + 1 /* CIE version */
3916 + strlen (augmentation) + 1 /* Augmentation */
3917 + size_of_uleb128 (1) /* Code alignment */
3918 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3919 + 1 /* RA column */
3920 + 1 /* Augmentation size */
3921 + 1 /* Personality encoding */ );
3922 int pad = -offset & (PTR_SIZE - 1);
3924 augmentation_size += pad;
3926 /* Augmentations should be small, so there's scarce need to
3927 iterate for a solution. Die if we exceed one uleb128 byte. */
3928 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3932 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3933 if (dw_cie_version >= 4)
3935 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3936 dw2_asm_output_data (1, 0, "CIE Segment Size");
3938 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3939 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3940 "CIE Data Alignment Factor");
3942 if (dw_cie_version == 1)
3943 dw2_asm_output_data (1, return_reg, "CIE RA Column");
3944 else
3945 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3947 if (augmentation[0])
3949 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3950 if (personality)
3952 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3953 eh_data_format_name (per_encoding));
3954 dw2_asm_output_encoded_addr_rtx (per_encoding,
3955 personality,
3956 true, NULL);
3959 if (any_lsda_needed)
3960 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3961 eh_data_format_name (lsda_encoding));
3963 if (fde_encoding != DW_EH_PE_absptr)
3964 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3965 eh_data_format_name (fde_encoding));
3968 FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
3969 output_cfi (cfi, NULL, for_eh);
3971 /* Pad the CIE out to an address sized boundary. */
3972 ASM_OUTPUT_ALIGN (asm_out_file,
3973 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3974 ASM_OUTPUT_LABEL (asm_out_file, l2);
3976 /* Loop through all of the FDE's. */
3977 for (i = 0; i < fde_table_in_use; i++)
3979 unsigned int k;
3980 fde = &fde_table[i];
3982 /* Don't emit EH unwind info for leaf functions that don't need it. */
3983 if (for_eh && !fde_needed_for_eh_p (fde))
3984 continue;
3986 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
3987 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3988 augmentation, any_lsda_needed, lsda_encoding);
3991 if (for_eh && targetm.terminate_dw2_eh_frame_info)
3992 dw2_asm_output_data (4, 0, "End of Table");
3993 #ifdef MIPS_DEBUGGING_INFO
3994 /* Work around Irix 6 assembler bug whereby labels at the end of a section
3995 get a value of 0. Putting .align 0 after the label fixes it. */
3996 ASM_OUTPUT_ALIGN (asm_out_file, 0);
3997 #endif
3999 /* Turn off app to make assembly quicker. */
4000 if (flag_debug_asm)
4001 app_disable ();
4004 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
4006 static void
4007 dwarf2out_do_cfi_startproc (bool second)
4009 int enc;
4010 rtx ref;
4011 rtx personality = get_personality_function (current_function_decl);
4013 fprintf (asm_out_file, "\t.cfi_startproc\n");
4015 if (personality)
4017 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
4018 ref = personality;
4020 /* ??? The GAS support isn't entirely consistent. We have to
4021 handle indirect support ourselves, but PC-relative is done
4022 in the assembler. Further, the assembler can't handle any
4023 of the weirder relocation types. */
4024 if (enc & DW_EH_PE_indirect)
4025 ref = dw2_force_const_mem (ref, true);
4027 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4028 output_addr_const (asm_out_file, ref);
4029 fputc ('\n', asm_out_file);
4032 if (crtl->uses_eh_lsda)
4034 char lab[20];
4036 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4037 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4038 current_function_funcdef_no);
4039 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4040 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4042 if (enc & DW_EH_PE_indirect)
4043 ref = dw2_force_const_mem (ref, true);
4045 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4046 output_addr_const (asm_out_file, ref);
4047 fputc ('\n', asm_out_file);
4051 /* Output a marker (i.e. a label) for the beginning of a function, before
4052 the prologue. */
4054 void
4055 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4056 const char *file ATTRIBUTE_UNUSED)
4058 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4059 char * dup_label;
4060 dw_fde_ref fde;
4061 section *fnsec;
4062 bool do_frame;
4064 current_function_func_begin_label = NULL;
4066 do_frame = dwarf2out_do_frame ();
4068 /* ??? current_function_func_begin_label is also used by except.c for
4069 call-site information. We must emit this label if it might be used. */
4070 if (!do_frame
4071 && (!flag_exceptions
4072 || targetm.except_unwind_info (&global_options) != UI_TARGET))
4073 return;
4075 fnsec = function_section (current_function_decl);
4076 switch_to_section (fnsec);
4077 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4078 current_function_funcdef_no);
4079 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4080 current_function_funcdef_no);
4081 dup_label = xstrdup (label);
4082 current_function_func_begin_label = dup_label;
4084 /* We can elide the fde allocation if we're not emitting debug info. */
4085 if (!do_frame)
4086 return;
4088 /* Expand the fde table if necessary. */
4089 if (fde_table_in_use == fde_table_allocated)
4091 fde_table_allocated += FDE_TABLE_INCREMENT;
4092 fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4093 memset (fde_table + fde_table_in_use, 0,
4094 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4097 /* Record the FDE associated with this function. */
4098 current_funcdef_fde = fde_table_in_use;
4100 /* Add the new FDE at the end of the fde_table. */
4101 fde = &fde_table[fde_table_in_use++];
4102 fde->decl = current_function_decl;
4103 fde->dw_fde_begin = dup_label;
4104 fde->dw_fde_end = NULL;
4105 fde->dw_fde_current_label = dup_label;
4106 fde->dw_fde_second_begin = NULL;
4107 fde->dw_fde_second_end = NULL;
4108 fde->dw_fde_vms_end_prologue = NULL;
4109 fde->dw_fde_vms_begin_epilogue = NULL;
4110 fde->dw_fde_cfi = VEC_alloc (dw_cfi_ref, gc, 20);
4111 fde->dw_fde_switch_cfi_index = 0;
4112 fde->funcdef_number = current_function_funcdef_no;
4113 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4114 fde->uses_eh_lsda = crtl->uses_eh_lsda;
4115 fde->nothrow = crtl->nothrow;
4116 fde->drap_reg = INVALID_REGNUM;
4117 fde->vdrap_reg = INVALID_REGNUM;
4118 fde->in_std_section = (fnsec == text_section
4119 || (cold_text_section && fnsec == cold_text_section));
4120 fde->second_in_std_section = 0;
4122 args_size = old_args_size = 0;
4124 /* We only want to output line number information for the genuine dwarf2
4125 prologue case, not the eh frame case. */
4126 #ifdef DWARF2_DEBUGGING_INFO
4127 if (file)
4128 dwarf2out_source_line (line, file, 0, true);
4129 #endif
4131 if (dwarf2out_do_cfi_asm ())
4132 dwarf2out_do_cfi_startproc (false);
4133 else
4135 rtx personality = get_personality_function (current_function_decl);
4136 if (!current_unit_personality)
4137 current_unit_personality = personality;
4139 /* We cannot keep a current personality per function as without CFI
4140 asm, at the point where we emit the CFI data, there is no current
4141 function anymore. */
4142 if (personality && current_unit_personality != personality)
4143 sorry ("multiple EH personalities are supported only with assemblers "
4144 "supporting .cfi_personality directive");
4148 /* Output a marker (i.e. a label) for the end of the generated code
4149 for a function prologue. This gets called *after* the prologue code has
4150 been generated. */
4152 void
4153 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4154 const char *file ATTRIBUTE_UNUSED)
4156 dw_fde_ref fde;
4157 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4159 /* Output a label to mark the endpoint of the code generated for this
4160 function. */
4161 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4162 current_function_funcdef_no);
4163 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4164 current_function_funcdef_no);
4165 fde = &fde_table[fde_table_in_use - 1];
4166 fde->dw_fde_vms_end_prologue = xstrdup (label);
4169 /* Output a marker (i.e. a label) for the beginning of the generated code
4170 for a function epilogue. This gets called *before* the prologue code has
4171 been generated. */
4173 void
4174 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4175 const char *file ATTRIBUTE_UNUSED)
4177 dw_fde_ref fde;
4178 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4180 fde = &fde_table[fde_table_in_use - 1];
4181 if (fde->dw_fde_vms_begin_epilogue)
4182 return;
4184 /* Output a label to mark the endpoint of the code generated for this
4185 function. */
4186 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4187 current_function_funcdef_no);
4188 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4189 current_function_funcdef_no);
4190 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4193 /* Output a marker (i.e. a label) for the absolute end of the generated code
4194 for a function definition. This gets called *after* the epilogue code has
4195 been generated. */
4197 void
4198 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4199 const char *file ATTRIBUTE_UNUSED)
4201 dw_fde_ref fde;
4202 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4204 last_var_location_insn = NULL_RTX;
4206 if (dwarf2out_do_cfi_asm ())
4207 fprintf (asm_out_file, "\t.cfi_endproc\n");
4209 /* Output a label to mark the endpoint of the code generated for this
4210 function. */
4211 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4212 current_function_funcdef_no);
4213 ASM_OUTPUT_LABEL (asm_out_file, label);
4214 fde = current_fde ();
4215 gcc_assert (fde != NULL);
4216 if (fde->dw_fde_second_begin == NULL)
4217 fde->dw_fde_end = xstrdup (label);
4220 void
4221 dwarf2out_frame_init (void)
4223 /* Allocate the initial hunk of the fde_table. */
4224 fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4225 fde_table_allocated = FDE_TABLE_INCREMENT;
4226 fde_table_in_use = 0;
4228 /* Generate the CFA instructions common to all FDE's. Do it now for the
4229 sake of lookup_cfa. */
4231 /* On entry, the Canonical Frame Address is at SP. */
4232 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4234 if (targetm.debug_unwind_info () == UI_DWARF2
4235 || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4236 initial_return_save (INCOMING_RETURN_ADDR_RTX);
4239 void
4240 dwarf2out_frame_finish (void)
4242 /* Output call frame information. */
4243 if (targetm.debug_unwind_info () == UI_DWARF2)
4244 output_call_frame_info (0);
4246 /* Output another copy for the unwinder. */
4247 if ((flag_unwind_tables || flag_exceptions)
4248 && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4249 output_call_frame_info (1);
4252 /* Note that the current function section is being used for code. */
4254 static void
4255 dwarf2out_note_section_used (void)
4257 section *sec = current_function_section ();
4258 if (sec == text_section)
4259 text_section_used = true;
4260 else if (sec == cold_text_section)
4261 cold_text_section_used = true;
4264 static void var_location_switch_text_section (void);
4265 static void set_cur_line_info_table (section *);
4267 void
4268 dwarf2out_switch_text_section (void)
4270 section *sect;
4271 dw_fde_ref fde = current_fde ();
4273 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
4275 if (!in_cold_section_p)
4277 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
4278 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
4279 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
4281 else
4283 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
4284 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
4285 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
4287 have_multiple_function_sections = true;
4289 /* Reset the current label on switching text sections, so that we
4290 don't attempt to advance_loc4 between labels in different sections. */
4291 fde->dw_fde_current_label = NULL;
4293 /* There is no need to mark used sections when not debugging. */
4294 if (cold_text_section != NULL)
4295 dwarf2out_note_section_used ();
4297 if (dwarf2out_do_cfi_asm ())
4298 fprintf (asm_out_file, "\t.cfi_endproc\n");
4300 /* Now do the real section switch. */
4301 sect = current_function_section ();
4302 switch_to_section (sect);
4304 fde->second_in_std_section
4305 = (sect == text_section
4306 || (cold_text_section && sect == cold_text_section));
4308 if (dwarf2out_do_cfi_asm ())
4310 dwarf2out_do_cfi_startproc (true);
4311 /* As this is a different FDE, insert all current CFI instructions
4312 again. */
4313 output_all_cfis (fde->dw_fde_cfi, true, fde, true);
4315 fde->dw_fde_switch_cfi_index = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
4316 var_location_switch_text_section ();
4318 set_cur_line_info_table (sect);
4321 /* And now, the subset of the debugging information support code necessary
4322 for emitting location expressions. */
4324 /* Data about a single source file. */
4325 struct GTY(()) dwarf_file_data {
4326 const char * filename;
4327 int emitted_number;
4330 typedef struct dw_val_struct *dw_val_ref;
4331 typedef struct die_struct *dw_die_ref;
4332 typedef const struct die_struct *const_dw_die_ref;
4333 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4334 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4336 typedef struct GTY(()) deferred_locations_struct
4338 tree variable;
4339 dw_die_ref die;
4340 } deferred_locations;
4342 DEF_VEC_O(deferred_locations);
4343 DEF_VEC_ALLOC_O(deferred_locations,gc);
4345 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4347 DEF_VEC_P(dw_die_ref);
4348 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4350 /* Each DIE may have a series of attribute/value pairs. Values
4351 can take on several forms. The forms that are used in this
4352 implementation are listed below. */
4354 enum dw_val_class
4356 dw_val_class_addr,
4357 dw_val_class_offset,
4358 dw_val_class_loc,
4359 dw_val_class_loc_list,
4360 dw_val_class_range_list,
4361 dw_val_class_const,
4362 dw_val_class_unsigned_const,
4363 dw_val_class_const_double,
4364 dw_val_class_vec,
4365 dw_val_class_flag,
4366 dw_val_class_die_ref,
4367 dw_val_class_fde_ref,
4368 dw_val_class_lbl_id,
4369 dw_val_class_lineptr,
4370 dw_val_class_str,
4371 dw_val_class_macptr,
4372 dw_val_class_file,
4373 dw_val_class_data8,
4374 dw_val_class_decl_ref,
4375 dw_val_class_vms_delta
4378 /* Describe a floating point constant value, or a vector constant value. */
4380 typedef struct GTY(()) dw_vec_struct {
4381 unsigned char * GTY((length ("%h.length"))) array;
4382 unsigned length;
4383 unsigned elt_size;
4385 dw_vec_const;
4387 /* The dw_val_node describes an attribute's value, as it is
4388 represented internally. */
4390 typedef struct GTY(()) dw_val_struct {
4391 enum dw_val_class val_class;
4392 union dw_val_struct_union
4394 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4395 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4396 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4397 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4398 HOST_WIDE_INT GTY ((default)) val_int;
4399 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4400 double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4401 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4402 struct dw_val_die_union
4404 dw_die_ref die;
4405 int external;
4406 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4407 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4408 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4409 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4410 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4411 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4412 unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4413 tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4414 struct dw_val_vms_delta_union
4416 char * lbl1;
4417 char * lbl2;
4418 } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4420 GTY ((desc ("%1.val_class"))) v;
4422 dw_val_node;
4424 /* Locations in memory are described using a sequence of stack machine
4425 operations. */
4427 typedef struct GTY(()) dw_loc_descr_struct {
4428 dw_loc_descr_ref dw_loc_next;
4429 ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4430 /* Used to distinguish DW_OP_addr with a direct symbol relocation
4431 from DW_OP_addr with a dtp-relative symbol relocation. */
4432 unsigned int dtprel : 1;
4433 int dw_loc_addr;
4434 dw_val_node dw_loc_oprnd1;
4435 dw_val_node dw_loc_oprnd2;
4437 dw_loc_descr_node;
4439 /* Location lists are ranges + location descriptions for that range,
4440 so you can track variables that are in different places over
4441 their entire life. */
4442 typedef struct GTY(()) dw_loc_list_struct {
4443 dw_loc_list_ref dw_loc_next;
4444 const char *begin; /* Label for begin address of range */
4445 const char *end; /* Label for end address of range */
4446 char *ll_symbol; /* Label for beginning of location list.
4447 Only on head of list */
4448 const char *section; /* Section this loclist is relative to */
4449 dw_loc_descr_ref expr;
4450 hashval_t hash;
4451 /* True if all addresses in this and subsequent lists are known to be
4452 resolved. */
4453 bool resolved_addr;
4454 /* True if this list has been replaced by dw_loc_next. */
4455 bool replaced;
4456 bool emitted;
4457 } dw_loc_list_node;
4459 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4461 /* Convert a DWARF stack opcode into its string name. */
4463 static const char *
4464 dwarf_stack_op_name (unsigned int op)
4466 switch (op)
4468 case DW_OP_addr:
4469 return "DW_OP_addr";
4470 case DW_OP_deref:
4471 return "DW_OP_deref";
4472 case DW_OP_const1u:
4473 return "DW_OP_const1u";
4474 case DW_OP_const1s:
4475 return "DW_OP_const1s";
4476 case DW_OP_const2u:
4477 return "DW_OP_const2u";
4478 case DW_OP_const2s:
4479 return "DW_OP_const2s";
4480 case DW_OP_const4u:
4481 return "DW_OP_const4u";
4482 case DW_OP_const4s:
4483 return "DW_OP_const4s";
4484 case DW_OP_const8u:
4485 return "DW_OP_const8u";
4486 case DW_OP_const8s:
4487 return "DW_OP_const8s";
4488 case DW_OP_constu:
4489 return "DW_OP_constu";
4490 case DW_OP_consts:
4491 return "DW_OP_consts";
4492 case DW_OP_dup:
4493 return "DW_OP_dup";
4494 case DW_OP_drop:
4495 return "DW_OP_drop";
4496 case DW_OP_over:
4497 return "DW_OP_over";
4498 case DW_OP_pick:
4499 return "DW_OP_pick";
4500 case DW_OP_swap:
4501 return "DW_OP_swap";
4502 case DW_OP_rot:
4503 return "DW_OP_rot";
4504 case DW_OP_xderef:
4505 return "DW_OP_xderef";
4506 case DW_OP_abs:
4507 return "DW_OP_abs";
4508 case DW_OP_and:
4509 return "DW_OP_and";
4510 case DW_OP_div:
4511 return "DW_OP_div";
4512 case DW_OP_minus:
4513 return "DW_OP_minus";
4514 case DW_OP_mod:
4515 return "DW_OP_mod";
4516 case DW_OP_mul:
4517 return "DW_OP_mul";
4518 case DW_OP_neg:
4519 return "DW_OP_neg";
4520 case DW_OP_not:
4521 return "DW_OP_not";
4522 case DW_OP_or:
4523 return "DW_OP_or";
4524 case DW_OP_plus:
4525 return "DW_OP_plus";
4526 case DW_OP_plus_uconst:
4527 return "DW_OP_plus_uconst";
4528 case DW_OP_shl:
4529 return "DW_OP_shl";
4530 case DW_OP_shr:
4531 return "DW_OP_shr";
4532 case DW_OP_shra:
4533 return "DW_OP_shra";
4534 case DW_OP_xor:
4535 return "DW_OP_xor";
4536 case DW_OP_bra:
4537 return "DW_OP_bra";
4538 case DW_OP_eq:
4539 return "DW_OP_eq";
4540 case DW_OP_ge:
4541 return "DW_OP_ge";
4542 case DW_OP_gt:
4543 return "DW_OP_gt";
4544 case DW_OP_le:
4545 return "DW_OP_le";
4546 case DW_OP_lt:
4547 return "DW_OP_lt";
4548 case DW_OP_ne:
4549 return "DW_OP_ne";
4550 case DW_OP_skip:
4551 return "DW_OP_skip";
4552 case DW_OP_lit0:
4553 return "DW_OP_lit0";
4554 case DW_OP_lit1:
4555 return "DW_OP_lit1";
4556 case DW_OP_lit2:
4557 return "DW_OP_lit2";
4558 case DW_OP_lit3:
4559 return "DW_OP_lit3";
4560 case DW_OP_lit4:
4561 return "DW_OP_lit4";
4562 case DW_OP_lit5:
4563 return "DW_OP_lit5";
4564 case DW_OP_lit6:
4565 return "DW_OP_lit6";
4566 case DW_OP_lit7:
4567 return "DW_OP_lit7";
4568 case DW_OP_lit8:
4569 return "DW_OP_lit8";
4570 case DW_OP_lit9:
4571 return "DW_OP_lit9";
4572 case DW_OP_lit10:
4573 return "DW_OP_lit10";
4574 case DW_OP_lit11:
4575 return "DW_OP_lit11";
4576 case DW_OP_lit12:
4577 return "DW_OP_lit12";
4578 case DW_OP_lit13:
4579 return "DW_OP_lit13";
4580 case DW_OP_lit14:
4581 return "DW_OP_lit14";
4582 case DW_OP_lit15:
4583 return "DW_OP_lit15";
4584 case DW_OP_lit16:
4585 return "DW_OP_lit16";
4586 case DW_OP_lit17:
4587 return "DW_OP_lit17";
4588 case DW_OP_lit18:
4589 return "DW_OP_lit18";
4590 case DW_OP_lit19:
4591 return "DW_OP_lit19";
4592 case DW_OP_lit20:
4593 return "DW_OP_lit20";
4594 case DW_OP_lit21:
4595 return "DW_OP_lit21";
4596 case DW_OP_lit22:
4597 return "DW_OP_lit22";
4598 case DW_OP_lit23:
4599 return "DW_OP_lit23";
4600 case DW_OP_lit24:
4601 return "DW_OP_lit24";
4602 case DW_OP_lit25:
4603 return "DW_OP_lit25";
4604 case DW_OP_lit26:
4605 return "DW_OP_lit26";
4606 case DW_OP_lit27:
4607 return "DW_OP_lit27";
4608 case DW_OP_lit28:
4609 return "DW_OP_lit28";
4610 case DW_OP_lit29:
4611 return "DW_OP_lit29";
4612 case DW_OP_lit30:
4613 return "DW_OP_lit30";
4614 case DW_OP_lit31:
4615 return "DW_OP_lit31";
4616 case DW_OP_reg0:
4617 return "DW_OP_reg0";
4618 case DW_OP_reg1:
4619 return "DW_OP_reg1";
4620 case DW_OP_reg2:
4621 return "DW_OP_reg2";
4622 case DW_OP_reg3:
4623 return "DW_OP_reg3";
4624 case DW_OP_reg4:
4625 return "DW_OP_reg4";
4626 case DW_OP_reg5:
4627 return "DW_OP_reg5";
4628 case DW_OP_reg6:
4629 return "DW_OP_reg6";
4630 case DW_OP_reg7:
4631 return "DW_OP_reg7";
4632 case DW_OP_reg8:
4633 return "DW_OP_reg8";
4634 case DW_OP_reg9:
4635 return "DW_OP_reg9";
4636 case DW_OP_reg10:
4637 return "DW_OP_reg10";
4638 case DW_OP_reg11:
4639 return "DW_OP_reg11";
4640 case DW_OP_reg12:
4641 return "DW_OP_reg12";
4642 case DW_OP_reg13:
4643 return "DW_OP_reg13";
4644 case DW_OP_reg14:
4645 return "DW_OP_reg14";
4646 case DW_OP_reg15:
4647 return "DW_OP_reg15";
4648 case DW_OP_reg16:
4649 return "DW_OP_reg16";
4650 case DW_OP_reg17:
4651 return "DW_OP_reg17";
4652 case DW_OP_reg18:
4653 return "DW_OP_reg18";
4654 case DW_OP_reg19:
4655 return "DW_OP_reg19";
4656 case DW_OP_reg20:
4657 return "DW_OP_reg20";
4658 case DW_OP_reg21:
4659 return "DW_OP_reg21";
4660 case DW_OP_reg22:
4661 return "DW_OP_reg22";
4662 case DW_OP_reg23:
4663 return "DW_OP_reg23";
4664 case DW_OP_reg24:
4665 return "DW_OP_reg24";
4666 case DW_OP_reg25:
4667 return "DW_OP_reg25";
4668 case DW_OP_reg26:
4669 return "DW_OP_reg26";
4670 case DW_OP_reg27:
4671 return "DW_OP_reg27";
4672 case DW_OP_reg28:
4673 return "DW_OP_reg28";
4674 case DW_OP_reg29:
4675 return "DW_OP_reg29";
4676 case DW_OP_reg30:
4677 return "DW_OP_reg30";
4678 case DW_OP_reg31:
4679 return "DW_OP_reg31";
4680 case DW_OP_breg0:
4681 return "DW_OP_breg0";
4682 case DW_OP_breg1:
4683 return "DW_OP_breg1";
4684 case DW_OP_breg2:
4685 return "DW_OP_breg2";
4686 case DW_OP_breg3:
4687 return "DW_OP_breg3";
4688 case DW_OP_breg4:
4689 return "DW_OP_breg4";
4690 case DW_OP_breg5:
4691 return "DW_OP_breg5";
4692 case DW_OP_breg6:
4693 return "DW_OP_breg6";
4694 case DW_OP_breg7:
4695 return "DW_OP_breg7";
4696 case DW_OP_breg8:
4697 return "DW_OP_breg8";
4698 case DW_OP_breg9:
4699 return "DW_OP_breg9";
4700 case DW_OP_breg10:
4701 return "DW_OP_breg10";
4702 case DW_OP_breg11:
4703 return "DW_OP_breg11";
4704 case DW_OP_breg12:
4705 return "DW_OP_breg12";
4706 case DW_OP_breg13:
4707 return "DW_OP_breg13";
4708 case DW_OP_breg14:
4709 return "DW_OP_breg14";
4710 case DW_OP_breg15:
4711 return "DW_OP_breg15";
4712 case DW_OP_breg16:
4713 return "DW_OP_breg16";
4714 case DW_OP_breg17:
4715 return "DW_OP_breg17";
4716 case DW_OP_breg18:
4717 return "DW_OP_breg18";
4718 case DW_OP_breg19:
4719 return "DW_OP_breg19";
4720 case DW_OP_breg20:
4721 return "DW_OP_breg20";
4722 case DW_OP_breg21:
4723 return "DW_OP_breg21";
4724 case DW_OP_breg22:
4725 return "DW_OP_breg22";
4726 case DW_OP_breg23:
4727 return "DW_OP_breg23";
4728 case DW_OP_breg24:
4729 return "DW_OP_breg24";
4730 case DW_OP_breg25:
4731 return "DW_OP_breg25";
4732 case DW_OP_breg26:
4733 return "DW_OP_breg26";
4734 case DW_OP_breg27:
4735 return "DW_OP_breg27";
4736 case DW_OP_breg28:
4737 return "DW_OP_breg28";
4738 case DW_OP_breg29:
4739 return "DW_OP_breg29";
4740 case DW_OP_breg30:
4741 return "DW_OP_breg30";
4742 case DW_OP_breg31:
4743 return "DW_OP_breg31";
4744 case DW_OP_regx:
4745 return "DW_OP_regx";
4746 case DW_OP_fbreg:
4747 return "DW_OP_fbreg";
4748 case DW_OP_bregx:
4749 return "DW_OP_bregx";
4750 case DW_OP_piece:
4751 return "DW_OP_piece";
4752 case DW_OP_deref_size:
4753 return "DW_OP_deref_size";
4754 case DW_OP_xderef_size:
4755 return "DW_OP_xderef_size";
4756 case DW_OP_nop:
4757 return "DW_OP_nop";
4759 case DW_OP_push_object_address:
4760 return "DW_OP_push_object_address";
4761 case DW_OP_call2:
4762 return "DW_OP_call2";
4763 case DW_OP_call4:
4764 return "DW_OP_call4";
4765 case DW_OP_call_ref:
4766 return "DW_OP_call_ref";
4767 case DW_OP_implicit_value:
4768 return "DW_OP_implicit_value";
4769 case DW_OP_stack_value:
4770 return "DW_OP_stack_value";
4771 case DW_OP_form_tls_address:
4772 return "DW_OP_form_tls_address";
4773 case DW_OP_call_frame_cfa:
4774 return "DW_OP_call_frame_cfa";
4775 case DW_OP_bit_piece:
4776 return "DW_OP_bit_piece";
4778 case DW_OP_GNU_push_tls_address:
4779 return "DW_OP_GNU_push_tls_address";
4780 case DW_OP_GNU_uninit:
4781 return "DW_OP_GNU_uninit";
4782 case DW_OP_GNU_encoded_addr:
4783 return "DW_OP_GNU_encoded_addr";
4784 case DW_OP_GNU_implicit_pointer:
4785 return "DW_OP_GNU_implicit_pointer";
4786 case DW_OP_GNU_entry_value:
4787 return "DW_OP_GNU_entry_value";
4789 default:
4790 return "OP_<unknown>";
4794 /* Return a pointer to a newly allocated location description. Location
4795 descriptions are simple expression terms that can be strung
4796 together to form more complicated location (address) descriptions. */
4798 static inline dw_loc_descr_ref
4799 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4800 unsigned HOST_WIDE_INT oprnd2)
4802 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4804 descr->dw_loc_opc = op;
4805 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4806 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4807 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4808 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4810 return descr;
4813 /* Return a pointer to a newly allocated location description for
4814 REG and OFFSET. */
4816 static inline dw_loc_descr_ref
4817 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
4819 if (reg <= 31)
4820 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4821 offset, 0);
4822 else
4823 return new_loc_descr (DW_OP_bregx, reg, offset);
4826 /* Add a location description term to a location description expression. */
4828 static inline void
4829 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4831 dw_loc_descr_ref *d;
4833 /* Find the end of the chain. */
4834 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4837 *d = descr;
4840 /* Add a constant OFFSET to a location expression. */
4842 static void
4843 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4845 dw_loc_descr_ref loc;
4846 HOST_WIDE_INT *p;
4848 gcc_assert (*list_head != NULL);
4850 if (!offset)
4851 return;
4853 /* Find the end of the chain. */
4854 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4857 p = NULL;
4858 if (loc->dw_loc_opc == DW_OP_fbreg
4859 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4860 p = &loc->dw_loc_oprnd1.v.val_int;
4861 else if (loc->dw_loc_opc == DW_OP_bregx)
4862 p = &loc->dw_loc_oprnd2.v.val_int;
4864 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4865 offset. Don't optimize if an signed integer overflow would happen. */
4866 if (p != NULL
4867 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4868 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4869 *p += offset;
4871 else if (offset > 0)
4872 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4874 else
4876 loc->dw_loc_next = int_loc_descriptor (-offset);
4877 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4881 /* Add a constant OFFSET to a location list. */
4883 static void
4884 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4886 dw_loc_list_ref d;
4887 for (d = list_head; d != NULL; d = d->dw_loc_next)
4888 loc_descr_plus_const (&d->expr, offset);
4891 #define DWARF_REF_SIZE \
4892 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4894 static unsigned long size_of_locs (dw_loc_descr_ref);
4896 /* Return the size of a location descriptor. */
4898 static unsigned long
4899 size_of_loc_descr (dw_loc_descr_ref loc)
4901 unsigned long size = 1;
4903 switch (loc->dw_loc_opc)
4905 case DW_OP_addr:
4906 size += DWARF2_ADDR_SIZE;
4907 break;
4908 case DW_OP_const1u:
4909 case DW_OP_const1s:
4910 size += 1;
4911 break;
4912 case DW_OP_const2u:
4913 case DW_OP_const2s:
4914 size += 2;
4915 break;
4916 case DW_OP_const4u:
4917 case DW_OP_const4s:
4918 size += 4;
4919 break;
4920 case DW_OP_const8u:
4921 case DW_OP_const8s:
4922 size += 8;
4923 break;
4924 case DW_OP_constu:
4925 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4926 break;
4927 case DW_OP_consts:
4928 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4929 break;
4930 case DW_OP_pick:
4931 size += 1;
4932 break;
4933 case DW_OP_plus_uconst:
4934 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4935 break;
4936 case DW_OP_skip:
4937 case DW_OP_bra:
4938 size += 2;
4939 break;
4940 case DW_OP_breg0:
4941 case DW_OP_breg1:
4942 case DW_OP_breg2:
4943 case DW_OP_breg3:
4944 case DW_OP_breg4:
4945 case DW_OP_breg5:
4946 case DW_OP_breg6:
4947 case DW_OP_breg7:
4948 case DW_OP_breg8:
4949 case DW_OP_breg9:
4950 case DW_OP_breg10:
4951 case DW_OP_breg11:
4952 case DW_OP_breg12:
4953 case DW_OP_breg13:
4954 case DW_OP_breg14:
4955 case DW_OP_breg15:
4956 case DW_OP_breg16:
4957 case DW_OP_breg17:
4958 case DW_OP_breg18:
4959 case DW_OP_breg19:
4960 case DW_OP_breg20:
4961 case DW_OP_breg21:
4962 case DW_OP_breg22:
4963 case DW_OP_breg23:
4964 case DW_OP_breg24:
4965 case DW_OP_breg25:
4966 case DW_OP_breg26:
4967 case DW_OP_breg27:
4968 case DW_OP_breg28:
4969 case DW_OP_breg29:
4970 case DW_OP_breg30:
4971 case DW_OP_breg31:
4972 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4973 break;
4974 case DW_OP_regx:
4975 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4976 break;
4977 case DW_OP_fbreg:
4978 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4979 break;
4980 case DW_OP_bregx:
4981 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4982 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4983 break;
4984 case DW_OP_piece:
4985 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4986 break;
4987 case DW_OP_bit_piece:
4988 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4989 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4990 break;
4991 case DW_OP_deref_size:
4992 case DW_OP_xderef_size:
4993 size += 1;
4994 break;
4995 case DW_OP_call2:
4996 size += 2;
4997 break;
4998 case DW_OP_call4:
4999 size += 4;
5000 break;
5001 case DW_OP_call_ref:
5002 size += DWARF_REF_SIZE;
5003 break;
5004 case DW_OP_implicit_value:
5005 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5006 + loc->dw_loc_oprnd1.v.val_unsigned;
5007 break;
5008 case DW_OP_GNU_implicit_pointer:
5009 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5010 break;
5011 case DW_OP_GNU_entry_value:
5013 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
5014 size += size_of_uleb128 (op_size) + op_size;
5015 break;
5017 default:
5018 break;
5021 return size;
5024 /* Return the size of a series of location descriptors. */
5026 static unsigned long
5027 size_of_locs (dw_loc_descr_ref loc)
5029 dw_loc_descr_ref l;
5030 unsigned long size;
5032 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5033 field, to avoid writing to a PCH file. */
5034 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5036 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5037 break;
5038 size += size_of_loc_descr (l);
5040 if (! l)
5041 return size;
5043 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5045 l->dw_loc_addr = size;
5046 size += size_of_loc_descr (l);
5049 return size;
5052 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5053 static void get_ref_die_offset_label (char *, dw_die_ref);
5054 static void output_loc_sequence (dw_loc_descr_ref, int);
5056 /* Output location description stack opcode's operands (if any).
5057 The for_eh_or_skip parameter controls whether register numbers are
5058 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5059 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5060 info). This should be suppressed for the cases that have not been converted
5061 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
5063 static void
5064 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
5066 dw_val_ref val1 = &loc->dw_loc_oprnd1;
5067 dw_val_ref val2 = &loc->dw_loc_oprnd2;
5069 switch (loc->dw_loc_opc)
5071 #ifdef DWARF2_DEBUGGING_INFO
5072 case DW_OP_const2u:
5073 case DW_OP_const2s:
5074 dw2_asm_output_data (2, val1->v.val_int, NULL);
5075 break;
5076 case DW_OP_const4u:
5077 if (loc->dtprel)
5079 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5080 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5081 val1->v.val_addr);
5082 fputc ('\n', asm_out_file);
5083 break;
5085 /* FALLTHRU */
5086 case DW_OP_const4s:
5087 dw2_asm_output_data (4, val1->v.val_int, NULL);
5088 break;
5089 case DW_OP_const8u:
5090 if (loc->dtprel)
5092 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5093 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5094 val1->v.val_addr);
5095 fputc ('\n', asm_out_file);
5096 break;
5098 /* FALLTHRU */
5099 case DW_OP_const8s:
5100 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5101 dw2_asm_output_data (8, val1->v.val_int, NULL);
5102 break;
5103 case DW_OP_skip:
5104 case DW_OP_bra:
5106 int offset;
5108 gcc_assert (val1->val_class == dw_val_class_loc);
5109 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5111 dw2_asm_output_data (2, offset, NULL);
5113 break;
5114 case DW_OP_implicit_value:
5115 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5116 switch (val2->val_class)
5118 case dw_val_class_const:
5119 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5120 break;
5121 case dw_val_class_vec:
5123 unsigned int elt_size = val2->v.val_vec.elt_size;
5124 unsigned int len = val2->v.val_vec.length;
5125 unsigned int i;
5126 unsigned char *p;
5128 if (elt_size > sizeof (HOST_WIDE_INT))
5130 elt_size /= 2;
5131 len *= 2;
5133 for (i = 0, p = val2->v.val_vec.array;
5134 i < len;
5135 i++, p += elt_size)
5136 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5137 "fp or vector constant word %u", i);
5139 break;
5140 case dw_val_class_const_double:
5142 unsigned HOST_WIDE_INT first, second;
5144 if (WORDS_BIG_ENDIAN)
5146 first = val2->v.val_double.high;
5147 second = val2->v.val_double.low;
5149 else
5151 first = val2->v.val_double.low;
5152 second = val2->v.val_double.high;
5154 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5155 first, NULL);
5156 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5157 second, NULL);
5159 break;
5160 case dw_val_class_addr:
5161 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5162 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5163 break;
5164 default:
5165 gcc_unreachable ();
5167 break;
5168 #else
5169 case DW_OP_const2u:
5170 case DW_OP_const2s:
5171 case DW_OP_const4u:
5172 case DW_OP_const4s:
5173 case DW_OP_const8u:
5174 case DW_OP_const8s:
5175 case DW_OP_skip:
5176 case DW_OP_bra:
5177 case DW_OP_implicit_value:
5178 /* We currently don't make any attempt to make sure these are
5179 aligned properly like we do for the main unwind info, so
5180 don't support emitting things larger than a byte if we're
5181 only doing unwinding. */
5182 gcc_unreachable ();
5183 #endif
5184 case DW_OP_const1u:
5185 case DW_OP_const1s:
5186 dw2_asm_output_data (1, val1->v.val_int, NULL);
5187 break;
5188 case DW_OP_constu:
5189 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5190 break;
5191 case DW_OP_consts:
5192 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5193 break;
5194 case DW_OP_pick:
5195 dw2_asm_output_data (1, val1->v.val_int, NULL);
5196 break;
5197 case DW_OP_plus_uconst:
5198 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5199 break;
5200 case DW_OP_breg0:
5201 case DW_OP_breg1:
5202 case DW_OP_breg2:
5203 case DW_OP_breg3:
5204 case DW_OP_breg4:
5205 case DW_OP_breg5:
5206 case DW_OP_breg6:
5207 case DW_OP_breg7:
5208 case DW_OP_breg8:
5209 case DW_OP_breg9:
5210 case DW_OP_breg10:
5211 case DW_OP_breg11:
5212 case DW_OP_breg12:
5213 case DW_OP_breg13:
5214 case DW_OP_breg14:
5215 case DW_OP_breg15:
5216 case DW_OP_breg16:
5217 case DW_OP_breg17:
5218 case DW_OP_breg18:
5219 case DW_OP_breg19:
5220 case DW_OP_breg20:
5221 case DW_OP_breg21:
5222 case DW_OP_breg22:
5223 case DW_OP_breg23:
5224 case DW_OP_breg24:
5225 case DW_OP_breg25:
5226 case DW_OP_breg26:
5227 case DW_OP_breg27:
5228 case DW_OP_breg28:
5229 case DW_OP_breg29:
5230 case DW_OP_breg30:
5231 case DW_OP_breg31:
5232 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5233 break;
5234 case DW_OP_regx:
5236 unsigned r = val1->v.val_unsigned;
5237 if (for_eh_or_skip >= 0)
5238 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5239 gcc_assert (size_of_uleb128 (r)
5240 == size_of_uleb128 (val1->v.val_unsigned));
5241 dw2_asm_output_data_uleb128 (r, NULL);
5243 break;
5244 case DW_OP_fbreg:
5245 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5246 break;
5247 case DW_OP_bregx:
5249 unsigned r = val1->v.val_unsigned;
5250 if (for_eh_or_skip >= 0)
5251 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5252 gcc_assert (size_of_uleb128 (r)
5253 == size_of_uleb128 (val1->v.val_unsigned));
5254 dw2_asm_output_data_uleb128 (r, NULL);
5255 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5257 break;
5258 case DW_OP_piece:
5259 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5260 break;
5261 case DW_OP_bit_piece:
5262 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5263 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5264 break;
5265 case DW_OP_deref_size:
5266 case DW_OP_xderef_size:
5267 dw2_asm_output_data (1, val1->v.val_int, NULL);
5268 break;
5270 case DW_OP_addr:
5271 if (loc->dtprel)
5273 if (targetm.asm_out.output_dwarf_dtprel)
5275 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5276 DWARF2_ADDR_SIZE,
5277 val1->v.val_addr);
5278 fputc ('\n', asm_out_file);
5280 else
5281 gcc_unreachable ();
5283 else
5285 #ifdef DWARF2_DEBUGGING_INFO
5286 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5287 #else
5288 gcc_unreachable ();
5289 #endif
5291 break;
5293 case DW_OP_GNU_implicit_pointer:
5295 char label[MAX_ARTIFICIAL_LABEL_BYTES
5296 + HOST_BITS_PER_WIDE_INT / 2 + 2];
5297 gcc_assert (val1->val_class == dw_val_class_die_ref);
5298 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5299 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5300 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5302 break;
5304 case DW_OP_GNU_entry_value:
5305 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
5306 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
5307 break;
5309 default:
5310 /* Other codes have no operands. */
5311 break;
5315 /* Output a sequence of location operations.
5316 The for_eh_or_skip parameter controls whether register numbers are
5317 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5318 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5319 info). This should be suppressed for the cases that have not been converted
5320 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
5322 static void
5323 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
5325 for (; loc != NULL; loc = loc->dw_loc_next)
5327 enum dwarf_location_atom opc = loc->dw_loc_opc;
5328 /* Output the opcode. */
5329 if (for_eh_or_skip >= 0
5330 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5332 unsigned r = (opc - DW_OP_breg0);
5333 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5334 gcc_assert (r <= 31);
5335 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5337 else if (for_eh_or_skip >= 0
5338 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5340 unsigned r = (opc - DW_OP_reg0);
5341 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5342 gcc_assert (r <= 31);
5343 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5346 dw2_asm_output_data (1, opc,
5347 "%s", dwarf_stack_op_name (opc));
5349 /* Output the operand(s) (if any). */
5350 output_loc_operands (loc, for_eh_or_skip);
5354 /* Output location description stack opcode's operands (if any).
5355 The output is single bytes on a line, suitable for .cfi_escape. */
5357 static void
5358 output_loc_operands_raw (dw_loc_descr_ref loc)
5360 dw_val_ref val1 = &loc->dw_loc_oprnd1;
5361 dw_val_ref val2 = &loc->dw_loc_oprnd2;
5363 switch (loc->dw_loc_opc)
5365 case DW_OP_addr:
5366 case DW_OP_implicit_value:
5367 /* We cannot output addresses in .cfi_escape, only bytes. */
5368 gcc_unreachable ();
5370 case DW_OP_const1u:
5371 case DW_OP_const1s:
5372 case DW_OP_pick:
5373 case DW_OP_deref_size:
5374 case DW_OP_xderef_size:
5375 fputc (',', asm_out_file);
5376 dw2_asm_output_data_raw (1, val1->v.val_int);
5377 break;
5379 case DW_OP_const2u:
5380 case DW_OP_const2s:
5381 fputc (',', asm_out_file);
5382 dw2_asm_output_data_raw (2, val1->v.val_int);
5383 break;
5385 case DW_OP_const4u:
5386 case DW_OP_const4s:
5387 fputc (',', asm_out_file);
5388 dw2_asm_output_data_raw (4, val1->v.val_int);
5389 break;
5391 case DW_OP_const8u:
5392 case DW_OP_const8s:
5393 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5394 fputc (',', asm_out_file);
5395 dw2_asm_output_data_raw (8, val1->v.val_int);
5396 break;
5398 case DW_OP_skip:
5399 case DW_OP_bra:
5401 int offset;
5403 gcc_assert (val1->val_class == dw_val_class_loc);
5404 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5406 fputc (',', asm_out_file);
5407 dw2_asm_output_data_raw (2, offset);
5409 break;
5411 case DW_OP_regx:
5413 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5414 gcc_assert (size_of_uleb128 (r)
5415 == size_of_uleb128 (val1->v.val_unsigned));
5416 fputc (',', asm_out_file);
5417 dw2_asm_output_data_uleb128_raw (r);
5419 break;
5421 case DW_OP_constu:
5422 case DW_OP_plus_uconst:
5423 case DW_OP_piece:
5424 fputc (',', asm_out_file);
5425 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5426 break;
5428 case DW_OP_bit_piece:
5429 fputc (',', asm_out_file);
5430 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5431 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5432 break;
5434 case DW_OP_consts:
5435 case DW_OP_breg0:
5436 case DW_OP_breg1:
5437 case DW_OP_breg2:
5438 case DW_OP_breg3:
5439 case DW_OP_breg4:
5440 case DW_OP_breg5:
5441 case DW_OP_breg6:
5442 case DW_OP_breg7:
5443 case DW_OP_breg8:
5444 case DW_OP_breg9:
5445 case DW_OP_breg10:
5446 case DW_OP_breg11:
5447 case DW_OP_breg12:
5448 case DW_OP_breg13:
5449 case DW_OP_breg14:
5450 case DW_OP_breg15:
5451 case DW_OP_breg16:
5452 case DW_OP_breg17:
5453 case DW_OP_breg18:
5454 case DW_OP_breg19:
5455 case DW_OP_breg20:
5456 case DW_OP_breg21:
5457 case DW_OP_breg22:
5458 case DW_OP_breg23:
5459 case DW_OP_breg24:
5460 case DW_OP_breg25:
5461 case DW_OP_breg26:
5462 case DW_OP_breg27:
5463 case DW_OP_breg28:
5464 case DW_OP_breg29:
5465 case DW_OP_breg30:
5466 case DW_OP_breg31:
5467 case DW_OP_fbreg:
5468 fputc (',', asm_out_file);
5469 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5470 break;
5472 case DW_OP_bregx:
5474 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5475 gcc_assert (size_of_uleb128 (r)
5476 == size_of_uleb128 (val1->v.val_unsigned));
5477 fputc (',', asm_out_file);
5478 dw2_asm_output_data_uleb128_raw (r);
5479 fputc (',', asm_out_file);
5480 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5482 break;
5484 case DW_OP_GNU_implicit_pointer:
5485 case DW_OP_GNU_entry_value:
5486 gcc_unreachable ();
5487 break;
5489 default:
5490 /* Other codes have no operands. */
5491 break;
5495 static void
5496 output_loc_sequence_raw (dw_loc_descr_ref loc)
5498 while (1)
5500 enum dwarf_location_atom opc = loc->dw_loc_opc;
5501 /* Output the opcode. */
5502 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5504 unsigned r = (opc - DW_OP_breg0);
5505 r = DWARF2_FRAME_REG_OUT (r, 1);
5506 gcc_assert (r <= 31);
5507 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5509 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5511 unsigned r = (opc - DW_OP_reg0);
5512 r = DWARF2_FRAME_REG_OUT (r, 1);
5513 gcc_assert (r <= 31);
5514 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5516 /* Output the opcode. */
5517 fprintf (asm_out_file, "%#x", opc);
5518 output_loc_operands_raw (loc);
5520 if (!loc->dw_loc_next)
5521 break;
5522 loc = loc->dw_loc_next;
5524 fputc (',', asm_out_file);
5528 /* This routine will generate the correct assembly data for a location
5529 description based on a cfi entry with a complex address. */
5531 static void
5532 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
5534 dw_loc_descr_ref loc;
5535 unsigned long size;
5537 if (cfi->dw_cfi_opc == DW_CFA_expression)
5539 unsigned r =
5540 DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
5541 dw2_asm_output_data (1, r, NULL);
5542 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5544 else
5545 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5547 /* Output the size of the block. */
5548 size = size_of_locs (loc);
5549 dw2_asm_output_data_uleb128 (size, NULL);
5551 /* Now output the operations themselves. */
5552 output_loc_sequence (loc, for_eh);
5555 /* Similar, but used for .cfi_escape. */
5557 static void
5558 output_cfa_loc_raw (dw_cfi_ref cfi)
5560 dw_loc_descr_ref loc;
5561 unsigned long size;
5563 if (cfi->dw_cfi_opc == DW_CFA_expression)
5565 unsigned r =
5566 DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
5567 fprintf (asm_out_file, "%#x,", r);
5568 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5570 else
5571 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5573 /* Output the size of the block. */
5574 size = size_of_locs (loc);
5575 dw2_asm_output_data_uleb128_raw (size);
5576 fputc (',', asm_out_file);
5578 /* Now output the operations themselves. */
5579 output_loc_sequence_raw (loc);
5582 /* This function builds a dwarf location descriptor sequence from a
5583 dw_cfa_location, adding the given OFFSET to the result of the
5584 expression. */
5586 static struct dw_loc_descr_struct *
5587 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5589 struct dw_loc_descr_struct *head, *tmp;
5591 offset += cfa->offset;
5593 if (cfa->indirect)
5595 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5596 head->dw_loc_oprnd1.val_class = dw_val_class_const;
5597 tmp = new_loc_descr (DW_OP_deref, 0, 0);
5598 add_loc_descr (&head, tmp);
5599 if (offset != 0)
5601 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5602 add_loc_descr (&head, tmp);
5605 else
5606 head = new_reg_loc_descr (cfa->reg, offset);
5608 return head;
5611 /* This function builds a dwarf location descriptor sequence for
5612 the address at OFFSET from the CFA when stack is aligned to
5613 ALIGNMENT byte. */
5615 static struct dw_loc_descr_struct *
5616 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5618 struct dw_loc_descr_struct *head;
5619 unsigned int dwarf_fp
5620 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5622 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
5623 if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5625 head = new_reg_loc_descr (dwarf_fp, 0);
5626 add_loc_descr (&head, int_loc_descriptor (alignment));
5627 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5628 loc_descr_plus_const (&head, offset);
5630 else
5631 head = new_reg_loc_descr (dwarf_fp, offset);
5632 return head;
5635 /* This function fills in aa dw_cfa_location structure from a dwarf location
5636 descriptor sequence. */
5638 static void
5639 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5641 struct dw_loc_descr_struct *ptr;
5642 cfa->offset = 0;
5643 cfa->base_offset = 0;
5644 cfa->indirect = 0;
5645 cfa->reg = -1;
5647 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5649 enum dwarf_location_atom op = ptr->dw_loc_opc;
5651 switch (op)
5653 case DW_OP_reg0:
5654 case DW_OP_reg1:
5655 case DW_OP_reg2:
5656 case DW_OP_reg3:
5657 case DW_OP_reg4:
5658 case DW_OP_reg5:
5659 case DW_OP_reg6:
5660 case DW_OP_reg7:
5661 case DW_OP_reg8:
5662 case DW_OP_reg9:
5663 case DW_OP_reg10:
5664 case DW_OP_reg11:
5665 case DW_OP_reg12:
5666 case DW_OP_reg13:
5667 case DW_OP_reg14:
5668 case DW_OP_reg15:
5669 case DW_OP_reg16:
5670 case DW_OP_reg17:
5671 case DW_OP_reg18:
5672 case DW_OP_reg19:
5673 case DW_OP_reg20:
5674 case DW_OP_reg21:
5675 case DW_OP_reg22:
5676 case DW_OP_reg23:
5677 case DW_OP_reg24:
5678 case DW_OP_reg25:
5679 case DW_OP_reg26:
5680 case DW_OP_reg27:
5681 case DW_OP_reg28:
5682 case DW_OP_reg29:
5683 case DW_OP_reg30:
5684 case DW_OP_reg31:
5685 cfa->reg = op - DW_OP_reg0;
5686 break;
5687 case DW_OP_regx:
5688 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5689 break;
5690 case DW_OP_breg0:
5691 case DW_OP_breg1:
5692 case DW_OP_breg2:
5693 case DW_OP_breg3:
5694 case DW_OP_breg4:
5695 case DW_OP_breg5:
5696 case DW_OP_breg6:
5697 case DW_OP_breg7:
5698 case DW_OP_breg8:
5699 case DW_OP_breg9:
5700 case DW_OP_breg10:
5701 case DW_OP_breg11:
5702 case DW_OP_breg12:
5703 case DW_OP_breg13:
5704 case DW_OP_breg14:
5705 case DW_OP_breg15:
5706 case DW_OP_breg16:
5707 case DW_OP_breg17:
5708 case DW_OP_breg18:
5709 case DW_OP_breg19:
5710 case DW_OP_breg20:
5711 case DW_OP_breg21:
5712 case DW_OP_breg22:
5713 case DW_OP_breg23:
5714 case DW_OP_breg24:
5715 case DW_OP_breg25:
5716 case DW_OP_breg26:
5717 case DW_OP_breg27:
5718 case DW_OP_breg28:
5719 case DW_OP_breg29:
5720 case DW_OP_breg30:
5721 case DW_OP_breg31:
5722 cfa->reg = op - DW_OP_breg0;
5723 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5724 break;
5725 case DW_OP_bregx:
5726 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5727 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5728 break;
5729 case DW_OP_deref:
5730 cfa->indirect = 1;
5731 break;
5732 case DW_OP_plus_uconst:
5733 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5734 break;
5735 default:
5736 internal_error ("DW_LOC_OP %s not implemented",
5737 dwarf_stack_op_name (ptr->dw_loc_opc));
5742 /* And now, the support for symbolic debugging information. */
5744 /* .debug_str support. */
5745 static int output_indirect_string (void **, void *);
5747 static void dwarf2out_init (const char *);
5748 static void dwarf2out_finish (const char *);
5749 static void dwarf2out_assembly_start (void);
5750 static void dwarf2out_define (unsigned int, const char *);
5751 static void dwarf2out_undef (unsigned int, const char *);
5752 static void dwarf2out_start_source_file (unsigned, const char *);
5753 static void dwarf2out_end_source_file (unsigned);
5754 static void dwarf2out_function_decl (tree);
5755 static void dwarf2out_begin_block (unsigned, unsigned);
5756 static void dwarf2out_end_block (unsigned, unsigned);
5757 static bool dwarf2out_ignore_block (const_tree);
5758 static void dwarf2out_global_decl (tree);
5759 static void dwarf2out_type_decl (tree, int);
5760 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5761 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5762 dw_die_ref);
5763 static void dwarf2out_abstract_function (tree);
5764 static void dwarf2out_var_location (rtx);
5765 static void dwarf2out_begin_function (tree);
5766 static void dwarf2out_set_name (tree, tree);
5768 /* The debug hooks structure. */
5770 const struct gcc_debug_hooks dwarf2_debug_hooks =
5772 dwarf2out_init,
5773 dwarf2out_finish,
5774 dwarf2out_assembly_start,
5775 dwarf2out_define,
5776 dwarf2out_undef,
5777 dwarf2out_start_source_file,
5778 dwarf2out_end_source_file,
5779 dwarf2out_begin_block,
5780 dwarf2out_end_block,
5781 dwarf2out_ignore_block,
5782 dwarf2out_source_line,
5783 dwarf2out_begin_prologue,
5784 #if VMS_DEBUGGING_INFO
5785 dwarf2out_vms_end_prologue,
5786 dwarf2out_vms_begin_epilogue,
5787 #else
5788 debug_nothing_int_charstar,
5789 debug_nothing_int_charstar,
5790 #endif
5791 dwarf2out_end_epilogue,
5792 dwarf2out_begin_function,
5793 debug_nothing_int, /* end_function */
5794 dwarf2out_function_decl, /* function_decl */
5795 dwarf2out_global_decl,
5796 dwarf2out_type_decl, /* type_decl */
5797 dwarf2out_imported_module_or_decl,
5798 debug_nothing_tree, /* deferred_inline_function */
5799 /* The DWARF 2 backend tries to reduce debugging bloat by not
5800 emitting the abstract description of inline functions until
5801 something tries to reference them. */
5802 dwarf2out_abstract_function, /* outlining_inline_function */
5803 debug_nothing_rtx, /* label */
5804 debug_nothing_int, /* handle_pch */
5805 dwarf2out_var_location,
5806 dwarf2out_switch_text_section,
5807 dwarf2out_set_name,
5808 1, /* start_end_main_source_file */
5809 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
5812 /* NOTE: In the comments in this file, many references are made to
5813 "Debugging Information Entries". This term is abbreviated as `DIE'
5814 throughout the remainder of this file. */
5816 /* An internal representation of the DWARF output is built, and then
5817 walked to generate the DWARF debugging info. The walk of the internal
5818 representation is done after the entire program has been compiled.
5819 The types below are used to describe the internal representation. */
5821 /* Whether to put type DIEs into their own section .debug_types instead
5822 of making them part of the .debug_info section. Only supported for
5823 Dwarf V4 or higher and the user didn't disable them through
5824 -fno-debug-types-section. It is more efficient to put them in a
5825 separate comdat sections since the linker will then be able to
5826 remove duplicates. But not all tools support .debug_types sections
5827 yet. */
5829 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
5831 /* Various DIE's use offsets relative to the beginning of the
5832 .debug_info section to refer to each other. */
5834 typedef long int dw_offset;
5836 /* Define typedefs here to avoid circular dependencies. */
5838 typedef struct dw_attr_struct *dw_attr_ref;
5839 typedef struct dw_line_info_struct *dw_line_info_ref;
5840 typedef struct pubname_struct *pubname_ref;
5841 typedef struct dw_ranges_struct *dw_ranges_ref;
5842 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5843 typedef struct comdat_type_struct *comdat_type_node_ref;
5845 /* The entries in the line_info table more-or-less mirror the opcodes
5846 that are used in the real dwarf line table. Arrays of these entries
5847 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
5848 supported. */
5850 enum dw_line_info_opcode {
5851 /* Emit DW_LNE_set_address; the operand is the label index. */
5852 LI_set_address,
5854 /* Emit a row to the matrix with the given line. This may be done
5855 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
5856 special opcodes. */
5857 LI_set_line,
5859 /* Emit a DW_LNS_set_file. */
5860 LI_set_file,
5862 /* Emit a DW_LNS_set_column. */
5863 LI_set_column,
5865 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
5866 LI_negate_stmt,
5868 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
5869 LI_set_prologue_end,
5870 LI_set_epilogue_begin,
5872 /* Emit a DW_LNE_set_discriminator. */
5873 LI_set_discriminator
5876 typedef struct GTY(()) dw_line_info_struct {
5877 enum dw_line_info_opcode opcode;
5878 unsigned int val;
5879 } dw_line_info_entry;
5881 DEF_VEC_O(dw_line_info_entry);
5882 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
5884 typedef struct GTY(()) dw_line_info_table_struct {
5885 /* The label that marks the end of this section. */
5886 const char *end_label;
5888 /* The values for the last row of the matrix, as collected in the table.
5889 These are used to minimize the changes to the next row. */
5890 unsigned int file_num;
5891 unsigned int line_num;
5892 unsigned int column_num;
5893 int discrim_num;
5894 bool is_stmt;
5895 bool in_use;
5897 VEC(dw_line_info_entry, gc) *entries;
5898 } dw_line_info_table;
5900 typedef dw_line_info_table *dw_line_info_table_p;
5902 DEF_VEC_P(dw_line_info_table_p);
5903 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
5905 /* Each DIE attribute has a field specifying the attribute kind,
5906 a link to the next attribute in the chain, and an attribute value.
5907 Attributes are typically linked below the DIE they modify. */
5909 typedef struct GTY(()) dw_attr_struct {
5910 enum dwarf_attribute dw_attr;
5911 dw_val_node dw_attr_val;
5913 dw_attr_node;
5915 DEF_VEC_O(dw_attr_node);
5916 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5918 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
5919 The children of each node form a circular list linked by
5920 die_sib. die_child points to the node *before* the "first" child node. */
5922 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5923 union die_symbol_or_type_node
5925 char * GTY ((tag ("0"))) die_symbol;
5926 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5928 GTY ((desc ("use_debug_types"))) die_id;
5929 VEC(dw_attr_node,gc) * die_attr;
5930 dw_die_ref die_parent;
5931 dw_die_ref die_child;
5932 dw_die_ref die_sib;
5933 dw_die_ref die_definition; /* ref from a specification to its definition */
5934 dw_offset die_offset;
5935 unsigned long die_abbrev;
5936 int die_mark;
5937 /* Die is used and must not be pruned as unused. */
5938 int die_perennial_p;
5939 unsigned int decl_id;
5940 enum dwarf_tag die_tag;
5942 die_node;
5944 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
5945 #define FOR_EACH_CHILD(die, c, expr) do { \
5946 c = die->die_child; \
5947 if (c) do { \
5948 c = c->die_sib; \
5949 expr; \
5950 } while (c != die->die_child); \
5951 } while (0)
5953 /* The pubname structure */
5955 typedef struct GTY(()) pubname_struct {
5956 dw_die_ref die;
5957 const char *name;
5959 pubname_entry;
5961 DEF_VEC_O(pubname_entry);
5962 DEF_VEC_ALLOC_O(pubname_entry, gc);
5964 struct GTY(()) dw_ranges_struct {
5965 /* If this is positive, it's a block number, otherwise it's a
5966 bitwise-negated index into dw_ranges_by_label. */
5967 int num;
5970 /* A structure to hold a macinfo entry. */
5972 typedef struct GTY(()) macinfo_struct {
5973 unsigned HOST_WIDE_INT code;
5974 unsigned HOST_WIDE_INT lineno;
5975 const char *info;
5977 macinfo_entry;
5979 DEF_VEC_O(macinfo_entry);
5980 DEF_VEC_ALLOC_O(macinfo_entry, gc);
5982 struct GTY(()) dw_ranges_by_label_struct {
5983 const char *begin;
5984 const char *end;
5987 /* The comdat type node structure. */
5988 typedef struct GTY(()) comdat_type_struct
5990 dw_die_ref root_die;
5991 dw_die_ref type_die;
5992 char signature[DWARF_TYPE_SIGNATURE_SIZE];
5993 struct comdat_type_struct *next;
5995 comdat_type_node;
5997 /* The limbo die list structure. */
5998 typedef struct GTY(()) limbo_die_struct {
5999 dw_die_ref die;
6000 tree created_for;
6001 struct limbo_die_struct *next;
6003 limbo_die_node;
6005 typedef struct skeleton_chain_struct
6007 dw_die_ref old_die;
6008 dw_die_ref new_die;
6009 struct skeleton_chain_struct *parent;
6011 skeleton_chain_node;
6013 /* How to start an assembler comment. */
6014 #ifndef ASM_COMMENT_START
6015 #define ASM_COMMENT_START ";#"
6016 #endif
6018 /* Define a macro which returns nonzero for a TYPE_DECL which was
6019 implicitly generated for a tagged type.
6021 Note that unlike the gcc front end (which generates a NULL named
6022 TYPE_DECL node for each complete tagged type, each array type, and
6023 each function type node created) the g++ front end generates a
6024 _named_ TYPE_DECL node for each tagged type node created.
6025 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
6026 generate a DW_TAG_typedef DIE for them. */
6028 #define TYPE_DECL_IS_STUB(decl) \
6029 (DECL_NAME (decl) == NULL_TREE \
6030 || (DECL_ARTIFICIAL (decl) \
6031 && is_tagged_type (TREE_TYPE (decl)) \
6032 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
6033 /* This is necessary for stub decls that \
6034 appear in nested inline functions. */ \
6035 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
6036 && (decl_ultimate_origin (decl) \
6037 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
6039 /* Information concerning the compilation unit's programming
6040 language, and compiler version. */
6042 /* Fixed size portion of the DWARF compilation unit header. */
6043 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
6044 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
6046 /* Fixed size portion of the DWARF comdat type unit header. */
6047 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
6048 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
6049 + DWARF_OFFSET_SIZE)
6051 /* Fixed size portion of public names info. */
6052 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
6054 /* Fixed size portion of the address range info. */
6055 #define DWARF_ARANGES_HEADER_SIZE \
6056 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6057 DWARF2_ADDR_SIZE * 2) \
6058 - DWARF_INITIAL_LENGTH_SIZE)
6060 /* Size of padding portion in the address range info. It must be
6061 aligned to twice the pointer size. */
6062 #define DWARF_ARANGES_PAD_SIZE \
6063 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6064 DWARF2_ADDR_SIZE * 2) \
6065 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
6067 /* Use assembler line directives if available. */
6068 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
6069 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
6070 #define DWARF2_ASM_LINE_DEBUG_INFO 1
6071 #else
6072 #define DWARF2_ASM_LINE_DEBUG_INFO 0
6073 #endif
6074 #endif
6076 /* Minimum line offset in a special line info. opcode.
6077 This value was chosen to give a reasonable range of values. */
6078 #define DWARF_LINE_BASE -10
6080 /* First special line opcode - leave room for the standard opcodes. */
6081 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
6083 /* Range of line offsets in a special line info. opcode. */
6084 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
6086 /* Flag that indicates the initial value of the is_stmt_start flag.
6087 In the present implementation, we do not mark any lines as
6088 the beginning of a source statement, because that information
6089 is not made available by the GCC front-end. */
6090 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
6092 /* Maximum number of operations per instruction bundle. */
6093 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
6094 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
6095 #endif
6097 /* This location is used by calc_die_sizes() to keep track
6098 the offset of each DIE within the .debug_info section. */
6099 static unsigned long next_die_offset;
6101 /* Record the root of the DIE's built for the current compilation unit. */
6102 static GTY(()) dw_die_ref single_comp_unit_die;
6104 /* A list of type DIEs that have been separated into comdat sections. */
6105 static GTY(()) comdat_type_node *comdat_type_list;
6107 /* A list of DIEs with a NULL parent waiting to be relocated. */
6108 static GTY(()) limbo_die_node *limbo_die_list;
6110 /* A list of DIEs for which we may have to generate
6111 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
6112 static GTY(()) limbo_die_node *deferred_asm_name;
6114 /* Filenames referenced by this compilation unit. */
6115 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
6117 /* A hash table of references to DIE's that describe declarations.
6118 The key is a DECL_UID() which is a unique number identifying each decl. */
6119 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
6121 /* A hash table of references to DIE's that describe COMMON blocks.
6122 The key is DECL_UID() ^ die_parent. */
6123 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6125 typedef struct GTY(()) die_arg_entry_struct {
6126 dw_die_ref die;
6127 tree arg;
6128 } die_arg_entry;
6130 DEF_VEC_O(die_arg_entry);
6131 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6133 /* Node of the variable location list. */
6134 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6135 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6136 EXPR_LIST chain. For small bitsizes, bitsize is encoded
6137 in mode of the EXPR_LIST node and first EXPR_LIST operand
6138 is either NOTE_INSN_VAR_LOCATION for a piece with a known
6139 location or NULL for padding. For larger bitsizes,
6140 mode is 0 and first operand is a CONCAT with bitsize
6141 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6142 NULL as second operand. */
6143 rtx GTY (()) loc;
6144 const char * GTY (()) label;
6145 struct var_loc_node * GTY (()) next;
6148 /* Variable location list. */
6149 struct GTY (()) var_loc_list_def {
6150 struct var_loc_node * GTY (()) first;
6152 /* Pointer to the last but one or last element of the
6153 chained list. If the list is empty, both first and
6154 last are NULL, if the list contains just one node
6155 or the last node certainly is not redundant, it points
6156 to the last node, otherwise points to the last but one.
6157 Do not mark it for GC because it is marked through the chain. */
6158 struct var_loc_node * GTY ((skip ("%h"))) last;
6160 /* Pointer to the last element before section switch,
6161 if NULL, either sections weren't switched or first
6162 is after section switch. */
6163 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
6165 /* DECL_UID of the variable decl. */
6166 unsigned int decl_id;
6168 typedef struct var_loc_list_def var_loc_list;
6170 /* Call argument location list. */
6171 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
6172 rtx GTY (()) call_arg_loc_note;
6173 const char * GTY (()) label;
6174 tree GTY (()) block;
6175 bool tail_call_p;
6176 rtx GTY (()) symbol_ref;
6177 struct call_arg_loc_node * GTY (()) next;
6181 /* Table of decl location linked lists. */
6182 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6184 /* Head and tail of call_arg_loc chain. */
6185 static GTY (()) struct call_arg_loc_node *call_arg_locations;
6186 static struct call_arg_loc_node *call_arg_loc_last;
6188 /* Number of call sites in the current function. */
6189 static int call_site_count = -1;
6190 /* Number of tail call sites in the current function. */
6191 static int tail_call_site_count = -1;
6193 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
6194 DIEs. */
6195 static VEC (dw_die_ref, heap) *block_map;
6197 /* A cached location list. */
6198 struct GTY (()) cached_dw_loc_list_def {
6199 /* The DECL_UID of the decl that this entry describes. */
6200 unsigned int decl_id;
6202 /* The cached location list. */
6203 dw_loc_list_ref loc_list;
6205 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
6207 /* Table of cached location lists. */
6208 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
6210 /* A pointer to the base of a list of references to DIE's that
6211 are uniquely identified by their tag, presence/absence of
6212 children DIE's, and list of attribute/value pairs. */
6213 static GTY((length ("abbrev_die_table_allocated")))
6214 dw_die_ref *abbrev_die_table;
6216 /* Number of elements currently allocated for abbrev_die_table. */
6217 static GTY(()) unsigned abbrev_die_table_allocated;
6219 /* Number of elements in type_die_table currently in use. */
6220 static GTY(()) unsigned abbrev_die_table_in_use;
6222 /* Size (in elements) of increments by which we may expand the
6223 abbrev_die_table. */
6224 #define ABBREV_DIE_TABLE_INCREMENT 256
6226 /* A global counter for generating labels for line number data. */
6227 static unsigned int line_info_label_num;
6229 /* The current table to which we should emit line number information
6230 for the current function. This will be set up at the beginning of
6231 assembly for the function. */
6232 static dw_line_info_table *cur_line_info_table;
6234 /* The two default tables of line number info. */
6235 static GTY(()) dw_line_info_table *text_section_line_info;
6236 static GTY(()) dw_line_info_table *cold_text_section_line_info;
6238 /* The set of all non-default tables of line number info. */
6239 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
6241 /* A flag to tell pubnames/types export if there is an info section to
6242 refer to. */
6243 static bool info_section_emitted;
6245 /* A pointer to the base of a table that contains a list of publicly
6246 accessible names. */
6247 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
6249 /* A pointer to the base of a table that contains a list of publicly
6250 accessible types. */
6251 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6253 /* A pointer to the base of a table that contains a list of macro
6254 defines/undefines (and file start/end markers). */
6255 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6257 /* Array of dies for which we should generate .debug_ranges info. */
6258 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6260 /* Number of elements currently allocated for ranges_table. */
6261 static GTY(()) unsigned ranges_table_allocated;
6263 /* Number of elements in ranges_table currently in use. */
6264 static GTY(()) unsigned ranges_table_in_use;
6266 /* Array of pairs of labels referenced in ranges_table. */
6267 static GTY ((length ("ranges_by_label_allocated")))
6268 dw_ranges_by_label_ref ranges_by_label;
6270 /* Number of elements currently allocated for ranges_by_label. */
6271 static GTY(()) unsigned ranges_by_label_allocated;
6273 /* Number of elements in ranges_by_label currently in use. */
6274 static GTY(()) unsigned ranges_by_label_in_use;
6276 /* Size (in elements) of increments by which we may expand the
6277 ranges_table. */
6278 #define RANGES_TABLE_INCREMENT 64
6280 /* Whether we have location lists that need outputting */
6281 static GTY(()) bool have_location_lists;
6283 /* Unique label counter. */
6284 static GTY(()) unsigned int loclabel_num;
6286 /* Unique label counter for point-of-call tables. */
6287 static GTY(()) unsigned int poc_label_num;
6289 /* Record whether the function being analyzed contains inlined functions. */
6290 static int current_function_has_inlines;
6292 /* The last file entry emitted by maybe_emit_file(). */
6293 static GTY(()) struct dwarf_file_data * last_emitted_file;
6295 /* Number of internal labels generated by gen_internal_sym(). */
6296 static GTY(()) int label_num;
6298 /* Cached result of previous call to lookup_filename. */
6299 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6301 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6303 /* Instances of generic types for which we need to generate debug
6304 info that describe their generic parameters and arguments. That
6305 generation needs to happen once all types are properly laid out so
6306 we do it at the end of compilation. */
6307 static GTY(()) VEC(tree,gc) *generic_type_instances;
6309 /* Offset from the "steady-state frame pointer" to the frame base,
6310 within the current function. */
6311 static HOST_WIDE_INT frame_pointer_fb_offset;
6313 /* Forward declarations for functions defined in this file. */
6315 static int is_pseudo_reg (const_rtx);
6316 static tree type_main_variant (tree);
6317 static int is_tagged_type (const_tree);
6318 static const char *dwarf_tag_name (unsigned);
6319 static const char *dwarf_attr_name (unsigned);
6320 static const char *dwarf_form_name (unsigned);
6321 static tree decl_ultimate_origin (const_tree);
6322 static tree decl_class_context (tree);
6323 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6324 static inline enum dw_val_class AT_class (dw_attr_ref);
6325 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6326 static inline unsigned AT_flag (dw_attr_ref);
6327 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6328 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6329 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6330 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6331 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6332 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6333 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6334 unsigned int, unsigned char *);
6335 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6336 static hashval_t debug_str_do_hash (const void *);
6337 static int debug_str_eq (const void *, const void *);
6338 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6339 static inline const char *AT_string (dw_attr_ref);
6340 static enum dwarf_form AT_string_form (dw_attr_ref);
6341 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6342 static void add_AT_specification (dw_die_ref, dw_die_ref);
6343 static inline dw_die_ref AT_ref (dw_attr_ref);
6344 static inline int AT_ref_external (dw_attr_ref);
6345 static inline void set_AT_ref_external (dw_attr_ref, int);
6346 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6347 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6348 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6349 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6350 dw_loc_list_ref);
6351 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6352 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6353 static inline rtx AT_addr (dw_attr_ref);
6354 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6355 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6356 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6357 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6358 unsigned HOST_WIDE_INT);
6359 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6360 unsigned long);
6361 static inline const char *AT_lbl (dw_attr_ref);
6362 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6363 static const char *get_AT_low_pc (dw_die_ref);
6364 static const char *get_AT_hi_pc (dw_die_ref);
6365 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6366 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6367 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6368 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6369 static bool is_cxx (void);
6370 static bool is_fortran (void);
6371 static bool is_ada (void);
6372 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6373 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6374 static void add_child_die (dw_die_ref, dw_die_ref);
6375 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6376 static dw_die_ref lookup_type_die (tree);
6377 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
6378 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6379 static void equate_type_number_to_die (tree, dw_die_ref);
6380 static hashval_t decl_die_table_hash (const void *);
6381 static int decl_die_table_eq (const void *, const void *);
6382 static dw_die_ref lookup_decl_die (tree);
6383 static hashval_t common_block_die_table_hash (const void *);
6384 static int common_block_die_table_eq (const void *, const void *);
6385 static hashval_t decl_loc_table_hash (const void *);
6386 static int decl_loc_table_eq (const void *, const void *);
6387 static var_loc_list *lookup_decl_loc (const_tree);
6388 static void equate_decl_number_to_die (tree, dw_die_ref);
6389 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6390 static void print_spaces (FILE *);
6391 static void print_die (dw_die_ref, FILE *);
6392 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6393 static dw_die_ref pop_compile_unit (dw_die_ref);
6394 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6395 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6396 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6397 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6398 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6399 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6400 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6401 struct md5_ctx *, int *);
6402 struct checksum_attributes;
6403 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6404 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6405 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6406 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6407 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6408 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6409 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6410 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6411 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6412 static void compute_section_prefix (dw_die_ref);
6413 static int is_type_die (dw_die_ref);
6414 static int is_comdat_die (dw_die_ref);
6415 static int is_symbol_die (dw_die_ref);
6416 static void assign_symbol_names (dw_die_ref);
6417 static void break_out_includes (dw_die_ref);
6418 static int is_declaration_die (dw_die_ref);
6419 static int should_move_die_to_comdat (dw_die_ref);
6420 static dw_die_ref clone_as_declaration (dw_die_ref);
6421 static dw_die_ref clone_die (dw_die_ref);
6422 static dw_die_ref clone_tree (dw_die_ref);
6423 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6424 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6425 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6426 static dw_die_ref generate_skeleton (dw_die_ref);
6427 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6428 dw_die_ref);
6429 static void break_out_comdat_types (dw_die_ref);
6430 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6431 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6432 static void copy_decls_for_unworthy_types (dw_die_ref);
6434 static hashval_t htab_cu_hash (const void *);
6435 static int htab_cu_eq (const void *, const void *);
6436 static void htab_cu_del (void *);
6437 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6438 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6439 static void add_sibling_attributes (dw_die_ref);
6440 static void build_abbrev_table (dw_die_ref);
6441 static void output_location_lists (dw_die_ref);
6442 static int constant_size (unsigned HOST_WIDE_INT);
6443 static unsigned long size_of_die (dw_die_ref);
6444 static void calc_die_sizes (dw_die_ref);
6445 static void mark_dies (dw_die_ref);
6446 static void unmark_dies (dw_die_ref);
6447 static void unmark_all_dies (dw_die_ref);
6448 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6449 static unsigned long size_of_aranges (void);
6450 static enum dwarf_form value_format (dw_attr_ref);
6451 static void output_value_format (dw_attr_ref);
6452 static void output_abbrev_section (void);
6453 static void output_die_symbol (dw_die_ref);
6454 static void output_die (dw_die_ref);
6455 static void output_compilation_unit_header (void);
6456 static void output_comp_unit (dw_die_ref, int);
6457 static void output_comdat_type_unit (comdat_type_node *);
6458 static const char *dwarf2_name (tree, int);
6459 static void add_pubname (tree, dw_die_ref);
6460 static void add_pubname_string (const char *, dw_die_ref);
6461 static void add_pubtype (tree, dw_die_ref);
6462 static void output_pubnames (VEC (pubname_entry,gc) *);
6463 static void output_aranges (unsigned long);
6464 static unsigned int add_ranges_num (int);
6465 static unsigned int add_ranges (const_tree);
6466 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6467 bool *);
6468 static void output_ranges (void);
6469 static dw_line_info_table *new_line_info_table (void);
6470 static void output_line_info (void);
6471 static void output_file_names (void);
6472 static dw_die_ref base_type_die (tree);
6473 static int is_base_type (tree);
6474 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6475 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6476 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6477 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6478 static int type_is_enum (const_tree);
6479 static unsigned int dbx_reg_number (const_rtx);
6480 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6481 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6482 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6483 enum var_init_status);
6484 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6485 enum var_init_status);
6486 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6487 enum var_init_status);
6488 static int is_based_loc (const_rtx);
6489 static int resolve_one_addr (rtx *, void *);
6490 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6491 enum var_init_status);
6492 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6493 enum var_init_status);
6494 static dw_loc_list_ref loc_list_from_tree (tree, int);
6495 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6496 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6497 static tree field_type (const_tree);
6498 static unsigned int simple_type_align_in_bits (const_tree);
6499 static unsigned int simple_decl_align_in_bits (const_tree);
6500 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6501 static HOST_WIDE_INT field_byte_offset (const_tree);
6502 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6503 dw_loc_list_ref);
6504 static void add_data_member_location_attribute (dw_die_ref, tree);
6505 static bool add_const_value_attribute (dw_die_ref, rtx);
6506 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6507 static void insert_double (double_int, unsigned char *);
6508 static void insert_float (const_rtx, unsigned char *);
6509 static rtx rtl_for_decl_location (tree);
6510 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
6511 enum dwarf_attribute);
6512 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6513 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6514 static void add_name_attribute (dw_die_ref, const char *);
6515 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
6516 static void add_comp_dir_attribute (dw_die_ref);
6517 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6518 static void add_subscript_info (dw_die_ref, tree, bool);
6519 static void add_byte_size_attribute (dw_die_ref, tree);
6520 static void add_bit_offset_attribute (dw_die_ref, tree);
6521 static void add_bit_size_attribute (dw_die_ref, tree);
6522 static void add_prototyped_attribute (dw_die_ref, tree);
6523 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6524 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6525 static void add_src_coords_attributes (dw_die_ref, tree);
6526 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6527 static void push_decl_scope (tree);
6528 static void pop_decl_scope (void);
6529 static dw_die_ref scope_die_for (tree, dw_die_ref);
6530 static inline int local_scope_p (dw_die_ref);
6531 static inline int class_scope_p (dw_die_ref);
6532 static inline int class_or_namespace_scope_p (dw_die_ref);
6533 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6534 static void add_calling_convention_attribute (dw_die_ref, tree);
6535 static const char *type_tag (const_tree);
6536 static tree member_declared_type (const_tree);
6537 #if 0
6538 static const char *decl_start_label (tree);
6539 #endif
6540 static void gen_array_type_die (tree, dw_die_ref);
6541 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6542 #if 0
6543 static void gen_entry_point_die (tree, dw_die_ref);
6544 #endif
6545 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6546 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6547 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
6548 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6549 static void gen_formal_types_die (tree, dw_die_ref);
6550 static void gen_subprogram_die (tree, dw_die_ref);
6551 static void gen_variable_die (tree, tree, dw_die_ref);
6552 static void gen_const_die (tree, dw_die_ref);
6553 static void gen_label_die (tree, dw_die_ref);
6554 static void gen_lexical_block_die (tree, dw_die_ref, int);
6555 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6556 static void gen_field_die (tree, dw_die_ref);
6557 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6558 static dw_die_ref gen_compile_unit_die (const char *);
6559 static void gen_inheritance_die (tree, tree, dw_die_ref);
6560 static void gen_member_die (tree, dw_die_ref);
6561 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6562 enum debug_info_usage);
6563 static void gen_subroutine_type_die (tree, dw_die_ref);
6564 static void gen_typedef_die (tree, dw_die_ref);
6565 static void gen_type_die (tree, dw_die_ref);
6566 static void gen_block_die (tree, dw_die_ref, int);
6567 static void decls_for_scope (tree, dw_die_ref, int);
6568 static inline int is_redundant_typedef (const_tree);
6569 static bool is_naming_typedef_decl (const_tree);
6570 static inline dw_die_ref get_context_die (tree);
6571 static void gen_namespace_die (tree, dw_die_ref);
6572 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6573 static dw_die_ref force_decl_die (tree);
6574 static dw_die_ref force_type_die (tree);
6575 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6576 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6577 static struct dwarf_file_data * lookup_filename (const char *);
6578 static void retry_incomplete_types (void);
6579 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6580 static void gen_generic_params_dies (tree);
6581 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6582 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6583 static void splice_child_die (dw_die_ref, dw_die_ref);
6584 static int file_info_cmp (const void *, const void *);
6585 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6586 const char *, const char *);
6587 static void output_loc_list (dw_loc_list_ref);
6588 static char *gen_internal_sym (const char *);
6590 static void prune_unmark_dies (dw_die_ref);
6591 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6592 static void prune_unused_types_mark (dw_die_ref, int);
6593 static void prune_unused_types_walk (dw_die_ref);
6594 static void prune_unused_types_walk_attribs (dw_die_ref);
6595 static void prune_unused_types_prune (dw_die_ref);
6596 static void prune_unused_types (void);
6597 static int maybe_emit_file (struct dwarf_file_data *fd);
6598 static inline const char *AT_vms_delta1 (dw_attr_ref);
6599 static inline const char *AT_vms_delta2 (dw_attr_ref);
6600 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6601 const char *, const char *);
6602 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6603 static void gen_remaining_tmpl_value_param_die_attribute (void);
6604 static bool generic_type_p (tree);
6605 static void schedule_generic_params_dies_gen (tree t);
6606 static void gen_scheduled_generic_parms_dies (void);
6608 /* Section names used to hold DWARF debugging information. */
6609 #ifndef DEBUG_INFO_SECTION
6610 #define DEBUG_INFO_SECTION ".debug_info"
6611 #endif
6612 #ifndef DEBUG_ABBREV_SECTION
6613 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
6614 #endif
6615 #ifndef DEBUG_ARANGES_SECTION
6616 #define DEBUG_ARANGES_SECTION ".debug_aranges"
6617 #endif
6618 #ifndef DEBUG_MACINFO_SECTION
6619 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
6620 #endif
6621 #ifndef DEBUG_LINE_SECTION
6622 #define DEBUG_LINE_SECTION ".debug_line"
6623 #endif
6624 #ifndef DEBUG_LOC_SECTION
6625 #define DEBUG_LOC_SECTION ".debug_loc"
6626 #endif
6627 #ifndef DEBUG_PUBNAMES_SECTION
6628 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
6629 #endif
6630 #ifndef DEBUG_PUBTYPES_SECTION
6631 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
6632 #endif
6633 #ifndef DEBUG_STR_SECTION
6634 #define DEBUG_STR_SECTION ".debug_str"
6635 #endif
6636 #ifndef DEBUG_RANGES_SECTION
6637 #define DEBUG_RANGES_SECTION ".debug_ranges"
6638 #endif
6640 /* Standard ELF section names for compiled code and data. */
6641 #ifndef TEXT_SECTION_NAME
6642 #define TEXT_SECTION_NAME ".text"
6643 #endif
6645 /* Section flags for .debug_str section. */
6646 #define DEBUG_STR_SECTION_FLAGS \
6647 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
6648 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
6649 : SECTION_DEBUG)
6651 /* Labels we insert at beginning sections we can reference instead of
6652 the section names themselves. */
6654 #ifndef TEXT_SECTION_LABEL
6655 #define TEXT_SECTION_LABEL "Ltext"
6656 #endif
6657 #ifndef COLD_TEXT_SECTION_LABEL
6658 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
6659 #endif
6660 #ifndef DEBUG_LINE_SECTION_LABEL
6661 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
6662 #endif
6663 #ifndef DEBUG_INFO_SECTION_LABEL
6664 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
6665 #endif
6666 #ifndef DEBUG_ABBREV_SECTION_LABEL
6667 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
6668 #endif
6669 #ifndef DEBUG_LOC_SECTION_LABEL
6670 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
6671 #endif
6672 #ifndef DEBUG_RANGES_SECTION_LABEL
6673 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
6674 #endif
6675 #ifndef DEBUG_MACINFO_SECTION_LABEL
6676 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
6677 #endif
6680 /* Definitions of defaults for formats and names of various special
6681 (artificial) labels which may be generated within this file (when the -g
6682 options is used and DWARF2_DEBUGGING_INFO is in effect.
6683 If necessary, these may be overridden from within the tm.h file, but
6684 typically, overriding these defaults is unnecessary. */
6686 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6687 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6688 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6689 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6690 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6691 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6692 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6693 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6694 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6695 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6697 #ifndef TEXT_END_LABEL
6698 #define TEXT_END_LABEL "Letext"
6699 #endif
6700 #ifndef COLD_END_LABEL
6701 #define COLD_END_LABEL "Letext_cold"
6702 #endif
6703 #ifndef BLOCK_BEGIN_LABEL
6704 #define BLOCK_BEGIN_LABEL "LBB"
6705 #endif
6706 #ifndef BLOCK_END_LABEL
6707 #define BLOCK_END_LABEL "LBE"
6708 #endif
6709 #ifndef LINE_CODE_LABEL
6710 #define LINE_CODE_LABEL "LM"
6711 #endif
6714 /* Return the root of the DIE's built for the current compilation unit. */
6715 static dw_die_ref
6716 comp_unit_die (void)
6718 if (!single_comp_unit_die)
6719 single_comp_unit_die = gen_compile_unit_die (NULL);
6720 return single_comp_unit_die;
6723 /* We allow a language front-end to designate a function that is to be
6724 called to "demangle" any name before it is put into a DIE. */
6726 static const char *(*demangle_name_func) (const char *);
6728 void
6729 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6731 demangle_name_func = func;
6734 /* Test if rtl node points to a pseudo register. */
6736 static inline int
6737 is_pseudo_reg (const_rtx rtl)
6739 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6740 || (GET_CODE (rtl) == SUBREG
6741 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6744 /* Return a reference to a type, with its const and volatile qualifiers
6745 removed. */
6747 static inline tree
6748 type_main_variant (tree type)
6750 type = TYPE_MAIN_VARIANT (type);
6752 /* ??? There really should be only one main variant among any group of
6753 variants of a given type (and all of the MAIN_VARIANT values for all
6754 members of the group should point to that one type) but sometimes the C
6755 front-end messes this up for array types, so we work around that bug
6756 here. */
6757 if (TREE_CODE (type) == ARRAY_TYPE)
6758 while (type != TYPE_MAIN_VARIANT (type))
6759 type = TYPE_MAIN_VARIANT (type);
6761 return type;
6764 /* Return nonzero if the given type node represents a tagged type. */
6766 static inline int
6767 is_tagged_type (const_tree type)
6769 enum tree_code code = TREE_CODE (type);
6771 return (code == RECORD_TYPE || code == UNION_TYPE
6772 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6775 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
6777 static void
6778 get_ref_die_offset_label (char *label, dw_die_ref ref)
6780 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6783 /* Convert a DIE tag into its string name. */
6785 static const char *
6786 dwarf_tag_name (unsigned int tag)
6788 switch (tag)
6790 case DW_TAG_padding:
6791 return "DW_TAG_padding";
6792 case DW_TAG_array_type:
6793 return "DW_TAG_array_type";
6794 case DW_TAG_class_type:
6795 return "DW_TAG_class_type";
6796 case DW_TAG_entry_point:
6797 return "DW_TAG_entry_point";
6798 case DW_TAG_enumeration_type:
6799 return "DW_TAG_enumeration_type";
6800 case DW_TAG_formal_parameter:
6801 return "DW_TAG_formal_parameter";
6802 case DW_TAG_imported_declaration:
6803 return "DW_TAG_imported_declaration";
6804 case DW_TAG_label:
6805 return "DW_TAG_label";
6806 case DW_TAG_lexical_block:
6807 return "DW_TAG_lexical_block";
6808 case DW_TAG_member:
6809 return "DW_TAG_member";
6810 case DW_TAG_pointer_type:
6811 return "DW_TAG_pointer_type";
6812 case DW_TAG_reference_type:
6813 return "DW_TAG_reference_type";
6814 case DW_TAG_compile_unit:
6815 return "DW_TAG_compile_unit";
6816 case DW_TAG_string_type:
6817 return "DW_TAG_string_type";
6818 case DW_TAG_structure_type:
6819 return "DW_TAG_structure_type";
6820 case DW_TAG_subroutine_type:
6821 return "DW_TAG_subroutine_type";
6822 case DW_TAG_typedef:
6823 return "DW_TAG_typedef";
6824 case DW_TAG_union_type:
6825 return "DW_TAG_union_type";
6826 case DW_TAG_unspecified_parameters:
6827 return "DW_TAG_unspecified_parameters";
6828 case DW_TAG_variant:
6829 return "DW_TAG_variant";
6830 case DW_TAG_common_block:
6831 return "DW_TAG_common_block";
6832 case DW_TAG_common_inclusion:
6833 return "DW_TAG_common_inclusion";
6834 case DW_TAG_inheritance:
6835 return "DW_TAG_inheritance";
6836 case DW_TAG_inlined_subroutine:
6837 return "DW_TAG_inlined_subroutine";
6838 case DW_TAG_module:
6839 return "DW_TAG_module";
6840 case DW_TAG_ptr_to_member_type:
6841 return "DW_TAG_ptr_to_member_type";
6842 case DW_TAG_set_type:
6843 return "DW_TAG_set_type";
6844 case DW_TAG_subrange_type:
6845 return "DW_TAG_subrange_type";
6846 case DW_TAG_with_stmt:
6847 return "DW_TAG_with_stmt";
6848 case DW_TAG_access_declaration:
6849 return "DW_TAG_access_declaration";
6850 case DW_TAG_base_type:
6851 return "DW_TAG_base_type";
6852 case DW_TAG_catch_block:
6853 return "DW_TAG_catch_block";
6854 case DW_TAG_const_type:
6855 return "DW_TAG_const_type";
6856 case DW_TAG_constant:
6857 return "DW_TAG_constant";
6858 case DW_TAG_enumerator:
6859 return "DW_TAG_enumerator";
6860 case DW_TAG_file_type:
6861 return "DW_TAG_file_type";
6862 case DW_TAG_friend:
6863 return "DW_TAG_friend";
6864 case DW_TAG_namelist:
6865 return "DW_TAG_namelist";
6866 case DW_TAG_namelist_item:
6867 return "DW_TAG_namelist_item";
6868 case DW_TAG_packed_type:
6869 return "DW_TAG_packed_type";
6870 case DW_TAG_subprogram:
6871 return "DW_TAG_subprogram";
6872 case DW_TAG_template_type_param:
6873 return "DW_TAG_template_type_param";
6874 case DW_TAG_template_value_param:
6875 return "DW_TAG_template_value_param";
6876 case DW_TAG_thrown_type:
6877 return "DW_TAG_thrown_type";
6878 case DW_TAG_try_block:
6879 return "DW_TAG_try_block";
6880 case DW_TAG_variant_part:
6881 return "DW_TAG_variant_part";
6882 case DW_TAG_variable:
6883 return "DW_TAG_variable";
6884 case DW_TAG_volatile_type:
6885 return "DW_TAG_volatile_type";
6886 case DW_TAG_dwarf_procedure:
6887 return "DW_TAG_dwarf_procedure";
6888 case DW_TAG_restrict_type:
6889 return "DW_TAG_restrict_type";
6890 case DW_TAG_interface_type:
6891 return "DW_TAG_interface_type";
6892 case DW_TAG_namespace:
6893 return "DW_TAG_namespace";
6894 case DW_TAG_imported_module:
6895 return "DW_TAG_imported_module";
6896 case DW_TAG_unspecified_type:
6897 return "DW_TAG_unspecified_type";
6898 case DW_TAG_partial_unit:
6899 return "DW_TAG_partial_unit";
6900 case DW_TAG_imported_unit:
6901 return "DW_TAG_imported_unit";
6902 case DW_TAG_condition:
6903 return "DW_TAG_condition";
6904 case DW_TAG_shared_type:
6905 return "DW_TAG_shared_type";
6906 case DW_TAG_type_unit:
6907 return "DW_TAG_type_unit";
6908 case DW_TAG_rvalue_reference_type:
6909 return "DW_TAG_rvalue_reference_type";
6910 case DW_TAG_template_alias:
6911 return "DW_TAG_template_alias";
6912 case DW_TAG_GNU_template_parameter_pack:
6913 return "DW_TAG_GNU_template_parameter_pack";
6914 case DW_TAG_GNU_formal_parameter_pack:
6915 return "DW_TAG_GNU_formal_parameter_pack";
6916 case DW_TAG_MIPS_loop:
6917 return "DW_TAG_MIPS_loop";
6918 case DW_TAG_format_label:
6919 return "DW_TAG_format_label";
6920 case DW_TAG_function_template:
6921 return "DW_TAG_function_template";
6922 case DW_TAG_class_template:
6923 return "DW_TAG_class_template";
6924 case DW_TAG_GNU_BINCL:
6925 return "DW_TAG_GNU_BINCL";
6926 case DW_TAG_GNU_EINCL:
6927 return "DW_TAG_GNU_EINCL";
6928 case DW_TAG_GNU_template_template_param:
6929 return "DW_TAG_GNU_template_template_param";
6930 case DW_TAG_GNU_call_site:
6931 return "DW_TAG_GNU_call_site";
6932 case DW_TAG_GNU_call_site_parameter:
6933 return "DW_TAG_GNU_call_site_parameter";
6934 default:
6935 return "DW_TAG_<unknown>";
6939 /* Convert a DWARF attribute code into its string name. */
6941 static const char *
6942 dwarf_attr_name (unsigned int attr)
6944 switch (attr)
6946 case DW_AT_sibling:
6947 return "DW_AT_sibling";
6948 case DW_AT_location:
6949 return "DW_AT_location";
6950 case DW_AT_name:
6951 return "DW_AT_name";
6952 case DW_AT_ordering:
6953 return "DW_AT_ordering";
6954 case DW_AT_subscr_data:
6955 return "DW_AT_subscr_data";
6956 case DW_AT_byte_size:
6957 return "DW_AT_byte_size";
6958 case DW_AT_bit_offset:
6959 return "DW_AT_bit_offset";
6960 case DW_AT_bit_size:
6961 return "DW_AT_bit_size";
6962 case DW_AT_element_list:
6963 return "DW_AT_element_list";
6964 case DW_AT_stmt_list:
6965 return "DW_AT_stmt_list";
6966 case DW_AT_low_pc:
6967 return "DW_AT_low_pc";
6968 case DW_AT_high_pc:
6969 return "DW_AT_high_pc";
6970 case DW_AT_language:
6971 return "DW_AT_language";
6972 case DW_AT_member:
6973 return "DW_AT_member";
6974 case DW_AT_discr:
6975 return "DW_AT_discr";
6976 case DW_AT_discr_value:
6977 return "DW_AT_discr_value";
6978 case DW_AT_visibility:
6979 return "DW_AT_visibility";
6980 case DW_AT_import:
6981 return "DW_AT_import";
6982 case DW_AT_string_length:
6983 return "DW_AT_string_length";
6984 case DW_AT_common_reference:
6985 return "DW_AT_common_reference";
6986 case DW_AT_comp_dir:
6987 return "DW_AT_comp_dir";
6988 case DW_AT_const_value:
6989 return "DW_AT_const_value";
6990 case DW_AT_containing_type:
6991 return "DW_AT_containing_type";
6992 case DW_AT_default_value:
6993 return "DW_AT_default_value";
6994 case DW_AT_inline:
6995 return "DW_AT_inline";
6996 case DW_AT_is_optional:
6997 return "DW_AT_is_optional";
6998 case DW_AT_lower_bound:
6999 return "DW_AT_lower_bound";
7000 case DW_AT_producer:
7001 return "DW_AT_producer";
7002 case DW_AT_prototyped:
7003 return "DW_AT_prototyped";
7004 case DW_AT_return_addr:
7005 return "DW_AT_return_addr";
7006 case DW_AT_start_scope:
7007 return "DW_AT_start_scope";
7008 case DW_AT_bit_stride:
7009 return "DW_AT_bit_stride";
7010 case DW_AT_upper_bound:
7011 return "DW_AT_upper_bound";
7012 case DW_AT_abstract_origin:
7013 return "DW_AT_abstract_origin";
7014 case DW_AT_accessibility:
7015 return "DW_AT_accessibility";
7016 case DW_AT_address_class:
7017 return "DW_AT_address_class";
7018 case DW_AT_artificial:
7019 return "DW_AT_artificial";
7020 case DW_AT_base_types:
7021 return "DW_AT_base_types";
7022 case DW_AT_calling_convention:
7023 return "DW_AT_calling_convention";
7024 case DW_AT_count:
7025 return "DW_AT_count";
7026 case DW_AT_data_member_location:
7027 return "DW_AT_data_member_location";
7028 case DW_AT_decl_column:
7029 return "DW_AT_decl_column";
7030 case DW_AT_decl_file:
7031 return "DW_AT_decl_file";
7032 case DW_AT_decl_line:
7033 return "DW_AT_decl_line";
7034 case DW_AT_declaration:
7035 return "DW_AT_declaration";
7036 case DW_AT_discr_list:
7037 return "DW_AT_discr_list";
7038 case DW_AT_encoding:
7039 return "DW_AT_encoding";
7040 case DW_AT_external:
7041 return "DW_AT_external";
7042 case DW_AT_explicit:
7043 return "DW_AT_explicit";
7044 case DW_AT_frame_base:
7045 return "DW_AT_frame_base";
7046 case DW_AT_friend:
7047 return "DW_AT_friend";
7048 case DW_AT_identifier_case:
7049 return "DW_AT_identifier_case";
7050 case DW_AT_macro_info:
7051 return "DW_AT_macro_info";
7052 case DW_AT_namelist_items:
7053 return "DW_AT_namelist_items";
7054 case DW_AT_priority:
7055 return "DW_AT_priority";
7056 case DW_AT_segment:
7057 return "DW_AT_segment";
7058 case DW_AT_specification:
7059 return "DW_AT_specification";
7060 case DW_AT_static_link:
7061 return "DW_AT_static_link";
7062 case DW_AT_type:
7063 return "DW_AT_type";
7064 case DW_AT_use_location:
7065 return "DW_AT_use_location";
7066 case DW_AT_variable_parameter:
7067 return "DW_AT_variable_parameter";
7068 case DW_AT_virtuality:
7069 return "DW_AT_virtuality";
7070 case DW_AT_vtable_elem_location:
7071 return "DW_AT_vtable_elem_location";
7073 case DW_AT_allocated:
7074 return "DW_AT_allocated";
7075 case DW_AT_associated:
7076 return "DW_AT_associated";
7077 case DW_AT_data_location:
7078 return "DW_AT_data_location";
7079 case DW_AT_byte_stride:
7080 return "DW_AT_byte_stride";
7081 case DW_AT_entry_pc:
7082 return "DW_AT_entry_pc";
7083 case DW_AT_use_UTF8:
7084 return "DW_AT_use_UTF8";
7085 case DW_AT_extension:
7086 return "DW_AT_extension";
7087 case DW_AT_ranges:
7088 return "DW_AT_ranges";
7089 case DW_AT_trampoline:
7090 return "DW_AT_trampoline";
7091 case DW_AT_call_column:
7092 return "DW_AT_call_column";
7093 case DW_AT_call_file:
7094 return "DW_AT_call_file";
7095 case DW_AT_call_line:
7096 return "DW_AT_call_line";
7097 case DW_AT_object_pointer:
7098 return "DW_AT_object_pointer";
7100 case DW_AT_signature:
7101 return "DW_AT_signature";
7102 case DW_AT_main_subprogram:
7103 return "DW_AT_main_subprogram";
7104 case DW_AT_data_bit_offset:
7105 return "DW_AT_data_bit_offset";
7106 case DW_AT_const_expr:
7107 return "DW_AT_const_expr";
7108 case DW_AT_enum_class:
7109 return "DW_AT_enum_class";
7110 case DW_AT_linkage_name:
7111 return "DW_AT_linkage_name";
7113 case DW_AT_MIPS_fde:
7114 return "DW_AT_MIPS_fde";
7115 case DW_AT_MIPS_loop_begin:
7116 return "DW_AT_MIPS_loop_begin";
7117 case DW_AT_MIPS_tail_loop_begin:
7118 return "DW_AT_MIPS_tail_loop_begin";
7119 case DW_AT_MIPS_epilog_begin:
7120 return "DW_AT_MIPS_epilog_begin";
7121 #if VMS_DEBUGGING_INFO
7122 case DW_AT_HP_prologue:
7123 return "DW_AT_HP_prologue";
7124 #else
7125 case DW_AT_MIPS_loop_unroll_factor:
7126 return "DW_AT_MIPS_loop_unroll_factor";
7127 #endif
7128 case DW_AT_MIPS_software_pipeline_depth:
7129 return "DW_AT_MIPS_software_pipeline_depth";
7130 case DW_AT_MIPS_linkage_name:
7131 return "DW_AT_MIPS_linkage_name";
7132 #if VMS_DEBUGGING_INFO
7133 case DW_AT_HP_epilogue:
7134 return "DW_AT_HP_epilogue";
7135 #else
7136 case DW_AT_MIPS_stride:
7137 return "DW_AT_MIPS_stride";
7138 #endif
7139 case DW_AT_MIPS_abstract_name:
7140 return "DW_AT_MIPS_abstract_name";
7141 case DW_AT_MIPS_clone_origin:
7142 return "DW_AT_MIPS_clone_origin";
7143 case DW_AT_MIPS_has_inlines:
7144 return "DW_AT_MIPS_has_inlines";
7146 case DW_AT_sf_names:
7147 return "DW_AT_sf_names";
7148 case DW_AT_src_info:
7149 return "DW_AT_src_info";
7150 case DW_AT_mac_info:
7151 return "DW_AT_mac_info";
7152 case DW_AT_src_coords:
7153 return "DW_AT_src_coords";
7154 case DW_AT_body_begin:
7155 return "DW_AT_body_begin";
7156 case DW_AT_body_end:
7157 return "DW_AT_body_end";
7159 case DW_AT_GNU_vector:
7160 return "DW_AT_GNU_vector";
7161 case DW_AT_GNU_guarded_by:
7162 return "DW_AT_GNU_guarded_by";
7163 case DW_AT_GNU_pt_guarded_by:
7164 return "DW_AT_GNU_pt_guarded_by";
7165 case DW_AT_GNU_guarded:
7166 return "DW_AT_GNU_guarded";
7167 case DW_AT_GNU_pt_guarded:
7168 return "DW_AT_GNU_pt_guarded";
7169 case DW_AT_GNU_locks_excluded:
7170 return "DW_AT_GNU_locks_excluded";
7171 case DW_AT_GNU_exclusive_locks_required:
7172 return "DW_AT_GNU_exclusive_locks_required";
7173 case DW_AT_GNU_shared_locks_required:
7174 return "DW_AT_GNU_shared_locks_required";
7175 case DW_AT_GNU_odr_signature:
7176 return "DW_AT_GNU_odr_signature";
7177 case DW_AT_GNU_template_name:
7178 return "DW_AT_GNU_template_name";
7179 case DW_AT_GNU_call_site_value:
7180 return "DW_AT_GNU_call_site_value";
7181 case DW_AT_GNU_call_site_data_value:
7182 return "DW_AT_GNU_call_site_data_value";
7183 case DW_AT_GNU_call_site_target:
7184 return "DW_AT_GNU_call_site_target";
7185 case DW_AT_GNU_call_site_target_clobbered:
7186 return "DW_AT_GNU_call_site_target_clobbered";
7187 case DW_AT_GNU_tail_call:
7188 return "DW_AT_GNU_tail_call";
7189 case DW_AT_GNU_all_tail_call_sites:
7190 return "DW_AT_GNU_all_tail_call_sites";
7191 case DW_AT_GNU_all_call_sites:
7192 return "DW_AT_GNU_all_call_sites";
7193 case DW_AT_GNU_all_source_call_sites:
7194 return "DW_AT_GNU_all_source_call_sites";
7196 case DW_AT_GNAT_descriptive_type:
7197 return "DW_AT_GNAT_descriptive_type";
7199 case DW_AT_VMS_rtnbeg_pd_address:
7200 return "DW_AT_VMS_rtnbeg_pd_address";
7202 default:
7203 return "DW_AT_<unknown>";
7207 /* Convert a DWARF value form code into its string name. */
7209 static const char *
7210 dwarf_form_name (unsigned int form)
7212 switch (form)
7214 case DW_FORM_addr:
7215 return "DW_FORM_addr";
7216 case DW_FORM_block2:
7217 return "DW_FORM_block2";
7218 case DW_FORM_block4:
7219 return "DW_FORM_block4";
7220 case DW_FORM_data2:
7221 return "DW_FORM_data2";
7222 case DW_FORM_data4:
7223 return "DW_FORM_data4";
7224 case DW_FORM_data8:
7225 return "DW_FORM_data8";
7226 case DW_FORM_string:
7227 return "DW_FORM_string";
7228 case DW_FORM_block:
7229 return "DW_FORM_block";
7230 case DW_FORM_block1:
7231 return "DW_FORM_block1";
7232 case DW_FORM_data1:
7233 return "DW_FORM_data1";
7234 case DW_FORM_flag:
7235 return "DW_FORM_flag";
7236 case DW_FORM_sdata:
7237 return "DW_FORM_sdata";
7238 case DW_FORM_strp:
7239 return "DW_FORM_strp";
7240 case DW_FORM_udata:
7241 return "DW_FORM_udata";
7242 case DW_FORM_ref_addr:
7243 return "DW_FORM_ref_addr";
7244 case DW_FORM_ref1:
7245 return "DW_FORM_ref1";
7246 case DW_FORM_ref2:
7247 return "DW_FORM_ref2";
7248 case DW_FORM_ref4:
7249 return "DW_FORM_ref4";
7250 case DW_FORM_ref8:
7251 return "DW_FORM_ref8";
7252 case DW_FORM_ref_udata:
7253 return "DW_FORM_ref_udata";
7254 case DW_FORM_indirect:
7255 return "DW_FORM_indirect";
7256 case DW_FORM_sec_offset:
7257 return "DW_FORM_sec_offset";
7258 case DW_FORM_exprloc:
7259 return "DW_FORM_exprloc";
7260 case DW_FORM_flag_present:
7261 return "DW_FORM_flag_present";
7262 case DW_FORM_ref_sig8:
7263 return "DW_FORM_ref_sig8";
7264 default:
7265 return "DW_FORM_<unknown>";
7269 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
7270 instance of an inlined instance of a decl which is local to an inline
7271 function, so we have to trace all of the way back through the origin chain
7272 to find out what sort of node actually served as the original seed for the
7273 given block. */
7275 static tree
7276 decl_ultimate_origin (const_tree decl)
7278 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7279 return NULL_TREE;
7281 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7282 nodes in the function to point to themselves; ignore that if
7283 we're trying to output the abstract instance of this function. */
7284 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7285 return NULL_TREE;
7287 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7288 most distant ancestor, this should never happen. */
7289 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7291 return DECL_ABSTRACT_ORIGIN (decl);
7294 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
7295 of a virtual function may refer to a base class, so we check the 'this'
7296 parameter. */
7298 static tree
7299 decl_class_context (tree decl)
7301 tree context = NULL_TREE;
7303 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7304 context = DECL_CONTEXT (decl);
7305 else
7306 context = TYPE_MAIN_VARIANT
7307 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7309 if (context && !TYPE_P (context))
7310 context = NULL_TREE;
7312 return context;
7315 /* Add an attribute/value pair to a DIE. */
7317 static inline void
7318 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7320 /* Maybe this should be an assert? */
7321 if (die == NULL)
7322 return;
7324 if (die->die_attr == NULL)
7325 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7326 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7329 static inline enum dw_val_class
7330 AT_class (dw_attr_ref a)
7332 return a->dw_attr_val.val_class;
7335 /* Add a flag value attribute to a DIE. */
7337 static inline void
7338 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7340 dw_attr_node attr;
7342 attr.dw_attr = attr_kind;
7343 attr.dw_attr_val.val_class = dw_val_class_flag;
7344 attr.dw_attr_val.v.val_flag = flag;
7345 add_dwarf_attr (die, &attr);
7348 static inline unsigned
7349 AT_flag (dw_attr_ref a)
7351 gcc_assert (a && AT_class (a) == dw_val_class_flag);
7352 return a->dw_attr_val.v.val_flag;
7355 /* Add a signed integer attribute value to a DIE. */
7357 static inline void
7358 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7360 dw_attr_node attr;
7362 attr.dw_attr = attr_kind;
7363 attr.dw_attr_val.val_class = dw_val_class_const;
7364 attr.dw_attr_val.v.val_int = int_val;
7365 add_dwarf_attr (die, &attr);
7368 static inline HOST_WIDE_INT
7369 AT_int (dw_attr_ref a)
7371 gcc_assert (a && AT_class (a) == dw_val_class_const);
7372 return a->dw_attr_val.v.val_int;
7375 /* Add an unsigned integer attribute value to a DIE. */
7377 static inline void
7378 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7379 unsigned HOST_WIDE_INT unsigned_val)
7381 dw_attr_node attr;
7383 attr.dw_attr = attr_kind;
7384 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7385 attr.dw_attr_val.v.val_unsigned = unsigned_val;
7386 add_dwarf_attr (die, &attr);
7389 static inline unsigned HOST_WIDE_INT
7390 AT_unsigned (dw_attr_ref a)
7392 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7393 return a->dw_attr_val.v.val_unsigned;
7396 /* Add an unsigned double integer attribute value to a DIE. */
7398 static inline void
7399 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7400 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7402 dw_attr_node attr;
7404 attr.dw_attr = attr_kind;
7405 attr.dw_attr_val.val_class = dw_val_class_const_double;
7406 attr.dw_attr_val.v.val_double.high = high;
7407 attr.dw_attr_val.v.val_double.low = low;
7408 add_dwarf_attr (die, &attr);
7411 /* Add a floating point attribute value to a DIE and return it. */
7413 static inline void
7414 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7415 unsigned int length, unsigned int elt_size, unsigned char *array)
7417 dw_attr_node attr;
7419 attr.dw_attr = attr_kind;
7420 attr.dw_attr_val.val_class = dw_val_class_vec;
7421 attr.dw_attr_val.v.val_vec.length = length;
7422 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7423 attr.dw_attr_val.v.val_vec.array = array;
7424 add_dwarf_attr (die, &attr);
7427 /* Add an 8-byte data attribute value to a DIE. */
7429 static inline void
7430 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7431 unsigned char data8[8])
7433 dw_attr_node attr;
7435 attr.dw_attr = attr_kind;
7436 attr.dw_attr_val.val_class = dw_val_class_data8;
7437 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7438 add_dwarf_attr (die, &attr);
7441 /* Hash and equality functions for debug_str_hash. */
7443 static hashval_t
7444 debug_str_do_hash (const void *x)
7446 return htab_hash_string (((const struct indirect_string_node *)x)->str);
7449 static int
7450 debug_str_eq (const void *x1, const void *x2)
7452 return strcmp ((((const struct indirect_string_node *)x1)->str),
7453 (const char *)x2) == 0;
7456 /* Add STR to the indirect string hash table. */
7458 static struct indirect_string_node *
7459 find_AT_string (const char *str)
7461 struct indirect_string_node *node;
7462 void **slot;
7464 if (! debug_str_hash)
7465 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7466 debug_str_eq, NULL);
7468 slot = htab_find_slot_with_hash (debug_str_hash, str,
7469 htab_hash_string (str), INSERT);
7470 if (*slot == NULL)
7472 node = ggc_alloc_cleared_indirect_string_node ();
7473 node->str = ggc_strdup (str);
7474 *slot = node;
7476 else
7477 node = (struct indirect_string_node *) *slot;
7479 node->refcount++;
7480 return node;
7483 /* Add a string attribute value to a DIE. */
7485 static inline void
7486 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7488 dw_attr_node attr;
7489 struct indirect_string_node *node;
7491 node = find_AT_string (str);
7493 attr.dw_attr = attr_kind;
7494 attr.dw_attr_val.val_class = dw_val_class_str;
7495 attr.dw_attr_val.v.val_str = node;
7496 add_dwarf_attr (die, &attr);
7499 /* Create a label for an indirect string node, ensuring it is going to
7500 be output, unless its reference count goes down to zero. */
7502 static inline void
7503 gen_label_for_indirect_string (struct indirect_string_node *node)
7505 char label[32];
7507 if (node->label)
7508 return;
7510 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7511 ++dw2_string_counter;
7512 node->label = xstrdup (label);
7515 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7516 debug string STR. */
7518 static inline rtx
7519 get_debug_string_label (const char *str)
7521 struct indirect_string_node *node = find_AT_string (str);
7523 debug_str_hash_forced = true;
7525 gen_label_for_indirect_string (node);
7527 return gen_rtx_SYMBOL_REF (Pmode, node->label);
7530 static inline const char *
7531 AT_string (dw_attr_ref a)
7533 gcc_assert (a && AT_class (a) == dw_val_class_str);
7534 return a->dw_attr_val.v.val_str->str;
7537 /* Find out whether a string should be output inline in DIE
7538 or out-of-line in .debug_str section. */
7540 static enum dwarf_form
7541 AT_string_form (dw_attr_ref a)
7543 struct indirect_string_node *node;
7544 unsigned int len;
7546 gcc_assert (a && AT_class (a) == dw_val_class_str);
7548 node = a->dw_attr_val.v.val_str;
7549 if (node->form)
7550 return node->form;
7552 len = strlen (node->str) + 1;
7554 /* If the string is shorter or equal to the size of the reference, it is
7555 always better to put it inline. */
7556 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7557 return node->form = DW_FORM_string;
7559 /* If we cannot expect the linker to merge strings in .debug_str
7560 section, only put it into .debug_str if it is worth even in this
7561 single module. */
7562 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7563 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7564 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7565 return node->form = DW_FORM_string;
7567 gen_label_for_indirect_string (node);
7569 return node->form = DW_FORM_strp;
7572 /* Add a DIE reference attribute value to a DIE. */
7574 static inline void
7575 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7577 dw_attr_node attr;
7579 #ifdef ENABLE_CHECKING
7580 gcc_assert (targ_die != NULL);
7581 #else
7582 /* With LTO we can end up trying to reference something we didn't create
7583 a DIE for. Avoid crashing later on a NULL referenced DIE. */
7584 if (targ_die == NULL)
7585 return;
7586 #endif
7588 attr.dw_attr = attr_kind;
7589 attr.dw_attr_val.val_class = dw_val_class_die_ref;
7590 attr.dw_attr_val.v.val_die_ref.die = targ_die;
7591 attr.dw_attr_val.v.val_die_ref.external = 0;
7592 add_dwarf_attr (die, &attr);
7595 /* Add an AT_specification attribute to a DIE, and also make the back
7596 pointer from the specification to the definition. */
7598 static inline void
7599 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7601 add_AT_die_ref (die, DW_AT_specification, targ_die);
7602 gcc_assert (!targ_die->die_definition);
7603 targ_die->die_definition = die;
7606 static inline dw_die_ref
7607 AT_ref (dw_attr_ref a)
7609 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7610 return a->dw_attr_val.v.val_die_ref.die;
7613 static inline int
7614 AT_ref_external (dw_attr_ref a)
7616 if (a && AT_class (a) == dw_val_class_die_ref)
7617 return a->dw_attr_val.v.val_die_ref.external;
7619 return 0;
7622 static inline void
7623 set_AT_ref_external (dw_attr_ref a, int i)
7625 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7626 a->dw_attr_val.v.val_die_ref.external = i;
7629 /* Add an FDE reference attribute value to a DIE. */
7631 static inline void
7632 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7634 dw_attr_node attr;
7636 attr.dw_attr = attr_kind;
7637 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7638 attr.dw_attr_val.v.val_fde_index = targ_fde;
7639 add_dwarf_attr (die, &attr);
7642 /* Add a location description attribute value to a DIE. */
7644 static inline void
7645 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7647 dw_attr_node attr;
7649 attr.dw_attr = attr_kind;
7650 attr.dw_attr_val.val_class = dw_val_class_loc;
7651 attr.dw_attr_val.v.val_loc = loc;
7652 add_dwarf_attr (die, &attr);
7655 static inline dw_loc_descr_ref
7656 AT_loc (dw_attr_ref a)
7658 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7659 return a->dw_attr_val.v.val_loc;
7662 static inline void
7663 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7665 dw_attr_node attr;
7667 attr.dw_attr = attr_kind;
7668 attr.dw_attr_val.val_class = dw_val_class_loc_list;
7669 attr.dw_attr_val.v.val_loc_list = loc_list;
7670 add_dwarf_attr (die, &attr);
7671 have_location_lists = true;
7674 static inline dw_loc_list_ref
7675 AT_loc_list (dw_attr_ref a)
7677 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7678 return a->dw_attr_val.v.val_loc_list;
7681 static inline dw_loc_list_ref *
7682 AT_loc_list_ptr (dw_attr_ref a)
7684 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7685 return &a->dw_attr_val.v.val_loc_list;
7688 /* Add an address constant attribute value to a DIE. */
7690 static inline void
7691 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7693 dw_attr_node attr;
7695 attr.dw_attr = attr_kind;
7696 attr.dw_attr_val.val_class = dw_val_class_addr;
7697 attr.dw_attr_val.v.val_addr = addr;
7698 add_dwarf_attr (die, &attr);
7701 /* Get the RTX from to an address DIE attribute. */
7703 static inline rtx
7704 AT_addr (dw_attr_ref a)
7706 gcc_assert (a && AT_class (a) == dw_val_class_addr);
7707 return a->dw_attr_val.v.val_addr;
7710 /* Add a file attribute value to a DIE. */
7712 static inline void
7713 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7714 struct dwarf_file_data *fd)
7716 dw_attr_node attr;
7718 attr.dw_attr = attr_kind;
7719 attr.dw_attr_val.val_class = dw_val_class_file;
7720 attr.dw_attr_val.v.val_file = fd;
7721 add_dwarf_attr (die, &attr);
7724 /* Get the dwarf_file_data from a file DIE attribute. */
7726 static inline struct dwarf_file_data *
7727 AT_file (dw_attr_ref a)
7729 gcc_assert (a && AT_class (a) == dw_val_class_file);
7730 return a->dw_attr_val.v.val_file;
7733 /* Add a vms delta attribute value to a DIE. */
7735 static inline void
7736 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7737 const char *lbl1, const char *lbl2)
7739 dw_attr_node attr;
7741 attr.dw_attr = attr_kind;
7742 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7743 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7744 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7745 add_dwarf_attr (die, &attr);
7748 /* Add a label identifier attribute value to a DIE. */
7750 static inline void
7751 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7753 dw_attr_node attr;
7755 attr.dw_attr = attr_kind;
7756 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7757 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7758 add_dwarf_attr (die, &attr);
7761 /* Add a section offset attribute value to a DIE, an offset into the
7762 debug_line section. */
7764 static inline void
7765 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7766 const char *label)
7768 dw_attr_node attr;
7770 attr.dw_attr = attr_kind;
7771 attr.dw_attr_val.val_class = dw_val_class_lineptr;
7772 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7773 add_dwarf_attr (die, &attr);
7776 /* Add a section offset attribute value to a DIE, an offset into the
7777 debug_macinfo section. */
7779 static inline void
7780 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7781 const char *label)
7783 dw_attr_node attr;
7785 attr.dw_attr = attr_kind;
7786 attr.dw_attr_val.val_class = dw_val_class_macptr;
7787 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7788 add_dwarf_attr (die, &attr);
7791 /* Add an offset attribute value to a DIE. */
7793 static inline void
7794 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7795 unsigned HOST_WIDE_INT offset)
7797 dw_attr_node attr;
7799 attr.dw_attr = attr_kind;
7800 attr.dw_attr_val.val_class = dw_val_class_offset;
7801 attr.dw_attr_val.v.val_offset = offset;
7802 add_dwarf_attr (die, &attr);
7805 /* Add an range_list attribute value to a DIE. */
7807 static void
7808 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7809 long unsigned int offset)
7811 dw_attr_node attr;
7813 attr.dw_attr = attr_kind;
7814 attr.dw_attr_val.val_class = dw_val_class_range_list;
7815 attr.dw_attr_val.v.val_offset = offset;
7816 add_dwarf_attr (die, &attr);
7819 /* Return the start label of a delta attribute. */
7821 static inline const char *
7822 AT_vms_delta1 (dw_attr_ref a)
7824 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7825 return a->dw_attr_val.v.val_vms_delta.lbl1;
7828 /* Return the end label of a delta attribute. */
7830 static inline const char *
7831 AT_vms_delta2 (dw_attr_ref a)
7833 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7834 return a->dw_attr_val.v.val_vms_delta.lbl2;
7837 static inline const char *
7838 AT_lbl (dw_attr_ref a)
7840 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7841 || AT_class (a) == dw_val_class_lineptr
7842 || AT_class (a) == dw_val_class_macptr));
7843 return a->dw_attr_val.v.val_lbl_id;
7846 /* Get the attribute of type attr_kind. */
7848 static dw_attr_ref
7849 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7851 dw_attr_ref a;
7852 unsigned ix;
7853 dw_die_ref spec = NULL;
7855 if (! die)
7856 return NULL;
7858 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7859 if (a->dw_attr == attr_kind)
7860 return a;
7861 else if (a->dw_attr == DW_AT_specification
7862 || a->dw_attr == DW_AT_abstract_origin)
7863 spec = AT_ref (a);
7865 if (spec)
7866 return get_AT (spec, attr_kind);
7868 return NULL;
7871 /* Return the "low pc" attribute value, typically associated with a subprogram
7872 DIE. Return null if the "low pc" attribute is either not present, or if it
7873 cannot be represented as an assembler label identifier. */
7875 static inline const char *
7876 get_AT_low_pc (dw_die_ref die)
7878 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7880 return a ? AT_lbl (a) : NULL;
7883 /* Return the "high pc" attribute value, typically associated with a subprogram
7884 DIE. Return null if the "high pc" attribute is either not present, or if it
7885 cannot be represented as an assembler label identifier. */
7887 static inline const char *
7888 get_AT_hi_pc (dw_die_ref die)
7890 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7892 return a ? AT_lbl (a) : NULL;
7895 /* Return the value of the string attribute designated by ATTR_KIND, or
7896 NULL if it is not present. */
7898 static inline const char *
7899 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7901 dw_attr_ref a = get_AT (die, attr_kind);
7903 return a ? AT_string (a) : NULL;
7906 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7907 if it is not present. */
7909 static inline int
7910 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7912 dw_attr_ref a = get_AT (die, attr_kind);
7914 return a ? AT_flag (a) : 0;
7917 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7918 if it is not present. */
7920 static inline unsigned
7921 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7923 dw_attr_ref a = get_AT (die, attr_kind);
7925 return a ? AT_unsigned (a) : 0;
7928 static inline dw_die_ref
7929 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7931 dw_attr_ref a = get_AT (die, attr_kind);
7933 return a ? AT_ref (a) : NULL;
7936 static inline struct dwarf_file_data *
7937 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7939 dw_attr_ref a = get_AT (die, attr_kind);
7941 return a ? AT_file (a) : NULL;
7944 /* Return TRUE if the language is C++. */
7946 static inline bool
7947 is_cxx (void)
7949 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7951 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7954 /* Return TRUE if the language is Fortran. */
7956 static inline bool
7957 is_fortran (void)
7959 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7961 return (lang == DW_LANG_Fortran77
7962 || lang == DW_LANG_Fortran90
7963 || lang == DW_LANG_Fortran95);
7966 /* Return TRUE if the language is Ada. */
7968 static inline bool
7969 is_ada (void)
7971 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7973 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7976 /* Remove the specified attribute if present. */
7978 static void
7979 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7981 dw_attr_ref a;
7982 unsigned ix;
7984 if (! die)
7985 return;
7987 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7988 if (a->dw_attr == attr_kind)
7990 if (AT_class (a) == dw_val_class_str)
7991 if (a->dw_attr_val.v.val_str->refcount)
7992 a->dw_attr_val.v.val_str->refcount--;
7994 /* VEC_ordered_remove should help reduce the number of abbrevs
7995 that are needed. */
7996 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7997 return;
8001 /* Remove CHILD from its parent. PREV must have the property that
8002 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
8004 static void
8005 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
8007 gcc_assert (child->die_parent == prev->die_parent);
8008 gcc_assert (prev->die_sib == child);
8009 if (prev == child)
8011 gcc_assert (child->die_parent->die_child == child);
8012 prev = NULL;
8014 else
8015 prev->die_sib = child->die_sib;
8016 if (child->die_parent->die_child == child)
8017 child->die_parent->die_child = prev;
8020 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
8021 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
8023 static void
8024 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
8026 dw_die_ref parent = old_child->die_parent;
8028 gcc_assert (parent == prev->die_parent);
8029 gcc_assert (prev->die_sib == old_child);
8031 new_child->die_parent = parent;
8032 if (prev == old_child)
8034 gcc_assert (parent->die_child == old_child);
8035 new_child->die_sib = new_child;
8037 else
8039 prev->die_sib = new_child;
8040 new_child->die_sib = old_child->die_sib;
8042 if (old_child->die_parent->die_child == old_child)
8043 old_child->die_parent->die_child = new_child;
8046 /* Move all children from OLD_PARENT to NEW_PARENT. */
8048 static void
8049 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
8051 dw_die_ref c;
8052 new_parent->die_child = old_parent->die_child;
8053 old_parent->die_child = NULL;
8054 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
8057 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
8058 matches TAG. */
8060 static void
8061 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8063 dw_die_ref c;
8065 c = die->die_child;
8066 if (c) do {
8067 dw_die_ref prev = c;
8068 c = c->die_sib;
8069 while (c->die_tag == tag)
8071 remove_child_with_prev (c, prev);
8072 /* Might have removed every child. */
8073 if (c == c->die_sib)
8074 return;
8075 c = c->die_sib;
8077 } while (c != die->die_child);
8080 /* Add a CHILD_DIE as the last child of DIE. */
8082 static void
8083 add_child_die (dw_die_ref die, dw_die_ref child_die)
8085 /* FIXME this should probably be an assert. */
8086 if (! die || ! child_die)
8087 return;
8088 gcc_assert (die != child_die);
8090 child_die->die_parent = die;
8091 if (die->die_child)
8093 child_die->die_sib = die->die_child->die_sib;
8094 die->die_child->die_sib = child_die;
8096 else
8097 child_die->die_sib = child_die;
8098 die->die_child = child_die;
8101 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8102 is the specification, to the end of PARENT's list of children.
8103 This is done by removing and re-adding it. */
8105 static void
8106 splice_child_die (dw_die_ref parent, dw_die_ref child)
8108 dw_die_ref p;
8110 /* We want the declaration DIE from inside the class, not the
8111 specification DIE at toplevel. */
8112 if (child->die_parent != parent)
8114 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8116 if (tmp)
8117 child = tmp;
8120 gcc_assert (child->die_parent == parent
8121 || (child->die_parent
8122 == get_AT_ref (parent, DW_AT_specification)));
8124 for (p = child->die_parent->die_child; ; p = p->die_sib)
8125 if (p->die_sib == child)
8127 remove_child_with_prev (child, p);
8128 break;
8131 add_child_die (parent, child);
8134 /* Return a pointer to a newly created DIE node. */
8136 static inline dw_die_ref
8137 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8139 dw_die_ref die = ggc_alloc_cleared_die_node ();
8141 die->die_tag = tag_value;
8143 if (parent_die != NULL)
8144 add_child_die (parent_die, die);
8145 else
8147 limbo_die_node *limbo_node;
8149 limbo_node = ggc_alloc_cleared_limbo_die_node ();
8150 limbo_node->die = die;
8151 limbo_node->created_for = t;
8152 limbo_node->next = limbo_die_list;
8153 limbo_die_list = limbo_node;
8156 return die;
8159 /* Return the DIE associated with the given type specifier. */
8161 static inline dw_die_ref
8162 lookup_type_die (tree type)
8164 return TYPE_SYMTAB_DIE (type);
8167 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
8168 anonymous type named by the typedef TYPE_DIE, return the DIE of the
8169 anonymous type instead the one of the naming typedef. */
8171 static inline dw_die_ref
8172 strip_naming_typedef (tree type, dw_die_ref type_die)
8174 if (type
8175 && TREE_CODE (type) == RECORD_TYPE
8176 && type_die
8177 && type_die->die_tag == DW_TAG_typedef
8178 && is_naming_typedef_decl (TYPE_NAME (type)))
8179 type_die = get_AT_ref (type_die, DW_AT_type);
8180 return type_die;
8183 /* Like lookup_type_die, but if type is an anonymous type named by a
8184 typedef[1], return the DIE of the anonymous type instead the one of
8185 the naming typedef. This is because in gen_typedef_die, we did
8186 equate the anonymous struct named by the typedef with the DIE of
8187 the naming typedef. So by default, lookup_type_die on an anonymous
8188 struct yields the DIE of the naming typedef.
8190 [1]: Read the comment of is_naming_typedef_decl to learn about what
8191 a naming typedef is. */
8193 static inline dw_die_ref
8194 lookup_type_die_strip_naming_typedef (tree type)
8196 dw_die_ref die = lookup_type_die (type);
8197 return strip_naming_typedef (type, die);
8200 /* Equate a DIE to a given type specifier. */
8202 static inline void
8203 equate_type_number_to_die (tree type, dw_die_ref type_die)
8205 TYPE_SYMTAB_DIE (type) = type_die;
8208 /* Returns a hash value for X (which really is a die_struct). */
8210 static hashval_t
8211 decl_die_table_hash (const void *x)
8213 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8216 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
8218 static int
8219 decl_die_table_eq (const void *x, const void *y)
8221 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8224 /* Return the DIE associated with a given declaration. */
8226 static inline dw_die_ref
8227 lookup_decl_die (tree decl)
8229 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8232 /* Returns a hash value for X (which really is a var_loc_list). */
8234 static hashval_t
8235 decl_loc_table_hash (const void *x)
8237 return (hashval_t) ((const var_loc_list *) x)->decl_id;
8240 /* Return nonzero if decl_id of var_loc_list X is the same as
8241 UID of decl *Y. */
8243 static int
8244 decl_loc_table_eq (const void *x, const void *y)
8246 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8249 /* Return the var_loc list associated with a given declaration. */
8251 static inline var_loc_list *
8252 lookup_decl_loc (const_tree decl)
8254 if (!decl_loc_table)
8255 return NULL;
8256 return (var_loc_list *)
8257 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8260 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
8262 static hashval_t
8263 cached_dw_loc_list_table_hash (const void *x)
8265 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
8268 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
8269 UID of decl *Y. */
8271 static int
8272 cached_dw_loc_list_table_eq (const void *x, const void *y)
8274 return (((const cached_dw_loc_list *) x)->decl_id
8275 == DECL_UID ((const_tree) y));
8278 /* Equate a DIE to a particular declaration. */
8280 static void
8281 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8283 unsigned int decl_id = DECL_UID (decl);
8284 void **slot;
8286 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8287 *slot = decl_die;
8288 decl_die->decl_id = decl_id;
8291 /* Return how many bits covers PIECE EXPR_LIST. */
8293 static int
8294 decl_piece_bitsize (rtx piece)
8296 int ret = (int) GET_MODE (piece);
8297 if (ret)
8298 return ret;
8299 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8300 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8301 return INTVAL (XEXP (XEXP (piece, 0), 0));
8304 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
8306 static rtx *
8307 decl_piece_varloc_ptr (rtx piece)
8309 if ((int) GET_MODE (piece))
8310 return &XEXP (piece, 0);
8311 else
8312 return &XEXP (XEXP (piece, 0), 1);
8315 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8316 Next is the chain of following piece nodes. */
8318 static rtx
8319 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8321 if (bitsize <= (int) MAX_MACHINE_MODE)
8322 return alloc_EXPR_LIST (bitsize, loc_note, next);
8323 else
8324 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8325 GEN_INT (bitsize),
8326 loc_note), next);
8329 /* Return rtx that should be stored into loc field for
8330 LOC_NOTE and BITPOS/BITSIZE. */
8332 static rtx
8333 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8334 HOST_WIDE_INT bitsize)
8336 if (bitsize != -1)
8338 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8339 if (bitpos != 0)
8340 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8342 return loc_note;
8345 /* This function either modifies location piece list *DEST in
8346 place (if SRC and INNER is NULL), or copies location piece list
8347 *SRC to *DEST while modifying it. Location BITPOS is modified
8348 to contain LOC_NOTE, any pieces overlapping it are removed resp.
8349 not copied and if needed some padding around it is added.
8350 When modifying in place, DEST should point to EXPR_LIST where
8351 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8352 to the start of the whole list and INNER points to the EXPR_LIST
8353 where earlier pieces cover PIECE_BITPOS bits. */
8355 static void
8356 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8357 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8358 HOST_WIDE_INT bitsize, rtx loc_note)
8360 int diff;
8361 bool copy = inner != NULL;
8363 if (copy)
8365 /* First copy all nodes preceeding the current bitpos. */
8366 while (src != inner)
8368 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8369 decl_piece_bitsize (*src), NULL_RTX);
8370 dest = &XEXP (*dest, 1);
8371 src = &XEXP (*src, 1);
8374 /* Add padding if needed. */
8375 if (bitpos != piece_bitpos)
8377 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8378 copy ? NULL_RTX : *dest);
8379 dest = &XEXP (*dest, 1);
8381 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8383 gcc_assert (!copy);
8384 /* A piece with correct bitpos and bitsize already exist,
8385 just update the location for it and return. */
8386 *decl_piece_varloc_ptr (*dest) = loc_note;
8387 return;
8389 /* Add the piece that changed. */
8390 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8391 dest = &XEXP (*dest, 1);
8392 /* Skip over pieces that overlap it. */
8393 diff = bitpos - piece_bitpos + bitsize;
8394 if (!copy)
8395 src = dest;
8396 while (diff > 0 && *src)
8398 rtx piece = *src;
8399 diff -= decl_piece_bitsize (piece);
8400 if (copy)
8401 src = &XEXP (piece, 1);
8402 else
8404 *src = XEXP (piece, 1);
8405 free_EXPR_LIST_node (piece);
8408 /* Add padding if needed. */
8409 if (diff < 0 && *src)
8411 if (!copy)
8412 dest = src;
8413 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8414 dest = &XEXP (*dest, 1);
8416 if (!copy)
8417 return;
8418 /* Finally copy all nodes following it. */
8419 while (*src)
8421 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8422 decl_piece_bitsize (*src), NULL_RTX);
8423 dest = &XEXP (*dest, 1);
8424 src = &XEXP (*src, 1);
8428 /* Add a variable location node to the linked list for DECL. */
8430 static struct var_loc_node *
8431 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8433 unsigned int decl_id;
8434 var_loc_list *temp;
8435 void **slot;
8436 struct var_loc_node *loc = NULL;
8437 HOST_WIDE_INT bitsize = -1, bitpos = -1;
8439 if (DECL_DEBUG_EXPR_IS_FROM (decl))
8441 tree realdecl = DECL_DEBUG_EXPR (decl);
8442 if (realdecl && handled_component_p (realdecl))
8444 HOST_WIDE_INT maxsize;
8445 tree innerdecl;
8446 innerdecl
8447 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8448 if (!DECL_P (innerdecl)
8449 || DECL_IGNORED_P (innerdecl)
8450 || TREE_STATIC (innerdecl)
8451 || bitsize <= 0
8452 || bitpos + bitsize > 256
8453 || bitsize != maxsize)
8454 return NULL;
8455 decl = innerdecl;
8459 decl_id = DECL_UID (decl);
8460 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8461 if (*slot == NULL)
8463 temp = ggc_alloc_cleared_var_loc_list ();
8464 temp->decl_id = decl_id;
8465 *slot = temp;
8467 else
8468 temp = (var_loc_list *) *slot;
8470 if (temp->last)
8472 struct var_loc_node *last = temp->last, *unused = NULL;
8473 rtx *piece_loc = NULL, last_loc_note;
8474 int piece_bitpos = 0;
8475 if (last->next)
8477 last = last->next;
8478 gcc_assert (last->next == NULL);
8480 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8482 piece_loc = &last->loc;
8485 int cur_bitsize = decl_piece_bitsize (*piece_loc);
8486 if (piece_bitpos + cur_bitsize > bitpos)
8487 break;
8488 piece_bitpos += cur_bitsize;
8489 piece_loc = &XEXP (*piece_loc, 1);
8491 while (*piece_loc);
8493 /* TEMP->LAST here is either pointer to the last but one or
8494 last element in the chained list, LAST is pointer to the
8495 last element. */
8496 if (label && strcmp (last->label, label) == 0)
8498 /* For SRA optimized variables if there weren't any real
8499 insns since last note, just modify the last node. */
8500 if (piece_loc != NULL)
8502 adjust_piece_list (piece_loc, NULL, NULL,
8503 bitpos, piece_bitpos, bitsize, loc_note);
8504 return NULL;
8506 /* If the last note doesn't cover any instructions, remove it. */
8507 if (temp->last != last)
8509 temp->last->next = NULL;
8510 unused = last;
8511 last = temp->last;
8512 gcc_assert (strcmp (last->label, label) != 0);
8514 else
8516 gcc_assert (temp->first == temp->last);
8517 memset (temp->last, '\0', sizeof (*temp->last));
8518 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8519 return temp->last;
8522 if (bitsize == -1 && NOTE_P (last->loc))
8523 last_loc_note = last->loc;
8524 else if (piece_loc != NULL
8525 && *piece_loc != NULL_RTX
8526 && piece_bitpos == bitpos
8527 && decl_piece_bitsize (*piece_loc) == bitsize)
8528 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8529 else
8530 last_loc_note = NULL_RTX;
8531 /* If the current location is the same as the end of the list,
8532 and either both or neither of the locations is uninitialized,
8533 we have nothing to do. */
8534 if (last_loc_note == NULL_RTX
8535 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8536 NOTE_VAR_LOCATION_LOC (loc_note)))
8537 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8538 != NOTE_VAR_LOCATION_STATUS (loc_note))
8539 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8540 == VAR_INIT_STATUS_UNINITIALIZED)
8541 || (NOTE_VAR_LOCATION_STATUS (loc_note)
8542 == VAR_INIT_STATUS_UNINITIALIZED))))
8544 /* Add LOC to the end of list and update LAST. If the last
8545 element of the list has been removed above, reuse its
8546 memory for the new node, otherwise allocate a new one. */
8547 if (unused)
8549 loc = unused;
8550 memset (loc, '\0', sizeof (*loc));
8552 else
8553 loc = ggc_alloc_cleared_var_loc_node ();
8554 if (bitsize == -1 || piece_loc == NULL)
8555 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8556 else
8557 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8558 bitpos, piece_bitpos, bitsize, loc_note);
8559 last->next = loc;
8560 /* Ensure TEMP->LAST will point either to the new last but one
8561 element of the chain, or to the last element in it. */
8562 if (last != temp->last)
8563 temp->last = last;
8565 else if (unused)
8566 ggc_free (unused);
8568 else
8570 loc = ggc_alloc_cleared_var_loc_node ();
8571 temp->first = loc;
8572 temp->last = loc;
8573 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8575 return loc;
8578 /* Keep track of the number of spaces used to indent the
8579 output of the debugging routines that print the structure of
8580 the DIE internal representation. */
8581 static int print_indent;
8583 /* Indent the line the number of spaces given by print_indent. */
8585 static inline void
8586 print_spaces (FILE *outfile)
8588 fprintf (outfile, "%*s", print_indent, "");
8591 /* Print a type signature in hex. */
8593 static inline void
8594 print_signature (FILE *outfile, char *sig)
8596 int i;
8598 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8599 fprintf (outfile, "%02x", sig[i] & 0xff);
8602 /* Print the information associated with a given DIE, and its children.
8603 This routine is a debugging aid only. */
8605 static void
8606 print_die (dw_die_ref die, FILE *outfile)
8608 dw_attr_ref a;
8609 dw_die_ref c;
8610 unsigned ix;
8612 print_spaces (outfile);
8613 fprintf (outfile, "DIE %4ld: %s (%p)\n",
8614 die->die_offset, dwarf_tag_name (die->die_tag),
8615 (void*) die);
8616 print_spaces (outfile);
8617 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
8618 fprintf (outfile, " offset: %ld", die->die_offset);
8619 fprintf (outfile, " mark: %d\n", die->die_mark);
8621 if (use_debug_types && die->die_id.die_type_node)
8623 print_spaces (outfile);
8624 fprintf (outfile, " signature: ");
8625 print_signature (outfile, die->die_id.die_type_node->signature);
8626 fprintf (outfile, "\n");
8629 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8631 print_spaces (outfile);
8632 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
8634 switch (AT_class (a))
8636 case dw_val_class_addr:
8637 fprintf (outfile, "address");
8638 break;
8639 case dw_val_class_offset:
8640 fprintf (outfile, "offset");
8641 break;
8642 case dw_val_class_loc:
8643 fprintf (outfile, "location descriptor");
8644 break;
8645 case dw_val_class_loc_list:
8646 fprintf (outfile, "location list -> label:%s",
8647 AT_loc_list (a)->ll_symbol);
8648 break;
8649 case dw_val_class_range_list:
8650 fprintf (outfile, "range list");
8651 break;
8652 case dw_val_class_const:
8653 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8654 break;
8655 case dw_val_class_unsigned_const:
8656 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8657 break;
8658 case dw_val_class_const_double:
8659 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8660 HOST_WIDE_INT_PRINT_UNSIGNED")",
8661 a->dw_attr_val.v.val_double.high,
8662 a->dw_attr_val.v.val_double.low);
8663 break;
8664 case dw_val_class_vec:
8665 fprintf (outfile, "floating-point or vector constant");
8666 break;
8667 case dw_val_class_flag:
8668 fprintf (outfile, "%u", AT_flag (a));
8669 break;
8670 case dw_val_class_die_ref:
8671 if (AT_ref (a) != NULL)
8673 if (use_debug_types && AT_ref (a)->die_id.die_type_node)
8675 fprintf (outfile, "die -> signature: ");
8676 print_signature (outfile,
8677 AT_ref (a)->die_id.die_type_node->signature);
8679 else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
8680 fprintf (outfile, "die -> label: %s",
8681 AT_ref (a)->die_id.die_symbol);
8682 else
8683 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8684 fprintf (outfile, " (%p)", (void *) AT_ref (a));
8686 else
8687 fprintf (outfile, "die -> <null>");
8688 break;
8689 case dw_val_class_vms_delta:
8690 fprintf (outfile, "delta: @slotcount(%s-%s)",
8691 AT_vms_delta2 (a), AT_vms_delta1 (a));
8692 break;
8693 case dw_val_class_lbl_id:
8694 case dw_val_class_lineptr:
8695 case dw_val_class_macptr:
8696 fprintf (outfile, "label: %s", AT_lbl (a));
8697 break;
8698 case dw_val_class_str:
8699 if (AT_string (a) != NULL)
8700 fprintf (outfile, "\"%s\"", AT_string (a));
8701 else
8702 fprintf (outfile, "<null>");
8703 break;
8704 case dw_val_class_file:
8705 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8706 AT_file (a)->emitted_number);
8707 break;
8708 case dw_val_class_data8:
8710 int i;
8712 for (i = 0; i < 8; i++)
8713 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8714 break;
8716 default:
8717 break;
8720 fprintf (outfile, "\n");
8723 if (die->die_child != NULL)
8725 print_indent += 4;
8726 FOR_EACH_CHILD (die, c, print_die (c, outfile));
8727 print_indent -= 4;
8729 if (print_indent == 0)
8730 fprintf (outfile, "\n");
8733 /* Print the information collected for a given DIE. */
8735 DEBUG_FUNCTION void
8736 debug_dwarf_die (dw_die_ref die)
8738 print_die (die, stderr);
8741 /* Print all DWARF information collected for the compilation unit.
8742 This routine is a debugging aid only. */
8744 DEBUG_FUNCTION void
8745 debug_dwarf (void)
8747 print_indent = 0;
8748 print_die (comp_unit_die (), stderr);
8751 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
8752 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
8753 DIE that marks the start of the DIEs for this include file. */
8755 static dw_die_ref
8756 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8758 const char *filename = get_AT_string (bincl_die, DW_AT_name);
8759 dw_die_ref new_unit = gen_compile_unit_die (filename);
8761 new_unit->die_sib = old_unit;
8762 return new_unit;
8765 /* Close an include-file CU and reopen the enclosing one. */
8767 static dw_die_ref
8768 pop_compile_unit (dw_die_ref old_unit)
8770 dw_die_ref new_unit = old_unit->die_sib;
8772 old_unit->die_sib = NULL;
8773 return new_unit;
8776 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8777 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8779 /* Calculate the checksum of a location expression. */
8781 static inline void
8782 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8784 int tem;
8786 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8787 CHECKSUM (tem);
8788 CHECKSUM (loc->dw_loc_oprnd1);
8789 CHECKSUM (loc->dw_loc_oprnd2);
8792 /* Calculate the checksum of an attribute. */
8794 static void
8795 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8797 dw_loc_descr_ref loc;
8798 rtx r;
8800 CHECKSUM (at->dw_attr);
8802 /* We don't care that this was compiled with a different compiler
8803 snapshot; if the output is the same, that's what matters. */
8804 if (at->dw_attr == DW_AT_producer)
8805 return;
8807 switch (AT_class (at))
8809 case dw_val_class_const:
8810 CHECKSUM (at->dw_attr_val.v.val_int);
8811 break;
8812 case dw_val_class_unsigned_const:
8813 CHECKSUM (at->dw_attr_val.v.val_unsigned);
8814 break;
8815 case dw_val_class_const_double:
8816 CHECKSUM (at->dw_attr_val.v.val_double);
8817 break;
8818 case dw_val_class_vec:
8819 CHECKSUM (at->dw_attr_val.v.val_vec);
8820 break;
8821 case dw_val_class_flag:
8822 CHECKSUM (at->dw_attr_val.v.val_flag);
8823 break;
8824 case dw_val_class_str:
8825 CHECKSUM_STRING (AT_string (at));
8826 break;
8828 case dw_val_class_addr:
8829 r = AT_addr (at);
8830 gcc_assert (GET_CODE (r) == SYMBOL_REF);
8831 CHECKSUM_STRING (XSTR (r, 0));
8832 break;
8834 case dw_val_class_offset:
8835 CHECKSUM (at->dw_attr_val.v.val_offset);
8836 break;
8838 case dw_val_class_loc:
8839 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8840 loc_checksum (loc, ctx);
8841 break;
8843 case dw_val_class_die_ref:
8844 die_checksum (AT_ref (at), ctx, mark);
8845 break;
8847 case dw_val_class_fde_ref:
8848 case dw_val_class_vms_delta:
8849 case dw_val_class_lbl_id:
8850 case dw_val_class_lineptr:
8851 case dw_val_class_macptr:
8852 break;
8854 case dw_val_class_file:
8855 CHECKSUM_STRING (AT_file (at)->filename);
8856 break;
8858 case dw_val_class_data8:
8859 CHECKSUM (at->dw_attr_val.v.val_data8);
8860 break;
8862 default:
8863 break;
8867 /* Calculate the checksum of a DIE. */
8869 static void
8870 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8872 dw_die_ref c;
8873 dw_attr_ref a;
8874 unsigned ix;
8876 /* To avoid infinite recursion. */
8877 if (die->die_mark)
8879 CHECKSUM (die->die_mark);
8880 return;
8882 die->die_mark = ++(*mark);
8884 CHECKSUM (die->die_tag);
8886 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8887 attr_checksum (a, ctx, mark);
8889 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8892 #undef CHECKSUM
8893 #undef CHECKSUM_STRING
8895 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
8896 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8897 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8898 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8899 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8900 #define CHECKSUM_ATTR(FOO) \
8901 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8903 /* Calculate the checksum of a number in signed LEB128 format. */
8905 static void
8906 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8908 unsigned char byte;
8909 bool more;
8911 while (1)
8913 byte = (value & 0x7f);
8914 value >>= 7;
8915 more = !((value == 0 && (byte & 0x40) == 0)
8916 || (value == -1 && (byte & 0x40) != 0));
8917 if (more)
8918 byte |= 0x80;
8919 CHECKSUM (byte);
8920 if (!more)
8921 break;
8925 /* Calculate the checksum of a number in unsigned LEB128 format. */
8927 static void
8928 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8930 while (1)
8932 unsigned char byte = (value & 0x7f);
8933 value >>= 7;
8934 if (value != 0)
8935 /* More bytes to follow. */
8936 byte |= 0x80;
8937 CHECKSUM (byte);
8938 if (value == 0)
8939 break;
8943 /* Checksum the context of the DIE. This adds the names of any
8944 surrounding namespaces or structures to the checksum. */
8946 static void
8947 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8949 const char *name;
8950 dw_die_ref spec;
8951 int tag = die->die_tag;
8953 if (tag != DW_TAG_namespace
8954 && tag != DW_TAG_structure_type
8955 && tag != DW_TAG_class_type)
8956 return;
8958 name = get_AT_string (die, DW_AT_name);
8960 spec = get_AT_ref (die, DW_AT_specification);
8961 if (spec != NULL)
8962 die = spec;
8964 if (die->die_parent != NULL)
8965 checksum_die_context (die->die_parent, ctx);
8967 CHECKSUM_ULEB128 ('C');
8968 CHECKSUM_ULEB128 (tag);
8969 if (name != NULL)
8970 CHECKSUM_STRING (name);
8973 /* Calculate the checksum of a location expression. */
8975 static inline void
8976 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8978 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8979 were emitted as a DW_FORM_sdata instead of a location expression. */
8980 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8982 CHECKSUM_ULEB128 (DW_FORM_sdata);
8983 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8984 return;
8987 /* Otherwise, just checksum the raw location expression. */
8988 while (loc != NULL)
8990 CHECKSUM_ULEB128 (loc->dw_loc_opc);
8991 CHECKSUM (loc->dw_loc_oprnd1);
8992 CHECKSUM (loc->dw_loc_oprnd2);
8993 loc = loc->dw_loc_next;
8997 /* Calculate the checksum of an attribute. */
8999 static void
9000 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
9001 struct md5_ctx *ctx, int *mark)
9003 dw_loc_descr_ref loc;
9004 rtx r;
9006 if (AT_class (at) == dw_val_class_die_ref)
9008 dw_die_ref target_die = AT_ref (at);
9010 /* For pointer and reference types, we checksum only the (qualified)
9011 name of the target type (if there is a name). For friend entries,
9012 we checksum only the (qualified) name of the target type or function.
9013 This allows the checksum to remain the same whether the target type
9014 is complete or not. */
9015 if ((at->dw_attr == DW_AT_type
9016 && (tag == DW_TAG_pointer_type
9017 || tag == DW_TAG_reference_type
9018 || tag == DW_TAG_rvalue_reference_type
9019 || tag == DW_TAG_ptr_to_member_type))
9020 || (at->dw_attr == DW_AT_friend
9021 && tag == DW_TAG_friend))
9023 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
9025 if (name_attr != NULL)
9027 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9029 if (decl == NULL)
9030 decl = target_die;
9031 CHECKSUM_ULEB128 ('N');
9032 CHECKSUM_ULEB128 (at->dw_attr);
9033 if (decl->die_parent != NULL)
9034 checksum_die_context (decl->die_parent, ctx);
9035 CHECKSUM_ULEB128 ('E');
9036 CHECKSUM_STRING (AT_string (name_attr));
9037 return;
9041 /* For all other references to another DIE, we check to see if the
9042 target DIE has already been visited. If it has, we emit a
9043 backward reference; if not, we descend recursively. */
9044 if (target_die->die_mark > 0)
9046 CHECKSUM_ULEB128 ('R');
9047 CHECKSUM_ULEB128 (at->dw_attr);
9048 CHECKSUM_ULEB128 (target_die->die_mark);
9050 else
9052 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9054 if (decl == NULL)
9055 decl = target_die;
9056 target_die->die_mark = ++(*mark);
9057 CHECKSUM_ULEB128 ('T');
9058 CHECKSUM_ULEB128 (at->dw_attr);
9059 if (decl->die_parent != NULL)
9060 checksum_die_context (decl->die_parent, ctx);
9061 die_checksum_ordered (target_die, ctx, mark);
9063 return;
9066 CHECKSUM_ULEB128 ('A');
9067 CHECKSUM_ULEB128 (at->dw_attr);
9069 switch (AT_class (at))
9071 case dw_val_class_const:
9072 CHECKSUM_ULEB128 (DW_FORM_sdata);
9073 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9074 break;
9076 case dw_val_class_unsigned_const:
9077 CHECKSUM_ULEB128 (DW_FORM_sdata);
9078 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9079 break;
9081 case dw_val_class_const_double:
9082 CHECKSUM_ULEB128 (DW_FORM_block);
9083 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9084 CHECKSUM (at->dw_attr_val.v.val_double);
9085 break;
9087 case dw_val_class_vec:
9088 CHECKSUM_ULEB128 (DW_FORM_block);
9089 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9090 CHECKSUM (at->dw_attr_val.v.val_vec);
9091 break;
9093 case dw_val_class_flag:
9094 CHECKSUM_ULEB128 (DW_FORM_flag);
9095 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9096 break;
9098 case dw_val_class_str:
9099 CHECKSUM_ULEB128 (DW_FORM_string);
9100 CHECKSUM_STRING (AT_string (at));
9101 break;
9103 case dw_val_class_addr:
9104 r = AT_addr (at);
9105 gcc_assert (GET_CODE (r) == SYMBOL_REF);
9106 CHECKSUM_ULEB128 (DW_FORM_string);
9107 CHECKSUM_STRING (XSTR (r, 0));
9108 break;
9110 case dw_val_class_offset:
9111 CHECKSUM_ULEB128 (DW_FORM_sdata);
9112 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9113 break;
9115 case dw_val_class_loc:
9116 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9117 loc_checksum_ordered (loc, ctx);
9118 break;
9120 case dw_val_class_fde_ref:
9121 case dw_val_class_lbl_id:
9122 case dw_val_class_lineptr:
9123 case dw_val_class_macptr:
9124 break;
9126 case dw_val_class_file:
9127 CHECKSUM_ULEB128 (DW_FORM_string);
9128 CHECKSUM_STRING (AT_file (at)->filename);
9129 break;
9131 case dw_val_class_data8:
9132 CHECKSUM (at->dw_attr_val.v.val_data8);
9133 break;
9135 default:
9136 break;
9140 struct checksum_attributes
9142 dw_attr_ref at_name;
9143 dw_attr_ref at_type;
9144 dw_attr_ref at_friend;
9145 dw_attr_ref at_accessibility;
9146 dw_attr_ref at_address_class;
9147 dw_attr_ref at_allocated;
9148 dw_attr_ref at_artificial;
9149 dw_attr_ref at_associated;
9150 dw_attr_ref at_binary_scale;
9151 dw_attr_ref at_bit_offset;
9152 dw_attr_ref at_bit_size;
9153 dw_attr_ref at_bit_stride;
9154 dw_attr_ref at_byte_size;
9155 dw_attr_ref at_byte_stride;
9156 dw_attr_ref at_const_value;
9157 dw_attr_ref at_containing_type;
9158 dw_attr_ref at_count;
9159 dw_attr_ref at_data_location;
9160 dw_attr_ref at_data_member_location;
9161 dw_attr_ref at_decimal_scale;
9162 dw_attr_ref at_decimal_sign;
9163 dw_attr_ref at_default_value;
9164 dw_attr_ref at_digit_count;
9165 dw_attr_ref at_discr;
9166 dw_attr_ref at_discr_list;
9167 dw_attr_ref at_discr_value;
9168 dw_attr_ref at_encoding;
9169 dw_attr_ref at_endianity;
9170 dw_attr_ref at_explicit;
9171 dw_attr_ref at_is_optional;
9172 dw_attr_ref at_location;
9173 dw_attr_ref at_lower_bound;
9174 dw_attr_ref at_mutable;
9175 dw_attr_ref at_ordering;
9176 dw_attr_ref at_picture_string;
9177 dw_attr_ref at_prototyped;
9178 dw_attr_ref at_small;
9179 dw_attr_ref at_segment;
9180 dw_attr_ref at_string_length;
9181 dw_attr_ref at_threads_scaled;
9182 dw_attr_ref at_upper_bound;
9183 dw_attr_ref at_use_location;
9184 dw_attr_ref at_use_UTF8;
9185 dw_attr_ref at_variable_parameter;
9186 dw_attr_ref at_virtuality;
9187 dw_attr_ref at_visibility;
9188 dw_attr_ref at_vtable_elem_location;
9191 /* Collect the attributes that we will want to use for the checksum. */
9193 static void
9194 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9196 dw_attr_ref a;
9197 unsigned ix;
9199 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9201 switch (a->dw_attr)
9203 case DW_AT_name:
9204 attrs->at_name = a;
9205 break;
9206 case DW_AT_type:
9207 attrs->at_type = a;
9208 break;
9209 case DW_AT_friend:
9210 attrs->at_friend = a;
9211 break;
9212 case DW_AT_accessibility:
9213 attrs->at_accessibility = a;
9214 break;
9215 case DW_AT_address_class:
9216 attrs->at_address_class = a;
9217 break;
9218 case DW_AT_allocated:
9219 attrs->at_allocated = a;
9220 break;
9221 case DW_AT_artificial:
9222 attrs->at_artificial = a;
9223 break;
9224 case DW_AT_associated:
9225 attrs->at_associated = a;
9226 break;
9227 case DW_AT_binary_scale:
9228 attrs->at_binary_scale = a;
9229 break;
9230 case DW_AT_bit_offset:
9231 attrs->at_bit_offset = a;
9232 break;
9233 case DW_AT_bit_size:
9234 attrs->at_bit_size = a;
9235 break;
9236 case DW_AT_bit_stride:
9237 attrs->at_bit_stride = a;
9238 break;
9239 case DW_AT_byte_size:
9240 attrs->at_byte_size = a;
9241 break;
9242 case DW_AT_byte_stride:
9243 attrs->at_byte_stride = a;
9244 break;
9245 case DW_AT_const_value:
9246 attrs->at_const_value = a;
9247 break;
9248 case DW_AT_containing_type:
9249 attrs->at_containing_type = a;
9250 break;
9251 case DW_AT_count:
9252 attrs->at_count = a;
9253 break;
9254 case DW_AT_data_location:
9255 attrs->at_data_location = a;
9256 break;
9257 case DW_AT_data_member_location:
9258 attrs->at_data_member_location = a;
9259 break;
9260 case DW_AT_decimal_scale:
9261 attrs->at_decimal_scale = a;
9262 break;
9263 case DW_AT_decimal_sign:
9264 attrs->at_decimal_sign = a;
9265 break;
9266 case DW_AT_default_value:
9267 attrs->at_default_value = a;
9268 break;
9269 case DW_AT_digit_count:
9270 attrs->at_digit_count = a;
9271 break;
9272 case DW_AT_discr:
9273 attrs->at_discr = a;
9274 break;
9275 case DW_AT_discr_list:
9276 attrs->at_discr_list = a;
9277 break;
9278 case DW_AT_discr_value:
9279 attrs->at_discr_value = a;
9280 break;
9281 case DW_AT_encoding:
9282 attrs->at_encoding = a;
9283 break;
9284 case DW_AT_endianity:
9285 attrs->at_endianity = a;
9286 break;
9287 case DW_AT_explicit:
9288 attrs->at_explicit = a;
9289 break;
9290 case DW_AT_is_optional:
9291 attrs->at_is_optional = a;
9292 break;
9293 case DW_AT_location:
9294 attrs->at_location = a;
9295 break;
9296 case DW_AT_lower_bound:
9297 attrs->at_lower_bound = a;
9298 break;
9299 case DW_AT_mutable:
9300 attrs->at_mutable = a;
9301 break;
9302 case DW_AT_ordering:
9303 attrs->at_ordering = a;
9304 break;
9305 case DW_AT_picture_string:
9306 attrs->at_picture_string = a;
9307 break;
9308 case DW_AT_prototyped:
9309 attrs->at_prototyped = a;
9310 break;
9311 case DW_AT_small:
9312 attrs->at_small = a;
9313 break;
9314 case DW_AT_segment:
9315 attrs->at_segment = a;
9316 break;
9317 case DW_AT_string_length:
9318 attrs->at_string_length = a;
9319 break;
9320 case DW_AT_threads_scaled:
9321 attrs->at_threads_scaled = a;
9322 break;
9323 case DW_AT_upper_bound:
9324 attrs->at_upper_bound = a;
9325 break;
9326 case DW_AT_use_location:
9327 attrs->at_use_location = a;
9328 break;
9329 case DW_AT_use_UTF8:
9330 attrs->at_use_UTF8 = a;
9331 break;
9332 case DW_AT_variable_parameter:
9333 attrs->at_variable_parameter = a;
9334 break;
9335 case DW_AT_virtuality:
9336 attrs->at_virtuality = a;
9337 break;
9338 case DW_AT_visibility:
9339 attrs->at_visibility = a;
9340 break;
9341 case DW_AT_vtable_elem_location:
9342 attrs->at_vtable_elem_location = a;
9343 break;
9344 default:
9345 break;
9350 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
9352 static void
9353 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9355 dw_die_ref c;
9356 dw_die_ref decl;
9357 struct checksum_attributes attrs;
9359 CHECKSUM_ULEB128 ('D');
9360 CHECKSUM_ULEB128 (die->die_tag);
9362 memset (&attrs, 0, sizeof (attrs));
9364 decl = get_AT_ref (die, DW_AT_specification);
9365 if (decl != NULL)
9366 collect_checksum_attributes (&attrs, decl);
9367 collect_checksum_attributes (&attrs, die);
9369 CHECKSUM_ATTR (attrs.at_name);
9370 CHECKSUM_ATTR (attrs.at_accessibility);
9371 CHECKSUM_ATTR (attrs.at_address_class);
9372 CHECKSUM_ATTR (attrs.at_allocated);
9373 CHECKSUM_ATTR (attrs.at_artificial);
9374 CHECKSUM_ATTR (attrs.at_associated);
9375 CHECKSUM_ATTR (attrs.at_binary_scale);
9376 CHECKSUM_ATTR (attrs.at_bit_offset);
9377 CHECKSUM_ATTR (attrs.at_bit_size);
9378 CHECKSUM_ATTR (attrs.at_bit_stride);
9379 CHECKSUM_ATTR (attrs.at_byte_size);
9380 CHECKSUM_ATTR (attrs.at_byte_stride);
9381 CHECKSUM_ATTR (attrs.at_const_value);
9382 CHECKSUM_ATTR (attrs.at_containing_type);
9383 CHECKSUM_ATTR (attrs.at_count);
9384 CHECKSUM_ATTR (attrs.at_data_location);
9385 CHECKSUM_ATTR (attrs.at_data_member_location);
9386 CHECKSUM_ATTR (attrs.at_decimal_scale);
9387 CHECKSUM_ATTR (attrs.at_decimal_sign);
9388 CHECKSUM_ATTR (attrs.at_default_value);
9389 CHECKSUM_ATTR (attrs.at_digit_count);
9390 CHECKSUM_ATTR (attrs.at_discr);
9391 CHECKSUM_ATTR (attrs.at_discr_list);
9392 CHECKSUM_ATTR (attrs.at_discr_value);
9393 CHECKSUM_ATTR (attrs.at_encoding);
9394 CHECKSUM_ATTR (attrs.at_endianity);
9395 CHECKSUM_ATTR (attrs.at_explicit);
9396 CHECKSUM_ATTR (attrs.at_is_optional);
9397 CHECKSUM_ATTR (attrs.at_location);
9398 CHECKSUM_ATTR (attrs.at_lower_bound);
9399 CHECKSUM_ATTR (attrs.at_mutable);
9400 CHECKSUM_ATTR (attrs.at_ordering);
9401 CHECKSUM_ATTR (attrs.at_picture_string);
9402 CHECKSUM_ATTR (attrs.at_prototyped);
9403 CHECKSUM_ATTR (attrs.at_small);
9404 CHECKSUM_ATTR (attrs.at_segment);
9405 CHECKSUM_ATTR (attrs.at_string_length);
9406 CHECKSUM_ATTR (attrs.at_threads_scaled);
9407 CHECKSUM_ATTR (attrs.at_upper_bound);
9408 CHECKSUM_ATTR (attrs.at_use_location);
9409 CHECKSUM_ATTR (attrs.at_use_UTF8);
9410 CHECKSUM_ATTR (attrs.at_variable_parameter);
9411 CHECKSUM_ATTR (attrs.at_virtuality);
9412 CHECKSUM_ATTR (attrs.at_visibility);
9413 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9414 CHECKSUM_ATTR (attrs.at_type);
9415 CHECKSUM_ATTR (attrs.at_friend);
9417 /* Checksum the child DIEs, except for nested types and member functions. */
9418 c = die->die_child;
9419 if (c) do {
9420 dw_attr_ref name_attr;
9422 c = c->die_sib;
9423 name_attr = get_AT (c, DW_AT_name);
9424 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9425 && name_attr != NULL)
9427 CHECKSUM_ULEB128 ('S');
9428 CHECKSUM_ULEB128 (c->die_tag);
9429 CHECKSUM_STRING (AT_string (name_attr));
9431 else
9433 /* Mark this DIE so it gets processed when unmarking. */
9434 if (c->die_mark == 0)
9435 c->die_mark = -1;
9436 die_checksum_ordered (c, ctx, mark);
9438 } while (c != die->die_child);
9440 CHECKSUM_ULEB128 (0);
9443 #undef CHECKSUM
9444 #undef CHECKSUM_STRING
9445 #undef CHECKSUM_ATTR
9446 #undef CHECKSUM_LEB128
9447 #undef CHECKSUM_ULEB128
9449 /* Generate the type signature for DIE. This is computed by generating an
9450 MD5 checksum over the DIE's tag, its relevant attributes, and its
9451 children. Attributes that are references to other DIEs are processed
9452 by recursion, using the MARK field to prevent infinite recursion.
9453 If the DIE is nested inside a namespace or another type, we also
9454 need to include that context in the signature. The lower 64 bits
9455 of the resulting MD5 checksum comprise the signature. */
9457 static void
9458 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9460 int mark;
9461 const char *name;
9462 unsigned char checksum[16];
9463 struct md5_ctx ctx;
9464 dw_die_ref decl;
9466 name = get_AT_string (die, DW_AT_name);
9467 decl = get_AT_ref (die, DW_AT_specification);
9469 /* First, compute a signature for just the type name (and its surrounding
9470 context, if any. This is stored in the type unit DIE for link-time
9471 ODR (one-definition rule) checking. */
9473 if (is_cxx() && name != NULL)
9475 md5_init_ctx (&ctx);
9477 /* Checksum the names of surrounding namespaces and structures. */
9478 if (decl != NULL && decl->die_parent != NULL)
9479 checksum_die_context (decl->die_parent, &ctx);
9481 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9482 md5_process_bytes (name, strlen (name) + 1, &ctx);
9483 md5_finish_ctx (&ctx, checksum);
9485 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9488 /* Next, compute the complete type signature. */
9490 md5_init_ctx (&ctx);
9491 mark = 1;
9492 die->die_mark = mark;
9494 /* Checksum the names of surrounding namespaces and structures. */
9495 if (decl != NULL && decl->die_parent != NULL)
9496 checksum_die_context (decl->die_parent, &ctx);
9498 /* Checksum the DIE and its children. */
9499 die_checksum_ordered (die, &ctx, &mark);
9500 unmark_all_dies (die);
9501 md5_finish_ctx (&ctx, checksum);
9503 /* Store the signature in the type node and link the type DIE and the
9504 type node together. */
9505 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9506 DWARF_TYPE_SIGNATURE_SIZE);
9507 die->die_id.die_type_node = type_node;
9508 type_node->type_die = die;
9510 /* If the DIE is a specification, link its declaration to the type node
9511 as well. */
9512 if (decl != NULL)
9513 decl->die_id.die_type_node = type_node;
9516 /* Do the location expressions look same? */
9517 static inline int
9518 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9520 return loc1->dw_loc_opc == loc2->dw_loc_opc
9521 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9522 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9525 /* Do the values look the same? */
9526 static int
9527 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9529 dw_loc_descr_ref loc1, loc2;
9530 rtx r1, r2;
9532 if (v1->val_class != v2->val_class)
9533 return 0;
9535 switch (v1->val_class)
9537 case dw_val_class_const:
9538 return v1->v.val_int == v2->v.val_int;
9539 case dw_val_class_unsigned_const:
9540 return v1->v.val_unsigned == v2->v.val_unsigned;
9541 case dw_val_class_const_double:
9542 return v1->v.val_double.high == v2->v.val_double.high
9543 && v1->v.val_double.low == v2->v.val_double.low;
9544 case dw_val_class_vec:
9545 if (v1->v.val_vec.length != v2->v.val_vec.length
9546 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9547 return 0;
9548 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9549 v1->v.val_vec.length * v1->v.val_vec.elt_size))
9550 return 0;
9551 return 1;
9552 case dw_val_class_flag:
9553 return v1->v.val_flag == v2->v.val_flag;
9554 case dw_val_class_str:
9555 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9557 case dw_val_class_addr:
9558 r1 = v1->v.val_addr;
9559 r2 = v2->v.val_addr;
9560 if (GET_CODE (r1) != GET_CODE (r2))
9561 return 0;
9562 return !rtx_equal_p (r1, r2);
9564 case dw_val_class_offset:
9565 return v1->v.val_offset == v2->v.val_offset;
9567 case dw_val_class_loc:
9568 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9569 loc1 && loc2;
9570 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9571 if (!same_loc_p (loc1, loc2, mark))
9572 return 0;
9573 return !loc1 && !loc2;
9575 case dw_val_class_die_ref:
9576 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9578 case dw_val_class_fde_ref:
9579 case dw_val_class_vms_delta:
9580 case dw_val_class_lbl_id:
9581 case dw_val_class_lineptr:
9582 case dw_val_class_macptr:
9583 return 1;
9585 case dw_val_class_file:
9586 return v1->v.val_file == v2->v.val_file;
9588 case dw_val_class_data8:
9589 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9591 default:
9592 return 1;
9596 /* Do the attributes look the same? */
9598 static int
9599 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9601 if (at1->dw_attr != at2->dw_attr)
9602 return 0;
9604 /* We don't care that this was compiled with a different compiler
9605 snapshot; if the output is the same, that's what matters. */
9606 if (at1->dw_attr == DW_AT_producer)
9607 return 1;
9609 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9612 /* Do the dies look the same? */
9614 static int
9615 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9617 dw_die_ref c1, c2;
9618 dw_attr_ref a1;
9619 unsigned ix;
9621 /* To avoid infinite recursion. */
9622 if (die1->die_mark)
9623 return die1->die_mark == die2->die_mark;
9624 die1->die_mark = die2->die_mark = ++(*mark);
9626 if (die1->die_tag != die2->die_tag)
9627 return 0;
9629 if (VEC_length (dw_attr_node, die1->die_attr)
9630 != VEC_length (dw_attr_node, die2->die_attr))
9631 return 0;
9633 FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9634 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9635 return 0;
9637 c1 = die1->die_child;
9638 c2 = die2->die_child;
9639 if (! c1)
9641 if (c2)
9642 return 0;
9644 else
9645 for (;;)
9647 if (!same_die_p (c1, c2, mark))
9648 return 0;
9649 c1 = c1->die_sib;
9650 c2 = c2->die_sib;
9651 if (c1 == die1->die_child)
9653 if (c2 == die2->die_child)
9654 break;
9655 else
9656 return 0;
9660 return 1;
9663 /* Do the dies look the same? Wrapper around same_die_p. */
9665 static int
9666 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9668 int mark = 0;
9669 int ret = same_die_p (die1, die2, &mark);
9671 unmark_all_dies (die1);
9672 unmark_all_dies (die2);
9674 return ret;
9677 /* The prefix to attach to symbols on DIEs in the current comdat debug
9678 info section. */
9679 static char *comdat_symbol_id;
9681 /* The index of the current symbol within the current comdat CU. */
9682 static unsigned int comdat_symbol_number;
9684 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9685 children, and set comdat_symbol_id accordingly. */
9687 static void
9688 compute_section_prefix (dw_die_ref unit_die)
9690 const char *die_name = get_AT_string (unit_die, DW_AT_name);
9691 const char *base = die_name ? lbasename (die_name) : "anonymous";
9692 char *name = XALLOCAVEC (char, strlen (base) + 64);
9693 char *p;
9694 int i, mark;
9695 unsigned char checksum[16];
9696 struct md5_ctx ctx;
9698 /* Compute the checksum of the DIE, then append part of it as hex digits to
9699 the name filename of the unit. */
9701 md5_init_ctx (&ctx);
9702 mark = 0;
9703 die_checksum (unit_die, &ctx, &mark);
9704 unmark_all_dies (unit_die);
9705 md5_finish_ctx (&ctx, checksum);
9707 sprintf (name, "%s.", base);
9708 clean_symbol_name (name);
9710 p = name + strlen (name);
9711 for (i = 0; i < 4; i++)
9713 sprintf (p, "%.2x", checksum[i]);
9714 p += 2;
9717 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9718 comdat_symbol_number = 0;
9721 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
9723 static int
9724 is_type_die (dw_die_ref die)
9726 switch (die->die_tag)
9728 case DW_TAG_array_type:
9729 case DW_TAG_class_type:
9730 case DW_TAG_interface_type:
9731 case DW_TAG_enumeration_type:
9732 case DW_TAG_pointer_type:
9733 case DW_TAG_reference_type:
9734 case DW_TAG_rvalue_reference_type:
9735 case DW_TAG_string_type:
9736 case DW_TAG_structure_type:
9737 case DW_TAG_subroutine_type:
9738 case DW_TAG_union_type:
9739 case DW_TAG_ptr_to_member_type:
9740 case DW_TAG_set_type:
9741 case DW_TAG_subrange_type:
9742 case DW_TAG_base_type:
9743 case DW_TAG_const_type:
9744 case DW_TAG_file_type:
9745 case DW_TAG_packed_type:
9746 case DW_TAG_volatile_type:
9747 case DW_TAG_typedef:
9748 return 1;
9749 default:
9750 return 0;
9754 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9755 Basically, we want to choose the bits that are likely to be shared between
9756 compilations (types) and leave out the bits that are specific to individual
9757 compilations (functions). */
9759 static int
9760 is_comdat_die (dw_die_ref c)
9762 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9763 we do for stabs. The advantage is a greater likelihood of sharing between
9764 objects that don't include headers in the same order (and therefore would
9765 put the base types in a different comdat). jason 8/28/00 */
9767 if (c->die_tag == DW_TAG_base_type)
9768 return 0;
9770 if (c->die_tag == DW_TAG_pointer_type
9771 || c->die_tag == DW_TAG_reference_type
9772 || c->die_tag == DW_TAG_rvalue_reference_type
9773 || c->die_tag == DW_TAG_const_type
9774 || c->die_tag == DW_TAG_volatile_type)
9776 dw_die_ref t = get_AT_ref (c, DW_AT_type);
9778 return t ? is_comdat_die (t) : 0;
9781 return is_type_die (c);
9784 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9785 compilation unit. */
9787 static int
9788 is_symbol_die (dw_die_ref c)
9790 return (is_type_die (c)
9791 || is_declaration_die (c)
9792 || c->die_tag == DW_TAG_namespace
9793 || c->die_tag == DW_TAG_module);
9796 /* Returns true iff C is a compile-unit DIE. */
9798 static inline bool
9799 is_cu_die (dw_die_ref c)
9801 return c && c->die_tag == DW_TAG_compile_unit;
9804 static char *
9805 gen_internal_sym (const char *prefix)
9807 char buf[256];
9809 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9810 return xstrdup (buf);
9813 /* Assign symbols to all worthy DIEs under DIE. */
9815 static void
9816 assign_symbol_names (dw_die_ref die)
9818 dw_die_ref c;
9820 if (is_symbol_die (die))
9822 if (comdat_symbol_id)
9824 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9826 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9827 comdat_symbol_id, comdat_symbol_number++);
9828 die->die_id.die_symbol = xstrdup (p);
9830 else
9831 die->die_id.die_symbol = gen_internal_sym ("LDIE");
9834 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9837 struct cu_hash_table_entry
9839 dw_die_ref cu;
9840 unsigned min_comdat_num, max_comdat_num;
9841 struct cu_hash_table_entry *next;
9844 /* Routines to manipulate hash table of CUs. */
9845 static hashval_t
9846 htab_cu_hash (const void *of)
9848 const struct cu_hash_table_entry *const entry =
9849 (const struct cu_hash_table_entry *) of;
9851 return htab_hash_string (entry->cu->die_id.die_symbol);
9854 static int
9855 htab_cu_eq (const void *of1, const void *of2)
9857 const struct cu_hash_table_entry *const entry1 =
9858 (const struct cu_hash_table_entry *) of1;
9859 const struct die_struct *const entry2 = (const struct die_struct *) of2;
9861 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9864 static void
9865 htab_cu_del (void *what)
9867 struct cu_hash_table_entry *next,
9868 *entry = (struct cu_hash_table_entry *) what;
9870 while (entry)
9872 next = entry->next;
9873 free (entry);
9874 entry = next;
9878 /* Check whether we have already seen this CU and set up SYM_NUM
9879 accordingly. */
9880 static int
9881 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9883 struct cu_hash_table_entry dummy;
9884 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9886 dummy.max_comdat_num = 0;
9888 slot = (struct cu_hash_table_entry **)
9889 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9890 INSERT);
9891 entry = *slot;
9893 for (; entry; last = entry, entry = entry->next)
9895 if (same_die_p_wrap (cu, entry->cu))
9896 break;
9899 if (entry)
9901 *sym_num = entry->min_comdat_num;
9902 return 1;
9905 entry = XCNEW (struct cu_hash_table_entry);
9906 entry->cu = cu;
9907 entry->min_comdat_num = *sym_num = last->max_comdat_num;
9908 entry->next = *slot;
9909 *slot = entry;
9911 return 0;
9914 /* Record SYM_NUM to record of CU in HTABLE. */
9915 static void
9916 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9918 struct cu_hash_table_entry **slot, *entry;
9920 slot = (struct cu_hash_table_entry **)
9921 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9922 NO_INSERT);
9923 entry = *slot;
9925 entry->max_comdat_num = sym_num;
9928 /* Traverse the DIE (which is always comp_unit_die), and set up
9929 additional compilation units for each of the include files we see
9930 bracketed by BINCL/EINCL. */
9932 static void
9933 break_out_includes (dw_die_ref die)
9935 dw_die_ref c;
9936 dw_die_ref unit = NULL;
9937 limbo_die_node *node, **pnode;
9938 htab_t cu_hash_table;
9940 c = die->die_child;
9941 if (c) do {
9942 dw_die_ref prev = c;
9943 c = c->die_sib;
9944 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9945 || (unit && is_comdat_die (c)))
9947 dw_die_ref next = c->die_sib;
9949 /* This DIE is for a secondary CU; remove it from the main one. */
9950 remove_child_with_prev (c, prev);
9952 if (c->die_tag == DW_TAG_GNU_BINCL)
9953 unit = push_new_compile_unit (unit, c);
9954 else if (c->die_tag == DW_TAG_GNU_EINCL)
9955 unit = pop_compile_unit (unit);
9956 else
9957 add_child_die (unit, c);
9958 c = next;
9959 if (c == die->die_child)
9960 break;
9962 } while (c != die->die_child);
9964 #if 0
9965 /* We can only use this in debugging, since the frontend doesn't check
9966 to make sure that we leave every include file we enter. */
9967 gcc_assert (!unit);
9968 #endif
9970 assign_symbol_names (die);
9971 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9972 for (node = limbo_die_list, pnode = &limbo_die_list;
9973 node;
9974 node = node->next)
9976 int is_dupl;
9978 compute_section_prefix (node->die);
9979 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9980 &comdat_symbol_number);
9981 assign_symbol_names (node->die);
9982 if (is_dupl)
9983 *pnode = node->next;
9984 else
9986 pnode = &node->next;
9987 record_comdat_symbol_number (node->die, cu_hash_table,
9988 comdat_symbol_number);
9991 htab_delete (cu_hash_table);
9994 /* Return non-zero if this DIE is a declaration. */
9996 static int
9997 is_declaration_die (dw_die_ref die)
9999 dw_attr_ref a;
10000 unsigned ix;
10002 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10003 if (a->dw_attr == DW_AT_declaration)
10004 return 1;
10006 return 0;
10009 /* Return non-zero if this DIE is nested inside a subprogram. */
10011 static int
10012 is_nested_in_subprogram (dw_die_ref die)
10014 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
10016 if (decl == NULL)
10017 decl = die;
10018 return local_scope_p (decl);
10021 /* Return non-zero if this is a type DIE that should be moved to a
10022 COMDAT .debug_types section. */
10024 static int
10025 should_move_die_to_comdat (dw_die_ref die)
10027 switch (die->die_tag)
10029 case DW_TAG_class_type:
10030 case DW_TAG_structure_type:
10031 case DW_TAG_enumeration_type:
10032 case DW_TAG_union_type:
10033 /* Don't move declarations, inlined instances, or types nested in a
10034 subprogram. */
10035 if (is_declaration_die (die)
10036 || get_AT (die, DW_AT_abstract_origin)
10037 || is_nested_in_subprogram (die))
10038 return 0;
10039 return 1;
10040 case DW_TAG_array_type:
10041 case DW_TAG_interface_type:
10042 case DW_TAG_pointer_type:
10043 case DW_TAG_reference_type:
10044 case DW_TAG_rvalue_reference_type:
10045 case DW_TAG_string_type:
10046 case DW_TAG_subroutine_type:
10047 case DW_TAG_ptr_to_member_type:
10048 case DW_TAG_set_type:
10049 case DW_TAG_subrange_type:
10050 case DW_TAG_base_type:
10051 case DW_TAG_const_type:
10052 case DW_TAG_file_type:
10053 case DW_TAG_packed_type:
10054 case DW_TAG_volatile_type:
10055 case DW_TAG_typedef:
10056 default:
10057 return 0;
10061 /* Make a clone of DIE. */
10063 static dw_die_ref
10064 clone_die (dw_die_ref die)
10066 dw_die_ref clone;
10067 dw_attr_ref a;
10068 unsigned ix;
10070 clone = ggc_alloc_cleared_die_node ();
10071 clone->die_tag = die->die_tag;
10073 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10074 add_dwarf_attr (clone, a);
10076 return clone;
10079 /* Make a clone of the tree rooted at DIE. */
10081 static dw_die_ref
10082 clone_tree (dw_die_ref die)
10084 dw_die_ref c;
10085 dw_die_ref clone = clone_die (die);
10087 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10089 return clone;
10092 /* Make a clone of DIE as a declaration. */
10094 static dw_die_ref
10095 clone_as_declaration (dw_die_ref die)
10097 dw_die_ref clone;
10098 dw_die_ref decl;
10099 dw_attr_ref a;
10100 unsigned ix;
10102 /* If the DIE is already a declaration, just clone it. */
10103 if (is_declaration_die (die))
10104 return clone_die (die);
10106 /* If the DIE is a specification, just clone its declaration DIE. */
10107 decl = get_AT_ref (die, DW_AT_specification);
10108 if (decl != NULL)
10109 return clone_die (decl);
10111 clone = ggc_alloc_cleared_die_node ();
10112 clone->die_tag = die->die_tag;
10114 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10116 /* We don't want to copy over all attributes.
10117 For example we don't want DW_AT_byte_size because otherwise we will no
10118 longer have a declaration and GDB will treat it as a definition. */
10120 switch (a->dw_attr)
10122 case DW_AT_artificial:
10123 case DW_AT_containing_type:
10124 case DW_AT_external:
10125 case DW_AT_name:
10126 case DW_AT_type:
10127 case DW_AT_virtuality:
10128 case DW_AT_linkage_name:
10129 case DW_AT_MIPS_linkage_name:
10130 add_dwarf_attr (clone, a);
10131 break;
10132 case DW_AT_byte_size:
10133 default:
10134 break;
10138 if (die->die_id.die_type_node)
10139 add_AT_die_ref (clone, DW_AT_signature, die);
10141 add_AT_flag (clone, DW_AT_declaration, 1);
10142 return clone;
10145 /* Copy the declaration context to the new compile unit DIE. This includes
10146 any surrounding namespace or type declarations. If the DIE has an
10147 AT_specification attribute, it also includes attributes and children
10148 attached to the specification. */
10150 static void
10151 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10153 dw_die_ref decl;
10154 dw_die_ref new_decl;
10156 decl = get_AT_ref (die, DW_AT_specification);
10157 if (decl == NULL)
10158 decl = die;
10159 else
10161 unsigned ix;
10162 dw_die_ref c;
10163 dw_attr_ref a;
10165 /* Copy the type node pointer from the new DIE to the original
10166 declaration DIE so we can forward references later. */
10167 decl->die_id.die_type_node = die->die_id.die_type_node;
10169 remove_AT (die, DW_AT_specification);
10171 FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10173 if (a->dw_attr != DW_AT_name
10174 && a->dw_attr != DW_AT_declaration
10175 && a->dw_attr != DW_AT_external)
10176 add_dwarf_attr (die, a);
10179 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10182 if (decl->die_parent != NULL
10183 && decl->die_parent->die_tag != DW_TAG_compile_unit
10184 && decl->die_parent->die_tag != DW_TAG_type_unit)
10186 new_decl = copy_ancestor_tree (unit, decl, NULL);
10187 if (new_decl != NULL)
10189 remove_AT (new_decl, DW_AT_signature);
10190 add_AT_specification (die, new_decl);
10195 /* Generate the skeleton ancestor tree for the given NODE, then clone
10196 the DIE and add the clone into the tree. */
10198 static void
10199 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10201 if (node->new_die != NULL)
10202 return;
10204 node->new_die = clone_as_declaration (node->old_die);
10206 if (node->parent != NULL)
10208 generate_skeleton_ancestor_tree (node->parent);
10209 add_child_die (node->parent->new_die, node->new_die);
10213 /* Generate a skeleton tree of DIEs containing any declarations that are
10214 found in the original tree. We traverse the tree looking for declaration
10215 DIEs, and construct the skeleton from the bottom up whenever we find one. */
10217 static void
10218 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10220 skeleton_chain_node node;
10221 dw_die_ref c;
10222 dw_die_ref first;
10223 dw_die_ref prev = NULL;
10224 dw_die_ref next = NULL;
10226 node.parent = parent;
10228 first = c = parent->old_die->die_child;
10229 if (c)
10230 next = c->die_sib;
10231 if (c) do {
10232 if (prev == NULL || prev->die_sib == c)
10233 prev = c;
10234 c = next;
10235 next = (c == first ? NULL : c->die_sib);
10236 node.old_die = c;
10237 node.new_die = NULL;
10238 if (is_declaration_die (c))
10240 /* Clone the existing DIE, move the original to the skeleton
10241 tree (which is in the main CU), and put the clone, with
10242 all the original's children, where the original came from. */
10243 dw_die_ref clone = clone_die (c);
10244 move_all_children (c, clone);
10246 replace_child (c, clone, prev);
10247 generate_skeleton_ancestor_tree (parent);
10248 add_child_die (parent->new_die, c);
10249 node.new_die = c;
10250 c = clone;
10252 generate_skeleton_bottom_up (&node);
10253 } while (next != NULL);
10256 /* Wrapper function for generate_skeleton_bottom_up. */
10258 static dw_die_ref
10259 generate_skeleton (dw_die_ref die)
10261 skeleton_chain_node node;
10263 node.old_die = die;
10264 node.new_die = NULL;
10265 node.parent = NULL;
10267 /* If this type definition is nested inside another type,
10268 always leave at least a declaration in its place. */
10269 if (die->die_parent != NULL && is_type_die (die->die_parent))
10270 node.new_die = clone_as_declaration (die);
10272 generate_skeleton_bottom_up (&node);
10273 return node.new_die;
10276 /* Remove the DIE from its parent, possibly replacing it with a cloned
10277 declaration. The original DIE will be moved to a new compile unit
10278 so that existing references to it follow it to the new location. If
10279 any of the original DIE's descendants is a declaration, we need to
10280 replace the original DIE with a skeleton tree and move the
10281 declarations back into the skeleton tree. */
10283 static dw_die_ref
10284 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10286 dw_die_ref skeleton;
10288 skeleton = generate_skeleton (child);
10289 if (skeleton == NULL)
10290 remove_child_with_prev (child, prev);
10291 else
10293 skeleton->die_id.die_type_node = child->die_id.die_type_node;
10294 replace_child (child, skeleton, prev);
10297 return skeleton;
10300 /* Traverse the DIE and set up additional .debug_types sections for each
10301 type worthy of being placed in a COMDAT section. */
10303 static void
10304 break_out_comdat_types (dw_die_ref die)
10306 dw_die_ref c;
10307 dw_die_ref first;
10308 dw_die_ref prev = NULL;
10309 dw_die_ref next = NULL;
10310 dw_die_ref unit = NULL;
10312 first = c = die->die_child;
10313 if (c)
10314 next = c->die_sib;
10315 if (c) do {
10316 if (prev == NULL || prev->die_sib == c)
10317 prev = c;
10318 c = next;
10319 next = (c == first ? NULL : c->die_sib);
10320 if (should_move_die_to_comdat (c))
10322 dw_die_ref replacement;
10323 comdat_type_node_ref type_node;
10325 /* Create a new type unit DIE as the root for the new tree, and
10326 add it to the list of comdat types. */
10327 unit = new_die (DW_TAG_type_unit, NULL, NULL);
10328 add_AT_unsigned (unit, DW_AT_language,
10329 get_AT_unsigned (comp_unit_die (), DW_AT_language));
10330 type_node = ggc_alloc_cleared_comdat_type_node ();
10331 type_node->root_die = unit;
10332 type_node->next = comdat_type_list;
10333 comdat_type_list = type_node;
10335 /* Generate the type signature. */
10336 generate_type_signature (c, type_node);
10338 /* Copy the declaration context, attributes, and children of the
10339 declaration into the new compile unit DIE. */
10340 copy_declaration_context (unit, c);
10342 /* Remove this DIE from the main CU. */
10343 replacement = remove_child_or_replace_with_skeleton (c, prev);
10345 /* Break out nested types into their own type units. */
10346 break_out_comdat_types (c);
10348 /* Add the DIE to the new compunit. */
10349 add_child_die (unit, c);
10351 if (replacement != NULL)
10352 c = replacement;
10354 else if (c->die_tag == DW_TAG_namespace
10355 || c->die_tag == DW_TAG_class_type
10356 || c->die_tag == DW_TAG_structure_type
10357 || c->die_tag == DW_TAG_union_type)
10359 /* Look for nested types that can be broken out. */
10360 break_out_comdat_types (c);
10362 } while (next != NULL);
10365 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
10367 struct decl_table_entry
10369 dw_die_ref orig;
10370 dw_die_ref copy;
10373 /* Routines to manipulate hash table of copied declarations. */
10375 static hashval_t
10376 htab_decl_hash (const void *of)
10378 const struct decl_table_entry *const entry =
10379 (const struct decl_table_entry *) of;
10381 return htab_hash_pointer (entry->orig);
10384 static int
10385 htab_decl_eq (const void *of1, const void *of2)
10387 const struct decl_table_entry *const entry1 =
10388 (const struct decl_table_entry *) of1;
10389 const struct die_struct *const entry2 = (const struct die_struct *) of2;
10391 return entry1->orig == entry2;
10394 static void
10395 htab_decl_del (void *what)
10397 struct decl_table_entry *entry = (struct decl_table_entry *) what;
10399 free (entry);
10402 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10403 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
10404 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
10405 to check if the ancestor has already been copied into UNIT. */
10407 static dw_die_ref
10408 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10410 dw_die_ref parent = die->die_parent;
10411 dw_die_ref new_parent = unit;
10412 dw_die_ref copy;
10413 void **slot = NULL;
10414 struct decl_table_entry *entry = NULL;
10416 if (decl_table)
10418 /* Check if the entry has already been copied to UNIT. */
10419 slot = htab_find_slot_with_hash (decl_table, die,
10420 htab_hash_pointer (die), INSERT);
10421 if (*slot != HTAB_EMPTY_ENTRY)
10423 entry = (struct decl_table_entry *) *slot;
10424 return entry->copy;
10427 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
10428 entry = XCNEW (struct decl_table_entry);
10429 entry->orig = die;
10430 entry->copy = NULL;
10431 *slot = entry;
10434 if (parent != NULL)
10436 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10437 if (spec != NULL)
10438 parent = spec;
10439 if (parent->die_tag != DW_TAG_compile_unit
10440 && parent->die_tag != DW_TAG_type_unit)
10441 new_parent = copy_ancestor_tree (unit, parent, decl_table);
10444 copy = clone_as_declaration (die);
10445 add_child_die (new_parent, copy);
10447 if (decl_table != NULL)
10449 /* Record the pointer to the copy. */
10450 entry->copy = copy;
10453 return copy;
10456 /* Walk the DIE and its children, looking for references to incomplete
10457 or trivial types that are unmarked (i.e., that are not in the current
10458 type_unit). */
10460 static void
10461 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10463 dw_die_ref c;
10464 dw_attr_ref a;
10465 unsigned ix;
10467 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10469 if (AT_class (a) == dw_val_class_die_ref)
10471 dw_die_ref targ = AT_ref (a);
10472 comdat_type_node_ref type_node = targ->die_id.die_type_node;
10473 void **slot;
10474 struct decl_table_entry *entry;
10476 if (targ->die_mark != 0 || type_node != NULL)
10477 continue;
10479 slot = htab_find_slot_with_hash (decl_table, targ,
10480 htab_hash_pointer (targ), INSERT);
10482 if (*slot != HTAB_EMPTY_ENTRY)
10484 /* TARG has already been copied, so we just need to
10485 modify the reference to point to the copy. */
10486 entry = (struct decl_table_entry *) *slot;
10487 a->dw_attr_val.v.val_die_ref.die = entry->copy;
10489 else
10491 dw_die_ref parent = unit;
10492 dw_die_ref copy = clone_tree (targ);
10494 /* Make sure the cloned tree is marked as part of the
10495 type unit. */
10496 mark_dies (copy);
10498 /* Record in DECL_TABLE that TARG has been copied.
10499 Need to do this now, before the recursive call,
10500 because DECL_TABLE may be expanded and SLOT
10501 would no longer be a valid pointer. */
10502 entry = XCNEW (struct decl_table_entry);
10503 entry->orig = targ;
10504 entry->copy = copy;
10505 *slot = entry;
10507 /* If TARG has surrounding context, copy its ancestor tree
10508 into the new type unit. */
10509 if (targ->die_parent != NULL
10510 && targ->die_parent->die_tag != DW_TAG_compile_unit
10511 && targ->die_parent->die_tag != DW_TAG_type_unit)
10512 parent = copy_ancestor_tree (unit, targ->die_parent,
10513 decl_table);
10515 add_child_die (parent, copy);
10516 a->dw_attr_val.v.val_die_ref.die = copy;
10518 /* Make sure the newly-copied DIE is walked. If it was
10519 installed in a previously-added context, it won't
10520 get visited otherwise. */
10521 if (parent != unit)
10523 /* Find the highest point of the newly-added tree,
10524 mark each node along the way, and walk from there. */
10525 parent->die_mark = 1;
10526 while (parent->die_parent
10527 && parent->die_parent->die_mark == 0)
10529 parent = parent->die_parent;
10530 parent->die_mark = 1;
10532 copy_decls_walk (unit, parent, decl_table);
10538 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10541 /* Copy declarations for "unworthy" types into the new comdat section.
10542 Incomplete types, modified types, and certain other types aren't broken
10543 out into comdat sections of their own, so they don't have a signature,
10544 and we need to copy the declaration into the same section so that we
10545 don't have an external reference. */
10547 static void
10548 copy_decls_for_unworthy_types (dw_die_ref unit)
10550 htab_t decl_table;
10552 mark_dies (unit);
10553 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10554 copy_decls_walk (unit, unit, decl_table);
10555 htab_delete (decl_table);
10556 unmark_dies (unit);
10559 /* Traverse the DIE and add a sibling attribute if it may have the
10560 effect of speeding up access to siblings. To save some space,
10561 avoid generating sibling attributes for DIE's without children. */
10563 static void
10564 add_sibling_attributes (dw_die_ref die)
10566 dw_die_ref c;
10568 if (! die->die_child)
10569 return;
10571 if (die->die_parent && die != die->die_parent->die_child)
10572 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10574 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10577 /* Output all location lists for the DIE and its children. */
10579 static void
10580 output_location_lists (dw_die_ref die)
10582 dw_die_ref c;
10583 dw_attr_ref a;
10584 unsigned ix;
10586 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10587 if (AT_class (a) == dw_val_class_loc_list)
10588 output_loc_list (AT_loc_list (a));
10590 FOR_EACH_CHILD (die, c, output_location_lists (c));
10593 /* The format of each DIE (and its attribute value pairs) is encoded in an
10594 abbreviation table. This routine builds the abbreviation table and assigns
10595 a unique abbreviation id for each abbreviation entry. The children of each
10596 die are visited recursively. */
10598 static void
10599 build_abbrev_table (dw_die_ref die)
10601 unsigned long abbrev_id;
10602 unsigned int n_alloc;
10603 dw_die_ref c;
10604 dw_attr_ref a;
10605 unsigned ix;
10607 /* Scan the DIE references, and mark as external any that refer to
10608 DIEs from other CUs (i.e. those which are not marked). */
10609 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10610 if (AT_class (a) == dw_val_class_die_ref
10611 && AT_ref (a)->die_mark == 0)
10613 gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
10614 set_AT_ref_external (a, 1);
10617 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10619 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10620 dw_attr_ref die_a, abbrev_a;
10621 unsigned ix;
10622 bool ok = true;
10624 if (abbrev->die_tag != die->die_tag)
10625 continue;
10626 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10627 continue;
10629 if (VEC_length (dw_attr_node, abbrev->die_attr)
10630 != VEC_length (dw_attr_node, die->die_attr))
10631 continue;
10633 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10635 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10636 if ((abbrev_a->dw_attr != die_a->dw_attr)
10637 || (value_format (abbrev_a) != value_format (die_a)))
10639 ok = false;
10640 break;
10643 if (ok)
10644 break;
10647 if (abbrev_id >= abbrev_die_table_in_use)
10649 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10651 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10652 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10653 n_alloc);
10655 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10656 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10657 abbrev_die_table_allocated = n_alloc;
10660 ++abbrev_die_table_in_use;
10661 abbrev_die_table[abbrev_id] = die;
10664 die->die_abbrev = abbrev_id;
10665 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10668 /* Return the power-of-two number of bytes necessary to represent VALUE. */
10670 static int
10671 constant_size (unsigned HOST_WIDE_INT value)
10673 int log;
10675 if (value == 0)
10676 log = 0;
10677 else
10678 log = floor_log2 (value);
10680 log = log / 8;
10681 log = 1 << (floor_log2 (log) + 1);
10683 return log;
10686 /* Return the size of a DIE as it is represented in the
10687 .debug_info section. */
10689 static unsigned long
10690 size_of_die (dw_die_ref die)
10692 unsigned long size = 0;
10693 dw_attr_ref a;
10694 unsigned ix;
10696 size += size_of_uleb128 (die->die_abbrev);
10697 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10699 switch (AT_class (a))
10701 case dw_val_class_addr:
10702 size += DWARF2_ADDR_SIZE;
10703 break;
10704 case dw_val_class_offset:
10705 size += DWARF_OFFSET_SIZE;
10706 break;
10707 case dw_val_class_loc:
10709 unsigned long lsize = size_of_locs (AT_loc (a));
10711 /* Block length. */
10712 if (dwarf_version >= 4)
10713 size += size_of_uleb128 (lsize);
10714 else
10715 size += constant_size (lsize);
10716 size += lsize;
10718 break;
10719 case dw_val_class_loc_list:
10720 size += DWARF_OFFSET_SIZE;
10721 break;
10722 case dw_val_class_range_list:
10723 size += DWARF_OFFSET_SIZE;
10724 break;
10725 case dw_val_class_const:
10726 size += size_of_sleb128 (AT_int (a));
10727 break;
10728 case dw_val_class_unsigned_const:
10729 size += constant_size (AT_unsigned (a));
10730 break;
10731 case dw_val_class_const_double:
10732 size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10733 if (HOST_BITS_PER_WIDE_INT >= 64)
10734 size++; /* block */
10735 break;
10736 case dw_val_class_vec:
10737 size += constant_size (a->dw_attr_val.v.val_vec.length
10738 * a->dw_attr_val.v.val_vec.elt_size)
10739 + a->dw_attr_val.v.val_vec.length
10740 * a->dw_attr_val.v.val_vec.elt_size; /* block */
10741 break;
10742 case dw_val_class_flag:
10743 if (dwarf_version >= 4)
10744 /* Currently all add_AT_flag calls pass in 1 as last argument,
10745 so DW_FORM_flag_present can be used. If that ever changes,
10746 we'll need to use DW_FORM_flag and have some optimization
10747 in build_abbrev_table that will change those to
10748 DW_FORM_flag_present if it is set to 1 in all DIEs using
10749 the same abbrev entry. */
10750 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10751 else
10752 size += 1;
10753 break;
10754 case dw_val_class_die_ref:
10755 if (AT_ref_external (a))
10757 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
10758 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
10759 is sized by target address length, whereas in DWARF3
10760 it's always sized as an offset. */
10761 if (use_debug_types)
10762 size += DWARF_TYPE_SIGNATURE_SIZE;
10763 else if (dwarf_version == 2)
10764 size += DWARF2_ADDR_SIZE;
10765 else
10766 size += DWARF_OFFSET_SIZE;
10768 else
10769 size += DWARF_OFFSET_SIZE;
10770 break;
10771 case dw_val_class_fde_ref:
10772 size += DWARF_OFFSET_SIZE;
10773 break;
10774 case dw_val_class_lbl_id:
10775 size += DWARF2_ADDR_SIZE;
10776 break;
10777 case dw_val_class_lineptr:
10778 case dw_val_class_macptr:
10779 size += DWARF_OFFSET_SIZE;
10780 break;
10781 case dw_val_class_str:
10782 if (AT_string_form (a) == DW_FORM_strp)
10783 size += DWARF_OFFSET_SIZE;
10784 else
10785 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10786 break;
10787 case dw_val_class_file:
10788 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10789 break;
10790 case dw_val_class_data8:
10791 size += 8;
10792 break;
10793 case dw_val_class_vms_delta:
10794 size += DWARF_OFFSET_SIZE;
10795 break;
10796 default:
10797 gcc_unreachable ();
10801 return size;
10804 /* Size the debugging information associated with a given DIE. Visits the
10805 DIE's children recursively. Updates the global variable next_die_offset, on
10806 each time through. Uses the current value of next_die_offset to update the
10807 die_offset field in each DIE. */
10809 static void
10810 calc_die_sizes (dw_die_ref die)
10812 dw_die_ref c;
10814 die->die_offset = next_die_offset;
10815 next_die_offset += size_of_die (die);
10817 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10819 if (die->die_child != NULL)
10820 /* Count the null byte used to terminate sibling lists. */
10821 next_die_offset += 1;
10824 /* Set the marks for a die and its children. We do this so
10825 that we know whether or not a reference needs to use FORM_ref_addr; only
10826 DIEs in the same CU will be marked. We used to clear out the offset
10827 and use that as the flag, but ran into ordering problems. */
10829 static void
10830 mark_dies (dw_die_ref die)
10832 dw_die_ref c;
10834 gcc_assert (!die->die_mark);
10836 die->die_mark = 1;
10837 FOR_EACH_CHILD (die, c, mark_dies (c));
10840 /* Clear the marks for a die and its children. */
10842 static void
10843 unmark_dies (dw_die_ref die)
10845 dw_die_ref c;
10847 if (! use_debug_types)
10848 gcc_assert (die->die_mark);
10850 die->die_mark = 0;
10851 FOR_EACH_CHILD (die, c, unmark_dies (c));
10854 /* Clear the marks for a die, its children and referred dies. */
10856 static void
10857 unmark_all_dies (dw_die_ref die)
10859 dw_die_ref c;
10860 dw_attr_ref a;
10861 unsigned ix;
10863 if (!die->die_mark)
10864 return;
10865 die->die_mark = 0;
10867 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10869 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10870 if (AT_class (a) == dw_val_class_die_ref)
10871 unmark_all_dies (AT_ref (a));
10874 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10875 generated for the compilation unit. */
10877 static unsigned long
10878 size_of_pubnames (VEC (pubname_entry, gc) * names)
10880 unsigned long size;
10881 unsigned i;
10882 pubname_ref p;
10884 size = DWARF_PUBNAMES_HEADER_SIZE;
10885 FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10886 if (names != pubtype_table
10887 || p->die->die_offset != 0
10888 || !flag_eliminate_unused_debug_types)
10889 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10891 size += DWARF_OFFSET_SIZE;
10892 return size;
10895 /* Return the size of the information in the .debug_aranges section. */
10897 static unsigned long
10898 size_of_aranges (void)
10900 unsigned long size;
10902 size = DWARF_ARANGES_HEADER_SIZE;
10904 /* Count the address/length pair for this compilation unit. */
10905 if (text_section_used)
10906 size += 2 * DWARF2_ADDR_SIZE;
10907 if (cold_text_section_used)
10908 size += 2 * DWARF2_ADDR_SIZE;
10909 if (have_multiple_function_sections)
10911 unsigned fde_idx = 0;
10913 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
10915 dw_fde_ref fde = &fde_table[fde_idx];
10917 if (!fde->in_std_section)
10918 size += 2 * DWARF2_ADDR_SIZE;
10919 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10920 size += 2 * DWARF2_ADDR_SIZE;
10924 /* Count the two zero words used to terminated the address range table. */
10925 size += 2 * DWARF2_ADDR_SIZE;
10926 return size;
10929 /* Select the encoding of an attribute value. */
10931 static enum dwarf_form
10932 value_format (dw_attr_ref a)
10934 switch (a->dw_attr_val.val_class)
10936 case dw_val_class_addr:
10937 /* Only very few attributes allow DW_FORM_addr. */
10938 switch (a->dw_attr)
10940 case DW_AT_low_pc:
10941 case DW_AT_high_pc:
10942 case DW_AT_entry_pc:
10943 case DW_AT_trampoline:
10944 return DW_FORM_addr;
10945 default:
10946 break;
10948 switch (DWARF2_ADDR_SIZE)
10950 case 1:
10951 return DW_FORM_data1;
10952 case 2:
10953 return DW_FORM_data2;
10954 case 4:
10955 return DW_FORM_data4;
10956 case 8:
10957 return DW_FORM_data8;
10958 default:
10959 gcc_unreachable ();
10961 case dw_val_class_range_list:
10962 case dw_val_class_loc_list:
10963 if (dwarf_version >= 4)
10964 return DW_FORM_sec_offset;
10965 /* FALLTHRU */
10966 case dw_val_class_vms_delta:
10967 case dw_val_class_offset:
10968 switch (DWARF_OFFSET_SIZE)
10970 case 4:
10971 return DW_FORM_data4;
10972 case 8:
10973 return DW_FORM_data8;
10974 default:
10975 gcc_unreachable ();
10977 case dw_val_class_loc:
10978 if (dwarf_version >= 4)
10979 return DW_FORM_exprloc;
10980 switch (constant_size (size_of_locs (AT_loc (a))))
10982 case 1:
10983 return DW_FORM_block1;
10984 case 2:
10985 return DW_FORM_block2;
10986 default:
10987 gcc_unreachable ();
10989 case dw_val_class_const:
10990 return DW_FORM_sdata;
10991 case dw_val_class_unsigned_const:
10992 switch (constant_size (AT_unsigned (a)))
10994 case 1:
10995 return DW_FORM_data1;
10996 case 2:
10997 return DW_FORM_data2;
10998 case 4:
10999 return DW_FORM_data4;
11000 case 8:
11001 return DW_FORM_data8;
11002 default:
11003 gcc_unreachable ();
11005 case dw_val_class_const_double:
11006 switch (HOST_BITS_PER_WIDE_INT)
11008 case 8:
11009 return DW_FORM_data2;
11010 case 16:
11011 return DW_FORM_data4;
11012 case 32:
11013 return DW_FORM_data8;
11014 case 64:
11015 default:
11016 return DW_FORM_block1;
11018 case dw_val_class_vec:
11019 switch (constant_size (a->dw_attr_val.v.val_vec.length
11020 * a->dw_attr_val.v.val_vec.elt_size))
11022 case 1:
11023 return DW_FORM_block1;
11024 case 2:
11025 return DW_FORM_block2;
11026 case 4:
11027 return DW_FORM_block4;
11028 default:
11029 gcc_unreachable ();
11031 case dw_val_class_flag:
11032 if (dwarf_version >= 4)
11034 /* Currently all add_AT_flag calls pass in 1 as last argument,
11035 so DW_FORM_flag_present can be used. If that ever changes,
11036 we'll need to use DW_FORM_flag and have some optimization
11037 in build_abbrev_table that will change those to
11038 DW_FORM_flag_present if it is set to 1 in all DIEs using
11039 the same abbrev entry. */
11040 gcc_assert (a->dw_attr_val.v.val_flag == 1);
11041 return DW_FORM_flag_present;
11043 return DW_FORM_flag;
11044 case dw_val_class_die_ref:
11045 if (AT_ref_external (a))
11046 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
11047 else
11048 return DW_FORM_ref;
11049 case dw_val_class_fde_ref:
11050 return DW_FORM_data;
11051 case dw_val_class_lbl_id:
11052 return DW_FORM_addr;
11053 case dw_val_class_lineptr:
11054 case dw_val_class_macptr:
11055 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11056 case dw_val_class_str:
11057 return AT_string_form (a);
11058 case dw_val_class_file:
11059 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11061 case 1:
11062 return DW_FORM_data1;
11063 case 2:
11064 return DW_FORM_data2;
11065 case 4:
11066 return DW_FORM_data4;
11067 default:
11068 gcc_unreachable ();
11071 case dw_val_class_data8:
11072 return DW_FORM_data8;
11074 default:
11075 gcc_unreachable ();
11079 /* Output the encoding of an attribute value. */
11081 static void
11082 output_value_format (dw_attr_ref a)
11084 enum dwarf_form form = value_format (a);
11086 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11089 /* Output the .debug_abbrev section which defines the DIE abbreviation
11090 table. */
11092 static void
11093 output_abbrev_section (void)
11095 unsigned long abbrev_id;
11097 if (abbrev_die_table_in_use == 1)
11098 return;
11100 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11102 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11103 unsigned ix;
11104 dw_attr_ref a_attr;
11106 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11107 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11108 dwarf_tag_name (abbrev->die_tag));
11110 if (abbrev->die_child != NULL)
11111 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11112 else
11113 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11115 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11116 ix++)
11118 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11119 dwarf_attr_name (a_attr->dw_attr));
11120 output_value_format (a_attr);
11123 dw2_asm_output_data (1, 0, NULL);
11124 dw2_asm_output_data (1, 0, NULL);
11127 /* Terminate the table. */
11128 dw2_asm_output_data (1, 0, NULL);
11131 /* Output a symbol we can use to refer to this DIE from another CU. */
11133 static inline void
11134 output_die_symbol (dw_die_ref die)
11136 char *sym = die->die_id.die_symbol;
11138 if (sym == 0)
11139 return;
11141 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11142 /* We make these global, not weak; if the target doesn't support
11143 .linkonce, it doesn't support combining the sections, so debugging
11144 will break. */
11145 targetm.asm_out.globalize_label (asm_out_file, sym);
11147 ASM_OUTPUT_LABEL (asm_out_file, sym);
11150 /* Return a new location list, given the begin and end range, and the
11151 expression. */
11153 static inline dw_loc_list_ref
11154 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11155 const char *section)
11157 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11159 retlist->begin = begin;
11160 retlist->end = end;
11161 retlist->expr = expr;
11162 retlist->section = section;
11164 return retlist;
11167 /* Generate a new internal symbol for this location list node, if it
11168 hasn't got one yet. */
11170 static inline void
11171 gen_llsym (dw_loc_list_ref list)
11173 gcc_assert (!list->ll_symbol);
11174 list->ll_symbol = gen_internal_sym ("LLST");
11177 /* Output the location list given to us. */
11179 static void
11180 output_loc_list (dw_loc_list_ref list_head)
11182 dw_loc_list_ref curr = list_head;
11184 if (list_head->emitted)
11185 return;
11186 list_head->emitted = true;
11188 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11190 /* Walk the location list, and output each range + expression. */
11191 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11193 unsigned long size;
11194 /* Don't output an entry that starts and ends at the same address. */
11195 if (strcmp (curr->begin, curr->end) == 0)
11196 continue;
11197 if (!have_multiple_function_sections)
11199 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11200 "Location list begin address (%s)",
11201 list_head->ll_symbol);
11202 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11203 "Location list end address (%s)",
11204 list_head->ll_symbol);
11206 else
11208 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11209 "Location list begin address (%s)",
11210 list_head->ll_symbol);
11211 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11212 "Location list end address (%s)",
11213 list_head->ll_symbol);
11215 size = size_of_locs (curr->expr);
11217 /* Output the block length for this list of location operations. */
11218 gcc_assert (size <= 0xffff);
11219 dw2_asm_output_data (2, size, "%s", "Location expression size");
11221 output_loc_sequence (curr->expr, -1);
11224 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11225 "Location list terminator begin (%s)",
11226 list_head->ll_symbol);
11227 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11228 "Location list terminator end (%s)",
11229 list_head->ll_symbol);
11232 /* Output a type signature. */
11234 static inline void
11235 output_signature (const char *sig, const char *name)
11237 int i;
11239 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11240 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11243 /* Output the DIE and its attributes. Called recursively to generate
11244 the definitions of each child DIE. */
11246 static void
11247 output_die (dw_die_ref die)
11249 dw_attr_ref a;
11250 dw_die_ref c;
11251 unsigned long size;
11252 unsigned ix;
11254 /* If someone in another CU might refer to us, set up a symbol for
11255 them to point to. */
11256 if (! use_debug_types && die->die_id.die_symbol)
11257 output_die_symbol (die);
11259 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11260 (unsigned long)die->die_offset,
11261 dwarf_tag_name (die->die_tag));
11263 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11265 const char *name = dwarf_attr_name (a->dw_attr);
11267 switch (AT_class (a))
11269 case dw_val_class_addr:
11270 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11271 break;
11273 case dw_val_class_offset:
11274 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11275 "%s", name);
11276 break;
11278 case dw_val_class_range_list:
11280 char *p = strchr (ranges_section_label, '\0');
11282 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11283 a->dw_attr_val.v.val_offset);
11284 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11285 debug_ranges_section, "%s", name);
11286 *p = '\0';
11288 break;
11290 case dw_val_class_loc:
11291 size = size_of_locs (AT_loc (a));
11293 /* Output the block length for this list of location operations. */
11294 if (dwarf_version >= 4)
11295 dw2_asm_output_data_uleb128 (size, "%s", name);
11296 else
11297 dw2_asm_output_data (constant_size (size), size, "%s", name);
11299 output_loc_sequence (AT_loc (a), -1);
11300 break;
11302 case dw_val_class_const:
11303 /* ??? It would be slightly more efficient to use a scheme like is
11304 used for unsigned constants below, but gdb 4.x does not sign
11305 extend. Gdb 5.x does sign extend. */
11306 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11307 break;
11309 case dw_val_class_unsigned_const:
11310 dw2_asm_output_data (constant_size (AT_unsigned (a)),
11311 AT_unsigned (a), "%s", name);
11312 break;
11314 case dw_val_class_const_double:
11316 unsigned HOST_WIDE_INT first, second;
11318 if (HOST_BITS_PER_WIDE_INT >= 64)
11319 dw2_asm_output_data (1,
11320 2 * HOST_BITS_PER_WIDE_INT
11321 / HOST_BITS_PER_CHAR,
11322 NULL);
11324 if (WORDS_BIG_ENDIAN)
11326 first = a->dw_attr_val.v.val_double.high;
11327 second = a->dw_attr_val.v.val_double.low;
11329 else
11331 first = a->dw_attr_val.v.val_double.low;
11332 second = a->dw_attr_val.v.val_double.high;
11335 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11336 first, name);
11337 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11338 second, NULL);
11340 break;
11342 case dw_val_class_vec:
11344 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11345 unsigned int len = a->dw_attr_val.v.val_vec.length;
11346 unsigned int i;
11347 unsigned char *p;
11349 dw2_asm_output_data (constant_size (len * elt_size),
11350 len * elt_size, "%s", name);
11351 if (elt_size > sizeof (HOST_WIDE_INT))
11353 elt_size /= 2;
11354 len *= 2;
11356 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11357 i < len;
11358 i++, p += elt_size)
11359 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11360 "fp or vector constant word %u", i);
11361 break;
11364 case dw_val_class_flag:
11365 if (dwarf_version >= 4)
11367 /* Currently all add_AT_flag calls pass in 1 as last argument,
11368 so DW_FORM_flag_present can be used. If that ever changes,
11369 we'll need to use DW_FORM_flag and have some optimization
11370 in build_abbrev_table that will change those to
11371 DW_FORM_flag_present if it is set to 1 in all DIEs using
11372 the same abbrev entry. */
11373 gcc_assert (AT_flag (a) == 1);
11374 if (flag_debug_asm)
11375 fprintf (asm_out_file, "\t\t\t%s %s\n",
11376 ASM_COMMENT_START, name);
11377 break;
11379 dw2_asm_output_data (1, AT_flag (a), "%s", name);
11380 break;
11382 case dw_val_class_loc_list:
11384 char *sym = AT_loc_list (a)->ll_symbol;
11386 gcc_assert (sym);
11387 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11388 "%s", name);
11390 break;
11392 case dw_val_class_die_ref:
11393 if (AT_ref_external (a))
11395 if (use_debug_types)
11397 comdat_type_node_ref type_node =
11398 AT_ref (a)->die_id.die_type_node;
11400 gcc_assert (type_node);
11401 output_signature (type_node->signature, name);
11403 else
11405 char *sym = AT_ref (a)->die_id.die_symbol;
11406 int size;
11408 gcc_assert (sym);
11409 /* In DWARF2, DW_FORM_ref_addr is sized by target address
11410 length, whereas in DWARF3 it's always sized as an
11411 offset. */
11412 if (dwarf_version == 2)
11413 size = DWARF2_ADDR_SIZE;
11414 else
11415 size = DWARF_OFFSET_SIZE;
11416 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11417 name);
11420 else
11422 gcc_assert (AT_ref (a)->die_offset);
11423 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11424 "%s", name);
11426 break;
11428 case dw_val_class_fde_ref:
11430 char l1[20];
11432 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11433 a->dw_attr_val.v.val_fde_index * 2);
11434 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11435 "%s", name);
11437 break;
11439 case dw_val_class_vms_delta:
11440 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11441 AT_vms_delta2 (a), AT_vms_delta1 (a),
11442 "%s", name);
11443 break;
11445 case dw_val_class_lbl_id:
11446 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11447 break;
11449 case dw_val_class_lineptr:
11450 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11451 debug_line_section, "%s", name);
11452 break;
11454 case dw_val_class_macptr:
11455 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11456 debug_macinfo_section, "%s", name);
11457 break;
11459 case dw_val_class_str:
11460 if (AT_string_form (a) == DW_FORM_strp)
11461 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11462 a->dw_attr_val.v.val_str->label,
11463 debug_str_section,
11464 "%s: \"%s\"", name, AT_string (a));
11465 else
11466 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11467 break;
11469 case dw_val_class_file:
11471 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11473 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11474 a->dw_attr_val.v.val_file->filename);
11475 break;
11478 case dw_val_class_data8:
11480 int i;
11482 for (i = 0; i < 8; i++)
11483 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11484 i == 0 ? "%s" : NULL, name);
11485 break;
11488 default:
11489 gcc_unreachable ();
11493 FOR_EACH_CHILD (die, c, output_die (c));
11495 /* Add null byte to terminate sibling list. */
11496 if (die->die_child != NULL)
11497 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11498 (unsigned long) die->die_offset);
11501 /* Output the compilation unit that appears at the beginning of the
11502 .debug_info section, and precedes the DIE descriptions. */
11504 static void
11505 output_compilation_unit_header (void)
11507 int ver = dwarf_version;
11509 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11510 dw2_asm_output_data (4, 0xffffffff,
11511 "Initial length escape value indicating 64-bit DWARF extension");
11512 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11513 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11514 "Length of Compilation Unit Info");
11515 dw2_asm_output_data (2, ver, "DWARF version number");
11516 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11517 debug_abbrev_section,
11518 "Offset Into Abbrev. Section");
11519 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11522 /* Output the compilation unit DIE and its children. */
11524 static void
11525 output_comp_unit (dw_die_ref die, int output_if_empty)
11527 const char *secname;
11528 char *oldsym, *tmp;
11530 /* Unless we are outputting main CU, we may throw away empty ones. */
11531 if (!output_if_empty && die->die_child == NULL)
11532 return;
11534 /* Even if there are no children of this DIE, we must output the information
11535 about the compilation unit. Otherwise, on an empty translation unit, we
11536 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11537 will then complain when examining the file. First mark all the DIEs in
11538 this CU so we know which get local refs. */
11539 mark_dies (die);
11541 build_abbrev_table (die);
11543 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11544 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11545 calc_die_sizes (die);
11547 oldsym = die->die_id.die_symbol;
11548 if (oldsym)
11550 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11552 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11553 secname = tmp;
11554 die->die_id.die_symbol = NULL;
11555 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11557 else
11559 switch_to_section (debug_info_section);
11560 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11561 info_section_emitted = true;
11564 /* Output debugging information. */
11565 output_compilation_unit_header ();
11566 output_die (die);
11568 /* Leave the marks on the main CU, so we can check them in
11569 output_pubnames. */
11570 if (oldsym)
11572 unmark_dies (die);
11573 die->die_id.die_symbol = oldsym;
11577 /* Output a comdat type unit DIE and its children. */
11579 static void
11580 output_comdat_type_unit (comdat_type_node *node)
11582 const char *secname;
11583 char *tmp;
11584 int i;
11585 #if defined (OBJECT_FORMAT_ELF)
11586 tree comdat_key;
11587 #endif
11589 /* First mark all the DIEs in this CU so we know which get local refs. */
11590 mark_dies (node->root_die);
11592 build_abbrev_table (node->root_die);
11594 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11595 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11596 calc_die_sizes (node->root_die);
11598 #if defined (OBJECT_FORMAT_ELF)
11599 secname = ".debug_types";
11600 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11601 sprintf (tmp, "wt.");
11602 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11603 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11604 comdat_key = get_identifier (tmp);
11605 targetm.asm_out.named_section (secname,
11606 SECTION_DEBUG | SECTION_LINKONCE,
11607 comdat_key);
11608 #else
11609 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11610 sprintf (tmp, ".gnu.linkonce.wt.");
11611 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11612 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11613 secname = tmp;
11614 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11615 #endif
11617 /* Output debugging information. */
11618 output_compilation_unit_header ();
11619 output_signature (node->signature, "Type Signature");
11620 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11621 "Offset to Type DIE");
11622 output_die (node->root_die);
11624 unmark_dies (node->root_die);
11627 /* Return the DWARF2/3 pubname associated with a decl. */
11629 static const char *
11630 dwarf2_name (tree decl, int scope)
11632 if (DECL_NAMELESS (decl))
11633 return NULL;
11634 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11637 /* Add a new entry to .debug_pubnames if appropriate. */
11639 static void
11640 add_pubname_string (const char *str, dw_die_ref die)
11642 if (targetm.want_debug_pub_sections)
11644 pubname_entry e;
11646 e.die = die;
11647 e.name = xstrdup (str);
11648 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11652 static void
11653 add_pubname (tree decl, dw_die_ref die)
11655 if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11657 const char *name = dwarf2_name (decl, 1);
11658 if (name)
11659 add_pubname_string (name, die);
11663 /* Add a new entry to .debug_pubtypes if appropriate. */
11665 static void
11666 add_pubtype (tree decl, dw_die_ref die)
11668 pubname_entry e;
11670 if (!targetm.want_debug_pub_sections)
11671 return;
11673 e.name = NULL;
11674 if ((TREE_PUBLIC (decl)
11675 || is_cu_die (die->die_parent))
11676 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11678 e.die = die;
11679 if (TYPE_P (decl))
11681 if (TYPE_NAME (decl))
11683 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11684 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11685 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11686 && DECL_NAME (TYPE_NAME (decl)))
11687 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11688 else
11689 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11692 else
11694 e.name = dwarf2_name (decl, 1);
11695 if (e.name)
11696 e.name = xstrdup (e.name);
11699 /* If we don't have a name for the type, there's no point in adding
11700 it to the table. */
11701 if (e.name && e.name[0] != '\0')
11702 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11706 /* Output the public names table used to speed up access to externally
11707 visible names; or the public types table used to find type definitions. */
11709 static void
11710 output_pubnames (VEC (pubname_entry, gc) * names)
11712 unsigned i;
11713 unsigned long pubnames_length = size_of_pubnames (names);
11714 pubname_ref pub;
11716 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11717 dw2_asm_output_data (4, 0xffffffff,
11718 "Initial length escape value indicating 64-bit DWARF extension");
11719 if (names == pubname_table)
11720 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11721 "Length of Public Names Info");
11722 else
11723 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11724 "Length of Public Type Names Info");
11725 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
11726 dw2_asm_output_data (2, 2, "DWARF Version");
11727 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11728 debug_info_section,
11729 "Offset of Compilation Unit Info");
11730 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11731 "Compilation Unit Length");
11733 FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11735 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11736 if (names == pubname_table)
11737 gcc_assert (pub->die->die_mark);
11739 if (names != pubtype_table
11740 || pub->die->die_offset != 0
11741 || !flag_eliminate_unused_debug_types)
11743 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11744 "DIE offset");
11746 dw2_asm_output_nstring (pub->name, -1, "external name");
11750 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11753 /* Output the information that goes into the .debug_aranges table.
11754 Namely, define the beginning and ending address range of the
11755 text section generated for this compilation unit. */
11757 static void
11758 output_aranges (unsigned long aranges_length)
11760 unsigned i;
11762 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11763 dw2_asm_output_data (4, 0xffffffff,
11764 "Initial length escape value indicating 64-bit DWARF extension");
11765 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11766 "Length of Address Ranges Info");
11767 /* Version number for aranges is still 2, even in DWARF3. */
11768 dw2_asm_output_data (2, 2, "DWARF Version");
11769 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11770 debug_info_section,
11771 "Offset of Compilation Unit Info");
11772 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11773 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11775 /* We need to align to twice the pointer size here. */
11776 if (DWARF_ARANGES_PAD_SIZE)
11778 /* Pad using a 2 byte words so that padding is correct for any
11779 pointer size. */
11780 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11781 2 * DWARF2_ADDR_SIZE);
11782 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11783 dw2_asm_output_data (2, 0, NULL);
11786 /* It is necessary not to output these entries if the sections were
11787 not used; if the sections were not used, the length will be 0 and
11788 the address may end up as 0 if the section is discarded by ld
11789 --gc-sections, leaving an invalid (0, 0) entry that can be
11790 confused with the terminator. */
11791 if (text_section_used)
11793 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11794 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11795 text_section_label, "Length");
11797 if (cold_text_section_used)
11799 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11800 "Address");
11801 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11802 cold_text_section_label, "Length");
11805 if (have_multiple_function_sections)
11807 unsigned fde_idx = 0;
11809 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
11811 dw_fde_ref fde = &fde_table[fde_idx];
11813 if (!fde->in_std_section)
11815 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11816 "Address");
11817 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11818 fde->dw_fde_begin, "Length");
11820 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11822 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11823 "Address");
11824 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11825 fde->dw_fde_second_begin, "Length");
11830 /* Output the terminator words. */
11831 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11832 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11835 /* Add a new entry to .debug_ranges. Return the offset at which it
11836 was placed. */
11838 static unsigned int
11839 add_ranges_num (int num)
11841 unsigned int in_use = ranges_table_in_use;
11843 if (in_use == ranges_table_allocated)
11845 ranges_table_allocated += RANGES_TABLE_INCREMENT;
11846 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11847 ranges_table_allocated);
11848 memset (ranges_table + ranges_table_in_use, 0,
11849 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11852 ranges_table[in_use].num = num;
11853 ranges_table_in_use = in_use + 1;
11855 return in_use * 2 * DWARF2_ADDR_SIZE;
11858 /* Add a new entry to .debug_ranges corresponding to a block, or a
11859 range terminator if BLOCK is NULL. */
11861 static unsigned int
11862 add_ranges (const_tree block)
11864 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11867 /* Add a new entry to .debug_ranges corresponding to a pair of
11868 labels. */
11870 static void
11871 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11872 bool *added)
11874 unsigned int in_use = ranges_by_label_in_use;
11875 unsigned int offset;
11877 if (in_use == ranges_by_label_allocated)
11879 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11880 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11881 ranges_by_label,
11882 ranges_by_label_allocated);
11883 memset (ranges_by_label + ranges_by_label_in_use, 0,
11884 RANGES_TABLE_INCREMENT
11885 * sizeof (struct dw_ranges_by_label_struct));
11888 ranges_by_label[in_use].begin = begin;
11889 ranges_by_label[in_use].end = end;
11890 ranges_by_label_in_use = in_use + 1;
11892 offset = add_ranges_num (-(int)in_use - 1);
11893 if (!*added)
11895 add_AT_range_list (die, DW_AT_ranges, offset);
11896 *added = true;
11900 static void
11901 output_ranges (void)
11903 unsigned i;
11904 static const char *const start_fmt = "Offset %#x";
11905 const char *fmt = start_fmt;
11907 for (i = 0; i < ranges_table_in_use; i++)
11909 int block_num = ranges_table[i].num;
11911 if (block_num > 0)
11913 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11914 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11916 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11917 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11919 /* If all code is in the text section, then the compilation
11920 unit base address defaults to DW_AT_low_pc, which is the
11921 base of the text section. */
11922 if (!have_multiple_function_sections)
11924 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11925 text_section_label,
11926 fmt, i * 2 * DWARF2_ADDR_SIZE);
11927 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11928 text_section_label, NULL);
11931 /* Otherwise, the compilation unit base address is zero,
11932 which allows us to use absolute addresses, and not worry
11933 about whether the target supports cross-section
11934 arithmetic. */
11935 else
11937 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11938 fmt, i * 2 * DWARF2_ADDR_SIZE);
11939 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11942 fmt = NULL;
11945 /* Negative block_num stands for an index into ranges_by_label. */
11946 else if (block_num < 0)
11948 int lab_idx = - block_num - 1;
11950 if (!have_multiple_function_sections)
11952 gcc_unreachable ();
11953 #if 0
11954 /* If we ever use add_ranges_by_labels () for a single
11955 function section, all we have to do is to take out
11956 the #if 0 above. */
11957 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11958 ranges_by_label[lab_idx].begin,
11959 text_section_label,
11960 fmt, i * 2 * DWARF2_ADDR_SIZE);
11961 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11962 ranges_by_label[lab_idx].end,
11963 text_section_label, NULL);
11964 #endif
11966 else
11968 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11969 ranges_by_label[lab_idx].begin,
11970 fmt, i * 2 * DWARF2_ADDR_SIZE);
11971 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11972 ranges_by_label[lab_idx].end,
11973 NULL);
11976 else
11978 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11979 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11980 fmt = start_fmt;
11985 /* Data structure containing information about input files. */
11986 struct file_info
11988 const char *path; /* Complete file name. */
11989 const char *fname; /* File name part. */
11990 int length; /* Length of entire string. */
11991 struct dwarf_file_data * file_idx; /* Index in input file table. */
11992 int dir_idx; /* Index in directory table. */
11995 /* Data structure containing information about directories with source
11996 files. */
11997 struct dir_info
11999 const char *path; /* Path including directory name. */
12000 int length; /* Path length. */
12001 int prefix; /* Index of directory entry which is a prefix. */
12002 int count; /* Number of files in this directory. */
12003 int dir_idx; /* Index of directory used as base. */
12006 /* Callback function for file_info comparison. We sort by looking at
12007 the directories in the path. */
12009 static int
12010 file_info_cmp (const void *p1, const void *p2)
12012 const struct file_info *const s1 = (const struct file_info *) p1;
12013 const struct file_info *const s2 = (const struct file_info *) p2;
12014 const unsigned char *cp1;
12015 const unsigned char *cp2;
12017 /* Take care of file names without directories. We need to make sure that
12018 we return consistent values to qsort since some will get confused if
12019 we return the same value when identical operands are passed in opposite
12020 orders. So if neither has a directory, return 0 and otherwise return
12021 1 or -1 depending on which one has the directory. */
12022 if ((s1->path == s1->fname || s2->path == s2->fname))
12023 return (s2->path == s2->fname) - (s1->path == s1->fname);
12025 cp1 = (const unsigned char *) s1->path;
12026 cp2 = (const unsigned char *) s2->path;
12028 while (1)
12030 ++cp1;
12031 ++cp2;
12032 /* Reached the end of the first path? If so, handle like above. */
12033 if ((cp1 == (const unsigned char *) s1->fname)
12034 || (cp2 == (const unsigned char *) s2->fname))
12035 return ((cp2 == (const unsigned char *) s2->fname)
12036 - (cp1 == (const unsigned char *) s1->fname));
12038 /* Character of current path component the same? */
12039 else if (*cp1 != *cp2)
12040 return *cp1 - *cp2;
12044 struct file_name_acquire_data
12046 struct file_info *files;
12047 int used_files;
12048 int max_files;
12051 /* Traversal function for the hash table. */
12053 static int
12054 file_name_acquire (void ** slot, void *data)
12056 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12057 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12058 struct file_info *fi;
12059 const char *f;
12061 gcc_assert (fnad->max_files >= d->emitted_number);
12063 if (! d->emitted_number)
12064 return 1;
12066 gcc_assert (fnad->max_files != fnad->used_files);
12068 fi = fnad->files + fnad->used_files++;
12070 /* Skip all leading "./". */
12071 f = d->filename;
12072 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12073 f += 2;
12075 /* Create a new array entry. */
12076 fi->path = f;
12077 fi->length = strlen (f);
12078 fi->file_idx = d;
12080 /* Search for the file name part. */
12081 f = strrchr (f, DIR_SEPARATOR);
12082 #if defined (DIR_SEPARATOR_2)
12084 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12086 if (g != NULL)
12088 if (f == NULL || f < g)
12089 f = g;
12092 #endif
12094 fi->fname = f == NULL ? fi->path : f + 1;
12095 return 1;
12098 /* Output the directory table and the file name table. We try to minimize
12099 the total amount of memory needed. A heuristic is used to avoid large
12100 slowdowns with many input files. */
12102 static void
12103 output_file_names (void)
12105 struct file_name_acquire_data fnad;
12106 int numfiles;
12107 struct file_info *files;
12108 struct dir_info *dirs;
12109 int *saved;
12110 int *savehere;
12111 int *backmap;
12112 int ndirs;
12113 int idx_offset;
12114 int i;
12116 if (!last_emitted_file)
12118 dw2_asm_output_data (1, 0, "End directory table");
12119 dw2_asm_output_data (1, 0, "End file name table");
12120 return;
12123 numfiles = last_emitted_file->emitted_number;
12125 /* Allocate the various arrays we need. */
12126 files = XALLOCAVEC (struct file_info, numfiles);
12127 dirs = XALLOCAVEC (struct dir_info, numfiles);
12129 fnad.files = files;
12130 fnad.used_files = 0;
12131 fnad.max_files = numfiles;
12132 htab_traverse (file_table, file_name_acquire, &fnad);
12133 gcc_assert (fnad.used_files == fnad.max_files);
12135 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12137 /* Find all the different directories used. */
12138 dirs[0].path = files[0].path;
12139 dirs[0].length = files[0].fname - files[0].path;
12140 dirs[0].prefix = -1;
12141 dirs[0].count = 1;
12142 dirs[0].dir_idx = 0;
12143 files[0].dir_idx = 0;
12144 ndirs = 1;
12146 for (i = 1; i < numfiles; i++)
12147 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12148 && memcmp (dirs[ndirs - 1].path, files[i].path,
12149 dirs[ndirs - 1].length) == 0)
12151 /* Same directory as last entry. */
12152 files[i].dir_idx = ndirs - 1;
12153 ++dirs[ndirs - 1].count;
12155 else
12157 int j;
12159 /* This is a new directory. */
12160 dirs[ndirs].path = files[i].path;
12161 dirs[ndirs].length = files[i].fname - files[i].path;
12162 dirs[ndirs].count = 1;
12163 dirs[ndirs].dir_idx = ndirs;
12164 files[i].dir_idx = ndirs;
12166 /* Search for a prefix. */
12167 dirs[ndirs].prefix = -1;
12168 for (j = 0; j < ndirs; j++)
12169 if (dirs[j].length < dirs[ndirs].length
12170 && dirs[j].length > 1
12171 && (dirs[ndirs].prefix == -1
12172 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12173 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12174 dirs[ndirs].prefix = j;
12176 ++ndirs;
12179 /* Now to the actual work. We have to find a subset of the directories which
12180 allow expressing the file name using references to the directory table
12181 with the least amount of characters. We do not do an exhaustive search
12182 where we would have to check out every combination of every single
12183 possible prefix. Instead we use a heuristic which provides nearly optimal
12184 results in most cases and never is much off. */
12185 saved = XALLOCAVEC (int, ndirs);
12186 savehere = XALLOCAVEC (int, ndirs);
12188 memset (saved, '\0', ndirs * sizeof (saved[0]));
12189 for (i = 0; i < ndirs; i++)
12191 int j;
12192 int total;
12194 /* We can always save some space for the current directory. But this
12195 does not mean it will be enough to justify adding the directory. */
12196 savehere[i] = dirs[i].length;
12197 total = (savehere[i] - saved[i]) * dirs[i].count;
12199 for (j = i + 1; j < ndirs; j++)
12201 savehere[j] = 0;
12202 if (saved[j] < dirs[i].length)
12204 /* Determine whether the dirs[i] path is a prefix of the
12205 dirs[j] path. */
12206 int k;
12208 k = dirs[j].prefix;
12209 while (k != -1 && k != (int) i)
12210 k = dirs[k].prefix;
12212 if (k == (int) i)
12214 /* Yes it is. We can possibly save some memory by
12215 writing the filenames in dirs[j] relative to
12216 dirs[i]. */
12217 savehere[j] = dirs[i].length;
12218 total += (savehere[j] - saved[j]) * dirs[j].count;
12223 /* Check whether we can save enough to justify adding the dirs[i]
12224 directory. */
12225 if (total > dirs[i].length + 1)
12227 /* It's worthwhile adding. */
12228 for (j = i; j < ndirs; j++)
12229 if (savehere[j] > 0)
12231 /* Remember how much we saved for this directory so far. */
12232 saved[j] = savehere[j];
12234 /* Remember the prefix directory. */
12235 dirs[j].dir_idx = i;
12240 /* Emit the directory name table. */
12241 idx_offset = dirs[0].length > 0 ? 1 : 0;
12242 for (i = 1 - idx_offset; i < ndirs; i++)
12243 dw2_asm_output_nstring (dirs[i].path,
12244 dirs[i].length
12245 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12246 "Directory Entry: %#x", i + idx_offset);
12248 dw2_asm_output_data (1, 0, "End directory table");
12250 /* We have to emit them in the order of emitted_number since that's
12251 used in the debug info generation. To do this efficiently we
12252 generate a back-mapping of the indices first. */
12253 backmap = XALLOCAVEC (int, numfiles);
12254 for (i = 0; i < numfiles; i++)
12255 backmap[files[i].file_idx->emitted_number - 1] = i;
12257 /* Now write all the file names. */
12258 for (i = 0; i < numfiles; i++)
12260 int file_idx = backmap[i];
12261 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12263 #ifdef VMS_DEBUGGING_INFO
12264 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12266 /* Setting these fields can lead to debugger miscomparisons,
12267 but VMS Debug requires them to be set correctly. */
12269 int ver;
12270 long long cdt;
12271 long siz;
12272 int maxfilelen = strlen (files[file_idx].path)
12273 + dirs[dir_idx].length
12274 + MAX_VMS_VERSION_LEN + 1;
12275 char *filebuf = XALLOCAVEC (char, maxfilelen);
12277 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12278 snprintf (filebuf, maxfilelen, "%s;%d",
12279 files[file_idx].path + dirs[dir_idx].length, ver);
12281 dw2_asm_output_nstring
12282 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12284 /* Include directory index. */
12285 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12287 /* Modification time. */
12288 dw2_asm_output_data_uleb128
12289 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12290 ? cdt : 0,
12291 NULL);
12293 /* File length in bytes. */
12294 dw2_asm_output_data_uleb128
12295 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12296 ? siz : 0,
12297 NULL);
12298 #else
12299 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12300 "File Entry: %#x", (unsigned) i + 1);
12302 /* Include directory index. */
12303 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12305 /* Modification time. */
12306 dw2_asm_output_data_uleb128 (0, NULL);
12308 /* File length in bytes. */
12309 dw2_asm_output_data_uleb128 (0, NULL);
12310 #endif /* VMS_DEBUGGING_INFO */
12313 dw2_asm_output_data (1, 0, "End file name table");
12317 /* Output one line number table into the .debug_line section. */
12319 static void
12320 output_one_line_info_table (dw_line_info_table *table)
12322 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12323 unsigned int current_line = 1;
12324 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12325 dw_line_info_entry *ent;
12326 size_t i;
12328 FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
12330 switch (ent->opcode)
12332 case LI_set_address:
12333 /* ??? Unfortunately, we have little choice here currently, and
12334 must always use the most general form. GCC does not know the
12335 address delta itself, so we can't use DW_LNS_advance_pc. Many
12336 ports do have length attributes which will give an upper bound
12337 on the address range. We could perhaps use length attributes
12338 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12339 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12341 /* This can handle any delta. This takes
12342 4+DWARF2_ADDR_SIZE bytes. */
12343 dw2_asm_output_data (1, 0, "set address %s", line_label);
12344 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12345 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12346 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12347 break;
12349 case LI_set_line:
12350 if (ent->val == current_line)
12352 /* We still need to start a new row, so output a copy insn. */
12353 dw2_asm_output_data (1, DW_LNS_copy,
12354 "copy line %u", current_line);
12356 else
12358 int line_offset = ent->val - current_line;
12359 int line_delta = line_offset - DWARF_LINE_BASE;
12361 current_line = ent->val;
12362 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12364 /* This can handle deltas from -10 to 234, using the current
12365 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12366 This takes 1 byte. */
12367 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12368 "line %u", current_line);
12370 else
12372 /* This can handle any delta. This takes at least 4 bytes,
12373 depending on the value being encoded. */
12374 dw2_asm_output_data (1, DW_LNS_advance_line,
12375 "advance to line %u", current_line);
12376 dw2_asm_output_data_sleb128 (line_offset, NULL);
12377 dw2_asm_output_data (1, DW_LNS_copy, NULL);
12380 break;
12382 case LI_set_file:
12383 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12384 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12385 break;
12387 case LI_set_column:
12388 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12389 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12390 break;
12392 case LI_negate_stmt:
12393 current_is_stmt = !current_is_stmt;
12394 dw2_asm_output_data (1, DW_LNS_negate_stmt,
12395 "is_stmt %d", current_is_stmt);
12396 break;
12398 case LI_set_prologue_end:
12399 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12400 "set prologue end");
12401 break;
12403 case LI_set_epilogue_begin:
12404 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12405 "set epilogue begin");
12406 break;
12408 case LI_set_discriminator:
12409 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12410 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12411 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12412 dw2_asm_output_data_uleb128 (ent->val, NULL);
12413 break;
12417 /* Emit debug info for the address of the end of the table. */
12418 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12419 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12420 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12421 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12423 dw2_asm_output_data (1, 0, "end sequence");
12424 dw2_asm_output_data_uleb128 (1, NULL);
12425 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12428 /* Output the source line number correspondence information. This
12429 information goes into the .debug_line section. */
12431 static void
12432 output_line_info (void)
12434 char l1[20], l2[20], p1[20], p2[20];
12435 int ver = dwarf_version;
12436 bool saw_one = false;
12437 int opc;
12439 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12440 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12441 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12442 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12444 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12445 dw2_asm_output_data (4, 0xffffffff,
12446 "Initial length escape value indicating 64-bit DWARF extension");
12447 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12448 "Length of Source Line Info");
12449 ASM_OUTPUT_LABEL (asm_out_file, l1);
12451 dw2_asm_output_data (2, ver, "DWARF Version");
12452 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12453 ASM_OUTPUT_LABEL (asm_out_file, p1);
12455 /* Define the architecture-dependent minimum instruction length (in bytes).
12456 In this implementation of DWARF, this field is used for information
12457 purposes only. Since GCC generates assembly language, we have no
12458 a priori knowledge of how many instruction bytes are generated for each
12459 source line, and therefore can use only the DW_LNE_set_address and
12460 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
12461 this as '1', which is "correct enough" for all architectures,
12462 and don't let the target override. */
12463 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12465 if (ver >= 4)
12466 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12467 "Maximum Operations Per Instruction");
12468 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12469 "Default is_stmt_start flag");
12470 dw2_asm_output_data (1, DWARF_LINE_BASE,
12471 "Line Base Value (Special Opcodes)");
12472 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12473 "Line Range Value (Special Opcodes)");
12474 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12475 "Special Opcode Base");
12477 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12479 int n_op_args;
12480 switch (opc)
12482 case DW_LNS_advance_pc:
12483 case DW_LNS_advance_line:
12484 case DW_LNS_set_file:
12485 case DW_LNS_set_column:
12486 case DW_LNS_fixed_advance_pc:
12487 case DW_LNS_set_isa:
12488 n_op_args = 1;
12489 break;
12490 default:
12491 n_op_args = 0;
12492 break;
12495 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12496 opc, n_op_args);
12499 /* Write out the information about the files we use. */
12500 output_file_names ();
12501 ASM_OUTPUT_LABEL (asm_out_file, p2);
12503 if (separate_line_info)
12505 dw_line_info_table *table;
12506 size_t i;
12508 FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
12509 if (table->in_use)
12511 output_one_line_info_table (table);
12512 saw_one = true;
12515 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12517 output_one_line_info_table (cold_text_section_line_info);
12518 saw_one = true;
12521 /* ??? Some Darwin linkers crash on a .debug_line section with no
12522 sequences. Further, merely a DW_LNE_end_sequence entry is not
12523 sufficient -- the address column must also be initialized.
12524 Make sure to output at least one set_address/end_sequence pair,
12525 choosing .text since that section is always present. */
12526 if (text_section_line_info->in_use || !saw_one)
12527 output_one_line_info_table (text_section_line_info);
12529 /* Output the marker for the end of the line number info. */
12530 ASM_OUTPUT_LABEL (asm_out_file, l2);
12533 /* Given a pointer to a tree node for some base type, return a pointer to
12534 a DIE that describes the given type.
12536 This routine must only be called for GCC type nodes that correspond to
12537 Dwarf base (fundamental) types. */
12539 static dw_die_ref
12540 base_type_die (tree type)
12542 dw_die_ref base_type_result;
12543 enum dwarf_type encoding;
12545 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12546 return 0;
12548 /* If this is a subtype that should not be emitted as a subrange type,
12549 use the base type. See subrange_type_for_debug_p. */
12550 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12551 type = TREE_TYPE (type);
12553 switch (TREE_CODE (type))
12555 case INTEGER_TYPE:
12556 if ((dwarf_version >= 4 || !dwarf_strict)
12557 && TYPE_NAME (type)
12558 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12559 && DECL_IS_BUILTIN (TYPE_NAME (type))
12560 && DECL_NAME (TYPE_NAME (type)))
12562 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12563 if (strcmp (name, "char16_t") == 0
12564 || strcmp (name, "char32_t") == 0)
12566 encoding = DW_ATE_UTF;
12567 break;
12570 if (TYPE_STRING_FLAG (type))
12572 if (TYPE_UNSIGNED (type))
12573 encoding = DW_ATE_unsigned_char;
12574 else
12575 encoding = DW_ATE_signed_char;
12577 else if (TYPE_UNSIGNED (type))
12578 encoding = DW_ATE_unsigned;
12579 else
12580 encoding = DW_ATE_signed;
12581 break;
12583 case REAL_TYPE:
12584 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12586 if (dwarf_version >= 3 || !dwarf_strict)
12587 encoding = DW_ATE_decimal_float;
12588 else
12589 encoding = DW_ATE_lo_user;
12591 else
12592 encoding = DW_ATE_float;
12593 break;
12595 case FIXED_POINT_TYPE:
12596 if (!(dwarf_version >= 3 || !dwarf_strict))
12597 encoding = DW_ATE_lo_user;
12598 else if (TYPE_UNSIGNED (type))
12599 encoding = DW_ATE_unsigned_fixed;
12600 else
12601 encoding = DW_ATE_signed_fixed;
12602 break;
12604 /* Dwarf2 doesn't know anything about complex ints, so use
12605 a user defined type for it. */
12606 case COMPLEX_TYPE:
12607 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12608 encoding = DW_ATE_complex_float;
12609 else
12610 encoding = DW_ATE_lo_user;
12611 break;
12613 case BOOLEAN_TYPE:
12614 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12615 encoding = DW_ATE_boolean;
12616 break;
12618 default:
12619 /* No other TREE_CODEs are Dwarf fundamental types. */
12620 gcc_unreachable ();
12623 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12625 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12626 int_size_in_bytes (type));
12627 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12629 return base_type_result;
12632 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12633 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12635 static inline int
12636 is_base_type (tree type)
12638 switch (TREE_CODE (type))
12640 case ERROR_MARK:
12641 case VOID_TYPE:
12642 case INTEGER_TYPE:
12643 case REAL_TYPE:
12644 case FIXED_POINT_TYPE:
12645 case COMPLEX_TYPE:
12646 case BOOLEAN_TYPE:
12647 return 1;
12649 case ARRAY_TYPE:
12650 case RECORD_TYPE:
12651 case UNION_TYPE:
12652 case QUAL_UNION_TYPE:
12653 case ENUMERAL_TYPE:
12654 case FUNCTION_TYPE:
12655 case METHOD_TYPE:
12656 case POINTER_TYPE:
12657 case REFERENCE_TYPE:
12658 case NULLPTR_TYPE:
12659 case OFFSET_TYPE:
12660 case LANG_TYPE:
12661 case VECTOR_TYPE:
12662 return 0;
12664 default:
12665 gcc_unreachable ();
12668 return 0;
12671 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12672 node, return the size in bits for the type if it is a constant, or else
12673 return the alignment for the type if the type's size is not constant, or
12674 else return BITS_PER_WORD if the type actually turns out to be an
12675 ERROR_MARK node. */
12677 static inline unsigned HOST_WIDE_INT
12678 simple_type_size_in_bits (const_tree type)
12680 if (TREE_CODE (type) == ERROR_MARK)
12681 return BITS_PER_WORD;
12682 else if (TYPE_SIZE (type) == NULL_TREE)
12683 return 0;
12684 else if (host_integerp (TYPE_SIZE (type), 1))
12685 return tree_low_cst (TYPE_SIZE (type), 1);
12686 else
12687 return TYPE_ALIGN (type);
12690 /* Similarly, but return a double_int instead of UHWI. */
12692 static inline double_int
12693 double_int_type_size_in_bits (const_tree type)
12695 if (TREE_CODE (type) == ERROR_MARK)
12696 return uhwi_to_double_int (BITS_PER_WORD);
12697 else if (TYPE_SIZE (type) == NULL_TREE)
12698 return double_int_zero;
12699 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12700 return tree_to_double_int (TYPE_SIZE (type));
12701 else
12702 return uhwi_to_double_int (TYPE_ALIGN (type));
12705 /* Given a pointer to a tree node for a subrange type, return a pointer
12706 to a DIE that describes the given type. */
12708 static dw_die_ref
12709 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12711 dw_die_ref subrange_die;
12712 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12714 if (context_die == NULL)
12715 context_die = comp_unit_die ();
12717 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12719 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12721 /* The size of the subrange type and its base type do not match,
12722 so we need to generate a size attribute for the subrange type. */
12723 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12726 if (low)
12727 add_bound_info (subrange_die, DW_AT_lower_bound, low);
12728 if (high)
12729 add_bound_info (subrange_die, DW_AT_upper_bound, high);
12731 return subrange_die;
12734 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12735 entry that chains various modifiers in front of the given type. */
12737 static dw_die_ref
12738 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12739 dw_die_ref context_die)
12741 enum tree_code code = TREE_CODE (type);
12742 dw_die_ref mod_type_die;
12743 dw_die_ref sub_die = NULL;
12744 tree item_type = NULL;
12745 tree qualified_type;
12746 tree name, low, high;
12748 if (code == ERROR_MARK)
12749 return NULL;
12751 /* See if we already have the appropriately qualified variant of
12752 this type. */
12753 qualified_type
12754 = get_qualified_type (type,
12755 ((is_const_type ? TYPE_QUAL_CONST : 0)
12756 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12758 if (qualified_type == sizetype
12759 && TYPE_NAME (qualified_type)
12760 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12762 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12764 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12765 && TYPE_PRECISION (t)
12766 == TYPE_PRECISION (qualified_type)
12767 && TYPE_UNSIGNED (t)
12768 == TYPE_UNSIGNED (qualified_type));
12769 qualified_type = t;
12772 /* If we do, then we can just use its DIE, if it exists. */
12773 if (qualified_type)
12775 mod_type_die = lookup_type_die (qualified_type);
12776 if (mod_type_die)
12777 return mod_type_die;
12780 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12782 /* Handle C typedef types. */
12783 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12784 && !DECL_ARTIFICIAL (name))
12786 tree dtype = TREE_TYPE (name);
12788 if (qualified_type == dtype)
12790 /* For a named type, use the typedef. */
12791 gen_type_die (qualified_type, context_die);
12792 return lookup_type_die (qualified_type);
12794 else if (is_const_type < TYPE_READONLY (dtype)
12795 || is_volatile_type < TYPE_VOLATILE (dtype)
12796 || (is_const_type <= TYPE_READONLY (dtype)
12797 && is_volatile_type <= TYPE_VOLATILE (dtype)
12798 && DECL_ORIGINAL_TYPE (name) != type))
12799 /* cv-unqualified version of named type. Just use the unnamed
12800 type to which it refers. */
12801 return modified_type_die (DECL_ORIGINAL_TYPE (name),
12802 is_const_type, is_volatile_type,
12803 context_die);
12804 /* Else cv-qualified version of named type; fall through. */
12807 if (is_const_type
12808 /* If both is_const_type and is_volatile_type, prefer the path
12809 which leads to a qualified type. */
12810 && (!is_volatile_type
12811 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
12812 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
12814 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12815 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12817 else if (is_volatile_type)
12819 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12820 sub_die = modified_type_die (type, is_const_type, 0, context_die);
12822 else if (code == POINTER_TYPE)
12824 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12825 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12826 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12827 item_type = TREE_TYPE (type);
12828 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12829 add_AT_unsigned (mod_type_die, DW_AT_address_class,
12830 TYPE_ADDR_SPACE (item_type));
12832 else if (code == REFERENCE_TYPE)
12834 if (TYPE_REF_IS_RVALUE (type) && use_debug_types)
12835 mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12836 type);
12837 else
12838 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12839 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12840 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12841 item_type = TREE_TYPE (type);
12842 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12843 add_AT_unsigned (mod_type_die, DW_AT_address_class,
12844 TYPE_ADDR_SPACE (item_type));
12846 else if (code == INTEGER_TYPE
12847 && TREE_TYPE (type) != NULL_TREE
12848 && subrange_type_for_debug_p (type, &low, &high))
12850 mod_type_die = subrange_type_die (type, low, high, context_die);
12851 item_type = TREE_TYPE (type);
12853 else if (is_base_type (type))
12854 mod_type_die = base_type_die (type);
12855 else
12857 gen_type_die (type, context_die);
12859 /* We have to get the type_main_variant here (and pass that to the
12860 `lookup_type_die' routine) because the ..._TYPE node we have
12861 might simply be a *copy* of some original type node (where the
12862 copy was created to help us keep track of typedef names) and
12863 that copy might have a different TYPE_UID from the original
12864 ..._TYPE node. */
12865 if (TREE_CODE (type) != VECTOR_TYPE)
12866 return lookup_type_die (type_main_variant (type));
12867 else
12868 /* Vectors have the debugging information in the type,
12869 not the main variant. */
12870 return lookup_type_die (type);
12873 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12874 don't output a DW_TAG_typedef, since there isn't one in the
12875 user's program; just attach a DW_AT_name to the type.
12876 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12877 if the base type already has the same name. */
12878 if (name
12879 && ((TREE_CODE (name) != TYPE_DECL
12880 && (qualified_type == TYPE_MAIN_VARIANT (type)
12881 || (!is_const_type && !is_volatile_type)))
12882 || (TREE_CODE (name) == TYPE_DECL
12883 && TREE_TYPE (name) == qualified_type
12884 && DECL_NAME (name))))
12886 if (TREE_CODE (name) == TYPE_DECL)
12887 /* Could just call add_name_and_src_coords_attributes here,
12888 but since this is a builtin type it doesn't have any
12889 useful source coordinates anyway. */
12890 name = DECL_NAME (name);
12891 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12892 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12894 /* This probably indicates a bug. */
12895 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12896 add_name_attribute (mod_type_die, "__unknown__");
12898 if (qualified_type)
12899 equate_type_number_to_die (qualified_type, mod_type_die);
12901 if (item_type)
12902 /* We must do this after the equate_type_number_to_die call, in case
12903 this is a recursive type. This ensures that the modified_type_die
12904 recursion will terminate even if the type is recursive. Recursive
12905 types are possible in Ada. */
12906 sub_die = modified_type_die (item_type,
12907 TYPE_READONLY (item_type),
12908 TYPE_VOLATILE (item_type),
12909 context_die);
12911 if (sub_die != NULL)
12912 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12914 return mod_type_die;
12917 /* Generate DIEs for the generic parameters of T.
12918 T must be either a generic type or a generic function.
12919 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12921 static void
12922 gen_generic_params_dies (tree t)
12924 tree parms, args;
12925 int parms_num, i;
12926 dw_die_ref die = NULL;
12928 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12929 return;
12931 if (TYPE_P (t))
12932 die = lookup_type_die (t);
12933 else if (DECL_P (t))
12934 die = lookup_decl_die (t);
12936 gcc_assert (die);
12938 parms = lang_hooks.get_innermost_generic_parms (t);
12939 if (!parms)
12940 /* T has no generic parameter. It means T is neither a generic type
12941 or function. End of story. */
12942 return;
12944 parms_num = TREE_VEC_LENGTH (parms);
12945 args = lang_hooks.get_innermost_generic_args (t);
12946 for (i = 0; i < parms_num; i++)
12948 tree parm, arg, arg_pack_elems;
12950 parm = TREE_VEC_ELT (parms, i);
12951 arg = TREE_VEC_ELT (args, i);
12952 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12953 gcc_assert (parm && TREE_VALUE (parm) && arg);
12955 if (parm && TREE_VALUE (parm) && arg)
12957 /* If PARM represents a template parameter pack,
12958 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12959 by DW_TAG_template_*_parameter DIEs for the argument
12960 pack elements of ARG. Note that ARG would then be
12961 an argument pack. */
12962 if (arg_pack_elems)
12963 template_parameter_pack_die (TREE_VALUE (parm),
12964 arg_pack_elems,
12965 die);
12966 else
12967 generic_parameter_die (TREE_VALUE (parm), arg,
12968 true /* Emit DW_AT_name */, die);
12973 /* Create and return a DIE for PARM which should be
12974 the representation of a generic type parameter.
12975 For instance, in the C++ front end, PARM would be a template parameter.
12976 ARG is the argument to PARM.
12977 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12978 name of the PARM.
12979 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12980 as a child node. */
12982 static dw_die_ref
12983 generic_parameter_die (tree parm, tree arg,
12984 bool emit_name_p,
12985 dw_die_ref parent_die)
12987 dw_die_ref tmpl_die = NULL;
12988 const char *name = NULL;
12990 if (!parm || !DECL_NAME (parm) || !arg)
12991 return NULL;
12993 /* We support non-type generic parameters and arguments,
12994 type generic parameters and arguments, as well as
12995 generic generic parameters (a.k.a. template template parameters in C++)
12996 and arguments. */
12997 if (TREE_CODE (parm) == PARM_DECL)
12998 /* PARM is a nontype generic parameter */
12999 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13000 else if (TREE_CODE (parm) == TYPE_DECL)
13001 /* PARM is a type generic parameter. */
13002 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13003 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13004 /* PARM is a generic generic parameter.
13005 Its DIE is a GNU extension. It shall have a
13006 DW_AT_name attribute to represent the name of the template template
13007 parameter, and a DW_AT_GNU_template_name attribute to represent the
13008 name of the template template argument. */
13009 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13010 parent_die, parm);
13011 else
13012 gcc_unreachable ();
13014 if (tmpl_die)
13016 tree tmpl_type;
13018 /* If PARM is a generic parameter pack, it means we are
13019 emitting debug info for a template argument pack element.
13020 In other terms, ARG is a template argument pack element.
13021 In that case, we don't emit any DW_AT_name attribute for
13022 the die. */
13023 if (emit_name_p)
13025 name = IDENTIFIER_POINTER (DECL_NAME (parm));
13026 gcc_assert (name);
13027 add_AT_string (tmpl_die, DW_AT_name, name);
13030 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13032 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13033 TMPL_DIE should have a child DW_AT_type attribute that is set
13034 to the type of the argument to PARM, which is ARG.
13035 If PARM is a type generic parameter, TMPL_DIE should have a
13036 child DW_AT_type that is set to ARG. */
13037 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13038 add_type_attribute (tmpl_die, tmpl_type, 0,
13039 TREE_THIS_VOLATILE (tmpl_type),
13040 parent_die);
13042 else
13044 /* So TMPL_DIE is a DIE representing a
13045 a generic generic template parameter, a.k.a template template
13046 parameter in C++ and arg is a template. */
13048 /* The DW_AT_GNU_template_name attribute of the DIE must be set
13049 to the name of the argument. */
13050 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13051 if (name)
13052 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13055 if (TREE_CODE (parm) == PARM_DECL)
13056 /* So PARM is a non-type generic parameter.
13057 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13058 attribute of TMPL_DIE which value represents the value
13059 of ARG.
13060 We must be careful here:
13061 The value of ARG might reference some function decls.
13062 We might currently be emitting debug info for a generic
13063 type and types are emitted before function decls, we don't
13064 know if the function decls referenced by ARG will actually be
13065 emitted after cgraph computations.
13066 So must defer the generation of the DW_AT_const_value to
13067 after cgraph is ready. */
13068 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13071 return tmpl_die;
13074 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13075 PARM_PACK must be a template parameter pack. The returned DIE
13076 will be child DIE of PARENT_DIE. */
13078 static dw_die_ref
13079 template_parameter_pack_die (tree parm_pack,
13080 tree parm_pack_args,
13081 dw_die_ref parent_die)
13083 dw_die_ref die;
13084 int j;
13086 gcc_assert (parent_die && parm_pack);
13088 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13089 add_name_and_src_coords_attributes (die, parm_pack);
13090 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13091 generic_parameter_die (parm_pack,
13092 TREE_VEC_ELT (parm_pack_args, j),
13093 false /* Don't emit DW_AT_name */,
13094 die);
13095 return die;
13098 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13099 an enumerated type. */
13101 static inline int
13102 type_is_enum (const_tree type)
13104 return TREE_CODE (type) == ENUMERAL_TYPE;
13107 /* Return the DBX register number described by a given RTL node. */
13109 static unsigned int
13110 dbx_reg_number (const_rtx rtl)
13112 unsigned regno = REGNO (rtl);
13114 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13116 #ifdef LEAF_REG_REMAP
13117 if (current_function_uses_only_leaf_regs)
13119 int leaf_reg = LEAF_REG_REMAP (regno);
13120 if (leaf_reg != -1)
13121 regno = (unsigned) leaf_reg;
13123 #endif
13125 return DBX_REGISTER_NUMBER (regno);
13128 /* Optionally add a DW_OP_piece term to a location description expression.
13129 DW_OP_piece is only added if the location description expression already
13130 doesn't end with DW_OP_piece. */
13132 static void
13133 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13135 dw_loc_descr_ref loc;
13137 if (*list_head != NULL)
13139 /* Find the end of the chain. */
13140 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13143 if (loc->dw_loc_opc != DW_OP_piece)
13144 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13148 /* Return a location descriptor that designates a machine register or
13149 zero if there is none. */
13151 static dw_loc_descr_ref
13152 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13154 rtx regs;
13156 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13157 return 0;
13159 /* We only use "frame base" when we're sure we're talking about the
13160 post-prologue local stack frame. We do this by *not* running
13161 register elimination until this point, and recognizing the special
13162 argument pointer and soft frame pointer rtx's.
13163 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13164 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13165 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13167 dw_loc_descr_ref result = NULL;
13169 if (dwarf_version >= 4 || !dwarf_strict)
13171 result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13172 if (result)
13173 add_loc_descr (&result,
13174 new_loc_descr (DW_OP_stack_value, 0, 0));
13176 return result;
13179 regs = targetm.dwarf_register_span (rtl);
13181 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13182 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13183 else
13184 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13187 /* Return a location descriptor that designates a machine register for
13188 a given hard register number. */
13190 static dw_loc_descr_ref
13191 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13193 dw_loc_descr_ref reg_loc_descr;
13195 if (regno <= 31)
13196 reg_loc_descr
13197 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13198 else
13199 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13201 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13202 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13204 return reg_loc_descr;
13207 /* Given an RTL of a register, return a location descriptor that
13208 designates a value that spans more than one register. */
13210 static dw_loc_descr_ref
13211 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13212 enum var_init_status initialized)
13214 int nregs, size, i;
13215 unsigned reg;
13216 dw_loc_descr_ref loc_result = NULL;
13218 reg = REGNO (rtl);
13219 #ifdef LEAF_REG_REMAP
13220 if (current_function_uses_only_leaf_regs)
13222 int leaf_reg = LEAF_REG_REMAP (reg);
13223 if (leaf_reg != -1)
13224 reg = (unsigned) leaf_reg;
13226 #endif
13227 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13228 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13230 /* Simple, contiguous registers. */
13231 if (regs == NULL_RTX)
13233 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13235 loc_result = NULL;
13236 while (nregs--)
13238 dw_loc_descr_ref t;
13240 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13241 VAR_INIT_STATUS_INITIALIZED);
13242 add_loc_descr (&loc_result, t);
13243 add_loc_descr_op_piece (&loc_result, size);
13244 ++reg;
13246 return loc_result;
13249 /* Now onto stupid register sets in non contiguous locations. */
13251 gcc_assert (GET_CODE (regs) == PARALLEL);
13253 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13254 loc_result = NULL;
13256 for (i = 0; i < XVECLEN (regs, 0); ++i)
13258 dw_loc_descr_ref t;
13260 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13261 VAR_INIT_STATUS_INITIALIZED);
13262 add_loc_descr (&loc_result, t);
13263 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13264 add_loc_descr_op_piece (&loc_result, size);
13267 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13268 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13269 return loc_result;
13272 /* Return a location descriptor that designates a constant. */
13274 static dw_loc_descr_ref
13275 int_loc_descriptor (HOST_WIDE_INT i)
13277 enum dwarf_location_atom op;
13279 /* Pick the smallest representation of a constant, rather than just
13280 defaulting to the LEB encoding. */
13281 if (i >= 0)
13283 if (i <= 31)
13284 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13285 else if (i <= 0xff)
13286 op = DW_OP_const1u;
13287 else if (i <= 0xffff)
13288 op = DW_OP_const2u;
13289 else if (HOST_BITS_PER_WIDE_INT == 32
13290 || i <= 0xffffffff)
13291 op = DW_OP_const4u;
13292 else
13293 op = DW_OP_constu;
13295 else
13297 if (i >= -0x80)
13298 op = DW_OP_const1s;
13299 else if (i >= -0x8000)
13300 op = DW_OP_const2s;
13301 else if (HOST_BITS_PER_WIDE_INT == 32
13302 || i >= -0x80000000)
13303 op = DW_OP_const4s;
13304 else
13305 op = DW_OP_consts;
13308 return new_loc_descr (op, i, 0);
13311 /* Return loc description representing "address" of integer value.
13312 This can appear only as toplevel expression. */
13314 static dw_loc_descr_ref
13315 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13317 int litsize;
13318 dw_loc_descr_ref loc_result = NULL;
13320 if (!(dwarf_version >= 4 || !dwarf_strict))
13321 return NULL;
13323 if (i >= 0)
13325 if (i <= 31)
13326 litsize = 1;
13327 else if (i <= 0xff)
13328 litsize = 2;
13329 else if (i <= 0xffff)
13330 litsize = 3;
13331 else if (HOST_BITS_PER_WIDE_INT == 32
13332 || i <= 0xffffffff)
13333 litsize = 5;
13334 else
13335 litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13337 else
13339 if (i >= -0x80)
13340 litsize = 2;
13341 else if (i >= -0x8000)
13342 litsize = 3;
13343 else if (HOST_BITS_PER_WIDE_INT == 32
13344 || i >= -0x80000000)
13345 litsize = 5;
13346 else
13347 litsize = 1 + size_of_sleb128 (i);
13349 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13350 is more compact. For DW_OP_stack_value we need:
13351 litsize + 1 (DW_OP_stack_value)
13352 and for DW_OP_implicit_value:
13353 1 (DW_OP_implicit_value) + 1 (length) + size. */
13354 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13356 loc_result = int_loc_descriptor (i);
13357 add_loc_descr (&loc_result,
13358 new_loc_descr (DW_OP_stack_value, 0, 0));
13359 return loc_result;
13362 loc_result = new_loc_descr (DW_OP_implicit_value,
13363 size, 0);
13364 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13365 loc_result->dw_loc_oprnd2.v.val_int = i;
13366 return loc_result;
13369 /* Return a location descriptor that designates a base+offset location. */
13371 static dw_loc_descr_ref
13372 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13373 enum var_init_status initialized)
13375 unsigned int regno;
13376 dw_loc_descr_ref result;
13377 dw_fde_ref fde = current_fde ();
13379 /* We only use "frame base" when we're sure we're talking about the
13380 post-prologue local stack frame. We do this by *not* running
13381 register elimination until this point, and recognizing the special
13382 argument pointer and soft frame pointer rtx's. */
13383 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13385 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13387 if (elim != reg)
13389 if (GET_CODE (elim) == PLUS)
13391 offset += INTVAL (XEXP (elim, 1));
13392 elim = XEXP (elim, 0);
13394 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13395 && (elim == hard_frame_pointer_rtx
13396 || elim == stack_pointer_rtx))
13397 || elim == (frame_pointer_needed
13398 ? hard_frame_pointer_rtx
13399 : stack_pointer_rtx));
13401 /* If drap register is used to align stack, use frame
13402 pointer + offset to access stack variables. If stack
13403 is aligned without drap, use stack pointer + offset to
13404 access stack variables. */
13405 if (crtl->stack_realign_tried
13406 && reg == frame_pointer_rtx)
13408 int base_reg
13409 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13410 ? HARD_FRAME_POINTER_REGNUM
13411 : REGNO (elim));
13412 return new_reg_loc_descr (base_reg, offset);
13415 offset += frame_pointer_fb_offset;
13416 return new_loc_descr (DW_OP_fbreg, offset, 0);
13419 else if (!optimize
13420 && fde
13421 && (fde->drap_reg == REGNO (reg)
13422 || fde->vdrap_reg == REGNO (reg)))
13424 /* Use cfa+offset to represent the location of arguments passed
13425 on the stack when drap is used to align stack.
13426 Only do this when not optimizing, for optimized code var-tracking
13427 is supposed to track where the arguments live and the register
13428 used as vdrap or drap in some spot might be used for something
13429 else in other part of the routine. */
13430 return new_loc_descr (DW_OP_fbreg, offset, 0);
13433 regno = dbx_reg_number (reg);
13434 if (regno <= 31)
13435 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13436 offset, 0);
13437 else
13438 result = new_loc_descr (DW_OP_bregx, regno, offset);
13440 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13441 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13443 return result;
13446 /* Return true if this RTL expression describes a base+offset calculation. */
13448 static inline int
13449 is_based_loc (const_rtx rtl)
13451 return (GET_CODE (rtl) == PLUS
13452 && ((REG_P (XEXP (rtl, 0))
13453 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13454 && CONST_INT_P (XEXP (rtl, 1)))));
13457 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13458 failed. */
13460 static dw_loc_descr_ref
13461 tls_mem_loc_descriptor (rtx mem)
13463 tree base;
13464 dw_loc_descr_ref loc_result;
13466 if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13467 return NULL;
13469 base = get_base_address (MEM_EXPR (mem));
13470 if (base == NULL
13471 || TREE_CODE (base) != VAR_DECL
13472 || !DECL_THREAD_LOCAL_P (base))
13473 return NULL;
13475 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13476 if (loc_result == NULL)
13477 return NULL;
13479 if (INTVAL (MEM_OFFSET (mem)))
13480 loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13482 return loc_result;
13485 /* Output debug info about reason why we failed to expand expression as dwarf
13486 expression. */
13488 static void
13489 expansion_failed (tree expr, rtx rtl, char const *reason)
13491 if (dump_file && (dump_flags & TDF_DETAILS))
13493 fprintf (dump_file, "Failed to expand as dwarf: ");
13494 if (expr)
13495 print_generic_expr (dump_file, expr, dump_flags);
13496 if (rtl)
13498 fprintf (dump_file, "\n");
13499 print_rtl (dump_file, rtl);
13501 fprintf (dump_file, "\nReason: %s\n", reason);
13505 /* Helper function for const_ok_for_output, called either directly
13506 or via for_each_rtx. */
13508 static int
13509 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13511 rtx rtl = *rtlp;
13513 if (GET_CODE (rtl) == UNSPEC)
13515 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13516 we can't express it in the debug info. */
13517 #ifdef ENABLE_CHECKING
13518 /* Don't complain about TLS UNSPECs, those are just too hard to
13519 delegitimize. */
13520 if (XVECLEN (rtl, 0) != 1
13521 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13522 || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13523 || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13524 || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13525 inform (current_function_decl
13526 ? DECL_SOURCE_LOCATION (current_function_decl)
13527 : UNKNOWN_LOCATION,
13528 #if NUM_UNSPEC_VALUES > 0
13529 "non-delegitimized UNSPEC %s (%d) found in variable location",
13530 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13531 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13532 XINT (rtl, 1));
13533 #else
13534 "non-delegitimized UNSPEC %d found in variable location",
13535 XINT (rtl, 1));
13536 #endif
13537 #endif
13538 expansion_failed (NULL_TREE, rtl,
13539 "UNSPEC hasn't been delegitimized.\n");
13540 return 1;
13543 if (GET_CODE (rtl) != SYMBOL_REF)
13544 return 0;
13546 if (CONSTANT_POOL_ADDRESS_P (rtl))
13548 bool marked;
13549 get_pool_constant_mark (rtl, &marked);
13550 /* If all references to this pool constant were optimized away,
13551 it was not output and thus we can't represent it. */
13552 if (!marked)
13554 expansion_failed (NULL_TREE, rtl,
13555 "Constant was removed from constant pool.\n");
13556 return 1;
13560 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13561 return 1;
13563 /* Avoid references to external symbols in debug info, on several targets
13564 the linker might even refuse to link when linking a shared library,
13565 and in many other cases the relocations for .debug_info/.debug_loc are
13566 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13567 to be defined within the same shared library or executable are fine. */
13568 if (SYMBOL_REF_EXTERNAL_P (rtl))
13570 tree decl = SYMBOL_REF_DECL (rtl);
13572 if (decl == NULL || !targetm.binds_local_p (decl))
13574 expansion_failed (NULL_TREE, rtl,
13575 "Symbol not defined in current TU.\n");
13576 return 1;
13580 return 0;
13583 /* Return true if constant RTL can be emitted in DW_OP_addr or
13584 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13585 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13587 static bool
13588 const_ok_for_output (rtx rtl)
13590 if (GET_CODE (rtl) == SYMBOL_REF)
13591 return const_ok_for_output_1 (&rtl, NULL) == 0;
13593 if (GET_CODE (rtl) == CONST)
13594 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13596 return true;
13599 /* The following routine converts the RTL for a variable or parameter
13600 (resident in memory) into an equivalent Dwarf representation of a
13601 mechanism for getting the address of that same variable onto the top of a
13602 hypothetical "address evaluation" stack.
13604 When creating memory location descriptors, we are effectively transforming
13605 the RTL for a memory-resident object into its Dwarf postfix expression
13606 equivalent. This routine recursively descends an RTL tree, turning
13607 it into Dwarf postfix code as it goes.
13609 MODE is the mode of the memory reference, needed to handle some
13610 autoincrement addressing modes.
13612 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13613 location list for RTL.
13615 Return 0 if we can't represent the location. */
13617 static dw_loc_descr_ref
13618 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13619 enum var_init_status initialized)
13621 dw_loc_descr_ref mem_loc_result = NULL;
13622 enum dwarf_location_atom op;
13623 dw_loc_descr_ref op0, op1;
13625 /* Note that for a dynamically sized array, the location we will generate a
13626 description of here will be the lowest numbered location which is
13627 actually within the array. That's *not* necessarily the same as the
13628 zeroth element of the array. */
13630 rtl = targetm.delegitimize_address (rtl);
13632 switch (GET_CODE (rtl))
13634 case POST_INC:
13635 case POST_DEC:
13636 case POST_MODIFY:
13637 return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13639 case SUBREG:
13640 /* The case of a subreg may arise when we have a local (register)
13641 variable or a formal (register) parameter which doesn't quite fill
13642 up an entire register. For now, just assume that it is
13643 legitimate to make the Dwarf info refer to the whole register which
13644 contains the given subreg. */
13645 if (!subreg_lowpart_p (rtl))
13646 break;
13647 rtl = SUBREG_REG (rtl);
13648 if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13649 break;
13650 if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13651 break;
13652 mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13653 break;
13655 case REG:
13656 /* Whenever a register number forms a part of the description of the
13657 method for calculating the (dynamic) address of a memory resident
13658 object, DWARF rules require the register number be referred to as
13659 a "base register". This distinction is not based in any way upon
13660 what category of register the hardware believes the given register
13661 belongs to. This is strictly DWARF terminology we're dealing with
13662 here. Note that in cases where the location of a memory-resident
13663 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13664 OP_CONST (0)) the actual DWARF location descriptor that we generate
13665 may just be OP_BASEREG (basereg). This may look deceptively like
13666 the object in question was allocated to a register (rather than in
13667 memory) so DWARF consumers need to be aware of the subtle
13668 distinction between OP_REG and OP_BASEREG. */
13669 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13670 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13671 else if (stack_realign_drap
13672 && crtl->drap_reg
13673 && crtl->args.internal_arg_pointer == rtl
13674 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13676 /* If RTL is internal_arg_pointer, which has been optimized
13677 out, use DRAP instead. */
13678 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13679 VAR_INIT_STATUS_INITIALIZED);
13681 break;
13683 case SIGN_EXTEND:
13684 case ZERO_EXTEND:
13685 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13686 VAR_INIT_STATUS_INITIALIZED);
13687 if (op0 == 0)
13688 break;
13689 else
13691 int shift = DWARF2_ADDR_SIZE
13692 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13693 shift *= BITS_PER_UNIT;
13694 if (GET_CODE (rtl) == SIGN_EXTEND)
13695 op = DW_OP_shra;
13696 else
13697 op = DW_OP_shr;
13698 mem_loc_result = op0;
13699 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13700 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13701 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13702 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13704 break;
13706 case MEM:
13707 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13708 VAR_INIT_STATUS_INITIALIZED);
13709 if (mem_loc_result == NULL)
13710 mem_loc_result = tls_mem_loc_descriptor (rtl);
13711 if (mem_loc_result != 0)
13713 if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13715 expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13716 return 0;
13718 else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13719 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13720 else
13721 add_loc_descr (&mem_loc_result,
13722 new_loc_descr (DW_OP_deref_size,
13723 GET_MODE_SIZE (GET_MODE (rtl)), 0));
13725 else
13727 rtx new_rtl = avoid_constant_pool_reference (rtl);
13728 if (new_rtl != rtl)
13729 return mem_loc_descriptor (new_rtl, mode, initialized);
13731 break;
13733 case LO_SUM:
13734 return mem_loc_descriptor (XEXP (rtl, 1), mode, initialized);
13736 case LABEL_REF:
13737 /* Some ports can transform a symbol ref into a label ref, because
13738 the symbol ref is too far away and has to be dumped into a constant
13739 pool. */
13740 case CONST:
13741 case SYMBOL_REF:
13742 if (GET_CODE (rtl) == SYMBOL_REF
13743 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13745 dw_loc_descr_ref temp;
13747 /* If this is not defined, we have no way to emit the data. */
13748 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13749 break;
13751 /* We used to emit DW_OP_addr here, but that's wrong, since
13752 DW_OP_addr should be relocated by the debug info consumer,
13753 while DW_OP_GNU_push_tls_address operand should not. */
13754 temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13755 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13756 temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13757 temp->dw_loc_oprnd1.v.val_addr = rtl;
13758 temp->dtprel = true;
13760 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13761 add_loc_descr (&mem_loc_result, temp);
13763 break;
13766 if (!const_ok_for_output (rtl))
13767 break;
13769 symref:
13770 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13771 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13772 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13773 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13774 break;
13776 case CONCAT:
13777 case CONCATN:
13778 case VAR_LOCATION:
13779 case DEBUG_IMPLICIT_PTR:
13780 expansion_failed (NULL_TREE, rtl,
13781 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13782 return 0;
13784 case ENTRY_VALUE:
13785 if (dwarf_strict)
13786 return NULL;
13787 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13788 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13789 if (REG_P (ENTRY_VALUE_EXP (rtl)))
13790 mem_loc_result->dw_loc_oprnd1.v.val_loc
13791 = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
13792 VAR_INIT_STATUS_INITIALIZED);
13793 else if (MEM_P (ENTRY_VALUE_EXP (rtl)) && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13795 dw_loc_descr_ref ref
13796 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), GET_MODE (rtl),
13797 VAR_INIT_STATUS_INITIALIZED);
13798 if (ref == NULL || ref->dw_loc_opc == DW_OP_fbreg)
13799 return NULL;
13800 mem_loc_result->dw_loc_oprnd1.v.val_loc = ref;
13802 else
13803 gcc_unreachable ();
13804 return mem_loc_result;
13806 case PRE_MODIFY:
13807 /* Extract the PLUS expression nested inside and fall into
13808 PLUS code below. */
13809 rtl = XEXP (rtl, 1);
13810 goto plus;
13812 case PRE_INC:
13813 case PRE_DEC:
13814 /* Turn these into a PLUS expression and fall into the PLUS code
13815 below. */
13816 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13817 GEN_INT (GET_CODE (rtl) == PRE_INC
13818 ? GET_MODE_UNIT_SIZE (mode)
13819 : -GET_MODE_UNIT_SIZE (mode)));
13821 /* ... fall through ... */
13823 case PLUS:
13824 plus:
13825 if (is_based_loc (rtl))
13826 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13827 INTVAL (XEXP (rtl, 1)),
13828 VAR_INIT_STATUS_INITIALIZED);
13829 else
13831 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13832 VAR_INIT_STATUS_INITIALIZED);
13833 if (mem_loc_result == 0)
13834 break;
13836 if (CONST_INT_P (XEXP (rtl, 1)))
13837 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13838 else
13840 dw_loc_descr_ref mem_loc_result2
13841 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13842 VAR_INIT_STATUS_INITIALIZED);
13843 if (mem_loc_result2 == 0)
13844 break;
13845 add_loc_descr (&mem_loc_result, mem_loc_result2);
13846 add_loc_descr (&mem_loc_result,
13847 new_loc_descr (DW_OP_plus, 0, 0));
13850 break;
13852 /* If a pseudo-reg is optimized away, it is possible for it to
13853 be replaced with a MEM containing a multiply or shift. */
13854 case MINUS:
13855 op = DW_OP_minus;
13856 goto do_binop;
13858 case MULT:
13859 op = DW_OP_mul;
13860 goto do_binop;
13862 case DIV:
13863 op = DW_OP_div;
13864 goto do_binop;
13866 case UMOD:
13867 op = DW_OP_mod;
13868 goto do_binop;
13870 case ASHIFT:
13871 op = DW_OP_shl;
13872 goto do_binop;
13874 case ASHIFTRT:
13875 op = DW_OP_shra;
13876 goto do_binop;
13878 case LSHIFTRT:
13879 op = DW_OP_shr;
13880 goto do_binop;
13882 case AND:
13883 op = DW_OP_and;
13884 goto do_binop;
13886 case IOR:
13887 op = DW_OP_or;
13888 goto do_binop;
13890 case XOR:
13891 op = DW_OP_xor;
13892 goto do_binop;
13894 do_binop:
13895 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13896 VAR_INIT_STATUS_INITIALIZED);
13897 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13898 VAR_INIT_STATUS_INITIALIZED);
13900 if (op0 == 0 || op1 == 0)
13901 break;
13903 mem_loc_result = op0;
13904 add_loc_descr (&mem_loc_result, op1);
13905 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13906 break;
13908 case MOD:
13909 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13910 VAR_INIT_STATUS_INITIALIZED);
13911 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13912 VAR_INIT_STATUS_INITIALIZED);
13914 if (op0 == 0 || op1 == 0)
13915 break;
13917 mem_loc_result = op0;
13918 add_loc_descr (&mem_loc_result, op1);
13919 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13920 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13921 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13922 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13923 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13924 break;
13926 case NOT:
13927 op = DW_OP_not;
13928 goto do_unop;
13930 case ABS:
13931 op = DW_OP_abs;
13932 goto do_unop;
13934 case NEG:
13935 op = DW_OP_neg;
13936 goto do_unop;
13938 do_unop:
13939 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13940 VAR_INIT_STATUS_INITIALIZED);
13942 if (op0 == 0)
13943 break;
13945 mem_loc_result = op0;
13946 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13947 break;
13949 case CONST_INT:
13950 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13951 break;
13953 case EQ:
13954 op = DW_OP_eq;
13955 goto do_scompare;
13957 case GE:
13958 op = DW_OP_ge;
13959 goto do_scompare;
13961 case GT:
13962 op = DW_OP_gt;
13963 goto do_scompare;
13965 case LE:
13966 op = DW_OP_le;
13967 goto do_scompare;
13969 case LT:
13970 op = DW_OP_lt;
13971 goto do_scompare;
13973 case NE:
13974 op = DW_OP_ne;
13975 goto do_scompare;
13977 do_scompare:
13978 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13979 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13980 break;
13981 else
13983 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13985 if (op_mode == VOIDmode)
13986 op_mode = GET_MODE (XEXP (rtl, 1));
13987 if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13988 break;
13990 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13991 VAR_INIT_STATUS_INITIALIZED);
13992 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13993 VAR_INIT_STATUS_INITIALIZED);
13995 if (op0 == 0 || op1 == 0)
13996 break;
13998 if (op_mode != VOIDmode
13999 && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14001 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14002 shift *= BITS_PER_UNIT;
14003 /* For eq/ne, if the operands are known to be zero-extended,
14004 there is no need to do the fancy shifting up. */
14005 if (op == DW_OP_eq || op == DW_OP_ne)
14007 dw_loc_descr_ref last0, last1;
14008 for (last0 = op0;
14009 last0->dw_loc_next != NULL;
14010 last0 = last0->dw_loc_next)
14012 for (last1 = op1;
14013 last1->dw_loc_next != NULL;
14014 last1 = last1->dw_loc_next)
14016 /* deref_size zero extends, and for constants we can check
14017 whether they are zero extended or not. */
14018 if (((last0->dw_loc_opc == DW_OP_deref_size
14019 && last0->dw_loc_oprnd1.v.val_int
14020 <= GET_MODE_SIZE (op_mode))
14021 || (CONST_INT_P (XEXP (rtl, 0))
14022 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14023 == (INTVAL (XEXP (rtl, 0))
14024 & GET_MODE_MASK (op_mode))))
14025 && ((last1->dw_loc_opc == DW_OP_deref_size
14026 && last1->dw_loc_oprnd1.v.val_int
14027 <= GET_MODE_SIZE (op_mode))
14028 || (CONST_INT_P (XEXP (rtl, 1))
14029 && (unsigned HOST_WIDE_INT)
14030 INTVAL (XEXP (rtl, 1))
14031 == (INTVAL (XEXP (rtl, 1))
14032 & GET_MODE_MASK (op_mode)))))
14033 goto do_compare;
14035 add_loc_descr (&op0, int_loc_descriptor (shift));
14036 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14037 if (CONST_INT_P (XEXP (rtl, 1)))
14038 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14039 else
14041 add_loc_descr (&op1, int_loc_descriptor (shift));
14042 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14047 do_compare:
14048 mem_loc_result = op0;
14049 add_loc_descr (&mem_loc_result, op1);
14050 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14051 if (STORE_FLAG_VALUE != 1)
14053 add_loc_descr (&mem_loc_result,
14054 int_loc_descriptor (STORE_FLAG_VALUE));
14055 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14057 break;
14059 case GEU:
14060 op = DW_OP_ge;
14061 goto do_ucompare;
14063 case GTU:
14064 op = DW_OP_gt;
14065 goto do_ucompare;
14067 case LEU:
14068 op = DW_OP_le;
14069 goto do_ucompare;
14071 case LTU:
14072 op = DW_OP_lt;
14073 goto do_ucompare;
14075 do_ucompare:
14076 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14077 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14078 break;
14079 else
14081 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14083 if (op_mode == VOIDmode)
14084 op_mode = GET_MODE (XEXP (rtl, 1));
14085 if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14086 break;
14088 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14089 VAR_INIT_STATUS_INITIALIZED);
14090 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14091 VAR_INIT_STATUS_INITIALIZED);
14093 if (op0 == 0 || op1 == 0)
14094 break;
14096 if (op_mode != VOIDmode
14097 && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14099 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14100 dw_loc_descr_ref last0, last1;
14101 for (last0 = op0;
14102 last0->dw_loc_next != NULL;
14103 last0 = last0->dw_loc_next)
14105 for (last1 = op1;
14106 last1->dw_loc_next != NULL;
14107 last1 = last1->dw_loc_next)
14109 if (CONST_INT_P (XEXP (rtl, 0)))
14110 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14111 /* deref_size zero extends, so no need to mask it again. */
14112 else if (last0->dw_loc_opc != DW_OP_deref_size
14113 || last0->dw_loc_oprnd1.v.val_int
14114 > GET_MODE_SIZE (op_mode))
14116 add_loc_descr (&op0, int_loc_descriptor (mask));
14117 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14119 if (CONST_INT_P (XEXP (rtl, 1)))
14120 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14121 /* deref_size zero extends, so no need to mask it again. */
14122 else if (last1->dw_loc_opc != DW_OP_deref_size
14123 || last1->dw_loc_oprnd1.v.val_int
14124 > GET_MODE_SIZE (op_mode))
14126 add_loc_descr (&op1, int_loc_descriptor (mask));
14127 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14130 else
14132 HOST_WIDE_INT bias = 1;
14133 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14134 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14135 if (CONST_INT_P (XEXP (rtl, 1)))
14136 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14137 + INTVAL (XEXP (rtl, 1)));
14138 else
14139 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14140 bias, 0));
14143 goto do_compare;
14145 case SMIN:
14146 case SMAX:
14147 case UMIN:
14148 case UMAX:
14149 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14150 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14151 || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14152 break;
14154 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14155 VAR_INIT_STATUS_INITIALIZED);
14156 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14157 VAR_INIT_STATUS_INITIALIZED);
14159 if (op0 == 0 || op1 == 0)
14160 break;
14162 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14163 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14164 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14165 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14167 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14169 HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14170 add_loc_descr (&op0, int_loc_descriptor (mask));
14171 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14172 add_loc_descr (&op1, int_loc_descriptor (mask));
14173 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14175 else
14177 HOST_WIDE_INT bias = 1;
14178 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14179 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14180 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14183 else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14185 int shift = DWARF2_ADDR_SIZE
14186 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14187 shift *= BITS_PER_UNIT;
14188 add_loc_descr (&op0, int_loc_descriptor (shift));
14189 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14190 add_loc_descr (&op1, int_loc_descriptor (shift));
14191 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14194 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14195 op = DW_OP_lt;
14196 else
14197 op = DW_OP_gt;
14198 mem_loc_result = op0;
14199 add_loc_descr (&mem_loc_result, op1);
14200 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14202 dw_loc_descr_ref bra_node, drop_node;
14204 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14205 add_loc_descr (&mem_loc_result, bra_node);
14206 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14207 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14208 add_loc_descr (&mem_loc_result, drop_node);
14209 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14210 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14212 break;
14214 case ZERO_EXTRACT:
14215 case SIGN_EXTRACT:
14216 if (CONST_INT_P (XEXP (rtl, 1))
14217 && CONST_INT_P (XEXP (rtl, 2))
14218 && ((unsigned) INTVAL (XEXP (rtl, 1))
14219 + (unsigned) INTVAL (XEXP (rtl, 2))
14220 <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14221 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14222 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14224 int shift, size;
14225 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14226 VAR_INIT_STATUS_INITIALIZED);
14227 if (op0 == 0)
14228 break;
14229 if (GET_CODE (rtl) == SIGN_EXTRACT)
14230 op = DW_OP_shra;
14231 else
14232 op = DW_OP_shr;
14233 mem_loc_result = op0;
14234 size = INTVAL (XEXP (rtl, 1));
14235 shift = INTVAL (XEXP (rtl, 2));
14236 if (BITS_BIG_ENDIAN)
14237 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14238 - shift - size;
14239 if (shift + size != (int) DWARF2_ADDR_SIZE)
14241 add_loc_descr (&mem_loc_result,
14242 int_loc_descriptor (DWARF2_ADDR_SIZE
14243 - shift - size));
14244 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14246 if (size != (int) DWARF2_ADDR_SIZE)
14248 add_loc_descr (&mem_loc_result,
14249 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14250 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14253 break;
14255 case IF_THEN_ELSE:
14257 dw_loc_descr_ref op2, bra_node, drop_node;
14258 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14259 VAR_INIT_STATUS_INITIALIZED);
14260 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14261 VAR_INIT_STATUS_INITIALIZED);
14262 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14263 VAR_INIT_STATUS_INITIALIZED);
14264 if (op0 == NULL || op1 == NULL || op2 == NULL)
14265 break;
14267 mem_loc_result = op1;
14268 add_loc_descr (&mem_loc_result, op2);
14269 add_loc_descr (&mem_loc_result, op0);
14270 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14271 add_loc_descr (&mem_loc_result, bra_node);
14272 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14273 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14274 add_loc_descr (&mem_loc_result, drop_node);
14275 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14276 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14278 break;
14280 case COMPARE:
14281 case ROTATE:
14282 case ROTATERT:
14283 case TRUNCATE:
14284 /* In theory, we could implement the above. */
14285 /* DWARF cannot represent the unsigned compare operations
14286 natively. */
14287 case SS_MULT:
14288 case US_MULT:
14289 case SS_DIV:
14290 case US_DIV:
14291 case SS_PLUS:
14292 case US_PLUS:
14293 case SS_MINUS:
14294 case US_MINUS:
14295 case SS_NEG:
14296 case US_NEG:
14297 case SS_ABS:
14298 case SS_ASHIFT:
14299 case US_ASHIFT:
14300 case SS_TRUNCATE:
14301 case US_TRUNCATE:
14302 case UDIV:
14303 case UNORDERED:
14304 case ORDERED:
14305 case UNEQ:
14306 case UNGE:
14307 case UNGT:
14308 case UNLE:
14309 case UNLT:
14310 case LTGT:
14311 case FLOAT_EXTEND:
14312 case FLOAT_TRUNCATE:
14313 case FLOAT:
14314 case UNSIGNED_FLOAT:
14315 case FIX:
14316 case UNSIGNED_FIX:
14317 case FRACT_CONVERT:
14318 case UNSIGNED_FRACT_CONVERT:
14319 case SAT_FRACT:
14320 case UNSIGNED_SAT_FRACT:
14321 case SQRT:
14322 case BSWAP:
14323 case FFS:
14324 case CLZ:
14325 case CTZ:
14326 case POPCOUNT:
14327 case PARITY:
14328 case ASM_OPERANDS:
14329 case VEC_MERGE:
14330 case VEC_SELECT:
14331 case VEC_CONCAT:
14332 case VEC_DUPLICATE:
14333 case UNSPEC:
14334 case HIGH:
14335 /* If delegitimize_address couldn't do anything with the UNSPEC, we
14336 can't express it in the debug info. This can happen e.g. with some
14337 TLS UNSPECs. */
14338 break;
14340 case CONST_STRING:
14341 resolve_one_addr (&rtl, NULL);
14342 goto symref;
14344 default:
14345 #ifdef ENABLE_CHECKING
14346 print_rtl (stderr, rtl);
14347 gcc_unreachable ();
14348 #else
14349 break;
14350 #endif
14353 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14354 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14356 return mem_loc_result;
14359 /* Return a descriptor that describes the concatenation of two locations.
14360 This is typically a complex variable. */
14362 static dw_loc_descr_ref
14363 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14365 dw_loc_descr_ref cc_loc_result = NULL;
14366 dw_loc_descr_ref x0_ref
14367 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14368 dw_loc_descr_ref x1_ref
14369 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14371 if (x0_ref == 0 || x1_ref == 0)
14372 return 0;
14374 cc_loc_result = x0_ref;
14375 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14377 add_loc_descr (&cc_loc_result, x1_ref);
14378 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14380 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14381 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14383 return cc_loc_result;
14386 /* Return a descriptor that describes the concatenation of N
14387 locations. */
14389 static dw_loc_descr_ref
14390 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14392 unsigned int i;
14393 dw_loc_descr_ref cc_loc_result = NULL;
14394 unsigned int n = XVECLEN (concatn, 0);
14396 for (i = 0; i < n; ++i)
14398 dw_loc_descr_ref ref;
14399 rtx x = XVECEXP (concatn, 0, i);
14401 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14402 if (ref == NULL)
14403 return NULL;
14405 add_loc_descr (&cc_loc_result, ref);
14406 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14409 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14410 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14412 return cc_loc_result;
14415 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
14416 for DEBUG_IMPLICIT_PTR RTL. */
14418 static dw_loc_descr_ref
14419 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14421 dw_loc_descr_ref ret;
14422 dw_die_ref ref;
14424 if (dwarf_strict)
14425 return NULL;
14426 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14427 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14428 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14429 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14430 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14431 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14432 if (ref)
14434 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14435 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14436 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14438 else
14440 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14441 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14443 return ret;
14446 /* Output a proper Dwarf location descriptor for a variable or parameter
14447 which is either allocated in a register or in a memory location. For a
14448 register, we just generate an OP_REG and the register number. For a
14449 memory location we provide a Dwarf postfix expression describing how to
14450 generate the (dynamic) address of the object onto the address stack.
14452 MODE is mode of the decl if this loc_descriptor is going to be used in
14453 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14454 allowed, VOIDmode otherwise.
14456 If we don't know how to describe it, return 0. */
14458 static dw_loc_descr_ref
14459 loc_descriptor (rtx rtl, enum machine_mode mode,
14460 enum var_init_status initialized)
14462 dw_loc_descr_ref loc_result = NULL;
14464 switch (GET_CODE (rtl))
14466 case SUBREG:
14467 /* The case of a subreg may arise when we have a local (register)
14468 variable or a formal (register) parameter which doesn't quite fill
14469 up an entire register. For now, just assume that it is
14470 legitimate to make the Dwarf info refer to the whole register which
14471 contains the given subreg. */
14472 loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14473 break;
14475 case REG:
14476 loc_result = reg_loc_descriptor (rtl, initialized);
14477 break;
14479 case MEM:
14480 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14481 initialized);
14482 if (loc_result == NULL)
14483 loc_result = tls_mem_loc_descriptor (rtl);
14484 if (loc_result == NULL)
14486 rtx new_rtl = avoid_constant_pool_reference (rtl);
14487 if (new_rtl != rtl)
14488 loc_result = loc_descriptor (new_rtl, mode, initialized);
14490 break;
14492 case CONCAT:
14493 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14494 initialized);
14495 break;
14497 case CONCATN:
14498 loc_result = concatn_loc_descriptor (rtl, initialized);
14499 break;
14501 case VAR_LOCATION:
14502 /* Single part. */
14503 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14505 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14506 if (GET_CODE (loc) == EXPR_LIST)
14507 loc = XEXP (loc, 0);
14508 loc_result = loc_descriptor (loc, mode, initialized);
14509 break;
14512 rtl = XEXP (rtl, 1);
14513 /* FALLTHRU */
14515 case PARALLEL:
14517 rtvec par_elems = XVEC (rtl, 0);
14518 int num_elem = GET_NUM_ELEM (par_elems);
14519 enum machine_mode mode;
14520 int i;
14522 /* Create the first one, so we have something to add to. */
14523 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14524 VOIDmode, initialized);
14525 if (loc_result == NULL)
14526 return NULL;
14527 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14528 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14529 for (i = 1; i < num_elem; i++)
14531 dw_loc_descr_ref temp;
14533 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14534 VOIDmode, initialized);
14535 if (temp == NULL)
14536 return NULL;
14537 add_loc_descr (&loc_result, temp);
14538 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14539 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14542 break;
14544 case CONST_INT:
14545 if (mode != VOIDmode && mode != BLKmode)
14546 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14547 INTVAL (rtl));
14548 break;
14550 case CONST_DOUBLE:
14551 if (mode == VOIDmode)
14552 mode = GET_MODE (rtl);
14554 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14556 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14558 /* Note that a CONST_DOUBLE rtx could represent either an integer
14559 or a floating-point constant. A CONST_DOUBLE is used whenever
14560 the constant requires more than one word in order to be
14561 adequately represented. We output CONST_DOUBLEs as blocks. */
14562 loc_result = new_loc_descr (DW_OP_implicit_value,
14563 GET_MODE_SIZE (mode), 0);
14564 if (SCALAR_FLOAT_MODE_P (mode))
14566 unsigned int length = GET_MODE_SIZE (mode);
14567 unsigned char *array
14568 = (unsigned char*) ggc_alloc_atomic (length);
14570 insert_float (rtl, array);
14571 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14572 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14573 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14574 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14576 else
14578 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14579 loc_result->dw_loc_oprnd2.v.val_double
14580 = rtx_to_double_int (rtl);
14583 break;
14585 case CONST_VECTOR:
14586 if (mode == VOIDmode)
14587 mode = GET_MODE (rtl);
14589 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14591 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14592 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14593 unsigned char *array = (unsigned char *)
14594 ggc_alloc_atomic (length * elt_size);
14595 unsigned int i;
14596 unsigned char *p;
14598 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14599 switch (GET_MODE_CLASS (mode))
14601 case MODE_VECTOR_INT:
14602 for (i = 0, p = array; i < length; i++, p += elt_size)
14604 rtx elt = CONST_VECTOR_ELT (rtl, i);
14605 double_int val = rtx_to_double_int (elt);
14607 if (elt_size <= sizeof (HOST_WIDE_INT))
14608 insert_int (double_int_to_shwi (val), elt_size, p);
14609 else
14611 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14612 insert_double (val, p);
14615 break;
14617 case MODE_VECTOR_FLOAT:
14618 for (i = 0, p = array; i < length; i++, p += elt_size)
14620 rtx elt = CONST_VECTOR_ELT (rtl, i);
14621 insert_float (elt, p);
14623 break;
14625 default:
14626 gcc_unreachable ();
14629 loc_result = new_loc_descr (DW_OP_implicit_value,
14630 length * elt_size, 0);
14631 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14632 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14633 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14634 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14636 break;
14638 case CONST:
14639 if (mode == VOIDmode
14640 || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14641 || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14642 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14644 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14645 break;
14647 /* FALLTHROUGH */
14648 case SYMBOL_REF:
14649 if (!const_ok_for_output (rtl))
14650 break;
14651 case LABEL_REF:
14652 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14653 && (dwarf_version >= 4 || !dwarf_strict))
14655 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14656 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14657 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14658 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14659 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14661 break;
14663 case DEBUG_IMPLICIT_PTR:
14664 loc_result = implicit_ptr_descriptor (rtl, 0);
14665 break;
14667 case PLUS:
14668 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14669 && CONST_INT_P (XEXP (rtl, 1)))
14671 loc_result
14672 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14673 break;
14675 /* FALLTHRU */
14676 default:
14677 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14678 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14679 && (dwarf_version >= 4 || !dwarf_strict))
14681 /* Value expression. */
14682 loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14683 if (loc_result)
14684 add_loc_descr (&loc_result,
14685 new_loc_descr (DW_OP_stack_value, 0, 0));
14687 break;
14690 return loc_result;
14693 /* We need to figure out what section we should use as the base for the
14694 address ranges where a given location is valid.
14695 1. If this particular DECL has a section associated with it, use that.
14696 2. If this function has a section associated with it, use that.
14697 3. Otherwise, use the text section.
14698 XXX: If you split a variable across multiple sections, we won't notice. */
14700 static const char *
14701 secname_for_decl (const_tree decl)
14703 const char *secname;
14705 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14707 tree sectree = DECL_SECTION_NAME (decl);
14708 secname = TREE_STRING_POINTER (sectree);
14710 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14712 tree sectree = DECL_SECTION_NAME (current_function_decl);
14713 secname = TREE_STRING_POINTER (sectree);
14715 else if (cfun && in_cold_section_p)
14716 secname = crtl->subsections.cold_section_label;
14717 else
14718 secname = text_section_label;
14720 return secname;
14723 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
14725 static bool
14726 decl_by_reference_p (tree decl)
14728 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14729 || TREE_CODE (decl) == VAR_DECL)
14730 && DECL_BY_REFERENCE (decl));
14733 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14734 for VARLOC. */
14736 static dw_loc_descr_ref
14737 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14738 enum var_init_status initialized)
14740 int have_address = 0;
14741 dw_loc_descr_ref descr;
14742 enum machine_mode mode;
14744 if (want_address != 2)
14746 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14747 /* Single part. */
14748 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14750 varloc = PAT_VAR_LOCATION_LOC (varloc);
14751 if (GET_CODE (varloc) == EXPR_LIST)
14752 varloc = XEXP (varloc, 0);
14753 mode = GET_MODE (varloc);
14754 if (MEM_P (varloc))
14756 rtx addr = XEXP (varloc, 0);
14757 descr = mem_loc_descriptor (addr, mode, initialized);
14758 if (descr)
14759 have_address = 1;
14760 else
14762 rtx x = avoid_constant_pool_reference (varloc);
14763 if (x != varloc)
14764 descr = mem_loc_descriptor (x, mode, initialized);
14767 else
14768 descr = mem_loc_descriptor (varloc, mode, initialized);
14770 else
14771 return 0;
14773 else
14775 if (GET_CODE (varloc) == VAR_LOCATION)
14776 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14777 else
14778 mode = DECL_MODE (loc);
14779 descr = loc_descriptor (varloc, mode, initialized);
14780 have_address = 1;
14783 if (!descr)
14784 return 0;
14786 if (want_address == 2 && !have_address
14787 && (dwarf_version >= 4 || !dwarf_strict))
14789 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14791 expansion_failed (loc, NULL_RTX,
14792 "DWARF address size mismatch");
14793 return 0;
14795 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14796 have_address = 1;
14798 /* Show if we can't fill the request for an address. */
14799 if (want_address && !have_address)
14801 expansion_failed (loc, NULL_RTX,
14802 "Want address and only have value");
14803 return 0;
14806 /* If we've got an address and don't want one, dereference. */
14807 if (!want_address && have_address)
14809 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14810 enum dwarf_location_atom op;
14812 if (size > DWARF2_ADDR_SIZE || size == -1)
14814 expansion_failed (loc, NULL_RTX,
14815 "DWARF address size mismatch");
14816 return 0;
14818 else if (size == DWARF2_ADDR_SIZE)
14819 op = DW_OP_deref;
14820 else
14821 op = DW_OP_deref_size;
14823 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14826 return descr;
14829 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14830 if it is not possible. */
14832 static dw_loc_descr_ref
14833 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14835 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14836 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14837 else if (dwarf_version >= 3 || !dwarf_strict)
14838 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14839 else
14840 return NULL;
14843 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14844 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14846 static dw_loc_descr_ref
14847 dw_sra_loc_expr (tree decl, rtx loc)
14849 rtx p;
14850 unsigned int padsize = 0;
14851 dw_loc_descr_ref descr, *descr_tail;
14852 unsigned HOST_WIDE_INT decl_size;
14853 rtx varloc;
14854 enum var_init_status initialized;
14856 if (DECL_SIZE (decl) == NULL
14857 || !host_integerp (DECL_SIZE (decl), 1))
14858 return NULL;
14860 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14861 descr = NULL;
14862 descr_tail = &descr;
14864 for (p = loc; p; p = XEXP (p, 1))
14866 unsigned int bitsize = decl_piece_bitsize (p);
14867 rtx loc_note = *decl_piece_varloc_ptr (p);
14868 dw_loc_descr_ref cur_descr;
14869 dw_loc_descr_ref *tail, last = NULL;
14870 unsigned int opsize = 0;
14872 if (loc_note == NULL_RTX
14873 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14875 padsize += bitsize;
14876 continue;
14878 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14879 varloc = NOTE_VAR_LOCATION (loc_note);
14880 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14881 if (cur_descr == NULL)
14883 padsize += bitsize;
14884 continue;
14887 /* Check that cur_descr either doesn't use
14888 DW_OP_*piece operations, or their sum is equal
14889 to bitsize. Otherwise we can't embed it. */
14890 for (tail = &cur_descr; *tail != NULL;
14891 tail = &(*tail)->dw_loc_next)
14892 if ((*tail)->dw_loc_opc == DW_OP_piece)
14894 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14895 * BITS_PER_UNIT;
14896 last = *tail;
14898 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14900 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14901 last = *tail;
14904 if (last != NULL && opsize != bitsize)
14906 padsize += bitsize;
14907 continue;
14910 /* If there is a hole, add DW_OP_*piece after empty DWARF
14911 expression, which means that those bits are optimized out. */
14912 if (padsize)
14914 if (padsize > decl_size)
14915 return NULL;
14916 decl_size -= padsize;
14917 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14918 if (*descr_tail == NULL)
14919 return NULL;
14920 descr_tail = &(*descr_tail)->dw_loc_next;
14921 padsize = 0;
14923 *descr_tail = cur_descr;
14924 descr_tail = tail;
14925 if (bitsize > decl_size)
14926 return NULL;
14927 decl_size -= bitsize;
14928 if (last == NULL)
14930 HOST_WIDE_INT offset = 0;
14931 if (GET_CODE (varloc) == VAR_LOCATION
14932 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14934 varloc = PAT_VAR_LOCATION_LOC (varloc);
14935 if (GET_CODE (varloc) == EXPR_LIST)
14936 varloc = XEXP (varloc, 0);
14940 if (GET_CODE (varloc) == CONST
14941 || GET_CODE (varloc) == SIGN_EXTEND
14942 || GET_CODE (varloc) == ZERO_EXTEND)
14943 varloc = XEXP (varloc, 0);
14944 else if (GET_CODE (varloc) == SUBREG)
14945 varloc = SUBREG_REG (varloc);
14946 else
14947 break;
14949 while (1);
14950 /* DW_OP_bit_size offset should be zero for register
14951 or implicit location descriptions and empty location
14952 descriptions, but for memory addresses needs big endian
14953 adjustment. */
14954 if (MEM_P (varloc))
14956 unsigned HOST_WIDE_INT memsize
14957 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
14958 if (memsize != bitsize)
14960 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14961 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14962 return NULL;
14963 if (memsize < bitsize)
14964 return NULL;
14965 if (BITS_BIG_ENDIAN)
14966 offset = memsize - bitsize;
14970 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14971 if (*descr_tail == NULL)
14972 return NULL;
14973 descr_tail = &(*descr_tail)->dw_loc_next;
14977 /* If there were any non-empty expressions, add padding till the end of
14978 the decl. */
14979 if (descr != NULL && decl_size != 0)
14981 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14982 if (*descr_tail == NULL)
14983 return NULL;
14985 return descr;
14988 /* Return the dwarf representation of the location list LOC_LIST of
14989 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14990 function. */
14992 static dw_loc_list_ref
14993 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14995 const char *endname, *secname;
14996 rtx varloc;
14997 enum var_init_status initialized;
14998 struct var_loc_node *node;
14999 dw_loc_descr_ref descr;
15000 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15001 dw_loc_list_ref list = NULL;
15002 dw_loc_list_ref *listp = &list;
15004 /* Now that we know what section we are using for a base,
15005 actually construct the list of locations.
15006 The first location information is what is passed to the
15007 function that creates the location list, and the remaining
15008 locations just get added on to that list.
15009 Note that we only know the start address for a location
15010 (IE location changes), so to build the range, we use
15011 the range [current location start, next location start].
15012 This means we have to special case the last node, and generate
15013 a range of [last location start, end of function label]. */
15015 secname = secname_for_decl (decl);
15017 for (node = loc_list->first; node; node = node->next)
15018 if (GET_CODE (node->loc) == EXPR_LIST
15019 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15021 if (GET_CODE (node->loc) == EXPR_LIST)
15023 /* This requires DW_OP_{,bit_}piece, which is not usable
15024 inside DWARF expressions. */
15025 if (want_address != 2)
15026 continue;
15027 descr = dw_sra_loc_expr (decl, node->loc);
15028 if (descr == NULL)
15029 continue;
15031 else
15033 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15034 varloc = NOTE_VAR_LOCATION (node->loc);
15035 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15037 if (descr)
15039 bool range_across_switch = false;
15040 /* If section switch happens in between node->label
15041 and node->next->label (or end of function) and
15042 we can't emit it as a single entry list,
15043 emit two ranges, first one ending at the end
15044 of first partition and second one starting at the
15045 beginning of second partition. */
15046 if (node == loc_list->last_before_switch
15047 && (node != loc_list->first || loc_list->first->next)
15048 && current_function_decl)
15050 endname = current_fde ()->dw_fde_end;
15051 range_across_switch = true;
15053 /* The variable has a location between NODE->LABEL and
15054 NODE->NEXT->LABEL. */
15055 else if (node->next)
15056 endname = node->next->label;
15057 /* If the variable has a location at the last label
15058 it keeps its location until the end of function. */
15059 else if (!current_function_decl)
15060 endname = text_end_label;
15061 else
15063 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15064 current_function_funcdef_no);
15065 endname = ggc_strdup (label_id);
15068 *listp = new_loc_list (descr, node->label, endname, secname);
15069 listp = &(*listp)->dw_loc_next;
15071 if (range_across_switch)
15073 if (GET_CODE (node->loc) == EXPR_LIST)
15074 descr = dw_sra_loc_expr (decl, node->loc);
15075 else
15077 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15078 varloc = NOTE_VAR_LOCATION (node->loc);
15079 descr = dw_loc_list_1 (decl, varloc, want_address,
15080 initialized);
15082 gcc_assert (descr);
15083 /* The variable has a location between NODE->LABEL and
15084 NODE->NEXT->LABEL. */
15085 if (node->next)
15086 endname = node->next->label;
15087 else
15088 endname = current_fde ()->dw_fde_second_end;
15089 *listp = new_loc_list (descr,
15090 current_fde ()->dw_fde_second_begin,
15091 endname, secname);
15092 listp = &(*listp)->dw_loc_next;
15097 /* Try to avoid the overhead of a location list emitting a location
15098 expression instead, but only if we didn't have more than one
15099 location entry in the first place. If some entries were not
15100 representable, we don't want to pretend a single entry that was
15101 applies to the entire scope in which the variable is
15102 available. */
15103 if (list && loc_list->first->next)
15104 gen_llsym (list);
15106 return list;
15109 /* Return if the loc_list has only single element and thus can be represented
15110 as location description. */
15112 static bool
15113 single_element_loc_list_p (dw_loc_list_ref list)
15115 gcc_assert (!list->dw_loc_next || list->ll_symbol);
15116 return !list->ll_symbol;
15119 /* To each location in list LIST add loc descr REF. */
15121 static void
15122 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15124 dw_loc_descr_ref copy;
15125 add_loc_descr (&list->expr, ref);
15126 list = list->dw_loc_next;
15127 while (list)
15129 copy = ggc_alloc_dw_loc_descr_node ();
15130 memcpy (copy, ref, sizeof (dw_loc_descr_node));
15131 add_loc_descr (&list->expr, copy);
15132 while (copy->dw_loc_next)
15134 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15135 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15136 copy->dw_loc_next = new_copy;
15137 copy = new_copy;
15139 list = list->dw_loc_next;
15143 /* Given two lists RET and LIST
15144 produce location list that is result of adding expression in LIST
15145 to expression in RET on each possition in program.
15146 Might be destructive on both RET and LIST.
15148 TODO: We handle only simple cases of RET or LIST having at most one
15149 element. General case would inolve sorting the lists in program order
15150 and merging them that will need some additional work.
15151 Adding that will improve quality of debug info especially for SRA-ed
15152 structures. */
15154 static void
15155 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15157 if (!list)
15158 return;
15159 if (!*ret)
15161 *ret = list;
15162 return;
15164 if (!list->dw_loc_next)
15166 add_loc_descr_to_each (*ret, list->expr);
15167 return;
15169 if (!(*ret)->dw_loc_next)
15171 add_loc_descr_to_each (list, (*ret)->expr);
15172 *ret = list;
15173 return;
15175 expansion_failed (NULL_TREE, NULL_RTX,
15176 "Don't know how to merge two non-trivial"
15177 " location lists.\n");
15178 *ret = NULL;
15179 return;
15182 /* LOC is constant expression. Try a luck, look it up in constant
15183 pool and return its loc_descr of its address. */
15185 static dw_loc_descr_ref
15186 cst_pool_loc_descr (tree loc)
15188 /* Get an RTL for this, if something has been emitted. */
15189 rtx rtl = lookup_constant_def (loc);
15190 enum machine_mode mode;
15192 if (!rtl || !MEM_P (rtl))
15194 gcc_assert (!rtl);
15195 return 0;
15197 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15199 /* TODO: We might get more coverage if we was actually delaying expansion
15200 of all expressions till end of compilation when constant pools are fully
15201 populated. */
15202 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15204 expansion_failed (loc, NULL_RTX,
15205 "CST value in contant pool but not marked.");
15206 return 0;
15208 mode = GET_MODE (rtl);
15209 rtl = XEXP (rtl, 0);
15210 return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15213 /* Return dw_loc_list representing address of addr_expr LOC
15214 by looking for innder INDIRECT_REF expression and turing it
15215 into simple arithmetics. */
15217 static dw_loc_list_ref
15218 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15220 tree obj, offset;
15221 HOST_WIDE_INT bitsize, bitpos, bytepos;
15222 enum machine_mode mode;
15223 int volatilep;
15224 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15225 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15227 obj = get_inner_reference (TREE_OPERAND (loc, 0),
15228 &bitsize, &bitpos, &offset, &mode,
15229 &unsignedp, &volatilep, false);
15230 STRIP_NOPS (obj);
15231 if (bitpos % BITS_PER_UNIT)
15233 expansion_failed (loc, NULL_RTX, "bitfield access");
15234 return 0;
15236 if (!INDIRECT_REF_P (obj))
15238 expansion_failed (obj,
15239 NULL_RTX, "no indirect ref in inner refrence");
15240 return 0;
15242 if (!offset && !bitpos)
15243 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15244 else if (toplev
15245 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15246 && (dwarf_version >= 4 || !dwarf_strict))
15248 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15249 if (!list_ret)
15250 return 0;
15251 if (offset)
15253 /* Variable offset. */
15254 list_ret1 = loc_list_from_tree (offset, 0);
15255 if (list_ret1 == 0)
15256 return 0;
15257 add_loc_list (&list_ret, list_ret1);
15258 if (!list_ret)
15259 return 0;
15260 add_loc_descr_to_each (list_ret,
15261 new_loc_descr (DW_OP_plus, 0, 0));
15263 bytepos = bitpos / BITS_PER_UNIT;
15264 if (bytepos > 0)
15265 add_loc_descr_to_each (list_ret,
15266 new_loc_descr (DW_OP_plus_uconst,
15267 bytepos, 0));
15268 else if (bytepos < 0)
15269 loc_list_plus_const (list_ret, bytepos);
15270 add_loc_descr_to_each (list_ret,
15271 new_loc_descr (DW_OP_stack_value, 0, 0));
15273 return list_ret;
15277 /* Generate Dwarf location list representing LOC.
15278 If WANT_ADDRESS is false, expression computing LOC will be computed
15279 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15280 if WANT_ADDRESS is 2, expression computing address useable in location
15281 will be returned (i.e. DW_OP_reg can be used
15282 to refer to register values). */
15284 static dw_loc_list_ref
15285 loc_list_from_tree (tree loc, int want_address)
15287 dw_loc_descr_ref ret = NULL, ret1 = NULL;
15288 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15289 int have_address = 0;
15290 enum dwarf_location_atom op;
15292 /* ??? Most of the time we do not take proper care for sign/zero
15293 extending the values properly. Hopefully this won't be a real
15294 problem... */
15296 switch (TREE_CODE (loc))
15298 case ERROR_MARK:
15299 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15300 return 0;
15302 case PLACEHOLDER_EXPR:
15303 /* This case involves extracting fields from an object to determine the
15304 position of other fields. We don't try to encode this here. The
15305 only user of this is Ada, which encodes the needed information using
15306 the names of types. */
15307 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15308 return 0;
15310 case CALL_EXPR:
15311 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15312 /* There are no opcodes for these operations. */
15313 return 0;
15315 case PREINCREMENT_EXPR:
15316 case PREDECREMENT_EXPR:
15317 case POSTINCREMENT_EXPR:
15318 case POSTDECREMENT_EXPR:
15319 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15320 /* There are no opcodes for these operations. */
15321 return 0;
15323 case ADDR_EXPR:
15324 /* If we already want an address, see if there is INDIRECT_REF inside
15325 e.g. for &this->field. */
15326 if (want_address)
15328 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15329 (loc, want_address == 2);
15330 if (list_ret)
15331 have_address = 1;
15332 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15333 && (ret = cst_pool_loc_descr (loc)))
15334 have_address = 1;
15336 /* Otherwise, process the argument and look for the address. */
15337 if (!list_ret && !ret)
15338 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15339 else
15341 if (want_address)
15342 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15343 return NULL;
15345 break;
15347 case VAR_DECL:
15348 if (DECL_THREAD_LOCAL_P (loc))
15350 rtx rtl;
15351 enum dwarf_location_atom first_op;
15352 enum dwarf_location_atom second_op;
15353 bool dtprel = false;
15355 if (targetm.have_tls)
15357 /* If this is not defined, we have no way to emit the
15358 data. */
15359 if (!targetm.asm_out.output_dwarf_dtprel)
15360 return 0;
15362 /* The way DW_OP_GNU_push_tls_address is specified, we
15363 can only look up addresses of objects in the current
15364 module. We used DW_OP_addr as first op, but that's
15365 wrong, because DW_OP_addr is relocated by the debug
15366 info consumer, while DW_OP_GNU_push_tls_address
15367 operand shouldn't be. */
15368 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15369 return 0;
15370 first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15371 dtprel = true;
15372 second_op = DW_OP_GNU_push_tls_address;
15374 else
15376 if (!targetm.emutls.debug_form_tls_address
15377 || !(dwarf_version >= 3 || !dwarf_strict))
15378 return 0;
15379 /* We stuffed the control variable into the DECL_VALUE_EXPR
15380 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15381 no longer appear in gimple code. We used the control
15382 variable in specific so that we could pick it up here. */
15383 loc = DECL_VALUE_EXPR (loc);
15384 first_op = DW_OP_addr;
15385 second_op = DW_OP_form_tls_address;
15388 rtl = rtl_for_decl_location (loc);
15389 if (rtl == NULL_RTX)
15390 return 0;
15392 if (!MEM_P (rtl))
15393 return 0;
15394 rtl = XEXP (rtl, 0);
15395 if (! CONSTANT_P (rtl))
15396 return 0;
15398 ret = new_loc_descr (first_op, 0, 0);
15399 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15400 ret->dw_loc_oprnd1.v.val_addr = rtl;
15401 ret->dtprel = dtprel;
15403 ret1 = new_loc_descr (second_op, 0, 0);
15404 add_loc_descr (&ret, ret1);
15406 have_address = 1;
15407 break;
15409 /* FALLTHRU */
15411 case PARM_DECL:
15412 case RESULT_DECL:
15413 if (DECL_HAS_VALUE_EXPR_P (loc))
15414 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15415 want_address);
15416 /* FALLTHRU */
15418 case FUNCTION_DECL:
15420 rtx rtl;
15421 var_loc_list *loc_list = lookup_decl_loc (loc);
15423 if (loc_list && loc_list->first)
15425 list_ret = dw_loc_list (loc_list, loc, want_address);
15426 have_address = want_address != 0;
15427 break;
15429 rtl = rtl_for_decl_location (loc);
15430 if (rtl == NULL_RTX)
15432 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15433 return 0;
15435 else if (CONST_INT_P (rtl))
15437 HOST_WIDE_INT val = INTVAL (rtl);
15438 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15439 val &= GET_MODE_MASK (DECL_MODE (loc));
15440 ret = int_loc_descriptor (val);
15442 else if (GET_CODE (rtl) == CONST_STRING)
15444 expansion_failed (loc, NULL_RTX, "CONST_STRING");
15445 return 0;
15447 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15449 ret = new_loc_descr (DW_OP_addr, 0, 0);
15450 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15451 ret->dw_loc_oprnd1.v.val_addr = rtl;
15453 else
15455 enum machine_mode mode;
15457 /* Certain constructs can only be represented at top-level. */
15458 if (want_address == 2)
15460 ret = loc_descriptor (rtl, VOIDmode,
15461 VAR_INIT_STATUS_INITIALIZED);
15462 have_address = 1;
15464 else
15466 mode = GET_MODE (rtl);
15467 if (MEM_P (rtl))
15469 rtl = XEXP (rtl, 0);
15470 have_address = 1;
15472 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15474 if (!ret)
15475 expansion_failed (loc, rtl,
15476 "failed to produce loc descriptor for rtl");
15479 break;
15481 case MEM_REF:
15482 /* ??? FIXME. */
15483 if (!integer_zerop (TREE_OPERAND (loc, 1)))
15484 return 0;
15485 /* Fallthru. */
15486 case INDIRECT_REF:
15487 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15488 have_address = 1;
15489 break;
15491 case COMPOUND_EXPR:
15492 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15494 CASE_CONVERT:
15495 case VIEW_CONVERT_EXPR:
15496 case SAVE_EXPR:
15497 case MODIFY_EXPR:
15498 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15500 case COMPONENT_REF:
15501 case BIT_FIELD_REF:
15502 case ARRAY_REF:
15503 case ARRAY_RANGE_REF:
15504 case REALPART_EXPR:
15505 case IMAGPART_EXPR:
15507 tree obj, offset;
15508 HOST_WIDE_INT bitsize, bitpos, bytepos;
15509 enum machine_mode mode;
15510 int volatilep;
15511 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15513 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15514 &unsignedp, &volatilep, false);
15516 gcc_assert (obj != loc);
15518 list_ret = loc_list_from_tree (obj,
15519 want_address == 2
15520 && !bitpos && !offset ? 2 : 1);
15521 /* TODO: We can extract value of the small expression via shifting even
15522 for nonzero bitpos. */
15523 if (list_ret == 0)
15524 return 0;
15525 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15527 expansion_failed (loc, NULL_RTX,
15528 "bitfield access");
15529 return 0;
15532 if (offset != NULL_TREE)
15534 /* Variable offset. */
15535 list_ret1 = loc_list_from_tree (offset, 0);
15536 if (list_ret1 == 0)
15537 return 0;
15538 add_loc_list (&list_ret, list_ret1);
15539 if (!list_ret)
15540 return 0;
15541 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15544 bytepos = bitpos / BITS_PER_UNIT;
15545 if (bytepos > 0)
15546 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15547 else if (bytepos < 0)
15548 loc_list_plus_const (list_ret, bytepos);
15550 have_address = 1;
15551 break;
15554 case INTEGER_CST:
15555 if ((want_address || !host_integerp (loc, 0))
15556 && (ret = cst_pool_loc_descr (loc)))
15557 have_address = 1;
15558 else if (want_address == 2
15559 && host_integerp (loc, 0)
15560 && (ret = address_of_int_loc_descriptor
15561 (int_size_in_bytes (TREE_TYPE (loc)),
15562 tree_low_cst (loc, 0))))
15563 have_address = 1;
15564 else if (host_integerp (loc, 0))
15565 ret = int_loc_descriptor (tree_low_cst (loc, 0));
15566 else
15568 expansion_failed (loc, NULL_RTX,
15569 "Integer operand is not host integer");
15570 return 0;
15572 break;
15574 case CONSTRUCTOR:
15575 case REAL_CST:
15576 case STRING_CST:
15577 case COMPLEX_CST:
15578 if ((ret = cst_pool_loc_descr (loc)))
15579 have_address = 1;
15580 else
15581 /* We can construct small constants here using int_loc_descriptor. */
15582 expansion_failed (loc, NULL_RTX,
15583 "constructor or constant not in constant pool");
15584 break;
15586 case TRUTH_AND_EXPR:
15587 case TRUTH_ANDIF_EXPR:
15588 case BIT_AND_EXPR:
15589 op = DW_OP_and;
15590 goto do_binop;
15592 case TRUTH_XOR_EXPR:
15593 case BIT_XOR_EXPR:
15594 op = DW_OP_xor;
15595 goto do_binop;
15597 case TRUTH_OR_EXPR:
15598 case TRUTH_ORIF_EXPR:
15599 case BIT_IOR_EXPR:
15600 op = DW_OP_or;
15601 goto do_binop;
15603 case FLOOR_DIV_EXPR:
15604 case CEIL_DIV_EXPR:
15605 case ROUND_DIV_EXPR:
15606 case TRUNC_DIV_EXPR:
15607 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15608 return 0;
15609 op = DW_OP_div;
15610 goto do_binop;
15612 case MINUS_EXPR:
15613 op = DW_OP_minus;
15614 goto do_binop;
15616 case FLOOR_MOD_EXPR:
15617 case CEIL_MOD_EXPR:
15618 case ROUND_MOD_EXPR:
15619 case TRUNC_MOD_EXPR:
15620 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15622 op = DW_OP_mod;
15623 goto do_binop;
15625 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15626 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15627 if (list_ret == 0 || list_ret1 == 0)
15628 return 0;
15630 add_loc_list (&list_ret, list_ret1);
15631 if (list_ret == 0)
15632 return 0;
15633 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15634 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15635 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15636 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15637 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15638 break;
15640 case MULT_EXPR:
15641 op = DW_OP_mul;
15642 goto do_binop;
15644 case LSHIFT_EXPR:
15645 op = DW_OP_shl;
15646 goto do_binop;
15648 case RSHIFT_EXPR:
15649 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15650 goto do_binop;
15652 case POINTER_PLUS_EXPR:
15653 case PLUS_EXPR:
15654 if (host_integerp (TREE_OPERAND (loc, 1), 0))
15656 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15657 if (list_ret == 0)
15658 return 0;
15660 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15661 break;
15664 op = DW_OP_plus;
15665 goto do_binop;
15667 case LE_EXPR:
15668 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15669 return 0;
15671 op = DW_OP_le;
15672 goto do_binop;
15674 case GE_EXPR:
15675 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15676 return 0;
15678 op = DW_OP_ge;
15679 goto do_binop;
15681 case LT_EXPR:
15682 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15683 return 0;
15685 op = DW_OP_lt;
15686 goto do_binop;
15688 case GT_EXPR:
15689 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15690 return 0;
15692 op = DW_OP_gt;
15693 goto do_binop;
15695 case EQ_EXPR:
15696 op = DW_OP_eq;
15697 goto do_binop;
15699 case NE_EXPR:
15700 op = DW_OP_ne;
15701 goto do_binop;
15703 do_binop:
15704 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15705 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15706 if (list_ret == 0 || list_ret1 == 0)
15707 return 0;
15709 add_loc_list (&list_ret, list_ret1);
15710 if (list_ret == 0)
15711 return 0;
15712 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15713 break;
15715 case TRUTH_NOT_EXPR:
15716 case BIT_NOT_EXPR:
15717 op = DW_OP_not;
15718 goto do_unop;
15720 case ABS_EXPR:
15721 op = DW_OP_abs;
15722 goto do_unop;
15724 case NEGATE_EXPR:
15725 op = DW_OP_neg;
15726 goto do_unop;
15728 do_unop:
15729 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15730 if (list_ret == 0)
15731 return 0;
15733 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15734 break;
15736 case MIN_EXPR:
15737 case MAX_EXPR:
15739 const enum tree_code code =
15740 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15742 loc = build3 (COND_EXPR, TREE_TYPE (loc),
15743 build2 (code, integer_type_node,
15744 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15745 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15748 /* ... fall through ... */
15750 case COND_EXPR:
15752 dw_loc_descr_ref lhs
15753 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15754 dw_loc_list_ref rhs
15755 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15756 dw_loc_descr_ref bra_node, jump_node, tmp;
15758 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15759 if (list_ret == 0 || lhs == 0 || rhs == 0)
15760 return 0;
15762 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15763 add_loc_descr_to_each (list_ret, bra_node);
15765 add_loc_list (&list_ret, rhs);
15766 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15767 add_loc_descr_to_each (list_ret, jump_node);
15769 add_loc_descr_to_each (list_ret, lhs);
15770 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15771 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15773 /* ??? Need a node to point the skip at. Use a nop. */
15774 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15775 add_loc_descr_to_each (list_ret, tmp);
15776 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15777 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15779 break;
15781 case FIX_TRUNC_EXPR:
15782 return 0;
15784 default:
15785 /* Leave front-end specific codes as simply unknown. This comes
15786 up, for instance, with the C STMT_EXPR. */
15787 if ((unsigned int) TREE_CODE (loc)
15788 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15790 expansion_failed (loc, NULL_RTX,
15791 "language specific tree node");
15792 return 0;
15795 #ifdef ENABLE_CHECKING
15796 /* Otherwise this is a generic code; we should just lists all of
15797 these explicitly. We forgot one. */
15798 gcc_unreachable ();
15799 #else
15800 /* In a release build, we want to degrade gracefully: better to
15801 generate incomplete debugging information than to crash. */
15802 return NULL;
15803 #endif
15806 if (!ret && !list_ret)
15807 return 0;
15809 if (want_address == 2 && !have_address
15810 && (dwarf_version >= 4 || !dwarf_strict))
15812 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15814 expansion_failed (loc, NULL_RTX,
15815 "DWARF address size mismatch");
15816 return 0;
15818 if (ret)
15819 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15820 else
15821 add_loc_descr_to_each (list_ret,
15822 new_loc_descr (DW_OP_stack_value, 0, 0));
15823 have_address = 1;
15825 /* Show if we can't fill the request for an address. */
15826 if (want_address && !have_address)
15828 expansion_failed (loc, NULL_RTX,
15829 "Want address and only have value");
15830 return 0;
15833 gcc_assert (!ret || !list_ret);
15835 /* If we've got an address and don't want one, dereference. */
15836 if (!want_address && have_address)
15838 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15840 if (size > DWARF2_ADDR_SIZE || size == -1)
15842 expansion_failed (loc, NULL_RTX,
15843 "DWARF address size mismatch");
15844 return 0;
15846 else if (size == DWARF2_ADDR_SIZE)
15847 op = DW_OP_deref;
15848 else
15849 op = DW_OP_deref_size;
15851 if (ret)
15852 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15853 else
15854 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15856 if (ret)
15857 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15859 return list_ret;
15862 /* Same as above but return only single location expression. */
15863 static dw_loc_descr_ref
15864 loc_descriptor_from_tree (tree loc, int want_address)
15866 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15867 if (!ret)
15868 return NULL;
15869 if (ret->dw_loc_next)
15871 expansion_failed (loc, NULL_RTX,
15872 "Location list where only loc descriptor needed");
15873 return NULL;
15875 return ret->expr;
15878 /* Given a value, round it up to the lowest multiple of `boundary'
15879 which is not less than the value itself. */
15881 static inline HOST_WIDE_INT
15882 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15884 return (((value + boundary - 1) / boundary) * boundary);
15887 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15888 pointer to the declared type for the relevant field variable, or return
15889 `integer_type_node' if the given node turns out to be an
15890 ERROR_MARK node. */
15892 static inline tree
15893 field_type (const_tree decl)
15895 tree type;
15897 if (TREE_CODE (decl) == ERROR_MARK)
15898 return integer_type_node;
15900 type = DECL_BIT_FIELD_TYPE (decl);
15901 if (type == NULL_TREE)
15902 type = TREE_TYPE (decl);
15904 return type;
15907 /* Given a pointer to a tree node, return the alignment in bits for
15908 it, or else return BITS_PER_WORD if the node actually turns out to
15909 be an ERROR_MARK node. */
15911 static inline unsigned
15912 simple_type_align_in_bits (const_tree type)
15914 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15917 static inline unsigned
15918 simple_decl_align_in_bits (const_tree decl)
15920 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15923 /* Return the result of rounding T up to ALIGN. */
15925 static inline double_int
15926 round_up_to_align (double_int t, unsigned int align)
15928 double_int alignd = uhwi_to_double_int (align);
15929 t = double_int_add (t, alignd);
15930 t = double_int_add (t, double_int_minus_one);
15931 t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15932 t = double_int_mul (t, alignd);
15933 return t;
15936 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15937 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15938 or return 0 if we are unable to determine what that offset is, either
15939 because the argument turns out to be a pointer to an ERROR_MARK node, or
15940 because the offset is actually variable. (We can't handle the latter case
15941 just yet). */
15943 static HOST_WIDE_INT
15944 field_byte_offset (const_tree decl)
15946 double_int object_offset_in_bits;
15947 double_int object_offset_in_bytes;
15948 double_int bitpos_int;
15950 if (TREE_CODE (decl) == ERROR_MARK)
15951 return 0;
15953 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15955 /* We cannot yet cope with fields whose positions are variable, so
15956 for now, when we see such things, we simply return 0. Someday, we may
15957 be able to handle such cases, but it will be damn difficult. */
15958 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15959 return 0;
15961 bitpos_int = tree_to_double_int (bit_position (decl));
15963 #ifdef PCC_BITFIELD_TYPE_MATTERS
15964 if (PCC_BITFIELD_TYPE_MATTERS)
15966 tree type;
15967 tree field_size_tree;
15968 double_int deepest_bitpos;
15969 double_int field_size_in_bits;
15970 unsigned int type_align_in_bits;
15971 unsigned int decl_align_in_bits;
15972 double_int type_size_in_bits;
15974 type = field_type (decl);
15975 type_size_in_bits = double_int_type_size_in_bits (type);
15976 type_align_in_bits = simple_type_align_in_bits (type);
15978 field_size_tree = DECL_SIZE (decl);
15980 /* The size could be unspecified if there was an error, or for
15981 a flexible array member. */
15982 if (!field_size_tree)
15983 field_size_tree = bitsize_zero_node;
15985 /* If the size of the field is not constant, use the type size. */
15986 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15987 field_size_in_bits = tree_to_double_int (field_size_tree);
15988 else
15989 field_size_in_bits = type_size_in_bits;
15991 decl_align_in_bits = simple_decl_align_in_bits (decl);
15993 /* The GCC front-end doesn't make any attempt to keep track of the
15994 starting bit offset (relative to the start of the containing
15995 structure type) of the hypothetical "containing object" for a
15996 bit-field. Thus, when computing the byte offset value for the
15997 start of the "containing object" of a bit-field, we must deduce
15998 this information on our own. This can be rather tricky to do in
15999 some cases. For example, handling the following structure type
16000 definition when compiling for an i386/i486 target (which only
16001 aligns long long's to 32-bit boundaries) can be very tricky:
16003 struct S { int field1; long long field2:31; };
16005 Fortunately, there is a simple rule-of-thumb which can be used
16006 in such cases. When compiling for an i386/i486, GCC will
16007 allocate 8 bytes for the structure shown above. It decides to
16008 do this based upon one simple rule for bit-field allocation.
16009 GCC allocates each "containing object" for each bit-field at
16010 the first (i.e. lowest addressed) legitimate alignment boundary
16011 (based upon the required minimum alignment for the declared
16012 type of the field) which it can possibly use, subject to the
16013 condition that there is still enough available space remaining
16014 in the containing object (when allocated at the selected point)
16015 to fully accommodate all of the bits of the bit-field itself.
16017 This simple rule makes it obvious why GCC allocates 8 bytes for
16018 each object of the structure type shown above. When looking
16019 for a place to allocate the "containing object" for `field2',
16020 the compiler simply tries to allocate a 64-bit "containing
16021 object" at each successive 32-bit boundary (starting at zero)
16022 until it finds a place to allocate that 64- bit field such that
16023 at least 31 contiguous (and previously unallocated) bits remain
16024 within that selected 64 bit field. (As it turns out, for the
16025 example above, the compiler finds it is OK to allocate the
16026 "containing object" 64-bit field at bit-offset zero within the
16027 structure type.)
16029 Here we attempt to work backwards from the limited set of facts
16030 we're given, and we try to deduce from those facts, where GCC
16031 must have believed that the containing object started (within
16032 the structure type). The value we deduce is then used (by the
16033 callers of this routine) to generate DW_AT_location and
16034 DW_AT_bit_offset attributes for fields (both bit-fields and, in
16035 the case of DW_AT_location, regular fields as well). */
16037 /* Figure out the bit-distance from the start of the structure to
16038 the "deepest" bit of the bit-field. */
16039 deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16041 /* This is the tricky part. Use some fancy footwork to deduce
16042 where the lowest addressed bit of the containing object must
16043 be. */
16044 object_offset_in_bits
16045 = double_int_sub (deepest_bitpos, type_size_in_bits);
16047 /* Round up to type_align by default. This works best for
16048 bitfields. */
16049 object_offset_in_bits
16050 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16052 if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16054 object_offset_in_bits
16055 = double_int_sub (deepest_bitpos, type_size_in_bits);
16057 /* Round up to decl_align instead. */
16058 object_offset_in_bits
16059 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16062 else
16063 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16064 object_offset_in_bits = bitpos_int;
16066 object_offset_in_bytes
16067 = double_int_div (object_offset_in_bits,
16068 uhwi_to_double_int (BITS_PER_UNIT), true,
16069 TRUNC_DIV_EXPR);
16070 return double_int_to_shwi (object_offset_in_bytes);
16073 /* The following routines define various Dwarf attributes and any data
16074 associated with them. */
16076 /* Add a location description attribute value to a DIE.
16078 This emits location attributes suitable for whole variables and
16079 whole parameters. Note that the location attributes for struct fields are
16080 generated by the routine `data_member_location_attribute' below. */
16082 static inline void
16083 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16084 dw_loc_list_ref descr)
16086 if (descr == 0)
16087 return;
16088 if (single_element_loc_list_p (descr))
16089 add_AT_loc (die, attr_kind, descr->expr);
16090 else
16091 add_AT_loc_list (die, attr_kind, descr);
16094 /* Add DW_AT_accessibility attribute to DIE if needed. */
16096 static void
16097 add_accessibility_attribute (dw_die_ref die, tree decl)
16099 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16100 children, otherwise the default is DW_ACCESS_public. In DWARF2
16101 the default has always been DW_ACCESS_public. */
16102 if (TREE_PROTECTED (decl))
16103 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16104 else if (TREE_PRIVATE (decl))
16106 if (dwarf_version == 2
16107 || die->die_parent == NULL
16108 || die->die_parent->die_tag != DW_TAG_class_type)
16109 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16111 else if (dwarf_version > 2
16112 && die->die_parent
16113 && die->die_parent->die_tag == DW_TAG_class_type)
16114 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16117 /* Attach the specialized form of location attribute used for data members of
16118 struct and union types. In the special case of a FIELD_DECL node which
16119 represents a bit-field, the "offset" part of this special location
16120 descriptor must indicate the distance in bytes from the lowest-addressed
16121 byte of the containing struct or union type to the lowest-addressed byte of
16122 the "containing object" for the bit-field. (See the `field_byte_offset'
16123 function above).
16125 For any given bit-field, the "containing object" is a hypothetical object
16126 (of some integral or enum type) within which the given bit-field lives. The
16127 type of this hypothetical "containing object" is always the same as the
16128 declared type of the individual bit-field itself (for GCC anyway... the
16129 DWARF spec doesn't actually mandate this). Note that it is the size (in
16130 bytes) of the hypothetical "containing object" which will be given in the
16131 DW_AT_byte_size attribute for this bit-field. (See the
16132 `byte_size_attribute' function below.) It is also used when calculating the
16133 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
16134 function below.) */
16136 static void
16137 add_data_member_location_attribute (dw_die_ref die, tree decl)
16139 HOST_WIDE_INT offset;
16140 dw_loc_descr_ref loc_descr = 0;
16142 if (TREE_CODE (decl) == TREE_BINFO)
16144 /* We're working on the TAG_inheritance for a base class. */
16145 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16147 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16148 aren't at a fixed offset from all (sub)objects of the same
16149 type. We need to extract the appropriate offset from our
16150 vtable. The following dwarf expression means
16152 BaseAddr = ObAddr + *((*ObAddr) - Offset)
16154 This is specific to the V3 ABI, of course. */
16156 dw_loc_descr_ref tmp;
16158 /* Make a copy of the object address. */
16159 tmp = new_loc_descr (DW_OP_dup, 0, 0);
16160 add_loc_descr (&loc_descr, tmp);
16162 /* Extract the vtable address. */
16163 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16164 add_loc_descr (&loc_descr, tmp);
16166 /* Calculate the address of the offset. */
16167 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16168 gcc_assert (offset < 0);
16170 tmp = int_loc_descriptor (-offset);
16171 add_loc_descr (&loc_descr, tmp);
16172 tmp = new_loc_descr (DW_OP_minus, 0, 0);
16173 add_loc_descr (&loc_descr, tmp);
16175 /* Extract the offset. */
16176 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16177 add_loc_descr (&loc_descr, tmp);
16179 /* Add it to the object address. */
16180 tmp = new_loc_descr (DW_OP_plus, 0, 0);
16181 add_loc_descr (&loc_descr, tmp);
16183 else
16184 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16186 else
16187 offset = field_byte_offset (decl);
16189 if (! loc_descr)
16191 if (dwarf_version > 2)
16193 /* Don't need to output a location expression, just the constant. */
16194 if (offset < 0)
16195 add_AT_int (die, DW_AT_data_member_location, offset);
16196 else
16197 add_AT_unsigned (die, DW_AT_data_member_location, offset);
16198 return;
16200 else
16202 enum dwarf_location_atom op;
16204 /* The DWARF2 standard says that we should assume that the structure
16205 address is already on the stack, so we can specify a structure
16206 field address by using DW_OP_plus_uconst. */
16208 #ifdef MIPS_DEBUGGING_INFO
16209 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16210 operator correctly. It works only if we leave the offset on the
16211 stack. */
16212 op = DW_OP_constu;
16213 #else
16214 op = DW_OP_plus_uconst;
16215 #endif
16217 loc_descr = new_loc_descr (op, offset, 0);
16221 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16224 /* Writes integer values to dw_vec_const array. */
16226 static void
16227 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16229 while (size != 0)
16231 *dest++ = val & 0xff;
16232 val >>= 8;
16233 --size;
16237 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
16239 static HOST_WIDE_INT
16240 extract_int (const unsigned char *src, unsigned int size)
16242 HOST_WIDE_INT val = 0;
16244 src += size;
16245 while (size != 0)
16247 val <<= 8;
16248 val |= *--src & 0xff;
16249 --size;
16251 return val;
16254 /* Writes double_int values to dw_vec_const array. */
16256 static void
16257 insert_double (double_int val, unsigned char *dest)
16259 unsigned char *p0 = dest;
16260 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16262 if (WORDS_BIG_ENDIAN)
16264 p0 = p1;
16265 p1 = dest;
16268 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16269 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16272 /* Writes floating point values to dw_vec_const array. */
16274 static void
16275 insert_float (const_rtx rtl, unsigned char *array)
16277 REAL_VALUE_TYPE rv;
16278 long val[4];
16279 int i;
16281 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16282 real_to_target (val, &rv, GET_MODE (rtl));
16284 /* real_to_target puts 32-bit pieces in each long. Pack them. */
16285 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16287 insert_int (val[i], 4, array);
16288 array += 4;
16292 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16293 does not have a "location" either in memory or in a register. These
16294 things can arise in GNU C when a constant is passed as an actual parameter
16295 to an inlined function. They can also arise in C++ where declared
16296 constants do not necessarily get memory "homes". */
16298 static bool
16299 add_const_value_attribute (dw_die_ref die, rtx rtl)
16301 switch (GET_CODE (rtl))
16303 case CONST_INT:
16305 HOST_WIDE_INT val = INTVAL (rtl);
16307 if (val < 0)
16308 add_AT_int (die, DW_AT_const_value, val);
16309 else
16310 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16312 return true;
16314 case CONST_DOUBLE:
16315 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16316 floating-point constant. A CONST_DOUBLE is used whenever the
16317 constant requires more than one word in order to be adequately
16318 represented. */
16320 enum machine_mode mode = GET_MODE (rtl);
16322 if (SCALAR_FLOAT_MODE_P (mode))
16324 unsigned int length = GET_MODE_SIZE (mode);
16325 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16327 insert_float (rtl, array);
16328 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16330 else
16331 add_AT_double (die, DW_AT_const_value,
16332 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16334 return true;
16336 case CONST_VECTOR:
16338 enum machine_mode mode = GET_MODE (rtl);
16339 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16340 unsigned int length = CONST_VECTOR_NUNITS (rtl);
16341 unsigned char *array = (unsigned char *) ggc_alloc_atomic
16342 (length * elt_size);
16343 unsigned int i;
16344 unsigned char *p;
16346 switch (GET_MODE_CLASS (mode))
16348 case MODE_VECTOR_INT:
16349 for (i = 0, p = array; i < length; i++, p += elt_size)
16351 rtx elt = CONST_VECTOR_ELT (rtl, i);
16352 double_int val = rtx_to_double_int (elt);
16354 if (elt_size <= sizeof (HOST_WIDE_INT))
16355 insert_int (double_int_to_shwi (val), elt_size, p);
16356 else
16358 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16359 insert_double (val, p);
16362 break;
16364 case MODE_VECTOR_FLOAT:
16365 for (i = 0, p = array; i < length; i++, p += elt_size)
16367 rtx elt = CONST_VECTOR_ELT (rtl, i);
16368 insert_float (elt, p);
16370 break;
16372 default:
16373 gcc_unreachable ();
16376 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16378 return true;
16380 case CONST_STRING:
16381 if (dwarf_version >= 4 || !dwarf_strict)
16383 dw_loc_descr_ref loc_result;
16384 resolve_one_addr (&rtl, NULL);
16385 rtl_addr:
16386 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16387 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16388 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16389 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16390 add_AT_loc (die, DW_AT_location, loc_result);
16391 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16392 return true;
16394 return false;
16396 case CONST:
16397 if (CONSTANT_P (XEXP (rtl, 0)))
16398 return add_const_value_attribute (die, XEXP (rtl, 0));
16399 /* FALLTHROUGH */
16400 case SYMBOL_REF:
16401 if (!const_ok_for_output (rtl))
16402 return false;
16403 case LABEL_REF:
16404 if (dwarf_version >= 4 || !dwarf_strict)
16405 goto rtl_addr;
16406 return false;
16408 case PLUS:
16409 /* In cases where an inlined instance of an inline function is passed
16410 the address of an `auto' variable (which is local to the caller) we
16411 can get a situation where the DECL_RTL of the artificial local
16412 variable (for the inlining) which acts as a stand-in for the
16413 corresponding formal parameter (of the inline function) will look
16414 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
16415 exactly a compile-time constant expression, but it isn't the address
16416 of the (artificial) local variable either. Rather, it represents the
16417 *value* which the artificial local variable always has during its
16418 lifetime. We currently have no way to represent such quasi-constant
16419 values in Dwarf, so for now we just punt and generate nothing. */
16420 return false;
16422 case HIGH:
16423 case CONST_FIXED:
16424 return false;
16426 case MEM:
16427 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16428 && MEM_READONLY_P (rtl)
16429 && GET_MODE (rtl) == BLKmode)
16431 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16432 return true;
16434 return false;
16436 default:
16437 /* No other kinds of rtx should be possible here. */
16438 gcc_unreachable ();
16440 return false;
16443 /* Determine whether the evaluation of EXPR references any variables
16444 or functions which aren't otherwise used (and therefore may not be
16445 output). */
16446 static tree
16447 reference_to_unused (tree * tp, int * walk_subtrees,
16448 void * data ATTRIBUTE_UNUSED)
16450 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16451 *walk_subtrees = 0;
16453 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16454 && ! TREE_ASM_WRITTEN (*tp))
16455 return *tp;
16456 /* ??? The C++ FE emits debug information for using decls, so
16457 putting gcc_unreachable here falls over. See PR31899. For now
16458 be conservative. */
16459 else if (!cgraph_global_info_ready
16460 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16461 return *tp;
16462 else if (TREE_CODE (*tp) == VAR_DECL)
16464 struct varpool_node *node = varpool_get_node (*tp);
16465 if (!node || !node->needed)
16466 return *tp;
16468 else if (TREE_CODE (*tp) == FUNCTION_DECL
16469 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16471 /* The call graph machinery must have finished analyzing,
16472 optimizing and gimplifying the CU by now.
16473 So if *TP has no call graph node associated
16474 to it, it means *TP will not be emitted. */
16475 if (!cgraph_get_node (*tp))
16476 return *tp;
16478 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16479 return *tp;
16481 return NULL_TREE;
16484 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16485 for use in a later add_const_value_attribute call. */
16487 static rtx
16488 rtl_for_decl_init (tree init, tree type)
16490 rtx rtl = NULL_RTX;
16492 STRIP_NOPS (init);
16494 /* If a variable is initialized with a string constant without embedded
16495 zeros, build CONST_STRING. */
16496 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16498 tree enttype = TREE_TYPE (type);
16499 tree domain = TYPE_DOMAIN (type);
16500 enum machine_mode mode = TYPE_MODE (enttype);
16502 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16503 && domain
16504 && integer_zerop (TYPE_MIN_VALUE (domain))
16505 && compare_tree_int (TYPE_MAX_VALUE (domain),
16506 TREE_STRING_LENGTH (init) - 1) == 0
16507 && ((size_t) TREE_STRING_LENGTH (init)
16508 == strlen (TREE_STRING_POINTER (init)) + 1))
16510 rtl = gen_rtx_CONST_STRING (VOIDmode,
16511 ggc_strdup (TREE_STRING_POINTER (init)));
16512 rtl = gen_rtx_MEM (BLKmode, rtl);
16513 MEM_READONLY_P (rtl) = 1;
16516 /* Other aggregates, and complex values, could be represented using
16517 CONCAT: FIXME! */
16518 else if (AGGREGATE_TYPE_P (type)
16519 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
16520 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
16521 || TREE_CODE (type) == COMPLEX_TYPE)
16523 /* Vectors only work if their mode is supported by the target.
16524 FIXME: generic vectors ought to work too. */
16525 else if (TREE_CODE (type) == VECTOR_TYPE
16526 && !VECTOR_MODE_P (TYPE_MODE (type)))
16528 /* If the initializer is something that we know will expand into an
16529 immediate RTL constant, expand it now. We must be careful not to
16530 reference variables which won't be output. */
16531 else if (initializer_constant_valid_p (init, type)
16532 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16534 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16535 possible. */
16536 if (TREE_CODE (type) == VECTOR_TYPE)
16537 switch (TREE_CODE (init))
16539 case VECTOR_CST:
16540 break;
16541 case CONSTRUCTOR:
16542 if (TREE_CONSTANT (init))
16544 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16545 bool constant_p = true;
16546 tree value;
16547 unsigned HOST_WIDE_INT ix;
16549 /* Even when ctor is constant, it might contain non-*_CST
16550 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16551 belong into VECTOR_CST nodes. */
16552 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16553 if (!CONSTANT_CLASS_P (value))
16555 constant_p = false;
16556 break;
16559 if (constant_p)
16561 init = build_vector_from_ctor (type, elts);
16562 break;
16565 /* FALLTHRU */
16567 default:
16568 return NULL;
16571 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16573 /* If expand_expr returns a MEM, it wasn't immediate. */
16574 gcc_assert (!rtl || !MEM_P (rtl));
16577 return rtl;
16580 /* Generate RTL for the variable DECL to represent its location. */
16582 static rtx
16583 rtl_for_decl_location (tree decl)
16585 rtx rtl;
16587 /* Here we have to decide where we are going to say the parameter "lives"
16588 (as far as the debugger is concerned). We only have a couple of
16589 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16591 DECL_RTL normally indicates where the parameter lives during most of the
16592 activation of the function. If optimization is enabled however, this
16593 could be either NULL or else a pseudo-reg. Both of those cases indicate
16594 that the parameter doesn't really live anywhere (as far as the code
16595 generation parts of GCC are concerned) during most of the function's
16596 activation. That will happen (for example) if the parameter is never
16597 referenced within the function.
16599 We could just generate a location descriptor here for all non-NULL
16600 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16601 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16602 where DECL_RTL is NULL or is a pseudo-reg.
16604 Note however that we can only get away with using DECL_INCOMING_RTL as
16605 a backup substitute for DECL_RTL in certain limited cases. In cases
16606 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16607 we can be sure that the parameter was passed using the same type as it is
16608 declared to have within the function, and that its DECL_INCOMING_RTL
16609 points us to a place where a value of that type is passed.
16611 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16612 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16613 because in these cases DECL_INCOMING_RTL points us to a value of some
16614 type which is *different* from the type of the parameter itself. Thus,
16615 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16616 such cases, the debugger would end up (for example) trying to fetch a
16617 `float' from a place which actually contains the first part of a
16618 `double'. That would lead to really incorrect and confusing
16619 output at debug-time.
16621 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16622 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
16623 are a couple of exceptions however. On little-endian machines we can
16624 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16625 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16626 an integral type that is smaller than TREE_TYPE (decl). These cases arise
16627 when (on a little-endian machine) a non-prototyped function has a
16628 parameter declared to be of type `short' or `char'. In such cases,
16629 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16630 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16631 passed `int' value. If the debugger then uses that address to fetch
16632 a `short' or a `char' (on a little-endian machine) the result will be
16633 the correct data, so we allow for such exceptional cases below.
16635 Note that our goal here is to describe the place where the given formal
16636 parameter lives during most of the function's activation (i.e. between the
16637 end of the prologue and the start of the epilogue). We'll do that as best
16638 as we can. Note however that if the given formal parameter is modified
16639 sometime during the execution of the function, then a stack backtrace (at
16640 debug-time) will show the function as having been called with the *new*
16641 value rather than the value which was originally passed in. This happens
16642 rarely enough that it is not a major problem, but it *is* a problem, and
16643 I'd like to fix it.
16645 A future version of dwarf2out.c may generate two additional attributes for
16646 any given DW_TAG_formal_parameter DIE which will describe the "passed
16647 type" and the "passed location" for the given formal parameter in addition
16648 to the attributes we now generate to indicate the "declared type" and the
16649 "active location" for each parameter. This additional set of attributes
16650 could be used by debuggers for stack backtraces. Separately, note that
16651 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16652 This happens (for example) for inlined-instances of inline function formal
16653 parameters which are never referenced. This really shouldn't be
16654 happening. All PARM_DECL nodes should get valid non-NULL
16655 DECL_INCOMING_RTL values. FIXME. */
16657 /* Use DECL_RTL as the "location" unless we find something better. */
16658 rtl = DECL_RTL_IF_SET (decl);
16660 /* When generating abstract instances, ignore everything except
16661 constants, symbols living in memory, and symbols living in
16662 fixed registers. */
16663 if (! reload_completed)
16665 if (rtl
16666 && (CONSTANT_P (rtl)
16667 || (MEM_P (rtl)
16668 && CONSTANT_P (XEXP (rtl, 0)))
16669 || (REG_P (rtl)
16670 && TREE_CODE (decl) == VAR_DECL
16671 && TREE_STATIC (decl))))
16673 rtl = targetm.delegitimize_address (rtl);
16674 return rtl;
16676 rtl = NULL_RTX;
16678 else if (TREE_CODE (decl) == PARM_DECL)
16680 if (rtl == NULL_RTX
16681 || is_pseudo_reg (rtl)
16682 || (MEM_P (rtl)
16683 && is_pseudo_reg (XEXP (rtl, 0))
16684 && DECL_INCOMING_RTL (decl)
16685 && MEM_P (DECL_INCOMING_RTL (decl))
16686 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
16688 tree declared_type = TREE_TYPE (decl);
16689 tree passed_type = DECL_ARG_TYPE (decl);
16690 enum machine_mode dmode = TYPE_MODE (declared_type);
16691 enum machine_mode pmode = TYPE_MODE (passed_type);
16693 /* This decl represents a formal parameter which was optimized out.
16694 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16695 all cases where (rtl == NULL_RTX) just below. */
16696 if (dmode == pmode)
16697 rtl = DECL_INCOMING_RTL (decl);
16698 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
16699 && SCALAR_INT_MODE_P (dmode)
16700 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16701 && DECL_INCOMING_RTL (decl))
16703 rtx inc = DECL_INCOMING_RTL (decl);
16704 if (REG_P (inc))
16705 rtl = inc;
16706 else if (MEM_P (inc))
16708 if (BYTES_BIG_ENDIAN)
16709 rtl = adjust_address_nv (inc, dmode,
16710 GET_MODE_SIZE (pmode)
16711 - GET_MODE_SIZE (dmode));
16712 else
16713 rtl = inc;
16718 /* If the parm was passed in registers, but lives on the stack, then
16719 make a big endian correction if the mode of the type of the
16720 parameter is not the same as the mode of the rtl. */
16721 /* ??? This is the same series of checks that are made in dbxout.c before
16722 we reach the big endian correction code there. It isn't clear if all
16723 of these checks are necessary here, but keeping them all is the safe
16724 thing to do. */
16725 else if (MEM_P (rtl)
16726 && XEXP (rtl, 0) != const0_rtx
16727 && ! CONSTANT_P (XEXP (rtl, 0))
16728 /* Not passed in memory. */
16729 && !MEM_P (DECL_INCOMING_RTL (decl))
16730 /* Not passed by invisible reference. */
16731 && (!REG_P (XEXP (rtl, 0))
16732 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16733 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16734 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16735 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16736 #endif
16738 /* Big endian correction check. */
16739 && BYTES_BIG_ENDIAN
16740 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16741 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16742 < UNITS_PER_WORD))
16744 int offset = (UNITS_PER_WORD
16745 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16747 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16748 plus_constant (XEXP (rtl, 0), offset));
16751 else if (TREE_CODE (decl) == VAR_DECL
16752 && rtl
16753 && MEM_P (rtl)
16754 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16755 && BYTES_BIG_ENDIAN)
16757 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16758 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16760 /* If a variable is declared "register" yet is smaller than
16761 a register, then if we store the variable to memory, it
16762 looks like we're storing a register-sized value, when in
16763 fact we are not. We need to adjust the offset of the
16764 storage location to reflect the actual value's bytes,
16765 else gdb will not be able to display it. */
16766 if (rsize > dsize)
16767 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16768 plus_constant (XEXP (rtl, 0), rsize-dsize));
16771 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16772 and will have been substituted directly into all expressions that use it.
16773 C does not have such a concept, but C++ and other languages do. */
16774 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16775 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16777 if (rtl)
16778 rtl = targetm.delegitimize_address (rtl);
16780 /* If we don't look past the constant pool, we risk emitting a
16781 reference to a constant pool entry that isn't referenced from
16782 code, and thus is not emitted. */
16783 if (rtl)
16784 rtl = avoid_constant_pool_reference (rtl);
16786 /* Try harder to get a rtl. If this symbol ends up not being emitted
16787 in the current CU, resolve_addr will remove the expression referencing
16788 it. */
16789 if (rtl == NULL_RTX
16790 && TREE_CODE (decl) == VAR_DECL
16791 && !DECL_EXTERNAL (decl)
16792 && TREE_STATIC (decl)
16793 && DECL_NAME (decl)
16794 && !DECL_HARD_REGISTER (decl)
16795 && DECL_MODE (decl) != VOIDmode)
16797 rtl = make_decl_rtl_for_debug (decl);
16798 if (!MEM_P (rtl)
16799 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16800 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16801 rtl = NULL_RTX;
16804 return rtl;
16807 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16808 returned. If so, the decl for the COMMON block is returned, and the
16809 value is the offset into the common block for the symbol. */
16811 static tree
16812 fortran_common (tree decl, HOST_WIDE_INT *value)
16814 tree val_expr, cvar;
16815 enum machine_mode mode;
16816 HOST_WIDE_INT bitsize, bitpos;
16817 tree offset;
16818 int volatilep = 0, unsignedp = 0;
16820 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16821 it does not have a value (the offset into the common area), or if it
16822 is thread local (as opposed to global) then it isn't common, and shouldn't
16823 be handled as such. */
16824 if (TREE_CODE (decl) != VAR_DECL
16825 || !TREE_STATIC (decl)
16826 || !DECL_HAS_VALUE_EXPR_P (decl)
16827 || !is_fortran ())
16828 return NULL_TREE;
16830 val_expr = DECL_VALUE_EXPR (decl);
16831 if (TREE_CODE (val_expr) != COMPONENT_REF)
16832 return NULL_TREE;
16834 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16835 &mode, &unsignedp, &volatilep, true);
16837 if (cvar == NULL_TREE
16838 || TREE_CODE (cvar) != VAR_DECL
16839 || DECL_ARTIFICIAL (cvar)
16840 || !TREE_PUBLIC (cvar))
16841 return NULL_TREE;
16843 *value = 0;
16844 if (offset != NULL)
16846 if (!host_integerp (offset, 0))
16847 return NULL_TREE;
16848 *value = tree_low_cst (offset, 0);
16850 if (bitpos != 0)
16851 *value += bitpos / BITS_PER_UNIT;
16853 return cvar;
16856 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16857 data attribute for a variable or a parameter. We generate the
16858 DW_AT_const_value attribute only in those cases where the given variable
16859 or parameter does not have a true "location" either in memory or in a
16860 register. This can happen (for example) when a constant is passed as an
16861 actual argument in a call to an inline function. (It's possible that
16862 these things can crop up in other ways also.) Note that one type of
16863 constant value which can be passed into an inlined function is a constant
16864 pointer. This can happen for example if an actual argument in an inlined
16865 function call evaluates to a compile-time constant address.
16867 CACHE_P is true if it is worth caching the location list for DECL,
16868 so that future calls can reuse it rather than regenerate it from scratch.
16869 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16870 since we will need to refer to them each time the function is inlined. */
16872 static bool
16873 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16874 enum dwarf_attribute attr)
16876 rtx rtl;
16877 dw_loc_list_ref list;
16878 var_loc_list *loc_list;
16879 cached_dw_loc_list *cache;
16880 void **slot;
16882 if (TREE_CODE (decl) == ERROR_MARK)
16883 return false;
16885 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16886 || TREE_CODE (decl) == RESULT_DECL);
16888 /* Try to get some constant RTL for this decl, and use that as the value of
16889 the location. */
16891 rtl = rtl_for_decl_location (decl);
16892 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16893 && add_const_value_attribute (die, rtl))
16894 return true;
16896 /* See if we have single element location list that is equivalent to
16897 a constant value. That way we are better to use add_const_value_attribute
16898 rather than expanding constant value equivalent. */
16899 loc_list = lookup_decl_loc (decl);
16900 if (loc_list
16901 && loc_list->first
16902 && loc_list->first->next == NULL
16903 && NOTE_P (loc_list->first->loc)
16904 && NOTE_VAR_LOCATION (loc_list->first->loc)
16905 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16907 struct var_loc_node *node;
16909 node = loc_list->first;
16910 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16911 if (GET_CODE (rtl) == EXPR_LIST)
16912 rtl = XEXP (rtl, 0);
16913 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16914 && add_const_value_attribute (die, rtl))
16915 return true;
16917 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16918 list several times. See if we've already cached the contents. */
16919 list = NULL;
16920 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16921 cache_p = false;
16922 if (cache_p)
16924 cache = (cached_dw_loc_list *)
16925 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
16926 if (cache)
16927 list = cache->loc_list;
16929 if (list == NULL)
16931 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16932 /* It is usually worth caching this result if the decl is from
16933 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16934 if (cache_p && list && list->dw_loc_next)
16936 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
16937 DECL_UID (decl), INSERT);
16938 cache = ggc_alloc_cleared_cached_dw_loc_list ();
16939 cache->decl_id = DECL_UID (decl);
16940 cache->loc_list = list;
16941 *slot = cache;
16944 if (list)
16946 add_AT_location_description (die, attr, list);
16947 return true;
16949 /* None of that worked, so it must not really have a location;
16950 try adding a constant value attribute from the DECL_INITIAL. */
16951 return tree_add_const_value_attribute_for_decl (die, decl);
16954 /* Add VARIABLE and DIE into deferred locations list. */
16956 static void
16957 defer_location (tree variable, dw_die_ref die)
16959 deferred_locations entry;
16960 entry.variable = variable;
16961 entry.die = die;
16962 VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16965 /* Helper function for tree_add_const_value_attribute. Natively encode
16966 initializer INIT into an array. Return true if successful. */
16968 static bool
16969 native_encode_initializer (tree init, unsigned char *array, int size)
16971 tree type;
16973 if (init == NULL_TREE)
16974 return false;
16976 STRIP_NOPS (init);
16977 switch (TREE_CODE (init))
16979 case STRING_CST:
16980 type = TREE_TYPE (init);
16981 if (TREE_CODE (type) == ARRAY_TYPE)
16983 tree enttype = TREE_TYPE (type);
16984 enum machine_mode mode = TYPE_MODE (enttype);
16986 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16987 return false;
16988 if (int_size_in_bytes (type) != size)
16989 return false;
16990 if (size > TREE_STRING_LENGTH (init))
16992 memcpy (array, TREE_STRING_POINTER (init),
16993 TREE_STRING_LENGTH (init));
16994 memset (array + TREE_STRING_LENGTH (init),
16995 '\0', size - TREE_STRING_LENGTH (init));
16997 else
16998 memcpy (array, TREE_STRING_POINTER (init), size);
16999 return true;
17001 return false;
17002 case CONSTRUCTOR:
17003 type = TREE_TYPE (init);
17004 if (int_size_in_bytes (type) != size)
17005 return false;
17006 if (TREE_CODE (type) == ARRAY_TYPE)
17008 HOST_WIDE_INT min_index;
17009 unsigned HOST_WIDE_INT cnt;
17010 int curpos = 0, fieldsize;
17011 constructor_elt *ce;
17013 if (TYPE_DOMAIN (type) == NULL_TREE
17014 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17015 return false;
17017 fieldsize = int_size_in_bytes (TREE_TYPE (type));
17018 if (fieldsize <= 0)
17019 return false;
17021 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17022 memset (array, '\0', size);
17023 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17025 tree val = ce->value;
17026 tree index = ce->index;
17027 int pos = curpos;
17028 if (index && TREE_CODE (index) == RANGE_EXPR)
17029 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17030 * fieldsize;
17031 else if (index)
17032 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17034 if (val)
17036 STRIP_NOPS (val);
17037 if (!native_encode_initializer (val, array + pos, fieldsize))
17038 return false;
17040 curpos = pos + fieldsize;
17041 if (index && TREE_CODE (index) == RANGE_EXPR)
17043 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17044 - tree_low_cst (TREE_OPERAND (index, 0), 0);
17045 while (count > 0)
17047 if (val)
17048 memcpy (array + curpos, array + pos, fieldsize);
17049 curpos += fieldsize;
17052 gcc_assert (curpos <= size);
17054 return true;
17056 else if (TREE_CODE (type) == RECORD_TYPE
17057 || TREE_CODE (type) == UNION_TYPE)
17059 tree field = NULL_TREE;
17060 unsigned HOST_WIDE_INT cnt;
17061 constructor_elt *ce;
17063 if (int_size_in_bytes (type) != size)
17064 return false;
17066 if (TREE_CODE (type) == RECORD_TYPE)
17067 field = TYPE_FIELDS (type);
17069 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17071 tree val = ce->value;
17072 int pos, fieldsize;
17074 if (ce->index != 0)
17075 field = ce->index;
17077 if (val)
17078 STRIP_NOPS (val);
17080 if (field == NULL_TREE || DECL_BIT_FIELD (field))
17081 return false;
17083 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17084 && TYPE_DOMAIN (TREE_TYPE (field))
17085 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17086 return false;
17087 else if (DECL_SIZE_UNIT (field) == NULL_TREE
17088 || !host_integerp (DECL_SIZE_UNIT (field), 0))
17089 return false;
17090 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17091 pos = int_byte_position (field);
17092 gcc_assert (pos + fieldsize <= size);
17093 if (val
17094 && !native_encode_initializer (val, array + pos, fieldsize))
17095 return false;
17097 return true;
17099 return false;
17100 case VIEW_CONVERT_EXPR:
17101 case NON_LVALUE_EXPR:
17102 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17103 default:
17104 return native_encode_expr (init, array, size) == size;
17108 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17109 attribute is the const value T. */
17111 static bool
17112 tree_add_const_value_attribute (dw_die_ref die, tree t)
17114 tree init;
17115 tree type = TREE_TYPE (t);
17116 rtx rtl;
17118 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17119 return false;
17121 init = t;
17122 gcc_assert (!DECL_P (init));
17124 rtl = rtl_for_decl_init (init, type);
17125 if (rtl)
17126 return add_const_value_attribute (die, rtl);
17127 /* If the host and target are sane, try harder. */
17128 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17129 && initializer_constant_valid_p (init, type))
17131 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17132 if (size > 0 && (int) size == size)
17134 unsigned char *array = (unsigned char *)
17135 ggc_alloc_cleared_atomic (size);
17137 if (native_encode_initializer (init, array, size))
17139 add_AT_vec (die, DW_AT_const_value, size, 1, array);
17140 return true;
17144 return false;
17147 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17148 attribute is the const value of T, where T is an integral constant
17149 variable with static storage duration
17150 (so it can't be a PARM_DECL or a RESULT_DECL). */
17152 static bool
17153 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17156 if (!decl
17157 || (TREE_CODE (decl) != VAR_DECL
17158 && TREE_CODE (decl) != CONST_DECL))
17159 return false;
17161 if (TREE_READONLY (decl)
17162 && ! TREE_THIS_VOLATILE (decl)
17163 && DECL_INITIAL (decl))
17164 /* OK */;
17165 else
17166 return false;
17168 /* Don't add DW_AT_const_value if abstract origin already has one. */
17169 if (get_AT (var_die, DW_AT_const_value))
17170 return false;
17172 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17175 /* Convert the CFI instructions for the current function into a
17176 location list. This is used for DW_AT_frame_base when we targeting
17177 a dwarf2 consumer that does not support the dwarf3
17178 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
17179 expressions. */
17181 static dw_loc_list_ref
17182 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17184 int ix;
17185 dw_fde_ref fde;
17186 dw_loc_list_ref list, *list_tail;
17187 dw_cfi_ref cfi;
17188 dw_cfa_location last_cfa, next_cfa;
17189 const char *start_label, *last_label, *section;
17190 dw_cfa_location remember;
17192 fde = current_fde ();
17193 gcc_assert (fde != NULL);
17195 section = secname_for_decl (current_function_decl);
17196 list_tail = &list;
17197 list = NULL;
17199 memset (&next_cfa, 0, sizeof (next_cfa));
17200 next_cfa.reg = INVALID_REGNUM;
17201 remember = next_cfa;
17203 start_label = fde->dw_fde_begin;
17205 /* ??? Bald assumption that the CIE opcode list does not contain
17206 advance opcodes. */
17207 FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
17208 lookup_cfa_1 (cfi, &next_cfa, &remember);
17210 last_cfa = next_cfa;
17211 last_label = start_label;
17213 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
17215 /* If the first partition contained no CFI adjustments, the
17216 CIE opcodes apply to the whole first partition. */
17217 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17218 fde->dw_fde_begin, fde->dw_fde_end, section);
17219 list_tail =&(*list_tail)->dw_loc_next;
17220 start_label = last_label = fde->dw_fde_second_begin;
17223 FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
17225 switch (cfi->dw_cfi_opc)
17227 case DW_CFA_set_loc:
17228 case DW_CFA_advance_loc1:
17229 case DW_CFA_advance_loc2:
17230 case DW_CFA_advance_loc4:
17231 if (!cfa_equal_p (&last_cfa, &next_cfa))
17233 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17234 start_label, last_label, section);
17236 list_tail = &(*list_tail)->dw_loc_next;
17237 last_cfa = next_cfa;
17238 start_label = last_label;
17240 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17241 break;
17243 case DW_CFA_advance_loc:
17244 /* The encoding is complex enough that we should never emit this. */
17245 gcc_unreachable ();
17247 default:
17248 lookup_cfa_1 (cfi, &next_cfa, &remember);
17249 break;
17251 if (ix + 1 == fde->dw_fde_switch_cfi_index)
17253 if (!cfa_equal_p (&last_cfa, &next_cfa))
17255 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17256 start_label, last_label, section);
17258 list_tail = &(*list_tail)->dw_loc_next;
17259 last_cfa = next_cfa;
17260 start_label = last_label;
17262 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17263 start_label, fde->dw_fde_end, section);
17264 list_tail = &(*list_tail)->dw_loc_next;
17265 start_label = last_label = fde->dw_fde_second_begin;
17269 if (!cfa_equal_p (&last_cfa, &next_cfa))
17271 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17272 start_label, last_label, section);
17273 list_tail = &(*list_tail)->dw_loc_next;
17274 start_label = last_label;
17277 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17278 start_label,
17279 fde->dw_fde_second_begin
17280 ? fde->dw_fde_second_end : fde->dw_fde_end,
17281 section);
17283 if (list && list->dw_loc_next)
17284 gen_llsym (list);
17286 return list;
17289 /* Compute a displacement from the "steady-state frame pointer" to the
17290 frame base (often the same as the CFA), and store it in
17291 frame_pointer_fb_offset. OFFSET is added to the displacement
17292 before the latter is negated. */
17294 static void
17295 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17297 rtx reg, elim;
17299 #ifdef FRAME_POINTER_CFA_OFFSET
17300 reg = frame_pointer_rtx;
17301 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17302 #else
17303 reg = arg_pointer_rtx;
17304 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17305 #endif
17307 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17308 if (GET_CODE (elim) == PLUS)
17310 offset += INTVAL (XEXP (elim, 1));
17311 elim = XEXP (elim, 0);
17314 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17315 && (elim == hard_frame_pointer_rtx
17316 || elim == stack_pointer_rtx))
17317 || elim == (frame_pointer_needed
17318 ? hard_frame_pointer_rtx
17319 : stack_pointer_rtx));
17321 frame_pointer_fb_offset = -offset;
17324 /* Generate a DW_AT_name attribute given some string value to be included as
17325 the value of the attribute. */
17327 static void
17328 add_name_attribute (dw_die_ref die, const char *name_string)
17330 if (name_string != NULL && *name_string != 0)
17332 if (demangle_name_func)
17333 name_string = (*demangle_name_func) (name_string);
17335 add_AT_string (die, DW_AT_name, name_string);
17339 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
17340 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
17341 of TYPE accordingly.
17343 ??? This is a temporary measure until after we're able to generate
17344 regular DWARF for the complex Ada type system. */
17346 static void
17347 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
17348 dw_die_ref context_die)
17350 tree dtype;
17351 dw_die_ref dtype_die;
17353 if (!lang_hooks.types.descriptive_type)
17354 return;
17356 dtype = lang_hooks.types.descriptive_type (type);
17357 if (!dtype)
17358 return;
17360 dtype_die = lookup_type_die (dtype);
17361 if (!dtype_die)
17363 gen_type_die (dtype, context_die);
17364 dtype_die = lookup_type_die (dtype);
17365 gcc_assert (dtype_die);
17368 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
17371 /* Generate a DW_AT_comp_dir attribute for DIE. */
17373 static void
17374 add_comp_dir_attribute (dw_die_ref die)
17376 const char *wd = get_src_pwd ();
17377 char *wd1;
17379 if (wd == NULL)
17380 return;
17382 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17384 int wdlen;
17386 wdlen = strlen (wd);
17387 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17388 strcpy (wd1, wd);
17389 wd1 [wdlen] = DIR_SEPARATOR;
17390 wd1 [wdlen + 1] = 0;
17391 wd = wd1;
17394 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17397 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17398 default. */
17400 static int
17401 lower_bound_default (void)
17403 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17405 case DW_LANG_C:
17406 case DW_LANG_C89:
17407 case DW_LANG_C99:
17408 case DW_LANG_C_plus_plus:
17409 case DW_LANG_ObjC:
17410 case DW_LANG_ObjC_plus_plus:
17411 case DW_LANG_Java:
17412 return 0;
17413 case DW_LANG_Fortran77:
17414 case DW_LANG_Fortran90:
17415 case DW_LANG_Fortran95:
17416 return 1;
17417 case DW_LANG_UPC:
17418 case DW_LANG_D:
17419 case DW_LANG_Python:
17420 return dwarf_version >= 4 ? 0 : -1;
17421 case DW_LANG_Ada95:
17422 case DW_LANG_Ada83:
17423 case DW_LANG_Cobol74:
17424 case DW_LANG_Cobol85:
17425 case DW_LANG_Pascal83:
17426 case DW_LANG_Modula2:
17427 case DW_LANG_PLI:
17428 return dwarf_version >= 4 ? 1 : -1;
17429 default:
17430 return -1;
17434 /* Given a tree node describing an array bound (either lower or upper) output
17435 a representation for that bound. */
17437 static void
17438 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17440 switch (TREE_CODE (bound))
17442 case ERROR_MARK:
17443 return;
17445 /* All fixed-bounds are represented by INTEGER_CST nodes. */
17446 case INTEGER_CST:
17448 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17449 int dflt;
17451 /* Use the default if possible. */
17452 if (bound_attr == DW_AT_lower_bound
17453 && host_integerp (bound, 0)
17454 && (dflt = lower_bound_default ()) != -1
17455 && tree_low_cst (bound, 0) == dflt)
17458 /* Otherwise represent the bound as an unsigned value with the
17459 precision of its type. The precision and signedness of the
17460 type will be necessary to re-interpret it unambiguously. */
17461 else if (prec < HOST_BITS_PER_WIDE_INT)
17463 unsigned HOST_WIDE_INT mask
17464 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17465 add_AT_unsigned (subrange_die, bound_attr,
17466 TREE_INT_CST_LOW (bound) & mask);
17468 else if (prec == HOST_BITS_PER_WIDE_INT
17469 || TREE_INT_CST_HIGH (bound) == 0)
17470 add_AT_unsigned (subrange_die, bound_attr,
17471 TREE_INT_CST_LOW (bound));
17472 else
17473 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17474 TREE_INT_CST_LOW (bound));
17476 break;
17478 CASE_CONVERT:
17479 case VIEW_CONVERT_EXPR:
17480 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17481 break;
17483 case SAVE_EXPR:
17484 break;
17486 case VAR_DECL:
17487 case PARM_DECL:
17488 case RESULT_DECL:
17490 dw_die_ref decl_die = lookup_decl_die (bound);
17492 /* ??? Can this happen, or should the variable have been bound
17493 first? Probably it can, since I imagine that we try to create
17494 the types of parameters in the order in which they exist in
17495 the list, and won't have created a forward reference to a
17496 later parameter. */
17497 if (decl_die != NULL)
17499 add_AT_die_ref (subrange_die, bound_attr, decl_die);
17500 break;
17503 /* FALLTHRU */
17505 default:
17507 /* Otherwise try to create a stack operation procedure to
17508 evaluate the value of the array bound. */
17510 dw_die_ref ctx, decl_die;
17511 dw_loc_list_ref list;
17513 list = loc_list_from_tree (bound, 2);
17514 if (list == NULL || single_element_loc_list_p (list))
17516 /* If DW_AT_*bound is not a reference nor constant, it is
17517 a DWARF expression rather than location description.
17518 For that loc_list_from_tree (bound, 0) is needed.
17519 If that fails to give a single element list,
17520 fall back to outputting this as a reference anyway. */
17521 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17522 if (list2 && single_element_loc_list_p (list2))
17524 add_AT_loc (subrange_die, bound_attr, list2->expr);
17525 break;
17528 if (list == NULL)
17529 break;
17531 if (current_function_decl == 0)
17532 ctx = comp_unit_die ();
17533 else
17534 ctx = lookup_decl_die (current_function_decl);
17536 decl_die = new_die (DW_TAG_variable, ctx, bound);
17537 add_AT_flag (decl_die, DW_AT_artificial, 1);
17538 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17539 add_AT_location_description (decl_die, DW_AT_location, list);
17540 add_AT_die_ref (subrange_die, bound_attr, decl_die);
17541 break;
17546 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17547 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17548 Note that the block of subscript information for an array type also
17549 includes information about the element type of the given array type. */
17551 static void
17552 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17554 unsigned dimension_number;
17555 tree lower, upper;
17556 dw_die_ref subrange_die;
17558 for (dimension_number = 0;
17559 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17560 type = TREE_TYPE (type), dimension_number++)
17562 tree domain = TYPE_DOMAIN (type);
17564 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17565 break;
17567 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17568 and (in GNU C only) variable bounds. Handle all three forms
17569 here. */
17570 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17571 if (domain)
17573 /* We have an array type with specified bounds. */
17574 lower = TYPE_MIN_VALUE (domain);
17575 upper = TYPE_MAX_VALUE (domain);
17577 /* Define the index type. */
17578 if (TREE_TYPE (domain))
17580 /* ??? This is probably an Ada unnamed subrange type. Ignore the
17581 TREE_TYPE field. We can't emit debug info for this
17582 because it is an unnamed integral type. */
17583 if (TREE_CODE (domain) == INTEGER_TYPE
17584 && TYPE_NAME (domain) == NULL_TREE
17585 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17586 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17588 else
17589 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17590 type_die);
17593 /* ??? If upper is NULL, the array has unspecified length,
17594 but it does have a lower bound. This happens with Fortran
17595 dimension arr(N:*)
17596 Since the debugger is definitely going to need to know N
17597 to produce useful results, go ahead and output the lower
17598 bound solo, and hope the debugger can cope. */
17600 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17601 if (upper)
17602 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17605 /* Otherwise we have an array type with an unspecified length. The
17606 DWARF-2 spec does not say how to handle this; let's just leave out the
17607 bounds. */
17611 static void
17612 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17614 unsigned size;
17616 switch (TREE_CODE (tree_node))
17618 case ERROR_MARK:
17619 size = 0;
17620 break;
17621 case ENUMERAL_TYPE:
17622 case RECORD_TYPE:
17623 case UNION_TYPE:
17624 case QUAL_UNION_TYPE:
17625 size = int_size_in_bytes (tree_node);
17626 break;
17627 case FIELD_DECL:
17628 /* For a data member of a struct or union, the DW_AT_byte_size is
17629 generally given as the number of bytes normally allocated for an
17630 object of the *declared* type of the member itself. This is true
17631 even for bit-fields. */
17632 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17633 break;
17634 default:
17635 gcc_unreachable ();
17638 /* Note that `size' might be -1 when we get to this point. If it is, that
17639 indicates that the byte size of the entity in question is variable. We
17640 have no good way of expressing this fact in Dwarf at the present time,
17641 so just let the -1 pass on through. */
17642 add_AT_unsigned (die, DW_AT_byte_size, size);
17645 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17646 which specifies the distance in bits from the highest order bit of the
17647 "containing object" for the bit-field to the highest order bit of the
17648 bit-field itself.
17650 For any given bit-field, the "containing object" is a hypothetical object
17651 (of some integral or enum type) within which the given bit-field lives. The
17652 type of this hypothetical "containing object" is always the same as the
17653 declared type of the individual bit-field itself. The determination of the
17654 exact location of the "containing object" for a bit-field is rather
17655 complicated. It's handled by the `field_byte_offset' function (above).
17657 Note that it is the size (in bytes) of the hypothetical "containing object"
17658 which will be given in the DW_AT_byte_size attribute for this bit-field.
17659 (See `byte_size_attribute' above). */
17661 static inline void
17662 add_bit_offset_attribute (dw_die_ref die, tree decl)
17664 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17665 tree type = DECL_BIT_FIELD_TYPE (decl);
17666 HOST_WIDE_INT bitpos_int;
17667 HOST_WIDE_INT highest_order_object_bit_offset;
17668 HOST_WIDE_INT highest_order_field_bit_offset;
17669 HOST_WIDE_INT bit_offset;
17671 /* Must be a field and a bit field. */
17672 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17674 /* We can't yet handle bit-fields whose offsets are variable, so if we
17675 encounter such things, just return without generating any attribute
17676 whatsoever. Likewise for variable or too large size. */
17677 if (! host_integerp (bit_position (decl), 0)
17678 || ! host_integerp (DECL_SIZE (decl), 1))
17679 return;
17681 bitpos_int = int_bit_position (decl);
17683 /* Note that the bit offset is always the distance (in bits) from the
17684 highest-order bit of the "containing object" to the highest-order bit of
17685 the bit-field itself. Since the "high-order end" of any object or field
17686 is different on big-endian and little-endian machines, the computation
17687 below must take account of these differences. */
17688 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17689 highest_order_field_bit_offset = bitpos_int;
17691 if (! BYTES_BIG_ENDIAN)
17693 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17694 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17697 bit_offset
17698 = (! BYTES_BIG_ENDIAN
17699 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17700 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17702 if (bit_offset < 0)
17703 add_AT_int (die, DW_AT_bit_offset, bit_offset);
17704 else
17705 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17708 /* For a FIELD_DECL node which represents a bit field, output an attribute
17709 which specifies the length in bits of the given field. */
17711 static inline void
17712 add_bit_size_attribute (dw_die_ref die, tree decl)
17714 /* Must be a field and a bit field. */
17715 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17716 && DECL_BIT_FIELD_TYPE (decl));
17718 if (host_integerp (DECL_SIZE (decl), 1))
17719 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17722 /* If the compiled language is ANSI C, then add a 'prototyped'
17723 attribute, if arg types are given for the parameters of a function. */
17725 static inline void
17726 add_prototyped_attribute (dw_die_ref die, tree func_type)
17728 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17729 && prototype_p (func_type))
17730 add_AT_flag (die, DW_AT_prototyped, 1);
17733 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17734 by looking in either the type declaration or object declaration
17735 equate table. */
17737 static inline dw_die_ref
17738 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17740 dw_die_ref origin_die = NULL;
17742 if (TREE_CODE (origin) != FUNCTION_DECL)
17744 /* We may have gotten separated from the block for the inlined
17745 function, if we're in an exception handler or some such; make
17746 sure that the abstract function has been written out.
17748 Doing this for nested functions is wrong, however; functions are
17749 distinct units, and our context might not even be inline. */
17750 tree fn = origin;
17752 if (TYPE_P (fn))
17753 fn = TYPE_STUB_DECL (fn);
17755 fn = decl_function_context (fn);
17756 if (fn)
17757 dwarf2out_abstract_function (fn);
17760 if (DECL_P (origin))
17761 origin_die = lookup_decl_die (origin);
17762 else if (TYPE_P (origin))
17763 origin_die = lookup_type_die (origin);
17765 /* XXX: Functions that are never lowered don't always have correct block
17766 trees (in the case of java, they simply have no block tree, in some other
17767 languages). For these functions, there is nothing we can really do to
17768 output correct debug info for inlined functions in all cases. Rather
17769 than die, we'll just produce deficient debug info now, in that we will
17770 have variables without a proper abstract origin. In the future, when all
17771 functions are lowered, we should re-add a gcc_assert (origin_die)
17772 here. */
17774 if (origin_die)
17775 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17776 return origin_die;
17779 /* We do not currently support the pure_virtual attribute. */
17781 static inline void
17782 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17784 if (DECL_VINDEX (func_decl))
17786 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17788 if (host_integerp (DECL_VINDEX (func_decl), 0))
17789 add_AT_loc (die, DW_AT_vtable_elem_location,
17790 new_loc_descr (DW_OP_constu,
17791 tree_low_cst (DECL_VINDEX (func_decl), 0),
17792 0));
17794 /* GNU extension: Record what type this method came from originally. */
17795 if (debug_info_level > DINFO_LEVEL_TERSE
17796 && DECL_CONTEXT (func_decl))
17797 add_AT_die_ref (die, DW_AT_containing_type,
17798 lookup_type_die (DECL_CONTEXT (func_decl)));
17802 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17803 given decl. This used to be a vendor extension until after DWARF 4
17804 standardized it. */
17806 static void
17807 add_linkage_attr (dw_die_ref die, tree decl)
17809 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17811 /* Mimic what assemble_name_raw does with a leading '*'. */
17812 if (name[0] == '*')
17813 name = &name[1];
17815 if (dwarf_version >= 4)
17816 add_AT_string (die, DW_AT_linkage_name, name);
17817 else
17818 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17821 /* Add source coordinate attributes for the given decl. */
17823 static void
17824 add_src_coords_attributes (dw_die_ref die, tree decl)
17826 expanded_location s;
17828 if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
17829 return;
17830 s = expand_location (DECL_SOURCE_LOCATION (decl));
17831 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17832 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17835 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17837 static void
17838 add_linkage_name (dw_die_ref die, tree decl)
17840 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17841 && TREE_PUBLIC (decl)
17842 && !DECL_ABSTRACT (decl)
17843 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17844 && die->die_tag != DW_TAG_member)
17846 /* Defer until we have an assembler name set. */
17847 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17849 limbo_die_node *asm_name;
17851 asm_name = ggc_alloc_cleared_limbo_die_node ();
17852 asm_name->die = die;
17853 asm_name->created_for = decl;
17854 asm_name->next = deferred_asm_name;
17855 deferred_asm_name = asm_name;
17857 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17858 add_linkage_attr (die, decl);
17862 /* Add a DW_AT_name attribute and source coordinate attribute for the
17863 given decl, but only if it actually has a name. */
17865 static void
17866 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17868 tree decl_name;
17870 decl_name = DECL_NAME (decl);
17871 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17873 const char *name = dwarf2_name (decl, 0);
17874 if (name)
17875 add_name_attribute (die, name);
17876 if (! DECL_ARTIFICIAL (decl))
17877 add_src_coords_attributes (die, decl);
17879 add_linkage_name (die, decl);
17882 #ifdef VMS_DEBUGGING_INFO
17883 /* Get the function's name, as described by its RTL. This may be different
17884 from the DECL_NAME name used in the source file. */
17885 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17887 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17888 XEXP (DECL_RTL (decl), 0));
17889 VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17891 #endif /* VMS_DEBUGGING_INFO */
17894 #ifdef VMS_DEBUGGING_INFO
17895 /* Output the debug main pointer die for VMS */
17897 void
17898 dwarf2out_vms_debug_main_pointer (void)
17900 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17901 dw_die_ref die;
17903 /* Allocate the VMS debug main subprogram die. */
17904 die = ggc_alloc_cleared_die_node ();
17905 die->die_tag = DW_TAG_subprogram;
17906 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17907 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17908 current_function_funcdef_no);
17909 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17911 /* Make it the first child of comp_unit_die (). */
17912 die->die_parent = comp_unit_die ();
17913 if (comp_unit_die ()->die_child)
17915 die->die_sib = comp_unit_die ()->die_child->die_sib;
17916 comp_unit_die ()->die_child->die_sib = die;
17918 else
17920 die->die_sib = die;
17921 comp_unit_die ()->die_child = die;
17924 #endif /* VMS_DEBUGGING_INFO */
17926 /* Push a new declaration scope. */
17928 static void
17929 push_decl_scope (tree scope)
17931 VEC_safe_push (tree, gc, decl_scope_table, scope);
17934 /* Pop a declaration scope. */
17936 static inline void
17937 pop_decl_scope (void)
17939 VEC_pop (tree, decl_scope_table);
17942 /* Return the DIE for the scope that immediately contains this type.
17943 Non-named types get global scope. Named types nested in other
17944 types get their containing scope if it's open, or global scope
17945 otherwise. All other types (i.e. function-local named types) get
17946 the current active scope. */
17948 static dw_die_ref
17949 scope_die_for (tree t, dw_die_ref context_die)
17951 dw_die_ref scope_die = NULL;
17952 tree containing_scope;
17953 int i;
17955 /* Non-types always go in the current scope. */
17956 gcc_assert (TYPE_P (t));
17958 containing_scope = TYPE_CONTEXT (t);
17960 /* Use the containing namespace if it was passed in (for a declaration). */
17961 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17963 if (context_die == lookup_decl_die (containing_scope))
17964 /* OK */;
17965 else
17966 containing_scope = NULL_TREE;
17969 /* Ignore function type "scopes" from the C frontend. They mean that
17970 a tagged type is local to a parmlist of a function declarator, but
17971 that isn't useful to DWARF. */
17972 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17973 containing_scope = NULL_TREE;
17975 if (SCOPE_FILE_SCOPE_P (containing_scope))
17976 scope_die = comp_unit_die ();
17977 else if (TYPE_P (containing_scope))
17979 /* For types, we can just look up the appropriate DIE. But
17980 first we check to see if we're in the middle of emitting it
17981 so we know where the new DIE should go. */
17982 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17983 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17984 break;
17986 if (i < 0)
17988 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17989 || TREE_ASM_WRITTEN (containing_scope));
17990 /*We are not in the middle of emitting the type
17991 CONTAINING_SCOPE. Let's see if it's emitted already. */
17992 scope_die = lookup_type_die (containing_scope);
17994 /* If none of the current dies are suitable, we get file scope. */
17995 if (scope_die == NULL)
17996 scope_die = comp_unit_die ();
17998 else
17999 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
18001 else
18002 scope_die = context_die;
18004 return scope_die;
18007 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
18009 static inline int
18010 local_scope_p (dw_die_ref context_die)
18012 for (; context_die; context_die = context_die->die_parent)
18013 if (context_die->die_tag == DW_TAG_inlined_subroutine
18014 || context_die->die_tag == DW_TAG_subprogram)
18015 return 1;
18017 return 0;
18020 /* Returns nonzero if CONTEXT_DIE is a class. */
18022 static inline int
18023 class_scope_p (dw_die_ref context_die)
18025 return (context_die
18026 && (context_die->die_tag == DW_TAG_structure_type
18027 || context_die->die_tag == DW_TAG_class_type
18028 || context_die->die_tag == DW_TAG_interface_type
18029 || context_die->die_tag == DW_TAG_union_type));
18032 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
18033 whether or not to treat a DIE in this context as a declaration. */
18035 static inline int
18036 class_or_namespace_scope_p (dw_die_ref context_die)
18038 return (class_scope_p (context_die)
18039 || (context_die && context_die->die_tag == DW_TAG_namespace));
18042 /* Many forms of DIEs require a "type description" attribute. This
18043 routine locates the proper "type descriptor" die for the type given
18044 by 'type', and adds a DW_AT_type attribute below the given die. */
18046 static void
18047 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
18048 int decl_volatile, dw_die_ref context_die)
18050 enum tree_code code = TREE_CODE (type);
18051 dw_die_ref type_die = NULL;
18053 /* ??? If this type is an unnamed subrange type of an integral, floating-point
18054 or fixed-point type, use the inner type. This is because we have no
18055 support for unnamed types in base_type_die. This can happen if this is
18056 an Ada subrange type. Correct solution is emit a subrange type die. */
18057 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
18058 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
18059 type = TREE_TYPE (type), code = TREE_CODE (type);
18061 if (code == ERROR_MARK
18062 /* Handle a special case. For functions whose return type is void, we
18063 generate *no* type attribute. (Note that no object may have type
18064 `void', so this only applies to function return types). */
18065 || code == VOID_TYPE)
18066 return;
18068 type_die = modified_type_die (type,
18069 decl_const || TYPE_READONLY (type),
18070 decl_volatile || TYPE_VOLATILE (type),
18071 context_die);
18073 if (type_die != NULL)
18074 add_AT_die_ref (object_die, DW_AT_type, type_die);
18077 /* Given an object die, add the calling convention attribute for the
18078 function call type. */
18079 static void
18080 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18082 enum dwarf_calling_convention value = DW_CC_normal;
18084 value = ((enum dwarf_calling_convention)
18085 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18087 if (is_fortran ()
18088 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18090 /* DWARF 2 doesn't provide a way to identify a program's source-level
18091 entry point. DW_AT_calling_convention attributes are only meant
18092 to describe functions' calling conventions. However, lacking a
18093 better way to signal the Fortran main program, we used this for
18094 a long time, following existing custom. Now, DWARF 4 has
18095 DW_AT_main_subprogram, which we add below, but some tools still
18096 rely on the old way, which we thus keep. */
18097 value = DW_CC_program;
18099 if (dwarf_version >= 4 || !dwarf_strict)
18100 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18103 /* Only add the attribute if the backend requests it, and
18104 is not DW_CC_normal. */
18105 if (value && (value != DW_CC_normal))
18106 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18109 /* Given a tree pointer to a struct, class, union, or enum type node, return
18110 a pointer to the (string) tag name for the given type, or zero if the type
18111 was declared without a tag. */
18113 static const char *
18114 type_tag (const_tree type)
18116 const char *name = 0;
18118 if (TYPE_NAME (type) != 0)
18120 tree t = 0;
18122 /* Find the IDENTIFIER_NODE for the type name. */
18123 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18124 && !TYPE_NAMELESS (type))
18125 t = TYPE_NAME (type);
18127 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18128 a TYPE_DECL node, regardless of whether or not a `typedef' was
18129 involved. */
18130 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18131 && ! DECL_IGNORED_P (TYPE_NAME (type)))
18133 /* We want to be extra verbose. Don't call dwarf_name if
18134 DECL_NAME isn't set. The default hook for decl_printable_name
18135 doesn't like that, and in this context it's correct to return
18136 0, instead of "<anonymous>" or the like. */
18137 if (DECL_NAME (TYPE_NAME (type))
18138 && !DECL_NAMELESS (TYPE_NAME (type)))
18139 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18142 /* Now get the name as a string, or invent one. */
18143 if (!name && t != 0)
18144 name = IDENTIFIER_POINTER (t);
18147 return (name == 0 || *name == '\0') ? 0 : name;
18150 /* Return the type associated with a data member, make a special check
18151 for bit field types. */
18153 static inline tree
18154 member_declared_type (const_tree member)
18156 return (DECL_BIT_FIELD_TYPE (member)
18157 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18160 /* Get the decl's label, as described by its RTL. This may be different
18161 from the DECL_NAME name used in the source file. */
18163 #if 0
18164 static const char *
18165 decl_start_label (tree decl)
18167 rtx x;
18168 const char *fnname;
18170 x = DECL_RTL (decl);
18171 gcc_assert (MEM_P (x));
18173 x = XEXP (x, 0);
18174 gcc_assert (GET_CODE (x) == SYMBOL_REF);
18176 fnname = XSTR (x, 0);
18177 return fnname;
18179 #endif
18181 /* These routines generate the internal representation of the DIE's for
18182 the compilation unit. Debugging information is collected by walking
18183 the declaration trees passed in from dwarf2out_decl(). */
18185 static void
18186 gen_array_type_die (tree type, dw_die_ref context_die)
18188 dw_die_ref scope_die = scope_die_for (type, context_die);
18189 dw_die_ref array_die;
18191 /* GNU compilers represent multidimensional array types as sequences of one
18192 dimensional array types whose element types are themselves array types.
18193 We sometimes squish that down to a single array_type DIE with multiple
18194 subscripts in the Dwarf debugging info. The draft Dwarf specification
18195 say that we are allowed to do this kind of compression in C, because
18196 there is no difference between an array of arrays and a multidimensional
18197 array. We don't do this for Ada to remain as close as possible to the
18198 actual representation, which is especially important against the language
18199 flexibilty wrt arrays of variable size. */
18201 bool collapse_nested_arrays = !is_ada ();
18202 tree element_type;
18204 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18205 DW_TAG_string_type doesn't have DW_AT_type attribute). */
18206 if (TYPE_STRING_FLAG (type)
18207 && TREE_CODE (type) == ARRAY_TYPE
18208 && is_fortran ()
18209 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18211 HOST_WIDE_INT size;
18213 array_die = new_die (DW_TAG_string_type, scope_die, type);
18214 add_name_attribute (array_die, type_tag (type));
18215 equate_type_number_to_die (type, array_die);
18216 size = int_size_in_bytes (type);
18217 if (size >= 0)
18218 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18219 else if (TYPE_DOMAIN (type) != NULL_TREE
18220 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18221 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18223 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18224 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18226 size = int_size_in_bytes (TREE_TYPE (szdecl));
18227 if (loc && size > 0)
18229 add_AT_location_description (array_die, DW_AT_string_length, loc);
18230 if (size != DWARF2_ADDR_SIZE)
18231 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18234 return;
18237 /* ??? The SGI dwarf reader fails for array of array of enum types
18238 (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18239 array type comes before the outer array type. We thus call gen_type_die
18240 before we new_die and must prevent nested array types collapsing for this
18241 target. */
18243 #ifdef MIPS_DEBUGGING_INFO
18244 gen_type_die (TREE_TYPE (type), context_die);
18245 collapse_nested_arrays = false;
18246 #endif
18248 array_die = new_die (DW_TAG_array_type, scope_die, type);
18249 add_name_attribute (array_die, type_tag (type));
18250 add_gnat_descriptive_type_attribute (array_die, type, context_die);
18251 equate_type_number_to_die (type, array_die);
18253 if (TREE_CODE (type) == VECTOR_TYPE)
18254 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18256 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
18257 if (is_fortran ()
18258 && TREE_CODE (type) == ARRAY_TYPE
18259 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18260 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18261 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18263 #if 0
18264 /* We default the array ordering. SDB will probably do
18265 the right things even if DW_AT_ordering is not present. It's not even
18266 an issue until we start to get into multidimensional arrays anyway. If
18267 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18268 then we'll have to put the DW_AT_ordering attribute back in. (But if
18269 and when we find out that we need to put these in, we will only do so
18270 for multidimensional arrays. */
18271 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18272 #endif
18274 #ifdef MIPS_DEBUGGING_INFO
18275 /* The SGI compilers handle arrays of unknown bound by setting
18276 AT_declaration and not emitting any subrange DIEs. */
18277 if (TREE_CODE (type) == ARRAY_TYPE
18278 && ! TYPE_DOMAIN (type))
18279 add_AT_flag (array_die, DW_AT_declaration, 1);
18280 else
18281 #endif
18282 if (TREE_CODE (type) == VECTOR_TYPE)
18284 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
18285 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18286 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18287 add_bound_info (subrange_die, DW_AT_upper_bound,
18288 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18290 else
18291 add_subscript_info (array_die, type, collapse_nested_arrays);
18293 /* Add representation of the type of the elements of this array type and
18294 emit the corresponding DIE if we haven't done it already. */
18295 element_type = TREE_TYPE (type);
18296 if (collapse_nested_arrays)
18297 while (TREE_CODE (element_type) == ARRAY_TYPE)
18299 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18300 break;
18301 element_type = TREE_TYPE (element_type);
18304 #ifndef MIPS_DEBUGGING_INFO
18305 gen_type_die (element_type, context_die);
18306 #endif
18308 add_type_attribute (array_die, element_type, 0, 0, context_die);
18310 if (get_AT (array_die, DW_AT_name))
18311 add_pubtype (type, array_die);
18314 static dw_loc_descr_ref
18315 descr_info_loc (tree val, tree base_decl)
18317 HOST_WIDE_INT size;
18318 dw_loc_descr_ref loc, loc2;
18319 enum dwarf_location_atom op;
18321 if (val == base_decl)
18322 return new_loc_descr (DW_OP_push_object_address, 0, 0);
18324 switch (TREE_CODE (val))
18326 CASE_CONVERT:
18327 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18328 case VAR_DECL:
18329 return loc_descriptor_from_tree (val, 0);
18330 case INTEGER_CST:
18331 if (host_integerp (val, 0))
18332 return int_loc_descriptor (tree_low_cst (val, 0));
18333 break;
18334 case INDIRECT_REF:
18335 size = int_size_in_bytes (TREE_TYPE (val));
18336 if (size < 0)
18337 break;
18338 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18339 if (!loc)
18340 break;
18341 if (size == DWARF2_ADDR_SIZE)
18342 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18343 else
18344 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18345 return loc;
18346 case POINTER_PLUS_EXPR:
18347 case PLUS_EXPR:
18348 if (host_integerp (TREE_OPERAND (val, 1), 1)
18349 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18350 < 16384)
18352 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18353 if (!loc)
18354 break;
18355 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18357 else
18359 op = DW_OP_plus;
18360 do_binop:
18361 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18362 if (!loc)
18363 break;
18364 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18365 if (!loc2)
18366 break;
18367 add_loc_descr (&loc, loc2);
18368 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18370 return loc;
18371 case MINUS_EXPR:
18372 op = DW_OP_minus;
18373 goto do_binop;
18374 case MULT_EXPR:
18375 op = DW_OP_mul;
18376 goto do_binop;
18377 case EQ_EXPR:
18378 op = DW_OP_eq;
18379 goto do_binop;
18380 case NE_EXPR:
18381 op = DW_OP_ne;
18382 goto do_binop;
18383 default:
18384 break;
18386 return NULL;
18389 static void
18390 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18391 tree val, tree base_decl)
18393 dw_loc_descr_ref loc;
18395 if (host_integerp (val, 0))
18397 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18398 return;
18401 loc = descr_info_loc (val, base_decl);
18402 if (!loc)
18403 return;
18405 add_AT_loc (die, attr, loc);
18408 /* This routine generates DIE for array with hidden descriptor, details
18409 are filled into *info by a langhook. */
18411 static void
18412 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18413 dw_die_ref context_die)
18415 dw_die_ref scope_die = scope_die_for (type, context_die);
18416 dw_die_ref array_die;
18417 int dim;
18419 array_die = new_die (DW_TAG_array_type, scope_die, type);
18420 add_name_attribute (array_die, type_tag (type));
18421 equate_type_number_to_die (type, array_die);
18423 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
18424 if (is_fortran ()
18425 && info->ndimensions >= 2)
18426 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18428 if (info->data_location)
18429 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18430 info->base_decl);
18431 if (info->associated)
18432 add_descr_info_field (array_die, DW_AT_associated, info->associated,
18433 info->base_decl);
18434 if (info->allocated)
18435 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18436 info->base_decl);
18438 for (dim = 0; dim < info->ndimensions; dim++)
18440 dw_die_ref subrange_die
18441 = new_die (DW_TAG_subrange_type, array_die, NULL);
18443 if (info->dimen[dim].lower_bound)
18445 /* If it is the default value, omit it. */
18446 int dflt;
18448 if (host_integerp (info->dimen[dim].lower_bound, 0)
18449 && (dflt = lower_bound_default ()) != -1
18450 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18452 else
18453 add_descr_info_field (subrange_die, DW_AT_lower_bound,
18454 info->dimen[dim].lower_bound,
18455 info->base_decl);
18457 if (info->dimen[dim].upper_bound)
18458 add_descr_info_field (subrange_die, DW_AT_upper_bound,
18459 info->dimen[dim].upper_bound,
18460 info->base_decl);
18461 if (info->dimen[dim].stride)
18462 add_descr_info_field (subrange_die, DW_AT_byte_stride,
18463 info->dimen[dim].stride,
18464 info->base_decl);
18467 gen_type_die (info->element_type, context_die);
18468 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18470 if (get_AT (array_die, DW_AT_name))
18471 add_pubtype (type, array_die);
18474 #if 0
18475 static void
18476 gen_entry_point_die (tree decl, dw_die_ref context_die)
18478 tree origin = decl_ultimate_origin (decl);
18479 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18481 if (origin != NULL)
18482 add_abstract_origin_attribute (decl_die, origin);
18483 else
18485 add_name_and_src_coords_attributes (decl_die, decl);
18486 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18487 0, 0, context_die);
18490 if (DECL_ABSTRACT (decl))
18491 equate_decl_number_to_die (decl, decl_die);
18492 else
18493 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18495 #endif
18497 /* Walk through the list of incomplete types again, trying once more to
18498 emit full debugging info for them. */
18500 static void
18501 retry_incomplete_types (void)
18503 int i;
18505 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18506 if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18507 DINFO_USAGE_DIR_USE))
18508 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18511 /* Determine what tag to use for a record type. */
18513 static enum dwarf_tag
18514 record_type_tag (tree type)
18516 if (! lang_hooks.types.classify_record)
18517 return DW_TAG_structure_type;
18519 switch (lang_hooks.types.classify_record (type))
18521 case RECORD_IS_STRUCT:
18522 return DW_TAG_structure_type;
18524 case RECORD_IS_CLASS:
18525 return DW_TAG_class_type;
18527 case RECORD_IS_INTERFACE:
18528 if (dwarf_version >= 3 || !dwarf_strict)
18529 return DW_TAG_interface_type;
18530 return DW_TAG_structure_type;
18532 default:
18533 gcc_unreachable ();
18537 /* Generate a DIE to represent an enumeration type. Note that these DIEs
18538 include all of the information about the enumeration values also. Each
18539 enumerated type name/value is listed as a child of the enumerated type
18540 DIE. */
18542 static dw_die_ref
18543 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18545 dw_die_ref type_die = lookup_type_die (type);
18547 if (type_die == NULL)
18549 type_die = new_die (DW_TAG_enumeration_type,
18550 scope_die_for (type, context_die), type);
18551 equate_type_number_to_die (type, type_die);
18552 add_name_attribute (type_die, type_tag (type));
18553 add_gnat_descriptive_type_attribute (type_die, type, context_die);
18554 if (dwarf_version >= 4 || !dwarf_strict)
18556 if (ENUM_IS_SCOPED (type))
18557 add_AT_flag (type_die, DW_AT_enum_class, 1);
18558 if (ENUM_IS_OPAQUE (type))
18559 add_AT_flag (type_die, DW_AT_declaration, 1);
18562 else if (! TYPE_SIZE (type))
18563 return type_die;
18564 else
18565 remove_AT (type_die, DW_AT_declaration);
18567 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
18568 given enum type is incomplete, do not generate the DW_AT_byte_size
18569 attribute or the DW_AT_element_list attribute. */
18570 if (TYPE_SIZE (type))
18572 tree link;
18574 TREE_ASM_WRITTEN (type) = 1;
18575 add_byte_size_attribute (type_die, type);
18576 if (TYPE_STUB_DECL (type) != NULL_TREE)
18578 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18579 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18582 /* If the first reference to this type was as the return type of an
18583 inline function, then it may not have a parent. Fix this now. */
18584 if (type_die->die_parent == NULL)
18585 add_child_die (scope_die_for (type, context_die), type_die);
18587 for (link = TYPE_VALUES (type);
18588 link != NULL; link = TREE_CHAIN (link))
18590 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18591 tree value = TREE_VALUE (link);
18593 add_name_attribute (enum_die,
18594 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18596 if (TREE_CODE (value) == CONST_DECL)
18597 value = DECL_INITIAL (value);
18599 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18600 /* DWARF2 does not provide a way of indicating whether or
18601 not enumeration constants are signed or unsigned. GDB
18602 always assumes the values are signed, so we output all
18603 values as if they were signed. That means that
18604 enumeration constants with very large unsigned values
18605 will appear to have negative values in the debugger. */
18606 add_AT_int (enum_die, DW_AT_const_value,
18607 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18610 else
18611 add_AT_flag (type_die, DW_AT_declaration, 1);
18613 if (get_AT (type_die, DW_AT_name))
18614 add_pubtype (type, type_die);
18616 return type_die;
18619 /* Generate a DIE to represent either a real live formal parameter decl or to
18620 represent just the type of some formal parameter position in some function
18621 type.
18623 Note that this routine is a bit unusual because its argument may be a
18624 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18625 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18626 node. If it's the former then this function is being called to output a
18627 DIE to represent a formal parameter object (or some inlining thereof). If
18628 it's the latter, then this function is only being called to output a
18629 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18630 argument type of some subprogram type.
18631 If EMIT_NAME_P is true, name and source coordinate attributes
18632 are emitted. */
18634 static dw_die_ref
18635 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18636 dw_die_ref context_die)
18638 tree node_or_origin = node ? node : origin;
18639 tree ultimate_origin;
18640 dw_die_ref parm_die
18641 = new_die (DW_TAG_formal_parameter, context_die, node);
18643 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18645 case tcc_declaration:
18646 ultimate_origin = decl_ultimate_origin (node_or_origin);
18647 if (node || ultimate_origin)
18648 origin = ultimate_origin;
18649 if (origin != NULL)
18650 add_abstract_origin_attribute (parm_die, origin);
18651 else if (emit_name_p)
18652 add_name_and_src_coords_attributes (parm_die, node);
18653 if (origin == NULL
18654 || (! DECL_ABSTRACT (node_or_origin)
18655 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18656 decl_function_context
18657 (node_or_origin))))
18659 tree type = TREE_TYPE (node_or_origin);
18660 if (decl_by_reference_p (node_or_origin))
18661 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18662 context_die);
18663 else
18664 add_type_attribute (parm_die, type,
18665 TREE_READONLY (node_or_origin),
18666 TREE_THIS_VOLATILE (node_or_origin),
18667 context_die);
18669 if (origin == NULL && DECL_ARTIFICIAL (node))
18670 add_AT_flag (parm_die, DW_AT_artificial, 1);
18672 if (node && node != origin)
18673 equate_decl_number_to_die (node, parm_die);
18674 if (! DECL_ABSTRACT (node_or_origin))
18675 add_location_or_const_value_attribute (parm_die, node_or_origin,
18676 node == NULL, DW_AT_location);
18678 break;
18680 case tcc_type:
18681 /* We were called with some kind of a ..._TYPE node. */
18682 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18683 break;
18685 default:
18686 gcc_unreachable ();
18689 return parm_die;
18692 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18693 children DW_TAG_formal_parameter DIEs representing the arguments of the
18694 parameter pack.
18696 PARM_PACK must be a function parameter pack.
18697 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18698 must point to the subsequent arguments of the function PACK_ARG belongs to.
18699 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18700 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18701 following the last one for which a DIE was generated. */
18703 static dw_die_ref
18704 gen_formal_parameter_pack_die (tree parm_pack,
18705 tree pack_arg,
18706 dw_die_ref subr_die,
18707 tree *next_arg)
18709 tree arg;
18710 dw_die_ref parm_pack_die;
18712 gcc_assert (parm_pack
18713 && lang_hooks.function_parameter_pack_p (parm_pack)
18714 && subr_die);
18716 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18717 add_src_coords_attributes (parm_pack_die, parm_pack);
18719 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18721 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18722 parm_pack))
18723 break;
18724 gen_formal_parameter_die (arg, NULL,
18725 false /* Don't emit name attribute. */,
18726 parm_pack_die);
18728 if (next_arg)
18729 *next_arg = arg;
18730 return parm_pack_die;
18733 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18734 at the end of an (ANSI prototyped) formal parameters list. */
18736 static void
18737 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18739 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18742 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18743 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18744 parameters as specified in some function type specification (except for
18745 those which appear as part of a function *definition*). */
18747 static void
18748 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18750 tree link;
18751 tree formal_type = NULL;
18752 tree first_parm_type;
18753 tree arg;
18755 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18757 arg = DECL_ARGUMENTS (function_or_method_type);
18758 function_or_method_type = TREE_TYPE (function_or_method_type);
18760 else
18761 arg = NULL_TREE;
18763 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18765 /* Make our first pass over the list of formal parameter types and output a
18766 DW_TAG_formal_parameter DIE for each one. */
18767 for (link = first_parm_type; link; )
18769 dw_die_ref parm_die;
18771 formal_type = TREE_VALUE (link);
18772 if (formal_type == void_type_node)
18773 break;
18775 /* Output a (nameless) DIE to represent the formal parameter itself. */
18776 parm_die = gen_formal_parameter_die (formal_type, NULL,
18777 true /* Emit name attribute. */,
18778 context_die);
18779 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18780 && link == first_parm_type)
18782 add_AT_flag (parm_die, DW_AT_artificial, 1);
18783 if (dwarf_version >= 3 || !dwarf_strict)
18784 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18786 else if (arg && DECL_ARTIFICIAL (arg))
18787 add_AT_flag (parm_die, DW_AT_artificial, 1);
18789 link = TREE_CHAIN (link);
18790 if (arg)
18791 arg = DECL_CHAIN (arg);
18794 /* If this function type has an ellipsis, add a
18795 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18796 if (formal_type != void_type_node)
18797 gen_unspecified_parameters_die (function_or_method_type, context_die);
18799 /* Make our second (and final) pass over the list of formal parameter types
18800 and output DIEs to represent those types (as necessary). */
18801 for (link = TYPE_ARG_TYPES (function_or_method_type);
18802 link && TREE_VALUE (link);
18803 link = TREE_CHAIN (link))
18804 gen_type_die (TREE_VALUE (link), context_die);
18807 /* We want to generate the DIE for TYPE so that we can generate the
18808 die for MEMBER, which has been defined; we will need to refer back
18809 to the member declaration nested within TYPE. If we're trying to
18810 generate minimal debug info for TYPE, processing TYPE won't do the
18811 trick; we need to attach the member declaration by hand. */
18813 static void
18814 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18816 gen_type_die (type, context_die);
18818 /* If we're trying to avoid duplicate debug info, we may not have
18819 emitted the member decl for this function. Emit it now. */
18820 if (TYPE_STUB_DECL (type)
18821 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18822 && ! lookup_decl_die (member))
18824 dw_die_ref type_die;
18825 gcc_assert (!decl_ultimate_origin (member));
18827 push_decl_scope (type);
18828 type_die = lookup_type_die_strip_naming_typedef (type);
18829 if (TREE_CODE (member) == FUNCTION_DECL)
18830 gen_subprogram_die (member, type_die);
18831 else if (TREE_CODE (member) == FIELD_DECL)
18833 /* Ignore the nameless fields that are used to skip bits but handle
18834 C++ anonymous unions and structs. */
18835 if (DECL_NAME (member) != NULL_TREE
18836 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18837 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18839 gen_type_die (member_declared_type (member), type_die);
18840 gen_field_die (member, type_die);
18843 else
18844 gen_variable_die (member, NULL_TREE, type_die);
18846 pop_decl_scope ();
18850 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18851 may later generate inlined and/or out-of-line instances of. */
18853 static void
18854 dwarf2out_abstract_function (tree decl)
18856 dw_die_ref old_die;
18857 tree save_fn;
18858 tree context;
18859 int was_abstract;
18860 htab_t old_decl_loc_table;
18861 htab_t old_cached_dw_loc_list_table;
18862 int old_call_site_count, old_tail_call_site_count;
18863 struct call_arg_loc_node *old_call_arg_locations;
18865 /* Make sure we have the actual abstract inline, not a clone. */
18866 decl = DECL_ORIGIN (decl);
18868 old_die = lookup_decl_die (decl);
18869 if (old_die && get_AT (old_die, DW_AT_inline))
18870 /* We've already generated the abstract instance. */
18871 return;
18873 /* We can be called while recursively when seeing block defining inlined subroutine
18874 DIE. Be sure to not clobber the outer location table nor use it or we would
18875 get locations in abstract instantces. */
18876 old_decl_loc_table = decl_loc_table;
18877 decl_loc_table = NULL;
18878 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18879 cached_dw_loc_list_table = NULL;
18880 old_call_arg_locations = call_arg_locations;
18881 call_arg_locations = NULL;
18882 old_call_site_count = call_site_count;
18883 call_site_count = -1;
18884 old_tail_call_site_count = tail_call_site_count;
18885 tail_call_site_count = -1;
18887 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18888 we don't get confused by DECL_ABSTRACT. */
18889 if (debug_info_level > DINFO_LEVEL_TERSE)
18891 context = decl_class_context (decl);
18892 if (context)
18893 gen_type_die_for_member
18894 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18897 /* Pretend we've just finished compiling this function. */
18898 save_fn = current_function_decl;
18899 current_function_decl = decl;
18900 push_cfun (DECL_STRUCT_FUNCTION (decl));
18902 was_abstract = DECL_ABSTRACT (decl);
18903 set_decl_abstract_flags (decl, 1);
18904 dwarf2out_decl (decl);
18905 if (! was_abstract)
18906 set_decl_abstract_flags (decl, 0);
18908 current_function_decl = save_fn;
18909 decl_loc_table = old_decl_loc_table;
18910 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18911 call_arg_locations = old_call_arg_locations;
18912 call_site_count = old_call_site_count;
18913 tail_call_site_count = old_tail_call_site_count;
18914 pop_cfun ();
18917 /* Helper function of premark_used_types() which gets called through
18918 htab_traverse.
18920 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18921 marked as unused by prune_unused_types. */
18923 static int
18924 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18926 tree type;
18927 dw_die_ref die;
18929 type = (tree) *slot;
18930 die = lookup_type_die (type);
18931 if (die != NULL)
18932 die->die_perennial_p = 1;
18933 return 1;
18936 /* Helper function of premark_types_used_by_global_vars which gets called
18937 through htab_traverse.
18939 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18940 marked as unused by prune_unused_types. The DIE of the type is marked
18941 only if the global variable using the type will actually be emitted. */
18943 static int
18944 premark_types_used_by_global_vars_helper (void **slot,
18945 void *data ATTRIBUTE_UNUSED)
18947 struct types_used_by_vars_entry *entry;
18948 dw_die_ref die;
18950 entry = (struct types_used_by_vars_entry *) *slot;
18951 gcc_assert (entry->type != NULL
18952 && entry->var_decl != NULL);
18953 die = lookup_type_die (entry->type);
18954 if (die)
18956 /* Ask cgraph if the global variable really is to be emitted.
18957 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18958 struct varpool_node *node = varpool_get_node (entry->var_decl);
18959 if (node && node->needed)
18961 die->die_perennial_p = 1;
18962 /* Keep the parent DIEs as well. */
18963 while ((die = die->die_parent) && die->die_perennial_p == 0)
18964 die->die_perennial_p = 1;
18967 return 1;
18970 /* Mark all members of used_types_hash as perennial. */
18972 static void
18973 premark_used_types (void)
18975 if (cfun && cfun->used_types_hash)
18976 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18979 /* Mark all members of types_used_by_vars_entry as perennial. */
18981 static void
18982 premark_types_used_by_global_vars (void)
18984 if (types_used_by_vars_hash)
18985 htab_traverse (types_used_by_vars_hash,
18986 premark_types_used_by_global_vars_helper, NULL);
18989 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18990 for CA_LOC call arg loc node. */
18992 static dw_die_ref
18993 gen_call_site_die (tree decl, dw_die_ref subr_die,
18994 struct call_arg_loc_node *ca_loc)
18996 dw_die_ref stmt_die = NULL, die;
18997 tree block = ca_loc->block;
18999 while (block
19000 && block != DECL_INITIAL (decl)
19001 && TREE_CODE (block) == BLOCK)
19003 if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
19004 stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
19005 if (stmt_die)
19006 break;
19007 block = BLOCK_SUPERCONTEXT (block);
19009 if (stmt_die == NULL)
19010 stmt_die = subr_die;
19011 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
19012 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
19013 if (ca_loc->tail_call_p)
19014 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
19015 if (ca_loc->symbol_ref)
19017 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
19018 if (tdie)
19019 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
19020 else
19021 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
19023 return die;
19026 /* Generate a DIE to represent a declared function (either file-scope or
19027 block-local). */
19029 static void
19030 gen_subprogram_die (tree decl, dw_die_ref context_die)
19032 tree origin = decl_ultimate_origin (decl);
19033 dw_die_ref subr_die;
19034 tree outer_scope;
19035 dw_die_ref old_die = lookup_decl_die (decl);
19036 int declaration = (current_function_decl != decl
19037 || class_or_namespace_scope_p (context_die));
19039 premark_used_types ();
19041 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
19042 started to generate the abstract instance of an inline, decided to output
19043 its containing class, and proceeded to emit the declaration of the inline
19044 from the member list for the class. If so, DECLARATION takes priority;
19045 we'll get back to the abstract instance when done with the class. */
19047 /* The class-scope declaration DIE must be the primary DIE. */
19048 if (origin && declaration && class_or_namespace_scope_p (context_die))
19050 origin = NULL;
19051 gcc_assert (!old_die);
19054 /* Now that the C++ front end lazily declares artificial member fns, we
19055 might need to retrofit the declaration into its class. */
19056 if (!declaration && !origin && !old_die
19057 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
19058 && !class_or_namespace_scope_p (context_die)
19059 && debug_info_level > DINFO_LEVEL_TERSE)
19060 old_die = force_decl_die (decl);
19062 if (origin != NULL)
19064 gcc_assert (!declaration || local_scope_p (context_die));
19066 /* Fixup die_parent for the abstract instance of a nested
19067 inline function. */
19068 if (old_die && old_die->die_parent == NULL)
19069 add_child_die (context_die, old_die);
19071 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19072 add_abstract_origin_attribute (subr_die, origin);
19074 else if (old_die)
19076 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19077 struct dwarf_file_data * file_index = lookup_filename (s.file);
19079 if (!get_AT_flag (old_die, DW_AT_declaration)
19080 /* We can have a normal definition following an inline one in the
19081 case of redefinition of GNU C extern inlines.
19082 It seems reasonable to use AT_specification in this case. */
19083 && !get_AT (old_die, DW_AT_inline))
19085 /* Detect and ignore this case, where we are trying to output
19086 something we have already output. */
19087 return;
19090 /* If the definition comes from the same place as the declaration,
19091 maybe use the old DIE. We always want the DIE for this function
19092 that has the *_pc attributes to be under comp_unit_die so the
19093 debugger can find it. We also need to do this for abstract
19094 instances of inlines, since the spec requires the out-of-line copy
19095 to have the same parent. For local class methods, this doesn't
19096 apply; we just use the old DIE. */
19097 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
19098 && (DECL_ARTIFICIAL (decl)
19099 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
19100 && (get_AT_unsigned (old_die, DW_AT_decl_line)
19101 == (unsigned) s.line))))
19103 subr_die = old_die;
19105 /* Clear out the declaration attribute and the formal parameters.
19106 Do not remove all children, because it is possible that this
19107 declaration die was forced using force_decl_die(). In such
19108 cases die that forced declaration die (e.g. TAG_imported_module)
19109 is one of the children that we do not want to remove. */
19110 remove_AT (subr_die, DW_AT_declaration);
19111 remove_AT (subr_die, DW_AT_object_pointer);
19112 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
19114 else
19116 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19117 add_AT_specification (subr_die, old_die);
19118 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19119 add_AT_file (subr_die, DW_AT_decl_file, file_index);
19120 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19121 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
19124 else
19126 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19128 if (TREE_PUBLIC (decl))
19129 add_AT_flag (subr_die, DW_AT_external, 1);
19131 add_name_and_src_coords_attributes (subr_die, decl);
19132 if (debug_info_level > DINFO_LEVEL_TERSE)
19134 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19135 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19136 0, 0, context_die);
19139 add_pure_or_virtual_attribute (subr_die, decl);
19140 if (DECL_ARTIFICIAL (decl))
19141 add_AT_flag (subr_die, DW_AT_artificial, 1);
19143 add_accessibility_attribute (subr_die, decl);
19146 if (declaration)
19148 if (!old_die || !get_AT (old_die, DW_AT_inline))
19150 add_AT_flag (subr_die, DW_AT_declaration, 1);
19152 /* If this is an explicit function declaration then generate
19153 a DW_AT_explicit attribute. */
19154 if (lang_hooks.decls.function_decl_explicit_p (decl)
19155 && (dwarf_version >= 3 || !dwarf_strict))
19156 add_AT_flag (subr_die, DW_AT_explicit, 1);
19158 /* The first time we see a member function, it is in the context of
19159 the class to which it belongs. We make sure of this by emitting
19160 the class first. The next time is the definition, which is
19161 handled above. The two may come from the same source text.
19163 Note that force_decl_die() forces function declaration die. It is
19164 later reused to represent definition. */
19165 equate_decl_number_to_die (decl, subr_die);
19168 else if (DECL_ABSTRACT (decl))
19170 if (DECL_DECLARED_INLINE_P (decl))
19172 if (cgraph_function_possibly_inlined_p (decl))
19173 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19174 else
19175 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19177 else
19179 if (cgraph_function_possibly_inlined_p (decl))
19180 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19181 else
19182 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19185 if (DECL_DECLARED_INLINE_P (decl)
19186 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19187 add_AT_flag (subr_die, DW_AT_artificial, 1);
19189 equate_decl_number_to_die (decl, subr_die);
19191 else if (!DECL_EXTERNAL (decl))
19193 HOST_WIDE_INT cfa_fb_offset;
19195 if (!old_die || !get_AT (old_die, DW_AT_inline))
19196 equate_decl_number_to_die (decl, subr_die);
19198 if (!flag_reorder_blocks_and_partition)
19200 dw_fde_ref fde = &fde_table[current_funcdef_fde];
19201 if (fde->dw_fde_begin)
19203 /* We have already generated the labels. */
19204 add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19205 add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19207 else
19209 /* Create start/end labels and add the range. */
19210 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19211 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19212 current_function_funcdef_no);
19213 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19214 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19215 current_function_funcdef_no);
19216 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19219 #if VMS_DEBUGGING_INFO
19220 /* HP OpenVMS Industry Standard 64: DWARF Extensions
19221 Section 2.3 Prologue and Epilogue Attributes:
19222 When a breakpoint is set on entry to a function, it is generally
19223 desirable for execution to be suspended, not on the very first
19224 instruction of the function, but rather at a point after the
19225 function's frame has been set up, after any language defined local
19226 declaration processing has been completed, and before execution of
19227 the first statement of the function begins. Debuggers generally
19228 cannot properly determine where this point is. Similarly for a
19229 breakpoint set on exit from a function. The prologue and epilogue
19230 attributes allow a compiler to communicate the location(s) to use. */
19233 if (fde->dw_fde_vms_end_prologue)
19234 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19235 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19237 if (fde->dw_fde_vms_begin_epilogue)
19238 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19239 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19241 #endif
19243 add_pubname (decl, subr_die);
19245 else
19246 { /* Generate pubnames entries for the split function code
19247 ranges. */
19248 dw_fde_ref fde = &fde_table[current_funcdef_fde];
19250 if (fde->dw_fde_second_begin)
19252 if (dwarf_version >= 3 || !dwarf_strict)
19254 /* We should use ranges for non-contiguous code section
19255 addresses. Use the actual code range for the initial
19256 section, since the HOT/COLD labels might precede an
19257 alignment offset. */
19258 bool range_list_added = false;
19259 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
19260 fde->dw_fde_end, &range_list_added);
19261 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
19262 fde->dw_fde_second_end,
19263 &range_list_added);
19264 add_pubname (decl, subr_die);
19265 if (range_list_added)
19266 add_ranges (NULL);
19268 else
19270 /* There is no real support in DW2 for this .. so we make
19271 a work-around. First, emit the pub name for the segment
19272 containing the function label. Then make and emit a
19273 simplified subprogram DIE for the second segment with the
19274 name pre-fixed by __hot/cold_sect_of_. We use the same
19275 linkage name for the second die so that gdb will find both
19276 sections when given "b foo". */
19277 const char *name = NULL;
19278 tree decl_name = DECL_NAME (decl);
19279 dw_die_ref seg_die;
19281 /* Do the 'primary' section. */
19282 add_AT_lbl_id (subr_die, DW_AT_low_pc,
19283 fde->dw_fde_begin);
19284 add_AT_lbl_id (subr_die, DW_AT_high_pc,
19285 fde->dw_fde_end);
19286 /* Add it. */
19287 add_pubname (decl, subr_die);
19289 /* Build a minimal DIE for the secondary section. */
19290 seg_die = new_die (DW_TAG_subprogram,
19291 subr_die->die_parent, decl);
19293 if (TREE_PUBLIC (decl))
19294 add_AT_flag (seg_die, DW_AT_external, 1);
19296 if (decl_name != NULL
19297 && IDENTIFIER_POINTER (decl_name) != NULL)
19299 name = dwarf2_name (decl, 1);
19300 if (! DECL_ARTIFICIAL (decl))
19301 add_src_coords_attributes (seg_die, decl);
19303 add_linkage_name (seg_die, decl);
19305 gcc_assert (name != NULL);
19306 add_pure_or_virtual_attribute (seg_die, decl);
19307 if (DECL_ARTIFICIAL (decl))
19308 add_AT_flag (seg_die, DW_AT_artificial, 1);
19310 name = concat ("__second_sect_of_", name, NULL);
19311 add_AT_lbl_id (seg_die, DW_AT_low_pc,
19312 fde->dw_fde_second_begin);
19313 add_AT_lbl_id (seg_die, DW_AT_high_pc,
19314 fde->dw_fde_second_end);
19315 add_name_attribute (seg_die, name);
19316 add_pubname_string (name, seg_die);
19319 else
19321 add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19322 add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19323 add_pubname (decl, subr_die);
19327 #ifdef MIPS_DEBUGGING_INFO
19328 /* Add a reference to the FDE for this routine. */
19329 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19330 #endif
19332 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19334 /* We define the "frame base" as the function's CFA. This is more
19335 convenient for several reasons: (1) It's stable across the prologue
19336 and epilogue, which makes it better than just a frame pointer,
19337 (2) With dwarf3, there exists a one-byte encoding that allows us
19338 to reference the .debug_frame data by proxy, but failing that,
19339 (3) We can at least reuse the code inspection and interpretation
19340 code that determines the CFA position at various points in the
19341 function. */
19342 if (dwarf_version >= 3)
19344 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19345 add_AT_loc (subr_die, DW_AT_frame_base, op);
19347 else
19349 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19350 if (list->dw_loc_next)
19351 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19352 else
19353 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19356 /* Compute a displacement from the "steady-state frame pointer" to
19357 the CFA. The former is what all stack slots and argument slots
19358 will reference in the rtl; the later is what we've told the
19359 debugger about. We'll need to adjust all frame_base references
19360 by this displacement. */
19361 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19363 if (cfun->static_chain_decl)
19364 add_AT_location_description (subr_die, DW_AT_static_link,
19365 loc_list_from_tree (cfun->static_chain_decl, 2));
19368 /* Generate child dies for template paramaters. */
19369 if (debug_info_level > DINFO_LEVEL_TERSE)
19370 gen_generic_params_dies (decl);
19372 /* Now output descriptions of the arguments for this function. This gets
19373 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19374 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19375 `...' at the end of the formal parameter list. In order to find out if
19376 there was a trailing ellipsis or not, we must instead look at the type
19377 associated with the FUNCTION_DECL. This will be a node of type
19378 FUNCTION_TYPE. If the chain of type nodes hanging off of this
19379 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19380 an ellipsis at the end. */
19382 /* In the case where we are describing a mere function declaration, all we
19383 need to do here (and all we *can* do here) is to describe the *types* of
19384 its formal parameters. */
19385 if (debug_info_level <= DINFO_LEVEL_TERSE)
19387 else if (declaration)
19388 gen_formal_types_die (decl, subr_die);
19389 else
19391 /* Generate DIEs to represent all known formal parameters. */
19392 tree parm = DECL_ARGUMENTS (decl);
19393 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19394 tree generic_decl_parm = generic_decl
19395 ? DECL_ARGUMENTS (generic_decl)
19396 : NULL;
19398 /* Now we want to walk the list of parameters of the function and
19399 emit their relevant DIEs.
19401 We consider the case of DECL being an instance of a generic function
19402 as well as it being a normal function.
19404 If DECL is an instance of a generic function we walk the
19405 parameters of the generic function declaration _and_ the parameters of
19406 DECL itself. This is useful because we want to emit specific DIEs for
19407 function parameter packs and those are declared as part of the
19408 generic function declaration. In that particular case,
19409 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19410 That DIE has children DIEs representing the set of arguments
19411 of the pack. Note that the set of pack arguments can be empty.
19412 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19413 children DIE.
19415 Otherwise, we just consider the parameters of DECL. */
19416 while (generic_decl_parm || parm)
19418 if (generic_decl_parm
19419 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19420 gen_formal_parameter_pack_die (generic_decl_parm,
19421 parm, subr_die,
19422 &parm);
19423 else if (parm)
19425 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19427 if (parm == DECL_ARGUMENTS (decl)
19428 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19429 && parm_die
19430 && (dwarf_version >= 3 || !dwarf_strict))
19431 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19433 parm = DECL_CHAIN (parm);
19436 if (generic_decl_parm)
19437 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19440 /* Decide whether we need an unspecified_parameters DIE at the end.
19441 There are 2 more cases to do this for: 1) the ansi ... declaration -
19442 this is detectable when the end of the arg list is not a
19443 void_type_node 2) an unprototyped function declaration (not a
19444 definition). This just means that we have no info about the
19445 parameters at all. */
19446 if (prototype_p (TREE_TYPE (decl)))
19448 /* This is the prototyped case, check for.... */
19449 if (stdarg_p (TREE_TYPE (decl)))
19450 gen_unspecified_parameters_die (decl, subr_die);
19452 else if (DECL_INITIAL (decl) == NULL_TREE)
19453 gen_unspecified_parameters_die (decl, subr_die);
19456 /* Output Dwarf info for all of the stuff within the body of the function
19457 (if it has one - it may be just a declaration). */
19458 outer_scope = DECL_INITIAL (decl);
19460 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19461 a function. This BLOCK actually represents the outermost binding contour
19462 for the function, i.e. the contour in which the function's formal
19463 parameters and labels get declared. Curiously, it appears that the front
19464 end doesn't actually put the PARM_DECL nodes for the current function onto
19465 the BLOCK_VARS list for this outer scope, but are strung off of the
19466 DECL_ARGUMENTS list for the function instead.
19468 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19469 the LABEL_DECL nodes for the function however, and we output DWARF info
19470 for those in decls_for_scope. Just within the `outer_scope' there will be
19471 a BLOCK node representing the function's outermost pair of curly braces,
19472 and any blocks used for the base and member initializers of a C++
19473 constructor function. */
19474 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19476 int call_site_note_count = 0;
19477 int tail_call_site_note_count = 0;
19479 /* Emit a DW_TAG_variable DIE for a named return value. */
19480 if (DECL_NAME (DECL_RESULT (decl)))
19481 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19483 current_function_has_inlines = 0;
19484 decls_for_scope (outer_scope, subr_die, 0);
19486 if (call_arg_locations && !dwarf_strict)
19488 struct call_arg_loc_node *ca_loc;
19489 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19491 dw_die_ref die = NULL;
19492 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19493 rtx arg, next_arg;
19495 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19496 arg; arg = next_arg)
19498 dw_loc_descr_ref reg, val;
19499 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19500 dw_die_ref cdie;
19502 next_arg = XEXP (arg, 1);
19503 if (REG_P (XEXP (XEXP (arg, 0), 0))
19504 && next_arg
19505 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19506 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19507 && REGNO (XEXP (XEXP (arg, 0), 0))
19508 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19509 next_arg = XEXP (next_arg, 1);
19510 if (mode == VOIDmode)
19512 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19513 if (mode == VOIDmode)
19514 mode = GET_MODE (XEXP (arg, 0));
19516 if (GET_MODE_CLASS (mode) != MODE_INT
19517 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
19518 continue;
19519 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19521 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19522 tloc = XEXP (XEXP (arg, 0), 1);
19523 continue;
19525 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19526 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19528 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19529 tlocc = XEXP (XEXP (arg, 0), 1);
19530 continue;
19532 if (REG_P (XEXP (XEXP (arg, 0), 0)))
19533 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19534 VAR_INIT_STATUS_INITIALIZED);
19535 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19536 reg = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 0),
19537 0), 0), mode,
19538 VAR_INIT_STATUS_INITIALIZED);
19539 else
19540 continue;
19541 if (reg == NULL)
19542 continue;
19543 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), VOIDmode,
19544 VAR_INIT_STATUS_INITIALIZED);
19545 if (val == NULL)
19546 continue;
19547 if (die == NULL)
19548 die = gen_call_site_die (decl, subr_die, ca_loc);
19549 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19550 NULL_TREE);
19551 add_AT_loc (cdie, DW_AT_location, reg);
19552 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19553 if (next_arg != XEXP (arg, 1))
19555 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19556 0), 1), VOIDmode,
19557 VAR_INIT_STATUS_INITIALIZED);
19558 if (val != NULL)
19559 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19562 if (die == NULL
19563 && (ca_loc->symbol_ref || tloc))
19564 die = gen_call_site_die (decl, subr_die, ca_loc);
19565 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19567 dw_loc_descr_ref tval = NULL;
19569 if (tloc != NULL_RTX)
19570 tval = mem_loc_descriptor (tloc, VOIDmode,
19571 VAR_INIT_STATUS_INITIALIZED);
19572 if (tval)
19573 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19574 else if (tlocc != NULL_RTX)
19576 tval = mem_loc_descriptor (tlocc, VOIDmode,
19577 VAR_INIT_STATUS_INITIALIZED);
19578 if (tval)
19579 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19580 tval);
19583 if (die != NULL)
19585 call_site_note_count++;
19586 if (ca_loc->tail_call_p)
19587 tail_call_site_note_count++;
19591 call_arg_locations = NULL;
19592 call_arg_loc_last = NULL;
19593 if (tail_call_site_count >= 0
19594 && tail_call_site_count == tail_call_site_note_count
19595 && !dwarf_strict)
19597 if (call_site_count >= 0
19598 && call_site_count == call_site_note_count)
19599 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19600 else
19601 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19603 call_site_count = -1;
19604 tail_call_site_count = -1;
19606 /* Add the calling convention attribute if requested. */
19607 add_calling_convention_attribute (subr_die, decl);
19611 /* Returns a hash value for X (which really is a die_struct). */
19613 static hashval_t
19614 common_block_die_table_hash (const void *x)
19616 const_dw_die_ref d = (const_dw_die_ref) x;
19617 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19620 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19621 as decl_id and die_parent of die_struct Y. */
19623 static int
19624 common_block_die_table_eq (const void *x, const void *y)
19626 const_dw_die_ref d = (const_dw_die_ref) x;
19627 const_dw_die_ref e = (const_dw_die_ref) y;
19628 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19631 /* Generate a DIE to represent a declared data object.
19632 Either DECL or ORIGIN must be non-null. */
19634 static void
19635 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19637 HOST_WIDE_INT off;
19638 tree com_decl;
19639 tree decl_or_origin = decl ? decl : origin;
19640 tree ultimate_origin;
19641 dw_die_ref var_die;
19642 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19643 dw_die_ref origin_die;
19644 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19645 || class_or_namespace_scope_p (context_die));
19646 bool specialization_p = false;
19648 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19649 if (decl || ultimate_origin)
19650 origin = ultimate_origin;
19651 com_decl = fortran_common (decl_or_origin, &off);
19653 /* Symbol in common gets emitted as a child of the common block, in the form
19654 of a data member. */
19655 if (com_decl)
19657 dw_die_ref com_die;
19658 dw_loc_list_ref loc;
19659 die_node com_die_arg;
19661 var_die = lookup_decl_die (decl_or_origin);
19662 if (var_die)
19664 if (get_AT (var_die, DW_AT_location) == NULL)
19666 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19667 if (loc)
19669 if (off)
19671 /* Optimize the common case. */
19672 if (single_element_loc_list_p (loc)
19673 && loc->expr->dw_loc_opc == DW_OP_addr
19674 && loc->expr->dw_loc_next == NULL
19675 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19676 == SYMBOL_REF)
19677 loc->expr->dw_loc_oprnd1.v.val_addr
19678 = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19679 else
19680 loc_list_plus_const (loc, off);
19682 add_AT_location_description (var_die, DW_AT_location, loc);
19683 remove_AT (var_die, DW_AT_declaration);
19686 return;
19689 if (common_block_die_table == NULL)
19690 common_block_die_table
19691 = htab_create_ggc (10, common_block_die_table_hash,
19692 common_block_die_table_eq, NULL);
19694 com_die_arg.decl_id = DECL_UID (com_decl);
19695 com_die_arg.die_parent = context_die;
19696 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19697 loc = loc_list_from_tree (com_decl, 2);
19698 if (com_die == NULL)
19700 const char *cnam
19701 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19702 void **slot;
19704 com_die = new_die (DW_TAG_common_block, context_die, decl);
19705 add_name_and_src_coords_attributes (com_die, com_decl);
19706 if (loc)
19708 add_AT_location_description (com_die, DW_AT_location, loc);
19709 /* Avoid sharing the same loc descriptor between
19710 DW_TAG_common_block and DW_TAG_variable. */
19711 loc = loc_list_from_tree (com_decl, 2);
19713 else if (DECL_EXTERNAL (decl))
19714 add_AT_flag (com_die, DW_AT_declaration, 1);
19715 add_pubname_string (cnam, com_die); /* ??? needed? */
19716 com_die->decl_id = DECL_UID (com_decl);
19717 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19718 *slot = (void *) com_die;
19720 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19722 add_AT_location_description (com_die, DW_AT_location, loc);
19723 loc = loc_list_from_tree (com_decl, 2);
19724 remove_AT (com_die, DW_AT_declaration);
19726 var_die = new_die (DW_TAG_variable, com_die, decl);
19727 add_name_and_src_coords_attributes (var_die, decl);
19728 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19729 TREE_THIS_VOLATILE (decl), context_die);
19730 add_AT_flag (var_die, DW_AT_external, 1);
19731 if (loc)
19733 if (off)
19735 /* Optimize the common case. */
19736 if (single_element_loc_list_p (loc)
19737 && loc->expr->dw_loc_opc == DW_OP_addr
19738 && loc->expr->dw_loc_next == NULL
19739 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19740 loc->expr->dw_loc_oprnd1.v.val_addr
19741 = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19742 else
19743 loc_list_plus_const (loc, off);
19745 add_AT_location_description (var_die, DW_AT_location, loc);
19747 else if (DECL_EXTERNAL (decl))
19748 add_AT_flag (var_die, DW_AT_declaration, 1);
19749 equate_decl_number_to_die (decl, var_die);
19750 return;
19753 /* If the compiler emitted a definition for the DECL declaration
19754 and if we already emitted a DIE for it, don't emit a second
19755 DIE for it again. Allow re-declarations of DECLs that are
19756 inside functions, though. */
19757 if (old_die && declaration && !local_scope_p (context_die))
19758 return;
19760 /* For static data members, the declaration in the class is supposed
19761 to have DW_TAG_member tag; the specification should still be
19762 DW_TAG_variable referencing the DW_TAG_member DIE. */
19763 if (declaration && class_scope_p (context_die))
19764 var_die = new_die (DW_TAG_member, context_die, decl);
19765 else
19766 var_die = new_die (DW_TAG_variable, context_die, decl);
19768 origin_die = NULL;
19769 if (origin != NULL)
19770 origin_die = add_abstract_origin_attribute (var_die, origin);
19772 /* Loop unrolling can create multiple blocks that refer to the same
19773 static variable, so we must test for the DW_AT_declaration flag.
19775 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19776 copy decls and set the DECL_ABSTRACT flag on them instead of
19777 sharing them.
19779 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19781 ??? The declare_in_namespace support causes us to get two DIEs for one
19782 variable, both of which are declarations. We want to avoid considering
19783 one to be a specification, so we must test that this DIE is not a
19784 declaration. */
19785 else if (old_die && TREE_STATIC (decl) && ! declaration
19786 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19788 /* This is a definition of a C++ class level static. */
19789 add_AT_specification (var_die, old_die);
19790 specialization_p = true;
19791 if (DECL_NAME (decl))
19793 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19794 struct dwarf_file_data * file_index = lookup_filename (s.file);
19796 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19797 add_AT_file (var_die, DW_AT_decl_file, file_index);
19799 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19800 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19802 if (old_die->die_tag == DW_TAG_member)
19803 add_linkage_name (var_die, decl);
19806 else
19807 add_name_and_src_coords_attributes (var_die, decl);
19809 if ((origin == NULL && !specialization_p)
19810 || (origin != NULL
19811 && !DECL_ABSTRACT (decl_or_origin)
19812 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19813 decl_function_context
19814 (decl_or_origin))))
19816 tree type = TREE_TYPE (decl_or_origin);
19818 if (decl_by_reference_p (decl_or_origin))
19819 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19820 else
19821 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19822 TREE_THIS_VOLATILE (decl_or_origin), context_die);
19825 if (origin == NULL && !specialization_p)
19827 if (TREE_PUBLIC (decl))
19828 add_AT_flag (var_die, DW_AT_external, 1);
19830 if (DECL_ARTIFICIAL (decl))
19831 add_AT_flag (var_die, DW_AT_artificial, 1);
19833 add_accessibility_attribute (var_die, decl);
19836 if (declaration)
19837 add_AT_flag (var_die, DW_AT_declaration, 1);
19839 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19840 equate_decl_number_to_die (decl, var_die);
19842 if (! declaration
19843 && (! DECL_ABSTRACT (decl_or_origin)
19844 /* Local static vars are shared between all clones/inlines,
19845 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19846 already set. */
19847 || (TREE_CODE (decl_or_origin) == VAR_DECL
19848 && TREE_STATIC (decl_or_origin)
19849 && DECL_RTL_SET_P (decl_or_origin)))
19850 /* When abstract origin already has DW_AT_location attribute, no need
19851 to add it again. */
19852 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19854 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19855 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19856 defer_location (decl_or_origin, var_die);
19857 else
19858 add_location_or_const_value_attribute (var_die, decl_or_origin,
19859 decl == NULL, DW_AT_location);
19860 add_pubname (decl_or_origin, var_die);
19862 else
19863 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19866 /* Generate a DIE to represent a named constant. */
19868 static void
19869 gen_const_die (tree decl, dw_die_ref context_die)
19871 dw_die_ref const_die;
19872 tree type = TREE_TYPE (decl);
19874 const_die = new_die (DW_TAG_constant, context_die, decl);
19875 add_name_and_src_coords_attributes (const_die, decl);
19876 add_type_attribute (const_die, type, 1, 0, context_die);
19877 if (TREE_PUBLIC (decl))
19878 add_AT_flag (const_die, DW_AT_external, 1);
19879 if (DECL_ARTIFICIAL (decl))
19880 add_AT_flag (const_die, DW_AT_artificial, 1);
19881 tree_add_const_value_attribute_for_decl (const_die, decl);
19884 /* Generate a DIE to represent a label identifier. */
19886 static void
19887 gen_label_die (tree decl, dw_die_ref context_die)
19889 tree origin = decl_ultimate_origin (decl);
19890 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19891 rtx insn;
19892 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19894 if (origin != NULL)
19895 add_abstract_origin_attribute (lbl_die, origin);
19896 else
19897 add_name_and_src_coords_attributes (lbl_die, decl);
19899 if (DECL_ABSTRACT (decl))
19900 equate_decl_number_to_die (decl, lbl_die);
19901 else
19903 insn = DECL_RTL_IF_SET (decl);
19905 /* Deleted labels are programmer specified labels which have been
19906 eliminated because of various optimizations. We still emit them
19907 here so that it is possible to put breakpoints on them. */
19908 if (insn
19909 && (LABEL_P (insn)
19910 || ((NOTE_P (insn)
19911 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19913 /* When optimization is enabled (via -O) some parts of the compiler
19914 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19915 represent source-level labels which were explicitly declared by
19916 the user. This really shouldn't be happening though, so catch
19917 it if it ever does happen. */
19918 gcc_assert (!INSN_DELETED_P (insn));
19920 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19921 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19926 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19927 attributes to the DIE for a block STMT, to describe where the inlined
19928 function was called from. This is similar to add_src_coords_attributes. */
19930 static inline void
19931 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19933 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19935 if (dwarf_version >= 3 || !dwarf_strict)
19937 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19938 add_AT_unsigned (die, DW_AT_call_line, s.line);
19943 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19944 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19946 static inline void
19947 add_high_low_attributes (tree stmt, dw_die_ref die)
19949 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19951 if (BLOCK_FRAGMENT_CHAIN (stmt)
19952 && (dwarf_version >= 3 || !dwarf_strict))
19954 tree chain;
19956 if (inlined_function_outer_scope_p (stmt))
19958 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19959 BLOCK_NUMBER (stmt));
19960 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19963 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19965 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19968 add_ranges (chain);
19969 chain = BLOCK_FRAGMENT_CHAIN (chain);
19971 while (chain);
19972 add_ranges (NULL);
19974 else
19976 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19977 BLOCK_NUMBER (stmt));
19978 add_AT_lbl_id (die, DW_AT_low_pc, label);
19979 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19980 BLOCK_NUMBER (stmt));
19981 add_AT_lbl_id (die, DW_AT_high_pc, label);
19985 /* Generate a DIE for a lexical block. */
19987 static void
19988 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19990 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19992 if (call_arg_locations)
19994 if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
19995 VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
19996 BLOCK_NUMBER (stmt) + 1);
19997 VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
20000 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
20001 add_high_low_attributes (stmt, stmt_die);
20003 decls_for_scope (stmt, stmt_die, depth);
20006 /* Generate a DIE for an inlined subprogram. */
20008 static void
20009 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
20011 tree decl;
20013 /* The instance of function that is effectively being inlined shall not
20014 be abstract. */
20015 gcc_assert (! BLOCK_ABSTRACT (stmt));
20017 decl = block_ultimate_origin (stmt);
20019 /* Emit info for the abstract instance first, if we haven't yet. We
20020 must emit this even if the block is abstract, otherwise when we
20021 emit the block below (or elsewhere), we may end up trying to emit
20022 a die whose origin die hasn't been emitted, and crashing. */
20023 dwarf2out_abstract_function (decl);
20025 if (! BLOCK_ABSTRACT (stmt))
20027 dw_die_ref subr_die
20028 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
20030 if (call_arg_locations)
20032 if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20033 VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20034 BLOCK_NUMBER (stmt) + 1);
20035 VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
20037 add_abstract_origin_attribute (subr_die, decl);
20038 if (TREE_ASM_WRITTEN (stmt))
20039 add_high_low_attributes (stmt, subr_die);
20040 add_call_src_coords_attributes (stmt, subr_die);
20042 decls_for_scope (stmt, subr_die, depth);
20043 current_function_has_inlines = 1;
20047 /* Generate a DIE for a field in a record, or structure. */
20049 static void
20050 gen_field_die (tree decl, dw_die_ref context_die)
20052 dw_die_ref decl_die;
20054 if (TREE_TYPE (decl) == error_mark_node)
20055 return;
20057 decl_die = new_die (DW_TAG_member, context_die, decl);
20058 add_name_and_src_coords_attributes (decl_die, decl);
20059 add_type_attribute (decl_die, member_declared_type (decl),
20060 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
20061 context_die);
20063 if (DECL_BIT_FIELD_TYPE (decl))
20065 add_byte_size_attribute (decl_die, decl);
20066 add_bit_size_attribute (decl_die, decl);
20067 add_bit_offset_attribute (decl_die, decl);
20070 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20071 add_data_member_location_attribute (decl_die, decl);
20073 if (DECL_ARTIFICIAL (decl))
20074 add_AT_flag (decl_die, DW_AT_artificial, 1);
20076 add_accessibility_attribute (decl_die, decl);
20078 /* Equate decl number to die, so that we can look up this decl later on. */
20079 equate_decl_number_to_die (decl, decl_die);
20082 #if 0
20083 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20084 Use modified_type_die instead.
20085 We keep this code here just in case these types of DIEs may be needed to
20086 represent certain things in other languages (e.g. Pascal) someday. */
20088 static void
20089 gen_pointer_type_die (tree type, dw_die_ref context_die)
20091 dw_die_ref ptr_die
20092 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20094 equate_type_number_to_die (type, ptr_die);
20095 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20096 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20099 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20100 Use modified_type_die instead.
20101 We keep this code here just in case these types of DIEs may be needed to
20102 represent certain things in other languages (e.g. Pascal) someday. */
20104 static void
20105 gen_reference_type_die (tree type, dw_die_ref context_die)
20107 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20109 if (TYPE_REF_IS_RVALUE (type) && use_debug_types)
20110 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20111 else
20112 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20114 equate_type_number_to_die (type, ref_die);
20115 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
20116 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20118 #endif
20120 /* Generate a DIE for a pointer to a member type. */
20122 static void
20123 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20125 dw_die_ref ptr_die
20126 = new_die (DW_TAG_ptr_to_member_type,
20127 scope_die_for (type, context_die), type);
20129 equate_type_number_to_die (type, ptr_die);
20130 add_AT_die_ref (ptr_die, DW_AT_containing_type,
20131 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20132 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20135 /* Generate the DIE for the compilation unit. */
20137 static dw_die_ref
20138 gen_compile_unit_die (const char *filename)
20140 dw_die_ref die;
20141 char producer[250];
20142 const char *language_string = lang_hooks.name;
20143 int language;
20145 die = new_die (DW_TAG_compile_unit, NULL, NULL);
20147 if (filename)
20149 add_name_attribute (die, filename);
20150 /* Don't add cwd for <built-in>. */
20151 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20152 add_comp_dir_attribute (die);
20155 sprintf (producer, "%s %s", language_string, version_string);
20157 #ifdef MIPS_DEBUGGING_INFO
20158 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
20159 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
20160 not appear in the producer string, the debugger reaches the conclusion
20161 that the object file is stripped and has no debugging information.
20162 To get the MIPS/SGI debugger to believe that there is debugging
20163 information in the object file, we add a -g to the producer string. */
20164 if (debug_info_level > DINFO_LEVEL_TERSE)
20165 strcat (producer, " -g");
20166 #endif
20168 add_AT_string (die, DW_AT_producer, producer);
20170 /* If our producer is LTO try to figure out a common language to use
20171 from the global list of translation units. */
20172 if (strcmp (language_string, "GNU GIMPLE") == 0)
20174 unsigned i;
20175 tree t;
20176 const char *common_lang = NULL;
20178 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
20180 if (!TRANSLATION_UNIT_LANGUAGE (t))
20181 continue;
20182 if (!common_lang)
20183 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20184 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20186 else if (strncmp (common_lang, "GNU C", 5) == 0
20187 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20188 /* Mixing C and C++ is ok, use C++ in that case. */
20189 common_lang = "GNU C++";
20190 else
20192 /* Fall back to C. */
20193 common_lang = NULL;
20194 break;
20198 if (common_lang)
20199 language_string = common_lang;
20202 language = DW_LANG_C89;
20203 if (strcmp (language_string, "GNU C++") == 0)
20204 language = DW_LANG_C_plus_plus;
20205 else if (strcmp (language_string, "GNU F77") == 0)
20206 language = DW_LANG_Fortran77;
20207 else if (strcmp (language_string, "GNU Pascal") == 0)
20208 language = DW_LANG_Pascal83;
20209 else if (dwarf_version >= 3 || !dwarf_strict)
20211 if (strcmp (language_string, "GNU Ada") == 0)
20212 language = DW_LANG_Ada95;
20213 else if (strcmp (language_string, "GNU Fortran") == 0)
20214 language = DW_LANG_Fortran95;
20215 else if (strcmp (language_string, "GNU Java") == 0)
20216 language = DW_LANG_Java;
20217 else if (strcmp (language_string, "GNU Objective-C") == 0)
20218 language = DW_LANG_ObjC;
20219 else if (strcmp (language_string, "GNU Objective-C++") == 0)
20220 language = DW_LANG_ObjC_plus_plus;
20223 add_AT_unsigned (die, DW_AT_language, language);
20225 switch (language)
20227 case DW_LANG_Fortran77:
20228 case DW_LANG_Fortran90:
20229 case DW_LANG_Fortran95:
20230 /* Fortran has case insensitive identifiers and the front-end
20231 lowercases everything. */
20232 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20233 break;
20234 default:
20235 /* The default DW_ID_case_sensitive doesn't need to be specified. */
20236 break;
20238 return die;
20241 /* Generate the DIE for a base class. */
20243 static void
20244 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20246 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20248 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20249 add_data_member_location_attribute (die, binfo);
20251 if (BINFO_VIRTUAL_P (binfo))
20252 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20254 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20255 children, otherwise the default is DW_ACCESS_public. In DWARF2
20256 the default has always been DW_ACCESS_private. */
20257 if (access == access_public_node)
20259 if (dwarf_version == 2
20260 || context_die->die_tag == DW_TAG_class_type)
20261 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20263 else if (access == access_protected_node)
20264 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20265 else if (dwarf_version > 2
20266 && context_die->die_tag != DW_TAG_class_type)
20267 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20270 /* Generate a DIE for a class member. */
20272 static void
20273 gen_member_die (tree type, dw_die_ref context_die)
20275 tree member;
20276 tree binfo = TYPE_BINFO (type);
20277 dw_die_ref child;
20279 /* If this is not an incomplete type, output descriptions of each of its
20280 members. Note that as we output the DIEs necessary to represent the
20281 members of this record or union type, we will also be trying to output
20282 DIEs to represent the *types* of those members. However the `type'
20283 function (above) will specifically avoid generating type DIEs for member
20284 types *within* the list of member DIEs for this (containing) type except
20285 for those types (of members) which are explicitly marked as also being
20286 members of this (containing) type themselves. The g++ front- end can
20287 force any given type to be treated as a member of some other (containing)
20288 type by setting the TYPE_CONTEXT of the given (member) type to point to
20289 the TREE node representing the appropriate (containing) type. */
20291 /* First output info about the base classes. */
20292 if (binfo)
20294 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20295 int i;
20296 tree base;
20298 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20299 gen_inheritance_die (base,
20300 (accesses ? VEC_index (tree, accesses, i)
20301 : access_public_node), context_die);
20304 /* Now output info about the data members and type members. */
20305 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20307 /* If we thought we were generating minimal debug info for TYPE
20308 and then changed our minds, some of the member declarations
20309 may have already been defined. Don't define them again, but
20310 do put them in the right order. */
20312 child = lookup_decl_die (member);
20313 if (child)
20314 splice_child_die (context_die, child);
20315 else
20316 gen_decl_die (member, NULL, context_die);
20319 /* Now output info about the function members (if any). */
20320 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20322 /* Don't include clones in the member list. */
20323 if (DECL_ABSTRACT_ORIGIN (member))
20324 continue;
20326 child = lookup_decl_die (member);
20327 if (child)
20328 splice_child_die (context_die, child);
20329 else
20330 gen_decl_die (member, NULL, context_die);
20334 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20335 is set, we pretend that the type was never defined, so we only get the
20336 member DIEs needed by later specification DIEs. */
20338 static void
20339 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20340 enum debug_info_usage usage)
20342 dw_die_ref type_die = lookup_type_die (type);
20343 dw_die_ref scope_die = 0;
20344 int nested = 0;
20345 int complete = (TYPE_SIZE (type)
20346 && (! TYPE_STUB_DECL (type)
20347 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20348 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20349 complete = complete && should_emit_struct_debug (type, usage);
20351 if (type_die && ! complete)
20352 return;
20354 if (TYPE_CONTEXT (type) != NULL_TREE
20355 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20356 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20357 nested = 1;
20359 scope_die = scope_die_for (type, context_die);
20361 if (! type_die || (nested && is_cu_die (scope_die)))
20362 /* First occurrence of type or toplevel definition of nested class. */
20364 dw_die_ref old_die = type_die;
20366 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20367 ? record_type_tag (type) : DW_TAG_union_type,
20368 scope_die, type);
20369 equate_type_number_to_die (type, type_die);
20370 if (old_die)
20371 add_AT_specification (type_die, old_die);
20372 else
20374 add_name_attribute (type_die, type_tag (type));
20375 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20378 else
20379 remove_AT (type_die, DW_AT_declaration);
20381 /* Generate child dies for template paramaters. */
20382 if (debug_info_level > DINFO_LEVEL_TERSE
20383 && COMPLETE_TYPE_P (type))
20384 schedule_generic_params_dies_gen (type);
20386 /* If this type has been completed, then give it a byte_size attribute and
20387 then give a list of members. */
20388 if (complete && !ns_decl)
20390 /* Prevent infinite recursion in cases where the type of some member of
20391 this type is expressed in terms of this type itself. */
20392 TREE_ASM_WRITTEN (type) = 1;
20393 add_byte_size_attribute (type_die, type);
20394 if (TYPE_STUB_DECL (type) != NULL_TREE)
20396 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20397 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20400 /* If the first reference to this type was as the return type of an
20401 inline function, then it may not have a parent. Fix this now. */
20402 if (type_die->die_parent == NULL)
20403 add_child_die (scope_die, type_die);
20405 push_decl_scope (type);
20406 gen_member_die (type, type_die);
20407 pop_decl_scope ();
20409 /* GNU extension: Record what type our vtable lives in. */
20410 if (TYPE_VFIELD (type))
20412 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20414 gen_type_die (vtype, context_die);
20415 add_AT_die_ref (type_die, DW_AT_containing_type,
20416 lookup_type_die (vtype));
20419 else
20421 add_AT_flag (type_die, DW_AT_declaration, 1);
20423 /* We don't need to do this for function-local types. */
20424 if (TYPE_STUB_DECL (type)
20425 && ! decl_function_context (TYPE_STUB_DECL (type)))
20426 VEC_safe_push (tree, gc, incomplete_types, type);
20429 if (get_AT (type_die, DW_AT_name))
20430 add_pubtype (type, type_die);
20433 /* Generate a DIE for a subroutine _type_. */
20435 static void
20436 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20438 tree return_type = TREE_TYPE (type);
20439 dw_die_ref subr_die
20440 = new_die (DW_TAG_subroutine_type,
20441 scope_die_for (type, context_die), type);
20443 equate_type_number_to_die (type, subr_die);
20444 add_prototyped_attribute (subr_die, type);
20445 add_type_attribute (subr_die, return_type, 0, 0, context_die);
20446 gen_formal_types_die (type, subr_die);
20448 if (get_AT (subr_die, DW_AT_name))
20449 add_pubtype (type, subr_die);
20452 /* Generate a DIE for a type definition. */
20454 static void
20455 gen_typedef_die (tree decl, dw_die_ref context_die)
20457 dw_die_ref type_die;
20458 tree origin;
20460 if (TREE_ASM_WRITTEN (decl))
20461 return;
20463 TREE_ASM_WRITTEN (decl) = 1;
20464 type_die = new_die (DW_TAG_typedef, context_die, decl);
20465 origin = decl_ultimate_origin (decl);
20466 if (origin != NULL)
20467 add_abstract_origin_attribute (type_die, origin);
20468 else
20470 tree type;
20472 add_name_and_src_coords_attributes (type_die, decl);
20473 if (DECL_ORIGINAL_TYPE (decl))
20475 type = DECL_ORIGINAL_TYPE (decl);
20477 gcc_assert (type != TREE_TYPE (decl));
20478 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20480 else
20482 type = TREE_TYPE (decl);
20484 if (is_naming_typedef_decl (TYPE_NAME (type)))
20486 /* Here, we are in the case of decl being a typedef naming
20487 an anonymous type, e.g:
20488 typedef struct {...} foo;
20489 In that case TREE_TYPE (decl) is not a typedef variant
20490 type and TYPE_NAME of the anonymous type is set to the
20491 TYPE_DECL of the typedef. This construct is emitted by
20492 the C++ FE.
20494 TYPE is the anonymous struct named by the typedef
20495 DECL. As we need the DW_AT_type attribute of the
20496 DW_TAG_typedef to point to the DIE of TYPE, let's
20497 generate that DIE right away. add_type_attribute
20498 called below will then pick (via lookup_type_die) that
20499 anonymous struct DIE. */
20500 if (!TREE_ASM_WRITTEN (type))
20501 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20503 /* This is a GNU Extension. We are adding a
20504 DW_AT_linkage_name attribute to the DIE of the
20505 anonymous struct TYPE. The value of that attribute
20506 is the name of the typedef decl naming the anonymous
20507 struct. This greatly eases the work of consumers of
20508 this debug info. */
20509 add_linkage_attr (lookup_type_die (type), decl);
20513 add_type_attribute (type_die, type, TREE_READONLY (decl),
20514 TREE_THIS_VOLATILE (decl), context_die);
20516 if (is_naming_typedef_decl (decl))
20517 /* We want that all subsequent calls to lookup_type_die with
20518 TYPE in argument yield the DW_TAG_typedef we have just
20519 created. */
20520 equate_type_number_to_die (type, type_die);
20522 add_accessibility_attribute (type_die, decl);
20525 if (DECL_ABSTRACT (decl))
20526 equate_decl_number_to_die (decl, type_die);
20528 if (get_AT (type_die, DW_AT_name))
20529 add_pubtype (decl, type_die);
20532 /* Generate a DIE for a struct, class, enum or union type. */
20534 static void
20535 gen_tagged_type_die (tree type,
20536 dw_die_ref context_die,
20537 enum debug_info_usage usage)
20539 int need_pop;
20541 if (type == NULL_TREE
20542 || !is_tagged_type (type))
20543 return;
20545 /* If this is a nested type whose containing class hasn't been written
20546 out yet, writing it out will cover this one, too. This does not apply
20547 to instantiations of member class templates; they need to be added to
20548 the containing class as they are generated. FIXME: This hurts the
20549 idea of combining type decls from multiple TUs, since we can't predict
20550 what set of template instantiations we'll get. */
20551 if (TYPE_CONTEXT (type)
20552 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20553 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20555 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20557 if (TREE_ASM_WRITTEN (type))
20558 return;
20560 /* If that failed, attach ourselves to the stub. */
20561 push_decl_scope (TYPE_CONTEXT (type));
20562 context_die = lookup_type_die (TYPE_CONTEXT (type));
20563 need_pop = 1;
20565 else if (TYPE_CONTEXT (type) != NULL_TREE
20566 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20568 /* If this type is local to a function that hasn't been written
20569 out yet, use a NULL context for now; it will be fixed up in
20570 decls_for_scope. */
20571 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20572 /* A declaration DIE doesn't count; nested types need to go in the
20573 specification. */
20574 if (context_die && is_declaration_die (context_die))
20575 context_die = NULL;
20576 need_pop = 0;
20578 else
20580 context_die = declare_in_namespace (type, context_die);
20581 need_pop = 0;
20584 if (TREE_CODE (type) == ENUMERAL_TYPE)
20586 /* This might have been written out by the call to
20587 declare_in_namespace. */
20588 if (!TREE_ASM_WRITTEN (type))
20589 gen_enumeration_type_die (type, context_die);
20591 else
20592 gen_struct_or_union_type_die (type, context_die, usage);
20594 if (need_pop)
20595 pop_decl_scope ();
20597 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20598 it up if it is ever completed. gen_*_type_die will set it for us
20599 when appropriate. */
20602 /* Generate a type description DIE. */
20604 static void
20605 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20606 enum debug_info_usage usage)
20608 struct array_descr_info info;
20610 if (type == NULL_TREE || type == error_mark_node)
20611 return;
20613 if (TYPE_NAME (type) != NULL_TREE
20614 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20615 && is_redundant_typedef (TYPE_NAME (type))
20616 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20617 /* The DECL of this type is a typedef we don't want to emit debug
20618 info for but we want debug info for its underlying typedef.
20619 This can happen for e.g, the injected-class-name of a C++
20620 type. */
20621 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20623 /* If TYPE is a typedef type variant, let's generate debug info
20624 for the parent typedef which TYPE is a type of. */
20625 if (typedef_variant_p (type))
20627 if (TREE_ASM_WRITTEN (type))
20628 return;
20630 /* Prevent broken recursion; we can't hand off to the same type. */
20631 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20633 /* Use the DIE of the containing namespace as the parent DIE of
20634 the type description DIE we want to generate. */
20635 if (DECL_CONTEXT (TYPE_NAME (type))
20636 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20637 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20639 TREE_ASM_WRITTEN (type) = 1;
20641 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20642 return;
20645 /* If type is an anonymous tagged type named by a typedef, let's
20646 generate debug info for the typedef. */
20647 if (is_naming_typedef_decl (TYPE_NAME (type)))
20649 /* Use the DIE of the containing namespace as the parent DIE of
20650 the type description DIE we want to generate. */
20651 if (DECL_CONTEXT (TYPE_NAME (type))
20652 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20653 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20655 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20656 return;
20659 /* If this is an array type with hidden descriptor, handle it first. */
20660 if (!TREE_ASM_WRITTEN (type)
20661 && lang_hooks.types.get_array_descr_info
20662 && lang_hooks.types.get_array_descr_info (type, &info)
20663 && (dwarf_version >= 3 || !dwarf_strict))
20665 gen_descr_array_type_die (type, &info, context_die);
20666 TREE_ASM_WRITTEN (type) = 1;
20667 return;
20670 /* We are going to output a DIE to represent the unqualified version
20671 of this type (i.e. without any const or volatile qualifiers) so
20672 get the main variant (i.e. the unqualified version) of this type
20673 now. (Vectors are special because the debugging info is in the
20674 cloned type itself). */
20675 if (TREE_CODE (type) != VECTOR_TYPE)
20676 type = type_main_variant (type);
20678 if (TREE_ASM_WRITTEN (type))
20679 return;
20681 switch (TREE_CODE (type))
20683 case ERROR_MARK:
20684 break;
20686 case POINTER_TYPE:
20687 case REFERENCE_TYPE:
20688 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20689 ensures that the gen_type_die recursion will terminate even if the
20690 type is recursive. Recursive types are possible in Ada. */
20691 /* ??? We could perhaps do this for all types before the switch
20692 statement. */
20693 TREE_ASM_WRITTEN (type) = 1;
20695 /* For these types, all that is required is that we output a DIE (or a
20696 set of DIEs) to represent the "basis" type. */
20697 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20698 DINFO_USAGE_IND_USE);
20699 break;
20701 case OFFSET_TYPE:
20702 /* This code is used for C++ pointer-to-data-member types.
20703 Output a description of the relevant class type. */
20704 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20705 DINFO_USAGE_IND_USE);
20707 /* Output a description of the type of the object pointed to. */
20708 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20709 DINFO_USAGE_IND_USE);
20711 /* Now output a DIE to represent this pointer-to-data-member type
20712 itself. */
20713 gen_ptr_to_mbr_type_die (type, context_die);
20714 break;
20716 case FUNCTION_TYPE:
20717 /* Force out return type (in case it wasn't forced out already). */
20718 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20719 DINFO_USAGE_DIR_USE);
20720 gen_subroutine_type_die (type, context_die);
20721 break;
20723 case METHOD_TYPE:
20724 /* Force out return type (in case it wasn't forced out already). */
20725 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20726 DINFO_USAGE_DIR_USE);
20727 gen_subroutine_type_die (type, context_die);
20728 break;
20730 case ARRAY_TYPE:
20731 gen_array_type_die (type, context_die);
20732 break;
20734 case VECTOR_TYPE:
20735 gen_array_type_die (type, context_die);
20736 break;
20738 case ENUMERAL_TYPE:
20739 case RECORD_TYPE:
20740 case UNION_TYPE:
20741 case QUAL_UNION_TYPE:
20742 gen_tagged_type_die (type, context_die, usage);
20743 return;
20745 case VOID_TYPE:
20746 case INTEGER_TYPE:
20747 case REAL_TYPE:
20748 case FIXED_POINT_TYPE:
20749 case COMPLEX_TYPE:
20750 case BOOLEAN_TYPE:
20751 /* No DIEs needed for fundamental types. */
20752 break;
20754 case NULLPTR_TYPE:
20755 case LANG_TYPE:
20756 /* Just use DW_TAG_unspecified_type. */
20758 dw_die_ref type_die = lookup_type_die (type);
20759 if (type_die == NULL)
20761 tree name = TYPE_NAME (type);
20762 if (TREE_CODE (name) == TYPE_DECL)
20763 name = DECL_NAME (name);
20764 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20765 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20766 equate_type_number_to_die (type, type_die);
20769 break;
20771 default:
20772 gcc_unreachable ();
20775 TREE_ASM_WRITTEN (type) = 1;
20778 static void
20779 gen_type_die (tree type, dw_die_ref context_die)
20781 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20784 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20785 things which are local to the given block. */
20787 static void
20788 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20790 int must_output_die = 0;
20791 bool inlined_func;
20793 /* Ignore blocks that are NULL. */
20794 if (stmt == NULL_TREE)
20795 return;
20797 inlined_func = inlined_function_outer_scope_p (stmt);
20799 /* If the block is one fragment of a non-contiguous block, do not
20800 process the variables, since they will have been done by the
20801 origin block. Do process subblocks. */
20802 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20804 tree sub;
20806 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20807 gen_block_die (sub, context_die, depth + 1);
20809 return;
20812 /* Determine if we need to output any Dwarf DIEs at all to represent this
20813 block. */
20814 if (inlined_func)
20815 /* The outer scopes for inlinings *must* always be represented. We
20816 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20817 must_output_die = 1;
20818 else
20820 /* Determine if this block directly contains any "significant"
20821 local declarations which we will need to output DIEs for. */
20822 if (debug_info_level > DINFO_LEVEL_TERSE)
20823 /* We are not in terse mode so *any* local declaration counts
20824 as being a "significant" one. */
20825 must_output_die = ((BLOCK_VARS (stmt) != NULL
20826 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20827 && (TREE_USED (stmt)
20828 || TREE_ASM_WRITTEN (stmt)
20829 || BLOCK_ABSTRACT (stmt)));
20830 else if ((TREE_USED (stmt)
20831 || TREE_ASM_WRITTEN (stmt)
20832 || BLOCK_ABSTRACT (stmt))
20833 && !dwarf2out_ignore_block (stmt))
20834 must_output_die = 1;
20837 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20838 DIE for any block which contains no significant local declarations at
20839 all. Rather, in such cases we just call `decls_for_scope' so that any
20840 needed Dwarf info for any sub-blocks will get properly generated. Note
20841 that in terse mode, our definition of what constitutes a "significant"
20842 local declaration gets restricted to include only inlined function
20843 instances and local (nested) function definitions. */
20844 if (must_output_die)
20846 if (inlined_func)
20848 /* If STMT block is abstract, that means we have been called
20849 indirectly from dwarf2out_abstract_function.
20850 That function rightfully marks the descendent blocks (of
20851 the abstract function it is dealing with) as being abstract,
20852 precisely to prevent us from emitting any
20853 DW_TAG_inlined_subroutine DIE as a descendent
20854 of an abstract function instance. So in that case, we should
20855 not call gen_inlined_subroutine_die.
20857 Later though, when cgraph asks dwarf2out to emit info
20858 for the concrete instance of the function decl into which
20859 the concrete instance of STMT got inlined, the later will lead
20860 to the generation of a DW_TAG_inlined_subroutine DIE. */
20861 if (! BLOCK_ABSTRACT (stmt))
20862 gen_inlined_subroutine_die (stmt, context_die, depth);
20864 else
20865 gen_lexical_block_die (stmt, context_die, depth);
20867 else
20868 decls_for_scope (stmt, context_die, depth);
20871 /* Process variable DECL (or variable with origin ORIGIN) within
20872 block STMT and add it to CONTEXT_DIE. */
20873 static void
20874 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20876 dw_die_ref die;
20877 tree decl_or_origin = decl ? decl : origin;
20879 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20880 die = lookup_decl_die (decl_or_origin);
20881 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20882 && TYPE_DECL_IS_STUB (decl_or_origin))
20883 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20884 else
20885 die = NULL;
20887 if (die != NULL && die->die_parent == NULL)
20888 add_child_die (context_die, die);
20889 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20890 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20891 stmt, context_die);
20892 else
20893 gen_decl_die (decl, origin, context_die);
20896 /* Generate all of the decls declared within a given scope and (recursively)
20897 all of its sub-blocks. */
20899 static void
20900 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20902 tree decl;
20903 unsigned int i;
20904 tree subblocks;
20906 /* Ignore NULL blocks. */
20907 if (stmt == NULL_TREE)
20908 return;
20910 /* Output the DIEs to represent all of the data objects and typedefs
20911 declared directly within this block but not within any nested
20912 sub-blocks. Also, nested function and tag DIEs have been
20913 generated with a parent of NULL; fix that up now. */
20914 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20915 process_scope_var (stmt, decl, NULL_TREE, context_die);
20916 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20917 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20918 context_die);
20920 /* If we're at -g1, we're not interested in subblocks. */
20921 if (debug_info_level <= DINFO_LEVEL_TERSE)
20922 return;
20924 /* Output the DIEs to represent all sub-blocks (and the items declared
20925 therein) of this block. */
20926 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20927 subblocks != NULL;
20928 subblocks = BLOCK_CHAIN (subblocks))
20929 gen_block_die (subblocks, context_die, depth + 1);
20932 /* Is this a typedef we can avoid emitting? */
20934 static inline int
20935 is_redundant_typedef (const_tree decl)
20937 if (TYPE_DECL_IS_STUB (decl))
20938 return 1;
20940 if (DECL_ARTIFICIAL (decl)
20941 && DECL_CONTEXT (decl)
20942 && is_tagged_type (DECL_CONTEXT (decl))
20943 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20944 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20945 /* Also ignore the artificial member typedef for the class name. */
20946 return 1;
20948 return 0;
20951 /* Return TRUE if TYPE is a typedef that names a type for linkage
20952 purposes. This kind of typedefs is produced by the C++ FE for
20953 constructs like:
20955 typedef struct {...} foo;
20957 In that case, there is no typedef variant type produced for foo.
20958 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20959 struct type. */
20961 static bool
20962 is_naming_typedef_decl (const_tree decl)
20964 if (decl == NULL_TREE
20965 || TREE_CODE (decl) != TYPE_DECL
20966 || !is_tagged_type (TREE_TYPE (decl))
20967 || DECL_IS_BUILTIN (decl)
20968 || is_redundant_typedef (decl)
20969 /* It looks like Ada produces TYPE_DECLs that are very similar
20970 to C++ naming typedefs but that have different
20971 semantics. Let's be specific to c++ for now. */
20972 || !is_cxx ())
20973 return FALSE;
20975 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20976 && TYPE_NAME (TREE_TYPE (decl)) == decl
20977 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20978 != TYPE_NAME (TREE_TYPE (decl))));
20981 /* Returns the DIE for a context. */
20983 static inline dw_die_ref
20984 get_context_die (tree context)
20986 if (context)
20988 /* Find die that represents this context. */
20989 if (TYPE_P (context))
20991 context = TYPE_MAIN_VARIANT (context);
20992 return strip_naming_typedef (context, force_type_die (context));
20994 else
20995 return force_decl_die (context);
20997 return comp_unit_die ();
21000 /* Returns the DIE for decl. A DIE will always be returned. */
21002 static dw_die_ref
21003 force_decl_die (tree decl)
21005 dw_die_ref decl_die;
21006 unsigned saved_external_flag;
21007 tree save_fn = NULL_TREE;
21008 decl_die = lookup_decl_die (decl);
21009 if (!decl_die)
21011 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21013 decl_die = lookup_decl_die (decl);
21014 if (decl_die)
21015 return decl_die;
21017 switch (TREE_CODE (decl))
21019 case FUNCTION_DECL:
21020 /* Clear current_function_decl, so that gen_subprogram_die thinks
21021 that this is a declaration. At this point, we just want to force
21022 declaration die. */
21023 save_fn = current_function_decl;
21024 current_function_decl = NULL_TREE;
21025 gen_subprogram_die (decl, context_die);
21026 current_function_decl = save_fn;
21027 break;
21029 case VAR_DECL:
21030 /* Set external flag to force declaration die. Restore it after
21031 gen_decl_die() call. */
21032 saved_external_flag = DECL_EXTERNAL (decl);
21033 DECL_EXTERNAL (decl) = 1;
21034 gen_decl_die (decl, NULL, context_die);
21035 DECL_EXTERNAL (decl) = saved_external_flag;
21036 break;
21038 case NAMESPACE_DECL:
21039 if (dwarf_version >= 3 || !dwarf_strict)
21040 dwarf2out_decl (decl);
21041 else
21042 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
21043 decl_die = comp_unit_die ();
21044 break;
21046 case TRANSLATION_UNIT_DECL:
21047 decl_die = comp_unit_die ();
21048 break;
21050 default:
21051 gcc_unreachable ();
21054 /* We should be able to find the DIE now. */
21055 if (!decl_die)
21056 decl_die = lookup_decl_die (decl);
21057 gcc_assert (decl_die);
21060 return decl_die;
21063 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
21064 always returned. */
21066 static dw_die_ref
21067 force_type_die (tree type)
21069 dw_die_ref type_die;
21071 type_die = lookup_type_die (type);
21072 if (!type_die)
21074 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21076 type_die = modified_type_die (type, TYPE_READONLY (type),
21077 TYPE_VOLATILE (type), context_die);
21078 gcc_assert (type_die);
21080 return type_die;
21083 /* Force out any required namespaces to be able to output DECL,
21084 and return the new context_die for it, if it's changed. */
21086 static dw_die_ref
21087 setup_namespace_context (tree thing, dw_die_ref context_die)
21089 tree context = (DECL_P (thing)
21090 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21091 if (context && TREE_CODE (context) == NAMESPACE_DECL)
21092 /* Force out the namespace. */
21093 context_die = force_decl_die (context);
21095 return context_die;
21098 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21099 type) within its namespace, if appropriate.
21101 For compatibility with older debuggers, namespace DIEs only contain
21102 declarations; all definitions are emitted at CU scope. */
21104 static dw_die_ref
21105 declare_in_namespace (tree thing, dw_die_ref context_die)
21107 dw_die_ref ns_context;
21109 if (debug_info_level <= DINFO_LEVEL_TERSE)
21110 return context_die;
21112 /* If this decl is from an inlined function, then don't try to emit it in its
21113 namespace, as we will get confused. It would have already been emitted
21114 when the abstract instance of the inline function was emitted anyways. */
21115 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21116 return context_die;
21118 ns_context = setup_namespace_context (thing, context_die);
21120 if (ns_context != context_die)
21122 if (is_fortran ())
21123 return ns_context;
21124 if (DECL_P (thing))
21125 gen_decl_die (thing, NULL, ns_context);
21126 else
21127 gen_type_die (thing, ns_context);
21129 return context_die;
21132 /* Generate a DIE for a namespace or namespace alias. */
21134 static void
21135 gen_namespace_die (tree decl, dw_die_ref context_die)
21137 dw_die_ref namespace_die;
21139 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21140 they are an alias of. */
21141 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21143 /* Output a real namespace or module. */
21144 context_die = setup_namespace_context (decl, comp_unit_die ());
21145 namespace_die = new_die (is_fortran ()
21146 ? DW_TAG_module : DW_TAG_namespace,
21147 context_die, decl);
21148 /* For Fortran modules defined in different CU don't add src coords. */
21149 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21151 const char *name = dwarf2_name (decl, 0);
21152 if (name)
21153 add_name_attribute (namespace_die, name);
21155 else
21156 add_name_and_src_coords_attributes (namespace_die, decl);
21157 if (DECL_EXTERNAL (decl))
21158 add_AT_flag (namespace_die, DW_AT_declaration, 1);
21159 equate_decl_number_to_die (decl, namespace_die);
21161 else
21163 /* Output a namespace alias. */
21165 /* Force out the namespace we are an alias of, if necessary. */
21166 dw_die_ref origin_die
21167 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21169 if (DECL_FILE_SCOPE_P (decl)
21170 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21171 context_die = setup_namespace_context (decl, comp_unit_die ());
21172 /* Now create the namespace alias DIE. */
21173 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21174 add_name_and_src_coords_attributes (namespace_die, decl);
21175 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21176 equate_decl_number_to_die (decl, namespace_die);
21180 /* Generate Dwarf debug information for a decl described by DECL.
21181 The return value is currently only meaningful for PARM_DECLs,
21182 for all other decls it returns NULL. */
21184 static dw_die_ref
21185 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21187 tree decl_or_origin = decl ? decl : origin;
21188 tree class_origin = NULL, ultimate_origin;
21190 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21191 return NULL;
21193 switch (TREE_CODE (decl_or_origin))
21195 case ERROR_MARK:
21196 break;
21198 case CONST_DECL:
21199 if (!is_fortran () && !is_ada ())
21201 /* The individual enumerators of an enum type get output when we output
21202 the Dwarf representation of the relevant enum type itself. */
21203 break;
21206 /* Emit its type. */
21207 gen_type_die (TREE_TYPE (decl), context_die);
21209 /* And its containing namespace. */
21210 context_die = declare_in_namespace (decl, context_die);
21212 gen_const_die (decl, context_die);
21213 break;
21215 case FUNCTION_DECL:
21216 /* Don't output any DIEs to represent mere function declarations,
21217 unless they are class members or explicit block externs. */
21218 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21219 && DECL_FILE_SCOPE_P (decl_or_origin)
21220 && (current_function_decl == NULL_TREE
21221 || DECL_ARTIFICIAL (decl_or_origin)))
21222 break;
21224 #if 0
21225 /* FIXME */
21226 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21227 on local redeclarations of global functions. That seems broken. */
21228 if (current_function_decl != decl)
21229 /* This is only a declaration. */;
21230 #endif
21232 /* If we're emitting a clone, emit info for the abstract instance. */
21233 if (origin || DECL_ORIGIN (decl) != decl)
21234 dwarf2out_abstract_function (origin
21235 ? DECL_ORIGIN (origin)
21236 : DECL_ABSTRACT_ORIGIN (decl));
21238 /* If we're emitting an out-of-line copy of an inline function,
21239 emit info for the abstract instance and set up to refer to it. */
21240 else if (cgraph_function_possibly_inlined_p (decl)
21241 && ! DECL_ABSTRACT (decl)
21242 && ! class_or_namespace_scope_p (context_die)
21243 /* dwarf2out_abstract_function won't emit a die if this is just
21244 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
21245 that case, because that works only if we have a die. */
21246 && DECL_INITIAL (decl) != NULL_TREE)
21248 dwarf2out_abstract_function (decl);
21249 set_decl_origin_self (decl);
21252 /* Otherwise we're emitting the primary DIE for this decl. */
21253 else if (debug_info_level > DINFO_LEVEL_TERSE)
21255 /* Before we describe the FUNCTION_DECL itself, make sure that we
21256 have its containing type. */
21257 if (!origin)
21258 origin = decl_class_context (decl);
21259 if (origin != NULL_TREE)
21260 gen_type_die (origin, context_die);
21262 /* And its return type. */
21263 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21265 /* And its virtual context. */
21266 if (DECL_VINDEX (decl) != NULL_TREE)
21267 gen_type_die (DECL_CONTEXT (decl), context_die);
21269 /* Make sure we have a member DIE for decl. */
21270 if (origin != NULL_TREE)
21271 gen_type_die_for_member (origin, decl, context_die);
21273 /* And its containing namespace. */
21274 context_die = declare_in_namespace (decl, context_die);
21277 /* Now output a DIE to represent the function itself. */
21278 if (decl)
21279 gen_subprogram_die (decl, context_die);
21280 break;
21282 case TYPE_DECL:
21283 /* If we are in terse mode, don't generate any DIEs to represent any
21284 actual typedefs. */
21285 if (debug_info_level <= DINFO_LEVEL_TERSE)
21286 break;
21288 /* In the special case of a TYPE_DECL node representing the declaration
21289 of some type tag, if the given TYPE_DECL is marked as having been
21290 instantiated from some other (original) TYPE_DECL node (e.g. one which
21291 was generated within the original definition of an inline function) we
21292 used to generate a special (abbreviated) DW_TAG_structure_type,
21293 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21294 should be actually referencing those DIEs, as variable DIEs with that
21295 type would be emitted already in the abstract origin, so it was always
21296 removed during unused type prunning. Don't add anything in this
21297 case. */
21298 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21299 break;
21301 if (is_redundant_typedef (decl))
21302 gen_type_die (TREE_TYPE (decl), context_die);
21303 else
21304 /* Output a DIE to represent the typedef itself. */
21305 gen_typedef_die (decl, context_die);
21306 break;
21308 case LABEL_DECL:
21309 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21310 gen_label_die (decl, context_die);
21311 break;
21313 case VAR_DECL:
21314 case RESULT_DECL:
21315 /* If we are in terse mode, don't generate any DIEs to represent any
21316 variable declarations or definitions. */
21317 if (debug_info_level <= DINFO_LEVEL_TERSE)
21318 break;
21320 /* Output any DIEs that are needed to specify the type of this data
21321 object. */
21322 if (decl_by_reference_p (decl_or_origin))
21323 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21324 else
21325 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21327 /* And its containing type. */
21328 class_origin = decl_class_context (decl_or_origin);
21329 if (class_origin != NULL_TREE)
21330 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21332 /* And its containing namespace. */
21333 context_die = declare_in_namespace (decl_or_origin, context_die);
21335 /* Now output the DIE to represent the data object itself. This gets
21336 complicated because of the possibility that the VAR_DECL really
21337 represents an inlined instance of a formal parameter for an inline
21338 function. */
21339 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21340 if (ultimate_origin != NULL_TREE
21341 && TREE_CODE (ultimate_origin) == PARM_DECL)
21342 gen_formal_parameter_die (decl, origin,
21343 true /* Emit name attribute. */,
21344 context_die);
21345 else
21346 gen_variable_die (decl, origin, context_die);
21347 break;
21349 case FIELD_DECL:
21350 /* Ignore the nameless fields that are used to skip bits but handle C++
21351 anonymous unions and structs. */
21352 if (DECL_NAME (decl) != NULL_TREE
21353 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21354 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21356 gen_type_die (member_declared_type (decl), context_die);
21357 gen_field_die (decl, context_die);
21359 break;
21361 case PARM_DECL:
21362 if (DECL_BY_REFERENCE (decl_or_origin))
21363 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21364 else
21365 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21366 return gen_formal_parameter_die (decl, origin,
21367 true /* Emit name attribute. */,
21368 context_die);
21370 case NAMESPACE_DECL:
21371 case IMPORTED_DECL:
21372 if (dwarf_version >= 3 || !dwarf_strict)
21373 gen_namespace_die (decl, context_die);
21374 break;
21376 default:
21377 /* Probably some frontend-internal decl. Assume we don't care. */
21378 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21379 break;
21382 return NULL;
21385 /* Output debug information for global decl DECL. Called from toplev.c after
21386 compilation proper has finished. */
21388 static void
21389 dwarf2out_global_decl (tree decl)
21391 /* Output DWARF2 information for file-scope tentative data object
21392 declarations, file-scope (extern) function declarations (which
21393 had no corresponding body) and file-scope tagged type declarations
21394 and definitions which have not yet been forced out. */
21395 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21396 dwarf2out_decl (decl);
21399 /* Output debug information for type decl DECL. Called from toplev.c
21400 and from language front ends (to record built-in types). */
21401 static void
21402 dwarf2out_type_decl (tree decl, int local)
21404 if (!local)
21405 dwarf2out_decl (decl);
21408 /* Output debug information for imported module or decl DECL.
21409 NAME is non-NULL name in the lexical block if the decl has been renamed.
21410 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21411 that DECL belongs to.
21412 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21413 static void
21414 dwarf2out_imported_module_or_decl_1 (tree decl,
21415 tree name,
21416 tree lexical_block,
21417 dw_die_ref lexical_block_die)
21419 expanded_location xloc;
21420 dw_die_ref imported_die = NULL;
21421 dw_die_ref at_import_die;
21423 if (TREE_CODE (decl) == IMPORTED_DECL)
21425 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21426 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21427 gcc_assert (decl);
21429 else
21430 xloc = expand_location (input_location);
21432 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21434 at_import_die = force_type_die (TREE_TYPE (decl));
21435 /* For namespace N { typedef void T; } using N::T; base_type_die
21436 returns NULL, but DW_TAG_imported_declaration requires
21437 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21438 if (!at_import_die)
21440 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21441 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21442 at_import_die = lookup_type_die (TREE_TYPE (decl));
21443 gcc_assert (at_import_die);
21446 else
21448 at_import_die = lookup_decl_die (decl);
21449 if (!at_import_die)
21451 /* If we're trying to avoid duplicate debug info, we may not have
21452 emitted the member decl for this field. Emit it now. */
21453 if (TREE_CODE (decl) == FIELD_DECL)
21455 tree type = DECL_CONTEXT (decl);
21457 if (TYPE_CONTEXT (type)
21458 && TYPE_P (TYPE_CONTEXT (type))
21459 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21460 DINFO_USAGE_DIR_USE))
21461 return;
21462 gen_type_die_for_member (type, decl,
21463 get_context_die (TYPE_CONTEXT (type)));
21465 at_import_die = force_decl_die (decl);
21469 if (TREE_CODE (decl) == NAMESPACE_DECL)
21471 if (dwarf_version >= 3 || !dwarf_strict)
21472 imported_die = new_die (DW_TAG_imported_module,
21473 lexical_block_die,
21474 lexical_block);
21475 else
21476 return;
21478 else
21479 imported_die = new_die (DW_TAG_imported_declaration,
21480 lexical_block_die,
21481 lexical_block);
21483 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21484 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21485 if (name)
21486 add_AT_string (imported_die, DW_AT_name,
21487 IDENTIFIER_POINTER (name));
21488 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21491 /* Output debug information for imported module or decl DECL.
21492 NAME is non-NULL name in context if the decl has been renamed.
21493 CHILD is true if decl is one of the renamed decls as part of
21494 importing whole module. */
21496 static void
21497 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21498 bool child)
21500 /* dw_die_ref at_import_die; */
21501 dw_die_ref scope_die;
21503 if (debug_info_level <= DINFO_LEVEL_TERSE)
21504 return;
21506 gcc_assert (decl);
21508 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21509 We need decl DIE for reference and scope die. First, get DIE for the decl
21510 itself. */
21512 /* Get the scope die for decl context. Use comp_unit_die for global module
21513 or decl. If die is not found for non globals, force new die. */
21514 if (context
21515 && TYPE_P (context)
21516 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21517 return;
21519 if (!(dwarf_version >= 3 || !dwarf_strict))
21520 return;
21522 scope_die = get_context_die (context);
21524 if (child)
21526 gcc_assert (scope_die->die_child);
21527 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21528 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21529 scope_die = scope_die->die_child;
21532 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21533 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21537 /* Write the debugging output for DECL. */
21539 void
21540 dwarf2out_decl (tree decl)
21542 dw_die_ref context_die = comp_unit_die ();
21544 switch (TREE_CODE (decl))
21546 case ERROR_MARK:
21547 return;
21549 case FUNCTION_DECL:
21550 /* What we would really like to do here is to filter out all mere
21551 file-scope declarations of file-scope functions which are never
21552 referenced later within this translation unit (and keep all of ones
21553 that *are* referenced later on) but we aren't clairvoyant, so we have
21554 no idea which functions will be referenced in the future (i.e. later
21555 on within the current translation unit). So here we just ignore all
21556 file-scope function declarations which are not also definitions. If
21557 and when the debugger needs to know something about these functions,
21558 it will have to hunt around and find the DWARF information associated
21559 with the definition of the function.
21561 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21562 nodes represent definitions and which ones represent mere
21563 declarations. We have to check DECL_INITIAL instead. That's because
21564 the C front-end supports some weird semantics for "extern inline"
21565 function definitions. These can get inlined within the current
21566 translation unit (and thus, we need to generate Dwarf info for their
21567 abstract instances so that the Dwarf info for the concrete inlined
21568 instances can have something to refer to) but the compiler never
21569 generates any out-of-lines instances of such things (despite the fact
21570 that they *are* definitions).
21572 The important point is that the C front-end marks these "extern
21573 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21574 them anyway. Note that the C++ front-end also plays some similar games
21575 for inline function definitions appearing within include files which
21576 also contain `#pragma interface' pragmas. */
21577 if (DECL_INITIAL (decl) == NULL_TREE)
21578 return;
21580 /* If we're a nested function, initially use a parent of NULL; if we're
21581 a plain function, this will be fixed up in decls_for_scope. If
21582 we're a method, it will be ignored, since we already have a DIE. */
21583 if (decl_function_context (decl)
21584 /* But if we're in terse mode, we don't care about scope. */
21585 && debug_info_level > DINFO_LEVEL_TERSE)
21586 context_die = NULL;
21587 break;
21589 case VAR_DECL:
21590 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21591 declaration and if the declaration was never even referenced from
21592 within this entire compilation unit. We suppress these DIEs in
21593 order to save space in the .debug section (by eliminating entries
21594 which are probably useless). Note that we must not suppress
21595 block-local extern declarations (whether used or not) because that
21596 would screw-up the debugger's name lookup mechanism and cause it to
21597 miss things which really ought to be in scope at a given point. */
21598 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21599 return;
21601 /* For local statics lookup proper context die. */
21602 if (TREE_STATIC (decl) && decl_function_context (decl))
21603 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21605 /* If we are in terse mode, don't generate any DIEs to represent any
21606 variable declarations or definitions. */
21607 if (debug_info_level <= DINFO_LEVEL_TERSE)
21608 return;
21609 break;
21611 case CONST_DECL:
21612 if (debug_info_level <= DINFO_LEVEL_TERSE)
21613 return;
21614 if (!is_fortran () && !is_ada ())
21615 return;
21616 if (TREE_STATIC (decl) && decl_function_context (decl))
21617 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21618 break;
21620 case NAMESPACE_DECL:
21621 case IMPORTED_DECL:
21622 if (debug_info_level <= DINFO_LEVEL_TERSE)
21623 return;
21624 if (lookup_decl_die (decl) != NULL)
21625 return;
21626 break;
21628 case TYPE_DECL:
21629 /* Don't emit stubs for types unless they are needed by other DIEs. */
21630 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21631 return;
21633 /* Don't bother trying to generate any DIEs to represent any of the
21634 normal built-in types for the language we are compiling. */
21635 if (DECL_IS_BUILTIN (decl))
21636 return;
21638 /* If we are in terse mode, don't generate any DIEs for types. */
21639 if (debug_info_level <= DINFO_LEVEL_TERSE)
21640 return;
21642 /* If we're a function-scope tag, initially use a parent of NULL;
21643 this will be fixed up in decls_for_scope. */
21644 if (decl_function_context (decl))
21645 context_die = NULL;
21647 break;
21649 default:
21650 return;
21653 gen_decl_die (decl, NULL, context_die);
21656 /* Write the debugging output for DECL. */
21658 static void
21659 dwarf2out_function_decl (tree decl)
21661 dwarf2out_decl (decl);
21662 call_arg_locations = NULL;
21663 call_arg_loc_last = NULL;
21664 call_site_count = -1;
21665 tail_call_site_count = -1;
21666 VEC_free (dw_die_ref, heap, block_map);
21667 htab_empty (decl_loc_table);
21668 htab_empty (cached_dw_loc_list_table);
21671 /* Output a marker (i.e. a label) for the beginning of the generated code for
21672 a lexical block. */
21674 static void
21675 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21676 unsigned int blocknum)
21678 switch_to_section (current_function_section ());
21679 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21682 /* Output a marker (i.e. a label) for the end of the generated code for a
21683 lexical block. */
21685 static void
21686 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21688 switch_to_section (current_function_section ());
21689 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21692 /* Returns nonzero if it is appropriate not to emit any debugging
21693 information for BLOCK, because it doesn't contain any instructions.
21695 Don't allow this for blocks with nested functions or local classes
21696 as we would end up with orphans, and in the presence of scheduling
21697 we may end up calling them anyway. */
21699 static bool
21700 dwarf2out_ignore_block (const_tree block)
21702 tree decl;
21703 unsigned int i;
21705 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21706 if (TREE_CODE (decl) == FUNCTION_DECL
21707 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21708 return 0;
21709 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21711 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21712 if (TREE_CODE (decl) == FUNCTION_DECL
21713 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21714 return 0;
21717 return 1;
21720 /* Hash table routines for file_hash. */
21722 static int
21723 file_table_eq (const void *p1_p, const void *p2_p)
21725 const struct dwarf_file_data *const p1 =
21726 (const struct dwarf_file_data *) p1_p;
21727 const char *const p2 = (const char *) p2_p;
21728 return filename_cmp (p1->filename, p2) == 0;
21731 static hashval_t
21732 file_table_hash (const void *p_p)
21734 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21735 return htab_hash_string (p->filename);
21738 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21739 dwarf2out.c) and return its "index". The index of each (known) filename is
21740 just a unique number which is associated with only that one filename. We
21741 need such numbers for the sake of generating labels (in the .debug_sfnames
21742 section) and references to those files numbers (in the .debug_srcinfo
21743 and.debug_macinfo sections). If the filename given as an argument is not
21744 found in our current list, add it to the list and assign it the next
21745 available unique index number. In order to speed up searches, we remember
21746 the index of the filename was looked up last. This handles the majority of
21747 all searches. */
21749 static struct dwarf_file_data *
21750 lookup_filename (const char *file_name)
21752 void ** slot;
21753 struct dwarf_file_data * created;
21755 /* Check to see if the file name that was searched on the previous
21756 call matches this file name. If so, return the index. */
21757 if (file_table_last_lookup
21758 && (file_name == file_table_last_lookup->filename
21759 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21760 return file_table_last_lookup;
21762 /* Didn't match the previous lookup, search the table. */
21763 slot = htab_find_slot_with_hash (file_table, file_name,
21764 htab_hash_string (file_name), INSERT);
21765 if (*slot)
21766 return (struct dwarf_file_data *) *slot;
21768 created = ggc_alloc_dwarf_file_data ();
21769 created->filename = file_name;
21770 created->emitted_number = 0;
21771 *slot = created;
21772 return created;
21775 /* If the assembler will construct the file table, then translate the compiler
21776 internal file table number into the assembler file table number, and emit
21777 a .file directive if we haven't already emitted one yet. The file table
21778 numbers are different because we prune debug info for unused variables and
21779 types, which may include filenames. */
21781 static int
21782 maybe_emit_file (struct dwarf_file_data * fd)
21784 if (! fd->emitted_number)
21786 if (last_emitted_file)
21787 fd->emitted_number = last_emitted_file->emitted_number + 1;
21788 else
21789 fd->emitted_number = 1;
21790 last_emitted_file = fd;
21792 if (DWARF2_ASM_LINE_DEBUG_INFO)
21794 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21795 output_quoted_string (asm_out_file,
21796 remap_debug_filename (fd->filename));
21797 fputc ('\n', asm_out_file);
21801 return fd->emitted_number;
21804 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21805 That generation should happen after function debug info has been
21806 generated. The value of the attribute is the constant value of ARG. */
21808 static void
21809 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21811 die_arg_entry entry;
21813 if (!die || !arg)
21814 return;
21816 if (!tmpl_value_parm_die_table)
21817 tmpl_value_parm_die_table
21818 = VEC_alloc (die_arg_entry, gc, 32);
21820 entry.die = die;
21821 entry.arg = arg;
21822 VEC_safe_push (die_arg_entry, gc,
21823 tmpl_value_parm_die_table,
21824 &entry);
21827 /* Return TRUE if T is an instance of generic type, FALSE
21828 otherwise. */
21830 static bool
21831 generic_type_p (tree t)
21833 if (t == NULL_TREE || !TYPE_P (t))
21834 return false;
21835 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21838 /* Schedule the generation of the generic parameter dies for the
21839 instance of generic type T. The proper generation itself is later
21840 done by gen_scheduled_generic_parms_dies. */
21842 static void
21843 schedule_generic_params_dies_gen (tree t)
21845 if (!generic_type_p (t))
21846 return;
21848 if (generic_type_instances == NULL)
21849 generic_type_instances = VEC_alloc (tree, gc, 256);
21851 VEC_safe_push (tree, gc, generic_type_instances, t);
21854 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21855 by append_entry_to_tmpl_value_parm_die_table. This function must
21856 be called after function DIEs have been generated. */
21858 static void
21859 gen_remaining_tmpl_value_param_die_attribute (void)
21861 if (tmpl_value_parm_die_table)
21863 unsigned i;
21864 die_arg_entry *e;
21866 FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21867 tree_add_const_value_attribute (e->die, e->arg);
21871 /* Generate generic parameters DIEs for instances of generic types
21872 that have been previously scheduled by
21873 schedule_generic_params_dies_gen. This function must be called
21874 after all the types of the CU have been laid out. */
21876 static void
21877 gen_scheduled_generic_parms_dies (void)
21879 unsigned i;
21880 tree t;
21882 if (generic_type_instances == NULL)
21883 return;
21885 FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
21886 gen_generic_params_dies (t);
21890 /* Replace DW_AT_name for the decl with name. */
21892 static void
21893 dwarf2out_set_name (tree decl, tree name)
21895 dw_die_ref die;
21896 dw_attr_ref attr;
21897 const char *dname;
21899 die = TYPE_SYMTAB_DIE (decl);
21900 if (!die)
21901 return;
21903 dname = dwarf2_name (name, 0);
21904 if (!dname)
21905 return;
21907 attr = get_AT (die, DW_AT_name);
21908 if (attr)
21910 struct indirect_string_node *node;
21912 node = find_AT_string (dname);
21913 /* replace the string. */
21914 attr->dw_attr_val.v.val_str = node;
21917 else
21918 add_name_attribute (die, dname);
21921 /* Called by the final INSN scan whenever we see a var location. We
21922 use it to drop labels in the right places, and throw the location in
21923 our lookup table. */
21925 static void
21926 dwarf2out_var_location (rtx loc_note)
21928 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21929 struct var_loc_node *newloc;
21930 rtx next_real;
21931 static const char *last_label;
21932 static const char *last_postcall_label;
21933 static bool last_in_cold_section_p;
21934 tree decl;
21935 bool var_loc_p;
21937 if (!NOTE_P (loc_note))
21939 if (CALL_P (loc_note))
21941 call_site_count++;
21942 if (SIBLING_CALL_P (loc_note))
21943 tail_call_site_count++;
21945 return;
21948 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21949 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21950 return;
21952 next_real = next_real_insn (loc_note);
21954 /* If there are no instructions which would be affected by this note,
21955 don't do anything. */
21956 if (var_loc_p
21957 && next_real == NULL_RTX
21958 && !NOTE_DURING_CALL_P (loc_note))
21959 return;
21961 if (next_real == NULL_RTX)
21962 next_real = get_last_insn ();
21964 /* If there were any real insns between note we processed last time
21965 and this note (or if it is the first note), clear
21966 last_{,postcall_}label so that they are not reused this time. */
21967 if (last_var_location_insn == NULL_RTX
21968 || last_var_location_insn != next_real
21969 || last_in_cold_section_p != in_cold_section_p)
21971 last_label = NULL;
21972 last_postcall_label = NULL;
21975 if (var_loc_p)
21977 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21978 newloc = add_var_loc_to_decl (decl, loc_note,
21979 NOTE_DURING_CALL_P (loc_note)
21980 ? last_postcall_label : last_label);
21981 if (newloc == NULL)
21982 return;
21984 else
21986 decl = NULL_TREE;
21987 newloc = NULL;
21990 /* If there were no real insns between note we processed last time
21991 and this note, use the label we emitted last time. Otherwise
21992 create a new label and emit it. */
21993 if (last_label == NULL)
21995 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21996 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21997 loclabel_num++;
21998 last_label = ggc_strdup (loclabel);
22001 if (!var_loc_p)
22003 struct call_arg_loc_node *ca_loc
22004 = ggc_alloc_cleared_call_arg_loc_node ();
22005 rtx prev = prev_real_insn (loc_note), x;
22006 ca_loc->call_arg_loc_note = loc_note;
22007 ca_loc->next = NULL;
22008 ca_loc->label = last_label;
22009 gcc_assert (prev
22010 && (CALL_P (prev)
22011 || (NONJUMP_INSN_P (prev)
22012 && GET_CODE (PATTERN (prev)) == SEQUENCE
22013 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22014 if (!CALL_P (prev))
22015 prev = XVECEXP (PATTERN (prev), 0, 0);
22016 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22017 x = PATTERN (prev);
22018 if (GET_CODE (x) == PARALLEL)
22019 x = XVECEXP (x, 0, 0);
22020 if (GET_CODE (x) == SET)
22021 x = SET_SRC (x);
22022 if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
22024 x = XEXP (XEXP (x, 0), 0);
22025 if (GET_CODE (x) == SYMBOL_REF
22026 && SYMBOL_REF_DECL (x)
22027 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22028 ca_loc->symbol_ref = x;
22030 ca_loc->block = insn_scope (prev);
22031 if (call_arg_locations)
22032 call_arg_loc_last->next = ca_loc;
22033 else
22034 call_arg_locations = ca_loc;
22035 call_arg_loc_last = ca_loc;
22037 else if (!NOTE_DURING_CALL_P (loc_note))
22038 newloc->label = last_label;
22039 else
22041 if (!last_postcall_label)
22043 sprintf (loclabel, "%s-1", last_label);
22044 last_postcall_label = ggc_strdup (loclabel);
22046 newloc->label = last_postcall_label;
22049 last_var_location_insn = next_real;
22050 last_in_cold_section_p = in_cold_section_p;
22053 /* Note in one location list that text section has changed. */
22055 static int
22056 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
22058 var_loc_list *list = (var_loc_list *) *slot;
22059 if (list->first)
22060 list->last_before_switch
22061 = list->last->next ? list->last->next : list->last;
22062 return 1;
22065 /* Note in all location lists that text section has changed. */
22067 static void
22068 var_location_switch_text_section (void)
22070 if (decl_loc_table == NULL)
22071 return;
22073 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
22076 /* Create a new line number table. */
22078 static dw_line_info_table *
22079 new_line_info_table (void)
22081 dw_line_info_table *table;
22083 table = ggc_alloc_cleared_dw_line_info_table_struct ();
22084 table->file_num = 1;
22085 table->line_num = 1;
22086 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22088 return table;
22091 /* Lookup the "current" table into which we emit line info, so
22092 that we don't have to do it for every source line. */
22094 static void
22095 set_cur_line_info_table (section *sec)
22097 dw_line_info_table *table;
22099 if (sec == text_section)
22100 table = text_section_line_info;
22101 else if (sec == cold_text_section)
22103 table = cold_text_section_line_info;
22104 if (!table)
22106 cold_text_section_line_info = table = new_line_info_table ();
22107 table->end_label = cold_end_label;
22110 else
22112 const char *end_label;
22114 if (flag_reorder_blocks_and_partition)
22116 if (in_cold_section_p)
22117 end_label = crtl->subsections.cold_section_end_label;
22118 else
22119 end_label = crtl->subsections.hot_section_end_label;
22121 else
22123 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22124 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22125 current_function_funcdef_no);
22126 end_label = ggc_strdup (label);
22129 table = new_line_info_table ();
22130 table->end_label = end_label;
22132 VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
22135 cur_line_info_table = table;
22139 /* We need to reset the locations at the beginning of each
22140 function. We can't do this in the end_function hook, because the
22141 declarations that use the locations won't have been output when
22142 that hook is called. Also compute have_multiple_function_sections here. */
22144 static void
22145 dwarf2out_begin_function (tree fun)
22147 section *sec = function_section (fun);
22149 if (sec != text_section)
22150 have_multiple_function_sections = true;
22152 if (flag_reorder_blocks_and_partition && !cold_text_section)
22154 gcc_assert (current_function_decl == fun);
22155 cold_text_section = unlikely_text_section ();
22156 switch_to_section (cold_text_section);
22157 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22158 switch_to_section (sec);
22161 dwarf2out_note_section_used ();
22162 call_site_count = 0;
22163 tail_call_site_count = 0;
22165 set_cur_line_info_table (sec);
22168 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
22170 static void
22171 push_dw_line_info_entry (dw_line_info_table *table,
22172 enum dw_line_info_opcode opcode, unsigned int val)
22174 dw_line_info_entry e;
22175 e.opcode = opcode;
22176 e.val = val;
22177 VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
22180 /* Output a label to mark the beginning of a source code line entry
22181 and record information relating to this source line, in
22182 'line_info_table' for later output of the .debug_line section. */
22183 /* ??? The discriminator parameter ought to be unsigned. */
22185 static void
22186 dwarf2out_source_line (unsigned int line, const char *filename,
22187 int discriminator, bool is_stmt)
22189 unsigned int file_num;
22190 dw_line_info_table *table;
22192 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
22193 return;
22195 /* The discriminator column was added in dwarf4. Simplify the below
22196 by simply removing it if we're not supposed to output it. */
22197 if (dwarf_version < 4 && dwarf_strict)
22198 discriminator = 0;
22200 table = cur_line_info_table;
22201 file_num = maybe_emit_file (lookup_filename (filename));
22203 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22204 the debugger has used the second (possibly duplicate) line number
22205 at the beginning of the function to mark the end of the prologue.
22206 We could eliminate any other duplicates within the function. For
22207 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22208 that second line number entry. */
22209 /* Recall that this end-of-prologue indication is *not* the same thing
22210 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22211 to which the hook corresponds, follows the last insn that was
22212 emitted by gen_prologue. What we need is to preceed the first insn
22213 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22214 insn that corresponds to something the user wrote. These may be
22215 very different locations once scheduling is enabled. */
22217 if (0 && file_num == table->file_num
22218 && line == table->line_num
22219 && discriminator == table->discrim_num
22220 && is_stmt == table->is_stmt)
22221 return;
22223 switch_to_section (current_function_section ());
22225 /* If requested, emit something human-readable. */
22226 if (flag_debug_asm)
22227 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22229 if (DWARF2_ASM_LINE_DEBUG_INFO)
22231 /* Emit the .loc directive understood by GNU as. */
22232 fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
22233 if (is_stmt != table->is_stmt)
22234 fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
22235 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22236 fprintf (asm_out_file, " discriminator %d", discriminator);
22237 fputc ('\n', asm_out_file);
22239 else
22241 unsigned int label_num = ++line_info_label_num;
22243 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22245 push_dw_line_info_entry (table, LI_set_address, label_num);
22246 if (file_num != table->file_num)
22247 push_dw_line_info_entry (table, LI_set_file, file_num);
22248 if (discriminator != table->discrim_num)
22249 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22250 if (is_stmt != table->is_stmt)
22251 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22252 push_dw_line_info_entry (table, LI_set_line, line);
22255 table->file_num = file_num;
22256 table->line_num = line;
22257 table->discrim_num = discriminator;
22258 table->is_stmt = is_stmt;
22259 table->in_use = true;
22262 /* Record the beginning of a new source file. */
22264 static void
22265 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22267 if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22269 /* Record the beginning of the file for break_out_includes. */
22270 dw_die_ref bincl_die;
22272 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22273 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22276 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22278 macinfo_entry e;
22279 e.code = DW_MACINFO_start_file;
22280 e.lineno = lineno;
22281 e.info = xstrdup (filename);
22282 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22286 /* Record the end of a source file. */
22288 static void
22289 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22291 if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22292 /* Record the end of the file for break_out_includes. */
22293 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22295 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22297 macinfo_entry e;
22298 e.code = DW_MACINFO_end_file;
22299 e.lineno = lineno;
22300 e.info = NULL;
22301 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22305 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22306 the tail part of the directive line, i.e. the part which is past the
22307 initial whitespace, #, whitespace, directive-name, whitespace part. */
22309 static void
22310 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22311 const char *buffer ATTRIBUTE_UNUSED)
22313 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22315 macinfo_entry e;
22316 e.code = DW_MACINFO_define;
22317 e.lineno = lineno;
22318 e.info = xstrdup (buffer);;
22319 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22323 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22324 the tail part of the directive line, i.e. the part which is past the
22325 initial whitespace, #, whitespace, directive-name, whitespace part. */
22327 static void
22328 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22329 const char *buffer ATTRIBUTE_UNUSED)
22331 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22333 macinfo_entry e;
22334 e.code = DW_MACINFO_undef;
22335 e.lineno = lineno;
22336 e.info = xstrdup (buffer);;
22337 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22341 static void
22342 output_macinfo (void)
22344 unsigned i;
22345 unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22346 macinfo_entry *ref;
22348 if (! length)
22349 return;
22351 for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22353 switch (ref->code)
22355 case DW_MACINFO_start_file:
22357 int file_num = maybe_emit_file (lookup_filename (ref->info));
22358 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22359 dw2_asm_output_data_uleb128
22360 (ref->lineno, "Included from line number %lu",
22361 (unsigned long)ref->lineno);
22362 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22364 break;
22365 case DW_MACINFO_end_file:
22366 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22367 break;
22368 case DW_MACINFO_define:
22369 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22370 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22371 (unsigned long)ref->lineno);
22372 dw2_asm_output_nstring (ref->info, -1, "The macro");
22373 break;
22374 case DW_MACINFO_undef:
22375 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22376 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22377 (unsigned long)ref->lineno);
22378 dw2_asm_output_nstring (ref->info, -1, "The macro");
22379 break;
22380 default:
22381 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22382 ASM_COMMENT_START, (unsigned long)ref->code);
22383 break;
22388 /* Set up for Dwarf output at the start of compilation. */
22390 static void
22391 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22393 /* Allocate the file_table. */
22394 file_table = htab_create_ggc (50, file_table_hash,
22395 file_table_eq, NULL);
22397 /* Allocate the decl_die_table. */
22398 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22399 decl_die_table_eq, NULL);
22401 /* Allocate the decl_loc_table. */
22402 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22403 decl_loc_table_eq, NULL);
22405 /* Allocate the cached_dw_loc_list_table. */
22406 cached_dw_loc_list_table
22407 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22408 cached_dw_loc_list_table_eq, NULL);
22410 /* Allocate the initial hunk of the decl_scope_table. */
22411 decl_scope_table = VEC_alloc (tree, gc, 256);
22413 /* Allocate the initial hunk of the abbrev_die_table. */
22414 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22415 (ABBREV_DIE_TABLE_INCREMENT);
22416 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22417 /* Zero-th entry is allocated, but unused. */
22418 abbrev_die_table_in_use = 1;
22420 /* Allocate the pubtypes and pubnames vectors. */
22421 pubname_table = VEC_alloc (pubname_entry, gc, 32);
22422 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
22424 incomplete_types = VEC_alloc (tree, gc, 64);
22426 used_rtx_array = VEC_alloc (rtx, gc, 32);
22428 debug_info_section = get_section (DEBUG_INFO_SECTION,
22429 SECTION_DEBUG, NULL);
22430 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22431 SECTION_DEBUG, NULL);
22432 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22433 SECTION_DEBUG, NULL);
22434 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
22435 SECTION_DEBUG, NULL);
22436 debug_line_section = get_section (DEBUG_LINE_SECTION,
22437 SECTION_DEBUG, NULL);
22438 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22439 SECTION_DEBUG, NULL);
22440 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22441 SECTION_DEBUG, NULL);
22442 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22443 SECTION_DEBUG, NULL);
22444 debug_str_section = get_section (DEBUG_STR_SECTION,
22445 DEBUG_STR_SECTION_FLAGS, NULL);
22446 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22447 SECTION_DEBUG, NULL);
22448 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22449 SECTION_DEBUG, NULL);
22451 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22452 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22453 DEBUG_ABBREV_SECTION_LABEL, 0);
22454 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22455 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22456 COLD_TEXT_SECTION_LABEL, 0);
22457 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22459 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22460 DEBUG_INFO_SECTION_LABEL, 0);
22461 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22462 DEBUG_LINE_SECTION_LABEL, 0);
22463 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22464 DEBUG_RANGES_SECTION_LABEL, 0);
22465 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22466 DEBUG_MACINFO_SECTION_LABEL, 0);
22468 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22469 macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
22471 switch_to_section (text_section);
22472 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22474 /* Make sure the line number table for .text always exists. */
22475 text_section_line_info = new_line_info_table ();
22476 text_section_line_info->end_label = text_end_label;
22479 /* Called before cgraph_optimize starts outputtting functions, variables
22480 and toplevel asms into assembly. */
22482 static void
22483 dwarf2out_assembly_start (void)
22485 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22486 && dwarf2out_do_cfi_asm ()
22487 && (!(flag_unwind_tables || flag_exceptions)
22488 || targetm.except_unwind_info (&global_options) != UI_DWARF2))
22489 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22492 /* A helper function for dwarf2out_finish called through
22493 htab_traverse. Emit one queued .debug_str string. */
22495 static int
22496 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22498 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22500 if (node->label && node->refcount)
22502 switch_to_section (debug_str_section);
22503 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22504 assemble_string (node->str, strlen (node->str) + 1);
22507 return 1;
22510 #if ENABLE_ASSERT_CHECKING
22511 /* Verify that all marks are clear. */
22513 static void
22514 verify_marks_clear (dw_die_ref die)
22516 dw_die_ref c;
22518 gcc_assert (! die->die_mark);
22519 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22521 #endif /* ENABLE_ASSERT_CHECKING */
22523 /* Clear the marks for a die and its children.
22524 Be cool if the mark isn't set. */
22526 static void
22527 prune_unmark_dies (dw_die_ref die)
22529 dw_die_ref c;
22531 if (die->die_mark)
22532 die->die_mark = 0;
22533 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22536 /* Given DIE that we're marking as used, find any other dies
22537 it references as attributes and mark them as used. */
22539 static void
22540 prune_unused_types_walk_attribs (dw_die_ref die)
22542 dw_attr_ref a;
22543 unsigned ix;
22545 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22547 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22549 /* A reference to another DIE.
22550 Make sure that it will get emitted.
22551 If it was broken out into a comdat group, don't follow it. */
22552 if (! use_debug_types
22553 || a->dw_attr == DW_AT_specification
22554 || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22555 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22557 /* Set the string's refcount to 0 so that prune_unused_types_mark
22558 accounts properly for it. */
22559 if (AT_class (a) == dw_val_class_str)
22560 a->dw_attr_val.v.val_str->refcount = 0;
22564 /* Mark the generic parameters and arguments children DIEs of DIE. */
22566 static void
22567 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22569 dw_die_ref c;
22571 if (die == NULL || die->die_child == NULL)
22572 return;
22573 c = die->die_child;
22576 switch (c->die_tag)
22578 case DW_TAG_template_type_param:
22579 case DW_TAG_template_value_param:
22580 case DW_TAG_GNU_template_template_param:
22581 case DW_TAG_GNU_template_parameter_pack:
22582 prune_unused_types_mark (c, 1);
22583 break;
22584 default:
22585 break;
22587 c = c->die_sib;
22588 } while (c && c != die->die_child);
22591 /* Mark DIE as being used. If DOKIDS is true, then walk down
22592 to DIE's children. */
22594 static void
22595 prune_unused_types_mark (dw_die_ref die, int dokids)
22597 dw_die_ref c;
22599 if (die->die_mark == 0)
22601 /* We haven't done this node yet. Mark it as used. */
22602 die->die_mark = 1;
22603 /* If this is the DIE of a generic type instantiation,
22604 mark the children DIEs that describe its generic parms and
22605 args. */
22606 prune_unused_types_mark_generic_parms_dies (die);
22608 /* We also have to mark its parents as used.
22609 (But we don't want to mark our parents' kids due to this.) */
22610 if (die->die_parent)
22611 prune_unused_types_mark (die->die_parent, 0);
22613 /* Mark any referenced nodes. */
22614 prune_unused_types_walk_attribs (die);
22616 /* If this node is a specification,
22617 also mark the definition, if it exists. */
22618 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22619 prune_unused_types_mark (die->die_definition, 1);
22622 if (dokids && die->die_mark != 2)
22624 /* We need to walk the children, but haven't done so yet.
22625 Remember that we've walked the kids. */
22626 die->die_mark = 2;
22628 /* If this is an array type, we need to make sure our
22629 kids get marked, even if they're types. If we're
22630 breaking out types into comdat sections, do this
22631 for all type definitions. */
22632 if (die->die_tag == DW_TAG_array_type
22633 || (use_debug_types
22634 && is_type_die (die) && ! is_declaration_die (die)))
22635 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22636 else
22637 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22641 /* For local classes, look if any static member functions were emitted
22642 and if so, mark them. */
22644 static void
22645 prune_unused_types_walk_local_classes (dw_die_ref die)
22647 dw_die_ref c;
22649 if (die->die_mark == 2)
22650 return;
22652 switch (die->die_tag)
22654 case DW_TAG_structure_type:
22655 case DW_TAG_union_type:
22656 case DW_TAG_class_type:
22657 break;
22659 case DW_TAG_subprogram:
22660 if (!get_AT_flag (die, DW_AT_declaration)
22661 || die->die_definition != NULL)
22662 prune_unused_types_mark (die, 1);
22663 return;
22665 default:
22666 return;
22669 /* Mark children. */
22670 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22673 /* Walk the tree DIE and mark types that we actually use. */
22675 static void
22676 prune_unused_types_walk (dw_die_ref die)
22678 dw_die_ref c;
22680 /* Don't do anything if this node is already marked and
22681 children have been marked as well. */
22682 if (die->die_mark == 2)
22683 return;
22685 switch (die->die_tag)
22687 case DW_TAG_structure_type:
22688 case DW_TAG_union_type:
22689 case DW_TAG_class_type:
22690 if (die->die_perennial_p)
22691 break;
22693 for (c = die->die_parent; c; c = c->die_parent)
22694 if (c->die_tag == DW_TAG_subprogram)
22695 break;
22697 /* Finding used static member functions inside of classes
22698 is needed just for local classes, because for other classes
22699 static member function DIEs with DW_AT_specification
22700 are emitted outside of the DW_TAG_*_type. If we ever change
22701 it, we'd need to call this even for non-local classes. */
22702 if (c)
22703 prune_unused_types_walk_local_classes (die);
22705 /* It's a type node --- don't mark it. */
22706 return;
22708 case DW_TAG_const_type:
22709 case DW_TAG_packed_type:
22710 case DW_TAG_pointer_type:
22711 case DW_TAG_reference_type:
22712 case DW_TAG_rvalue_reference_type:
22713 case DW_TAG_volatile_type:
22714 case DW_TAG_typedef:
22715 case DW_TAG_array_type:
22716 case DW_TAG_interface_type:
22717 case DW_TAG_friend:
22718 case DW_TAG_variant_part:
22719 case DW_TAG_enumeration_type:
22720 case DW_TAG_subroutine_type:
22721 case DW_TAG_string_type:
22722 case DW_TAG_set_type:
22723 case DW_TAG_subrange_type:
22724 case DW_TAG_ptr_to_member_type:
22725 case DW_TAG_file_type:
22726 if (die->die_perennial_p)
22727 break;
22729 /* It's a type node --- don't mark it. */
22730 return;
22732 default:
22733 /* Mark everything else. */
22734 break;
22737 if (die->die_mark == 0)
22739 die->die_mark = 1;
22741 /* Now, mark any dies referenced from here. */
22742 prune_unused_types_walk_attribs (die);
22745 die->die_mark = 2;
22747 /* Mark children. */
22748 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22751 /* Increment the string counts on strings referred to from DIE's
22752 attributes. */
22754 static void
22755 prune_unused_types_update_strings (dw_die_ref die)
22757 dw_attr_ref a;
22758 unsigned ix;
22760 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22761 if (AT_class (a) == dw_val_class_str)
22763 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22764 s->refcount++;
22765 /* Avoid unnecessarily putting strings that are used less than
22766 twice in the hash table. */
22767 if (s->refcount
22768 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22770 void ** slot;
22771 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22772 htab_hash_string (s->str),
22773 INSERT);
22774 gcc_assert (*slot == NULL);
22775 *slot = s;
22780 /* Remove from the tree DIE any dies that aren't marked. */
22782 static void
22783 prune_unused_types_prune (dw_die_ref die)
22785 dw_die_ref c;
22787 gcc_assert (die->die_mark);
22788 prune_unused_types_update_strings (die);
22790 if (! die->die_child)
22791 return;
22793 c = die->die_child;
22794 do {
22795 dw_die_ref prev = c;
22796 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22797 if (c == die->die_child)
22799 /* No marked children between 'prev' and the end of the list. */
22800 if (prev == c)
22801 /* No marked children at all. */
22802 die->die_child = NULL;
22803 else
22805 prev->die_sib = c->die_sib;
22806 die->die_child = prev;
22808 return;
22811 if (c != prev->die_sib)
22812 prev->die_sib = c;
22813 prune_unused_types_prune (c);
22814 } while (c != die->die_child);
22817 /* A helper function for dwarf2out_finish called through
22818 htab_traverse. Clear .debug_str strings that we haven't already
22819 decided to emit. */
22821 static int
22822 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22824 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22826 if (!node->label || !node->refcount)
22827 htab_clear_slot (debug_str_hash, h);
22829 return 1;
22832 /* Remove dies representing declarations that we never use. */
22834 static void
22835 prune_unused_types (void)
22837 unsigned int i;
22838 limbo_die_node *node;
22839 comdat_type_node *ctnode;
22840 pubname_ref pub;
22842 #if ENABLE_ASSERT_CHECKING
22843 /* All the marks should already be clear. */
22844 verify_marks_clear (comp_unit_die ());
22845 for (node = limbo_die_list; node; node = node->next)
22846 verify_marks_clear (node->die);
22847 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22848 verify_marks_clear (ctnode->root_die);
22849 #endif /* ENABLE_ASSERT_CHECKING */
22851 /* Mark types that are used in global variables. */
22852 premark_types_used_by_global_vars ();
22854 /* Set the mark on nodes that are actually used. */
22855 prune_unused_types_walk (comp_unit_die ());
22856 for (node = limbo_die_list; node; node = node->next)
22857 prune_unused_types_walk (node->die);
22858 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22860 prune_unused_types_walk (ctnode->root_die);
22861 prune_unused_types_mark (ctnode->type_die, 1);
22864 /* Also set the mark on nodes referenced from the
22865 pubname_table. */
22866 FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22867 prune_unused_types_mark (pub->die, 1);
22869 /* Get rid of nodes that aren't marked; and update the string counts. */
22870 if (debug_str_hash && debug_str_hash_forced)
22871 htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22872 else if (debug_str_hash)
22873 htab_empty (debug_str_hash);
22874 prune_unused_types_prune (comp_unit_die ());
22875 for (node = limbo_die_list; node; node = node->next)
22876 prune_unused_types_prune (node->die);
22877 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22878 prune_unused_types_prune (ctnode->root_die);
22880 /* Leave the marks clear. */
22881 prune_unmark_dies (comp_unit_die ());
22882 for (node = limbo_die_list; node; node = node->next)
22883 prune_unmark_dies (node->die);
22884 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22885 prune_unmark_dies (ctnode->root_die);
22888 /* Set the parameter to true if there are any relative pathnames in
22889 the file table. */
22890 static int
22891 file_table_relative_p (void ** slot, void *param)
22893 bool *p = (bool *) param;
22894 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22895 if (!IS_ABSOLUTE_PATH (d->filename))
22897 *p = true;
22898 return 0;
22900 return 1;
22903 /* Routines to manipulate hash table of comdat type units. */
22905 static hashval_t
22906 htab_ct_hash (const void *of)
22908 hashval_t h;
22909 const comdat_type_node *const type_node = (const comdat_type_node *) of;
22911 memcpy (&h, type_node->signature, sizeof (h));
22912 return h;
22915 static int
22916 htab_ct_eq (const void *of1, const void *of2)
22918 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22919 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22921 return (! memcmp (type_node_1->signature, type_node_2->signature,
22922 DWARF_TYPE_SIGNATURE_SIZE));
22925 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22926 to the location it would have been added, should we know its
22927 DECL_ASSEMBLER_NAME when we added other attributes. This will
22928 probably improve compactness of debug info, removing equivalent
22929 abbrevs, and hide any differences caused by deferring the
22930 computation of the assembler name, triggered by e.g. PCH. */
22932 static inline void
22933 move_linkage_attr (dw_die_ref die)
22935 unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22936 dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22938 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22939 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22941 while (--ix > 0)
22943 dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22945 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22946 break;
22949 if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22951 VEC_pop (dw_attr_node, die->die_attr);
22952 VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22956 /* Helper function for resolve_addr, attempt to resolve
22957 one CONST_STRING, return non-zero if not successful. Similarly verify that
22958 SYMBOL_REFs refer to variables emitted in the current CU. */
22960 static int
22961 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22963 rtx rtl = *addr;
22965 if (GET_CODE (rtl) == CONST_STRING)
22967 size_t len = strlen (XSTR (rtl, 0)) + 1;
22968 tree t = build_string (len, XSTR (rtl, 0));
22969 tree tlen = build_int_cst (NULL_TREE, len - 1);
22970 TREE_TYPE (t)
22971 = build_array_type (char_type_node, build_index_type (tlen));
22972 rtl = lookup_constant_def (t);
22973 if (!rtl || !MEM_P (rtl))
22974 return 1;
22975 rtl = XEXP (rtl, 0);
22976 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22977 *addr = rtl;
22978 return 0;
22981 if (GET_CODE (rtl) == SYMBOL_REF
22982 && SYMBOL_REF_DECL (rtl))
22984 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22986 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22987 return 1;
22989 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22990 return 1;
22993 if (GET_CODE (rtl) == CONST
22994 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22995 return 1;
22997 return 0;
23000 /* Helper function for resolve_addr, handle one location
23001 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23002 the location list couldn't be resolved. */
23004 static bool
23005 resolve_addr_in_expr (dw_loc_descr_ref loc)
23007 for (; loc; loc = loc->dw_loc_next)
23008 if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
23009 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23010 || (loc->dw_loc_opc == DW_OP_implicit_value
23011 && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23012 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
23013 return false;
23014 else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
23015 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23017 dw_die_ref ref
23018 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23019 if (ref == NULL)
23020 return false;
23021 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23022 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23023 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23025 return true;
23028 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23029 an address in .rodata section if the string literal is emitted there,
23030 or remove the containing location list or replace DW_AT_const_value
23031 with DW_AT_location and empty location expression, if it isn't found
23032 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23033 to something that has been emitted in the current CU. */
23035 static void
23036 resolve_addr (dw_die_ref die)
23038 dw_die_ref c;
23039 dw_attr_ref a;
23040 dw_loc_list_ref *curr, *start, loc;
23041 unsigned ix;
23043 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23044 switch (AT_class (a))
23046 case dw_val_class_loc_list:
23047 start = curr = AT_loc_list_ptr (a);
23048 loc = *curr;
23049 gcc_assert (loc);
23050 /* The same list can be referenced more than once. See if we have
23051 already recorded the result from a previous pass. */
23052 if (loc->replaced)
23053 *curr = loc->dw_loc_next;
23054 else if (!loc->resolved_addr)
23056 /* As things stand, we do not expect or allow one die to
23057 reference a suffix of another die's location list chain.
23058 References must be identical or completely separate.
23059 There is therefore no need to cache the result of this
23060 pass on any list other than the first; doing so
23061 would lead to unnecessary writes. */
23062 while (*curr)
23064 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23065 if (!resolve_addr_in_expr ((*curr)->expr))
23067 dw_loc_list_ref next = (*curr)->dw_loc_next;
23068 if (next && (*curr)->ll_symbol)
23070 gcc_assert (!next->ll_symbol);
23071 next->ll_symbol = (*curr)->ll_symbol;
23073 *curr = next;
23075 else
23076 curr = &(*curr)->dw_loc_next;
23078 if (loc == *start)
23079 loc->resolved_addr = 1;
23080 else
23082 loc->replaced = 1;
23083 loc->dw_loc_next = *start;
23086 if (!*start)
23088 remove_AT (die, a->dw_attr);
23089 ix--;
23091 break;
23092 case dw_val_class_loc:
23093 if (!resolve_addr_in_expr (AT_loc (a)))
23095 remove_AT (die, a->dw_attr);
23096 ix--;
23098 break;
23099 case dw_val_class_addr:
23100 if (a->dw_attr == DW_AT_const_value
23101 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23103 remove_AT (die, a->dw_attr);
23104 ix--;
23106 if (die->die_tag == DW_TAG_GNU_call_site
23107 && a->dw_attr == DW_AT_abstract_origin)
23109 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23110 dw_die_ref tdie = lookup_decl_die (tdecl);
23111 if (tdie == NULL
23112 && DECL_EXTERNAL (tdecl)
23113 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23115 force_decl_die (tdecl);
23116 tdie = lookup_decl_die (tdecl);
23118 if (tdie)
23120 a->dw_attr_val.val_class = dw_val_class_die_ref;
23121 a->dw_attr_val.v.val_die_ref.die = tdie;
23122 a->dw_attr_val.v.val_die_ref.external = 0;
23124 else
23126 remove_AT (die, a->dw_attr);
23127 ix--;
23130 break;
23131 default:
23132 break;
23135 FOR_EACH_CHILD (die, c, resolve_addr (c));
23138 /* Helper routines for optimize_location_lists.
23139 This pass tries to share identical local lists in .debug_loc
23140 section. */
23142 /* Iteratively hash operands of LOC opcode. */
23144 static inline hashval_t
23145 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23147 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23148 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23150 switch (loc->dw_loc_opc)
23152 case DW_OP_const4u:
23153 case DW_OP_const8u:
23154 if (loc->dtprel)
23155 goto hash_addr;
23156 /* FALLTHRU */
23157 case DW_OP_const1u:
23158 case DW_OP_const1s:
23159 case DW_OP_const2u:
23160 case DW_OP_const2s:
23161 case DW_OP_const4s:
23162 case DW_OP_const8s:
23163 case DW_OP_constu:
23164 case DW_OP_consts:
23165 case DW_OP_pick:
23166 case DW_OP_plus_uconst:
23167 case DW_OP_breg0:
23168 case DW_OP_breg1:
23169 case DW_OP_breg2:
23170 case DW_OP_breg3:
23171 case DW_OP_breg4:
23172 case DW_OP_breg5:
23173 case DW_OP_breg6:
23174 case DW_OP_breg7:
23175 case DW_OP_breg8:
23176 case DW_OP_breg9:
23177 case DW_OP_breg10:
23178 case DW_OP_breg11:
23179 case DW_OP_breg12:
23180 case DW_OP_breg13:
23181 case DW_OP_breg14:
23182 case DW_OP_breg15:
23183 case DW_OP_breg16:
23184 case DW_OP_breg17:
23185 case DW_OP_breg18:
23186 case DW_OP_breg19:
23187 case DW_OP_breg20:
23188 case DW_OP_breg21:
23189 case DW_OP_breg22:
23190 case DW_OP_breg23:
23191 case DW_OP_breg24:
23192 case DW_OP_breg25:
23193 case DW_OP_breg26:
23194 case DW_OP_breg27:
23195 case DW_OP_breg28:
23196 case DW_OP_breg29:
23197 case DW_OP_breg30:
23198 case DW_OP_breg31:
23199 case DW_OP_regx:
23200 case DW_OP_fbreg:
23201 case DW_OP_piece:
23202 case DW_OP_deref_size:
23203 case DW_OP_xderef_size:
23204 hash = iterative_hash_object (val1->v.val_int, hash);
23205 break;
23206 case DW_OP_skip:
23207 case DW_OP_bra:
23209 int offset;
23211 gcc_assert (val1->val_class == dw_val_class_loc);
23212 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23213 hash = iterative_hash_object (offset, hash);
23215 break;
23216 case DW_OP_implicit_value:
23217 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23218 switch (val2->val_class)
23220 case dw_val_class_const:
23221 hash = iterative_hash_object (val2->v.val_int, hash);
23222 break;
23223 case dw_val_class_vec:
23225 unsigned int elt_size = val2->v.val_vec.elt_size;
23226 unsigned int len = val2->v.val_vec.length;
23228 hash = iterative_hash_object (elt_size, hash);
23229 hash = iterative_hash_object (len, hash);
23230 hash = iterative_hash (val2->v.val_vec.array,
23231 len * elt_size, hash);
23233 break;
23234 case dw_val_class_const_double:
23235 hash = iterative_hash_object (val2->v.val_double.low, hash);
23236 hash = iterative_hash_object (val2->v.val_double.high, hash);
23237 break;
23238 case dw_val_class_addr:
23239 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23240 break;
23241 default:
23242 gcc_unreachable ();
23244 break;
23245 case DW_OP_bregx:
23246 case DW_OP_bit_piece:
23247 hash = iterative_hash_object (val1->v.val_int, hash);
23248 hash = iterative_hash_object (val2->v.val_int, hash);
23249 break;
23250 case DW_OP_addr:
23251 hash_addr:
23252 if (loc->dtprel)
23254 unsigned char dtprel = 0xd1;
23255 hash = iterative_hash_object (dtprel, hash);
23257 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23258 break;
23259 case DW_OP_GNU_implicit_pointer:
23260 hash = iterative_hash_object (val2->v.val_int, hash);
23261 break;
23262 case DW_OP_GNU_entry_value:
23263 hash = hash_loc_operands (val1->v.val_loc, hash);
23264 break;
23266 default:
23267 /* Other codes have no operands. */
23268 break;
23270 return hash;
23273 /* Iteratively hash the whole DWARF location expression LOC. */
23275 static inline hashval_t
23276 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23278 dw_loc_descr_ref l;
23279 bool sizes_computed = false;
23280 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23281 size_of_locs (loc);
23283 for (l = loc; l != NULL; l = l->dw_loc_next)
23285 enum dwarf_location_atom opc = l->dw_loc_opc;
23286 hash = iterative_hash_object (opc, hash);
23287 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23289 size_of_locs (loc);
23290 sizes_computed = true;
23292 hash = hash_loc_operands (l, hash);
23294 return hash;
23297 /* Compute hash of the whole location list LIST_HEAD. */
23299 static inline void
23300 hash_loc_list (dw_loc_list_ref list_head)
23302 dw_loc_list_ref curr = list_head;
23303 hashval_t hash = 0;
23305 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23307 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23308 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23309 if (curr->section)
23310 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23311 hash);
23312 hash = hash_locs (curr->expr, hash);
23314 list_head->hash = hash;
23317 /* Return true if X and Y opcodes have the same operands. */
23319 static inline bool
23320 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23322 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23323 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23324 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23325 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23327 switch (x->dw_loc_opc)
23329 case DW_OP_const4u:
23330 case DW_OP_const8u:
23331 if (x->dtprel)
23332 goto hash_addr;
23333 /* FALLTHRU */
23334 case DW_OP_const1u:
23335 case DW_OP_const1s:
23336 case DW_OP_const2u:
23337 case DW_OP_const2s:
23338 case DW_OP_const4s:
23339 case DW_OP_const8s:
23340 case DW_OP_constu:
23341 case DW_OP_consts:
23342 case DW_OP_pick:
23343 case DW_OP_plus_uconst:
23344 case DW_OP_breg0:
23345 case DW_OP_breg1:
23346 case DW_OP_breg2:
23347 case DW_OP_breg3:
23348 case DW_OP_breg4:
23349 case DW_OP_breg5:
23350 case DW_OP_breg6:
23351 case DW_OP_breg7:
23352 case DW_OP_breg8:
23353 case DW_OP_breg9:
23354 case DW_OP_breg10:
23355 case DW_OP_breg11:
23356 case DW_OP_breg12:
23357 case DW_OP_breg13:
23358 case DW_OP_breg14:
23359 case DW_OP_breg15:
23360 case DW_OP_breg16:
23361 case DW_OP_breg17:
23362 case DW_OP_breg18:
23363 case DW_OP_breg19:
23364 case DW_OP_breg20:
23365 case DW_OP_breg21:
23366 case DW_OP_breg22:
23367 case DW_OP_breg23:
23368 case DW_OP_breg24:
23369 case DW_OP_breg25:
23370 case DW_OP_breg26:
23371 case DW_OP_breg27:
23372 case DW_OP_breg28:
23373 case DW_OP_breg29:
23374 case DW_OP_breg30:
23375 case DW_OP_breg31:
23376 case DW_OP_regx:
23377 case DW_OP_fbreg:
23378 case DW_OP_piece:
23379 case DW_OP_deref_size:
23380 case DW_OP_xderef_size:
23381 return valx1->v.val_int == valy1->v.val_int;
23382 case DW_OP_skip:
23383 case DW_OP_bra:
23384 gcc_assert (valx1->val_class == dw_val_class_loc
23385 && valy1->val_class == dw_val_class_loc
23386 && x->dw_loc_addr == y->dw_loc_addr);
23387 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23388 case DW_OP_implicit_value:
23389 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23390 || valx2->val_class != valy2->val_class)
23391 return false;
23392 switch (valx2->val_class)
23394 case dw_val_class_const:
23395 return valx2->v.val_int == valy2->v.val_int;
23396 case dw_val_class_vec:
23397 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23398 && valx2->v.val_vec.length == valy2->v.val_vec.length
23399 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23400 valx2->v.val_vec.elt_size
23401 * valx2->v.val_vec.length) == 0;
23402 case dw_val_class_const_double:
23403 return valx2->v.val_double.low == valy2->v.val_double.low
23404 && valx2->v.val_double.high == valy2->v.val_double.high;
23405 case dw_val_class_addr:
23406 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23407 default:
23408 gcc_unreachable ();
23410 case DW_OP_bregx:
23411 case DW_OP_bit_piece:
23412 return valx1->v.val_int == valy1->v.val_int
23413 && valx2->v.val_int == valy2->v.val_int;
23414 case DW_OP_addr:
23415 hash_addr:
23416 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23417 case DW_OP_GNU_implicit_pointer:
23418 return valx1->val_class == dw_val_class_die_ref
23419 && valx1->val_class == valy1->val_class
23420 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23421 && valx2->v.val_int == valy2->v.val_int;
23422 case DW_OP_GNU_entry_value:
23423 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23424 default:
23425 /* Other codes have no operands. */
23426 return true;
23430 /* Return true if DWARF location expressions X and Y are the same. */
23432 static inline bool
23433 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23435 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23436 if (x->dw_loc_opc != y->dw_loc_opc
23437 || x->dtprel != y->dtprel
23438 || !compare_loc_operands (x, y))
23439 break;
23440 return x == NULL && y == NULL;
23443 /* Return precomputed hash of location list X. */
23445 static hashval_t
23446 loc_list_hash (const void *x)
23448 return ((const struct dw_loc_list_struct *) x)->hash;
23451 /* Return 1 if location lists X and Y are the same. */
23453 static int
23454 loc_list_eq (const void *x, const void *y)
23456 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23457 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23458 if (a == b)
23459 return 1;
23460 if (a->hash != b->hash)
23461 return 0;
23462 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23463 if (strcmp (a->begin, b->begin) != 0
23464 || strcmp (a->end, b->end) != 0
23465 || (a->section == NULL) != (b->section == NULL)
23466 || (a->section && strcmp (a->section, b->section) != 0)
23467 || !compare_locs (a->expr, b->expr))
23468 break;
23469 return a == NULL && b == NULL;
23472 /* Recursively optimize location lists referenced from DIE
23473 children and share them whenever possible. */
23475 static void
23476 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23478 dw_die_ref c;
23479 dw_attr_ref a;
23480 unsigned ix;
23481 void **slot;
23483 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23484 if (AT_class (a) == dw_val_class_loc_list)
23486 dw_loc_list_ref list = AT_loc_list (a);
23487 /* TODO: perform some optimizations here, before hashing
23488 it and storing into the hash table. */
23489 hash_loc_list (list);
23490 slot = htab_find_slot_with_hash (htab, list, list->hash,
23491 INSERT);
23492 if (*slot == NULL)
23493 *slot = (void *) list;
23494 else
23495 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23498 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23501 /* Optimize location lists referenced from DIE
23502 children and share them whenever possible. */
23504 static void
23505 optimize_location_lists (dw_die_ref die)
23507 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23508 optimize_location_lists_1 (die, htab);
23509 htab_delete (htab);
23512 /* Output stuff that dwarf requires at the end of every file,
23513 and generate the DWARF-2 debugging info. */
23515 static void
23516 dwarf2out_finish (const char *filename)
23518 limbo_die_node *node, *next_node;
23519 comdat_type_node *ctnode;
23520 htab_t comdat_type_table;
23521 unsigned int i;
23523 gen_scheduled_generic_parms_dies ();
23524 gen_remaining_tmpl_value_param_die_attribute ();
23526 /* Add the name for the main input file now. We delayed this from
23527 dwarf2out_init to avoid complications with PCH. */
23528 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23529 if (!IS_ABSOLUTE_PATH (filename))
23530 add_comp_dir_attribute (comp_unit_die ());
23531 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23533 bool p = false;
23534 htab_traverse (file_table, file_table_relative_p, &p);
23535 if (p)
23536 add_comp_dir_attribute (comp_unit_die ());
23539 for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
23541 add_location_or_const_value_attribute (
23542 VEC_index (deferred_locations, deferred_locations_list, i)->die,
23543 VEC_index (deferred_locations, deferred_locations_list, i)->variable,
23544 false,
23545 DW_AT_location);
23548 /* Traverse the limbo die list, and add parent/child links. The only
23549 dies without parents that should be here are concrete instances of
23550 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23551 For concrete instances, we can get the parent die from the abstract
23552 instance. */
23553 for (node = limbo_die_list; node; node = next_node)
23555 dw_die_ref die = node->die;
23556 next_node = node->next;
23558 if (die->die_parent == NULL)
23560 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23562 if (origin)
23563 add_child_die (origin->die_parent, die);
23564 else if (is_cu_die (die))
23566 else if (seen_error ())
23567 /* It's OK to be confused by errors in the input. */
23568 add_child_die (comp_unit_die (), die);
23569 else
23571 /* In certain situations, the lexical block containing a
23572 nested function can be optimized away, which results
23573 in the nested function die being orphaned. Likewise
23574 with the return type of that nested function. Force
23575 this to be a child of the containing function.
23577 It may happen that even the containing function got fully
23578 inlined and optimized out. In that case we are lost and
23579 assign the empty child. This should not be big issue as
23580 the function is likely unreachable too. */
23581 tree context = NULL_TREE;
23583 gcc_assert (node->created_for);
23585 if (DECL_P (node->created_for))
23586 context = DECL_CONTEXT (node->created_for);
23587 else if (TYPE_P (node->created_for))
23588 context = TYPE_CONTEXT (node->created_for);
23590 gcc_assert (context
23591 && (TREE_CODE (context) == FUNCTION_DECL
23592 || TREE_CODE (context) == NAMESPACE_DECL));
23594 origin = lookup_decl_die (context);
23595 if (origin)
23596 add_child_die (origin, die);
23597 else
23598 add_child_die (comp_unit_die (), die);
23603 limbo_die_list = NULL;
23605 resolve_addr (comp_unit_die ());
23607 for (node = deferred_asm_name; node; node = node->next)
23609 tree decl = node->created_for;
23610 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23612 add_linkage_attr (node->die, decl);
23613 move_linkage_attr (node->die);
23617 deferred_asm_name = NULL;
23619 /* Walk through the list of incomplete types again, trying once more to
23620 emit full debugging info for them. */
23621 retry_incomplete_types ();
23623 if (flag_eliminate_unused_debug_types)
23624 prune_unused_types ();
23626 /* Generate separate CUs for each of the include files we've seen.
23627 They will go into limbo_die_list. */
23628 if (flag_eliminate_dwarf2_dups && ! use_debug_types)
23629 break_out_includes (comp_unit_die ());
23631 /* Generate separate COMDAT sections for type DIEs. */
23632 if (use_debug_types)
23634 break_out_comdat_types (comp_unit_die ());
23636 /* Each new type_unit DIE was added to the limbo die list when created.
23637 Since these have all been added to comdat_type_list, clear the
23638 limbo die list. */
23639 limbo_die_list = NULL;
23641 /* For each new comdat type unit, copy declarations for incomplete
23642 types to make the new unit self-contained (i.e., no direct
23643 references to the main compile unit). */
23644 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23645 copy_decls_for_unworthy_types (ctnode->root_die);
23646 copy_decls_for_unworthy_types (comp_unit_die ());
23648 /* In the process of copying declarations from one unit to another,
23649 we may have left some declarations behind that are no longer
23650 referenced. Prune them. */
23651 prune_unused_types ();
23654 /* Traverse the DIE's and add add sibling attributes to those DIE's
23655 that have children. */
23656 add_sibling_attributes (comp_unit_die ());
23657 for (node = limbo_die_list; node; node = node->next)
23658 add_sibling_attributes (node->die);
23659 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23660 add_sibling_attributes (ctnode->root_die);
23662 /* Output a terminator label for the .text section. */
23663 switch_to_section (text_section);
23664 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23665 if (cold_text_section)
23667 switch_to_section (cold_text_section);
23668 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23671 /* We can only use the low/high_pc attributes if all of the code was
23672 in .text. */
23673 if (!have_multiple_function_sections
23674 || (dwarf_version < 3 && dwarf_strict))
23676 /* Don't add if the CU has no associated code. */
23677 if (text_section_used)
23679 add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23680 add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23683 else
23685 unsigned fde_idx = 0;
23686 bool range_list_added = false;
23688 if (text_section_used)
23689 add_ranges_by_labels (comp_unit_die (), text_section_label,
23690 text_end_label, &range_list_added);
23691 if (cold_text_section_used)
23692 add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23693 cold_end_label, &range_list_added);
23695 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23697 dw_fde_ref fde = &fde_table[fde_idx];
23699 if (!fde->in_std_section)
23700 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23701 fde->dw_fde_end, &range_list_added);
23702 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23703 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
23704 fde->dw_fde_second_end, &range_list_added);
23707 if (range_list_added)
23709 /* We need to give .debug_loc and .debug_ranges an appropriate
23710 "base address". Use zero so that these addresses become
23711 absolute. Historically, we've emitted the unexpected
23712 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23713 Emit both to give time for other tools to adapt. */
23714 add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23715 if (! dwarf_strict && dwarf_version < 4)
23716 add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23718 add_ranges (NULL);
23722 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23723 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23724 debug_line_section_label);
23726 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23727 add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23729 if (have_location_lists)
23730 optimize_location_lists (comp_unit_die ());
23732 /* Output all of the compilation units. We put the main one last so that
23733 the offsets are available to output_pubnames. */
23734 for (node = limbo_die_list; node; node = node->next)
23735 output_comp_unit (node->die, 0);
23737 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23738 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23740 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23742 /* Don't output duplicate types. */
23743 if (*slot != HTAB_EMPTY_ENTRY)
23744 continue;
23746 /* Add a pointer to the line table for the main compilation unit
23747 so that the debugger can make sense of DW_AT_decl_file
23748 attributes. */
23749 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23750 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23751 debug_line_section_label);
23753 output_comdat_type_unit (ctnode);
23754 *slot = ctnode;
23756 htab_delete (comdat_type_table);
23758 /* Output the main compilation unit if non-empty or if .debug_macinfo
23759 will be emitted. */
23760 output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23762 /* Output the abbreviation table. */
23763 switch_to_section (debug_abbrev_section);
23764 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23765 output_abbrev_section ();
23767 /* Output location list section if necessary. */
23768 if (have_location_lists)
23770 /* Output the location lists info. */
23771 switch_to_section (debug_loc_section);
23772 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23773 DEBUG_LOC_SECTION_LABEL, 0);
23774 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23775 output_location_lists (comp_unit_die ());
23778 /* Output public names table if necessary. */
23779 if (!VEC_empty (pubname_entry, pubname_table))
23781 gcc_assert (info_section_emitted);
23782 switch_to_section (debug_pubnames_section);
23783 output_pubnames (pubname_table);
23786 /* Output public types table if necessary. */
23787 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23788 It shouldn't hurt to emit it always, since pure DWARF2 consumers
23789 simply won't look for the section. */
23790 if (!VEC_empty (pubname_entry, pubtype_table))
23792 bool empty = false;
23794 if (flag_eliminate_unused_debug_types)
23796 /* The pubtypes table might be emptied by pruning unused items. */
23797 unsigned i;
23798 pubname_ref p;
23799 empty = true;
23800 FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23801 if (p->die->die_offset != 0)
23803 empty = false;
23804 break;
23807 if (!empty)
23809 gcc_assert (info_section_emitted);
23810 switch_to_section (debug_pubtypes_section);
23811 output_pubnames (pubtype_table);
23815 /* Output the address range information. We only put functions in the
23816 arange table, so don't write it out if we don't have any. */
23817 if (info_section_emitted)
23819 unsigned long aranges_length = size_of_aranges ();
23821 /* Empty .debug_aranges would contain just header and
23822 terminating 0,0. */
23823 if (aranges_length
23824 != (unsigned long) (DWARF_ARANGES_HEADER_SIZE
23825 + 2 * DWARF2_ADDR_SIZE))
23827 switch_to_section (debug_aranges_section);
23828 output_aranges (aranges_length);
23832 /* Output ranges section if necessary. */
23833 if (ranges_table_in_use)
23835 switch_to_section (debug_ranges_section);
23836 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23837 output_ranges ();
23840 /* Output the source line correspondence table. We must do this
23841 even if there is no line information. Otherwise, on an empty
23842 translation unit, we will generate a present, but empty,
23843 .debug_info section. IRIX 6.5 `nm' will then complain when
23844 examining the file. This is done late so that any filenames
23845 used by the debug_info section are marked as 'used'. */
23846 switch_to_section (debug_line_section);
23847 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23848 if (! DWARF2_ASM_LINE_DEBUG_INFO)
23849 output_line_info ();
23851 /* Have to end the macro section. */
23852 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23854 switch_to_section (debug_macinfo_section);
23855 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23856 if (!VEC_empty (macinfo_entry, macinfo_table))
23857 output_macinfo ();
23858 dw2_asm_output_data (1, 0, "End compilation unit");
23861 /* If we emitted any DW_FORM_strp form attribute, output the string
23862 table too. */
23863 if (debug_str_hash)
23864 htab_traverse (debug_str_hash, output_indirect_string, NULL);
23867 #include "gt-dwarf2out.h"