Bring in all split-stack work done over on gccgo branch.
[official-gcc.git] / gcc / dwarf2out.c
blob84997a2886574b19d1c9f3a86a033b2d542c56cd
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
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"
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
99 #ifdef VMS_DEBUGGING_INFO
100 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
102 /* Define this macro to be a nonzero value if the directory specifications
103 which are output in the debug info should end with a separator. */
104 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
105 /* Define this macro to evaluate to a nonzero value if GCC should refrain
106 from generating indirect strings in DWARF2 debug information, for instance
107 if your target is stuck with an old version of GDB that is unable to
108 process them properly or uses VMS Debug. */
109 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
110 #else
111 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
113 #endif
115 #ifndef DWARF2_UNWIND_INFO
116 #define DWARF2_UNWIND_INFO 0
117 #endif
119 #ifndef INCOMING_RETURN_ADDR_RTX
120 #define INCOMING_RETURN_ADDR_RTX (gcc_unreachable (), NULL_RTX)
121 #endif
123 #ifndef DWARF2_FRAME_INFO
124 # ifdef DWARF2_DEBUGGING_INFO
125 # define DWARF2_FRAME_INFO \
126 (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
127 # else
128 # define DWARF2_FRAME_INFO 0
129 # endif
130 #endif
132 /* Map register numbers held in the call frame info that gcc has
133 collected using DWARF_FRAME_REGNUM to those that should be output in
134 .debug_frame and .eh_frame. */
135 #ifndef DWARF2_FRAME_REG_OUT
136 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
137 #endif
139 /* Save the result of dwarf2out_do_frame across PCH. */
140 static GTY(()) bool saved_do_cfi_asm = 0;
142 /* Decide whether we want to emit frame unwind information for the current
143 translation unit. */
146 dwarf2out_do_frame (void)
148 /* We want to emit correct CFA location expressions or lists, so we
149 have to return true if we're going to output debug info, even if
150 we're not going to output frame or unwind info. */
151 return (write_symbols == DWARF2_DEBUG
152 || write_symbols == VMS_AND_DWARF2_DEBUG
153 || DWARF2_FRAME_INFO || saved_do_cfi_asm
154 || (DWARF2_UNWIND_INFO
155 && (flag_unwind_tables
156 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
160 /* Decide whether to emit frame unwind via assembler directives. */
163 dwarf2out_do_cfi_asm (void)
165 int enc;
167 #ifdef MIPS_DEBUGGING_INFO
168 return false;
169 #endif
170 if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
171 return false;
172 if (saved_do_cfi_asm)
173 return true;
174 if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
175 return false;
177 /* Make sure the personality encoding is one the assembler can support.
178 In particular, aligned addresses can't be handled. */
179 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
180 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
181 return false;
182 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
183 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
184 return false;
186 if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE)
188 #ifdef TARGET_UNWIND_INFO
189 return false;
190 #else
191 if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
192 return false;
193 #endif
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_dcall_section;
232 static GTY(()) section *debug_vcall_section;
233 static GTY(()) section *debug_str_section;
234 static GTY(()) section *debug_ranges_section;
235 static GTY(()) section *debug_frame_section;
237 /* Personality decl of current unit. Used only when assembler does not support
238 personality CFI. */
239 static GTY(()) rtx current_unit_personality;
241 /* How to start an assembler comment. */
242 #ifndef ASM_COMMENT_START
243 #define ASM_COMMENT_START ";#"
244 #endif
246 typedef struct dw_cfi_struct *dw_cfi_ref;
247 typedef struct dw_fde_struct *dw_fde_ref;
248 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
250 /* Call frames are described using a sequence of Call Frame
251 Information instructions. The register number, offset
252 and address fields are provided as possible operands;
253 their use is selected by the opcode field. */
255 enum dw_cfi_oprnd_type {
256 dw_cfi_oprnd_unused,
257 dw_cfi_oprnd_reg_num,
258 dw_cfi_oprnd_offset,
259 dw_cfi_oprnd_addr,
260 dw_cfi_oprnd_loc
263 typedef union GTY(()) dw_cfi_oprnd_struct {
264 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
265 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
266 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
267 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
269 dw_cfi_oprnd;
271 typedef struct GTY(()) dw_cfi_struct {
272 dw_cfi_ref dw_cfi_next;
273 enum dwarf_call_frame_info dw_cfi_opc;
274 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
275 dw_cfi_oprnd1;
276 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
277 dw_cfi_oprnd2;
279 dw_cfi_node;
281 /* This is how we define the location of the CFA. We use to handle it
282 as REG + OFFSET all the time, but now it can be more complex.
283 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
284 Instead of passing around REG and OFFSET, we pass a copy
285 of this structure. */
286 typedef struct GTY(()) cfa_loc {
287 HOST_WIDE_INT offset;
288 HOST_WIDE_INT base_offset;
289 unsigned int reg;
290 BOOL_BITFIELD indirect : 1; /* 1 if CFA is accessed via a dereference. */
291 BOOL_BITFIELD in_use : 1; /* 1 if a saved cfa is stored here. */
292 } dw_cfa_location;
294 /* All call frame descriptions (FDE's) in the GCC generated DWARF
295 refer to a single Common Information Entry (CIE), defined at
296 the beginning of the .debug_frame section. This use of a single
297 CIE obviates the need to keep track of multiple CIE's
298 in the DWARF generation routines below. */
300 typedef struct GTY(()) dw_fde_struct {
301 tree decl;
302 const char *dw_fde_begin;
303 const char *dw_fde_current_label;
304 const char *dw_fde_end;
305 const char *dw_fde_vms_end_prologue;
306 const char *dw_fde_vms_begin_epilogue;
307 const char *dw_fde_hot_section_label;
308 const char *dw_fde_hot_section_end_label;
309 const char *dw_fde_unlikely_section_label;
310 const char *dw_fde_unlikely_section_end_label;
311 dw_cfi_ref dw_fde_cfi;
312 dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections. */
313 HOST_WIDE_INT stack_realignment;
314 unsigned funcdef_number;
315 /* Dynamic realign argument pointer register. */
316 unsigned int drap_reg;
317 /* Virtual dynamic realign argument pointer register. */
318 unsigned int vdrap_reg;
319 /* These 3 flags are copied from rtl_data in function.h. */
320 unsigned all_throwers_are_sibcalls : 1;
321 unsigned uses_eh_lsda : 1;
322 unsigned nothrow : 1;
323 /* Whether we did stack realign in this call frame. */
324 unsigned stack_realign : 1;
325 /* Whether dynamic realign argument pointer register has been saved. */
326 unsigned drap_reg_saved: 1;
327 /* True iff dw_fde_begin label is in text_section or cold_text_section. */
328 unsigned in_std_section : 1;
329 /* True iff dw_fde_unlikely_section_label is in text_section or
330 cold_text_section. */
331 unsigned cold_in_std_section : 1;
332 /* True iff switched sections. */
333 unsigned dw_fde_switched_sections : 1;
334 /* True iff switching from cold to hot section. */
335 unsigned dw_fde_switched_cold_to_hot : 1;
337 dw_fde_node;
339 /* Maximum size (in bytes) of an artificially generated label. */
340 #define MAX_ARTIFICIAL_LABEL_BYTES 30
342 /* The size of addresses as they appear in the Dwarf 2 data.
343 Some architectures use word addresses to refer to code locations,
344 but Dwarf 2 info always uses byte addresses. On such machines,
345 Dwarf 2 addresses need to be larger than the architecture's
346 pointers. */
347 #ifndef DWARF2_ADDR_SIZE
348 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
349 #endif
351 /* The size in bytes of a DWARF field indicating an offset or length
352 relative to a debug info section, specified to be 4 bytes in the
353 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
354 as PTR_SIZE. */
356 #ifndef DWARF_OFFSET_SIZE
357 #define DWARF_OFFSET_SIZE 4
358 #endif
360 /* The size in bytes of a DWARF 4 type signature. */
362 #ifndef DWARF_TYPE_SIGNATURE_SIZE
363 #define DWARF_TYPE_SIGNATURE_SIZE 8
364 #endif
366 /* According to the (draft) DWARF 3 specification, the initial length
367 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
368 bytes are 0xffffffff, followed by the length stored in the next 8
369 bytes.
371 However, the SGI/MIPS ABI uses an initial length which is equal to
372 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
374 #ifndef DWARF_INITIAL_LENGTH_SIZE
375 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
376 #endif
378 /* Round SIZE up to the nearest BOUNDARY. */
379 #define DWARF_ROUND(SIZE,BOUNDARY) \
380 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
382 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
383 #ifndef DWARF_CIE_DATA_ALIGNMENT
384 #ifdef STACK_GROWS_DOWNWARD
385 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
386 #else
387 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
388 #endif
389 #endif
391 /* CIE identifier. */
392 #if HOST_BITS_PER_WIDE_INT >= 64
393 #define DWARF_CIE_ID \
394 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
395 #else
396 #define DWARF_CIE_ID DW_CIE_ID
397 #endif
399 /* A pointer to the base of a table that contains frame description
400 information for each routine. */
401 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
403 /* Number of elements currently allocated for fde_table. */
404 static GTY(()) unsigned fde_table_allocated;
406 /* Number of elements in fde_table currently in use. */
407 static GTY(()) unsigned fde_table_in_use;
409 /* Size (in elements) of increments by which we may expand the
410 fde_table. */
411 #define FDE_TABLE_INCREMENT 256
413 /* Get the current fde_table entry we should use. */
415 static inline dw_fde_ref
416 current_fde (void)
418 return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
421 /* A list of call frame insns for the CIE. */
422 static GTY(()) dw_cfi_ref cie_cfi_head;
424 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
425 attribute that accelerates the lookup of the FDE associated
426 with the subprogram. This variable holds the table index of the FDE
427 associated with the current function (body) definition. */
428 static unsigned current_funcdef_fde;
430 struct GTY(()) indirect_string_node {
431 const char *str;
432 unsigned int refcount;
433 enum dwarf_form form;
434 char *label;
437 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
439 /* True if the compilation unit has location entries that reference
440 debug strings. */
441 static GTY(()) bool debug_str_hash_forced = false;
443 static GTY(()) int dw2_string_counter;
444 static GTY(()) unsigned long dwarf2out_cfi_label_num;
446 /* True if the compilation unit places functions in more than one section. */
447 static GTY(()) bool have_multiple_function_sections = false;
449 /* Whether the default text and cold text sections have been used at all. */
451 static GTY(()) bool text_section_used = false;
452 static GTY(()) bool cold_text_section_used = false;
454 /* The default cold text section. */
455 static GTY(()) section *cold_text_section;
457 /* Forward declarations for functions defined in this file. */
459 static char *stripattributes (const char *);
460 static const char *dwarf_cfi_name (unsigned);
461 static dw_cfi_ref new_cfi (void);
462 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
463 static void add_fde_cfi (const char *, dw_cfi_ref);
464 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
465 static void lookup_cfa (dw_cfa_location *);
466 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
467 static void initial_return_save (rtx);
468 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
469 HOST_WIDE_INT);
470 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
471 static void output_cfi_directive (dw_cfi_ref);
472 static void output_call_frame_info (int);
473 static void dwarf2out_note_section_used (void);
474 static bool clobbers_queued_reg_save (const_rtx);
475 static void dwarf2out_frame_debug_expr (rtx, const char *);
476 static void dwarf2out_frame_debug_remember_state (void);
478 /* Support for complex CFA locations. */
479 static void output_cfa_loc (dw_cfi_ref);
480 static void output_cfa_loc_raw (dw_cfi_ref);
481 static void get_cfa_from_loc_descr (dw_cfa_location *,
482 struct dw_loc_descr_struct *);
483 static struct dw_loc_descr_struct *build_cfa_loc
484 (dw_cfa_location *, HOST_WIDE_INT);
485 static struct dw_loc_descr_struct *build_cfa_aligned_loc
486 (HOST_WIDE_INT, HOST_WIDE_INT);
487 static void def_cfa_1 (const char *, dw_cfa_location *);
488 static struct dw_loc_descr_struct *mem_loc_descriptor
489 (rtx, enum machine_mode mode, enum var_init_status);
491 /* How to start an assembler comment. */
492 #ifndef ASM_COMMENT_START
493 #define ASM_COMMENT_START ";#"
494 #endif
496 /* Data and reference forms for relocatable data. */
497 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
498 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
500 #ifndef DEBUG_FRAME_SECTION
501 #define DEBUG_FRAME_SECTION ".debug_frame"
502 #endif
504 #ifndef FUNC_BEGIN_LABEL
505 #define FUNC_BEGIN_LABEL "LFB"
506 #endif
508 #ifndef FUNC_END_LABEL
509 #define FUNC_END_LABEL "LFE"
510 #endif
512 #ifndef PROLOGUE_END_LABEL
513 #define PROLOGUE_END_LABEL "LPE"
514 #endif
516 #ifndef EPILOGUE_BEGIN_LABEL
517 #define EPILOGUE_BEGIN_LABEL "LEB"
518 #endif
520 #ifndef FRAME_BEGIN_LABEL
521 #define FRAME_BEGIN_LABEL "Lframe"
522 #endif
523 #define CIE_AFTER_SIZE_LABEL "LSCIE"
524 #define CIE_END_LABEL "LECIE"
525 #define FDE_LABEL "LSFDE"
526 #define FDE_AFTER_SIZE_LABEL "LASFDE"
527 #define FDE_END_LABEL "LEFDE"
528 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
529 #define LINE_NUMBER_END_LABEL "LELT"
530 #define LN_PROLOG_AS_LABEL "LASLTP"
531 #define LN_PROLOG_END_LABEL "LELTP"
532 #define DIE_LABEL_PREFIX "DW"
534 /* The DWARF 2 CFA column which tracks the return address. Normally this
535 is the column for PC, or the first column after all of the hard
536 registers. */
537 #ifndef DWARF_FRAME_RETURN_COLUMN
538 #ifdef PC_REGNUM
539 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
540 #else
541 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
542 #endif
543 #endif
545 /* The mapping from gcc register number to DWARF 2 CFA column number. By
546 default, we just provide columns for all registers. */
547 #ifndef DWARF_FRAME_REGNUM
548 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
549 #endif
551 /* Hook used by __throw. */
554 expand_builtin_dwarf_sp_column (void)
556 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
557 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
560 /* Return a pointer to a copy of the section string name S with all
561 attributes stripped off, and an asterisk prepended (for assemble_name). */
563 static inline char *
564 stripattributes (const char *s)
566 char *stripped = XNEWVEC (char, strlen (s) + 2);
567 char *p = stripped;
569 *p++ = '*';
571 while (*s && *s != ',')
572 *p++ = *s++;
574 *p = '\0';
575 return stripped;
578 /* MEM is a memory reference for the register size table, each element of
579 which has mode MODE. Initialize column C as a return address column. */
581 static void
582 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
584 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
585 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
586 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
589 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder. */
591 static inline HOST_WIDE_INT
592 div_data_align (HOST_WIDE_INT off)
594 HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
595 gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
596 return r;
599 /* Return true if we need a signed version of a given opcode
600 (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended). */
602 static inline bool
603 need_data_align_sf_opcode (HOST_WIDE_INT off)
605 return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
608 /* Generate code to initialize the register size table. */
610 void
611 expand_builtin_init_dwarf_reg_sizes (tree address)
613 unsigned int i;
614 enum machine_mode mode = TYPE_MODE (char_type_node);
615 rtx addr = expand_normal (address);
616 rtx mem = gen_rtx_MEM (BLKmode, addr);
617 bool wrote_return_column = false;
619 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
621 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
623 if (rnum < DWARF_FRAME_REGISTERS)
625 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
626 enum machine_mode save_mode = reg_raw_mode[i];
627 HOST_WIDE_INT size;
629 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
630 save_mode = choose_hard_reg_mode (i, 1, true);
631 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
633 if (save_mode == VOIDmode)
634 continue;
635 wrote_return_column = true;
637 size = GET_MODE_SIZE (save_mode);
638 if (offset < 0)
639 continue;
641 emit_move_insn (adjust_address (mem, mode, offset),
642 gen_int_mode (size, mode));
646 if (!wrote_return_column)
647 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
649 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
650 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
651 #endif
653 targetm.init_dwarf_reg_sizes_extra (address);
656 /* Convert a DWARF call frame info. operation to its string name */
658 static const char *
659 dwarf_cfi_name (unsigned int cfi_opc)
661 switch (cfi_opc)
663 case DW_CFA_advance_loc:
664 return "DW_CFA_advance_loc";
665 case DW_CFA_offset:
666 return "DW_CFA_offset";
667 case DW_CFA_restore:
668 return "DW_CFA_restore";
669 case DW_CFA_nop:
670 return "DW_CFA_nop";
671 case DW_CFA_set_loc:
672 return "DW_CFA_set_loc";
673 case DW_CFA_advance_loc1:
674 return "DW_CFA_advance_loc1";
675 case DW_CFA_advance_loc2:
676 return "DW_CFA_advance_loc2";
677 case DW_CFA_advance_loc4:
678 return "DW_CFA_advance_loc4";
679 case DW_CFA_offset_extended:
680 return "DW_CFA_offset_extended";
681 case DW_CFA_restore_extended:
682 return "DW_CFA_restore_extended";
683 case DW_CFA_undefined:
684 return "DW_CFA_undefined";
685 case DW_CFA_same_value:
686 return "DW_CFA_same_value";
687 case DW_CFA_register:
688 return "DW_CFA_register";
689 case DW_CFA_remember_state:
690 return "DW_CFA_remember_state";
691 case DW_CFA_restore_state:
692 return "DW_CFA_restore_state";
693 case DW_CFA_def_cfa:
694 return "DW_CFA_def_cfa";
695 case DW_CFA_def_cfa_register:
696 return "DW_CFA_def_cfa_register";
697 case DW_CFA_def_cfa_offset:
698 return "DW_CFA_def_cfa_offset";
700 /* DWARF 3 */
701 case DW_CFA_def_cfa_expression:
702 return "DW_CFA_def_cfa_expression";
703 case DW_CFA_expression:
704 return "DW_CFA_expression";
705 case DW_CFA_offset_extended_sf:
706 return "DW_CFA_offset_extended_sf";
707 case DW_CFA_def_cfa_sf:
708 return "DW_CFA_def_cfa_sf";
709 case DW_CFA_def_cfa_offset_sf:
710 return "DW_CFA_def_cfa_offset_sf";
712 /* SGI/MIPS specific */
713 case DW_CFA_MIPS_advance_loc8:
714 return "DW_CFA_MIPS_advance_loc8";
716 /* GNU extensions */
717 case DW_CFA_GNU_window_save:
718 return "DW_CFA_GNU_window_save";
719 case DW_CFA_GNU_args_size:
720 return "DW_CFA_GNU_args_size";
721 case DW_CFA_GNU_negative_offset_extended:
722 return "DW_CFA_GNU_negative_offset_extended";
724 default:
725 return "DW_CFA_<unknown>";
729 /* Return a pointer to a newly allocated Call Frame Instruction. */
731 static inline dw_cfi_ref
732 new_cfi (void)
734 dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
736 cfi->dw_cfi_next = NULL;
737 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
738 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
740 return cfi;
743 /* Add a Call Frame Instruction to list of instructions. */
745 static inline void
746 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
748 dw_cfi_ref *p;
749 dw_fde_ref fde = current_fde ();
751 /* When DRAP is used, CFA is defined with an expression. Redefine
752 CFA may lead to a different CFA value. */
753 /* ??? Of course, this heuristic fails when we're annotating epilogues,
754 because of course we'll always want to redefine the CFA back to the
755 stack pointer on the way out. Where should we move this check? */
756 if (0 && fde && fde->drap_reg != INVALID_REGNUM)
757 switch (cfi->dw_cfi_opc)
759 case DW_CFA_def_cfa_register:
760 case DW_CFA_def_cfa_offset:
761 case DW_CFA_def_cfa_offset_sf:
762 case DW_CFA_def_cfa:
763 case DW_CFA_def_cfa_sf:
764 gcc_unreachable ();
766 default:
767 break;
770 /* Find the end of the chain. */
771 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
774 *p = cfi;
777 /* Generate a new label for the CFI info to refer to. FORCE is true
778 if a label needs to be output even when using .cfi_* directives. */
780 char *
781 dwarf2out_cfi_label (bool force)
783 static char label[20];
785 if (!force && dwarf2out_do_cfi_asm ())
787 /* In this case, we will be emitting the asm directive instead of
788 the label, so just return a placeholder to keep the rest of the
789 interfaces happy. */
790 strcpy (label, "<do not output>");
792 else
794 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
795 ASM_OUTPUT_LABEL (asm_out_file, label);
798 return label;
801 /* True if remember_state should be emitted before following CFI directive. */
802 static bool emit_cfa_remember;
804 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
805 or to the CIE if LABEL is NULL. */
807 static void
808 add_fde_cfi (const char *label, dw_cfi_ref cfi)
810 dw_cfi_ref *list_head;
812 if (emit_cfa_remember)
814 dw_cfi_ref cfi_remember;
816 /* Emit the state save. */
817 emit_cfa_remember = false;
818 cfi_remember = new_cfi ();
819 cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
820 add_fde_cfi (label, cfi_remember);
823 list_head = &cie_cfi_head;
825 if (dwarf2out_do_cfi_asm ())
827 if (label)
829 dw_fde_ref fde = current_fde ();
831 gcc_assert (fde != NULL);
833 /* We still have to add the cfi to the list so that lookup_cfa
834 works later on. When -g2 and above we even need to force
835 emitting of CFI labels and add to list a DW_CFA_set_loc for
836 convert_cfa_to_fb_loc_list purposes. If we're generating
837 DWARF3 output we use DW_OP_call_frame_cfa and so don't use
838 convert_cfa_to_fb_loc_list. */
839 if (dwarf_version == 2
840 && debug_info_level > DINFO_LEVEL_TERSE
841 && (write_symbols == DWARF2_DEBUG
842 || write_symbols == VMS_AND_DWARF2_DEBUG))
844 switch (cfi->dw_cfi_opc)
846 case DW_CFA_def_cfa_offset:
847 case DW_CFA_def_cfa_offset_sf:
848 case DW_CFA_def_cfa_register:
849 case DW_CFA_def_cfa:
850 case DW_CFA_def_cfa_sf:
851 case DW_CFA_def_cfa_expression:
852 case DW_CFA_restore_state:
853 if (*label == 0 || strcmp (label, "<do not output>") == 0)
854 label = dwarf2out_cfi_label (true);
856 if (fde->dw_fde_current_label == NULL
857 || strcmp (label, fde->dw_fde_current_label) != 0)
859 dw_cfi_ref xcfi;
861 label = xstrdup (label);
863 /* Set the location counter to the new label. */
864 xcfi = new_cfi ();
865 /* It doesn't metter whether DW_CFA_set_loc
866 or DW_CFA_advance_loc4 is added here, those aren't
867 emitted into assembly, only looked up by
868 convert_cfa_to_fb_loc_list. */
869 xcfi->dw_cfi_opc = DW_CFA_set_loc;
870 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
871 add_cfi (&fde->dw_fde_cfi, xcfi);
872 fde->dw_fde_current_label = label;
874 break;
875 default:
876 break;
880 output_cfi_directive (cfi);
882 list_head = &fde->dw_fde_cfi;
884 /* ??? If this is a CFI for the CIE, we don't emit. This
885 assumes that the standard CIE contents that the assembler
886 uses matches the standard CIE contents that the compiler
887 uses. This is probably a bad assumption. I'm not quite
888 sure how to address this for now. */
890 else if (label)
892 dw_fde_ref fde = current_fde ();
894 gcc_assert (fde != NULL);
896 if (*label == 0)
897 label = dwarf2out_cfi_label (false);
899 if (fde->dw_fde_current_label == NULL
900 || strcmp (label, fde->dw_fde_current_label) != 0)
902 dw_cfi_ref xcfi;
904 label = xstrdup (label);
906 /* Set the location counter to the new label. */
907 xcfi = new_cfi ();
908 /* If we have a current label, advance from there, otherwise
909 set the location directly using set_loc. */
910 xcfi->dw_cfi_opc = fde->dw_fde_current_label
911 ? DW_CFA_advance_loc4
912 : DW_CFA_set_loc;
913 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
914 add_cfi (&fde->dw_fde_cfi, xcfi);
916 fde->dw_fde_current_label = label;
919 list_head = &fde->dw_fde_cfi;
922 add_cfi (list_head, cfi);
925 /* Subroutine of lookup_cfa. */
927 static void
928 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
930 switch (cfi->dw_cfi_opc)
932 case DW_CFA_def_cfa_offset:
933 case DW_CFA_def_cfa_offset_sf:
934 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
935 break;
936 case DW_CFA_def_cfa_register:
937 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
938 break;
939 case DW_CFA_def_cfa:
940 case DW_CFA_def_cfa_sf:
941 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
942 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
943 break;
944 case DW_CFA_def_cfa_expression:
945 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
946 break;
948 case DW_CFA_remember_state:
949 gcc_assert (!remember->in_use);
950 *remember = *loc;
951 remember->in_use = 1;
952 break;
953 case DW_CFA_restore_state:
954 gcc_assert (remember->in_use);
955 *loc = *remember;
956 remember->in_use = 0;
957 break;
959 default:
960 break;
964 /* Find the previous value for the CFA. */
966 static void
967 lookup_cfa (dw_cfa_location *loc)
969 dw_cfi_ref cfi;
970 dw_fde_ref fde;
971 dw_cfa_location remember;
973 memset (loc, 0, sizeof (*loc));
974 loc->reg = INVALID_REGNUM;
975 remember = *loc;
977 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
978 lookup_cfa_1 (cfi, loc, &remember);
980 fde = current_fde ();
981 if (fde)
982 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
983 lookup_cfa_1 (cfi, loc, &remember);
986 /* The current rule for calculating the DWARF2 canonical frame address. */
987 static dw_cfa_location cfa;
989 /* The register used for saving registers to the stack, and its offset
990 from the CFA. */
991 static dw_cfa_location cfa_store;
993 /* The current save location around an epilogue. */
994 static dw_cfa_location cfa_remember;
996 /* The running total of the size of arguments pushed onto the stack. */
997 static HOST_WIDE_INT args_size;
999 /* The last args_size we actually output. */
1000 static HOST_WIDE_INT old_args_size;
1002 /* Entry point to update the canonical frame address (CFA).
1003 LABEL is passed to add_fde_cfi. The value of CFA is now to be
1004 calculated from REG+OFFSET. */
1006 void
1007 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1009 dw_cfa_location loc;
1010 loc.indirect = 0;
1011 loc.base_offset = 0;
1012 loc.reg = reg;
1013 loc.offset = offset;
1014 def_cfa_1 (label, &loc);
1017 /* Determine if two dw_cfa_location structures define the same data. */
1019 static bool
1020 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1022 return (loc1->reg == loc2->reg
1023 && loc1->offset == loc2->offset
1024 && loc1->indirect == loc2->indirect
1025 && (loc1->indirect == 0
1026 || loc1->base_offset == loc2->base_offset));
1029 /* This routine does the actual work. The CFA is now calculated from
1030 the dw_cfa_location structure. */
1032 static void
1033 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1035 dw_cfi_ref cfi;
1036 dw_cfa_location old_cfa, loc;
1038 cfa = *loc_p;
1039 loc = *loc_p;
1041 if (cfa_store.reg == loc.reg && loc.indirect == 0)
1042 cfa_store.offset = loc.offset;
1044 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1045 lookup_cfa (&old_cfa);
1047 /* If nothing changed, no need to issue any call frame instructions. */
1048 if (cfa_equal_p (&loc, &old_cfa))
1049 return;
1051 cfi = new_cfi ();
1053 if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1055 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1056 the CFA register did not change but the offset did. The data
1057 factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1058 in the assembler via the .cfi_def_cfa_offset directive. */
1059 if (loc.offset < 0)
1060 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1061 else
1062 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1063 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1066 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
1067 else if (loc.offset == old_cfa.offset
1068 && old_cfa.reg != INVALID_REGNUM
1069 && !loc.indirect
1070 && !old_cfa.indirect)
1072 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1073 indicating the CFA register has changed to <register> but the
1074 offset has not changed. */
1075 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1076 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1078 #endif
1080 else if (loc.indirect == 0)
1082 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1083 indicating the CFA register has changed to <register> with
1084 the specified offset. The data factoring for DW_CFA_def_cfa_sf
1085 happens in output_cfi, or in the assembler via the .cfi_def_cfa
1086 directive. */
1087 if (loc.offset < 0)
1088 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1089 else
1090 cfi->dw_cfi_opc = DW_CFA_def_cfa;
1091 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1092 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1094 else
1096 /* Construct a DW_CFA_def_cfa_expression instruction to
1097 calculate the CFA using a full location expression since no
1098 register-offset pair is available. */
1099 struct dw_loc_descr_struct *loc_list;
1101 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1102 loc_list = build_cfa_loc (&loc, 0);
1103 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1106 add_fde_cfi (label, cfi);
1109 /* Add the CFI for saving a register. REG is the CFA column number.
1110 LABEL is passed to add_fde_cfi.
1111 If SREG is -1, the register is saved at OFFSET from the CFA;
1112 otherwise it is saved in SREG. */
1114 static void
1115 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1117 dw_cfi_ref cfi = new_cfi ();
1118 dw_fde_ref fde = current_fde ();
1120 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1122 /* When stack is aligned, store REG using DW_CFA_expression with
1123 FP. */
1124 if (fde
1125 && fde->stack_realign
1126 && sreg == INVALID_REGNUM)
1128 cfi->dw_cfi_opc = DW_CFA_expression;
1129 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1130 cfi->dw_cfi_oprnd2.dw_cfi_loc
1131 = build_cfa_aligned_loc (offset, fde->stack_realignment);
1133 else if (sreg == INVALID_REGNUM)
1135 if (need_data_align_sf_opcode (offset))
1136 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1137 else if (reg & ~0x3f)
1138 cfi->dw_cfi_opc = DW_CFA_offset_extended;
1139 else
1140 cfi->dw_cfi_opc = DW_CFA_offset;
1141 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1143 else if (sreg == reg)
1144 cfi->dw_cfi_opc = DW_CFA_same_value;
1145 else
1147 cfi->dw_cfi_opc = DW_CFA_register;
1148 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1151 add_fde_cfi (label, cfi);
1154 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
1155 This CFI tells the unwinder that it needs to restore the window registers
1156 from the previous frame's window save area.
1158 ??? Perhaps we should note in the CIE where windows are saved (instead of
1159 assuming 0(cfa)) and what registers are in the window. */
1161 void
1162 dwarf2out_window_save (const char *label)
1164 dw_cfi_ref cfi = new_cfi ();
1166 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1167 add_fde_cfi (label, cfi);
1170 /* Entry point for saving a register to the stack. REG is the GCC register
1171 number. LABEL and OFFSET are passed to reg_save. */
1173 void
1174 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1176 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1179 /* Entry point for saving the return address in the stack.
1180 LABEL and OFFSET are passed to reg_save. */
1182 void
1183 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1185 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1188 /* Entry point for saving the return address in a register.
1189 LABEL and SREG are passed to reg_save. */
1191 void
1192 dwarf2out_return_reg (const char *label, unsigned int sreg)
1194 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1197 /* Record the initial position of the return address. RTL is
1198 INCOMING_RETURN_ADDR_RTX. */
1200 static void
1201 initial_return_save (rtx rtl)
1203 unsigned int reg = INVALID_REGNUM;
1204 HOST_WIDE_INT offset = 0;
1206 switch (GET_CODE (rtl))
1208 case REG:
1209 /* RA is in a register. */
1210 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1211 break;
1213 case MEM:
1214 /* RA is on the stack. */
1215 rtl = XEXP (rtl, 0);
1216 switch (GET_CODE (rtl))
1218 case REG:
1219 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1220 offset = 0;
1221 break;
1223 case PLUS:
1224 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1225 offset = INTVAL (XEXP (rtl, 1));
1226 break;
1228 case MINUS:
1229 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1230 offset = -INTVAL (XEXP (rtl, 1));
1231 break;
1233 default:
1234 gcc_unreachable ();
1237 break;
1239 case PLUS:
1240 /* The return address is at some offset from any value we can
1241 actually load. For instance, on the SPARC it is in %i7+8. Just
1242 ignore the offset for now; it doesn't matter for unwinding frames. */
1243 gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1244 initial_return_save (XEXP (rtl, 0));
1245 return;
1247 default:
1248 gcc_unreachable ();
1251 if (reg != DWARF_FRAME_RETURN_COLUMN)
1252 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1255 /* Given a SET, calculate the amount of stack adjustment it
1256 contains. */
1258 static HOST_WIDE_INT
1259 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1260 HOST_WIDE_INT cur_offset)
1262 const_rtx src = SET_SRC (pattern);
1263 const_rtx dest = SET_DEST (pattern);
1264 HOST_WIDE_INT offset = 0;
1265 enum rtx_code code;
1267 if (dest == stack_pointer_rtx)
1269 code = GET_CODE (src);
1271 /* Assume (set (reg sp) (reg whatever)) sets args_size
1272 level to 0. */
1273 if (code == REG && src != stack_pointer_rtx)
1275 offset = -cur_args_size;
1276 #ifndef STACK_GROWS_DOWNWARD
1277 offset = -offset;
1278 #endif
1279 return offset - cur_offset;
1282 if (! (code == PLUS || code == MINUS)
1283 || XEXP (src, 0) != stack_pointer_rtx
1284 || !CONST_INT_P (XEXP (src, 1)))
1285 return 0;
1287 /* (set (reg sp) (plus (reg sp) (const_int))) */
1288 offset = INTVAL (XEXP (src, 1));
1289 if (code == PLUS)
1290 offset = -offset;
1291 return offset;
1294 if (MEM_P (src) && !MEM_P (dest))
1295 dest = src;
1296 if (MEM_P (dest))
1298 /* (set (mem (pre_dec (reg sp))) (foo)) */
1299 src = XEXP (dest, 0);
1300 code = GET_CODE (src);
1302 switch (code)
1304 case PRE_MODIFY:
1305 case POST_MODIFY:
1306 if (XEXP (src, 0) == stack_pointer_rtx)
1308 rtx val = XEXP (XEXP (src, 1), 1);
1309 /* We handle only adjustments by constant amount. */
1310 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1311 && CONST_INT_P (val));
1312 offset = -INTVAL (val);
1313 break;
1315 return 0;
1317 case PRE_DEC:
1318 case POST_DEC:
1319 if (XEXP (src, 0) == stack_pointer_rtx)
1321 offset = GET_MODE_SIZE (GET_MODE (dest));
1322 break;
1324 return 0;
1326 case PRE_INC:
1327 case POST_INC:
1328 if (XEXP (src, 0) == stack_pointer_rtx)
1330 offset = -GET_MODE_SIZE (GET_MODE (dest));
1331 break;
1333 return 0;
1335 default:
1336 return 0;
1339 else
1340 return 0;
1342 return offset;
1345 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1346 indexed by INSN_UID. */
1348 static HOST_WIDE_INT *barrier_args_size;
1350 /* Helper function for compute_barrier_args_size. Handle one insn. */
1352 static HOST_WIDE_INT
1353 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1354 VEC (rtx, heap) **next)
1356 HOST_WIDE_INT offset = 0;
1357 int i;
1359 if (! RTX_FRAME_RELATED_P (insn))
1361 if (prologue_epilogue_contains (insn))
1362 /* Nothing */;
1363 else if (GET_CODE (PATTERN (insn)) == SET)
1364 offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1365 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1366 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1368 /* There may be stack adjustments inside compound insns. Search
1369 for them. */
1370 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1371 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1372 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1373 cur_args_size, offset);
1376 else
1378 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1380 if (expr)
1382 expr = XEXP (expr, 0);
1383 if (GET_CODE (expr) == PARALLEL
1384 || GET_CODE (expr) == SEQUENCE)
1385 for (i = 1; i < XVECLEN (expr, 0); i++)
1387 rtx elem = XVECEXP (expr, 0, i);
1389 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1390 offset += stack_adjust_offset (elem, cur_args_size, offset);
1395 #ifndef STACK_GROWS_DOWNWARD
1396 offset = -offset;
1397 #endif
1399 cur_args_size += offset;
1400 if (cur_args_size < 0)
1401 cur_args_size = 0;
1403 if (JUMP_P (insn))
1405 rtx dest = JUMP_LABEL (insn);
1407 if (dest)
1409 if (barrier_args_size [INSN_UID (dest)] < 0)
1411 barrier_args_size [INSN_UID (dest)] = cur_args_size;
1412 VEC_safe_push (rtx, heap, *next, dest);
1417 return cur_args_size;
1420 /* Walk the whole function and compute args_size on BARRIERs. */
1422 static void
1423 compute_barrier_args_size (void)
1425 int max_uid = get_max_uid (), i;
1426 rtx insn;
1427 VEC (rtx, heap) *worklist, *next, *tmp;
1429 barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1430 for (i = 0; i < max_uid; i++)
1431 barrier_args_size[i] = -1;
1433 worklist = VEC_alloc (rtx, heap, 20);
1434 next = VEC_alloc (rtx, heap, 20);
1435 insn = get_insns ();
1436 barrier_args_size[INSN_UID (insn)] = 0;
1437 VEC_quick_push (rtx, worklist, insn);
1438 for (;;)
1440 while (!VEC_empty (rtx, worklist))
1442 rtx prev, body, first_insn;
1443 HOST_WIDE_INT cur_args_size;
1445 first_insn = insn = VEC_pop (rtx, worklist);
1446 cur_args_size = barrier_args_size[INSN_UID (insn)];
1447 prev = prev_nonnote_insn (insn);
1448 if (prev && BARRIER_P (prev))
1449 barrier_args_size[INSN_UID (prev)] = cur_args_size;
1451 for (; insn; insn = NEXT_INSN (insn))
1453 if (INSN_DELETED_P (insn) || NOTE_P (insn))
1454 continue;
1455 if (BARRIER_P (insn))
1456 break;
1458 if (LABEL_P (insn))
1460 if (insn == first_insn)
1461 continue;
1462 else if (barrier_args_size[INSN_UID (insn)] < 0)
1464 barrier_args_size[INSN_UID (insn)] = cur_args_size;
1465 continue;
1467 else
1469 /* The insns starting with this label have been
1470 already scanned or are in the worklist. */
1471 break;
1475 body = PATTERN (insn);
1476 if (GET_CODE (body) == SEQUENCE)
1478 HOST_WIDE_INT dest_args_size = cur_args_size;
1479 for (i = 1; i < XVECLEN (body, 0); i++)
1480 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1481 && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1482 dest_args_size
1483 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1484 dest_args_size, &next);
1485 else
1486 cur_args_size
1487 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1488 cur_args_size, &next);
1490 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1491 compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1492 dest_args_size, &next);
1493 else
1494 cur_args_size
1495 = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1496 cur_args_size, &next);
1498 else
1499 cur_args_size
1500 = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1504 if (VEC_empty (rtx, next))
1505 break;
1507 /* Swap WORKLIST with NEXT and truncate NEXT for next iteration. */
1508 tmp = next;
1509 next = worklist;
1510 worklist = tmp;
1511 VEC_truncate (rtx, next, 0);
1514 VEC_free (rtx, heap, worklist);
1515 VEC_free (rtx, heap, next);
1518 /* Add a CFI to update the running total of the size of arguments
1519 pushed onto the stack. */
1521 static void
1522 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1524 dw_cfi_ref cfi;
1526 if (size == old_args_size)
1527 return;
1529 old_args_size = size;
1531 cfi = new_cfi ();
1532 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1533 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1534 add_fde_cfi (label, cfi);
1537 /* Record a stack adjustment of OFFSET bytes. */
1539 static void
1540 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1542 if (cfa.reg == STACK_POINTER_REGNUM)
1543 cfa.offset += offset;
1545 if (cfa_store.reg == STACK_POINTER_REGNUM)
1546 cfa_store.offset += offset;
1548 if (ACCUMULATE_OUTGOING_ARGS)
1549 return;
1551 #ifndef STACK_GROWS_DOWNWARD
1552 offset = -offset;
1553 #endif
1555 args_size += offset;
1556 if (args_size < 0)
1557 args_size = 0;
1559 def_cfa_1 (label, &cfa);
1560 if (flag_asynchronous_unwind_tables)
1561 dwarf2out_args_size (label, args_size);
1564 /* Check INSN to see if it looks like a push or a stack adjustment, and
1565 make a note of it if it does. EH uses this information to find out
1566 how much extra space it needs to pop off the stack. */
1568 static void
1569 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1571 HOST_WIDE_INT offset;
1572 const char *label;
1573 int i;
1575 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1576 with this function. Proper support would require all frame-related
1577 insns to be marked, and to be able to handle saving state around
1578 epilogues textually in the middle of the function. */
1579 if (prologue_epilogue_contains (insn))
1580 return;
1582 /* If INSN is an instruction from target of an annulled branch, the
1583 effects are for the target only and so current argument size
1584 shouldn't change at all. */
1585 if (final_sequence
1586 && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1587 && INSN_FROM_TARGET_P (insn))
1588 return;
1590 /* If only calls can throw, and we have a frame pointer,
1591 save up adjustments until we see the CALL_INSN. */
1592 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1594 if (CALL_P (insn) && !after_p)
1596 /* Extract the size of the args from the CALL rtx itself. */
1597 insn = PATTERN (insn);
1598 if (GET_CODE (insn) == PARALLEL)
1599 insn = XVECEXP (insn, 0, 0);
1600 if (GET_CODE (insn) == SET)
1601 insn = SET_SRC (insn);
1602 gcc_assert (GET_CODE (insn) == CALL);
1603 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1605 return;
1608 if (CALL_P (insn) && !after_p)
1610 if (!flag_asynchronous_unwind_tables)
1611 dwarf2out_args_size ("", args_size);
1612 return;
1614 else if (BARRIER_P (insn))
1616 /* Don't call compute_barrier_args_size () if the only
1617 BARRIER is at the end of function. */
1618 if (barrier_args_size == NULL && next_nonnote_insn (insn))
1619 compute_barrier_args_size ();
1620 if (barrier_args_size == NULL)
1621 offset = 0;
1622 else
1624 offset = barrier_args_size[INSN_UID (insn)];
1625 if (offset < 0)
1626 offset = 0;
1629 offset -= args_size;
1630 #ifndef STACK_GROWS_DOWNWARD
1631 offset = -offset;
1632 #endif
1634 else if (GET_CODE (PATTERN (insn)) == SET)
1635 offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1636 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1637 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1639 /* There may be stack adjustments inside compound insns. Search
1640 for them. */
1641 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1642 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1643 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1644 args_size, offset);
1646 else
1647 return;
1649 if (offset == 0)
1650 return;
1652 label = dwarf2out_cfi_label (false);
1653 dwarf2out_stack_adjust (offset, label);
1656 /* We delay emitting a register save until either (a) we reach the end
1657 of the prologue or (b) the register is clobbered. This clusters
1658 register saves so that there are fewer pc advances. */
1660 struct GTY(()) queued_reg_save {
1661 struct queued_reg_save *next;
1662 rtx reg;
1663 HOST_WIDE_INT cfa_offset;
1664 rtx saved_reg;
1667 static GTY(()) struct queued_reg_save *queued_reg_saves;
1669 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1670 struct GTY(()) reg_saved_in_data {
1671 rtx orig_reg;
1672 rtx saved_in_reg;
1675 /* A list of registers saved in other registers.
1676 The list intentionally has a small maximum capacity of 4; if your
1677 port needs more than that, you might consider implementing a
1678 more efficient data structure. */
1679 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1680 static GTY(()) size_t num_regs_saved_in_regs;
1682 static const char *last_reg_save_label;
1684 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1685 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1687 static void
1688 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1690 struct queued_reg_save *q;
1692 /* Duplicates waste space, but it's also necessary to remove them
1693 for correctness, since the queue gets output in reverse
1694 order. */
1695 for (q = queued_reg_saves; q != NULL; q = q->next)
1696 if (REGNO (q->reg) == REGNO (reg))
1697 break;
1699 if (q == NULL)
1701 q = ggc_alloc_queued_reg_save ();
1702 q->next = queued_reg_saves;
1703 queued_reg_saves = q;
1706 q->reg = reg;
1707 q->cfa_offset = offset;
1708 q->saved_reg = sreg;
1710 last_reg_save_label = label;
1713 /* Output all the entries in QUEUED_REG_SAVES. */
1715 void
1716 dwarf2out_flush_queued_reg_saves (void)
1718 struct queued_reg_save *q;
1720 for (q = queued_reg_saves; q; q = q->next)
1722 size_t i;
1723 unsigned int reg, sreg;
1725 for (i = 0; i < num_regs_saved_in_regs; i++)
1726 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1727 break;
1728 if (q->saved_reg && i == num_regs_saved_in_regs)
1730 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1731 num_regs_saved_in_regs++;
1733 if (i != num_regs_saved_in_regs)
1735 regs_saved_in_regs[i].orig_reg = q->reg;
1736 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1739 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1740 if (q->saved_reg)
1741 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1742 else
1743 sreg = INVALID_REGNUM;
1744 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1747 queued_reg_saves = NULL;
1748 last_reg_save_label = NULL;
1751 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1752 location for? Or, does it clobber a register which we've previously
1753 said that some other register is saved in, and for which we now
1754 have a new location for? */
1756 static bool
1757 clobbers_queued_reg_save (const_rtx insn)
1759 struct queued_reg_save *q;
1761 for (q = queued_reg_saves; q; q = q->next)
1763 size_t i;
1764 if (modified_in_p (q->reg, insn))
1765 return true;
1766 for (i = 0; i < num_regs_saved_in_regs; i++)
1767 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1768 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1769 return true;
1772 return false;
1775 /* Entry point for saving the first register into the second. */
1777 void
1778 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1780 size_t i;
1781 unsigned int regno, sregno;
1783 for (i = 0; i < num_regs_saved_in_regs; i++)
1784 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1785 break;
1786 if (i == num_regs_saved_in_regs)
1788 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1789 num_regs_saved_in_regs++;
1791 regs_saved_in_regs[i].orig_reg = reg;
1792 regs_saved_in_regs[i].saved_in_reg = sreg;
1794 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1795 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1796 reg_save (label, regno, sregno, 0);
1799 /* What register, if any, is currently saved in REG? */
1801 static rtx
1802 reg_saved_in (rtx reg)
1804 unsigned int regn = REGNO (reg);
1805 size_t i;
1806 struct queued_reg_save *q;
1808 for (q = queued_reg_saves; q; q = q->next)
1809 if (q->saved_reg && regn == REGNO (q->saved_reg))
1810 return q->reg;
1812 for (i = 0; i < num_regs_saved_in_regs; i++)
1813 if (regs_saved_in_regs[i].saved_in_reg
1814 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1815 return regs_saved_in_regs[i].orig_reg;
1817 return NULL_RTX;
1821 /* A temporary register holding an integral value used in adjusting SP
1822 or setting up the store_reg. The "offset" field holds the integer
1823 value, not an offset. */
1824 static dw_cfa_location cfa_temp;
1826 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note. */
1828 static void
1829 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1831 memset (&cfa, 0, sizeof (cfa));
1833 switch (GET_CODE (pat))
1835 case PLUS:
1836 cfa.reg = REGNO (XEXP (pat, 0));
1837 cfa.offset = INTVAL (XEXP (pat, 1));
1838 break;
1840 case REG:
1841 cfa.reg = REGNO (pat);
1842 break;
1844 case MEM:
1845 cfa.indirect = 1;
1846 pat = XEXP (pat, 0);
1847 if (GET_CODE (pat) == PLUS)
1849 cfa.base_offset = INTVAL (XEXP (pat, 1));
1850 pat = XEXP (pat, 0);
1852 cfa.reg = REGNO (pat);
1853 break;
1855 default:
1856 /* Recurse and define an expression. */
1857 gcc_unreachable ();
1860 def_cfa_1 (label, &cfa);
1863 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note. */
1865 static void
1866 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1868 rtx src, dest;
1870 gcc_assert (GET_CODE (pat) == SET);
1871 dest = XEXP (pat, 0);
1872 src = XEXP (pat, 1);
1874 switch (GET_CODE (src))
1876 case PLUS:
1877 gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1878 cfa.offset -= INTVAL (XEXP (src, 1));
1879 break;
1881 case REG:
1882 break;
1884 default:
1885 gcc_unreachable ();
1888 cfa.reg = REGNO (dest);
1889 gcc_assert (cfa.indirect == 0);
1891 def_cfa_1 (label, &cfa);
1894 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note. */
1896 static void
1897 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1899 HOST_WIDE_INT offset;
1900 rtx src, addr, span;
1902 src = XEXP (set, 1);
1903 addr = XEXP (set, 0);
1904 gcc_assert (MEM_P (addr));
1905 addr = XEXP (addr, 0);
1907 /* As documented, only consider extremely simple addresses. */
1908 switch (GET_CODE (addr))
1910 case REG:
1911 gcc_assert (REGNO (addr) == cfa.reg);
1912 offset = -cfa.offset;
1913 break;
1914 case PLUS:
1915 gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1916 offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1917 break;
1918 default:
1919 gcc_unreachable ();
1922 span = targetm.dwarf_register_span (src);
1924 /* ??? We'd like to use queue_reg_save, but we need to come up with
1925 a different flushing heuristic for epilogues. */
1926 if (!span)
1927 reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1928 else
1930 /* We have a PARALLEL describing where the contents of SRC live.
1931 Queue register saves for each piece of the PARALLEL. */
1932 int par_index;
1933 int limit;
1934 HOST_WIDE_INT span_offset = offset;
1936 gcc_assert (GET_CODE (span) == PARALLEL);
1938 limit = XVECLEN (span, 0);
1939 for (par_index = 0; par_index < limit; par_index++)
1941 rtx elem = XVECEXP (span, 0, par_index);
1943 reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1944 INVALID_REGNUM, span_offset);
1945 span_offset += GET_MODE_SIZE (GET_MODE (elem));
1950 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note. */
1952 static void
1953 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1955 rtx src, dest;
1956 unsigned sregno, dregno;
1958 src = XEXP (set, 1);
1959 dest = XEXP (set, 0);
1961 if (src == pc_rtx)
1962 sregno = DWARF_FRAME_RETURN_COLUMN;
1963 else
1964 sregno = DWARF_FRAME_REGNUM (REGNO (src));
1966 dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1968 /* ??? We'd like to use queue_reg_save, but we need to come up with
1969 a different flushing heuristic for epilogues. */
1970 reg_save (label, sregno, dregno, 0);
1973 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
1975 static void
1976 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
1978 rtx src, dest, span;
1979 dw_cfi_ref cfi = new_cfi ();
1981 dest = SET_DEST (set);
1982 src = SET_SRC (set);
1984 gcc_assert (REG_P (src));
1985 gcc_assert (MEM_P (dest));
1987 span = targetm.dwarf_register_span (src);
1988 gcc_assert (!span);
1990 cfi->dw_cfi_opc = DW_CFA_expression;
1991 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
1992 cfi->dw_cfi_oprnd2.dw_cfi_loc
1993 = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
1994 VAR_INIT_STATUS_INITIALIZED);
1996 /* ??? We'd like to use queue_reg_save, were the interface different,
1997 and, as above, we could manage flushing for epilogues. */
1998 add_fde_cfi (label, cfi);
2001 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note. */
2003 static void
2004 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2006 dw_cfi_ref cfi = new_cfi ();
2007 unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2009 cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2010 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2012 add_fde_cfi (label, cfi);
2015 /* Record call frame debugging information for an expression EXPR,
2016 which either sets SP or FP (adjusting how we calculate the frame
2017 address) or saves a register to the stack or another register.
2018 LABEL indicates the address of EXPR.
2020 This function encodes a state machine mapping rtxes to actions on
2021 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
2022 users need not read the source code.
2024 The High-Level Picture
2026 Changes in the register we use to calculate the CFA: Currently we
2027 assume that if you copy the CFA register into another register, we
2028 should take the other one as the new CFA register; this seems to
2029 work pretty well. If it's wrong for some target, it's simple
2030 enough not to set RTX_FRAME_RELATED_P on the insn in question.
2032 Changes in the register we use for saving registers to the stack:
2033 This is usually SP, but not always. Again, we deduce that if you
2034 copy SP into another register (and SP is not the CFA register),
2035 then the new register is the one we will be using for register
2036 saves. This also seems to work.
2038 Register saves: There's not much guesswork about this one; if
2039 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2040 register save, and the register used to calculate the destination
2041 had better be the one we think we're using for this purpose.
2042 It's also assumed that a copy from a call-saved register to another
2043 register is saving that register if RTX_FRAME_RELATED_P is set on
2044 that instruction. If the copy is from a call-saved register to
2045 the *same* register, that means that the register is now the same
2046 value as in the caller.
2048 Except: If the register being saved is the CFA register, and the
2049 offset is nonzero, we are saving the CFA, so we assume we have to
2050 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
2051 the intent is to save the value of SP from the previous frame.
2053 In addition, if a register has previously been saved to a different
2054 register,
2056 Invariants / Summaries of Rules
2058 cfa current rule for calculating the CFA. It usually
2059 consists of a register and an offset.
2060 cfa_store register used by prologue code to save things to the stack
2061 cfa_store.offset is the offset from the value of
2062 cfa_store.reg to the actual CFA
2063 cfa_temp register holding an integral value. cfa_temp.offset
2064 stores the value, which will be used to adjust the
2065 stack pointer. cfa_temp is also used like cfa_store,
2066 to track stores to the stack via fp or a temp reg.
2068 Rules 1- 4: Setting a register's value to cfa.reg or an expression
2069 with cfa.reg as the first operand changes the cfa.reg and its
2070 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
2071 cfa_temp.offset.
2073 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
2074 expression yielding a constant. This sets cfa_temp.reg
2075 and cfa_temp.offset.
2077 Rule 5: Create a new register cfa_store used to save items to the
2078 stack.
2080 Rules 10-14: Save a register to the stack. Define offset as the
2081 difference of the original location and cfa_store's
2082 location (or cfa_temp's location if cfa_temp is used).
2084 Rules 16-20: If AND operation happens on sp in prologue, we assume
2085 stack is realigned. We will use a group of DW_OP_XXX
2086 expressions to represent the location of the stored
2087 register instead of CFA+offset.
2089 The Rules
2091 "{a,b}" indicates a choice of a xor b.
2092 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2094 Rule 1:
2095 (set <reg1> <reg2>:cfa.reg)
2096 effects: cfa.reg = <reg1>
2097 cfa.offset unchanged
2098 cfa_temp.reg = <reg1>
2099 cfa_temp.offset = cfa.offset
2101 Rule 2:
2102 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2103 {<const_int>,<reg>:cfa_temp.reg}))
2104 effects: cfa.reg = sp if fp used
2105 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2106 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2107 if cfa_store.reg==sp
2109 Rule 3:
2110 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2111 effects: cfa.reg = fp
2112 cfa_offset += +/- <const_int>
2114 Rule 4:
2115 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2116 constraints: <reg1> != fp
2117 <reg1> != sp
2118 effects: cfa.reg = <reg1>
2119 cfa_temp.reg = <reg1>
2120 cfa_temp.offset = cfa.offset
2122 Rule 5:
2123 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2124 constraints: <reg1> != fp
2125 <reg1> != sp
2126 effects: cfa_store.reg = <reg1>
2127 cfa_store.offset = cfa.offset - cfa_temp.offset
2129 Rule 6:
2130 (set <reg> <const_int>)
2131 effects: cfa_temp.reg = <reg>
2132 cfa_temp.offset = <const_int>
2134 Rule 7:
2135 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2136 effects: cfa_temp.reg = <reg1>
2137 cfa_temp.offset |= <const_int>
2139 Rule 8:
2140 (set <reg> (high <exp>))
2141 effects: none
2143 Rule 9:
2144 (set <reg> (lo_sum <exp> <const_int>))
2145 effects: cfa_temp.reg = <reg>
2146 cfa_temp.offset = <const_int>
2148 Rule 10:
2149 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2150 effects: cfa_store.offset -= <const_int>
2151 cfa.offset = cfa_store.offset if cfa.reg == sp
2152 cfa.reg = sp
2153 cfa.base_offset = -cfa_store.offset
2155 Rule 11:
2156 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2157 effects: cfa_store.offset += -/+ mode_size(mem)
2158 cfa.offset = cfa_store.offset if cfa.reg == sp
2159 cfa.reg = sp
2160 cfa.base_offset = -cfa_store.offset
2162 Rule 12:
2163 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2165 <reg2>)
2166 effects: cfa.reg = <reg1>
2167 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2169 Rule 13:
2170 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2171 effects: cfa.reg = <reg1>
2172 cfa.base_offset = -{cfa_store,cfa_temp}.offset
2174 Rule 14:
2175 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2176 effects: cfa.reg = <reg1>
2177 cfa.base_offset = -cfa_temp.offset
2178 cfa_temp.offset -= mode_size(mem)
2180 Rule 15:
2181 (set <reg> {unspec, unspec_volatile})
2182 effects: target-dependent
2184 Rule 16:
2185 (set sp (and: sp <const_int>))
2186 constraints: cfa_store.reg == sp
2187 effects: current_fde.stack_realign = 1
2188 cfa_store.offset = 0
2189 fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2191 Rule 17:
2192 (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2193 effects: cfa_store.offset += -/+ mode_size(mem)
2195 Rule 18:
2196 (set (mem ({pre_inc, pre_dec} sp)) fp)
2197 constraints: fde->stack_realign == 1
2198 effects: cfa_store.offset = 0
2199 cfa.reg != HARD_FRAME_POINTER_REGNUM
2201 Rule 19:
2202 (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2203 constraints: fde->stack_realign == 1
2204 && cfa.offset == 0
2205 && cfa.indirect == 0
2206 && cfa.reg != HARD_FRAME_POINTER_REGNUM
2207 effects: Use DW_CFA_def_cfa_expression to define cfa
2208 cfa.reg == fde->drap_reg */
2210 static void
2211 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2213 rtx src, dest, span;
2214 HOST_WIDE_INT offset;
2215 dw_fde_ref fde;
2217 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2218 the PARALLEL independently. The first element is always processed if
2219 it is a SET. This is for backward compatibility. Other elements
2220 are processed only if they are SETs and the RTX_FRAME_RELATED_P
2221 flag is set in them. */
2222 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2224 int par_index;
2225 int limit = XVECLEN (expr, 0);
2226 rtx elem;
2228 /* PARALLELs have strict read-modify-write semantics, so we
2229 ought to evaluate every rvalue before changing any lvalue.
2230 It's cumbersome to do that in general, but there's an
2231 easy approximation that is enough for all current users:
2232 handle register saves before register assignments. */
2233 if (GET_CODE (expr) == PARALLEL)
2234 for (par_index = 0; par_index < limit; par_index++)
2236 elem = XVECEXP (expr, 0, par_index);
2237 if (GET_CODE (elem) == SET
2238 && MEM_P (SET_DEST (elem))
2239 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2240 dwarf2out_frame_debug_expr (elem, label);
2243 for (par_index = 0; par_index < limit; par_index++)
2245 elem = XVECEXP (expr, 0, par_index);
2246 if (GET_CODE (elem) == SET
2247 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2248 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2249 dwarf2out_frame_debug_expr (elem, label);
2250 else if (GET_CODE (elem) == SET
2251 && par_index != 0
2252 && !RTX_FRAME_RELATED_P (elem))
2254 /* Stack adjustment combining might combine some post-prologue
2255 stack adjustment into a prologue stack adjustment. */
2256 HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2258 if (offset != 0)
2259 dwarf2out_stack_adjust (offset, label);
2262 return;
2265 gcc_assert (GET_CODE (expr) == SET);
2267 src = SET_SRC (expr);
2268 dest = SET_DEST (expr);
2270 if (REG_P (src))
2272 rtx rsi = reg_saved_in (src);
2273 if (rsi)
2274 src = rsi;
2277 fde = current_fde ();
2279 switch (GET_CODE (dest))
2281 case REG:
2282 switch (GET_CODE (src))
2284 /* Setting FP from SP. */
2285 case REG:
2286 if (cfa.reg == (unsigned) REGNO (src))
2288 /* Rule 1 */
2289 /* Update the CFA rule wrt SP or FP. Make sure src is
2290 relative to the current CFA register.
2292 We used to require that dest be either SP or FP, but the
2293 ARM copies SP to a temporary register, and from there to
2294 FP. So we just rely on the backends to only set
2295 RTX_FRAME_RELATED_P on appropriate insns. */
2296 cfa.reg = REGNO (dest);
2297 cfa_temp.reg = cfa.reg;
2298 cfa_temp.offset = cfa.offset;
2300 else
2302 /* Saving a register in a register. */
2303 gcc_assert (!fixed_regs [REGNO (dest)]
2304 /* For the SPARC and its register window. */
2305 || (DWARF_FRAME_REGNUM (REGNO (src))
2306 == DWARF_FRAME_RETURN_COLUMN));
2308 /* After stack is aligned, we can only save SP in FP
2309 if drap register is used. In this case, we have
2310 to restore stack pointer with the CFA value and we
2311 don't generate this DWARF information. */
2312 if (fde
2313 && fde->stack_realign
2314 && REGNO (src) == STACK_POINTER_REGNUM)
2315 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2316 && fde->drap_reg != INVALID_REGNUM
2317 && cfa.reg != REGNO (src));
2318 else
2319 queue_reg_save (label, src, dest, 0);
2321 break;
2323 case PLUS:
2324 case MINUS:
2325 case LO_SUM:
2326 if (dest == stack_pointer_rtx)
2328 /* Rule 2 */
2329 /* Adjusting SP. */
2330 switch (GET_CODE (XEXP (src, 1)))
2332 case CONST_INT:
2333 offset = INTVAL (XEXP (src, 1));
2334 break;
2335 case REG:
2336 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2337 == cfa_temp.reg);
2338 offset = cfa_temp.offset;
2339 break;
2340 default:
2341 gcc_unreachable ();
2344 if (XEXP (src, 0) == hard_frame_pointer_rtx)
2346 /* Restoring SP from FP in the epilogue. */
2347 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2348 cfa.reg = STACK_POINTER_REGNUM;
2350 else if (GET_CODE (src) == LO_SUM)
2351 /* Assume we've set the source reg of the LO_SUM from sp. */
2353 else
2354 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2356 if (GET_CODE (src) != MINUS)
2357 offset = -offset;
2358 if (cfa.reg == STACK_POINTER_REGNUM)
2359 cfa.offset += offset;
2360 if (cfa_store.reg == STACK_POINTER_REGNUM)
2361 cfa_store.offset += offset;
2363 else if (dest == hard_frame_pointer_rtx)
2365 /* Rule 3 */
2366 /* Either setting the FP from an offset of the SP,
2367 or adjusting the FP */
2368 gcc_assert (frame_pointer_needed);
2370 gcc_assert (REG_P (XEXP (src, 0))
2371 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2372 && CONST_INT_P (XEXP (src, 1)));
2373 offset = INTVAL (XEXP (src, 1));
2374 if (GET_CODE (src) != MINUS)
2375 offset = -offset;
2376 cfa.offset += offset;
2377 cfa.reg = HARD_FRAME_POINTER_REGNUM;
2379 else
2381 gcc_assert (GET_CODE (src) != MINUS);
2383 /* Rule 4 */
2384 if (REG_P (XEXP (src, 0))
2385 && REGNO (XEXP (src, 0)) == cfa.reg
2386 && CONST_INT_P (XEXP (src, 1)))
2388 /* Setting a temporary CFA register that will be copied
2389 into the FP later on. */
2390 offset = - INTVAL (XEXP (src, 1));
2391 cfa.offset += offset;
2392 cfa.reg = REGNO (dest);
2393 /* Or used to save regs to the stack. */
2394 cfa_temp.reg = cfa.reg;
2395 cfa_temp.offset = cfa.offset;
2398 /* Rule 5 */
2399 else if (REG_P (XEXP (src, 0))
2400 && REGNO (XEXP (src, 0)) == cfa_temp.reg
2401 && XEXP (src, 1) == stack_pointer_rtx)
2403 /* Setting a scratch register that we will use instead
2404 of SP for saving registers to the stack. */
2405 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2406 cfa_store.reg = REGNO (dest);
2407 cfa_store.offset = cfa.offset - cfa_temp.offset;
2410 /* Rule 9 */
2411 else if (GET_CODE (src) == LO_SUM
2412 && CONST_INT_P (XEXP (src, 1)))
2414 cfa_temp.reg = REGNO (dest);
2415 cfa_temp.offset = INTVAL (XEXP (src, 1));
2417 else
2418 gcc_unreachable ();
2420 break;
2422 /* Rule 6 */
2423 case CONST_INT:
2424 cfa_temp.reg = REGNO (dest);
2425 cfa_temp.offset = INTVAL (src);
2426 break;
2428 /* Rule 7 */
2429 case IOR:
2430 gcc_assert (REG_P (XEXP (src, 0))
2431 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2432 && CONST_INT_P (XEXP (src, 1)));
2434 if ((unsigned) REGNO (dest) != cfa_temp.reg)
2435 cfa_temp.reg = REGNO (dest);
2436 cfa_temp.offset |= INTVAL (XEXP (src, 1));
2437 break;
2439 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2440 which will fill in all of the bits. */
2441 /* Rule 8 */
2442 case HIGH:
2443 break;
2445 /* Rule 15 */
2446 case UNSPEC:
2447 case UNSPEC_VOLATILE:
2448 gcc_assert (targetm.dwarf_handle_frame_unspec);
2449 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2450 return;
2452 /* Rule 16 */
2453 case AND:
2454 /* If this AND operation happens on stack pointer in prologue,
2455 we assume the stack is realigned and we extract the
2456 alignment. */
2457 if (fde && XEXP (src, 0) == stack_pointer_rtx)
2459 /* We interpret reg_save differently with stack_realign set.
2460 Thus we must flush whatever we have queued first. */
2461 dwarf2out_flush_queued_reg_saves ();
2463 gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2464 fde->stack_realign = 1;
2465 fde->stack_realignment = INTVAL (XEXP (src, 1));
2466 cfa_store.offset = 0;
2468 if (cfa.reg != STACK_POINTER_REGNUM
2469 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2470 fde->drap_reg = cfa.reg;
2472 return;
2474 default:
2475 gcc_unreachable ();
2478 def_cfa_1 (label, &cfa);
2479 break;
2481 case MEM:
2483 /* Saving a register to the stack. Make sure dest is relative to the
2484 CFA register. */
2485 switch (GET_CODE (XEXP (dest, 0)))
2487 /* Rule 10 */
2488 /* With a push. */
2489 case PRE_MODIFY:
2490 /* We can't handle variable size modifications. */
2491 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2492 == CONST_INT);
2493 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2495 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2496 && cfa_store.reg == STACK_POINTER_REGNUM);
2498 cfa_store.offset += offset;
2499 if (cfa.reg == STACK_POINTER_REGNUM)
2500 cfa.offset = cfa_store.offset;
2502 offset = -cfa_store.offset;
2503 break;
2505 /* Rule 11 */
2506 case PRE_INC:
2507 case PRE_DEC:
2508 offset = GET_MODE_SIZE (GET_MODE (dest));
2509 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2510 offset = -offset;
2512 gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2513 == STACK_POINTER_REGNUM)
2514 && cfa_store.reg == STACK_POINTER_REGNUM);
2516 cfa_store.offset += offset;
2518 /* Rule 18: If stack is aligned, we will use FP as a
2519 reference to represent the address of the stored
2520 regiser. */
2521 if (fde
2522 && fde->stack_realign
2523 && src == hard_frame_pointer_rtx)
2525 gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2526 cfa_store.offset = 0;
2529 if (cfa.reg == STACK_POINTER_REGNUM)
2530 cfa.offset = cfa_store.offset;
2532 offset = -cfa_store.offset;
2533 break;
2535 /* Rule 12 */
2536 /* With an offset. */
2537 case PLUS:
2538 case MINUS:
2539 case LO_SUM:
2541 int regno;
2543 gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2544 && REG_P (XEXP (XEXP (dest, 0), 0)));
2545 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2546 if (GET_CODE (XEXP (dest, 0)) == MINUS)
2547 offset = -offset;
2549 regno = REGNO (XEXP (XEXP (dest, 0), 0));
2551 if (cfa_store.reg == (unsigned) regno)
2552 offset -= cfa_store.offset;
2553 else
2555 gcc_assert (cfa_temp.reg == (unsigned) regno);
2556 offset -= cfa_temp.offset;
2559 break;
2561 /* Rule 13 */
2562 /* Without an offset. */
2563 case REG:
2565 int regno = REGNO (XEXP (dest, 0));
2567 if (cfa_store.reg == (unsigned) regno)
2568 offset = -cfa_store.offset;
2569 else
2571 gcc_assert (cfa_temp.reg == (unsigned) regno);
2572 offset = -cfa_temp.offset;
2575 break;
2577 /* Rule 14 */
2578 case POST_INC:
2579 gcc_assert (cfa_temp.reg
2580 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2581 offset = -cfa_temp.offset;
2582 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2583 break;
2585 default:
2586 gcc_unreachable ();
2589 /* Rule 17 */
2590 /* If the source operand of this MEM operation is not a
2591 register, basically the source is return address. Here
2592 we only care how much stack grew and we don't save it. */
2593 if (!REG_P (src))
2594 break;
2596 if (REGNO (src) != STACK_POINTER_REGNUM
2597 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2598 && (unsigned) REGNO (src) == cfa.reg)
2600 /* We're storing the current CFA reg into the stack. */
2602 if (cfa.offset == 0)
2604 /* Rule 19 */
2605 /* If stack is aligned, putting CFA reg into stack means
2606 we can no longer use reg + offset to represent CFA.
2607 Here we use DW_CFA_def_cfa_expression instead. The
2608 result of this expression equals to the original CFA
2609 value. */
2610 if (fde
2611 && fde->stack_realign
2612 && cfa.indirect == 0
2613 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2615 dw_cfa_location cfa_exp;
2617 gcc_assert (fde->drap_reg == cfa.reg);
2619 cfa_exp.indirect = 1;
2620 cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2621 cfa_exp.base_offset = offset;
2622 cfa_exp.offset = 0;
2624 fde->drap_reg_saved = 1;
2626 def_cfa_1 (label, &cfa_exp);
2627 break;
2630 /* If the source register is exactly the CFA, assume
2631 we're saving SP like any other register; this happens
2632 on the ARM. */
2633 def_cfa_1 (label, &cfa);
2634 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2635 break;
2637 else
2639 /* Otherwise, we'll need to look in the stack to
2640 calculate the CFA. */
2641 rtx x = XEXP (dest, 0);
2643 if (!REG_P (x))
2644 x = XEXP (x, 0);
2645 gcc_assert (REG_P (x));
2647 cfa.reg = REGNO (x);
2648 cfa.base_offset = offset;
2649 cfa.indirect = 1;
2650 def_cfa_1 (label, &cfa);
2651 break;
2655 def_cfa_1 (label, &cfa);
2657 span = targetm.dwarf_register_span (src);
2659 if (!span)
2660 queue_reg_save (label, src, NULL_RTX, offset);
2661 else
2663 /* We have a PARALLEL describing where the contents of SRC
2664 live. Queue register saves for each piece of the
2665 PARALLEL. */
2666 int par_index;
2667 int limit;
2668 HOST_WIDE_INT span_offset = offset;
2670 gcc_assert (GET_CODE (span) == PARALLEL);
2672 limit = XVECLEN (span, 0);
2673 for (par_index = 0; par_index < limit; par_index++)
2675 rtx elem = XVECEXP (span, 0, par_index);
2677 queue_reg_save (label, elem, NULL_RTX, span_offset);
2678 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2682 break;
2684 default:
2685 gcc_unreachable ();
2689 /* Record call frame debugging information for INSN, which either
2690 sets SP or FP (adjusting how we calculate the frame address) or saves a
2691 register to the stack. If INSN is NULL_RTX, initialize our state.
2693 If AFTER_P is false, we're being called before the insn is emitted,
2694 otherwise after. Call instructions get invoked twice. */
2696 void
2697 dwarf2out_frame_debug (rtx insn, bool after_p)
2699 const char *label;
2700 rtx note, n;
2701 bool handled_one = false;
2703 if (insn == NULL_RTX)
2705 size_t i;
2707 /* Flush any queued register saves. */
2708 dwarf2out_flush_queued_reg_saves ();
2710 /* Set up state for generating call frame debug info. */
2711 lookup_cfa (&cfa);
2712 gcc_assert (cfa.reg
2713 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2715 cfa.reg = STACK_POINTER_REGNUM;
2716 cfa_store = cfa;
2717 cfa_temp.reg = -1;
2718 cfa_temp.offset = 0;
2720 for (i = 0; i < num_regs_saved_in_regs; i++)
2722 regs_saved_in_regs[i].orig_reg = NULL_RTX;
2723 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2725 num_regs_saved_in_regs = 0;
2727 if (barrier_args_size)
2729 XDELETEVEC (barrier_args_size);
2730 barrier_args_size = NULL;
2732 return;
2735 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2736 dwarf2out_flush_queued_reg_saves ();
2738 if (!RTX_FRAME_RELATED_P (insn))
2740 /* ??? This should be done unconditionally since stack adjustments
2741 matter if the stack pointer is not the CFA register anymore but
2742 is still used to save registers. */
2743 if (!ACCUMULATE_OUTGOING_ARGS)
2744 dwarf2out_notice_stack_adjust (insn, after_p);
2745 return;
2748 label = dwarf2out_cfi_label (false);
2750 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2751 switch (REG_NOTE_KIND (note))
2753 case REG_FRAME_RELATED_EXPR:
2754 insn = XEXP (note, 0);
2755 goto found;
2757 case REG_CFA_DEF_CFA:
2758 dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2759 handled_one = true;
2760 break;
2762 case REG_CFA_ADJUST_CFA:
2763 n = XEXP (note, 0);
2764 if (n == NULL)
2766 n = PATTERN (insn);
2767 if (GET_CODE (n) == PARALLEL)
2768 n = XVECEXP (n, 0, 0);
2770 dwarf2out_frame_debug_adjust_cfa (n, label);
2771 handled_one = true;
2772 break;
2774 case REG_CFA_OFFSET:
2775 n = XEXP (note, 0);
2776 if (n == NULL)
2777 n = single_set (insn);
2778 dwarf2out_frame_debug_cfa_offset (n, label);
2779 handled_one = true;
2780 break;
2782 case REG_CFA_REGISTER:
2783 n = XEXP (note, 0);
2784 if (n == NULL)
2786 n = PATTERN (insn);
2787 if (GET_CODE (n) == PARALLEL)
2788 n = XVECEXP (n, 0, 0);
2790 dwarf2out_frame_debug_cfa_register (n, label);
2791 handled_one = true;
2792 break;
2794 case REG_CFA_EXPRESSION:
2795 n = XEXP (note, 0);
2796 if (n == NULL)
2797 n = single_set (insn);
2798 dwarf2out_frame_debug_cfa_expression (n, label);
2799 handled_one = true;
2800 break;
2802 case REG_CFA_RESTORE:
2803 n = XEXP (note, 0);
2804 if (n == NULL)
2806 n = PATTERN (insn);
2807 if (GET_CODE (n) == PARALLEL)
2808 n = XVECEXP (n, 0, 0);
2809 n = XEXP (n, 0);
2811 dwarf2out_frame_debug_cfa_restore (n, label);
2812 handled_one = true;
2813 break;
2815 case REG_CFA_SET_VDRAP:
2816 n = XEXP (note, 0);
2817 if (REG_P (n))
2819 dw_fde_ref fde = current_fde ();
2820 if (fde)
2822 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2823 if (REG_P (n))
2824 fde->vdrap_reg = REGNO (n);
2827 handled_one = true;
2828 break;
2830 case REG_CFA_TEMPORARY:
2831 if (!after_p)
2833 dwarf2out_frame_debug_remember_state ();
2834 dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2836 else
2837 dwarf2out_frame_debug_restore_state ();
2838 handled_one = true;
2839 break;
2841 default:
2842 break;
2844 if (handled_one)
2845 return;
2847 insn = PATTERN (insn);
2848 found:
2849 dwarf2out_frame_debug_expr (insn, label);
2851 /* Check again. A parallel can save and update the same register.
2852 We could probably check just once, here, but this is safer than
2853 removing the check above. */
2854 if (clobbers_queued_reg_save (insn))
2855 dwarf2out_flush_queued_reg_saves ();
2858 /* Determine if we need to save and restore CFI information around this
2859 epilogue. If SIBCALL is true, then this is a sibcall epilogue. If
2860 we do need to save/restore, then emit the save now, and insert a
2861 NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream. */
2863 void
2864 dwarf2out_cfi_begin_epilogue (rtx insn)
2866 bool saw_frp = false;
2867 rtx i;
2869 /* Scan forward to the return insn, noticing if there are possible
2870 frame related insns. */
2871 for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2873 if (!INSN_P (i))
2874 continue;
2876 /* Look for both regular and sibcalls to end the block. */
2877 if (returnjump_p (i))
2878 break;
2879 if (CALL_P (i) && SIBLING_CALL_P (i))
2880 break;
2882 if (GET_CODE (PATTERN (i)) == SEQUENCE)
2884 int idx;
2885 rtx seq = PATTERN (i);
2887 if (returnjump_p (XVECEXP (seq, 0, 0)))
2888 break;
2889 if (CALL_P (XVECEXP (seq, 0, 0))
2890 && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2891 break;
2893 for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2894 if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2895 saw_frp = true;
2898 if (RTX_FRAME_RELATED_P (i))
2899 saw_frp = true;
2902 /* If the port doesn't emit epilogue unwind info, we don't need a
2903 save/restore pair. */
2904 if (!saw_frp)
2905 return;
2907 /* Otherwise, search forward to see if the return insn was the last
2908 basic block of the function. If so, we don't need save/restore. */
2909 gcc_assert (i != NULL);
2910 i = next_real_insn (i);
2911 if (i == NULL)
2912 return;
2914 /* Insert the restore before that next real insn in the stream, and before
2915 a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2916 properly nested. This should be after any label or alignment. This
2917 will be pushed into the CFI stream by the function below. */
2918 while (1)
2920 rtx p = PREV_INSN (i);
2921 if (!NOTE_P (p))
2922 break;
2923 if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2924 break;
2925 i = p;
2927 emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2929 dwarf2out_frame_debug_remember_state ();
2932 /* Remember the current state. */
2934 static void
2935 dwarf2out_frame_debug_remember_state (void)
2937 emit_cfa_remember = true;
2939 /* Emulate the state save. */
2940 gcc_assert (!cfa_remember.in_use);
2941 cfa_remember = cfa;
2942 cfa_remember.in_use = 1;
2945 /* A "subroutine" of dwarf2out_cfi_begin_epilogue. Emit the restore
2946 required. */
2948 void
2949 dwarf2out_frame_debug_restore_state (void)
2951 dw_cfi_ref cfi = new_cfi ();
2952 const char *label = dwarf2out_cfi_label (false);
2954 cfi->dw_cfi_opc = DW_CFA_restore_state;
2955 add_fde_cfi (label, cfi);
2957 gcc_assert (cfa_remember.in_use);
2958 cfa = cfa_remember;
2959 cfa_remember.in_use = 0;
2962 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
2963 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2964 (enum dwarf_call_frame_info cfi);
2966 static enum dw_cfi_oprnd_type
2967 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2969 switch (cfi)
2971 case DW_CFA_nop:
2972 case DW_CFA_GNU_window_save:
2973 case DW_CFA_remember_state:
2974 case DW_CFA_restore_state:
2975 return dw_cfi_oprnd_unused;
2977 case DW_CFA_set_loc:
2978 case DW_CFA_advance_loc1:
2979 case DW_CFA_advance_loc2:
2980 case DW_CFA_advance_loc4:
2981 case DW_CFA_MIPS_advance_loc8:
2982 return dw_cfi_oprnd_addr;
2984 case DW_CFA_offset:
2985 case DW_CFA_offset_extended:
2986 case DW_CFA_def_cfa:
2987 case DW_CFA_offset_extended_sf:
2988 case DW_CFA_def_cfa_sf:
2989 case DW_CFA_restore:
2990 case DW_CFA_restore_extended:
2991 case DW_CFA_undefined:
2992 case DW_CFA_same_value:
2993 case DW_CFA_def_cfa_register:
2994 case DW_CFA_register:
2995 case DW_CFA_expression:
2996 return dw_cfi_oprnd_reg_num;
2998 case DW_CFA_def_cfa_offset:
2999 case DW_CFA_GNU_args_size:
3000 case DW_CFA_def_cfa_offset_sf:
3001 return dw_cfi_oprnd_offset;
3003 case DW_CFA_def_cfa_expression:
3004 return dw_cfi_oprnd_loc;
3006 default:
3007 gcc_unreachable ();
3011 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
3012 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3013 (enum dwarf_call_frame_info cfi);
3015 static enum dw_cfi_oprnd_type
3016 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3018 switch (cfi)
3020 case DW_CFA_def_cfa:
3021 case DW_CFA_def_cfa_sf:
3022 case DW_CFA_offset:
3023 case DW_CFA_offset_extended_sf:
3024 case DW_CFA_offset_extended:
3025 return dw_cfi_oprnd_offset;
3027 case DW_CFA_register:
3028 return dw_cfi_oprnd_reg_num;
3030 case DW_CFA_expression:
3031 return dw_cfi_oprnd_loc;
3033 default:
3034 return dw_cfi_oprnd_unused;
3038 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
3039 switch to the data section instead, and write out a synthetic start label
3040 for collect2 the first time around. */
3042 static void
3043 switch_to_eh_frame_section (bool back)
3045 tree label;
3047 #ifdef EH_FRAME_SECTION_NAME
3048 if (eh_frame_section == 0)
3050 int flags;
3052 if (EH_TABLES_CAN_BE_READ_ONLY)
3054 int fde_encoding;
3055 int per_encoding;
3056 int lsda_encoding;
3058 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3059 /*global=*/0);
3060 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3061 /*global=*/1);
3062 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3063 /*global=*/0);
3064 flags = ((! flag_pic
3065 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3066 && (fde_encoding & 0x70) != DW_EH_PE_aligned
3067 && (per_encoding & 0x70) != DW_EH_PE_absptr
3068 && (per_encoding & 0x70) != DW_EH_PE_aligned
3069 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3070 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3071 ? 0 : SECTION_WRITE);
3073 else
3074 flags = SECTION_WRITE;
3075 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3077 #endif /* EH_FRAME_SECTION_NAME */
3079 if (eh_frame_section)
3080 switch_to_section (eh_frame_section);
3081 else
3083 /* We have no special eh_frame section. Put the information in
3084 the data section and emit special labels to guide collect2. */
3085 switch_to_section (data_section);
3087 if (!back)
3089 label = get_file_function_name ("F");
3090 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3091 targetm.asm_out.globalize_label (asm_out_file,
3092 IDENTIFIER_POINTER (label));
3093 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3098 /* Switch [BACK] to the eh or debug frame table section, depending on
3099 FOR_EH. */
3101 static void
3102 switch_to_frame_table_section (int for_eh, bool back)
3104 if (for_eh)
3105 switch_to_eh_frame_section (back);
3106 else
3108 if (!debug_frame_section)
3109 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3110 SECTION_DEBUG, NULL);
3111 switch_to_section (debug_frame_section);
3115 /* Output a Call Frame Information opcode and its operand(s). */
3117 static void
3118 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3120 unsigned long r;
3121 HOST_WIDE_INT off;
3123 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3124 dw2_asm_output_data (1, (cfi->dw_cfi_opc
3125 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3126 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3127 ((unsigned HOST_WIDE_INT)
3128 cfi->dw_cfi_oprnd1.dw_cfi_offset));
3129 else if (cfi->dw_cfi_opc == DW_CFA_offset)
3131 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3132 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3133 "DW_CFA_offset, column %#lx", r);
3134 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3135 dw2_asm_output_data_uleb128 (off, NULL);
3137 else if (cfi->dw_cfi_opc == DW_CFA_restore)
3139 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3140 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3141 "DW_CFA_restore, column %#lx", r);
3143 else
3145 dw2_asm_output_data (1, cfi->dw_cfi_opc,
3146 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3148 switch (cfi->dw_cfi_opc)
3150 case DW_CFA_set_loc:
3151 if (for_eh)
3152 dw2_asm_output_encoded_addr_rtx (
3153 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3154 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3155 false, NULL);
3156 else
3157 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3158 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3159 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3160 break;
3162 case DW_CFA_advance_loc1:
3163 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3164 fde->dw_fde_current_label, NULL);
3165 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3166 break;
3168 case DW_CFA_advance_loc2:
3169 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3170 fde->dw_fde_current_label, NULL);
3171 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3172 break;
3174 case DW_CFA_advance_loc4:
3175 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3176 fde->dw_fde_current_label, NULL);
3177 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3178 break;
3180 case DW_CFA_MIPS_advance_loc8:
3181 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3182 fde->dw_fde_current_label, NULL);
3183 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3184 break;
3186 case DW_CFA_offset_extended:
3187 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3188 dw2_asm_output_data_uleb128 (r, NULL);
3189 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3190 dw2_asm_output_data_uleb128 (off, NULL);
3191 break;
3193 case DW_CFA_def_cfa:
3194 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3195 dw2_asm_output_data_uleb128 (r, NULL);
3196 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3197 break;
3199 case DW_CFA_offset_extended_sf:
3200 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3201 dw2_asm_output_data_uleb128 (r, NULL);
3202 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3203 dw2_asm_output_data_sleb128 (off, NULL);
3204 break;
3206 case DW_CFA_def_cfa_sf:
3207 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3208 dw2_asm_output_data_uleb128 (r, NULL);
3209 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3210 dw2_asm_output_data_sleb128 (off, NULL);
3211 break;
3213 case DW_CFA_restore_extended:
3214 case DW_CFA_undefined:
3215 case DW_CFA_same_value:
3216 case DW_CFA_def_cfa_register:
3217 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3218 dw2_asm_output_data_uleb128 (r, NULL);
3219 break;
3221 case DW_CFA_register:
3222 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3223 dw2_asm_output_data_uleb128 (r, NULL);
3224 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3225 dw2_asm_output_data_uleb128 (r, NULL);
3226 break;
3228 case DW_CFA_def_cfa_offset:
3229 case DW_CFA_GNU_args_size:
3230 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3231 break;
3233 case DW_CFA_def_cfa_offset_sf:
3234 off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3235 dw2_asm_output_data_sleb128 (off, NULL);
3236 break;
3238 case DW_CFA_GNU_window_save:
3239 break;
3241 case DW_CFA_def_cfa_expression:
3242 case DW_CFA_expression:
3243 output_cfa_loc (cfi);
3244 break;
3246 case DW_CFA_GNU_negative_offset_extended:
3247 /* Obsoleted by DW_CFA_offset_extended_sf. */
3248 gcc_unreachable ();
3250 default:
3251 break;
3256 /* Similar, but do it via assembler directives instead. */
3258 static void
3259 output_cfi_directive (dw_cfi_ref cfi)
3261 unsigned long r, r2;
3263 switch (cfi->dw_cfi_opc)
3265 case DW_CFA_advance_loc:
3266 case DW_CFA_advance_loc1:
3267 case DW_CFA_advance_loc2:
3268 case DW_CFA_advance_loc4:
3269 case DW_CFA_MIPS_advance_loc8:
3270 case DW_CFA_set_loc:
3271 /* Should only be created by add_fde_cfi in a code path not
3272 followed when emitting via directives. The assembler is
3273 going to take care of this for us. */
3274 gcc_unreachable ();
3276 case DW_CFA_offset:
3277 case DW_CFA_offset_extended:
3278 case DW_CFA_offset_extended_sf:
3279 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3280 fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3281 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3282 break;
3284 case DW_CFA_restore:
3285 case DW_CFA_restore_extended:
3286 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3287 fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3288 break;
3290 case DW_CFA_undefined:
3291 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3292 fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3293 break;
3295 case DW_CFA_same_value:
3296 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3297 fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3298 break;
3300 case DW_CFA_def_cfa:
3301 case DW_CFA_def_cfa_sf:
3302 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3303 fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3304 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3305 break;
3307 case DW_CFA_def_cfa_register:
3308 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3309 fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3310 break;
3312 case DW_CFA_register:
3313 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3314 r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3315 fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3316 break;
3318 case DW_CFA_def_cfa_offset:
3319 case DW_CFA_def_cfa_offset_sf:
3320 fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3321 HOST_WIDE_INT_PRINT_DEC"\n",
3322 cfi->dw_cfi_oprnd1.dw_cfi_offset);
3323 break;
3325 case DW_CFA_remember_state:
3326 fprintf (asm_out_file, "\t.cfi_remember_state\n");
3327 break;
3328 case DW_CFA_restore_state:
3329 fprintf (asm_out_file, "\t.cfi_restore_state\n");
3330 break;
3332 case DW_CFA_GNU_args_size:
3333 fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3334 dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3335 if (flag_debug_asm)
3336 fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3337 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3338 fputc ('\n', asm_out_file);
3339 break;
3341 case DW_CFA_GNU_window_save:
3342 fprintf (asm_out_file, "\t.cfi_window_save\n");
3343 break;
3345 case DW_CFA_def_cfa_expression:
3346 case DW_CFA_expression:
3347 fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3348 output_cfa_loc_raw (cfi);
3349 fputc ('\n', asm_out_file);
3350 break;
3352 default:
3353 gcc_unreachable ();
3357 DEF_VEC_P (dw_cfi_ref);
3358 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3360 /* Output CFIs to bring current FDE to the same state as after executing
3361 CFIs in CFI chain. DO_CFI_ASM is true if .cfi_* directives shall
3362 be emitted, false otherwise. If it is false, FDE and FOR_EH are the
3363 other arguments to pass to output_cfi. */
3365 static void
3366 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3368 struct dw_cfi_struct cfi_buf;
3369 dw_cfi_ref cfi2;
3370 dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3371 VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3372 unsigned int len, idx;
3374 for (;; cfi = cfi->dw_cfi_next)
3375 switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3377 case DW_CFA_advance_loc:
3378 case DW_CFA_advance_loc1:
3379 case DW_CFA_advance_loc2:
3380 case DW_CFA_advance_loc4:
3381 case DW_CFA_MIPS_advance_loc8:
3382 case DW_CFA_set_loc:
3383 /* All advances should be ignored. */
3384 break;
3385 case DW_CFA_remember_state:
3387 dw_cfi_ref args_size = cfi_args_size;
3389 /* Skip everything between .cfi_remember_state and
3390 .cfi_restore_state. */
3391 for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3392 if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3393 break;
3394 else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3395 args_size = cfi2;
3396 else
3397 gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3399 if (cfi2 == NULL)
3400 goto flush_all;
3401 else
3403 cfi = cfi2;
3404 cfi_args_size = args_size;
3406 break;
3408 case DW_CFA_GNU_args_size:
3409 cfi_args_size = cfi;
3410 break;
3411 case DW_CFA_GNU_window_save:
3412 goto flush_all;
3413 case DW_CFA_offset:
3414 case DW_CFA_offset_extended:
3415 case DW_CFA_offset_extended_sf:
3416 case DW_CFA_restore:
3417 case DW_CFA_restore_extended:
3418 case DW_CFA_undefined:
3419 case DW_CFA_same_value:
3420 case DW_CFA_register:
3421 case DW_CFA_val_offset:
3422 case DW_CFA_val_offset_sf:
3423 case DW_CFA_expression:
3424 case DW_CFA_val_expression:
3425 case DW_CFA_GNU_negative_offset_extended:
3426 if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3427 VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3428 cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3429 VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3430 break;
3431 case DW_CFA_def_cfa:
3432 case DW_CFA_def_cfa_sf:
3433 case DW_CFA_def_cfa_expression:
3434 cfi_cfa = cfi;
3435 cfi_cfa_offset = cfi;
3436 break;
3437 case DW_CFA_def_cfa_register:
3438 cfi_cfa = cfi;
3439 break;
3440 case DW_CFA_def_cfa_offset:
3441 case DW_CFA_def_cfa_offset_sf:
3442 cfi_cfa_offset = cfi;
3443 break;
3444 case DW_CFA_nop:
3445 gcc_assert (cfi == NULL);
3446 flush_all:
3447 len = VEC_length (dw_cfi_ref, regs);
3448 for (idx = 0; idx < len; idx++)
3450 cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3451 if (cfi2 != NULL
3452 && cfi2->dw_cfi_opc != DW_CFA_restore
3453 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3455 if (do_cfi_asm)
3456 output_cfi_directive (cfi2);
3457 else
3458 output_cfi (cfi2, fde, for_eh);
3461 if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3463 gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3464 cfi_buf = *cfi_cfa;
3465 switch (cfi_cfa_offset->dw_cfi_opc)
3467 case DW_CFA_def_cfa_offset:
3468 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3469 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3470 break;
3471 case DW_CFA_def_cfa_offset_sf:
3472 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3473 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3474 break;
3475 case DW_CFA_def_cfa:
3476 case DW_CFA_def_cfa_sf:
3477 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3478 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3479 break;
3480 default:
3481 gcc_unreachable ();
3483 cfi_cfa = &cfi_buf;
3485 else if (cfi_cfa_offset)
3486 cfi_cfa = cfi_cfa_offset;
3487 if (cfi_cfa)
3489 if (do_cfi_asm)
3490 output_cfi_directive (cfi_cfa);
3491 else
3492 output_cfi (cfi_cfa, fde, for_eh);
3494 cfi_cfa = NULL;
3495 cfi_cfa_offset = NULL;
3496 if (cfi_args_size
3497 && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3499 if (do_cfi_asm)
3500 output_cfi_directive (cfi_args_size);
3501 else
3502 output_cfi (cfi_args_size, fde, for_eh);
3504 cfi_args_size = NULL;
3505 if (cfi == NULL)
3507 VEC_free (dw_cfi_ref, heap, regs);
3508 return;
3510 else if (do_cfi_asm)
3511 output_cfi_directive (cfi);
3512 else
3513 output_cfi (cfi, fde, for_eh);
3514 break;
3515 default:
3516 gcc_unreachable ();
3520 /* Output one FDE. */
3522 static void
3523 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3524 char *section_start_label, int fde_encoding, char *augmentation,
3525 bool any_lsda_needed, int lsda_encoding)
3527 const char *begin, *end;
3528 static unsigned int j;
3529 char l1[20], l2[20];
3530 dw_cfi_ref cfi;
3532 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3533 /* empty */ 0);
3534 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3535 for_eh + j);
3536 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3537 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3538 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3539 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3540 " indicating 64-bit DWARF extension");
3541 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3542 "FDE Length");
3543 ASM_OUTPUT_LABEL (asm_out_file, l1);
3545 if (for_eh)
3546 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3547 else
3548 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3549 debug_frame_section, "FDE CIE offset");
3551 if (!fde->dw_fde_switched_sections)
3553 begin = fde->dw_fde_begin;
3554 end = fde->dw_fde_end;
3556 else
3558 /* For the first section, prefer dw_fde_begin over
3559 dw_fde_{hot,cold}_section_label, as the latter
3560 might be separated from the real start of the
3561 function by alignment padding. */
3562 if (!second)
3563 begin = fde->dw_fde_begin;
3564 else if (fde->dw_fde_switched_cold_to_hot)
3565 begin = fde->dw_fde_hot_section_label;
3566 else
3567 begin = fde->dw_fde_unlikely_section_label;
3568 if (second ^ fde->dw_fde_switched_cold_to_hot)
3569 end = fde->dw_fde_unlikely_section_end_label;
3570 else
3571 end = fde->dw_fde_hot_section_end_label;
3574 if (for_eh)
3576 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3577 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3578 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3579 "FDE initial location");
3580 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3581 end, begin, "FDE address range");
3583 else
3585 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3586 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3589 if (augmentation[0])
3591 if (any_lsda_needed)
3593 int size = size_of_encoded_value (lsda_encoding);
3595 if (lsda_encoding == DW_EH_PE_aligned)
3597 int offset = ( 4 /* Length */
3598 + 4 /* CIE offset */
3599 + 2 * size_of_encoded_value (fde_encoding)
3600 + 1 /* Augmentation size */ );
3601 int pad = -offset & (PTR_SIZE - 1);
3603 size += pad;
3604 gcc_assert (size_of_uleb128 (size) == 1);
3607 dw2_asm_output_data_uleb128 (size, "Augmentation size");
3609 if (fde->uses_eh_lsda)
3611 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3612 fde->funcdef_number);
3613 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3614 gen_rtx_SYMBOL_REF (Pmode, l1),
3615 false,
3616 "Language Specific Data Area");
3618 else
3620 if (lsda_encoding == DW_EH_PE_aligned)
3621 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3622 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3623 "Language Specific Data Area (none)");
3626 else
3627 dw2_asm_output_data_uleb128 (0, "Augmentation size");
3630 /* Loop through the Call Frame Instructions associated with
3631 this FDE. */
3632 fde->dw_fde_current_label = begin;
3633 if (!fde->dw_fde_switched_sections)
3634 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3635 output_cfi (cfi, fde, for_eh);
3636 else if (!second)
3638 if (fde->dw_fde_switch_cfi)
3639 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3641 output_cfi (cfi, fde, for_eh);
3642 if (cfi == fde->dw_fde_switch_cfi)
3643 break;
3646 else
3648 dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3650 if (fde->dw_fde_switch_cfi)
3652 cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3653 fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3654 output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3655 fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3657 for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3658 output_cfi (cfi, fde, for_eh);
3661 /* If we are to emit a ref/link from function bodies to their frame tables,
3662 do it now. This is typically performed to make sure that tables
3663 associated with functions are dragged with them and not discarded in
3664 garbage collecting links. We need to do this on a per function basis to
3665 cope with -ffunction-sections. */
3667 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3668 /* Switch to the function section, emit the ref to the tables, and
3669 switch *back* into the table section. */
3670 switch_to_section (function_section (fde->decl));
3671 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3672 switch_to_frame_table_section (for_eh, true);
3673 #endif
3675 /* Pad the FDE out to an address sized boundary. */
3676 ASM_OUTPUT_ALIGN (asm_out_file,
3677 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3678 ASM_OUTPUT_LABEL (asm_out_file, l2);
3680 j += 2;
3683 /* Return true if frame description entry FDE is needed for EH. */
3685 static bool
3686 fde_needed_for_eh_p (dw_fde_ref fde)
3688 if (flag_asynchronous_unwind_tables)
3689 return true;
3691 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3692 return true;
3694 if (fde->uses_eh_lsda)
3695 return true;
3697 /* If exceptions are enabled, we have collected nothrow info. */
3698 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3699 return false;
3701 return true;
3704 /* Output the call frame information used to record information
3705 that relates to calculating the frame pointer, and records the
3706 location of saved registers. */
3708 static void
3709 output_call_frame_info (int for_eh)
3711 unsigned int i;
3712 dw_fde_ref fde;
3713 dw_cfi_ref cfi;
3714 char l1[20], l2[20], section_start_label[20];
3715 bool any_lsda_needed = false;
3716 char augmentation[6];
3717 int augmentation_size;
3718 int fde_encoding = DW_EH_PE_absptr;
3719 int per_encoding = DW_EH_PE_absptr;
3720 int lsda_encoding = DW_EH_PE_absptr;
3721 int return_reg;
3722 rtx personality = NULL;
3723 int dw_cie_version;
3725 /* Don't emit a CIE if there won't be any FDEs. */
3726 if (fde_table_in_use == 0)
3727 return;
3729 /* Nothing to do if the assembler's doing it all. */
3730 if (dwarf2out_do_cfi_asm ())
3731 return;
3733 /* If we don't have any functions we'll want to unwind out of, don't emit
3734 any EH unwind information. If we make FDEs linkonce, we may have to
3735 emit an empty label for an FDE that wouldn't otherwise be emitted. We
3736 want to avoid having an FDE kept around when the function it refers to
3737 is discarded. Example where this matters: a primary function template
3738 in C++ requires EH information, an explicit specialization doesn't. */
3739 if (for_eh)
3741 bool any_eh_needed = false;
3743 for (i = 0; i < fde_table_in_use; i++)
3744 if (fde_table[i].uses_eh_lsda)
3745 any_eh_needed = any_lsda_needed = true;
3746 else if (fde_needed_for_eh_p (&fde_table[i]))
3747 any_eh_needed = true;
3748 else if (TARGET_USES_WEAK_UNWIND_INFO)
3749 targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3750 1, 1);
3752 if (!any_eh_needed)
3753 return;
3756 /* We're going to be generating comments, so turn on app. */
3757 if (flag_debug_asm)
3758 app_enable ();
3760 /* Switch to the proper frame section, first time. */
3761 switch_to_frame_table_section (for_eh, false);
3763 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3764 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3766 /* Output the CIE. */
3767 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3768 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3769 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3770 dw2_asm_output_data (4, 0xffffffff,
3771 "Initial length escape value indicating 64-bit DWARF extension");
3772 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3773 "Length of Common Information Entry");
3774 ASM_OUTPUT_LABEL (asm_out_file, l1);
3776 /* Now that the CIE pointer is PC-relative for EH,
3777 use 0 to identify the CIE. */
3778 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3779 (for_eh ? 0 : DWARF_CIE_ID),
3780 "CIE Identifier Tag");
3782 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3783 use CIE version 1, unless that would produce incorrect results
3784 due to overflowing the return register column. */
3785 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3786 dw_cie_version = 1;
3787 if (return_reg >= 256 || dwarf_version > 2)
3788 dw_cie_version = 3;
3789 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3791 augmentation[0] = 0;
3792 augmentation_size = 0;
3794 personality = current_unit_personality;
3795 if (for_eh)
3797 char *p;
3799 /* Augmentation:
3800 z Indicates that a uleb128 is present to size the
3801 augmentation section.
3802 L Indicates the encoding (and thus presence) of
3803 an LSDA pointer in the FDE augmentation.
3804 R Indicates a non-default pointer encoding for
3805 FDE code pointers.
3806 P Indicates the presence of an encoding + language
3807 personality routine in the CIE augmentation. */
3809 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3810 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3811 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3813 p = augmentation + 1;
3814 if (personality)
3816 *p++ = 'P';
3817 augmentation_size += 1 + size_of_encoded_value (per_encoding);
3818 assemble_external_libcall (personality);
3820 if (any_lsda_needed)
3822 *p++ = 'L';
3823 augmentation_size += 1;
3825 if (fde_encoding != DW_EH_PE_absptr)
3827 *p++ = 'R';
3828 augmentation_size += 1;
3830 if (p > augmentation + 1)
3832 augmentation[0] = 'z';
3833 *p = '\0';
3836 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
3837 if (personality && per_encoding == DW_EH_PE_aligned)
3839 int offset = ( 4 /* Length */
3840 + 4 /* CIE Id */
3841 + 1 /* CIE version */
3842 + strlen (augmentation) + 1 /* Augmentation */
3843 + size_of_uleb128 (1) /* Code alignment */
3844 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3845 + 1 /* RA column */
3846 + 1 /* Augmentation size */
3847 + 1 /* Personality encoding */ );
3848 int pad = -offset & (PTR_SIZE - 1);
3850 augmentation_size += pad;
3852 /* Augmentations should be small, so there's scarce need to
3853 iterate for a solution. Die if we exceed one uleb128 byte. */
3854 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3858 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3859 if (dw_cie_version >= 4)
3861 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3862 dw2_asm_output_data (1, 0, "CIE Segment Size");
3864 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3865 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3866 "CIE Data Alignment Factor");
3868 if (dw_cie_version == 1)
3869 dw2_asm_output_data (1, return_reg, "CIE RA Column");
3870 else
3871 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3873 if (augmentation[0])
3875 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3876 if (personality)
3878 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3879 eh_data_format_name (per_encoding));
3880 dw2_asm_output_encoded_addr_rtx (per_encoding,
3881 personality,
3882 true, NULL);
3885 if (any_lsda_needed)
3886 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3887 eh_data_format_name (lsda_encoding));
3889 if (fde_encoding != DW_EH_PE_absptr)
3890 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3891 eh_data_format_name (fde_encoding));
3894 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3895 output_cfi (cfi, NULL, for_eh);
3897 /* Pad the CIE out to an address sized boundary. */
3898 ASM_OUTPUT_ALIGN (asm_out_file,
3899 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3900 ASM_OUTPUT_LABEL (asm_out_file, l2);
3902 /* Loop through all of the FDE's. */
3903 for (i = 0; i < fde_table_in_use; i++)
3905 unsigned int k;
3906 fde = &fde_table[i];
3908 /* Don't emit EH unwind info for leaf functions that don't need it. */
3909 if (for_eh && !fde_needed_for_eh_p (fde))
3910 continue;
3912 for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3913 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3914 augmentation, any_lsda_needed, lsda_encoding);
3917 if (for_eh && targetm.terminate_dw2_eh_frame_info)
3918 dw2_asm_output_data (4, 0, "End of Table");
3919 #ifdef MIPS_DEBUGGING_INFO
3920 /* Work around Irix 6 assembler bug whereby labels at the end of a section
3921 get a value of 0. Putting .align 0 after the label fixes it. */
3922 ASM_OUTPUT_ALIGN (asm_out_file, 0);
3923 #endif
3925 /* Turn off app to make assembly quicker. */
3926 if (flag_debug_asm)
3927 app_disable ();
3930 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
3932 static void
3933 dwarf2out_do_cfi_startproc (bool second)
3935 int enc;
3936 rtx ref;
3937 rtx personality = get_personality_function (current_function_decl);
3939 fprintf (asm_out_file, "\t.cfi_startproc\n");
3941 if (personality)
3943 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3944 ref = personality;
3946 /* ??? The GAS support isn't entirely consistent. We have to
3947 handle indirect support ourselves, but PC-relative is done
3948 in the assembler. Further, the assembler can't handle any
3949 of the weirder relocation types. */
3950 if (enc & DW_EH_PE_indirect)
3951 ref = dw2_force_const_mem (ref, true);
3953 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
3954 output_addr_const (asm_out_file, ref);
3955 fputc ('\n', asm_out_file);
3958 if (crtl->uses_eh_lsda)
3960 char lab[20];
3962 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3963 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3964 current_function_funcdef_no);
3965 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3966 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3968 if (enc & DW_EH_PE_indirect)
3969 ref = dw2_force_const_mem (ref, true);
3971 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
3972 output_addr_const (asm_out_file, ref);
3973 fputc ('\n', asm_out_file);
3977 /* Output a marker (i.e. a label) for the beginning of a function, before
3978 the prologue. */
3980 void
3981 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3982 const char *file ATTRIBUTE_UNUSED)
3984 char label[MAX_ARTIFICIAL_LABEL_BYTES];
3985 char * dup_label;
3986 dw_fde_ref fde;
3987 section *fnsec;
3989 current_function_func_begin_label = NULL;
3991 #ifdef TARGET_UNWIND_INFO
3992 /* ??? current_function_func_begin_label is also used by except.c
3993 for call-site information. We must emit this label if it might
3994 be used. */
3995 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3996 && ! dwarf2out_do_frame ())
3997 return;
3998 #else
3999 if (! dwarf2out_do_frame ())
4000 return;
4001 #endif
4003 fnsec = function_section (current_function_decl);
4004 switch_to_section (fnsec);
4005 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4006 current_function_funcdef_no);
4007 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4008 current_function_funcdef_no);
4009 dup_label = xstrdup (label);
4010 current_function_func_begin_label = dup_label;
4012 #ifdef TARGET_UNWIND_INFO
4013 /* We can elide the fde allocation if we're not emitting debug info. */
4014 if (! dwarf2out_do_frame ())
4015 return;
4016 #endif
4018 /* Expand the fde table if necessary. */
4019 if (fde_table_in_use == fde_table_allocated)
4021 fde_table_allocated += FDE_TABLE_INCREMENT;
4022 fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4023 memset (fde_table + fde_table_in_use, 0,
4024 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4027 /* Record the FDE associated with this function. */
4028 current_funcdef_fde = fde_table_in_use;
4030 /* Add the new FDE at the end of the fde_table. */
4031 fde = &fde_table[fde_table_in_use++];
4032 fde->decl = current_function_decl;
4033 fde->dw_fde_begin = dup_label;
4034 fde->dw_fde_current_label = dup_label;
4035 fde->dw_fde_hot_section_label = NULL;
4036 fde->dw_fde_hot_section_end_label = NULL;
4037 fde->dw_fde_unlikely_section_label = NULL;
4038 fde->dw_fde_unlikely_section_end_label = NULL;
4039 fde->dw_fde_switched_sections = 0;
4040 fde->dw_fde_switched_cold_to_hot = 0;
4041 fde->dw_fde_end = NULL;
4042 fde->dw_fde_vms_end_prologue = NULL;
4043 fde->dw_fde_vms_begin_epilogue = NULL;
4044 fde->dw_fde_cfi = NULL;
4045 fde->dw_fde_switch_cfi = NULL;
4046 fde->funcdef_number = current_function_funcdef_no;
4047 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4048 fde->uses_eh_lsda = crtl->uses_eh_lsda;
4049 fde->nothrow = crtl->nothrow;
4050 fde->drap_reg = INVALID_REGNUM;
4051 fde->vdrap_reg = INVALID_REGNUM;
4052 if (flag_reorder_blocks_and_partition)
4054 section *unlikelysec;
4055 if (first_function_block_is_cold)
4056 fde->in_std_section = 1;
4057 else
4058 fde->in_std_section
4059 = (fnsec == text_section
4060 || (cold_text_section && fnsec == cold_text_section));
4061 unlikelysec = unlikely_text_section ();
4062 fde->cold_in_std_section
4063 = (unlikelysec == text_section
4064 || (cold_text_section && unlikelysec == cold_text_section));
4066 else
4068 fde->in_std_section
4069 = (fnsec == text_section
4070 || (cold_text_section && fnsec == cold_text_section));
4071 fde->cold_in_std_section = 0;
4074 args_size = old_args_size = 0;
4076 /* We only want to output line number information for the genuine dwarf2
4077 prologue case, not the eh frame case. */
4078 #ifdef DWARF2_DEBUGGING_INFO
4079 if (file)
4080 dwarf2out_source_line (line, file, 0, true);
4081 #endif
4083 if (dwarf2out_do_cfi_asm ())
4084 dwarf2out_do_cfi_startproc (false);
4085 else
4087 rtx personality = get_personality_function (current_function_decl);
4088 if (!current_unit_personality)
4089 current_unit_personality = personality;
4091 /* We cannot keep a current personality per function as without CFI
4092 asm, at the point where we emit the CFI data, there is no current
4093 function anymore. */
4094 if (personality && current_unit_personality != personality)
4095 sorry ("multiple EH personalities are supported only with assemblers "
4096 "supporting .cfi_personality directive");
4100 /* Output a marker (i.e. a label) for the end of the generated code
4101 for a function prologue. This gets called *after* the prologue code has
4102 been generated. */
4104 void
4105 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4106 const char *file ATTRIBUTE_UNUSED)
4108 dw_fde_ref fde;
4109 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4111 /* Output a label to mark the endpoint of the code generated for this
4112 function. */
4113 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4114 current_function_funcdef_no);
4115 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4116 current_function_funcdef_no);
4117 fde = &fde_table[fde_table_in_use - 1];
4118 fde->dw_fde_vms_end_prologue = xstrdup (label);
4121 /* Output a marker (i.e. a label) for the beginning of the generated code
4122 for a function epilogue. This gets called *before* the prologue code has
4123 been generated. */
4125 void
4126 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4127 const char *file ATTRIBUTE_UNUSED)
4129 dw_fde_ref fde;
4130 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4132 fde = &fde_table[fde_table_in_use - 1];
4133 if (fde->dw_fde_vms_begin_epilogue)
4134 return;
4136 /* Output a label to mark the endpoint of the code generated for this
4137 function. */
4138 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4139 current_function_funcdef_no);
4140 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4141 current_function_funcdef_no);
4142 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4145 /* Output a marker (i.e. a label) for the absolute end of the generated code
4146 for a function definition. This gets called *after* the epilogue code has
4147 been generated. */
4149 void
4150 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4151 const char *file ATTRIBUTE_UNUSED)
4153 dw_fde_ref fde;
4154 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4156 last_var_location_insn = NULL_RTX;
4158 if (dwarf2out_do_cfi_asm ())
4159 fprintf (asm_out_file, "\t.cfi_endproc\n");
4161 /* Output a label to mark the endpoint of the code generated for this
4162 function. */
4163 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4164 current_function_funcdef_no);
4165 ASM_OUTPUT_LABEL (asm_out_file, label);
4166 fde = current_fde ();
4167 gcc_assert (fde != NULL);
4168 fde->dw_fde_end = xstrdup (label);
4171 void
4172 dwarf2out_frame_init (void)
4174 /* Allocate the initial hunk of the fde_table. */
4175 fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4176 fde_table_allocated = FDE_TABLE_INCREMENT;
4177 fde_table_in_use = 0;
4179 /* Generate the CFA instructions common to all FDE's. Do it now for the
4180 sake of lookup_cfa. */
4182 /* On entry, the Canonical Frame Address is at SP. */
4183 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4185 if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4186 initial_return_save (INCOMING_RETURN_ADDR_RTX);
4189 void
4190 dwarf2out_frame_finish (void)
4192 /* Output call frame information. */
4193 if (DWARF2_FRAME_INFO)
4194 output_call_frame_info (0);
4196 #ifndef TARGET_UNWIND_INFO
4197 /* Output another copy for the unwinder. */
4198 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4199 output_call_frame_info (1);
4200 #endif
4203 /* Note that the current function section is being used for code. */
4205 static void
4206 dwarf2out_note_section_used (void)
4208 section *sec = current_function_section ();
4209 if (sec == text_section)
4210 text_section_used = true;
4211 else if (sec == cold_text_section)
4212 cold_text_section_used = true;
4215 void
4216 dwarf2out_switch_text_section (void)
4218 dw_fde_ref fde = current_fde ();
4220 gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4222 fde->dw_fde_switched_sections = 1;
4223 fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4225 fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4226 fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4227 fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4228 fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4229 have_multiple_function_sections = true;
4231 /* Reset the current label on switching text sections, so that we
4232 don't attempt to advance_loc4 between labels in different sections. */
4233 fde->dw_fde_current_label = NULL;
4235 /* There is no need to mark used sections when not debugging. */
4236 if (cold_text_section != NULL)
4237 dwarf2out_note_section_used ();
4239 if (dwarf2out_do_cfi_asm ())
4240 fprintf (asm_out_file, "\t.cfi_endproc\n");
4242 /* Now do the real section switch. */
4243 switch_to_section (current_function_section ());
4245 if (dwarf2out_do_cfi_asm ())
4247 dwarf2out_do_cfi_startproc (true);
4248 /* As this is a different FDE, insert all current CFI instructions
4249 again. */
4250 output_cfis (fde->dw_fde_cfi, true, fde, true);
4252 else
4254 dw_cfi_ref cfi = fde->dw_fde_cfi;
4256 cfi = fde->dw_fde_cfi;
4257 if (cfi)
4258 while (cfi->dw_cfi_next != NULL)
4259 cfi = cfi->dw_cfi_next;
4260 fde->dw_fde_switch_cfi = cfi;
4264 /* And now, the subset of the debugging information support code necessary
4265 for emitting location expressions. */
4267 /* Data about a single source file. */
4268 struct GTY(()) dwarf_file_data {
4269 const char * filename;
4270 int emitted_number;
4273 typedef struct dw_val_struct *dw_val_ref;
4274 typedef struct die_struct *dw_die_ref;
4275 typedef const struct die_struct *const_dw_die_ref;
4276 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4277 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4279 typedef struct GTY(()) deferred_locations_struct
4281 tree variable;
4282 dw_die_ref die;
4283 } deferred_locations;
4285 DEF_VEC_O(deferred_locations);
4286 DEF_VEC_ALLOC_O(deferred_locations,gc);
4288 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4290 DEF_VEC_P(dw_die_ref);
4291 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4293 /* Each DIE may have a series of attribute/value pairs. Values
4294 can take on several forms. The forms that are used in this
4295 implementation are listed below. */
4297 enum dw_val_class
4299 dw_val_class_addr,
4300 dw_val_class_offset,
4301 dw_val_class_loc,
4302 dw_val_class_loc_list,
4303 dw_val_class_range_list,
4304 dw_val_class_const,
4305 dw_val_class_unsigned_const,
4306 dw_val_class_const_double,
4307 dw_val_class_vec,
4308 dw_val_class_flag,
4309 dw_val_class_die_ref,
4310 dw_val_class_fde_ref,
4311 dw_val_class_lbl_id,
4312 dw_val_class_lineptr,
4313 dw_val_class_str,
4314 dw_val_class_macptr,
4315 dw_val_class_file,
4316 dw_val_class_data8,
4317 dw_val_class_decl_ref,
4318 dw_val_class_vms_delta
4321 /* Describe a floating point constant value, or a vector constant value. */
4323 typedef struct GTY(()) dw_vec_struct {
4324 unsigned char * GTY((length ("%h.length"))) array;
4325 unsigned length;
4326 unsigned elt_size;
4328 dw_vec_const;
4330 /* The dw_val_node describes an attribute's value, as it is
4331 represented internally. */
4333 typedef struct GTY(()) dw_val_struct {
4334 enum dw_val_class val_class;
4335 union dw_val_struct_union
4337 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4338 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4339 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4340 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4341 HOST_WIDE_INT GTY ((default)) val_int;
4342 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4343 double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4344 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4345 struct dw_val_die_union
4347 dw_die_ref die;
4348 int external;
4349 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4350 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4351 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4352 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4353 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4354 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4355 unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4356 tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4357 struct dw_val_vms_delta_union
4359 char * lbl1;
4360 char * lbl2;
4361 } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4363 GTY ((desc ("%1.val_class"))) v;
4365 dw_val_node;
4367 /* Locations in memory are described using a sequence of stack machine
4368 operations. */
4370 typedef struct GTY(()) dw_loc_descr_struct {
4371 dw_loc_descr_ref dw_loc_next;
4372 ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4373 /* Used to distinguish DW_OP_addr with a direct symbol relocation
4374 from DW_OP_addr with a dtp-relative symbol relocation. */
4375 unsigned int dtprel : 1;
4376 int dw_loc_addr;
4377 dw_val_node dw_loc_oprnd1;
4378 dw_val_node dw_loc_oprnd2;
4380 dw_loc_descr_node;
4382 /* Location lists are ranges + location descriptions for that range,
4383 so you can track variables that are in different places over
4384 their entire life. */
4385 typedef struct GTY(()) dw_loc_list_struct {
4386 dw_loc_list_ref dw_loc_next;
4387 const char *begin; /* Label for begin address of range */
4388 const char *end; /* Label for end address of range */
4389 char *ll_symbol; /* Label for beginning of location list.
4390 Only on head of list */
4391 const char *section; /* Section this loclist is relative to */
4392 dw_loc_descr_ref expr;
4393 } dw_loc_list_node;
4395 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4397 /* Convert a DWARF stack opcode into its string name. */
4399 static const char *
4400 dwarf_stack_op_name (unsigned int op)
4402 switch (op)
4404 case DW_OP_addr:
4405 return "DW_OP_addr";
4406 case DW_OP_deref:
4407 return "DW_OP_deref";
4408 case DW_OP_const1u:
4409 return "DW_OP_const1u";
4410 case DW_OP_const1s:
4411 return "DW_OP_const1s";
4412 case DW_OP_const2u:
4413 return "DW_OP_const2u";
4414 case DW_OP_const2s:
4415 return "DW_OP_const2s";
4416 case DW_OP_const4u:
4417 return "DW_OP_const4u";
4418 case DW_OP_const4s:
4419 return "DW_OP_const4s";
4420 case DW_OP_const8u:
4421 return "DW_OP_const8u";
4422 case DW_OP_const8s:
4423 return "DW_OP_const8s";
4424 case DW_OP_constu:
4425 return "DW_OP_constu";
4426 case DW_OP_consts:
4427 return "DW_OP_consts";
4428 case DW_OP_dup:
4429 return "DW_OP_dup";
4430 case DW_OP_drop:
4431 return "DW_OP_drop";
4432 case DW_OP_over:
4433 return "DW_OP_over";
4434 case DW_OP_pick:
4435 return "DW_OP_pick";
4436 case DW_OP_swap:
4437 return "DW_OP_swap";
4438 case DW_OP_rot:
4439 return "DW_OP_rot";
4440 case DW_OP_xderef:
4441 return "DW_OP_xderef";
4442 case DW_OP_abs:
4443 return "DW_OP_abs";
4444 case DW_OP_and:
4445 return "DW_OP_and";
4446 case DW_OP_div:
4447 return "DW_OP_div";
4448 case DW_OP_minus:
4449 return "DW_OP_minus";
4450 case DW_OP_mod:
4451 return "DW_OP_mod";
4452 case DW_OP_mul:
4453 return "DW_OP_mul";
4454 case DW_OP_neg:
4455 return "DW_OP_neg";
4456 case DW_OP_not:
4457 return "DW_OP_not";
4458 case DW_OP_or:
4459 return "DW_OP_or";
4460 case DW_OP_plus:
4461 return "DW_OP_plus";
4462 case DW_OP_plus_uconst:
4463 return "DW_OP_plus_uconst";
4464 case DW_OP_shl:
4465 return "DW_OP_shl";
4466 case DW_OP_shr:
4467 return "DW_OP_shr";
4468 case DW_OP_shra:
4469 return "DW_OP_shra";
4470 case DW_OP_xor:
4471 return "DW_OP_xor";
4472 case DW_OP_bra:
4473 return "DW_OP_bra";
4474 case DW_OP_eq:
4475 return "DW_OP_eq";
4476 case DW_OP_ge:
4477 return "DW_OP_ge";
4478 case DW_OP_gt:
4479 return "DW_OP_gt";
4480 case DW_OP_le:
4481 return "DW_OP_le";
4482 case DW_OP_lt:
4483 return "DW_OP_lt";
4484 case DW_OP_ne:
4485 return "DW_OP_ne";
4486 case DW_OP_skip:
4487 return "DW_OP_skip";
4488 case DW_OP_lit0:
4489 return "DW_OP_lit0";
4490 case DW_OP_lit1:
4491 return "DW_OP_lit1";
4492 case DW_OP_lit2:
4493 return "DW_OP_lit2";
4494 case DW_OP_lit3:
4495 return "DW_OP_lit3";
4496 case DW_OP_lit4:
4497 return "DW_OP_lit4";
4498 case DW_OP_lit5:
4499 return "DW_OP_lit5";
4500 case DW_OP_lit6:
4501 return "DW_OP_lit6";
4502 case DW_OP_lit7:
4503 return "DW_OP_lit7";
4504 case DW_OP_lit8:
4505 return "DW_OP_lit8";
4506 case DW_OP_lit9:
4507 return "DW_OP_lit9";
4508 case DW_OP_lit10:
4509 return "DW_OP_lit10";
4510 case DW_OP_lit11:
4511 return "DW_OP_lit11";
4512 case DW_OP_lit12:
4513 return "DW_OP_lit12";
4514 case DW_OP_lit13:
4515 return "DW_OP_lit13";
4516 case DW_OP_lit14:
4517 return "DW_OP_lit14";
4518 case DW_OP_lit15:
4519 return "DW_OP_lit15";
4520 case DW_OP_lit16:
4521 return "DW_OP_lit16";
4522 case DW_OP_lit17:
4523 return "DW_OP_lit17";
4524 case DW_OP_lit18:
4525 return "DW_OP_lit18";
4526 case DW_OP_lit19:
4527 return "DW_OP_lit19";
4528 case DW_OP_lit20:
4529 return "DW_OP_lit20";
4530 case DW_OP_lit21:
4531 return "DW_OP_lit21";
4532 case DW_OP_lit22:
4533 return "DW_OP_lit22";
4534 case DW_OP_lit23:
4535 return "DW_OP_lit23";
4536 case DW_OP_lit24:
4537 return "DW_OP_lit24";
4538 case DW_OP_lit25:
4539 return "DW_OP_lit25";
4540 case DW_OP_lit26:
4541 return "DW_OP_lit26";
4542 case DW_OP_lit27:
4543 return "DW_OP_lit27";
4544 case DW_OP_lit28:
4545 return "DW_OP_lit28";
4546 case DW_OP_lit29:
4547 return "DW_OP_lit29";
4548 case DW_OP_lit30:
4549 return "DW_OP_lit30";
4550 case DW_OP_lit31:
4551 return "DW_OP_lit31";
4552 case DW_OP_reg0:
4553 return "DW_OP_reg0";
4554 case DW_OP_reg1:
4555 return "DW_OP_reg1";
4556 case DW_OP_reg2:
4557 return "DW_OP_reg2";
4558 case DW_OP_reg3:
4559 return "DW_OP_reg3";
4560 case DW_OP_reg4:
4561 return "DW_OP_reg4";
4562 case DW_OP_reg5:
4563 return "DW_OP_reg5";
4564 case DW_OP_reg6:
4565 return "DW_OP_reg6";
4566 case DW_OP_reg7:
4567 return "DW_OP_reg7";
4568 case DW_OP_reg8:
4569 return "DW_OP_reg8";
4570 case DW_OP_reg9:
4571 return "DW_OP_reg9";
4572 case DW_OP_reg10:
4573 return "DW_OP_reg10";
4574 case DW_OP_reg11:
4575 return "DW_OP_reg11";
4576 case DW_OP_reg12:
4577 return "DW_OP_reg12";
4578 case DW_OP_reg13:
4579 return "DW_OP_reg13";
4580 case DW_OP_reg14:
4581 return "DW_OP_reg14";
4582 case DW_OP_reg15:
4583 return "DW_OP_reg15";
4584 case DW_OP_reg16:
4585 return "DW_OP_reg16";
4586 case DW_OP_reg17:
4587 return "DW_OP_reg17";
4588 case DW_OP_reg18:
4589 return "DW_OP_reg18";
4590 case DW_OP_reg19:
4591 return "DW_OP_reg19";
4592 case DW_OP_reg20:
4593 return "DW_OP_reg20";
4594 case DW_OP_reg21:
4595 return "DW_OP_reg21";
4596 case DW_OP_reg22:
4597 return "DW_OP_reg22";
4598 case DW_OP_reg23:
4599 return "DW_OP_reg23";
4600 case DW_OP_reg24:
4601 return "DW_OP_reg24";
4602 case DW_OP_reg25:
4603 return "DW_OP_reg25";
4604 case DW_OP_reg26:
4605 return "DW_OP_reg26";
4606 case DW_OP_reg27:
4607 return "DW_OP_reg27";
4608 case DW_OP_reg28:
4609 return "DW_OP_reg28";
4610 case DW_OP_reg29:
4611 return "DW_OP_reg29";
4612 case DW_OP_reg30:
4613 return "DW_OP_reg30";
4614 case DW_OP_reg31:
4615 return "DW_OP_reg31";
4616 case DW_OP_breg0:
4617 return "DW_OP_breg0";
4618 case DW_OP_breg1:
4619 return "DW_OP_breg1";
4620 case DW_OP_breg2:
4621 return "DW_OP_breg2";
4622 case DW_OP_breg3:
4623 return "DW_OP_breg3";
4624 case DW_OP_breg4:
4625 return "DW_OP_breg4";
4626 case DW_OP_breg5:
4627 return "DW_OP_breg5";
4628 case DW_OP_breg6:
4629 return "DW_OP_breg6";
4630 case DW_OP_breg7:
4631 return "DW_OP_breg7";
4632 case DW_OP_breg8:
4633 return "DW_OP_breg8";
4634 case DW_OP_breg9:
4635 return "DW_OP_breg9";
4636 case DW_OP_breg10:
4637 return "DW_OP_breg10";
4638 case DW_OP_breg11:
4639 return "DW_OP_breg11";
4640 case DW_OP_breg12:
4641 return "DW_OP_breg12";
4642 case DW_OP_breg13:
4643 return "DW_OP_breg13";
4644 case DW_OP_breg14:
4645 return "DW_OP_breg14";
4646 case DW_OP_breg15:
4647 return "DW_OP_breg15";
4648 case DW_OP_breg16:
4649 return "DW_OP_breg16";
4650 case DW_OP_breg17:
4651 return "DW_OP_breg17";
4652 case DW_OP_breg18:
4653 return "DW_OP_breg18";
4654 case DW_OP_breg19:
4655 return "DW_OP_breg19";
4656 case DW_OP_breg20:
4657 return "DW_OP_breg20";
4658 case DW_OP_breg21:
4659 return "DW_OP_breg21";
4660 case DW_OP_breg22:
4661 return "DW_OP_breg22";
4662 case DW_OP_breg23:
4663 return "DW_OP_breg23";
4664 case DW_OP_breg24:
4665 return "DW_OP_breg24";
4666 case DW_OP_breg25:
4667 return "DW_OP_breg25";
4668 case DW_OP_breg26:
4669 return "DW_OP_breg26";
4670 case DW_OP_breg27:
4671 return "DW_OP_breg27";
4672 case DW_OP_breg28:
4673 return "DW_OP_breg28";
4674 case DW_OP_breg29:
4675 return "DW_OP_breg29";
4676 case DW_OP_breg30:
4677 return "DW_OP_breg30";
4678 case DW_OP_breg31:
4679 return "DW_OP_breg31";
4680 case DW_OP_regx:
4681 return "DW_OP_regx";
4682 case DW_OP_fbreg:
4683 return "DW_OP_fbreg";
4684 case DW_OP_bregx:
4685 return "DW_OP_bregx";
4686 case DW_OP_piece:
4687 return "DW_OP_piece";
4688 case DW_OP_deref_size:
4689 return "DW_OP_deref_size";
4690 case DW_OP_xderef_size:
4691 return "DW_OP_xderef_size";
4692 case DW_OP_nop:
4693 return "DW_OP_nop";
4695 case DW_OP_push_object_address:
4696 return "DW_OP_push_object_address";
4697 case DW_OP_call2:
4698 return "DW_OP_call2";
4699 case DW_OP_call4:
4700 return "DW_OP_call4";
4701 case DW_OP_call_ref:
4702 return "DW_OP_call_ref";
4703 case DW_OP_implicit_value:
4704 return "DW_OP_implicit_value";
4705 case DW_OP_stack_value:
4706 return "DW_OP_stack_value";
4707 case DW_OP_form_tls_address:
4708 return "DW_OP_form_tls_address";
4709 case DW_OP_call_frame_cfa:
4710 return "DW_OP_call_frame_cfa";
4711 case DW_OP_bit_piece:
4712 return "DW_OP_bit_piece";
4714 case DW_OP_GNU_push_tls_address:
4715 return "DW_OP_GNU_push_tls_address";
4716 case DW_OP_GNU_uninit:
4717 return "DW_OP_GNU_uninit";
4718 case DW_OP_GNU_encoded_addr:
4719 return "DW_OP_GNU_encoded_addr";
4720 case DW_OP_GNU_implicit_pointer:
4721 return "DW_OP_GNU_implicit_pointer";
4723 default:
4724 return "OP_<unknown>";
4728 /* Return a pointer to a newly allocated location description. Location
4729 descriptions are simple expression terms that can be strung
4730 together to form more complicated location (address) descriptions. */
4732 static inline dw_loc_descr_ref
4733 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4734 unsigned HOST_WIDE_INT oprnd2)
4736 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4738 descr->dw_loc_opc = op;
4739 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4740 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4741 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4742 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4744 return descr;
4747 /* Return a pointer to a newly allocated location description for
4748 REG and OFFSET. */
4750 static inline dw_loc_descr_ref
4751 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
4753 if (reg <= 31)
4754 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4755 offset, 0);
4756 else
4757 return new_loc_descr (DW_OP_bregx, reg, offset);
4760 /* Add a location description term to a location description expression. */
4762 static inline void
4763 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4765 dw_loc_descr_ref *d;
4767 /* Find the end of the chain. */
4768 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4771 *d = descr;
4774 /* Add a constant OFFSET to a location expression. */
4776 static void
4777 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4779 dw_loc_descr_ref loc;
4780 HOST_WIDE_INT *p;
4782 gcc_assert (*list_head != NULL);
4784 if (!offset)
4785 return;
4787 /* Find the end of the chain. */
4788 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4791 p = NULL;
4792 if (loc->dw_loc_opc == DW_OP_fbreg
4793 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4794 p = &loc->dw_loc_oprnd1.v.val_int;
4795 else if (loc->dw_loc_opc == DW_OP_bregx)
4796 p = &loc->dw_loc_oprnd2.v.val_int;
4798 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4799 offset. Don't optimize if an signed integer overflow would happen. */
4800 if (p != NULL
4801 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4802 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4803 *p += offset;
4805 else if (offset > 0)
4806 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4808 else
4810 loc->dw_loc_next = int_loc_descriptor (-offset);
4811 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4815 /* Add a constant OFFSET to a location list. */
4817 static void
4818 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4820 dw_loc_list_ref d;
4821 for (d = list_head; d != NULL; d = d->dw_loc_next)
4822 loc_descr_plus_const (&d->expr, offset);
4825 #define DWARF_REF_SIZE \
4826 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4828 /* Return the size of a location descriptor. */
4830 static unsigned long
4831 size_of_loc_descr (dw_loc_descr_ref loc)
4833 unsigned long size = 1;
4835 switch (loc->dw_loc_opc)
4837 case DW_OP_addr:
4838 size += DWARF2_ADDR_SIZE;
4839 break;
4840 case DW_OP_const1u:
4841 case DW_OP_const1s:
4842 size += 1;
4843 break;
4844 case DW_OP_const2u:
4845 case DW_OP_const2s:
4846 size += 2;
4847 break;
4848 case DW_OP_const4u:
4849 case DW_OP_const4s:
4850 size += 4;
4851 break;
4852 case DW_OP_const8u:
4853 case DW_OP_const8s:
4854 size += 8;
4855 break;
4856 case DW_OP_constu:
4857 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4858 break;
4859 case DW_OP_consts:
4860 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4861 break;
4862 case DW_OP_pick:
4863 size += 1;
4864 break;
4865 case DW_OP_plus_uconst:
4866 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4867 break;
4868 case DW_OP_skip:
4869 case DW_OP_bra:
4870 size += 2;
4871 break;
4872 case DW_OP_breg0:
4873 case DW_OP_breg1:
4874 case DW_OP_breg2:
4875 case DW_OP_breg3:
4876 case DW_OP_breg4:
4877 case DW_OP_breg5:
4878 case DW_OP_breg6:
4879 case DW_OP_breg7:
4880 case DW_OP_breg8:
4881 case DW_OP_breg9:
4882 case DW_OP_breg10:
4883 case DW_OP_breg11:
4884 case DW_OP_breg12:
4885 case DW_OP_breg13:
4886 case DW_OP_breg14:
4887 case DW_OP_breg15:
4888 case DW_OP_breg16:
4889 case DW_OP_breg17:
4890 case DW_OP_breg18:
4891 case DW_OP_breg19:
4892 case DW_OP_breg20:
4893 case DW_OP_breg21:
4894 case DW_OP_breg22:
4895 case DW_OP_breg23:
4896 case DW_OP_breg24:
4897 case DW_OP_breg25:
4898 case DW_OP_breg26:
4899 case DW_OP_breg27:
4900 case DW_OP_breg28:
4901 case DW_OP_breg29:
4902 case DW_OP_breg30:
4903 case DW_OP_breg31:
4904 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4905 break;
4906 case DW_OP_regx:
4907 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4908 break;
4909 case DW_OP_fbreg:
4910 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4911 break;
4912 case DW_OP_bregx:
4913 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4914 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4915 break;
4916 case DW_OP_piece:
4917 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4918 break;
4919 case DW_OP_bit_piece:
4920 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4921 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4922 break;
4923 case DW_OP_deref_size:
4924 case DW_OP_xderef_size:
4925 size += 1;
4926 break;
4927 case DW_OP_call2:
4928 size += 2;
4929 break;
4930 case DW_OP_call4:
4931 size += 4;
4932 break;
4933 case DW_OP_call_ref:
4934 size += DWARF_REF_SIZE;
4935 break;
4936 case DW_OP_implicit_value:
4937 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4938 + loc->dw_loc_oprnd1.v.val_unsigned;
4939 break;
4940 case DW_OP_GNU_implicit_pointer:
4941 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4942 break;
4943 default:
4944 break;
4947 return size;
4950 /* Return the size of a series of location descriptors. */
4952 static unsigned long
4953 size_of_locs (dw_loc_descr_ref loc)
4955 dw_loc_descr_ref l;
4956 unsigned long size;
4958 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4959 field, to avoid writing to a PCH file. */
4960 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4962 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4963 break;
4964 size += size_of_loc_descr (l);
4966 if (! l)
4967 return size;
4969 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4971 l->dw_loc_addr = size;
4972 size += size_of_loc_descr (l);
4975 return size;
4978 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4979 static void get_ref_die_offset_label (char *, dw_die_ref);
4981 /* Output location description stack opcode's operands (if any). */
4983 static void
4984 output_loc_operands (dw_loc_descr_ref loc)
4986 dw_val_ref val1 = &loc->dw_loc_oprnd1;
4987 dw_val_ref val2 = &loc->dw_loc_oprnd2;
4989 switch (loc->dw_loc_opc)
4991 #ifdef DWARF2_DEBUGGING_INFO
4992 case DW_OP_const2u:
4993 case DW_OP_const2s:
4994 dw2_asm_output_data (2, val1->v.val_int, NULL);
4995 break;
4996 case DW_OP_const4u:
4997 if (loc->dtprel)
4999 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5000 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5001 val1->v.val_addr);
5002 fputc ('\n', asm_out_file);
5003 break;
5005 /* FALLTHRU */
5006 case DW_OP_const4s:
5007 dw2_asm_output_data (4, val1->v.val_int, NULL);
5008 break;
5009 case DW_OP_const8u:
5010 if (loc->dtprel)
5012 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5013 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5014 val1->v.val_addr);
5015 fputc ('\n', asm_out_file);
5016 break;
5018 /* FALLTHRU */
5019 case DW_OP_const8s:
5020 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5021 dw2_asm_output_data (8, val1->v.val_int, NULL);
5022 break;
5023 case DW_OP_skip:
5024 case DW_OP_bra:
5026 int offset;
5028 gcc_assert (val1->val_class == dw_val_class_loc);
5029 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5031 dw2_asm_output_data (2, offset, NULL);
5033 break;
5034 case DW_OP_implicit_value:
5035 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5036 switch (val2->val_class)
5038 case dw_val_class_const:
5039 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5040 break;
5041 case dw_val_class_vec:
5043 unsigned int elt_size = val2->v.val_vec.elt_size;
5044 unsigned int len = val2->v.val_vec.length;
5045 unsigned int i;
5046 unsigned char *p;
5048 if (elt_size > sizeof (HOST_WIDE_INT))
5050 elt_size /= 2;
5051 len *= 2;
5053 for (i = 0, p = val2->v.val_vec.array;
5054 i < len;
5055 i++, p += elt_size)
5056 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5057 "fp or vector constant word %u", i);
5059 break;
5060 case dw_val_class_const_double:
5062 unsigned HOST_WIDE_INT first, second;
5064 if (WORDS_BIG_ENDIAN)
5066 first = val2->v.val_double.high;
5067 second = val2->v.val_double.low;
5069 else
5071 first = val2->v.val_double.low;
5072 second = val2->v.val_double.high;
5074 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5075 first, NULL);
5076 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5077 second, NULL);
5079 break;
5080 case dw_val_class_addr:
5081 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5082 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5083 break;
5084 default:
5085 gcc_unreachable ();
5087 break;
5088 #else
5089 case DW_OP_const2u:
5090 case DW_OP_const2s:
5091 case DW_OP_const4u:
5092 case DW_OP_const4s:
5093 case DW_OP_const8u:
5094 case DW_OP_const8s:
5095 case DW_OP_skip:
5096 case DW_OP_bra:
5097 case DW_OP_implicit_value:
5098 /* We currently don't make any attempt to make sure these are
5099 aligned properly like we do for the main unwind info, so
5100 don't support emitting things larger than a byte if we're
5101 only doing unwinding. */
5102 gcc_unreachable ();
5103 #endif
5104 case DW_OP_const1u:
5105 case DW_OP_const1s:
5106 dw2_asm_output_data (1, val1->v.val_int, NULL);
5107 break;
5108 case DW_OP_constu:
5109 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5110 break;
5111 case DW_OP_consts:
5112 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5113 break;
5114 case DW_OP_pick:
5115 dw2_asm_output_data (1, val1->v.val_int, NULL);
5116 break;
5117 case DW_OP_plus_uconst:
5118 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5119 break;
5120 case DW_OP_breg0:
5121 case DW_OP_breg1:
5122 case DW_OP_breg2:
5123 case DW_OP_breg3:
5124 case DW_OP_breg4:
5125 case DW_OP_breg5:
5126 case DW_OP_breg6:
5127 case DW_OP_breg7:
5128 case DW_OP_breg8:
5129 case DW_OP_breg9:
5130 case DW_OP_breg10:
5131 case DW_OP_breg11:
5132 case DW_OP_breg12:
5133 case DW_OP_breg13:
5134 case DW_OP_breg14:
5135 case DW_OP_breg15:
5136 case DW_OP_breg16:
5137 case DW_OP_breg17:
5138 case DW_OP_breg18:
5139 case DW_OP_breg19:
5140 case DW_OP_breg20:
5141 case DW_OP_breg21:
5142 case DW_OP_breg22:
5143 case DW_OP_breg23:
5144 case DW_OP_breg24:
5145 case DW_OP_breg25:
5146 case DW_OP_breg26:
5147 case DW_OP_breg27:
5148 case DW_OP_breg28:
5149 case DW_OP_breg29:
5150 case DW_OP_breg30:
5151 case DW_OP_breg31:
5152 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5153 break;
5154 case DW_OP_regx:
5155 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5156 break;
5157 case DW_OP_fbreg:
5158 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5159 break;
5160 case DW_OP_bregx:
5161 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5162 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5163 break;
5164 case DW_OP_piece:
5165 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5166 break;
5167 case DW_OP_bit_piece:
5168 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5169 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5170 break;
5171 case DW_OP_deref_size:
5172 case DW_OP_xderef_size:
5173 dw2_asm_output_data (1, val1->v.val_int, NULL);
5174 break;
5176 case DW_OP_addr:
5177 if (loc->dtprel)
5179 if (targetm.asm_out.output_dwarf_dtprel)
5181 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5182 DWARF2_ADDR_SIZE,
5183 val1->v.val_addr);
5184 fputc ('\n', asm_out_file);
5186 else
5187 gcc_unreachable ();
5189 else
5191 #ifdef DWARF2_DEBUGGING_INFO
5192 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5193 #else
5194 gcc_unreachable ();
5195 #endif
5197 break;
5199 case DW_OP_GNU_implicit_pointer:
5201 char label[MAX_ARTIFICIAL_LABEL_BYTES
5202 + HOST_BITS_PER_WIDE_INT / 2 + 2];
5203 gcc_assert (val1->val_class == dw_val_class_die_ref);
5204 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5205 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5206 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5208 break;
5210 default:
5211 /* Other codes have no operands. */
5212 break;
5216 /* Output a sequence of location operations. */
5218 static void
5219 output_loc_sequence (dw_loc_descr_ref loc)
5221 for (; loc != NULL; loc = loc->dw_loc_next)
5223 /* Output the opcode. */
5224 dw2_asm_output_data (1, loc->dw_loc_opc,
5225 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5227 /* Output the operand(s) (if any). */
5228 output_loc_operands (loc);
5232 /* Output location description stack opcode's operands (if any).
5233 The output is single bytes on a line, suitable for .cfi_escape. */
5235 static void
5236 output_loc_operands_raw (dw_loc_descr_ref loc)
5238 dw_val_ref val1 = &loc->dw_loc_oprnd1;
5239 dw_val_ref val2 = &loc->dw_loc_oprnd2;
5241 switch (loc->dw_loc_opc)
5243 case DW_OP_addr:
5244 case DW_OP_implicit_value:
5245 /* We cannot output addresses in .cfi_escape, only bytes. */
5246 gcc_unreachable ();
5248 case DW_OP_const1u:
5249 case DW_OP_const1s:
5250 case DW_OP_pick:
5251 case DW_OP_deref_size:
5252 case DW_OP_xderef_size:
5253 fputc (',', asm_out_file);
5254 dw2_asm_output_data_raw (1, val1->v.val_int);
5255 break;
5257 case DW_OP_const2u:
5258 case DW_OP_const2s:
5259 fputc (',', asm_out_file);
5260 dw2_asm_output_data_raw (2, val1->v.val_int);
5261 break;
5263 case DW_OP_const4u:
5264 case DW_OP_const4s:
5265 fputc (',', asm_out_file);
5266 dw2_asm_output_data_raw (4, val1->v.val_int);
5267 break;
5269 case DW_OP_const8u:
5270 case DW_OP_const8s:
5271 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5272 fputc (',', asm_out_file);
5273 dw2_asm_output_data_raw (8, val1->v.val_int);
5274 break;
5276 case DW_OP_skip:
5277 case DW_OP_bra:
5279 int offset;
5281 gcc_assert (val1->val_class == dw_val_class_loc);
5282 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5284 fputc (',', asm_out_file);
5285 dw2_asm_output_data_raw (2, offset);
5287 break;
5289 case DW_OP_constu:
5290 case DW_OP_plus_uconst:
5291 case DW_OP_regx:
5292 case DW_OP_piece:
5293 fputc (',', asm_out_file);
5294 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5295 break;
5297 case DW_OP_bit_piece:
5298 fputc (',', asm_out_file);
5299 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5300 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5301 break;
5303 case DW_OP_consts:
5304 case DW_OP_breg0:
5305 case DW_OP_breg1:
5306 case DW_OP_breg2:
5307 case DW_OP_breg3:
5308 case DW_OP_breg4:
5309 case DW_OP_breg5:
5310 case DW_OP_breg6:
5311 case DW_OP_breg7:
5312 case DW_OP_breg8:
5313 case DW_OP_breg9:
5314 case DW_OP_breg10:
5315 case DW_OP_breg11:
5316 case DW_OP_breg12:
5317 case DW_OP_breg13:
5318 case DW_OP_breg14:
5319 case DW_OP_breg15:
5320 case DW_OP_breg16:
5321 case DW_OP_breg17:
5322 case DW_OP_breg18:
5323 case DW_OP_breg19:
5324 case DW_OP_breg20:
5325 case DW_OP_breg21:
5326 case DW_OP_breg22:
5327 case DW_OP_breg23:
5328 case DW_OP_breg24:
5329 case DW_OP_breg25:
5330 case DW_OP_breg26:
5331 case DW_OP_breg27:
5332 case DW_OP_breg28:
5333 case DW_OP_breg29:
5334 case DW_OP_breg30:
5335 case DW_OP_breg31:
5336 case DW_OP_fbreg:
5337 fputc (',', asm_out_file);
5338 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5339 break;
5341 case DW_OP_bregx:
5342 fputc (',', asm_out_file);
5343 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5344 fputc (',', asm_out_file);
5345 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5346 break;
5348 case DW_OP_GNU_implicit_pointer:
5349 gcc_unreachable ();
5350 break;
5352 default:
5353 /* Other codes have no operands. */
5354 break;
5358 static void
5359 output_loc_sequence_raw (dw_loc_descr_ref loc)
5361 while (1)
5363 /* Output the opcode. */
5364 fprintf (asm_out_file, "%#x", loc->dw_loc_opc);
5365 output_loc_operands_raw (loc);
5367 if (!loc->dw_loc_next)
5368 break;
5369 loc = loc->dw_loc_next;
5371 fputc (',', asm_out_file);
5375 /* This routine will generate the correct assembly data for a location
5376 description based on a cfi entry with a complex address. */
5378 static void
5379 output_cfa_loc (dw_cfi_ref cfi)
5381 dw_loc_descr_ref loc;
5382 unsigned long size;
5384 if (cfi->dw_cfi_opc == DW_CFA_expression)
5386 dw2_asm_output_data (1, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, NULL);
5387 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5389 else
5390 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5392 /* Output the size of the block. */
5393 size = size_of_locs (loc);
5394 dw2_asm_output_data_uleb128 (size, NULL);
5396 /* Now output the operations themselves. */
5397 output_loc_sequence (loc);
5400 /* Similar, but used for .cfi_escape. */
5402 static void
5403 output_cfa_loc_raw (dw_cfi_ref cfi)
5405 dw_loc_descr_ref loc;
5406 unsigned long size;
5408 if (cfi->dw_cfi_opc == DW_CFA_expression)
5410 fprintf (asm_out_file, "%#x,", cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
5411 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5413 else
5414 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5416 /* Output the size of the block. */
5417 size = size_of_locs (loc);
5418 dw2_asm_output_data_uleb128_raw (size);
5419 fputc (',', asm_out_file);
5421 /* Now output the operations themselves. */
5422 output_loc_sequence_raw (loc);
5425 /* This function builds a dwarf location descriptor sequence from a
5426 dw_cfa_location, adding the given OFFSET to the result of the
5427 expression. */
5429 static struct dw_loc_descr_struct *
5430 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5432 struct dw_loc_descr_struct *head, *tmp;
5434 offset += cfa->offset;
5436 if (cfa->indirect)
5438 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5439 head->dw_loc_oprnd1.val_class = dw_val_class_const;
5440 tmp = new_loc_descr (DW_OP_deref, 0, 0);
5441 add_loc_descr (&head, tmp);
5442 if (offset != 0)
5444 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5445 add_loc_descr (&head, tmp);
5448 else
5449 head = new_reg_loc_descr (cfa->reg, offset);
5451 return head;
5454 /* This function builds a dwarf location descriptor sequence for
5455 the address at OFFSET from the CFA when stack is aligned to
5456 ALIGNMENT byte. */
5458 static struct dw_loc_descr_struct *
5459 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5461 struct dw_loc_descr_struct *head;
5462 unsigned int dwarf_fp
5463 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5465 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
5466 if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5468 head = new_reg_loc_descr (dwarf_fp, 0);
5469 add_loc_descr (&head, int_loc_descriptor (alignment));
5470 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5471 loc_descr_plus_const (&head, offset);
5473 else
5474 head = new_reg_loc_descr (dwarf_fp, offset);
5475 return head;
5478 /* This function fills in aa dw_cfa_location structure from a dwarf location
5479 descriptor sequence. */
5481 static void
5482 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5484 struct dw_loc_descr_struct *ptr;
5485 cfa->offset = 0;
5486 cfa->base_offset = 0;
5487 cfa->indirect = 0;
5488 cfa->reg = -1;
5490 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5492 enum dwarf_location_atom op = ptr->dw_loc_opc;
5494 switch (op)
5496 case DW_OP_reg0:
5497 case DW_OP_reg1:
5498 case DW_OP_reg2:
5499 case DW_OP_reg3:
5500 case DW_OP_reg4:
5501 case DW_OP_reg5:
5502 case DW_OP_reg6:
5503 case DW_OP_reg7:
5504 case DW_OP_reg8:
5505 case DW_OP_reg9:
5506 case DW_OP_reg10:
5507 case DW_OP_reg11:
5508 case DW_OP_reg12:
5509 case DW_OP_reg13:
5510 case DW_OP_reg14:
5511 case DW_OP_reg15:
5512 case DW_OP_reg16:
5513 case DW_OP_reg17:
5514 case DW_OP_reg18:
5515 case DW_OP_reg19:
5516 case DW_OP_reg20:
5517 case DW_OP_reg21:
5518 case DW_OP_reg22:
5519 case DW_OP_reg23:
5520 case DW_OP_reg24:
5521 case DW_OP_reg25:
5522 case DW_OP_reg26:
5523 case DW_OP_reg27:
5524 case DW_OP_reg28:
5525 case DW_OP_reg29:
5526 case DW_OP_reg30:
5527 case DW_OP_reg31:
5528 cfa->reg = op - DW_OP_reg0;
5529 break;
5530 case DW_OP_regx:
5531 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5532 break;
5533 case DW_OP_breg0:
5534 case DW_OP_breg1:
5535 case DW_OP_breg2:
5536 case DW_OP_breg3:
5537 case DW_OP_breg4:
5538 case DW_OP_breg5:
5539 case DW_OP_breg6:
5540 case DW_OP_breg7:
5541 case DW_OP_breg8:
5542 case DW_OP_breg9:
5543 case DW_OP_breg10:
5544 case DW_OP_breg11:
5545 case DW_OP_breg12:
5546 case DW_OP_breg13:
5547 case DW_OP_breg14:
5548 case DW_OP_breg15:
5549 case DW_OP_breg16:
5550 case DW_OP_breg17:
5551 case DW_OP_breg18:
5552 case DW_OP_breg19:
5553 case DW_OP_breg20:
5554 case DW_OP_breg21:
5555 case DW_OP_breg22:
5556 case DW_OP_breg23:
5557 case DW_OP_breg24:
5558 case DW_OP_breg25:
5559 case DW_OP_breg26:
5560 case DW_OP_breg27:
5561 case DW_OP_breg28:
5562 case DW_OP_breg29:
5563 case DW_OP_breg30:
5564 case DW_OP_breg31:
5565 cfa->reg = op - DW_OP_breg0;
5566 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5567 break;
5568 case DW_OP_bregx:
5569 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5570 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5571 break;
5572 case DW_OP_deref:
5573 cfa->indirect = 1;
5574 break;
5575 case DW_OP_plus_uconst:
5576 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5577 break;
5578 default:
5579 internal_error ("DW_LOC_OP %s not implemented",
5580 dwarf_stack_op_name (ptr->dw_loc_opc));
5585 /* And now, the support for symbolic debugging information. */
5587 /* .debug_str support. */
5588 static int output_indirect_string (void **, void *);
5590 static void dwarf2out_init (const char *);
5591 static void dwarf2out_finish (const char *);
5592 static void dwarf2out_assembly_start (void);
5593 static void dwarf2out_define (unsigned int, const char *);
5594 static void dwarf2out_undef (unsigned int, const char *);
5595 static void dwarf2out_start_source_file (unsigned, const char *);
5596 static void dwarf2out_end_source_file (unsigned);
5597 static void dwarf2out_function_decl (tree);
5598 static void dwarf2out_begin_block (unsigned, unsigned);
5599 static void dwarf2out_end_block (unsigned, unsigned);
5600 static bool dwarf2out_ignore_block (const_tree);
5601 static void dwarf2out_global_decl (tree);
5602 static void dwarf2out_type_decl (tree, int);
5603 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5604 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5605 dw_die_ref);
5606 static void dwarf2out_abstract_function (tree);
5607 static void dwarf2out_var_location (rtx);
5608 static void dwarf2out_direct_call (tree);
5609 static void dwarf2out_virtual_call_token (tree, int);
5610 static void dwarf2out_copy_call_info (rtx, rtx);
5611 static void dwarf2out_virtual_call (int);
5612 static void dwarf2out_begin_function (tree);
5613 static void dwarf2out_set_name (tree, tree);
5615 /* The debug hooks structure. */
5617 const struct gcc_debug_hooks dwarf2_debug_hooks =
5619 dwarf2out_init,
5620 dwarf2out_finish,
5621 dwarf2out_assembly_start,
5622 dwarf2out_define,
5623 dwarf2out_undef,
5624 dwarf2out_start_source_file,
5625 dwarf2out_end_source_file,
5626 dwarf2out_begin_block,
5627 dwarf2out_end_block,
5628 dwarf2out_ignore_block,
5629 dwarf2out_source_line,
5630 dwarf2out_begin_prologue,
5631 #if VMS_DEBUGGING_INFO
5632 dwarf2out_vms_end_prologue,
5633 dwarf2out_vms_begin_epilogue,
5634 #else
5635 debug_nothing_int_charstar,
5636 debug_nothing_int_charstar,
5637 #endif
5638 dwarf2out_end_epilogue,
5639 dwarf2out_begin_function,
5640 debug_nothing_int, /* end_function */
5641 dwarf2out_function_decl, /* function_decl */
5642 dwarf2out_global_decl,
5643 dwarf2out_type_decl, /* type_decl */
5644 dwarf2out_imported_module_or_decl,
5645 debug_nothing_tree, /* deferred_inline_function */
5646 /* The DWARF 2 backend tries to reduce debugging bloat by not
5647 emitting the abstract description of inline functions until
5648 something tries to reference them. */
5649 dwarf2out_abstract_function, /* outlining_inline_function */
5650 debug_nothing_rtx, /* label */
5651 debug_nothing_int, /* handle_pch */
5652 dwarf2out_var_location,
5653 dwarf2out_switch_text_section,
5654 dwarf2out_direct_call,
5655 dwarf2out_virtual_call_token,
5656 dwarf2out_copy_call_info,
5657 dwarf2out_virtual_call,
5658 dwarf2out_set_name,
5659 1 /* start_end_main_source_file */
5662 /* NOTE: In the comments in this file, many references are made to
5663 "Debugging Information Entries". This term is abbreviated as `DIE'
5664 throughout the remainder of this file. */
5666 /* An internal representation of the DWARF output is built, and then
5667 walked to generate the DWARF debugging info. The walk of the internal
5668 representation is done after the entire program has been compiled.
5669 The types below are used to describe the internal representation. */
5671 /* Various DIE's use offsets relative to the beginning of the
5672 .debug_info section to refer to each other. */
5674 typedef long int dw_offset;
5676 /* Define typedefs here to avoid circular dependencies. */
5678 typedef struct dw_attr_struct *dw_attr_ref;
5679 typedef struct dw_line_info_struct *dw_line_info_ref;
5680 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5681 typedef struct pubname_struct *pubname_ref;
5682 typedef struct dw_ranges_struct *dw_ranges_ref;
5683 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5684 typedef struct comdat_type_struct *comdat_type_node_ref;
5686 /* Each entry in the line_info_table maintains the file and
5687 line number associated with the label generated for that
5688 entry. The label gives the PC value associated with
5689 the line number entry. */
5691 typedef struct GTY(()) dw_line_info_struct {
5692 unsigned long dw_file_num;
5693 unsigned long dw_line_num;
5695 dw_line_info_entry;
5697 /* Line information for functions in separate sections; each one gets its
5698 own sequence. */
5699 typedef struct GTY(()) dw_separate_line_info_struct {
5700 unsigned long dw_file_num;
5701 unsigned long dw_line_num;
5702 unsigned long function;
5704 dw_separate_line_info_entry;
5706 /* Each DIE attribute has a field specifying the attribute kind,
5707 a link to the next attribute in the chain, and an attribute value.
5708 Attributes are typically linked below the DIE they modify. */
5710 typedef struct GTY(()) dw_attr_struct {
5711 enum dwarf_attribute dw_attr;
5712 dw_val_node dw_attr_val;
5714 dw_attr_node;
5716 DEF_VEC_O(dw_attr_node);
5717 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5719 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
5720 The children of each node form a circular list linked by
5721 die_sib. die_child points to the node *before* the "first" child node. */
5723 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5724 union die_symbol_or_type_node
5726 char * GTY ((tag ("0"))) die_symbol;
5727 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5729 GTY ((desc ("dwarf_version >= 4"))) die_id;
5730 VEC(dw_attr_node,gc) * die_attr;
5731 dw_die_ref die_parent;
5732 dw_die_ref die_child;
5733 dw_die_ref die_sib;
5734 dw_die_ref die_definition; /* ref from a specification to its definition */
5735 dw_offset die_offset;
5736 unsigned long die_abbrev;
5737 int die_mark;
5738 /* Die is used and must not be pruned as unused. */
5739 int die_perennial_p;
5740 unsigned int decl_id;
5741 enum dwarf_tag die_tag;
5743 die_node;
5745 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
5746 #define FOR_EACH_CHILD(die, c, expr) do { \
5747 c = die->die_child; \
5748 if (c) do { \
5749 c = c->die_sib; \
5750 expr; \
5751 } while (c != die->die_child); \
5752 } while (0)
5754 /* The pubname structure */
5756 typedef struct GTY(()) pubname_struct {
5757 dw_die_ref die;
5758 const char *name;
5760 pubname_entry;
5762 DEF_VEC_O(pubname_entry);
5763 DEF_VEC_ALLOC_O(pubname_entry, gc);
5765 struct GTY(()) dw_ranges_struct {
5766 /* If this is positive, it's a block number, otherwise it's a
5767 bitwise-negated index into dw_ranges_by_label. */
5768 int num;
5771 struct GTY(()) dw_ranges_by_label_struct {
5772 const char *begin;
5773 const char *end;
5776 /* The comdat type node structure. */
5777 typedef struct GTY(()) comdat_type_struct
5779 dw_die_ref root_die;
5780 dw_die_ref type_die;
5781 char signature[DWARF_TYPE_SIGNATURE_SIZE];
5782 struct comdat_type_struct *next;
5784 comdat_type_node;
5786 /* The limbo die list structure. */
5787 typedef struct GTY(()) limbo_die_struct {
5788 dw_die_ref die;
5789 tree created_for;
5790 struct limbo_die_struct *next;
5792 limbo_die_node;
5794 typedef struct GTY(()) skeleton_chain_struct
5796 dw_die_ref old_die;
5797 dw_die_ref new_die;
5798 struct skeleton_chain_struct *parent;
5800 skeleton_chain_node;
5802 /* How to start an assembler comment. */
5803 #ifndef ASM_COMMENT_START
5804 #define ASM_COMMENT_START ";#"
5805 #endif
5807 /* Define a macro which returns nonzero for a TYPE_DECL which was
5808 implicitly generated for a tagged type.
5810 Note that unlike the gcc front end (which generates a NULL named
5811 TYPE_DECL node for each complete tagged type, each array type, and
5812 each function type node created) the g++ front end generates a
5813 _named_ TYPE_DECL node for each tagged type node created.
5814 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5815 generate a DW_TAG_typedef DIE for them. */
5817 #define TYPE_DECL_IS_STUB(decl) \
5818 (DECL_NAME (decl) == NULL_TREE \
5819 || (DECL_ARTIFICIAL (decl) \
5820 && is_tagged_type (TREE_TYPE (decl)) \
5821 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
5822 /* This is necessary for stub decls that \
5823 appear in nested inline functions. */ \
5824 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5825 && (decl_ultimate_origin (decl) \
5826 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5828 /* Information concerning the compilation unit's programming
5829 language, and compiler version. */
5831 /* Fixed size portion of the DWARF compilation unit header. */
5832 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5833 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5835 /* Fixed size portion of the DWARF comdat type unit header. */
5836 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5837 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5838 + DWARF_OFFSET_SIZE)
5840 /* Fixed size portion of public names info. */
5841 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5843 /* Fixed size portion of the address range info. */
5844 #define DWARF_ARANGES_HEADER_SIZE \
5845 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5846 DWARF2_ADDR_SIZE * 2) \
5847 - DWARF_INITIAL_LENGTH_SIZE)
5849 /* Size of padding portion in the address range info. It must be
5850 aligned to twice the pointer size. */
5851 #define DWARF_ARANGES_PAD_SIZE \
5852 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5853 DWARF2_ADDR_SIZE * 2) \
5854 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5856 /* Use assembler line directives if available. */
5857 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5858 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5859 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5860 #else
5861 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5862 #endif
5863 #endif
5865 /* Minimum line offset in a special line info. opcode.
5866 This value was chosen to give a reasonable range of values. */
5867 #define DWARF_LINE_BASE -10
5869 /* First special line opcode - leave room for the standard opcodes. */
5870 #define DWARF_LINE_OPCODE_BASE 10
5872 /* Range of line offsets in a special line info. opcode. */
5873 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
5875 /* Flag that indicates the initial value of the is_stmt_start flag.
5876 In the present implementation, we do not mark any lines as
5877 the beginning of a source statement, because that information
5878 is not made available by the GCC front-end. */
5879 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5881 /* Maximum number of operations per instruction bundle. */
5882 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
5883 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
5884 #endif
5886 /* This location is used by calc_die_sizes() to keep track
5887 the offset of each DIE within the .debug_info section. */
5888 static unsigned long next_die_offset;
5890 /* Record the root of the DIE's built for the current compilation unit. */
5891 static GTY(()) dw_die_ref comp_unit_die;
5893 /* A list of type DIEs that have been separated into comdat sections. */
5894 static GTY(()) comdat_type_node *comdat_type_list;
5896 /* A list of DIEs with a NULL parent waiting to be relocated. */
5897 static GTY(()) limbo_die_node *limbo_die_list;
5899 /* A list of DIEs for which we may have to generate
5900 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
5901 static GTY(()) limbo_die_node *deferred_asm_name;
5903 /* Filenames referenced by this compilation unit. */
5904 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5906 /* A hash table of references to DIE's that describe declarations.
5907 The key is a DECL_UID() which is a unique number identifying each decl. */
5908 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5910 /* A hash table of references to DIE's that describe COMMON blocks.
5911 The key is DECL_UID() ^ die_parent. */
5912 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5914 typedef struct GTY(()) die_arg_entry_struct {
5915 dw_die_ref die;
5916 tree arg;
5917 } die_arg_entry;
5919 DEF_VEC_O(die_arg_entry);
5920 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5922 /* Node of the variable location list. */
5923 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5924 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
5925 EXPR_LIST chain. For small bitsizes, bitsize is encoded
5926 in mode of the EXPR_LIST node and first EXPR_LIST operand
5927 is either NOTE_INSN_VAR_LOCATION for a piece with a known
5928 location or NULL for padding. For larger bitsizes,
5929 mode is 0 and first operand is a CONCAT with bitsize
5930 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
5931 NULL as second operand. */
5932 rtx GTY (()) loc;
5933 const char * GTY (()) label;
5934 struct var_loc_node * GTY (()) next;
5937 /* Variable location list. */
5938 struct GTY (()) var_loc_list_def {
5939 struct var_loc_node * GTY (()) first;
5941 /* Pointer to the last but one or last element of the
5942 chained list. If the list is empty, both first and
5943 last are NULL, if the list contains just one node
5944 or the last node certainly is not redundant, it points
5945 to the last node, otherwise points to the last but one.
5946 Do not mark it for GC because it is marked through the chain. */
5947 struct var_loc_node * GTY ((skip ("%h"))) last;
5949 /* DECL_UID of the variable decl. */
5950 unsigned int decl_id;
5952 typedef struct var_loc_list_def var_loc_list;
5955 /* Table of decl location linked lists. */
5956 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5958 /* A pointer to the base of a list of references to DIE's that
5959 are uniquely identified by their tag, presence/absence of
5960 children DIE's, and list of attribute/value pairs. */
5961 static GTY((length ("abbrev_die_table_allocated")))
5962 dw_die_ref *abbrev_die_table;
5964 /* Number of elements currently allocated for abbrev_die_table. */
5965 static GTY(()) unsigned abbrev_die_table_allocated;
5967 /* Number of elements in type_die_table currently in use. */
5968 static GTY(()) unsigned abbrev_die_table_in_use;
5970 /* Size (in elements) of increments by which we may expand the
5971 abbrev_die_table. */
5972 #define ABBREV_DIE_TABLE_INCREMENT 256
5974 /* A pointer to the base of a table that contains line information
5975 for each source code line in .text in the compilation unit. */
5976 static GTY((length ("line_info_table_allocated")))
5977 dw_line_info_ref line_info_table;
5979 /* Number of elements currently allocated for line_info_table. */
5980 static GTY(()) unsigned line_info_table_allocated;
5982 /* Number of elements in line_info_table currently in use. */
5983 static GTY(()) unsigned line_info_table_in_use;
5985 /* A pointer to the base of a table that contains line information
5986 for each source code line outside of .text in the compilation unit. */
5987 static GTY ((length ("separate_line_info_table_allocated")))
5988 dw_separate_line_info_ref separate_line_info_table;
5990 /* Number of elements currently allocated for separate_line_info_table. */
5991 static GTY(()) unsigned separate_line_info_table_allocated;
5993 /* Number of elements in separate_line_info_table currently in use. */
5994 static GTY(()) unsigned separate_line_info_table_in_use;
5996 /* Size (in elements) of increments by which we may expand the
5997 line_info_table. */
5998 #define LINE_INFO_TABLE_INCREMENT 1024
6000 /* A pointer to the base of a table that contains a list of publicly
6001 accessible names. */
6002 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
6004 /* A pointer to the base of a table that contains a list of publicly
6005 accessible types. */
6006 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6008 /* Array of dies for which we should generate .debug_arange info. */
6009 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
6011 /* Number of elements currently allocated for arange_table. */
6012 static GTY(()) unsigned arange_table_allocated;
6014 /* Number of elements in arange_table currently in use. */
6015 static GTY(()) unsigned arange_table_in_use;
6017 /* Size (in elements) of increments by which we may expand the
6018 arange_table. */
6019 #define ARANGE_TABLE_INCREMENT 64
6021 /* Array of dies for which we should generate .debug_ranges info. */
6022 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6024 /* Number of elements currently allocated for ranges_table. */
6025 static GTY(()) unsigned ranges_table_allocated;
6027 /* Number of elements in ranges_table currently in use. */
6028 static GTY(()) unsigned ranges_table_in_use;
6030 /* Array of pairs of labels referenced in ranges_table. */
6031 static GTY ((length ("ranges_by_label_allocated")))
6032 dw_ranges_by_label_ref ranges_by_label;
6034 /* Number of elements currently allocated for ranges_by_label. */
6035 static GTY(()) unsigned ranges_by_label_allocated;
6037 /* Number of elements in ranges_by_label currently in use. */
6038 static GTY(()) unsigned ranges_by_label_in_use;
6040 /* Size (in elements) of increments by which we may expand the
6041 ranges_table. */
6042 #define RANGES_TABLE_INCREMENT 64
6044 /* Whether we have location lists that need outputting */
6045 static GTY(()) bool have_location_lists;
6047 /* Unique label counter. */
6048 static GTY(()) unsigned int loclabel_num;
6050 /* Unique label counter for point-of-call tables. */
6051 static GTY(()) unsigned int poc_label_num;
6053 /* The direct call table structure. */
6055 typedef struct GTY(()) dcall_struct {
6056 unsigned int poc_label_num;
6057 tree poc_decl;
6058 dw_die_ref targ_die;
6060 dcall_entry;
6062 DEF_VEC_O(dcall_entry);
6063 DEF_VEC_ALLOC_O(dcall_entry, gc);
6065 /* The virtual call table structure. */
6067 typedef struct GTY(()) vcall_struct {
6068 unsigned int poc_label_num;
6069 unsigned int vtable_slot;
6071 vcall_entry;
6073 DEF_VEC_O(vcall_entry);
6074 DEF_VEC_ALLOC_O(vcall_entry, gc);
6076 /* Pointers to the direct and virtual call tables. */
6077 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6078 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6080 /* A hash table to map INSN_UIDs to vtable slot indexes. */
6082 struct GTY (()) vcall_insn {
6083 int insn_uid;
6084 unsigned int vtable_slot;
6087 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6089 /* Record whether the function being analyzed contains inlined functions. */
6090 static int current_function_has_inlines;
6092 /* The last file entry emitted by maybe_emit_file(). */
6093 static GTY(()) struct dwarf_file_data * last_emitted_file;
6095 /* Number of internal labels generated by gen_internal_sym(). */
6096 static GTY(()) int label_num;
6098 /* Cached result of previous call to lookup_filename. */
6099 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6101 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6103 /* Offset from the "steady-state frame pointer" to the frame base,
6104 within the current function. */
6105 static HOST_WIDE_INT frame_pointer_fb_offset;
6107 /* Forward declarations for functions defined in this file. */
6109 static int is_pseudo_reg (const_rtx);
6110 static tree type_main_variant (tree);
6111 static int is_tagged_type (const_tree);
6112 static const char *dwarf_tag_name (unsigned);
6113 static const char *dwarf_attr_name (unsigned);
6114 static const char *dwarf_form_name (unsigned);
6115 static tree decl_ultimate_origin (const_tree);
6116 static tree decl_class_context (tree);
6117 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6118 static inline enum dw_val_class AT_class (dw_attr_ref);
6119 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6120 static inline unsigned AT_flag (dw_attr_ref);
6121 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6122 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6123 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6124 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6125 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6126 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6127 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6128 unsigned int, unsigned char *);
6129 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6130 static hashval_t debug_str_do_hash (const void *);
6131 static int debug_str_eq (const void *, const void *);
6132 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6133 static inline const char *AT_string (dw_attr_ref);
6134 static enum dwarf_form AT_string_form (dw_attr_ref);
6135 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6136 static void add_AT_specification (dw_die_ref, dw_die_ref);
6137 static inline dw_die_ref AT_ref (dw_attr_ref);
6138 static inline int AT_ref_external (dw_attr_ref);
6139 static inline void set_AT_ref_external (dw_attr_ref, int);
6140 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6141 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6142 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6143 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6144 dw_loc_list_ref);
6145 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6146 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6147 static inline rtx AT_addr (dw_attr_ref);
6148 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6149 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6150 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6151 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6152 unsigned HOST_WIDE_INT);
6153 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6154 unsigned long);
6155 static inline const char *AT_lbl (dw_attr_ref);
6156 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6157 static const char *get_AT_low_pc (dw_die_ref);
6158 static const char *get_AT_hi_pc (dw_die_ref);
6159 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6160 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6161 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6162 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6163 static bool is_cxx (void);
6164 static bool is_fortran (void);
6165 static bool is_ada (void);
6166 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6167 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6168 static void add_child_die (dw_die_ref, dw_die_ref);
6169 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6170 static dw_die_ref lookup_type_die (tree);
6171 static void equate_type_number_to_die (tree, dw_die_ref);
6172 static hashval_t decl_die_table_hash (const void *);
6173 static int decl_die_table_eq (const void *, const void *);
6174 static dw_die_ref lookup_decl_die (tree);
6175 static hashval_t common_block_die_table_hash (const void *);
6176 static int common_block_die_table_eq (const void *, const void *);
6177 static hashval_t decl_loc_table_hash (const void *);
6178 static int decl_loc_table_eq (const void *, const void *);
6179 static var_loc_list *lookup_decl_loc (const_tree);
6180 static void equate_decl_number_to_die (tree, dw_die_ref);
6181 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6182 static void print_spaces (FILE *);
6183 static void print_die (dw_die_ref, FILE *);
6184 static void print_dwarf_line_table (FILE *);
6185 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6186 static dw_die_ref pop_compile_unit (dw_die_ref);
6187 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6188 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6189 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6190 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6191 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6192 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6193 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6194 struct md5_ctx *, int *);
6195 struct checksum_attributes;
6196 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6197 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6198 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6199 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6200 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6201 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6202 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6203 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6204 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6205 static void compute_section_prefix (dw_die_ref);
6206 static int is_type_die (dw_die_ref);
6207 static int is_comdat_die (dw_die_ref);
6208 static int is_symbol_die (dw_die_ref);
6209 static void assign_symbol_names (dw_die_ref);
6210 static void break_out_includes (dw_die_ref);
6211 static int is_declaration_die (dw_die_ref);
6212 static int should_move_die_to_comdat (dw_die_ref);
6213 static dw_die_ref clone_as_declaration (dw_die_ref);
6214 static dw_die_ref clone_die (dw_die_ref);
6215 static dw_die_ref clone_tree (dw_die_ref);
6216 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6217 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6218 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6219 static dw_die_ref generate_skeleton (dw_die_ref);
6220 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6221 dw_die_ref);
6222 static void break_out_comdat_types (dw_die_ref);
6223 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6224 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6225 static void copy_decls_for_unworthy_types (dw_die_ref);
6227 static hashval_t htab_cu_hash (const void *);
6228 static int htab_cu_eq (const void *, const void *);
6229 static void htab_cu_del (void *);
6230 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6231 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6232 static void add_sibling_attributes (dw_die_ref);
6233 static void build_abbrev_table (dw_die_ref);
6234 static void output_location_lists (dw_die_ref);
6235 static int constant_size (unsigned HOST_WIDE_INT);
6236 static unsigned long size_of_die (dw_die_ref);
6237 static void calc_die_sizes (dw_die_ref);
6238 static void mark_dies (dw_die_ref);
6239 static void unmark_dies (dw_die_ref);
6240 static void unmark_all_dies (dw_die_ref);
6241 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6242 static unsigned long size_of_aranges (void);
6243 static enum dwarf_form value_format (dw_attr_ref);
6244 static void output_value_format (dw_attr_ref);
6245 static void output_abbrev_section (void);
6246 static void output_die_symbol (dw_die_ref);
6247 static void output_die (dw_die_ref);
6248 static void output_compilation_unit_header (void);
6249 static void output_comp_unit (dw_die_ref, int);
6250 static void output_comdat_type_unit (comdat_type_node *);
6251 static const char *dwarf2_name (tree, int);
6252 static void add_pubname (tree, dw_die_ref);
6253 static void add_pubname_string (const char *, dw_die_ref);
6254 static void add_pubtype (tree, dw_die_ref);
6255 static void output_pubnames (VEC (pubname_entry,gc) *);
6256 static void add_arange (tree, dw_die_ref);
6257 static void output_aranges (void);
6258 static unsigned int add_ranges_num (int);
6259 static unsigned int add_ranges (const_tree);
6260 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6261 bool *);
6262 static void output_ranges (void);
6263 static void output_line_info (void);
6264 static void output_file_names (void);
6265 static dw_die_ref base_type_die (tree);
6266 static int is_base_type (tree);
6267 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6268 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6269 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6270 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6271 static int type_is_enum (const_tree);
6272 static unsigned int dbx_reg_number (const_rtx);
6273 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6274 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6275 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6276 enum var_init_status);
6277 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6278 enum var_init_status);
6279 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6280 enum var_init_status);
6281 static int is_based_loc (const_rtx);
6282 static int resolve_one_addr (rtx *, void *);
6283 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6284 enum var_init_status);
6285 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6286 enum var_init_status);
6287 static dw_loc_list_ref loc_list_from_tree (tree, int);
6288 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6289 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6290 static tree field_type (const_tree);
6291 static unsigned int simple_type_align_in_bits (const_tree);
6292 static unsigned int simple_decl_align_in_bits (const_tree);
6293 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6294 static HOST_WIDE_INT field_byte_offset (const_tree);
6295 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6296 dw_loc_list_ref);
6297 static void add_data_member_location_attribute (dw_die_ref, tree);
6298 static bool add_const_value_attribute (dw_die_ref, rtx);
6299 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6300 static void insert_double (double_int, unsigned char *);
6301 static void insert_float (const_rtx, unsigned char *);
6302 static rtx rtl_for_decl_location (tree);
6303 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6304 enum dwarf_attribute);
6305 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6306 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6307 static void add_name_attribute (dw_die_ref, const char *);
6308 static void add_comp_dir_attribute (dw_die_ref);
6309 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6310 static void add_subscript_info (dw_die_ref, tree, bool);
6311 static void add_byte_size_attribute (dw_die_ref, tree);
6312 static void add_bit_offset_attribute (dw_die_ref, tree);
6313 static void add_bit_size_attribute (dw_die_ref, tree);
6314 static void add_prototyped_attribute (dw_die_ref, tree);
6315 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6316 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6317 static void add_src_coords_attributes (dw_die_ref, tree);
6318 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6319 static void push_decl_scope (tree);
6320 static void pop_decl_scope (void);
6321 static dw_die_ref scope_die_for (tree, dw_die_ref);
6322 static inline int local_scope_p (dw_die_ref);
6323 static inline int class_scope_p (dw_die_ref);
6324 static inline int class_or_namespace_scope_p (dw_die_ref);
6325 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6326 static void add_calling_convention_attribute (dw_die_ref, tree);
6327 static const char *type_tag (const_tree);
6328 static tree member_declared_type (const_tree);
6329 #if 0
6330 static const char *decl_start_label (tree);
6331 #endif
6332 static void gen_array_type_die (tree, dw_die_ref);
6333 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6334 #if 0
6335 static void gen_entry_point_die (tree, dw_die_ref);
6336 #endif
6337 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6338 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6339 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
6340 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6341 static void gen_formal_types_die (tree, dw_die_ref);
6342 static void gen_subprogram_die (tree, dw_die_ref);
6343 static void gen_variable_die (tree, tree, dw_die_ref);
6344 static void gen_const_die (tree, dw_die_ref);
6345 static void gen_label_die (tree, dw_die_ref);
6346 static void gen_lexical_block_die (tree, dw_die_ref, int);
6347 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6348 static void gen_field_die (tree, dw_die_ref);
6349 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6350 static dw_die_ref gen_compile_unit_die (const char *);
6351 static void gen_inheritance_die (tree, tree, dw_die_ref);
6352 static void gen_member_die (tree, dw_die_ref);
6353 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6354 enum debug_info_usage);
6355 static void gen_subroutine_type_die (tree, dw_die_ref);
6356 static void gen_typedef_die (tree, dw_die_ref);
6357 static void gen_type_die (tree, dw_die_ref);
6358 static void gen_block_die (tree, dw_die_ref, int);
6359 static void decls_for_scope (tree, dw_die_ref, int);
6360 static int is_redundant_typedef (const_tree);
6361 static bool is_naming_typedef_decl (const_tree);
6362 static inline dw_die_ref get_context_die (tree);
6363 static void gen_namespace_die (tree, dw_die_ref);
6364 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6365 static dw_die_ref force_decl_die (tree);
6366 static dw_die_ref force_type_die (tree);
6367 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6368 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6369 static struct dwarf_file_data * lookup_filename (const char *);
6370 static void retry_incomplete_types (void);
6371 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6372 static void gen_generic_params_dies (tree);
6373 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6374 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6375 static void splice_child_die (dw_die_ref, dw_die_ref);
6376 static int file_info_cmp (const void *, const void *);
6377 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6378 const char *, const char *);
6379 static void output_loc_list (dw_loc_list_ref);
6380 static char *gen_internal_sym (const char *);
6382 static void prune_unmark_dies (dw_die_ref);
6383 static void prune_unused_types_mark (dw_die_ref, int);
6384 static void prune_unused_types_walk (dw_die_ref);
6385 static void prune_unused_types_walk_attribs (dw_die_ref);
6386 static void prune_unused_types_prune (dw_die_ref);
6387 static void prune_unused_types (void);
6388 static int maybe_emit_file (struct dwarf_file_data *fd);
6389 static inline const char *AT_vms_delta1 (dw_attr_ref);
6390 static inline const char *AT_vms_delta2 (dw_attr_ref);
6391 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6392 const char *, const char *);
6393 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6394 static void gen_remaining_tmpl_value_param_die_attribute (void);
6396 /* Section names used to hold DWARF debugging information. */
6397 #ifndef DEBUG_INFO_SECTION
6398 #define DEBUG_INFO_SECTION ".debug_info"
6399 #endif
6400 #ifndef DEBUG_ABBREV_SECTION
6401 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
6402 #endif
6403 #ifndef DEBUG_ARANGES_SECTION
6404 #define DEBUG_ARANGES_SECTION ".debug_aranges"
6405 #endif
6406 #ifndef DEBUG_MACINFO_SECTION
6407 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
6408 #endif
6409 #ifndef DEBUG_LINE_SECTION
6410 #define DEBUG_LINE_SECTION ".debug_line"
6411 #endif
6412 #ifndef DEBUG_LOC_SECTION
6413 #define DEBUG_LOC_SECTION ".debug_loc"
6414 #endif
6415 #ifndef DEBUG_PUBNAMES_SECTION
6416 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
6417 #endif
6418 #ifndef DEBUG_PUBTYPES_SECTION
6419 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
6420 #endif
6421 #ifndef DEBUG_DCALL_SECTION
6422 #define DEBUG_DCALL_SECTION ".debug_dcall"
6423 #endif
6424 #ifndef DEBUG_VCALL_SECTION
6425 #define DEBUG_VCALL_SECTION ".debug_vcall"
6426 #endif
6427 #ifndef DEBUG_STR_SECTION
6428 #define DEBUG_STR_SECTION ".debug_str"
6429 #endif
6430 #ifndef DEBUG_RANGES_SECTION
6431 #define DEBUG_RANGES_SECTION ".debug_ranges"
6432 #endif
6434 /* Standard ELF section names for compiled code and data. */
6435 #ifndef TEXT_SECTION_NAME
6436 #define TEXT_SECTION_NAME ".text"
6437 #endif
6439 /* Section flags for .debug_str section. */
6440 #define DEBUG_STR_SECTION_FLAGS \
6441 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
6442 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
6443 : SECTION_DEBUG)
6445 /* Labels we insert at beginning sections we can reference instead of
6446 the section names themselves. */
6448 #ifndef TEXT_SECTION_LABEL
6449 #define TEXT_SECTION_LABEL "Ltext"
6450 #endif
6451 #ifndef COLD_TEXT_SECTION_LABEL
6452 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
6453 #endif
6454 #ifndef DEBUG_LINE_SECTION_LABEL
6455 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
6456 #endif
6457 #ifndef DEBUG_INFO_SECTION_LABEL
6458 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
6459 #endif
6460 #ifndef DEBUG_ABBREV_SECTION_LABEL
6461 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
6462 #endif
6463 #ifndef DEBUG_LOC_SECTION_LABEL
6464 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
6465 #endif
6466 #ifndef DEBUG_RANGES_SECTION_LABEL
6467 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
6468 #endif
6469 #ifndef DEBUG_MACINFO_SECTION_LABEL
6470 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
6471 #endif
6474 /* Definitions of defaults for formats and names of various special
6475 (artificial) labels which may be generated within this file (when the -g
6476 options is used and DWARF2_DEBUGGING_INFO is in effect.
6477 If necessary, these may be overridden from within the tm.h file, but
6478 typically, overriding these defaults is unnecessary. */
6480 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6481 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6482 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6483 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6484 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6485 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6486 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6487 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6488 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6489 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6491 #ifndef TEXT_END_LABEL
6492 #define TEXT_END_LABEL "Letext"
6493 #endif
6494 #ifndef COLD_END_LABEL
6495 #define COLD_END_LABEL "Letext_cold"
6496 #endif
6497 #ifndef BLOCK_BEGIN_LABEL
6498 #define BLOCK_BEGIN_LABEL "LBB"
6499 #endif
6500 #ifndef BLOCK_END_LABEL
6501 #define BLOCK_END_LABEL "LBE"
6502 #endif
6503 #ifndef LINE_CODE_LABEL
6504 #define LINE_CODE_LABEL "LM"
6505 #endif
6506 #ifndef SEPARATE_LINE_CODE_LABEL
6507 #define SEPARATE_LINE_CODE_LABEL "LSM"
6508 #endif
6511 /* We allow a language front-end to designate a function that is to be
6512 called to "demangle" any name before it is put into a DIE. */
6514 static const char *(*demangle_name_func) (const char *);
6516 void
6517 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6519 demangle_name_func = func;
6522 /* Test if rtl node points to a pseudo register. */
6524 static inline int
6525 is_pseudo_reg (const_rtx rtl)
6527 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6528 || (GET_CODE (rtl) == SUBREG
6529 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6532 /* Return a reference to a type, with its const and volatile qualifiers
6533 removed. */
6535 static inline tree
6536 type_main_variant (tree type)
6538 type = TYPE_MAIN_VARIANT (type);
6540 /* ??? There really should be only one main variant among any group of
6541 variants of a given type (and all of the MAIN_VARIANT values for all
6542 members of the group should point to that one type) but sometimes the C
6543 front-end messes this up for array types, so we work around that bug
6544 here. */
6545 if (TREE_CODE (type) == ARRAY_TYPE)
6546 while (type != TYPE_MAIN_VARIANT (type))
6547 type = TYPE_MAIN_VARIANT (type);
6549 return type;
6552 /* Return nonzero if the given type node represents a tagged type. */
6554 static inline int
6555 is_tagged_type (const_tree type)
6557 enum tree_code code = TREE_CODE (type);
6559 return (code == RECORD_TYPE || code == UNION_TYPE
6560 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6563 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
6565 static void
6566 get_ref_die_offset_label (char *label, dw_die_ref ref)
6568 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6571 /* Convert a DIE tag into its string name. */
6573 static const char *
6574 dwarf_tag_name (unsigned int tag)
6576 switch (tag)
6578 case DW_TAG_padding:
6579 return "DW_TAG_padding";
6580 case DW_TAG_array_type:
6581 return "DW_TAG_array_type";
6582 case DW_TAG_class_type:
6583 return "DW_TAG_class_type";
6584 case DW_TAG_entry_point:
6585 return "DW_TAG_entry_point";
6586 case DW_TAG_enumeration_type:
6587 return "DW_TAG_enumeration_type";
6588 case DW_TAG_formal_parameter:
6589 return "DW_TAG_formal_parameter";
6590 case DW_TAG_imported_declaration:
6591 return "DW_TAG_imported_declaration";
6592 case DW_TAG_label:
6593 return "DW_TAG_label";
6594 case DW_TAG_lexical_block:
6595 return "DW_TAG_lexical_block";
6596 case DW_TAG_member:
6597 return "DW_TAG_member";
6598 case DW_TAG_pointer_type:
6599 return "DW_TAG_pointer_type";
6600 case DW_TAG_reference_type:
6601 return "DW_TAG_reference_type";
6602 case DW_TAG_compile_unit:
6603 return "DW_TAG_compile_unit";
6604 case DW_TAG_string_type:
6605 return "DW_TAG_string_type";
6606 case DW_TAG_structure_type:
6607 return "DW_TAG_structure_type";
6608 case DW_TAG_subroutine_type:
6609 return "DW_TAG_subroutine_type";
6610 case DW_TAG_typedef:
6611 return "DW_TAG_typedef";
6612 case DW_TAG_union_type:
6613 return "DW_TAG_union_type";
6614 case DW_TAG_unspecified_parameters:
6615 return "DW_TAG_unspecified_parameters";
6616 case DW_TAG_variant:
6617 return "DW_TAG_variant";
6618 case DW_TAG_common_block:
6619 return "DW_TAG_common_block";
6620 case DW_TAG_common_inclusion:
6621 return "DW_TAG_common_inclusion";
6622 case DW_TAG_inheritance:
6623 return "DW_TAG_inheritance";
6624 case DW_TAG_inlined_subroutine:
6625 return "DW_TAG_inlined_subroutine";
6626 case DW_TAG_module:
6627 return "DW_TAG_module";
6628 case DW_TAG_ptr_to_member_type:
6629 return "DW_TAG_ptr_to_member_type";
6630 case DW_TAG_set_type:
6631 return "DW_TAG_set_type";
6632 case DW_TAG_subrange_type:
6633 return "DW_TAG_subrange_type";
6634 case DW_TAG_with_stmt:
6635 return "DW_TAG_with_stmt";
6636 case DW_TAG_access_declaration:
6637 return "DW_TAG_access_declaration";
6638 case DW_TAG_base_type:
6639 return "DW_TAG_base_type";
6640 case DW_TAG_catch_block:
6641 return "DW_TAG_catch_block";
6642 case DW_TAG_const_type:
6643 return "DW_TAG_const_type";
6644 case DW_TAG_constant:
6645 return "DW_TAG_constant";
6646 case DW_TAG_enumerator:
6647 return "DW_TAG_enumerator";
6648 case DW_TAG_file_type:
6649 return "DW_TAG_file_type";
6650 case DW_TAG_friend:
6651 return "DW_TAG_friend";
6652 case DW_TAG_namelist:
6653 return "DW_TAG_namelist";
6654 case DW_TAG_namelist_item:
6655 return "DW_TAG_namelist_item";
6656 case DW_TAG_packed_type:
6657 return "DW_TAG_packed_type";
6658 case DW_TAG_subprogram:
6659 return "DW_TAG_subprogram";
6660 case DW_TAG_template_type_param:
6661 return "DW_TAG_template_type_param";
6662 case DW_TAG_template_value_param:
6663 return "DW_TAG_template_value_param";
6664 case DW_TAG_thrown_type:
6665 return "DW_TAG_thrown_type";
6666 case DW_TAG_try_block:
6667 return "DW_TAG_try_block";
6668 case DW_TAG_variant_part:
6669 return "DW_TAG_variant_part";
6670 case DW_TAG_variable:
6671 return "DW_TAG_variable";
6672 case DW_TAG_volatile_type:
6673 return "DW_TAG_volatile_type";
6674 case DW_TAG_dwarf_procedure:
6675 return "DW_TAG_dwarf_procedure";
6676 case DW_TAG_restrict_type:
6677 return "DW_TAG_restrict_type";
6678 case DW_TAG_interface_type:
6679 return "DW_TAG_interface_type";
6680 case DW_TAG_namespace:
6681 return "DW_TAG_namespace";
6682 case DW_TAG_imported_module:
6683 return "DW_TAG_imported_module";
6684 case DW_TAG_unspecified_type:
6685 return "DW_TAG_unspecified_type";
6686 case DW_TAG_partial_unit:
6687 return "DW_TAG_partial_unit";
6688 case DW_TAG_imported_unit:
6689 return "DW_TAG_imported_unit";
6690 case DW_TAG_condition:
6691 return "DW_TAG_condition";
6692 case DW_TAG_shared_type:
6693 return "DW_TAG_shared_type";
6694 case DW_TAG_type_unit:
6695 return "DW_TAG_type_unit";
6696 case DW_TAG_rvalue_reference_type:
6697 return "DW_TAG_rvalue_reference_type";
6698 case DW_TAG_template_alias:
6699 return "DW_TAG_template_alias";
6700 case DW_TAG_GNU_template_parameter_pack:
6701 return "DW_TAG_GNU_template_parameter_pack";
6702 case DW_TAG_GNU_formal_parameter_pack:
6703 return "DW_TAG_GNU_formal_parameter_pack";
6704 case DW_TAG_MIPS_loop:
6705 return "DW_TAG_MIPS_loop";
6706 case DW_TAG_format_label:
6707 return "DW_TAG_format_label";
6708 case DW_TAG_function_template:
6709 return "DW_TAG_function_template";
6710 case DW_TAG_class_template:
6711 return "DW_TAG_class_template";
6712 case DW_TAG_GNU_BINCL:
6713 return "DW_TAG_GNU_BINCL";
6714 case DW_TAG_GNU_EINCL:
6715 return "DW_TAG_GNU_EINCL";
6716 case DW_TAG_GNU_template_template_param:
6717 return "DW_TAG_GNU_template_template_param";
6718 default:
6719 return "DW_TAG_<unknown>";
6723 /* Convert a DWARF attribute code into its string name. */
6725 static const char *
6726 dwarf_attr_name (unsigned int attr)
6728 switch (attr)
6730 case DW_AT_sibling:
6731 return "DW_AT_sibling";
6732 case DW_AT_location:
6733 return "DW_AT_location";
6734 case DW_AT_name:
6735 return "DW_AT_name";
6736 case DW_AT_ordering:
6737 return "DW_AT_ordering";
6738 case DW_AT_subscr_data:
6739 return "DW_AT_subscr_data";
6740 case DW_AT_byte_size:
6741 return "DW_AT_byte_size";
6742 case DW_AT_bit_offset:
6743 return "DW_AT_bit_offset";
6744 case DW_AT_bit_size:
6745 return "DW_AT_bit_size";
6746 case DW_AT_element_list:
6747 return "DW_AT_element_list";
6748 case DW_AT_stmt_list:
6749 return "DW_AT_stmt_list";
6750 case DW_AT_low_pc:
6751 return "DW_AT_low_pc";
6752 case DW_AT_high_pc:
6753 return "DW_AT_high_pc";
6754 case DW_AT_language:
6755 return "DW_AT_language";
6756 case DW_AT_member:
6757 return "DW_AT_member";
6758 case DW_AT_discr:
6759 return "DW_AT_discr";
6760 case DW_AT_discr_value:
6761 return "DW_AT_discr_value";
6762 case DW_AT_visibility:
6763 return "DW_AT_visibility";
6764 case DW_AT_import:
6765 return "DW_AT_import";
6766 case DW_AT_string_length:
6767 return "DW_AT_string_length";
6768 case DW_AT_common_reference:
6769 return "DW_AT_common_reference";
6770 case DW_AT_comp_dir:
6771 return "DW_AT_comp_dir";
6772 case DW_AT_const_value:
6773 return "DW_AT_const_value";
6774 case DW_AT_containing_type:
6775 return "DW_AT_containing_type";
6776 case DW_AT_default_value:
6777 return "DW_AT_default_value";
6778 case DW_AT_inline:
6779 return "DW_AT_inline";
6780 case DW_AT_is_optional:
6781 return "DW_AT_is_optional";
6782 case DW_AT_lower_bound:
6783 return "DW_AT_lower_bound";
6784 case DW_AT_producer:
6785 return "DW_AT_producer";
6786 case DW_AT_prototyped:
6787 return "DW_AT_prototyped";
6788 case DW_AT_return_addr:
6789 return "DW_AT_return_addr";
6790 case DW_AT_start_scope:
6791 return "DW_AT_start_scope";
6792 case DW_AT_bit_stride:
6793 return "DW_AT_bit_stride";
6794 case DW_AT_upper_bound:
6795 return "DW_AT_upper_bound";
6796 case DW_AT_abstract_origin:
6797 return "DW_AT_abstract_origin";
6798 case DW_AT_accessibility:
6799 return "DW_AT_accessibility";
6800 case DW_AT_address_class:
6801 return "DW_AT_address_class";
6802 case DW_AT_artificial:
6803 return "DW_AT_artificial";
6804 case DW_AT_base_types:
6805 return "DW_AT_base_types";
6806 case DW_AT_calling_convention:
6807 return "DW_AT_calling_convention";
6808 case DW_AT_count:
6809 return "DW_AT_count";
6810 case DW_AT_data_member_location:
6811 return "DW_AT_data_member_location";
6812 case DW_AT_decl_column:
6813 return "DW_AT_decl_column";
6814 case DW_AT_decl_file:
6815 return "DW_AT_decl_file";
6816 case DW_AT_decl_line:
6817 return "DW_AT_decl_line";
6818 case DW_AT_declaration:
6819 return "DW_AT_declaration";
6820 case DW_AT_discr_list:
6821 return "DW_AT_discr_list";
6822 case DW_AT_encoding:
6823 return "DW_AT_encoding";
6824 case DW_AT_external:
6825 return "DW_AT_external";
6826 case DW_AT_explicit:
6827 return "DW_AT_explicit";
6828 case DW_AT_frame_base:
6829 return "DW_AT_frame_base";
6830 case DW_AT_friend:
6831 return "DW_AT_friend";
6832 case DW_AT_identifier_case:
6833 return "DW_AT_identifier_case";
6834 case DW_AT_macro_info:
6835 return "DW_AT_macro_info";
6836 case DW_AT_namelist_items:
6837 return "DW_AT_namelist_items";
6838 case DW_AT_priority:
6839 return "DW_AT_priority";
6840 case DW_AT_segment:
6841 return "DW_AT_segment";
6842 case DW_AT_specification:
6843 return "DW_AT_specification";
6844 case DW_AT_static_link:
6845 return "DW_AT_static_link";
6846 case DW_AT_type:
6847 return "DW_AT_type";
6848 case DW_AT_use_location:
6849 return "DW_AT_use_location";
6850 case DW_AT_variable_parameter:
6851 return "DW_AT_variable_parameter";
6852 case DW_AT_virtuality:
6853 return "DW_AT_virtuality";
6854 case DW_AT_vtable_elem_location:
6855 return "DW_AT_vtable_elem_location";
6857 case DW_AT_allocated:
6858 return "DW_AT_allocated";
6859 case DW_AT_associated:
6860 return "DW_AT_associated";
6861 case DW_AT_data_location:
6862 return "DW_AT_data_location";
6863 case DW_AT_byte_stride:
6864 return "DW_AT_byte_stride";
6865 case DW_AT_entry_pc:
6866 return "DW_AT_entry_pc";
6867 case DW_AT_use_UTF8:
6868 return "DW_AT_use_UTF8";
6869 case DW_AT_extension:
6870 return "DW_AT_extension";
6871 case DW_AT_ranges:
6872 return "DW_AT_ranges";
6873 case DW_AT_trampoline:
6874 return "DW_AT_trampoline";
6875 case DW_AT_call_column:
6876 return "DW_AT_call_column";
6877 case DW_AT_call_file:
6878 return "DW_AT_call_file";
6879 case DW_AT_call_line:
6880 return "DW_AT_call_line";
6881 case DW_AT_object_pointer:
6882 return "DW_AT_object_pointer";
6884 case DW_AT_signature:
6885 return "DW_AT_signature";
6886 case DW_AT_main_subprogram:
6887 return "DW_AT_main_subprogram";
6888 case DW_AT_data_bit_offset:
6889 return "DW_AT_data_bit_offset";
6890 case DW_AT_const_expr:
6891 return "DW_AT_const_expr";
6892 case DW_AT_enum_class:
6893 return "DW_AT_enum_class";
6894 case DW_AT_linkage_name:
6895 return "DW_AT_linkage_name";
6897 case DW_AT_MIPS_fde:
6898 return "DW_AT_MIPS_fde";
6899 case DW_AT_MIPS_loop_begin:
6900 return "DW_AT_MIPS_loop_begin";
6901 case DW_AT_MIPS_tail_loop_begin:
6902 return "DW_AT_MIPS_tail_loop_begin";
6903 case DW_AT_MIPS_epilog_begin:
6904 return "DW_AT_MIPS_epilog_begin";
6905 #if VMS_DEBUGGING_INFO
6906 case DW_AT_HP_prologue:
6907 return "DW_AT_HP_prologue";
6908 #else
6909 case DW_AT_MIPS_loop_unroll_factor:
6910 return "DW_AT_MIPS_loop_unroll_factor";
6911 #endif
6912 case DW_AT_MIPS_software_pipeline_depth:
6913 return "DW_AT_MIPS_software_pipeline_depth";
6914 case DW_AT_MIPS_linkage_name:
6915 return "DW_AT_MIPS_linkage_name";
6916 #if VMS_DEBUGGING_INFO
6917 case DW_AT_HP_epilogue:
6918 return "DW_AT_HP_epilogue";
6919 #else
6920 case DW_AT_MIPS_stride:
6921 return "DW_AT_MIPS_stride";
6922 #endif
6923 case DW_AT_MIPS_abstract_name:
6924 return "DW_AT_MIPS_abstract_name";
6925 case DW_AT_MIPS_clone_origin:
6926 return "DW_AT_MIPS_clone_origin";
6927 case DW_AT_MIPS_has_inlines:
6928 return "DW_AT_MIPS_has_inlines";
6930 case DW_AT_sf_names:
6931 return "DW_AT_sf_names";
6932 case DW_AT_src_info:
6933 return "DW_AT_src_info";
6934 case DW_AT_mac_info:
6935 return "DW_AT_mac_info";
6936 case DW_AT_src_coords:
6937 return "DW_AT_src_coords";
6938 case DW_AT_body_begin:
6939 return "DW_AT_body_begin";
6940 case DW_AT_body_end:
6941 return "DW_AT_body_end";
6942 case DW_AT_GNU_vector:
6943 return "DW_AT_GNU_vector";
6944 case DW_AT_GNU_guarded_by:
6945 return "DW_AT_GNU_guarded_by";
6946 case DW_AT_GNU_pt_guarded_by:
6947 return "DW_AT_GNU_pt_guarded_by";
6948 case DW_AT_GNU_guarded:
6949 return "DW_AT_GNU_guarded";
6950 case DW_AT_GNU_pt_guarded:
6951 return "DW_AT_GNU_pt_guarded";
6952 case DW_AT_GNU_locks_excluded:
6953 return "DW_AT_GNU_locks_excluded";
6954 case DW_AT_GNU_exclusive_locks_required:
6955 return "DW_AT_GNU_exclusive_locks_required";
6956 case DW_AT_GNU_shared_locks_required:
6957 return "DW_AT_GNU_shared_locks_required";
6958 case DW_AT_GNU_odr_signature:
6959 return "DW_AT_GNU_odr_signature";
6960 case DW_AT_GNU_template_name:
6961 return "DW_AT_GNU_template_name";
6963 case DW_AT_VMS_rtnbeg_pd_address:
6964 return "DW_AT_VMS_rtnbeg_pd_address";
6966 default:
6967 return "DW_AT_<unknown>";
6971 /* Convert a DWARF value form code into its string name. */
6973 static const char *
6974 dwarf_form_name (unsigned int form)
6976 switch (form)
6978 case DW_FORM_addr:
6979 return "DW_FORM_addr";
6980 case DW_FORM_block2:
6981 return "DW_FORM_block2";
6982 case DW_FORM_block4:
6983 return "DW_FORM_block4";
6984 case DW_FORM_data2:
6985 return "DW_FORM_data2";
6986 case DW_FORM_data4:
6987 return "DW_FORM_data4";
6988 case DW_FORM_data8:
6989 return "DW_FORM_data8";
6990 case DW_FORM_string:
6991 return "DW_FORM_string";
6992 case DW_FORM_block:
6993 return "DW_FORM_block";
6994 case DW_FORM_block1:
6995 return "DW_FORM_block1";
6996 case DW_FORM_data1:
6997 return "DW_FORM_data1";
6998 case DW_FORM_flag:
6999 return "DW_FORM_flag";
7000 case DW_FORM_sdata:
7001 return "DW_FORM_sdata";
7002 case DW_FORM_strp:
7003 return "DW_FORM_strp";
7004 case DW_FORM_udata:
7005 return "DW_FORM_udata";
7006 case DW_FORM_ref_addr:
7007 return "DW_FORM_ref_addr";
7008 case DW_FORM_ref1:
7009 return "DW_FORM_ref1";
7010 case DW_FORM_ref2:
7011 return "DW_FORM_ref2";
7012 case DW_FORM_ref4:
7013 return "DW_FORM_ref4";
7014 case DW_FORM_ref8:
7015 return "DW_FORM_ref8";
7016 case DW_FORM_ref_udata:
7017 return "DW_FORM_ref_udata";
7018 case DW_FORM_indirect:
7019 return "DW_FORM_indirect";
7020 case DW_FORM_sec_offset:
7021 return "DW_FORM_sec_offset";
7022 case DW_FORM_exprloc:
7023 return "DW_FORM_exprloc";
7024 case DW_FORM_flag_present:
7025 return "DW_FORM_flag_present";
7026 case DW_FORM_ref_sig8:
7027 return "DW_FORM_ref_sig8";
7028 default:
7029 return "DW_FORM_<unknown>";
7033 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
7034 instance of an inlined instance of a decl which is local to an inline
7035 function, so we have to trace all of the way back through the origin chain
7036 to find out what sort of node actually served as the original seed for the
7037 given block. */
7039 static tree
7040 decl_ultimate_origin (const_tree decl)
7042 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7043 return NULL_TREE;
7045 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7046 nodes in the function to point to themselves; ignore that if
7047 we're trying to output the abstract instance of this function. */
7048 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7049 return NULL_TREE;
7051 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7052 most distant ancestor, this should never happen. */
7053 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7055 return DECL_ABSTRACT_ORIGIN (decl);
7058 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
7059 of a virtual function may refer to a base class, so we check the 'this'
7060 parameter. */
7062 static tree
7063 decl_class_context (tree decl)
7065 tree context = NULL_TREE;
7067 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7068 context = DECL_CONTEXT (decl);
7069 else
7070 context = TYPE_MAIN_VARIANT
7071 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7073 if (context && !TYPE_P (context))
7074 context = NULL_TREE;
7076 return context;
7079 /* Add an attribute/value pair to a DIE. */
7081 static inline void
7082 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7084 /* Maybe this should be an assert? */
7085 if (die == NULL)
7086 return;
7088 if (die->die_attr == NULL)
7089 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7090 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7093 static inline enum dw_val_class
7094 AT_class (dw_attr_ref a)
7096 return a->dw_attr_val.val_class;
7099 /* Add a flag value attribute to a DIE. */
7101 static inline void
7102 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7104 dw_attr_node attr;
7106 attr.dw_attr = attr_kind;
7107 attr.dw_attr_val.val_class = dw_val_class_flag;
7108 attr.dw_attr_val.v.val_flag = flag;
7109 add_dwarf_attr (die, &attr);
7112 static inline unsigned
7113 AT_flag (dw_attr_ref a)
7115 gcc_assert (a && AT_class (a) == dw_val_class_flag);
7116 return a->dw_attr_val.v.val_flag;
7119 /* Add a signed integer attribute value to a DIE. */
7121 static inline void
7122 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7124 dw_attr_node attr;
7126 attr.dw_attr = attr_kind;
7127 attr.dw_attr_val.val_class = dw_val_class_const;
7128 attr.dw_attr_val.v.val_int = int_val;
7129 add_dwarf_attr (die, &attr);
7132 static inline HOST_WIDE_INT
7133 AT_int (dw_attr_ref a)
7135 gcc_assert (a && AT_class (a) == dw_val_class_const);
7136 return a->dw_attr_val.v.val_int;
7139 /* Add an unsigned integer attribute value to a DIE. */
7141 static inline void
7142 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7143 unsigned HOST_WIDE_INT unsigned_val)
7145 dw_attr_node attr;
7147 attr.dw_attr = attr_kind;
7148 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7149 attr.dw_attr_val.v.val_unsigned = unsigned_val;
7150 add_dwarf_attr (die, &attr);
7153 static inline unsigned HOST_WIDE_INT
7154 AT_unsigned (dw_attr_ref a)
7156 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7157 return a->dw_attr_val.v.val_unsigned;
7160 /* Add an unsigned double integer attribute value to a DIE. */
7162 static inline void
7163 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7164 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7166 dw_attr_node attr;
7168 attr.dw_attr = attr_kind;
7169 attr.dw_attr_val.val_class = dw_val_class_const_double;
7170 attr.dw_attr_val.v.val_double.high = high;
7171 attr.dw_attr_val.v.val_double.low = low;
7172 add_dwarf_attr (die, &attr);
7175 /* Add a floating point attribute value to a DIE and return it. */
7177 static inline void
7178 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7179 unsigned int length, unsigned int elt_size, unsigned char *array)
7181 dw_attr_node attr;
7183 attr.dw_attr = attr_kind;
7184 attr.dw_attr_val.val_class = dw_val_class_vec;
7185 attr.dw_attr_val.v.val_vec.length = length;
7186 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7187 attr.dw_attr_val.v.val_vec.array = array;
7188 add_dwarf_attr (die, &attr);
7191 /* Add an 8-byte data attribute value to a DIE. */
7193 static inline void
7194 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7195 unsigned char data8[8])
7197 dw_attr_node attr;
7199 attr.dw_attr = attr_kind;
7200 attr.dw_attr_val.val_class = dw_val_class_data8;
7201 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7202 add_dwarf_attr (die, &attr);
7205 /* Hash and equality functions for debug_str_hash. */
7207 static hashval_t
7208 debug_str_do_hash (const void *x)
7210 return htab_hash_string (((const struct indirect_string_node *)x)->str);
7213 static int
7214 debug_str_eq (const void *x1, const void *x2)
7216 return strcmp ((((const struct indirect_string_node *)x1)->str),
7217 (const char *)x2) == 0;
7220 /* Add STR to the indirect string hash table. */
7222 static struct indirect_string_node *
7223 find_AT_string (const char *str)
7225 struct indirect_string_node *node;
7226 void **slot;
7228 if (! debug_str_hash)
7229 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7230 debug_str_eq, NULL);
7232 slot = htab_find_slot_with_hash (debug_str_hash, str,
7233 htab_hash_string (str), INSERT);
7234 if (*slot == NULL)
7236 node = ggc_alloc_cleared_indirect_string_node ();
7237 node->str = ggc_strdup (str);
7238 *slot = node;
7240 else
7241 node = (struct indirect_string_node *) *slot;
7243 node->refcount++;
7244 return node;
7247 /* Add a string attribute value to a DIE. */
7249 static inline void
7250 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7252 dw_attr_node attr;
7253 struct indirect_string_node *node;
7255 node = find_AT_string (str);
7257 attr.dw_attr = attr_kind;
7258 attr.dw_attr_val.val_class = dw_val_class_str;
7259 attr.dw_attr_val.v.val_str = node;
7260 add_dwarf_attr (die, &attr);
7263 /* Create a label for an indirect string node, ensuring it is going to
7264 be output, unless its reference count goes down to zero. */
7266 static inline void
7267 gen_label_for_indirect_string (struct indirect_string_node *node)
7269 char label[32];
7271 if (node->label)
7272 return;
7274 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7275 ++dw2_string_counter;
7276 node->label = xstrdup (label);
7279 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7280 debug string STR. */
7282 static inline rtx
7283 get_debug_string_label (const char *str)
7285 struct indirect_string_node *node = find_AT_string (str);
7287 debug_str_hash_forced = true;
7289 gen_label_for_indirect_string (node);
7291 return gen_rtx_SYMBOL_REF (Pmode, node->label);
7294 static inline const char *
7295 AT_string (dw_attr_ref a)
7297 gcc_assert (a && AT_class (a) == dw_val_class_str);
7298 return a->dw_attr_val.v.val_str->str;
7301 /* Find out whether a string should be output inline in DIE
7302 or out-of-line in .debug_str section. */
7304 static enum dwarf_form
7305 AT_string_form (dw_attr_ref a)
7307 struct indirect_string_node *node;
7308 unsigned int len;
7310 gcc_assert (a && AT_class (a) == dw_val_class_str);
7312 node = a->dw_attr_val.v.val_str;
7313 if (node->form)
7314 return node->form;
7316 len = strlen (node->str) + 1;
7318 /* If the string is shorter or equal to the size of the reference, it is
7319 always better to put it inline. */
7320 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7321 return node->form = DW_FORM_string;
7323 /* If we cannot expect the linker to merge strings in .debug_str
7324 section, only put it into .debug_str if it is worth even in this
7325 single module. */
7326 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7327 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7328 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7329 return node->form = DW_FORM_string;
7331 gen_label_for_indirect_string (node);
7333 return node->form = DW_FORM_strp;
7336 /* Add a DIE reference attribute value to a DIE. */
7338 static inline void
7339 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7341 dw_attr_node attr;
7343 attr.dw_attr = attr_kind;
7344 attr.dw_attr_val.val_class = dw_val_class_die_ref;
7345 attr.dw_attr_val.v.val_die_ref.die = targ_die;
7346 attr.dw_attr_val.v.val_die_ref.external = 0;
7347 add_dwarf_attr (die, &attr);
7350 /* Add an AT_specification attribute to a DIE, and also make the back
7351 pointer from the specification to the definition. */
7353 static inline void
7354 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7356 add_AT_die_ref (die, DW_AT_specification, targ_die);
7357 gcc_assert (!targ_die->die_definition);
7358 targ_die->die_definition = die;
7361 static inline dw_die_ref
7362 AT_ref (dw_attr_ref a)
7364 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7365 return a->dw_attr_val.v.val_die_ref.die;
7368 static inline int
7369 AT_ref_external (dw_attr_ref a)
7371 if (a && AT_class (a) == dw_val_class_die_ref)
7372 return a->dw_attr_val.v.val_die_ref.external;
7374 return 0;
7377 static inline void
7378 set_AT_ref_external (dw_attr_ref a, int i)
7380 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7381 a->dw_attr_val.v.val_die_ref.external = i;
7384 /* Add an FDE reference attribute value to a DIE. */
7386 static inline void
7387 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7389 dw_attr_node attr;
7391 attr.dw_attr = attr_kind;
7392 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7393 attr.dw_attr_val.v.val_fde_index = targ_fde;
7394 add_dwarf_attr (die, &attr);
7397 /* Add a location description attribute value to a DIE. */
7399 static inline void
7400 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7402 dw_attr_node attr;
7404 attr.dw_attr = attr_kind;
7405 attr.dw_attr_val.val_class = dw_val_class_loc;
7406 attr.dw_attr_val.v.val_loc = loc;
7407 add_dwarf_attr (die, &attr);
7410 static inline dw_loc_descr_ref
7411 AT_loc (dw_attr_ref a)
7413 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7414 return a->dw_attr_val.v.val_loc;
7417 static inline void
7418 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7420 dw_attr_node attr;
7422 attr.dw_attr = attr_kind;
7423 attr.dw_attr_val.val_class = dw_val_class_loc_list;
7424 attr.dw_attr_val.v.val_loc_list = loc_list;
7425 add_dwarf_attr (die, &attr);
7426 have_location_lists = true;
7429 static inline dw_loc_list_ref
7430 AT_loc_list (dw_attr_ref a)
7432 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7433 return a->dw_attr_val.v.val_loc_list;
7436 static inline dw_loc_list_ref *
7437 AT_loc_list_ptr (dw_attr_ref a)
7439 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7440 return &a->dw_attr_val.v.val_loc_list;
7443 /* Add an address constant attribute value to a DIE. */
7445 static inline void
7446 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7448 dw_attr_node attr;
7450 attr.dw_attr = attr_kind;
7451 attr.dw_attr_val.val_class = dw_val_class_addr;
7452 attr.dw_attr_val.v.val_addr = addr;
7453 add_dwarf_attr (die, &attr);
7456 /* Get the RTX from to an address DIE attribute. */
7458 static inline rtx
7459 AT_addr (dw_attr_ref a)
7461 gcc_assert (a && AT_class (a) == dw_val_class_addr);
7462 return a->dw_attr_val.v.val_addr;
7465 /* Add a file attribute value to a DIE. */
7467 static inline void
7468 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7469 struct dwarf_file_data *fd)
7471 dw_attr_node attr;
7473 attr.dw_attr = attr_kind;
7474 attr.dw_attr_val.val_class = dw_val_class_file;
7475 attr.dw_attr_val.v.val_file = fd;
7476 add_dwarf_attr (die, &attr);
7479 /* Get the dwarf_file_data from a file DIE attribute. */
7481 static inline struct dwarf_file_data *
7482 AT_file (dw_attr_ref a)
7484 gcc_assert (a && AT_class (a) == dw_val_class_file);
7485 return a->dw_attr_val.v.val_file;
7488 /* Add a vms delta attribute value to a DIE. */
7490 static inline void
7491 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7492 const char *lbl1, const char *lbl2)
7494 dw_attr_node attr;
7496 attr.dw_attr = attr_kind;
7497 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7498 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7499 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7500 add_dwarf_attr (die, &attr);
7503 /* Add a label identifier attribute value to a DIE. */
7505 static inline void
7506 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7508 dw_attr_node attr;
7510 attr.dw_attr = attr_kind;
7511 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7512 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7513 add_dwarf_attr (die, &attr);
7516 /* Add a section offset attribute value to a DIE, an offset into the
7517 debug_line section. */
7519 static inline void
7520 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7521 const char *label)
7523 dw_attr_node attr;
7525 attr.dw_attr = attr_kind;
7526 attr.dw_attr_val.val_class = dw_val_class_lineptr;
7527 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7528 add_dwarf_attr (die, &attr);
7531 /* Add a section offset attribute value to a DIE, an offset into the
7532 debug_macinfo section. */
7534 static inline void
7535 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7536 const char *label)
7538 dw_attr_node attr;
7540 attr.dw_attr = attr_kind;
7541 attr.dw_attr_val.val_class = dw_val_class_macptr;
7542 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7543 add_dwarf_attr (die, &attr);
7546 /* Add an offset attribute value to a DIE. */
7548 static inline void
7549 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7550 unsigned HOST_WIDE_INT offset)
7552 dw_attr_node attr;
7554 attr.dw_attr = attr_kind;
7555 attr.dw_attr_val.val_class = dw_val_class_offset;
7556 attr.dw_attr_val.v.val_offset = offset;
7557 add_dwarf_attr (die, &attr);
7560 /* Add an range_list attribute value to a DIE. */
7562 static void
7563 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7564 long unsigned int offset)
7566 dw_attr_node attr;
7568 attr.dw_attr = attr_kind;
7569 attr.dw_attr_val.val_class = dw_val_class_range_list;
7570 attr.dw_attr_val.v.val_offset = offset;
7571 add_dwarf_attr (die, &attr);
7574 /* Return the start label of a delta attribute. */
7576 static inline const char *
7577 AT_vms_delta1 (dw_attr_ref a)
7579 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7580 return a->dw_attr_val.v.val_vms_delta.lbl1;
7583 /* Return the end label of a delta attribute. */
7585 static inline const char *
7586 AT_vms_delta2 (dw_attr_ref a)
7588 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7589 return a->dw_attr_val.v.val_vms_delta.lbl2;
7592 static inline const char *
7593 AT_lbl (dw_attr_ref a)
7595 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7596 || AT_class (a) == dw_val_class_lineptr
7597 || AT_class (a) == dw_val_class_macptr));
7598 return a->dw_attr_val.v.val_lbl_id;
7601 /* Get the attribute of type attr_kind. */
7603 static dw_attr_ref
7604 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7606 dw_attr_ref a;
7607 unsigned ix;
7608 dw_die_ref spec = NULL;
7610 if (! die)
7611 return NULL;
7613 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7614 if (a->dw_attr == attr_kind)
7615 return a;
7616 else if (a->dw_attr == DW_AT_specification
7617 || a->dw_attr == DW_AT_abstract_origin)
7618 spec = AT_ref (a);
7620 if (spec)
7621 return get_AT (spec, attr_kind);
7623 return NULL;
7626 /* Return the "low pc" attribute value, typically associated with a subprogram
7627 DIE. Return null if the "low pc" attribute is either not present, or if it
7628 cannot be represented as an assembler label identifier. */
7630 static inline const char *
7631 get_AT_low_pc (dw_die_ref die)
7633 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7635 return a ? AT_lbl (a) : NULL;
7638 /* Return the "high pc" attribute value, typically associated with a subprogram
7639 DIE. Return null if the "high pc" attribute is either not present, or if it
7640 cannot be represented as an assembler label identifier. */
7642 static inline const char *
7643 get_AT_hi_pc (dw_die_ref die)
7645 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7647 return a ? AT_lbl (a) : NULL;
7650 /* Return the value of the string attribute designated by ATTR_KIND, or
7651 NULL if it is not present. */
7653 static inline const char *
7654 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7656 dw_attr_ref a = get_AT (die, attr_kind);
7658 return a ? AT_string (a) : NULL;
7661 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7662 if it is not present. */
7664 static inline int
7665 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7667 dw_attr_ref a = get_AT (die, attr_kind);
7669 return a ? AT_flag (a) : 0;
7672 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7673 if it is not present. */
7675 static inline unsigned
7676 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7678 dw_attr_ref a = get_AT (die, attr_kind);
7680 return a ? AT_unsigned (a) : 0;
7683 static inline dw_die_ref
7684 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7686 dw_attr_ref a = get_AT (die, attr_kind);
7688 return a ? AT_ref (a) : NULL;
7691 static inline struct dwarf_file_data *
7692 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7694 dw_attr_ref a = get_AT (die, attr_kind);
7696 return a ? AT_file (a) : NULL;
7699 /* Return TRUE if the language is C++. */
7701 static inline bool
7702 is_cxx (void)
7704 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7706 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7709 /* Return TRUE if the language is Fortran. */
7711 static inline bool
7712 is_fortran (void)
7714 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7716 return (lang == DW_LANG_Fortran77
7717 || lang == DW_LANG_Fortran90
7718 || lang == DW_LANG_Fortran95);
7721 /* Return TRUE if the language is Ada. */
7723 static inline bool
7724 is_ada (void)
7726 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7728 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7731 /* Remove the specified attribute if present. */
7733 static void
7734 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7736 dw_attr_ref a;
7737 unsigned ix;
7739 if (! die)
7740 return;
7742 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7743 if (a->dw_attr == attr_kind)
7745 if (AT_class (a) == dw_val_class_str)
7746 if (a->dw_attr_val.v.val_str->refcount)
7747 a->dw_attr_val.v.val_str->refcount--;
7749 /* VEC_ordered_remove should help reduce the number of abbrevs
7750 that are needed. */
7751 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7752 return;
7756 /* Remove CHILD from its parent. PREV must have the property that
7757 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
7759 static void
7760 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7762 gcc_assert (child->die_parent == prev->die_parent);
7763 gcc_assert (prev->die_sib == child);
7764 if (prev == child)
7766 gcc_assert (child->die_parent->die_child == child);
7767 prev = NULL;
7769 else
7770 prev->die_sib = child->die_sib;
7771 if (child->die_parent->die_child == child)
7772 child->die_parent->die_child = prev;
7775 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
7776 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
7778 static void
7779 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7781 dw_die_ref parent = old_child->die_parent;
7783 gcc_assert (parent == prev->die_parent);
7784 gcc_assert (prev->die_sib == old_child);
7786 new_child->die_parent = parent;
7787 if (prev == old_child)
7789 gcc_assert (parent->die_child == old_child);
7790 new_child->die_sib = new_child;
7792 else
7794 prev->die_sib = new_child;
7795 new_child->die_sib = old_child->die_sib;
7797 if (old_child->die_parent->die_child == old_child)
7798 old_child->die_parent->die_child = new_child;
7801 /* Move all children from OLD_PARENT to NEW_PARENT. */
7803 static void
7804 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7806 dw_die_ref c;
7807 new_parent->die_child = old_parent->die_child;
7808 old_parent->die_child = NULL;
7809 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7812 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
7813 matches TAG. */
7815 static void
7816 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7818 dw_die_ref c;
7820 c = die->die_child;
7821 if (c) do {
7822 dw_die_ref prev = c;
7823 c = c->die_sib;
7824 while (c->die_tag == tag)
7826 remove_child_with_prev (c, prev);
7827 /* Might have removed every child. */
7828 if (c == c->die_sib)
7829 return;
7830 c = c->die_sib;
7832 } while (c != die->die_child);
7835 /* Add a CHILD_DIE as the last child of DIE. */
7837 static void
7838 add_child_die (dw_die_ref die, dw_die_ref child_die)
7840 /* FIXME this should probably be an assert. */
7841 if (! die || ! child_die)
7842 return;
7843 gcc_assert (die != child_die);
7845 child_die->die_parent = die;
7846 if (die->die_child)
7848 child_die->die_sib = die->die_child->die_sib;
7849 die->die_child->die_sib = child_die;
7851 else
7852 child_die->die_sib = child_die;
7853 die->die_child = child_die;
7856 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7857 is the specification, to the end of PARENT's list of children.
7858 This is done by removing and re-adding it. */
7860 static void
7861 splice_child_die (dw_die_ref parent, dw_die_ref child)
7863 dw_die_ref p;
7865 /* We want the declaration DIE from inside the class, not the
7866 specification DIE at toplevel. */
7867 if (child->die_parent != parent)
7869 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7871 if (tmp)
7872 child = tmp;
7875 gcc_assert (child->die_parent == parent
7876 || (child->die_parent
7877 == get_AT_ref (parent, DW_AT_specification)));
7879 for (p = child->die_parent->die_child; ; p = p->die_sib)
7880 if (p->die_sib == child)
7882 remove_child_with_prev (child, p);
7883 break;
7886 add_child_die (parent, child);
7889 /* Return a pointer to a newly created DIE node. */
7891 static inline dw_die_ref
7892 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7894 dw_die_ref die = ggc_alloc_cleared_die_node ();
7896 die->die_tag = tag_value;
7898 if (parent_die != NULL)
7899 add_child_die (parent_die, die);
7900 else
7902 limbo_die_node *limbo_node;
7904 limbo_node = ggc_alloc_cleared_limbo_die_node ();
7905 limbo_node->die = die;
7906 limbo_node->created_for = t;
7907 limbo_node->next = limbo_die_list;
7908 limbo_die_list = limbo_node;
7911 return die;
7914 /* Return the DIE associated with the given type specifier. */
7916 static inline dw_die_ref
7917 lookup_type_die (tree type)
7919 return TYPE_SYMTAB_DIE (type);
7922 /* Equate a DIE to a given type specifier. */
7924 static inline void
7925 equate_type_number_to_die (tree type, dw_die_ref type_die)
7927 TYPE_SYMTAB_DIE (type) = type_die;
7930 /* Returns a hash value for X (which really is a die_struct). */
7932 static hashval_t
7933 decl_die_table_hash (const void *x)
7935 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7938 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
7940 static int
7941 decl_die_table_eq (const void *x, const void *y)
7943 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7946 /* Return the DIE associated with a given declaration. */
7948 static inline dw_die_ref
7949 lookup_decl_die (tree decl)
7951 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7954 /* Returns a hash value for X (which really is a var_loc_list). */
7956 static hashval_t
7957 decl_loc_table_hash (const void *x)
7959 return (hashval_t) ((const var_loc_list *) x)->decl_id;
7962 /* Return nonzero if decl_id of var_loc_list X is the same as
7963 UID of decl *Y. */
7965 static int
7966 decl_loc_table_eq (const void *x, const void *y)
7968 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7971 /* Return the var_loc list associated with a given declaration. */
7973 static inline var_loc_list *
7974 lookup_decl_loc (const_tree decl)
7976 if (!decl_loc_table)
7977 return NULL;
7978 return (var_loc_list *)
7979 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7982 /* Equate a DIE to a particular declaration. */
7984 static void
7985 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7987 unsigned int decl_id = DECL_UID (decl);
7988 void **slot;
7990 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7991 *slot = decl_die;
7992 decl_die->decl_id = decl_id;
7995 /* Return how many bits covers PIECE EXPR_LIST. */
7997 static int
7998 decl_piece_bitsize (rtx piece)
8000 int ret = (int) GET_MODE (piece);
8001 if (ret)
8002 return ret;
8003 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8004 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8005 return INTVAL (XEXP (XEXP (piece, 0), 0));
8008 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
8010 static rtx *
8011 decl_piece_varloc_ptr (rtx piece)
8013 if ((int) GET_MODE (piece))
8014 return &XEXP (piece, 0);
8015 else
8016 return &XEXP (XEXP (piece, 0), 1);
8019 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8020 Next is the chain of following piece nodes. */
8022 static rtx
8023 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8025 if (bitsize <= (int) MAX_MACHINE_MODE)
8026 return alloc_EXPR_LIST (bitsize, loc_note, next);
8027 else
8028 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8029 GEN_INT (bitsize),
8030 loc_note), next);
8033 /* Return rtx that should be stored into loc field for
8034 LOC_NOTE and BITPOS/BITSIZE. */
8036 static rtx
8037 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8038 HOST_WIDE_INT bitsize)
8040 if (bitsize != -1)
8042 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8043 if (bitpos != 0)
8044 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8046 return loc_note;
8049 /* This function either modifies location piece list *DEST in
8050 place (if SRC and INNER is NULL), or copies location piece list
8051 *SRC to *DEST while modifying it. Location BITPOS is modified
8052 to contain LOC_NOTE, any pieces overlapping it are removed resp.
8053 not copied and if needed some padding around it is added.
8054 When modifying in place, DEST should point to EXPR_LIST where
8055 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8056 to the start of the whole list and INNER points to the EXPR_LIST
8057 where earlier pieces cover PIECE_BITPOS bits. */
8059 static void
8060 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8061 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8062 HOST_WIDE_INT bitsize, rtx loc_note)
8064 int diff;
8065 bool copy = inner != NULL;
8067 if (copy)
8069 /* First copy all nodes preceeding the current bitpos. */
8070 while (src != inner)
8072 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8073 decl_piece_bitsize (*src), NULL_RTX);
8074 dest = &XEXP (*dest, 1);
8075 src = &XEXP (*src, 1);
8078 /* Add padding if needed. */
8079 if (bitpos != piece_bitpos)
8081 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8082 copy ? NULL_RTX : *dest);
8083 dest = &XEXP (*dest, 1);
8085 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8087 gcc_assert (!copy);
8088 /* A piece with correct bitpos and bitsize already exist,
8089 just update the location for it and return. */
8090 *decl_piece_varloc_ptr (*dest) = loc_note;
8091 return;
8093 /* Add the piece that changed. */
8094 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8095 dest = &XEXP (*dest, 1);
8096 /* Skip over pieces that overlap it. */
8097 diff = bitpos - piece_bitpos + bitsize;
8098 if (!copy)
8099 src = dest;
8100 while (diff > 0 && *src)
8102 rtx piece = *src;
8103 diff -= decl_piece_bitsize (piece);
8104 if (copy)
8105 src = &XEXP (piece, 1);
8106 else
8108 *src = XEXP (piece, 1);
8109 free_EXPR_LIST_node (piece);
8112 /* Add padding if needed. */
8113 if (diff < 0 && *src)
8115 if (!copy)
8116 dest = src;
8117 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8118 dest = &XEXP (*dest, 1);
8120 if (!copy)
8121 return;
8122 /* Finally copy all nodes following it. */
8123 while (*src)
8125 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8126 decl_piece_bitsize (*src), NULL_RTX);
8127 dest = &XEXP (*dest, 1);
8128 src = &XEXP (*src, 1);
8132 /* Add a variable location node to the linked list for DECL. */
8134 static struct var_loc_node *
8135 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8137 unsigned int decl_id;
8138 var_loc_list *temp;
8139 void **slot;
8140 struct var_loc_node *loc = NULL;
8141 HOST_WIDE_INT bitsize = -1, bitpos = -1;
8143 if (DECL_DEBUG_EXPR_IS_FROM (decl))
8145 tree realdecl = DECL_DEBUG_EXPR (decl);
8146 if (realdecl && handled_component_p (realdecl))
8148 HOST_WIDE_INT maxsize;
8149 tree innerdecl;
8150 innerdecl
8151 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8152 if (!DECL_P (innerdecl)
8153 || DECL_IGNORED_P (innerdecl)
8154 || TREE_STATIC (innerdecl)
8155 || bitsize <= 0
8156 || bitpos + bitsize > 256
8157 || bitsize != maxsize)
8158 return NULL;
8159 decl = innerdecl;
8163 decl_id = DECL_UID (decl);
8164 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8165 if (*slot == NULL)
8167 temp = ggc_alloc_cleared_var_loc_list ();
8168 temp->decl_id = decl_id;
8169 *slot = temp;
8171 else
8172 temp = (var_loc_list *) *slot;
8174 if (temp->last)
8176 struct var_loc_node *last = temp->last, *unused = NULL;
8177 rtx *piece_loc = NULL, last_loc_note;
8178 int piece_bitpos = 0;
8179 if (last->next)
8181 last = last->next;
8182 gcc_assert (last->next == NULL);
8184 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8186 piece_loc = &last->loc;
8189 int cur_bitsize = decl_piece_bitsize (*piece_loc);
8190 if (piece_bitpos + cur_bitsize > bitpos)
8191 break;
8192 piece_bitpos += cur_bitsize;
8193 piece_loc = &XEXP (*piece_loc, 1);
8195 while (*piece_loc);
8197 /* TEMP->LAST here is either pointer to the last but one or
8198 last element in the chained list, LAST is pointer to the
8199 last element. */
8200 if (label && strcmp (last->label, label) == 0)
8202 /* For SRA optimized variables if there weren't any real
8203 insns since last note, just modify the last node. */
8204 if (piece_loc != NULL)
8206 adjust_piece_list (piece_loc, NULL, NULL,
8207 bitpos, piece_bitpos, bitsize, loc_note);
8208 return NULL;
8210 /* If the last note doesn't cover any instructions, remove it. */
8211 if (temp->last != last)
8213 temp->last->next = NULL;
8214 unused = last;
8215 last = temp->last;
8216 gcc_assert (strcmp (last->label, label) != 0);
8218 else
8220 gcc_assert (temp->first == temp->last);
8221 memset (temp->last, '\0', sizeof (*temp->last));
8222 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8223 return temp->last;
8226 if (bitsize == -1 && NOTE_P (last->loc))
8227 last_loc_note = last->loc;
8228 else if (piece_loc != NULL
8229 && *piece_loc != NULL_RTX
8230 && piece_bitpos == bitpos
8231 && decl_piece_bitsize (*piece_loc) == bitsize)
8232 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8233 else
8234 last_loc_note = NULL_RTX;
8235 /* If the current location is the same as the end of the list,
8236 and either both or neither of the locations is uninitialized,
8237 we have nothing to do. */
8238 if (last_loc_note == NULL_RTX
8239 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8240 NOTE_VAR_LOCATION_LOC (loc_note)))
8241 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8242 != NOTE_VAR_LOCATION_STATUS (loc_note))
8243 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8244 == VAR_INIT_STATUS_UNINITIALIZED)
8245 || (NOTE_VAR_LOCATION_STATUS (loc_note)
8246 == VAR_INIT_STATUS_UNINITIALIZED))))
8248 /* Add LOC to the end of list and update LAST. If the last
8249 element of the list has been removed above, reuse its
8250 memory for the new node, otherwise allocate a new one. */
8251 if (unused)
8253 loc = unused;
8254 memset (loc, '\0', sizeof (*loc));
8256 else
8257 loc = ggc_alloc_cleared_var_loc_node ();
8258 if (bitsize == -1 || piece_loc == NULL)
8259 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8260 else
8261 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8262 bitpos, piece_bitpos, bitsize, loc_note);
8263 last->next = loc;
8264 /* Ensure TEMP->LAST will point either to the new last but one
8265 element of the chain, or to the last element in it. */
8266 if (last != temp->last)
8267 temp->last = last;
8269 else if (unused)
8270 ggc_free (unused);
8272 else
8274 loc = ggc_alloc_cleared_var_loc_node ();
8275 temp->first = loc;
8276 temp->last = loc;
8277 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8279 return loc;
8282 /* Keep track of the number of spaces used to indent the
8283 output of the debugging routines that print the structure of
8284 the DIE internal representation. */
8285 static int print_indent;
8287 /* Indent the line the number of spaces given by print_indent. */
8289 static inline void
8290 print_spaces (FILE *outfile)
8292 fprintf (outfile, "%*s", print_indent, "");
8295 /* Print a type signature in hex. */
8297 static inline void
8298 print_signature (FILE *outfile, char *sig)
8300 int i;
8302 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8303 fprintf (outfile, "%02x", sig[i] & 0xff);
8306 /* Print the information associated with a given DIE, and its children.
8307 This routine is a debugging aid only. */
8309 static void
8310 print_die (dw_die_ref die, FILE *outfile)
8312 dw_attr_ref a;
8313 dw_die_ref c;
8314 unsigned ix;
8316 print_spaces (outfile);
8317 fprintf (outfile, "DIE %4ld: %s\n",
8318 die->die_offset, dwarf_tag_name (die->die_tag));
8319 print_spaces (outfile);
8320 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
8321 fprintf (outfile, " offset: %ld\n", die->die_offset);
8322 if (dwarf_version >= 4 && die->die_id.die_type_node)
8324 print_spaces (outfile);
8325 fprintf (outfile, " signature: ");
8326 print_signature (outfile, die->die_id.die_type_node->signature);
8327 fprintf (outfile, "\n");
8330 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8332 print_spaces (outfile);
8333 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
8335 switch (AT_class (a))
8337 case dw_val_class_addr:
8338 fprintf (outfile, "address");
8339 break;
8340 case dw_val_class_offset:
8341 fprintf (outfile, "offset");
8342 break;
8343 case dw_val_class_loc:
8344 fprintf (outfile, "location descriptor");
8345 break;
8346 case dw_val_class_loc_list:
8347 fprintf (outfile, "location list -> label:%s",
8348 AT_loc_list (a)->ll_symbol);
8349 break;
8350 case dw_val_class_range_list:
8351 fprintf (outfile, "range list");
8352 break;
8353 case dw_val_class_const:
8354 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8355 break;
8356 case dw_val_class_unsigned_const:
8357 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8358 break;
8359 case dw_val_class_const_double:
8360 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8361 HOST_WIDE_INT_PRINT_UNSIGNED")",
8362 a->dw_attr_val.v.val_double.high,
8363 a->dw_attr_val.v.val_double.low);
8364 break;
8365 case dw_val_class_vec:
8366 fprintf (outfile, "floating-point or vector constant");
8367 break;
8368 case dw_val_class_flag:
8369 fprintf (outfile, "%u", AT_flag (a));
8370 break;
8371 case dw_val_class_die_ref:
8372 if (AT_ref (a) != NULL)
8374 if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8376 fprintf (outfile, "die -> signature: ");
8377 print_signature (outfile,
8378 AT_ref (a)->die_id.die_type_node->signature);
8380 else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8381 fprintf (outfile, "die -> label: %s",
8382 AT_ref (a)->die_id.die_symbol);
8383 else
8384 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8386 else
8387 fprintf (outfile, "die -> <null>");
8388 break;
8389 case dw_val_class_vms_delta:
8390 fprintf (outfile, "delta: @slotcount(%s-%s)",
8391 AT_vms_delta2 (a), AT_vms_delta1 (a));
8392 break;
8393 case dw_val_class_lbl_id:
8394 case dw_val_class_lineptr:
8395 case dw_val_class_macptr:
8396 fprintf (outfile, "label: %s", AT_lbl (a));
8397 break;
8398 case dw_val_class_str:
8399 if (AT_string (a) != NULL)
8400 fprintf (outfile, "\"%s\"", AT_string (a));
8401 else
8402 fprintf (outfile, "<null>");
8403 break;
8404 case dw_val_class_file:
8405 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8406 AT_file (a)->emitted_number);
8407 break;
8408 case dw_val_class_data8:
8410 int i;
8412 for (i = 0; i < 8; i++)
8413 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8414 break;
8416 default:
8417 break;
8420 fprintf (outfile, "\n");
8423 if (die->die_child != NULL)
8425 print_indent += 4;
8426 FOR_EACH_CHILD (die, c, print_die (c, outfile));
8427 print_indent -= 4;
8429 if (print_indent == 0)
8430 fprintf (outfile, "\n");
8433 /* Print the contents of the source code line number correspondence table.
8434 This routine is a debugging aid only. */
8436 static void
8437 print_dwarf_line_table (FILE *outfile)
8439 unsigned i;
8440 dw_line_info_ref line_info;
8442 fprintf (outfile, "\n\nDWARF source line information\n");
8443 for (i = 1; i < line_info_table_in_use; i++)
8445 line_info = &line_info_table[i];
8446 fprintf (outfile, "%5d: %4ld %6ld\n", i,
8447 line_info->dw_file_num,
8448 line_info->dw_line_num);
8451 fprintf (outfile, "\n\n");
8454 /* Print the information collected for a given DIE. */
8456 DEBUG_FUNCTION void
8457 debug_dwarf_die (dw_die_ref die)
8459 print_die (die, stderr);
8462 /* Print all DWARF information collected for the compilation unit.
8463 This routine is a debugging aid only. */
8465 DEBUG_FUNCTION void
8466 debug_dwarf (void)
8468 print_indent = 0;
8469 print_die (comp_unit_die, stderr);
8470 if (! DWARF2_ASM_LINE_DEBUG_INFO)
8471 print_dwarf_line_table (stderr);
8474 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
8475 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
8476 DIE that marks the start of the DIEs for this include file. */
8478 static dw_die_ref
8479 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8481 const char *filename = get_AT_string (bincl_die, DW_AT_name);
8482 dw_die_ref new_unit = gen_compile_unit_die (filename);
8484 new_unit->die_sib = old_unit;
8485 return new_unit;
8488 /* Close an include-file CU and reopen the enclosing one. */
8490 static dw_die_ref
8491 pop_compile_unit (dw_die_ref old_unit)
8493 dw_die_ref new_unit = old_unit->die_sib;
8495 old_unit->die_sib = NULL;
8496 return new_unit;
8499 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8500 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8502 /* Calculate the checksum of a location expression. */
8504 static inline void
8505 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8507 int tem;
8509 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8510 CHECKSUM (tem);
8511 CHECKSUM (loc->dw_loc_oprnd1);
8512 CHECKSUM (loc->dw_loc_oprnd2);
8515 /* Calculate the checksum of an attribute. */
8517 static void
8518 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8520 dw_loc_descr_ref loc;
8521 rtx r;
8523 CHECKSUM (at->dw_attr);
8525 /* We don't care that this was compiled with a different compiler
8526 snapshot; if the output is the same, that's what matters. */
8527 if (at->dw_attr == DW_AT_producer)
8528 return;
8530 switch (AT_class (at))
8532 case dw_val_class_const:
8533 CHECKSUM (at->dw_attr_val.v.val_int);
8534 break;
8535 case dw_val_class_unsigned_const:
8536 CHECKSUM (at->dw_attr_val.v.val_unsigned);
8537 break;
8538 case dw_val_class_const_double:
8539 CHECKSUM (at->dw_attr_val.v.val_double);
8540 break;
8541 case dw_val_class_vec:
8542 CHECKSUM (at->dw_attr_val.v.val_vec);
8543 break;
8544 case dw_val_class_flag:
8545 CHECKSUM (at->dw_attr_val.v.val_flag);
8546 break;
8547 case dw_val_class_str:
8548 CHECKSUM_STRING (AT_string (at));
8549 break;
8551 case dw_val_class_addr:
8552 r = AT_addr (at);
8553 gcc_assert (GET_CODE (r) == SYMBOL_REF);
8554 CHECKSUM_STRING (XSTR (r, 0));
8555 break;
8557 case dw_val_class_offset:
8558 CHECKSUM (at->dw_attr_val.v.val_offset);
8559 break;
8561 case dw_val_class_loc:
8562 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8563 loc_checksum (loc, ctx);
8564 break;
8566 case dw_val_class_die_ref:
8567 die_checksum (AT_ref (at), ctx, mark);
8568 break;
8570 case dw_val_class_fde_ref:
8571 case dw_val_class_vms_delta:
8572 case dw_val_class_lbl_id:
8573 case dw_val_class_lineptr:
8574 case dw_val_class_macptr:
8575 break;
8577 case dw_val_class_file:
8578 CHECKSUM_STRING (AT_file (at)->filename);
8579 break;
8581 case dw_val_class_data8:
8582 CHECKSUM (at->dw_attr_val.v.val_data8);
8583 break;
8585 default:
8586 break;
8590 /* Calculate the checksum of a DIE. */
8592 static void
8593 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8595 dw_die_ref c;
8596 dw_attr_ref a;
8597 unsigned ix;
8599 /* To avoid infinite recursion. */
8600 if (die->die_mark)
8602 CHECKSUM (die->die_mark);
8603 return;
8605 die->die_mark = ++(*mark);
8607 CHECKSUM (die->die_tag);
8609 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8610 attr_checksum (a, ctx, mark);
8612 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8615 #undef CHECKSUM
8616 #undef CHECKSUM_STRING
8618 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
8619 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8620 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8621 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8622 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8623 #define CHECKSUM_ATTR(FOO) \
8624 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8626 /* Calculate the checksum of a number in signed LEB128 format. */
8628 static void
8629 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8631 unsigned char byte;
8632 bool more;
8634 while (1)
8636 byte = (value & 0x7f);
8637 value >>= 7;
8638 more = !((value == 0 && (byte & 0x40) == 0)
8639 || (value == -1 && (byte & 0x40) != 0));
8640 if (more)
8641 byte |= 0x80;
8642 CHECKSUM (byte);
8643 if (!more)
8644 break;
8648 /* Calculate the checksum of a number in unsigned LEB128 format. */
8650 static void
8651 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8653 while (1)
8655 unsigned char byte = (value & 0x7f);
8656 value >>= 7;
8657 if (value != 0)
8658 /* More bytes to follow. */
8659 byte |= 0x80;
8660 CHECKSUM (byte);
8661 if (value == 0)
8662 break;
8666 /* Checksum the context of the DIE. This adds the names of any
8667 surrounding namespaces or structures to the checksum. */
8669 static void
8670 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8672 const char *name;
8673 dw_die_ref spec;
8674 int tag = die->die_tag;
8676 if (tag != DW_TAG_namespace
8677 && tag != DW_TAG_structure_type
8678 && tag != DW_TAG_class_type)
8679 return;
8681 name = get_AT_string (die, DW_AT_name);
8683 spec = get_AT_ref (die, DW_AT_specification);
8684 if (spec != NULL)
8685 die = spec;
8687 if (die->die_parent != NULL)
8688 checksum_die_context (die->die_parent, ctx);
8690 CHECKSUM_ULEB128 ('C');
8691 CHECKSUM_ULEB128 (tag);
8692 if (name != NULL)
8693 CHECKSUM_STRING (name);
8696 /* Calculate the checksum of a location expression. */
8698 static inline void
8699 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8701 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8702 were emitted as a DW_FORM_sdata instead of a location expression. */
8703 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8705 CHECKSUM_ULEB128 (DW_FORM_sdata);
8706 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8707 return;
8710 /* Otherwise, just checksum the raw location expression. */
8711 while (loc != NULL)
8713 CHECKSUM_ULEB128 (loc->dw_loc_opc);
8714 CHECKSUM (loc->dw_loc_oprnd1);
8715 CHECKSUM (loc->dw_loc_oprnd2);
8716 loc = loc->dw_loc_next;
8720 /* Calculate the checksum of an attribute. */
8722 static void
8723 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8724 struct md5_ctx *ctx, int *mark)
8726 dw_loc_descr_ref loc;
8727 rtx r;
8729 if (AT_class (at) == dw_val_class_die_ref)
8731 dw_die_ref target_die = AT_ref (at);
8733 /* For pointer and reference types, we checksum only the (qualified)
8734 name of the target type (if there is a name). For friend entries,
8735 we checksum only the (qualified) name of the target type or function.
8736 This allows the checksum to remain the same whether the target type
8737 is complete or not. */
8738 if ((at->dw_attr == DW_AT_type
8739 && (tag == DW_TAG_pointer_type
8740 || tag == DW_TAG_reference_type
8741 || tag == DW_TAG_rvalue_reference_type
8742 || tag == DW_TAG_ptr_to_member_type))
8743 || (at->dw_attr == DW_AT_friend
8744 && tag == DW_TAG_friend))
8746 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8748 if (name_attr != NULL)
8750 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8752 if (decl == NULL)
8753 decl = target_die;
8754 CHECKSUM_ULEB128 ('N');
8755 CHECKSUM_ULEB128 (at->dw_attr);
8756 if (decl->die_parent != NULL)
8757 checksum_die_context (decl->die_parent, ctx);
8758 CHECKSUM_ULEB128 ('E');
8759 CHECKSUM_STRING (AT_string (name_attr));
8760 return;
8764 /* For all other references to another DIE, we check to see if the
8765 target DIE has already been visited. If it has, we emit a
8766 backward reference; if not, we descend recursively. */
8767 if (target_die->die_mark > 0)
8769 CHECKSUM_ULEB128 ('R');
8770 CHECKSUM_ULEB128 (at->dw_attr);
8771 CHECKSUM_ULEB128 (target_die->die_mark);
8773 else
8775 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8777 if (decl == NULL)
8778 decl = target_die;
8779 target_die->die_mark = ++(*mark);
8780 CHECKSUM_ULEB128 ('T');
8781 CHECKSUM_ULEB128 (at->dw_attr);
8782 if (decl->die_parent != NULL)
8783 checksum_die_context (decl->die_parent, ctx);
8784 die_checksum_ordered (target_die, ctx, mark);
8786 return;
8789 CHECKSUM_ULEB128 ('A');
8790 CHECKSUM_ULEB128 (at->dw_attr);
8792 switch (AT_class (at))
8794 case dw_val_class_const:
8795 CHECKSUM_ULEB128 (DW_FORM_sdata);
8796 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8797 break;
8799 case dw_val_class_unsigned_const:
8800 CHECKSUM_ULEB128 (DW_FORM_sdata);
8801 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8802 break;
8804 case dw_val_class_const_double:
8805 CHECKSUM_ULEB128 (DW_FORM_block);
8806 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8807 CHECKSUM (at->dw_attr_val.v.val_double);
8808 break;
8810 case dw_val_class_vec:
8811 CHECKSUM_ULEB128 (DW_FORM_block);
8812 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8813 CHECKSUM (at->dw_attr_val.v.val_vec);
8814 break;
8816 case dw_val_class_flag:
8817 CHECKSUM_ULEB128 (DW_FORM_flag);
8818 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8819 break;
8821 case dw_val_class_str:
8822 CHECKSUM_ULEB128 (DW_FORM_string);
8823 CHECKSUM_STRING (AT_string (at));
8824 break;
8826 case dw_val_class_addr:
8827 r = AT_addr (at);
8828 gcc_assert (GET_CODE (r) == SYMBOL_REF);
8829 CHECKSUM_ULEB128 (DW_FORM_string);
8830 CHECKSUM_STRING (XSTR (r, 0));
8831 break;
8833 case dw_val_class_offset:
8834 CHECKSUM_ULEB128 (DW_FORM_sdata);
8835 CHECKSUM_ULEB128 (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_ordered (loc, ctx);
8841 break;
8843 case dw_val_class_fde_ref:
8844 case dw_val_class_lbl_id:
8845 case dw_val_class_lineptr:
8846 case dw_val_class_macptr:
8847 break;
8849 case dw_val_class_file:
8850 CHECKSUM_ULEB128 (DW_FORM_string);
8851 CHECKSUM_STRING (AT_file (at)->filename);
8852 break;
8854 case dw_val_class_data8:
8855 CHECKSUM (at->dw_attr_val.v.val_data8);
8856 break;
8858 default:
8859 break;
8863 struct checksum_attributes
8865 dw_attr_ref at_name;
8866 dw_attr_ref at_type;
8867 dw_attr_ref at_friend;
8868 dw_attr_ref at_accessibility;
8869 dw_attr_ref at_address_class;
8870 dw_attr_ref at_allocated;
8871 dw_attr_ref at_artificial;
8872 dw_attr_ref at_associated;
8873 dw_attr_ref at_binary_scale;
8874 dw_attr_ref at_bit_offset;
8875 dw_attr_ref at_bit_size;
8876 dw_attr_ref at_bit_stride;
8877 dw_attr_ref at_byte_size;
8878 dw_attr_ref at_byte_stride;
8879 dw_attr_ref at_const_value;
8880 dw_attr_ref at_containing_type;
8881 dw_attr_ref at_count;
8882 dw_attr_ref at_data_location;
8883 dw_attr_ref at_data_member_location;
8884 dw_attr_ref at_decimal_scale;
8885 dw_attr_ref at_decimal_sign;
8886 dw_attr_ref at_default_value;
8887 dw_attr_ref at_digit_count;
8888 dw_attr_ref at_discr;
8889 dw_attr_ref at_discr_list;
8890 dw_attr_ref at_discr_value;
8891 dw_attr_ref at_encoding;
8892 dw_attr_ref at_endianity;
8893 dw_attr_ref at_explicit;
8894 dw_attr_ref at_is_optional;
8895 dw_attr_ref at_location;
8896 dw_attr_ref at_lower_bound;
8897 dw_attr_ref at_mutable;
8898 dw_attr_ref at_ordering;
8899 dw_attr_ref at_picture_string;
8900 dw_attr_ref at_prototyped;
8901 dw_attr_ref at_small;
8902 dw_attr_ref at_segment;
8903 dw_attr_ref at_string_length;
8904 dw_attr_ref at_threads_scaled;
8905 dw_attr_ref at_upper_bound;
8906 dw_attr_ref at_use_location;
8907 dw_attr_ref at_use_UTF8;
8908 dw_attr_ref at_variable_parameter;
8909 dw_attr_ref at_virtuality;
8910 dw_attr_ref at_visibility;
8911 dw_attr_ref at_vtable_elem_location;
8914 /* Collect the attributes that we will want to use for the checksum. */
8916 static void
8917 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8919 dw_attr_ref a;
8920 unsigned ix;
8922 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8924 switch (a->dw_attr)
8926 case DW_AT_name:
8927 attrs->at_name = a;
8928 break;
8929 case DW_AT_type:
8930 attrs->at_type = a;
8931 break;
8932 case DW_AT_friend:
8933 attrs->at_friend = a;
8934 break;
8935 case DW_AT_accessibility:
8936 attrs->at_accessibility = a;
8937 break;
8938 case DW_AT_address_class:
8939 attrs->at_address_class = a;
8940 break;
8941 case DW_AT_allocated:
8942 attrs->at_allocated = a;
8943 break;
8944 case DW_AT_artificial:
8945 attrs->at_artificial = a;
8946 break;
8947 case DW_AT_associated:
8948 attrs->at_associated = a;
8949 break;
8950 case DW_AT_binary_scale:
8951 attrs->at_binary_scale = a;
8952 break;
8953 case DW_AT_bit_offset:
8954 attrs->at_bit_offset = a;
8955 break;
8956 case DW_AT_bit_size:
8957 attrs->at_bit_size = a;
8958 break;
8959 case DW_AT_bit_stride:
8960 attrs->at_bit_stride = a;
8961 break;
8962 case DW_AT_byte_size:
8963 attrs->at_byte_size = a;
8964 break;
8965 case DW_AT_byte_stride:
8966 attrs->at_byte_stride = a;
8967 break;
8968 case DW_AT_const_value:
8969 attrs->at_const_value = a;
8970 break;
8971 case DW_AT_containing_type:
8972 attrs->at_containing_type = a;
8973 break;
8974 case DW_AT_count:
8975 attrs->at_count = a;
8976 break;
8977 case DW_AT_data_location:
8978 attrs->at_data_location = a;
8979 break;
8980 case DW_AT_data_member_location:
8981 attrs->at_data_member_location = a;
8982 break;
8983 case DW_AT_decimal_scale:
8984 attrs->at_decimal_scale = a;
8985 break;
8986 case DW_AT_decimal_sign:
8987 attrs->at_decimal_sign = a;
8988 break;
8989 case DW_AT_default_value:
8990 attrs->at_default_value = a;
8991 break;
8992 case DW_AT_digit_count:
8993 attrs->at_digit_count = a;
8994 break;
8995 case DW_AT_discr:
8996 attrs->at_discr = a;
8997 break;
8998 case DW_AT_discr_list:
8999 attrs->at_discr_list = a;
9000 break;
9001 case DW_AT_discr_value:
9002 attrs->at_discr_value = a;
9003 break;
9004 case DW_AT_encoding:
9005 attrs->at_encoding = a;
9006 break;
9007 case DW_AT_endianity:
9008 attrs->at_endianity = a;
9009 break;
9010 case DW_AT_explicit:
9011 attrs->at_explicit = a;
9012 break;
9013 case DW_AT_is_optional:
9014 attrs->at_is_optional = a;
9015 break;
9016 case DW_AT_location:
9017 attrs->at_location = a;
9018 break;
9019 case DW_AT_lower_bound:
9020 attrs->at_lower_bound = a;
9021 break;
9022 case DW_AT_mutable:
9023 attrs->at_mutable = a;
9024 break;
9025 case DW_AT_ordering:
9026 attrs->at_ordering = a;
9027 break;
9028 case DW_AT_picture_string:
9029 attrs->at_picture_string = a;
9030 break;
9031 case DW_AT_prototyped:
9032 attrs->at_prototyped = a;
9033 break;
9034 case DW_AT_small:
9035 attrs->at_small = a;
9036 break;
9037 case DW_AT_segment:
9038 attrs->at_segment = a;
9039 break;
9040 case DW_AT_string_length:
9041 attrs->at_string_length = a;
9042 break;
9043 case DW_AT_threads_scaled:
9044 attrs->at_threads_scaled = a;
9045 break;
9046 case DW_AT_upper_bound:
9047 attrs->at_upper_bound = a;
9048 break;
9049 case DW_AT_use_location:
9050 attrs->at_use_location = a;
9051 break;
9052 case DW_AT_use_UTF8:
9053 attrs->at_use_UTF8 = a;
9054 break;
9055 case DW_AT_variable_parameter:
9056 attrs->at_variable_parameter = a;
9057 break;
9058 case DW_AT_virtuality:
9059 attrs->at_virtuality = a;
9060 break;
9061 case DW_AT_visibility:
9062 attrs->at_visibility = a;
9063 break;
9064 case DW_AT_vtable_elem_location:
9065 attrs->at_vtable_elem_location = a;
9066 break;
9067 default:
9068 break;
9073 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
9075 static void
9076 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9078 dw_die_ref c;
9079 dw_die_ref decl;
9080 struct checksum_attributes attrs;
9082 CHECKSUM_ULEB128 ('D');
9083 CHECKSUM_ULEB128 (die->die_tag);
9085 memset (&attrs, 0, sizeof (attrs));
9087 decl = get_AT_ref (die, DW_AT_specification);
9088 if (decl != NULL)
9089 collect_checksum_attributes (&attrs, decl);
9090 collect_checksum_attributes (&attrs, die);
9092 CHECKSUM_ATTR (attrs.at_name);
9093 CHECKSUM_ATTR (attrs.at_accessibility);
9094 CHECKSUM_ATTR (attrs.at_address_class);
9095 CHECKSUM_ATTR (attrs.at_allocated);
9096 CHECKSUM_ATTR (attrs.at_artificial);
9097 CHECKSUM_ATTR (attrs.at_associated);
9098 CHECKSUM_ATTR (attrs.at_binary_scale);
9099 CHECKSUM_ATTR (attrs.at_bit_offset);
9100 CHECKSUM_ATTR (attrs.at_bit_size);
9101 CHECKSUM_ATTR (attrs.at_bit_stride);
9102 CHECKSUM_ATTR (attrs.at_byte_size);
9103 CHECKSUM_ATTR (attrs.at_byte_stride);
9104 CHECKSUM_ATTR (attrs.at_const_value);
9105 CHECKSUM_ATTR (attrs.at_containing_type);
9106 CHECKSUM_ATTR (attrs.at_count);
9107 CHECKSUM_ATTR (attrs.at_data_location);
9108 CHECKSUM_ATTR (attrs.at_data_member_location);
9109 CHECKSUM_ATTR (attrs.at_decimal_scale);
9110 CHECKSUM_ATTR (attrs.at_decimal_sign);
9111 CHECKSUM_ATTR (attrs.at_default_value);
9112 CHECKSUM_ATTR (attrs.at_digit_count);
9113 CHECKSUM_ATTR (attrs.at_discr);
9114 CHECKSUM_ATTR (attrs.at_discr_list);
9115 CHECKSUM_ATTR (attrs.at_discr_value);
9116 CHECKSUM_ATTR (attrs.at_encoding);
9117 CHECKSUM_ATTR (attrs.at_endianity);
9118 CHECKSUM_ATTR (attrs.at_explicit);
9119 CHECKSUM_ATTR (attrs.at_is_optional);
9120 CHECKSUM_ATTR (attrs.at_location);
9121 CHECKSUM_ATTR (attrs.at_lower_bound);
9122 CHECKSUM_ATTR (attrs.at_mutable);
9123 CHECKSUM_ATTR (attrs.at_ordering);
9124 CHECKSUM_ATTR (attrs.at_picture_string);
9125 CHECKSUM_ATTR (attrs.at_prototyped);
9126 CHECKSUM_ATTR (attrs.at_small);
9127 CHECKSUM_ATTR (attrs.at_segment);
9128 CHECKSUM_ATTR (attrs.at_string_length);
9129 CHECKSUM_ATTR (attrs.at_threads_scaled);
9130 CHECKSUM_ATTR (attrs.at_upper_bound);
9131 CHECKSUM_ATTR (attrs.at_use_location);
9132 CHECKSUM_ATTR (attrs.at_use_UTF8);
9133 CHECKSUM_ATTR (attrs.at_variable_parameter);
9134 CHECKSUM_ATTR (attrs.at_virtuality);
9135 CHECKSUM_ATTR (attrs.at_visibility);
9136 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9137 CHECKSUM_ATTR (attrs.at_type);
9138 CHECKSUM_ATTR (attrs.at_friend);
9140 /* Checksum the child DIEs, except for nested types and member functions. */
9141 c = die->die_child;
9142 if (c) do {
9143 dw_attr_ref name_attr;
9145 c = c->die_sib;
9146 name_attr = get_AT (c, DW_AT_name);
9147 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9148 && name_attr != NULL)
9150 CHECKSUM_ULEB128 ('S');
9151 CHECKSUM_ULEB128 (c->die_tag);
9152 CHECKSUM_STRING (AT_string (name_attr));
9154 else
9156 /* Mark this DIE so it gets processed when unmarking. */
9157 if (c->die_mark == 0)
9158 c->die_mark = -1;
9159 die_checksum_ordered (c, ctx, mark);
9161 } while (c != die->die_child);
9163 CHECKSUM_ULEB128 (0);
9166 #undef CHECKSUM
9167 #undef CHECKSUM_STRING
9168 #undef CHECKSUM_ATTR
9169 #undef CHECKSUM_LEB128
9170 #undef CHECKSUM_ULEB128
9172 /* Generate the type signature for DIE. This is computed by generating an
9173 MD5 checksum over the DIE's tag, its relevant attributes, and its
9174 children. Attributes that are references to other DIEs are processed
9175 by recursion, using the MARK field to prevent infinite recursion.
9176 If the DIE is nested inside a namespace or another type, we also
9177 need to include that context in the signature. The lower 64 bits
9178 of the resulting MD5 checksum comprise the signature. */
9180 static void
9181 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9183 int mark;
9184 const char *name;
9185 unsigned char checksum[16];
9186 struct md5_ctx ctx;
9187 dw_die_ref decl;
9189 name = get_AT_string (die, DW_AT_name);
9190 decl = get_AT_ref (die, DW_AT_specification);
9192 /* First, compute a signature for just the type name (and its surrounding
9193 context, if any. This is stored in the type unit DIE for link-time
9194 ODR (one-definition rule) checking. */
9196 if (is_cxx() && name != NULL)
9198 md5_init_ctx (&ctx);
9200 /* Checksum the names of surrounding namespaces and structures. */
9201 if (decl != NULL && decl->die_parent != NULL)
9202 checksum_die_context (decl->die_parent, &ctx);
9204 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9205 md5_process_bytes (name, strlen (name) + 1, &ctx);
9206 md5_finish_ctx (&ctx, checksum);
9208 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9211 /* Next, compute the complete type signature. */
9213 md5_init_ctx (&ctx);
9214 mark = 1;
9215 die->die_mark = mark;
9217 /* Checksum the names of surrounding namespaces and structures. */
9218 if (decl != NULL && decl->die_parent != NULL)
9219 checksum_die_context (decl->die_parent, &ctx);
9221 /* Checksum the DIE and its children. */
9222 die_checksum_ordered (die, &ctx, &mark);
9223 unmark_all_dies (die);
9224 md5_finish_ctx (&ctx, checksum);
9226 /* Store the signature in the type node and link the type DIE and the
9227 type node together. */
9228 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9229 DWARF_TYPE_SIGNATURE_SIZE);
9230 die->die_id.die_type_node = type_node;
9231 type_node->type_die = die;
9233 /* If the DIE is a specification, link its declaration to the type node
9234 as well. */
9235 if (decl != NULL)
9236 decl->die_id.die_type_node = type_node;
9239 /* Do the location expressions look same? */
9240 static inline int
9241 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9243 return loc1->dw_loc_opc == loc2->dw_loc_opc
9244 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9245 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9248 /* Do the values look the same? */
9249 static int
9250 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9252 dw_loc_descr_ref loc1, loc2;
9253 rtx r1, r2;
9255 if (v1->val_class != v2->val_class)
9256 return 0;
9258 switch (v1->val_class)
9260 case dw_val_class_const:
9261 return v1->v.val_int == v2->v.val_int;
9262 case dw_val_class_unsigned_const:
9263 return v1->v.val_unsigned == v2->v.val_unsigned;
9264 case dw_val_class_const_double:
9265 return v1->v.val_double.high == v2->v.val_double.high
9266 && v1->v.val_double.low == v2->v.val_double.low;
9267 case dw_val_class_vec:
9268 if (v1->v.val_vec.length != v2->v.val_vec.length
9269 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9270 return 0;
9271 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9272 v1->v.val_vec.length * v1->v.val_vec.elt_size))
9273 return 0;
9274 return 1;
9275 case dw_val_class_flag:
9276 return v1->v.val_flag == v2->v.val_flag;
9277 case dw_val_class_str:
9278 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9280 case dw_val_class_addr:
9281 r1 = v1->v.val_addr;
9282 r2 = v2->v.val_addr;
9283 if (GET_CODE (r1) != GET_CODE (r2))
9284 return 0;
9285 return !rtx_equal_p (r1, r2);
9287 case dw_val_class_offset:
9288 return v1->v.val_offset == v2->v.val_offset;
9290 case dw_val_class_loc:
9291 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9292 loc1 && loc2;
9293 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9294 if (!same_loc_p (loc1, loc2, mark))
9295 return 0;
9296 return !loc1 && !loc2;
9298 case dw_val_class_die_ref:
9299 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9301 case dw_val_class_fde_ref:
9302 case dw_val_class_vms_delta:
9303 case dw_val_class_lbl_id:
9304 case dw_val_class_lineptr:
9305 case dw_val_class_macptr:
9306 return 1;
9308 case dw_val_class_file:
9309 return v1->v.val_file == v2->v.val_file;
9311 case dw_val_class_data8:
9312 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9314 default:
9315 return 1;
9319 /* Do the attributes look the same? */
9321 static int
9322 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9324 if (at1->dw_attr != at2->dw_attr)
9325 return 0;
9327 /* We don't care that this was compiled with a different compiler
9328 snapshot; if the output is the same, that's what matters. */
9329 if (at1->dw_attr == DW_AT_producer)
9330 return 1;
9332 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9335 /* Do the dies look the same? */
9337 static int
9338 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9340 dw_die_ref c1, c2;
9341 dw_attr_ref a1;
9342 unsigned ix;
9344 /* To avoid infinite recursion. */
9345 if (die1->die_mark)
9346 return die1->die_mark == die2->die_mark;
9347 die1->die_mark = die2->die_mark = ++(*mark);
9349 if (die1->die_tag != die2->die_tag)
9350 return 0;
9352 if (VEC_length (dw_attr_node, die1->die_attr)
9353 != VEC_length (dw_attr_node, die2->die_attr))
9354 return 0;
9356 FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9357 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9358 return 0;
9360 c1 = die1->die_child;
9361 c2 = die2->die_child;
9362 if (! c1)
9364 if (c2)
9365 return 0;
9367 else
9368 for (;;)
9370 if (!same_die_p (c1, c2, mark))
9371 return 0;
9372 c1 = c1->die_sib;
9373 c2 = c2->die_sib;
9374 if (c1 == die1->die_child)
9376 if (c2 == die2->die_child)
9377 break;
9378 else
9379 return 0;
9383 return 1;
9386 /* Do the dies look the same? Wrapper around same_die_p. */
9388 static int
9389 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9391 int mark = 0;
9392 int ret = same_die_p (die1, die2, &mark);
9394 unmark_all_dies (die1);
9395 unmark_all_dies (die2);
9397 return ret;
9400 /* The prefix to attach to symbols on DIEs in the current comdat debug
9401 info section. */
9402 static char *comdat_symbol_id;
9404 /* The index of the current symbol within the current comdat CU. */
9405 static unsigned int comdat_symbol_number;
9407 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9408 children, and set comdat_symbol_id accordingly. */
9410 static void
9411 compute_section_prefix (dw_die_ref unit_die)
9413 const char *die_name = get_AT_string (unit_die, DW_AT_name);
9414 const char *base = die_name ? lbasename (die_name) : "anonymous";
9415 char *name = XALLOCAVEC (char, strlen (base) + 64);
9416 char *p;
9417 int i, mark;
9418 unsigned char checksum[16];
9419 struct md5_ctx ctx;
9421 /* Compute the checksum of the DIE, then append part of it as hex digits to
9422 the name filename of the unit. */
9424 md5_init_ctx (&ctx);
9425 mark = 0;
9426 die_checksum (unit_die, &ctx, &mark);
9427 unmark_all_dies (unit_die);
9428 md5_finish_ctx (&ctx, checksum);
9430 sprintf (name, "%s.", base);
9431 clean_symbol_name (name);
9433 p = name + strlen (name);
9434 for (i = 0; i < 4; i++)
9436 sprintf (p, "%.2x", checksum[i]);
9437 p += 2;
9440 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9441 comdat_symbol_number = 0;
9444 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
9446 static int
9447 is_type_die (dw_die_ref die)
9449 switch (die->die_tag)
9451 case DW_TAG_array_type:
9452 case DW_TAG_class_type:
9453 case DW_TAG_interface_type:
9454 case DW_TAG_enumeration_type:
9455 case DW_TAG_pointer_type:
9456 case DW_TAG_reference_type:
9457 case DW_TAG_rvalue_reference_type:
9458 case DW_TAG_string_type:
9459 case DW_TAG_structure_type:
9460 case DW_TAG_subroutine_type:
9461 case DW_TAG_union_type:
9462 case DW_TAG_ptr_to_member_type:
9463 case DW_TAG_set_type:
9464 case DW_TAG_subrange_type:
9465 case DW_TAG_base_type:
9466 case DW_TAG_const_type:
9467 case DW_TAG_file_type:
9468 case DW_TAG_packed_type:
9469 case DW_TAG_volatile_type:
9470 case DW_TAG_typedef:
9471 return 1;
9472 default:
9473 return 0;
9477 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9478 Basically, we want to choose the bits that are likely to be shared between
9479 compilations (types) and leave out the bits that are specific to individual
9480 compilations (functions). */
9482 static int
9483 is_comdat_die (dw_die_ref c)
9485 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9486 we do for stabs. The advantage is a greater likelihood of sharing between
9487 objects that don't include headers in the same order (and therefore would
9488 put the base types in a different comdat). jason 8/28/00 */
9490 if (c->die_tag == DW_TAG_base_type)
9491 return 0;
9493 if (c->die_tag == DW_TAG_pointer_type
9494 || c->die_tag == DW_TAG_reference_type
9495 || c->die_tag == DW_TAG_rvalue_reference_type
9496 || c->die_tag == DW_TAG_const_type
9497 || c->die_tag == DW_TAG_volatile_type)
9499 dw_die_ref t = get_AT_ref (c, DW_AT_type);
9501 return t ? is_comdat_die (t) : 0;
9504 return is_type_die (c);
9507 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9508 compilation unit. */
9510 static int
9511 is_symbol_die (dw_die_ref c)
9513 return (is_type_die (c)
9514 || is_declaration_die (c)
9515 || c->die_tag == DW_TAG_namespace
9516 || c->die_tag == DW_TAG_module);
9519 static char *
9520 gen_internal_sym (const char *prefix)
9522 char buf[256];
9524 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9525 return xstrdup (buf);
9528 /* Assign symbols to all worthy DIEs under DIE. */
9530 static void
9531 assign_symbol_names (dw_die_ref die)
9533 dw_die_ref c;
9535 if (is_symbol_die (die))
9537 if (comdat_symbol_id)
9539 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9541 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9542 comdat_symbol_id, comdat_symbol_number++);
9543 die->die_id.die_symbol = xstrdup (p);
9545 else
9546 die->die_id.die_symbol = gen_internal_sym ("LDIE");
9549 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9552 struct cu_hash_table_entry
9554 dw_die_ref cu;
9555 unsigned min_comdat_num, max_comdat_num;
9556 struct cu_hash_table_entry *next;
9559 /* Routines to manipulate hash table of CUs. */
9560 static hashval_t
9561 htab_cu_hash (const void *of)
9563 const struct cu_hash_table_entry *const entry =
9564 (const struct cu_hash_table_entry *) of;
9566 return htab_hash_string (entry->cu->die_id.die_symbol);
9569 static int
9570 htab_cu_eq (const void *of1, const void *of2)
9572 const struct cu_hash_table_entry *const entry1 =
9573 (const struct cu_hash_table_entry *) of1;
9574 const struct die_struct *const entry2 = (const struct die_struct *) of2;
9576 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9579 static void
9580 htab_cu_del (void *what)
9582 struct cu_hash_table_entry *next,
9583 *entry = (struct cu_hash_table_entry *) what;
9585 while (entry)
9587 next = entry->next;
9588 free (entry);
9589 entry = next;
9593 /* Check whether we have already seen this CU and set up SYM_NUM
9594 accordingly. */
9595 static int
9596 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9598 struct cu_hash_table_entry dummy;
9599 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9601 dummy.max_comdat_num = 0;
9603 slot = (struct cu_hash_table_entry **)
9604 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9605 INSERT);
9606 entry = *slot;
9608 for (; entry; last = entry, entry = entry->next)
9610 if (same_die_p_wrap (cu, entry->cu))
9611 break;
9614 if (entry)
9616 *sym_num = entry->min_comdat_num;
9617 return 1;
9620 entry = XCNEW (struct cu_hash_table_entry);
9621 entry->cu = cu;
9622 entry->min_comdat_num = *sym_num = last->max_comdat_num;
9623 entry->next = *slot;
9624 *slot = entry;
9626 return 0;
9629 /* Record SYM_NUM to record of CU in HTABLE. */
9630 static void
9631 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9633 struct cu_hash_table_entry **slot, *entry;
9635 slot = (struct cu_hash_table_entry **)
9636 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9637 NO_INSERT);
9638 entry = *slot;
9640 entry->max_comdat_num = sym_num;
9643 /* Traverse the DIE (which is always comp_unit_die), and set up
9644 additional compilation units for each of the include files we see
9645 bracketed by BINCL/EINCL. */
9647 static void
9648 break_out_includes (dw_die_ref die)
9650 dw_die_ref c;
9651 dw_die_ref unit = NULL;
9652 limbo_die_node *node, **pnode;
9653 htab_t cu_hash_table;
9655 c = die->die_child;
9656 if (c) do {
9657 dw_die_ref prev = c;
9658 c = c->die_sib;
9659 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9660 || (unit && is_comdat_die (c)))
9662 dw_die_ref next = c->die_sib;
9664 /* This DIE is for a secondary CU; remove it from the main one. */
9665 remove_child_with_prev (c, prev);
9667 if (c->die_tag == DW_TAG_GNU_BINCL)
9668 unit = push_new_compile_unit (unit, c);
9669 else if (c->die_tag == DW_TAG_GNU_EINCL)
9670 unit = pop_compile_unit (unit);
9671 else
9672 add_child_die (unit, c);
9673 c = next;
9674 if (c == die->die_child)
9675 break;
9677 } while (c != die->die_child);
9679 #if 0
9680 /* We can only use this in debugging, since the frontend doesn't check
9681 to make sure that we leave every include file we enter. */
9682 gcc_assert (!unit);
9683 #endif
9685 assign_symbol_names (die);
9686 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9687 for (node = limbo_die_list, pnode = &limbo_die_list;
9688 node;
9689 node = node->next)
9691 int is_dupl;
9693 compute_section_prefix (node->die);
9694 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9695 &comdat_symbol_number);
9696 assign_symbol_names (node->die);
9697 if (is_dupl)
9698 *pnode = node->next;
9699 else
9701 pnode = &node->next;
9702 record_comdat_symbol_number (node->die, cu_hash_table,
9703 comdat_symbol_number);
9706 htab_delete (cu_hash_table);
9709 /* Return non-zero if this DIE is a declaration. */
9711 static int
9712 is_declaration_die (dw_die_ref die)
9714 dw_attr_ref a;
9715 unsigned ix;
9717 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9718 if (a->dw_attr == DW_AT_declaration)
9719 return 1;
9721 return 0;
9724 /* Return non-zero if this DIE is nested inside a subprogram. */
9726 static int
9727 is_nested_in_subprogram (dw_die_ref die)
9729 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9731 if (decl == NULL)
9732 decl = die;
9733 return local_scope_p (decl);
9736 /* Return non-zero if this is a type DIE that should be moved to a
9737 COMDAT .debug_types section. */
9739 static int
9740 should_move_die_to_comdat (dw_die_ref die)
9742 switch (die->die_tag)
9744 case DW_TAG_class_type:
9745 case DW_TAG_structure_type:
9746 case DW_TAG_enumeration_type:
9747 case DW_TAG_union_type:
9748 /* Don't move declarations, inlined instances, or types nested in a
9749 subprogram. */
9750 if (is_declaration_die (die)
9751 || get_AT (die, DW_AT_abstract_origin)
9752 || is_nested_in_subprogram (die))
9753 return 0;
9754 return 1;
9755 case DW_TAG_array_type:
9756 case DW_TAG_interface_type:
9757 case DW_TAG_pointer_type:
9758 case DW_TAG_reference_type:
9759 case DW_TAG_rvalue_reference_type:
9760 case DW_TAG_string_type:
9761 case DW_TAG_subroutine_type:
9762 case DW_TAG_ptr_to_member_type:
9763 case DW_TAG_set_type:
9764 case DW_TAG_subrange_type:
9765 case DW_TAG_base_type:
9766 case DW_TAG_const_type:
9767 case DW_TAG_file_type:
9768 case DW_TAG_packed_type:
9769 case DW_TAG_volatile_type:
9770 case DW_TAG_typedef:
9771 default:
9772 return 0;
9776 /* Make a clone of DIE. */
9778 static dw_die_ref
9779 clone_die (dw_die_ref die)
9781 dw_die_ref clone;
9782 dw_attr_ref a;
9783 unsigned ix;
9785 clone = ggc_alloc_cleared_die_node ();
9786 clone->die_tag = die->die_tag;
9788 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9789 add_dwarf_attr (clone, a);
9791 return clone;
9794 /* Make a clone of the tree rooted at DIE. */
9796 static dw_die_ref
9797 clone_tree (dw_die_ref die)
9799 dw_die_ref c;
9800 dw_die_ref clone = clone_die (die);
9802 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9804 return clone;
9807 /* Make a clone of DIE as a declaration. */
9809 static dw_die_ref
9810 clone_as_declaration (dw_die_ref die)
9812 dw_die_ref clone;
9813 dw_die_ref decl;
9814 dw_attr_ref a;
9815 unsigned ix;
9817 /* If the DIE is already a declaration, just clone it. */
9818 if (is_declaration_die (die))
9819 return clone_die (die);
9821 /* If the DIE is a specification, just clone its declaration DIE. */
9822 decl = get_AT_ref (die, DW_AT_specification);
9823 if (decl != NULL)
9824 return clone_die (decl);
9826 clone = ggc_alloc_cleared_die_node ();
9827 clone->die_tag = die->die_tag;
9829 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9831 /* We don't want to copy over all attributes.
9832 For example we don't want DW_AT_byte_size because otherwise we will no
9833 longer have a declaration and GDB will treat it as a definition. */
9835 switch (a->dw_attr)
9837 case DW_AT_artificial:
9838 case DW_AT_containing_type:
9839 case DW_AT_external:
9840 case DW_AT_name:
9841 case DW_AT_type:
9842 case DW_AT_virtuality:
9843 case DW_AT_linkage_name:
9844 case DW_AT_MIPS_linkage_name:
9845 add_dwarf_attr (clone, a);
9846 break;
9847 case DW_AT_byte_size:
9848 default:
9849 break;
9853 if (die->die_id.die_type_node)
9854 add_AT_die_ref (clone, DW_AT_signature, die);
9856 add_AT_flag (clone, DW_AT_declaration, 1);
9857 return clone;
9860 /* Copy the declaration context to the new compile unit DIE. This includes
9861 any surrounding namespace or type declarations. If the DIE has an
9862 AT_specification attribute, it also includes attributes and children
9863 attached to the specification. */
9865 static void
9866 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9868 dw_die_ref decl;
9869 dw_die_ref new_decl;
9871 decl = get_AT_ref (die, DW_AT_specification);
9872 if (decl == NULL)
9873 decl = die;
9874 else
9876 unsigned ix;
9877 dw_die_ref c;
9878 dw_attr_ref a;
9880 /* Copy the type node pointer from the new DIE to the original
9881 declaration DIE so we can forward references later. */
9882 decl->die_id.die_type_node = die->die_id.die_type_node;
9884 remove_AT (die, DW_AT_specification);
9886 FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
9888 if (a->dw_attr != DW_AT_name
9889 && a->dw_attr != DW_AT_declaration
9890 && a->dw_attr != DW_AT_external)
9891 add_dwarf_attr (die, a);
9894 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9897 if (decl->die_parent != NULL
9898 && decl->die_parent->die_tag != DW_TAG_compile_unit
9899 && decl->die_parent->die_tag != DW_TAG_type_unit)
9901 new_decl = copy_ancestor_tree (unit, decl, NULL);
9902 if (new_decl != NULL)
9904 remove_AT (new_decl, DW_AT_signature);
9905 add_AT_specification (die, new_decl);
9910 /* Generate the skeleton ancestor tree for the given NODE, then clone
9911 the DIE and add the clone into the tree. */
9913 static void
9914 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9916 if (node->new_die != NULL)
9917 return;
9919 node->new_die = clone_as_declaration (node->old_die);
9921 if (node->parent != NULL)
9923 generate_skeleton_ancestor_tree (node->parent);
9924 add_child_die (node->parent->new_die, node->new_die);
9928 /* Generate a skeleton tree of DIEs containing any declarations that are
9929 found in the original tree. We traverse the tree looking for declaration
9930 DIEs, and construct the skeleton from the bottom up whenever we find one. */
9932 static void
9933 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9935 skeleton_chain_node node;
9936 dw_die_ref c;
9937 dw_die_ref first;
9938 dw_die_ref prev = NULL;
9939 dw_die_ref next = NULL;
9941 node.parent = parent;
9943 first = c = parent->old_die->die_child;
9944 if (c)
9945 next = c->die_sib;
9946 if (c) do {
9947 if (prev == NULL || prev->die_sib == c)
9948 prev = c;
9949 c = next;
9950 next = (c == first ? NULL : c->die_sib);
9951 node.old_die = c;
9952 node.new_die = NULL;
9953 if (is_declaration_die (c))
9955 /* Clone the existing DIE, move the original to the skeleton
9956 tree (which is in the main CU), and put the clone, with
9957 all the original's children, where the original came from. */
9958 dw_die_ref clone = clone_die (c);
9959 move_all_children (c, clone);
9961 replace_child (c, clone, prev);
9962 generate_skeleton_ancestor_tree (parent);
9963 add_child_die (parent->new_die, c);
9964 node.new_die = c;
9965 c = clone;
9967 generate_skeleton_bottom_up (&node);
9968 } while (next != NULL);
9971 /* Wrapper function for generate_skeleton_bottom_up. */
9973 static dw_die_ref
9974 generate_skeleton (dw_die_ref die)
9976 skeleton_chain_node node;
9978 node.old_die = die;
9979 node.new_die = NULL;
9980 node.parent = NULL;
9982 /* If this type definition is nested inside another type,
9983 always leave at least a declaration in its place. */
9984 if (die->die_parent != NULL && is_type_die (die->die_parent))
9985 node.new_die = clone_as_declaration (die);
9987 generate_skeleton_bottom_up (&node);
9988 return node.new_die;
9991 /* Remove the DIE from its parent, possibly replacing it with a cloned
9992 declaration. The original DIE will be moved to a new compile unit
9993 so that existing references to it follow it to the new location. If
9994 any of the original DIE's descendants is a declaration, we need to
9995 replace the original DIE with a skeleton tree and move the
9996 declarations back into the skeleton tree. */
9998 static dw_die_ref
9999 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10001 dw_die_ref skeleton;
10003 skeleton = generate_skeleton (child);
10004 if (skeleton == NULL)
10005 remove_child_with_prev (child, prev);
10006 else
10008 skeleton->die_id.die_type_node = child->die_id.die_type_node;
10009 replace_child (child, skeleton, prev);
10012 return skeleton;
10015 /* Traverse the DIE and set up additional .debug_types sections for each
10016 type worthy of being placed in a COMDAT section. */
10018 static void
10019 break_out_comdat_types (dw_die_ref die)
10021 dw_die_ref c;
10022 dw_die_ref first;
10023 dw_die_ref prev = NULL;
10024 dw_die_ref next = NULL;
10025 dw_die_ref unit = NULL;
10027 first = c = die->die_child;
10028 if (c)
10029 next = c->die_sib;
10030 if (c) do {
10031 if (prev == NULL || prev->die_sib == c)
10032 prev = c;
10033 c = next;
10034 next = (c == first ? NULL : c->die_sib);
10035 if (should_move_die_to_comdat (c))
10037 dw_die_ref replacement;
10038 comdat_type_node_ref type_node;
10040 /* Create a new type unit DIE as the root for the new tree, and
10041 add it to the list of comdat types. */
10042 unit = new_die (DW_TAG_type_unit, NULL, NULL);
10043 add_AT_unsigned (unit, DW_AT_language,
10044 get_AT_unsigned (comp_unit_die, DW_AT_language));
10045 type_node = ggc_alloc_cleared_comdat_type_node ();
10046 type_node->root_die = unit;
10047 type_node->next = comdat_type_list;
10048 comdat_type_list = type_node;
10050 /* Generate the type signature. */
10051 generate_type_signature (c, type_node);
10053 /* Copy the declaration context, attributes, and children of the
10054 declaration into the new compile unit DIE. */
10055 copy_declaration_context (unit, c);
10057 /* Remove this DIE from the main CU. */
10058 replacement = remove_child_or_replace_with_skeleton (c, prev);
10060 /* Break out nested types into their own type units. */
10061 break_out_comdat_types (c);
10063 /* Add the DIE to the new compunit. */
10064 add_child_die (unit, c);
10066 if (replacement != NULL)
10067 c = replacement;
10069 else if (c->die_tag == DW_TAG_namespace
10070 || c->die_tag == DW_TAG_class_type
10071 || c->die_tag == DW_TAG_structure_type
10072 || c->die_tag == DW_TAG_union_type)
10074 /* Look for nested types that can be broken out. */
10075 break_out_comdat_types (c);
10077 } while (next != NULL);
10080 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
10082 struct decl_table_entry
10084 dw_die_ref orig;
10085 dw_die_ref copy;
10088 /* Routines to manipulate hash table of copied declarations. */
10090 static hashval_t
10091 htab_decl_hash (const void *of)
10093 const struct decl_table_entry *const entry =
10094 (const struct decl_table_entry *) of;
10096 return htab_hash_pointer (entry->orig);
10099 static int
10100 htab_decl_eq (const void *of1, const void *of2)
10102 const struct decl_table_entry *const entry1 =
10103 (const struct decl_table_entry *) of1;
10104 const struct die_struct *const entry2 = (const struct die_struct *) of2;
10106 return entry1->orig == entry2;
10109 static void
10110 htab_decl_del (void *what)
10112 struct decl_table_entry *entry = (struct decl_table_entry *) what;
10114 free (entry);
10117 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10118 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
10119 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
10120 to check if the ancestor has already been copied into UNIT. */
10122 static dw_die_ref
10123 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10125 dw_die_ref parent = die->die_parent;
10126 dw_die_ref new_parent = unit;
10127 dw_die_ref copy;
10128 void **slot = NULL;
10129 struct decl_table_entry *entry = NULL;
10131 if (decl_table)
10133 /* Check if the entry has already been copied to UNIT. */
10134 slot = htab_find_slot_with_hash (decl_table, die,
10135 htab_hash_pointer (die), INSERT);
10136 if (*slot != HTAB_EMPTY_ENTRY)
10138 entry = (struct decl_table_entry *) *slot;
10139 return entry->copy;
10142 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
10143 entry = XCNEW (struct decl_table_entry);
10144 entry->orig = die;
10145 entry->copy = NULL;
10146 *slot = entry;
10149 if (parent != NULL)
10151 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10152 if (spec != NULL)
10153 parent = spec;
10154 if (parent->die_tag != DW_TAG_compile_unit
10155 && parent->die_tag != DW_TAG_type_unit)
10156 new_parent = copy_ancestor_tree (unit, parent, decl_table);
10159 copy = clone_as_declaration (die);
10160 add_child_die (new_parent, copy);
10162 if (decl_table != NULL)
10164 /* Record the pointer to the copy. */
10165 entry->copy = copy;
10168 return copy;
10171 /* Walk the DIE and its children, looking for references to incomplete
10172 or trivial types that are unmarked (i.e., that are not in the current
10173 type_unit). */
10175 static void
10176 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10178 dw_die_ref c;
10179 dw_attr_ref a;
10180 unsigned ix;
10182 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10184 if (AT_class (a) == dw_val_class_die_ref)
10186 dw_die_ref targ = AT_ref (a);
10187 comdat_type_node_ref type_node = targ->die_id.die_type_node;
10188 void **slot;
10189 struct decl_table_entry *entry;
10191 if (targ->die_mark != 0 || type_node != NULL)
10192 continue;
10194 slot = htab_find_slot_with_hash (decl_table, targ,
10195 htab_hash_pointer (targ), INSERT);
10197 if (*slot != HTAB_EMPTY_ENTRY)
10199 /* TARG has already been copied, so we just need to
10200 modify the reference to point to the copy. */
10201 entry = (struct decl_table_entry *) *slot;
10202 a->dw_attr_val.v.val_die_ref.die = entry->copy;
10204 else
10206 dw_die_ref parent = unit;
10207 dw_die_ref copy = clone_tree (targ);
10209 /* Make sure the cloned tree is marked as part of the
10210 type unit. */
10211 mark_dies (copy);
10213 /* Record in DECL_TABLE that TARG has been copied.
10214 Need to do this now, before the recursive call,
10215 because DECL_TABLE may be expanded and SLOT
10216 would no longer be a valid pointer. */
10217 entry = XCNEW (struct decl_table_entry);
10218 entry->orig = targ;
10219 entry->copy = copy;
10220 *slot = entry;
10222 /* If TARG has surrounding context, copy its ancestor tree
10223 into the new type unit. */
10224 if (targ->die_parent != NULL
10225 && targ->die_parent->die_tag != DW_TAG_compile_unit
10226 && targ->die_parent->die_tag != DW_TAG_type_unit)
10227 parent = copy_ancestor_tree (unit, targ->die_parent,
10228 decl_table);
10230 add_child_die (parent, copy);
10231 a->dw_attr_val.v.val_die_ref.die = copy;
10233 /* Make sure the newly-copied DIE is walked. If it was
10234 installed in a previously-added context, it won't
10235 get visited otherwise. */
10236 if (parent != unit)
10238 /* Find the highest point of the newly-added tree,
10239 mark each node along the way, and walk from there. */
10240 parent->die_mark = 1;
10241 while (parent->die_parent
10242 && parent->die_parent->die_mark == 0)
10244 parent = parent->die_parent;
10245 parent->die_mark = 1;
10247 copy_decls_walk (unit, parent, decl_table);
10253 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10256 /* Copy declarations for "unworthy" types into the new comdat section.
10257 Incomplete types, modified types, and certain other types aren't broken
10258 out into comdat sections of their own, so they don't have a signature,
10259 and we need to copy the declaration into the same section so that we
10260 don't have an external reference. */
10262 static void
10263 copy_decls_for_unworthy_types (dw_die_ref unit)
10265 htab_t decl_table;
10267 mark_dies (unit);
10268 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10269 copy_decls_walk (unit, unit, decl_table);
10270 htab_delete (decl_table);
10271 unmark_dies (unit);
10274 /* Traverse the DIE and add a sibling attribute if it may have the
10275 effect of speeding up access to siblings. To save some space,
10276 avoid generating sibling attributes for DIE's without children. */
10278 static void
10279 add_sibling_attributes (dw_die_ref die)
10281 dw_die_ref c;
10283 if (! die->die_child)
10284 return;
10286 if (die->die_parent && die != die->die_parent->die_child)
10287 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10289 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10292 /* Output all location lists for the DIE and its children. */
10294 static void
10295 output_location_lists (dw_die_ref die)
10297 dw_die_ref c;
10298 dw_attr_ref a;
10299 unsigned ix;
10301 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10302 if (AT_class (a) == dw_val_class_loc_list)
10303 output_loc_list (AT_loc_list (a));
10305 FOR_EACH_CHILD (die, c, output_location_lists (c));
10308 /* The format of each DIE (and its attribute value pairs) is encoded in an
10309 abbreviation table. This routine builds the abbreviation table and assigns
10310 a unique abbreviation id for each abbreviation entry. The children of each
10311 die are visited recursively. */
10313 static void
10314 build_abbrev_table (dw_die_ref die)
10316 unsigned long abbrev_id;
10317 unsigned int n_alloc;
10318 dw_die_ref c;
10319 dw_attr_ref a;
10320 unsigned ix;
10322 /* Scan the DIE references, and mark as external any that refer to
10323 DIEs from other CUs (i.e. those which are not marked). */
10324 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10325 if (AT_class (a) == dw_val_class_die_ref
10326 && AT_ref (a)->die_mark == 0)
10328 gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10329 set_AT_ref_external (a, 1);
10332 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10334 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10335 dw_attr_ref die_a, abbrev_a;
10336 unsigned ix;
10337 bool ok = true;
10339 if (abbrev->die_tag != die->die_tag)
10340 continue;
10341 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10342 continue;
10344 if (VEC_length (dw_attr_node, abbrev->die_attr)
10345 != VEC_length (dw_attr_node, die->die_attr))
10346 continue;
10348 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10350 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10351 if ((abbrev_a->dw_attr != die_a->dw_attr)
10352 || (value_format (abbrev_a) != value_format (die_a)))
10354 ok = false;
10355 break;
10358 if (ok)
10359 break;
10362 if (abbrev_id >= abbrev_die_table_in_use)
10364 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10366 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10367 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10368 n_alloc);
10370 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10371 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10372 abbrev_die_table_allocated = n_alloc;
10375 ++abbrev_die_table_in_use;
10376 abbrev_die_table[abbrev_id] = die;
10379 die->die_abbrev = abbrev_id;
10380 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10383 /* Return the power-of-two number of bytes necessary to represent VALUE. */
10385 static int
10386 constant_size (unsigned HOST_WIDE_INT value)
10388 int log;
10390 if (value == 0)
10391 log = 0;
10392 else
10393 log = floor_log2 (value);
10395 log = log / 8;
10396 log = 1 << (floor_log2 (log) + 1);
10398 return log;
10401 /* Return the size of a DIE as it is represented in the
10402 .debug_info section. */
10404 static unsigned long
10405 size_of_die (dw_die_ref die)
10407 unsigned long size = 0;
10408 dw_attr_ref a;
10409 unsigned ix;
10411 size += size_of_uleb128 (die->die_abbrev);
10412 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10414 switch (AT_class (a))
10416 case dw_val_class_addr:
10417 size += DWARF2_ADDR_SIZE;
10418 break;
10419 case dw_val_class_offset:
10420 size += DWARF_OFFSET_SIZE;
10421 break;
10422 case dw_val_class_loc:
10424 unsigned long lsize = size_of_locs (AT_loc (a));
10426 /* Block length. */
10427 if (dwarf_version >= 4)
10428 size += size_of_uleb128 (lsize);
10429 else
10430 size += constant_size (lsize);
10431 size += lsize;
10433 break;
10434 case dw_val_class_loc_list:
10435 size += DWARF_OFFSET_SIZE;
10436 break;
10437 case dw_val_class_range_list:
10438 size += DWARF_OFFSET_SIZE;
10439 break;
10440 case dw_val_class_const:
10441 size += size_of_sleb128 (AT_int (a));
10442 break;
10443 case dw_val_class_unsigned_const:
10444 size += constant_size (AT_unsigned (a));
10445 break;
10446 case dw_val_class_const_double:
10447 size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10448 if (HOST_BITS_PER_WIDE_INT >= 64)
10449 size++; /* block */
10450 break;
10451 case dw_val_class_vec:
10452 size += constant_size (a->dw_attr_val.v.val_vec.length
10453 * a->dw_attr_val.v.val_vec.elt_size)
10454 + a->dw_attr_val.v.val_vec.length
10455 * a->dw_attr_val.v.val_vec.elt_size; /* block */
10456 break;
10457 case dw_val_class_flag:
10458 if (dwarf_version >= 4)
10459 /* Currently all add_AT_flag calls pass in 1 as last argument,
10460 so DW_FORM_flag_present can be used. If that ever changes,
10461 we'll need to use DW_FORM_flag and have some optimization
10462 in build_abbrev_table that will change those to
10463 DW_FORM_flag_present if it is set to 1 in all DIEs using
10464 the same abbrev entry. */
10465 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10466 else
10467 size += 1;
10468 break;
10469 case dw_val_class_die_ref:
10470 if (AT_ref_external (a))
10472 /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10473 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
10474 is sized by target address length, whereas in DWARF3
10475 it's always sized as an offset. */
10476 if (dwarf_version >= 4)
10477 size += DWARF_TYPE_SIGNATURE_SIZE;
10478 else if (dwarf_version == 2)
10479 size += DWARF2_ADDR_SIZE;
10480 else
10481 size += DWARF_OFFSET_SIZE;
10483 else
10484 size += DWARF_OFFSET_SIZE;
10485 break;
10486 case dw_val_class_fde_ref:
10487 size += DWARF_OFFSET_SIZE;
10488 break;
10489 case dw_val_class_lbl_id:
10490 size += DWARF2_ADDR_SIZE;
10491 break;
10492 case dw_val_class_lineptr:
10493 case dw_val_class_macptr:
10494 size += DWARF_OFFSET_SIZE;
10495 break;
10496 case dw_val_class_str:
10497 if (AT_string_form (a) == DW_FORM_strp)
10498 size += DWARF_OFFSET_SIZE;
10499 else
10500 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10501 break;
10502 case dw_val_class_file:
10503 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10504 break;
10505 case dw_val_class_data8:
10506 size += 8;
10507 break;
10508 case dw_val_class_vms_delta:
10509 size += DWARF_OFFSET_SIZE;
10510 break;
10511 default:
10512 gcc_unreachable ();
10516 return size;
10519 /* Size the debugging information associated with a given DIE. Visits the
10520 DIE's children recursively. Updates the global variable next_die_offset, on
10521 each time through. Uses the current value of next_die_offset to update the
10522 die_offset field in each DIE. */
10524 static void
10525 calc_die_sizes (dw_die_ref die)
10527 dw_die_ref c;
10529 die->die_offset = next_die_offset;
10530 next_die_offset += size_of_die (die);
10532 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10534 if (die->die_child != NULL)
10535 /* Count the null byte used to terminate sibling lists. */
10536 next_die_offset += 1;
10539 /* Set the marks for a die and its children. We do this so
10540 that we know whether or not a reference needs to use FORM_ref_addr; only
10541 DIEs in the same CU will be marked. We used to clear out the offset
10542 and use that as the flag, but ran into ordering problems. */
10544 static void
10545 mark_dies (dw_die_ref die)
10547 dw_die_ref c;
10549 gcc_assert (!die->die_mark);
10551 die->die_mark = 1;
10552 FOR_EACH_CHILD (die, c, mark_dies (c));
10555 /* Clear the marks for a die and its children. */
10557 static void
10558 unmark_dies (dw_die_ref die)
10560 dw_die_ref c;
10562 if (dwarf_version < 4)
10563 gcc_assert (die->die_mark);
10565 die->die_mark = 0;
10566 FOR_EACH_CHILD (die, c, unmark_dies (c));
10569 /* Clear the marks for a die, its children and referred dies. */
10571 static void
10572 unmark_all_dies (dw_die_ref die)
10574 dw_die_ref c;
10575 dw_attr_ref a;
10576 unsigned ix;
10578 if (!die->die_mark)
10579 return;
10580 die->die_mark = 0;
10582 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10584 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10585 if (AT_class (a) == dw_val_class_die_ref)
10586 unmark_all_dies (AT_ref (a));
10589 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10590 generated for the compilation unit. */
10592 static unsigned long
10593 size_of_pubnames (VEC (pubname_entry, gc) * names)
10595 unsigned long size;
10596 unsigned i;
10597 pubname_ref p;
10599 size = DWARF_PUBNAMES_HEADER_SIZE;
10600 FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10601 if (names != pubtype_table
10602 || p->die->die_offset != 0
10603 || !flag_eliminate_unused_debug_types)
10604 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10606 size += DWARF_OFFSET_SIZE;
10607 return size;
10610 /* Return the size of the information in the .debug_aranges section. */
10612 static unsigned long
10613 size_of_aranges (void)
10615 unsigned long size;
10617 size = DWARF_ARANGES_HEADER_SIZE;
10619 /* Count the address/length pair for this compilation unit. */
10620 if (text_section_used)
10621 size += 2 * DWARF2_ADDR_SIZE;
10622 if (cold_text_section_used)
10623 size += 2 * DWARF2_ADDR_SIZE;
10624 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10626 /* Count the two zero words used to terminated the address range table. */
10627 size += 2 * DWARF2_ADDR_SIZE;
10628 return size;
10631 /* Select the encoding of an attribute value. */
10633 static enum dwarf_form
10634 value_format (dw_attr_ref a)
10636 switch (a->dw_attr_val.val_class)
10638 case dw_val_class_addr:
10639 /* Only very few attributes allow DW_FORM_addr. */
10640 switch (a->dw_attr)
10642 case DW_AT_low_pc:
10643 case DW_AT_high_pc:
10644 case DW_AT_entry_pc:
10645 case DW_AT_trampoline:
10646 return DW_FORM_addr;
10647 default:
10648 break;
10650 switch (DWARF2_ADDR_SIZE)
10652 case 1:
10653 return DW_FORM_data1;
10654 case 2:
10655 return DW_FORM_data2;
10656 case 4:
10657 return DW_FORM_data4;
10658 case 8:
10659 return DW_FORM_data8;
10660 default:
10661 gcc_unreachable ();
10663 case dw_val_class_range_list:
10664 case dw_val_class_loc_list:
10665 if (dwarf_version >= 4)
10666 return DW_FORM_sec_offset;
10667 /* FALLTHRU */
10668 case dw_val_class_vms_delta:
10669 case dw_val_class_offset:
10670 switch (DWARF_OFFSET_SIZE)
10672 case 4:
10673 return DW_FORM_data4;
10674 case 8:
10675 return DW_FORM_data8;
10676 default:
10677 gcc_unreachable ();
10679 case dw_val_class_loc:
10680 if (dwarf_version >= 4)
10681 return DW_FORM_exprloc;
10682 switch (constant_size (size_of_locs (AT_loc (a))))
10684 case 1:
10685 return DW_FORM_block1;
10686 case 2:
10687 return DW_FORM_block2;
10688 default:
10689 gcc_unreachable ();
10691 case dw_val_class_const:
10692 return DW_FORM_sdata;
10693 case dw_val_class_unsigned_const:
10694 switch (constant_size (AT_unsigned (a)))
10696 case 1:
10697 return DW_FORM_data1;
10698 case 2:
10699 return DW_FORM_data2;
10700 case 4:
10701 return DW_FORM_data4;
10702 case 8:
10703 return DW_FORM_data8;
10704 default:
10705 gcc_unreachable ();
10707 case dw_val_class_const_double:
10708 switch (HOST_BITS_PER_WIDE_INT)
10710 case 8:
10711 return DW_FORM_data2;
10712 case 16:
10713 return DW_FORM_data4;
10714 case 32:
10715 return DW_FORM_data8;
10716 case 64:
10717 default:
10718 return DW_FORM_block1;
10720 case dw_val_class_vec:
10721 switch (constant_size (a->dw_attr_val.v.val_vec.length
10722 * a->dw_attr_val.v.val_vec.elt_size))
10724 case 1:
10725 return DW_FORM_block1;
10726 case 2:
10727 return DW_FORM_block2;
10728 case 4:
10729 return DW_FORM_block4;
10730 default:
10731 gcc_unreachable ();
10733 case dw_val_class_flag:
10734 if (dwarf_version >= 4)
10736 /* Currently all add_AT_flag calls pass in 1 as last argument,
10737 so DW_FORM_flag_present can be used. If that ever changes,
10738 we'll need to use DW_FORM_flag and have some optimization
10739 in build_abbrev_table that will change those to
10740 DW_FORM_flag_present if it is set to 1 in all DIEs using
10741 the same abbrev entry. */
10742 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10743 return DW_FORM_flag_present;
10745 return DW_FORM_flag;
10746 case dw_val_class_die_ref:
10747 if (AT_ref_external (a))
10748 return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10749 else
10750 return DW_FORM_ref;
10751 case dw_val_class_fde_ref:
10752 return DW_FORM_data;
10753 case dw_val_class_lbl_id:
10754 return DW_FORM_addr;
10755 case dw_val_class_lineptr:
10756 case dw_val_class_macptr:
10757 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10758 case dw_val_class_str:
10759 return AT_string_form (a);
10760 case dw_val_class_file:
10761 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10763 case 1:
10764 return DW_FORM_data1;
10765 case 2:
10766 return DW_FORM_data2;
10767 case 4:
10768 return DW_FORM_data4;
10769 default:
10770 gcc_unreachable ();
10773 case dw_val_class_data8:
10774 return DW_FORM_data8;
10776 default:
10777 gcc_unreachable ();
10781 /* Output the encoding of an attribute value. */
10783 static void
10784 output_value_format (dw_attr_ref a)
10786 enum dwarf_form form = value_format (a);
10788 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10791 /* Output the .debug_abbrev section which defines the DIE abbreviation
10792 table. */
10794 static void
10795 output_abbrev_section (void)
10797 unsigned long abbrev_id;
10799 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10801 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10802 unsigned ix;
10803 dw_attr_ref a_attr;
10805 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10806 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10807 dwarf_tag_name (abbrev->die_tag));
10809 if (abbrev->die_child != NULL)
10810 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10811 else
10812 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10814 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10815 ix++)
10817 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10818 dwarf_attr_name (a_attr->dw_attr));
10819 output_value_format (a_attr);
10822 dw2_asm_output_data (1, 0, NULL);
10823 dw2_asm_output_data (1, 0, NULL);
10826 /* Terminate the table. */
10827 dw2_asm_output_data (1, 0, NULL);
10830 /* Output a symbol we can use to refer to this DIE from another CU. */
10832 static inline void
10833 output_die_symbol (dw_die_ref die)
10835 char *sym = die->die_id.die_symbol;
10837 if (sym == 0)
10838 return;
10840 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10841 /* We make these global, not weak; if the target doesn't support
10842 .linkonce, it doesn't support combining the sections, so debugging
10843 will break. */
10844 targetm.asm_out.globalize_label (asm_out_file, sym);
10846 ASM_OUTPUT_LABEL (asm_out_file, sym);
10849 /* Return a new location list, given the begin and end range, and the
10850 expression. */
10852 static inline dw_loc_list_ref
10853 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10854 const char *section)
10856 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
10858 retlist->begin = begin;
10859 retlist->end = end;
10860 retlist->expr = expr;
10861 retlist->section = section;
10863 return retlist;
10866 /* Generate a new internal symbol for this location list node, if it
10867 hasn't got one yet. */
10869 static inline void
10870 gen_llsym (dw_loc_list_ref list)
10872 gcc_assert (!list->ll_symbol);
10873 list->ll_symbol = gen_internal_sym ("LLST");
10876 /* Output the location list given to us. */
10878 static void
10879 output_loc_list (dw_loc_list_ref list_head)
10881 dw_loc_list_ref curr = list_head;
10883 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10885 /* Walk the location list, and output each range + expression. */
10886 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10888 unsigned long size;
10889 /* Don't output an entry that starts and ends at the same address. */
10890 if (strcmp (curr->begin, curr->end) == 0)
10891 continue;
10892 if (!have_multiple_function_sections)
10894 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10895 "Location list begin address (%s)",
10896 list_head->ll_symbol);
10897 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10898 "Location list end address (%s)",
10899 list_head->ll_symbol);
10901 else
10903 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10904 "Location list begin address (%s)",
10905 list_head->ll_symbol);
10906 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10907 "Location list end address (%s)",
10908 list_head->ll_symbol);
10910 size = size_of_locs (curr->expr);
10912 /* Output the block length for this list of location operations. */
10913 gcc_assert (size <= 0xffff);
10914 dw2_asm_output_data (2, size, "%s", "Location expression size");
10916 output_loc_sequence (curr->expr);
10919 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10920 "Location list terminator begin (%s)",
10921 list_head->ll_symbol);
10922 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10923 "Location list terminator end (%s)",
10924 list_head->ll_symbol);
10927 /* Output a type signature. */
10929 static inline void
10930 output_signature (const char *sig, const char *name)
10932 int i;
10934 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10935 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10938 /* Output the DIE and its attributes. Called recursively to generate
10939 the definitions of each child DIE. */
10941 static void
10942 output_die (dw_die_ref die)
10944 dw_attr_ref a;
10945 dw_die_ref c;
10946 unsigned long size;
10947 unsigned ix;
10949 /* If someone in another CU might refer to us, set up a symbol for
10950 them to point to. */
10951 if (dwarf_version < 4 && die->die_id.die_symbol)
10952 output_die_symbol (die);
10954 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10955 (unsigned long)die->die_offset,
10956 dwarf_tag_name (die->die_tag));
10958 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10960 const char *name = dwarf_attr_name (a->dw_attr);
10962 switch (AT_class (a))
10964 case dw_val_class_addr:
10965 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10966 break;
10968 case dw_val_class_offset:
10969 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10970 "%s", name);
10971 break;
10973 case dw_val_class_range_list:
10975 char *p = strchr (ranges_section_label, '\0');
10977 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10978 a->dw_attr_val.v.val_offset);
10979 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10980 debug_ranges_section, "%s", name);
10981 *p = '\0';
10983 break;
10985 case dw_val_class_loc:
10986 size = size_of_locs (AT_loc (a));
10988 /* Output the block length for this list of location operations. */
10989 if (dwarf_version >= 4)
10990 dw2_asm_output_data_uleb128 (size, "%s", name);
10991 else
10992 dw2_asm_output_data (constant_size (size), size, "%s", name);
10994 output_loc_sequence (AT_loc (a));
10995 break;
10997 case dw_val_class_const:
10998 /* ??? It would be slightly more efficient to use a scheme like is
10999 used for unsigned constants below, but gdb 4.x does not sign
11000 extend. Gdb 5.x does sign extend. */
11001 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11002 break;
11004 case dw_val_class_unsigned_const:
11005 dw2_asm_output_data (constant_size (AT_unsigned (a)),
11006 AT_unsigned (a), "%s", name);
11007 break;
11009 case dw_val_class_const_double:
11011 unsigned HOST_WIDE_INT first, second;
11013 if (HOST_BITS_PER_WIDE_INT >= 64)
11014 dw2_asm_output_data (1,
11015 2 * HOST_BITS_PER_WIDE_INT
11016 / HOST_BITS_PER_CHAR,
11017 NULL);
11019 if (WORDS_BIG_ENDIAN)
11021 first = a->dw_attr_val.v.val_double.high;
11022 second = a->dw_attr_val.v.val_double.low;
11024 else
11026 first = a->dw_attr_val.v.val_double.low;
11027 second = a->dw_attr_val.v.val_double.high;
11030 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11031 first, name);
11032 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11033 second, NULL);
11035 break;
11037 case dw_val_class_vec:
11039 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11040 unsigned int len = a->dw_attr_val.v.val_vec.length;
11041 unsigned int i;
11042 unsigned char *p;
11044 dw2_asm_output_data (constant_size (len * elt_size),
11045 len * elt_size, "%s", name);
11046 if (elt_size > sizeof (HOST_WIDE_INT))
11048 elt_size /= 2;
11049 len *= 2;
11051 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11052 i < len;
11053 i++, p += elt_size)
11054 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11055 "fp or vector constant word %u", i);
11056 break;
11059 case dw_val_class_flag:
11060 if (dwarf_version >= 4)
11062 /* Currently all add_AT_flag calls pass in 1 as last argument,
11063 so DW_FORM_flag_present can be used. If that ever changes,
11064 we'll need to use DW_FORM_flag and have some optimization
11065 in build_abbrev_table that will change those to
11066 DW_FORM_flag_present if it is set to 1 in all DIEs using
11067 the same abbrev entry. */
11068 gcc_assert (AT_flag (a) == 1);
11069 if (flag_debug_asm)
11070 fprintf (asm_out_file, "\t\t\t%s %s\n",
11071 ASM_COMMENT_START, name);
11072 break;
11074 dw2_asm_output_data (1, AT_flag (a), "%s", name);
11075 break;
11077 case dw_val_class_loc_list:
11079 char *sym = AT_loc_list (a)->ll_symbol;
11081 gcc_assert (sym);
11082 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11083 "%s", name);
11085 break;
11087 case dw_val_class_die_ref:
11088 if (AT_ref_external (a))
11090 if (dwarf_version >= 4)
11092 comdat_type_node_ref type_node =
11093 AT_ref (a)->die_id.die_type_node;
11095 gcc_assert (type_node);
11096 output_signature (type_node->signature, name);
11098 else
11100 char *sym = AT_ref (a)->die_id.die_symbol;
11101 int size;
11103 gcc_assert (sym);
11104 /* In DWARF2, DW_FORM_ref_addr is sized by target address
11105 length, whereas in DWARF3 it's always sized as an
11106 offset. */
11107 if (dwarf_version == 2)
11108 size = DWARF2_ADDR_SIZE;
11109 else
11110 size = DWARF_OFFSET_SIZE;
11111 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11112 name);
11115 else
11117 gcc_assert (AT_ref (a)->die_offset);
11118 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11119 "%s", name);
11121 break;
11123 case dw_val_class_fde_ref:
11125 char l1[20];
11127 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11128 a->dw_attr_val.v.val_fde_index * 2);
11129 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11130 "%s", name);
11132 break;
11134 case dw_val_class_vms_delta:
11135 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11136 AT_vms_delta2 (a), AT_vms_delta1 (a),
11137 "%s", name);
11138 break;
11140 case dw_val_class_lbl_id:
11141 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11142 break;
11144 case dw_val_class_lineptr:
11145 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11146 debug_line_section, "%s", name);
11147 break;
11149 case dw_val_class_macptr:
11150 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11151 debug_macinfo_section, "%s", name);
11152 break;
11154 case dw_val_class_str:
11155 if (AT_string_form (a) == DW_FORM_strp)
11156 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11157 a->dw_attr_val.v.val_str->label,
11158 debug_str_section,
11159 "%s: \"%s\"", name, AT_string (a));
11160 else
11161 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11162 break;
11164 case dw_val_class_file:
11166 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11168 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11169 a->dw_attr_val.v.val_file->filename);
11170 break;
11173 case dw_val_class_data8:
11175 int i;
11177 for (i = 0; i < 8; i++)
11178 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11179 i == 0 ? "%s" : NULL, name);
11180 break;
11183 default:
11184 gcc_unreachable ();
11188 FOR_EACH_CHILD (die, c, output_die (c));
11190 /* Add null byte to terminate sibling list. */
11191 if (die->die_child != NULL)
11192 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11193 (unsigned long) die->die_offset);
11196 /* Output the compilation unit that appears at the beginning of the
11197 .debug_info section, and precedes the DIE descriptions. */
11199 static void
11200 output_compilation_unit_header (void)
11202 int ver = dwarf_version;
11204 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11205 dw2_asm_output_data (4, 0xffffffff,
11206 "Initial length escape value indicating 64-bit DWARF extension");
11207 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11208 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11209 "Length of Compilation Unit Info");
11210 dw2_asm_output_data (2, ver, "DWARF version number");
11211 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11212 debug_abbrev_section,
11213 "Offset Into Abbrev. Section");
11214 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11217 /* Output the compilation unit DIE and its children. */
11219 static void
11220 output_comp_unit (dw_die_ref die, int output_if_empty)
11222 const char *secname;
11223 char *oldsym, *tmp;
11225 /* Unless we are outputting main CU, we may throw away empty ones. */
11226 if (!output_if_empty && die->die_child == NULL)
11227 return;
11229 /* Even if there are no children of this DIE, we must output the information
11230 about the compilation unit. Otherwise, on an empty translation unit, we
11231 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11232 will then complain when examining the file. First mark all the DIEs in
11233 this CU so we know which get local refs. */
11234 mark_dies (die);
11236 build_abbrev_table (die);
11238 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11239 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11240 calc_die_sizes (die);
11242 oldsym = die->die_id.die_symbol;
11243 if (oldsym)
11245 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11247 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11248 secname = tmp;
11249 die->die_id.die_symbol = NULL;
11250 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11252 else
11253 switch_to_section (debug_info_section);
11255 /* Output debugging information. */
11256 output_compilation_unit_header ();
11257 output_die (die);
11259 /* Leave the marks on the main CU, so we can check them in
11260 output_pubnames. */
11261 if (oldsym)
11263 unmark_dies (die);
11264 die->die_id.die_symbol = oldsym;
11268 /* Output a comdat type unit DIE and its children. */
11270 static void
11271 output_comdat_type_unit (comdat_type_node *node)
11273 const char *secname;
11274 char *tmp;
11275 int i;
11276 #if defined (OBJECT_FORMAT_ELF)
11277 tree comdat_key;
11278 #endif
11280 /* First mark all the DIEs in this CU so we know which get local refs. */
11281 mark_dies (node->root_die);
11283 build_abbrev_table (node->root_die);
11285 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11286 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11287 calc_die_sizes (node->root_die);
11289 #if defined (OBJECT_FORMAT_ELF)
11290 secname = ".debug_types";
11291 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11292 sprintf (tmp, "wt.");
11293 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11294 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11295 comdat_key = get_identifier (tmp);
11296 targetm.asm_out.named_section (secname,
11297 SECTION_DEBUG | SECTION_LINKONCE,
11298 comdat_key);
11299 #else
11300 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11301 sprintf (tmp, ".gnu.linkonce.wt.");
11302 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11303 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11304 secname = tmp;
11305 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11306 #endif
11308 /* Output debugging information. */
11309 output_compilation_unit_header ();
11310 output_signature (node->signature, "Type Signature");
11311 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11312 "Offset to Type DIE");
11313 output_die (node->root_die);
11315 unmark_dies (node->root_die);
11318 /* Return the DWARF2/3 pubname associated with a decl. */
11320 static const char *
11321 dwarf2_name (tree decl, int scope)
11323 if (DECL_NAMELESS (decl))
11324 return NULL;
11325 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11328 /* Add a new entry to .debug_pubnames if appropriate. */
11330 static void
11331 add_pubname_string (const char *str, dw_die_ref die)
11333 if (targetm.want_debug_pub_sections)
11335 pubname_entry e;
11337 e.die = die;
11338 e.name = xstrdup (str);
11339 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11343 static void
11344 add_pubname (tree decl, dw_die_ref die)
11346 if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11348 const char *name = dwarf2_name (decl, 1);
11349 if (name)
11350 add_pubname_string (name, die);
11354 /* Add a new entry to .debug_pubtypes if appropriate. */
11356 static void
11357 add_pubtype (tree decl, dw_die_ref die)
11359 pubname_entry e;
11361 if (!targetm.want_debug_pub_sections)
11362 return;
11364 e.name = NULL;
11365 if ((TREE_PUBLIC (decl)
11366 || die->die_parent == comp_unit_die)
11367 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11369 e.die = die;
11370 if (TYPE_P (decl))
11372 if (TYPE_NAME (decl))
11374 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11375 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11376 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11377 && DECL_NAME (TYPE_NAME (decl)))
11378 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11379 else
11380 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11383 else
11385 e.name = dwarf2_name (decl, 1);
11386 if (e.name)
11387 e.name = xstrdup (e.name);
11390 /* If we don't have a name for the type, there's no point in adding
11391 it to the table. */
11392 if (e.name && e.name[0] != '\0')
11393 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11397 /* Output the public names table used to speed up access to externally
11398 visible names; or the public types table used to find type definitions. */
11400 static void
11401 output_pubnames (VEC (pubname_entry, gc) * names)
11403 unsigned i;
11404 unsigned long pubnames_length = size_of_pubnames (names);
11405 pubname_ref pub;
11407 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11408 dw2_asm_output_data (4, 0xffffffff,
11409 "Initial length escape value indicating 64-bit DWARF extension");
11410 if (names == pubname_table)
11411 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11412 "Length of Public Names Info");
11413 else
11414 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11415 "Length of Public Type Names Info");
11416 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
11417 dw2_asm_output_data (2, 2, "DWARF Version");
11418 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11419 debug_info_section,
11420 "Offset of Compilation Unit Info");
11421 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11422 "Compilation Unit Length");
11424 FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11426 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11427 if (names == pubname_table)
11428 gcc_assert (pub->die->die_mark);
11430 if (names != pubtype_table
11431 || pub->die->die_offset != 0
11432 || !flag_eliminate_unused_debug_types)
11434 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11435 "DIE offset");
11437 dw2_asm_output_nstring (pub->name, -1, "external name");
11441 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11444 /* Add a new entry to .debug_aranges if appropriate. */
11446 static void
11447 add_arange (tree decl, dw_die_ref die)
11449 if (! DECL_SECTION_NAME (decl))
11450 return;
11452 if (arange_table_in_use == arange_table_allocated)
11454 arange_table_allocated += ARANGE_TABLE_INCREMENT;
11455 arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11456 arange_table_allocated);
11457 memset (arange_table + arange_table_in_use, 0,
11458 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11461 arange_table[arange_table_in_use++] = die;
11464 /* Output the information that goes into the .debug_aranges table.
11465 Namely, define the beginning and ending address range of the
11466 text section generated for this compilation unit. */
11468 static void
11469 output_aranges (void)
11471 unsigned i;
11472 unsigned long aranges_length = size_of_aranges ();
11474 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11475 dw2_asm_output_data (4, 0xffffffff,
11476 "Initial length escape value indicating 64-bit DWARF extension");
11477 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11478 "Length of Address Ranges Info");
11479 /* Version number for aranges is still 2, even in DWARF3. */
11480 dw2_asm_output_data (2, 2, "DWARF Version");
11481 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11482 debug_info_section,
11483 "Offset of Compilation Unit Info");
11484 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11485 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11487 /* We need to align to twice the pointer size here. */
11488 if (DWARF_ARANGES_PAD_SIZE)
11490 /* Pad using a 2 byte words so that padding is correct for any
11491 pointer size. */
11492 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11493 2 * DWARF2_ADDR_SIZE);
11494 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11495 dw2_asm_output_data (2, 0, NULL);
11498 /* It is necessary not to output these entries if the sections were
11499 not used; if the sections were not used, the length will be 0 and
11500 the address may end up as 0 if the section is discarded by ld
11501 --gc-sections, leaving an invalid (0, 0) entry that can be
11502 confused with the terminator. */
11503 if (text_section_used)
11505 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11506 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11507 text_section_label, "Length");
11509 if (cold_text_section_used)
11511 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11512 "Address");
11513 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11514 cold_text_section_label, "Length");
11517 for (i = 0; i < arange_table_in_use; i++)
11519 dw_die_ref die = arange_table[i];
11521 /* We shouldn't see aranges for DIEs outside of the main CU. */
11522 gcc_assert (die->die_mark);
11524 if (die->die_tag == DW_TAG_subprogram)
11526 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11527 "Address");
11528 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11529 get_AT_low_pc (die), "Length");
11531 else
11533 /* A static variable; extract the symbol from DW_AT_location.
11534 Note that this code isn't currently hit, as we only emit
11535 aranges for functions (jason 9/23/99). */
11536 dw_attr_ref a = get_AT (die, DW_AT_location);
11537 dw_loc_descr_ref loc;
11539 gcc_assert (a && AT_class (a) == dw_val_class_loc);
11541 loc = AT_loc (a);
11542 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11544 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11545 loc->dw_loc_oprnd1.v.val_addr, "Address");
11546 dw2_asm_output_data (DWARF2_ADDR_SIZE,
11547 get_AT_unsigned (die, DW_AT_byte_size),
11548 "Length");
11552 /* Output the terminator words. */
11553 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11554 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11557 /* Add a new entry to .debug_ranges. Return the offset at which it
11558 was placed. */
11560 static unsigned int
11561 add_ranges_num (int num)
11563 unsigned int in_use = ranges_table_in_use;
11565 if (in_use == ranges_table_allocated)
11567 ranges_table_allocated += RANGES_TABLE_INCREMENT;
11568 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11569 ranges_table_allocated);
11570 memset (ranges_table + ranges_table_in_use, 0,
11571 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11574 ranges_table[in_use].num = num;
11575 ranges_table_in_use = in_use + 1;
11577 return in_use * 2 * DWARF2_ADDR_SIZE;
11580 /* Add a new entry to .debug_ranges corresponding to a block, or a
11581 range terminator if BLOCK is NULL. */
11583 static unsigned int
11584 add_ranges (const_tree block)
11586 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11589 /* Add a new entry to .debug_ranges corresponding to a pair of
11590 labels. */
11592 static void
11593 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11594 bool *added)
11596 unsigned int in_use = ranges_by_label_in_use;
11597 unsigned int offset;
11599 if (in_use == ranges_by_label_allocated)
11601 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11602 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11603 ranges_by_label,
11604 ranges_by_label_allocated);
11605 memset (ranges_by_label + ranges_by_label_in_use, 0,
11606 RANGES_TABLE_INCREMENT
11607 * sizeof (struct dw_ranges_by_label_struct));
11610 ranges_by_label[in_use].begin = begin;
11611 ranges_by_label[in_use].end = end;
11612 ranges_by_label_in_use = in_use + 1;
11614 offset = add_ranges_num (-(int)in_use - 1);
11615 if (!*added)
11617 add_AT_range_list (die, DW_AT_ranges, offset);
11618 *added = true;
11622 static void
11623 output_ranges (void)
11625 unsigned i;
11626 static const char *const start_fmt = "Offset %#x";
11627 const char *fmt = start_fmt;
11629 for (i = 0; i < ranges_table_in_use; i++)
11631 int block_num = ranges_table[i].num;
11633 if (block_num > 0)
11635 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11636 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11638 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11639 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11641 /* If all code is in the text section, then the compilation
11642 unit base address defaults to DW_AT_low_pc, which is the
11643 base of the text section. */
11644 if (!have_multiple_function_sections)
11646 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11647 text_section_label,
11648 fmt, i * 2 * DWARF2_ADDR_SIZE);
11649 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11650 text_section_label, NULL);
11653 /* Otherwise, the compilation unit base address is zero,
11654 which allows us to use absolute addresses, and not worry
11655 about whether the target supports cross-section
11656 arithmetic. */
11657 else
11659 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11660 fmt, i * 2 * DWARF2_ADDR_SIZE);
11661 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11664 fmt = NULL;
11667 /* Negative block_num stands for an index into ranges_by_label. */
11668 else if (block_num < 0)
11670 int lab_idx = - block_num - 1;
11672 if (!have_multiple_function_sections)
11674 gcc_unreachable ();
11675 #if 0
11676 /* If we ever use add_ranges_by_labels () for a single
11677 function section, all we have to do is to take out
11678 the #if 0 above. */
11679 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11680 ranges_by_label[lab_idx].begin,
11681 text_section_label,
11682 fmt, i * 2 * DWARF2_ADDR_SIZE);
11683 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11684 ranges_by_label[lab_idx].end,
11685 text_section_label, NULL);
11686 #endif
11688 else
11690 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11691 ranges_by_label[lab_idx].begin,
11692 fmt, i * 2 * DWARF2_ADDR_SIZE);
11693 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11694 ranges_by_label[lab_idx].end,
11695 NULL);
11698 else
11700 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11701 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11702 fmt = start_fmt;
11707 /* Data structure containing information about input files. */
11708 struct file_info
11710 const char *path; /* Complete file name. */
11711 const char *fname; /* File name part. */
11712 int length; /* Length of entire string. */
11713 struct dwarf_file_data * file_idx; /* Index in input file table. */
11714 int dir_idx; /* Index in directory table. */
11717 /* Data structure containing information about directories with source
11718 files. */
11719 struct dir_info
11721 const char *path; /* Path including directory name. */
11722 int length; /* Path length. */
11723 int prefix; /* Index of directory entry which is a prefix. */
11724 int count; /* Number of files in this directory. */
11725 int dir_idx; /* Index of directory used as base. */
11728 /* Callback function for file_info comparison. We sort by looking at
11729 the directories in the path. */
11731 static int
11732 file_info_cmp (const void *p1, const void *p2)
11734 const struct file_info *const s1 = (const struct file_info *) p1;
11735 const struct file_info *const s2 = (const struct file_info *) p2;
11736 const unsigned char *cp1;
11737 const unsigned char *cp2;
11739 /* Take care of file names without directories. We need to make sure that
11740 we return consistent values to qsort since some will get confused if
11741 we return the same value when identical operands are passed in opposite
11742 orders. So if neither has a directory, return 0 and otherwise return
11743 1 or -1 depending on which one has the directory. */
11744 if ((s1->path == s1->fname || s2->path == s2->fname))
11745 return (s2->path == s2->fname) - (s1->path == s1->fname);
11747 cp1 = (const unsigned char *) s1->path;
11748 cp2 = (const unsigned char *) s2->path;
11750 while (1)
11752 ++cp1;
11753 ++cp2;
11754 /* Reached the end of the first path? If so, handle like above. */
11755 if ((cp1 == (const unsigned char *) s1->fname)
11756 || (cp2 == (const unsigned char *) s2->fname))
11757 return ((cp2 == (const unsigned char *) s2->fname)
11758 - (cp1 == (const unsigned char *) s1->fname));
11760 /* Character of current path component the same? */
11761 else if (*cp1 != *cp2)
11762 return *cp1 - *cp2;
11766 struct file_name_acquire_data
11768 struct file_info *files;
11769 int used_files;
11770 int max_files;
11773 /* Traversal function for the hash table. */
11775 static int
11776 file_name_acquire (void ** slot, void *data)
11778 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11779 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11780 struct file_info *fi;
11781 const char *f;
11783 gcc_assert (fnad->max_files >= d->emitted_number);
11785 if (! d->emitted_number)
11786 return 1;
11788 gcc_assert (fnad->max_files != fnad->used_files);
11790 fi = fnad->files + fnad->used_files++;
11792 /* Skip all leading "./". */
11793 f = d->filename;
11794 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11795 f += 2;
11797 /* Create a new array entry. */
11798 fi->path = f;
11799 fi->length = strlen (f);
11800 fi->file_idx = d;
11802 /* Search for the file name part. */
11803 f = strrchr (f, DIR_SEPARATOR);
11804 #if defined (DIR_SEPARATOR_2)
11806 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11808 if (g != NULL)
11810 if (f == NULL || f < g)
11811 f = g;
11814 #endif
11816 fi->fname = f == NULL ? fi->path : f + 1;
11817 return 1;
11820 /* Output the directory table and the file name table. We try to minimize
11821 the total amount of memory needed. A heuristic is used to avoid large
11822 slowdowns with many input files. */
11824 static void
11825 output_file_names (void)
11827 struct file_name_acquire_data fnad;
11828 int numfiles;
11829 struct file_info *files;
11830 struct dir_info *dirs;
11831 int *saved;
11832 int *savehere;
11833 int *backmap;
11834 int ndirs;
11835 int idx_offset;
11836 int i;
11838 if (!last_emitted_file)
11840 dw2_asm_output_data (1, 0, "End directory table");
11841 dw2_asm_output_data (1, 0, "End file name table");
11842 return;
11845 numfiles = last_emitted_file->emitted_number;
11847 /* Allocate the various arrays we need. */
11848 files = XALLOCAVEC (struct file_info, numfiles);
11849 dirs = XALLOCAVEC (struct dir_info, numfiles);
11851 fnad.files = files;
11852 fnad.used_files = 0;
11853 fnad.max_files = numfiles;
11854 htab_traverse (file_table, file_name_acquire, &fnad);
11855 gcc_assert (fnad.used_files == fnad.max_files);
11857 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11859 /* Find all the different directories used. */
11860 dirs[0].path = files[0].path;
11861 dirs[0].length = files[0].fname - files[0].path;
11862 dirs[0].prefix = -1;
11863 dirs[0].count = 1;
11864 dirs[0].dir_idx = 0;
11865 files[0].dir_idx = 0;
11866 ndirs = 1;
11868 for (i = 1; i < numfiles; i++)
11869 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11870 && memcmp (dirs[ndirs - 1].path, files[i].path,
11871 dirs[ndirs - 1].length) == 0)
11873 /* Same directory as last entry. */
11874 files[i].dir_idx = ndirs - 1;
11875 ++dirs[ndirs - 1].count;
11877 else
11879 int j;
11881 /* This is a new directory. */
11882 dirs[ndirs].path = files[i].path;
11883 dirs[ndirs].length = files[i].fname - files[i].path;
11884 dirs[ndirs].count = 1;
11885 dirs[ndirs].dir_idx = ndirs;
11886 files[i].dir_idx = ndirs;
11888 /* Search for a prefix. */
11889 dirs[ndirs].prefix = -1;
11890 for (j = 0; j < ndirs; j++)
11891 if (dirs[j].length < dirs[ndirs].length
11892 && dirs[j].length > 1
11893 && (dirs[ndirs].prefix == -1
11894 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11895 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11896 dirs[ndirs].prefix = j;
11898 ++ndirs;
11901 /* Now to the actual work. We have to find a subset of the directories which
11902 allow expressing the file name using references to the directory table
11903 with the least amount of characters. We do not do an exhaustive search
11904 where we would have to check out every combination of every single
11905 possible prefix. Instead we use a heuristic which provides nearly optimal
11906 results in most cases and never is much off. */
11907 saved = XALLOCAVEC (int, ndirs);
11908 savehere = XALLOCAVEC (int, ndirs);
11910 memset (saved, '\0', ndirs * sizeof (saved[0]));
11911 for (i = 0; i < ndirs; i++)
11913 int j;
11914 int total;
11916 /* We can always save some space for the current directory. But this
11917 does not mean it will be enough to justify adding the directory. */
11918 savehere[i] = dirs[i].length;
11919 total = (savehere[i] - saved[i]) * dirs[i].count;
11921 for (j = i + 1; j < ndirs; j++)
11923 savehere[j] = 0;
11924 if (saved[j] < dirs[i].length)
11926 /* Determine whether the dirs[i] path is a prefix of the
11927 dirs[j] path. */
11928 int k;
11930 k = dirs[j].prefix;
11931 while (k != -1 && k != (int) i)
11932 k = dirs[k].prefix;
11934 if (k == (int) i)
11936 /* Yes it is. We can possibly save some memory by
11937 writing the filenames in dirs[j] relative to
11938 dirs[i]. */
11939 savehere[j] = dirs[i].length;
11940 total += (savehere[j] - saved[j]) * dirs[j].count;
11945 /* Check whether we can save enough to justify adding the dirs[i]
11946 directory. */
11947 if (total > dirs[i].length + 1)
11949 /* It's worthwhile adding. */
11950 for (j = i; j < ndirs; j++)
11951 if (savehere[j] > 0)
11953 /* Remember how much we saved for this directory so far. */
11954 saved[j] = savehere[j];
11956 /* Remember the prefix directory. */
11957 dirs[j].dir_idx = i;
11962 /* Emit the directory name table. */
11963 idx_offset = dirs[0].length > 0 ? 1 : 0;
11964 for (i = 1 - idx_offset; i < ndirs; i++)
11965 dw2_asm_output_nstring (dirs[i].path,
11966 dirs[i].length
11967 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11968 "Directory Entry: %#x", i + idx_offset);
11970 dw2_asm_output_data (1, 0, "End directory table");
11972 /* We have to emit them in the order of emitted_number since that's
11973 used in the debug info generation. To do this efficiently we
11974 generate a back-mapping of the indices first. */
11975 backmap = XALLOCAVEC (int, numfiles);
11976 for (i = 0; i < numfiles; i++)
11977 backmap[files[i].file_idx->emitted_number - 1] = i;
11979 /* Now write all the file names. */
11980 for (i = 0; i < numfiles; i++)
11982 int file_idx = backmap[i];
11983 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11985 #ifdef VMS_DEBUGGING_INFO
11986 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11988 /* Setting these fields can lead to debugger miscomparisons,
11989 but VMS Debug requires them to be set correctly. */
11991 int ver;
11992 long long cdt;
11993 long siz;
11994 int maxfilelen = strlen (files[file_idx].path)
11995 + dirs[dir_idx].length
11996 + MAX_VMS_VERSION_LEN + 1;
11997 char *filebuf = XALLOCAVEC (char, maxfilelen);
11999 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12000 snprintf (filebuf, maxfilelen, "%s;%d",
12001 files[file_idx].path + dirs[dir_idx].length, ver);
12003 dw2_asm_output_nstring
12004 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12006 /* Include directory index. */
12007 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12009 /* Modification time. */
12010 dw2_asm_output_data_uleb128
12011 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12012 ? cdt : 0,
12013 NULL);
12015 /* File length in bytes. */
12016 dw2_asm_output_data_uleb128
12017 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12018 ? siz : 0,
12019 NULL);
12020 #else
12021 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12022 "File Entry: %#x", (unsigned) i + 1);
12024 /* Include directory index. */
12025 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12027 /* Modification time. */
12028 dw2_asm_output_data_uleb128 (0, NULL);
12030 /* File length in bytes. */
12031 dw2_asm_output_data_uleb128 (0, NULL);
12032 #endif /* VMS_DEBUGGING_INFO */
12035 dw2_asm_output_data (1, 0, "End file name table");
12039 /* Output the source line number correspondence information. This
12040 information goes into the .debug_line section. */
12042 static void
12043 output_line_info (void)
12045 char l1[20], l2[20], p1[20], p2[20];
12046 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12047 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12048 unsigned opc;
12049 unsigned n_op_args;
12050 unsigned long lt_index;
12051 unsigned long current_line;
12052 long line_offset;
12053 long line_delta;
12054 unsigned long current_file;
12055 unsigned long function;
12056 int ver = dwarf_version;
12058 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12059 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12060 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12061 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12063 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12064 dw2_asm_output_data (4, 0xffffffff,
12065 "Initial length escape value indicating 64-bit DWARF extension");
12066 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12067 "Length of Source Line Info");
12068 ASM_OUTPUT_LABEL (asm_out_file, l1);
12070 dw2_asm_output_data (2, ver, "DWARF Version");
12071 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12072 ASM_OUTPUT_LABEL (asm_out_file, p1);
12074 /* Define the architecture-dependent minimum instruction length (in
12075 bytes). In this implementation of DWARF, this field is used for
12076 information purposes only. Since GCC generates assembly language,
12077 we have no a priori knowledge of how many instruction bytes are
12078 generated for each source line, and therefore can use only the
12079 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12080 commands. Accordingly, we fix this as `1', which is "correct
12081 enough" for all architectures, and don't let the target override. */
12082 dw2_asm_output_data (1, 1,
12083 "Minimum Instruction Length");
12085 if (ver >= 4)
12086 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12087 "Maximum Operations Per Instruction");
12088 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12089 "Default is_stmt_start flag");
12090 dw2_asm_output_data (1, DWARF_LINE_BASE,
12091 "Line Base Value (Special Opcodes)");
12092 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12093 "Line Range Value (Special Opcodes)");
12094 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12095 "Special Opcode Base");
12097 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12099 switch (opc)
12101 case DW_LNS_advance_pc:
12102 case DW_LNS_advance_line:
12103 case DW_LNS_set_file:
12104 case DW_LNS_set_column:
12105 case DW_LNS_fixed_advance_pc:
12106 n_op_args = 1;
12107 break;
12108 default:
12109 n_op_args = 0;
12110 break;
12113 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12114 opc, n_op_args);
12117 /* Write out the information about the files we use. */
12118 output_file_names ();
12119 ASM_OUTPUT_LABEL (asm_out_file, p2);
12121 /* We used to set the address register to the first location in the text
12122 section here, but that didn't accomplish anything since we already
12123 have a line note for the opening brace of the first function. */
12125 /* Generate the line number to PC correspondence table, encoded as
12126 a series of state machine operations. */
12127 current_file = 1;
12128 current_line = 1;
12130 if (cfun && in_cold_section_p)
12131 strcpy (prev_line_label, crtl->subsections.cold_section_label);
12132 else
12133 strcpy (prev_line_label, text_section_label);
12134 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12136 dw_line_info_ref line_info = &line_info_table[lt_index];
12138 #if 0
12139 /* Disable this optimization for now; GDB wants to see two line notes
12140 at the beginning of a function so it can find the end of the
12141 prologue. */
12143 /* Don't emit anything for redundant notes. Just updating the
12144 address doesn't accomplish anything, because we already assume
12145 that anything after the last address is this line. */
12146 if (line_info->dw_line_num == current_line
12147 && line_info->dw_file_num == current_file)
12148 continue;
12149 #endif
12151 /* Emit debug info for the address of the current line.
12153 Unfortunately, we have little choice here currently, and must always
12154 use the most general form. GCC does not know the address delta
12155 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
12156 attributes which will give an upper bound on the address range. We
12157 could perhaps use length attributes to determine when it is safe to
12158 use DW_LNS_fixed_advance_pc. */
12160 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12161 if (0)
12163 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
12164 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12165 "DW_LNS_fixed_advance_pc");
12166 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12168 else
12170 /* This can handle any delta. This takes
12171 4+DWARF2_ADDR_SIZE bytes. */
12172 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12173 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12174 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12175 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12178 strcpy (prev_line_label, line_label);
12180 /* Emit debug info for the source file of the current line, if
12181 different from the previous line. */
12182 if (line_info->dw_file_num != current_file)
12184 current_file = line_info->dw_file_num;
12185 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12186 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12189 /* Emit debug info for the current line number, choosing the encoding
12190 that uses the least amount of space. */
12191 if (line_info->dw_line_num != current_line)
12193 line_offset = line_info->dw_line_num - current_line;
12194 line_delta = line_offset - DWARF_LINE_BASE;
12195 current_line = line_info->dw_line_num;
12196 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12197 /* This can handle deltas from -10 to 234, using the current
12198 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
12199 takes 1 byte. */
12200 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12201 "line %lu", current_line);
12202 else
12204 /* This can handle any delta. This takes at least 4 bytes,
12205 depending on the value being encoded. */
12206 dw2_asm_output_data (1, DW_LNS_advance_line,
12207 "advance to line %lu", current_line);
12208 dw2_asm_output_data_sleb128 (line_offset, NULL);
12209 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12212 else
12213 /* We still need to start a new row, so output a copy insn. */
12214 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12217 /* Emit debug info for the address of the end of the function. */
12218 if (0)
12220 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12221 "DW_LNS_fixed_advance_pc");
12222 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12224 else
12226 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12227 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12228 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12229 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12232 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12233 dw2_asm_output_data_uleb128 (1, NULL);
12234 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12236 function = 0;
12237 current_file = 1;
12238 current_line = 1;
12239 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12241 dw_separate_line_info_ref line_info
12242 = &separate_line_info_table[lt_index];
12244 #if 0
12245 /* Don't emit anything for redundant notes. */
12246 if (line_info->dw_line_num == current_line
12247 && line_info->dw_file_num == current_file
12248 && line_info->function == function)
12249 goto cont;
12250 #endif
12252 /* Emit debug info for the address of the current line. If this is
12253 a new function, or the first line of a function, then we need
12254 to handle it differently. */
12255 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12256 lt_index);
12257 if (function != line_info->function)
12259 function = line_info->function;
12261 /* Set the address register to the first line in the function. */
12262 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12263 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12264 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12265 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12267 else
12269 /* ??? See the DW_LNS_advance_pc comment above. */
12270 if (0)
12272 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12273 "DW_LNS_fixed_advance_pc");
12274 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12276 else
12278 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12279 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12280 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12281 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12285 strcpy (prev_line_label, line_label);
12287 /* Emit debug info for the source file of the current line, if
12288 different from the previous line. */
12289 if (line_info->dw_file_num != current_file)
12291 current_file = line_info->dw_file_num;
12292 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12293 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12296 /* Emit debug info for the current line number, choosing the encoding
12297 that uses the least amount of space. */
12298 if (line_info->dw_line_num != current_line)
12300 line_offset = line_info->dw_line_num - current_line;
12301 line_delta = line_offset - DWARF_LINE_BASE;
12302 current_line = line_info->dw_line_num;
12303 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12304 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12305 "line %lu", current_line);
12306 else
12308 dw2_asm_output_data (1, DW_LNS_advance_line,
12309 "advance to line %lu", current_line);
12310 dw2_asm_output_data_sleb128 (line_offset, NULL);
12311 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12314 else
12315 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12317 #if 0
12318 cont:
12319 #endif
12321 lt_index++;
12323 /* If we're done with a function, end its sequence. */
12324 if (lt_index == separate_line_info_table_in_use
12325 || separate_line_info_table[lt_index].function != function)
12327 current_file = 1;
12328 current_line = 1;
12330 /* Emit debug info for the address of the end of the function. */
12331 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12332 if (0)
12334 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12335 "DW_LNS_fixed_advance_pc");
12336 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12338 else
12340 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12341 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12342 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12343 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12346 /* Output the marker for the end of this sequence. */
12347 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12348 dw2_asm_output_data_uleb128 (1, NULL);
12349 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12353 /* Output the marker for the end of the line number info. */
12354 ASM_OUTPUT_LABEL (asm_out_file, l2);
12357 /* Return the size of the .debug_dcall table for the compilation unit. */
12359 static unsigned long
12360 size_of_dcall_table (void)
12362 unsigned long size;
12363 unsigned int i;
12364 dcall_entry *p;
12365 tree last_poc_decl = NULL;
12367 /* Header: version + debug info section pointer + pointer size. */
12368 size = 2 + DWARF_OFFSET_SIZE + 1;
12370 /* Each entry: code label + DIE offset. */
12371 FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12373 gcc_assert (p->targ_die != NULL);
12374 /* Insert a "from" entry when the point-of-call DIE offset changes. */
12375 if (p->poc_decl != last_poc_decl)
12377 dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12378 gcc_assert (poc_die);
12379 last_poc_decl = p->poc_decl;
12380 if (poc_die)
12381 size += (DWARF_OFFSET_SIZE
12382 + size_of_uleb128 (poc_die->die_offset));
12384 size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12387 return size;
12390 /* Output the direct call table used to disambiguate PC values when
12391 identical function have been merged. */
12393 static void
12394 output_dcall_table (void)
12396 unsigned i;
12397 unsigned long dcall_length = size_of_dcall_table ();
12398 dcall_entry *p;
12399 char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12400 tree last_poc_decl = NULL;
12402 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12403 dw2_asm_output_data (4, 0xffffffff,
12404 "Initial length escape value indicating 64-bit DWARF extension");
12405 dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12406 "Length of Direct Call Table");
12407 dw2_asm_output_data (2, 4, "Version number");
12408 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12409 debug_info_section,
12410 "Offset of Compilation Unit Info");
12411 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12413 FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12415 /* Insert a "from" entry when the point-of-call DIE offset changes. */
12416 if (p->poc_decl != last_poc_decl)
12418 dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12419 last_poc_decl = p->poc_decl;
12420 if (poc_die)
12422 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12423 dw2_asm_output_data_uleb128 (poc_die->die_offset,
12424 "Caller DIE offset");
12427 ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12428 dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12429 dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12430 "Callee DIE offset");
12434 /* Return the size of the .debug_vcall table for the compilation unit. */
12436 static unsigned long
12437 size_of_vcall_table (void)
12439 unsigned long size;
12440 unsigned int i;
12441 vcall_entry *p;
12443 /* Header: version + pointer size. */
12444 size = 2 + 1;
12446 /* Each entry: code label + vtable slot index. */
12447 FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12448 size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12450 return size;
12453 /* Output the virtual call table used to disambiguate PC values when
12454 identical function have been merged. */
12456 static void
12457 output_vcall_table (void)
12459 unsigned i;
12460 unsigned long vcall_length = size_of_vcall_table ();
12461 vcall_entry *p;
12462 char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12464 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12465 dw2_asm_output_data (4, 0xffffffff,
12466 "Initial length escape value indicating 64-bit DWARF extension");
12467 dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12468 "Length of Virtual Call Table");
12469 dw2_asm_output_data (2, 4, "Version number");
12470 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12472 FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12474 ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12475 dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12476 dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12480 /* Given a pointer to a tree node for some base type, return a pointer to
12481 a DIE that describes the given type.
12483 This routine must only be called for GCC type nodes that correspond to
12484 Dwarf base (fundamental) types. */
12486 static dw_die_ref
12487 base_type_die (tree type)
12489 dw_die_ref base_type_result;
12490 enum dwarf_type encoding;
12492 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12493 return 0;
12495 /* If this is a subtype that should not be emitted as a subrange type,
12496 use the base type. See subrange_type_for_debug_p. */
12497 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12498 type = TREE_TYPE (type);
12500 switch (TREE_CODE (type))
12502 case INTEGER_TYPE:
12503 if ((dwarf_version >= 4 || !dwarf_strict)
12504 && TYPE_NAME (type)
12505 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12506 && DECL_IS_BUILTIN (TYPE_NAME (type))
12507 && DECL_NAME (TYPE_NAME (type)))
12509 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12510 if (strcmp (name, "char16_t") == 0
12511 || strcmp (name, "char32_t") == 0)
12513 encoding = DW_ATE_UTF;
12514 break;
12517 if (TYPE_STRING_FLAG (type))
12519 if (TYPE_UNSIGNED (type))
12520 encoding = DW_ATE_unsigned_char;
12521 else
12522 encoding = DW_ATE_signed_char;
12524 else if (TYPE_UNSIGNED (type))
12525 encoding = DW_ATE_unsigned;
12526 else
12527 encoding = DW_ATE_signed;
12528 break;
12530 case REAL_TYPE:
12531 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12533 if (dwarf_version >= 3 || !dwarf_strict)
12534 encoding = DW_ATE_decimal_float;
12535 else
12536 encoding = DW_ATE_lo_user;
12538 else
12539 encoding = DW_ATE_float;
12540 break;
12542 case FIXED_POINT_TYPE:
12543 if (!(dwarf_version >= 3 || !dwarf_strict))
12544 encoding = DW_ATE_lo_user;
12545 else if (TYPE_UNSIGNED (type))
12546 encoding = DW_ATE_unsigned_fixed;
12547 else
12548 encoding = DW_ATE_signed_fixed;
12549 break;
12551 /* Dwarf2 doesn't know anything about complex ints, so use
12552 a user defined type for it. */
12553 case COMPLEX_TYPE:
12554 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12555 encoding = DW_ATE_complex_float;
12556 else
12557 encoding = DW_ATE_lo_user;
12558 break;
12560 case BOOLEAN_TYPE:
12561 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12562 encoding = DW_ATE_boolean;
12563 break;
12565 default:
12566 /* No other TREE_CODEs are Dwarf fundamental types. */
12567 gcc_unreachable ();
12570 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
12572 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12573 int_size_in_bytes (type));
12574 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12576 return base_type_result;
12579 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12580 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12582 static inline int
12583 is_base_type (tree type)
12585 switch (TREE_CODE (type))
12587 case ERROR_MARK:
12588 case VOID_TYPE:
12589 case INTEGER_TYPE:
12590 case REAL_TYPE:
12591 case FIXED_POINT_TYPE:
12592 case COMPLEX_TYPE:
12593 case BOOLEAN_TYPE:
12594 return 1;
12596 case ARRAY_TYPE:
12597 case RECORD_TYPE:
12598 case UNION_TYPE:
12599 case QUAL_UNION_TYPE:
12600 case ENUMERAL_TYPE:
12601 case FUNCTION_TYPE:
12602 case METHOD_TYPE:
12603 case POINTER_TYPE:
12604 case REFERENCE_TYPE:
12605 case OFFSET_TYPE:
12606 case LANG_TYPE:
12607 case VECTOR_TYPE:
12608 return 0;
12610 default:
12611 gcc_unreachable ();
12614 return 0;
12617 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12618 node, return the size in bits for the type if it is a constant, or else
12619 return the alignment for the type if the type's size is not constant, or
12620 else return BITS_PER_WORD if the type actually turns out to be an
12621 ERROR_MARK node. */
12623 static inline unsigned HOST_WIDE_INT
12624 simple_type_size_in_bits (const_tree type)
12626 if (TREE_CODE (type) == ERROR_MARK)
12627 return BITS_PER_WORD;
12628 else if (TYPE_SIZE (type) == NULL_TREE)
12629 return 0;
12630 else if (host_integerp (TYPE_SIZE (type), 1))
12631 return tree_low_cst (TYPE_SIZE (type), 1);
12632 else
12633 return TYPE_ALIGN (type);
12636 /* Similarly, but return a double_int instead of UHWI. */
12638 static inline double_int
12639 double_int_type_size_in_bits (const_tree type)
12641 if (TREE_CODE (type) == ERROR_MARK)
12642 return uhwi_to_double_int (BITS_PER_WORD);
12643 else if (TYPE_SIZE (type) == NULL_TREE)
12644 return double_int_zero;
12645 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12646 return tree_to_double_int (TYPE_SIZE (type));
12647 else
12648 return uhwi_to_double_int (TYPE_ALIGN (type));
12651 /* Given a pointer to a tree node for a subrange type, return a pointer
12652 to a DIE that describes the given type. */
12654 static dw_die_ref
12655 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12657 dw_die_ref subrange_die;
12658 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12660 if (context_die == NULL)
12661 context_die = comp_unit_die;
12663 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12665 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12667 /* The size of the subrange type and its base type do not match,
12668 so we need to generate a size attribute for the subrange type. */
12669 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12672 if (low)
12673 add_bound_info (subrange_die, DW_AT_lower_bound, low);
12674 if (high)
12675 add_bound_info (subrange_die, DW_AT_upper_bound, high);
12677 return subrange_die;
12680 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12681 entry that chains various modifiers in front of the given type. */
12683 static dw_die_ref
12684 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12685 dw_die_ref context_die)
12687 enum tree_code code = TREE_CODE (type);
12688 dw_die_ref mod_type_die;
12689 dw_die_ref sub_die = NULL;
12690 tree item_type = NULL;
12691 tree qualified_type;
12692 tree name, low, high;
12694 if (code == ERROR_MARK)
12695 return NULL;
12697 /* See if we already have the appropriately qualified variant of
12698 this type. */
12699 qualified_type
12700 = get_qualified_type (type,
12701 ((is_const_type ? TYPE_QUAL_CONST : 0)
12702 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12704 if (qualified_type == sizetype
12705 && TYPE_NAME (qualified_type)
12706 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12708 #ifdef ENABLE_CHECKING
12709 gcc_assert (TREE_CODE (TREE_TYPE (TYPE_NAME (qualified_type)))
12710 == INTEGER_TYPE
12711 && TYPE_PRECISION (TREE_TYPE (TYPE_NAME (qualified_type)))
12712 == TYPE_PRECISION (qualified_type)
12713 && TYPE_UNSIGNED (TREE_TYPE (TYPE_NAME (qualified_type)))
12714 == TYPE_UNSIGNED (qualified_type));
12715 #endif
12716 qualified_type = TREE_TYPE (TYPE_NAME (qualified_type));
12719 /* If we do, then we can just use its DIE, if it exists. */
12720 if (qualified_type)
12722 mod_type_die = lookup_type_die (qualified_type);
12723 if (mod_type_die)
12724 return mod_type_die;
12727 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12729 /* Handle C typedef types. */
12730 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12731 && !DECL_ARTIFICIAL (name))
12733 tree dtype = TREE_TYPE (name);
12735 if (qualified_type == dtype)
12737 /* For a named type, use the typedef. */
12738 gen_type_die (qualified_type, context_die);
12739 return lookup_type_die (qualified_type);
12741 else if (is_const_type < TYPE_READONLY (dtype)
12742 || is_volatile_type < TYPE_VOLATILE (dtype)
12743 || (is_const_type <= TYPE_READONLY (dtype)
12744 && is_volatile_type <= TYPE_VOLATILE (dtype)
12745 && DECL_ORIGINAL_TYPE (name) != type))
12746 /* cv-unqualified version of named type. Just use the unnamed
12747 type to which it refers. */
12748 return modified_type_die (DECL_ORIGINAL_TYPE (name),
12749 is_const_type, is_volatile_type,
12750 context_die);
12751 /* Else cv-qualified version of named type; fall through. */
12754 if (is_const_type)
12756 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
12757 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12759 else if (is_volatile_type)
12761 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
12762 sub_die = modified_type_die (type, 0, 0, context_die);
12764 else if (code == POINTER_TYPE)
12766 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
12767 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12768 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12769 item_type = TREE_TYPE (type);
12770 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12771 add_AT_unsigned (mod_type_die, DW_AT_address_class,
12772 TYPE_ADDR_SPACE (item_type));
12774 else if (code == REFERENCE_TYPE)
12776 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12777 mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die,
12778 type);
12779 else
12780 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
12781 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12782 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12783 item_type = TREE_TYPE (type);
12784 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12785 add_AT_unsigned (mod_type_die, DW_AT_address_class,
12786 TYPE_ADDR_SPACE (item_type));
12788 else if (code == INTEGER_TYPE
12789 && TREE_TYPE (type) != NULL_TREE
12790 && subrange_type_for_debug_p (type, &low, &high))
12792 mod_type_die = subrange_type_die (type, low, high, context_die);
12793 item_type = TREE_TYPE (type);
12795 else if (is_base_type (type))
12796 mod_type_die = base_type_die (type);
12797 else
12799 gen_type_die (type, context_die);
12801 /* We have to get the type_main_variant here (and pass that to the
12802 `lookup_type_die' routine) because the ..._TYPE node we have
12803 might simply be a *copy* of some original type node (where the
12804 copy was created to help us keep track of typedef names) and
12805 that copy might have a different TYPE_UID from the original
12806 ..._TYPE node. */
12807 if (TREE_CODE (type) != VECTOR_TYPE)
12808 return lookup_type_die (type_main_variant (type));
12809 else
12810 /* Vectors have the debugging information in the type,
12811 not the main variant. */
12812 return lookup_type_die (type);
12815 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12816 don't output a DW_TAG_typedef, since there isn't one in the
12817 user's program; just attach a DW_AT_name to the type.
12818 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12819 if the base type already has the same name. */
12820 if (name
12821 && ((TREE_CODE (name) != TYPE_DECL
12822 && (qualified_type == TYPE_MAIN_VARIANT (type)
12823 || (!is_const_type && !is_volatile_type)))
12824 || (TREE_CODE (name) == TYPE_DECL
12825 && TREE_TYPE (name) == qualified_type
12826 && DECL_NAME (name))))
12828 if (TREE_CODE (name) == TYPE_DECL)
12829 /* Could just call add_name_and_src_coords_attributes here,
12830 but since this is a builtin type it doesn't have any
12831 useful source coordinates anyway. */
12832 name = DECL_NAME (name);
12833 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12835 /* This probably indicates a bug. */
12836 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12837 add_name_attribute (mod_type_die, "__unknown__");
12839 if (qualified_type)
12840 equate_type_number_to_die (qualified_type, mod_type_die);
12842 if (item_type)
12843 /* We must do this after the equate_type_number_to_die call, in case
12844 this is a recursive type. This ensures that the modified_type_die
12845 recursion will terminate even if the type is recursive. Recursive
12846 types are possible in Ada. */
12847 sub_die = modified_type_die (item_type,
12848 TYPE_READONLY (item_type),
12849 TYPE_VOLATILE (item_type),
12850 context_die);
12852 if (sub_die != NULL)
12853 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12855 return mod_type_die;
12858 /* Generate DIEs for the generic parameters of T.
12859 T must be either a generic type or a generic function.
12860 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12862 static void
12863 gen_generic_params_dies (tree t)
12865 tree parms, args;
12866 int parms_num, i;
12867 dw_die_ref die = NULL;
12869 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12870 return;
12872 if (TYPE_P (t))
12873 die = lookup_type_die (t);
12874 else if (DECL_P (t))
12875 die = lookup_decl_die (t);
12877 gcc_assert (die);
12879 parms = lang_hooks.get_innermost_generic_parms (t);
12880 if (!parms)
12881 /* T has no generic parameter. It means T is neither a generic type
12882 or function. End of story. */
12883 return;
12885 parms_num = TREE_VEC_LENGTH (parms);
12886 args = lang_hooks.get_innermost_generic_args (t);
12887 for (i = 0; i < parms_num; i++)
12889 tree parm, arg, arg_pack_elems;
12891 parm = TREE_VEC_ELT (parms, i);
12892 arg = TREE_VEC_ELT (args, i);
12893 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12894 gcc_assert (parm && TREE_VALUE (parm) && arg);
12896 if (parm && TREE_VALUE (parm) && arg)
12898 /* If PARM represents a template parameter pack,
12899 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12900 by DW_TAG_template_*_parameter DIEs for the argument
12901 pack elements of ARG. Note that ARG would then be
12902 an argument pack. */
12903 if (arg_pack_elems)
12904 template_parameter_pack_die (TREE_VALUE (parm),
12905 arg_pack_elems,
12906 die);
12907 else
12908 generic_parameter_die (TREE_VALUE (parm), arg,
12909 true /* Emit DW_AT_name */, die);
12914 /* Create and return a DIE for PARM which should be
12915 the representation of a generic type parameter.
12916 For instance, in the C++ front end, PARM would be a template parameter.
12917 ARG is the argument to PARM.
12918 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12919 name of the PARM.
12920 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12921 as a child node. */
12923 static dw_die_ref
12924 generic_parameter_die (tree parm, tree arg,
12925 bool emit_name_p,
12926 dw_die_ref parent_die)
12928 dw_die_ref tmpl_die = NULL;
12929 const char *name = NULL;
12931 if (!parm || !DECL_NAME (parm) || !arg)
12932 return NULL;
12934 /* We support non-type generic parameters and arguments,
12935 type generic parameters and arguments, as well as
12936 generic generic parameters (a.k.a. template template parameters in C++)
12937 and arguments. */
12938 if (TREE_CODE (parm) == PARM_DECL)
12939 /* PARM is a nontype generic parameter */
12940 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12941 else if (TREE_CODE (parm) == TYPE_DECL)
12942 /* PARM is a type generic parameter. */
12943 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12944 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12945 /* PARM is a generic generic parameter.
12946 Its DIE is a GNU extension. It shall have a
12947 DW_AT_name attribute to represent the name of the template template
12948 parameter, and a DW_AT_GNU_template_name attribute to represent the
12949 name of the template template argument. */
12950 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12951 parent_die, parm);
12952 else
12953 gcc_unreachable ();
12955 if (tmpl_die)
12957 tree tmpl_type;
12959 /* If PARM is a generic parameter pack, it means we are
12960 emitting debug info for a template argument pack element.
12961 In other terms, ARG is a template argument pack element.
12962 In that case, we don't emit any DW_AT_name attribute for
12963 the die. */
12964 if (emit_name_p)
12966 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12967 gcc_assert (name);
12968 add_AT_string (tmpl_die, DW_AT_name, name);
12971 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12973 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12974 TMPL_DIE should have a child DW_AT_type attribute that is set
12975 to the type of the argument to PARM, which is ARG.
12976 If PARM is a type generic parameter, TMPL_DIE should have a
12977 child DW_AT_type that is set to ARG. */
12978 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12979 add_type_attribute (tmpl_die, tmpl_type, 0,
12980 TREE_THIS_VOLATILE (tmpl_type),
12981 parent_die);
12983 else
12985 /* So TMPL_DIE is a DIE representing a
12986 a generic generic template parameter, a.k.a template template
12987 parameter in C++ and arg is a template. */
12989 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12990 to the name of the argument. */
12991 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12992 if (name)
12993 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12996 if (TREE_CODE (parm) == PARM_DECL)
12997 /* So PARM is a non-type generic parameter.
12998 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12999 attribute of TMPL_DIE which value represents the value
13000 of ARG.
13001 We must be careful here:
13002 The value of ARG might reference some function decls.
13003 We might currently be emitting debug info for a generic
13004 type and types are emitted before function decls, we don't
13005 know if the function decls referenced by ARG will actually be
13006 emitted after cgraph computations.
13007 So must defer the generation of the DW_AT_const_value to
13008 after cgraph is ready. */
13009 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13012 return tmpl_die;
13015 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13016 PARM_PACK must be a template parameter pack. The returned DIE
13017 will be child DIE of PARENT_DIE. */
13019 static dw_die_ref
13020 template_parameter_pack_die (tree parm_pack,
13021 tree parm_pack_args,
13022 dw_die_ref parent_die)
13024 dw_die_ref die;
13025 int j;
13027 gcc_assert (parent_die && parm_pack);
13029 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13030 add_name_and_src_coords_attributes (die, parm_pack);
13031 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13032 generic_parameter_die (parm_pack,
13033 TREE_VEC_ELT (parm_pack_args, j),
13034 false /* Don't emit DW_AT_name */,
13035 die);
13036 return die;
13039 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13040 an enumerated type. */
13042 static inline int
13043 type_is_enum (const_tree type)
13045 return TREE_CODE (type) == ENUMERAL_TYPE;
13048 /* Return the DBX register number described by a given RTL node. */
13050 static unsigned int
13051 dbx_reg_number (const_rtx rtl)
13053 unsigned regno = REGNO (rtl);
13055 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13057 #ifdef LEAF_REG_REMAP
13058 if (current_function_uses_only_leaf_regs)
13060 int leaf_reg = LEAF_REG_REMAP (regno);
13061 if (leaf_reg != -1)
13062 regno = (unsigned) leaf_reg;
13064 #endif
13066 return DBX_REGISTER_NUMBER (regno);
13069 /* Optionally add a DW_OP_piece term to a location description expression.
13070 DW_OP_piece is only added if the location description expression already
13071 doesn't end with DW_OP_piece. */
13073 static void
13074 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13076 dw_loc_descr_ref loc;
13078 if (*list_head != NULL)
13080 /* Find the end of the chain. */
13081 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13084 if (loc->dw_loc_opc != DW_OP_piece)
13085 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13089 /* Return a location descriptor that designates a machine register or
13090 zero if there is none. */
13092 static dw_loc_descr_ref
13093 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13095 rtx regs;
13097 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13098 return 0;
13100 /* We only use "frame base" when we're sure we're talking about the
13101 post-prologue local stack frame. We do this by *not* running
13102 register elimination until this point, and recognizing the special
13103 argument pointer and soft frame pointer rtx's.
13104 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13105 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13106 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13108 dw_loc_descr_ref result = NULL;
13110 if (dwarf_version >= 4 || !dwarf_strict)
13112 result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13113 if (result)
13114 add_loc_descr (&result,
13115 new_loc_descr (DW_OP_stack_value, 0, 0));
13117 return result;
13120 regs = targetm.dwarf_register_span (rtl);
13122 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13123 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13124 else
13125 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13128 /* Return a location descriptor that designates a machine register for
13129 a given hard register number. */
13131 static dw_loc_descr_ref
13132 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13134 dw_loc_descr_ref reg_loc_descr;
13136 if (regno <= 31)
13137 reg_loc_descr
13138 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13139 else
13140 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13142 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13143 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13145 return reg_loc_descr;
13148 /* Given an RTL of a register, return a location descriptor that
13149 designates a value that spans more than one register. */
13151 static dw_loc_descr_ref
13152 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13153 enum var_init_status initialized)
13155 int nregs, size, i;
13156 unsigned reg;
13157 dw_loc_descr_ref loc_result = NULL;
13159 reg = REGNO (rtl);
13160 #ifdef LEAF_REG_REMAP
13161 if (current_function_uses_only_leaf_regs)
13163 int leaf_reg = LEAF_REG_REMAP (reg);
13164 if (leaf_reg != -1)
13165 reg = (unsigned) leaf_reg;
13167 #endif
13168 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13169 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13171 /* Simple, contiguous registers. */
13172 if (regs == NULL_RTX)
13174 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13176 loc_result = NULL;
13177 while (nregs--)
13179 dw_loc_descr_ref t;
13181 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13182 VAR_INIT_STATUS_INITIALIZED);
13183 add_loc_descr (&loc_result, t);
13184 add_loc_descr_op_piece (&loc_result, size);
13185 ++reg;
13187 return loc_result;
13190 /* Now onto stupid register sets in non contiguous locations. */
13192 gcc_assert (GET_CODE (regs) == PARALLEL);
13194 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13195 loc_result = NULL;
13197 for (i = 0; i < XVECLEN (regs, 0); ++i)
13199 dw_loc_descr_ref t;
13201 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13202 VAR_INIT_STATUS_INITIALIZED);
13203 add_loc_descr (&loc_result, t);
13204 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13205 add_loc_descr_op_piece (&loc_result, size);
13208 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13209 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13210 return loc_result;
13213 /* Return a location descriptor that designates a constant. */
13215 static dw_loc_descr_ref
13216 int_loc_descriptor (HOST_WIDE_INT i)
13218 enum dwarf_location_atom op;
13220 /* Pick the smallest representation of a constant, rather than just
13221 defaulting to the LEB encoding. */
13222 if (i >= 0)
13224 if (i <= 31)
13225 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13226 else if (i <= 0xff)
13227 op = DW_OP_const1u;
13228 else if (i <= 0xffff)
13229 op = DW_OP_const2u;
13230 else if (HOST_BITS_PER_WIDE_INT == 32
13231 || i <= 0xffffffff)
13232 op = DW_OP_const4u;
13233 else
13234 op = DW_OP_constu;
13236 else
13238 if (i >= -0x80)
13239 op = DW_OP_const1s;
13240 else if (i >= -0x8000)
13241 op = DW_OP_const2s;
13242 else if (HOST_BITS_PER_WIDE_INT == 32
13243 || i >= -0x80000000)
13244 op = DW_OP_const4s;
13245 else
13246 op = DW_OP_consts;
13249 return new_loc_descr (op, i, 0);
13252 /* Return loc description representing "address" of integer value.
13253 This can appear only as toplevel expression. */
13255 static dw_loc_descr_ref
13256 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13258 int litsize;
13259 dw_loc_descr_ref loc_result = NULL;
13261 if (!(dwarf_version >= 4 || !dwarf_strict))
13262 return NULL;
13264 if (i >= 0)
13266 if (i <= 31)
13267 litsize = 1;
13268 else if (i <= 0xff)
13269 litsize = 2;
13270 else if (i <= 0xffff)
13271 litsize = 3;
13272 else if (HOST_BITS_PER_WIDE_INT == 32
13273 || i <= 0xffffffff)
13274 litsize = 5;
13275 else
13276 litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13278 else
13280 if (i >= -0x80)
13281 litsize = 2;
13282 else if (i >= -0x8000)
13283 litsize = 3;
13284 else if (HOST_BITS_PER_WIDE_INT == 32
13285 || i >= -0x80000000)
13286 litsize = 5;
13287 else
13288 litsize = 1 + size_of_sleb128 (i);
13290 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13291 is more compact. For DW_OP_stack_value we need:
13292 litsize + 1 (DW_OP_stack_value)
13293 and for DW_OP_implicit_value:
13294 1 (DW_OP_implicit_value) + 1 (length) + size. */
13295 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13297 loc_result = int_loc_descriptor (i);
13298 add_loc_descr (&loc_result,
13299 new_loc_descr (DW_OP_stack_value, 0, 0));
13300 return loc_result;
13303 loc_result = new_loc_descr (DW_OP_implicit_value,
13304 size, 0);
13305 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13306 loc_result->dw_loc_oprnd2.v.val_int = i;
13307 return loc_result;
13310 /* Return a location descriptor that designates a base+offset location. */
13312 static dw_loc_descr_ref
13313 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13314 enum var_init_status initialized)
13316 unsigned int regno;
13317 dw_loc_descr_ref result;
13318 dw_fde_ref fde = current_fde ();
13320 /* We only use "frame base" when we're sure we're talking about the
13321 post-prologue local stack frame. We do this by *not* running
13322 register elimination until this point, and recognizing the special
13323 argument pointer and soft frame pointer rtx's. */
13324 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13326 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13328 if (elim != reg)
13330 if (GET_CODE (elim) == PLUS)
13332 offset += INTVAL (XEXP (elim, 1));
13333 elim = XEXP (elim, 0);
13335 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13336 && (elim == hard_frame_pointer_rtx
13337 || elim == stack_pointer_rtx))
13338 || elim == (frame_pointer_needed
13339 ? hard_frame_pointer_rtx
13340 : stack_pointer_rtx));
13342 /* If drap register is used to align stack, use frame
13343 pointer + offset to access stack variables. If stack
13344 is aligned without drap, use stack pointer + offset to
13345 access stack variables. */
13346 if (crtl->stack_realign_tried
13347 && reg == frame_pointer_rtx)
13349 int base_reg
13350 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13351 ? HARD_FRAME_POINTER_REGNUM
13352 : STACK_POINTER_REGNUM);
13353 return new_reg_loc_descr (base_reg, offset);
13356 offset += frame_pointer_fb_offset;
13357 return new_loc_descr (DW_OP_fbreg, offset, 0);
13360 else if (!optimize
13361 && fde
13362 && (fde->drap_reg == REGNO (reg)
13363 || fde->vdrap_reg == REGNO (reg)))
13365 /* Use cfa+offset to represent the location of arguments passed
13366 on the stack when drap is used to align stack.
13367 Only do this when not optimizing, for optimized code var-tracking
13368 is supposed to track where the arguments live and the register
13369 used as vdrap or drap in some spot might be used for something
13370 else in other part of the routine. */
13371 return new_loc_descr (DW_OP_fbreg, offset, 0);
13374 regno = dbx_reg_number (reg);
13375 if (regno <= 31)
13376 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13377 offset, 0);
13378 else
13379 result = new_loc_descr (DW_OP_bregx, regno, offset);
13381 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13382 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13384 return result;
13387 /* Return true if this RTL expression describes a base+offset calculation. */
13389 static inline int
13390 is_based_loc (const_rtx rtl)
13392 return (GET_CODE (rtl) == PLUS
13393 && ((REG_P (XEXP (rtl, 0))
13394 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13395 && CONST_INT_P (XEXP (rtl, 1)))));
13398 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13399 failed. */
13401 static dw_loc_descr_ref
13402 tls_mem_loc_descriptor (rtx mem)
13404 tree base;
13405 dw_loc_descr_ref loc_result;
13407 if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13408 return NULL;
13410 base = get_base_address (MEM_EXPR (mem));
13411 if (base == NULL
13412 || TREE_CODE (base) != VAR_DECL
13413 || !DECL_THREAD_LOCAL_P (base))
13414 return NULL;
13416 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13417 if (loc_result == NULL)
13418 return NULL;
13420 if (INTVAL (MEM_OFFSET (mem)))
13421 loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13423 return loc_result;
13426 /* Output debug info about reason why we failed to expand expression as dwarf
13427 expression. */
13429 static void
13430 expansion_failed (tree expr, rtx rtl, char const *reason)
13432 if (dump_file && (dump_flags & TDF_DETAILS))
13434 fprintf (dump_file, "Failed to expand as dwarf: ");
13435 if (expr)
13436 print_generic_expr (dump_file, expr, dump_flags);
13437 if (rtl)
13439 fprintf (dump_file, "\n");
13440 print_rtl (dump_file, rtl);
13442 fprintf (dump_file, "\nReason: %s\n", reason);
13446 /* Helper function for const_ok_for_output, called either directly
13447 or via for_each_rtx. */
13449 static int
13450 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13452 rtx rtl = *rtlp;
13454 if (GET_CODE (rtl) == UNSPEC)
13456 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13457 we can't express it in the debug info. */
13458 #ifdef ENABLE_CHECKING
13459 inform (current_function_decl
13460 ? DECL_SOURCE_LOCATION (current_function_decl)
13461 : UNKNOWN_LOCATION,
13462 "non-delegitimized UNSPEC %d found in variable location",
13463 XINT (rtl, 1));
13464 #endif
13465 expansion_failed (NULL_TREE, rtl,
13466 "UNSPEC hasn't been delegitimized.\n");
13467 return 1;
13470 if (GET_CODE (rtl) != SYMBOL_REF)
13471 return 0;
13473 if (CONSTANT_POOL_ADDRESS_P (rtl))
13475 bool marked;
13476 get_pool_constant_mark (rtl, &marked);
13477 /* If all references to this pool constant were optimized away,
13478 it was not output and thus we can't represent it. */
13479 if (!marked)
13481 expansion_failed (NULL_TREE, rtl,
13482 "Constant was removed from constant pool.\n");
13483 return 1;
13487 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13488 return 1;
13490 /* Avoid references to external symbols in debug info, on several targets
13491 the linker might even refuse to link when linking a shared library,
13492 and in many other cases the relocations for .debug_info/.debug_loc are
13493 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13494 to be defined within the same shared library or executable are fine. */
13495 if (SYMBOL_REF_EXTERNAL_P (rtl))
13497 tree decl = SYMBOL_REF_DECL (rtl);
13499 if (decl == NULL || !targetm.binds_local_p (decl))
13501 expansion_failed (NULL_TREE, rtl,
13502 "Symbol not defined in current TU.\n");
13503 return 1;
13507 return 0;
13510 /* Return true if constant RTL can be emitted in DW_OP_addr or
13511 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13512 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13514 static bool
13515 const_ok_for_output (rtx rtl)
13517 if (GET_CODE (rtl) == SYMBOL_REF)
13518 return const_ok_for_output_1 (&rtl, NULL) == 0;
13520 if (GET_CODE (rtl) == CONST)
13521 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13523 return true;
13526 /* The following routine converts the RTL for a variable or parameter
13527 (resident in memory) into an equivalent Dwarf representation of a
13528 mechanism for getting the address of that same variable onto the top of a
13529 hypothetical "address evaluation" stack.
13531 When creating memory location descriptors, we are effectively transforming
13532 the RTL for a memory-resident object into its Dwarf postfix expression
13533 equivalent. This routine recursively descends an RTL tree, turning
13534 it into Dwarf postfix code as it goes.
13536 MODE is the mode of the memory reference, needed to handle some
13537 autoincrement addressing modes.
13539 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13540 location list for RTL.
13542 Return 0 if we can't represent the location. */
13544 static dw_loc_descr_ref
13545 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13546 enum var_init_status initialized)
13548 dw_loc_descr_ref mem_loc_result = NULL;
13549 enum dwarf_location_atom op;
13550 dw_loc_descr_ref op0, op1;
13552 /* Note that for a dynamically sized array, the location we will generate a
13553 description of here will be the lowest numbered location which is
13554 actually within the array. That's *not* necessarily the same as the
13555 zeroth element of the array. */
13557 rtl = targetm.delegitimize_address (rtl);
13559 switch (GET_CODE (rtl))
13561 case POST_INC:
13562 case POST_DEC:
13563 case POST_MODIFY:
13564 return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13566 case SUBREG:
13567 /* The case of a subreg may arise when we have a local (register)
13568 variable or a formal (register) parameter which doesn't quite fill
13569 up an entire register. For now, just assume that it is
13570 legitimate to make the Dwarf info refer to the whole register which
13571 contains the given subreg. */
13572 if (!subreg_lowpart_p (rtl))
13573 break;
13574 rtl = SUBREG_REG (rtl);
13575 if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13576 break;
13577 if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13578 break;
13579 mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13580 break;
13582 case REG:
13583 /* Whenever a register number forms a part of the description of the
13584 method for calculating the (dynamic) address of a memory resident
13585 object, DWARF rules require the register number be referred to as
13586 a "base register". This distinction is not based in any way upon
13587 what category of register the hardware believes the given register
13588 belongs to. This is strictly DWARF terminology we're dealing with
13589 here. Note that in cases where the location of a memory-resident
13590 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13591 OP_CONST (0)) the actual DWARF location descriptor that we generate
13592 may just be OP_BASEREG (basereg). This may look deceptively like
13593 the object in question was allocated to a register (rather than in
13594 memory) so DWARF consumers need to be aware of the subtle
13595 distinction between OP_REG and OP_BASEREG. */
13596 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13597 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13598 else if (stack_realign_drap
13599 && crtl->drap_reg
13600 && crtl->args.internal_arg_pointer == rtl
13601 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13603 /* If RTL is internal_arg_pointer, which has been optimized
13604 out, use DRAP instead. */
13605 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13606 VAR_INIT_STATUS_INITIALIZED);
13608 break;
13610 case SIGN_EXTEND:
13611 case ZERO_EXTEND:
13612 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13613 VAR_INIT_STATUS_INITIALIZED);
13614 if (op0 == 0)
13615 break;
13616 else
13618 int shift = DWARF2_ADDR_SIZE
13619 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13620 shift *= BITS_PER_UNIT;
13621 if (GET_CODE (rtl) == SIGN_EXTEND)
13622 op = DW_OP_shra;
13623 else
13624 op = DW_OP_shr;
13625 mem_loc_result = op0;
13626 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13627 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13628 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13629 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13631 break;
13633 case MEM:
13634 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13635 VAR_INIT_STATUS_INITIALIZED);
13636 if (mem_loc_result == NULL)
13637 mem_loc_result = tls_mem_loc_descriptor (rtl);
13638 if (mem_loc_result != 0)
13640 if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13642 expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13643 return 0;
13645 else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13646 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13647 else
13648 add_loc_descr (&mem_loc_result,
13649 new_loc_descr (DW_OP_deref_size,
13650 GET_MODE_SIZE (GET_MODE (rtl)), 0));
13652 else
13654 rtx new_rtl = avoid_constant_pool_reference (rtl);
13655 if (new_rtl != rtl)
13656 return mem_loc_descriptor (new_rtl, mode, initialized);
13658 break;
13660 case LO_SUM:
13661 rtl = XEXP (rtl, 1);
13663 /* ... fall through ... */
13665 case LABEL_REF:
13666 /* Some ports can transform a symbol ref into a label ref, because
13667 the symbol ref is too far away and has to be dumped into a constant
13668 pool. */
13669 case CONST:
13670 case SYMBOL_REF:
13671 if (GET_CODE (rtl) == SYMBOL_REF
13672 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13674 dw_loc_descr_ref temp;
13676 /* If this is not defined, we have no way to emit the data. */
13677 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13678 break;
13680 /* We used to emit DW_OP_addr here, but that's wrong, since
13681 DW_OP_addr should be relocated by the debug info consumer,
13682 while DW_OP_GNU_push_tls_address operand should not. */
13683 temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13684 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13685 temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13686 temp->dw_loc_oprnd1.v.val_addr = rtl;
13687 temp->dtprel = true;
13689 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13690 add_loc_descr (&mem_loc_result, temp);
13692 break;
13695 if (!const_ok_for_output (rtl))
13696 break;
13698 symref:
13699 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13700 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13701 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13702 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13703 break;
13705 case CONCAT:
13706 case CONCATN:
13707 case VAR_LOCATION:
13708 case DEBUG_IMPLICIT_PTR:
13709 expansion_failed (NULL_TREE, rtl,
13710 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13711 return 0;
13713 case PRE_MODIFY:
13714 /* Extract the PLUS expression nested inside and fall into
13715 PLUS code below. */
13716 rtl = XEXP (rtl, 1);
13717 goto plus;
13719 case PRE_INC:
13720 case PRE_DEC:
13721 /* Turn these into a PLUS expression and fall into the PLUS code
13722 below. */
13723 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13724 GEN_INT (GET_CODE (rtl) == PRE_INC
13725 ? GET_MODE_UNIT_SIZE (mode)
13726 : -GET_MODE_UNIT_SIZE (mode)));
13728 /* ... fall through ... */
13730 case PLUS:
13731 plus:
13732 if (is_based_loc (rtl))
13733 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13734 INTVAL (XEXP (rtl, 1)),
13735 VAR_INIT_STATUS_INITIALIZED);
13736 else
13738 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13739 VAR_INIT_STATUS_INITIALIZED);
13740 if (mem_loc_result == 0)
13741 break;
13743 if (CONST_INT_P (XEXP (rtl, 1)))
13744 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13745 else
13747 dw_loc_descr_ref mem_loc_result2
13748 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13749 VAR_INIT_STATUS_INITIALIZED);
13750 if (mem_loc_result2 == 0)
13751 break;
13752 add_loc_descr (&mem_loc_result, mem_loc_result2);
13753 add_loc_descr (&mem_loc_result,
13754 new_loc_descr (DW_OP_plus, 0, 0));
13757 break;
13759 /* If a pseudo-reg is optimized away, it is possible for it to
13760 be replaced with a MEM containing a multiply or shift. */
13761 case MINUS:
13762 op = DW_OP_minus;
13763 goto do_binop;
13765 case MULT:
13766 op = DW_OP_mul;
13767 goto do_binop;
13769 case DIV:
13770 op = DW_OP_div;
13771 goto do_binop;
13773 case UMOD:
13774 op = DW_OP_mod;
13775 goto do_binop;
13777 case ASHIFT:
13778 op = DW_OP_shl;
13779 goto do_binop;
13781 case ASHIFTRT:
13782 op = DW_OP_shra;
13783 goto do_binop;
13785 case LSHIFTRT:
13786 op = DW_OP_shr;
13787 goto do_binop;
13789 case AND:
13790 op = DW_OP_and;
13791 goto do_binop;
13793 case IOR:
13794 op = DW_OP_or;
13795 goto do_binop;
13797 case XOR:
13798 op = DW_OP_xor;
13799 goto do_binop;
13801 do_binop:
13802 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13803 VAR_INIT_STATUS_INITIALIZED);
13804 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13805 VAR_INIT_STATUS_INITIALIZED);
13807 if (op0 == 0 || op1 == 0)
13808 break;
13810 mem_loc_result = op0;
13811 add_loc_descr (&mem_loc_result, op1);
13812 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13813 break;
13815 case MOD:
13816 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13817 VAR_INIT_STATUS_INITIALIZED);
13818 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13819 VAR_INIT_STATUS_INITIALIZED);
13821 if (op0 == 0 || op1 == 0)
13822 break;
13824 mem_loc_result = op0;
13825 add_loc_descr (&mem_loc_result, op1);
13826 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13827 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13828 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13829 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13830 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13831 break;
13833 case NOT:
13834 op = DW_OP_not;
13835 goto do_unop;
13837 case ABS:
13838 op = DW_OP_abs;
13839 goto do_unop;
13841 case NEG:
13842 op = DW_OP_neg;
13843 goto do_unop;
13845 do_unop:
13846 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13847 VAR_INIT_STATUS_INITIALIZED);
13849 if (op0 == 0)
13850 break;
13852 mem_loc_result = op0;
13853 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13854 break;
13856 case CONST_INT:
13857 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13858 break;
13860 case EQ:
13861 op = DW_OP_eq;
13862 goto do_scompare;
13864 case GE:
13865 op = DW_OP_ge;
13866 goto do_scompare;
13868 case GT:
13869 op = DW_OP_gt;
13870 goto do_scompare;
13872 case LE:
13873 op = DW_OP_le;
13874 goto do_scompare;
13876 case LT:
13877 op = DW_OP_lt;
13878 goto do_scompare;
13880 case NE:
13881 op = DW_OP_ne;
13882 goto do_scompare;
13884 do_scompare:
13885 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13886 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13887 break;
13888 else
13890 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13892 if (op_mode == VOIDmode)
13893 op_mode = GET_MODE (XEXP (rtl, 1));
13894 if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13895 break;
13897 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13898 VAR_INIT_STATUS_INITIALIZED);
13899 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13900 VAR_INIT_STATUS_INITIALIZED);
13902 if (op0 == 0 || op1 == 0)
13903 break;
13905 if (op_mode != VOIDmode
13906 && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13908 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
13909 shift *= BITS_PER_UNIT;
13910 /* For eq/ne, if the operands are known to be zero-extended,
13911 there is no need to do the fancy shifting up. */
13912 if (op == DW_OP_eq || op == DW_OP_ne)
13914 dw_loc_descr_ref last0, last1;
13915 for (last0 = op0;
13916 last0->dw_loc_next != NULL;
13917 last0 = last0->dw_loc_next)
13919 for (last1 = op1;
13920 last1->dw_loc_next != NULL;
13921 last1 = last1->dw_loc_next)
13923 /* deref_size zero extends, and for constants we can check
13924 whether they are zero extended or not. */
13925 if (((last0->dw_loc_opc == DW_OP_deref_size
13926 && last0->dw_loc_oprnd1.v.val_int
13927 <= GET_MODE_SIZE (op_mode))
13928 || (CONST_INT_P (XEXP (rtl, 0))
13929 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13930 == (INTVAL (XEXP (rtl, 0))
13931 & GET_MODE_MASK (op_mode))))
13932 && ((last1->dw_loc_opc == DW_OP_deref_size
13933 && last1->dw_loc_oprnd1.v.val_int
13934 <= GET_MODE_SIZE (op_mode))
13935 || (CONST_INT_P (XEXP (rtl, 1))
13936 && (unsigned HOST_WIDE_INT)
13937 INTVAL (XEXP (rtl, 1))
13938 == (INTVAL (XEXP (rtl, 1))
13939 & GET_MODE_MASK (op_mode)))))
13940 goto do_compare;
13942 add_loc_descr (&op0, int_loc_descriptor (shift));
13943 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13944 if (CONST_INT_P (XEXP (rtl, 1)))
13945 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13946 else
13948 add_loc_descr (&op1, int_loc_descriptor (shift));
13949 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13954 do_compare:
13955 mem_loc_result = op0;
13956 add_loc_descr (&mem_loc_result, op1);
13957 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13958 if (STORE_FLAG_VALUE != 1)
13960 add_loc_descr (&mem_loc_result,
13961 int_loc_descriptor (STORE_FLAG_VALUE));
13962 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13964 break;
13966 case GEU:
13967 op = DW_OP_ge;
13968 goto do_ucompare;
13970 case GTU:
13971 op = DW_OP_gt;
13972 goto do_ucompare;
13974 case LEU:
13975 op = DW_OP_le;
13976 goto do_ucompare;
13978 case LTU:
13979 op = DW_OP_lt;
13980 goto do_ucompare;
13982 do_ucompare:
13983 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13984 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13985 break;
13986 else
13988 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13990 if (op_mode == VOIDmode)
13991 op_mode = GET_MODE (XEXP (rtl, 1));
13992 if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13993 break;
13995 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13996 VAR_INIT_STATUS_INITIALIZED);
13997 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13998 VAR_INIT_STATUS_INITIALIZED);
14000 if (op0 == 0 || op1 == 0)
14001 break;
14003 if (op_mode != VOIDmode
14004 && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14006 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
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 if (CONST_INT_P (XEXP (rtl, 0)))
14017 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14018 /* deref_size zero extends, so no need to mask it again. */
14019 else if (last0->dw_loc_opc != DW_OP_deref_size
14020 || last0->dw_loc_oprnd1.v.val_int
14021 > GET_MODE_SIZE (op_mode))
14023 add_loc_descr (&op0, int_loc_descriptor (mask));
14024 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14026 if (CONST_INT_P (XEXP (rtl, 1)))
14027 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14028 /* deref_size zero extends, so no need to mask it again. */
14029 else if (last1->dw_loc_opc != DW_OP_deref_size
14030 || last1->dw_loc_oprnd1.v.val_int
14031 > GET_MODE_SIZE (op_mode))
14033 add_loc_descr (&op1, int_loc_descriptor (mask));
14034 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14037 else
14039 HOST_WIDE_INT bias = 1;
14040 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14041 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14042 if (CONST_INT_P (XEXP (rtl, 1)))
14043 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14044 + INTVAL (XEXP (rtl, 1)));
14045 else
14046 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14047 bias, 0));
14050 goto do_compare;
14052 case SMIN:
14053 case SMAX:
14054 case UMIN:
14055 case UMAX:
14056 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14057 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14058 || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14059 break;
14061 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14062 VAR_INIT_STATUS_INITIALIZED);
14063 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14064 VAR_INIT_STATUS_INITIALIZED);
14066 if (op0 == 0 || op1 == 0)
14067 break;
14069 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14070 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14071 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14072 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14074 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14076 HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14077 add_loc_descr (&op0, int_loc_descriptor (mask));
14078 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14079 add_loc_descr (&op1, int_loc_descriptor (mask));
14080 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14082 else
14084 HOST_WIDE_INT bias = 1;
14085 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14086 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14087 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14090 else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14092 int shift = DWARF2_ADDR_SIZE
14093 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14094 shift *= BITS_PER_UNIT;
14095 add_loc_descr (&op0, int_loc_descriptor (shift));
14096 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14097 add_loc_descr (&op1, int_loc_descriptor (shift));
14098 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14101 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14102 op = DW_OP_lt;
14103 else
14104 op = DW_OP_gt;
14105 mem_loc_result = op0;
14106 add_loc_descr (&mem_loc_result, op1);
14107 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14109 dw_loc_descr_ref bra_node, drop_node;
14111 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14112 add_loc_descr (&mem_loc_result, bra_node);
14113 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14114 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14115 add_loc_descr (&mem_loc_result, drop_node);
14116 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14117 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14119 break;
14121 case ZERO_EXTRACT:
14122 case SIGN_EXTRACT:
14123 if (CONST_INT_P (XEXP (rtl, 1))
14124 && CONST_INT_P (XEXP (rtl, 2))
14125 && ((unsigned) INTVAL (XEXP (rtl, 1))
14126 + (unsigned) INTVAL (XEXP (rtl, 2))
14127 <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14128 && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14129 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14131 int shift, size;
14132 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14133 VAR_INIT_STATUS_INITIALIZED);
14134 if (op0 == 0)
14135 break;
14136 if (GET_CODE (rtl) == SIGN_EXTRACT)
14137 op = DW_OP_shra;
14138 else
14139 op = DW_OP_shr;
14140 mem_loc_result = op0;
14141 size = INTVAL (XEXP (rtl, 1));
14142 shift = INTVAL (XEXP (rtl, 2));
14143 if (BITS_BIG_ENDIAN)
14144 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14145 - shift - size;
14146 if (shift + size != (int) DWARF2_ADDR_SIZE)
14148 add_loc_descr (&mem_loc_result,
14149 int_loc_descriptor (DWARF2_ADDR_SIZE
14150 - shift - size));
14151 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14153 if (size != (int) DWARF2_ADDR_SIZE)
14155 add_loc_descr (&mem_loc_result,
14156 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14157 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14160 break;
14162 case COMPARE:
14163 case IF_THEN_ELSE:
14164 case ROTATE:
14165 case ROTATERT:
14166 case TRUNCATE:
14167 /* In theory, we could implement the above. */
14168 /* DWARF cannot represent the unsigned compare operations
14169 natively. */
14170 case SS_MULT:
14171 case US_MULT:
14172 case SS_DIV:
14173 case US_DIV:
14174 case SS_PLUS:
14175 case US_PLUS:
14176 case SS_MINUS:
14177 case US_MINUS:
14178 case SS_NEG:
14179 case US_NEG:
14180 case SS_ABS:
14181 case SS_ASHIFT:
14182 case US_ASHIFT:
14183 case SS_TRUNCATE:
14184 case US_TRUNCATE:
14185 case UDIV:
14186 case UNORDERED:
14187 case ORDERED:
14188 case UNEQ:
14189 case UNGE:
14190 case UNGT:
14191 case UNLE:
14192 case UNLT:
14193 case LTGT:
14194 case FLOAT_EXTEND:
14195 case FLOAT_TRUNCATE:
14196 case FLOAT:
14197 case UNSIGNED_FLOAT:
14198 case FIX:
14199 case UNSIGNED_FIX:
14200 case FRACT_CONVERT:
14201 case UNSIGNED_FRACT_CONVERT:
14202 case SAT_FRACT:
14203 case UNSIGNED_SAT_FRACT:
14204 case SQRT:
14205 case BSWAP:
14206 case FFS:
14207 case CLZ:
14208 case CTZ:
14209 case POPCOUNT:
14210 case PARITY:
14211 case ASM_OPERANDS:
14212 case VEC_MERGE:
14213 case VEC_SELECT:
14214 case VEC_CONCAT:
14215 case VEC_DUPLICATE:
14216 case UNSPEC:
14217 case HIGH:
14218 /* If delegitimize_address couldn't do anything with the UNSPEC, we
14219 can't express it in the debug info. This can happen e.g. with some
14220 TLS UNSPECs. */
14221 break;
14223 case CONST_STRING:
14224 resolve_one_addr (&rtl, NULL);
14225 goto symref;
14227 default:
14228 #ifdef ENABLE_CHECKING
14229 print_rtl (stderr, rtl);
14230 gcc_unreachable ();
14231 #else
14232 break;
14233 #endif
14236 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14237 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14239 return mem_loc_result;
14242 /* Return a descriptor that describes the concatenation of two locations.
14243 This is typically a complex variable. */
14245 static dw_loc_descr_ref
14246 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14248 dw_loc_descr_ref cc_loc_result = NULL;
14249 dw_loc_descr_ref x0_ref
14250 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14251 dw_loc_descr_ref x1_ref
14252 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14254 if (x0_ref == 0 || x1_ref == 0)
14255 return 0;
14257 cc_loc_result = x0_ref;
14258 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14260 add_loc_descr (&cc_loc_result, x1_ref);
14261 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14263 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14264 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14266 return cc_loc_result;
14269 /* Return a descriptor that describes the concatenation of N
14270 locations. */
14272 static dw_loc_descr_ref
14273 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14275 unsigned int i;
14276 dw_loc_descr_ref cc_loc_result = NULL;
14277 unsigned int n = XVECLEN (concatn, 0);
14279 for (i = 0; i < n; ++i)
14281 dw_loc_descr_ref ref;
14282 rtx x = XVECEXP (concatn, 0, i);
14284 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14285 if (ref == NULL)
14286 return NULL;
14288 add_loc_descr (&cc_loc_result, ref);
14289 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14292 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14293 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14295 return cc_loc_result;
14298 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
14299 for DEBUG_IMPLICIT_PTR RTL. */
14301 static dw_loc_descr_ref
14302 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14304 dw_loc_descr_ref ret;
14305 dw_die_ref ref;
14307 if (dwarf_strict)
14308 return NULL;
14309 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14310 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14311 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14312 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14313 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14314 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14315 if (ref)
14317 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14318 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14319 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14321 else
14323 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14324 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14326 return ret;
14329 /* Output a proper Dwarf location descriptor for a variable or parameter
14330 which is either allocated in a register or in a memory location. For a
14331 register, we just generate an OP_REG and the register number. For a
14332 memory location we provide a Dwarf postfix expression describing how to
14333 generate the (dynamic) address of the object onto the address stack.
14335 MODE is mode of the decl if this loc_descriptor is going to be used in
14336 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14337 allowed, VOIDmode otherwise.
14339 If we don't know how to describe it, return 0. */
14341 static dw_loc_descr_ref
14342 loc_descriptor (rtx rtl, enum machine_mode mode,
14343 enum var_init_status initialized)
14345 dw_loc_descr_ref loc_result = NULL;
14347 switch (GET_CODE (rtl))
14349 case SUBREG:
14350 /* The case of a subreg may arise when we have a local (register)
14351 variable or a formal (register) parameter which doesn't quite fill
14352 up an entire register. For now, just assume that it is
14353 legitimate to make the Dwarf info refer to the whole register which
14354 contains the given subreg. */
14355 loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14356 break;
14358 case REG:
14359 loc_result = reg_loc_descriptor (rtl, initialized);
14360 break;
14362 case MEM:
14363 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14364 initialized);
14365 if (loc_result == NULL)
14366 loc_result = tls_mem_loc_descriptor (rtl);
14367 if (loc_result == NULL)
14369 rtx new_rtl = avoid_constant_pool_reference (rtl);
14370 if (new_rtl != rtl)
14371 loc_result = loc_descriptor (new_rtl, mode, initialized);
14373 break;
14375 case CONCAT:
14376 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14377 initialized);
14378 break;
14380 case CONCATN:
14381 loc_result = concatn_loc_descriptor (rtl, initialized);
14382 break;
14384 case VAR_LOCATION:
14385 /* Single part. */
14386 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14388 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14389 if (GET_CODE (loc) == EXPR_LIST)
14390 loc = XEXP (loc, 0);
14391 loc_result = loc_descriptor (loc, mode, initialized);
14392 break;
14395 rtl = XEXP (rtl, 1);
14396 /* FALLTHRU */
14398 case PARALLEL:
14400 rtvec par_elems = XVEC (rtl, 0);
14401 int num_elem = GET_NUM_ELEM (par_elems);
14402 enum machine_mode mode;
14403 int i;
14405 /* Create the first one, so we have something to add to. */
14406 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14407 VOIDmode, initialized);
14408 if (loc_result == NULL)
14409 return NULL;
14410 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14411 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14412 for (i = 1; i < num_elem; i++)
14414 dw_loc_descr_ref temp;
14416 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14417 VOIDmode, initialized);
14418 if (temp == NULL)
14419 return NULL;
14420 add_loc_descr (&loc_result, temp);
14421 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14422 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14425 break;
14427 case CONST_INT:
14428 if (mode != VOIDmode && mode != BLKmode)
14429 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14430 INTVAL (rtl));
14431 break;
14433 case CONST_DOUBLE:
14434 if (mode == VOIDmode)
14435 mode = GET_MODE (rtl);
14437 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14439 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14441 /* Note that a CONST_DOUBLE rtx could represent either an integer
14442 or a floating-point constant. A CONST_DOUBLE is used whenever
14443 the constant requires more than one word in order to be
14444 adequately represented. We output CONST_DOUBLEs as blocks. */
14445 loc_result = new_loc_descr (DW_OP_implicit_value,
14446 GET_MODE_SIZE (mode), 0);
14447 if (SCALAR_FLOAT_MODE_P (mode))
14449 unsigned int length = GET_MODE_SIZE (mode);
14450 unsigned char *array
14451 = (unsigned char*) ggc_alloc_atomic (length);
14453 insert_float (rtl, array);
14454 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14455 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14456 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14457 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14459 else
14461 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14462 loc_result->dw_loc_oprnd2.v.val_double
14463 = rtx_to_double_int (rtl);
14466 break;
14468 case CONST_VECTOR:
14469 if (mode == VOIDmode)
14470 mode = GET_MODE (rtl);
14472 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14474 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14475 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14476 unsigned char *array = (unsigned char *)
14477 ggc_alloc_atomic (length * elt_size);
14478 unsigned int i;
14479 unsigned char *p;
14481 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14482 switch (GET_MODE_CLASS (mode))
14484 case MODE_VECTOR_INT:
14485 for (i = 0, p = array; i < length; i++, p += elt_size)
14487 rtx elt = CONST_VECTOR_ELT (rtl, i);
14488 double_int val = rtx_to_double_int (elt);
14490 if (elt_size <= sizeof (HOST_WIDE_INT))
14491 insert_int (double_int_to_shwi (val), elt_size, p);
14492 else
14494 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14495 insert_double (val, p);
14498 break;
14500 case MODE_VECTOR_FLOAT:
14501 for (i = 0, p = array; i < length; i++, p += elt_size)
14503 rtx elt = CONST_VECTOR_ELT (rtl, i);
14504 insert_float (elt, p);
14506 break;
14508 default:
14509 gcc_unreachable ();
14512 loc_result = new_loc_descr (DW_OP_implicit_value,
14513 length * elt_size, 0);
14514 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14515 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14516 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14517 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14519 break;
14521 case CONST:
14522 if (mode == VOIDmode
14523 || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14524 || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14525 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14527 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14528 break;
14530 /* FALLTHROUGH */
14531 case SYMBOL_REF:
14532 if (!const_ok_for_output (rtl))
14533 break;
14534 case LABEL_REF:
14535 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14536 && (dwarf_version >= 4 || !dwarf_strict))
14538 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14539 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14540 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14541 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14542 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14544 break;
14546 case DEBUG_IMPLICIT_PTR:
14547 loc_result = implicit_ptr_descriptor (rtl, 0);
14548 break;
14550 case PLUS:
14551 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14552 && CONST_INT_P (XEXP (rtl, 1)))
14554 loc_result
14555 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14556 break;
14558 /* FALLTHRU */
14559 default:
14560 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14561 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14562 && (dwarf_version >= 4 || !dwarf_strict))
14564 /* Value expression. */
14565 loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14566 if (loc_result)
14567 add_loc_descr (&loc_result,
14568 new_loc_descr (DW_OP_stack_value, 0, 0));
14570 break;
14573 return loc_result;
14576 /* We need to figure out what section we should use as the base for the
14577 address ranges where a given location is valid.
14578 1. If this particular DECL has a section associated with it, use that.
14579 2. If this function has a section associated with it, use that.
14580 3. Otherwise, use the text section.
14581 XXX: If you split a variable across multiple sections, we won't notice. */
14583 static const char *
14584 secname_for_decl (const_tree decl)
14586 const char *secname;
14588 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14590 tree sectree = DECL_SECTION_NAME (decl);
14591 secname = TREE_STRING_POINTER (sectree);
14593 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14595 tree sectree = DECL_SECTION_NAME (current_function_decl);
14596 secname = TREE_STRING_POINTER (sectree);
14598 else if (cfun && in_cold_section_p)
14599 secname = crtl->subsections.cold_section_label;
14600 else
14601 secname = text_section_label;
14603 return secname;
14606 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
14608 static bool
14609 decl_by_reference_p (tree decl)
14611 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14612 || TREE_CODE (decl) == VAR_DECL)
14613 && DECL_BY_REFERENCE (decl));
14616 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14617 for VARLOC. */
14619 static dw_loc_descr_ref
14620 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14621 enum var_init_status initialized)
14623 int have_address = 0;
14624 dw_loc_descr_ref descr;
14625 enum machine_mode mode;
14627 if (want_address != 2)
14629 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14630 /* Single part. */
14631 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14633 varloc = PAT_VAR_LOCATION_LOC (varloc);
14634 if (GET_CODE (varloc) == EXPR_LIST)
14635 varloc = XEXP (varloc, 0);
14636 mode = GET_MODE (varloc);
14637 if (MEM_P (varloc))
14639 rtx addr = XEXP (varloc, 0);
14640 descr = mem_loc_descriptor (addr, mode, initialized);
14641 if (descr)
14642 have_address = 1;
14643 else
14645 rtx x = avoid_constant_pool_reference (varloc);
14646 if (x != varloc)
14647 descr = mem_loc_descriptor (x, mode, initialized);
14650 else
14651 descr = mem_loc_descriptor (varloc, mode, initialized);
14653 else
14654 return 0;
14656 else
14658 if (GET_CODE (varloc) == VAR_LOCATION)
14659 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14660 else
14661 mode = DECL_MODE (loc);
14662 descr = loc_descriptor (varloc, mode, initialized);
14663 have_address = 1;
14666 if (!descr)
14667 return 0;
14669 if (want_address == 2 && !have_address
14670 && (dwarf_version >= 4 || !dwarf_strict))
14672 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14674 expansion_failed (loc, NULL_RTX,
14675 "DWARF address size mismatch");
14676 return 0;
14678 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14679 have_address = 1;
14681 /* Show if we can't fill the request for an address. */
14682 if (want_address && !have_address)
14684 expansion_failed (loc, NULL_RTX,
14685 "Want address and only have value");
14686 return 0;
14689 /* If we've got an address and don't want one, dereference. */
14690 if (!want_address && have_address)
14692 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14693 enum dwarf_location_atom op;
14695 if (size > DWARF2_ADDR_SIZE || size == -1)
14697 expansion_failed (loc, NULL_RTX,
14698 "DWARF address size mismatch");
14699 return 0;
14701 else if (size == DWARF2_ADDR_SIZE)
14702 op = DW_OP_deref;
14703 else
14704 op = DW_OP_deref_size;
14706 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14709 return descr;
14712 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14713 if it is not possible. */
14715 static dw_loc_descr_ref
14716 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14718 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14719 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14720 else if (dwarf_version >= 3 || !dwarf_strict)
14721 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14722 else
14723 return NULL;
14726 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14727 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14729 static dw_loc_descr_ref
14730 dw_sra_loc_expr (tree decl, rtx loc)
14732 rtx p;
14733 unsigned int padsize = 0;
14734 dw_loc_descr_ref descr, *descr_tail;
14735 unsigned HOST_WIDE_INT decl_size;
14736 rtx varloc;
14737 enum var_init_status initialized;
14739 if (DECL_SIZE (decl) == NULL
14740 || !host_integerp (DECL_SIZE (decl), 1))
14741 return NULL;
14743 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14744 descr = NULL;
14745 descr_tail = &descr;
14747 for (p = loc; p; p = XEXP (p, 1))
14749 unsigned int bitsize = decl_piece_bitsize (p);
14750 rtx loc_note = *decl_piece_varloc_ptr (p);
14751 dw_loc_descr_ref cur_descr;
14752 dw_loc_descr_ref *tail, last = NULL;
14753 unsigned int opsize = 0;
14755 if (loc_note == NULL_RTX
14756 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14758 padsize += bitsize;
14759 continue;
14761 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14762 varloc = NOTE_VAR_LOCATION (loc_note);
14763 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14764 if (cur_descr == NULL)
14766 padsize += bitsize;
14767 continue;
14770 /* Check that cur_descr either doesn't use
14771 DW_OP_*piece operations, or their sum is equal
14772 to bitsize. Otherwise we can't embed it. */
14773 for (tail = &cur_descr; *tail != NULL;
14774 tail = &(*tail)->dw_loc_next)
14775 if ((*tail)->dw_loc_opc == DW_OP_piece)
14777 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14778 * BITS_PER_UNIT;
14779 last = *tail;
14781 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14783 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14784 last = *tail;
14787 if (last != NULL && opsize != bitsize)
14789 padsize += bitsize;
14790 continue;
14793 /* If there is a hole, add DW_OP_*piece after empty DWARF
14794 expression, which means that those bits are optimized out. */
14795 if (padsize)
14797 if (padsize > decl_size)
14798 return NULL;
14799 decl_size -= padsize;
14800 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14801 if (*descr_tail == NULL)
14802 return NULL;
14803 descr_tail = &(*descr_tail)->dw_loc_next;
14804 padsize = 0;
14806 *descr_tail = cur_descr;
14807 descr_tail = tail;
14808 if (bitsize > decl_size)
14809 return NULL;
14810 decl_size -= bitsize;
14811 if (last == NULL)
14813 HOST_WIDE_INT offset = 0;
14814 if (GET_CODE (varloc) == VAR_LOCATION
14815 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14817 varloc = PAT_VAR_LOCATION_LOC (varloc);
14818 if (GET_CODE (varloc) == EXPR_LIST)
14819 varloc = XEXP (varloc, 0);
14823 if (GET_CODE (varloc) == CONST
14824 || GET_CODE (varloc) == SIGN_EXTEND
14825 || GET_CODE (varloc) == ZERO_EXTEND)
14826 varloc = XEXP (varloc, 0);
14827 else if (GET_CODE (varloc) == SUBREG)
14828 varloc = SUBREG_REG (varloc);
14829 else
14830 break;
14832 while (1);
14833 /* DW_OP_bit_size offset should be zero for register
14834 or implicit location descriptions and empty location
14835 descriptions, but for memory addresses needs big endian
14836 adjustment. */
14837 if (MEM_P (varloc))
14839 unsigned HOST_WIDE_INT memsize
14840 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
14841 if (memsize != bitsize)
14843 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14844 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14845 return NULL;
14846 if (memsize < bitsize)
14847 return NULL;
14848 if (BITS_BIG_ENDIAN)
14849 offset = memsize - bitsize;
14853 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14854 if (*descr_tail == NULL)
14855 return NULL;
14856 descr_tail = &(*descr_tail)->dw_loc_next;
14860 /* If there were any non-empty expressions, add padding till the end of
14861 the decl. */
14862 if (descr != NULL && decl_size != 0)
14864 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14865 if (*descr_tail == NULL)
14866 return NULL;
14868 return descr;
14871 /* Return the dwarf representation of the location list LOC_LIST of
14872 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14873 function. */
14875 static dw_loc_list_ref
14876 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14878 const char *endname, *secname;
14879 rtx varloc;
14880 enum var_init_status initialized;
14881 struct var_loc_node *node;
14882 dw_loc_descr_ref descr;
14883 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14884 dw_loc_list_ref list = NULL;
14885 dw_loc_list_ref *listp = &list;
14887 /* Now that we know what section we are using for a base,
14888 actually construct the list of locations.
14889 The first location information is what is passed to the
14890 function that creates the location list, and the remaining
14891 locations just get added on to that list.
14892 Note that we only know the start address for a location
14893 (IE location changes), so to build the range, we use
14894 the range [current location start, next location start].
14895 This means we have to special case the last node, and generate
14896 a range of [last location start, end of function label]. */
14898 secname = secname_for_decl (decl);
14900 for (node = loc_list->first; node; node = node->next)
14901 if (GET_CODE (node->loc) == EXPR_LIST
14902 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14904 if (GET_CODE (node->loc) == EXPR_LIST)
14906 /* This requires DW_OP_{,bit_}piece, which is not usable
14907 inside DWARF expressions. */
14908 if (want_address != 2)
14909 continue;
14910 descr = dw_sra_loc_expr (decl, node->loc);
14911 if (descr == NULL)
14912 continue;
14914 else
14916 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14917 varloc = NOTE_VAR_LOCATION (node->loc);
14918 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14920 if (descr)
14922 /* The variable has a location between NODE->LABEL and
14923 NODE->NEXT->LABEL. */
14924 if (node->next)
14925 endname = node->next->label;
14926 /* If the variable has a location at the last label
14927 it keeps its location until the end of function. */
14928 else if (!current_function_decl)
14929 endname = text_end_label;
14930 else
14932 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14933 current_function_funcdef_no);
14934 endname = ggc_strdup (label_id);
14937 *listp = new_loc_list (descr, node->label, endname, secname);
14938 listp = &(*listp)->dw_loc_next;
14942 /* Try to avoid the overhead of a location list emitting a location
14943 expression instead, but only if we didn't have more than one
14944 location entry in the first place. If some entries were not
14945 representable, we don't want to pretend a single entry that was
14946 applies to the entire scope in which the variable is
14947 available. */
14948 if (list && loc_list->first->next)
14949 gen_llsym (list);
14951 return list;
14954 /* Return if the loc_list has only single element and thus can be represented
14955 as location description. */
14957 static bool
14958 single_element_loc_list_p (dw_loc_list_ref list)
14960 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14961 return !list->ll_symbol;
14964 /* To each location in list LIST add loc descr REF. */
14966 static void
14967 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14969 dw_loc_descr_ref copy;
14970 add_loc_descr (&list->expr, ref);
14971 list = list->dw_loc_next;
14972 while (list)
14974 copy = ggc_alloc_dw_loc_descr_node ();
14975 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14976 add_loc_descr (&list->expr, copy);
14977 while (copy->dw_loc_next)
14979 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
14980 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14981 copy->dw_loc_next = new_copy;
14982 copy = new_copy;
14984 list = list->dw_loc_next;
14988 /* Given two lists RET and LIST
14989 produce location list that is result of adding expression in LIST
14990 to expression in RET on each possition in program.
14991 Might be destructive on both RET and LIST.
14993 TODO: We handle only simple cases of RET or LIST having at most one
14994 element. General case would inolve sorting the lists in program order
14995 and merging them that will need some additional work.
14996 Adding that will improve quality of debug info especially for SRA-ed
14997 structures. */
14999 static void
15000 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15002 if (!list)
15003 return;
15004 if (!*ret)
15006 *ret = list;
15007 return;
15009 if (!list->dw_loc_next)
15011 add_loc_descr_to_each (*ret, list->expr);
15012 return;
15014 if (!(*ret)->dw_loc_next)
15016 add_loc_descr_to_each (list, (*ret)->expr);
15017 *ret = list;
15018 return;
15020 expansion_failed (NULL_TREE, NULL_RTX,
15021 "Don't know how to merge two non-trivial"
15022 " location lists.\n");
15023 *ret = NULL;
15024 return;
15027 /* LOC is constant expression. Try a luck, look it up in constant
15028 pool and return its loc_descr of its address. */
15030 static dw_loc_descr_ref
15031 cst_pool_loc_descr (tree loc)
15033 /* Get an RTL for this, if something has been emitted. */
15034 rtx rtl = lookup_constant_def (loc);
15035 enum machine_mode mode;
15037 if (!rtl || !MEM_P (rtl))
15039 gcc_assert (!rtl);
15040 return 0;
15042 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15044 /* TODO: We might get more coverage if we was actually delaying expansion
15045 of all expressions till end of compilation when constant pools are fully
15046 populated. */
15047 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15049 expansion_failed (loc, NULL_RTX,
15050 "CST value in contant pool but not marked.");
15051 return 0;
15053 mode = GET_MODE (rtl);
15054 rtl = XEXP (rtl, 0);
15055 return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15058 /* Return dw_loc_list representing address of addr_expr LOC
15059 by looking for innder INDIRECT_REF expression and turing it
15060 into simple arithmetics. */
15062 static dw_loc_list_ref
15063 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15065 tree obj, offset;
15066 HOST_WIDE_INT bitsize, bitpos, bytepos;
15067 enum machine_mode mode;
15068 int volatilep;
15069 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15070 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15072 obj = get_inner_reference (TREE_OPERAND (loc, 0),
15073 &bitsize, &bitpos, &offset, &mode,
15074 &unsignedp, &volatilep, false);
15075 STRIP_NOPS (obj);
15076 if (bitpos % BITS_PER_UNIT)
15078 expansion_failed (loc, NULL_RTX, "bitfield access");
15079 return 0;
15081 if (!INDIRECT_REF_P (obj))
15083 expansion_failed (obj,
15084 NULL_RTX, "no indirect ref in inner refrence");
15085 return 0;
15087 if (!offset && !bitpos)
15088 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15089 else if (toplev
15090 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15091 && (dwarf_version >= 4 || !dwarf_strict))
15093 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15094 if (!list_ret)
15095 return 0;
15096 if (offset)
15098 /* Variable offset. */
15099 list_ret1 = loc_list_from_tree (offset, 0);
15100 if (list_ret1 == 0)
15101 return 0;
15102 add_loc_list (&list_ret, list_ret1);
15103 if (!list_ret)
15104 return 0;
15105 add_loc_descr_to_each (list_ret,
15106 new_loc_descr (DW_OP_plus, 0, 0));
15108 bytepos = bitpos / BITS_PER_UNIT;
15109 if (bytepos > 0)
15110 add_loc_descr_to_each (list_ret,
15111 new_loc_descr (DW_OP_plus_uconst,
15112 bytepos, 0));
15113 else if (bytepos < 0)
15114 loc_list_plus_const (list_ret, bytepos);
15115 add_loc_descr_to_each (list_ret,
15116 new_loc_descr (DW_OP_stack_value, 0, 0));
15118 return list_ret;
15122 /* Generate Dwarf location list representing LOC.
15123 If WANT_ADDRESS is false, expression computing LOC will be computed
15124 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15125 if WANT_ADDRESS is 2, expression computing address useable in location
15126 will be returned (i.e. DW_OP_reg can be used
15127 to refer to register values). */
15129 static dw_loc_list_ref
15130 loc_list_from_tree (tree loc, int want_address)
15132 dw_loc_descr_ref ret = NULL, ret1 = NULL;
15133 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15134 int have_address = 0;
15135 enum dwarf_location_atom op;
15137 /* ??? Most of the time we do not take proper care for sign/zero
15138 extending the values properly. Hopefully this won't be a real
15139 problem... */
15141 switch (TREE_CODE (loc))
15143 case ERROR_MARK:
15144 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15145 return 0;
15147 case PLACEHOLDER_EXPR:
15148 /* This case involves extracting fields from an object to determine the
15149 position of other fields. We don't try to encode this here. The
15150 only user of this is Ada, which encodes the needed information using
15151 the names of types. */
15152 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15153 return 0;
15155 case CALL_EXPR:
15156 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15157 /* There are no opcodes for these operations. */
15158 return 0;
15160 case PREINCREMENT_EXPR:
15161 case PREDECREMENT_EXPR:
15162 case POSTINCREMENT_EXPR:
15163 case POSTDECREMENT_EXPR:
15164 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15165 /* There are no opcodes for these operations. */
15166 return 0;
15168 case ADDR_EXPR:
15169 /* If we already want an address, see if there is INDIRECT_REF inside
15170 e.g. for &this->field. */
15171 if (want_address)
15173 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15174 (loc, want_address == 2);
15175 if (list_ret)
15176 have_address = 1;
15177 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15178 && (ret = cst_pool_loc_descr (loc)))
15179 have_address = 1;
15181 /* Otherwise, process the argument and look for the address. */
15182 if (!list_ret && !ret)
15183 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15184 else
15186 if (want_address)
15187 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15188 return NULL;
15190 break;
15192 case VAR_DECL:
15193 if (DECL_THREAD_LOCAL_P (loc))
15195 rtx rtl;
15196 enum dwarf_location_atom first_op;
15197 enum dwarf_location_atom second_op;
15198 bool dtprel = false;
15200 if (targetm.have_tls)
15202 /* If this is not defined, we have no way to emit the
15203 data. */
15204 if (!targetm.asm_out.output_dwarf_dtprel)
15205 return 0;
15207 /* The way DW_OP_GNU_push_tls_address is specified, we
15208 can only look up addresses of objects in the current
15209 module. We used DW_OP_addr as first op, but that's
15210 wrong, because DW_OP_addr is relocated by the debug
15211 info consumer, while DW_OP_GNU_push_tls_address
15212 operand shouldn't be. */
15213 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15214 return 0;
15215 first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15216 dtprel = true;
15217 second_op = DW_OP_GNU_push_tls_address;
15219 else
15221 if (!targetm.emutls.debug_form_tls_address
15222 || !(dwarf_version >= 3 || !dwarf_strict))
15223 return 0;
15224 /* We stuffed the control variable into the DECL_VALUE_EXPR
15225 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15226 no longer appear in gimple code. We used the control
15227 variable in specific so that we could pick it up here. */
15228 loc = DECL_VALUE_EXPR (loc);
15229 first_op = DW_OP_addr;
15230 second_op = DW_OP_form_tls_address;
15233 rtl = rtl_for_decl_location (loc);
15234 if (rtl == NULL_RTX)
15235 return 0;
15237 if (!MEM_P (rtl))
15238 return 0;
15239 rtl = XEXP (rtl, 0);
15240 if (! CONSTANT_P (rtl))
15241 return 0;
15243 ret = new_loc_descr (first_op, 0, 0);
15244 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15245 ret->dw_loc_oprnd1.v.val_addr = rtl;
15246 ret->dtprel = dtprel;
15248 ret1 = new_loc_descr (second_op, 0, 0);
15249 add_loc_descr (&ret, ret1);
15251 have_address = 1;
15252 break;
15254 /* FALLTHRU */
15256 case PARM_DECL:
15257 if (DECL_HAS_VALUE_EXPR_P (loc))
15258 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15259 want_address);
15260 /* FALLTHRU */
15262 case RESULT_DECL:
15263 case FUNCTION_DECL:
15265 rtx rtl;
15266 var_loc_list *loc_list = lookup_decl_loc (loc);
15268 if (loc_list && loc_list->first)
15270 list_ret = dw_loc_list (loc_list, loc, want_address);
15271 have_address = want_address != 0;
15272 break;
15274 rtl = rtl_for_decl_location (loc);
15275 if (rtl == NULL_RTX)
15277 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15278 return 0;
15280 else if (CONST_INT_P (rtl))
15282 HOST_WIDE_INT val = INTVAL (rtl);
15283 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15284 val &= GET_MODE_MASK (DECL_MODE (loc));
15285 ret = int_loc_descriptor (val);
15287 else if (GET_CODE (rtl) == CONST_STRING)
15289 expansion_failed (loc, NULL_RTX, "CONST_STRING");
15290 return 0;
15292 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15294 ret = new_loc_descr (DW_OP_addr, 0, 0);
15295 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15296 ret->dw_loc_oprnd1.v.val_addr = rtl;
15298 else
15300 enum machine_mode mode;
15302 /* Certain constructs can only be represented at top-level. */
15303 if (want_address == 2)
15305 ret = loc_descriptor (rtl, VOIDmode,
15306 VAR_INIT_STATUS_INITIALIZED);
15307 have_address = 1;
15309 else
15311 mode = GET_MODE (rtl);
15312 if (MEM_P (rtl))
15314 rtl = XEXP (rtl, 0);
15315 have_address = 1;
15317 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15319 if (!ret)
15320 expansion_failed (loc, rtl,
15321 "failed to produce loc descriptor for rtl");
15324 break;
15326 case MEM_REF:
15327 /* ??? FIXME. */
15328 if (!integer_zerop (TREE_OPERAND (loc, 1)))
15329 return 0;
15330 /* Fallthru. */
15331 case INDIRECT_REF:
15332 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15333 have_address = 1;
15334 break;
15336 case COMPOUND_EXPR:
15337 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15339 CASE_CONVERT:
15340 case VIEW_CONVERT_EXPR:
15341 case SAVE_EXPR:
15342 case MODIFY_EXPR:
15343 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15345 case COMPONENT_REF:
15346 case BIT_FIELD_REF:
15347 case ARRAY_REF:
15348 case ARRAY_RANGE_REF:
15349 case REALPART_EXPR:
15350 case IMAGPART_EXPR:
15352 tree obj, offset;
15353 HOST_WIDE_INT bitsize, bitpos, bytepos;
15354 enum machine_mode mode;
15355 int volatilep;
15356 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15358 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15359 &unsignedp, &volatilep, false);
15361 gcc_assert (obj != loc);
15363 list_ret = loc_list_from_tree (obj,
15364 want_address == 2
15365 && !bitpos && !offset ? 2 : 1);
15366 /* TODO: We can extract value of the small expression via shifting even
15367 for nonzero bitpos. */
15368 if (list_ret == 0)
15369 return 0;
15370 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15372 expansion_failed (loc, NULL_RTX,
15373 "bitfield access");
15374 return 0;
15377 if (offset != NULL_TREE)
15379 /* Variable offset. */
15380 list_ret1 = loc_list_from_tree (offset, 0);
15381 if (list_ret1 == 0)
15382 return 0;
15383 add_loc_list (&list_ret, list_ret1);
15384 if (!list_ret)
15385 return 0;
15386 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15389 bytepos = bitpos / BITS_PER_UNIT;
15390 if (bytepos > 0)
15391 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15392 else if (bytepos < 0)
15393 loc_list_plus_const (list_ret, bytepos);
15395 have_address = 1;
15396 break;
15399 case INTEGER_CST:
15400 if ((want_address || !host_integerp (loc, 0))
15401 && (ret = cst_pool_loc_descr (loc)))
15402 have_address = 1;
15403 else if (want_address == 2
15404 && host_integerp (loc, 0)
15405 && (ret = address_of_int_loc_descriptor
15406 (int_size_in_bytes (TREE_TYPE (loc)),
15407 tree_low_cst (loc, 0))))
15408 have_address = 1;
15409 else if (host_integerp (loc, 0))
15410 ret = int_loc_descriptor (tree_low_cst (loc, 0));
15411 else
15413 expansion_failed (loc, NULL_RTX,
15414 "Integer operand is not host integer");
15415 return 0;
15417 break;
15419 case CONSTRUCTOR:
15420 case REAL_CST:
15421 case STRING_CST:
15422 case COMPLEX_CST:
15423 if ((ret = cst_pool_loc_descr (loc)))
15424 have_address = 1;
15425 else
15426 /* We can construct small constants here using int_loc_descriptor. */
15427 expansion_failed (loc, NULL_RTX,
15428 "constructor or constant not in constant pool");
15429 break;
15431 case TRUTH_AND_EXPR:
15432 case TRUTH_ANDIF_EXPR:
15433 case BIT_AND_EXPR:
15434 op = DW_OP_and;
15435 goto do_binop;
15437 case TRUTH_XOR_EXPR:
15438 case BIT_XOR_EXPR:
15439 op = DW_OP_xor;
15440 goto do_binop;
15442 case TRUTH_OR_EXPR:
15443 case TRUTH_ORIF_EXPR:
15444 case BIT_IOR_EXPR:
15445 op = DW_OP_or;
15446 goto do_binop;
15448 case FLOOR_DIV_EXPR:
15449 case CEIL_DIV_EXPR:
15450 case ROUND_DIV_EXPR:
15451 case TRUNC_DIV_EXPR:
15452 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15453 return 0;
15454 op = DW_OP_div;
15455 goto do_binop;
15457 case MINUS_EXPR:
15458 op = DW_OP_minus;
15459 goto do_binop;
15461 case FLOOR_MOD_EXPR:
15462 case CEIL_MOD_EXPR:
15463 case ROUND_MOD_EXPR:
15464 case TRUNC_MOD_EXPR:
15465 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15467 op = DW_OP_mod;
15468 goto do_binop;
15470 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15471 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15472 if (list_ret == 0 || list_ret1 == 0)
15473 return 0;
15475 add_loc_list (&list_ret, list_ret1);
15476 if (list_ret == 0)
15477 return 0;
15478 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15479 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15480 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15481 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15482 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15483 break;
15485 case MULT_EXPR:
15486 op = DW_OP_mul;
15487 goto do_binop;
15489 case LSHIFT_EXPR:
15490 op = DW_OP_shl;
15491 goto do_binop;
15493 case RSHIFT_EXPR:
15494 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15495 goto do_binop;
15497 case POINTER_PLUS_EXPR:
15498 case PLUS_EXPR:
15499 if (host_integerp (TREE_OPERAND (loc, 1), 0))
15501 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15502 if (list_ret == 0)
15503 return 0;
15505 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15506 break;
15509 op = DW_OP_plus;
15510 goto do_binop;
15512 case LE_EXPR:
15513 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15514 return 0;
15516 op = DW_OP_le;
15517 goto do_binop;
15519 case GE_EXPR:
15520 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15521 return 0;
15523 op = DW_OP_ge;
15524 goto do_binop;
15526 case LT_EXPR:
15527 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15528 return 0;
15530 op = DW_OP_lt;
15531 goto do_binop;
15533 case GT_EXPR:
15534 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15535 return 0;
15537 op = DW_OP_gt;
15538 goto do_binop;
15540 case EQ_EXPR:
15541 op = DW_OP_eq;
15542 goto do_binop;
15544 case NE_EXPR:
15545 op = DW_OP_ne;
15546 goto do_binop;
15548 do_binop:
15549 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15550 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15551 if (list_ret == 0 || list_ret1 == 0)
15552 return 0;
15554 add_loc_list (&list_ret, list_ret1);
15555 if (list_ret == 0)
15556 return 0;
15557 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15558 break;
15560 case TRUTH_NOT_EXPR:
15561 case BIT_NOT_EXPR:
15562 op = DW_OP_not;
15563 goto do_unop;
15565 case ABS_EXPR:
15566 op = DW_OP_abs;
15567 goto do_unop;
15569 case NEGATE_EXPR:
15570 op = DW_OP_neg;
15571 goto do_unop;
15573 do_unop:
15574 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15575 if (list_ret == 0)
15576 return 0;
15578 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15579 break;
15581 case MIN_EXPR:
15582 case MAX_EXPR:
15584 const enum tree_code code =
15585 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15587 loc = build3 (COND_EXPR, TREE_TYPE (loc),
15588 build2 (code, integer_type_node,
15589 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15590 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15593 /* ... fall through ... */
15595 case COND_EXPR:
15597 dw_loc_descr_ref lhs
15598 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15599 dw_loc_list_ref rhs
15600 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15601 dw_loc_descr_ref bra_node, jump_node, tmp;
15603 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15604 if (list_ret == 0 || lhs == 0 || rhs == 0)
15605 return 0;
15607 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15608 add_loc_descr_to_each (list_ret, bra_node);
15610 add_loc_list (&list_ret, rhs);
15611 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15612 add_loc_descr_to_each (list_ret, jump_node);
15614 add_loc_descr_to_each (list_ret, lhs);
15615 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15616 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15618 /* ??? Need a node to point the skip at. Use a nop. */
15619 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15620 add_loc_descr_to_each (list_ret, tmp);
15621 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15622 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15624 break;
15626 case FIX_TRUNC_EXPR:
15627 return 0;
15629 default:
15630 /* Leave front-end specific codes as simply unknown. This comes
15631 up, for instance, with the C STMT_EXPR. */
15632 if ((unsigned int) TREE_CODE (loc)
15633 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15635 expansion_failed (loc, NULL_RTX,
15636 "language specific tree node");
15637 return 0;
15640 #ifdef ENABLE_CHECKING
15641 /* Otherwise this is a generic code; we should just lists all of
15642 these explicitly. We forgot one. */
15643 gcc_unreachable ();
15644 #else
15645 /* In a release build, we want to degrade gracefully: better to
15646 generate incomplete debugging information than to crash. */
15647 return NULL;
15648 #endif
15651 if (!ret && !list_ret)
15652 return 0;
15654 if (want_address == 2 && !have_address
15655 && (dwarf_version >= 4 || !dwarf_strict))
15657 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15659 expansion_failed (loc, NULL_RTX,
15660 "DWARF address size mismatch");
15661 return 0;
15663 if (ret)
15664 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15665 else
15666 add_loc_descr_to_each (list_ret,
15667 new_loc_descr (DW_OP_stack_value, 0, 0));
15668 have_address = 1;
15670 /* Show if we can't fill the request for an address. */
15671 if (want_address && !have_address)
15673 expansion_failed (loc, NULL_RTX,
15674 "Want address and only have value");
15675 return 0;
15678 gcc_assert (!ret || !list_ret);
15680 /* If we've got an address and don't want one, dereference. */
15681 if (!want_address && have_address)
15683 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15685 if (size > DWARF2_ADDR_SIZE || size == -1)
15687 expansion_failed (loc, NULL_RTX,
15688 "DWARF address size mismatch");
15689 return 0;
15691 else if (size == DWARF2_ADDR_SIZE)
15692 op = DW_OP_deref;
15693 else
15694 op = DW_OP_deref_size;
15696 if (ret)
15697 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15698 else
15699 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15701 if (ret)
15702 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15704 return list_ret;
15707 /* Same as above but return only single location expression. */
15708 static dw_loc_descr_ref
15709 loc_descriptor_from_tree (tree loc, int want_address)
15711 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15712 if (!ret)
15713 return NULL;
15714 if (ret->dw_loc_next)
15716 expansion_failed (loc, NULL_RTX,
15717 "Location list where only loc descriptor needed");
15718 return NULL;
15720 return ret->expr;
15723 /* Given a value, round it up to the lowest multiple of `boundary'
15724 which is not less than the value itself. */
15726 static inline HOST_WIDE_INT
15727 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15729 return (((value + boundary - 1) / boundary) * boundary);
15732 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15733 pointer to the declared type for the relevant field variable, or return
15734 `integer_type_node' if the given node turns out to be an
15735 ERROR_MARK node. */
15737 static inline tree
15738 field_type (const_tree decl)
15740 tree type;
15742 if (TREE_CODE (decl) == ERROR_MARK)
15743 return integer_type_node;
15745 type = DECL_BIT_FIELD_TYPE (decl);
15746 if (type == NULL_TREE)
15747 type = TREE_TYPE (decl);
15749 return type;
15752 /* Given a pointer to a tree node, return the alignment in bits for
15753 it, or else return BITS_PER_WORD if the node actually turns out to
15754 be an ERROR_MARK node. */
15756 static inline unsigned
15757 simple_type_align_in_bits (const_tree type)
15759 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15762 static inline unsigned
15763 simple_decl_align_in_bits (const_tree decl)
15765 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15768 /* Return the result of rounding T up to ALIGN. */
15770 static inline double_int
15771 round_up_to_align (double_int t, unsigned int align)
15773 double_int alignd = uhwi_to_double_int (align);
15774 t = double_int_add (t, alignd);
15775 t = double_int_add (t, double_int_minus_one);
15776 t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15777 t = double_int_mul (t, alignd);
15778 return t;
15781 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15782 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15783 or return 0 if we are unable to determine what that offset is, either
15784 because the argument turns out to be a pointer to an ERROR_MARK node, or
15785 because the offset is actually variable. (We can't handle the latter case
15786 just yet). */
15788 static HOST_WIDE_INT
15789 field_byte_offset (const_tree decl)
15791 double_int object_offset_in_bits;
15792 double_int object_offset_in_bytes;
15793 double_int bitpos_int;
15795 if (TREE_CODE (decl) == ERROR_MARK)
15796 return 0;
15798 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15800 /* We cannot yet cope with fields whose positions are variable, so
15801 for now, when we see such things, we simply return 0. Someday, we may
15802 be able to handle such cases, but it will be damn difficult. */
15803 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15804 return 0;
15806 bitpos_int = tree_to_double_int (bit_position (decl));
15808 #ifdef PCC_BITFIELD_TYPE_MATTERS
15809 if (PCC_BITFIELD_TYPE_MATTERS)
15811 tree type;
15812 tree field_size_tree;
15813 double_int deepest_bitpos;
15814 double_int field_size_in_bits;
15815 unsigned int type_align_in_bits;
15816 unsigned int decl_align_in_bits;
15817 double_int type_size_in_bits;
15819 type = field_type (decl);
15820 type_size_in_bits = double_int_type_size_in_bits (type);
15821 type_align_in_bits = simple_type_align_in_bits (type);
15823 field_size_tree = DECL_SIZE (decl);
15825 /* The size could be unspecified if there was an error, or for
15826 a flexible array member. */
15827 if (!field_size_tree)
15828 field_size_tree = bitsize_zero_node;
15830 /* If the size of the field is not constant, use the type size. */
15831 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15832 field_size_in_bits = tree_to_double_int (field_size_tree);
15833 else
15834 field_size_in_bits = type_size_in_bits;
15836 decl_align_in_bits = simple_decl_align_in_bits (decl);
15838 /* The GCC front-end doesn't make any attempt to keep track of the
15839 starting bit offset (relative to the start of the containing
15840 structure type) of the hypothetical "containing object" for a
15841 bit-field. Thus, when computing the byte offset value for the
15842 start of the "containing object" of a bit-field, we must deduce
15843 this information on our own. This can be rather tricky to do in
15844 some cases. For example, handling the following structure type
15845 definition when compiling for an i386/i486 target (which only
15846 aligns long long's to 32-bit boundaries) can be very tricky:
15848 struct S { int field1; long long field2:31; };
15850 Fortunately, there is a simple rule-of-thumb which can be used
15851 in such cases. When compiling for an i386/i486, GCC will
15852 allocate 8 bytes for the structure shown above. It decides to
15853 do this based upon one simple rule for bit-field allocation.
15854 GCC allocates each "containing object" for each bit-field at
15855 the first (i.e. lowest addressed) legitimate alignment boundary
15856 (based upon the required minimum alignment for the declared
15857 type of the field) which it can possibly use, subject to the
15858 condition that there is still enough available space remaining
15859 in the containing object (when allocated at the selected point)
15860 to fully accommodate all of the bits of the bit-field itself.
15862 This simple rule makes it obvious why GCC allocates 8 bytes for
15863 each object of the structure type shown above. When looking
15864 for a place to allocate the "containing object" for `field2',
15865 the compiler simply tries to allocate a 64-bit "containing
15866 object" at each successive 32-bit boundary (starting at zero)
15867 until it finds a place to allocate that 64- bit field such that
15868 at least 31 contiguous (and previously unallocated) bits remain
15869 within that selected 64 bit field. (As it turns out, for the
15870 example above, the compiler finds it is OK to allocate the
15871 "containing object" 64-bit field at bit-offset zero within the
15872 structure type.)
15874 Here we attempt to work backwards from the limited set of facts
15875 we're given, and we try to deduce from those facts, where GCC
15876 must have believed that the containing object started (within
15877 the structure type). The value we deduce is then used (by the
15878 callers of this routine) to generate DW_AT_location and
15879 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15880 the case of DW_AT_location, regular fields as well). */
15882 /* Figure out the bit-distance from the start of the structure to
15883 the "deepest" bit of the bit-field. */
15884 deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
15886 /* This is the tricky part. Use some fancy footwork to deduce
15887 where the lowest addressed bit of the containing object must
15888 be. */
15889 object_offset_in_bits
15890 = double_int_sub (deepest_bitpos, type_size_in_bits);
15892 /* Round up to type_align by default. This works best for
15893 bitfields. */
15894 object_offset_in_bits
15895 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15897 if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
15899 object_offset_in_bits
15900 = double_int_sub (deepest_bitpos, type_size_in_bits);
15902 /* Round up to decl_align instead. */
15903 object_offset_in_bits
15904 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15907 else
15908 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15909 object_offset_in_bits = bitpos_int;
15911 object_offset_in_bytes
15912 = double_int_div (object_offset_in_bits,
15913 uhwi_to_double_int (BITS_PER_UNIT), true,
15914 TRUNC_DIV_EXPR);
15915 return double_int_to_shwi (object_offset_in_bytes);
15918 /* The following routines define various Dwarf attributes and any data
15919 associated with them. */
15921 /* Add a location description attribute value to a DIE.
15923 This emits location attributes suitable for whole variables and
15924 whole parameters. Note that the location attributes for struct fields are
15925 generated by the routine `data_member_location_attribute' below. */
15927 static inline void
15928 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15929 dw_loc_list_ref descr)
15931 if (descr == 0)
15932 return;
15933 if (single_element_loc_list_p (descr))
15934 add_AT_loc (die, attr_kind, descr->expr);
15935 else
15936 add_AT_loc_list (die, attr_kind, descr);
15939 /* Add DW_AT_accessibility attribute to DIE if needed. */
15941 static void
15942 add_accessibility_attribute (dw_die_ref die, tree decl)
15944 if (TREE_PROTECTED (decl))
15945 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15946 else if (TREE_PRIVATE (decl))
15947 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15950 /* Attach the specialized form of location attribute used for data members of
15951 struct and union types. In the special case of a FIELD_DECL node which
15952 represents a bit-field, the "offset" part of this special location
15953 descriptor must indicate the distance in bytes from the lowest-addressed
15954 byte of the containing struct or union type to the lowest-addressed byte of
15955 the "containing object" for the bit-field. (See the `field_byte_offset'
15956 function above).
15958 For any given bit-field, the "containing object" is a hypothetical object
15959 (of some integral or enum type) within which the given bit-field lives. The
15960 type of this hypothetical "containing object" is always the same as the
15961 declared type of the individual bit-field itself (for GCC anyway... the
15962 DWARF spec doesn't actually mandate this). Note that it is the size (in
15963 bytes) of the hypothetical "containing object" which will be given in the
15964 DW_AT_byte_size attribute for this bit-field. (See the
15965 `byte_size_attribute' function below.) It is also used when calculating the
15966 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15967 function below.) */
15969 static void
15970 add_data_member_location_attribute (dw_die_ref die, tree decl)
15972 HOST_WIDE_INT offset;
15973 dw_loc_descr_ref loc_descr = 0;
15975 if (TREE_CODE (decl) == TREE_BINFO)
15977 /* We're working on the TAG_inheritance for a base class. */
15978 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15980 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15981 aren't at a fixed offset from all (sub)objects of the same
15982 type. We need to extract the appropriate offset from our
15983 vtable. The following dwarf expression means
15985 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15987 This is specific to the V3 ABI, of course. */
15989 dw_loc_descr_ref tmp;
15991 /* Make a copy of the object address. */
15992 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15993 add_loc_descr (&loc_descr, tmp);
15995 /* Extract the vtable address. */
15996 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15997 add_loc_descr (&loc_descr, tmp);
15999 /* Calculate the address of the offset. */
16000 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16001 gcc_assert (offset < 0);
16003 tmp = int_loc_descriptor (-offset);
16004 add_loc_descr (&loc_descr, tmp);
16005 tmp = new_loc_descr (DW_OP_minus, 0, 0);
16006 add_loc_descr (&loc_descr, tmp);
16008 /* Extract the offset. */
16009 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16010 add_loc_descr (&loc_descr, tmp);
16012 /* Add it to the object address. */
16013 tmp = new_loc_descr (DW_OP_plus, 0, 0);
16014 add_loc_descr (&loc_descr, tmp);
16016 else
16017 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16019 else
16020 offset = field_byte_offset (decl);
16022 if (! loc_descr)
16024 if (dwarf_version > 2)
16026 /* Don't need to output a location expression, just the constant. */
16027 if (offset < 0)
16028 add_AT_int (die, DW_AT_data_member_location, offset);
16029 else
16030 add_AT_unsigned (die, DW_AT_data_member_location, offset);
16031 return;
16033 else
16035 enum dwarf_location_atom op;
16037 /* The DWARF2 standard says that we should assume that the structure
16038 address is already on the stack, so we can specify a structure
16039 field address by using DW_OP_plus_uconst. */
16041 #ifdef MIPS_DEBUGGING_INFO
16042 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16043 operator correctly. It works only if we leave the offset on the
16044 stack. */
16045 op = DW_OP_constu;
16046 #else
16047 op = DW_OP_plus_uconst;
16048 #endif
16050 loc_descr = new_loc_descr (op, offset, 0);
16054 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16057 /* Writes integer values to dw_vec_const array. */
16059 static void
16060 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16062 while (size != 0)
16064 *dest++ = val & 0xff;
16065 val >>= 8;
16066 --size;
16070 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
16072 static HOST_WIDE_INT
16073 extract_int (const unsigned char *src, unsigned int size)
16075 HOST_WIDE_INT val = 0;
16077 src += size;
16078 while (size != 0)
16080 val <<= 8;
16081 val |= *--src & 0xff;
16082 --size;
16084 return val;
16087 /* Writes double_int values to dw_vec_const array. */
16089 static void
16090 insert_double (double_int val, unsigned char *dest)
16092 unsigned char *p0 = dest;
16093 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16095 if (WORDS_BIG_ENDIAN)
16097 p0 = p1;
16098 p1 = dest;
16101 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16102 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16105 /* Writes floating point values to dw_vec_const array. */
16107 static void
16108 insert_float (const_rtx rtl, unsigned char *array)
16110 REAL_VALUE_TYPE rv;
16111 long val[4];
16112 int i;
16114 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16115 real_to_target (val, &rv, GET_MODE (rtl));
16117 /* real_to_target puts 32-bit pieces in each long. Pack them. */
16118 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16120 insert_int (val[i], 4, array);
16121 array += 4;
16125 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16126 does not have a "location" either in memory or in a register. These
16127 things can arise in GNU C when a constant is passed as an actual parameter
16128 to an inlined function. They can also arise in C++ where declared
16129 constants do not necessarily get memory "homes". */
16131 static bool
16132 add_const_value_attribute (dw_die_ref die, rtx rtl)
16134 switch (GET_CODE (rtl))
16136 case CONST_INT:
16138 HOST_WIDE_INT val = INTVAL (rtl);
16140 if (val < 0)
16141 add_AT_int (die, DW_AT_const_value, val);
16142 else
16143 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16145 return true;
16147 case CONST_DOUBLE:
16148 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16149 floating-point constant. A CONST_DOUBLE is used whenever the
16150 constant requires more than one word in order to be adequately
16151 represented. */
16153 enum machine_mode mode = GET_MODE (rtl);
16155 if (SCALAR_FLOAT_MODE_P (mode))
16157 unsigned int length = GET_MODE_SIZE (mode);
16158 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16160 insert_float (rtl, array);
16161 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16163 else
16164 add_AT_double (die, DW_AT_const_value,
16165 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16167 return true;
16169 case CONST_VECTOR:
16171 enum machine_mode mode = GET_MODE (rtl);
16172 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16173 unsigned int length = CONST_VECTOR_NUNITS (rtl);
16174 unsigned char *array = (unsigned char *) ggc_alloc_atomic
16175 (length * elt_size);
16176 unsigned int i;
16177 unsigned char *p;
16179 switch (GET_MODE_CLASS (mode))
16181 case MODE_VECTOR_INT:
16182 for (i = 0, p = array; i < length; i++, p += elt_size)
16184 rtx elt = CONST_VECTOR_ELT (rtl, i);
16185 double_int val = rtx_to_double_int (elt);
16187 if (elt_size <= sizeof (HOST_WIDE_INT))
16188 insert_int (double_int_to_shwi (val), elt_size, p);
16189 else
16191 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16192 insert_double (val, p);
16195 break;
16197 case MODE_VECTOR_FLOAT:
16198 for (i = 0, p = array; i < length; i++, p += elt_size)
16200 rtx elt = CONST_VECTOR_ELT (rtl, i);
16201 insert_float (elt, p);
16203 break;
16205 default:
16206 gcc_unreachable ();
16209 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16211 return true;
16213 case CONST_STRING:
16214 if (dwarf_version >= 4 || !dwarf_strict)
16216 dw_loc_descr_ref loc_result;
16217 resolve_one_addr (&rtl, NULL);
16218 rtl_addr:
16219 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16220 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16221 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16222 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16223 add_AT_loc (die, DW_AT_location, loc_result);
16224 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16225 return true;
16227 return false;
16229 case CONST:
16230 if (CONSTANT_P (XEXP (rtl, 0)))
16231 return add_const_value_attribute (die, XEXP (rtl, 0));
16232 /* FALLTHROUGH */
16233 case SYMBOL_REF:
16234 if (!const_ok_for_output (rtl))
16235 return false;
16236 case LABEL_REF:
16237 if (dwarf_version >= 4 || !dwarf_strict)
16238 goto rtl_addr;
16239 return false;
16241 case PLUS:
16242 /* In cases where an inlined instance of an inline function is passed
16243 the address of an `auto' variable (which is local to the caller) we
16244 can get a situation where the DECL_RTL of the artificial local
16245 variable (for the inlining) which acts as a stand-in for the
16246 corresponding formal parameter (of the inline function) will look
16247 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
16248 exactly a compile-time constant expression, but it isn't the address
16249 of the (artificial) local variable either. Rather, it represents the
16250 *value* which the artificial local variable always has during its
16251 lifetime. We currently have no way to represent such quasi-constant
16252 values in Dwarf, so for now we just punt and generate nothing. */
16253 return false;
16255 case HIGH:
16256 case CONST_FIXED:
16257 return false;
16259 case MEM:
16260 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16261 && MEM_READONLY_P (rtl)
16262 && GET_MODE (rtl) == BLKmode)
16264 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16265 return true;
16267 return false;
16269 default:
16270 /* No other kinds of rtx should be possible here. */
16271 gcc_unreachable ();
16273 return false;
16276 /* Determine whether the evaluation of EXPR references any variables
16277 or functions which aren't otherwise used (and therefore may not be
16278 output). */
16279 static tree
16280 reference_to_unused (tree * tp, int * walk_subtrees,
16281 void * data ATTRIBUTE_UNUSED)
16283 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16284 *walk_subtrees = 0;
16286 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16287 && ! TREE_ASM_WRITTEN (*tp))
16288 return *tp;
16289 /* ??? The C++ FE emits debug information for using decls, so
16290 putting gcc_unreachable here falls over. See PR31899. For now
16291 be conservative. */
16292 else if (!cgraph_global_info_ready
16293 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16294 return *tp;
16295 else if (TREE_CODE (*tp) == VAR_DECL)
16297 struct varpool_node *node = varpool_get_node (*tp);
16298 if (!node || !node->needed)
16299 return *tp;
16301 else if (TREE_CODE (*tp) == FUNCTION_DECL
16302 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16304 /* The call graph machinery must have finished analyzing,
16305 optimizing and gimplifying the CU by now.
16306 So if *TP has no call graph node associated
16307 to it, it means *TP will not be emitted. */
16308 if (!cgraph_get_node (*tp))
16309 return *tp;
16311 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16312 return *tp;
16314 return NULL_TREE;
16317 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16318 for use in a later add_const_value_attribute call. */
16320 static rtx
16321 rtl_for_decl_init (tree init, tree type)
16323 rtx rtl = NULL_RTX;
16325 /* If a variable is initialized with a string constant without embedded
16326 zeros, build CONST_STRING. */
16327 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16329 tree enttype = TREE_TYPE (type);
16330 tree domain = TYPE_DOMAIN (type);
16331 enum machine_mode mode = TYPE_MODE (enttype);
16333 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16334 && domain
16335 && integer_zerop (TYPE_MIN_VALUE (domain))
16336 && compare_tree_int (TYPE_MAX_VALUE (domain),
16337 TREE_STRING_LENGTH (init) - 1) == 0
16338 && ((size_t) TREE_STRING_LENGTH (init)
16339 == strlen (TREE_STRING_POINTER (init)) + 1))
16341 rtl = gen_rtx_CONST_STRING (VOIDmode,
16342 ggc_strdup (TREE_STRING_POINTER (init)));
16343 rtl = gen_rtx_MEM (BLKmode, rtl);
16344 MEM_READONLY_P (rtl) = 1;
16347 /* Other aggregates, and complex values, could be represented using
16348 CONCAT: FIXME! */
16349 else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
16351 /* Vectors only work if their mode is supported by the target.
16352 FIXME: generic vectors ought to work too. */
16353 else if (TREE_CODE (type) == VECTOR_TYPE
16354 && !VECTOR_MODE_P (TYPE_MODE (type)))
16356 /* If the initializer is something that we know will expand into an
16357 immediate RTL constant, expand it now. We must be careful not to
16358 reference variables which won't be output. */
16359 else if (initializer_constant_valid_p (init, type)
16360 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16362 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16363 possible. */
16364 if (TREE_CODE (type) == VECTOR_TYPE)
16365 switch (TREE_CODE (init))
16367 case VECTOR_CST:
16368 break;
16369 case CONSTRUCTOR:
16370 if (TREE_CONSTANT (init))
16372 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16373 bool constant_p = true;
16374 tree value;
16375 unsigned HOST_WIDE_INT ix;
16377 /* Even when ctor is constant, it might contain non-*_CST
16378 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16379 belong into VECTOR_CST nodes. */
16380 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16381 if (!CONSTANT_CLASS_P (value))
16383 constant_p = false;
16384 break;
16387 if (constant_p)
16389 init = build_vector_from_ctor (type, elts);
16390 break;
16393 /* FALLTHRU */
16395 default:
16396 return NULL;
16399 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16401 /* If expand_expr returns a MEM, it wasn't immediate. */
16402 gcc_assert (!rtl || !MEM_P (rtl));
16405 return rtl;
16408 /* Generate RTL for the variable DECL to represent its location. */
16410 static rtx
16411 rtl_for_decl_location (tree decl)
16413 rtx rtl;
16415 /* Here we have to decide where we are going to say the parameter "lives"
16416 (as far as the debugger is concerned). We only have a couple of
16417 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16419 DECL_RTL normally indicates where the parameter lives during most of the
16420 activation of the function. If optimization is enabled however, this
16421 could be either NULL or else a pseudo-reg. Both of those cases indicate
16422 that the parameter doesn't really live anywhere (as far as the code
16423 generation parts of GCC are concerned) during most of the function's
16424 activation. That will happen (for example) if the parameter is never
16425 referenced within the function.
16427 We could just generate a location descriptor here for all non-NULL
16428 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16429 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16430 where DECL_RTL is NULL or is a pseudo-reg.
16432 Note however that we can only get away with using DECL_INCOMING_RTL as
16433 a backup substitute for DECL_RTL in certain limited cases. In cases
16434 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16435 we can be sure that the parameter was passed using the same type as it is
16436 declared to have within the function, and that its DECL_INCOMING_RTL
16437 points us to a place where a value of that type is passed.
16439 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16440 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16441 because in these cases DECL_INCOMING_RTL points us to a value of some
16442 type which is *different* from the type of the parameter itself. Thus,
16443 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16444 such cases, the debugger would end up (for example) trying to fetch a
16445 `float' from a place which actually contains the first part of a
16446 `double'. That would lead to really incorrect and confusing
16447 output at debug-time.
16449 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16450 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
16451 are a couple of exceptions however. On little-endian machines we can
16452 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16453 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16454 an integral type that is smaller than TREE_TYPE (decl). These cases arise
16455 when (on a little-endian machine) a non-prototyped function has a
16456 parameter declared to be of type `short' or `char'. In such cases,
16457 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16458 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16459 passed `int' value. If the debugger then uses that address to fetch
16460 a `short' or a `char' (on a little-endian machine) the result will be
16461 the correct data, so we allow for such exceptional cases below.
16463 Note that our goal here is to describe the place where the given formal
16464 parameter lives during most of the function's activation (i.e. between the
16465 end of the prologue and the start of the epilogue). We'll do that as best
16466 as we can. Note however that if the given formal parameter is modified
16467 sometime during the execution of the function, then a stack backtrace (at
16468 debug-time) will show the function as having been called with the *new*
16469 value rather than the value which was originally passed in. This happens
16470 rarely enough that it is not a major problem, but it *is* a problem, and
16471 I'd like to fix it.
16473 A future version of dwarf2out.c may generate two additional attributes for
16474 any given DW_TAG_formal_parameter DIE which will describe the "passed
16475 type" and the "passed location" for the given formal parameter in addition
16476 to the attributes we now generate to indicate the "declared type" and the
16477 "active location" for each parameter. This additional set of attributes
16478 could be used by debuggers for stack backtraces. Separately, note that
16479 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16480 This happens (for example) for inlined-instances of inline function formal
16481 parameters which are never referenced. This really shouldn't be
16482 happening. All PARM_DECL nodes should get valid non-NULL
16483 DECL_INCOMING_RTL values. FIXME. */
16485 /* Use DECL_RTL as the "location" unless we find something better. */
16486 rtl = DECL_RTL_IF_SET (decl);
16488 /* When generating abstract instances, ignore everything except
16489 constants, symbols living in memory, and symbols living in
16490 fixed registers. */
16491 if (! reload_completed)
16493 if (rtl
16494 && (CONSTANT_P (rtl)
16495 || (MEM_P (rtl)
16496 && CONSTANT_P (XEXP (rtl, 0)))
16497 || (REG_P (rtl)
16498 && TREE_CODE (decl) == VAR_DECL
16499 && TREE_STATIC (decl))))
16501 rtl = targetm.delegitimize_address (rtl);
16502 return rtl;
16504 rtl = NULL_RTX;
16506 else if (TREE_CODE (decl) == PARM_DECL)
16508 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16510 tree declared_type = TREE_TYPE (decl);
16511 tree passed_type = DECL_ARG_TYPE (decl);
16512 enum machine_mode dmode = TYPE_MODE (declared_type);
16513 enum machine_mode pmode = TYPE_MODE (passed_type);
16515 /* This decl represents a formal parameter which was optimized out.
16516 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16517 all cases where (rtl == NULL_RTX) just below. */
16518 if (dmode == pmode)
16519 rtl = DECL_INCOMING_RTL (decl);
16520 else if (SCALAR_INT_MODE_P (dmode)
16521 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16522 && DECL_INCOMING_RTL (decl))
16524 rtx inc = DECL_INCOMING_RTL (decl);
16525 if (REG_P (inc))
16526 rtl = inc;
16527 else if (MEM_P (inc))
16529 if (BYTES_BIG_ENDIAN)
16530 rtl = adjust_address_nv (inc, dmode,
16531 GET_MODE_SIZE (pmode)
16532 - GET_MODE_SIZE (dmode));
16533 else
16534 rtl = inc;
16539 /* If the parm was passed in registers, but lives on the stack, then
16540 make a big endian correction if the mode of the type of the
16541 parameter is not the same as the mode of the rtl. */
16542 /* ??? This is the same series of checks that are made in dbxout.c before
16543 we reach the big endian correction code there. It isn't clear if all
16544 of these checks are necessary here, but keeping them all is the safe
16545 thing to do. */
16546 else if (MEM_P (rtl)
16547 && XEXP (rtl, 0) != const0_rtx
16548 && ! CONSTANT_P (XEXP (rtl, 0))
16549 /* Not passed in memory. */
16550 && !MEM_P (DECL_INCOMING_RTL (decl))
16551 /* Not passed by invisible reference. */
16552 && (!REG_P (XEXP (rtl, 0))
16553 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16554 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16555 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
16556 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16557 #endif
16559 /* Big endian correction check. */
16560 && BYTES_BIG_ENDIAN
16561 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16562 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16563 < UNITS_PER_WORD))
16565 int offset = (UNITS_PER_WORD
16566 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16568 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16569 plus_constant (XEXP (rtl, 0), offset));
16572 else if (TREE_CODE (decl) == VAR_DECL
16573 && rtl
16574 && MEM_P (rtl)
16575 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16576 && BYTES_BIG_ENDIAN)
16578 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16579 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16581 /* If a variable is declared "register" yet is smaller than
16582 a register, then if we store the variable to memory, it
16583 looks like we're storing a register-sized value, when in
16584 fact we are not. We need to adjust the offset of the
16585 storage location to reflect the actual value's bytes,
16586 else gdb will not be able to display it. */
16587 if (rsize > dsize)
16588 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16589 plus_constant (XEXP (rtl, 0), rsize-dsize));
16592 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16593 and will have been substituted directly into all expressions that use it.
16594 C does not have such a concept, but C++ and other languages do. */
16595 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16596 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16598 if (rtl)
16599 rtl = targetm.delegitimize_address (rtl);
16601 /* If we don't look past the constant pool, we risk emitting a
16602 reference to a constant pool entry that isn't referenced from
16603 code, and thus is not emitted. */
16604 if (rtl)
16605 rtl = avoid_constant_pool_reference (rtl);
16607 /* Try harder to get a rtl. If this symbol ends up not being emitted
16608 in the current CU, resolve_addr will remove the expression referencing
16609 it. */
16610 if (rtl == NULL_RTX
16611 && TREE_CODE (decl) == VAR_DECL
16612 && !DECL_EXTERNAL (decl)
16613 && TREE_STATIC (decl)
16614 && DECL_NAME (decl)
16615 && !DECL_HARD_REGISTER (decl)
16616 && DECL_MODE (decl) != VOIDmode)
16618 rtl = make_decl_rtl_for_debug (decl);
16619 if (!MEM_P (rtl)
16620 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16621 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16622 rtl = NULL_RTX;
16625 return rtl;
16628 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16629 returned. If so, the decl for the COMMON block is returned, and the
16630 value is the offset into the common block for the symbol. */
16632 static tree
16633 fortran_common (tree decl, HOST_WIDE_INT *value)
16635 tree val_expr, cvar;
16636 enum machine_mode mode;
16637 HOST_WIDE_INT bitsize, bitpos;
16638 tree offset;
16639 int volatilep = 0, unsignedp = 0;
16641 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16642 it does not have a value (the offset into the common area), or if it
16643 is thread local (as opposed to global) then it isn't common, and shouldn't
16644 be handled as such. */
16645 if (TREE_CODE (decl) != VAR_DECL
16646 || !TREE_STATIC (decl)
16647 || !DECL_HAS_VALUE_EXPR_P (decl)
16648 || !is_fortran ())
16649 return NULL_TREE;
16651 val_expr = DECL_VALUE_EXPR (decl);
16652 if (TREE_CODE (val_expr) != COMPONENT_REF)
16653 return NULL_TREE;
16655 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16656 &mode, &unsignedp, &volatilep, true);
16658 if (cvar == NULL_TREE
16659 || TREE_CODE (cvar) != VAR_DECL
16660 || DECL_ARTIFICIAL (cvar)
16661 || !TREE_PUBLIC (cvar))
16662 return NULL_TREE;
16664 *value = 0;
16665 if (offset != NULL)
16667 if (!host_integerp (offset, 0))
16668 return NULL_TREE;
16669 *value = tree_low_cst (offset, 0);
16671 if (bitpos != 0)
16672 *value += bitpos / BITS_PER_UNIT;
16674 return cvar;
16677 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16678 data attribute for a variable or a parameter. We generate the
16679 DW_AT_const_value attribute only in those cases where the given variable
16680 or parameter does not have a true "location" either in memory or in a
16681 register. This can happen (for example) when a constant is passed as an
16682 actual argument in a call to an inline function. (It's possible that
16683 these things can crop up in other ways also.) Note that one type of
16684 constant value which can be passed into an inlined function is a constant
16685 pointer. This can happen for example if an actual argument in an inlined
16686 function call evaluates to a compile-time constant address. */
16688 static bool
16689 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16690 enum dwarf_attribute attr)
16692 rtx rtl;
16693 dw_loc_list_ref list;
16694 var_loc_list *loc_list;
16696 if (TREE_CODE (decl) == ERROR_MARK)
16697 return false;
16699 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16700 || TREE_CODE (decl) == RESULT_DECL);
16702 /* Try to get some constant RTL for this decl, and use that as the value of
16703 the location. */
16705 rtl = rtl_for_decl_location (decl);
16706 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16707 && add_const_value_attribute (die, rtl))
16708 return true;
16710 /* See if we have single element location list that is equivalent to
16711 a constant value. That way we are better to use add_const_value_attribute
16712 rather than expanding constant value equivalent. */
16713 loc_list = lookup_decl_loc (decl);
16714 if (loc_list
16715 && loc_list->first
16716 && loc_list->first->next == NULL
16717 && NOTE_P (loc_list->first->loc)
16718 && NOTE_VAR_LOCATION (loc_list->first->loc)
16719 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16721 struct var_loc_node *node;
16723 node = loc_list->first;
16724 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16725 if (GET_CODE (rtl) == EXPR_LIST)
16726 rtl = XEXP (rtl, 0);
16727 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16728 && add_const_value_attribute (die, rtl))
16729 return true;
16731 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16732 if (list)
16734 add_AT_location_description (die, attr, list);
16735 return true;
16737 /* None of that worked, so it must not really have a location;
16738 try adding a constant value attribute from the DECL_INITIAL. */
16739 return tree_add_const_value_attribute_for_decl (die, decl);
16742 /* Add VARIABLE and DIE into deferred locations list. */
16744 static void
16745 defer_location (tree variable, dw_die_ref die)
16747 deferred_locations entry;
16748 entry.variable = variable;
16749 entry.die = die;
16750 VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16753 /* Helper function for tree_add_const_value_attribute. Natively encode
16754 initializer INIT into an array. Return true if successful. */
16756 static bool
16757 native_encode_initializer (tree init, unsigned char *array, int size)
16759 tree type;
16761 if (init == NULL_TREE)
16762 return false;
16764 STRIP_NOPS (init);
16765 switch (TREE_CODE (init))
16767 case STRING_CST:
16768 type = TREE_TYPE (init);
16769 if (TREE_CODE (type) == ARRAY_TYPE)
16771 tree enttype = TREE_TYPE (type);
16772 enum machine_mode mode = TYPE_MODE (enttype);
16774 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16775 return false;
16776 if (int_size_in_bytes (type) != size)
16777 return false;
16778 if (size > TREE_STRING_LENGTH (init))
16780 memcpy (array, TREE_STRING_POINTER (init),
16781 TREE_STRING_LENGTH (init));
16782 memset (array + TREE_STRING_LENGTH (init),
16783 '\0', size - TREE_STRING_LENGTH (init));
16785 else
16786 memcpy (array, TREE_STRING_POINTER (init), size);
16787 return true;
16789 return false;
16790 case CONSTRUCTOR:
16791 type = TREE_TYPE (init);
16792 if (int_size_in_bytes (type) != size)
16793 return false;
16794 if (TREE_CODE (type) == ARRAY_TYPE)
16796 HOST_WIDE_INT min_index;
16797 unsigned HOST_WIDE_INT cnt;
16798 int curpos = 0, fieldsize;
16799 constructor_elt *ce;
16801 if (TYPE_DOMAIN (type) == NULL_TREE
16802 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
16803 return false;
16805 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16806 if (fieldsize <= 0)
16807 return false;
16809 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
16810 memset (array, '\0', size);
16811 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
16813 tree val = ce->value;
16814 tree index = ce->index;
16815 int pos = curpos;
16816 if (index && TREE_CODE (index) == RANGE_EXPR)
16817 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
16818 * fieldsize;
16819 else if (index)
16820 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
16822 if (val)
16824 STRIP_NOPS (val);
16825 if (!native_encode_initializer (val, array + pos, fieldsize))
16826 return false;
16828 curpos = pos + fieldsize;
16829 if (index && TREE_CODE (index) == RANGE_EXPR)
16831 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
16832 - tree_low_cst (TREE_OPERAND (index, 0), 0);
16833 while (count > 0)
16835 if (val)
16836 memcpy (array + curpos, array + pos, fieldsize);
16837 curpos += fieldsize;
16840 gcc_assert (curpos <= size);
16842 return true;
16844 else if (TREE_CODE (type) == RECORD_TYPE
16845 || TREE_CODE (type) == UNION_TYPE)
16847 tree field = NULL_TREE;
16848 unsigned HOST_WIDE_INT cnt;
16849 constructor_elt *ce;
16851 if (int_size_in_bytes (type) != size)
16852 return false;
16854 if (TREE_CODE (type) == RECORD_TYPE)
16855 field = TYPE_FIELDS (type);
16857 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
16859 tree val = ce->value;
16860 int pos, fieldsize;
16862 if (ce->index != 0)
16863 field = ce->index;
16865 if (val)
16866 STRIP_NOPS (val);
16868 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16869 return false;
16871 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16872 && TYPE_DOMAIN (TREE_TYPE (field))
16873 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16874 return false;
16875 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16876 || !host_integerp (DECL_SIZE_UNIT (field), 0))
16877 return false;
16878 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
16879 pos = int_byte_position (field);
16880 gcc_assert (pos + fieldsize <= size);
16881 if (val
16882 && !native_encode_initializer (val, array + pos, fieldsize))
16883 return false;
16885 return true;
16887 return false;
16888 case VIEW_CONVERT_EXPR:
16889 case NON_LVALUE_EXPR:
16890 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16891 default:
16892 return native_encode_expr (init, array, size) == size;
16896 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16897 attribute is the const value T. */
16899 static bool
16900 tree_add_const_value_attribute (dw_die_ref die, tree t)
16902 tree init;
16903 tree type = TREE_TYPE (t);
16904 rtx rtl;
16906 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16907 return false;
16909 init = t;
16910 gcc_assert (!DECL_P (init));
16912 rtl = rtl_for_decl_init (init, type);
16913 if (rtl)
16914 return add_const_value_attribute (die, rtl);
16915 /* If the host and target are sane, try harder. */
16916 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16917 && initializer_constant_valid_p (init, type))
16919 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16920 if (size > 0 && (int) size == size)
16922 unsigned char *array = (unsigned char *)
16923 ggc_alloc_cleared_atomic (size);
16925 if (native_encode_initializer (init, array, size))
16927 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16928 return true;
16932 return false;
16935 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16936 attribute is the const value of T, where T is an integral constant
16937 variable with static storage duration
16938 (so it can't be a PARM_DECL or a RESULT_DECL). */
16940 static bool
16941 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16944 if (!decl
16945 || (TREE_CODE (decl) != VAR_DECL
16946 && TREE_CODE (decl) != CONST_DECL))
16947 return false;
16949 if (TREE_READONLY (decl)
16950 && ! TREE_THIS_VOLATILE (decl)
16951 && DECL_INITIAL (decl))
16952 /* OK */;
16953 else
16954 return false;
16956 /* Don't add DW_AT_const_value if abstract origin already has one. */
16957 if (get_AT (var_die, DW_AT_const_value))
16958 return false;
16960 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16963 /* Convert the CFI instructions for the current function into a
16964 location list. This is used for DW_AT_frame_base when we targeting
16965 a dwarf2 consumer that does not support the dwarf3
16966 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16967 expressions. */
16969 static dw_loc_list_ref
16970 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16972 dw_fde_ref fde;
16973 dw_loc_list_ref list, *list_tail;
16974 dw_cfi_ref cfi;
16975 dw_cfa_location last_cfa, next_cfa;
16976 const char *start_label, *last_label, *section;
16977 dw_cfa_location remember;
16979 fde = current_fde ();
16980 gcc_assert (fde != NULL);
16982 section = secname_for_decl (current_function_decl);
16983 list_tail = &list;
16984 list = NULL;
16986 memset (&next_cfa, 0, sizeof (next_cfa));
16987 next_cfa.reg = INVALID_REGNUM;
16988 remember = next_cfa;
16990 start_label = fde->dw_fde_begin;
16992 /* ??? Bald assumption that the CIE opcode list does not contain
16993 advance opcodes. */
16994 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
16995 lookup_cfa_1 (cfi, &next_cfa, &remember);
16997 last_cfa = next_cfa;
16998 last_label = start_label;
17000 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17001 switch (cfi->dw_cfi_opc)
17003 case DW_CFA_set_loc:
17004 case DW_CFA_advance_loc1:
17005 case DW_CFA_advance_loc2:
17006 case DW_CFA_advance_loc4:
17007 if (!cfa_equal_p (&last_cfa, &next_cfa))
17009 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17010 start_label, last_label, section);
17012 list_tail = &(*list_tail)->dw_loc_next;
17013 last_cfa = next_cfa;
17014 start_label = last_label;
17016 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17017 break;
17019 case DW_CFA_advance_loc:
17020 /* The encoding is complex enough that we should never emit this. */
17021 gcc_unreachable ();
17023 default:
17024 lookup_cfa_1 (cfi, &next_cfa, &remember);
17025 break;
17028 if (!cfa_equal_p (&last_cfa, &next_cfa))
17030 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17031 start_label, last_label, section);
17032 list_tail = &(*list_tail)->dw_loc_next;
17033 start_label = last_label;
17036 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17037 start_label, fde->dw_fde_end, section);
17039 if (list && list->dw_loc_next)
17040 gen_llsym (list);
17042 return list;
17045 /* Compute a displacement from the "steady-state frame pointer" to the
17046 frame base (often the same as the CFA), and store it in
17047 frame_pointer_fb_offset. OFFSET is added to the displacement
17048 before the latter is negated. */
17050 static void
17051 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17053 rtx reg, elim;
17055 #ifdef FRAME_POINTER_CFA_OFFSET
17056 reg = frame_pointer_rtx;
17057 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17058 #else
17059 reg = arg_pointer_rtx;
17060 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17061 #endif
17063 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17064 if (GET_CODE (elim) == PLUS)
17066 offset += INTVAL (XEXP (elim, 1));
17067 elim = XEXP (elim, 0);
17070 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17071 && (elim == hard_frame_pointer_rtx
17072 || elim == stack_pointer_rtx))
17073 || elim == (frame_pointer_needed
17074 ? hard_frame_pointer_rtx
17075 : stack_pointer_rtx));
17077 frame_pointer_fb_offset = -offset;
17080 /* Generate a DW_AT_name attribute given some string value to be included as
17081 the value of the attribute. */
17083 static void
17084 add_name_attribute (dw_die_ref die, const char *name_string)
17086 if (name_string != NULL && *name_string != 0)
17088 if (demangle_name_func)
17089 name_string = (*demangle_name_func) (name_string);
17091 add_AT_string (die, DW_AT_name, name_string);
17095 /* Generate a DW_AT_comp_dir attribute for DIE. */
17097 static void
17098 add_comp_dir_attribute (dw_die_ref die)
17100 const char *wd = get_src_pwd ();
17101 char *wd1;
17103 if (wd == NULL)
17104 return;
17106 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17108 int wdlen;
17110 wdlen = strlen (wd);
17111 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17112 strcpy (wd1, wd);
17113 wd1 [wdlen] = DIR_SEPARATOR;
17114 wd1 [wdlen + 1] = 0;
17115 wd = wd1;
17118 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17121 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17122 default. */
17124 static int
17125 lower_bound_default (void)
17127 switch (get_AT_unsigned (comp_unit_die, DW_AT_language))
17129 case DW_LANG_C:
17130 case DW_LANG_C89:
17131 case DW_LANG_C99:
17132 case DW_LANG_C_plus_plus:
17133 case DW_LANG_ObjC:
17134 case DW_LANG_ObjC_plus_plus:
17135 case DW_LANG_Java:
17136 return 0;
17137 case DW_LANG_Fortran77:
17138 case DW_LANG_Fortran90:
17139 case DW_LANG_Fortran95:
17140 return 1;
17141 case DW_LANG_UPC:
17142 case DW_LANG_D:
17143 case DW_LANG_Python:
17144 return dwarf_version >= 4 ? 0 : -1;
17145 case DW_LANG_Ada95:
17146 case DW_LANG_Ada83:
17147 case DW_LANG_Cobol74:
17148 case DW_LANG_Cobol85:
17149 case DW_LANG_Pascal83:
17150 case DW_LANG_Modula2:
17151 case DW_LANG_PLI:
17152 return dwarf_version >= 4 ? 1 : -1;
17153 default:
17154 return -1;
17158 /* Given a tree node describing an array bound (either lower or upper) output
17159 a representation for that bound. */
17161 static void
17162 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17164 switch (TREE_CODE (bound))
17166 case ERROR_MARK:
17167 return;
17169 /* All fixed-bounds are represented by INTEGER_CST nodes. */
17170 case INTEGER_CST:
17172 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17173 int dflt;
17175 /* Use the default if possible. */
17176 if (bound_attr == DW_AT_lower_bound
17177 && host_integerp (bound, 0)
17178 && (dflt = lower_bound_default ()) != -1
17179 && tree_low_cst (bound, 0) == dflt)
17182 /* Otherwise represent the bound as an unsigned value with the
17183 precision of its type. The precision and signedness of the
17184 type will be necessary to re-interpret it unambiguously. */
17185 else if (prec < HOST_BITS_PER_WIDE_INT)
17187 unsigned HOST_WIDE_INT mask
17188 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17189 add_AT_unsigned (subrange_die, bound_attr,
17190 TREE_INT_CST_LOW (bound) & mask);
17192 else if (prec == HOST_BITS_PER_WIDE_INT
17193 || TREE_INT_CST_HIGH (bound) == 0)
17194 add_AT_unsigned (subrange_die, bound_attr,
17195 TREE_INT_CST_LOW (bound));
17196 else
17197 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17198 TREE_INT_CST_LOW (bound));
17200 break;
17202 CASE_CONVERT:
17203 case VIEW_CONVERT_EXPR:
17204 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17205 break;
17207 case SAVE_EXPR:
17208 break;
17210 case VAR_DECL:
17211 case PARM_DECL:
17212 case RESULT_DECL:
17214 dw_die_ref decl_die = lookup_decl_die (bound);
17216 /* ??? Can this happen, or should the variable have been bound
17217 first? Probably it can, since I imagine that we try to create
17218 the types of parameters in the order in which they exist in
17219 the list, and won't have created a forward reference to a
17220 later parameter. */
17221 if (decl_die != NULL)
17223 add_AT_die_ref (subrange_die, bound_attr, decl_die);
17224 break;
17227 /* FALLTHRU */
17229 default:
17231 /* Otherwise try to create a stack operation procedure to
17232 evaluate the value of the array bound. */
17234 dw_die_ref ctx, decl_die;
17235 dw_loc_list_ref list;
17237 list = loc_list_from_tree (bound, 2);
17238 if (list == NULL || single_element_loc_list_p (list))
17240 /* If DW_AT_*bound is not a reference nor constant, it is
17241 a DWARF expression rather than location description.
17242 For that loc_list_from_tree (bound, 0) is needed.
17243 If that fails to give a single element list,
17244 fall back to outputting this as a reference anyway. */
17245 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17246 if (list2 && single_element_loc_list_p (list2))
17248 add_AT_loc (subrange_die, bound_attr, list2->expr);
17249 break;
17252 if (list == NULL)
17253 break;
17255 if (current_function_decl == 0)
17256 ctx = comp_unit_die;
17257 else
17258 ctx = lookup_decl_die (current_function_decl);
17260 decl_die = new_die (DW_TAG_variable, ctx, bound);
17261 add_AT_flag (decl_die, DW_AT_artificial, 1);
17262 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17263 add_AT_location_description (decl_die, DW_AT_location, list);
17264 add_AT_die_ref (subrange_die, bound_attr, decl_die);
17265 break;
17270 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17271 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17272 Note that the block of subscript information for an array type also
17273 includes information about the element type of the given array type. */
17275 static void
17276 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17278 unsigned dimension_number;
17279 tree lower, upper;
17280 dw_die_ref subrange_die;
17282 for (dimension_number = 0;
17283 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17284 type = TREE_TYPE (type), dimension_number++)
17286 tree domain = TYPE_DOMAIN (type);
17288 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17289 break;
17291 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17292 and (in GNU C only) variable bounds. Handle all three forms
17293 here. */
17294 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17295 if (domain)
17297 /* We have an array type with specified bounds. */
17298 lower = TYPE_MIN_VALUE (domain);
17299 upper = TYPE_MAX_VALUE (domain);
17301 /* Define the index type. */
17302 if (TREE_TYPE (domain))
17304 /* ??? This is probably an Ada unnamed subrange type. Ignore the
17305 TREE_TYPE field. We can't emit debug info for this
17306 because it is an unnamed integral type. */
17307 if (TREE_CODE (domain) == INTEGER_TYPE
17308 && TYPE_NAME (domain) == NULL_TREE
17309 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17310 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17312 else
17313 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17314 type_die);
17317 /* ??? If upper is NULL, the array has unspecified length,
17318 but it does have a lower bound. This happens with Fortran
17319 dimension arr(N:*)
17320 Since the debugger is definitely going to need to know N
17321 to produce useful results, go ahead and output the lower
17322 bound solo, and hope the debugger can cope. */
17324 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17325 if (upper)
17326 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17329 /* Otherwise we have an array type with an unspecified length. The
17330 DWARF-2 spec does not say how to handle this; let's just leave out the
17331 bounds. */
17335 static void
17336 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17338 unsigned size;
17340 switch (TREE_CODE (tree_node))
17342 case ERROR_MARK:
17343 size = 0;
17344 break;
17345 case ENUMERAL_TYPE:
17346 case RECORD_TYPE:
17347 case UNION_TYPE:
17348 case QUAL_UNION_TYPE:
17349 size = int_size_in_bytes (tree_node);
17350 break;
17351 case FIELD_DECL:
17352 /* For a data member of a struct or union, the DW_AT_byte_size is
17353 generally given as the number of bytes normally allocated for an
17354 object of the *declared* type of the member itself. This is true
17355 even for bit-fields. */
17356 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17357 break;
17358 default:
17359 gcc_unreachable ();
17362 /* Note that `size' might be -1 when we get to this point. If it is, that
17363 indicates that the byte size of the entity in question is variable. We
17364 have no good way of expressing this fact in Dwarf at the present time,
17365 so just let the -1 pass on through. */
17366 add_AT_unsigned (die, DW_AT_byte_size, size);
17369 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17370 which specifies the distance in bits from the highest order bit of the
17371 "containing object" for the bit-field to the highest order bit of the
17372 bit-field itself.
17374 For any given bit-field, the "containing object" is a hypothetical object
17375 (of some integral or enum type) within which the given bit-field lives. The
17376 type of this hypothetical "containing object" is always the same as the
17377 declared type of the individual bit-field itself. The determination of the
17378 exact location of the "containing object" for a bit-field is rather
17379 complicated. It's handled by the `field_byte_offset' function (above).
17381 Note that it is the size (in bytes) of the hypothetical "containing object"
17382 which will be given in the DW_AT_byte_size attribute for this bit-field.
17383 (See `byte_size_attribute' above). */
17385 static inline void
17386 add_bit_offset_attribute (dw_die_ref die, tree decl)
17388 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17389 tree type = DECL_BIT_FIELD_TYPE (decl);
17390 HOST_WIDE_INT bitpos_int;
17391 HOST_WIDE_INT highest_order_object_bit_offset;
17392 HOST_WIDE_INT highest_order_field_bit_offset;
17393 HOST_WIDE_INT unsigned bit_offset;
17395 /* Must be a field and a bit field. */
17396 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17398 /* We can't yet handle bit-fields whose offsets are variable, so if we
17399 encounter such things, just return without generating any attribute
17400 whatsoever. Likewise for variable or too large size. */
17401 if (! host_integerp (bit_position (decl), 0)
17402 || ! host_integerp (DECL_SIZE (decl), 1))
17403 return;
17405 bitpos_int = int_bit_position (decl);
17407 /* Note that the bit offset is always the distance (in bits) from the
17408 highest-order bit of the "containing object" to the highest-order bit of
17409 the bit-field itself. Since the "high-order end" of any object or field
17410 is different on big-endian and little-endian machines, the computation
17411 below must take account of these differences. */
17412 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17413 highest_order_field_bit_offset = bitpos_int;
17415 if (! BYTES_BIG_ENDIAN)
17417 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17418 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17421 bit_offset
17422 = (! BYTES_BIG_ENDIAN
17423 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17424 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17426 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17429 /* For a FIELD_DECL node which represents a bit field, output an attribute
17430 which specifies the length in bits of the given field. */
17432 static inline void
17433 add_bit_size_attribute (dw_die_ref die, tree decl)
17435 /* Must be a field and a bit field. */
17436 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17437 && DECL_BIT_FIELD_TYPE (decl));
17439 if (host_integerp (DECL_SIZE (decl), 1))
17440 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17443 /* If the compiled language is ANSI C, then add a 'prototyped'
17444 attribute, if arg types are given for the parameters of a function. */
17446 static inline void
17447 add_prototyped_attribute (dw_die_ref die, tree func_type)
17449 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
17450 && TYPE_ARG_TYPES (func_type) != NULL)
17451 add_AT_flag (die, DW_AT_prototyped, 1);
17454 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17455 by looking in either the type declaration or object declaration
17456 equate table. */
17458 static inline dw_die_ref
17459 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17461 dw_die_ref origin_die = NULL;
17463 if (TREE_CODE (origin) != FUNCTION_DECL)
17465 /* We may have gotten separated from the block for the inlined
17466 function, if we're in an exception handler or some such; make
17467 sure that the abstract function has been written out.
17469 Doing this for nested functions is wrong, however; functions are
17470 distinct units, and our context might not even be inline. */
17471 tree fn = origin;
17473 if (TYPE_P (fn))
17474 fn = TYPE_STUB_DECL (fn);
17476 fn = decl_function_context (fn);
17477 if (fn)
17478 dwarf2out_abstract_function (fn);
17481 if (DECL_P (origin))
17482 origin_die = lookup_decl_die (origin);
17483 else if (TYPE_P (origin))
17484 origin_die = lookup_type_die (origin);
17486 /* XXX: Functions that are never lowered don't always have correct block
17487 trees (in the case of java, they simply have no block tree, in some other
17488 languages). For these functions, there is nothing we can really do to
17489 output correct debug info for inlined functions in all cases. Rather
17490 than die, we'll just produce deficient debug info now, in that we will
17491 have variables without a proper abstract origin. In the future, when all
17492 functions are lowered, we should re-add a gcc_assert (origin_die)
17493 here. */
17495 if (origin_die)
17496 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17497 return origin_die;
17500 /* We do not currently support the pure_virtual attribute. */
17502 static inline void
17503 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17505 if (DECL_VINDEX (func_decl))
17507 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17509 if (host_integerp (DECL_VINDEX (func_decl), 0))
17510 add_AT_loc (die, DW_AT_vtable_elem_location,
17511 new_loc_descr (DW_OP_constu,
17512 tree_low_cst (DECL_VINDEX (func_decl), 0),
17513 0));
17515 /* GNU extension: Record what type this method came from originally. */
17516 if (debug_info_level > DINFO_LEVEL_TERSE
17517 && DECL_CONTEXT (func_decl))
17518 add_AT_die_ref (die, DW_AT_containing_type,
17519 lookup_type_die (DECL_CONTEXT (func_decl)));
17523 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17524 given decl. This used to be a vendor extension until after DWARF 4
17525 standardized it. */
17527 static void
17528 add_linkage_attr (dw_die_ref die, tree decl)
17530 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17532 /* Mimic what assemble_name_raw does with a leading '*'. */
17533 if (name[0] == '*')
17534 name = &name[1];
17536 if (dwarf_version >= 4)
17537 add_AT_string (die, DW_AT_linkage_name, name);
17538 else
17539 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17542 /* Add source coordinate attributes for the given decl. */
17544 static void
17545 add_src_coords_attributes (dw_die_ref die, tree decl)
17547 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17549 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17550 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17553 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17555 static void
17556 add_linkage_name (dw_die_ref die, tree decl)
17558 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17559 && TREE_PUBLIC (decl)
17560 && !DECL_ABSTRACT (decl)
17561 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17562 && die->die_tag != DW_TAG_member)
17564 /* Defer until we have an assembler name set. */
17565 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17567 limbo_die_node *asm_name;
17569 asm_name = ggc_alloc_cleared_limbo_die_node ();
17570 asm_name->die = die;
17571 asm_name->created_for = decl;
17572 asm_name->next = deferred_asm_name;
17573 deferred_asm_name = asm_name;
17575 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17576 add_linkage_attr (die, decl);
17580 /* Add a DW_AT_name attribute and source coordinate attribute for the
17581 given decl, but only if it actually has a name. */
17583 static void
17584 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17586 tree decl_name;
17588 decl_name = DECL_NAME (decl);
17589 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17591 const char *name = dwarf2_name (decl, 0);
17592 if (name)
17593 add_name_attribute (die, name);
17594 if (! DECL_ARTIFICIAL (decl))
17595 add_src_coords_attributes (die, decl);
17597 add_linkage_name (die, decl);
17600 #ifdef VMS_DEBUGGING_INFO
17601 /* Get the function's name, as described by its RTL. This may be different
17602 from the DECL_NAME name used in the source file. */
17603 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17605 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17606 XEXP (DECL_RTL (decl), 0));
17607 VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17609 #endif /* VMS_DEBUGGING_INFO */
17612 #ifdef VMS_DEBUGGING_INFO
17613 /* Output the debug main pointer die for VMS */
17615 void
17616 dwarf2out_vms_debug_main_pointer (void)
17618 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17619 dw_die_ref die;
17621 /* Allocate the VMS debug main subprogram die. */
17622 die = ggc_alloc_cleared_die_node ();
17623 die->die_tag = DW_TAG_subprogram;
17624 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17625 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17626 current_function_funcdef_no);
17627 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17629 /* Make it the first child of comp_unit_die. */
17630 die->die_parent = comp_unit_die;
17631 if (comp_unit_die->die_child)
17633 die->die_sib = comp_unit_die->die_child->die_sib;
17634 comp_unit_die->die_child->die_sib = die;
17636 else
17638 die->die_sib = die;
17639 comp_unit_die->die_child = die;
17642 #endif /* VMS_DEBUGGING_INFO */
17644 /* Push a new declaration scope. */
17646 static void
17647 push_decl_scope (tree scope)
17649 VEC_safe_push (tree, gc, decl_scope_table, scope);
17652 /* Pop a declaration scope. */
17654 static inline void
17655 pop_decl_scope (void)
17657 VEC_pop (tree, decl_scope_table);
17660 /* Return the DIE for the scope that immediately contains this type.
17661 Non-named types get global scope. Named types nested in other
17662 types get their containing scope if it's open, or global scope
17663 otherwise. All other types (i.e. function-local named types) get
17664 the current active scope. */
17666 static dw_die_ref
17667 scope_die_for (tree t, dw_die_ref context_die)
17669 dw_die_ref scope_die = NULL;
17670 tree containing_scope;
17671 int i;
17673 /* Non-types always go in the current scope. */
17674 gcc_assert (TYPE_P (t));
17676 containing_scope = TYPE_CONTEXT (t);
17678 /* Use the containing namespace if it was passed in (for a declaration). */
17679 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17681 if (context_die == lookup_decl_die (containing_scope))
17682 /* OK */;
17683 else
17684 containing_scope = NULL_TREE;
17687 /* Ignore function type "scopes" from the C frontend. They mean that
17688 a tagged type is local to a parmlist of a function declarator, but
17689 that isn't useful to DWARF. */
17690 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17691 containing_scope = NULL_TREE;
17693 if (containing_scope == NULL_TREE)
17694 scope_die = comp_unit_die;
17695 else if (TYPE_P (containing_scope))
17697 /* For types, we can just look up the appropriate DIE. But
17698 first we check to see if we're in the middle of emitting it
17699 so we know where the new DIE should go. */
17700 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17701 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17702 break;
17704 if (i < 0)
17706 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17707 || TREE_ASM_WRITTEN (containing_scope));
17708 /*We are not in the middle of emitting the type
17709 CONTAINING_SCOPE. Let's see if it's emitted already. */
17710 scope_die = lookup_type_die (containing_scope);
17712 /* If none of the current dies are suitable, we get file scope. */
17713 if (scope_die == NULL)
17714 scope_die = comp_unit_die;
17716 else
17717 scope_die = lookup_type_die (containing_scope);
17719 else
17720 scope_die = context_die;
17722 return scope_die;
17725 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17727 static inline int
17728 local_scope_p (dw_die_ref context_die)
17730 for (; context_die; context_die = context_die->die_parent)
17731 if (context_die->die_tag == DW_TAG_inlined_subroutine
17732 || context_die->die_tag == DW_TAG_subprogram)
17733 return 1;
17735 return 0;
17738 /* Returns nonzero if CONTEXT_DIE is a class. */
17740 static inline int
17741 class_scope_p (dw_die_ref context_die)
17743 return (context_die
17744 && (context_die->die_tag == DW_TAG_structure_type
17745 || context_die->die_tag == DW_TAG_class_type
17746 || context_die->die_tag == DW_TAG_interface_type
17747 || context_die->die_tag == DW_TAG_union_type));
17750 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17751 whether or not to treat a DIE in this context as a declaration. */
17753 static inline int
17754 class_or_namespace_scope_p (dw_die_ref context_die)
17756 return (class_scope_p (context_die)
17757 || (context_die && context_die->die_tag == DW_TAG_namespace));
17760 /* Many forms of DIEs require a "type description" attribute. This
17761 routine locates the proper "type descriptor" die for the type given
17762 by 'type', and adds a DW_AT_type attribute below the given die. */
17764 static void
17765 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17766 int decl_volatile, dw_die_ref context_die)
17768 enum tree_code code = TREE_CODE (type);
17769 dw_die_ref type_die = NULL;
17771 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17772 or fixed-point type, use the inner type. This is because we have no
17773 support for unnamed types in base_type_die. This can happen if this is
17774 an Ada subrange type. Correct solution is emit a subrange type die. */
17775 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17776 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17777 type = TREE_TYPE (type), code = TREE_CODE (type);
17779 if (code == ERROR_MARK
17780 /* Handle a special case. For functions whose return type is void, we
17781 generate *no* type attribute. (Note that no object may have type
17782 `void', so this only applies to function return types). */
17783 || code == VOID_TYPE)
17784 return;
17786 type_die = modified_type_die (type,
17787 decl_const || TYPE_READONLY (type),
17788 decl_volatile || TYPE_VOLATILE (type),
17789 context_die);
17791 if (type_die != NULL)
17792 add_AT_die_ref (object_die, DW_AT_type, type_die);
17795 /* Given an object die, add the calling convention attribute for the
17796 function call type. */
17797 static void
17798 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17800 enum dwarf_calling_convention value = DW_CC_normal;
17802 value = ((enum dwarf_calling_convention)
17803 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17805 /* DWARF doesn't provide a way to identify a program's source-level
17806 entry point. DW_AT_calling_convention attributes are only meant
17807 to describe functions' calling conventions. However, lacking a
17808 better way to signal the Fortran main program, we use this for the
17809 time being, following existing custom. */
17810 if (is_fortran ()
17811 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17812 value = DW_CC_program;
17814 /* Only add the attribute if the backend requests it, and
17815 is not DW_CC_normal. */
17816 if (value && (value != DW_CC_normal))
17817 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17820 /* Given a tree pointer to a struct, class, union, or enum type node, return
17821 a pointer to the (string) tag name for the given type, or zero if the type
17822 was declared without a tag. */
17824 static const char *
17825 type_tag (const_tree type)
17827 const char *name = 0;
17829 if (TYPE_NAME (type) != 0)
17831 tree t = 0;
17833 /* Find the IDENTIFIER_NODE for the type name. */
17834 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17835 && !TYPE_NAMELESS (type))
17836 t = TYPE_NAME (type);
17838 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17839 a TYPE_DECL node, regardless of whether or not a `typedef' was
17840 involved. */
17841 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17842 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17844 /* We want to be extra verbose. Don't call dwarf_name if
17845 DECL_NAME isn't set. The default hook for decl_printable_name
17846 doesn't like that, and in this context it's correct to return
17847 0, instead of "<anonymous>" or the like. */
17848 if (DECL_NAME (TYPE_NAME (type))
17849 && !DECL_NAMELESS (TYPE_NAME (type)))
17850 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17853 /* Now get the name as a string, or invent one. */
17854 if (!name && t != 0)
17855 name = IDENTIFIER_POINTER (t);
17858 return (name == 0 || *name == '\0') ? 0 : name;
17861 /* Return the type associated with a data member, make a special check
17862 for bit field types. */
17864 static inline tree
17865 member_declared_type (const_tree member)
17867 return (DECL_BIT_FIELD_TYPE (member)
17868 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17871 /* Get the decl's label, as described by its RTL. This may be different
17872 from the DECL_NAME name used in the source file. */
17874 #if 0
17875 static const char *
17876 decl_start_label (tree decl)
17878 rtx x;
17879 const char *fnname;
17881 x = DECL_RTL (decl);
17882 gcc_assert (MEM_P (x));
17884 x = XEXP (x, 0);
17885 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17887 fnname = XSTR (x, 0);
17888 return fnname;
17890 #endif
17892 /* These routines generate the internal representation of the DIE's for
17893 the compilation unit. Debugging information is collected by walking
17894 the declaration trees passed in from dwarf2out_decl(). */
17896 static void
17897 gen_array_type_die (tree type, dw_die_ref context_die)
17899 dw_die_ref scope_die = scope_die_for (type, context_die);
17900 dw_die_ref array_die;
17902 /* GNU compilers represent multidimensional array types as sequences of one
17903 dimensional array types whose element types are themselves array types.
17904 We sometimes squish that down to a single array_type DIE with multiple
17905 subscripts in the Dwarf debugging info. The draft Dwarf specification
17906 say that we are allowed to do this kind of compression in C, because
17907 there is no difference between an array of arrays and a multidimensional
17908 array. We don't do this for Ada to remain as close as possible to the
17909 actual representation, which is especially important against the language
17910 flexibilty wrt arrays of variable size. */
17912 bool collapse_nested_arrays = !is_ada ();
17913 tree element_type;
17915 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17916 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17917 if (TYPE_STRING_FLAG (type)
17918 && TREE_CODE (type) == ARRAY_TYPE
17919 && is_fortran ()
17920 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17922 HOST_WIDE_INT size;
17924 array_die = new_die (DW_TAG_string_type, scope_die, type);
17925 add_name_attribute (array_die, type_tag (type));
17926 equate_type_number_to_die (type, array_die);
17927 size = int_size_in_bytes (type);
17928 if (size >= 0)
17929 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17930 else if (TYPE_DOMAIN (type) != NULL_TREE
17931 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17932 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17934 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17935 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17937 size = int_size_in_bytes (TREE_TYPE (szdecl));
17938 if (loc && size > 0)
17940 add_AT_location_description (array_die, DW_AT_string_length, loc);
17941 if (size != DWARF2_ADDR_SIZE)
17942 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17945 return;
17948 /* ??? The SGI dwarf reader fails for array of array of enum types
17949 (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
17950 array type comes before the outer array type. We thus call gen_type_die
17951 before we new_die and must prevent nested array types collapsing for this
17952 target. */
17954 #ifdef MIPS_DEBUGGING_INFO
17955 gen_type_die (TREE_TYPE (type), context_die);
17956 collapse_nested_arrays = false;
17957 #endif
17959 array_die = new_die (DW_TAG_array_type, scope_die, type);
17960 add_name_attribute (array_die, type_tag (type));
17961 equate_type_number_to_die (type, array_die);
17963 if (TREE_CODE (type) == VECTOR_TYPE)
17964 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17966 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17967 if (is_fortran ()
17968 && TREE_CODE (type) == ARRAY_TYPE
17969 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17970 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17971 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17973 #if 0
17974 /* We default the array ordering. SDB will probably do
17975 the right things even if DW_AT_ordering is not present. It's not even
17976 an issue until we start to get into multidimensional arrays anyway. If
17977 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17978 then we'll have to put the DW_AT_ordering attribute back in. (But if
17979 and when we find out that we need to put these in, we will only do so
17980 for multidimensional arrays. */
17981 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17982 #endif
17984 #ifdef MIPS_DEBUGGING_INFO
17985 /* The SGI compilers handle arrays of unknown bound by setting
17986 AT_declaration and not emitting any subrange DIEs. */
17987 if (TREE_CODE (type) == ARRAY_TYPE
17988 && ! TYPE_DOMAIN (type))
17989 add_AT_flag (array_die, DW_AT_declaration, 1);
17990 else
17991 #endif
17992 if (TREE_CODE (type) == VECTOR_TYPE)
17994 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17995 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17996 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17997 add_bound_info (subrange_die, DW_AT_upper_bound,
17998 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18000 else
18001 add_subscript_info (array_die, type, collapse_nested_arrays);
18003 /* Add representation of the type of the elements of this array type and
18004 emit the corresponding DIE if we haven't done it already. */
18005 element_type = TREE_TYPE (type);
18006 if (collapse_nested_arrays)
18007 while (TREE_CODE (element_type) == ARRAY_TYPE)
18009 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18010 break;
18011 element_type = TREE_TYPE (element_type);
18014 #ifndef MIPS_DEBUGGING_INFO
18015 gen_type_die (element_type, context_die);
18016 #endif
18018 add_type_attribute (array_die, element_type, 0, 0, context_die);
18020 if (get_AT (array_die, DW_AT_name))
18021 add_pubtype (type, array_die);
18024 static dw_loc_descr_ref
18025 descr_info_loc (tree val, tree base_decl)
18027 HOST_WIDE_INT size;
18028 dw_loc_descr_ref loc, loc2;
18029 enum dwarf_location_atom op;
18031 if (val == base_decl)
18032 return new_loc_descr (DW_OP_push_object_address, 0, 0);
18034 switch (TREE_CODE (val))
18036 CASE_CONVERT:
18037 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18038 case VAR_DECL:
18039 return loc_descriptor_from_tree (val, 0);
18040 case INTEGER_CST:
18041 if (host_integerp (val, 0))
18042 return int_loc_descriptor (tree_low_cst (val, 0));
18043 break;
18044 case INDIRECT_REF:
18045 size = int_size_in_bytes (TREE_TYPE (val));
18046 if (size < 0)
18047 break;
18048 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18049 if (!loc)
18050 break;
18051 if (size == DWARF2_ADDR_SIZE)
18052 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18053 else
18054 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18055 return loc;
18056 case POINTER_PLUS_EXPR:
18057 case PLUS_EXPR:
18058 if (host_integerp (TREE_OPERAND (val, 1), 1)
18059 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18060 < 16384)
18062 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18063 if (!loc)
18064 break;
18065 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18067 else
18069 op = DW_OP_plus;
18070 do_binop:
18071 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18072 if (!loc)
18073 break;
18074 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18075 if (!loc2)
18076 break;
18077 add_loc_descr (&loc, loc2);
18078 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18080 return loc;
18081 case MINUS_EXPR:
18082 op = DW_OP_minus;
18083 goto do_binop;
18084 case MULT_EXPR:
18085 op = DW_OP_mul;
18086 goto do_binop;
18087 case EQ_EXPR:
18088 op = DW_OP_eq;
18089 goto do_binop;
18090 case NE_EXPR:
18091 op = DW_OP_ne;
18092 goto do_binop;
18093 default:
18094 break;
18096 return NULL;
18099 static void
18100 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18101 tree val, tree base_decl)
18103 dw_loc_descr_ref loc;
18105 if (host_integerp (val, 0))
18107 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18108 return;
18111 loc = descr_info_loc (val, base_decl);
18112 if (!loc)
18113 return;
18115 add_AT_loc (die, attr, loc);
18118 /* This routine generates DIE for array with hidden descriptor, details
18119 are filled into *info by a langhook. */
18121 static void
18122 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18123 dw_die_ref context_die)
18125 dw_die_ref scope_die = scope_die_for (type, context_die);
18126 dw_die_ref array_die;
18127 int dim;
18129 array_die = new_die (DW_TAG_array_type, scope_die, type);
18130 add_name_attribute (array_die, type_tag (type));
18131 equate_type_number_to_die (type, array_die);
18133 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
18134 if (is_fortran ()
18135 && info->ndimensions >= 2)
18136 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18138 if (info->data_location)
18139 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18140 info->base_decl);
18141 if (info->associated)
18142 add_descr_info_field (array_die, DW_AT_associated, info->associated,
18143 info->base_decl);
18144 if (info->allocated)
18145 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18146 info->base_decl);
18148 for (dim = 0; dim < info->ndimensions; dim++)
18150 dw_die_ref subrange_die
18151 = new_die (DW_TAG_subrange_type, array_die, NULL);
18153 if (info->dimen[dim].lower_bound)
18155 /* If it is the default value, omit it. */
18156 int dflt;
18158 if (host_integerp (info->dimen[dim].lower_bound, 0)
18159 && (dflt = lower_bound_default ()) != -1
18160 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18162 else
18163 add_descr_info_field (subrange_die, DW_AT_lower_bound,
18164 info->dimen[dim].lower_bound,
18165 info->base_decl);
18167 if (info->dimen[dim].upper_bound)
18168 add_descr_info_field (subrange_die, DW_AT_upper_bound,
18169 info->dimen[dim].upper_bound,
18170 info->base_decl);
18171 if (info->dimen[dim].stride)
18172 add_descr_info_field (subrange_die, DW_AT_byte_stride,
18173 info->dimen[dim].stride,
18174 info->base_decl);
18177 gen_type_die (info->element_type, context_die);
18178 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18180 if (get_AT (array_die, DW_AT_name))
18181 add_pubtype (type, array_die);
18184 #if 0
18185 static void
18186 gen_entry_point_die (tree decl, dw_die_ref context_die)
18188 tree origin = decl_ultimate_origin (decl);
18189 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18191 if (origin != NULL)
18192 add_abstract_origin_attribute (decl_die, origin);
18193 else
18195 add_name_and_src_coords_attributes (decl_die, decl);
18196 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18197 0, 0, context_die);
18200 if (DECL_ABSTRACT (decl))
18201 equate_decl_number_to_die (decl, decl_die);
18202 else
18203 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18205 #endif
18207 /* Walk through the list of incomplete types again, trying once more to
18208 emit full debugging info for them. */
18210 static void
18211 retry_incomplete_types (void)
18213 int i;
18215 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18216 if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18217 DINFO_USAGE_DIR_USE))
18218 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
18221 /* Determine what tag to use for a record type. */
18223 static enum dwarf_tag
18224 record_type_tag (tree type)
18226 if (! lang_hooks.types.classify_record)
18227 return DW_TAG_structure_type;
18229 switch (lang_hooks.types.classify_record (type))
18231 case RECORD_IS_STRUCT:
18232 return DW_TAG_structure_type;
18234 case RECORD_IS_CLASS:
18235 return DW_TAG_class_type;
18237 case RECORD_IS_INTERFACE:
18238 if (dwarf_version >= 3 || !dwarf_strict)
18239 return DW_TAG_interface_type;
18240 return DW_TAG_structure_type;
18242 default:
18243 gcc_unreachable ();
18247 /* Generate a DIE to represent an enumeration type. Note that these DIEs
18248 include all of the information about the enumeration values also. Each
18249 enumerated type name/value is listed as a child of the enumerated type
18250 DIE. */
18252 static dw_die_ref
18253 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18255 dw_die_ref type_die = lookup_type_die (type);
18257 if (type_die == NULL)
18259 type_die = new_die (DW_TAG_enumeration_type,
18260 scope_die_for (type, context_die), type);
18261 equate_type_number_to_die (type, type_die);
18262 add_name_attribute (type_die, type_tag (type));
18263 if ((dwarf_version >= 4 || !dwarf_strict)
18264 && ENUM_IS_SCOPED (type))
18265 add_AT_flag (type_die, DW_AT_enum_class, 1);
18267 else if (! TYPE_SIZE (type))
18268 return type_die;
18269 else
18270 remove_AT (type_die, DW_AT_declaration);
18272 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
18273 given enum type is incomplete, do not generate the DW_AT_byte_size
18274 attribute or the DW_AT_element_list attribute. */
18275 if (TYPE_SIZE (type))
18277 tree link;
18279 TREE_ASM_WRITTEN (type) = 1;
18280 add_byte_size_attribute (type_die, type);
18281 if (TYPE_STUB_DECL (type) != NULL_TREE)
18283 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18284 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18287 /* If the first reference to this type was as the return type of an
18288 inline function, then it may not have a parent. Fix this now. */
18289 if (type_die->die_parent == NULL)
18290 add_child_die (scope_die_for (type, context_die), type_die);
18292 for (link = TYPE_VALUES (type);
18293 link != NULL; link = TREE_CHAIN (link))
18295 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18296 tree value = TREE_VALUE (link);
18298 add_name_attribute (enum_die,
18299 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18301 if (TREE_CODE (value) == CONST_DECL)
18302 value = DECL_INITIAL (value);
18304 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18305 /* DWARF2 does not provide a way of indicating whether or
18306 not enumeration constants are signed or unsigned. GDB
18307 always assumes the values are signed, so we output all
18308 values as if they were signed. That means that
18309 enumeration constants with very large unsigned values
18310 will appear to have negative values in the debugger. */
18311 add_AT_int (enum_die, DW_AT_const_value,
18312 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18315 else
18316 add_AT_flag (type_die, DW_AT_declaration, 1);
18318 if (get_AT (type_die, DW_AT_name))
18319 add_pubtype (type, type_die);
18321 return type_die;
18324 /* Generate a DIE to represent either a real live formal parameter decl or to
18325 represent just the type of some formal parameter position in some function
18326 type.
18328 Note that this routine is a bit unusual because its argument may be a
18329 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18330 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18331 node. If it's the former then this function is being called to output a
18332 DIE to represent a formal parameter object (or some inlining thereof). If
18333 it's the latter, then this function is only being called to output a
18334 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18335 argument type of some subprogram type.
18336 If EMIT_NAME_P is true, name and source coordinate attributes
18337 are emitted. */
18339 static dw_die_ref
18340 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18341 dw_die_ref context_die)
18343 tree node_or_origin = node ? node : origin;
18344 tree ultimate_origin;
18345 dw_die_ref parm_die
18346 = new_die (DW_TAG_formal_parameter, context_die, node);
18348 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18350 case tcc_declaration:
18351 ultimate_origin = decl_ultimate_origin (node_or_origin);
18352 if (node || ultimate_origin)
18353 origin = ultimate_origin;
18354 if (origin != NULL)
18355 add_abstract_origin_attribute (parm_die, origin);
18356 else if (emit_name_p)
18357 add_name_and_src_coords_attributes (parm_die, node);
18358 if (origin == NULL
18359 || (! DECL_ABSTRACT (node_or_origin)
18360 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18361 decl_function_context
18362 (node_or_origin))))
18364 tree type = TREE_TYPE (node_or_origin);
18365 if (decl_by_reference_p (node_or_origin))
18366 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18367 context_die);
18368 else
18369 add_type_attribute (parm_die, type,
18370 TREE_READONLY (node_or_origin),
18371 TREE_THIS_VOLATILE (node_or_origin),
18372 context_die);
18374 if (origin == NULL && DECL_ARTIFICIAL (node))
18375 add_AT_flag (parm_die, DW_AT_artificial, 1);
18377 if (node && node != origin)
18378 equate_decl_number_to_die (node, parm_die);
18379 if (! DECL_ABSTRACT (node_or_origin))
18380 add_location_or_const_value_attribute (parm_die, node_or_origin,
18381 DW_AT_location);
18383 break;
18385 case tcc_type:
18386 /* We were called with some kind of a ..._TYPE node. */
18387 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18388 break;
18390 default:
18391 gcc_unreachable ();
18394 return parm_die;
18397 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18398 children DW_TAG_formal_parameter DIEs representing the arguments of the
18399 parameter pack.
18401 PARM_PACK must be a function parameter pack.
18402 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18403 must point to the subsequent arguments of the function PACK_ARG belongs to.
18404 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18405 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18406 following the last one for which a DIE was generated. */
18408 static dw_die_ref
18409 gen_formal_parameter_pack_die (tree parm_pack,
18410 tree pack_arg,
18411 dw_die_ref subr_die,
18412 tree *next_arg)
18414 tree arg;
18415 dw_die_ref parm_pack_die;
18417 gcc_assert (parm_pack
18418 && lang_hooks.function_parameter_pack_p (parm_pack)
18419 && subr_die);
18421 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18422 add_src_coords_attributes (parm_pack_die, parm_pack);
18424 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18426 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18427 parm_pack))
18428 break;
18429 gen_formal_parameter_die (arg, NULL,
18430 false /* Don't emit name attribute. */,
18431 parm_pack_die);
18433 if (next_arg)
18434 *next_arg = arg;
18435 return parm_pack_die;
18438 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18439 at the end of an (ANSI prototyped) formal parameters list. */
18441 static void
18442 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18444 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18447 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18448 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18449 parameters as specified in some function type specification (except for
18450 those which appear as part of a function *definition*). */
18452 static void
18453 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18455 tree link;
18456 tree formal_type = NULL;
18457 tree first_parm_type;
18458 tree arg;
18460 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18462 arg = DECL_ARGUMENTS (function_or_method_type);
18463 function_or_method_type = TREE_TYPE (function_or_method_type);
18465 else
18466 arg = NULL_TREE;
18468 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18470 /* Make our first pass over the list of formal parameter types and output a
18471 DW_TAG_formal_parameter DIE for each one. */
18472 for (link = first_parm_type; link; )
18474 dw_die_ref parm_die;
18476 formal_type = TREE_VALUE (link);
18477 if (formal_type == void_type_node)
18478 break;
18480 /* Output a (nameless) DIE to represent the formal parameter itself. */
18481 parm_die = gen_formal_parameter_die (formal_type, NULL,
18482 true /* Emit name attribute. */,
18483 context_die);
18484 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18485 && link == first_parm_type)
18487 add_AT_flag (parm_die, DW_AT_artificial, 1);
18488 if (dwarf_version >= 3 || !dwarf_strict)
18489 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18491 else if (arg && DECL_ARTIFICIAL (arg))
18492 add_AT_flag (parm_die, DW_AT_artificial, 1);
18494 link = TREE_CHAIN (link);
18495 if (arg)
18496 arg = DECL_CHAIN (arg);
18499 /* If this function type has an ellipsis, add a
18500 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18501 if (formal_type != void_type_node)
18502 gen_unspecified_parameters_die (function_or_method_type, context_die);
18504 /* Make our second (and final) pass over the list of formal parameter types
18505 and output DIEs to represent those types (as necessary). */
18506 for (link = TYPE_ARG_TYPES (function_or_method_type);
18507 link && TREE_VALUE (link);
18508 link = TREE_CHAIN (link))
18509 gen_type_die (TREE_VALUE (link), context_die);
18512 /* We want to generate the DIE for TYPE so that we can generate the
18513 die for MEMBER, which has been defined; we will need to refer back
18514 to the member declaration nested within TYPE. If we're trying to
18515 generate minimal debug info for TYPE, processing TYPE won't do the
18516 trick; we need to attach the member declaration by hand. */
18518 static void
18519 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18521 gen_type_die (type, context_die);
18523 /* If we're trying to avoid duplicate debug info, we may not have
18524 emitted the member decl for this function. Emit it now. */
18525 if (TYPE_STUB_DECL (type)
18526 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18527 && ! lookup_decl_die (member))
18529 dw_die_ref type_die;
18530 gcc_assert (!decl_ultimate_origin (member));
18532 push_decl_scope (type);
18533 type_die = lookup_type_die (type);
18534 if (TREE_CODE (member) == FUNCTION_DECL)
18535 gen_subprogram_die (member, type_die);
18536 else if (TREE_CODE (member) == FIELD_DECL)
18538 /* Ignore the nameless fields that are used to skip bits but handle
18539 C++ anonymous unions and structs. */
18540 if (DECL_NAME (member) != NULL_TREE
18541 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18542 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18544 gen_type_die (member_declared_type (member), type_die);
18545 gen_field_die (member, type_die);
18548 else
18549 gen_variable_die (member, NULL_TREE, type_die);
18551 pop_decl_scope ();
18555 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18556 may later generate inlined and/or out-of-line instances of. */
18558 static void
18559 dwarf2out_abstract_function (tree decl)
18561 dw_die_ref old_die;
18562 tree save_fn;
18563 tree context;
18564 int was_abstract;
18565 htab_t old_decl_loc_table;
18567 /* Make sure we have the actual abstract inline, not a clone. */
18568 decl = DECL_ORIGIN (decl);
18570 old_die = lookup_decl_die (decl);
18571 if (old_die && get_AT (old_die, DW_AT_inline))
18572 /* We've already generated the abstract instance. */
18573 return;
18575 /* We can be called while recursively when seeing block defining inlined subroutine
18576 DIE. Be sure to not clobber the outer location table nor use it or we would
18577 get locations in abstract instantces. */
18578 old_decl_loc_table = decl_loc_table;
18579 decl_loc_table = NULL;
18581 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18582 we don't get confused by DECL_ABSTRACT. */
18583 if (debug_info_level > DINFO_LEVEL_TERSE)
18585 context = decl_class_context (decl);
18586 if (context)
18587 gen_type_die_for_member
18588 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
18591 /* Pretend we've just finished compiling this function. */
18592 save_fn = current_function_decl;
18593 current_function_decl = decl;
18594 push_cfun (DECL_STRUCT_FUNCTION (decl));
18596 was_abstract = DECL_ABSTRACT (decl);
18597 set_decl_abstract_flags (decl, 1);
18598 dwarf2out_decl (decl);
18599 if (! was_abstract)
18600 set_decl_abstract_flags (decl, 0);
18602 current_function_decl = save_fn;
18603 decl_loc_table = old_decl_loc_table;
18604 pop_cfun ();
18607 /* Helper function of premark_used_types() which gets called through
18608 htab_traverse.
18610 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18611 marked as unused by prune_unused_types. */
18613 static int
18614 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18616 tree type;
18617 dw_die_ref die;
18619 type = (tree) *slot;
18620 die = lookup_type_die (type);
18621 if (die != NULL)
18622 die->die_perennial_p = 1;
18623 return 1;
18626 /* Helper function of premark_types_used_by_global_vars which gets called
18627 through htab_traverse.
18629 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18630 marked as unused by prune_unused_types. The DIE of the type is marked
18631 only if the global variable using the type will actually be emitted. */
18633 static int
18634 premark_types_used_by_global_vars_helper (void **slot,
18635 void *data ATTRIBUTE_UNUSED)
18637 struct types_used_by_vars_entry *entry;
18638 dw_die_ref die;
18640 entry = (struct types_used_by_vars_entry *) *slot;
18641 gcc_assert (entry->type != NULL
18642 && entry->var_decl != NULL);
18643 die = lookup_type_die (entry->type);
18644 if (die)
18646 /* Ask cgraph if the global variable really is to be emitted.
18647 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18648 struct varpool_node *node = varpool_get_node (entry->var_decl);
18649 if (node && node->needed)
18651 die->die_perennial_p = 1;
18652 /* Keep the parent DIEs as well. */
18653 while ((die = die->die_parent) && die->die_perennial_p == 0)
18654 die->die_perennial_p = 1;
18657 return 1;
18660 /* Mark all members of used_types_hash as perennial. */
18662 static void
18663 premark_used_types (void)
18665 if (cfun && cfun->used_types_hash)
18666 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18669 /* Mark all members of types_used_by_vars_entry as perennial. */
18671 static void
18672 premark_types_used_by_global_vars (void)
18674 if (types_used_by_vars_hash)
18675 htab_traverse (types_used_by_vars_hash,
18676 premark_types_used_by_global_vars_helper, NULL);
18679 /* Generate a DIE to represent a declared function (either file-scope or
18680 block-local). */
18682 static void
18683 gen_subprogram_die (tree decl, dw_die_ref context_die)
18685 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
18686 tree origin = decl_ultimate_origin (decl);
18687 dw_die_ref subr_die;
18688 tree fn_arg_types;
18689 tree outer_scope;
18690 dw_die_ref old_die = lookup_decl_die (decl);
18691 int declaration = (current_function_decl != decl
18692 || class_or_namespace_scope_p (context_die));
18694 premark_used_types ();
18696 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18697 started to generate the abstract instance of an inline, decided to output
18698 its containing class, and proceeded to emit the declaration of the inline
18699 from the member list for the class. If so, DECLARATION takes priority;
18700 we'll get back to the abstract instance when done with the class. */
18702 /* The class-scope declaration DIE must be the primary DIE. */
18703 if (origin && declaration && class_or_namespace_scope_p (context_die))
18705 origin = NULL;
18706 gcc_assert (!old_die);
18709 /* Now that the C++ front end lazily declares artificial member fns, we
18710 might need to retrofit the declaration into its class. */
18711 if (!declaration && !origin && !old_die
18712 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18713 && !class_or_namespace_scope_p (context_die)
18714 && debug_info_level > DINFO_LEVEL_TERSE)
18715 old_die = force_decl_die (decl);
18717 if (origin != NULL)
18719 gcc_assert (!declaration || local_scope_p (context_die));
18721 /* Fixup die_parent for the abstract instance of a nested
18722 inline function. */
18723 if (old_die && old_die->die_parent == NULL)
18724 add_child_die (context_die, old_die);
18726 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18727 add_abstract_origin_attribute (subr_die, origin);
18729 else if (old_die)
18731 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18732 struct dwarf_file_data * file_index = lookup_filename (s.file);
18734 if (!get_AT_flag (old_die, DW_AT_declaration)
18735 /* We can have a normal definition following an inline one in the
18736 case of redefinition of GNU C extern inlines.
18737 It seems reasonable to use AT_specification in this case. */
18738 && !get_AT (old_die, DW_AT_inline))
18740 /* Detect and ignore this case, where we are trying to output
18741 something we have already output. */
18742 return;
18745 /* If the definition comes from the same place as the declaration,
18746 maybe use the old DIE. We always want the DIE for this function
18747 that has the *_pc attributes to be under comp_unit_die so the
18748 debugger can find it. We also need to do this for abstract
18749 instances of inlines, since the spec requires the out-of-line copy
18750 to have the same parent. For local class methods, this doesn't
18751 apply; we just use the old DIE. */
18752 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
18753 && (DECL_ARTIFICIAL (decl)
18754 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18755 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18756 == (unsigned) s.line))))
18758 subr_die = old_die;
18760 /* Clear out the declaration attribute and the formal parameters.
18761 Do not remove all children, because it is possible that this
18762 declaration die was forced using force_decl_die(). In such
18763 cases die that forced declaration die (e.g. TAG_imported_module)
18764 is one of the children that we do not want to remove. */
18765 remove_AT (subr_die, DW_AT_declaration);
18766 remove_AT (subr_die, DW_AT_object_pointer);
18767 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18769 else
18771 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18772 add_AT_specification (subr_die, old_die);
18773 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18774 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18775 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18776 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18779 else
18781 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18783 if (TREE_PUBLIC (decl))
18784 add_AT_flag (subr_die, DW_AT_external, 1);
18786 add_name_and_src_coords_attributes (subr_die, decl);
18787 if (debug_info_level > DINFO_LEVEL_TERSE)
18789 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18790 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18791 0, 0, context_die);
18794 add_pure_or_virtual_attribute (subr_die, decl);
18795 if (DECL_ARTIFICIAL (decl))
18796 add_AT_flag (subr_die, DW_AT_artificial, 1);
18798 add_accessibility_attribute (subr_die, decl);
18801 if (declaration)
18803 if (!old_die || !get_AT (old_die, DW_AT_inline))
18805 add_AT_flag (subr_die, DW_AT_declaration, 1);
18807 /* If this is an explicit function declaration then generate
18808 a DW_AT_explicit attribute. */
18809 if (lang_hooks.decls.function_decl_explicit_p (decl)
18810 && (dwarf_version >= 3 || !dwarf_strict))
18811 add_AT_flag (subr_die, DW_AT_explicit, 1);
18813 /* The first time we see a member function, it is in the context of
18814 the class to which it belongs. We make sure of this by emitting
18815 the class first. The next time is the definition, which is
18816 handled above. The two may come from the same source text.
18818 Note that force_decl_die() forces function declaration die. It is
18819 later reused to represent definition. */
18820 equate_decl_number_to_die (decl, subr_die);
18823 else if (DECL_ABSTRACT (decl))
18825 if (DECL_DECLARED_INLINE_P (decl))
18827 if (cgraph_function_possibly_inlined_p (decl))
18828 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18829 else
18830 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18832 else
18834 if (cgraph_function_possibly_inlined_p (decl))
18835 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18836 else
18837 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18840 if (DECL_DECLARED_INLINE_P (decl)
18841 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18842 add_AT_flag (subr_die, DW_AT_artificial, 1);
18844 equate_decl_number_to_die (decl, subr_die);
18846 else if (!DECL_EXTERNAL (decl))
18848 HOST_WIDE_INT cfa_fb_offset;
18850 if (!old_die || !get_AT (old_die, DW_AT_inline))
18851 equate_decl_number_to_die (decl, subr_die);
18853 if (!flag_reorder_blocks_and_partition)
18855 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
18856 current_function_funcdef_no);
18857 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
18858 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
18859 current_function_funcdef_no);
18860 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
18862 #if VMS_DEBUGGING_INFO
18863 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18864 Section 2.3 Prologue and Epilogue Attributes:
18865 When a breakpoint is set on entry to a function, it is generally
18866 desirable for execution to be suspended, not on the very first
18867 instruction of the function, but rather at a point after the
18868 function's frame has been set up, after any language defined local
18869 declaration processing has been completed, and before execution of
18870 the first statement of the function begins. Debuggers generally
18871 cannot properly determine where this point is. Similarly for a
18872 breakpoint set on exit from a function. The prologue and epilogue
18873 attributes allow a compiler to communicate the location(s) to use. */
18876 dw_fde_ref fde = &fde_table[current_funcdef_fde];
18878 if (fde->dw_fde_vms_end_prologue)
18879 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18880 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18882 if (fde->dw_fde_vms_begin_epilogue)
18883 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18884 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18886 #endif
18888 add_pubname (decl, subr_die);
18889 add_arange (decl, subr_die);
18891 else
18892 { /* Do nothing for now; maybe need to duplicate die, one for
18893 hot section and one for cold section, then use the hot/cold
18894 section begin/end labels to generate the aranges... */
18896 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
18897 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
18898 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
18899 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
18901 add_pubname (decl, subr_die);
18902 add_arange (decl, subr_die);
18903 add_arange (decl, subr_die);
18907 #ifdef MIPS_DEBUGGING_INFO
18908 /* Add a reference to the FDE for this routine. */
18909 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
18910 #endif
18912 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18914 /* We define the "frame base" as the function's CFA. This is more
18915 convenient for several reasons: (1) It's stable across the prologue
18916 and epilogue, which makes it better than just a frame pointer,
18917 (2) With dwarf3, there exists a one-byte encoding that allows us
18918 to reference the .debug_frame data by proxy, but failing that,
18919 (3) We can at least reuse the code inspection and interpretation
18920 code that determines the CFA position at various points in the
18921 function. */
18922 if (dwarf_version >= 3)
18924 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18925 add_AT_loc (subr_die, DW_AT_frame_base, op);
18927 else
18929 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18930 if (list->dw_loc_next)
18931 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18932 else
18933 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18936 /* Compute a displacement from the "steady-state frame pointer" to
18937 the CFA. The former is what all stack slots and argument slots
18938 will reference in the rtl; the later is what we've told the
18939 debugger about. We'll need to adjust all frame_base references
18940 by this displacement. */
18941 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18943 if (cfun->static_chain_decl)
18944 add_AT_location_description (subr_die, DW_AT_static_link,
18945 loc_list_from_tree (cfun->static_chain_decl, 2));
18948 /* Generate child dies for template paramaters. */
18949 if (debug_info_level > DINFO_LEVEL_TERSE)
18950 gen_generic_params_dies (decl);
18952 /* Now output descriptions of the arguments for this function. This gets
18953 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18954 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18955 `...' at the end of the formal parameter list. In order to find out if
18956 there was a trailing ellipsis or not, we must instead look at the type
18957 associated with the FUNCTION_DECL. This will be a node of type
18958 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18959 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18960 an ellipsis at the end. */
18962 /* In the case where we are describing a mere function declaration, all we
18963 need to do here (and all we *can* do here) is to describe the *types* of
18964 its formal parameters. */
18965 if (debug_info_level <= DINFO_LEVEL_TERSE)
18967 else if (declaration)
18968 gen_formal_types_die (decl, subr_die);
18969 else
18971 /* Generate DIEs to represent all known formal parameters. */
18972 tree parm = DECL_ARGUMENTS (decl);
18973 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18974 tree generic_decl_parm = generic_decl
18975 ? DECL_ARGUMENTS (generic_decl)
18976 : NULL;
18978 /* Now we want to walk the list of parameters of the function and
18979 emit their relevant DIEs.
18981 We consider the case of DECL being an instance of a generic function
18982 as well as it being a normal function.
18984 If DECL is an instance of a generic function we walk the
18985 parameters of the generic function declaration _and_ the parameters of
18986 DECL itself. This is useful because we want to emit specific DIEs for
18987 function parameter packs and those are declared as part of the
18988 generic function declaration. In that particular case,
18989 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18990 That DIE has children DIEs representing the set of arguments
18991 of the pack. Note that the set of pack arguments can be empty.
18992 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18993 children DIE.
18995 Otherwise, we just consider the parameters of DECL. */
18996 while (generic_decl_parm || parm)
18998 if (generic_decl_parm
18999 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19000 gen_formal_parameter_pack_die (generic_decl_parm,
19001 parm, subr_die,
19002 &parm);
19003 else if (parm)
19005 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19007 if (parm == DECL_ARGUMENTS (decl)
19008 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19009 && parm_die
19010 && (dwarf_version >= 3 || !dwarf_strict))
19011 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19013 parm = DECL_CHAIN (parm);
19016 if (generic_decl_parm)
19017 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19020 /* Decide whether we need an unspecified_parameters DIE at the end.
19021 There are 2 more cases to do this for: 1) the ansi ... declaration -
19022 this is detectable when the end of the arg list is not a
19023 void_type_node 2) an unprototyped function declaration (not a
19024 definition). This just means that we have no info about the
19025 parameters at all. */
19026 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
19027 if (fn_arg_types != NULL)
19029 /* This is the prototyped case, check for.... */
19030 if (stdarg_p (TREE_TYPE (decl)))
19031 gen_unspecified_parameters_die (decl, subr_die);
19033 else if (DECL_INITIAL (decl) == NULL_TREE)
19034 gen_unspecified_parameters_die (decl, subr_die);
19037 /* Output Dwarf info for all of the stuff within the body of the function
19038 (if it has one - it may be just a declaration). */
19039 outer_scope = DECL_INITIAL (decl);
19041 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19042 a function. This BLOCK actually represents the outermost binding contour
19043 for the function, i.e. the contour in which the function's formal
19044 parameters and labels get declared. Curiously, it appears that the front
19045 end doesn't actually put the PARM_DECL nodes for the current function onto
19046 the BLOCK_VARS list for this outer scope, but are strung off of the
19047 DECL_ARGUMENTS list for the function instead.
19049 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19050 the LABEL_DECL nodes for the function however, and we output DWARF info
19051 for those in decls_for_scope. Just within the `outer_scope' there will be
19052 a BLOCK node representing the function's outermost pair of curly braces,
19053 and any blocks used for the base and member initializers of a C++
19054 constructor function. */
19055 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19057 /* Emit a DW_TAG_variable DIE for a named return value. */
19058 if (DECL_NAME (DECL_RESULT (decl)))
19059 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19061 current_function_has_inlines = 0;
19062 decls_for_scope (outer_scope, subr_die, 0);
19064 /* Add the calling convention attribute if requested. */
19065 add_calling_convention_attribute (subr_die, decl);
19069 /* Returns a hash value for X (which really is a die_struct). */
19071 static hashval_t
19072 common_block_die_table_hash (const void *x)
19074 const_dw_die_ref d = (const_dw_die_ref) x;
19075 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19078 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19079 as decl_id and die_parent of die_struct Y. */
19081 static int
19082 common_block_die_table_eq (const void *x, const void *y)
19084 const_dw_die_ref d = (const_dw_die_ref) x;
19085 const_dw_die_ref e = (const_dw_die_ref) y;
19086 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19089 /* Generate a DIE to represent a declared data object.
19090 Either DECL or ORIGIN must be non-null. */
19092 static void
19093 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19095 HOST_WIDE_INT off;
19096 tree com_decl;
19097 tree decl_or_origin = decl ? decl : origin;
19098 tree ultimate_origin;
19099 dw_die_ref var_die;
19100 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19101 dw_die_ref origin_die;
19102 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19103 || class_or_namespace_scope_p (context_die));
19104 bool specialization_p = false;
19106 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19107 if (decl || ultimate_origin)
19108 origin = ultimate_origin;
19109 com_decl = fortran_common (decl_or_origin, &off);
19111 /* Symbol in common gets emitted as a child of the common block, in the form
19112 of a data member. */
19113 if (com_decl)
19115 dw_die_ref com_die;
19116 dw_loc_list_ref loc;
19117 die_node com_die_arg;
19119 var_die = lookup_decl_die (decl_or_origin);
19120 if (var_die)
19122 if (get_AT (var_die, DW_AT_location) == NULL)
19124 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19125 if (loc)
19127 if (off)
19129 /* Optimize the common case. */
19130 if (single_element_loc_list_p (loc)
19131 && loc->expr->dw_loc_opc == DW_OP_addr
19132 && loc->expr->dw_loc_next == NULL
19133 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19134 == SYMBOL_REF)
19135 loc->expr->dw_loc_oprnd1.v.val_addr
19136 = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19137 else
19138 loc_list_plus_const (loc, off);
19140 add_AT_location_description (var_die, DW_AT_location, loc);
19141 remove_AT (var_die, DW_AT_declaration);
19144 return;
19147 if (common_block_die_table == NULL)
19148 common_block_die_table
19149 = htab_create_ggc (10, common_block_die_table_hash,
19150 common_block_die_table_eq, NULL);
19152 com_die_arg.decl_id = DECL_UID (com_decl);
19153 com_die_arg.die_parent = context_die;
19154 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19155 loc = loc_list_from_tree (com_decl, 2);
19156 if (com_die == NULL)
19158 const char *cnam
19159 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19160 void **slot;
19162 com_die = new_die (DW_TAG_common_block, context_die, decl);
19163 add_name_and_src_coords_attributes (com_die, com_decl);
19164 if (loc)
19166 add_AT_location_description (com_die, DW_AT_location, loc);
19167 /* Avoid sharing the same loc descriptor between
19168 DW_TAG_common_block and DW_TAG_variable. */
19169 loc = loc_list_from_tree (com_decl, 2);
19171 else if (DECL_EXTERNAL (decl))
19172 add_AT_flag (com_die, DW_AT_declaration, 1);
19173 add_pubname_string (cnam, com_die); /* ??? needed? */
19174 com_die->decl_id = DECL_UID (com_decl);
19175 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19176 *slot = (void *) com_die;
19178 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19180 add_AT_location_description (com_die, DW_AT_location, loc);
19181 loc = loc_list_from_tree (com_decl, 2);
19182 remove_AT (com_die, DW_AT_declaration);
19184 var_die = new_die (DW_TAG_variable, com_die, decl);
19185 add_name_and_src_coords_attributes (var_die, decl);
19186 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19187 TREE_THIS_VOLATILE (decl), context_die);
19188 add_AT_flag (var_die, DW_AT_external, 1);
19189 if (loc)
19191 if (off)
19193 /* Optimize the common case. */
19194 if (single_element_loc_list_p (loc)
19195 && loc->expr->dw_loc_opc == DW_OP_addr
19196 && loc->expr->dw_loc_next == NULL
19197 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19198 loc->expr->dw_loc_oprnd1.v.val_addr
19199 = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19200 else
19201 loc_list_plus_const (loc, off);
19203 add_AT_location_description (var_die, DW_AT_location, loc);
19205 else if (DECL_EXTERNAL (decl))
19206 add_AT_flag (var_die, DW_AT_declaration, 1);
19207 equate_decl_number_to_die (decl, var_die);
19208 return;
19211 /* If the compiler emitted a definition for the DECL declaration
19212 and if we already emitted a DIE for it, don't emit a second
19213 DIE for it again. Allow re-declarations of DECLs that are
19214 inside functions, though. */
19215 if (old_die && declaration && !local_scope_p (context_die))
19216 return;
19218 /* For static data members, the declaration in the class is supposed
19219 to have DW_TAG_member tag; the specification should still be
19220 DW_TAG_variable referencing the DW_TAG_member DIE. */
19221 if (declaration && class_scope_p (context_die))
19222 var_die = new_die (DW_TAG_member, context_die, decl);
19223 else
19224 var_die = new_die (DW_TAG_variable, context_die, decl);
19226 origin_die = NULL;
19227 if (origin != NULL)
19228 origin_die = add_abstract_origin_attribute (var_die, origin);
19230 /* Loop unrolling can create multiple blocks that refer to the same
19231 static variable, so we must test for the DW_AT_declaration flag.
19233 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19234 copy decls and set the DECL_ABSTRACT flag on them instead of
19235 sharing them.
19237 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19239 ??? The declare_in_namespace support causes us to get two DIEs for one
19240 variable, both of which are declarations. We want to avoid considering
19241 one to be a specification, so we must test that this DIE is not a
19242 declaration. */
19243 else if (old_die && TREE_STATIC (decl) && ! declaration
19244 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19246 /* This is a definition of a C++ class level static. */
19247 add_AT_specification (var_die, old_die);
19248 specialization_p = true;
19249 if (DECL_NAME (decl))
19251 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19252 struct dwarf_file_data * file_index = lookup_filename (s.file);
19254 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19255 add_AT_file (var_die, DW_AT_decl_file, file_index);
19257 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19258 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19260 if (old_die->die_tag == DW_TAG_member)
19261 add_linkage_name (var_die, decl);
19264 else
19265 add_name_and_src_coords_attributes (var_die, decl);
19267 if ((origin == NULL && !specialization_p)
19268 || (origin != NULL
19269 && !DECL_ABSTRACT (decl_or_origin)
19270 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19271 decl_function_context
19272 (decl_or_origin))))
19274 tree type = TREE_TYPE (decl_or_origin);
19276 if (decl_by_reference_p (decl_or_origin))
19277 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19278 else
19279 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19280 TREE_THIS_VOLATILE (decl_or_origin), context_die);
19283 if (origin == NULL && !specialization_p)
19285 if (TREE_PUBLIC (decl))
19286 add_AT_flag (var_die, DW_AT_external, 1);
19288 if (DECL_ARTIFICIAL (decl))
19289 add_AT_flag (var_die, DW_AT_artificial, 1);
19291 add_accessibility_attribute (var_die, decl);
19294 if (declaration)
19295 add_AT_flag (var_die, DW_AT_declaration, 1);
19297 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19298 equate_decl_number_to_die (decl, var_die);
19300 if (! declaration
19301 && (! DECL_ABSTRACT (decl_or_origin)
19302 /* Local static vars are shared between all clones/inlines,
19303 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19304 already set. */
19305 || (TREE_CODE (decl_or_origin) == VAR_DECL
19306 && TREE_STATIC (decl_or_origin)
19307 && DECL_RTL_SET_P (decl_or_origin)))
19308 /* When abstract origin already has DW_AT_location attribute, no need
19309 to add it again. */
19310 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19312 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19313 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19314 defer_location (decl_or_origin, var_die);
19315 else
19316 add_location_or_const_value_attribute (var_die,
19317 decl_or_origin,
19318 DW_AT_location);
19319 add_pubname (decl_or_origin, var_die);
19321 else
19322 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19325 /* Generate a DIE to represent a named constant. */
19327 static void
19328 gen_const_die (tree decl, dw_die_ref context_die)
19330 dw_die_ref const_die;
19331 tree type = TREE_TYPE (decl);
19333 const_die = new_die (DW_TAG_constant, context_die, decl);
19334 add_name_and_src_coords_attributes (const_die, decl);
19335 add_type_attribute (const_die, type, 1, 0, context_die);
19336 if (TREE_PUBLIC (decl))
19337 add_AT_flag (const_die, DW_AT_external, 1);
19338 if (DECL_ARTIFICIAL (decl))
19339 add_AT_flag (const_die, DW_AT_artificial, 1);
19340 tree_add_const_value_attribute_for_decl (const_die, decl);
19343 /* Generate a DIE to represent a label identifier. */
19345 static void
19346 gen_label_die (tree decl, dw_die_ref context_die)
19348 tree origin = decl_ultimate_origin (decl);
19349 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19350 rtx insn;
19351 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19353 if (origin != NULL)
19354 add_abstract_origin_attribute (lbl_die, origin);
19355 else
19356 add_name_and_src_coords_attributes (lbl_die, decl);
19358 if (DECL_ABSTRACT (decl))
19359 equate_decl_number_to_die (decl, lbl_die);
19360 else
19362 insn = DECL_RTL_IF_SET (decl);
19364 /* Deleted labels are programmer specified labels which have been
19365 eliminated because of various optimizations. We still emit them
19366 here so that it is possible to put breakpoints on them. */
19367 if (insn
19368 && (LABEL_P (insn)
19369 || ((NOTE_P (insn)
19370 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19372 /* When optimization is enabled (via -O) some parts of the compiler
19373 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19374 represent source-level labels which were explicitly declared by
19375 the user. This really shouldn't be happening though, so catch
19376 it if it ever does happen. */
19377 gcc_assert (!INSN_DELETED_P (insn));
19379 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19380 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19385 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19386 attributes to the DIE for a block STMT, to describe where the inlined
19387 function was called from. This is similar to add_src_coords_attributes. */
19389 static inline void
19390 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19392 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19394 if (dwarf_version >= 3 || !dwarf_strict)
19396 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19397 add_AT_unsigned (die, DW_AT_call_line, s.line);
19402 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19403 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19405 static inline void
19406 add_high_low_attributes (tree stmt, dw_die_ref die)
19408 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19410 if (BLOCK_FRAGMENT_CHAIN (stmt)
19411 && (dwarf_version >= 3 || !dwarf_strict))
19413 tree chain;
19415 if (inlined_function_outer_scope_p (stmt))
19417 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19418 BLOCK_NUMBER (stmt));
19419 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19422 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19424 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19427 add_ranges (chain);
19428 chain = BLOCK_FRAGMENT_CHAIN (chain);
19430 while (chain);
19431 add_ranges (NULL);
19433 else
19435 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19436 BLOCK_NUMBER (stmt));
19437 add_AT_lbl_id (die, DW_AT_low_pc, label);
19438 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19439 BLOCK_NUMBER (stmt));
19440 add_AT_lbl_id (die, DW_AT_high_pc, label);
19444 /* Generate a DIE for a lexical block. */
19446 static void
19447 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19449 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19451 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19452 add_high_low_attributes (stmt, stmt_die);
19454 decls_for_scope (stmt, stmt_die, depth);
19457 /* Generate a DIE for an inlined subprogram. */
19459 static void
19460 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19462 tree decl;
19464 /* The instance of function that is effectively being inlined shall not
19465 be abstract. */
19466 gcc_assert (! BLOCK_ABSTRACT (stmt));
19468 decl = block_ultimate_origin (stmt);
19470 /* Emit info for the abstract instance first, if we haven't yet. We
19471 must emit this even if the block is abstract, otherwise when we
19472 emit the block below (or elsewhere), we may end up trying to emit
19473 a die whose origin die hasn't been emitted, and crashing. */
19474 dwarf2out_abstract_function (decl);
19476 if (! BLOCK_ABSTRACT (stmt))
19478 dw_die_ref subr_die
19479 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19481 add_abstract_origin_attribute (subr_die, decl);
19482 if (TREE_ASM_WRITTEN (stmt))
19483 add_high_low_attributes (stmt, subr_die);
19484 add_call_src_coords_attributes (stmt, subr_die);
19486 decls_for_scope (stmt, subr_die, depth);
19487 current_function_has_inlines = 1;
19491 /* Generate a DIE for a field in a record, or structure. */
19493 static void
19494 gen_field_die (tree decl, dw_die_ref context_die)
19496 dw_die_ref decl_die;
19498 if (TREE_TYPE (decl) == error_mark_node)
19499 return;
19501 decl_die = new_die (DW_TAG_member, context_die, decl);
19502 add_name_and_src_coords_attributes (decl_die, decl);
19503 add_type_attribute (decl_die, member_declared_type (decl),
19504 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19505 context_die);
19507 if (DECL_BIT_FIELD_TYPE (decl))
19509 add_byte_size_attribute (decl_die, decl);
19510 add_bit_size_attribute (decl_die, decl);
19511 add_bit_offset_attribute (decl_die, decl);
19514 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19515 add_data_member_location_attribute (decl_die, decl);
19517 if (DECL_ARTIFICIAL (decl))
19518 add_AT_flag (decl_die, DW_AT_artificial, 1);
19520 add_accessibility_attribute (decl_die, decl);
19522 /* Equate decl number to die, so that we can look up this decl later on. */
19523 equate_decl_number_to_die (decl, decl_die);
19526 #if 0
19527 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19528 Use modified_type_die instead.
19529 We keep this code here just in case these types of DIEs may be needed to
19530 represent certain things in other languages (e.g. Pascal) someday. */
19532 static void
19533 gen_pointer_type_die (tree type, dw_die_ref context_die)
19535 dw_die_ref ptr_die
19536 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19538 equate_type_number_to_die (type, ptr_die);
19539 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19540 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19543 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19544 Use modified_type_die instead.
19545 We keep this code here just in case these types of DIEs may be needed to
19546 represent certain things in other languages (e.g. Pascal) someday. */
19548 static void
19549 gen_reference_type_die (tree type, dw_die_ref context_die)
19551 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19553 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19554 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19555 else
19556 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19558 equate_type_number_to_die (type, ref_die);
19559 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19560 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19562 #endif
19564 /* Generate a DIE for a pointer to a member type. */
19566 static void
19567 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19569 dw_die_ref ptr_die
19570 = new_die (DW_TAG_ptr_to_member_type,
19571 scope_die_for (type, context_die), type);
19573 equate_type_number_to_die (type, ptr_die);
19574 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19575 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19576 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19579 /* Generate the DIE for the compilation unit. */
19581 static dw_die_ref
19582 gen_compile_unit_die (const char *filename)
19584 dw_die_ref die;
19585 char producer[250];
19586 const char *language_string = lang_hooks.name;
19587 int language;
19589 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19591 if (filename)
19593 add_name_attribute (die, filename);
19594 /* Don't add cwd for <built-in>. */
19595 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19596 add_comp_dir_attribute (die);
19599 sprintf (producer, "%s %s", language_string, version_string);
19601 #ifdef MIPS_DEBUGGING_INFO
19602 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19603 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19604 not appear in the producer string, the debugger reaches the conclusion
19605 that the object file is stripped and has no debugging information.
19606 To get the MIPS/SGI debugger to believe that there is debugging
19607 information in the object file, we add a -g to the producer string. */
19608 if (debug_info_level > DINFO_LEVEL_TERSE)
19609 strcat (producer, " -g");
19610 #endif
19612 add_AT_string (die, DW_AT_producer, producer);
19614 language = DW_LANG_C89;
19615 if (strcmp (language_string, "GNU C++") == 0)
19616 language = DW_LANG_C_plus_plus;
19617 else if (strcmp (language_string, "GNU F77") == 0)
19618 language = DW_LANG_Fortran77;
19619 else if (strcmp (language_string, "GNU Pascal") == 0)
19620 language = DW_LANG_Pascal83;
19621 else if (dwarf_version >= 3 || !dwarf_strict)
19623 if (strcmp (language_string, "GNU Ada") == 0)
19624 language = DW_LANG_Ada95;
19625 else if (strcmp (language_string, "GNU Fortran") == 0)
19626 language = DW_LANG_Fortran95;
19627 else if (strcmp (language_string, "GNU Java") == 0)
19628 language = DW_LANG_Java;
19629 else if (strcmp (language_string, "GNU Objective-C") == 0)
19630 language = DW_LANG_ObjC;
19631 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19632 language = DW_LANG_ObjC_plus_plus;
19635 add_AT_unsigned (die, DW_AT_language, language);
19637 switch (language)
19639 case DW_LANG_Fortran77:
19640 case DW_LANG_Fortran90:
19641 case DW_LANG_Fortran95:
19642 /* Fortran has case insensitive identifiers and the front-end
19643 lowercases everything. */
19644 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19645 break;
19646 default:
19647 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19648 break;
19650 return die;
19653 /* Generate the DIE for a base class. */
19655 static void
19656 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19658 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19660 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19661 add_data_member_location_attribute (die, binfo);
19663 if (BINFO_VIRTUAL_P (binfo))
19664 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19666 if (access == access_public_node)
19667 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19668 else if (access == access_protected_node)
19669 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19672 /* Generate a DIE for a class member. */
19674 static void
19675 gen_member_die (tree type, dw_die_ref context_die)
19677 tree member;
19678 tree binfo = TYPE_BINFO (type);
19679 dw_die_ref child;
19681 /* If this is not an incomplete type, output descriptions of each of its
19682 members. Note that as we output the DIEs necessary to represent the
19683 members of this record or union type, we will also be trying to output
19684 DIEs to represent the *types* of those members. However the `type'
19685 function (above) will specifically avoid generating type DIEs for member
19686 types *within* the list of member DIEs for this (containing) type except
19687 for those types (of members) which are explicitly marked as also being
19688 members of this (containing) type themselves. The g++ front- end can
19689 force any given type to be treated as a member of some other (containing)
19690 type by setting the TYPE_CONTEXT of the given (member) type to point to
19691 the TREE node representing the appropriate (containing) type. */
19693 /* First output info about the base classes. */
19694 if (binfo)
19696 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19697 int i;
19698 tree base;
19700 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19701 gen_inheritance_die (base,
19702 (accesses ? VEC_index (tree, accesses, i)
19703 : access_public_node), context_die);
19706 /* Now output info about the data members and type members. */
19707 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19709 /* If we thought we were generating minimal debug info for TYPE
19710 and then changed our minds, some of the member declarations
19711 may have already been defined. Don't define them again, but
19712 do put them in the right order. */
19714 child = lookup_decl_die (member);
19715 if (child)
19716 splice_child_die (context_die, child);
19717 else
19718 gen_decl_die (member, NULL, context_die);
19721 /* Now output info about the function members (if any). */
19722 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19724 /* Don't include clones in the member list. */
19725 if (DECL_ABSTRACT_ORIGIN (member))
19726 continue;
19728 child = lookup_decl_die (member);
19729 if (child)
19730 splice_child_die (context_die, child);
19731 else
19732 gen_decl_die (member, NULL, context_die);
19736 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19737 is set, we pretend that the type was never defined, so we only get the
19738 member DIEs needed by later specification DIEs. */
19740 static void
19741 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19742 enum debug_info_usage usage)
19744 dw_die_ref type_die = lookup_type_die (type);
19745 dw_die_ref scope_die = 0;
19746 int nested = 0;
19747 int complete = (TYPE_SIZE (type)
19748 && (! TYPE_STUB_DECL (type)
19749 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19750 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19751 complete = complete && should_emit_struct_debug (type, usage);
19753 if (type_die && ! complete)
19754 return;
19756 if (TYPE_CONTEXT (type) != NULL_TREE
19757 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19758 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19759 nested = 1;
19761 scope_die = scope_die_for (type, context_die);
19763 if (! type_die || (nested && scope_die == comp_unit_die))
19764 /* First occurrence of type or toplevel definition of nested class. */
19766 dw_die_ref old_die = type_die;
19768 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19769 ? record_type_tag (type) : DW_TAG_union_type,
19770 scope_die, type);
19771 equate_type_number_to_die (type, type_die);
19772 if (old_die)
19773 add_AT_specification (type_die, old_die);
19774 else
19775 add_name_attribute (type_die, type_tag (type));
19777 else
19778 remove_AT (type_die, DW_AT_declaration);
19780 /* Generate child dies for template paramaters. */
19781 if (debug_info_level > DINFO_LEVEL_TERSE
19782 && COMPLETE_TYPE_P (type))
19783 gen_generic_params_dies (type);
19785 /* If this type has been completed, then give it a byte_size attribute and
19786 then give a list of members. */
19787 if (complete && !ns_decl)
19789 /* Prevent infinite recursion in cases where the type of some member of
19790 this type is expressed in terms of this type itself. */
19791 TREE_ASM_WRITTEN (type) = 1;
19792 add_byte_size_attribute (type_die, type);
19793 if (TYPE_STUB_DECL (type) != NULL_TREE)
19795 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19796 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19799 /* If the first reference to this type was as the return type of an
19800 inline function, then it may not have a parent. Fix this now. */
19801 if (type_die->die_parent == NULL)
19802 add_child_die (scope_die, type_die);
19804 push_decl_scope (type);
19805 gen_member_die (type, type_die);
19806 pop_decl_scope ();
19808 /* GNU extension: Record what type our vtable lives in. */
19809 if (TYPE_VFIELD (type))
19811 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19813 gen_type_die (vtype, context_die);
19814 add_AT_die_ref (type_die, DW_AT_containing_type,
19815 lookup_type_die (vtype));
19818 else
19820 add_AT_flag (type_die, DW_AT_declaration, 1);
19822 /* We don't need to do this for function-local types. */
19823 if (TYPE_STUB_DECL (type)
19824 && ! decl_function_context (TYPE_STUB_DECL (type)))
19825 VEC_safe_push (tree, gc, incomplete_types, type);
19828 if (get_AT (type_die, DW_AT_name))
19829 add_pubtype (type, type_die);
19832 /* Generate a DIE for a subroutine _type_. */
19834 static void
19835 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19837 tree return_type = TREE_TYPE (type);
19838 dw_die_ref subr_die
19839 = new_die (DW_TAG_subroutine_type,
19840 scope_die_for (type, context_die), type);
19842 equate_type_number_to_die (type, subr_die);
19843 add_prototyped_attribute (subr_die, type);
19844 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19845 gen_formal_types_die (type, subr_die);
19847 if (get_AT (subr_die, DW_AT_name))
19848 add_pubtype (type, subr_die);
19851 /* Generate a DIE for a type definition. */
19853 static void
19854 gen_typedef_die (tree decl, dw_die_ref context_die)
19856 dw_die_ref type_die;
19857 tree origin;
19859 if (TREE_ASM_WRITTEN (decl))
19860 return;
19862 TREE_ASM_WRITTEN (decl) = 1;
19863 type_die = new_die (DW_TAG_typedef, context_die, decl);
19864 origin = decl_ultimate_origin (decl);
19865 if (origin != NULL)
19866 add_abstract_origin_attribute (type_die, origin);
19867 else
19869 tree type;
19871 add_name_and_src_coords_attributes (type_die, decl);
19872 if (DECL_ORIGINAL_TYPE (decl))
19874 type = DECL_ORIGINAL_TYPE (decl);
19876 gcc_assert (type != TREE_TYPE (decl));
19877 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19879 else
19881 type = TREE_TYPE (decl);
19883 if (is_naming_typedef_decl (TYPE_NAME (type)))
19885 /* Here, we are in the case of decl being a typedef naming
19886 an anonymous type, e.g:
19887 typedef struct {...} foo;
19888 In that case TREE_TYPE (decl) is not a typedef variant
19889 type and TYPE_NAME of the anonymous type is set to the
19890 TYPE_DECL of the typedef. This construct is emitted by
19891 the C++ FE.
19893 TYPE is the anonymous struct named by the typedef
19894 DECL. As we need the DW_AT_type attribute of the
19895 DW_TAG_typedef to point to the DIE of TYPE, let's
19896 generate that DIE right away. add_type_attribute
19897 called below will then pick (via lookup_type_die) that
19898 anonymous struct DIE. */
19899 if (!TREE_ASM_WRITTEN (type))
19900 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19904 add_type_attribute (type_die, type, TREE_READONLY (decl),
19905 TREE_THIS_VOLATILE (decl), context_die);
19907 if (is_naming_typedef_decl (decl))
19908 /* We want that all subsequent calls to lookup_type_die with
19909 TYPE in argument yield the DW_TAG_typedef we have just
19910 created. */
19911 equate_type_number_to_die (type, type_die);
19913 add_accessibility_attribute (type_die, decl);
19916 if (DECL_ABSTRACT (decl))
19917 equate_decl_number_to_die (decl, type_die);
19919 if (get_AT (type_die, DW_AT_name))
19920 add_pubtype (decl, type_die);
19923 /* Generate a DIE for a struct, class, enum or union type. */
19925 static void
19926 gen_tagged_type_die (tree type,
19927 dw_die_ref context_die,
19928 enum debug_info_usage usage)
19930 int need_pop;
19932 if (type == NULL_TREE
19933 || !is_tagged_type (type))
19934 return;
19936 /* If this is a nested type whose containing class hasn't been written
19937 out yet, writing it out will cover this one, too. This does not apply
19938 to instantiations of member class templates; they need to be added to
19939 the containing class as they are generated. FIXME: This hurts the
19940 idea of combining type decls from multiple TUs, since we can't predict
19941 what set of template instantiations we'll get. */
19942 if (TYPE_CONTEXT (type)
19943 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19944 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19946 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19948 if (TREE_ASM_WRITTEN (type))
19949 return;
19951 /* If that failed, attach ourselves to the stub. */
19952 push_decl_scope (TYPE_CONTEXT (type));
19953 context_die = lookup_type_die (TYPE_CONTEXT (type));
19954 need_pop = 1;
19956 else if (TYPE_CONTEXT (type) != NULL_TREE
19957 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19959 /* If this type is local to a function that hasn't been written
19960 out yet, use a NULL context for now; it will be fixed up in
19961 decls_for_scope. */
19962 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19963 need_pop = 0;
19965 else
19967 context_die = declare_in_namespace (type, context_die);
19968 need_pop = 0;
19971 if (TREE_CODE (type) == ENUMERAL_TYPE)
19973 /* This might have been written out by the call to
19974 declare_in_namespace. */
19975 if (!TREE_ASM_WRITTEN (type))
19976 gen_enumeration_type_die (type, context_die);
19978 else
19979 gen_struct_or_union_type_die (type, context_die, usage);
19981 if (need_pop)
19982 pop_decl_scope ();
19984 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19985 it up if it is ever completed. gen_*_type_die will set it for us
19986 when appropriate. */
19989 /* Generate a type description DIE. */
19991 static void
19992 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19993 enum debug_info_usage usage)
19995 struct array_descr_info info;
19997 if (type == NULL_TREE || type == error_mark_node)
19998 return;
20000 /* If TYPE is a typedef type variant, let's generate debug info
20001 for the parent typedef which TYPE is a type of. */
20002 if (typedef_variant_p (type))
20004 if (TREE_ASM_WRITTEN (type))
20005 return;
20007 /* Prevent broken recursion; we can't hand off to the same type. */
20008 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20010 /* Use the DIE of the containing namespace as the parent DIE of
20011 the type description DIE we want to generate. */
20012 if (DECL_CONTEXT (TYPE_NAME (type))
20013 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20014 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20016 TREE_ASM_WRITTEN (type) = 1;
20018 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20019 return;
20022 /* If type is an anonymous tagged type named by a typedef, let's
20023 generate debug info for the typedef. */
20024 if (is_naming_typedef_decl (TYPE_NAME (type)))
20026 /* Use the DIE of the containing namespace as the parent DIE of
20027 the type description DIE we want to generate. */
20028 if (DECL_CONTEXT (TYPE_NAME (type))
20029 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20030 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20032 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20033 return;
20036 /* If this is an array type with hidden descriptor, handle it first. */
20037 if (!TREE_ASM_WRITTEN (type)
20038 && lang_hooks.types.get_array_descr_info
20039 && lang_hooks.types.get_array_descr_info (type, &info)
20040 && (dwarf_version >= 3 || !dwarf_strict))
20042 gen_descr_array_type_die (type, &info, context_die);
20043 TREE_ASM_WRITTEN (type) = 1;
20044 return;
20047 /* We are going to output a DIE to represent the unqualified version
20048 of this type (i.e. without any const or volatile qualifiers) so
20049 get the main variant (i.e. the unqualified version) of this type
20050 now. (Vectors are special because the debugging info is in the
20051 cloned type itself). */
20052 if (TREE_CODE (type) != VECTOR_TYPE)
20053 type = type_main_variant (type);
20055 if (TREE_ASM_WRITTEN (type))
20056 return;
20058 switch (TREE_CODE (type))
20060 case ERROR_MARK:
20061 break;
20063 case POINTER_TYPE:
20064 case REFERENCE_TYPE:
20065 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20066 ensures that the gen_type_die recursion will terminate even if the
20067 type is recursive. Recursive types are possible in Ada. */
20068 /* ??? We could perhaps do this for all types before the switch
20069 statement. */
20070 TREE_ASM_WRITTEN (type) = 1;
20072 /* For these types, all that is required is that we output a DIE (or a
20073 set of DIEs) to represent the "basis" type. */
20074 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20075 DINFO_USAGE_IND_USE);
20076 break;
20078 case OFFSET_TYPE:
20079 /* This code is used for C++ pointer-to-data-member types.
20080 Output a description of the relevant class type. */
20081 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20082 DINFO_USAGE_IND_USE);
20084 /* Output a description of the type of the object pointed to. */
20085 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20086 DINFO_USAGE_IND_USE);
20088 /* Now output a DIE to represent this pointer-to-data-member type
20089 itself. */
20090 gen_ptr_to_mbr_type_die (type, context_die);
20091 break;
20093 case FUNCTION_TYPE:
20094 /* Force out return type (in case it wasn't forced out already). */
20095 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20096 DINFO_USAGE_DIR_USE);
20097 gen_subroutine_type_die (type, context_die);
20098 break;
20100 case METHOD_TYPE:
20101 /* Force out return type (in case it wasn't forced out already). */
20102 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20103 DINFO_USAGE_DIR_USE);
20104 gen_subroutine_type_die (type, context_die);
20105 break;
20107 case ARRAY_TYPE:
20108 gen_array_type_die (type, context_die);
20109 break;
20111 case VECTOR_TYPE:
20112 gen_array_type_die (type, context_die);
20113 break;
20115 case ENUMERAL_TYPE:
20116 case RECORD_TYPE:
20117 case UNION_TYPE:
20118 case QUAL_UNION_TYPE:
20119 gen_tagged_type_die (type, context_die, usage);
20120 return;
20122 case VOID_TYPE:
20123 case INTEGER_TYPE:
20124 case REAL_TYPE:
20125 case FIXED_POINT_TYPE:
20126 case COMPLEX_TYPE:
20127 case BOOLEAN_TYPE:
20128 /* No DIEs needed for fundamental types. */
20129 break;
20131 case LANG_TYPE:
20132 /* Just use DW_TAG_unspecified_type. */
20134 dw_die_ref type_die = lookup_type_die (type);
20135 if (type_die == NULL)
20137 tree name = TYPE_NAME (type);
20138 if (TREE_CODE (name) == TYPE_DECL)
20139 name = DECL_NAME (name);
20140 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die, type);
20141 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20142 equate_type_number_to_die (type, type_die);
20145 break;
20147 default:
20148 gcc_unreachable ();
20151 TREE_ASM_WRITTEN (type) = 1;
20154 static void
20155 gen_type_die (tree type, dw_die_ref context_die)
20157 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20160 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20161 things which are local to the given block. */
20163 static void
20164 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20166 int must_output_die = 0;
20167 bool inlined_func;
20169 /* Ignore blocks that are NULL. */
20170 if (stmt == NULL_TREE)
20171 return;
20173 inlined_func = inlined_function_outer_scope_p (stmt);
20175 /* If the block is one fragment of a non-contiguous block, do not
20176 process the variables, since they will have been done by the
20177 origin block. Do process subblocks. */
20178 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20180 tree sub;
20182 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20183 gen_block_die (sub, context_die, depth + 1);
20185 return;
20188 /* Determine if we need to output any Dwarf DIEs at all to represent this
20189 block. */
20190 if (inlined_func)
20191 /* The outer scopes for inlinings *must* always be represented. We
20192 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20193 must_output_die = 1;
20194 else
20196 /* Determine if this block directly contains any "significant"
20197 local declarations which we will need to output DIEs for. */
20198 if (debug_info_level > DINFO_LEVEL_TERSE)
20199 /* We are not in terse mode so *any* local declaration counts
20200 as being a "significant" one. */
20201 must_output_die = ((BLOCK_VARS (stmt) != NULL
20202 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20203 && (TREE_USED (stmt)
20204 || TREE_ASM_WRITTEN (stmt)
20205 || BLOCK_ABSTRACT (stmt)));
20206 else if ((TREE_USED (stmt)
20207 || TREE_ASM_WRITTEN (stmt)
20208 || BLOCK_ABSTRACT (stmt))
20209 && !dwarf2out_ignore_block (stmt))
20210 must_output_die = 1;
20213 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20214 DIE for any block which contains no significant local declarations at
20215 all. Rather, in such cases we just call `decls_for_scope' so that any
20216 needed Dwarf info for any sub-blocks will get properly generated. Note
20217 that in terse mode, our definition of what constitutes a "significant"
20218 local declaration gets restricted to include only inlined function
20219 instances and local (nested) function definitions. */
20220 if (must_output_die)
20222 if (inlined_func)
20224 /* If STMT block is abstract, that means we have been called
20225 indirectly from dwarf2out_abstract_function.
20226 That function rightfully marks the descendent blocks (of
20227 the abstract function it is dealing with) as being abstract,
20228 precisely to prevent us from emitting any
20229 DW_TAG_inlined_subroutine DIE as a descendent
20230 of an abstract function instance. So in that case, we should
20231 not call gen_inlined_subroutine_die.
20233 Later though, when cgraph asks dwarf2out to emit info
20234 for the concrete instance of the function decl into which
20235 the concrete instance of STMT got inlined, the later will lead
20236 to the generation of a DW_TAG_inlined_subroutine DIE. */
20237 if (! BLOCK_ABSTRACT (stmt))
20238 gen_inlined_subroutine_die (stmt, context_die, depth);
20240 else
20241 gen_lexical_block_die (stmt, context_die, depth);
20243 else
20244 decls_for_scope (stmt, context_die, depth);
20247 /* Process variable DECL (or variable with origin ORIGIN) within
20248 block STMT and add it to CONTEXT_DIE. */
20249 static void
20250 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20252 dw_die_ref die;
20253 tree decl_or_origin = decl ? decl : origin;
20255 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20256 die = lookup_decl_die (decl_or_origin);
20257 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20258 && TYPE_DECL_IS_STUB (decl_or_origin))
20259 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20260 else
20261 die = NULL;
20263 if (die != NULL && die->die_parent == NULL)
20264 add_child_die (context_die, die);
20265 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20266 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20267 stmt, context_die);
20268 else
20269 gen_decl_die (decl, origin, context_die);
20272 /* Generate all of the decls declared within a given scope and (recursively)
20273 all of its sub-blocks. */
20275 static void
20276 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20278 tree decl;
20279 unsigned int i;
20280 tree subblocks;
20282 /* Ignore NULL blocks. */
20283 if (stmt == NULL_TREE)
20284 return;
20286 /* Output the DIEs to represent all of the data objects and typedefs
20287 declared directly within this block but not within any nested
20288 sub-blocks. Also, nested function and tag DIEs have been
20289 generated with a parent of NULL; fix that up now. */
20290 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20291 process_scope_var (stmt, decl, NULL_TREE, context_die);
20292 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20293 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20294 context_die);
20296 /* If we're at -g1, we're not interested in subblocks. */
20297 if (debug_info_level <= DINFO_LEVEL_TERSE)
20298 return;
20300 /* Output the DIEs to represent all sub-blocks (and the items declared
20301 therein) of this block. */
20302 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20303 subblocks != NULL;
20304 subblocks = BLOCK_CHAIN (subblocks))
20305 gen_block_die (subblocks, context_die, depth + 1);
20308 /* Is this a typedef we can avoid emitting? */
20310 static inline int
20311 is_redundant_typedef (const_tree decl)
20313 if (TYPE_DECL_IS_STUB (decl))
20314 return 1;
20316 if (DECL_ARTIFICIAL (decl)
20317 && DECL_CONTEXT (decl)
20318 && is_tagged_type (DECL_CONTEXT (decl))
20319 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20320 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20321 /* Also ignore the artificial member typedef for the class name. */
20322 return 1;
20324 return 0;
20327 /* Return TRUE if TYPE is a typedef that names a type for linkage
20328 purposes. This kind of typedefs is produced by the C++ FE for
20329 constructs like:
20331 typedef struct {...} foo;
20333 In that case, there is no typedef variant type produced for foo.
20334 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20335 struct type. */
20337 static bool
20338 is_naming_typedef_decl (const_tree decl)
20340 if (decl == NULL_TREE
20341 || TREE_CODE (decl) != TYPE_DECL
20342 || !is_tagged_type (TREE_TYPE (decl))
20343 || DECL_IS_BUILTIN (decl)
20344 || is_redundant_typedef (decl)
20345 /* It looks like Ada produces TYPE_DECLs that are very similar
20346 to C++ naming typedefs but that have different
20347 semantics. Let's be specific to c++ for now. */
20348 || !is_cxx ())
20349 return FALSE;
20351 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20352 && TYPE_NAME (TREE_TYPE (decl)) == decl
20353 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20354 != TYPE_NAME (TREE_TYPE (decl))));
20357 /* Returns the DIE for a context. */
20359 static inline dw_die_ref
20360 get_context_die (tree context)
20362 if (context)
20364 /* Find die that represents this context. */
20365 if (TYPE_P (context))
20366 return force_type_die (TYPE_MAIN_VARIANT (context));
20367 else
20368 return force_decl_die (context);
20370 return comp_unit_die;
20373 /* Returns the DIE for decl. A DIE will always be returned. */
20375 static dw_die_ref
20376 force_decl_die (tree decl)
20378 dw_die_ref decl_die;
20379 unsigned saved_external_flag;
20380 tree save_fn = NULL_TREE;
20381 decl_die = lookup_decl_die (decl);
20382 if (!decl_die)
20384 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20386 decl_die = lookup_decl_die (decl);
20387 if (decl_die)
20388 return decl_die;
20390 switch (TREE_CODE (decl))
20392 case FUNCTION_DECL:
20393 /* Clear current_function_decl, so that gen_subprogram_die thinks
20394 that this is a declaration. At this point, we just want to force
20395 declaration die. */
20396 save_fn = current_function_decl;
20397 current_function_decl = NULL_TREE;
20398 gen_subprogram_die (decl, context_die);
20399 current_function_decl = save_fn;
20400 break;
20402 case VAR_DECL:
20403 /* Set external flag to force declaration die. Restore it after
20404 gen_decl_die() call. */
20405 saved_external_flag = DECL_EXTERNAL (decl);
20406 DECL_EXTERNAL (decl) = 1;
20407 gen_decl_die (decl, NULL, context_die);
20408 DECL_EXTERNAL (decl) = saved_external_flag;
20409 break;
20411 case NAMESPACE_DECL:
20412 if (dwarf_version >= 3 || !dwarf_strict)
20413 dwarf2out_decl (decl);
20414 else
20415 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20416 decl_die = comp_unit_die;
20417 break;
20419 default:
20420 gcc_unreachable ();
20423 /* We should be able to find the DIE now. */
20424 if (!decl_die)
20425 decl_die = lookup_decl_die (decl);
20426 gcc_assert (decl_die);
20429 return decl_die;
20432 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20433 always returned. */
20435 static dw_die_ref
20436 force_type_die (tree type)
20438 dw_die_ref type_die;
20440 type_die = lookup_type_die (type);
20441 if (!type_die)
20443 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20445 type_die = modified_type_die (type, TYPE_READONLY (type),
20446 TYPE_VOLATILE (type), context_die);
20447 gcc_assert (type_die);
20449 return type_die;
20452 /* Force out any required namespaces to be able to output DECL,
20453 and return the new context_die for it, if it's changed. */
20455 static dw_die_ref
20456 setup_namespace_context (tree thing, dw_die_ref context_die)
20458 tree context = (DECL_P (thing)
20459 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20460 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20461 /* Force out the namespace. */
20462 context_die = force_decl_die (context);
20464 return context_die;
20467 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20468 type) within its namespace, if appropriate.
20470 For compatibility with older debuggers, namespace DIEs only contain
20471 declarations; all definitions are emitted at CU scope. */
20473 static dw_die_ref
20474 declare_in_namespace (tree thing, dw_die_ref context_die)
20476 dw_die_ref ns_context;
20478 if (debug_info_level <= DINFO_LEVEL_TERSE)
20479 return context_die;
20481 /* If this decl is from an inlined function, then don't try to emit it in its
20482 namespace, as we will get confused. It would have already been emitted
20483 when the abstract instance of the inline function was emitted anyways. */
20484 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20485 return context_die;
20487 ns_context = setup_namespace_context (thing, context_die);
20489 if (ns_context != context_die)
20491 if (is_fortran ())
20492 return ns_context;
20493 if (DECL_P (thing))
20494 gen_decl_die (thing, NULL, ns_context);
20495 else
20496 gen_type_die (thing, ns_context);
20498 return context_die;
20501 /* Generate a DIE for a namespace or namespace alias. */
20503 static void
20504 gen_namespace_die (tree decl, dw_die_ref context_die)
20506 dw_die_ref namespace_die;
20508 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20509 they are an alias of. */
20510 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20512 /* Output a real namespace or module. */
20513 context_die = setup_namespace_context (decl, comp_unit_die);
20514 namespace_die = new_die (is_fortran ()
20515 ? DW_TAG_module : DW_TAG_namespace,
20516 context_die, decl);
20517 /* For Fortran modules defined in different CU don't add src coords. */
20518 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20520 const char *name = dwarf2_name (decl, 0);
20521 if (name)
20522 add_name_attribute (namespace_die, name);
20524 else
20525 add_name_and_src_coords_attributes (namespace_die, decl);
20526 if (DECL_EXTERNAL (decl))
20527 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20528 equate_decl_number_to_die (decl, namespace_die);
20530 else
20532 /* Output a namespace alias. */
20534 /* Force out the namespace we are an alias of, if necessary. */
20535 dw_die_ref origin_die
20536 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20538 if (DECL_CONTEXT (decl) == NULL_TREE
20539 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20540 context_die = setup_namespace_context (decl, comp_unit_die);
20541 /* Now create the namespace alias DIE. */
20542 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20543 add_name_and_src_coords_attributes (namespace_die, decl);
20544 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20545 equate_decl_number_to_die (decl, namespace_die);
20549 /* Generate Dwarf debug information for a decl described by DECL.
20550 The return value is currently only meaningful for PARM_DECLs,
20551 for all other decls it returns NULL. */
20553 static dw_die_ref
20554 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20556 tree decl_or_origin = decl ? decl : origin;
20557 tree class_origin = NULL, ultimate_origin;
20559 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20560 return NULL;
20562 switch (TREE_CODE (decl_or_origin))
20564 case ERROR_MARK:
20565 break;
20567 case CONST_DECL:
20568 if (!is_fortran () && !is_ada ())
20570 /* The individual enumerators of an enum type get output when we output
20571 the Dwarf representation of the relevant enum type itself. */
20572 break;
20575 /* Emit its type. */
20576 gen_type_die (TREE_TYPE (decl), context_die);
20578 /* And its containing namespace. */
20579 context_die = declare_in_namespace (decl, context_die);
20581 gen_const_die (decl, context_die);
20582 break;
20584 case FUNCTION_DECL:
20585 /* Don't output any DIEs to represent mere function declarations,
20586 unless they are class members or explicit block externs. */
20587 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20588 && DECL_CONTEXT (decl_or_origin) == NULL_TREE
20589 && (current_function_decl == NULL_TREE
20590 || DECL_ARTIFICIAL (decl_or_origin)))
20591 break;
20593 #if 0
20594 /* FIXME */
20595 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20596 on local redeclarations of global functions. That seems broken. */
20597 if (current_function_decl != decl)
20598 /* This is only a declaration. */;
20599 #endif
20601 /* If we're emitting a clone, emit info for the abstract instance. */
20602 if (origin || DECL_ORIGIN (decl) != decl)
20603 dwarf2out_abstract_function (origin
20604 ? DECL_ORIGIN (origin)
20605 : DECL_ABSTRACT_ORIGIN (decl));
20607 /* If we're emitting an out-of-line copy of an inline function,
20608 emit info for the abstract instance and set up to refer to it. */
20609 else if (cgraph_function_possibly_inlined_p (decl)
20610 && ! DECL_ABSTRACT (decl)
20611 && ! class_or_namespace_scope_p (context_die)
20612 /* dwarf2out_abstract_function won't emit a die if this is just
20613 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20614 that case, because that works only if we have a die. */
20615 && DECL_INITIAL (decl) != NULL_TREE)
20617 dwarf2out_abstract_function (decl);
20618 set_decl_origin_self (decl);
20621 /* Otherwise we're emitting the primary DIE for this decl. */
20622 else if (debug_info_level > DINFO_LEVEL_TERSE)
20624 /* Before we describe the FUNCTION_DECL itself, make sure that we
20625 have its containing type. */
20626 if (!origin)
20627 origin = decl_class_context (decl);
20628 if (origin != NULL_TREE)
20629 gen_type_die (origin, context_die);
20631 /* And its return type. */
20632 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20634 /* And its virtual context. */
20635 if (DECL_VINDEX (decl) != NULL_TREE)
20636 gen_type_die (DECL_CONTEXT (decl), context_die);
20638 /* Make sure we have a member DIE for decl. */
20639 if (origin != NULL_TREE)
20640 gen_type_die_for_member (origin, decl, context_die);
20642 /* And its containing namespace. */
20643 context_die = declare_in_namespace (decl, context_die);
20646 /* Now output a DIE to represent the function itself. */
20647 if (decl)
20648 gen_subprogram_die (decl, context_die);
20649 break;
20651 case TYPE_DECL:
20652 /* If we are in terse mode, don't generate any DIEs to represent any
20653 actual typedefs. */
20654 if (debug_info_level <= DINFO_LEVEL_TERSE)
20655 break;
20657 /* In the special case of a TYPE_DECL node representing the declaration
20658 of some type tag, if the given TYPE_DECL is marked as having been
20659 instantiated from some other (original) TYPE_DECL node (e.g. one which
20660 was generated within the original definition of an inline function) we
20661 used to generate a special (abbreviated) DW_TAG_structure_type,
20662 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20663 should be actually referencing those DIEs, as variable DIEs with that
20664 type would be emitted already in the abstract origin, so it was always
20665 removed during unused type prunning. Don't add anything in this
20666 case. */
20667 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20668 break;
20670 if (is_redundant_typedef (decl))
20671 gen_type_die (TREE_TYPE (decl), context_die);
20672 else
20673 /* Output a DIE to represent the typedef itself. */
20674 gen_typedef_die (decl, context_die);
20675 break;
20677 case LABEL_DECL:
20678 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20679 gen_label_die (decl, context_die);
20680 break;
20682 case VAR_DECL:
20683 case RESULT_DECL:
20684 /* If we are in terse mode, don't generate any DIEs to represent any
20685 variable declarations or definitions. */
20686 if (debug_info_level <= DINFO_LEVEL_TERSE)
20687 break;
20689 /* Output any DIEs that are needed to specify the type of this data
20690 object. */
20691 if (decl_by_reference_p (decl_or_origin))
20692 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20693 else
20694 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20696 /* And its containing type. */
20697 class_origin = decl_class_context (decl_or_origin);
20698 if (class_origin != NULL_TREE)
20699 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20701 /* And its containing namespace. */
20702 context_die = declare_in_namespace (decl_or_origin, context_die);
20704 /* Now output the DIE to represent the data object itself. This gets
20705 complicated because of the possibility that the VAR_DECL really
20706 represents an inlined instance of a formal parameter for an inline
20707 function. */
20708 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20709 if (ultimate_origin != NULL_TREE
20710 && TREE_CODE (ultimate_origin) == PARM_DECL)
20711 gen_formal_parameter_die (decl, origin,
20712 true /* Emit name attribute. */,
20713 context_die);
20714 else
20715 gen_variable_die (decl, origin, context_die);
20716 break;
20718 case FIELD_DECL:
20719 /* Ignore the nameless fields that are used to skip bits but handle C++
20720 anonymous unions and structs. */
20721 if (DECL_NAME (decl) != NULL_TREE
20722 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20723 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20725 gen_type_die (member_declared_type (decl), context_die);
20726 gen_field_die (decl, context_die);
20728 break;
20730 case PARM_DECL:
20731 if (DECL_BY_REFERENCE (decl_or_origin))
20732 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20733 else
20734 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20735 return gen_formal_parameter_die (decl, origin,
20736 true /* Emit name attribute. */,
20737 context_die);
20739 case NAMESPACE_DECL:
20740 case IMPORTED_DECL:
20741 if (dwarf_version >= 3 || !dwarf_strict)
20742 gen_namespace_die (decl, context_die);
20743 break;
20745 default:
20746 /* Probably some frontend-internal decl. Assume we don't care. */
20747 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20748 break;
20751 return NULL;
20754 /* Output debug information for global decl DECL. Called from toplev.c after
20755 compilation proper has finished. */
20757 static void
20758 dwarf2out_global_decl (tree decl)
20760 /* Output DWARF2 information for file-scope tentative data object
20761 declarations, file-scope (extern) function declarations (which
20762 had no corresponding body) and file-scope tagged type declarations
20763 and definitions which have not yet been forced out. */
20764 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20765 dwarf2out_decl (decl);
20768 /* Output debug information for type decl DECL. Called from toplev.c
20769 and from language front ends (to record built-in types). */
20770 static void
20771 dwarf2out_type_decl (tree decl, int local)
20773 if (!local)
20774 dwarf2out_decl (decl);
20777 /* Output debug information for imported module or decl DECL.
20778 NAME is non-NULL name in the lexical block if the decl has been renamed.
20779 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20780 that DECL belongs to.
20781 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20782 static void
20783 dwarf2out_imported_module_or_decl_1 (tree decl,
20784 tree name,
20785 tree lexical_block,
20786 dw_die_ref lexical_block_die)
20788 expanded_location xloc;
20789 dw_die_ref imported_die = NULL;
20790 dw_die_ref at_import_die;
20792 if (TREE_CODE (decl) == IMPORTED_DECL)
20794 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20795 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20796 gcc_assert (decl);
20798 else
20799 xloc = expand_location (input_location);
20801 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20803 at_import_die = force_type_die (TREE_TYPE (decl));
20804 /* For namespace N { typedef void T; } using N::T; base_type_die
20805 returns NULL, but DW_TAG_imported_declaration requires
20806 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20807 if (!at_import_die)
20809 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20810 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20811 at_import_die = lookup_type_die (TREE_TYPE (decl));
20812 gcc_assert (at_import_die);
20815 else
20817 at_import_die = lookup_decl_die (decl);
20818 if (!at_import_die)
20820 /* If we're trying to avoid duplicate debug info, we may not have
20821 emitted the member decl for this field. Emit it now. */
20822 if (TREE_CODE (decl) == FIELD_DECL)
20824 tree type = DECL_CONTEXT (decl);
20826 if (TYPE_CONTEXT (type)
20827 && TYPE_P (TYPE_CONTEXT (type))
20828 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20829 DINFO_USAGE_DIR_USE))
20830 return;
20831 gen_type_die_for_member (type, decl,
20832 get_context_die (TYPE_CONTEXT (type)));
20834 at_import_die = force_decl_die (decl);
20838 if (TREE_CODE (decl) == NAMESPACE_DECL)
20840 if (dwarf_version >= 3 || !dwarf_strict)
20841 imported_die = new_die (DW_TAG_imported_module,
20842 lexical_block_die,
20843 lexical_block);
20844 else
20845 return;
20847 else
20848 imported_die = new_die (DW_TAG_imported_declaration,
20849 lexical_block_die,
20850 lexical_block);
20852 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20853 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20854 if (name)
20855 add_AT_string (imported_die, DW_AT_name,
20856 IDENTIFIER_POINTER (name));
20857 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20860 /* Output debug information for imported module or decl DECL.
20861 NAME is non-NULL name in context if the decl has been renamed.
20862 CHILD is true if decl is one of the renamed decls as part of
20863 importing whole module. */
20865 static void
20866 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20867 bool child)
20869 /* dw_die_ref at_import_die; */
20870 dw_die_ref scope_die;
20872 if (debug_info_level <= DINFO_LEVEL_TERSE)
20873 return;
20875 gcc_assert (decl);
20877 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20878 We need decl DIE for reference and scope die. First, get DIE for the decl
20879 itself. */
20881 /* Get the scope die for decl context. Use comp_unit_die for global module
20882 or decl. If die is not found for non globals, force new die. */
20883 if (context
20884 && TYPE_P (context)
20885 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20886 return;
20888 if (!(dwarf_version >= 3 || !dwarf_strict))
20889 return;
20891 scope_die = get_context_die (context);
20893 if (child)
20895 gcc_assert (scope_die->die_child);
20896 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20897 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20898 scope_die = scope_die->die_child;
20901 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20902 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20906 /* Write the debugging output for DECL. */
20908 void
20909 dwarf2out_decl (tree decl)
20911 dw_die_ref context_die = comp_unit_die;
20913 switch (TREE_CODE (decl))
20915 case ERROR_MARK:
20916 return;
20918 case FUNCTION_DECL:
20919 /* What we would really like to do here is to filter out all mere
20920 file-scope declarations of file-scope functions which are never
20921 referenced later within this translation unit (and keep all of ones
20922 that *are* referenced later on) but we aren't clairvoyant, so we have
20923 no idea which functions will be referenced in the future (i.e. later
20924 on within the current translation unit). So here we just ignore all
20925 file-scope function declarations which are not also definitions. If
20926 and when the debugger needs to know something about these functions,
20927 it will have to hunt around and find the DWARF information associated
20928 with the definition of the function.
20930 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20931 nodes represent definitions and which ones represent mere
20932 declarations. We have to check DECL_INITIAL instead. That's because
20933 the C front-end supports some weird semantics for "extern inline"
20934 function definitions. These can get inlined within the current
20935 translation unit (and thus, we need to generate Dwarf info for their
20936 abstract instances so that the Dwarf info for the concrete inlined
20937 instances can have something to refer to) but the compiler never
20938 generates any out-of-lines instances of such things (despite the fact
20939 that they *are* definitions).
20941 The important point is that the C front-end marks these "extern
20942 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20943 them anyway. Note that the C++ front-end also plays some similar games
20944 for inline function definitions appearing within include files which
20945 also contain `#pragma interface' pragmas. */
20946 if (DECL_INITIAL (decl) == NULL_TREE)
20947 return;
20949 /* If we're a nested function, initially use a parent of NULL; if we're
20950 a plain function, this will be fixed up in decls_for_scope. If
20951 we're a method, it will be ignored, since we already have a DIE. */
20952 if (decl_function_context (decl)
20953 /* But if we're in terse mode, we don't care about scope. */
20954 && debug_info_level > DINFO_LEVEL_TERSE)
20955 context_die = NULL;
20956 break;
20958 case VAR_DECL:
20959 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20960 declaration and if the declaration was never even referenced from
20961 within this entire compilation unit. We suppress these DIEs in
20962 order to save space in the .debug section (by eliminating entries
20963 which are probably useless). Note that we must not suppress
20964 block-local extern declarations (whether used or not) because that
20965 would screw-up the debugger's name lookup mechanism and cause it to
20966 miss things which really ought to be in scope at a given point. */
20967 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20968 return;
20970 /* For local statics lookup proper context die. */
20971 if (TREE_STATIC (decl) && decl_function_context (decl))
20972 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20974 /* If we are in terse mode, don't generate any DIEs to represent any
20975 variable declarations or definitions. */
20976 if (debug_info_level <= DINFO_LEVEL_TERSE)
20977 return;
20978 break;
20980 case CONST_DECL:
20981 if (debug_info_level <= DINFO_LEVEL_TERSE)
20982 return;
20983 if (!is_fortran () && !is_ada ())
20984 return;
20985 if (TREE_STATIC (decl) && decl_function_context (decl))
20986 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20987 break;
20989 case NAMESPACE_DECL:
20990 case IMPORTED_DECL:
20991 if (debug_info_level <= DINFO_LEVEL_TERSE)
20992 return;
20993 if (lookup_decl_die (decl) != NULL)
20994 return;
20995 break;
20997 case TYPE_DECL:
20998 /* Don't emit stubs for types unless they are needed by other DIEs. */
20999 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21000 return;
21002 /* Don't bother trying to generate any DIEs to represent any of the
21003 normal built-in types for the language we are compiling. */
21004 if (DECL_IS_BUILTIN (decl))
21006 /* OK, we need to generate one for `bool' so GDB knows what type
21007 comparisons have. */
21008 if (is_cxx ()
21009 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
21010 && ! DECL_IGNORED_P (decl))
21011 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
21013 return;
21016 /* If we are in terse mode, don't generate any DIEs for types. */
21017 if (debug_info_level <= DINFO_LEVEL_TERSE)
21018 return;
21020 /* If we're a function-scope tag, initially use a parent of NULL;
21021 this will be fixed up in decls_for_scope. */
21022 if (decl_function_context (decl))
21023 context_die = NULL;
21025 break;
21027 default:
21028 return;
21031 gen_decl_die (decl, NULL, context_die);
21034 /* Write the debugging output for DECL. */
21036 static void
21037 dwarf2out_function_decl (tree decl)
21039 dwarf2out_decl (decl);
21041 htab_empty (decl_loc_table);
21044 /* Output a marker (i.e. a label) for the beginning of the generated code for
21045 a lexical block. */
21047 static void
21048 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21049 unsigned int blocknum)
21051 switch_to_section (current_function_section ());
21052 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21055 /* Output a marker (i.e. a label) for the end of the generated code for a
21056 lexical block. */
21058 static void
21059 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21061 switch_to_section (current_function_section ());
21062 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21065 /* Returns nonzero if it is appropriate not to emit any debugging
21066 information for BLOCK, because it doesn't contain any instructions.
21068 Don't allow this for blocks with nested functions or local classes
21069 as we would end up with orphans, and in the presence of scheduling
21070 we may end up calling them anyway. */
21072 static bool
21073 dwarf2out_ignore_block (const_tree block)
21075 tree decl;
21076 unsigned int i;
21078 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21079 if (TREE_CODE (decl) == FUNCTION_DECL
21080 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21081 return 0;
21082 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21084 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21085 if (TREE_CODE (decl) == FUNCTION_DECL
21086 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21087 return 0;
21090 return 1;
21093 /* Hash table routines for file_hash. */
21095 static int
21096 file_table_eq (const void *p1_p, const void *p2_p)
21098 const struct dwarf_file_data *const p1 =
21099 (const struct dwarf_file_data *) p1_p;
21100 const char *const p2 = (const char *) p2_p;
21101 return strcmp (p1->filename, p2) == 0;
21104 static hashval_t
21105 file_table_hash (const void *p_p)
21107 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21108 return htab_hash_string (p->filename);
21111 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21112 dwarf2out.c) and return its "index". The index of each (known) filename is
21113 just a unique number which is associated with only that one filename. We
21114 need such numbers for the sake of generating labels (in the .debug_sfnames
21115 section) and references to those files numbers (in the .debug_srcinfo
21116 and.debug_macinfo sections). If the filename given as an argument is not
21117 found in our current list, add it to the list and assign it the next
21118 available unique index number. In order to speed up searches, we remember
21119 the index of the filename was looked up last. This handles the majority of
21120 all searches. */
21122 static struct dwarf_file_data *
21123 lookup_filename (const char *file_name)
21125 void ** slot;
21126 struct dwarf_file_data * created;
21128 /* Check to see if the file name that was searched on the previous
21129 call matches this file name. If so, return the index. */
21130 if (file_table_last_lookup
21131 && (file_name == file_table_last_lookup->filename
21132 || strcmp (file_table_last_lookup->filename, file_name) == 0))
21133 return file_table_last_lookup;
21135 /* Didn't match the previous lookup, search the table. */
21136 slot = htab_find_slot_with_hash (file_table, file_name,
21137 htab_hash_string (file_name), INSERT);
21138 if (*slot)
21139 return (struct dwarf_file_data *) *slot;
21141 created = ggc_alloc_dwarf_file_data ();
21142 created->filename = file_name;
21143 created->emitted_number = 0;
21144 *slot = created;
21145 return created;
21148 /* If the assembler will construct the file table, then translate the compiler
21149 internal file table number into the assembler file table number, and emit
21150 a .file directive if we haven't already emitted one yet. The file table
21151 numbers are different because we prune debug info for unused variables and
21152 types, which may include filenames. */
21154 static int
21155 maybe_emit_file (struct dwarf_file_data * fd)
21157 if (! fd->emitted_number)
21159 if (last_emitted_file)
21160 fd->emitted_number = last_emitted_file->emitted_number + 1;
21161 else
21162 fd->emitted_number = 1;
21163 last_emitted_file = fd;
21165 if (DWARF2_ASM_LINE_DEBUG_INFO)
21167 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21168 output_quoted_string (asm_out_file,
21169 remap_debug_filename (fd->filename));
21170 fputc ('\n', asm_out_file);
21174 return fd->emitted_number;
21177 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21178 That generation should happen after function debug info has been
21179 generated. The value of the attribute is the constant value of ARG. */
21181 static void
21182 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21184 die_arg_entry entry;
21186 if (!die || !arg)
21187 return;
21189 if (!tmpl_value_parm_die_table)
21190 tmpl_value_parm_die_table
21191 = VEC_alloc (die_arg_entry, gc, 32);
21193 entry.die = die;
21194 entry.arg = arg;
21195 VEC_safe_push (die_arg_entry, gc,
21196 tmpl_value_parm_die_table,
21197 &entry);
21200 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21201 by append_entry_to_tmpl_value_parm_die_table. This function must
21202 be called after function DIEs have been generated. */
21204 static void
21205 gen_remaining_tmpl_value_param_die_attribute (void)
21207 if (tmpl_value_parm_die_table)
21209 unsigned i;
21210 die_arg_entry *e;
21212 FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21213 tree_add_const_value_attribute (e->die, e->arg);
21218 /* Replace DW_AT_name for the decl with name. */
21220 static void
21221 dwarf2out_set_name (tree decl, tree name)
21223 dw_die_ref die;
21224 dw_attr_ref attr;
21225 const char *dname;
21227 die = TYPE_SYMTAB_DIE (decl);
21228 if (!die)
21229 return;
21231 dname = dwarf2_name (name, 0);
21232 if (!dname)
21233 return;
21235 attr = get_AT (die, DW_AT_name);
21236 if (attr)
21238 struct indirect_string_node *node;
21240 node = find_AT_string (dname);
21241 /* replace the string. */
21242 attr->dw_attr_val.v.val_str = node;
21245 else
21246 add_name_attribute (die, dname);
21249 /* Called by the final INSN scan whenever we see a direct function call.
21250 Make an entry into the direct call table, recording the point of call
21251 and a reference to the target function's debug entry. */
21253 static void
21254 dwarf2out_direct_call (tree targ)
21256 dcall_entry e;
21257 tree origin = decl_ultimate_origin (targ);
21259 /* If this is a clone, use the abstract origin as the target. */
21260 if (origin)
21261 targ = origin;
21263 e.poc_label_num = poc_label_num++;
21264 e.poc_decl = current_function_decl;
21265 e.targ_die = force_decl_die (targ);
21266 VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21268 /* Drop a label at the return point to mark the point of call. */
21269 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21272 /* Returns a hash value for X (which really is a struct vcall_insn). */
21274 static hashval_t
21275 vcall_insn_table_hash (const void *x)
21277 return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21280 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21281 insnd_uid of *Y. */
21283 static int
21284 vcall_insn_table_eq (const void *x, const void *y)
21286 return (((const struct vcall_insn *) x)->insn_uid
21287 == ((const struct vcall_insn *) y)->insn_uid);
21290 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE. */
21292 static void
21293 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21295 struct vcall_insn *item = ggc_alloc_vcall_insn ();
21296 struct vcall_insn **slot;
21298 gcc_assert (item);
21299 item->insn_uid = insn_uid;
21300 item->vtable_slot = vtable_slot;
21301 slot = (struct vcall_insn **)
21302 htab_find_slot_with_hash (vcall_insn_table, &item,
21303 (hashval_t) insn_uid, INSERT);
21304 *slot = item;
21307 /* Return the VTABLE_SLOT associated with INSN_UID. */
21309 static unsigned int
21310 lookup_vcall_insn (unsigned int insn_uid)
21312 struct vcall_insn item;
21313 struct vcall_insn *p;
21315 item.insn_uid = insn_uid;
21316 item.vtable_slot = 0;
21317 p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21318 (void *) &item,
21319 (hashval_t) insn_uid);
21320 if (p == NULL)
21321 return (unsigned int) -1;
21322 return p->vtable_slot;
21326 /* Called when lowering indirect calls to RTL. We make a note of INSN_UID
21327 and the OBJ_TYPE_REF_TOKEN from ADDR. For C++ virtual calls, the token
21328 is the vtable slot index that we will need to put in the virtual call
21329 table later. */
21331 static void
21332 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21334 if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21336 tree token = OBJ_TYPE_REF_TOKEN (addr);
21337 if (TREE_CODE (token) == INTEGER_CST)
21338 store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21342 /* Called when scheduling RTL, when a CALL_INSN is split. Copies the
21343 OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21344 with NEW_INSN. */
21346 static void
21347 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21349 unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21351 if (vtable_slot != (unsigned int) -1)
21352 store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21355 /* Called by the final INSN scan whenever we see a virtual function call.
21356 Make an entry into the virtual call table, recording the point of call
21357 and the slot index of the vtable entry used to call the virtual member
21358 function. The slot index was associated with the INSN_UID during the
21359 lowering to RTL. */
21361 static void
21362 dwarf2out_virtual_call (int insn_uid)
21364 unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21365 vcall_entry e;
21367 if (vtable_slot == (unsigned int) -1)
21368 return;
21370 e.poc_label_num = poc_label_num++;
21371 e.vtable_slot = vtable_slot;
21372 VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21374 /* Drop a label at the return point to mark the point of call. */
21375 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21378 /* Called by the final INSN scan whenever we see a var location. We
21379 use it to drop labels in the right places, and throw the location in
21380 our lookup table. */
21382 static void
21383 dwarf2out_var_location (rtx loc_note)
21385 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21386 struct var_loc_node *newloc;
21387 rtx next_real;
21388 static const char *last_label;
21389 static const char *last_postcall_label;
21390 static bool last_in_cold_section_p;
21391 tree decl;
21393 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21394 return;
21396 next_real = next_real_insn (loc_note);
21397 /* If there are no instructions which would be affected by this note,
21398 don't do anything. */
21399 if (next_real == NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
21400 return;
21402 /* If there were any real insns between note we processed last time
21403 and this note (or if it is the first note), clear
21404 last_{,postcall_}label so that they are not reused this time. */
21405 if (last_var_location_insn == NULL_RTX
21406 || last_var_location_insn != next_real
21407 || last_in_cold_section_p != in_cold_section_p)
21409 last_label = NULL;
21410 last_postcall_label = NULL;
21413 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21414 newloc = add_var_loc_to_decl (decl, loc_note,
21415 NOTE_DURING_CALL_P (loc_note)
21416 ? last_postcall_label : last_label);
21417 if (newloc == NULL)
21418 return;
21420 /* If there were no real insns between note we processed last time
21421 and this note, use the label we emitted last time. Otherwise
21422 create a new label and emit it. */
21423 if (last_label == NULL)
21425 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21426 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21427 loclabel_num++;
21428 last_label = ggc_strdup (loclabel);
21431 if (!NOTE_DURING_CALL_P (loc_note))
21432 newloc->label = last_label;
21433 else
21435 if (!last_postcall_label)
21437 sprintf (loclabel, "%s-1", last_label);
21438 last_postcall_label = ggc_strdup (loclabel);
21440 newloc->label = last_postcall_label;
21443 last_var_location_insn = next_real;
21444 last_in_cold_section_p = in_cold_section_p;
21447 /* We need to reset the locations at the beginning of each
21448 function. We can't do this in the end_function hook, because the
21449 declarations that use the locations won't have been output when
21450 that hook is called. Also compute have_multiple_function_sections here. */
21452 static void
21453 dwarf2out_begin_function (tree fun)
21455 if (function_section (fun) != text_section)
21456 have_multiple_function_sections = true;
21458 dwarf2out_note_section_used ();
21461 /* Output a label to mark the beginning of a source code line entry
21462 and record information relating to this source line, in
21463 'line_info_table' for later output of the .debug_line section. */
21465 static void
21466 dwarf2out_source_line (unsigned int line, const char *filename,
21467 int discriminator, bool is_stmt)
21469 static bool last_is_stmt = true;
21471 if (debug_info_level >= DINFO_LEVEL_NORMAL
21472 && line != 0)
21474 int file_num = maybe_emit_file (lookup_filename (filename));
21476 switch_to_section (current_function_section ());
21478 /* If requested, emit something human-readable. */
21479 if (flag_debug_asm)
21480 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21481 filename, line);
21483 if (DWARF2_ASM_LINE_DEBUG_INFO)
21485 /* Emit the .loc directive understood by GNU as. */
21486 fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21487 if (is_stmt != last_is_stmt)
21489 fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21490 last_is_stmt = is_stmt;
21492 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21493 fprintf (asm_out_file, " discriminator %d", discriminator);
21494 fputc ('\n', asm_out_file);
21496 /* Indicate that line number info exists. */
21497 line_info_table_in_use++;
21499 else if (function_section (current_function_decl) != text_section)
21501 dw_separate_line_info_ref line_info;
21502 targetm.asm_out.internal_label (asm_out_file,
21503 SEPARATE_LINE_CODE_LABEL,
21504 separate_line_info_table_in_use);
21506 /* Expand the line info table if necessary. */
21507 if (separate_line_info_table_in_use
21508 == separate_line_info_table_allocated)
21510 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21511 separate_line_info_table
21512 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21513 separate_line_info_table,
21514 separate_line_info_table_allocated);
21515 memset (separate_line_info_table
21516 + separate_line_info_table_in_use,
21518 (LINE_INFO_TABLE_INCREMENT
21519 * sizeof (dw_separate_line_info_entry)));
21522 /* Add the new entry at the end of the line_info_table. */
21523 line_info
21524 = &separate_line_info_table[separate_line_info_table_in_use++];
21525 line_info->dw_file_num = file_num;
21526 line_info->dw_line_num = line;
21527 line_info->function = current_function_funcdef_no;
21529 else
21531 dw_line_info_ref line_info;
21533 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21534 line_info_table_in_use);
21536 /* Expand the line info table if necessary. */
21537 if (line_info_table_in_use == line_info_table_allocated)
21539 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21540 line_info_table
21541 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21542 line_info_table_allocated);
21543 memset (line_info_table + line_info_table_in_use, 0,
21544 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21547 /* Add the new entry at the end of the line_info_table. */
21548 line_info = &line_info_table[line_info_table_in_use++];
21549 line_info->dw_file_num = file_num;
21550 line_info->dw_line_num = line;
21555 /* Record the beginning of a new source file. */
21557 static void
21558 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21560 if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21562 /* Record the beginning of the file for break_out_includes. */
21563 dw_die_ref bincl_die;
21565 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
21566 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21569 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21571 int file_num = maybe_emit_file (lookup_filename (filename));
21573 switch_to_section (debug_macinfo_section);
21574 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21575 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
21576 lineno);
21578 dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
21582 /* Record the end of a source file. */
21584 static void
21585 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21587 if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21588 /* Record the end of the file for break_out_includes. */
21589 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
21591 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21593 switch_to_section (debug_macinfo_section);
21594 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21598 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21599 the tail part of the directive line, i.e. the part which is past the
21600 initial whitespace, #, whitespace, directive-name, whitespace part. */
21602 static void
21603 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21604 const char *buffer ATTRIBUTE_UNUSED)
21606 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21608 switch_to_section (debug_macinfo_section);
21609 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
21610 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21611 dw2_asm_output_nstring (buffer, -1, "The macro");
21615 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21616 the tail part of the directive line, i.e. the part which is past the
21617 initial whitespace, #, whitespace, directive-name, whitespace part. */
21619 static void
21620 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21621 const char *buffer ATTRIBUTE_UNUSED)
21623 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21625 switch_to_section (debug_macinfo_section);
21626 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
21627 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21628 dw2_asm_output_nstring (buffer, -1, "The macro");
21632 /* Set up for Dwarf output at the start of compilation. */
21634 static void
21635 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21637 /* Allocate the file_table. */
21638 file_table = htab_create_ggc (50, file_table_hash,
21639 file_table_eq, NULL);
21641 /* Allocate the decl_die_table. */
21642 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21643 decl_die_table_eq, NULL);
21645 /* Allocate the decl_loc_table. */
21646 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21647 decl_loc_table_eq, NULL);
21649 /* Allocate the initial hunk of the decl_scope_table. */
21650 decl_scope_table = VEC_alloc (tree, gc, 256);
21652 /* Allocate the initial hunk of the abbrev_die_table. */
21653 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21654 (ABBREV_DIE_TABLE_INCREMENT);
21655 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21656 /* Zero-th entry is allocated, but unused. */
21657 abbrev_die_table_in_use = 1;
21659 /* Allocate the initial hunk of the line_info_table. */
21660 line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
21661 (LINE_INFO_TABLE_INCREMENT);
21662 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
21664 /* Zero-th entry is allocated, but unused. */
21665 line_info_table_in_use = 1;
21667 /* Allocate the pubtypes and pubnames vectors. */
21668 pubname_table = VEC_alloc (pubname_entry, gc, 32);
21669 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21671 /* Allocate the table that maps insn UIDs to vtable slot indexes. */
21672 vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
21673 vcall_insn_table_eq, NULL);
21675 /* Generate the initial DIE for the .debug section. Note that the (string)
21676 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
21677 will (typically) be a relative pathname and that this pathname should be
21678 taken as being relative to the directory from which the compiler was
21679 invoked when the given (base) source file was compiled. We will fill
21680 in this value in dwarf2out_finish. */
21681 comp_unit_die = gen_compile_unit_die (NULL);
21683 incomplete_types = VEC_alloc (tree, gc, 64);
21685 used_rtx_array = VEC_alloc (rtx, gc, 32);
21687 debug_info_section = get_section (DEBUG_INFO_SECTION,
21688 SECTION_DEBUG, NULL);
21689 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21690 SECTION_DEBUG, NULL);
21691 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21692 SECTION_DEBUG, NULL);
21693 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
21694 SECTION_DEBUG, NULL);
21695 debug_line_section = get_section (DEBUG_LINE_SECTION,
21696 SECTION_DEBUG, NULL);
21697 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21698 SECTION_DEBUG, NULL);
21699 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21700 SECTION_DEBUG, NULL);
21701 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21702 SECTION_DEBUG, NULL);
21703 debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
21704 SECTION_DEBUG, NULL);
21705 debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
21706 SECTION_DEBUG, NULL);
21707 debug_str_section = get_section (DEBUG_STR_SECTION,
21708 DEBUG_STR_SECTION_FLAGS, NULL);
21709 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21710 SECTION_DEBUG, NULL);
21711 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21712 SECTION_DEBUG, NULL);
21714 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21715 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21716 DEBUG_ABBREV_SECTION_LABEL, 0);
21717 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21718 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21719 COLD_TEXT_SECTION_LABEL, 0);
21720 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21722 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21723 DEBUG_INFO_SECTION_LABEL, 0);
21724 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21725 DEBUG_LINE_SECTION_LABEL, 0);
21726 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21727 DEBUG_RANGES_SECTION_LABEL, 0);
21728 switch_to_section (debug_abbrev_section);
21729 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
21730 switch_to_section (debug_info_section);
21731 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
21732 switch_to_section (debug_line_section);
21733 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
21735 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21737 switch_to_section (debug_macinfo_section);
21738 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21739 DEBUG_MACINFO_SECTION_LABEL, 0);
21740 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
21743 switch_to_section (text_section);
21744 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21745 if (flag_reorder_blocks_and_partition)
21747 cold_text_section = unlikely_text_section ();
21748 switch_to_section (cold_text_section);
21749 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21754 /* Called before cgraph_optimize starts outputtting functions, variables
21755 and toplevel asms into assembly. */
21757 static void
21758 dwarf2out_assembly_start (void)
21760 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE && dwarf2out_do_cfi_asm ())
21762 #ifndef TARGET_UNWIND_INFO
21763 if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
21764 #endif
21765 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21769 /* A helper function for dwarf2out_finish called through
21770 htab_traverse. Emit one queued .debug_str string. */
21772 static int
21773 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21775 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21777 if (node->label && node->refcount)
21779 switch_to_section (debug_str_section);
21780 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21781 assemble_string (node->str, strlen (node->str) + 1);
21784 return 1;
21787 #if ENABLE_ASSERT_CHECKING
21788 /* Verify that all marks are clear. */
21790 static void
21791 verify_marks_clear (dw_die_ref die)
21793 dw_die_ref c;
21795 gcc_assert (! die->die_mark);
21796 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21798 #endif /* ENABLE_ASSERT_CHECKING */
21800 /* Clear the marks for a die and its children.
21801 Be cool if the mark isn't set. */
21803 static void
21804 prune_unmark_dies (dw_die_ref die)
21806 dw_die_ref c;
21808 if (die->die_mark)
21809 die->die_mark = 0;
21810 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21813 /* Given DIE that we're marking as used, find any other dies
21814 it references as attributes and mark them as used. */
21816 static void
21817 prune_unused_types_walk_attribs (dw_die_ref die)
21819 dw_attr_ref a;
21820 unsigned ix;
21822 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21824 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21826 /* A reference to another DIE.
21827 Make sure that it will get emitted.
21828 If it was broken out into a comdat group, don't follow it. */
21829 if (dwarf_version < 4
21830 || a->dw_attr == DW_AT_specification
21831 || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21832 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21834 /* Set the string's refcount to 0 so that prune_unused_types_mark
21835 accounts properly for it. */
21836 if (AT_class (a) == dw_val_class_str)
21837 a->dw_attr_val.v.val_str->refcount = 0;
21842 /* Mark DIE as being used. If DOKIDS is true, then walk down
21843 to DIE's children. */
21845 static void
21846 prune_unused_types_mark (dw_die_ref die, int dokids)
21848 dw_die_ref c;
21850 if (die->die_mark == 0)
21852 /* We haven't done this node yet. Mark it as used. */
21853 die->die_mark = 1;
21855 /* We also have to mark its parents as used.
21856 (But we don't want to mark our parents' kids due to this.) */
21857 if (die->die_parent)
21858 prune_unused_types_mark (die->die_parent, 0);
21860 /* Mark any referenced nodes. */
21861 prune_unused_types_walk_attribs (die);
21863 /* If this node is a specification,
21864 also mark the definition, if it exists. */
21865 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21866 prune_unused_types_mark (die->die_definition, 1);
21869 if (dokids && die->die_mark != 2)
21871 /* We need to walk the children, but haven't done so yet.
21872 Remember that we've walked the kids. */
21873 die->die_mark = 2;
21875 /* If this is an array type, we need to make sure our
21876 kids get marked, even if they're types. If we're
21877 breaking out types into comdat sections, do this
21878 for all type definitions. */
21879 if (die->die_tag == DW_TAG_array_type
21880 || (dwarf_version >= 4
21881 && is_type_die (die) && ! is_declaration_die (die)))
21882 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21883 else
21884 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21888 /* For local classes, look if any static member functions were emitted
21889 and if so, mark them. */
21891 static void
21892 prune_unused_types_walk_local_classes (dw_die_ref die)
21894 dw_die_ref c;
21896 if (die->die_mark == 2)
21897 return;
21899 switch (die->die_tag)
21901 case DW_TAG_structure_type:
21902 case DW_TAG_union_type:
21903 case DW_TAG_class_type:
21904 break;
21906 case DW_TAG_subprogram:
21907 if (!get_AT_flag (die, DW_AT_declaration)
21908 || die->die_definition != NULL)
21909 prune_unused_types_mark (die, 1);
21910 return;
21912 default:
21913 return;
21916 /* Mark children. */
21917 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21920 /* Walk the tree DIE and mark types that we actually use. */
21922 static void
21923 prune_unused_types_walk (dw_die_ref die)
21925 dw_die_ref c;
21927 /* Don't do anything if this node is already marked and
21928 children have been marked as well. */
21929 if (die->die_mark == 2)
21930 return;
21932 switch (die->die_tag)
21934 case DW_TAG_structure_type:
21935 case DW_TAG_union_type:
21936 case DW_TAG_class_type:
21937 if (die->die_perennial_p)
21938 break;
21940 for (c = die->die_parent; c; c = c->die_parent)
21941 if (c->die_tag == DW_TAG_subprogram)
21942 break;
21944 /* Finding used static member functions inside of classes
21945 is needed just for local classes, because for other classes
21946 static member function DIEs with DW_AT_specification
21947 are emitted outside of the DW_TAG_*_type. If we ever change
21948 it, we'd need to call this even for non-local classes. */
21949 if (c)
21950 prune_unused_types_walk_local_classes (die);
21952 /* It's a type node --- don't mark it. */
21953 return;
21955 case DW_TAG_const_type:
21956 case DW_TAG_packed_type:
21957 case DW_TAG_pointer_type:
21958 case DW_TAG_reference_type:
21959 case DW_TAG_rvalue_reference_type:
21960 case DW_TAG_volatile_type:
21961 case DW_TAG_typedef:
21962 case DW_TAG_array_type:
21963 case DW_TAG_interface_type:
21964 case DW_TAG_friend:
21965 case DW_TAG_variant_part:
21966 case DW_TAG_enumeration_type:
21967 case DW_TAG_subroutine_type:
21968 case DW_TAG_string_type:
21969 case DW_TAG_set_type:
21970 case DW_TAG_subrange_type:
21971 case DW_TAG_ptr_to_member_type:
21972 case DW_TAG_file_type:
21973 if (die->die_perennial_p)
21974 break;
21976 /* It's a type node --- don't mark it. */
21977 return;
21979 default:
21980 /* Mark everything else. */
21981 break;
21984 if (die->die_mark == 0)
21986 die->die_mark = 1;
21988 /* Now, mark any dies referenced from here. */
21989 prune_unused_types_walk_attribs (die);
21992 die->die_mark = 2;
21994 /* Mark children. */
21995 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21998 /* Increment the string counts on strings referred to from DIE's
21999 attributes. */
22001 static void
22002 prune_unused_types_update_strings (dw_die_ref die)
22004 dw_attr_ref a;
22005 unsigned ix;
22007 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22008 if (AT_class (a) == dw_val_class_str)
22010 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22011 s->refcount++;
22012 /* Avoid unnecessarily putting strings that are used less than
22013 twice in the hash table. */
22014 if (s->refcount
22015 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22017 void ** slot;
22018 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22019 htab_hash_string (s->str),
22020 INSERT);
22021 gcc_assert (*slot == NULL);
22022 *slot = s;
22027 /* Remove from the tree DIE any dies that aren't marked. */
22029 static void
22030 prune_unused_types_prune (dw_die_ref die)
22032 dw_die_ref c;
22034 gcc_assert (die->die_mark);
22035 prune_unused_types_update_strings (die);
22037 if (! die->die_child)
22038 return;
22040 c = die->die_child;
22041 do {
22042 dw_die_ref prev = c;
22043 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22044 if (c == die->die_child)
22046 /* No marked children between 'prev' and the end of the list. */
22047 if (prev == c)
22048 /* No marked children at all. */
22049 die->die_child = NULL;
22050 else
22052 prev->die_sib = c->die_sib;
22053 die->die_child = prev;
22055 return;
22058 if (c != prev->die_sib)
22059 prev->die_sib = c;
22060 prune_unused_types_prune (c);
22061 } while (c != die->die_child);
22064 /* A helper function for dwarf2out_finish called through
22065 htab_traverse. Clear .debug_str strings that we haven't already
22066 decided to emit. */
22068 static int
22069 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22071 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22073 if (!node->label || !node->refcount)
22074 htab_clear_slot (debug_str_hash, h);
22076 return 1;
22079 /* Remove dies representing declarations that we never use. */
22081 static void
22082 prune_unused_types (void)
22084 unsigned int i;
22085 limbo_die_node *node;
22086 comdat_type_node *ctnode;
22087 pubname_ref pub;
22088 dcall_entry *dcall;
22090 #if ENABLE_ASSERT_CHECKING
22091 /* All the marks should already be clear. */
22092 verify_marks_clear (comp_unit_die);
22093 for (node = limbo_die_list; node; node = node->next)
22094 verify_marks_clear (node->die);
22095 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22096 verify_marks_clear (ctnode->root_die);
22097 #endif /* ENABLE_ASSERT_CHECKING */
22099 /* Mark types that are used in global variables. */
22100 premark_types_used_by_global_vars ();
22102 /* Set the mark on nodes that are actually used. */
22103 prune_unused_types_walk (comp_unit_die);
22104 for (node = limbo_die_list; node; node = node->next)
22105 prune_unused_types_walk (node->die);
22106 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22108 prune_unused_types_walk (ctnode->root_die);
22109 prune_unused_types_mark (ctnode->type_die, 1);
22112 /* Also set the mark on nodes referenced from the
22113 pubname_table or arange_table. */
22114 FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22115 prune_unused_types_mark (pub->die, 1);
22116 for (i = 0; i < arange_table_in_use; i++)
22117 prune_unused_types_mark (arange_table[i], 1);
22119 /* Mark nodes referenced from the direct call table. */
22120 FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, dcall)
22121 prune_unused_types_mark (dcall->targ_die, 1);
22123 /* Get rid of nodes that aren't marked; and update the string counts. */
22124 if (debug_str_hash && debug_str_hash_forced)
22125 htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22126 else if (debug_str_hash)
22127 htab_empty (debug_str_hash);
22128 prune_unused_types_prune (comp_unit_die);
22129 for (node = limbo_die_list; node; node = node->next)
22130 prune_unused_types_prune (node->die);
22131 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22132 prune_unused_types_prune (ctnode->root_die);
22134 /* Leave the marks clear. */
22135 prune_unmark_dies (comp_unit_die);
22136 for (node = limbo_die_list; node; node = node->next)
22137 prune_unmark_dies (node->die);
22138 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22139 prune_unmark_dies (ctnode->root_die);
22142 /* Set the parameter to true if there are any relative pathnames in
22143 the file table. */
22144 static int
22145 file_table_relative_p (void ** slot, void *param)
22147 bool *p = (bool *) param;
22148 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22149 if (!IS_ABSOLUTE_PATH (d->filename))
22151 *p = true;
22152 return 0;
22154 return 1;
22157 /* Routines to manipulate hash table of comdat type units. */
22159 static hashval_t
22160 htab_ct_hash (const void *of)
22162 hashval_t h;
22163 const comdat_type_node *const type_node = (const comdat_type_node *) of;
22165 memcpy (&h, type_node->signature, sizeof (h));
22166 return h;
22169 static int
22170 htab_ct_eq (const void *of1, const void *of2)
22172 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22173 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22175 return (! memcmp (type_node_1->signature, type_node_2->signature,
22176 DWARF_TYPE_SIGNATURE_SIZE));
22179 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22180 to the location it would have been added, should we know its
22181 DECL_ASSEMBLER_NAME when we added other attributes. This will
22182 probably improve compactness of debug info, removing equivalent
22183 abbrevs, and hide any differences caused by deferring the
22184 computation of the assembler name, triggered by e.g. PCH. */
22186 static inline void
22187 move_linkage_attr (dw_die_ref die)
22189 unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22190 dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22192 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22193 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22195 while (--ix > 0)
22197 dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22199 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22200 break;
22203 if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22205 VEC_pop (dw_attr_node, die->die_attr);
22206 VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22210 /* Helper function for resolve_addr, attempt to resolve
22211 one CONST_STRING, return non-zero if not successful. Similarly verify that
22212 SYMBOL_REFs refer to variables emitted in the current CU. */
22214 static int
22215 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22217 rtx rtl = *addr;
22219 if (GET_CODE (rtl) == CONST_STRING)
22221 size_t len = strlen (XSTR (rtl, 0)) + 1;
22222 tree t = build_string (len, XSTR (rtl, 0));
22223 tree tlen = build_int_cst (NULL_TREE, len - 1);
22224 TREE_TYPE (t)
22225 = build_array_type (char_type_node, build_index_type (tlen));
22226 rtl = lookup_constant_def (t);
22227 if (!rtl || !MEM_P (rtl))
22228 return 1;
22229 rtl = XEXP (rtl, 0);
22230 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22231 *addr = rtl;
22232 return 0;
22235 if (GET_CODE (rtl) == SYMBOL_REF
22236 && SYMBOL_REF_DECL (rtl)
22237 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22238 return 1;
22240 if (GET_CODE (rtl) == CONST
22241 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22242 return 1;
22244 return 0;
22247 /* Helper function for resolve_addr, handle one location
22248 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22249 the location list couldn't be resolved. */
22251 static bool
22252 resolve_addr_in_expr (dw_loc_descr_ref loc)
22254 for (; loc; loc = loc->dw_loc_next)
22255 if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22256 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22257 || (loc->dw_loc_opc == DW_OP_implicit_value
22258 && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22259 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22260 return false;
22261 else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22262 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22264 dw_die_ref ref
22265 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22266 if (ref == NULL)
22267 return false;
22268 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22269 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22270 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22272 return true;
22275 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22276 an address in .rodata section if the string literal is emitted there,
22277 or remove the containing location list or replace DW_AT_const_value
22278 with DW_AT_location and empty location expression, if it isn't found
22279 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22280 to something that has been emitted in the current CU. */
22282 static void
22283 resolve_addr (dw_die_ref die)
22285 dw_die_ref c;
22286 dw_attr_ref a;
22287 dw_loc_list_ref *curr;
22288 unsigned ix;
22290 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22291 switch (AT_class (a))
22293 case dw_val_class_loc_list:
22294 curr = AT_loc_list_ptr (a);
22295 while (*curr)
22297 if (!resolve_addr_in_expr ((*curr)->expr))
22299 dw_loc_list_ref next = (*curr)->dw_loc_next;
22300 if (next && (*curr)->ll_symbol)
22302 gcc_assert (!next->ll_symbol);
22303 next->ll_symbol = (*curr)->ll_symbol;
22305 *curr = next;
22307 else
22308 curr = &(*curr)->dw_loc_next;
22310 if (!AT_loc_list (a))
22312 remove_AT (die, a->dw_attr);
22313 ix--;
22315 break;
22316 case dw_val_class_loc:
22317 if (!resolve_addr_in_expr (AT_loc (a)))
22319 remove_AT (die, a->dw_attr);
22320 ix--;
22322 break;
22323 case dw_val_class_addr:
22324 if (a->dw_attr == DW_AT_const_value
22325 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22327 remove_AT (die, a->dw_attr);
22328 ix--;
22330 break;
22331 default:
22332 break;
22335 FOR_EACH_CHILD (die, c, resolve_addr (c));
22338 /* Output stuff that dwarf requires at the end of every file,
22339 and generate the DWARF-2 debugging info. */
22341 static void
22342 dwarf2out_finish (const char *filename)
22344 limbo_die_node *node, *next_node;
22345 comdat_type_node *ctnode;
22346 htab_t comdat_type_table;
22347 dw_die_ref die = 0;
22348 unsigned int i;
22350 gen_remaining_tmpl_value_param_die_attribute ();
22352 /* Add the name for the main input file now. We delayed this from
22353 dwarf2out_init to avoid complications with PCH. */
22354 add_name_attribute (comp_unit_die, remap_debug_filename (filename));
22355 if (!IS_ABSOLUTE_PATH (filename))
22356 add_comp_dir_attribute (comp_unit_die);
22357 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
22359 bool p = false;
22360 htab_traverse (file_table, file_table_relative_p, &p);
22361 if (p)
22362 add_comp_dir_attribute (comp_unit_die);
22365 for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22367 add_location_or_const_value_attribute (
22368 VEC_index (deferred_locations, deferred_locations_list, i)->die,
22369 VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22370 DW_AT_location);
22373 /* Traverse the limbo die list, and add parent/child links. The only
22374 dies without parents that should be here are concrete instances of
22375 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
22376 For concrete instances, we can get the parent die from the abstract
22377 instance. */
22378 for (node = limbo_die_list; node; node = next_node)
22380 next_node = node->next;
22381 die = node->die;
22383 if (die->die_parent == NULL)
22385 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22387 if (origin)
22388 add_child_die (origin->die_parent, die);
22389 else if (die == comp_unit_die)
22391 else if (seen_error ())
22392 /* It's OK to be confused by errors in the input. */
22393 add_child_die (comp_unit_die, die);
22394 else
22396 /* In certain situations, the lexical block containing a
22397 nested function can be optimized away, which results
22398 in the nested function die being orphaned. Likewise
22399 with the return type of that nested function. Force
22400 this to be a child of the containing function.
22402 It may happen that even the containing function got fully
22403 inlined and optimized out. In that case we are lost and
22404 assign the empty child. This should not be big issue as
22405 the function is likely unreachable too. */
22406 tree context = NULL_TREE;
22408 gcc_assert (node->created_for);
22410 if (DECL_P (node->created_for))
22411 context = DECL_CONTEXT (node->created_for);
22412 else if (TYPE_P (node->created_for))
22413 context = TYPE_CONTEXT (node->created_for);
22415 gcc_assert (context
22416 && (TREE_CODE (context) == FUNCTION_DECL
22417 || TREE_CODE (context) == NAMESPACE_DECL));
22419 origin = lookup_decl_die (context);
22420 if (origin)
22421 add_child_die (origin, die);
22422 else
22423 add_child_die (comp_unit_die, die);
22428 limbo_die_list = NULL;
22430 resolve_addr (comp_unit_die);
22432 for (node = deferred_asm_name; node; node = node->next)
22434 tree decl = node->created_for;
22435 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22437 add_linkage_attr (node->die, decl);
22438 move_linkage_attr (node->die);
22442 deferred_asm_name = NULL;
22444 /* Walk through the list of incomplete types again, trying once more to
22445 emit full debugging info for them. */
22446 retry_incomplete_types ();
22448 if (flag_eliminate_unused_debug_types)
22449 prune_unused_types ();
22451 /* Generate separate CUs for each of the include files we've seen.
22452 They will go into limbo_die_list. */
22453 if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22454 break_out_includes (comp_unit_die);
22456 /* Generate separate COMDAT sections for type DIEs. */
22457 if (dwarf_version >= 4)
22459 break_out_comdat_types (comp_unit_die);
22461 /* Each new type_unit DIE was added to the limbo die list when created.
22462 Since these have all been added to comdat_type_list, clear the
22463 limbo die list. */
22464 limbo_die_list = NULL;
22466 /* For each new comdat type unit, copy declarations for incomplete
22467 types to make the new unit self-contained (i.e., no direct
22468 references to the main compile unit). */
22469 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22470 copy_decls_for_unworthy_types (ctnode->root_die);
22471 copy_decls_for_unworthy_types (comp_unit_die);
22473 /* In the process of copying declarations from one unit to another,
22474 we may have left some declarations behind that are no longer
22475 referenced. Prune them. */
22476 prune_unused_types ();
22479 /* Traverse the DIE's and add add sibling attributes to those DIE's
22480 that have children. */
22481 add_sibling_attributes (comp_unit_die);
22482 for (node = limbo_die_list; node; node = node->next)
22483 add_sibling_attributes (node->die);
22484 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22485 add_sibling_attributes (ctnode->root_die);
22487 /* Output a terminator label for the .text section. */
22488 switch_to_section (text_section);
22489 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22490 if (flag_reorder_blocks_and_partition)
22492 switch_to_section (unlikely_text_section ());
22493 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22496 /* We can only use the low/high_pc attributes if all of the code was
22497 in .text. */
22498 if (!have_multiple_function_sections
22499 || !(dwarf_version >= 3 || !dwarf_strict))
22501 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
22502 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
22505 else
22507 unsigned fde_idx = 0;
22508 bool range_list_added = false;
22510 /* We need to give .debug_loc and .debug_ranges an appropriate
22511 "base address". Use zero so that these addresses become
22512 absolute. Historically, we've emitted the unexpected
22513 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22514 Emit both to give time for other tools to adapt. */
22515 add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
22516 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
22518 if (text_section_used)
22519 add_ranges_by_labels (comp_unit_die, text_section_label,
22520 text_end_label, &range_list_added);
22521 if (flag_reorder_blocks_and_partition && cold_text_section_used)
22522 add_ranges_by_labels (comp_unit_die, cold_text_section_label,
22523 cold_end_label, &range_list_added);
22525 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
22527 dw_fde_ref fde = &fde_table[fde_idx];
22529 if (fde->dw_fde_switched_sections)
22531 if (!fde->in_std_section)
22532 add_ranges_by_labels (comp_unit_die,
22533 fde->dw_fde_hot_section_label,
22534 fde->dw_fde_hot_section_end_label,
22535 &range_list_added);
22536 if (!fde->cold_in_std_section)
22537 add_ranges_by_labels (comp_unit_die,
22538 fde->dw_fde_unlikely_section_label,
22539 fde->dw_fde_unlikely_section_end_label,
22540 &range_list_added);
22542 else if (!fde->in_std_section)
22543 add_ranges_by_labels (comp_unit_die, fde->dw_fde_begin,
22544 fde->dw_fde_end, &range_list_added);
22547 if (range_list_added)
22548 add_ranges (NULL);
22551 if (debug_info_level >= DINFO_LEVEL_NORMAL)
22552 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
22553 debug_line_section_label);
22555 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22556 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
22558 /* Output all of the compilation units. We put the main one last so that
22559 the offsets are available to output_pubnames. */
22560 for (node = limbo_die_list; node; node = node->next)
22561 output_comp_unit (node->die, 0);
22563 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22564 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22566 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22568 /* Don't output duplicate types. */
22569 if (*slot != HTAB_EMPTY_ENTRY)
22570 continue;
22572 /* Add a pointer to the line table for the main compilation unit
22573 so that the debugger can make sense of DW_AT_decl_file
22574 attributes. */
22575 if (debug_info_level >= DINFO_LEVEL_NORMAL)
22576 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22577 debug_line_section_label);
22579 output_comdat_type_unit (ctnode);
22580 *slot = ctnode;
22582 htab_delete (comdat_type_table);
22584 /* Output the main compilation unit if non-empty or if .debug_macinfo
22585 has been emitted. */
22586 output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
22588 /* Output the abbreviation table. */
22589 switch_to_section (debug_abbrev_section);
22590 output_abbrev_section ();
22592 /* Output location list section if necessary. */
22593 if (have_location_lists)
22595 /* Output the location lists info. */
22596 switch_to_section (debug_loc_section);
22597 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22598 DEBUG_LOC_SECTION_LABEL, 0);
22599 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22600 output_location_lists (die);
22603 /* Output public names table if necessary. */
22604 if (!VEC_empty (pubname_entry, pubname_table))
22606 switch_to_section (debug_pubnames_section);
22607 output_pubnames (pubname_table);
22610 /* Output public types table if necessary. */
22611 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22612 It shouldn't hurt to emit it always, since pure DWARF2 consumers
22613 simply won't look for the section. */
22614 if (!VEC_empty (pubname_entry, pubtype_table))
22616 switch_to_section (debug_pubtypes_section);
22617 output_pubnames (pubtype_table);
22620 /* Output direct and virtual call tables if necessary. */
22621 if (!VEC_empty (dcall_entry, dcall_table))
22623 switch_to_section (debug_dcall_section);
22624 output_dcall_table ();
22626 if (!VEC_empty (vcall_entry, vcall_table))
22628 switch_to_section (debug_vcall_section);
22629 output_vcall_table ();
22632 /* Output the address range information. We only put functions in the arange
22633 table, so don't write it out if we don't have any. */
22634 if (fde_table_in_use)
22636 switch_to_section (debug_aranges_section);
22637 output_aranges ();
22640 /* Output ranges section if necessary. */
22641 if (ranges_table_in_use)
22643 switch_to_section (debug_ranges_section);
22644 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22645 output_ranges ();
22648 /* Output the source line correspondence table. We must do this
22649 even if there is no line information. Otherwise, on an empty
22650 translation unit, we will generate a present, but empty,
22651 .debug_info section. IRIX 6.5 `nm' will then complain when
22652 examining the file. This is done late so that any filenames
22653 used by the debug_info section are marked as 'used'. */
22654 if (! DWARF2_ASM_LINE_DEBUG_INFO)
22656 switch_to_section (debug_line_section);
22657 output_line_info ();
22660 /* Have to end the macro section. */
22661 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22663 switch_to_section (debug_macinfo_section);
22664 dw2_asm_output_data (1, 0, "End compilation unit");
22667 /* If we emitted any DW_FORM_strp form attribute, output the string
22668 table too. */
22669 if (debug_str_hash)
22670 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22673 #include "gt-dwarf2out.h"