re PR fortran/42852 (gfortran -Wall warns about truncated lines when only a continuat...
[official-gcc.git] / gcc / dwarf2out.c
blob68b6483282908bce12d84822bea0696a57678c94
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 #ifdef DWARF2_DEBUGGING_INFO
97 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
99 static rtx last_var_location_insn;
100 #endif
102 #ifdef VMS_DEBUGGING_INFO
103 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
105 /* Define this macro to be a nonzero value if the directory specifications
106 which are output in the debug info should end with a separator. */
107 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
108 /* Define this macro to evaluate to a nonzero value if GCC should refrain
109 from generating indirect strings in DWARF2 debug information, for instance
110 if your target is stuck with an old version of GDB that is unable to
111 process them properly or uses VMS Debug. */
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
113 #else
114 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
116 #endif
118 #ifndef DWARF2_FRAME_INFO
119 # ifdef DWARF2_DEBUGGING_INFO
120 # define DWARF2_FRAME_INFO \
121 (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
122 # else
123 # define DWARF2_FRAME_INFO 0
124 # endif
125 #endif
127 /* Map register numbers held in the call frame info that gcc has
128 collected using DWARF_FRAME_REGNUM to those that should be output in
129 .debug_frame and .eh_frame. */
130 #ifndef DWARF2_FRAME_REG_OUT
131 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
132 #endif
134 /* Save the result of dwarf2out_do_frame across PCH. */
135 static GTY(()) bool saved_do_cfi_asm = 0;
137 /* Decide whether we want to emit frame unwind information for the current
138 translation unit. */
141 dwarf2out_do_frame (void)
143 /* We want to emit correct CFA location expressions or lists, so we
144 have to return true if we're going to output debug info, even if
145 we're not going to output frame or unwind info. */
146 return (write_symbols == DWARF2_DEBUG
147 || write_symbols == VMS_AND_DWARF2_DEBUG
148 || DWARF2_FRAME_INFO || saved_do_cfi_asm
149 #ifdef DWARF2_UNWIND_INFO
150 || (DWARF2_UNWIND_INFO
151 && (flag_unwind_tables
152 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
153 #endif
157 /* Decide whether to emit frame unwind via assembler directives. */
160 dwarf2out_do_cfi_asm (void)
162 int enc;
164 #ifdef MIPS_DEBUGGING_INFO
165 return false;
166 #endif
167 if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
168 return false;
169 if (saved_do_cfi_asm)
170 return true;
171 if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
172 return false;
174 /* Make sure the personality encoding is one the assembler can support.
175 In particular, aligned addresses can't be handled. */
176 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
177 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
178 return false;
179 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
180 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
181 return false;
183 if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE)
185 #ifdef TARGET_UNWIND_INFO
186 return false;
187 #else
188 if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
189 return false;
190 #endif
193 saved_do_cfi_asm = true;
194 return true;
197 /* The size of the target's pointer type. */
198 #ifndef PTR_SIZE
199 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
200 #endif
202 /* Array of RTXes referenced by the debugging information, which therefore
203 must be kept around forever. */
204 static GTY(()) VEC(rtx,gc) *used_rtx_array;
206 /* A pointer to the base of a list of incomplete types which might be
207 completed at some later time. incomplete_types_list needs to be a
208 VEC(tree,gc) because we want to tell the garbage collector about
209 it. */
210 static GTY(()) VEC(tree,gc) *incomplete_types;
212 /* A pointer to the base of a table of references to declaration
213 scopes. This table is a display which tracks the nesting
214 of declaration scopes at the current scope and containing
215 scopes. This table is used to find the proper place to
216 define type declaration DIE's. */
217 static GTY(()) VEC(tree,gc) *decl_scope_table;
219 /* Pointers to various DWARF2 sections. */
220 static GTY(()) section *debug_info_section;
221 static GTY(()) section *debug_abbrev_section;
222 static GTY(()) section *debug_aranges_section;
223 static GTY(()) section *debug_macinfo_section;
224 static GTY(()) section *debug_line_section;
225 static GTY(()) section *debug_loc_section;
226 static GTY(()) section *debug_pubnames_section;
227 static GTY(()) section *debug_pubtypes_section;
228 static GTY(()) section *debug_dcall_section;
229 static GTY(()) section *debug_vcall_section;
230 static GTY(()) section *debug_str_section;
231 static GTY(()) section *debug_ranges_section;
232 static GTY(()) section *debug_frame_section;
234 /* Personality decl of current unit. Used only when assembler does not support
235 personality CFI. */
236 static GTY(()) rtx current_unit_personality;
238 /* How to start an assembler comment. */
239 #ifndef ASM_COMMENT_START
240 #define ASM_COMMENT_START ";#"
241 #endif
243 typedef struct dw_cfi_struct *dw_cfi_ref;
244 typedef struct dw_fde_struct *dw_fde_ref;
245 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
247 /* Call frames are described using a sequence of Call Frame
248 Information instructions. The register number, offset
249 and address fields are provided as possible operands;
250 their use is selected by the opcode field. */
252 enum dw_cfi_oprnd_type {
253 dw_cfi_oprnd_unused,
254 dw_cfi_oprnd_reg_num,
255 dw_cfi_oprnd_offset,
256 dw_cfi_oprnd_addr,
257 dw_cfi_oprnd_loc
260 typedef union GTY(()) dw_cfi_oprnd_struct {
261 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
262 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
263 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
264 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
266 dw_cfi_oprnd;
268 typedef struct GTY(()) dw_cfi_struct {
269 dw_cfi_ref dw_cfi_next;
270 enum dwarf_call_frame_info dw_cfi_opc;
271 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
272 dw_cfi_oprnd1;
273 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
274 dw_cfi_oprnd2;
276 dw_cfi_node;
278 /* This is how we define the location of the CFA. We use to handle it
279 as REG + OFFSET all the time, but now it can be more complex.
280 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
281 Instead of passing around REG and OFFSET, we pass a copy
282 of this structure. */
283 typedef struct GTY(()) cfa_loc {
284 HOST_WIDE_INT offset;
285 HOST_WIDE_INT base_offset;
286 unsigned int reg;
287 BOOL_BITFIELD indirect : 1; /* 1 if CFA is accessed via a dereference. */
288 BOOL_BITFIELD in_use : 1; /* 1 if a saved cfa is stored here. */
289 } dw_cfa_location;
291 /* All call frame descriptions (FDE's) in the GCC generated DWARF
292 refer to a single Common Information Entry (CIE), defined at
293 the beginning of the .debug_frame section. This use of a single
294 CIE obviates the need to keep track of multiple CIE's
295 in the DWARF generation routines below. */
297 typedef struct GTY(()) dw_fde_struct {
298 tree decl;
299 const char *dw_fde_begin;
300 const char *dw_fde_current_label;
301 const char *dw_fde_end;
302 const char *dw_fde_vms_end_prologue;
303 const char *dw_fde_vms_begin_epilogue;
304 const char *dw_fde_hot_section_label;
305 const char *dw_fde_hot_section_end_label;
306 const char *dw_fde_unlikely_section_label;
307 const char *dw_fde_unlikely_section_end_label;
308 dw_cfi_ref dw_fde_cfi;
309 dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections. */
310 HOST_WIDE_INT stack_realignment;
311 unsigned funcdef_number;
312 /* Dynamic realign argument pointer register. */
313 unsigned int drap_reg;
314 /* Virtual dynamic realign argument pointer register. */
315 unsigned int vdrap_reg;
316 /* These 3 flags are copied from rtl_data in function.h. */
317 unsigned all_throwers_are_sibcalls : 1;
318 unsigned uses_eh_lsda : 1;
319 unsigned nothrow : 1;
320 /* Whether we did stack realign in this call frame. */
321 unsigned stack_realign : 1;
322 /* Whether dynamic realign argument pointer register has been saved. */
323 unsigned drap_reg_saved: 1;
324 /* True iff dw_fde_begin label is in text_section or cold_text_section. */
325 unsigned in_std_section : 1;
326 /* True iff dw_fde_unlikely_section_label is in text_section or
327 cold_text_section. */
328 unsigned cold_in_std_section : 1;
329 /* True iff switched sections. */
330 unsigned dw_fde_switched_sections : 1;
331 /* True iff switching from cold to hot section. */
332 unsigned dw_fde_switched_cold_to_hot : 1;
334 dw_fde_node;
336 /* Maximum size (in bytes) of an artificially generated label. */
337 #define MAX_ARTIFICIAL_LABEL_BYTES 30
339 /* The size of addresses as they appear in the Dwarf 2 data.
340 Some architectures use word addresses to refer to code locations,
341 but Dwarf 2 info always uses byte addresses. On such machines,
342 Dwarf 2 addresses need to be larger than the architecture's
343 pointers. */
344 #ifndef DWARF2_ADDR_SIZE
345 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
346 #endif
348 /* The size in bytes of a DWARF field indicating an offset or length
349 relative to a debug info section, specified to be 4 bytes in the
350 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
351 as PTR_SIZE. */
353 #ifndef DWARF_OFFSET_SIZE
354 #define DWARF_OFFSET_SIZE 4
355 #endif
357 /* The size in bytes of a DWARF 4 type signature. */
359 #ifndef DWARF_TYPE_SIGNATURE_SIZE
360 #define DWARF_TYPE_SIGNATURE_SIZE 8
361 #endif
363 /* According to the (draft) DWARF 3 specification, the initial length
364 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
365 bytes are 0xffffffff, followed by the length stored in the next 8
366 bytes.
368 However, the SGI/MIPS ABI uses an initial length which is equal to
369 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
371 #ifndef DWARF_INITIAL_LENGTH_SIZE
372 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
373 #endif
375 /* Round SIZE up to the nearest BOUNDARY. */
376 #define DWARF_ROUND(SIZE,BOUNDARY) \
377 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
379 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
380 #ifndef DWARF_CIE_DATA_ALIGNMENT
381 #ifdef STACK_GROWS_DOWNWARD
382 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
383 #else
384 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
385 #endif
386 #endif
388 /* CIE identifier. */
389 #if HOST_BITS_PER_WIDE_INT >= 64
390 #define DWARF_CIE_ID \
391 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
392 #else
393 #define DWARF_CIE_ID DW_CIE_ID
394 #endif
396 /* A pointer to the base of a table that contains frame description
397 information for each routine. */
398 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
400 /* Number of elements currently allocated for fde_table. */
401 static GTY(()) unsigned fde_table_allocated;
403 /* Number of elements in fde_table currently in use. */
404 static GTY(()) unsigned fde_table_in_use;
406 /* Size (in elements) of increments by which we may expand the
407 fde_table. */
408 #define FDE_TABLE_INCREMENT 256
410 /* Get the current fde_table entry we should use. */
412 static inline dw_fde_ref
413 current_fde (void)
415 return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
418 /* A list of call frame insns for the CIE. */
419 static GTY(()) dw_cfi_ref cie_cfi_head;
421 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
422 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
423 attribute that accelerates the lookup of the FDE associated
424 with the subprogram. This variable holds the table index of the FDE
425 associated with the current function (body) definition. */
426 static unsigned current_funcdef_fde;
427 #endif
429 struct GTY(()) indirect_string_node {
430 const char *str;
431 unsigned int refcount;
432 enum dwarf_form form;
433 char *label;
436 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
438 /* True if the compilation unit has location entries that reference
439 debug strings. */
440 static GTY(()) bool debug_str_hash_forced = false;
442 static GTY(()) int dw2_string_counter;
443 static GTY(()) unsigned long dwarf2out_cfi_label_num;
445 /* True if the compilation unit places functions in more than one section. */
446 static GTY(()) bool have_multiple_function_sections = false;
448 /* Whether the default text and cold text sections have been used at all. */
450 static GTY(()) bool text_section_used = false;
451 static GTY(()) bool cold_text_section_used = false;
453 /* The default cold text section. */
454 static GTY(()) section *cold_text_section;
456 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
458 /* Forward declarations for functions defined in this file. */
460 static char *stripattributes (const char *);
461 static const char *dwarf_cfi_name (unsigned);
462 static dw_cfi_ref new_cfi (void);
463 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
464 static void add_fde_cfi (const char *, dw_cfi_ref);
465 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
466 static void lookup_cfa (dw_cfa_location *);
467 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
468 #ifdef DWARF2_UNWIND_INFO
469 static void initial_return_save (rtx);
470 #endif
471 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
472 HOST_WIDE_INT);
473 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
474 static void output_cfi_directive (dw_cfi_ref);
475 static void output_call_frame_info (int);
476 static void dwarf2out_note_section_used (void);
477 static void flush_queued_reg_saves (void);
478 static bool clobbers_queued_reg_save (const_rtx);
479 static void dwarf2out_frame_debug_expr (rtx, const char *);
481 /* Support for complex CFA locations. */
482 static void output_cfa_loc (dw_cfi_ref);
483 static void output_cfa_loc_raw (dw_cfi_ref);
484 static void get_cfa_from_loc_descr (dw_cfa_location *,
485 struct dw_loc_descr_struct *);
486 static struct dw_loc_descr_struct *build_cfa_loc
487 (dw_cfa_location *, HOST_WIDE_INT);
488 static struct dw_loc_descr_struct *build_cfa_aligned_loc
489 (HOST_WIDE_INT, HOST_WIDE_INT);
490 static void def_cfa_1 (const char *, dw_cfa_location *);
492 /* How to start an assembler comment. */
493 #ifndef ASM_COMMENT_START
494 #define ASM_COMMENT_START ";#"
495 #endif
497 /* Data and reference forms for relocatable data. */
498 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
499 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
501 #ifndef DEBUG_FRAME_SECTION
502 #define DEBUG_FRAME_SECTION ".debug_frame"
503 #endif
505 #ifndef FUNC_BEGIN_LABEL
506 #define FUNC_BEGIN_LABEL "LFB"
507 #endif
509 #ifndef FUNC_END_LABEL
510 #define FUNC_END_LABEL "LFE"
511 #endif
513 #ifndef PROLOGUE_END_LABEL
514 #define PROLOGUE_END_LABEL "LPE"
515 #endif
517 #ifndef EPILOGUE_BEGIN_LABEL
518 #define EPILOGUE_BEGIN_LABEL "LEB"
519 #endif
521 #ifndef FRAME_BEGIN_LABEL
522 #define FRAME_BEGIN_LABEL "Lframe"
523 #endif
524 #define CIE_AFTER_SIZE_LABEL "LSCIE"
525 #define CIE_END_LABEL "LECIE"
526 #define FDE_LABEL "LSFDE"
527 #define FDE_AFTER_SIZE_LABEL "LASFDE"
528 #define FDE_END_LABEL "LEFDE"
529 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
530 #define LINE_NUMBER_END_LABEL "LELT"
531 #define LN_PROLOG_AS_LABEL "LASLTP"
532 #define LN_PROLOG_END_LABEL "LELTP"
533 #define DIE_LABEL_PREFIX "DW"
535 /* The DWARF 2 CFA column which tracks the return address. Normally this
536 is the column for PC, or the first column after all of the hard
537 registers. */
538 #ifndef DWARF_FRAME_RETURN_COLUMN
539 #ifdef PC_REGNUM
540 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
541 #else
542 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
543 #endif
544 #endif
546 /* The mapping from gcc register number to DWARF 2 CFA column number. By
547 default, we just provide columns for all registers. */
548 #ifndef DWARF_FRAME_REGNUM
549 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
550 #endif
552 /* Hook used by __throw. */
555 expand_builtin_dwarf_sp_column (void)
557 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
558 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
561 /* Return a pointer to a copy of the section string name S with all
562 attributes stripped off, and an asterisk prepended (for assemble_name). */
564 static inline char *
565 stripattributes (const char *s)
567 char *stripped = XNEWVEC (char, strlen (s) + 2);
568 char *p = stripped;
570 *p++ = '*';
572 while (*s && *s != ',')
573 *p++ = *s++;
575 *p = '\0';
576 return stripped;
579 /* MEM is a memory reference for the register size table, each element of
580 which has mode MODE. Initialize column C as a return address column. */
582 static void
583 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
585 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
586 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
587 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
590 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder. */
592 static inline HOST_WIDE_INT
593 div_data_align (HOST_WIDE_INT off)
595 HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
596 gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
597 return r;
600 /* Return true if we need a signed version of a given opcode
601 (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended). */
603 static inline bool
604 need_data_align_sf_opcode (HOST_WIDE_INT off)
606 return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
609 /* Generate code to initialize the register size table. */
611 void
612 expand_builtin_init_dwarf_reg_sizes (tree address)
614 unsigned int i;
615 enum machine_mode mode = TYPE_MODE (char_type_node);
616 rtx addr = expand_normal (address);
617 rtx mem = gen_rtx_MEM (BLKmode, addr);
618 bool wrote_return_column = false;
620 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
622 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
624 if (rnum < DWARF_FRAME_REGISTERS)
626 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
627 enum machine_mode save_mode = reg_raw_mode[i];
628 HOST_WIDE_INT size;
630 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
631 save_mode = choose_hard_reg_mode (i, 1, true);
632 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
634 if (save_mode == VOIDmode)
635 continue;
636 wrote_return_column = true;
638 size = GET_MODE_SIZE (save_mode);
639 if (offset < 0)
640 continue;
642 emit_move_insn (adjust_address (mem, mode, offset),
643 gen_int_mode (size, mode));
647 if (!wrote_return_column)
648 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
650 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
651 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
652 #endif
654 targetm.init_dwarf_reg_sizes_extra (address);
657 /* Convert a DWARF call frame info. operation to its string name */
659 static const char *
660 dwarf_cfi_name (unsigned int cfi_opc)
662 switch (cfi_opc)
664 case DW_CFA_advance_loc:
665 return "DW_CFA_advance_loc";
666 case DW_CFA_offset:
667 return "DW_CFA_offset";
668 case DW_CFA_restore:
669 return "DW_CFA_restore";
670 case DW_CFA_nop:
671 return "DW_CFA_nop";
672 case DW_CFA_set_loc:
673 return "DW_CFA_set_loc";
674 case DW_CFA_advance_loc1:
675 return "DW_CFA_advance_loc1";
676 case DW_CFA_advance_loc2:
677 return "DW_CFA_advance_loc2";
678 case DW_CFA_advance_loc4:
679 return "DW_CFA_advance_loc4";
680 case DW_CFA_offset_extended:
681 return "DW_CFA_offset_extended";
682 case DW_CFA_restore_extended:
683 return "DW_CFA_restore_extended";
684 case DW_CFA_undefined:
685 return "DW_CFA_undefined";
686 case DW_CFA_same_value:
687 return "DW_CFA_same_value";
688 case DW_CFA_register:
689 return "DW_CFA_register";
690 case DW_CFA_remember_state:
691 return "DW_CFA_remember_state";
692 case DW_CFA_restore_state:
693 return "DW_CFA_restore_state";
694 case DW_CFA_def_cfa:
695 return "DW_CFA_def_cfa";
696 case DW_CFA_def_cfa_register:
697 return "DW_CFA_def_cfa_register";
698 case DW_CFA_def_cfa_offset:
699 return "DW_CFA_def_cfa_offset";
701 /* DWARF 3 */
702 case DW_CFA_def_cfa_expression:
703 return "DW_CFA_def_cfa_expression";
704 case DW_CFA_expression:
705 return "DW_CFA_expression";
706 case DW_CFA_offset_extended_sf:
707 return "DW_CFA_offset_extended_sf";
708 case DW_CFA_def_cfa_sf:
709 return "DW_CFA_def_cfa_sf";
710 case DW_CFA_def_cfa_offset_sf:
711 return "DW_CFA_def_cfa_offset_sf";
713 /* SGI/MIPS specific */
714 case DW_CFA_MIPS_advance_loc8:
715 return "DW_CFA_MIPS_advance_loc8";
717 /* GNU extensions */
718 case DW_CFA_GNU_window_save:
719 return "DW_CFA_GNU_window_save";
720 case DW_CFA_GNU_args_size:
721 return "DW_CFA_GNU_args_size";
722 case DW_CFA_GNU_negative_offset_extended:
723 return "DW_CFA_GNU_negative_offset_extended";
725 default:
726 return "DW_CFA_<unknown>";
730 /* Return a pointer to a newly allocated Call Frame Instruction. */
732 static inline dw_cfi_ref
733 new_cfi (void)
735 dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
737 cfi->dw_cfi_next = NULL;
738 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
739 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
741 return cfi;
744 /* Add a Call Frame Instruction to list of instructions. */
746 static inline void
747 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
749 dw_cfi_ref *p;
750 dw_fde_ref fde = current_fde ();
752 /* When DRAP is used, CFA is defined with an expression. Redefine
753 CFA may lead to a different CFA value. */
754 /* ??? Of course, this heuristic fails when we're annotating epilogues,
755 because of course we'll always want to redefine the CFA back to the
756 stack pointer on the way out. Where should we move this check? */
757 if (0 && fde && fde->drap_reg != INVALID_REGNUM)
758 switch (cfi->dw_cfi_opc)
760 case DW_CFA_def_cfa_register:
761 case DW_CFA_def_cfa_offset:
762 case DW_CFA_def_cfa_offset_sf:
763 case DW_CFA_def_cfa:
764 case DW_CFA_def_cfa_sf:
765 gcc_unreachable ();
767 default:
768 break;
771 /* Find the end of the chain. */
772 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
775 *p = cfi;
778 /* Generate a new label for the CFI info to refer to. FORCE is true
779 if a label needs to be output even when using .cfi_* directives. */
781 char *
782 dwarf2out_cfi_label (bool force)
784 static char label[20];
786 if (!force && dwarf2out_do_cfi_asm ())
788 /* In this case, we will be emitting the asm directive instead of
789 the label, so just return a placeholder to keep the rest of the
790 interfaces happy. */
791 strcpy (label, "<do not output>");
793 else
795 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
796 ASM_OUTPUT_LABEL (asm_out_file, label);
799 return label;
802 /* True if remember_state should be emitted before following CFI directive. */
803 static bool emit_cfa_remember;
805 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
806 or to the CIE if LABEL is NULL. */
808 static void
809 add_fde_cfi (const char *label, dw_cfi_ref cfi)
811 dw_cfi_ref *list_head;
813 if (emit_cfa_remember)
815 dw_cfi_ref cfi_remember;
817 /* Emit the state save. */
818 emit_cfa_remember = false;
819 cfi_remember = new_cfi ();
820 cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
821 add_fde_cfi (label, cfi_remember);
824 list_head = &cie_cfi_head;
826 if (dwarf2out_do_cfi_asm ())
828 if (label)
830 dw_fde_ref fde = current_fde ();
832 gcc_assert (fde != NULL);
834 /* We still have to add the cfi to the list so that lookup_cfa
835 works later on. When -g2 and above we even need to force
836 emitting of CFI labels and add to list a DW_CFA_set_loc for
837 convert_cfa_to_fb_loc_list purposes. If we're generating
838 DWARF3 output we use DW_OP_call_frame_cfa and so don't use
839 convert_cfa_to_fb_loc_list. */
840 if (dwarf_version == 2
841 && debug_info_level > DINFO_LEVEL_TERSE
842 && (write_symbols == DWARF2_DEBUG
843 || write_symbols == VMS_AND_DWARF2_DEBUG))
845 switch (cfi->dw_cfi_opc)
847 case DW_CFA_def_cfa_offset:
848 case DW_CFA_def_cfa_offset_sf:
849 case DW_CFA_def_cfa_register:
850 case DW_CFA_def_cfa:
851 case DW_CFA_def_cfa_sf:
852 case DW_CFA_def_cfa_expression:
853 case DW_CFA_restore_state:
854 if (*label == 0 || strcmp (label, "<do not output>") == 0)
855 label = dwarf2out_cfi_label (true);
857 if (fde->dw_fde_current_label == NULL
858 || strcmp (label, fde->dw_fde_current_label) != 0)
860 dw_cfi_ref xcfi;
862 label = xstrdup (label);
864 /* Set the location counter to the new label. */
865 xcfi = new_cfi ();
866 /* It doesn't metter whether DW_CFA_set_loc
867 or DW_CFA_advance_loc4 is added here, those aren't
868 emitted into assembly, only looked up by
869 convert_cfa_to_fb_loc_list. */
870 xcfi->dw_cfi_opc = DW_CFA_set_loc;
871 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
872 add_cfi (&fde->dw_fde_cfi, xcfi);
873 fde->dw_fde_current_label = label;
875 break;
876 default:
877 break;
881 output_cfi_directive (cfi);
883 list_head = &fde->dw_fde_cfi;
885 /* ??? If this is a CFI for the CIE, we don't emit. This
886 assumes that the standard CIE contents that the assembler
887 uses matches the standard CIE contents that the compiler
888 uses. This is probably a bad assumption. I'm not quite
889 sure how to address this for now. */
891 else if (label)
893 dw_fde_ref fde = current_fde ();
895 gcc_assert (fde != NULL);
897 if (*label == 0)
898 label = dwarf2out_cfi_label (false);
900 if (fde->dw_fde_current_label == NULL
901 || strcmp (label, fde->dw_fde_current_label) != 0)
903 dw_cfi_ref xcfi;
905 label = xstrdup (label);
907 /* Set the location counter to the new label. */
908 xcfi = new_cfi ();
909 /* If we have a current label, advance from there, otherwise
910 set the location directly using set_loc. */
911 xcfi->dw_cfi_opc = fde->dw_fde_current_label
912 ? DW_CFA_advance_loc4
913 : DW_CFA_set_loc;
914 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
915 add_cfi (&fde->dw_fde_cfi, xcfi);
917 fde->dw_fde_current_label = label;
920 list_head = &fde->dw_fde_cfi;
923 add_cfi (list_head, cfi);
926 /* Subroutine of lookup_cfa. */
928 static void
929 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
931 switch (cfi->dw_cfi_opc)
933 case DW_CFA_def_cfa_offset:
934 case DW_CFA_def_cfa_offset_sf:
935 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
936 break;
937 case DW_CFA_def_cfa_register:
938 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
939 break;
940 case DW_CFA_def_cfa:
941 case DW_CFA_def_cfa_sf:
942 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
943 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
944 break;
945 case DW_CFA_def_cfa_expression:
946 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
947 break;
949 case DW_CFA_remember_state:
950 gcc_assert (!remember->in_use);
951 *remember = *loc;
952 remember->in_use = 1;
953 break;
954 case DW_CFA_restore_state:
955 gcc_assert (remember->in_use);
956 *loc = *remember;
957 remember->in_use = 0;
958 break;
960 default:
961 break;
965 /* Find the previous value for the CFA. */
967 static void
968 lookup_cfa (dw_cfa_location *loc)
970 dw_cfi_ref cfi;
971 dw_fde_ref fde;
972 dw_cfa_location remember;
974 memset (loc, 0, sizeof (*loc));
975 loc->reg = INVALID_REGNUM;
976 remember = *loc;
978 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
979 lookup_cfa_1 (cfi, loc, &remember);
981 fde = current_fde ();
982 if (fde)
983 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
984 lookup_cfa_1 (cfi, loc, &remember);
987 /* The current rule for calculating the DWARF2 canonical frame address. */
988 static dw_cfa_location cfa;
990 /* The register used for saving registers to the stack, and its offset
991 from the CFA. */
992 static dw_cfa_location cfa_store;
994 /* The current save location around an epilogue. */
995 static dw_cfa_location cfa_remember;
997 /* The running total of the size of arguments pushed onto the stack. */
998 static HOST_WIDE_INT args_size;
1000 /* The last args_size we actually output. */
1001 static HOST_WIDE_INT old_args_size;
1003 /* Entry point to update the canonical frame address (CFA).
1004 LABEL is passed to add_fde_cfi. The value of CFA is now to be
1005 calculated from REG+OFFSET. */
1007 void
1008 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1010 dw_cfa_location loc;
1011 loc.indirect = 0;
1012 loc.base_offset = 0;
1013 loc.reg = reg;
1014 loc.offset = offset;
1015 def_cfa_1 (label, &loc);
1018 /* Determine if two dw_cfa_location structures define the same data. */
1020 static bool
1021 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1023 return (loc1->reg == loc2->reg
1024 && loc1->offset == loc2->offset
1025 && loc1->indirect == loc2->indirect
1026 && (loc1->indirect == 0
1027 || loc1->base_offset == loc2->base_offset));
1030 /* This routine does the actual work. The CFA is now calculated from
1031 the dw_cfa_location structure. */
1033 static void
1034 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1036 dw_cfi_ref cfi;
1037 dw_cfa_location old_cfa, loc;
1039 cfa = *loc_p;
1040 loc = *loc_p;
1042 if (cfa_store.reg == loc.reg && loc.indirect == 0)
1043 cfa_store.offset = loc.offset;
1045 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1046 lookup_cfa (&old_cfa);
1048 /* If nothing changed, no need to issue any call frame instructions. */
1049 if (cfa_equal_p (&loc, &old_cfa))
1050 return;
1052 cfi = new_cfi ();
1054 if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1056 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1057 the CFA register did not change but the offset did. The data
1058 factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1059 in the assembler via the .cfi_def_cfa_offset directive. */
1060 if (loc.offset < 0)
1061 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1062 else
1063 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1064 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1067 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
1068 else if (loc.offset == old_cfa.offset
1069 && old_cfa.reg != INVALID_REGNUM
1070 && !loc.indirect
1071 && !old_cfa.indirect)
1073 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1074 indicating the CFA register has changed to <register> but the
1075 offset has not changed. */
1076 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1077 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1079 #endif
1081 else if (loc.indirect == 0)
1083 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1084 indicating the CFA register has changed to <register> with
1085 the specified offset. The data factoring for DW_CFA_def_cfa_sf
1086 happens in output_cfi, or in the assembler via the .cfi_def_cfa
1087 directive. */
1088 if (loc.offset < 0)
1089 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1090 else
1091 cfi->dw_cfi_opc = DW_CFA_def_cfa;
1092 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1093 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1095 else
1097 /* Construct a DW_CFA_def_cfa_expression instruction to
1098 calculate the CFA using a full location expression since no
1099 register-offset pair is available. */
1100 struct dw_loc_descr_struct *loc_list;
1102 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1103 loc_list = build_cfa_loc (&loc, 0);
1104 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1107 add_fde_cfi (label, cfi);
1110 /* Add the CFI for saving a register. REG is the CFA column number.
1111 LABEL is passed to add_fde_cfi.
1112 If SREG is -1, the register is saved at OFFSET from the CFA;
1113 otherwise it is saved in SREG. */
1115 static void
1116 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1118 dw_cfi_ref cfi = new_cfi ();
1119 dw_fde_ref fde = current_fde ();
1121 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1123 /* When stack is aligned, store REG using DW_CFA_expression with
1124 FP. */
1125 if (fde
1126 && fde->stack_realign
1127 && sreg == INVALID_REGNUM)
1129 cfi->dw_cfi_opc = DW_CFA_expression;
1130 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1131 cfi->dw_cfi_oprnd2.dw_cfi_loc
1132 = build_cfa_aligned_loc (offset, fde->stack_realignment);
1134 else if (sreg == INVALID_REGNUM)
1136 if (need_data_align_sf_opcode (offset))
1137 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1138 else if (reg & ~0x3f)
1139 cfi->dw_cfi_opc = DW_CFA_offset_extended;
1140 else
1141 cfi->dw_cfi_opc = DW_CFA_offset;
1142 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1144 else if (sreg == reg)
1145 cfi->dw_cfi_opc = DW_CFA_same_value;
1146 else
1148 cfi->dw_cfi_opc = DW_CFA_register;
1149 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1152 add_fde_cfi (label, cfi);
1155 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
1156 This CFI tells the unwinder that it needs to restore the window registers
1157 from the previous frame's window save area.
1159 ??? Perhaps we should note in the CIE where windows are saved (instead of
1160 assuming 0(cfa)) and what registers are in the window. */
1162 void
1163 dwarf2out_window_save (const char *label)
1165 dw_cfi_ref cfi = new_cfi ();
1167 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1168 add_fde_cfi (label, cfi);
1171 /* Entry point for saving a register to the stack. REG is the GCC register
1172 number. LABEL and OFFSET are passed to reg_save. */
1174 void
1175 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1177 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1180 /* Entry point for saving the return address in the stack.
1181 LABEL and OFFSET are passed to reg_save. */
1183 void
1184 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1186 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1189 /* Entry point for saving the return address in a register.
1190 LABEL and SREG are passed to reg_save. */
1192 void
1193 dwarf2out_return_reg (const char *label, unsigned int sreg)
1195 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1198 #ifdef DWARF2_UNWIND_INFO
1199 /* Record the initial position of the return address. RTL is
1200 INCOMING_RETURN_ADDR_RTX. */
1202 static void
1203 initial_return_save (rtx rtl)
1205 unsigned int reg = INVALID_REGNUM;
1206 HOST_WIDE_INT offset = 0;
1208 switch (GET_CODE (rtl))
1210 case REG:
1211 /* RA is in a register. */
1212 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1213 break;
1215 case MEM:
1216 /* RA is on the stack. */
1217 rtl = XEXP (rtl, 0);
1218 switch (GET_CODE (rtl))
1220 case REG:
1221 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1222 offset = 0;
1223 break;
1225 case PLUS:
1226 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1227 offset = INTVAL (XEXP (rtl, 1));
1228 break;
1230 case MINUS:
1231 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1232 offset = -INTVAL (XEXP (rtl, 1));
1233 break;
1235 default:
1236 gcc_unreachable ();
1239 break;
1241 case PLUS:
1242 /* The return address is at some offset from any value we can
1243 actually load. For instance, on the SPARC it is in %i7+8. Just
1244 ignore the offset for now; it doesn't matter for unwinding frames. */
1245 gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1246 initial_return_save (XEXP (rtl, 0));
1247 return;
1249 default:
1250 gcc_unreachable ();
1253 if (reg != DWARF_FRAME_RETURN_COLUMN)
1254 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1256 #endif
1258 /* Given a SET, calculate the amount of stack adjustment it
1259 contains. */
1261 static HOST_WIDE_INT
1262 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1263 HOST_WIDE_INT cur_offset)
1265 const_rtx src = SET_SRC (pattern);
1266 const_rtx dest = SET_DEST (pattern);
1267 HOST_WIDE_INT offset = 0;
1268 enum rtx_code code;
1270 if (dest == stack_pointer_rtx)
1272 code = GET_CODE (src);
1274 /* Assume (set (reg sp) (reg whatever)) sets args_size
1275 level to 0. */
1276 if (code == REG && src != stack_pointer_rtx)
1278 offset = -cur_args_size;
1279 #ifndef STACK_GROWS_DOWNWARD
1280 offset = -offset;
1281 #endif
1282 return offset - cur_offset;
1285 if (! (code == PLUS || code == MINUS)
1286 || XEXP (src, 0) != stack_pointer_rtx
1287 || !CONST_INT_P (XEXP (src, 1)))
1288 return 0;
1290 /* (set (reg sp) (plus (reg sp) (const_int))) */
1291 offset = INTVAL (XEXP (src, 1));
1292 if (code == PLUS)
1293 offset = -offset;
1294 return offset;
1297 if (MEM_P (src) && !MEM_P (dest))
1298 dest = src;
1299 if (MEM_P (dest))
1301 /* (set (mem (pre_dec (reg sp))) (foo)) */
1302 src = XEXP (dest, 0);
1303 code = GET_CODE (src);
1305 switch (code)
1307 case PRE_MODIFY:
1308 case POST_MODIFY:
1309 if (XEXP (src, 0) == stack_pointer_rtx)
1311 rtx val = XEXP (XEXP (src, 1), 1);
1312 /* We handle only adjustments by constant amount. */
1313 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1314 && CONST_INT_P (val));
1315 offset = -INTVAL (val);
1316 break;
1318 return 0;
1320 case PRE_DEC:
1321 case POST_DEC:
1322 if (XEXP (src, 0) == stack_pointer_rtx)
1324 offset = GET_MODE_SIZE (GET_MODE (dest));
1325 break;
1327 return 0;
1329 case PRE_INC:
1330 case POST_INC:
1331 if (XEXP (src, 0) == stack_pointer_rtx)
1333 offset = -GET_MODE_SIZE (GET_MODE (dest));
1334 break;
1336 return 0;
1338 default:
1339 return 0;
1342 else
1343 return 0;
1345 return offset;
1348 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1349 indexed by INSN_UID. */
1351 static HOST_WIDE_INT *barrier_args_size;
1353 /* Helper function for compute_barrier_args_size. Handle one insn. */
1355 static HOST_WIDE_INT
1356 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1357 VEC (rtx, heap) **next)
1359 HOST_WIDE_INT offset = 0;
1360 int i;
1362 if (! RTX_FRAME_RELATED_P (insn))
1364 if (prologue_epilogue_contains (insn))
1365 /* Nothing */;
1366 else if (GET_CODE (PATTERN (insn)) == SET)
1367 offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1368 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1369 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1371 /* There may be stack adjustments inside compound insns. Search
1372 for them. */
1373 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1374 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1375 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1376 cur_args_size, offset);
1379 else
1381 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1383 if (expr)
1385 expr = XEXP (expr, 0);
1386 if (GET_CODE (expr) == PARALLEL
1387 || GET_CODE (expr) == SEQUENCE)
1388 for (i = 1; i < XVECLEN (expr, 0); i++)
1390 rtx elem = XVECEXP (expr, 0, i);
1392 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1393 offset += stack_adjust_offset (elem, cur_args_size, offset);
1398 #ifndef STACK_GROWS_DOWNWARD
1399 offset = -offset;
1400 #endif
1402 cur_args_size += offset;
1403 if (cur_args_size < 0)
1404 cur_args_size = 0;
1406 if (JUMP_P (insn))
1408 rtx dest = JUMP_LABEL (insn);
1410 if (dest)
1412 if (barrier_args_size [INSN_UID (dest)] < 0)
1414 barrier_args_size [INSN_UID (dest)] = cur_args_size;
1415 VEC_safe_push (rtx, heap, *next, dest);
1420 return cur_args_size;
1423 /* Walk the whole function and compute args_size on BARRIERs. */
1425 static void
1426 compute_barrier_args_size (void)
1428 int max_uid = get_max_uid (), i;
1429 rtx insn;
1430 VEC (rtx, heap) *worklist, *next, *tmp;
1432 barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1433 for (i = 0; i < max_uid; i++)
1434 barrier_args_size[i] = -1;
1436 worklist = VEC_alloc (rtx, heap, 20);
1437 next = VEC_alloc (rtx, heap, 20);
1438 insn = get_insns ();
1439 barrier_args_size[INSN_UID (insn)] = 0;
1440 VEC_quick_push (rtx, worklist, insn);
1441 for (;;)
1443 while (!VEC_empty (rtx, worklist))
1445 rtx prev, body, first_insn;
1446 HOST_WIDE_INT cur_args_size;
1448 first_insn = insn = VEC_pop (rtx, worklist);
1449 cur_args_size = barrier_args_size[INSN_UID (insn)];
1450 prev = prev_nonnote_insn (insn);
1451 if (prev && BARRIER_P (prev))
1452 barrier_args_size[INSN_UID (prev)] = cur_args_size;
1454 for (; insn; insn = NEXT_INSN (insn))
1456 if (INSN_DELETED_P (insn) || NOTE_P (insn))
1457 continue;
1458 if (BARRIER_P (insn))
1459 break;
1461 if (LABEL_P (insn))
1463 if (insn == first_insn)
1464 continue;
1465 else if (barrier_args_size[INSN_UID (insn)] < 0)
1467 barrier_args_size[INSN_UID (insn)] = cur_args_size;
1468 continue;
1470 else
1472 /* The insns starting with this label have been
1473 already scanned or are in the worklist. */
1474 break;
1478 body = PATTERN (insn);
1479 if (GET_CODE (body) == SEQUENCE)
1481 HOST_WIDE_INT dest_args_size = cur_args_size;
1482 for (i = 1; i < XVECLEN (body, 0); i++)
1483 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1484 && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1485 dest_args_size
1486 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1487 dest_args_size, &next);
1488 else
1489 cur_args_size
1490 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1491 cur_args_size, &next);
1493 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1494 compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1495 dest_args_size, &next);
1496 else
1497 cur_args_size
1498 = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1499 cur_args_size, &next);
1501 else
1502 cur_args_size
1503 = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1507 if (VEC_empty (rtx, next))
1508 break;
1510 /* Swap WORKLIST with NEXT and truncate NEXT for next iteration. */
1511 tmp = next;
1512 next = worklist;
1513 worklist = tmp;
1514 VEC_truncate (rtx, next, 0);
1517 VEC_free (rtx, heap, worklist);
1518 VEC_free (rtx, heap, next);
1521 /* Add a CFI to update the running total of the size of arguments
1522 pushed onto the stack. */
1524 static void
1525 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1527 dw_cfi_ref cfi;
1529 if (size == old_args_size)
1530 return;
1532 old_args_size = size;
1534 cfi = new_cfi ();
1535 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1536 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1537 add_fde_cfi (label, cfi);
1540 /* Record a stack adjustment of OFFSET bytes. */
1542 static void
1543 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1545 if (cfa.reg == STACK_POINTER_REGNUM)
1546 cfa.offset += offset;
1548 if (cfa_store.reg == STACK_POINTER_REGNUM)
1549 cfa_store.offset += offset;
1551 if (ACCUMULATE_OUTGOING_ARGS)
1552 return;
1554 #ifndef STACK_GROWS_DOWNWARD
1555 offset = -offset;
1556 #endif
1558 args_size += offset;
1559 if (args_size < 0)
1560 args_size = 0;
1562 def_cfa_1 (label, &cfa);
1563 if (flag_asynchronous_unwind_tables)
1564 dwarf2out_args_size (label, args_size);
1567 /* Check INSN to see if it looks like a push or a stack adjustment, and
1568 make a note of it if it does. EH uses this information to find out
1569 how much extra space it needs to pop off the stack. */
1571 static void
1572 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1574 HOST_WIDE_INT offset;
1575 const char *label;
1576 int i;
1578 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1579 with this function. Proper support would require all frame-related
1580 insns to be marked, and to be able to handle saving state around
1581 epilogues textually in the middle of the function. */
1582 if (prologue_epilogue_contains (insn))
1583 return;
1585 /* If INSN is an instruction from target of an annulled branch, the
1586 effects are for the target only and so current argument size
1587 shouldn't change at all. */
1588 if (final_sequence
1589 && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1590 && INSN_FROM_TARGET_P (insn))
1591 return;
1593 /* If only calls can throw, and we have a frame pointer,
1594 save up adjustments until we see the CALL_INSN. */
1595 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1597 if (CALL_P (insn) && !after_p)
1599 /* Extract the size of the args from the CALL rtx itself. */
1600 insn = PATTERN (insn);
1601 if (GET_CODE (insn) == PARALLEL)
1602 insn = XVECEXP (insn, 0, 0);
1603 if (GET_CODE (insn) == SET)
1604 insn = SET_SRC (insn);
1605 gcc_assert (GET_CODE (insn) == CALL);
1606 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1608 return;
1611 if (CALL_P (insn) && !after_p)
1613 if (!flag_asynchronous_unwind_tables)
1614 dwarf2out_args_size ("", args_size);
1615 return;
1617 else if (BARRIER_P (insn))
1619 /* Don't call compute_barrier_args_size () if the only
1620 BARRIER is at the end of function. */
1621 if (barrier_args_size == NULL && next_nonnote_insn (insn))
1622 compute_barrier_args_size ();
1623 if (barrier_args_size == NULL)
1624 offset = 0;
1625 else
1627 offset = barrier_args_size[INSN_UID (insn)];
1628 if (offset < 0)
1629 offset = 0;
1632 offset -= args_size;
1633 #ifndef STACK_GROWS_DOWNWARD
1634 offset = -offset;
1635 #endif
1637 else if (GET_CODE (PATTERN (insn)) == SET)
1638 offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1639 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1640 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1642 /* There may be stack adjustments inside compound insns. Search
1643 for them. */
1644 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1645 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1646 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1647 args_size, offset);
1649 else
1650 return;
1652 if (offset == 0)
1653 return;
1655 label = dwarf2out_cfi_label (false);
1656 dwarf2out_stack_adjust (offset, label);
1659 #endif
1661 /* We delay emitting a register save until either (a) we reach the end
1662 of the prologue or (b) the register is clobbered. This clusters
1663 register saves so that there are fewer pc advances. */
1665 struct GTY(()) queued_reg_save {
1666 struct queued_reg_save *next;
1667 rtx reg;
1668 HOST_WIDE_INT cfa_offset;
1669 rtx saved_reg;
1672 static GTY(()) struct queued_reg_save *queued_reg_saves;
1674 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1675 struct GTY(()) reg_saved_in_data {
1676 rtx orig_reg;
1677 rtx saved_in_reg;
1680 /* A list of registers saved in other registers.
1681 The list intentionally has a small maximum capacity of 4; if your
1682 port needs more than that, you might consider implementing a
1683 more efficient data structure. */
1684 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1685 static GTY(()) size_t num_regs_saved_in_regs;
1687 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1688 static const char *last_reg_save_label;
1690 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1691 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1693 static void
1694 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1696 struct queued_reg_save *q;
1698 /* Duplicates waste space, but it's also necessary to remove them
1699 for correctness, since the queue gets output in reverse
1700 order. */
1701 for (q = queued_reg_saves; q != NULL; q = q->next)
1702 if (REGNO (q->reg) == REGNO (reg))
1703 break;
1705 if (q == NULL)
1707 q = ggc_alloc_queued_reg_save ();
1708 q->next = queued_reg_saves;
1709 queued_reg_saves = q;
1712 q->reg = reg;
1713 q->cfa_offset = offset;
1714 q->saved_reg = sreg;
1716 last_reg_save_label = label;
1719 /* Output all the entries in QUEUED_REG_SAVES. */
1721 static void
1722 flush_queued_reg_saves (void)
1724 struct queued_reg_save *q;
1726 for (q = queued_reg_saves; q; q = q->next)
1728 size_t i;
1729 unsigned int reg, sreg;
1731 for (i = 0; i < num_regs_saved_in_regs; i++)
1732 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1733 break;
1734 if (q->saved_reg && i == num_regs_saved_in_regs)
1736 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1737 num_regs_saved_in_regs++;
1739 if (i != num_regs_saved_in_regs)
1741 regs_saved_in_regs[i].orig_reg = q->reg;
1742 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1745 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1746 if (q->saved_reg)
1747 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1748 else
1749 sreg = INVALID_REGNUM;
1750 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1753 queued_reg_saves = NULL;
1754 last_reg_save_label = NULL;
1757 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1758 location for? Or, does it clobber a register which we've previously
1759 said that some other register is saved in, and for which we now
1760 have a new location for? */
1762 static bool
1763 clobbers_queued_reg_save (const_rtx insn)
1765 struct queued_reg_save *q;
1767 for (q = queued_reg_saves; q; q = q->next)
1769 size_t i;
1770 if (modified_in_p (q->reg, insn))
1771 return true;
1772 for (i = 0; i < num_regs_saved_in_regs; i++)
1773 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1774 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1775 return true;
1778 return false;
1781 /* Entry point for saving the first register into the second. */
1783 void
1784 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1786 size_t i;
1787 unsigned int regno, sregno;
1789 for (i = 0; i < num_regs_saved_in_regs; i++)
1790 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1791 break;
1792 if (i == num_regs_saved_in_regs)
1794 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1795 num_regs_saved_in_regs++;
1797 regs_saved_in_regs[i].orig_reg = reg;
1798 regs_saved_in_regs[i].saved_in_reg = sreg;
1800 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1801 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1802 reg_save (label, regno, sregno, 0);
1805 /* What register, if any, is currently saved in REG? */
1807 static rtx
1808 reg_saved_in (rtx reg)
1810 unsigned int regn = REGNO (reg);
1811 size_t i;
1812 struct queued_reg_save *q;
1814 for (q = queued_reg_saves; q; q = q->next)
1815 if (q->saved_reg && regn == REGNO (q->saved_reg))
1816 return q->reg;
1818 for (i = 0; i < num_regs_saved_in_regs; i++)
1819 if (regs_saved_in_regs[i].saved_in_reg
1820 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1821 return regs_saved_in_regs[i].orig_reg;
1823 return NULL_RTX;
1827 /* A temporary register holding an integral value used in adjusting SP
1828 or setting up the store_reg. The "offset" field holds the integer
1829 value, not an offset. */
1830 static dw_cfa_location cfa_temp;
1832 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note. */
1834 static void
1835 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1837 memset (&cfa, 0, sizeof (cfa));
1839 switch (GET_CODE (pat))
1841 case PLUS:
1842 cfa.reg = REGNO (XEXP (pat, 0));
1843 cfa.offset = INTVAL (XEXP (pat, 1));
1844 break;
1846 case REG:
1847 cfa.reg = REGNO (pat);
1848 break;
1850 default:
1851 /* Recurse and define an expression. */
1852 gcc_unreachable ();
1855 def_cfa_1 (label, &cfa);
1858 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note. */
1860 static void
1861 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1863 rtx src, dest;
1865 gcc_assert (GET_CODE (pat) == SET);
1866 dest = XEXP (pat, 0);
1867 src = XEXP (pat, 1);
1869 switch (GET_CODE (src))
1871 case PLUS:
1872 gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1873 cfa.offset -= INTVAL (XEXP (src, 1));
1874 break;
1876 case REG:
1877 break;
1879 default:
1880 gcc_unreachable ();
1883 cfa.reg = REGNO (dest);
1884 gcc_assert (cfa.indirect == 0);
1886 def_cfa_1 (label, &cfa);
1889 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note. */
1891 static void
1892 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1894 HOST_WIDE_INT offset;
1895 rtx src, addr, span;
1897 src = XEXP (set, 1);
1898 addr = XEXP (set, 0);
1899 gcc_assert (MEM_P (addr));
1900 addr = XEXP (addr, 0);
1902 /* As documented, only consider extremely simple addresses. */
1903 switch (GET_CODE (addr))
1905 case REG:
1906 gcc_assert (REGNO (addr) == cfa.reg);
1907 offset = -cfa.offset;
1908 break;
1909 case PLUS:
1910 gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1911 offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1912 break;
1913 default:
1914 gcc_unreachable ();
1917 span = targetm.dwarf_register_span (src);
1919 /* ??? We'd like to use queue_reg_save, but we need to come up with
1920 a different flushing heuristic for epilogues. */
1921 if (!span)
1922 reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1923 else
1925 /* We have a PARALLEL describing where the contents of SRC live.
1926 Queue register saves for each piece of the PARALLEL. */
1927 int par_index;
1928 int limit;
1929 HOST_WIDE_INT span_offset = offset;
1931 gcc_assert (GET_CODE (span) == PARALLEL);
1933 limit = XVECLEN (span, 0);
1934 for (par_index = 0; par_index < limit; par_index++)
1936 rtx elem = XVECEXP (span, 0, par_index);
1938 reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1939 INVALID_REGNUM, span_offset);
1940 span_offset += GET_MODE_SIZE (GET_MODE (elem));
1945 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note. */
1947 static void
1948 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1950 rtx src, dest;
1951 unsigned sregno, dregno;
1953 src = XEXP (set, 1);
1954 dest = XEXP (set, 0);
1956 if (src == pc_rtx)
1957 sregno = DWARF_FRAME_RETURN_COLUMN;
1958 else
1959 sregno = DWARF_FRAME_REGNUM (REGNO (src));
1961 dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1963 /* ??? We'd like to use queue_reg_save, but we need to come up with
1964 a different flushing heuristic for epilogues. */
1965 reg_save (label, sregno, dregno, 0);
1968 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note. */
1970 static void
1971 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1973 dw_cfi_ref cfi = new_cfi ();
1974 unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1976 cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1977 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1979 add_fde_cfi (label, cfi);
1982 /* Record call frame debugging information for an expression EXPR,
1983 which either sets SP or FP (adjusting how we calculate the frame
1984 address) or saves a register to the stack or another register.
1985 LABEL indicates the address of EXPR.
1987 This function encodes a state machine mapping rtxes to actions on
1988 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1989 users need not read the source code.
1991 The High-Level Picture
1993 Changes in the register we use to calculate the CFA: Currently we
1994 assume that if you copy the CFA register into another register, we
1995 should take the other one as the new CFA register; this seems to
1996 work pretty well. If it's wrong for some target, it's simple
1997 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1999 Changes in the register we use for saving registers to the stack:
2000 This is usually SP, but not always. Again, we deduce that if you
2001 copy SP into another register (and SP is not the CFA register),
2002 then the new register is the one we will be using for register
2003 saves. This also seems to work.
2005 Register saves: There's not much guesswork about this one; if
2006 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2007 register save, and the register used to calculate the destination
2008 had better be the one we think we're using for this purpose.
2009 It's also assumed that a copy from a call-saved register to another
2010 register is saving that register if RTX_FRAME_RELATED_P is set on
2011 that instruction. If the copy is from a call-saved register to
2012 the *same* register, that means that the register is now the same
2013 value as in the caller.
2015 Except: If the register being saved is the CFA register, and the
2016 offset is nonzero, we are saving the CFA, so we assume we have to
2017 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
2018 the intent is to save the value of SP from the previous frame.
2020 In addition, if a register has previously been saved to a different
2021 register,
2023 Invariants / Summaries of Rules
2025 cfa current rule for calculating the CFA. It usually
2026 consists of a register and an offset.
2027 cfa_store register used by prologue code to save things to the stack
2028 cfa_store.offset is the offset from the value of
2029 cfa_store.reg to the actual CFA
2030 cfa_temp register holding an integral value. cfa_temp.offset
2031 stores the value, which will be used to adjust the
2032 stack pointer. cfa_temp is also used like cfa_store,
2033 to track stores to the stack via fp or a temp reg.
2035 Rules 1- 4: Setting a register's value to cfa.reg or an expression
2036 with cfa.reg as the first operand changes the cfa.reg and its
2037 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
2038 cfa_temp.offset.
2040 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
2041 expression yielding a constant. This sets cfa_temp.reg
2042 and cfa_temp.offset.
2044 Rule 5: Create a new register cfa_store used to save items to the
2045 stack.
2047 Rules 10-14: Save a register to the stack. Define offset as the
2048 difference of the original location and cfa_store's
2049 location (or cfa_temp's location if cfa_temp is used).
2051 Rules 16-20: If AND operation happens on sp in prologue, we assume
2052 stack is realigned. We will use a group of DW_OP_XXX
2053 expressions to represent the location of the stored
2054 register instead of CFA+offset.
2056 The Rules
2058 "{a,b}" indicates a choice of a xor b.
2059 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2061 Rule 1:
2062 (set <reg1> <reg2>:cfa.reg)
2063 effects: cfa.reg = <reg1>
2064 cfa.offset unchanged
2065 cfa_temp.reg = <reg1>
2066 cfa_temp.offset = cfa.offset
2068 Rule 2:
2069 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2070 {<const_int>,<reg>:cfa_temp.reg}))
2071 effects: cfa.reg = sp if fp used
2072 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2073 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2074 if cfa_store.reg==sp
2076 Rule 3:
2077 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2078 effects: cfa.reg = fp
2079 cfa_offset += +/- <const_int>
2081 Rule 4:
2082 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2083 constraints: <reg1> != fp
2084 <reg1> != sp
2085 effects: cfa.reg = <reg1>
2086 cfa_temp.reg = <reg1>
2087 cfa_temp.offset = cfa.offset
2089 Rule 5:
2090 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2091 constraints: <reg1> != fp
2092 <reg1> != sp
2093 effects: cfa_store.reg = <reg1>
2094 cfa_store.offset = cfa.offset - cfa_temp.offset
2096 Rule 6:
2097 (set <reg> <const_int>)
2098 effects: cfa_temp.reg = <reg>
2099 cfa_temp.offset = <const_int>
2101 Rule 7:
2102 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2103 effects: cfa_temp.reg = <reg1>
2104 cfa_temp.offset |= <const_int>
2106 Rule 8:
2107 (set <reg> (high <exp>))
2108 effects: none
2110 Rule 9:
2111 (set <reg> (lo_sum <exp> <const_int>))
2112 effects: cfa_temp.reg = <reg>
2113 cfa_temp.offset = <const_int>
2115 Rule 10:
2116 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2117 effects: cfa_store.offset -= <const_int>
2118 cfa.offset = cfa_store.offset if cfa.reg == sp
2119 cfa.reg = sp
2120 cfa.base_offset = -cfa_store.offset
2122 Rule 11:
2123 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2124 effects: cfa_store.offset += -/+ mode_size(mem)
2125 cfa.offset = cfa_store.offset if cfa.reg == sp
2126 cfa.reg = sp
2127 cfa.base_offset = -cfa_store.offset
2129 Rule 12:
2130 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2132 <reg2>)
2133 effects: cfa.reg = <reg1>
2134 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2136 Rule 13:
2137 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2138 effects: cfa.reg = <reg1>
2139 cfa.base_offset = -{cfa_store,cfa_temp}.offset
2141 Rule 14:
2142 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2143 effects: cfa.reg = <reg1>
2144 cfa.base_offset = -cfa_temp.offset
2145 cfa_temp.offset -= mode_size(mem)
2147 Rule 15:
2148 (set <reg> {unspec, unspec_volatile})
2149 effects: target-dependent
2151 Rule 16:
2152 (set sp (and: sp <const_int>))
2153 constraints: cfa_store.reg == sp
2154 effects: current_fde.stack_realign = 1
2155 cfa_store.offset = 0
2156 fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2158 Rule 17:
2159 (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2160 effects: cfa_store.offset += -/+ mode_size(mem)
2162 Rule 18:
2163 (set (mem ({pre_inc, pre_dec} sp)) fp)
2164 constraints: fde->stack_realign == 1
2165 effects: cfa_store.offset = 0
2166 cfa.reg != HARD_FRAME_POINTER_REGNUM
2168 Rule 19:
2169 (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2170 constraints: fde->stack_realign == 1
2171 && cfa.offset == 0
2172 && cfa.indirect == 0
2173 && cfa.reg != HARD_FRAME_POINTER_REGNUM
2174 effects: Use DW_CFA_def_cfa_expression to define cfa
2175 cfa.reg == fde->drap_reg */
2177 static void
2178 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2180 rtx src, dest, span;
2181 HOST_WIDE_INT offset;
2182 dw_fde_ref fde;
2184 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2185 the PARALLEL independently. The first element is always processed if
2186 it is a SET. This is for backward compatibility. Other elements
2187 are processed only if they are SETs and the RTX_FRAME_RELATED_P
2188 flag is set in them. */
2189 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2191 int par_index;
2192 int limit = XVECLEN (expr, 0);
2193 rtx elem;
2195 /* PARALLELs have strict read-modify-write semantics, so we
2196 ought to evaluate every rvalue before changing any lvalue.
2197 It's cumbersome to do that in general, but there's an
2198 easy approximation that is enough for all current users:
2199 handle register saves before register assignments. */
2200 if (GET_CODE (expr) == PARALLEL)
2201 for (par_index = 0; par_index < limit; par_index++)
2203 elem = XVECEXP (expr, 0, par_index);
2204 if (GET_CODE (elem) == SET
2205 && MEM_P (SET_DEST (elem))
2206 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2207 dwarf2out_frame_debug_expr (elem, label);
2210 for (par_index = 0; par_index < limit; par_index++)
2212 elem = XVECEXP (expr, 0, par_index);
2213 if (GET_CODE (elem) == SET
2214 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2215 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2216 dwarf2out_frame_debug_expr (elem, label);
2217 else if (GET_CODE (elem) == SET
2218 && par_index != 0
2219 && !RTX_FRAME_RELATED_P (elem))
2221 /* Stack adjustment combining might combine some post-prologue
2222 stack adjustment into a prologue stack adjustment. */
2223 HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2225 if (offset != 0)
2226 dwarf2out_stack_adjust (offset, label);
2229 return;
2232 gcc_assert (GET_CODE (expr) == SET);
2234 src = SET_SRC (expr);
2235 dest = SET_DEST (expr);
2237 if (REG_P (src))
2239 rtx rsi = reg_saved_in (src);
2240 if (rsi)
2241 src = rsi;
2244 fde = current_fde ();
2246 switch (GET_CODE (dest))
2248 case REG:
2249 switch (GET_CODE (src))
2251 /* Setting FP from SP. */
2252 case REG:
2253 if (cfa.reg == (unsigned) REGNO (src))
2255 /* Rule 1 */
2256 /* Update the CFA rule wrt SP or FP. Make sure src is
2257 relative to the current CFA register.
2259 We used to require that dest be either SP or FP, but the
2260 ARM copies SP to a temporary register, and from there to
2261 FP. So we just rely on the backends to only set
2262 RTX_FRAME_RELATED_P on appropriate insns. */
2263 cfa.reg = REGNO (dest);
2264 cfa_temp.reg = cfa.reg;
2265 cfa_temp.offset = cfa.offset;
2267 else
2269 /* Saving a register in a register. */
2270 gcc_assert (!fixed_regs [REGNO (dest)]
2271 /* For the SPARC and its register window. */
2272 || (DWARF_FRAME_REGNUM (REGNO (src))
2273 == DWARF_FRAME_RETURN_COLUMN));
2275 /* After stack is aligned, we can only save SP in FP
2276 if drap register is used. In this case, we have
2277 to restore stack pointer with the CFA value and we
2278 don't generate this DWARF information. */
2279 if (fde
2280 && fde->stack_realign
2281 && REGNO (src) == STACK_POINTER_REGNUM)
2282 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2283 && fde->drap_reg != INVALID_REGNUM
2284 && cfa.reg != REGNO (src));
2285 else
2286 queue_reg_save (label, src, dest, 0);
2288 break;
2290 case PLUS:
2291 case MINUS:
2292 case LO_SUM:
2293 if (dest == stack_pointer_rtx)
2295 /* Rule 2 */
2296 /* Adjusting SP. */
2297 switch (GET_CODE (XEXP (src, 1)))
2299 case CONST_INT:
2300 offset = INTVAL (XEXP (src, 1));
2301 break;
2302 case REG:
2303 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2304 == cfa_temp.reg);
2305 offset = cfa_temp.offset;
2306 break;
2307 default:
2308 gcc_unreachable ();
2311 if (XEXP (src, 0) == hard_frame_pointer_rtx)
2313 /* Restoring SP from FP in the epilogue. */
2314 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2315 cfa.reg = STACK_POINTER_REGNUM;
2317 else if (GET_CODE (src) == LO_SUM)
2318 /* Assume we've set the source reg of the LO_SUM from sp. */
2320 else
2321 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2323 if (GET_CODE (src) != MINUS)
2324 offset = -offset;
2325 if (cfa.reg == STACK_POINTER_REGNUM)
2326 cfa.offset += offset;
2327 if (cfa_store.reg == STACK_POINTER_REGNUM)
2328 cfa_store.offset += offset;
2330 else if (dest == hard_frame_pointer_rtx)
2332 /* Rule 3 */
2333 /* Either setting the FP from an offset of the SP,
2334 or adjusting the FP */
2335 gcc_assert (frame_pointer_needed);
2337 gcc_assert (REG_P (XEXP (src, 0))
2338 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2339 && CONST_INT_P (XEXP (src, 1)));
2340 offset = INTVAL (XEXP (src, 1));
2341 if (GET_CODE (src) != MINUS)
2342 offset = -offset;
2343 cfa.offset += offset;
2344 cfa.reg = HARD_FRAME_POINTER_REGNUM;
2346 else
2348 gcc_assert (GET_CODE (src) != MINUS);
2350 /* Rule 4 */
2351 if (REG_P (XEXP (src, 0))
2352 && REGNO (XEXP (src, 0)) == cfa.reg
2353 && CONST_INT_P (XEXP (src, 1)))
2355 /* Setting a temporary CFA register that will be copied
2356 into the FP later on. */
2357 offset = - INTVAL (XEXP (src, 1));
2358 cfa.offset += offset;
2359 cfa.reg = REGNO (dest);
2360 /* Or used to save regs to the stack. */
2361 cfa_temp.reg = cfa.reg;
2362 cfa_temp.offset = cfa.offset;
2365 /* Rule 5 */
2366 else if (REG_P (XEXP (src, 0))
2367 && REGNO (XEXP (src, 0)) == cfa_temp.reg
2368 && XEXP (src, 1) == stack_pointer_rtx)
2370 /* Setting a scratch register that we will use instead
2371 of SP for saving registers to the stack. */
2372 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2373 cfa_store.reg = REGNO (dest);
2374 cfa_store.offset = cfa.offset - cfa_temp.offset;
2377 /* Rule 9 */
2378 else if (GET_CODE (src) == LO_SUM
2379 && CONST_INT_P (XEXP (src, 1)))
2381 cfa_temp.reg = REGNO (dest);
2382 cfa_temp.offset = INTVAL (XEXP (src, 1));
2384 else
2385 gcc_unreachable ();
2387 break;
2389 /* Rule 6 */
2390 case CONST_INT:
2391 cfa_temp.reg = REGNO (dest);
2392 cfa_temp.offset = INTVAL (src);
2393 break;
2395 /* Rule 7 */
2396 case IOR:
2397 gcc_assert (REG_P (XEXP (src, 0))
2398 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2399 && CONST_INT_P (XEXP (src, 1)));
2401 if ((unsigned) REGNO (dest) != cfa_temp.reg)
2402 cfa_temp.reg = REGNO (dest);
2403 cfa_temp.offset |= INTVAL (XEXP (src, 1));
2404 break;
2406 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2407 which will fill in all of the bits. */
2408 /* Rule 8 */
2409 case HIGH:
2410 break;
2412 /* Rule 15 */
2413 case UNSPEC:
2414 case UNSPEC_VOLATILE:
2415 gcc_assert (targetm.dwarf_handle_frame_unspec);
2416 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2417 return;
2419 /* Rule 16 */
2420 case AND:
2421 /* If this AND operation happens on stack pointer in prologue,
2422 we assume the stack is realigned and we extract the
2423 alignment. */
2424 if (fde && XEXP (src, 0) == stack_pointer_rtx)
2426 gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2427 fde->stack_realign = 1;
2428 fde->stack_realignment = INTVAL (XEXP (src, 1));
2429 cfa_store.offset = 0;
2431 if (cfa.reg != STACK_POINTER_REGNUM
2432 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2433 fde->drap_reg = cfa.reg;
2435 return;
2437 default:
2438 gcc_unreachable ();
2441 def_cfa_1 (label, &cfa);
2442 break;
2444 case MEM:
2446 /* Saving a register to the stack. Make sure dest is relative to the
2447 CFA register. */
2448 switch (GET_CODE (XEXP (dest, 0)))
2450 /* Rule 10 */
2451 /* With a push. */
2452 case PRE_MODIFY:
2453 /* We can't handle variable size modifications. */
2454 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2455 == CONST_INT);
2456 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2458 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2459 && cfa_store.reg == STACK_POINTER_REGNUM);
2461 cfa_store.offset += offset;
2462 if (cfa.reg == STACK_POINTER_REGNUM)
2463 cfa.offset = cfa_store.offset;
2465 offset = -cfa_store.offset;
2466 break;
2468 /* Rule 11 */
2469 case PRE_INC:
2470 case PRE_DEC:
2471 offset = GET_MODE_SIZE (GET_MODE (dest));
2472 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2473 offset = -offset;
2475 gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2476 == STACK_POINTER_REGNUM)
2477 && cfa_store.reg == STACK_POINTER_REGNUM);
2479 cfa_store.offset += offset;
2481 /* Rule 18: If stack is aligned, we will use FP as a
2482 reference to represent the address of the stored
2483 regiser. */
2484 if (fde
2485 && fde->stack_realign
2486 && src == hard_frame_pointer_rtx)
2488 gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2489 cfa_store.offset = 0;
2492 if (cfa.reg == STACK_POINTER_REGNUM)
2493 cfa.offset = cfa_store.offset;
2495 offset = -cfa_store.offset;
2496 break;
2498 /* Rule 12 */
2499 /* With an offset. */
2500 case PLUS:
2501 case MINUS:
2502 case LO_SUM:
2504 int regno;
2506 gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2507 && REG_P (XEXP (XEXP (dest, 0), 0)));
2508 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2509 if (GET_CODE (XEXP (dest, 0)) == MINUS)
2510 offset = -offset;
2512 regno = REGNO (XEXP (XEXP (dest, 0), 0));
2514 if (cfa_store.reg == (unsigned) regno)
2515 offset -= cfa_store.offset;
2516 else
2518 gcc_assert (cfa_temp.reg == (unsigned) regno);
2519 offset -= cfa_temp.offset;
2522 break;
2524 /* Rule 13 */
2525 /* Without an offset. */
2526 case REG:
2528 int regno = REGNO (XEXP (dest, 0));
2530 if (cfa_store.reg == (unsigned) regno)
2531 offset = -cfa_store.offset;
2532 else
2534 gcc_assert (cfa_temp.reg == (unsigned) regno);
2535 offset = -cfa_temp.offset;
2538 break;
2540 /* Rule 14 */
2541 case POST_INC:
2542 gcc_assert (cfa_temp.reg
2543 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2544 offset = -cfa_temp.offset;
2545 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2546 break;
2548 default:
2549 gcc_unreachable ();
2552 /* Rule 17 */
2553 /* If the source operand of this MEM operation is not a
2554 register, basically the source is return address. Here
2555 we only care how much stack grew and we don't save it. */
2556 if (!REG_P (src))
2557 break;
2559 if (REGNO (src) != STACK_POINTER_REGNUM
2560 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2561 && (unsigned) REGNO (src) == cfa.reg)
2563 /* We're storing the current CFA reg into the stack. */
2565 if (cfa.offset == 0)
2567 /* Rule 19 */
2568 /* If stack is aligned, putting CFA reg into stack means
2569 we can no longer use reg + offset to represent CFA.
2570 Here we use DW_CFA_def_cfa_expression instead. The
2571 result of this expression equals to the original CFA
2572 value. */
2573 if (fde
2574 && fde->stack_realign
2575 && cfa.indirect == 0
2576 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2578 dw_cfa_location cfa_exp;
2580 gcc_assert (fde->drap_reg == cfa.reg);
2582 cfa_exp.indirect = 1;
2583 cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2584 cfa_exp.base_offset = offset;
2585 cfa_exp.offset = 0;
2587 fde->drap_reg_saved = 1;
2589 def_cfa_1 (label, &cfa_exp);
2590 break;
2593 /* If the source register is exactly the CFA, assume
2594 we're saving SP like any other register; this happens
2595 on the ARM. */
2596 def_cfa_1 (label, &cfa);
2597 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2598 break;
2600 else
2602 /* Otherwise, we'll need to look in the stack to
2603 calculate the CFA. */
2604 rtx x = XEXP (dest, 0);
2606 if (!REG_P (x))
2607 x = XEXP (x, 0);
2608 gcc_assert (REG_P (x));
2610 cfa.reg = REGNO (x);
2611 cfa.base_offset = offset;
2612 cfa.indirect = 1;
2613 def_cfa_1 (label, &cfa);
2614 break;
2618 def_cfa_1 (label, &cfa);
2620 span = targetm.dwarf_register_span (src);
2622 if (!span)
2623 queue_reg_save (label, src, NULL_RTX, offset);
2624 else
2626 /* We have a PARALLEL describing where the contents of SRC
2627 live. Queue register saves for each piece of the
2628 PARALLEL. */
2629 int par_index;
2630 int limit;
2631 HOST_WIDE_INT span_offset = offset;
2633 gcc_assert (GET_CODE (span) == PARALLEL);
2635 limit = XVECLEN (span, 0);
2636 for (par_index = 0; par_index < limit; par_index++)
2638 rtx elem = XVECEXP (span, 0, par_index);
2640 queue_reg_save (label, elem, NULL_RTX, span_offset);
2641 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2645 break;
2647 default:
2648 gcc_unreachable ();
2652 /* Record call frame debugging information for INSN, which either
2653 sets SP or FP (adjusting how we calculate the frame address) or saves a
2654 register to the stack. If INSN is NULL_RTX, initialize our state.
2656 If AFTER_P is false, we're being called before the insn is emitted,
2657 otherwise after. Call instructions get invoked twice. */
2659 void
2660 dwarf2out_frame_debug (rtx insn, bool after_p)
2662 const char *label;
2663 rtx note, n;
2664 bool handled_one = false;
2666 if (insn == NULL_RTX)
2668 size_t i;
2670 /* Flush any queued register saves. */
2671 flush_queued_reg_saves ();
2673 /* Set up state for generating call frame debug info. */
2674 lookup_cfa (&cfa);
2675 gcc_assert (cfa.reg
2676 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2678 cfa.reg = STACK_POINTER_REGNUM;
2679 cfa_store = cfa;
2680 cfa_temp.reg = -1;
2681 cfa_temp.offset = 0;
2683 for (i = 0; i < num_regs_saved_in_regs; i++)
2685 regs_saved_in_regs[i].orig_reg = NULL_RTX;
2686 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2688 num_regs_saved_in_regs = 0;
2690 if (barrier_args_size)
2692 XDELETEVEC (barrier_args_size);
2693 barrier_args_size = NULL;
2695 return;
2698 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2699 flush_queued_reg_saves ();
2701 if (!RTX_FRAME_RELATED_P (insn))
2703 /* ??? This should be done unconditionally since stack adjustments
2704 matter if the stack pointer is not the CFA register anymore but
2705 is still used to save registers. */
2706 if (!ACCUMULATE_OUTGOING_ARGS)
2707 dwarf2out_notice_stack_adjust (insn, after_p);
2708 return;
2711 label = dwarf2out_cfi_label (false);
2713 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2714 switch (REG_NOTE_KIND (note))
2716 case REG_FRAME_RELATED_EXPR:
2717 insn = XEXP (note, 0);
2718 goto found;
2720 case REG_CFA_DEF_CFA:
2721 dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2722 handled_one = true;
2723 break;
2725 case REG_CFA_ADJUST_CFA:
2726 n = XEXP (note, 0);
2727 if (n == NULL)
2729 n = PATTERN (insn);
2730 if (GET_CODE (n) == PARALLEL)
2731 n = XVECEXP (n, 0, 0);
2733 dwarf2out_frame_debug_adjust_cfa (n, label);
2734 handled_one = true;
2735 break;
2737 case REG_CFA_OFFSET:
2738 n = XEXP (note, 0);
2739 if (n == NULL)
2740 n = single_set (insn);
2741 dwarf2out_frame_debug_cfa_offset (n, label);
2742 handled_one = true;
2743 break;
2745 case REG_CFA_REGISTER:
2746 n = XEXP (note, 0);
2747 if (n == NULL)
2749 n = PATTERN (insn);
2750 if (GET_CODE (n) == PARALLEL)
2751 n = XVECEXP (n, 0, 0);
2753 dwarf2out_frame_debug_cfa_register (n, label);
2754 handled_one = true;
2755 break;
2757 case REG_CFA_RESTORE:
2758 n = XEXP (note, 0);
2759 if (n == NULL)
2761 n = PATTERN (insn);
2762 if (GET_CODE (n) == PARALLEL)
2763 n = XVECEXP (n, 0, 0);
2764 n = XEXP (n, 0);
2766 dwarf2out_frame_debug_cfa_restore (n, label);
2767 handled_one = true;
2768 break;
2770 case REG_CFA_SET_VDRAP:
2771 n = XEXP (note, 0);
2772 if (REG_P (n))
2774 dw_fde_ref fde = current_fde ();
2775 if (fde)
2777 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2778 if (REG_P (n))
2779 fde->vdrap_reg = REGNO (n);
2782 handled_one = true;
2783 break;
2785 default:
2786 break;
2788 if (handled_one)
2789 return;
2791 insn = PATTERN (insn);
2792 found:
2793 dwarf2out_frame_debug_expr (insn, label);
2795 /* Check again. A parallel can save and update the same register.
2796 We could probably check just once, here, but this is safer than
2797 removing the check above. */
2798 if (clobbers_queued_reg_save (insn))
2799 flush_queued_reg_saves ();
2802 /* Determine if we need to save and restore CFI information around this
2803 epilogue. If SIBCALL is true, then this is a sibcall epilogue. If
2804 we do need to save/restore, then emit the save now, and insert a
2805 NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream. */
2807 void
2808 dwarf2out_cfi_begin_epilogue (rtx insn)
2810 bool saw_frp = false;
2811 rtx i;
2813 /* Scan forward to the return insn, noticing if there are possible
2814 frame related insns. */
2815 for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2817 if (!INSN_P (i))
2818 continue;
2820 /* Look for both regular and sibcalls to end the block. */
2821 if (returnjump_p (i))
2822 break;
2823 if (CALL_P (i) && SIBLING_CALL_P (i))
2824 break;
2826 if (GET_CODE (PATTERN (i)) == SEQUENCE)
2828 int idx;
2829 rtx seq = PATTERN (i);
2831 if (returnjump_p (XVECEXP (seq, 0, 0)))
2832 break;
2833 if (CALL_P (XVECEXP (seq, 0, 0))
2834 && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2835 break;
2837 for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2838 if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2839 saw_frp = true;
2842 if (RTX_FRAME_RELATED_P (i))
2843 saw_frp = true;
2846 /* If the port doesn't emit epilogue unwind info, we don't need a
2847 save/restore pair. */
2848 if (!saw_frp)
2849 return;
2851 /* Otherwise, search forward to see if the return insn was the last
2852 basic block of the function. If so, we don't need save/restore. */
2853 gcc_assert (i != NULL);
2854 i = next_real_insn (i);
2855 if (i == NULL)
2856 return;
2858 /* Insert the restore before that next real insn in the stream, and before
2859 a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2860 properly nested. This should be after any label or alignment. This
2861 will be pushed into the CFI stream by the function below. */
2862 while (1)
2864 rtx p = PREV_INSN (i);
2865 if (!NOTE_P (p))
2866 break;
2867 if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2868 break;
2869 i = p;
2871 emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2873 emit_cfa_remember = true;
2875 /* And emulate the state save. */
2876 gcc_assert (!cfa_remember.in_use);
2877 cfa_remember = cfa;
2878 cfa_remember.in_use = 1;
2881 /* A "subroutine" of dwarf2out_cfi_begin_epilogue. Emit the restore
2882 required. */
2884 void
2885 dwarf2out_frame_debug_restore_state (void)
2887 dw_cfi_ref cfi = new_cfi ();
2888 const char *label = dwarf2out_cfi_label (false);
2890 cfi->dw_cfi_opc = DW_CFA_restore_state;
2891 add_fde_cfi (label, cfi);
2893 gcc_assert (cfa_remember.in_use);
2894 cfa = cfa_remember;
2895 cfa_remember.in_use = 0;
2898 #endif
2900 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
2901 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2902 (enum dwarf_call_frame_info cfi);
2904 static enum dw_cfi_oprnd_type
2905 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2907 switch (cfi)
2909 case DW_CFA_nop:
2910 case DW_CFA_GNU_window_save:
2911 case DW_CFA_remember_state:
2912 case DW_CFA_restore_state:
2913 return dw_cfi_oprnd_unused;
2915 case DW_CFA_set_loc:
2916 case DW_CFA_advance_loc1:
2917 case DW_CFA_advance_loc2:
2918 case DW_CFA_advance_loc4:
2919 case DW_CFA_MIPS_advance_loc8:
2920 return dw_cfi_oprnd_addr;
2922 case DW_CFA_offset:
2923 case DW_CFA_offset_extended:
2924 case DW_CFA_def_cfa:
2925 case DW_CFA_offset_extended_sf:
2926 case DW_CFA_def_cfa_sf:
2927 case DW_CFA_restore:
2928 case DW_CFA_restore_extended:
2929 case DW_CFA_undefined:
2930 case DW_CFA_same_value:
2931 case DW_CFA_def_cfa_register:
2932 case DW_CFA_register:
2933 case DW_CFA_expression:
2934 return dw_cfi_oprnd_reg_num;
2936 case DW_CFA_def_cfa_offset:
2937 case DW_CFA_GNU_args_size:
2938 case DW_CFA_def_cfa_offset_sf:
2939 return dw_cfi_oprnd_offset;
2941 case DW_CFA_def_cfa_expression:
2942 return dw_cfi_oprnd_loc;
2944 default:
2945 gcc_unreachable ();
2949 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
2950 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2951 (enum dwarf_call_frame_info cfi);
2953 static enum dw_cfi_oprnd_type
2954 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2956 switch (cfi)
2958 case DW_CFA_def_cfa:
2959 case DW_CFA_def_cfa_sf:
2960 case DW_CFA_offset:
2961 case DW_CFA_offset_extended_sf:
2962 case DW_CFA_offset_extended:
2963 return dw_cfi_oprnd_offset;
2965 case DW_CFA_register:
2966 return dw_cfi_oprnd_reg_num;
2968 case DW_CFA_expression:
2969 return dw_cfi_oprnd_loc;
2971 default:
2972 return dw_cfi_oprnd_unused;
2976 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2978 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
2979 switch to the data section instead, and write out a synthetic start label
2980 for collect2 the first time around. */
2982 static void
2983 switch_to_eh_frame_section (bool back)
2985 tree label;
2987 #ifdef EH_FRAME_SECTION_NAME
2988 if (eh_frame_section == 0)
2990 int flags;
2992 if (EH_TABLES_CAN_BE_READ_ONLY)
2994 int fde_encoding;
2995 int per_encoding;
2996 int lsda_encoding;
2998 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2999 /*global=*/0);
3000 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3001 /*global=*/1);
3002 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3003 /*global=*/0);
3004 flags = ((! flag_pic
3005 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3006 && (fde_encoding & 0x70) != DW_EH_PE_aligned
3007 && (per_encoding & 0x70) != DW_EH_PE_absptr
3008 && (per_encoding & 0x70) != DW_EH_PE_aligned
3009 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3010 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3011 ? 0 : SECTION_WRITE);
3013 else
3014 flags = SECTION_WRITE;
3015 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3017 #endif
3019 if (eh_frame_section)
3020 switch_to_section (eh_frame_section);
3021 else
3023 /* We have no special eh_frame section. Put the information in
3024 the data section and emit special labels to guide collect2. */
3025 switch_to_section (data_section);
3027 if (!back)
3029 label = get_file_function_name ("F");
3030 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3031 targetm.asm_out.globalize_label (asm_out_file,
3032 IDENTIFIER_POINTER (label));
3033 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3038 /* Switch [BACK] to the eh or debug frame table section, depending on
3039 FOR_EH. */
3041 static void
3042 switch_to_frame_table_section (int for_eh, bool back)
3044 if (for_eh)
3045 switch_to_eh_frame_section (back);
3046 else
3048 if (!debug_frame_section)
3049 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3050 SECTION_DEBUG, NULL);
3051 switch_to_section (debug_frame_section);
3055 /* Output a Call Frame Information opcode and its operand(s). */
3057 static void
3058 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3060 unsigned long r;
3061 HOST_WIDE_INT off;
3063 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3064 dw2_asm_output_data (1, (cfi->dw_cfi_opc
3065 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3066 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3067 ((unsigned HOST_WIDE_INT)
3068 cfi->dw_cfi_oprnd1.dw_cfi_offset));
3069 else if (cfi->dw_cfi_opc == DW_CFA_offset)
3071 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3072 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3073 "DW_CFA_offset, column %#lx", r);
3074 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3075 dw2_asm_output_data_uleb128 (off, NULL);
3077 else if (cfi->dw_cfi_opc == DW_CFA_restore)
3079 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3080 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3081 "DW_CFA_restore, column %#lx", r);
3083 else
3085 dw2_asm_output_data (1, cfi->dw_cfi_opc,
3086 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3088 switch (cfi->dw_cfi_opc)
3090 case DW_CFA_set_loc:
3091 if (for_eh)
3092 dw2_asm_output_encoded_addr_rtx (
3093 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3094 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3095 false, NULL);
3096 else
3097 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3098 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3099 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3100 break;
3102 case DW_CFA_advance_loc1:
3103 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3104 fde->dw_fde_current_label, NULL);
3105 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3106 break;
3108 case DW_CFA_advance_loc2:
3109 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3110 fde->dw_fde_current_label, NULL);
3111 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3112 break;
3114 case DW_CFA_advance_loc4:
3115 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3116 fde->dw_fde_current_label, NULL);
3117 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3118 break;
3120 case DW_CFA_MIPS_advance_loc8:
3121 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3122 fde->dw_fde_current_label, NULL);
3123 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3124 break;
3126 case DW_CFA_offset_extended:
3127 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3128 dw2_asm_output_data_uleb128 (r, NULL);
3129 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3130 dw2_asm_output_data_uleb128 (off, NULL);
3131 break;
3133 case DW_CFA_def_cfa:
3134 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3135 dw2_asm_output_data_uleb128 (r, NULL);
3136 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3137 break;
3139 case DW_CFA_offset_extended_sf:
3140 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3141 dw2_asm_output_data_uleb128 (r, NULL);
3142 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3143 dw2_asm_output_data_sleb128 (off, NULL);
3144 break;
3146 case DW_CFA_def_cfa_sf:
3147 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3148 dw2_asm_output_data_uleb128 (r, NULL);
3149 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3150 dw2_asm_output_data_sleb128 (off, NULL);
3151 break;
3153 case DW_CFA_restore_extended:
3154 case DW_CFA_undefined:
3155 case DW_CFA_same_value:
3156 case DW_CFA_def_cfa_register:
3157 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3158 dw2_asm_output_data_uleb128 (r, NULL);
3159 break;
3161 case DW_CFA_register:
3162 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3163 dw2_asm_output_data_uleb128 (r, NULL);
3164 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3165 dw2_asm_output_data_uleb128 (r, NULL);
3166 break;
3168 case DW_CFA_def_cfa_offset:
3169 case DW_CFA_GNU_args_size:
3170 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3171 break;
3173 case DW_CFA_def_cfa_offset_sf:
3174 off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3175 dw2_asm_output_data_sleb128 (off, NULL);
3176 break;
3178 case DW_CFA_GNU_window_save:
3179 break;
3181 case DW_CFA_def_cfa_expression:
3182 case DW_CFA_expression:
3183 output_cfa_loc (cfi);
3184 break;
3186 case DW_CFA_GNU_negative_offset_extended:
3187 /* Obsoleted by DW_CFA_offset_extended_sf. */
3188 gcc_unreachable ();
3190 default:
3191 break;
3196 /* Similar, but do it via assembler directives instead. */
3198 static void
3199 output_cfi_directive (dw_cfi_ref cfi)
3201 unsigned long r, r2;
3203 switch (cfi->dw_cfi_opc)
3205 case DW_CFA_advance_loc:
3206 case DW_CFA_advance_loc1:
3207 case DW_CFA_advance_loc2:
3208 case DW_CFA_advance_loc4:
3209 case DW_CFA_MIPS_advance_loc8:
3210 case DW_CFA_set_loc:
3211 /* Should only be created by add_fde_cfi in a code path not
3212 followed when emitting via directives. The assembler is
3213 going to take care of this for us. */
3214 gcc_unreachable ();
3216 case DW_CFA_offset:
3217 case DW_CFA_offset_extended:
3218 case DW_CFA_offset_extended_sf:
3219 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3220 fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3221 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3222 break;
3224 case DW_CFA_restore:
3225 case DW_CFA_restore_extended:
3226 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3227 fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3228 break;
3230 case DW_CFA_undefined:
3231 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3232 fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3233 break;
3235 case DW_CFA_same_value:
3236 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3237 fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3238 break;
3240 case DW_CFA_def_cfa:
3241 case DW_CFA_def_cfa_sf:
3242 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3243 fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3244 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3245 break;
3247 case DW_CFA_def_cfa_register:
3248 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3249 fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3250 break;
3252 case DW_CFA_register:
3253 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3254 r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3255 fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3256 break;
3258 case DW_CFA_def_cfa_offset:
3259 case DW_CFA_def_cfa_offset_sf:
3260 fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3261 HOST_WIDE_INT_PRINT_DEC"\n",
3262 cfi->dw_cfi_oprnd1.dw_cfi_offset);
3263 break;
3265 case DW_CFA_remember_state:
3266 fprintf (asm_out_file, "\t.cfi_remember_state\n");
3267 break;
3268 case DW_CFA_restore_state:
3269 fprintf (asm_out_file, "\t.cfi_restore_state\n");
3270 break;
3272 case DW_CFA_GNU_args_size:
3273 fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3274 dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3275 if (flag_debug_asm)
3276 fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3277 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3278 fputc ('\n', asm_out_file);
3279 break;
3281 case DW_CFA_GNU_window_save:
3282 fprintf (asm_out_file, "\t.cfi_window_save\n");
3283 break;
3285 case DW_CFA_def_cfa_expression:
3286 case DW_CFA_expression:
3287 fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3288 output_cfa_loc_raw (cfi);
3289 fputc ('\n', asm_out_file);
3290 break;
3292 default:
3293 gcc_unreachable ();
3297 DEF_VEC_P (dw_cfi_ref);
3298 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3300 /* Output CFIs to bring current FDE to the same state as after executing
3301 CFIs in CFI chain. DO_CFI_ASM is true if .cfi_* directives shall
3302 be emitted, false otherwise. If it is false, FDE and FOR_EH are the
3303 other arguments to pass to output_cfi. */
3305 static void
3306 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3308 struct dw_cfi_struct cfi_buf;
3309 dw_cfi_ref cfi2;
3310 dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3311 VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3312 unsigned int len, idx;
3314 for (;; cfi = cfi->dw_cfi_next)
3315 switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3317 case DW_CFA_advance_loc:
3318 case DW_CFA_advance_loc1:
3319 case DW_CFA_advance_loc2:
3320 case DW_CFA_advance_loc4:
3321 case DW_CFA_MIPS_advance_loc8:
3322 case DW_CFA_set_loc:
3323 /* All advances should be ignored. */
3324 break;
3325 case DW_CFA_remember_state:
3327 dw_cfi_ref args_size = cfi_args_size;
3329 /* Skip everything between .cfi_remember_state and
3330 .cfi_restore_state. */
3331 for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3332 if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3333 break;
3334 else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3335 args_size = cfi2;
3336 else
3337 gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3339 if (cfi2 == NULL)
3340 goto flush_all;
3341 else
3343 cfi = cfi2;
3344 cfi_args_size = args_size;
3346 break;
3348 case DW_CFA_GNU_args_size:
3349 cfi_args_size = cfi;
3350 break;
3351 case DW_CFA_GNU_window_save:
3352 goto flush_all;
3353 case DW_CFA_offset:
3354 case DW_CFA_offset_extended:
3355 case DW_CFA_offset_extended_sf:
3356 case DW_CFA_restore:
3357 case DW_CFA_restore_extended:
3358 case DW_CFA_undefined:
3359 case DW_CFA_same_value:
3360 case DW_CFA_register:
3361 case DW_CFA_val_offset:
3362 case DW_CFA_val_offset_sf:
3363 case DW_CFA_expression:
3364 case DW_CFA_val_expression:
3365 case DW_CFA_GNU_negative_offset_extended:
3366 if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3367 VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3368 cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3369 VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3370 break;
3371 case DW_CFA_def_cfa:
3372 case DW_CFA_def_cfa_sf:
3373 case DW_CFA_def_cfa_expression:
3374 cfi_cfa = cfi;
3375 cfi_cfa_offset = cfi;
3376 break;
3377 case DW_CFA_def_cfa_register:
3378 cfi_cfa = cfi;
3379 break;
3380 case DW_CFA_def_cfa_offset:
3381 case DW_CFA_def_cfa_offset_sf:
3382 cfi_cfa_offset = cfi;
3383 break;
3384 case DW_CFA_nop:
3385 gcc_assert (cfi == NULL);
3386 flush_all:
3387 len = VEC_length (dw_cfi_ref, regs);
3388 for (idx = 0; idx < len; idx++)
3390 cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3391 if (cfi2 != NULL
3392 && cfi2->dw_cfi_opc != DW_CFA_restore
3393 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3395 if (do_cfi_asm)
3396 output_cfi_directive (cfi2);
3397 else
3398 output_cfi (cfi2, fde, for_eh);
3401 if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3403 gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3404 cfi_buf = *cfi_cfa;
3405 switch (cfi_cfa_offset->dw_cfi_opc)
3407 case DW_CFA_def_cfa_offset:
3408 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3409 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3410 break;
3411 case DW_CFA_def_cfa_offset_sf:
3412 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3413 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3414 break;
3415 case DW_CFA_def_cfa:
3416 case DW_CFA_def_cfa_sf:
3417 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3418 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3419 break;
3420 default:
3421 gcc_unreachable ();
3423 cfi_cfa = &cfi_buf;
3425 else if (cfi_cfa_offset)
3426 cfi_cfa = cfi_cfa_offset;
3427 if (cfi_cfa)
3429 if (do_cfi_asm)
3430 output_cfi_directive (cfi_cfa);
3431 else
3432 output_cfi (cfi_cfa, fde, for_eh);
3434 cfi_cfa = NULL;
3435 cfi_cfa_offset = NULL;
3436 if (cfi_args_size
3437 && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3439 if (do_cfi_asm)
3440 output_cfi_directive (cfi_args_size);
3441 else
3442 output_cfi (cfi_args_size, fde, for_eh);
3444 cfi_args_size = NULL;
3445 if (cfi == NULL)
3447 VEC_free (dw_cfi_ref, heap, regs);
3448 return;
3450 else if (do_cfi_asm)
3451 output_cfi_directive (cfi);
3452 else
3453 output_cfi (cfi, fde, for_eh);
3454 break;
3455 default:
3456 gcc_unreachable ();
3460 /* Output one FDE. */
3462 static void
3463 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3464 char *section_start_label, int fde_encoding, char *augmentation,
3465 bool any_lsda_needed, int lsda_encoding)
3467 const char *begin, *end;
3468 static unsigned int j;
3469 char l1[20], l2[20];
3470 dw_cfi_ref cfi;
3472 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3473 /* empty */ 0);
3474 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3475 for_eh + j);
3476 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3477 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3478 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3479 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3480 " indicating 64-bit DWARF extension");
3481 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3482 "FDE Length");
3483 ASM_OUTPUT_LABEL (asm_out_file, l1);
3485 if (for_eh)
3486 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3487 else
3488 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3489 debug_frame_section, "FDE CIE offset");
3491 if (!fde->dw_fde_switched_sections)
3493 begin = fde->dw_fde_begin;
3494 end = fde->dw_fde_end;
3496 else
3498 /* For the first section, prefer dw_fde_begin over
3499 dw_fde_{hot,cold}_section_label, as the latter
3500 might be separated from the real start of the
3501 function by alignment padding. */
3502 if (!second)
3503 begin = fde->dw_fde_begin;
3504 else if (fde->dw_fde_switched_cold_to_hot)
3505 begin = fde->dw_fde_hot_section_label;
3506 else
3507 begin = fde->dw_fde_unlikely_section_label;
3508 if (second ^ fde->dw_fde_switched_cold_to_hot)
3509 end = fde->dw_fde_unlikely_section_end_label;
3510 else
3511 end = fde->dw_fde_hot_section_end_label;
3514 if (for_eh)
3516 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3517 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3518 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3519 "FDE initial location");
3520 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3521 end, begin, "FDE address range");
3523 else
3525 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3526 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3529 if (augmentation[0])
3531 if (any_lsda_needed)
3533 int size = size_of_encoded_value (lsda_encoding);
3535 if (lsda_encoding == DW_EH_PE_aligned)
3537 int offset = ( 4 /* Length */
3538 + 4 /* CIE offset */
3539 + 2 * size_of_encoded_value (fde_encoding)
3540 + 1 /* Augmentation size */ );
3541 int pad = -offset & (PTR_SIZE - 1);
3543 size += pad;
3544 gcc_assert (size_of_uleb128 (size) == 1);
3547 dw2_asm_output_data_uleb128 (size, "Augmentation size");
3549 if (fde->uses_eh_lsda)
3551 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3552 fde->funcdef_number);
3553 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3554 gen_rtx_SYMBOL_REF (Pmode, l1),
3555 false,
3556 "Language Specific Data Area");
3558 else
3560 if (lsda_encoding == DW_EH_PE_aligned)
3561 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3562 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3563 "Language Specific Data Area (none)");
3566 else
3567 dw2_asm_output_data_uleb128 (0, "Augmentation size");
3570 /* Loop through the Call Frame Instructions associated with
3571 this FDE. */
3572 fde->dw_fde_current_label = begin;
3573 if (!fde->dw_fde_switched_sections)
3574 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3575 output_cfi (cfi, fde, for_eh);
3576 else if (!second)
3578 if (fde->dw_fde_switch_cfi)
3579 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3581 output_cfi (cfi, fde, for_eh);
3582 if (cfi == fde->dw_fde_switch_cfi)
3583 break;
3586 else
3588 dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3590 if (fde->dw_fde_switch_cfi)
3592 cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3593 fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3594 output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3595 fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3597 for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3598 output_cfi (cfi, fde, for_eh);
3601 /* If we are to emit a ref/link from function bodies to their frame tables,
3602 do it now. This is typically performed to make sure that tables
3603 associated with functions are dragged with them and not discarded in
3604 garbage collecting links. We need to do this on a per function basis to
3605 cope with -ffunction-sections. */
3607 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3608 /* Switch to the function section, emit the ref to the tables, and
3609 switch *back* into the table section. */
3610 switch_to_section (function_section (fde->decl));
3611 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3612 switch_to_frame_table_section (for_eh, true);
3613 #endif
3615 /* Pad the FDE out to an address sized boundary. */
3616 ASM_OUTPUT_ALIGN (asm_out_file,
3617 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3618 ASM_OUTPUT_LABEL (asm_out_file, l2);
3620 j += 2;
3623 /* Return true if frame description entry FDE is needed for EH. */
3625 static bool
3626 fde_needed_for_eh_p (dw_fde_ref fde)
3628 if (flag_asynchronous_unwind_tables)
3629 return true;
3631 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3632 return true;
3634 if (fde->uses_eh_lsda)
3635 return true;
3637 /* If exceptions are enabled, we have collected nothrow info. */
3638 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3639 return false;
3641 return true;
3644 /* Output the call frame information used to record information
3645 that relates to calculating the frame pointer, and records the
3646 location of saved registers. */
3648 static void
3649 output_call_frame_info (int for_eh)
3651 unsigned int i;
3652 dw_fde_ref fde;
3653 dw_cfi_ref cfi;
3654 char l1[20], l2[20], section_start_label[20];
3655 bool any_lsda_needed = false;
3656 char augmentation[6];
3657 int augmentation_size;
3658 int fde_encoding = DW_EH_PE_absptr;
3659 int per_encoding = DW_EH_PE_absptr;
3660 int lsda_encoding = DW_EH_PE_absptr;
3661 int return_reg;
3662 rtx personality = NULL;
3663 int dw_cie_version;
3665 /* Don't emit a CIE if there won't be any FDEs. */
3666 if (fde_table_in_use == 0)
3667 return;
3669 /* Nothing to do if the assembler's doing it all. */
3670 if (dwarf2out_do_cfi_asm ())
3671 return;
3673 /* If we don't have any functions we'll want to unwind out of, don't emit
3674 any EH unwind information. If we make FDEs linkonce, we may have to
3675 emit an empty label for an FDE that wouldn't otherwise be emitted. We
3676 want to avoid having an FDE kept around when the function it refers to
3677 is discarded. Example where this matters: a primary function template
3678 in C++ requires EH information, an explicit specialization doesn't. */
3679 if (for_eh)
3681 bool any_eh_needed = false;
3683 for (i = 0; i < fde_table_in_use; i++)
3684 if (fde_table[i].uses_eh_lsda)
3685 any_eh_needed = any_lsda_needed = true;
3686 else if (fde_needed_for_eh_p (&fde_table[i]))
3687 any_eh_needed = true;
3688 else if (TARGET_USES_WEAK_UNWIND_INFO)
3689 targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3690 1, 1);
3692 if (!any_eh_needed)
3693 return;
3696 /* We're going to be generating comments, so turn on app. */
3697 if (flag_debug_asm)
3698 app_enable ();
3700 /* Switch to the proper frame section, first time. */
3701 switch_to_frame_table_section (for_eh, false);
3703 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3704 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3706 /* Output the CIE. */
3707 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3708 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3709 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3710 dw2_asm_output_data (4, 0xffffffff,
3711 "Initial length escape value indicating 64-bit DWARF extension");
3712 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3713 "Length of Common Information Entry");
3714 ASM_OUTPUT_LABEL (asm_out_file, l1);
3716 /* Now that the CIE pointer is PC-relative for EH,
3717 use 0 to identify the CIE. */
3718 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3719 (for_eh ? 0 : DWARF_CIE_ID),
3720 "CIE Identifier Tag");
3722 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3723 use CIE version 1, unless that would produce incorrect results
3724 due to overflowing the return register column. */
3725 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3726 dw_cie_version = 1;
3727 if (return_reg >= 256 || dwarf_version > 2)
3728 dw_cie_version = 3;
3729 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3731 augmentation[0] = 0;
3732 augmentation_size = 0;
3734 personality = current_unit_personality;
3735 if (for_eh)
3737 char *p;
3739 /* Augmentation:
3740 z Indicates that a uleb128 is present to size the
3741 augmentation section.
3742 L Indicates the encoding (and thus presence) of
3743 an LSDA pointer in the FDE augmentation.
3744 R Indicates a non-default pointer encoding for
3745 FDE code pointers.
3746 P Indicates the presence of an encoding + language
3747 personality routine in the CIE augmentation. */
3749 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3750 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3751 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3753 p = augmentation + 1;
3754 if (personality)
3756 *p++ = 'P';
3757 augmentation_size += 1 + size_of_encoded_value (per_encoding);
3758 assemble_external_libcall (personality);
3760 if (any_lsda_needed)
3762 *p++ = 'L';
3763 augmentation_size += 1;
3765 if (fde_encoding != DW_EH_PE_absptr)
3767 *p++ = 'R';
3768 augmentation_size += 1;
3770 if (p > augmentation + 1)
3772 augmentation[0] = 'z';
3773 *p = '\0';
3776 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
3777 if (personality && per_encoding == DW_EH_PE_aligned)
3779 int offset = ( 4 /* Length */
3780 + 4 /* CIE Id */
3781 + 1 /* CIE version */
3782 + strlen (augmentation) + 1 /* Augmentation */
3783 + size_of_uleb128 (1) /* Code alignment */
3784 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3785 + 1 /* RA column */
3786 + 1 /* Augmentation size */
3787 + 1 /* Personality encoding */ );
3788 int pad = -offset & (PTR_SIZE - 1);
3790 augmentation_size += pad;
3792 /* Augmentations should be small, so there's scarce need to
3793 iterate for a solution. Die if we exceed one uleb128 byte. */
3794 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3798 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3799 if (dw_cie_version >= 4)
3801 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3802 dw2_asm_output_data (1, 0, "CIE Segment Size");
3804 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3805 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3806 "CIE Data Alignment Factor");
3808 if (dw_cie_version == 1)
3809 dw2_asm_output_data (1, return_reg, "CIE RA Column");
3810 else
3811 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3813 if (augmentation[0])
3815 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3816 if (personality)
3818 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3819 eh_data_format_name (per_encoding));
3820 dw2_asm_output_encoded_addr_rtx (per_encoding,
3821 personality,
3822 true, NULL);
3825 if (any_lsda_needed)
3826 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3827 eh_data_format_name (lsda_encoding));
3829 if (fde_encoding != DW_EH_PE_absptr)
3830 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3831 eh_data_format_name (fde_encoding));
3834 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3835 output_cfi (cfi, NULL, for_eh);
3837 /* Pad the CIE out to an address sized boundary. */
3838 ASM_OUTPUT_ALIGN (asm_out_file,
3839 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3840 ASM_OUTPUT_LABEL (asm_out_file, l2);
3842 /* Loop through all of the FDE's. */
3843 for (i = 0; i < fde_table_in_use; i++)
3845 unsigned int k;
3846 fde = &fde_table[i];
3848 /* Don't emit EH unwind info for leaf functions that don't need it. */
3849 if (for_eh && !fde_needed_for_eh_p (fde))
3850 continue;
3852 for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3853 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3854 augmentation, any_lsda_needed, lsda_encoding);
3857 if (for_eh && targetm.terminate_dw2_eh_frame_info)
3858 dw2_asm_output_data (4, 0, "End of Table");
3859 #ifdef MIPS_DEBUGGING_INFO
3860 /* Work around Irix 6 assembler bug whereby labels at the end of a section
3861 get a value of 0. Putting .align 0 after the label fixes it. */
3862 ASM_OUTPUT_ALIGN (asm_out_file, 0);
3863 #endif
3865 /* Turn off app to make assembly quicker. */
3866 if (flag_debug_asm)
3867 app_disable ();
3870 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
3872 static void
3873 dwarf2out_do_cfi_startproc (bool second)
3875 int enc;
3876 rtx ref;
3877 rtx personality = get_personality_function (current_function_decl);
3879 fprintf (asm_out_file, "\t.cfi_startproc\n");
3881 if (personality)
3883 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3884 ref = personality;
3886 /* ??? The GAS support isn't entirely consistent. We have to
3887 handle indirect support ourselves, but PC-relative is done
3888 in the assembler. Further, the assembler can't handle any
3889 of the weirder relocation types. */
3890 if (enc & DW_EH_PE_indirect)
3891 ref = dw2_force_const_mem (ref, true);
3893 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
3894 output_addr_const (asm_out_file, ref);
3895 fputc ('\n', asm_out_file);
3898 if (crtl->uses_eh_lsda)
3900 char lab[20];
3902 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3903 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3904 current_function_funcdef_no);
3905 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3906 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3908 if (enc & DW_EH_PE_indirect)
3909 ref = dw2_force_const_mem (ref, true);
3911 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
3912 output_addr_const (asm_out_file, ref);
3913 fputc ('\n', asm_out_file);
3917 /* Output a marker (i.e. a label) for the beginning of a function, before
3918 the prologue. */
3920 void
3921 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3922 const char *file ATTRIBUTE_UNUSED)
3924 char label[MAX_ARTIFICIAL_LABEL_BYTES];
3925 char * dup_label;
3926 dw_fde_ref fde;
3927 section *fnsec;
3929 current_function_func_begin_label = NULL;
3931 #ifdef TARGET_UNWIND_INFO
3932 /* ??? current_function_func_begin_label is also used by except.c
3933 for call-site information. We must emit this label if it might
3934 be used. */
3935 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3936 && ! dwarf2out_do_frame ())
3937 return;
3938 #else
3939 if (! dwarf2out_do_frame ())
3940 return;
3941 #endif
3943 fnsec = function_section (current_function_decl);
3944 switch_to_section (fnsec);
3945 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3946 current_function_funcdef_no);
3947 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3948 current_function_funcdef_no);
3949 dup_label = xstrdup (label);
3950 current_function_func_begin_label = dup_label;
3952 #ifdef TARGET_UNWIND_INFO
3953 /* We can elide the fde allocation if we're not emitting debug info. */
3954 if (! dwarf2out_do_frame ())
3955 return;
3956 #endif
3958 /* Expand the fde table if necessary. */
3959 if (fde_table_in_use == fde_table_allocated)
3961 fde_table_allocated += FDE_TABLE_INCREMENT;
3962 fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3963 memset (fde_table + fde_table_in_use, 0,
3964 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3967 /* Record the FDE associated with this function. */
3968 current_funcdef_fde = fde_table_in_use;
3970 /* Add the new FDE at the end of the fde_table. */
3971 fde = &fde_table[fde_table_in_use++];
3972 fde->decl = current_function_decl;
3973 fde->dw_fde_begin = dup_label;
3974 fde->dw_fde_current_label = dup_label;
3975 fde->dw_fde_hot_section_label = NULL;
3976 fde->dw_fde_hot_section_end_label = NULL;
3977 fde->dw_fde_unlikely_section_label = NULL;
3978 fde->dw_fde_unlikely_section_end_label = NULL;
3979 fde->dw_fde_switched_sections = 0;
3980 fde->dw_fde_switched_cold_to_hot = 0;
3981 fde->dw_fde_end = NULL;
3982 fde->dw_fde_vms_end_prologue = NULL;
3983 fde->dw_fde_vms_begin_epilogue = NULL;
3984 fde->dw_fde_cfi = NULL;
3985 fde->dw_fde_switch_cfi = NULL;
3986 fde->funcdef_number = current_function_funcdef_no;
3987 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3988 fde->uses_eh_lsda = crtl->uses_eh_lsda;
3989 fde->nothrow = crtl->nothrow;
3990 fde->drap_reg = INVALID_REGNUM;
3991 fde->vdrap_reg = INVALID_REGNUM;
3992 if (flag_reorder_blocks_and_partition)
3994 section *unlikelysec;
3995 if (first_function_block_is_cold)
3996 fde->in_std_section = 1;
3997 else
3998 fde->in_std_section
3999 = (fnsec == text_section
4000 || (cold_text_section && fnsec == cold_text_section));
4001 unlikelysec = unlikely_text_section ();
4002 fde->cold_in_std_section
4003 = (unlikelysec == text_section
4004 || (cold_text_section && unlikelysec == cold_text_section));
4006 else
4008 fde->in_std_section
4009 = (fnsec == text_section
4010 || (cold_text_section && fnsec == cold_text_section));
4011 fde->cold_in_std_section = 0;
4014 args_size = old_args_size = 0;
4016 /* We only want to output line number information for the genuine dwarf2
4017 prologue case, not the eh frame case. */
4018 #ifdef DWARF2_DEBUGGING_INFO
4019 if (file)
4020 dwarf2out_source_line (line, file, 0, true);
4021 #endif
4023 if (dwarf2out_do_cfi_asm ())
4024 dwarf2out_do_cfi_startproc (false);
4025 else
4027 rtx personality = get_personality_function (current_function_decl);
4028 if (!current_unit_personality)
4029 current_unit_personality = personality;
4031 /* We cannot keep a current personality per function as without CFI
4032 asm, at the point where we emit the CFI data, there is no current
4033 function anymore. */
4034 if (personality && current_unit_personality != personality)
4035 sorry ("multiple EH personalities are supported only with assemblers "
4036 "supporting .cfi_personality directive");
4040 /* Output a marker (i.e. a label) for the end of the generated code
4041 for a function prologue. This gets called *after* the prologue code has
4042 been generated. */
4044 void
4045 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4046 const char *file ATTRIBUTE_UNUSED)
4048 dw_fde_ref fde;
4049 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4051 /* Output a label to mark the endpoint of the code generated for this
4052 function. */
4053 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4054 current_function_funcdef_no);
4055 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4056 current_function_funcdef_no);
4057 fde = &fde_table[fde_table_in_use - 1];
4058 fde->dw_fde_vms_end_prologue = xstrdup (label);
4061 /* Output a marker (i.e. a label) for the beginning of the generated code
4062 for a function epilogue. This gets called *before* the prologue code has
4063 been generated. */
4065 void
4066 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4067 const char *file ATTRIBUTE_UNUSED)
4069 dw_fde_ref fde;
4070 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4072 fde = &fde_table[fde_table_in_use - 1];
4073 if (fde->dw_fde_vms_begin_epilogue)
4074 return;
4076 /* Output a label to mark the endpoint of the code generated for this
4077 function. */
4078 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4079 current_function_funcdef_no);
4080 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4081 current_function_funcdef_no);
4082 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4085 /* Output a marker (i.e. a label) for the absolute end of the generated code
4086 for a function definition. This gets called *after* the epilogue code has
4087 been generated. */
4089 void
4090 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4091 const char *file ATTRIBUTE_UNUSED)
4093 dw_fde_ref fde;
4094 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4096 #ifdef DWARF2_DEBUGGING_INFO
4097 last_var_location_insn = NULL_RTX;
4098 #endif
4100 if (dwarf2out_do_cfi_asm ())
4101 fprintf (asm_out_file, "\t.cfi_endproc\n");
4103 /* Output a label to mark the endpoint of the code generated for this
4104 function. */
4105 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4106 current_function_funcdef_no);
4107 ASM_OUTPUT_LABEL (asm_out_file, label);
4108 fde = current_fde ();
4109 gcc_assert (fde != NULL);
4110 fde->dw_fde_end = xstrdup (label);
4113 void
4114 dwarf2out_frame_init (void)
4116 /* Allocate the initial hunk of the fde_table. */
4117 fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4118 fde_table_allocated = FDE_TABLE_INCREMENT;
4119 fde_table_in_use = 0;
4121 /* Generate the CFA instructions common to all FDE's. Do it now for the
4122 sake of lookup_cfa. */
4124 /* On entry, the Canonical Frame Address is at SP. */
4125 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4127 #ifdef DWARF2_UNWIND_INFO
4128 if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4129 initial_return_save (INCOMING_RETURN_ADDR_RTX);
4130 #endif
4133 void
4134 dwarf2out_frame_finish (void)
4136 /* Output call frame information. */
4137 if (DWARF2_FRAME_INFO)
4138 output_call_frame_info (0);
4140 #ifndef TARGET_UNWIND_INFO
4141 /* Output another copy for the unwinder. */
4142 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4143 output_call_frame_info (1);
4144 #endif
4147 /* Note that the current function section is being used for code. */
4149 static void
4150 dwarf2out_note_section_used (void)
4152 section *sec = current_function_section ();
4153 if (sec == text_section)
4154 text_section_used = true;
4155 else if (sec == cold_text_section)
4156 cold_text_section_used = true;
4159 void
4160 dwarf2out_switch_text_section (void)
4162 dw_fde_ref fde = current_fde ();
4164 gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4166 fde->dw_fde_switched_sections = 1;
4167 fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4169 fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4170 fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4171 fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4172 fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4173 have_multiple_function_sections = true;
4175 /* Reset the current label on switching text sections, so that we
4176 don't attempt to advance_loc4 between labels in different sections. */
4177 fde->dw_fde_current_label = NULL;
4179 /* There is no need to mark used sections when not debugging. */
4180 if (cold_text_section != NULL)
4181 dwarf2out_note_section_used ();
4183 if (dwarf2out_do_cfi_asm ())
4184 fprintf (asm_out_file, "\t.cfi_endproc\n");
4186 /* Now do the real section switch. */
4187 switch_to_section (current_function_section ());
4189 if (dwarf2out_do_cfi_asm ())
4191 dwarf2out_do_cfi_startproc (true);
4192 /* As this is a different FDE, insert all current CFI instructions
4193 again. */
4194 output_cfis (fde->dw_fde_cfi, true, fde, true);
4196 else
4198 dw_cfi_ref cfi = fde->dw_fde_cfi;
4200 cfi = fde->dw_fde_cfi;
4201 if (cfi)
4202 while (cfi->dw_cfi_next != NULL)
4203 cfi = cfi->dw_cfi_next;
4204 fde->dw_fde_switch_cfi = cfi;
4207 #endif
4209 /* And now, the subset of the debugging information support code necessary
4210 for emitting location expressions. */
4212 /* Data about a single source file. */
4213 struct GTY(()) dwarf_file_data {
4214 const char * filename;
4215 int emitted_number;
4218 typedef struct dw_val_struct *dw_val_ref;
4219 typedef struct die_struct *dw_die_ref;
4220 typedef const struct die_struct *const_dw_die_ref;
4221 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4222 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4224 typedef struct GTY(()) deferred_locations_struct
4226 tree variable;
4227 dw_die_ref die;
4228 } deferred_locations;
4230 DEF_VEC_O(deferred_locations);
4231 DEF_VEC_ALLOC_O(deferred_locations,gc);
4233 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4235 DEF_VEC_P(dw_die_ref);
4236 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4238 /* Each DIE may have a series of attribute/value pairs. Values
4239 can take on several forms. The forms that are used in this
4240 implementation are listed below. */
4242 enum dw_val_class
4244 dw_val_class_addr,
4245 dw_val_class_offset,
4246 dw_val_class_loc,
4247 dw_val_class_loc_list,
4248 dw_val_class_range_list,
4249 dw_val_class_const,
4250 dw_val_class_unsigned_const,
4251 dw_val_class_const_double,
4252 dw_val_class_vec,
4253 dw_val_class_flag,
4254 dw_val_class_die_ref,
4255 dw_val_class_fde_ref,
4256 dw_val_class_lbl_id,
4257 dw_val_class_lineptr,
4258 dw_val_class_str,
4259 dw_val_class_macptr,
4260 dw_val_class_file,
4261 dw_val_class_data8,
4262 dw_val_class_vms_delta
4265 /* Describe a floating point constant value, or a vector constant value. */
4267 typedef struct GTY(()) dw_vec_struct {
4268 unsigned char * GTY((length ("%h.length"))) array;
4269 unsigned length;
4270 unsigned elt_size;
4272 dw_vec_const;
4274 /* The dw_val_node describes an attribute's value, as it is
4275 represented internally. */
4277 typedef struct GTY(()) dw_val_struct {
4278 enum dw_val_class val_class;
4279 union dw_val_struct_union
4281 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4282 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4283 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4284 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4285 HOST_WIDE_INT GTY ((default)) val_int;
4286 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4287 double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4288 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4289 struct dw_val_die_union
4291 dw_die_ref die;
4292 int external;
4293 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4294 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4295 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4296 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4297 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4298 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4299 unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4300 struct dw_val_vms_delta_union
4302 char * lbl1;
4303 char * lbl2;
4304 } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4306 GTY ((desc ("%1.val_class"))) v;
4308 dw_val_node;
4310 /* Locations in memory are described using a sequence of stack machine
4311 operations. */
4313 typedef struct GTY(()) dw_loc_descr_struct {
4314 dw_loc_descr_ref dw_loc_next;
4315 ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4316 /* Used to distinguish DW_OP_addr with a direct symbol relocation
4317 from DW_OP_addr with a dtp-relative symbol relocation. */
4318 unsigned int dtprel : 1;
4319 int dw_loc_addr;
4320 dw_val_node dw_loc_oprnd1;
4321 dw_val_node dw_loc_oprnd2;
4323 dw_loc_descr_node;
4325 /* Location lists are ranges + location descriptions for that range,
4326 so you can track variables that are in different places over
4327 their entire life. */
4328 typedef struct GTY(()) dw_loc_list_struct {
4329 dw_loc_list_ref dw_loc_next;
4330 const char *begin; /* Label for begin address of range */
4331 const char *end; /* Label for end address of range */
4332 char *ll_symbol; /* Label for beginning of location list.
4333 Only on head of list */
4334 const char *section; /* Section this loclist is relative to */
4335 dw_loc_descr_ref expr;
4336 } dw_loc_list_node;
4338 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4340 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4342 /* Convert a DWARF stack opcode into its string name. */
4344 static const char *
4345 dwarf_stack_op_name (unsigned int op)
4347 switch (op)
4349 case DW_OP_addr:
4350 return "DW_OP_addr";
4351 case DW_OP_deref:
4352 return "DW_OP_deref";
4353 case DW_OP_const1u:
4354 return "DW_OP_const1u";
4355 case DW_OP_const1s:
4356 return "DW_OP_const1s";
4357 case DW_OP_const2u:
4358 return "DW_OP_const2u";
4359 case DW_OP_const2s:
4360 return "DW_OP_const2s";
4361 case DW_OP_const4u:
4362 return "DW_OP_const4u";
4363 case DW_OP_const4s:
4364 return "DW_OP_const4s";
4365 case DW_OP_const8u:
4366 return "DW_OP_const8u";
4367 case DW_OP_const8s:
4368 return "DW_OP_const8s";
4369 case DW_OP_constu:
4370 return "DW_OP_constu";
4371 case DW_OP_consts:
4372 return "DW_OP_consts";
4373 case DW_OP_dup:
4374 return "DW_OP_dup";
4375 case DW_OP_drop:
4376 return "DW_OP_drop";
4377 case DW_OP_over:
4378 return "DW_OP_over";
4379 case DW_OP_pick:
4380 return "DW_OP_pick";
4381 case DW_OP_swap:
4382 return "DW_OP_swap";
4383 case DW_OP_rot:
4384 return "DW_OP_rot";
4385 case DW_OP_xderef:
4386 return "DW_OP_xderef";
4387 case DW_OP_abs:
4388 return "DW_OP_abs";
4389 case DW_OP_and:
4390 return "DW_OP_and";
4391 case DW_OP_div:
4392 return "DW_OP_div";
4393 case DW_OP_minus:
4394 return "DW_OP_minus";
4395 case DW_OP_mod:
4396 return "DW_OP_mod";
4397 case DW_OP_mul:
4398 return "DW_OP_mul";
4399 case DW_OP_neg:
4400 return "DW_OP_neg";
4401 case DW_OP_not:
4402 return "DW_OP_not";
4403 case DW_OP_or:
4404 return "DW_OP_or";
4405 case DW_OP_plus:
4406 return "DW_OP_plus";
4407 case DW_OP_plus_uconst:
4408 return "DW_OP_plus_uconst";
4409 case DW_OP_shl:
4410 return "DW_OP_shl";
4411 case DW_OP_shr:
4412 return "DW_OP_shr";
4413 case DW_OP_shra:
4414 return "DW_OP_shra";
4415 case DW_OP_xor:
4416 return "DW_OP_xor";
4417 case DW_OP_bra:
4418 return "DW_OP_bra";
4419 case DW_OP_eq:
4420 return "DW_OP_eq";
4421 case DW_OP_ge:
4422 return "DW_OP_ge";
4423 case DW_OP_gt:
4424 return "DW_OP_gt";
4425 case DW_OP_le:
4426 return "DW_OP_le";
4427 case DW_OP_lt:
4428 return "DW_OP_lt";
4429 case DW_OP_ne:
4430 return "DW_OP_ne";
4431 case DW_OP_skip:
4432 return "DW_OP_skip";
4433 case DW_OP_lit0:
4434 return "DW_OP_lit0";
4435 case DW_OP_lit1:
4436 return "DW_OP_lit1";
4437 case DW_OP_lit2:
4438 return "DW_OP_lit2";
4439 case DW_OP_lit3:
4440 return "DW_OP_lit3";
4441 case DW_OP_lit4:
4442 return "DW_OP_lit4";
4443 case DW_OP_lit5:
4444 return "DW_OP_lit5";
4445 case DW_OP_lit6:
4446 return "DW_OP_lit6";
4447 case DW_OP_lit7:
4448 return "DW_OP_lit7";
4449 case DW_OP_lit8:
4450 return "DW_OP_lit8";
4451 case DW_OP_lit9:
4452 return "DW_OP_lit9";
4453 case DW_OP_lit10:
4454 return "DW_OP_lit10";
4455 case DW_OP_lit11:
4456 return "DW_OP_lit11";
4457 case DW_OP_lit12:
4458 return "DW_OP_lit12";
4459 case DW_OP_lit13:
4460 return "DW_OP_lit13";
4461 case DW_OP_lit14:
4462 return "DW_OP_lit14";
4463 case DW_OP_lit15:
4464 return "DW_OP_lit15";
4465 case DW_OP_lit16:
4466 return "DW_OP_lit16";
4467 case DW_OP_lit17:
4468 return "DW_OP_lit17";
4469 case DW_OP_lit18:
4470 return "DW_OP_lit18";
4471 case DW_OP_lit19:
4472 return "DW_OP_lit19";
4473 case DW_OP_lit20:
4474 return "DW_OP_lit20";
4475 case DW_OP_lit21:
4476 return "DW_OP_lit21";
4477 case DW_OP_lit22:
4478 return "DW_OP_lit22";
4479 case DW_OP_lit23:
4480 return "DW_OP_lit23";
4481 case DW_OP_lit24:
4482 return "DW_OP_lit24";
4483 case DW_OP_lit25:
4484 return "DW_OP_lit25";
4485 case DW_OP_lit26:
4486 return "DW_OP_lit26";
4487 case DW_OP_lit27:
4488 return "DW_OP_lit27";
4489 case DW_OP_lit28:
4490 return "DW_OP_lit28";
4491 case DW_OP_lit29:
4492 return "DW_OP_lit29";
4493 case DW_OP_lit30:
4494 return "DW_OP_lit30";
4495 case DW_OP_lit31:
4496 return "DW_OP_lit31";
4497 case DW_OP_reg0:
4498 return "DW_OP_reg0";
4499 case DW_OP_reg1:
4500 return "DW_OP_reg1";
4501 case DW_OP_reg2:
4502 return "DW_OP_reg2";
4503 case DW_OP_reg3:
4504 return "DW_OP_reg3";
4505 case DW_OP_reg4:
4506 return "DW_OP_reg4";
4507 case DW_OP_reg5:
4508 return "DW_OP_reg5";
4509 case DW_OP_reg6:
4510 return "DW_OP_reg6";
4511 case DW_OP_reg7:
4512 return "DW_OP_reg7";
4513 case DW_OP_reg8:
4514 return "DW_OP_reg8";
4515 case DW_OP_reg9:
4516 return "DW_OP_reg9";
4517 case DW_OP_reg10:
4518 return "DW_OP_reg10";
4519 case DW_OP_reg11:
4520 return "DW_OP_reg11";
4521 case DW_OP_reg12:
4522 return "DW_OP_reg12";
4523 case DW_OP_reg13:
4524 return "DW_OP_reg13";
4525 case DW_OP_reg14:
4526 return "DW_OP_reg14";
4527 case DW_OP_reg15:
4528 return "DW_OP_reg15";
4529 case DW_OP_reg16:
4530 return "DW_OP_reg16";
4531 case DW_OP_reg17:
4532 return "DW_OP_reg17";
4533 case DW_OP_reg18:
4534 return "DW_OP_reg18";
4535 case DW_OP_reg19:
4536 return "DW_OP_reg19";
4537 case DW_OP_reg20:
4538 return "DW_OP_reg20";
4539 case DW_OP_reg21:
4540 return "DW_OP_reg21";
4541 case DW_OP_reg22:
4542 return "DW_OP_reg22";
4543 case DW_OP_reg23:
4544 return "DW_OP_reg23";
4545 case DW_OP_reg24:
4546 return "DW_OP_reg24";
4547 case DW_OP_reg25:
4548 return "DW_OP_reg25";
4549 case DW_OP_reg26:
4550 return "DW_OP_reg26";
4551 case DW_OP_reg27:
4552 return "DW_OP_reg27";
4553 case DW_OP_reg28:
4554 return "DW_OP_reg28";
4555 case DW_OP_reg29:
4556 return "DW_OP_reg29";
4557 case DW_OP_reg30:
4558 return "DW_OP_reg30";
4559 case DW_OP_reg31:
4560 return "DW_OP_reg31";
4561 case DW_OP_breg0:
4562 return "DW_OP_breg0";
4563 case DW_OP_breg1:
4564 return "DW_OP_breg1";
4565 case DW_OP_breg2:
4566 return "DW_OP_breg2";
4567 case DW_OP_breg3:
4568 return "DW_OP_breg3";
4569 case DW_OP_breg4:
4570 return "DW_OP_breg4";
4571 case DW_OP_breg5:
4572 return "DW_OP_breg5";
4573 case DW_OP_breg6:
4574 return "DW_OP_breg6";
4575 case DW_OP_breg7:
4576 return "DW_OP_breg7";
4577 case DW_OP_breg8:
4578 return "DW_OP_breg8";
4579 case DW_OP_breg9:
4580 return "DW_OP_breg9";
4581 case DW_OP_breg10:
4582 return "DW_OP_breg10";
4583 case DW_OP_breg11:
4584 return "DW_OP_breg11";
4585 case DW_OP_breg12:
4586 return "DW_OP_breg12";
4587 case DW_OP_breg13:
4588 return "DW_OP_breg13";
4589 case DW_OP_breg14:
4590 return "DW_OP_breg14";
4591 case DW_OP_breg15:
4592 return "DW_OP_breg15";
4593 case DW_OP_breg16:
4594 return "DW_OP_breg16";
4595 case DW_OP_breg17:
4596 return "DW_OP_breg17";
4597 case DW_OP_breg18:
4598 return "DW_OP_breg18";
4599 case DW_OP_breg19:
4600 return "DW_OP_breg19";
4601 case DW_OP_breg20:
4602 return "DW_OP_breg20";
4603 case DW_OP_breg21:
4604 return "DW_OP_breg21";
4605 case DW_OP_breg22:
4606 return "DW_OP_breg22";
4607 case DW_OP_breg23:
4608 return "DW_OP_breg23";
4609 case DW_OP_breg24:
4610 return "DW_OP_breg24";
4611 case DW_OP_breg25:
4612 return "DW_OP_breg25";
4613 case DW_OP_breg26:
4614 return "DW_OP_breg26";
4615 case DW_OP_breg27:
4616 return "DW_OP_breg27";
4617 case DW_OP_breg28:
4618 return "DW_OP_breg28";
4619 case DW_OP_breg29:
4620 return "DW_OP_breg29";
4621 case DW_OP_breg30:
4622 return "DW_OP_breg30";
4623 case DW_OP_breg31:
4624 return "DW_OP_breg31";
4625 case DW_OP_regx:
4626 return "DW_OP_regx";
4627 case DW_OP_fbreg:
4628 return "DW_OP_fbreg";
4629 case DW_OP_bregx:
4630 return "DW_OP_bregx";
4631 case DW_OP_piece:
4632 return "DW_OP_piece";
4633 case DW_OP_deref_size:
4634 return "DW_OP_deref_size";
4635 case DW_OP_xderef_size:
4636 return "DW_OP_xderef_size";
4637 case DW_OP_nop:
4638 return "DW_OP_nop";
4640 case DW_OP_push_object_address:
4641 return "DW_OP_push_object_address";
4642 case DW_OP_call2:
4643 return "DW_OP_call2";
4644 case DW_OP_call4:
4645 return "DW_OP_call4";
4646 case DW_OP_call_ref:
4647 return "DW_OP_call_ref";
4648 case DW_OP_implicit_value:
4649 return "DW_OP_implicit_value";
4650 case DW_OP_stack_value:
4651 return "DW_OP_stack_value";
4652 case DW_OP_form_tls_address:
4653 return "DW_OP_form_tls_address";
4654 case DW_OP_call_frame_cfa:
4655 return "DW_OP_call_frame_cfa";
4656 case DW_OP_bit_piece:
4657 return "DW_OP_bit_piece";
4659 case DW_OP_GNU_push_tls_address:
4660 return "DW_OP_GNU_push_tls_address";
4661 case DW_OP_GNU_uninit:
4662 return "DW_OP_GNU_uninit";
4663 case DW_OP_GNU_encoded_addr:
4664 return "DW_OP_GNU_encoded_addr";
4666 default:
4667 return "OP_<unknown>";
4671 /* Return a pointer to a newly allocated location description. Location
4672 descriptions are simple expression terms that can be strung
4673 together to form more complicated location (address) descriptions. */
4675 static inline dw_loc_descr_ref
4676 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4677 unsigned HOST_WIDE_INT oprnd2)
4679 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4681 descr->dw_loc_opc = op;
4682 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4683 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4684 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4685 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4687 return descr;
4690 /* Return a pointer to a newly allocated location description for
4691 REG and OFFSET. */
4693 static inline dw_loc_descr_ref
4694 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
4696 if (reg <= 31)
4697 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4698 offset, 0);
4699 else
4700 return new_loc_descr (DW_OP_bregx, reg, offset);
4703 /* Add a location description term to a location description expression. */
4705 static inline void
4706 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4708 dw_loc_descr_ref *d;
4710 /* Find the end of the chain. */
4711 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4714 *d = descr;
4717 /* Add a constant OFFSET to a location expression. */
4719 static void
4720 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4722 dw_loc_descr_ref loc;
4723 HOST_WIDE_INT *p;
4725 gcc_assert (*list_head != NULL);
4727 if (!offset)
4728 return;
4730 /* Find the end of the chain. */
4731 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4734 p = NULL;
4735 if (loc->dw_loc_opc == DW_OP_fbreg
4736 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4737 p = &loc->dw_loc_oprnd1.v.val_int;
4738 else if (loc->dw_loc_opc == DW_OP_bregx)
4739 p = &loc->dw_loc_oprnd2.v.val_int;
4741 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4742 offset. Don't optimize if an signed integer overflow would happen. */
4743 if (p != NULL
4744 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4745 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4746 *p += offset;
4748 else if (offset > 0)
4749 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4751 else
4753 loc->dw_loc_next = int_loc_descriptor (-offset);
4754 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4758 #ifdef DWARF2_DEBUGGING_INFO
4759 /* Add a constant OFFSET to a location list. */
4761 static void
4762 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4764 dw_loc_list_ref d;
4765 for (d = list_head; d != NULL; d = d->dw_loc_next)
4766 loc_descr_plus_const (&d->expr, offset);
4768 #endif
4770 /* Return the size of a location descriptor. */
4772 static unsigned long
4773 size_of_loc_descr (dw_loc_descr_ref loc)
4775 unsigned long size = 1;
4777 switch (loc->dw_loc_opc)
4779 case DW_OP_addr:
4780 size += DWARF2_ADDR_SIZE;
4781 break;
4782 case DW_OP_const1u:
4783 case DW_OP_const1s:
4784 size += 1;
4785 break;
4786 case DW_OP_const2u:
4787 case DW_OP_const2s:
4788 size += 2;
4789 break;
4790 case DW_OP_const4u:
4791 case DW_OP_const4s:
4792 size += 4;
4793 break;
4794 case DW_OP_const8u:
4795 case DW_OP_const8s:
4796 size += 8;
4797 break;
4798 case DW_OP_constu:
4799 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4800 break;
4801 case DW_OP_consts:
4802 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4803 break;
4804 case DW_OP_pick:
4805 size += 1;
4806 break;
4807 case DW_OP_plus_uconst:
4808 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4809 break;
4810 case DW_OP_skip:
4811 case DW_OP_bra:
4812 size += 2;
4813 break;
4814 case DW_OP_breg0:
4815 case DW_OP_breg1:
4816 case DW_OP_breg2:
4817 case DW_OP_breg3:
4818 case DW_OP_breg4:
4819 case DW_OP_breg5:
4820 case DW_OP_breg6:
4821 case DW_OP_breg7:
4822 case DW_OP_breg8:
4823 case DW_OP_breg9:
4824 case DW_OP_breg10:
4825 case DW_OP_breg11:
4826 case DW_OP_breg12:
4827 case DW_OP_breg13:
4828 case DW_OP_breg14:
4829 case DW_OP_breg15:
4830 case DW_OP_breg16:
4831 case DW_OP_breg17:
4832 case DW_OP_breg18:
4833 case DW_OP_breg19:
4834 case DW_OP_breg20:
4835 case DW_OP_breg21:
4836 case DW_OP_breg22:
4837 case DW_OP_breg23:
4838 case DW_OP_breg24:
4839 case DW_OP_breg25:
4840 case DW_OP_breg26:
4841 case DW_OP_breg27:
4842 case DW_OP_breg28:
4843 case DW_OP_breg29:
4844 case DW_OP_breg30:
4845 case DW_OP_breg31:
4846 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4847 break;
4848 case DW_OP_regx:
4849 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4850 break;
4851 case DW_OP_fbreg:
4852 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4853 break;
4854 case DW_OP_bregx:
4855 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4856 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4857 break;
4858 case DW_OP_piece:
4859 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4860 break;
4861 case DW_OP_bit_piece:
4862 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4863 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4864 break;
4865 case DW_OP_deref_size:
4866 case DW_OP_xderef_size:
4867 size += 1;
4868 break;
4869 case DW_OP_call2:
4870 size += 2;
4871 break;
4872 case DW_OP_call4:
4873 size += 4;
4874 break;
4875 case DW_OP_call_ref:
4876 size += DWARF2_ADDR_SIZE;
4877 break;
4878 case DW_OP_implicit_value:
4879 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4880 + loc->dw_loc_oprnd1.v.val_unsigned;
4881 break;
4882 default:
4883 break;
4886 return size;
4889 /* Return the size of a series of location descriptors. */
4891 static unsigned long
4892 size_of_locs (dw_loc_descr_ref loc)
4894 dw_loc_descr_ref l;
4895 unsigned long size;
4897 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4898 field, to avoid writing to a PCH file. */
4899 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4901 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4902 break;
4903 size += size_of_loc_descr (l);
4905 if (! l)
4906 return size;
4908 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4910 l->dw_loc_addr = size;
4911 size += size_of_loc_descr (l);
4914 return size;
4917 #ifdef DWARF2_DEBUGGING_INFO
4918 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4919 #endif
4921 /* Output location description stack opcode's operands (if any). */
4923 static void
4924 output_loc_operands (dw_loc_descr_ref loc)
4926 dw_val_ref val1 = &loc->dw_loc_oprnd1;
4927 dw_val_ref val2 = &loc->dw_loc_oprnd2;
4929 switch (loc->dw_loc_opc)
4931 #ifdef DWARF2_DEBUGGING_INFO
4932 case DW_OP_const2u:
4933 case DW_OP_const2s:
4934 dw2_asm_output_data (2, val1->v.val_int, NULL);
4935 break;
4936 case DW_OP_const4u:
4937 if (loc->dtprel)
4939 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4940 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
4941 val1->v.val_addr);
4942 fputc ('\n', asm_out_file);
4943 break;
4945 /* FALLTHRU */
4946 case DW_OP_const4s:
4947 dw2_asm_output_data (4, val1->v.val_int, NULL);
4948 break;
4949 case DW_OP_const8u:
4950 if (loc->dtprel)
4952 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4953 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
4954 val1->v.val_addr);
4955 fputc ('\n', asm_out_file);
4956 break;
4958 /* FALLTHRU */
4959 case DW_OP_const8s:
4960 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4961 dw2_asm_output_data (8, val1->v.val_int, NULL);
4962 break;
4963 case DW_OP_skip:
4964 case DW_OP_bra:
4966 int offset;
4968 gcc_assert (val1->val_class == dw_val_class_loc);
4969 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4971 dw2_asm_output_data (2, offset, NULL);
4973 break;
4974 case DW_OP_implicit_value:
4975 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4976 switch (val2->val_class)
4978 case dw_val_class_const:
4979 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
4980 break;
4981 case dw_val_class_vec:
4983 unsigned int elt_size = val2->v.val_vec.elt_size;
4984 unsigned int len = val2->v.val_vec.length;
4985 unsigned int i;
4986 unsigned char *p;
4988 if (elt_size > sizeof (HOST_WIDE_INT))
4990 elt_size /= 2;
4991 len *= 2;
4993 for (i = 0, p = val2->v.val_vec.array;
4994 i < len;
4995 i++, p += elt_size)
4996 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
4997 "fp or vector constant word %u", i);
4999 break;
5000 case dw_val_class_const_double:
5002 unsigned HOST_WIDE_INT first, second;
5004 if (WORDS_BIG_ENDIAN)
5006 first = val2->v.val_double.high;
5007 second = val2->v.val_double.low;
5009 else
5011 first = val2->v.val_double.low;
5012 second = val2->v.val_double.high;
5014 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5015 first, NULL);
5016 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5017 second, NULL);
5019 break;
5020 case dw_val_class_addr:
5021 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5022 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5023 break;
5024 default:
5025 gcc_unreachable ();
5027 break;
5028 #else
5029 case DW_OP_const2u:
5030 case DW_OP_const2s:
5031 case DW_OP_const4u:
5032 case DW_OP_const4s:
5033 case DW_OP_const8u:
5034 case DW_OP_const8s:
5035 case DW_OP_skip:
5036 case DW_OP_bra:
5037 case DW_OP_implicit_value:
5038 /* We currently don't make any attempt to make sure these are
5039 aligned properly like we do for the main unwind info, so
5040 don't support emitting things larger than a byte if we're
5041 only doing unwinding. */
5042 gcc_unreachable ();
5043 #endif
5044 case DW_OP_const1u:
5045 case DW_OP_const1s:
5046 dw2_asm_output_data (1, val1->v.val_int, NULL);
5047 break;
5048 case DW_OP_constu:
5049 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5050 break;
5051 case DW_OP_consts:
5052 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5053 break;
5054 case DW_OP_pick:
5055 dw2_asm_output_data (1, val1->v.val_int, NULL);
5056 break;
5057 case DW_OP_plus_uconst:
5058 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5059 break;
5060 case DW_OP_breg0:
5061 case DW_OP_breg1:
5062 case DW_OP_breg2:
5063 case DW_OP_breg3:
5064 case DW_OP_breg4:
5065 case DW_OP_breg5:
5066 case DW_OP_breg6:
5067 case DW_OP_breg7:
5068 case DW_OP_breg8:
5069 case DW_OP_breg9:
5070 case DW_OP_breg10:
5071 case DW_OP_breg11:
5072 case DW_OP_breg12:
5073 case DW_OP_breg13:
5074 case DW_OP_breg14:
5075 case DW_OP_breg15:
5076 case DW_OP_breg16:
5077 case DW_OP_breg17:
5078 case DW_OP_breg18:
5079 case DW_OP_breg19:
5080 case DW_OP_breg20:
5081 case DW_OP_breg21:
5082 case DW_OP_breg22:
5083 case DW_OP_breg23:
5084 case DW_OP_breg24:
5085 case DW_OP_breg25:
5086 case DW_OP_breg26:
5087 case DW_OP_breg27:
5088 case DW_OP_breg28:
5089 case DW_OP_breg29:
5090 case DW_OP_breg30:
5091 case DW_OP_breg31:
5092 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5093 break;
5094 case DW_OP_regx:
5095 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5096 break;
5097 case DW_OP_fbreg:
5098 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5099 break;
5100 case DW_OP_bregx:
5101 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5102 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5103 break;
5104 case DW_OP_piece:
5105 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5106 break;
5107 case DW_OP_bit_piece:
5108 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5109 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5110 break;
5111 case DW_OP_deref_size:
5112 case DW_OP_xderef_size:
5113 dw2_asm_output_data (1, val1->v.val_int, NULL);
5114 break;
5116 case DW_OP_addr:
5117 if (loc->dtprel)
5119 if (targetm.asm_out.output_dwarf_dtprel)
5121 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5122 DWARF2_ADDR_SIZE,
5123 val1->v.val_addr);
5124 fputc ('\n', asm_out_file);
5126 else
5127 gcc_unreachable ();
5129 else
5131 #ifdef DWARF2_DEBUGGING_INFO
5132 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5133 #else
5134 gcc_unreachable ();
5135 #endif
5137 break;
5139 default:
5140 /* Other codes have no operands. */
5141 break;
5145 /* Output a sequence of location operations. */
5147 static void
5148 output_loc_sequence (dw_loc_descr_ref loc)
5150 for (; loc != NULL; loc = loc->dw_loc_next)
5152 /* Output the opcode. */
5153 dw2_asm_output_data (1, loc->dw_loc_opc,
5154 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5156 /* Output the operand(s) (if any). */
5157 output_loc_operands (loc);
5161 /* Output location description stack opcode's operands (if any).
5162 The output is single bytes on a line, suitable for .cfi_escape. */
5164 static void
5165 output_loc_operands_raw (dw_loc_descr_ref loc)
5167 dw_val_ref val1 = &loc->dw_loc_oprnd1;
5168 dw_val_ref val2 = &loc->dw_loc_oprnd2;
5170 switch (loc->dw_loc_opc)
5172 case DW_OP_addr:
5173 case DW_OP_implicit_value:
5174 /* We cannot output addresses in .cfi_escape, only bytes. */
5175 gcc_unreachable ();
5177 case DW_OP_const1u:
5178 case DW_OP_const1s:
5179 case DW_OP_pick:
5180 case DW_OP_deref_size:
5181 case DW_OP_xderef_size:
5182 fputc (',', asm_out_file);
5183 dw2_asm_output_data_raw (1, val1->v.val_int);
5184 break;
5186 case DW_OP_const2u:
5187 case DW_OP_const2s:
5188 fputc (',', asm_out_file);
5189 dw2_asm_output_data_raw (2, val1->v.val_int);
5190 break;
5192 case DW_OP_const4u:
5193 case DW_OP_const4s:
5194 fputc (',', asm_out_file);
5195 dw2_asm_output_data_raw (4, val1->v.val_int);
5196 break;
5198 case DW_OP_const8u:
5199 case DW_OP_const8s:
5200 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5201 fputc (',', asm_out_file);
5202 dw2_asm_output_data_raw (8, val1->v.val_int);
5203 break;
5205 case DW_OP_skip:
5206 case DW_OP_bra:
5208 int offset;
5210 gcc_assert (val1->val_class == dw_val_class_loc);
5211 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5213 fputc (',', asm_out_file);
5214 dw2_asm_output_data_raw (2, offset);
5216 break;
5218 case DW_OP_constu:
5219 case DW_OP_plus_uconst:
5220 case DW_OP_regx:
5221 case DW_OP_piece:
5222 fputc (',', asm_out_file);
5223 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5224 break;
5226 case DW_OP_bit_piece:
5227 fputc (',', asm_out_file);
5228 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5229 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5230 break;
5232 case DW_OP_consts:
5233 case DW_OP_breg0:
5234 case DW_OP_breg1:
5235 case DW_OP_breg2:
5236 case DW_OP_breg3:
5237 case DW_OP_breg4:
5238 case DW_OP_breg5:
5239 case DW_OP_breg6:
5240 case DW_OP_breg7:
5241 case DW_OP_breg8:
5242 case DW_OP_breg9:
5243 case DW_OP_breg10:
5244 case DW_OP_breg11:
5245 case DW_OP_breg12:
5246 case DW_OP_breg13:
5247 case DW_OP_breg14:
5248 case DW_OP_breg15:
5249 case DW_OP_breg16:
5250 case DW_OP_breg17:
5251 case DW_OP_breg18:
5252 case DW_OP_breg19:
5253 case DW_OP_breg20:
5254 case DW_OP_breg21:
5255 case DW_OP_breg22:
5256 case DW_OP_breg23:
5257 case DW_OP_breg24:
5258 case DW_OP_breg25:
5259 case DW_OP_breg26:
5260 case DW_OP_breg27:
5261 case DW_OP_breg28:
5262 case DW_OP_breg29:
5263 case DW_OP_breg30:
5264 case DW_OP_breg31:
5265 case DW_OP_fbreg:
5266 fputc (',', asm_out_file);
5267 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5268 break;
5270 case DW_OP_bregx:
5271 fputc (',', asm_out_file);
5272 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5273 fputc (',', asm_out_file);
5274 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5275 break;
5277 default:
5278 /* Other codes have no operands. */
5279 break;
5283 static void
5284 output_loc_sequence_raw (dw_loc_descr_ref loc)
5286 while (1)
5288 /* Output the opcode. */
5289 fprintf (asm_out_file, "%#x", loc->dw_loc_opc);
5290 output_loc_operands_raw (loc);
5292 if (!loc->dw_loc_next)
5293 break;
5294 loc = loc->dw_loc_next;
5296 fputc (',', asm_out_file);
5300 /* This routine will generate the correct assembly data for a location
5301 description based on a cfi entry with a complex address. */
5303 static void
5304 output_cfa_loc (dw_cfi_ref cfi)
5306 dw_loc_descr_ref loc;
5307 unsigned long size;
5309 if (cfi->dw_cfi_opc == DW_CFA_expression)
5311 dw2_asm_output_data (1, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, NULL);
5312 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5314 else
5315 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5317 /* Output the size of the block. */
5318 size = size_of_locs (loc);
5319 dw2_asm_output_data_uleb128 (size, NULL);
5321 /* Now output the operations themselves. */
5322 output_loc_sequence (loc);
5325 /* Similar, but used for .cfi_escape. */
5327 static void
5328 output_cfa_loc_raw (dw_cfi_ref cfi)
5330 dw_loc_descr_ref loc;
5331 unsigned long size;
5333 if (cfi->dw_cfi_opc == DW_CFA_expression)
5335 fprintf (asm_out_file, "%#x,", cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
5336 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5338 else
5339 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5341 /* Output the size of the block. */
5342 size = size_of_locs (loc);
5343 dw2_asm_output_data_uleb128_raw (size);
5344 fputc (',', asm_out_file);
5346 /* Now output the operations themselves. */
5347 output_loc_sequence_raw (loc);
5350 /* This function builds a dwarf location descriptor sequence from a
5351 dw_cfa_location, adding the given OFFSET to the result of the
5352 expression. */
5354 static struct dw_loc_descr_struct *
5355 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5357 struct dw_loc_descr_struct *head, *tmp;
5359 offset += cfa->offset;
5361 if (cfa->indirect)
5363 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5364 head->dw_loc_oprnd1.val_class = dw_val_class_const;
5365 tmp = new_loc_descr (DW_OP_deref, 0, 0);
5366 add_loc_descr (&head, tmp);
5367 if (offset != 0)
5369 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5370 add_loc_descr (&head, tmp);
5373 else
5374 head = new_reg_loc_descr (cfa->reg, offset);
5376 return head;
5379 /* This function builds a dwarf location descriptor sequence for
5380 the address at OFFSET from the CFA when stack is aligned to
5381 ALIGNMENT byte. */
5383 static struct dw_loc_descr_struct *
5384 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5386 struct dw_loc_descr_struct *head;
5387 unsigned int dwarf_fp
5388 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5390 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
5391 if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5393 head = new_reg_loc_descr (dwarf_fp, 0);
5394 add_loc_descr (&head, int_loc_descriptor (alignment));
5395 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5396 loc_descr_plus_const (&head, offset);
5398 else
5399 head = new_reg_loc_descr (dwarf_fp, offset);
5400 return head;
5403 /* This function fills in aa dw_cfa_location structure from a dwarf location
5404 descriptor sequence. */
5406 static void
5407 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5409 struct dw_loc_descr_struct *ptr;
5410 cfa->offset = 0;
5411 cfa->base_offset = 0;
5412 cfa->indirect = 0;
5413 cfa->reg = -1;
5415 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5417 enum dwarf_location_atom op = ptr->dw_loc_opc;
5419 switch (op)
5421 case DW_OP_reg0:
5422 case DW_OP_reg1:
5423 case DW_OP_reg2:
5424 case DW_OP_reg3:
5425 case DW_OP_reg4:
5426 case DW_OP_reg5:
5427 case DW_OP_reg6:
5428 case DW_OP_reg7:
5429 case DW_OP_reg8:
5430 case DW_OP_reg9:
5431 case DW_OP_reg10:
5432 case DW_OP_reg11:
5433 case DW_OP_reg12:
5434 case DW_OP_reg13:
5435 case DW_OP_reg14:
5436 case DW_OP_reg15:
5437 case DW_OP_reg16:
5438 case DW_OP_reg17:
5439 case DW_OP_reg18:
5440 case DW_OP_reg19:
5441 case DW_OP_reg20:
5442 case DW_OP_reg21:
5443 case DW_OP_reg22:
5444 case DW_OP_reg23:
5445 case DW_OP_reg24:
5446 case DW_OP_reg25:
5447 case DW_OP_reg26:
5448 case DW_OP_reg27:
5449 case DW_OP_reg28:
5450 case DW_OP_reg29:
5451 case DW_OP_reg30:
5452 case DW_OP_reg31:
5453 cfa->reg = op - DW_OP_reg0;
5454 break;
5455 case DW_OP_regx:
5456 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5457 break;
5458 case DW_OP_breg0:
5459 case DW_OP_breg1:
5460 case DW_OP_breg2:
5461 case DW_OP_breg3:
5462 case DW_OP_breg4:
5463 case DW_OP_breg5:
5464 case DW_OP_breg6:
5465 case DW_OP_breg7:
5466 case DW_OP_breg8:
5467 case DW_OP_breg9:
5468 case DW_OP_breg10:
5469 case DW_OP_breg11:
5470 case DW_OP_breg12:
5471 case DW_OP_breg13:
5472 case DW_OP_breg14:
5473 case DW_OP_breg15:
5474 case DW_OP_breg16:
5475 case DW_OP_breg17:
5476 case DW_OP_breg18:
5477 case DW_OP_breg19:
5478 case DW_OP_breg20:
5479 case DW_OP_breg21:
5480 case DW_OP_breg22:
5481 case DW_OP_breg23:
5482 case DW_OP_breg24:
5483 case DW_OP_breg25:
5484 case DW_OP_breg26:
5485 case DW_OP_breg27:
5486 case DW_OP_breg28:
5487 case DW_OP_breg29:
5488 case DW_OP_breg30:
5489 case DW_OP_breg31:
5490 cfa->reg = op - DW_OP_breg0;
5491 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5492 break;
5493 case DW_OP_bregx:
5494 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5495 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5496 break;
5497 case DW_OP_deref:
5498 cfa->indirect = 1;
5499 break;
5500 case DW_OP_plus_uconst:
5501 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5502 break;
5503 default:
5504 internal_error ("DW_LOC_OP %s not implemented",
5505 dwarf_stack_op_name (ptr->dw_loc_opc));
5509 #endif /* .debug_frame support */
5511 /* And now, the support for symbolic debugging information. */
5512 #ifdef DWARF2_DEBUGGING_INFO
5514 /* .debug_str support. */
5515 static int output_indirect_string (void **, void *);
5517 static void dwarf2out_init (const char *);
5518 static void dwarf2out_finish (const char *);
5519 static void dwarf2out_assembly_start (void);
5520 static void dwarf2out_define (unsigned int, const char *);
5521 static void dwarf2out_undef (unsigned int, const char *);
5522 static void dwarf2out_start_source_file (unsigned, const char *);
5523 static void dwarf2out_end_source_file (unsigned);
5524 static void dwarf2out_function_decl (tree);
5525 static void dwarf2out_begin_block (unsigned, unsigned);
5526 static void dwarf2out_end_block (unsigned, unsigned);
5527 static bool dwarf2out_ignore_block (const_tree);
5528 static void dwarf2out_global_decl (tree);
5529 static void dwarf2out_type_decl (tree, int);
5530 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5531 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5532 dw_die_ref);
5533 static void dwarf2out_abstract_function (tree);
5534 static void dwarf2out_var_location (rtx);
5535 static void dwarf2out_direct_call (tree);
5536 static void dwarf2out_virtual_call_token (tree, int);
5537 static void dwarf2out_copy_call_info (rtx, rtx);
5538 static void dwarf2out_virtual_call (int);
5539 static void dwarf2out_begin_function (tree);
5540 static void dwarf2out_set_name (tree, tree);
5542 /* The debug hooks structure. */
5544 const struct gcc_debug_hooks dwarf2_debug_hooks =
5546 dwarf2out_init,
5547 dwarf2out_finish,
5548 dwarf2out_assembly_start,
5549 dwarf2out_define,
5550 dwarf2out_undef,
5551 dwarf2out_start_source_file,
5552 dwarf2out_end_source_file,
5553 dwarf2out_begin_block,
5554 dwarf2out_end_block,
5555 dwarf2out_ignore_block,
5556 dwarf2out_source_line,
5557 dwarf2out_begin_prologue,
5558 #if VMS_DEBUGGING_INFO
5559 dwarf2out_vms_end_prologue,
5560 dwarf2out_vms_begin_epilogue,
5561 #else
5562 debug_nothing_int_charstar,
5563 debug_nothing_int_charstar,
5564 #endif
5565 dwarf2out_end_epilogue,
5566 dwarf2out_begin_function,
5567 debug_nothing_int, /* end_function */
5568 dwarf2out_function_decl, /* function_decl */
5569 dwarf2out_global_decl,
5570 dwarf2out_type_decl, /* type_decl */
5571 dwarf2out_imported_module_or_decl,
5572 debug_nothing_tree, /* deferred_inline_function */
5573 /* The DWARF 2 backend tries to reduce debugging bloat by not
5574 emitting the abstract description of inline functions until
5575 something tries to reference them. */
5576 dwarf2out_abstract_function, /* outlining_inline_function */
5577 debug_nothing_rtx, /* label */
5578 debug_nothing_int, /* handle_pch */
5579 dwarf2out_var_location,
5580 dwarf2out_switch_text_section,
5581 dwarf2out_direct_call,
5582 dwarf2out_virtual_call_token,
5583 dwarf2out_copy_call_info,
5584 dwarf2out_virtual_call,
5585 dwarf2out_set_name,
5586 1 /* start_end_main_source_file */
5588 #endif
5590 /* NOTE: In the comments in this file, many references are made to
5591 "Debugging Information Entries". This term is abbreviated as `DIE'
5592 throughout the remainder of this file. */
5594 /* An internal representation of the DWARF output is built, and then
5595 walked to generate the DWARF debugging info. The walk of the internal
5596 representation is done after the entire program has been compiled.
5597 The types below are used to describe the internal representation. */
5599 /* Various DIE's use offsets relative to the beginning of the
5600 .debug_info section to refer to each other. */
5602 typedef long int dw_offset;
5604 /* Define typedefs here to avoid circular dependencies. */
5606 typedef struct dw_attr_struct *dw_attr_ref;
5607 typedef struct dw_line_info_struct *dw_line_info_ref;
5608 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5609 typedef struct pubname_struct *pubname_ref;
5610 typedef struct dw_ranges_struct *dw_ranges_ref;
5611 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5612 typedef struct comdat_type_struct *comdat_type_node_ref;
5614 /* Each entry in the line_info_table maintains the file and
5615 line number associated with the label generated for that
5616 entry. The label gives the PC value associated with
5617 the line number entry. */
5619 typedef struct GTY(()) dw_line_info_struct {
5620 unsigned long dw_file_num;
5621 unsigned long dw_line_num;
5623 dw_line_info_entry;
5625 /* Line information for functions in separate sections; each one gets its
5626 own sequence. */
5627 typedef struct GTY(()) dw_separate_line_info_struct {
5628 unsigned long dw_file_num;
5629 unsigned long dw_line_num;
5630 unsigned long function;
5632 dw_separate_line_info_entry;
5634 /* Each DIE attribute has a field specifying the attribute kind,
5635 a link to the next attribute in the chain, and an attribute value.
5636 Attributes are typically linked below the DIE they modify. */
5638 typedef struct GTY(()) dw_attr_struct {
5639 enum dwarf_attribute dw_attr;
5640 dw_val_node dw_attr_val;
5642 dw_attr_node;
5644 DEF_VEC_O(dw_attr_node);
5645 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5647 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
5648 The children of each node form a circular list linked by
5649 die_sib. die_child points to the node *before* the "first" child node. */
5651 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5652 enum dwarf_tag die_tag;
5653 union die_symbol_or_type_node
5655 char * GTY ((tag ("0"))) die_symbol;
5656 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5658 GTY ((desc ("dwarf_version >= 4"))) die_id;
5659 VEC(dw_attr_node,gc) * die_attr;
5660 dw_die_ref die_parent;
5661 dw_die_ref die_child;
5662 dw_die_ref die_sib;
5663 dw_die_ref die_definition; /* ref from a specification to its definition */
5664 dw_offset die_offset;
5665 unsigned long die_abbrev;
5666 int die_mark;
5667 /* Die is used and must not be pruned as unused. */
5668 int die_perennial_p;
5669 unsigned int decl_id;
5671 die_node;
5673 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
5674 #define FOR_EACH_CHILD(die, c, expr) do { \
5675 c = die->die_child; \
5676 if (c) do { \
5677 c = c->die_sib; \
5678 expr; \
5679 } while (c != die->die_child); \
5680 } while (0)
5682 /* The pubname structure */
5684 typedef struct GTY(()) pubname_struct {
5685 dw_die_ref die;
5686 const char *name;
5688 pubname_entry;
5690 DEF_VEC_O(pubname_entry);
5691 DEF_VEC_ALLOC_O(pubname_entry, gc);
5693 struct GTY(()) dw_ranges_struct {
5694 /* If this is positive, it's a block number, otherwise it's a
5695 bitwise-negated index into dw_ranges_by_label. */
5696 int num;
5699 struct GTY(()) dw_ranges_by_label_struct {
5700 const char *begin;
5701 const char *end;
5704 /* The comdat type node structure. */
5705 typedef struct GTY(()) comdat_type_struct
5707 dw_die_ref root_die;
5708 dw_die_ref type_die;
5709 char signature[DWARF_TYPE_SIGNATURE_SIZE];
5710 struct comdat_type_struct *next;
5712 comdat_type_node;
5714 /* The limbo die list structure. */
5715 typedef struct GTY(()) limbo_die_struct {
5716 dw_die_ref die;
5717 tree created_for;
5718 struct limbo_die_struct *next;
5720 limbo_die_node;
5722 typedef struct GTY(()) skeleton_chain_struct
5724 dw_die_ref old_die;
5725 dw_die_ref new_die;
5726 struct skeleton_chain_struct *parent;
5728 skeleton_chain_node;
5730 /* How to start an assembler comment. */
5731 #ifndef ASM_COMMENT_START
5732 #define ASM_COMMENT_START ";#"
5733 #endif
5735 /* Define a macro which returns nonzero for a TYPE_DECL which was
5736 implicitly generated for a tagged type.
5738 Note that unlike the gcc front end (which generates a NULL named
5739 TYPE_DECL node for each complete tagged type, each array type, and
5740 each function type node created) the g++ front end generates a
5741 _named_ TYPE_DECL node for each tagged type node created.
5742 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5743 generate a DW_TAG_typedef DIE for them. */
5745 #define TYPE_DECL_IS_STUB(decl) \
5746 (DECL_NAME (decl) == NULL_TREE \
5747 || (DECL_ARTIFICIAL (decl) \
5748 && is_tagged_type (TREE_TYPE (decl)) \
5749 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
5750 /* This is necessary for stub decls that \
5751 appear in nested inline functions. */ \
5752 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5753 && (decl_ultimate_origin (decl) \
5754 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5756 /* Information concerning the compilation unit's programming
5757 language, and compiler version. */
5759 /* Fixed size portion of the DWARF compilation unit header. */
5760 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5761 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5763 /* Fixed size portion of the DWARF comdat type unit header. */
5764 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5765 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5766 + DWARF_OFFSET_SIZE)
5768 /* Fixed size portion of public names info. */
5769 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5771 /* Fixed size portion of the address range info. */
5772 #define DWARF_ARANGES_HEADER_SIZE \
5773 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5774 DWARF2_ADDR_SIZE * 2) \
5775 - DWARF_INITIAL_LENGTH_SIZE)
5777 /* Size of padding portion in the address range info. It must be
5778 aligned to twice the pointer size. */
5779 #define DWARF_ARANGES_PAD_SIZE \
5780 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5781 DWARF2_ADDR_SIZE * 2) \
5782 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5784 /* Use assembler line directives if available. */
5785 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5786 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5787 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5788 #else
5789 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5790 #endif
5791 #endif
5793 /* Minimum line offset in a special line info. opcode.
5794 This value was chosen to give a reasonable range of values. */
5795 #define DWARF_LINE_BASE -10
5797 /* First special line opcode - leave room for the standard opcodes. */
5798 #define DWARF_LINE_OPCODE_BASE 10
5800 /* Range of line offsets in a special line info. opcode. */
5801 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
5803 /* Flag that indicates the initial value of the is_stmt_start flag.
5804 In the present implementation, we do not mark any lines as
5805 the beginning of a source statement, because that information
5806 is not made available by the GCC front-end. */
5807 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5809 /* Maximum number of operations per instruction bundle. */
5810 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
5811 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
5812 #endif
5814 #ifdef DWARF2_DEBUGGING_INFO
5815 /* This location is used by calc_die_sizes() to keep track
5816 the offset of each DIE within the .debug_info section. */
5817 static unsigned long next_die_offset;
5818 #endif
5820 /* Record the root of the DIE's built for the current compilation unit. */
5821 static GTY(()) dw_die_ref comp_unit_die;
5823 /* A list of type DIEs that have been separated into comdat sections. */
5824 static GTY(()) comdat_type_node *comdat_type_list;
5826 /* A list of DIEs with a NULL parent waiting to be relocated. */
5827 static GTY(()) limbo_die_node *limbo_die_list;
5829 /* A list of DIEs for which we may have to generate
5830 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
5831 static GTY(()) limbo_die_node *deferred_asm_name;
5833 /* Filenames referenced by this compilation unit. */
5834 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5836 /* A hash table of references to DIE's that describe declarations.
5837 The key is a DECL_UID() which is a unique number identifying each decl. */
5838 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5840 /* A hash table of references to DIE's that describe COMMON blocks.
5841 The key is DECL_UID() ^ die_parent. */
5842 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5844 typedef struct GTY(()) die_arg_entry_struct {
5845 dw_die_ref die;
5846 tree arg;
5847 } die_arg_entry;
5849 DEF_VEC_O(die_arg_entry);
5850 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5852 /* Node of the variable location list. */
5853 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5854 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
5855 EXPR_LIST chain. For small bitsizes, bitsize is encoded
5856 in mode of the EXPR_LIST node and first EXPR_LIST operand
5857 is either NOTE_INSN_VAR_LOCATION for a piece with a known
5858 location or NULL for padding. For larger bitsizes,
5859 mode is 0 and first operand is a CONCAT with bitsize
5860 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
5861 NULL as second operand. */
5862 rtx GTY (()) loc;
5863 const char * GTY (()) label;
5864 struct var_loc_node * GTY (()) next;
5867 /* Variable location list. */
5868 struct GTY (()) var_loc_list_def {
5869 struct var_loc_node * GTY (()) first;
5871 /* Pointer to the last but one or last element of the
5872 chained list. If the list is empty, both first and
5873 last are NULL, if the list contains just one node
5874 or the last node certainly is not redundant, it points
5875 to the last node, otherwise points to the last but one.
5876 Do not mark it for GC because it is marked through the chain. */
5877 struct var_loc_node * GTY ((skip ("%h"))) last;
5879 /* DECL_UID of the variable decl. */
5880 unsigned int decl_id;
5882 typedef struct var_loc_list_def var_loc_list;
5885 /* Table of decl location linked lists. */
5886 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5888 /* A pointer to the base of a list of references to DIE's that
5889 are uniquely identified by their tag, presence/absence of
5890 children DIE's, and list of attribute/value pairs. */
5891 static GTY((length ("abbrev_die_table_allocated")))
5892 dw_die_ref *abbrev_die_table;
5894 /* Number of elements currently allocated for abbrev_die_table. */
5895 static GTY(()) unsigned abbrev_die_table_allocated;
5897 /* Number of elements in type_die_table currently in use. */
5898 static GTY(()) unsigned abbrev_die_table_in_use;
5900 /* Size (in elements) of increments by which we may expand the
5901 abbrev_die_table. */
5902 #define ABBREV_DIE_TABLE_INCREMENT 256
5904 /* A pointer to the base of a table that contains line information
5905 for each source code line in .text in the compilation unit. */
5906 static GTY((length ("line_info_table_allocated")))
5907 dw_line_info_ref line_info_table;
5909 /* Number of elements currently allocated for line_info_table. */
5910 static GTY(()) unsigned line_info_table_allocated;
5912 /* Number of elements in line_info_table currently in use. */
5913 static GTY(()) unsigned line_info_table_in_use;
5915 /* A pointer to the base of a table that contains line information
5916 for each source code line outside of .text in the compilation unit. */
5917 static GTY ((length ("separate_line_info_table_allocated")))
5918 dw_separate_line_info_ref separate_line_info_table;
5920 /* Number of elements currently allocated for separate_line_info_table. */
5921 static GTY(()) unsigned separate_line_info_table_allocated;
5923 /* Number of elements in separate_line_info_table currently in use. */
5924 static GTY(()) unsigned separate_line_info_table_in_use;
5926 /* Size (in elements) of increments by which we may expand the
5927 line_info_table. */
5928 #define LINE_INFO_TABLE_INCREMENT 1024
5930 /* A pointer to the base of a table that contains a list of publicly
5931 accessible names. */
5932 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
5934 /* A pointer to the base of a table that contains a list of publicly
5935 accessible types. */
5936 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5938 /* Array of dies for which we should generate .debug_arange info. */
5939 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5941 /* Number of elements currently allocated for arange_table. */
5942 static GTY(()) unsigned arange_table_allocated;
5944 /* Number of elements in arange_table currently in use. */
5945 static GTY(()) unsigned arange_table_in_use;
5947 /* Size (in elements) of increments by which we may expand the
5948 arange_table. */
5949 #define ARANGE_TABLE_INCREMENT 64
5951 /* Array of dies for which we should generate .debug_ranges info. */
5952 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5954 /* Number of elements currently allocated for ranges_table. */
5955 static GTY(()) unsigned ranges_table_allocated;
5957 /* Number of elements in ranges_table currently in use. */
5958 static GTY(()) unsigned ranges_table_in_use;
5960 /* Array of pairs of labels referenced in ranges_table. */
5961 static GTY ((length ("ranges_by_label_allocated")))
5962 dw_ranges_by_label_ref ranges_by_label;
5964 /* Number of elements currently allocated for ranges_by_label. */
5965 static GTY(()) unsigned ranges_by_label_allocated;
5967 /* Number of elements in ranges_by_label currently in use. */
5968 static GTY(()) unsigned ranges_by_label_in_use;
5970 /* Size (in elements) of increments by which we may expand the
5971 ranges_table. */
5972 #define RANGES_TABLE_INCREMENT 64
5974 /* Whether we have location lists that need outputting */
5975 static GTY(()) bool have_location_lists;
5977 /* Unique label counter. */
5978 static GTY(()) unsigned int loclabel_num;
5980 /* Unique label counter for point-of-call tables. */
5981 static GTY(()) unsigned int poc_label_num;
5983 /* The direct call table structure. */
5985 typedef struct GTY(()) dcall_struct {
5986 unsigned int poc_label_num;
5987 tree poc_decl;
5988 dw_die_ref targ_die;
5990 dcall_entry;
5992 DEF_VEC_O(dcall_entry);
5993 DEF_VEC_ALLOC_O(dcall_entry, gc);
5995 /* The virtual call table structure. */
5997 typedef struct GTY(()) vcall_struct {
5998 unsigned int poc_label_num;
5999 unsigned int vtable_slot;
6001 vcall_entry;
6003 DEF_VEC_O(vcall_entry);
6004 DEF_VEC_ALLOC_O(vcall_entry, gc);
6006 /* Pointers to the direct and virtual call tables. */
6007 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6008 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6010 /* A hash table to map INSN_UIDs to vtable slot indexes. */
6012 struct GTY (()) vcall_insn {
6013 int insn_uid;
6014 unsigned int vtable_slot;
6017 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6019 #ifdef DWARF2_DEBUGGING_INFO
6020 /* Record whether the function being analyzed contains inlined functions. */
6021 static int current_function_has_inlines;
6022 #endif
6023 #if 0 && defined (MIPS_DEBUGGING_INFO)
6024 static int comp_unit_has_inlines;
6025 #endif
6027 /* The last file entry emitted by maybe_emit_file(). */
6028 static GTY(()) struct dwarf_file_data * last_emitted_file;
6030 /* Number of internal labels generated by gen_internal_sym(). */
6031 static GTY(()) int label_num;
6033 /* Cached result of previous call to lookup_filename. */
6034 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6036 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6038 #ifdef DWARF2_DEBUGGING_INFO
6040 /* Offset from the "steady-state frame pointer" to the frame base,
6041 within the current function. */
6042 static HOST_WIDE_INT frame_pointer_fb_offset;
6044 /* Forward declarations for functions defined in this file. */
6046 static int is_pseudo_reg (const_rtx);
6047 static tree type_main_variant (tree);
6048 static int is_tagged_type (const_tree);
6049 static const char *dwarf_tag_name (unsigned);
6050 static const char *dwarf_attr_name (unsigned);
6051 static const char *dwarf_form_name (unsigned);
6052 static tree decl_ultimate_origin (const_tree);
6053 static tree decl_class_context (tree);
6054 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6055 static inline enum dw_val_class AT_class (dw_attr_ref);
6056 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6057 static inline unsigned AT_flag (dw_attr_ref);
6058 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6059 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6060 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6061 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6062 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6063 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6064 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6065 unsigned int, unsigned char *);
6066 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6067 static hashval_t debug_str_do_hash (const void *);
6068 static int debug_str_eq (const void *, const void *);
6069 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6070 static inline const char *AT_string (dw_attr_ref);
6071 static enum dwarf_form AT_string_form (dw_attr_ref);
6072 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6073 static void add_AT_specification (dw_die_ref, dw_die_ref);
6074 static inline dw_die_ref AT_ref (dw_attr_ref);
6075 static inline int AT_ref_external (dw_attr_ref);
6076 static inline void set_AT_ref_external (dw_attr_ref, int);
6077 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6078 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6079 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6080 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6081 dw_loc_list_ref);
6082 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6083 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6084 static inline rtx AT_addr (dw_attr_ref);
6085 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6086 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6087 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6088 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6089 unsigned HOST_WIDE_INT);
6090 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6091 unsigned long);
6092 static inline const char *AT_lbl (dw_attr_ref);
6093 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6094 static const char *get_AT_low_pc (dw_die_ref);
6095 static const char *get_AT_hi_pc (dw_die_ref);
6096 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6097 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6098 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6099 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6100 static bool is_cxx (void);
6101 static bool is_fortran (void);
6102 static bool is_ada (void);
6103 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6104 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6105 static void add_child_die (dw_die_ref, dw_die_ref);
6106 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6107 static dw_die_ref lookup_type_die (tree);
6108 static void equate_type_number_to_die (tree, dw_die_ref);
6109 static hashval_t decl_die_table_hash (const void *);
6110 static int decl_die_table_eq (const void *, const void *);
6111 static dw_die_ref lookup_decl_die (tree);
6112 static hashval_t common_block_die_table_hash (const void *);
6113 static int common_block_die_table_eq (const void *, const void *);
6114 static hashval_t decl_loc_table_hash (const void *);
6115 static int decl_loc_table_eq (const void *, const void *);
6116 static var_loc_list *lookup_decl_loc (const_tree);
6117 static void equate_decl_number_to_die (tree, dw_die_ref);
6118 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6119 static void print_spaces (FILE *);
6120 static void print_die (dw_die_ref, FILE *);
6121 static void print_dwarf_line_table (FILE *);
6122 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6123 static dw_die_ref pop_compile_unit (dw_die_ref);
6124 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6125 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6126 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6127 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6128 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6129 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6130 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6131 struct md5_ctx *, int *);
6132 struct checksum_attributes;
6133 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6134 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6135 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6136 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6137 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6138 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6139 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6140 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6141 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6142 static void compute_section_prefix (dw_die_ref);
6143 static int is_type_die (dw_die_ref);
6144 static int is_comdat_die (dw_die_ref);
6145 static int is_symbol_die (dw_die_ref);
6146 static void assign_symbol_names (dw_die_ref);
6147 static void break_out_includes (dw_die_ref);
6148 static int is_declaration_die (dw_die_ref);
6149 static int should_move_die_to_comdat (dw_die_ref);
6150 static dw_die_ref clone_as_declaration (dw_die_ref);
6151 static dw_die_ref clone_die (dw_die_ref);
6152 static dw_die_ref clone_tree (dw_die_ref);
6153 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6154 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6155 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6156 static dw_die_ref generate_skeleton (dw_die_ref);
6157 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6158 dw_die_ref);
6159 static void break_out_comdat_types (dw_die_ref);
6160 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6161 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6162 static void copy_decls_for_unworthy_types (dw_die_ref);
6164 static hashval_t htab_cu_hash (const void *);
6165 static int htab_cu_eq (const void *, const void *);
6166 static void htab_cu_del (void *);
6167 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6168 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6169 static void add_sibling_attributes (dw_die_ref);
6170 static void build_abbrev_table (dw_die_ref);
6171 static void output_location_lists (dw_die_ref);
6172 static int constant_size (unsigned HOST_WIDE_INT);
6173 static unsigned long size_of_die (dw_die_ref);
6174 static void calc_die_sizes (dw_die_ref);
6175 static void mark_dies (dw_die_ref);
6176 static void unmark_dies (dw_die_ref);
6177 static void unmark_all_dies (dw_die_ref);
6178 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6179 static unsigned long size_of_aranges (void);
6180 static enum dwarf_form value_format (dw_attr_ref);
6181 static void output_value_format (dw_attr_ref);
6182 static void output_abbrev_section (void);
6183 static void output_die_symbol (dw_die_ref);
6184 static void output_die (dw_die_ref);
6185 static void output_compilation_unit_header (void);
6186 static void output_comp_unit (dw_die_ref, int);
6187 static void output_comdat_type_unit (comdat_type_node *);
6188 static const char *dwarf2_name (tree, int);
6189 static void add_pubname (tree, dw_die_ref);
6190 static void add_pubname_string (const char *, dw_die_ref);
6191 static void add_pubtype (tree, dw_die_ref);
6192 static void output_pubnames (VEC (pubname_entry,gc) *);
6193 static void add_arange (tree, dw_die_ref);
6194 static void output_aranges (void);
6195 static unsigned int add_ranges_num (int);
6196 static unsigned int add_ranges (const_tree);
6197 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6198 bool *);
6199 static void output_ranges (void);
6200 static void output_line_info (void);
6201 static void output_file_names (void);
6202 static dw_die_ref base_type_die (tree);
6203 static int is_base_type (tree);
6204 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6205 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6206 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6207 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6208 static int type_is_enum (const_tree);
6209 static unsigned int dbx_reg_number (const_rtx);
6210 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6211 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6212 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6213 enum var_init_status);
6214 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6215 enum var_init_status);
6216 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6217 enum var_init_status);
6218 static int is_based_loc (const_rtx);
6219 static int resolve_one_addr (rtx *, void *);
6220 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
6221 enum var_init_status);
6222 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6223 enum var_init_status);
6224 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6225 enum var_init_status);
6226 static dw_loc_list_ref loc_list_from_tree (tree, int);
6227 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6228 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6229 static tree field_type (const_tree);
6230 static unsigned int simple_type_align_in_bits (const_tree);
6231 static unsigned int simple_decl_align_in_bits (const_tree);
6232 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6233 static HOST_WIDE_INT field_byte_offset (const_tree);
6234 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6235 dw_loc_list_ref);
6236 static void add_data_member_location_attribute (dw_die_ref, tree);
6237 static bool add_const_value_attribute (dw_die_ref, rtx);
6238 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6239 static void insert_double (double_int, unsigned char *);
6240 static void insert_float (const_rtx, unsigned char *);
6241 static rtx rtl_for_decl_location (tree);
6242 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6243 enum dwarf_attribute);
6244 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6245 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6246 static void add_name_attribute (dw_die_ref, const char *);
6247 static void add_comp_dir_attribute (dw_die_ref);
6248 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6249 static void add_subscript_info (dw_die_ref, tree, bool);
6250 static void add_byte_size_attribute (dw_die_ref, tree);
6251 static void add_bit_offset_attribute (dw_die_ref, tree);
6252 static void add_bit_size_attribute (dw_die_ref, tree);
6253 static void add_prototyped_attribute (dw_die_ref, tree);
6254 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6255 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6256 static void add_src_coords_attributes (dw_die_ref, tree);
6257 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6258 static void push_decl_scope (tree);
6259 static void pop_decl_scope (void);
6260 static dw_die_ref scope_die_for (tree, dw_die_ref);
6261 static inline int local_scope_p (dw_die_ref);
6262 static inline int class_scope_p (dw_die_ref);
6263 static inline int class_or_namespace_scope_p (dw_die_ref);
6264 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6265 static void add_calling_convention_attribute (dw_die_ref, tree);
6266 static const char *type_tag (const_tree);
6267 static tree member_declared_type (const_tree);
6268 #if 0
6269 static const char *decl_start_label (tree);
6270 #endif
6271 static void gen_array_type_die (tree, dw_die_ref);
6272 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6273 #if 0
6274 static void gen_entry_point_die (tree, dw_die_ref);
6275 #endif
6276 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6277 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6278 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
6279 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6280 static void gen_formal_types_die (tree, dw_die_ref);
6281 static void gen_subprogram_die (tree, dw_die_ref);
6282 static void gen_variable_die (tree, tree, dw_die_ref);
6283 static void gen_const_die (tree, dw_die_ref);
6284 static void gen_label_die (tree, dw_die_ref);
6285 static void gen_lexical_block_die (tree, dw_die_ref, int);
6286 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6287 static void gen_field_die (tree, dw_die_ref);
6288 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6289 static dw_die_ref gen_compile_unit_die (const char *);
6290 static void gen_inheritance_die (tree, tree, dw_die_ref);
6291 static void gen_member_die (tree, dw_die_ref);
6292 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6293 enum debug_info_usage);
6294 static void gen_subroutine_type_die (tree, dw_die_ref);
6295 static void gen_typedef_die (tree, dw_die_ref);
6296 static void gen_type_die (tree, dw_die_ref);
6297 static void gen_block_die (tree, dw_die_ref, int);
6298 static void decls_for_scope (tree, dw_die_ref, int);
6299 static int is_redundant_typedef (const_tree);
6300 static bool is_naming_typedef_decl (const_tree);
6301 static inline dw_die_ref get_context_die (tree);
6302 static void gen_namespace_die (tree, dw_die_ref);
6303 static void gen_decl_die (tree, tree, dw_die_ref);
6304 static dw_die_ref force_decl_die (tree);
6305 static dw_die_ref force_type_die (tree);
6306 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6307 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6308 static struct dwarf_file_data * lookup_filename (const char *);
6309 static void retry_incomplete_types (void);
6310 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6311 static void gen_generic_params_dies (tree);
6312 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6313 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6314 static void splice_child_die (dw_die_ref, dw_die_ref);
6315 static int file_info_cmp (const void *, const void *);
6316 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6317 const char *, const char *);
6318 static void output_loc_list (dw_loc_list_ref);
6319 static char *gen_internal_sym (const char *);
6321 static void prune_unmark_dies (dw_die_ref);
6322 static void prune_unused_types_mark (dw_die_ref, int);
6323 static void prune_unused_types_walk (dw_die_ref);
6324 static void prune_unused_types_walk_attribs (dw_die_ref);
6325 static void prune_unused_types_prune (dw_die_ref);
6326 static void prune_unused_types (void);
6327 static int maybe_emit_file (struct dwarf_file_data *fd);
6328 static inline const char *AT_vms_delta1 (dw_attr_ref);
6329 static inline const char *AT_vms_delta2 (dw_attr_ref);
6330 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6331 const char *, const char *);
6332 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6333 static void gen_remaining_tmpl_value_param_die_attribute (void);
6335 /* Section names used to hold DWARF debugging information. */
6336 #ifndef DEBUG_INFO_SECTION
6337 #define DEBUG_INFO_SECTION ".debug_info"
6338 #endif
6339 #ifndef DEBUG_ABBREV_SECTION
6340 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
6341 #endif
6342 #ifndef DEBUG_ARANGES_SECTION
6343 #define DEBUG_ARANGES_SECTION ".debug_aranges"
6344 #endif
6345 #ifndef DEBUG_MACINFO_SECTION
6346 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
6347 #endif
6348 #ifndef DEBUG_LINE_SECTION
6349 #define DEBUG_LINE_SECTION ".debug_line"
6350 #endif
6351 #ifndef DEBUG_LOC_SECTION
6352 #define DEBUG_LOC_SECTION ".debug_loc"
6353 #endif
6354 #ifndef DEBUG_PUBNAMES_SECTION
6355 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
6356 #endif
6357 #ifndef DEBUG_PUBTYPES_SECTION
6358 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
6359 #endif
6360 #ifndef DEBUG_DCALL_SECTION
6361 #define DEBUG_DCALL_SECTION ".debug_dcall"
6362 #endif
6363 #ifndef DEBUG_VCALL_SECTION
6364 #define DEBUG_VCALL_SECTION ".debug_vcall"
6365 #endif
6366 #ifndef DEBUG_STR_SECTION
6367 #define DEBUG_STR_SECTION ".debug_str"
6368 #endif
6369 #ifndef DEBUG_RANGES_SECTION
6370 #define DEBUG_RANGES_SECTION ".debug_ranges"
6371 #endif
6373 /* Standard ELF section names for compiled code and data. */
6374 #ifndef TEXT_SECTION_NAME
6375 #define TEXT_SECTION_NAME ".text"
6376 #endif
6378 /* Section flags for .debug_str section. */
6379 #define DEBUG_STR_SECTION_FLAGS \
6380 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
6381 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
6382 : SECTION_DEBUG)
6384 /* Labels we insert at beginning sections we can reference instead of
6385 the section names themselves. */
6387 #ifndef TEXT_SECTION_LABEL
6388 #define TEXT_SECTION_LABEL "Ltext"
6389 #endif
6390 #ifndef COLD_TEXT_SECTION_LABEL
6391 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
6392 #endif
6393 #ifndef DEBUG_LINE_SECTION_LABEL
6394 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
6395 #endif
6396 #ifndef DEBUG_INFO_SECTION_LABEL
6397 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
6398 #endif
6399 #ifndef DEBUG_ABBREV_SECTION_LABEL
6400 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
6401 #endif
6402 #ifndef DEBUG_LOC_SECTION_LABEL
6403 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
6404 #endif
6405 #ifndef DEBUG_RANGES_SECTION_LABEL
6406 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
6407 #endif
6408 #ifndef DEBUG_MACINFO_SECTION_LABEL
6409 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
6410 #endif
6413 /* Definitions of defaults for formats and names of various special
6414 (artificial) labels which may be generated within this file (when the -g
6415 options is used and DWARF2_DEBUGGING_INFO is in effect.
6416 If necessary, these may be overridden from within the tm.h file, but
6417 typically, overriding these defaults is unnecessary. */
6419 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6420 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6421 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6422 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6423 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6424 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6425 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6426 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6427 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6428 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6430 #ifndef TEXT_END_LABEL
6431 #define TEXT_END_LABEL "Letext"
6432 #endif
6433 #ifndef COLD_END_LABEL
6434 #define COLD_END_LABEL "Letext_cold"
6435 #endif
6436 #ifndef BLOCK_BEGIN_LABEL
6437 #define BLOCK_BEGIN_LABEL "LBB"
6438 #endif
6439 #ifndef BLOCK_END_LABEL
6440 #define BLOCK_END_LABEL "LBE"
6441 #endif
6442 #ifndef LINE_CODE_LABEL
6443 #define LINE_CODE_LABEL "LM"
6444 #endif
6445 #ifndef SEPARATE_LINE_CODE_LABEL
6446 #define SEPARATE_LINE_CODE_LABEL "LSM"
6447 #endif
6450 /* We allow a language front-end to designate a function that is to be
6451 called to "demangle" any name before it is put into a DIE. */
6453 static const char *(*demangle_name_func) (const char *);
6455 void
6456 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6458 demangle_name_func = func;
6461 /* Test if rtl node points to a pseudo register. */
6463 static inline int
6464 is_pseudo_reg (const_rtx rtl)
6466 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6467 || (GET_CODE (rtl) == SUBREG
6468 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6471 /* Return a reference to a type, with its const and volatile qualifiers
6472 removed. */
6474 static inline tree
6475 type_main_variant (tree type)
6477 type = TYPE_MAIN_VARIANT (type);
6479 /* ??? There really should be only one main variant among any group of
6480 variants of a given type (and all of the MAIN_VARIANT values for all
6481 members of the group should point to that one type) but sometimes the C
6482 front-end messes this up for array types, so we work around that bug
6483 here. */
6484 if (TREE_CODE (type) == ARRAY_TYPE)
6485 while (type != TYPE_MAIN_VARIANT (type))
6486 type = TYPE_MAIN_VARIANT (type);
6488 return type;
6491 /* Return nonzero if the given type node represents a tagged type. */
6493 static inline int
6494 is_tagged_type (const_tree type)
6496 enum tree_code code = TREE_CODE (type);
6498 return (code == RECORD_TYPE || code == UNION_TYPE
6499 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6502 /* Convert a DIE tag into its string name. */
6504 static const char *
6505 dwarf_tag_name (unsigned int tag)
6507 switch (tag)
6509 case DW_TAG_padding:
6510 return "DW_TAG_padding";
6511 case DW_TAG_array_type:
6512 return "DW_TAG_array_type";
6513 case DW_TAG_class_type:
6514 return "DW_TAG_class_type";
6515 case DW_TAG_entry_point:
6516 return "DW_TAG_entry_point";
6517 case DW_TAG_enumeration_type:
6518 return "DW_TAG_enumeration_type";
6519 case DW_TAG_formal_parameter:
6520 return "DW_TAG_formal_parameter";
6521 case DW_TAG_imported_declaration:
6522 return "DW_TAG_imported_declaration";
6523 case DW_TAG_label:
6524 return "DW_TAG_label";
6525 case DW_TAG_lexical_block:
6526 return "DW_TAG_lexical_block";
6527 case DW_TAG_member:
6528 return "DW_TAG_member";
6529 case DW_TAG_pointer_type:
6530 return "DW_TAG_pointer_type";
6531 case DW_TAG_reference_type:
6532 return "DW_TAG_reference_type";
6533 case DW_TAG_compile_unit:
6534 return "DW_TAG_compile_unit";
6535 case DW_TAG_string_type:
6536 return "DW_TAG_string_type";
6537 case DW_TAG_structure_type:
6538 return "DW_TAG_structure_type";
6539 case DW_TAG_subroutine_type:
6540 return "DW_TAG_subroutine_type";
6541 case DW_TAG_typedef:
6542 return "DW_TAG_typedef";
6543 case DW_TAG_union_type:
6544 return "DW_TAG_union_type";
6545 case DW_TAG_unspecified_parameters:
6546 return "DW_TAG_unspecified_parameters";
6547 case DW_TAG_variant:
6548 return "DW_TAG_variant";
6549 case DW_TAG_common_block:
6550 return "DW_TAG_common_block";
6551 case DW_TAG_common_inclusion:
6552 return "DW_TAG_common_inclusion";
6553 case DW_TAG_inheritance:
6554 return "DW_TAG_inheritance";
6555 case DW_TAG_inlined_subroutine:
6556 return "DW_TAG_inlined_subroutine";
6557 case DW_TAG_module:
6558 return "DW_TAG_module";
6559 case DW_TAG_ptr_to_member_type:
6560 return "DW_TAG_ptr_to_member_type";
6561 case DW_TAG_set_type:
6562 return "DW_TAG_set_type";
6563 case DW_TAG_subrange_type:
6564 return "DW_TAG_subrange_type";
6565 case DW_TAG_with_stmt:
6566 return "DW_TAG_with_stmt";
6567 case DW_TAG_access_declaration:
6568 return "DW_TAG_access_declaration";
6569 case DW_TAG_base_type:
6570 return "DW_TAG_base_type";
6571 case DW_TAG_catch_block:
6572 return "DW_TAG_catch_block";
6573 case DW_TAG_const_type:
6574 return "DW_TAG_const_type";
6575 case DW_TAG_constant:
6576 return "DW_TAG_constant";
6577 case DW_TAG_enumerator:
6578 return "DW_TAG_enumerator";
6579 case DW_TAG_file_type:
6580 return "DW_TAG_file_type";
6581 case DW_TAG_friend:
6582 return "DW_TAG_friend";
6583 case DW_TAG_namelist:
6584 return "DW_TAG_namelist";
6585 case DW_TAG_namelist_item:
6586 return "DW_TAG_namelist_item";
6587 case DW_TAG_packed_type:
6588 return "DW_TAG_packed_type";
6589 case DW_TAG_subprogram:
6590 return "DW_TAG_subprogram";
6591 case DW_TAG_template_type_param:
6592 return "DW_TAG_template_type_param";
6593 case DW_TAG_template_value_param:
6594 return "DW_TAG_template_value_param";
6595 case DW_TAG_thrown_type:
6596 return "DW_TAG_thrown_type";
6597 case DW_TAG_try_block:
6598 return "DW_TAG_try_block";
6599 case DW_TAG_variant_part:
6600 return "DW_TAG_variant_part";
6601 case DW_TAG_variable:
6602 return "DW_TAG_variable";
6603 case DW_TAG_volatile_type:
6604 return "DW_TAG_volatile_type";
6605 case DW_TAG_dwarf_procedure:
6606 return "DW_TAG_dwarf_procedure";
6607 case DW_TAG_restrict_type:
6608 return "DW_TAG_restrict_type";
6609 case DW_TAG_interface_type:
6610 return "DW_TAG_interface_type";
6611 case DW_TAG_namespace:
6612 return "DW_TAG_namespace";
6613 case DW_TAG_imported_module:
6614 return "DW_TAG_imported_module";
6615 case DW_TAG_unspecified_type:
6616 return "DW_TAG_unspecified_type";
6617 case DW_TAG_partial_unit:
6618 return "DW_TAG_partial_unit";
6619 case DW_TAG_imported_unit:
6620 return "DW_TAG_imported_unit";
6621 case DW_TAG_condition:
6622 return "DW_TAG_condition";
6623 case DW_TAG_shared_type:
6624 return "DW_TAG_shared_type";
6625 case DW_TAG_type_unit:
6626 return "DW_TAG_type_unit";
6627 case DW_TAG_rvalue_reference_type:
6628 return "DW_TAG_rvalue_reference_type";
6629 case DW_TAG_template_alias:
6630 return "DW_TAG_template_alias";
6631 case DW_TAG_GNU_template_parameter_pack:
6632 return "DW_TAG_GNU_template_parameter_pack";
6633 case DW_TAG_GNU_formal_parameter_pack:
6634 return "DW_TAG_GNU_formal_parameter_pack";
6635 case DW_TAG_MIPS_loop:
6636 return "DW_TAG_MIPS_loop";
6637 case DW_TAG_format_label:
6638 return "DW_TAG_format_label";
6639 case DW_TAG_function_template:
6640 return "DW_TAG_function_template";
6641 case DW_TAG_class_template:
6642 return "DW_TAG_class_template";
6643 case DW_TAG_GNU_BINCL:
6644 return "DW_TAG_GNU_BINCL";
6645 case DW_TAG_GNU_EINCL:
6646 return "DW_TAG_GNU_EINCL";
6647 case DW_TAG_GNU_template_template_param:
6648 return "DW_TAG_GNU_template_template_param";
6649 default:
6650 return "DW_TAG_<unknown>";
6654 /* Convert a DWARF attribute code into its string name. */
6656 static const char *
6657 dwarf_attr_name (unsigned int attr)
6659 switch (attr)
6661 case DW_AT_sibling:
6662 return "DW_AT_sibling";
6663 case DW_AT_location:
6664 return "DW_AT_location";
6665 case DW_AT_name:
6666 return "DW_AT_name";
6667 case DW_AT_ordering:
6668 return "DW_AT_ordering";
6669 case DW_AT_subscr_data:
6670 return "DW_AT_subscr_data";
6671 case DW_AT_byte_size:
6672 return "DW_AT_byte_size";
6673 case DW_AT_bit_offset:
6674 return "DW_AT_bit_offset";
6675 case DW_AT_bit_size:
6676 return "DW_AT_bit_size";
6677 case DW_AT_element_list:
6678 return "DW_AT_element_list";
6679 case DW_AT_stmt_list:
6680 return "DW_AT_stmt_list";
6681 case DW_AT_low_pc:
6682 return "DW_AT_low_pc";
6683 case DW_AT_high_pc:
6684 return "DW_AT_high_pc";
6685 case DW_AT_language:
6686 return "DW_AT_language";
6687 case DW_AT_member:
6688 return "DW_AT_member";
6689 case DW_AT_discr:
6690 return "DW_AT_discr";
6691 case DW_AT_discr_value:
6692 return "DW_AT_discr_value";
6693 case DW_AT_visibility:
6694 return "DW_AT_visibility";
6695 case DW_AT_import:
6696 return "DW_AT_import";
6697 case DW_AT_string_length:
6698 return "DW_AT_string_length";
6699 case DW_AT_common_reference:
6700 return "DW_AT_common_reference";
6701 case DW_AT_comp_dir:
6702 return "DW_AT_comp_dir";
6703 case DW_AT_const_value:
6704 return "DW_AT_const_value";
6705 case DW_AT_containing_type:
6706 return "DW_AT_containing_type";
6707 case DW_AT_default_value:
6708 return "DW_AT_default_value";
6709 case DW_AT_inline:
6710 return "DW_AT_inline";
6711 case DW_AT_is_optional:
6712 return "DW_AT_is_optional";
6713 case DW_AT_lower_bound:
6714 return "DW_AT_lower_bound";
6715 case DW_AT_producer:
6716 return "DW_AT_producer";
6717 case DW_AT_prototyped:
6718 return "DW_AT_prototyped";
6719 case DW_AT_return_addr:
6720 return "DW_AT_return_addr";
6721 case DW_AT_start_scope:
6722 return "DW_AT_start_scope";
6723 case DW_AT_bit_stride:
6724 return "DW_AT_bit_stride";
6725 case DW_AT_upper_bound:
6726 return "DW_AT_upper_bound";
6727 case DW_AT_abstract_origin:
6728 return "DW_AT_abstract_origin";
6729 case DW_AT_accessibility:
6730 return "DW_AT_accessibility";
6731 case DW_AT_address_class:
6732 return "DW_AT_address_class";
6733 case DW_AT_artificial:
6734 return "DW_AT_artificial";
6735 case DW_AT_base_types:
6736 return "DW_AT_base_types";
6737 case DW_AT_calling_convention:
6738 return "DW_AT_calling_convention";
6739 case DW_AT_count:
6740 return "DW_AT_count";
6741 case DW_AT_data_member_location:
6742 return "DW_AT_data_member_location";
6743 case DW_AT_decl_column:
6744 return "DW_AT_decl_column";
6745 case DW_AT_decl_file:
6746 return "DW_AT_decl_file";
6747 case DW_AT_decl_line:
6748 return "DW_AT_decl_line";
6749 case DW_AT_declaration:
6750 return "DW_AT_declaration";
6751 case DW_AT_discr_list:
6752 return "DW_AT_discr_list";
6753 case DW_AT_encoding:
6754 return "DW_AT_encoding";
6755 case DW_AT_external:
6756 return "DW_AT_external";
6757 case DW_AT_explicit:
6758 return "DW_AT_explicit";
6759 case DW_AT_frame_base:
6760 return "DW_AT_frame_base";
6761 case DW_AT_friend:
6762 return "DW_AT_friend";
6763 case DW_AT_identifier_case:
6764 return "DW_AT_identifier_case";
6765 case DW_AT_macro_info:
6766 return "DW_AT_macro_info";
6767 case DW_AT_namelist_items:
6768 return "DW_AT_namelist_items";
6769 case DW_AT_priority:
6770 return "DW_AT_priority";
6771 case DW_AT_segment:
6772 return "DW_AT_segment";
6773 case DW_AT_specification:
6774 return "DW_AT_specification";
6775 case DW_AT_static_link:
6776 return "DW_AT_static_link";
6777 case DW_AT_type:
6778 return "DW_AT_type";
6779 case DW_AT_use_location:
6780 return "DW_AT_use_location";
6781 case DW_AT_variable_parameter:
6782 return "DW_AT_variable_parameter";
6783 case DW_AT_virtuality:
6784 return "DW_AT_virtuality";
6785 case DW_AT_vtable_elem_location:
6786 return "DW_AT_vtable_elem_location";
6788 case DW_AT_allocated:
6789 return "DW_AT_allocated";
6790 case DW_AT_associated:
6791 return "DW_AT_associated";
6792 case DW_AT_data_location:
6793 return "DW_AT_data_location";
6794 case DW_AT_byte_stride:
6795 return "DW_AT_byte_stride";
6796 case DW_AT_entry_pc:
6797 return "DW_AT_entry_pc";
6798 case DW_AT_use_UTF8:
6799 return "DW_AT_use_UTF8";
6800 case DW_AT_extension:
6801 return "DW_AT_extension";
6802 case DW_AT_ranges:
6803 return "DW_AT_ranges";
6804 case DW_AT_trampoline:
6805 return "DW_AT_trampoline";
6806 case DW_AT_call_column:
6807 return "DW_AT_call_column";
6808 case DW_AT_call_file:
6809 return "DW_AT_call_file";
6810 case DW_AT_call_line:
6811 return "DW_AT_call_line";
6813 case DW_AT_signature:
6814 return "DW_AT_signature";
6815 case DW_AT_main_subprogram:
6816 return "DW_AT_main_subprogram";
6817 case DW_AT_data_bit_offset:
6818 return "DW_AT_data_bit_offset";
6819 case DW_AT_const_expr:
6820 return "DW_AT_const_expr";
6821 case DW_AT_enum_class:
6822 return "DW_AT_enum_class";
6823 case DW_AT_linkage_name:
6824 return "DW_AT_linkage_name";
6826 case DW_AT_MIPS_fde:
6827 return "DW_AT_MIPS_fde";
6828 case DW_AT_MIPS_loop_begin:
6829 return "DW_AT_MIPS_loop_begin";
6830 case DW_AT_MIPS_tail_loop_begin:
6831 return "DW_AT_MIPS_tail_loop_begin";
6832 case DW_AT_MIPS_epilog_begin:
6833 return "DW_AT_MIPS_epilog_begin";
6834 #if VMS_DEBUGGING_INFO
6835 case DW_AT_HP_prologue:
6836 return "DW_AT_HP_prologue";
6837 #else
6838 case DW_AT_MIPS_loop_unroll_factor:
6839 return "DW_AT_MIPS_loop_unroll_factor";
6840 #endif
6841 case DW_AT_MIPS_software_pipeline_depth:
6842 return "DW_AT_MIPS_software_pipeline_depth";
6843 case DW_AT_MIPS_linkage_name:
6844 return "DW_AT_MIPS_linkage_name";
6845 #if VMS_DEBUGGING_INFO
6846 case DW_AT_HP_epilogue:
6847 return "DW_AT_HP_epilogue";
6848 #else
6849 case DW_AT_MIPS_stride:
6850 return "DW_AT_MIPS_stride";
6851 #endif
6852 case DW_AT_MIPS_abstract_name:
6853 return "DW_AT_MIPS_abstract_name";
6854 case DW_AT_MIPS_clone_origin:
6855 return "DW_AT_MIPS_clone_origin";
6856 case DW_AT_MIPS_has_inlines:
6857 return "DW_AT_MIPS_has_inlines";
6859 case DW_AT_sf_names:
6860 return "DW_AT_sf_names";
6861 case DW_AT_src_info:
6862 return "DW_AT_src_info";
6863 case DW_AT_mac_info:
6864 return "DW_AT_mac_info";
6865 case DW_AT_src_coords:
6866 return "DW_AT_src_coords";
6867 case DW_AT_body_begin:
6868 return "DW_AT_body_begin";
6869 case DW_AT_body_end:
6870 return "DW_AT_body_end";
6871 case DW_AT_GNU_vector:
6872 return "DW_AT_GNU_vector";
6873 case DW_AT_GNU_guarded_by:
6874 return "DW_AT_GNU_guarded_by";
6875 case DW_AT_GNU_pt_guarded_by:
6876 return "DW_AT_GNU_pt_guarded_by";
6877 case DW_AT_GNU_guarded:
6878 return "DW_AT_GNU_guarded";
6879 case DW_AT_GNU_pt_guarded:
6880 return "DW_AT_GNU_pt_guarded";
6881 case DW_AT_GNU_locks_excluded:
6882 return "DW_AT_GNU_locks_excluded";
6883 case DW_AT_GNU_exclusive_locks_required:
6884 return "DW_AT_GNU_exclusive_locks_required";
6885 case DW_AT_GNU_shared_locks_required:
6886 return "DW_AT_GNU_shared_locks_required";
6887 case DW_AT_GNU_odr_signature:
6888 return "DW_AT_GNU_odr_signature";
6889 case DW_AT_GNU_template_name:
6890 return "DW_AT_GNU_template_name";
6892 case DW_AT_VMS_rtnbeg_pd_address:
6893 return "DW_AT_VMS_rtnbeg_pd_address";
6895 default:
6896 return "DW_AT_<unknown>";
6900 /* Convert a DWARF value form code into its string name. */
6902 static const char *
6903 dwarf_form_name (unsigned int form)
6905 switch (form)
6907 case DW_FORM_addr:
6908 return "DW_FORM_addr";
6909 case DW_FORM_block2:
6910 return "DW_FORM_block2";
6911 case DW_FORM_block4:
6912 return "DW_FORM_block4";
6913 case DW_FORM_data2:
6914 return "DW_FORM_data2";
6915 case DW_FORM_data4:
6916 return "DW_FORM_data4";
6917 case DW_FORM_data8:
6918 return "DW_FORM_data8";
6919 case DW_FORM_string:
6920 return "DW_FORM_string";
6921 case DW_FORM_block:
6922 return "DW_FORM_block";
6923 case DW_FORM_block1:
6924 return "DW_FORM_block1";
6925 case DW_FORM_data1:
6926 return "DW_FORM_data1";
6927 case DW_FORM_flag:
6928 return "DW_FORM_flag";
6929 case DW_FORM_sdata:
6930 return "DW_FORM_sdata";
6931 case DW_FORM_strp:
6932 return "DW_FORM_strp";
6933 case DW_FORM_udata:
6934 return "DW_FORM_udata";
6935 case DW_FORM_ref_addr:
6936 return "DW_FORM_ref_addr";
6937 case DW_FORM_ref1:
6938 return "DW_FORM_ref1";
6939 case DW_FORM_ref2:
6940 return "DW_FORM_ref2";
6941 case DW_FORM_ref4:
6942 return "DW_FORM_ref4";
6943 case DW_FORM_ref8:
6944 return "DW_FORM_ref8";
6945 case DW_FORM_ref_udata:
6946 return "DW_FORM_ref_udata";
6947 case DW_FORM_indirect:
6948 return "DW_FORM_indirect";
6949 case DW_FORM_sec_offset:
6950 return "DW_FORM_sec_offset";
6951 case DW_FORM_exprloc:
6952 return "DW_FORM_exprloc";
6953 case DW_FORM_flag_present:
6954 return "DW_FORM_flag_present";
6955 case DW_FORM_ref_sig8:
6956 return "DW_FORM_ref_sig8";
6957 default:
6958 return "DW_FORM_<unknown>";
6962 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
6963 instance of an inlined instance of a decl which is local to an inline
6964 function, so we have to trace all of the way back through the origin chain
6965 to find out what sort of node actually served as the original seed for the
6966 given block. */
6968 static tree
6969 decl_ultimate_origin (const_tree decl)
6971 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6972 return NULL_TREE;
6974 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6975 nodes in the function to point to themselves; ignore that if
6976 we're trying to output the abstract instance of this function. */
6977 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6978 return NULL_TREE;
6980 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6981 most distant ancestor, this should never happen. */
6982 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6984 return DECL_ABSTRACT_ORIGIN (decl);
6987 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
6988 of a virtual function may refer to a base class, so we check the 'this'
6989 parameter. */
6991 static tree
6992 decl_class_context (tree decl)
6994 tree context = NULL_TREE;
6996 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6997 context = DECL_CONTEXT (decl);
6998 else
6999 context = TYPE_MAIN_VARIANT
7000 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7002 if (context && !TYPE_P (context))
7003 context = NULL_TREE;
7005 return context;
7008 /* Add an attribute/value pair to a DIE. */
7010 static inline void
7011 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7013 /* Maybe this should be an assert? */
7014 if (die == NULL)
7015 return;
7017 if (die->die_attr == NULL)
7018 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7019 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7022 static inline enum dw_val_class
7023 AT_class (dw_attr_ref a)
7025 return a->dw_attr_val.val_class;
7028 /* Add a flag value attribute to a DIE. */
7030 static inline void
7031 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7033 dw_attr_node attr;
7035 attr.dw_attr = attr_kind;
7036 attr.dw_attr_val.val_class = dw_val_class_flag;
7037 attr.dw_attr_val.v.val_flag = flag;
7038 add_dwarf_attr (die, &attr);
7041 static inline unsigned
7042 AT_flag (dw_attr_ref a)
7044 gcc_assert (a && AT_class (a) == dw_val_class_flag);
7045 return a->dw_attr_val.v.val_flag;
7048 /* Add a signed integer attribute value to a DIE. */
7050 static inline void
7051 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7053 dw_attr_node attr;
7055 attr.dw_attr = attr_kind;
7056 attr.dw_attr_val.val_class = dw_val_class_const;
7057 attr.dw_attr_val.v.val_int = int_val;
7058 add_dwarf_attr (die, &attr);
7061 static inline HOST_WIDE_INT
7062 AT_int (dw_attr_ref a)
7064 gcc_assert (a && AT_class (a) == dw_val_class_const);
7065 return a->dw_attr_val.v.val_int;
7068 /* Add an unsigned integer attribute value to a DIE. */
7070 static inline void
7071 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7072 unsigned HOST_WIDE_INT unsigned_val)
7074 dw_attr_node attr;
7076 attr.dw_attr = attr_kind;
7077 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7078 attr.dw_attr_val.v.val_unsigned = unsigned_val;
7079 add_dwarf_attr (die, &attr);
7082 static inline unsigned HOST_WIDE_INT
7083 AT_unsigned (dw_attr_ref a)
7085 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7086 return a->dw_attr_val.v.val_unsigned;
7089 /* Add an unsigned double integer attribute value to a DIE. */
7091 static inline void
7092 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7093 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7095 dw_attr_node attr;
7097 attr.dw_attr = attr_kind;
7098 attr.dw_attr_val.val_class = dw_val_class_const_double;
7099 attr.dw_attr_val.v.val_double.high = high;
7100 attr.dw_attr_val.v.val_double.low = low;
7101 add_dwarf_attr (die, &attr);
7104 /* Add a floating point attribute value to a DIE and return it. */
7106 static inline void
7107 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7108 unsigned int length, unsigned int elt_size, unsigned char *array)
7110 dw_attr_node attr;
7112 attr.dw_attr = attr_kind;
7113 attr.dw_attr_val.val_class = dw_val_class_vec;
7114 attr.dw_attr_val.v.val_vec.length = length;
7115 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7116 attr.dw_attr_val.v.val_vec.array = array;
7117 add_dwarf_attr (die, &attr);
7120 /* Add an 8-byte data attribute value to a DIE. */
7122 static inline void
7123 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7124 unsigned char data8[8])
7126 dw_attr_node attr;
7128 attr.dw_attr = attr_kind;
7129 attr.dw_attr_val.val_class = dw_val_class_data8;
7130 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7131 add_dwarf_attr (die, &attr);
7134 /* Hash and equality functions for debug_str_hash. */
7136 static hashval_t
7137 debug_str_do_hash (const void *x)
7139 return htab_hash_string (((const struct indirect_string_node *)x)->str);
7142 static int
7143 debug_str_eq (const void *x1, const void *x2)
7145 return strcmp ((((const struct indirect_string_node *)x1)->str),
7146 (const char *)x2) == 0;
7149 /* Add STR to the indirect string hash table. */
7151 static struct indirect_string_node *
7152 find_AT_string (const char *str)
7154 struct indirect_string_node *node;
7155 void **slot;
7157 if (! debug_str_hash)
7158 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7159 debug_str_eq, NULL);
7161 slot = htab_find_slot_with_hash (debug_str_hash, str,
7162 htab_hash_string (str), INSERT);
7163 if (*slot == NULL)
7165 node = ggc_alloc_cleared_indirect_string_node ();
7166 node->str = ggc_strdup (str);
7167 *slot = node;
7169 else
7170 node = (struct indirect_string_node *) *slot;
7172 node->refcount++;
7173 return node;
7176 /* Add a string attribute value to a DIE. */
7178 static inline void
7179 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7181 dw_attr_node attr;
7182 struct indirect_string_node *node;
7184 node = find_AT_string (str);
7186 attr.dw_attr = attr_kind;
7187 attr.dw_attr_val.val_class = dw_val_class_str;
7188 attr.dw_attr_val.v.val_str = node;
7189 add_dwarf_attr (die, &attr);
7192 /* Create a label for an indirect string node, ensuring it is going to
7193 be output, unless its reference count goes down to zero. */
7195 static inline void
7196 gen_label_for_indirect_string (struct indirect_string_node *node)
7198 char label[32];
7200 if (node->label)
7201 return;
7203 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7204 ++dw2_string_counter;
7205 node->label = xstrdup (label);
7208 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7209 debug string STR. */
7211 static inline rtx
7212 get_debug_string_label (const char *str)
7214 struct indirect_string_node *node = find_AT_string (str);
7216 debug_str_hash_forced = true;
7218 gen_label_for_indirect_string (node);
7220 return gen_rtx_SYMBOL_REF (Pmode, node->label);
7223 static inline const char *
7224 AT_string (dw_attr_ref a)
7226 gcc_assert (a && AT_class (a) == dw_val_class_str);
7227 return a->dw_attr_val.v.val_str->str;
7230 /* Find out whether a string should be output inline in DIE
7231 or out-of-line in .debug_str section. */
7233 static enum dwarf_form
7234 AT_string_form (dw_attr_ref a)
7236 struct indirect_string_node *node;
7237 unsigned int len;
7239 gcc_assert (a && AT_class (a) == dw_val_class_str);
7241 node = a->dw_attr_val.v.val_str;
7242 if (node->form)
7243 return node->form;
7245 len = strlen (node->str) + 1;
7247 /* If the string is shorter or equal to the size of the reference, it is
7248 always better to put it inline. */
7249 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7250 return node->form = DW_FORM_string;
7252 /* If we cannot expect the linker to merge strings in .debug_str
7253 section, only put it into .debug_str if it is worth even in this
7254 single module. */
7255 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7256 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7257 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7258 return node->form = DW_FORM_string;
7260 gen_label_for_indirect_string (node);
7262 return node->form = DW_FORM_strp;
7265 /* Add a DIE reference attribute value to a DIE. */
7267 static inline void
7268 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7270 dw_attr_node attr;
7272 attr.dw_attr = attr_kind;
7273 attr.dw_attr_val.val_class = dw_val_class_die_ref;
7274 attr.dw_attr_val.v.val_die_ref.die = targ_die;
7275 attr.dw_attr_val.v.val_die_ref.external = 0;
7276 add_dwarf_attr (die, &attr);
7279 /* Add an AT_specification attribute to a DIE, and also make the back
7280 pointer from the specification to the definition. */
7282 static inline void
7283 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7285 add_AT_die_ref (die, DW_AT_specification, targ_die);
7286 gcc_assert (!targ_die->die_definition);
7287 targ_die->die_definition = die;
7290 static inline dw_die_ref
7291 AT_ref (dw_attr_ref a)
7293 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7294 return a->dw_attr_val.v.val_die_ref.die;
7297 static inline int
7298 AT_ref_external (dw_attr_ref a)
7300 if (a && AT_class (a) == dw_val_class_die_ref)
7301 return a->dw_attr_val.v.val_die_ref.external;
7303 return 0;
7306 static inline void
7307 set_AT_ref_external (dw_attr_ref a, int i)
7309 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7310 a->dw_attr_val.v.val_die_ref.external = i;
7313 /* Add an FDE reference attribute value to a DIE. */
7315 static inline void
7316 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7318 dw_attr_node attr;
7320 attr.dw_attr = attr_kind;
7321 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7322 attr.dw_attr_val.v.val_fde_index = targ_fde;
7323 add_dwarf_attr (die, &attr);
7326 /* Add a location description attribute value to a DIE. */
7328 static inline void
7329 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7331 dw_attr_node attr;
7333 attr.dw_attr = attr_kind;
7334 attr.dw_attr_val.val_class = dw_val_class_loc;
7335 attr.dw_attr_val.v.val_loc = loc;
7336 add_dwarf_attr (die, &attr);
7339 static inline dw_loc_descr_ref
7340 AT_loc (dw_attr_ref a)
7342 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7343 return a->dw_attr_val.v.val_loc;
7346 static inline void
7347 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7349 dw_attr_node attr;
7351 attr.dw_attr = attr_kind;
7352 attr.dw_attr_val.val_class = dw_val_class_loc_list;
7353 attr.dw_attr_val.v.val_loc_list = loc_list;
7354 add_dwarf_attr (die, &attr);
7355 have_location_lists = true;
7358 static inline dw_loc_list_ref
7359 AT_loc_list (dw_attr_ref a)
7361 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7362 return a->dw_attr_val.v.val_loc_list;
7365 static inline dw_loc_list_ref *
7366 AT_loc_list_ptr (dw_attr_ref a)
7368 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7369 return &a->dw_attr_val.v.val_loc_list;
7372 /* Add an address constant attribute value to a DIE. */
7374 static inline void
7375 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7377 dw_attr_node attr;
7379 attr.dw_attr = attr_kind;
7380 attr.dw_attr_val.val_class = dw_val_class_addr;
7381 attr.dw_attr_val.v.val_addr = addr;
7382 add_dwarf_attr (die, &attr);
7385 /* Get the RTX from to an address DIE attribute. */
7387 static inline rtx
7388 AT_addr (dw_attr_ref a)
7390 gcc_assert (a && AT_class (a) == dw_val_class_addr);
7391 return a->dw_attr_val.v.val_addr;
7394 /* Add a file attribute value to a DIE. */
7396 static inline void
7397 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7398 struct dwarf_file_data *fd)
7400 dw_attr_node attr;
7402 attr.dw_attr = attr_kind;
7403 attr.dw_attr_val.val_class = dw_val_class_file;
7404 attr.dw_attr_val.v.val_file = fd;
7405 add_dwarf_attr (die, &attr);
7408 /* Get the dwarf_file_data from a file DIE attribute. */
7410 static inline struct dwarf_file_data *
7411 AT_file (dw_attr_ref a)
7413 gcc_assert (a && AT_class (a) == dw_val_class_file);
7414 return a->dw_attr_val.v.val_file;
7417 /* Add a vms delta attribute value to a DIE. */
7419 static inline void
7420 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7421 const char *lbl1, const char *lbl2)
7423 dw_attr_node attr;
7425 attr.dw_attr = attr_kind;
7426 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7427 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7428 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7429 add_dwarf_attr (die, &attr);
7432 /* Add a label identifier attribute value to a DIE. */
7434 static inline void
7435 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7437 dw_attr_node attr;
7439 attr.dw_attr = attr_kind;
7440 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7441 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7442 add_dwarf_attr (die, &attr);
7445 /* Add a section offset attribute value to a DIE, an offset into the
7446 debug_line section. */
7448 static inline void
7449 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7450 const char *label)
7452 dw_attr_node attr;
7454 attr.dw_attr = attr_kind;
7455 attr.dw_attr_val.val_class = dw_val_class_lineptr;
7456 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7457 add_dwarf_attr (die, &attr);
7460 /* Add a section offset attribute value to a DIE, an offset into the
7461 debug_macinfo section. */
7463 static inline void
7464 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7465 const char *label)
7467 dw_attr_node attr;
7469 attr.dw_attr = attr_kind;
7470 attr.dw_attr_val.val_class = dw_val_class_macptr;
7471 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7472 add_dwarf_attr (die, &attr);
7475 /* Add an offset attribute value to a DIE. */
7477 static inline void
7478 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7479 unsigned HOST_WIDE_INT offset)
7481 dw_attr_node attr;
7483 attr.dw_attr = attr_kind;
7484 attr.dw_attr_val.val_class = dw_val_class_offset;
7485 attr.dw_attr_val.v.val_offset = offset;
7486 add_dwarf_attr (die, &attr);
7489 /* Add an range_list attribute value to a DIE. */
7491 static void
7492 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7493 long unsigned int offset)
7495 dw_attr_node attr;
7497 attr.dw_attr = attr_kind;
7498 attr.dw_attr_val.val_class = dw_val_class_range_list;
7499 attr.dw_attr_val.v.val_offset = offset;
7500 add_dwarf_attr (die, &attr);
7503 /* Return the start label of a delta attribute. */
7505 static inline const char *
7506 AT_vms_delta1 (dw_attr_ref a)
7508 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7509 return a->dw_attr_val.v.val_vms_delta.lbl1;
7512 /* Return the end label of a delta attribute. */
7514 static inline const char *
7515 AT_vms_delta2 (dw_attr_ref a)
7517 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7518 return a->dw_attr_val.v.val_vms_delta.lbl2;
7521 static inline const char *
7522 AT_lbl (dw_attr_ref a)
7524 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7525 || AT_class (a) == dw_val_class_lineptr
7526 || AT_class (a) == dw_val_class_macptr));
7527 return a->dw_attr_val.v.val_lbl_id;
7530 /* Get the attribute of type attr_kind. */
7532 static dw_attr_ref
7533 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7535 dw_attr_ref a;
7536 unsigned ix;
7537 dw_die_ref spec = NULL;
7539 if (! die)
7540 return NULL;
7542 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7543 if (a->dw_attr == attr_kind)
7544 return a;
7545 else if (a->dw_attr == DW_AT_specification
7546 || a->dw_attr == DW_AT_abstract_origin)
7547 spec = AT_ref (a);
7549 if (spec)
7550 return get_AT (spec, attr_kind);
7552 return NULL;
7555 /* Return the "low pc" attribute value, typically associated with a subprogram
7556 DIE. Return null if the "low pc" attribute is either not present, or if it
7557 cannot be represented as an assembler label identifier. */
7559 static inline const char *
7560 get_AT_low_pc (dw_die_ref die)
7562 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7564 return a ? AT_lbl (a) : NULL;
7567 /* Return the "high pc" attribute value, typically associated with a subprogram
7568 DIE. Return null if the "high pc" attribute is either not present, or if it
7569 cannot be represented as an assembler label identifier. */
7571 static inline const char *
7572 get_AT_hi_pc (dw_die_ref die)
7574 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7576 return a ? AT_lbl (a) : NULL;
7579 /* Return the value of the string attribute designated by ATTR_KIND, or
7580 NULL if it is not present. */
7582 static inline const char *
7583 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7585 dw_attr_ref a = get_AT (die, attr_kind);
7587 return a ? AT_string (a) : NULL;
7590 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7591 if it is not present. */
7593 static inline int
7594 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7596 dw_attr_ref a = get_AT (die, attr_kind);
7598 return a ? AT_flag (a) : 0;
7601 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7602 if it is not present. */
7604 static inline unsigned
7605 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7607 dw_attr_ref a = get_AT (die, attr_kind);
7609 return a ? AT_unsigned (a) : 0;
7612 static inline dw_die_ref
7613 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7615 dw_attr_ref a = get_AT (die, attr_kind);
7617 return a ? AT_ref (a) : NULL;
7620 static inline struct dwarf_file_data *
7621 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7623 dw_attr_ref a = get_AT (die, attr_kind);
7625 return a ? AT_file (a) : NULL;
7628 /* Return TRUE if the language is C++. */
7630 static inline bool
7631 is_cxx (void)
7633 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7635 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7638 /* Return TRUE if the language is Fortran. */
7640 static inline bool
7641 is_fortran (void)
7643 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7645 return (lang == DW_LANG_Fortran77
7646 || lang == DW_LANG_Fortran90
7647 || lang == DW_LANG_Fortran95);
7650 /* Return TRUE if the language is Ada. */
7652 static inline bool
7653 is_ada (void)
7655 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7657 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7660 /* Remove the specified attribute if present. */
7662 static void
7663 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7665 dw_attr_ref a;
7666 unsigned ix;
7668 if (! die)
7669 return;
7671 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7672 if (a->dw_attr == attr_kind)
7674 if (AT_class (a) == dw_val_class_str)
7675 if (a->dw_attr_val.v.val_str->refcount)
7676 a->dw_attr_val.v.val_str->refcount--;
7678 /* VEC_ordered_remove should help reduce the number of abbrevs
7679 that are needed. */
7680 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7681 return;
7685 /* Remove CHILD from its parent. PREV must have the property that
7686 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
7688 static void
7689 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7691 gcc_assert (child->die_parent == prev->die_parent);
7692 gcc_assert (prev->die_sib == child);
7693 if (prev == child)
7695 gcc_assert (child->die_parent->die_child == child);
7696 prev = NULL;
7698 else
7699 prev->die_sib = child->die_sib;
7700 if (child->die_parent->die_child == child)
7701 child->die_parent->die_child = prev;
7704 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
7705 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
7707 static void
7708 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7710 dw_die_ref parent = old_child->die_parent;
7712 gcc_assert (parent == prev->die_parent);
7713 gcc_assert (prev->die_sib == old_child);
7715 new_child->die_parent = parent;
7716 if (prev == old_child)
7718 gcc_assert (parent->die_child == old_child);
7719 new_child->die_sib = new_child;
7721 else
7723 prev->die_sib = new_child;
7724 new_child->die_sib = old_child->die_sib;
7726 if (old_child->die_parent->die_child == old_child)
7727 old_child->die_parent->die_child = new_child;
7730 /* Move all children from OLD_PARENT to NEW_PARENT. */
7732 static void
7733 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7735 dw_die_ref c;
7736 new_parent->die_child = old_parent->die_child;
7737 old_parent->die_child = NULL;
7738 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7741 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
7742 matches TAG. */
7744 static void
7745 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7747 dw_die_ref c;
7749 c = die->die_child;
7750 if (c) do {
7751 dw_die_ref prev = c;
7752 c = c->die_sib;
7753 while (c->die_tag == tag)
7755 remove_child_with_prev (c, prev);
7756 /* Might have removed every child. */
7757 if (c == c->die_sib)
7758 return;
7759 c = c->die_sib;
7761 } while (c != die->die_child);
7764 /* Add a CHILD_DIE as the last child of DIE. */
7766 static void
7767 add_child_die (dw_die_ref die, dw_die_ref child_die)
7769 /* FIXME this should probably be an assert. */
7770 if (! die || ! child_die)
7771 return;
7772 gcc_assert (die != child_die);
7774 child_die->die_parent = die;
7775 if (die->die_child)
7777 child_die->die_sib = die->die_child->die_sib;
7778 die->die_child->die_sib = child_die;
7780 else
7781 child_die->die_sib = child_die;
7782 die->die_child = child_die;
7785 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7786 is the specification, to the end of PARENT's list of children.
7787 This is done by removing and re-adding it. */
7789 static void
7790 splice_child_die (dw_die_ref parent, dw_die_ref child)
7792 dw_die_ref p;
7794 /* We want the declaration DIE from inside the class, not the
7795 specification DIE at toplevel. */
7796 if (child->die_parent != parent)
7798 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7800 if (tmp)
7801 child = tmp;
7804 gcc_assert (child->die_parent == parent
7805 || (child->die_parent
7806 == get_AT_ref (parent, DW_AT_specification)));
7808 for (p = child->die_parent->die_child; ; p = p->die_sib)
7809 if (p->die_sib == child)
7811 remove_child_with_prev (child, p);
7812 break;
7815 add_child_die (parent, child);
7818 /* Return a pointer to a newly created DIE node. */
7820 static inline dw_die_ref
7821 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7823 dw_die_ref die = ggc_alloc_cleared_die_node ();
7825 die->die_tag = tag_value;
7827 if (parent_die != NULL)
7828 add_child_die (parent_die, die);
7829 else
7831 limbo_die_node *limbo_node;
7833 limbo_node = ggc_alloc_cleared_limbo_die_node ();
7834 limbo_node->die = die;
7835 limbo_node->created_for = t;
7836 limbo_node->next = limbo_die_list;
7837 limbo_die_list = limbo_node;
7840 return die;
7843 /* Return the DIE associated with the given type specifier. */
7845 static inline dw_die_ref
7846 lookup_type_die (tree type)
7848 return TYPE_SYMTAB_DIE (type);
7851 /* Equate a DIE to a given type specifier. */
7853 static inline void
7854 equate_type_number_to_die (tree type, dw_die_ref type_die)
7856 TYPE_SYMTAB_DIE (type) = type_die;
7859 /* Returns a hash value for X (which really is a die_struct). */
7861 static hashval_t
7862 decl_die_table_hash (const void *x)
7864 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7867 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
7869 static int
7870 decl_die_table_eq (const void *x, const void *y)
7872 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7875 /* Return the DIE associated with a given declaration. */
7877 static inline dw_die_ref
7878 lookup_decl_die (tree decl)
7880 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7883 /* Returns a hash value for X (which really is a var_loc_list). */
7885 static hashval_t
7886 decl_loc_table_hash (const void *x)
7888 return (hashval_t) ((const var_loc_list *) x)->decl_id;
7891 /* Return nonzero if decl_id of var_loc_list X is the same as
7892 UID of decl *Y. */
7894 static int
7895 decl_loc_table_eq (const void *x, const void *y)
7897 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7900 /* Return the var_loc list associated with a given declaration. */
7902 static inline var_loc_list *
7903 lookup_decl_loc (const_tree decl)
7905 if (!decl_loc_table)
7906 return NULL;
7907 return (var_loc_list *)
7908 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7911 /* Equate a DIE to a particular declaration. */
7913 static void
7914 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7916 unsigned int decl_id = DECL_UID (decl);
7917 void **slot;
7919 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7920 *slot = decl_die;
7921 decl_die->decl_id = decl_id;
7924 /* Return how many bits covers PIECE EXPR_LIST. */
7926 static int
7927 decl_piece_bitsize (rtx piece)
7929 int ret = (int) GET_MODE (piece);
7930 if (ret)
7931 return ret;
7932 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
7933 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
7934 return INTVAL (XEXP (XEXP (piece, 0), 0));
7937 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
7939 static rtx *
7940 decl_piece_varloc_ptr (rtx piece)
7942 if ((int) GET_MODE (piece))
7943 return &XEXP (piece, 0);
7944 else
7945 return &XEXP (XEXP (piece, 0), 1);
7948 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
7949 Next is the chain of following piece nodes. */
7951 static rtx
7952 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
7954 if (bitsize <= (int) MAX_MACHINE_MODE)
7955 return alloc_EXPR_LIST (bitsize, loc_note, next);
7956 else
7957 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
7958 GEN_INT (bitsize),
7959 loc_note), next);
7962 /* Return rtx that should be stored into loc field for
7963 LOC_NOTE and BITPOS/BITSIZE. */
7965 static rtx
7966 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
7967 HOST_WIDE_INT bitsize)
7969 if (bitsize != -1)
7971 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
7972 if (bitpos != 0)
7973 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
7975 return loc_note;
7978 /* This function either modifies location piece list *DEST in
7979 place (if SRC and INNER is NULL), or copies location piece list
7980 *SRC to *DEST while modifying it. Location BITPOS is modified
7981 to contain LOC_NOTE, any pieces overlapping it are removed resp.
7982 not copied and if needed some padding around it is added.
7983 When modifying in place, DEST should point to EXPR_LIST where
7984 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
7985 to the start of the whole list and INNER points to the EXPR_LIST
7986 where earlier pieces cover PIECE_BITPOS bits. */
7988 static void
7989 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
7990 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
7991 HOST_WIDE_INT bitsize, rtx loc_note)
7993 int diff;
7994 bool copy = inner != NULL;
7996 if (copy)
7998 /* First copy all nodes preceeding the current bitpos. */
7999 while (src != inner)
8001 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8002 decl_piece_bitsize (*src), NULL_RTX);
8003 dest = &XEXP (*dest, 1);
8004 src = &XEXP (*src, 1);
8007 /* Add padding if needed. */
8008 if (bitpos != piece_bitpos)
8010 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8011 copy ? NULL_RTX : *dest);
8012 dest = &XEXP (*dest, 1);
8014 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8016 gcc_assert (!copy);
8017 /* A piece with correct bitpos and bitsize already exist,
8018 just update the location for it and return. */
8019 *decl_piece_varloc_ptr (*dest) = loc_note;
8020 return;
8022 /* Add the piece that changed. */
8023 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8024 dest = &XEXP (*dest, 1);
8025 /* Skip over pieces that overlap it. */
8026 diff = bitpos - piece_bitpos + bitsize;
8027 if (!copy)
8028 src = dest;
8029 while (diff > 0 && *src)
8031 rtx piece = *src;
8032 diff -= decl_piece_bitsize (piece);
8033 if (copy)
8034 src = &XEXP (piece, 1);
8035 else
8037 *src = XEXP (piece, 1);
8038 free_EXPR_LIST_node (piece);
8041 /* Add padding if needed. */
8042 if (diff < 0 && *src)
8044 if (!copy)
8045 dest = src;
8046 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8047 dest = &XEXP (*dest, 1);
8049 if (!copy)
8050 return;
8051 /* Finally copy all nodes following it. */
8052 while (*src)
8054 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8055 decl_piece_bitsize (*src), NULL_RTX);
8056 dest = &XEXP (*dest, 1);
8057 src = &XEXP (*src, 1);
8061 /* Add a variable location node to the linked list for DECL. */
8063 static struct var_loc_node *
8064 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8066 unsigned int decl_id;
8067 var_loc_list *temp;
8068 void **slot;
8069 struct var_loc_node *loc = NULL;
8070 HOST_WIDE_INT bitsize = -1, bitpos = -1;
8072 if (DECL_DEBUG_EXPR_IS_FROM (decl))
8074 tree realdecl = DECL_DEBUG_EXPR (decl);
8075 if (realdecl && handled_component_p (realdecl))
8077 HOST_WIDE_INT maxsize;
8078 tree innerdecl;
8079 innerdecl
8080 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8081 if (!DECL_P (innerdecl)
8082 || DECL_IGNORED_P (innerdecl)
8083 || TREE_STATIC (innerdecl)
8084 || bitsize <= 0
8085 || bitpos + bitsize > 256
8086 || bitsize != maxsize)
8087 return NULL;
8088 decl = innerdecl;
8092 decl_id = DECL_UID (decl);
8093 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8094 if (*slot == NULL)
8096 temp = ggc_alloc_cleared_var_loc_list ();
8097 temp->decl_id = decl_id;
8098 *slot = temp;
8100 else
8101 temp = (var_loc_list *) *slot;
8103 if (temp->last)
8105 struct var_loc_node *last = temp->last, *unused = NULL;
8106 rtx *piece_loc = NULL, last_loc_note;
8107 int piece_bitpos = 0;
8108 if (last->next)
8110 last = last->next;
8111 gcc_assert (last->next == NULL);
8113 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8115 piece_loc = &last->loc;
8118 int cur_bitsize = decl_piece_bitsize (*piece_loc);
8119 if (piece_bitpos + cur_bitsize > bitpos)
8120 break;
8121 piece_bitpos += cur_bitsize;
8122 piece_loc = &XEXP (*piece_loc, 1);
8124 while (*piece_loc);
8126 /* TEMP->LAST here is either pointer to the last but one or
8127 last element in the chained list, LAST is pointer to the
8128 last element. */
8129 if (label && strcmp (last->label, label) == 0)
8131 /* For SRA optimized variables if there weren't any real
8132 insns since last note, just modify the last node. */
8133 if (piece_loc != NULL)
8135 adjust_piece_list (piece_loc, NULL, NULL,
8136 bitpos, piece_bitpos, bitsize, loc_note);
8137 return NULL;
8139 /* If the last note doesn't cover any instructions, remove it. */
8140 if (temp->last != last)
8142 temp->last->next = NULL;
8143 unused = last;
8144 last = temp->last;
8145 gcc_assert (strcmp (last->label, label) != 0);
8147 else
8149 gcc_assert (temp->first == temp->last);
8150 memset (temp->last, '\0', sizeof (*temp->last));
8151 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8152 return temp->last;
8155 if (bitsize == -1 && NOTE_P (last->loc))
8156 last_loc_note = last->loc;
8157 else if (piece_loc != NULL
8158 && *piece_loc != NULL_RTX
8159 && piece_bitpos == bitpos
8160 && decl_piece_bitsize (*piece_loc) == bitsize)
8161 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8162 else
8163 last_loc_note = NULL_RTX;
8164 /* If the current location is the same as the end of the list,
8165 and either both or neither of the locations is uninitialized,
8166 we have nothing to do. */
8167 if (last_loc_note == NULL_RTX
8168 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8169 NOTE_VAR_LOCATION_LOC (loc_note)))
8170 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8171 != NOTE_VAR_LOCATION_STATUS (loc_note))
8172 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8173 == VAR_INIT_STATUS_UNINITIALIZED)
8174 || (NOTE_VAR_LOCATION_STATUS (loc_note)
8175 == VAR_INIT_STATUS_UNINITIALIZED))))
8177 /* Add LOC to the end of list and update LAST. If the last
8178 element of the list has been removed above, reuse its
8179 memory for the new node, otherwise allocate a new one. */
8180 if (unused)
8182 loc = unused;
8183 memset (loc, '\0', sizeof (*loc));
8185 else
8186 loc = ggc_alloc_cleared_var_loc_node ();
8187 if (bitsize == -1 || piece_loc == NULL)
8188 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8189 else
8190 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8191 bitpos, piece_bitpos, bitsize, loc_note);
8192 last->next = loc;
8193 /* Ensure TEMP->LAST will point either to the new last but one
8194 element of the chain, or to the last element in it. */
8195 if (last != temp->last)
8196 temp->last = last;
8198 else if (unused)
8199 ggc_free (unused);
8201 else
8203 loc = ggc_alloc_cleared_var_loc_node ();
8204 temp->first = loc;
8205 temp->last = loc;
8206 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8208 return loc;
8211 /* Keep track of the number of spaces used to indent the
8212 output of the debugging routines that print the structure of
8213 the DIE internal representation. */
8214 static int print_indent;
8216 /* Indent the line the number of spaces given by print_indent. */
8218 static inline void
8219 print_spaces (FILE *outfile)
8221 fprintf (outfile, "%*s", print_indent, "");
8224 /* Print a type signature in hex. */
8226 static inline void
8227 print_signature (FILE *outfile, char *sig)
8229 int i;
8231 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8232 fprintf (outfile, "%02x", sig[i] & 0xff);
8235 /* Print the information associated with a given DIE, and its children.
8236 This routine is a debugging aid only. */
8238 static void
8239 print_die (dw_die_ref die, FILE *outfile)
8241 dw_attr_ref a;
8242 dw_die_ref c;
8243 unsigned ix;
8245 print_spaces (outfile);
8246 fprintf (outfile, "DIE %4ld: %s\n",
8247 die->die_offset, dwarf_tag_name (die->die_tag));
8248 print_spaces (outfile);
8249 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
8250 fprintf (outfile, " offset: %ld\n", die->die_offset);
8251 if (dwarf_version >= 4 && die->die_id.die_type_node)
8253 print_spaces (outfile);
8254 fprintf (outfile, " signature: ");
8255 print_signature (outfile, die->die_id.die_type_node->signature);
8256 fprintf (outfile, "\n");
8259 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8261 print_spaces (outfile);
8262 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
8264 switch (AT_class (a))
8266 case dw_val_class_addr:
8267 fprintf (outfile, "address");
8268 break;
8269 case dw_val_class_offset:
8270 fprintf (outfile, "offset");
8271 break;
8272 case dw_val_class_loc:
8273 fprintf (outfile, "location descriptor");
8274 break;
8275 case dw_val_class_loc_list:
8276 fprintf (outfile, "location list -> label:%s",
8277 AT_loc_list (a)->ll_symbol);
8278 break;
8279 case dw_val_class_range_list:
8280 fprintf (outfile, "range list");
8281 break;
8282 case dw_val_class_const:
8283 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8284 break;
8285 case dw_val_class_unsigned_const:
8286 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8287 break;
8288 case dw_val_class_const_double:
8289 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8290 HOST_WIDE_INT_PRINT_UNSIGNED")",
8291 a->dw_attr_val.v.val_double.high,
8292 a->dw_attr_val.v.val_double.low);
8293 break;
8294 case dw_val_class_vec:
8295 fprintf (outfile, "floating-point or vector constant");
8296 break;
8297 case dw_val_class_flag:
8298 fprintf (outfile, "%u", AT_flag (a));
8299 break;
8300 case dw_val_class_die_ref:
8301 if (AT_ref (a) != NULL)
8303 if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8305 fprintf (outfile, "die -> signature: ");
8306 print_signature (outfile,
8307 AT_ref (a)->die_id.die_type_node->signature);
8309 else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8310 fprintf (outfile, "die -> label: %s",
8311 AT_ref (a)->die_id.die_symbol);
8312 else
8313 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8315 else
8316 fprintf (outfile, "die -> <null>");
8317 break;
8318 case dw_val_class_vms_delta:
8319 fprintf (outfile, "delta: @slotcount(%s-%s)",
8320 AT_vms_delta2 (a), AT_vms_delta1 (a));
8321 break;
8322 case dw_val_class_lbl_id:
8323 case dw_val_class_lineptr:
8324 case dw_val_class_macptr:
8325 fprintf (outfile, "label: %s", AT_lbl (a));
8326 break;
8327 case dw_val_class_str:
8328 if (AT_string (a) != NULL)
8329 fprintf (outfile, "\"%s\"", AT_string (a));
8330 else
8331 fprintf (outfile, "<null>");
8332 break;
8333 case dw_val_class_file:
8334 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8335 AT_file (a)->emitted_number);
8336 break;
8337 case dw_val_class_data8:
8339 int i;
8341 for (i = 0; i < 8; i++)
8342 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8343 break;
8345 default:
8346 break;
8349 fprintf (outfile, "\n");
8352 if (die->die_child != NULL)
8354 print_indent += 4;
8355 FOR_EACH_CHILD (die, c, print_die (c, outfile));
8356 print_indent -= 4;
8358 if (print_indent == 0)
8359 fprintf (outfile, "\n");
8362 /* Print the contents of the source code line number correspondence table.
8363 This routine is a debugging aid only. */
8365 static void
8366 print_dwarf_line_table (FILE *outfile)
8368 unsigned i;
8369 dw_line_info_ref line_info;
8371 fprintf (outfile, "\n\nDWARF source line information\n");
8372 for (i = 1; i < line_info_table_in_use; i++)
8374 line_info = &line_info_table[i];
8375 fprintf (outfile, "%5d: %4ld %6ld\n", i,
8376 line_info->dw_file_num,
8377 line_info->dw_line_num);
8380 fprintf (outfile, "\n\n");
8383 /* Print the information collected for a given DIE. */
8385 DEBUG_FUNCTION void
8386 debug_dwarf_die (dw_die_ref die)
8388 print_die (die, stderr);
8391 /* Print all DWARF information collected for the compilation unit.
8392 This routine is a debugging aid only. */
8394 DEBUG_FUNCTION void
8395 debug_dwarf (void)
8397 print_indent = 0;
8398 print_die (comp_unit_die, stderr);
8399 if (! DWARF2_ASM_LINE_DEBUG_INFO)
8400 print_dwarf_line_table (stderr);
8403 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
8404 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
8405 DIE that marks the start of the DIEs for this include file. */
8407 static dw_die_ref
8408 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8410 const char *filename = get_AT_string (bincl_die, DW_AT_name);
8411 dw_die_ref new_unit = gen_compile_unit_die (filename);
8413 new_unit->die_sib = old_unit;
8414 return new_unit;
8417 /* Close an include-file CU and reopen the enclosing one. */
8419 static dw_die_ref
8420 pop_compile_unit (dw_die_ref old_unit)
8422 dw_die_ref new_unit = old_unit->die_sib;
8424 old_unit->die_sib = NULL;
8425 return new_unit;
8428 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8429 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8431 /* Calculate the checksum of a location expression. */
8433 static inline void
8434 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8436 int tem;
8438 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8439 CHECKSUM (tem);
8440 CHECKSUM (loc->dw_loc_oprnd1);
8441 CHECKSUM (loc->dw_loc_oprnd2);
8444 /* Calculate the checksum of an attribute. */
8446 static void
8447 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8449 dw_loc_descr_ref loc;
8450 rtx r;
8452 CHECKSUM (at->dw_attr);
8454 /* We don't care that this was compiled with a different compiler
8455 snapshot; if the output is the same, that's what matters. */
8456 if (at->dw_attr == DW_AT_producer)
8457 return;
8459 switch (AT_class (at))
8461 case dw_val_class_const:
8462 CHECKSUM (at->dw_attr_val.v.val_int);
8463 break;
8464 case dw_val_class_unsigned_const:
8465 CHECKSUM (at->dw_attr_val.v.val_unsigned);
8466 break;
8467 case dw_val_class_const_double:
8468 CHECKSUM (at->dw_attr_val.v.val_double);
8469 break;
8470 case dw_val_class_vec:
8471 CHECKSUM (at->dw_attr_val.v.val_vec);
8472 break;
8473 case dw_val_class_flag:
8474 CHECKSUM (at->dw_attr_val.v.val_flag);
8475 break;
8476 case dw_val_class_str:
8477 CHECKSUM_STRING (AT_string (at));
8478 break;
8480 case dw_val_class_addr:
8481 r = AT_addr (at);
8482 gcc_assert (GET_CODE (r) == SYMBOL_REF);
8483 CHECKSUM_STRING (XSTR (r, 0));
8484 break;
8486 case dw_val_class_offset:
8487 CHECKSUM (at->dw_attr_val.v.val_offset);
8488 break;
8490 case dw_val_class_loc:
8491 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8492 loc_checksum (loc, ctx);
8493 break;
8495 case dw_val_class_die_ref:
8496 die_checksum (AT_ref (at), ctx, mark);
8497 break;
8499 case dw_val_class_fde_ref:
8500 case dw_val_class_vms_delta:
8501 case dw_val_class_lbl_id:
8502 case dw_val_class_lineptr:
8503 case dw_val_class_macptr:
8504 break;
8506 case dw_val_class_file:
8507 CHECKSUM_STRING (AT_file (at)->filename);
8508 break;
8510 case dw_val_class_data8:
8511 CHECKSUM (at->dw_attr_val.v.val_data8);
8512 break;
8514 default:
8515 break;
8519 /* Calculate the checksum of a DIE. */
8521 static void
8522 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8524 dw_die_ref c;
8525 dw_attr_ref a;
8526 unsigned ix;
8528 /* To avoid infinite recursion. */
8529 if (die->die_mark)
8531 CHECKSUM (die->die_mark);
8532 return;
8534 die->die_mark = ++(*mark);
8536 CHECKSUM (die->die_tag);
8538 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8539 attr_checksum (a, ctx, mark);
8541 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8544 #undef CHECKSUM
8545 #undef CHECKSUM_STRING
8547 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
8548 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8549 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8550 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8551 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8552 #define CHECKSUM_ATTR(FOO) \
8553 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8555 /* Calculate the checksum of a number in signed LEB128 format. */
8557 static void
8558 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8560 unsigned char byte;
8561 bool more;
8563 while (1)
8565 byte = (value & 0x7f);
8566 value >>= 7;
8567 more = !((value == 0 && (byte & 0x40) == 0)
8568 || (value == -1 && (byte & 0x40) != 0));
8569 if (more)
8570 byte |= 0x80;
8571 CHECKSUM (byte);
8572 if (!more)
8573 break;
8577 /* Calculate the checksum of a number in unsigned LEB128 format. */
8579 static void
8580 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8582 while (1)
8584 unsigned char byte = (value & 0x7f);
8585 value >>= 7;
8586 if (value != 0)
8587 /* More bytes to follow. */
8588 byte |= 0x80;
8589 CHECKSUM (byte);
8590 if (value == 0)
8591 break;
8595 /* Checksum the context of the DIE. This adds the names of any
8596 surrounding namespaces or structures to the checksum. */
8598 static void
8599 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8601 const char *name;
8602 dw_die_ref spec;
8603 int tag = die->die_tag;
8605 if (tag != DW_TAG_namespace
8606 && tag != DW_TAG_structure_type
8607 && tag != DW_TAG_class_type)
8608 return;
8610 name = get_AT_string (die, DW_AT_name);
8612 spec = get_AT_ref (die, DW_AT_specification);
8613 if (spec != NULL)
8614 die = spec;
8616 if (die->die_parent != NULL)
8617 checksum_die_context (die->die_parent, ctx);
8619 CHECKSUM_ULEB128 ('C');
8620 CHECKSUM_ULEB128 (tag);
8621 if (name != NULL)
8622 CHECKSUM_STRING (name);
8625 /* Calculate the checksum of a location expression. */
8627 static inline void
8628 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8630 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8631 were emitted as a DW_FORM_sdata instead of a location expression. */
8632 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8634 CHECKSUM_ULEB128 (DW_FORM_sdata);
8635 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8636 return;
8639 /* Otherwise, just checksum the raw location expression. */
8640 while (loc != NULL)
8642 CHECKSUM_ULEB128 (loc->dw_loc_opc);
8643 CHECKSUM (loc->dw_loc_oprnd1);
8644 CHECKSUM (loc->dw_loc_oprnd2);
8645 loc = loc->dw_loc_next;
8649 /* Calculate the checksum of an attribute. */
8651 static void
8652 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8653 struct md5_ctx *ctx, int *mark)
8655 dw_loc_descr_ref loc;
8656 rtx r;
8658 if (AT_class (at) == dw_val_class_die_ref)
8660 dw_die_ref target_die = AT_ref (at);
8662 /* For pointer and reference types, we checksum only the (qualified)
8663 name of the target type (if there is a name). For friend entries,
8664 we checksum only the (qualified) name of the target type or function.
8665 This allows the checksum to remain the same whether the target type
8666 is complete or not. */
8667 if ((at->dw_attr == DW_AT_type
8668 && (tag == DW_TAG_pointer_type
8669 || tag == DW_TAG_reference_type
8670 || tag == DW_TAG_rvalue_reference_type
8671 || tag == DW_TAG_ptr_to_member_type))
8672 || (at->dw_attr == DW_AT_friend
8673 && tag == DW_TAG_friend))
8675 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8677 if (name_attr != NULL)
8679 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8681 if (decl == NULL)
8682 decl = target_die;
8683 CHECKSUM_ULEB128 ('N');
8684 CHECKSUM_ULEB128 (at->dw_attr);
8685 if (decl->die_parent != NULL)
8686 checksum_die_context (decl->die_parent, ctx);
8687 CHECKSUM_ULEB128 ('E');
8688 CHECKSUM_STRING (AT_string (name_attr));
8689 return;
8693 /* For all other references to another DIE, we check to see if the
8694 target DIE has already been visited. If it has, we emit a
8695 backward reference; if not, we descend recursively. */
8696 if (target_die->die_mark > 0)
8698 CHECKSUM_ULEB128 ('R');
8699 CHECKSUM_ULEB128 (at->dw_attr);
8700 CHECKSUM_ULEB128 (target_die->die_mark);
8702 else
8704 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8706 if (decl == NULL)
8707 decl = target_die;
8708 target_die->die_mark = ++(*mark);
8709 CHECKSUM_ULEB128 ('T');
8710 CHECKSUM_ULEB128 (at->dw_attr);
8711 if (decl->die_parent != NULL)
8712 checksum_die_context (decl->die_parent, ctx);
8713 die_checksum_ordered (target_die, ctx, mark);
8715 return;
8718 CHECKSUM_ULEB128 ('A');
8719 CHECKSUM_ULEB128 (at->dw_attr);
8721 switch (AT_class (at))
8723 case dw_val_class_const:
8724 CHECKSUM_ULEB128 (DW_FORM_sdata);
8725 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8726 break;
8728 case dw_val_class_unsigned_const:
8729 CHECKSUM_ULEB128 (DW_FORM_sdata);
8730 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8731 break;
8733 case dw_val_class_const_double:
8734 CHECKSUM_ULEB128 (DW_FORM_block);
8735 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8736 CHECKSUM (at->dw_attr_val.v.val_double);
8737 break;
8739 case dw_val_class_vec:
8740 CHECKSUM_ULEB128 (DW_FORM_block);
8741 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8742 CHECKSUM (at->dw_attr_val.v.val_vec);
8743 break;
8745 case dw_val_class_flag:
8746 CHECKSUM_ULEB128 (DW_FORM_flag);
8747 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8748 break;
8750 case dw_val_class_str:
8751 CHECKSUM_ULEB128 (DW_FORM_string);
8752 CHECKSUM_STRING (AT_string (at));
8753 break;
8755 case dw_val_class_addr:
8756 r = AT_addr (at);
8757 gcc_assert (GET_CODE (r) == SYMBOL_REF);
8758 CHECKSUM_ULEB128 (DW_FORM_string);
8759 CHECKSUM_STRING (XSTR (r, 0));
8760 break;
8762 case dw_val_class_offset:
8763 CHECKSUM_ULEB128 (DW_FORM_sdata);
8764 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8765 break;
8767 case dw_val_class_loc:
8768 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8769 loc_checksum_ordered (loc, ctx);
8770 break;
8772 case dw_val_class_fde_ref:
8773 case dw_val_class_lbl_id:
8774 case dw_val_class_lineptr:
8775 case dw_val_class_macptr:
8776 break;
8778 case dw_val_class_file:
8779 CHECKSUM_ULEB128 (DW_FORM_string);
8780 CHECKSUM_STRING (AT_file (at)->filename);
8781 break;
8783 case dw_val_class_data8:
8784 CHECKSUM (at->dw_attr_val.v.val_data8);
8785 break;
8787 default:
8788 break;
8792 struct checksum_attributes
8794 dw_attr_ref at_name;
8795 dw_attr_ref at_type;
8796 dw_attr_ref at_friend;
8797 dw_attr_ref at_accessibility;
8798 dw_attr_ref at_address_class;
8799 dw_attr_ref at_allocated;
8800 dw_attr_ref at_artificial;
8801 dw_attr_ref at_associated;
8802 dw_attr_ref at_binary_scale;
8803 dw_attr_ref at_bit_offset;
8804 dw_attr_ref at_bit_size;
8805 dw_attr_ref at_bit_stride;
8806 dw_attr_ref at_byte_size;
8807 dw_attr_ref at_byte_stride;
8808 dw_attr_ref at_const_value;
8809 dw_attr_ref at_containing_type;
8810 dw_attr_ref at_count;
8811 dw_attr_ref at_data_location;
8812 dw_attr_ref at_data_member_location;
8813 dw_attr_ref at_decimal_scale;
8814 dw_attr_ref at_decimal_sign;
8815 dw_attr_ref at_default_value;
8816 dw_attr_ref at_digit_count;
8817 dw_attr_ref at_discr;
8818 dw_attr_ref at_discr_list;
8819 dw_attr_ref at_discr_value;
8820 dw_attr_ref at_encoding;
8821 dw_attr_ref at_endianity;
8822 dw_attr_ref at_explicit;
8823 dw_attr_ref at_is_optional;
8824 dw_attr_ref at_location;
8825 dw_attr_ref at_lower_bound;
8826 dw_attr_ref at_mutable;
8827 dw_attr_ref at_ordering;
8828 dw_attr_ref at_picture_string;
8829 dw_attr_ref at_prototyped;
8830 dw_attr_ref at_small;
8831 dw_attr_ref at_segment;
8832 dw_attr_ref at_string_length;
8833 dw_attr_ref at_threads_scaled;
8834 dw_attr_ref at_upper_bound;
8835 dw_attr_ref at_use_location;
8836 dw_attr_ref at_use_UTF8;
8837 dw_attr_ref at_variable_parameter;
8838 dw_attr_ref at_virtuality;
8839 dw_attr_ref at_visibility;
8840 dw_attr_ref at_vtable_elem_location;
8843 /* Collect the attributes that we will want to use for the checksum. */
8845 static void
8846 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8848 dw_attr_ref a;
8849 unsigned ix;
8851 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8853 switch (a->dw_attr)
8855 case DW_AT_name:
8856 attrs->at_name = a;
8857 break;
8858 case DW_AT_type:
8859 attrs->at_type = a;
8860 break;
8861 case DW_AT_friend:
8862 attrs->at_friend = a;
8863 break;
8864 case DW_AT_accessibility:
8865 attrs->at_accessibility = a;
8866 break;
8867 case DW_AT_address_class:
8868 attrs->at_address_class = a;
8869 break;
8870 case DW_AT_allocated:
8871 attrs->at_allocated = a;
8872 break;
8873 case DW_AT_artificial:
8874 attrs->at_artificial = a;
8875 break;
8876 case DW_AT_associated:
8877 attrs->at_associated = a;
8878 break;
8879 case DW_AT_binary_scale:
8880 attrs->at_binary_scale = a;
8881 break;
8882 case DW_AT_bit_offset:
8883 attrs->at_bit_offset = a;
8884 break;
8885 case DW_AT_bit_size:
8886 attrs->at_bit_size = a;
8887 break;
8888 case DW_AT_bit_stride:
8889 attrs->at_bit_stride = a;
8890 break;
8891 case DW_AT_byte_size:
8892 attrs->at_byte_size = a;
8893 break;
8894 case DW_AT_byte_stride:
8895 attrs->at_byte_stride = a;
8896 break;
8897 case DW_AT_const_value:
8898 attrs->at_const_value = a;
8899 break;
8900 case DW_AT_containing_type:
8901 attrs->at_containing_type = a;
8902 break;
8903 case DW_AT_count:
8904 attrs->at_count = a;
8905 break;
8906 case DW_AT_data_location:
8907 attrs->at_data_location = a;
8908 break;
8909 case DW_AT_data_member_location:
8910 attrs->at_data_member_location = a;
8911 break;
8912 case DW_AT_decimal_scale:
8913 attrs->at_decimal_scale = a;
8914 break;
8915 case DW_AT_decimal_sign:
8916 attrs->at_decimal_sign = a;
8917 break;
8918 case DW_AT_default_value:
8919 attrs->at_default_value = a;
8920 break;
8921 case DW_AT_digit_count:
8922 attrs->at_digit_count = a;
8923 break;
8924 case DW_AT_discr:
8925 attrs->at_discr = a;
8926 break;
8927 case DW_AT_discr_list:
8928 attrs->at_discr_list = a;
8929 break;
8930 case DW_AT_discr_value:
8931 attrs->at_discr_value = a;
8932 break;
8933 case DW_AT_encoding:
8934 attrs->at_encoding = a;
8935 break;
8936 case DW_AT_endianity:
8937 attrs->at_endianity = a;
8938 break;
8939 case DW_AT_explicit:
8940 attrs->at_explicit = a;
8941 break;
8942 case DW_AT_is_optional:
8943 attrs->at_is_optional = a;
8944 break;
8945 case DW_AT_location:
8946 attrs->at_location = a;
8947 break;
8948 case DW_AT_lower_bound:
8949 attrs->at_lower_bound = a;
8950 break;
8951 case DW_AT_mutable:
8952 attrs->at_mutable = a;
8953 break;
8954 case DW_AT_ordering:
8955 attrs->at_ordering = a;
8956 break;
8957 case DW_AT_picture_string:
8958 attrs->at_picture_string = a;
8959 break;
8960 case DW_AT_prototyped:
8961 attrs->at_prototyped = a;
8962 break;
8963 case DW_AT_small:
8964 attrs->at_small = a;
8965 break;
8966 case DW_AT_segment:
8967 attrs->at_segment = a;
8968 break;
8969 case DW_AT_string_length:
8970 attrs->at_string_length = a;
8971 break;
8972 case DW_AT_threads_scaled:
8973 attrs->at_threads_scaled = a;
8974 break;
8975 case DW_AT_upper_bound:
8976 attrs->at_upper_bound = a;
8977 break;
8978 case DW_AT_use_location:
8979 attrs->at_use_location = a;
8980 break;
8981 case DW_AT_use_UTF8:
8982 attrs->at_use_UTF8 = a;
8983 break;
8984 case DW_AT_variable_parameter:
8985 attrs->at_variable_parameter = a;
8986 break;
8987 case DW_AT_virtuality:
8988 attrs->at_virtuality = a;
8989 break;
8990 case DW_AT_visibility:
8991 attrs->at_visibility = a;
8992 break;
8993 case DW_AT_vtable_elem_location:
8994 attrs->at_vtable_elem_location = a;
8995 break;
8996 default:
8997 break;
9002 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
9004 static void
9005 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9007 dw_die_ref c;
9008 dw_die_ref decl;
9009 struct checksum_attributes attrs;
9011 CHECKSUM_ULEB128 ('D');
9012 CHECKSUM_ULEB128 (die->die_tag);
9014 memset (&attrs, 0, sizeof (attrs));
9016 decl = get_AT_ref (die, DW_AT_specification);
9017 if (decl != NULL)
9018 collect_checksum_attributes (&attrs, decl);
9019 collect_checksum_attributes (&attrs, die);
9021 CHECKSUM_ATTR (attrs.at_name);
9022 CHECKSUM_ATTR (attrs.at_accessibility);
9023 CHECKSUM_ATTR (attrs.at_address_class);
9024 CHECKSUM_ATTR (attrs.at_allocated);
9025 CHECKSUM_ATTR (attrs.at_artificial);
9026 CHECKSUM_ATTR (attrs.at_associated);
9027 CHECKSUM_ATTR (attrs.at_binary_scale);
9028 CHECKSUM_ATTR (attrs.at_bit_offset);
9029 CHECKSUM_ATTR (attrs.at_bit_size);
9030 CHECKSUM_ATTR (attrs.at_bit_stride);
9031 CHECKSUM_ATTR (attrs.at_byte_size);
9032 CHECKSUM_ATTR (attrs.at_byte_stride);
9033 CHECKSUM_ATTR (attrs.at_const_value);
9034 CHECKSUM_ATTR (attrs.at_containing_type);
9035 CHECKSUM_ATTR (attrs.at_count);
9036 CHECKSUM_ATTR (attrs.at_data_location);
9037 CHECKSUM_ATTR (attrs.at_data_member_location);
9038 CHECKSUM_ATTR (attrs.at_decimal_scale);
9039 CHECKSUM_ATTR (attrs.at_decimal_sign);
9040 CHECKSUM_ATTR (attrs.at_default_value);
9041 CHECKSUM_ATTR (attrs.at_digit_count);
9042 CHECKSUM_ATTR (attrs.at_discr);
9043 CHECKSUM_ATTR (attrs.at_discr_list);
9044 CHECKSUM_ATTR (attrs.at_discr_value);
9045 CHECKSUM_ATTR (attrs.at_encoding);
9046 CHECKSUM_ATTR (attrs.at_endianity);
9047 CHECKSUM_ATTR (attrs.at_explicit);
9048 CHECKSUM_ATTR (attrs.at_is_optional);
9049 CHECKSUM_ATTR (attrs.at_location);
9050 CHECKSUM_ATTR (attrs.at_lower_bound);
9051 CHECKSUM_ATTR (attrs.at_mutable);
9052 CHECKSUM_ATTR (attrs.at_ordering);
9053 CHECKSUM_ATTR (attrs.at_picture_string);
9054 CHECKSUM_ATTR (attrs.at_prototyped);
9055 CHECKSUM_ATTR (attrs.at_small);
9056 CHECKSUM_ATTR (attrs.at_segment);
9057 CHECKSUM_ATTR (attrs.at_string_length);
9058 CHECKSUM_ATTR (attrs.at_threads_scaled);
9059 CHECKSUM_ATTR (attrs.at_upper_bound);
9060 CHECKSUM_ATTR (attrs.at_use_location);
9061 CHECKSUM_ATTR (attrs.at_use_UTF8);
9062 CHECKSUM_ATTR (attrs.at_variable_parameter);
9063 CHECKSUM_ATTR (attrs.at_virtuality);
9064 CHECKSUM_ATTR (attrs.at_visibility);
9065 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9066 CHECKSUM_ATTR (attrs.at_type);
9067 CHECKSUM_ATTR (attrs.at_friend);
9069 /* Checksum the child DIEs, except for nested types and member functions. */
9070 c = die->die_child;
9071 if (c) do {
9072 dw_attr_ref name_attr;
9074 c = c->die_sib;
9075 name_attr = get_AT (c, DW_AT_name);
9076 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9077 && name_attr != NULL)
9079 CHECKSUM_ULEB128 ('S');
9080 CHECKSUM_ULEB128 (c->die_tag);
9081 CHECKSUM_STRING (AT_string (name_attr));
9083 else
9085 /* Mark this DIE so it gets processed when unmarking. */
9086 if (c->die_mark == 0)
9087 c->die_mark = -1;
9088 die_checksum_ordered (c, ctx, mark);
9090 } while (c != die->die_child);
9092 CHECKSUM_ULEB128 (0);
9095 #undef CHECKSUM
9096 #undef CHECKSUM_STRING
9097 #undef CHECKSUM_ATTR
9098 #undef CHECKSUM_LEB128
9099 #undef CHECKSUM_ULEB128
9101 /* Generate the type signature for DIE. This is computed by generating an
9102 MD5 checksum over the DIE's tag, its relevant attributes, and its
9103 children. Attributes that are references to other DIEs are processed
9104 by recursion, using the MARK field to prevent infinite recursion.
9105 If the DIE is nested inside a namespace or another type, we also
9106 need to include that context in the signature. The lower 64 bits
9107 of the resulting MD5 checksum comprise the signature. */
9109 static void
9110 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9112 int mark;
9113 const char *name;
9114 unsigned char checksum[16];
9115 struct md5_ctx ctx;
9116 dw_die_ref decl;
9118 name = get_AT_string (die, DW_AT_name);
9119 decl = get_AT_ref (die, DW_AT_specification);
9121 /* First, compute a signature for just the type name (and its surrounding
9122 context, if any. This is stored in the type unit DIE for link-time
9123 ODR (one-definition rule) checking. */
9125 if (is_cxx() && name != NULL)
9127 md5_init_ctx (&ctx);
9129 /* Checksum the names of surrounding namespaces and structures. */
9130 if (decl != NULL && decl->die_parent != NULL)
9131 checksum_die_context (decl->die_parent, &ctx);
9133 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9134 md5_process_bytes (name, strlen (name) + 1, &ctx);
9135 md5_finish_ctx (&ctx, checksum);
9137 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9140 /* Next, compute the complete type signature. */
9142 md5_init_ctx (&ctx);
9143 mark = 1;
9144 die->die_mark = mark;
9146 /* Checksum the names of surrounding namespaces and structures. */
9147 if (decl != NULL && decl->die_parent != NULL)
9148 checksum_die_context (decl->die_parent, &ctx);
9150 /* Checksum the DIE and its children. */
9151 die_checksum_ordered (die, &ctx, &mark);
9152 unmark_all_dies (die);
9153 md5_finish_ctx (&ctx, checksum);
9155 /* Store the signature in the type node and link the type DIE and the
9156 type node together. */
9157 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9158 DWARF_TYPE_SIGNATURE_SIZE);
9159 die->die_id.die_type_node = type_node;
9160 type_node->type_die = die;
9162 /* If the DIE is a specification, link its declaration to the type node
9163 as well. */
9164 if (decl != NULL)
9165 decl->die_id.die_type_node = type_node;
9168 /* Do the location expressions look same? */
9169 static inline int
9170 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9172 return loc1->dw_loc_opc == loc2->dw_loc_opc
9173 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9174 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9177 /* Do the values look the same? */
9178 static int
9179 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9181 dw_loc_descr_ref loc1, loc2;
9182 rtx r1, r2;
9184 if (v1->val_class != v2->val_class)
9185 return 0;
9187 switch (v1->val_class)
9189 case dw_val_class_const:
9190 return v1->v.val_int == v2->v.val_int;
9191 case dw_val_class_unsigned_const:
9192 return v1->v.val_unsigned == v2->v.val_unsigned;
9193 case dw_val_class_const_double:
9194 return v1->v.val_double.high == v2->v.val_double.high
9195 && v1->v.val_double.low == v2->v.val_double.low;
9196 case dw_val_class_vec:
9197 if (v1->v.val_vec.length != v2->v.val_vec.length
9198 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9199 return 0;
9200 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9201 v1->v.val_vec.length * v1->v.val_vec.elt_size))
9202 return 0;
9203 return 1;
9204 case dw_val_class_flag:
9205 return v1->v.val_flag == v2->v.val_flag;
9206 case dw_val_class_str:
9207 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9209 case dw_val_class_addr:
9210 r1 = v1->v.val_addr;
9211 r2 = v2->v.val_addr;
9212 if (GET_CODE (r1) != GET_CODE (r2))
9213 return 0;
9214 return !rtx_equal_p (r1, r2);
9216 case dw_val_class_offset:
9217 return v1->v.val_offset == v2->v.val_offset;
9219 case dw_val_class_loc:
9220 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9221 loc1 && loc2;
9222 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9223 if (!same_loc_p (loc1, loc2, mark))
9224 return 0;
9225 return !loc1 && !loc2;
9227 case dw_val_class_die_ref:
9228 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9230 case dw_val_class_fde_ref:
9231 case dw_val_class_vms_delta:
9232 case dw_val_class_lbl_id:
9233 case dw_val_class_lineptr:
9234 case dw_val_class_macptr:
9235 return 1;
9237 case dw_val_class_file:
9238 return v1->v.val_file == v2->v.val_file;
9240 case dw_val_class_data8:
9241 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9243 default:
9244 return 1;
9248 /* Do the attributes look the same? */
9250 static int
9251 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9253 if (at1->dw_attr != at2->dw_attr)
9254 return 0;
9256 /* We don't care that this was compiled with a different compiler
9257 snapshot; if the output is the same, that's what matters. */
9258 if (at1->dw_attr == DW_AT_producer)
9259 return 1;
9261 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9264 /* Do the dies look the same? */
9266 static int
9267 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9269 dw_die_ref c1, c2;
9270 dw_attr_ref a1;
9271 unsigned ix;
9273 /* To avoid infinite recursion. */
9274 if (die1->die_mark)
9275 return die1->die_mark == die2->die_mark;
9276 die1->die_mark = die2->die_mark = ++(*mark);
9278 if (die1->die_tag != die2->die_tag)
9279 return 0;
9281 if (VEC_length (dw_attr_node, die1->die_attr)
9282 != VEC_length (dw_attr_node, die2->die_attr))
9283 return 0;
9285 for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
9286 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9287 return 0;
9289 c1 = die1->die_child;
9290 c2 = die2->die_child;
9291 if (! c1)
9293 if (c2)
9294 return 0;
9296 else
9297 for (;;)
9299 if (!same_die_p (c1, c2, mark))
9300 return 0;
9301 c1 = c1->die_sib;
9302 c2 = c2->die_sib;
9303 if (c1 == die1->die_child)
9305 if (c2 == die2->die_child)
9306 break;
9307 else
9308 return 0;
9312 return 1;
9315 /* Do the dies look the same? Wrapper around same_die_p. */
9317 static int
9318 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9320 int mark = 0;
9321 int ret = same_die_p (die1, die2, &mark);
9323 unmark_all_dies (die1);
9324 unmark_all_dies (die2);
9326 return ret;
9329 /* The prefix to attach to symbols on DIEs in the current comdat debug
9330 info section. */
9331 static char *comdat_symbol_id;
9333 /* The index of the current symbol within the current comdat CU. */
9334 static unsigned int comdat_symbol_number;
9336 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9337 children, and set comdat_symbol_id accordingly. */
9339 static void
9340 compute_section_prefix (dw_die_ref unit_die)
9342 const char *die_name = get_AT_string (unit_die, DW_AT_name);
9343 const char *base = die_name ? lbasename (die_name) : "anonymous";
9344 char *name = XALLOCAVEC (char, strlen (base) + 64);
9345 char *p;
9346 int i, mark;
9347 unsigned char checksum[16];
9348 struct md5_ctx ctx;
9350 /* Compute the checksum of the DIE, then append part of it as hex digits to
9351 the name filename of the unit. */
9353 md5_init_ctx (&ctx);
9354 mark = 0;
9355 die_checksum (unit_die, &ctx, &mark);
9356 unmark_all_dies (unit_die);
9357 md5_finish_ctx (&ctx, checksum);
9359 sprintf (name, "%s.", base);
9360 clean_symbol_name (name);
9362 p = name + strlen (name);
9363 for (i = 0; i < 4; i++)
9365 sprintf (p, "%.2x", checksum[i]);
9366 p += 2;
9369 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9370 comdat_symbol_number = 0;
9373 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
9375 static int
9376 is_type_die (dw_die_ref die)
9378 switch (die->die_tag)
9380 case DW_TAG_array_type:
9381 case DW_TAG_class_type:
9382 case DW_TAG_interface_type:
9383 case DW_TAG_enumeration_type:
9384 case DW_TAG_pointer_type:
9385 case DW_TAG_reference_type:
9386 case DW_TAG_rvalue_reference_type:
9387 case DW_TAG_string_type:
9388 case DW_TAG_structure_type:
9389 case DW_TAG_subroutine_type:
9390 case DW_TAG_union_type:
9391 case DW_TAG_ptr_to_member_type:
9392 case DW_TAG_set_type:
9393 case DW_TAG_subrange_type:
9394 case DW_TAG_base_type:
9395 case DW_TAG_const_type:
9396 case DW_TAG_file_type:
9397 case DW_TAG_packed_type:
9398 case DW_TAG_volatile_type:
9399 case DW_TAG_typedef:
9400 return 1;
9401 default:
9402 return 0;
9406 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9407 Basically, we want to choose the bits that are likely to be shared between
9408 compilations (types) and leave out the bits that are specific to individual
9409 compilations (functions). */
9411 static int
9412 is_comdat_die (dw_die_ref c)
9414 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9415 we do for stabs. The advantage is a greater likelihood of sharing between
9416 objects that don't include headers in the same order (and therefore would
9417 put the base types in a different comdat). jason 8/28/00 */
9419 if (c->die_tag == DW_TAG_base_type)
9420 return 0;
9422 if (c->die_tag == DW_TAG_pointer_type
9423 || c->die_tag == DW_TAG_reference_type
9424 || c->die_tag == DW_TAG_rvalue_reference_type
9425 || c->die_tag == DW_TAG_const_type
9426 || c->die_tag == DW_TAG_volatile_type)
9428 dw_die_ref t = get_AT_ref (c, DW_AT_type);
9430 return t ? is_comdat_die (t) : 0;
9433 return is_type_die (c);
9436 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9437 compilation unit. */
9439 static int
9440 is_symbol_die (dw_die_ref c)
9442 return (is_type_die (c)
9443 || is_declaration_die (c)
9444 || c->die_tag == DW_TAG_namespace
9445 || c->die_tag == DW_TAG_module);
9448 static char *
9449 gen_internal_sym (const char *prefix)
9451 char buf[256];
9453 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9454 return xstrdup (buf);
9457 /* Assign symbols to all worthy DIEs under DIE. */
9459 static void
9460 assign_symbol_names (dw_die_ref die)
9462 dw_die_ref c;
9464 if (is_symbol_die (die))
9466 if (comdat_symbol_id)
9468 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9470 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9471 comdat_symbol_id, comdat_symbol_number++);
9472 die->die_id.die_symbol = xstrdup (p);
9474 else
9475 die->die_id.die_symbol = gen_internal_sym ("LDIE");
9478 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9481 struct cu_hash_table_entry
9483 dw_die_ref cu;
9484 unsigned min_comdat_num, max_comdat_num;
9485 struct cu_hash_table_entry *next;
9488 /* Routines to manipulate hash table of CUs. */
9489 static hashval_t
9490 htab_cu_hash (const void *of)
9492 const struct cu_hash_table_entry *const entry =
9493 (const struct cu_hash_table_entry *) of;
9495 return htab_hash_string (entry->cu->die_id.die_symbol);
9498 static int
9499 htab_cu_eq (const void *of1, const void *of2)
9501 const struct cu_hash_table_entry *const entry1 =
9502 (const struct cu_hash_table_entry *) of1;
9503 const struct die_struct *const entry2 = (const struct die_struct *) of2;
9505 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9508 static void
9509 htab_cu_del (void *what)
9511 struct cu_hash_table_entry *next,
9512 *entry = (struct cu_hash_table_entry *) what;
9514 while (entry)
9516 next = entry->next;
9517 free (entry);
9518 entry = next;
9522 /* Check whether we have already seen this CU and set up SYM_NUM
9523 accordingly. */
9524 static int
9525 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9527 struct cu_hash_table_entry dummy;
9528 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9530 dummy.max_comdat_num = 0;
9532 slot = (struct cu_hash_table_entry **)
9533 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9534 INSERT);
9535 entry = *slot;
9537 for (; entry; last = entry, entry = entry->next)
9539 if (same_die_p_wrap (cu, entry->cu))
9540 break;
9543 if (entry)
9545 *sym_num = entry->min_comdat_num;
9546 return 1;
9549 entry = XCNEW (struct cu_hash_table_entry);
9550 entry->cu = cu;
9551 entry->min_comdat_num = *sym_num = last->max_comdat_num;
9552 entry->next = *slot;
9553 *slot = entry;
9555 return 0;
9558 /* Record SYM_NUM to record of CU in HTABLE. */
9559 static void
9560 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9562 struct cu_hash_table_entry **slot, *entry;
9564 slot = (struct cu_hash_table_entry **)
9565 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9566 NO_INSERT);
9567 entry = *slot;
9569 entry->max_comdat_num = sym_num;
9572 /* Traverse the DIE (which is always comp_unit_die), and set up
9573 additional compilation units for each of the include files we see
9574 bracketed by BINCL/EINCL. */
9576 static void
9577 break_out_includes (dw_die_ref die)
9579 dw_die_ref c;
9580 dw_die_ref unit = NULL;
9581 limbo_die_node *node, **pnode;
9582 htab_t cu_hash_table;
9584 c = die->die_child;
9585 if (c) do {
9586 dw_die_ref prev = c;
9587 c = c->die_sib;
9588 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9589 || (unit && is_comdat_die (c)))
9591 dw_die_ref next = c->die_sib;
9593 /* This DIE is for a secondary CU; remove it from the main one. */
9594 remove_child_with_prev (c, prev);
9596 if (c->die_tag == DW_TAG_GNU_BINCL)
9597 unit = push_new_compile_unit (unit, c);
9598 else if (c->die_tag == DW_TAG_GNU_EINCL)
9599 unit = pop_compile_unit (unit);
9600 else
9601 add_child_die (unit, c);
9602 c = next;
9603 if (c == die->die_child)
9604 break;
9606 } while (c != die->die_child);
9608 #if 0
9609 /* We can only use this in debugging, since the frontend doesn't check
9610 to make sure that we leave every include file we enter. */
9611 gcc_assert (!unit);
9612 #endif
9614 assign_symbol_names (die);
9615 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9616 for (node = limbo_die_list, pnode = &limbo_die_list;
9617 node;
9618 node = node->next)
9620 int is_dupl;
9622 compute_section_prefix (node->die);
9623 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9624 &comdat_symbol_number);
9625 assign_symbol_names (node->die);
9626 if (is_dupl)
9627 *pnode = node->next;
9628 else
9630 pnode = &node->next;
9631 record_comdat_symbol_number (node->die, cu_hash_table,
9632 comdat_symbol_number);
9635 htab_delete (cu_hash_table);
9638 /* Return non-zero if this DIE is a declaration. */
9640 static int
9641 is_declaration_die (dw_die_ref die)
9643 dw_attr_ref a;
9644 unsigned ix;
9646 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9647 if (a->dw_attr == DW_AT_declaration)
9648 return 1;
9650 return 0;
9653 /* Return non-zero if this DIE is nested inside a subprogram. */
9655 static int
9656 is_nested_in_subprogram (dw_die_ref die)
9658 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9660 if (decl == NULL)
9661 decl = die;
9662 return local_scope_p (decl);
9665 /* Return non-zero if this is a type DIE that should be moved to a
9666 COMDAT .debug_types section. */
9668 static int
9669 should_move_die_to_comdat (dw_die_ref die)
9671 switch (die->die_tag)
9673 case DW_TAG_class_type:
9674 case DW_TAG_structure_type:
9675 case DW_TAG_enumeration_type:
9676 case DW_TAG_union_type:
9677 /* Don't move declarations, inlined instances, or types nested in a
9678 subprogram. */
9679 if (is_declaration_die (die)
9680 || get_AT (die, DW_AT_abstract_origin)
9681 || is_nested_in_subprogram (die))
9682 return 0;
9683 return 1;
9684 case DW_TAG_array_type:
9685 case DW_TAG_interface_type:
9686 case DW_TAG_pointer_type:
9687 case DW_TAG_reference_type:
9688 case DW_TAG_rvalue_reference_type:
9689 case DW_TAG_string_type:
9690 case DW_TAG_subroutine_type:
9691 case DW_TAG_ptr_to_member_type:
9692 case DW_TAG_set_type:
9693 case DW_TAG_subrange_type:
9694 case DW_TAG_base_type:
9695 case DW_TAG_const_type:
9696 case DW_TAG_file_type:
9697 case DW_TAG_packed_type:
9698 case DW_TAG_volatile_type:
9699 case DW_TAG_typedef:
9700 default:
9701 return 0;
9705 /* Make a clone of DIE. */
9707 static dw_die_ref
9708 clone_die (dw_die_ref die)
9710 dw_die_ref clone;
9711 dw_attr_ref a;
9712 unsigned ix;
9714 clone = ggc_alloc_cleared_die_node ();
9715 clone->die_tag = die->die_tag;
9717 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9718 add_dwarf_attr (clone, a);
9720 return clone;
9723 /* Make a clone of the tree rooted at DIE. */
9725 static dw_die_ref
9726 clone_tree (dw_die_ref die)
9728 dw_die_ref c;
9729 dw_die_ref clone = clone_die (die);
9731 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9733 return clone;
9736 /* Make a clone of DIE as a declaration. */
9738 static dw_die_ref
9739 clone_as_declaration (dw_die_ref die)
9741 dw_die_ref clone;
9742 dw_die_ref decl;
9743 dw_attr_ref a;
9744 unsigned ix;
9746 /* If the DIE is already a declaration, just clone it. */
9747 if (is_declaration_die (die))
9748 return clone_die (die);
9750 /* If the DIE is a specification, just clone its declaration DIE. */
9751 decl = get_AT_ref (die, DW_AT_specification);
9752 if (decl != NULL)
9753 return clone_die (decl);
9755 clone = ggc_alloc_cleared_die_node ();
9756 clone->die_tag = die->die_tag;
9758 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9760 /* We don't want to copy over all attributes.
9761 For example we don't want DW_AT_byte_size because otherwise we will no
9762 longer have a declaration and GDB will treat it as a definition. */
9764 switch (a->dw_attr)
9766 case DW_AT_artificial:
9767 case DW_AT_containing_type:
9768 case DW_AT_external:
9769 case DW_AT_name:
9770 case DW_AT_type:
9771 case DW_AT_virtuality:
9772 case DW_AT_linkage_name:
9773 case DW_AT_MIPS_linkage_name:
9774 add_dwarf_attr (clone, a);
9775 break;
9776 case DW_AT_byte_size:
9777 default:
9778 break;
9782 if (die->die_id.die_type_node)
9783 add_AT_die_ref (clone, DW_AT_signature, die);
9785 add_AT_flag (clone, DW_AT_declaration, 1);
9786 return clone;
9789 /* Copy the declaration context to the new compile unit DIE. This includes
9790 any surrounding namespace or type declarations. If the DIE has an
9791 AT_specification attribute, it also includes attributes and children
9792 attached to the specification. */
9794 static void
9795 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9797 dw_die_ref decl;
9798 dw_die_ref new_decl;
9800 decl = get_AT_ref (die, DW_AT_specification);
9801 if (decl == NULL)
9802 decl = die;
9803 else
9805 unsigned ix;
9806 dw_die_ref c;
9807 dw_attr_ref a;
9809 /* Copy the type node pointer from the new DIE to the original
9810 declaration DIE so we can forward references later. */
9811 decl->die_id.die_type_node = die->die_id.die_type_node;
9813 remove_AT (die, DW_AT_specification);
9815 for (ix = 0; VEC_iterate (dw_attr_node, decl->die_attr, ix, a); ix++)
9817 if (a->dw_attr != DW_AT_name
9818 && a->dw_attr != DW_AT_declaration
9819 && a->dw_attr != DW_AT_external)
9820 add_dwarf_attr (die, a);
9823 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9826 if (decl->die_parent != NULL
9827 && decl->die_parent->die_tag != DW_TAG_compile_unit
9828 && decl->die_parent->die_tag != DW_TAG_type_unit)
9830 new_decl = copy_ancestor_tree (unit, decl, NULL);
9831 if (new_decl != NULL)
9833 remove_AT (new_decl, DW_AT_signature);
9834 add_AT_specification (die, new_decl);
9839 /* Generate the skeleton ancestor tree for the given NODE, then clone
9840 the DIE and add the clone into the tree. */
9842 static void
9843 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9845 if (node->new_die != NULL)
9846 return;
9848 node->new_die = clone_as_declaration (node->old_die);
9850 if (node->parent != NULL)
9852 generate_skeleton_ancestor_tree (node->parent);
9853 add_child_die (node->parent->new_die, node->new_die);
9857 /* Generate a skeleton tree of DIEs containing any declarations that are
9858 found in the original tree. We traverse the tree looking for declaration
9859 DIEs, and construct the skeleton from the bottom up whenever we find one. */
9861 static void
9862 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9864 skeleton_chain_node node;
9865 dw_die_ref c;
9866 dw_die_ref first;
9867 dw_die_ref prev = NULL;
9868 dw_die_ref next = NULL;
9870 node.parent = parent;
9872 first = c = parent->old_die->die_child;
9873 if (c)
9874 next = c->die_sib;
9875 if (c) do {
9876 if (prev == NULL || prev->die_sib == c)
9877 prev = c;
9878 c = next;
9879 next = (c == first ? NULL : c->die_sib);
9880 node.old_die = c;
9881 node.new_die = NULL;
9882 if (is_declaration_die (c))
9884 /* Clone the existing DIE, move the original to the skeleton
9885 tree (which is in the main CU), and put the clone, with
9886 all the original's children, where the original came from. */
9887 dw_die_ref clone = clone_die (c);
9888 move_all_children (c, clone);
9890 replace_child (c, clone, prev);
9891 generate_skeleton_ancestor_tree (parent);
9892 add_child_die (parent->new_die, c);
9893 node.new_die = c;
9894 c = clone;
9896 generate_skeleton_bottom_up (&node);
9897 } while (next != NULL);
9900 /* Wrapper function for generate_skeleton_bottom_up. */
9902 static dw_die_ref
9903 generate_skeleton (dw_die_ref die)
9905 skeleton_chain_node node;
9907 node.old_die = die;
9908 node.new_die = NULL;
9909 node.parent = NULL;
9911 /* If this type definition is nested inside another type,
9912 always leave at least a declaration in its place. */
9913 if (die->die_parent != NULL && is_type_die (die->die_parent))
9914 node.new_die = clone_as_declaration (die);
9916 generate_skeleton_bottom_up (&node);
9917 return node.new_die;
9920 /* Remove the DIE from its parent, possibly replacing it with a cloned
9921 declaration. The original DIE will be moved to a new compile unit
9922 so that existing references to it follow it to the new location. If
9923 any of the original DIE's descendants is a declaration, we need to
9924 replace the original DIE with a skeleton tree and move the
9925 declarations back into the skeleton tree. */
9927 static dw_die_ref
9928 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
9930 dw_die_ref skeleton;
9932 skeleton = generate_skeleton (child);
9933 if (skeleton == NULL)
9934 remove_child_with_prev (child, prev);
9935 else
9937 skeleton->die_id.die_type_node = child->die_id.die_type_node;
9938 replace_child (child, skeleton, prev);
9941 return skeleton;
9944 /* Traverse the DIE and set up additional .debug_types sections for each
9945 type worthy of being placed in a COMDAT section. */
9947 static void
9948 break_out_comdat_types (dw_die_ref die)
9950 dw_die_ref c;
9951 dw_die_ref first;
9952 dw_die_ref prev = NULL;
9953 dw_die_ref next = NULL;
9954 dw_die_ref unit = NULL;
9956 first = c = die->die_child;
9957 if (c)
9958 next = c->die_sib;
9959 if (c) do {
9960 if (prev == NULL || prev->die_sib == c)
9961 prev = c;
9962 c = next;
9963 next = (c == first ? NULL : c->die_sib);
9964 if (should_move_die_to_comdat (c))
9966 dw_die_ref replacement;
9967 comdat_type_node_ref type_node;
9969 /* Create a new type unit DIE as the root for the new tree, and
9970 add it to the list of comdat types. */
9971 unit = new_die (DW_TAG_type_unit, NULL, NULL);
9972 add_AT_unsigned (unit, DW_AT_language,
9973 get_AT_unsigned (comp_unit_die, DW_AT_language));
9974 type_node = ggc_alloc_cleared_comdat_type_node ();
9975 type_node->root_die = unit;
9976 type_node->next = comdat_type_list;
9977 comdat_type_list = type_node;
9979 /* Generate the type signature. */
9980 generate_type_signature (c, type_node);
9982 /* Copy the declaration context, attributes, and children of the
9983 declaration into the new compile unit DIE. */
9984 copy_declaration_context (unit, c);
9986 /* Remove this DIE from the main CU. */
9987 replacement = remove_child_or_replace_with_skeleton (c, prev);
9989 /* Break out nested types into their own type units. */
9990 break_out_comdat_types (c);
9992 /* Add the DIE to the new compunit. */
9993 add_child_die (unit, c);
9995 if (replacement != NULL)
9996 c = replacement;
9998 else if (c->die_tag == DW_TAG_namespace
9999 || c->die_tag == DW_TAG_class_type
10000 || c->die_tag == DW_TAG_structure_type
10001 || c->die_tag == DW_TAG_union_type)
10003 /* Look for nested types that can be broken out. */
10004 break_out_comdat_types (c);
10006 } while (next != NULL);
10009 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
10011 struct decl_table_entry
10013 dw_die_ref orig;
10014 dw_die_ref copy;
10017 /* Routines to manipulate hash table of copied declarations. */
10019 static hashval_t
10020 htab_decl_hash (const void *of)
10022 const struct decl_table_entry *const entry =
10023 (const struct decl_table_entry *) of;
10025 return htab_hash_pointer (entry->orig);
10028 static int
10029 htab_decl_eq (const void *of1, const void *of2)
10031 const struct decl_table_entry *const entry1 =
10032 (const struct decl_table_entry *) of1;
10033 const struct die_struct *const entry2 = (const struct die_struct *) of2;
10035 return entry1->orig == entry2;
10038 static void
10039 htab_decl_del (void *what)
10041 struct decl_table_entry *entry = (struct decl_table_entry *) what;
10043 free (entry);
10046 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10047 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
10048 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
10049 to check if the ancestor has already been copied into UNIT. */
10051 static dw_die_ref
10052 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10054 dw_die_ref parent = die->die_parent;
10055 dw_die_ref new_parent = unit;
10056 dw_die_ref copy;
10057 void **slot = NULL;
10058 struct decl_table_entry *entry = NULL;
10060 if (decl_table)
10062 /* Check if the entry has already been copied to UNIT. */
10063 slot = htab_find_slot_with_hash (decl_table, die,
10064 htab_hash_pointer (die), INSERT);
10065 if (*slot != HTAB_EMPTY_ENTRY)
10067 entry = (struct decl_table_entry *) *slot;
10068 return entry->copy;
10071 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
10072 entry = XCNEW (struct decl_table_entry);
10073 entry->orig = die;
10074 entry->copy = NULL;
10075 *slot = entry;
10078 if (parent != NULL)
10080 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10081 if (spec != NULL)
10082 parent = spec;
10083 if (parent->die_tag != DW_TAG_compile_unit
10084 && parent->die_tag != DW_TAG_type_unit)
10085 new_parent = copy_ancestor_tree (unit, parent, decl_table);
10088 copy = clone_as_declaration (die);
10089 add_child_die (new_parent, copy);
10091 if (decl_table != NULL)
10093 /* Record the pointer to the copy. */
10094 entry->copy = copy;
10097 return copy;
10100 /* Walk the DIE and its children, looking for references to incomplete
10101 or trivial types that are unmarked (i.e., that are not in the current
10102 type_unit). */
10104 static void
10105 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10107 dw_die_ref c;
10108 dw_attr_ref a;
10109 unsigned ix;
10111 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10113 if (AT_class (a) == dw_val_class_die_ref)
10115 dw_die_ref targ = AT_ref (a);
10116 comdat_type_node_ref type_node = targ->die_id.die_type_node;
10117 void **slot;
10118 struct decl_table_entry *entry;
10120 if (targ->die_mark != 0 || type_node != NULL)
10121 continue;
10123 slot = htab_find_slot_with_hash (decl_table, targ,
10124 htab_hash_pointer (targ), INSERT);
10126 if (*slot != HTAB_EMPTY_ENTRY)
10128 /* TARG has already been copied, so we just need to
10129 modify the reference to point to the copy. */
10130 entry = (struct decl_table_entry *) *slot;
10131 a->dw_attr_val.v.val_die_ref.die = entry->copy;
10133 else
10135 dw_die_ref parent = unit;
10136 dw_die_ref copy = clone_tree (targ);
10138 /* Make sure the cloned tree is marked as part of the
10139 type unit. */
10140 mark_dies (copy);
10142 /* Record in DECL_TABLE that TARG has been copied.
10143 Need to do this now, before the recursive call,
10144 because DECL_TABLE may be expanded and SLOT
10145 would no longer be a valid pointer. */
10146 entry = XCNEW (struct decl_table_entry);
10147 entry->orig = targ;
10148 entry->copy = copy;
10149 *slot = entry;
10151 /* If TARG has surrounding context, copy its ancestor tree
10152 into the new type unit. */
10153 if (targ->die_parent != NULL
10154 && targ->die_parent->die_tag != DW_TAG_compile_unit
10155 && targ->die_parent->die_tag != DW_TAG_type_unit)
10156 parent = copy_ancestor_tree (unit, targ->die_parent,
10157 decl_table);
10159 add_child_die (parent, copy);
10160 a->dw_attr_val.v.val_die_ref.die = copy;
10162 /* Make sure the newly-copied DIE is walked. If it was
10163 installed in a previously-added context, it won't
10164 get visited otherwise. */
10165 if (parent != unit)
10167 /* Find the highest point of the newly-added tree,
10168 mark each node along the way, and walk from there. */
10169 parent->die_mark = 1;
10170 while (parent->die_parent
10171 && parent->die_parent->die_mark == 0)
10173 parent = parent->die_parent;
10174 parent->die_mark = 1;
10176 copy_decls_walk (unit, parent, decl_table);
10182 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10185 /* Copy declarations for "unworthy" types into the new comdat section.
10186 Incomplete types, modified types, and certain other types aren't broken
10187 out into comdat sections of their own, so they don't have a signature,
10188 and we need to copy the declaration into the same section so that we
10189 don't have an external reference. */
10191 static void
10192 copy_decls_for_unworthy_types (dw_die_ref unit)
10194 htab_t decl_table;
10196 mark_dies (unit);
10197 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10198 copy_decls_walk (unit, unit, decl_table);
10199 htab_delete (decl_table);
10200 unmark_dies (unit);
10203 /* Traverse the DIE and add a sibling attribute if it may have the
10204 effect of speeding up access to siblings. To save some space,
10205 avoid generating sibling attributes for DIE's without children. */
10207 static void
10208 add_sibling_attributes (dw_die_ref die)
10210 dw_die_ref c;
10212 if (! die->die_child)
10213 return;
10215 if (die->die_parent && die != die->die_parent->die_child)
10216 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10218 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10221 /* Output all location lists for the DIE and its children. */
10223 static void
10224 output_location_lists (dw_die_ref die)
10226 dw_die_ref c;
10227 dw_attr_ref a;
10228 unsigned ix;
10230 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10231 if (AT_class (a) == dw_val_class_loc_list)
10232 output_loc_list (AT_loc_list (a));
10234 FOR_EACH_CHILD (die, c, output_location_lists (c));
10237 /* The format of each DIE (and its attribute value pairs) is encoded in an
10238 abbreviation table. This routine builds the abbreviation table and assigns
10239 a unique abbreviation id for each abbreviation entry. The children of each
10240 die are visited recursively. */
10242 static void
10243 build_abbrev_table (dw_die_ref die)
10245 unsigned long abbrev_id;
10246 unsigned int n_alloc;
10247 dw_die_ref c;
10248 dw_attr_ref a;
10249 unsigned ix;
10251 /* Scan the DIE references, and mark as external any that refer to
10252 DIEs from other CUs (i.e. those which are not marked). */
10253 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10254 if (AT_class (a) == dw_val_class_die_ref
10255 && AT_ref (a)->die_mark == 0)
10257 gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10258 set_AT_ref_external (a, 1);
10261 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10263 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10264 dw_attr_ref die_a, abbrev_a;
10265 unsigned ix;
10266 bool ok = true;
10268 if (abbrev->die_tag != die->die_tag)
10269 continue;
10270 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10271 continue;
10273 if (VEC_length (dw_attr_node, abbrev->die_attr)
10274 != VEC_length (dw_attr_node, die->die_attr))
10275 continue;
10277 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
10279 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10280 if ((abbrev_a->dw_attr != die_a->dw_attr)
10281 || (value_format (abbrev_a) != value_format (die_a)))
10283 ok = false;
10284 break;
10287 if (ok)
10288 break;
10291 if (abbrev_id >= abbrev_die_table_in_use)
10293 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10295 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10296 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10297 n_alloc);
10299 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10300 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10301 abbrev_die_table_allocated = n_alloc;
10304 ++abbrev_die_table_in_use;
10305 abbrev_die_table[abbrev_id] = die;
10308 die->die_abbrev = abbrev_id;
10309 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10312 /* Return the power-of-two number of bytes necessary to represent VALUE. */
10314 static int
10315 constant_size (unsigned HOST_WIDE_INT value)
10317 int log;
10319 if (value == 0)
10320 log = 0;
10321 else
10322 log = floor_log2 (value);
10324 log = log / 8;
10325 log = 1 << (floor_log2 (log) + 1);
10327 return log;
10330 /* Return the size of a DIE as it is represented in the
10331 .debug_info section. */
10333 static unsigned long
10334 size_of_die (dw_die_ref die)
10336 unsigned long size = 0;
10337 dw_attr_ref a;
10338 unsigned ix;
10340 size += size_of_uleb128 (die->die_abbrev);
10341 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10343 switch (AT_class (a))
10345 case dw_val_class_addr:
10346 size += DWARF2_ADDR_SIZE;
10347 break;
10348 case dw_val_class_offset:
10349 size += DWARF_OFFSET_SIZE;
10350 break;
10351 case dw_val_class_loc:
10353 unsigned long lsize = size_of_locs (AT_loc (a));
10355 /* Block length. */
10356 if (dwarf_version >= 4)
10357 size += size_of_uleb128 (lsize);
10358 else
10359 size += constant_size (lsize);
10360 size += lsize;
10362 break;
10363 case dw_val_class_loc_list:
10364 size += DWARF_OFFSET_SIZE;
10365 break;
10366 case dw_val_class_range_list:
10367 size += DWARF_OFFSET_SIZE;
10368 break;
10369 case dw_val_class_const:
10370 size += size_of_sleb128 (AT_int (a));
10371 break;
10372 case dw_val_class_unsigned_const:
10373 size += constant_size (AT_unsigned (a));
10374 break;
10375 case dw_val_class_const_double:
10376 size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10377 if (HOST_BITS_PER_WIDE_INT >= 64)
10378 size++; /* block */
10379 break;
10380 case dw_val_class_vec:
10381 size += constant_size (a->dw_attr_val.v.val_vec.length
10382 * a->dw_attr_val.v.val_vec.elt_size)
10383 + a->dw_attr_val.v.val_vec.length
10384 * a->dw_attr_val.v.val_vec.elt_size; /* block */
10385 break;
10386 case dw_val_class_flag:
10387 if (dwarf_version >= 4)
10388 /* Currently all add_AT_flag calls pass in 1 as last argument,
10389 so DW_FORM_flag_present can be used. If that ever changes,
10390 we'll need to use DW_FORM_flag and have some optimization
10391 in build_abbrev_table that will change those to
10392 DW_FORM_flag_present if it is set to 1 in all DIEs using
10393 the same abbrev entry. */
10394 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10395 else
10396 size += 1;
10397 break;
10398 case dw_val_class_die_ref:
10399 if (AT_ref_external (a))
10401 /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10402 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
10403 is sized by target address length, whereas in DWARF3
10404 it's always sized as an offset. */
10405 if (dwarf_version >= 4)
10406 size += DWARF_TYPE_SIGNATURE_SIZE;
10407 else if (dwarf_version == 2)
10408 size += DWARF2_ADDR_SIZE;
10409 else
10410 size += DWARF_OFFSET_SIZE;
10412 else
10413 size += DWARF_OFFSET_SIZE;
10414 break;
10415 case dw_val_class_fde_ref:
10416 size += DWARF_OFFSET_SIZE;
10417 break;
10418 case dw_val_class_lbl_id:
10419 size += DWARF2_ADDR_SIZE;
10420 break;
10421 case dw_val_class_lineptr:
10422 case dw_val_class_macptr:
10423 size += DWARF_OFFSET_SIZE;
10424 break;
10425 case dw_val_class_str:
10426 if (AT_string_form (a) == DW_FORM_strp)
10427 size += DWARF_OFFSET_SIZE;
10428 else
10429 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10430 break;
10431 case dw_val_class_file:
10432 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10433 break;
10434 case dw_val_class_data8:
10435 size += 8;
10436 break;
10437 case dw_val_class_vms_delta:
10438 size += DWARF_OFFSET_SIZE;
10439 break;
10440 default:
10441 gcc_unreachable ();
10445 return size;
10448 /* Size the debugging information associated with a given DIE. Visits the
10449 DIE's children recursively. Updates the global variable next_die_offset, on
10450 each time through. Uses the current value of next_die_offset to update the
10451 die_offset field in each DIE. */
10453 static void
10454 calc_die_sizes (dw_die_ref die)
10456 dw_die_ref c;
10458 die->die_offset = next_die_offset;
10459 next_die_offset += size_of_die (die);
10461 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10463 if (die->die_child != NULL)
10464 /* Count the null byte used to terminate sibling lists. */
10465 next_die_offset += 1;
10468 /* Set the marks for a die and its children. We do this so
10469 that we know whether or not a reference needs to use FORM_ref_addr; only
10470 DIEs in the same CU will be marked. We used to clear out the offset
10471 and use that as the flag, but ran into ordering problems. */
10473 static void
10474 mark_dies (dw_die_ref die)
10476 dw_die_ref c;
10478 gcc_assert (!die->die_mark);
10480 die->die_mark = 1;
10481 FOR_EACH_CHILD (die, c, mark_dies (c));
10484 /* Clear the marks for a die and its children. */
10486 static void
10487 unmark_dies (dw_die_ref die)
10489 dw_die_ref c;
10491 if (dwarf_version < 4)
10492 gcc_assert (die->die_mark);
10494 die->die_mark = 0;
10495 FOR_EACH_CHILD (die, c, unmark_dies (c));
10498 /* Clear the marks for a die, its children and referred dies. */
10500 static void
10501 unmark_all_dies (dw_die_ref die)
10503 dw_die_ref c;
10504 dw_attr_ref a;
10505 unsigned ix;
10507 if (!die->die_mark)
10508 return;
10509 die->die_mark = 0;
10511 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10513 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10514 if (AT_class (a) == dw_val_class_die_ref)
10515 unmark_all_dies (AT_ref (a));
10518 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10519 generated for the compilation unit. */
10521 static unsigned long
10522 size_of_pubnames (VEC (pubname_entry, gc) * names)
10524 unsigned long size;
10525 unsigned i;
10526 pubname_ref p;
10528 size = DWARF_PUBNAMES_HEADER_SIZE;
10529 for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
10530 if (names != pubtype_table
10531 || p->die->die_offset != 0
10532 || !flag_eliminate_unused_debug_types)
10533 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10535 size += DWARF_OFFSET_SIZE;
10536 return size;
10539 /* Return the size of the information in the .debug_aranges section. */
10541 static unsigned long
10542 size_of_aranges (void)
10544 unsigned long size;
10546 size = DWARF_ARANGES_HEADER_SIZE;
10548 /* Count the address/length pair for this compilation unit. */
10549 if (text_section_used)
10550 size += 2 * DWARF2_ADDR_SIZE;
10551 if (cold_text_section_used)
10552 size += 2 * DWARF2_ADDR_SIZE;
10553 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10555 /* Count the two zero words used to terminated the address range table. */
10556 size += 2 * DWARF2_ADDR_SIZE;
10557 return size;
10560 /* Select the encoding of an attribute value. */
10562 static enum dwarf_form
10563 value_format (dw_attr_ref a)
10565 switch (a->dw_attr_val.val_class)
10567 case dw_val_class_addr:
10568 /* Only very few attributes allow DW_FORM_addr. */
10569 switch (a->dw_attr)
10571 case DW_AT_low_pc:
10572 case DW_AT_high_pc:
10573 case DW_AT_entry_pc:
10574 case DW_AT_trampoline:
10575 return DW_FORM_addr;
10576 default:
10577 break;
10579 switch (DWARF2_ADDR_SIZE)
10581 case 1:
10582 return DW_FORM_data1;
10583 case 2:
10584 return DW_FORM_data2;
10585 case 4:
10586 return DW_FORM_data4;
10587 case 8:
10588 return DW_FORM_data8;
10589 default:
10590 gcc_unreachable ();
10592 case dw_val_class_range_list:
10593 case dw_val_class_loc_list:
10594 if (dwarf_version >= 4)
10595 return DW_FORM_sec_offset;
10596 /* FALLTHRU */
10597 case dw_val_class_vms_delta:
10598 case dw_val_class_offset:
10599 switch (DWARF_OFFSET_SIZE)
10601 case 4:
10602 return DW_FORM_data4;
10603 case 8:
10604 return DW_FORM_data8;
10605 default:
10606 gcc_unreachable ();
10608 case dw_val_class_loc:
10609 if (dwarf_version >= 4)
10610 return DW_FORM_exprloc;
10611 switch (constant_size (size_of_locs (AT_loc (a))))
10613 case 1:
10614 return DW_FORM_block1;
10615 case 2:
10616 return DW_FORM_block2;
10617 default:
10618 gcc_unreachable ();
10620 case dw_val_class_const:
10621 return DW_FORM_sdata;
10622 case dw_val_class_unsigned_const:
10623 switch (constant_size (AT_unsigned (a)))
10625 case 1:
10626 return DW_FORM_data1;
10627 case 2:
10628 return DW_FORM_data2;
10629 case 4:
10630 return DW_FORM_data4;
10631 case 8:
10632 return DW_FORM_data8;
10633 default:
10634 gcc_unreachable ();
10636 case dw_val_class_const_double:
10637 switch (HOST_BITS_PER_WIDE_INT)
10639 case 8:
10640 return DW_FORM_data2;
10641 case 16:
10642 return DW_FORM_data4;
10643 case 32:
10644 return DW_FORM_data8;
10645 case 64:
10646 default:
10647 return DW_FORM_block1;
10649 case dw_val_class_vec:
10650 switch (constant_size (a->dw_attr_val.v.val_vec.length
10651 * a->dw_attr_val.v.val_vec.elt_size))
10653 case 1:
10654 return DW_FORM_block1;
10655 case 2:
10656 return DW_FORM_block2;
10657 case 4:
10658 return DW_FORM_block4;
10659 default:
10660 gcc_unreachable ();
10662 case dw_val_class_flag:
10663 if (dwarf_version >= 4)
10665 /* Currently all add_AT_flag calls pass in 1 as last argument,
10666 so DW_FORM_flag_present can be used. If that ever changes,
10667 we'll need to use DW_FORM_flag and have some optimization
10668 in build_abbrev_table that will change those to
10669 DW_FORM_flag_present if it is set to 1 in all DIEs using
10670 the same abbrev entry. */
10671 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10672 return DW_FORM_flag_present;
10674 return DW_FORM_flag;
10675 case dw_val_class_die_ref:
10676 if (AT_ref_external (a))
10677 return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10678 else
10679 return DW_FORM_ref;
10680 case dw_val_class_fde_ref:
10681 return DW_FORM_data;
10682 case dw_val_class_lbl_id:
10683 return DW_FORM_addr;
10684 case dw_val_class_lineptr:
10685 case dw_val_class_macptr:
10686 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10687 case dw_val_class_str:
10688 return AT_string_form (a);
10689 case dw_val_class_file:
10690 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10692 case 1:
10693 return DW_FORM_data1;
10694 case 2:
10695 return DW_FORM_data2;
10696 case 4:
10697 return DW_FORM_data4;
10698 default:
10699 gcc_unreachable ();
10702 case dw_val_class_data8:
10703 return DW_FORM_data8;
10705 default:
10706 gcc_unreachable ();
10710 /* Output the encoding of an attribute value. */
10712 static void
10713 output_value_format (dw_attr_ref a)
10715 enum dwarf_form form = value_format (a);
10717 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10720 /* Output the .debug_abbrev section which defines the DIE abbreviation
10721 table. */
10723 static void
10724 output_abbrev_section (void)
10726 unsigned long abbrev_id;
10728 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10730 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10731 unsigned ix;
10732 dw_attr_ref a_attr;
10734 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10735 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10736 dwarf_tag_name (abbrev->die_tag));
10738 if (abbrev->die_child != NULL)
10739 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10740 else
10741 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10743 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10744 ix++)
10746 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10747 dwarf_attr_name (a_attr->dw_attr));
10748 output_value_format (a_attr);
10751 dw2_asm_output_data (1, 0, NULL);
10752 dw2_asm_output_data (1, 0, NULL);
10755 /* Terminate the table. */
10756 dw2_asm_output_data (1, 0, NULL);
10759 /* Output a symbol we can use to refer to this DIE from another CU. */
10761 static inline void
10762 output_die_symbol (dw_die_ref die)
10764 char *sym = die->die_id.die_symbol;
10766 if (sym == 0)
10767 return;
10769 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10770 /* We make these global, not weak; if the target doesn't support
10771 .linkonce, it doesn't support combining the sections, so debugging
10772 will break. */
10773 targetm.asm_out.globalize_label (asm_out_file, sym);
10775 ASM_OUTPUT_LABEL (asm_out_file, sym);
10778 /* Return a new location list, given the begin and end range, and the
10779 expression. */
10781 static inline dw_loc_list_ref
10782 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10783 const char *section)
10785 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
10787 retlist->begin = begin;
10788 retlist->end = end;
10789 retlist->expr = expr;
10790 retlist->section = section;
10792 return retlist;
10795 /* Generate a new internal symbol for this location list node, if it
10796 hasn't got one yet. */
10798 static inline void
10799 gen_llsym (dw_loc_list_ref list)
10801 gcc_assert (!list->ll_symbol);
10802 list->ll_symbol = gen_internal_sym ("LLST");
10805 /* Output the location list given to us. */
10807 static void
10808 output_loc_list (dw_loc_list_ref list_head)
10810 dw_loc_list_ref curr = list_head;
10812 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10814 /* Walk the location list, and output each range + expression. */
10815 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10817 unsigned long size;
10818 /* Don't output an entry that starts and ends at the same address. */
10819 if (strcmp (curr->begin, curr->end) == 0)
10820 continue;
10821 if (!have_multiple_function_sections)
10823 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10824 "Location list begin address (%s)",
10825 list_head->ll_symbol);
10826 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10827 "Location list end address (%s)",
10828 list_head->ll_symbol);
10830 else
10832 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10833 "Location list begin address (%s)",
10834 list_head->ll_symbol);
10835 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10836 "Location list end address (%s)",
10837 list_head->ll_symbol);
10839 size = size_of_locs (curr->expr);
10841 /* Output the block length for this list of location operations. */
10842 gcc_assert (size <= 0xffff);
10843 dw2_asm_output_data (2, size, "%s", "Location expression size");
10845 output_loc_sequence (curr->expr);
10848 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10849 "Location list terminator begin (%s)",
10850 list_head->ll_symbol);
10851 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10852 "Location list terminator end (%s)",
10853 list_head->ll_symbol);
10856 /* Output a type signature. */
10858 static inline void
10859 output_signature (const char *sig, const char *name)
10861 int i;
10863 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10864 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10867 /* Output the DIE and its attributes. Called recursively to generate
10868 the definitions of each child DIE. */
10870 static void
10871 output_die (dw_die_ref die)
10873 dw_attr_ref a;
10874 dw_die_ref c;
10875 unsigned long size;
10876 unsigned ix;
10878 /* If someone in another CU might refer to us, set up a symbol for
10879 them to point to. */
10880 if (dwarf_version < 4 && die->die_id.die_symbol)
10881 output_die_symbol (die);
10883 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10884 (unsigned long)die->die_offset,
10885 dwarf_tag_name (die->die_tag));
10887 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10889 const char *name = dwarf_attr_name (a->dw_attr);
10891 switch (AT_class (a))
10893 case dw_val_class_addr:
10894 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10895 break;
10897 case dw_val_class_offset:
10898 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10899 "%s", name);
10900 break;
10902 case dw_val_class_range_list:
10904 char *p = strchr (ranges_section_label, '\0');
10906 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10907 a->dw_attr_val.v.val_offset);
10908 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10909 debug_ranges_section, "%s", name);
10910 *p = '\0';
10912 break;
10914 case dw_val_class_loc:
10915 size = size_of_locs (AT_loc (a));
10917 /* Output the block length for this list of location operations. */
10918 if (dwarf_version >= 4)
10919 dw2_asm_output_data_uleb128 (size, "%s", name);
10920 else
10921 dw2_asm_output_data (constant_size (size), size, "%s", name);
10923 output_loc_sequence (AT_loc (a));
10924 break;
10926 case dw_val_class_const:
10927 /* ??? It would be slightly more efficient to use a scheme like is
10928 used for unsigned constants below, but gdb 4.x does not sign
10929 extend. Gdb 5.x does sign extend. */
10930 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10931 break;
10933 case dw_val_class_unsigned_const:
10934 dw2_asm_output_data (constant_size (AT_unsigned (a)),
10935 AT_unsigned (a), "%s", name);
10936 break;
10938 case dw_val_class_const_double:
10940 unsigned HOST_WIDE_INT first, second;
10942 if (HOST_BITS_PER_WIDE_INT >= 64)
10943 dw2_asm_output_data (1,
10944 2 * HOST_BITS_PER_WIDE_INT
10945 / HOST_BITS_PER_CHAR,
10946 NULL);
10948 if (WORDS_BIG_ENDIAN)
10950 first = a->dw_attr_val.v.val_double.high;
10951 second = a->dw_attr_val.v.val_double.low;
10953 else
10955 first = a->dw_attr_val.v.val_double.low;
10956 second = a->dw_attr_val.v.val_double.high;
10959 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10960 first, name);
10961 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10962 second, NULL);
10964 break;
10966 case dw_val_class_vec:
10968 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10969 unsigned int len = a->dw_attr_val.v.val_vec.length;
10970 unsigned int i;
10971 unsigned char *p;
10973 dw2_asm_output_data (constant_size (len * elt_size),
10974 len * elt_size, "%s", name);
10975 if (elt_size > sizeof (HOST_WIDE_INT))
10977 elt_size /= 2;
10978 len *= 2;
10980 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
10981 i < len;
10982 i++, p += elt_size)
10983 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10984 "fp or vector constant word %u", i);
10985 break;
10988 case dw_val_class_flag:
10989 if (dwarf_version >= 4)
10991 /* Currently all add_AT_flag calls pass in 1 as last argument,
10992 so DW_FORM_flag_present can be used. If that ever changes,
10993 we'll need to use DW_FORM_flag and have some optimization
10994 in build_abbrev_table that will change those to
10995 DW_FORM_flag_present if it is set to 1 in all DIEs using
10996 the same abbrev entry. */
10997 gcc_assert (AT_flag (a) == 1);
10998 if (flag_debug_asm)
10999 fprintf (asm_out_file, "\t\t\t%s %s\n",
11000 ASM_COMMENT_START, name);
11001 break;
11003 dw2_asm_output_data (1, AT_flag (a), "%s", name);
11004 break;
11006 case dw_val_class_loc_list:
11008 char *sym = AT_loc_list (a)->ll_symbol;
11010 gcc_assert (sym);
11011 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11012 "%s", name);
11014 break;
11016 case dw_val_class_die_ref:
11017 if (AT_ref_external (a))
11019 if (dwarf_version >= 4)
11021 comdat_type_node_ref type_node =
11022 AT_ref (a)->die_id.die_type_node;
11024 gcc_assert (type_node);
11025 output_signature (type_node->signature, name);
11027 else
11029 char *sym = AT_ref (a)->die_id.die_symbol;
11030 int size;
11032 gcc_assert (sym);
11033 /* In DWARF2, DW_FORM_ref_addr is sized by target address
11034 length, whereas in DWARF3 it's always sized as an
11035 offset. */
11036 if (dwarf_version == 2)
11037 size = DWARF2_ADDR_SIZE;
11038 else
11039 size = DWARF_OFFSET_SIZE;
11040 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11041 name);
11044 else
11046 gcc_assert (AT_ref (a)->die_offset);
11047 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11048 "%s", name);
11050 break;
11052 case dw_val_class_fde_ref:
11054 char l1[20];
11056 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11057 a->dw_attr_val.v.val_fde_index * 2);
11058 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11059 "%s", name);
11061 break;
11063 case dw_val_class_vms_delta:
11064 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11065 AT_vms_delta2 (a), AT_vms_delta1 (a),
11066 "%s", name);
11067 break;
11069 case dw_val_class_lbl_id:
11070 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11071 break;
11073 case dw_val_class_lineptr:
11074 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11075 debug_line_section, "%s", name);
11076 break;
11078 case dw_val_class_macptr:
11079 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11080 debug_macinfo_section, "%s", name);
11081 break;
11083 case dw_val_class_str:
11084 if (AT_string_form (a) == DW_FORM_strp)
11085 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11086 a->dw_attr_val.v.val_str->label,
11087 debug_str_section,
11088 "%s: \"%s\"", name, AT_string (a));
11089 else
11090 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11091 break;
11093 case dw_val_class_file:
11095 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11097 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11098 a->dw_attr_val.v.val_file->filename);
11099 break;
11102 case dw_val_class_data8:
11104 int i;
11106 for (i = 0; i < 8; i++)
11107 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11108 i == 0 ? "%s" : NULL, name);
11109 break;
11112 default:
11113 gcc_unreachable ();
11117 FOR_EACH_CHILD (die, c, output_die (c));
11119 /* Add null byte to terminate sibling list. */
11120 if (die->die_child != NULL)
11121 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11122 (unsigned long) die->die_offset);
11125 /* Output the compilation unit that appears at the beginning of the
11126 .debug_info section, and precedes the DIE descriptions. */
11128 static void
11129 output_compilation_unit_header (void)
11131 int ver = dwarf_version;
11133 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11134 dw2_asm_output_data (4, 0xffffffff,
11135 "Initial length escape value indicating 64-bit DWARF extension");
11136 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11137 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11138 "Length of Compilation Unit Info");
11139 dw2_asm_output_data (2, ver, "DWARF version number");
11140 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11141 debug_abbrev_section,
11142 "Offset Into Abbrev. Section");
11143 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11146 /* Output the compilation unit DIE and its children. */
11148 static void
11149 output_comp_unit (dw_die_ref die, int output_if_empty)
11151 const char *secname;
11152 char *oldsym, *tmp;
11154 /* Unless we are outputting main CU, we may throw away empty ones. */
11155 if (!output_if_empty && die->die_child == NULL)
11156 return;
11158 /* Even if there are no children of this DIE, we must output the information
11159 about the compilation unit. Otherwise, on an empty translation unit, we
11160 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11161 will then complain when examining the file. First mark all the DIEs in
11162 this CU so we know which get local refs. */
11163 mark_dies (die);
11165 build_abbrev_table (die);
11167 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11168 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11169 calc_die_sizes (die);
11171 oldsym = die->die_id.die_symbol;
11172 if (oldsym)
11174 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11176 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11177 secname = tmp;
11178 die->die_id.die_symbol = NULL;
11179 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11181 else
11182 switch_to_section (debug_info_section);
11184 /* Output debugging information. */
11185 output_compilation_unit_header ();
11186 output_die (die);
11188 /* Leave the marks on the main CU, so we can check them in
11189 output_pubnames. */
11190 if (oldsym)
11192 unmark_dies (die);
11193 die->die_id.die_symbol = oldsym;
11197 /* Output a comdat type unit DIE and its children. */
11199 static void
11200 output_comdat_type_unit (comdat_type_node *node)
11202 const char *secname;
11203 char *tmp;
11204 int i;
11205 #if defined (OBJECT_FORMAT_ELF)
11206 tree comdat_key;
11207 #endif
11209 /* First mark all the DIEs in this CU so we know which get local refs. */
11210 mark_dies (node->root_die);
11212 build_abbrev_table (node->root_die);
11214 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11215 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11216 calc_die_sizes (node->root_die);
11218 #if defined (OBJECT_FORMAT_ELF)
11219 secname = ".debug_types";
11220 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11221 sprintf (tmp, "wt.");
11222 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11223 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11224 comdat_key = get_identifier (tmp);
11225 targetm.asm_out.named_section (secname,
11226 SECTION_DEBUG | SECTION_LINKONCE,
11227 comdat_key);
11228 #else
11229 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11230 sprintf (tmp, ".gnu.linkonce.wt.");
11231 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11232 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11233 secname = tmp;
11234 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11235 #endif
11237 /* Output debugging information. */
11238 output_compilation_unit_header ();
11239 output_signature (node->signature, "Type Signature");
11240 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11241 "Offset to Type DIE");
11242 output_die (node->root_die);
11244 unmark_dies (node->root_die);
11247 /* Return the DWARF2/3 pubname associated with a decl. */
11249 static const char *
11250 dwarf2_name (tree decl, int scope)
11252 if (DECL_NAMELESS (decl))
11253 return NULL;
11254 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11257 /* Add a new entry to .debug_pubnames if appropriate. */
11259 static void
11260 add_pubname_string (const char *str, dw_die_ref die)
11262 if (targetm.want_debug_pub_sections)
11264 pubname_entry e;
11266 e.die = die;
11267 e.name = xstrdup (str);
11268 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11272 static void
11273 add_pubname (tree decl, dw_die_ref die)
11275 if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11277 const char *name = dwarf2_name (decl, 1);
11278 if (name)
11279 add_pubname_string (name, die);
11283 /* Add a new entry to .debug_pubtypes if appropriate. */
11285 static void
11286 add_pubtype (tree decl, dw_die_ref die)
11288 pubname_entry e;
11290 if (!targetm.want_debug_pub_sections)
11291 return;
11293 e.name = NULL;
11294 if ((TREE_PUBLIC (decl)
11295 || die->die_parent == comp_unit_die)
11296 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11298 e.die = die;
11299 if (TYPE_P (decl))
11301 if (TYPE_NAME (decl))
11303 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11304 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11305 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11306 && DECL_NAME (TYPE_NAME (decl)))
11307 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11308 else
11309 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11312 else
11314 e.name = dwarf2_name (decl, 1);
11315 if (e.name)
11316 e.name = xstrdup (e.name);
11319 /* If we don't have a name for the type, there's no point in adding
11320 it to the table. */
11321 if (e.name && e.name[0] != '\0')
11322 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11326 /* Output the public names table used to speed up access to externally
11327 visible names; or the public types table used to find type definitions. */
11329 static void
11330 output_pubnames (VEC (pubname_entry, gc) * names)
11332 unsigned i;
11333 unsigned long pubnames_length = size_of_pubnames (names);
11334 pubname_ref pub;
11336 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11337 dw2_asm_output_data (4, 0xffffffff,
11338 "Initial length escape value indicating 64-bit DWARF extension");
11339 if (names == pubname_table)
11340 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11341 "Length of Public Names Info");
11342 else
11343 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11344 "Length of Public Type Names Info");
11345 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
11346 dw2_asm_output_data (2, 2, "DWARF Version");
11347 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11348 debug_info_section,
11349 "Offset of Compilation Unit Info");
11350 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11351 "Compilation Unit Length");
11353 for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
11355 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11356 if (names == pubname_table)
11357 gcc_assert (pub->die->die_mark);
11359 if (names != pubtype_table
11360 || pub->die->die_offset != 0
11361 || !flag_eliminate_unused_debug_types)
11363 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11364 "DIE offset");
11366 dw2_asm_output_nstring (pub->name, -1, "external name");
11370 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11373 /* Add a new entry to .debug_aranges if appropriate. */
11375 static void
11376 add_arange (tree decl, dw_die_ref die)
11378 if (! DECL_SECTION_NAME (decl))
11379 return;
11381 if (arange_table_in_use == arange_table_allocated)
11383 arange_table_allocated += ARANGE_TABLE_INCREMENT;
11384 arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11385 arange_table_allocated);
11386 memset (arange_table + arange_table_in_use, 0,
11387 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11390 arange_table[arange_table_in_use++] = die;
11393 /* Output the information that goes into the .debug_aranges table.
11394 Namely, define the beginning and ending address range of the
11395 text section generated for this compilation unit. */
11397 static void
11398 output_aranges (void)
11400 unsigned i;
11401 unsigned long aranges_length = size_of_aranges ();
11403 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11404 dw2_asm_output_data (4, 0xffffffff,
11405 "Initial length escape value indicating 64-bit DWARF extension");
11406 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11407 "Length of Address Ranges Info");
11408 /* Version number for aranges is still 2, even in DWARF3. */
11409 dw2_asm_output_data (2, 2, "DWARF Version");
11410 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11411 debug_info_section,
11412 "Offset of Compilation Unit Info");
11413 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11414 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11416 /* We need to align to twice the pointer size here. */
11417 if (DWARF_ARANGES_PAD_SIZE)
11419 /* Pad using a 2 byte words so that padding is correct for any
11420 pointer size. */
11421 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11422 2 * DWARF2_ADDR_SIZE);
11423 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11424 dw2_asm_output_data (2, 0, NULL);
11427 /* It is necessary not to output these entries if the sections were
11428 not used; if the sections were not used, the length will be 0 and
11429 the address may end up as 0 if the section is discarded by ld
11430 --gc-sections, leaving an invalid (0, 0) entry that can be
11431 confused with the terminator. */
11432 if (text_section_used)
11434 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11435 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11436 text_section_label, "Length");
11438 if (cold_text_section_used)
11440 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11441 "Address");
11442 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11443 cold_text_section_label, "Length");
11446 for (i = 0; i < arange_table_in_use; i++)
11448 dw_die_ref die = arange_table[i];
11450 /* We shouldn't see aranges for DIEs outside of the main CU. */
11451 gcc_assert (die->die_mark);
11453 if (die->die_tag == DW_TAG_subprogram)
11455 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11456 "Address");
11457 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11458 get_AT_low_pc (die), "Length");
11460 else
11462 /* A static variable; extract the symbol from DW_AT_location.
11463 Note that this code isn't currently hit, as we only emit
11464 aranges for functions (jason 9/23/99). */
11465 dw_attr_ref a = get_AT (die, DW_AT_location);
11466 dw_loc_descr_ref loc;
11468 gcc_assert (a && AT_class (a) == dw_val_class_loc);
11470 loc = AT_loc (a);
11471 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11473 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11474 loc->dw_loc_oprnd1.v.val_addr, "Address");
11475 dw2_asm_output_data (DWARF2_ADDR_SIZE,
11476 get_AT_unsigned (die, DW_AT_byte_size),
11477 "Length");
11481 /* Output the terminator words. */
11482 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11483 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11486 /* Add a new entry to .debug_ranges. Return the offset at which it
11487 was placed. */
11489 static unsigned int
11490 add_ranges_num (int num)
11492 unsigned int in_use = ranges_table_in_use;
11494 if (in_use == ranges_table_allocated)
11496 ranges_table_allocated += RANGES_TABLE_INCREMENT;
11497 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11498 ranges_table_allocated);
11499 memset (ranges_table + ranges_table_in_use, 0,
11500 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11503 ranges_table[in_use].num = num;
11504 ranges_table_in_use = in_use + 1;
11506 return in_use * 2 * DWARF2_ADDR_SIZE;
11509 /* Add a new entry to .debug_ranges corresponding to a block, or a
11510 range terminator if BLOCK is NULL. */
11512 static unsigned int
11513 add_ranges (const_tree block)
11515 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11518 /* Add a new entry to .debug_ranges corresponding to a pair of
11519 labels. */
11521 static void
11522 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11523 bool *added)
11525 unsigned int in_use = ranges_by_label_in_use;
11526 unsigned int offset;
11528 if (in_use == ranges_by_label_allocated)
11530 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11531 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11532 ranges_by_label,
11533 ranges_by_label_allocated);
11534 memset (ranges_by_label + ranges_by_label_in_use, 0,
11535 RANGES_TABLE_INCREMENT
11536 * sizeof (struct dw_ranges_by_label_struct));
11539 ranges_by_label[in_use].begin = begin;
11540 ranges_by_label[in_use].end = end;
11541 ranges_by_label_in_use = in_use + 1;
11543 offset = add_ranges_num (-(int)in_use - 1);
11544 if (!*added)
11546 add_AT_range_list (die, DW_AT_ranges, offset);
11547 *added = true;
11551 static void
11552 output_ranges (void)
11554 unsigned i;
11555 static const char *const start_fmt = "Offset %#x";
11556 const char *fmt = start_fmt;
11558 for (i = 0; i < ranges_table_in_use; i++)
11560 int block_num = ranges_table[i].num;
11562 if (block_num > 0)
11564 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11565 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11567 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11568 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11570 /* If all code is in the text section, then the compilation
11571 unit base address defaults to DW_AT_low_pc, which is the
11572 base of the text section. */
11573 if (!have_multiple_function_sections)
11575 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11576 text_section_label,
11577 fmt, i * 2 * DWARF2_ADDR_SIZE);
11578 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11579 text_section_label, NULL);
11582 /* Otherwise, the compilation unit base address is zero,
11583 which allows us to use absolute addresses, and not worry
11584 about whether the target supports cross-section
11585 arithmetic. */
11586 else
11588 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11589 fmt, i * 2 * DWARF2_ADDR_SIZE);
11590 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11593 fmt = NULL;
11596 /* Negative block_num stands for an index into ranges_by_label. */
11597 else if (block_num < 0)
11599 int lab_idx = - block_num - 1;
11601 if (!have_multiple_function_sections)
11603 gcc_unreachable ();
11604 #if 0
11605 /* If we ever use add_ranges_by_labels () for a single
11606 function section, all we have to do is to take out
11607 the #if 0 above. */
11608 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11609 ranges_by_label[lab_idx].begin,
11610 text_section_label,
11611 fmt, i * 2 * DWARF2_ADDR_SIZE);
11612 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11613 ranges_by_label[lab_idx].end,
11614 text_section_label, NULL);
11615 #endif
11617 else
11619 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11620 ranges_by_label[lab_idx].begin,
11621 fmt, i * 2 * DWARF2_ADDR_SIZE);
11622 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11623 ranges_by_label[lab_idx].end,
11624 NULL);
11627 else
11629 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11630 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11631 fmt = start_fmt;
11636 /* Data structure containing information about input files. */
11637 struct file_info
11639 const char *path; /* Complete file name. */
11640 const char *fname; /* File name part. */
11641 int length; /* Length of entire string. */
11642 struct dwarf_file_data * file_idx; /* Index in input file table. */
11643 int dir_idx; /* Index in directory table. */
11646 /* Data structure containing information about directories with source
11647 files. */
11648 struct dir_info
11650 const char *path; /* Path including directory name. */
11651 int length; /* Path length. */
11652 int prefix; /* Index of directory entry which is a prefix. */
11653 int count; /* Number of files in this directory. */
11654 int dir_idx; /* Index of directory used as base. */
11657 /* Callback function for file_info comparison. We sort by looking at
11658 the directories in the path. */
11660 static int
11661 file_info_cmp (const void *p1, const void *p2)
11663 const struct file_info *const s1 = (const struct file_info *) p1;
11664 const struct file_info *const s2 = (const struct file_info *) p2;
11665 const unsigned char *cp1;
11666 const unsigned char *cp2;
11668 /* Take care of file names without directories. We need to make sure that
11669 we return consistent values to qsort since some will get confused if
11670 we return the same value when identical operands are passed in opposite
11671 orders. So if neither has a directory, return 0 and otherwise return
11672 1 or -1 depending on which one has the directory. */
11673 if ((s1->path == s1->fname || s2->path == s2->fname))
11674 return (s2->path == s2->fname) - (s1->path == s1->fname);
11676 cp1 = (const unsigned char *) s1->path;
11677 cp2 = (const unsigned char *) s2->path;
11679 while (1)
11681 ++cp1;
11682 ++cp2;
11683 /* Reached the end of the first path? If so, handle like above. */
11684 if ((cp1 == (const unsigned char *) s1->fname)
11685 || (cp2 == (const unsigned char *) s2->fname))
11686 return ((cp2 == (const unsigned char *) s2->fname)
11687 - (cp1 == (const unsigned char *) s1->fname));
11689 /* Character of current path component the same? */
11690 else if (*cp1 != *cp2)
11691 return *cp1 - *cp2;
11695 struct file_name_acquire_data
11697 struct file_info *files;
11698 int used_files;
11699 int max_files;
11702 /* Traversal function for the hash table. */
11704 static int
11705 file_name_acquire (void ** slot, void *data)
11707 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11708 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11709 struct file_info *fi;
11710 const char *f;
11712 gcc_assert (fnad->max_files >= d->emitted_number);
11714 if (! d->emitted_number)
11715 return 1;
11717 gcc_assert (fnad->max_files != fnad->used_files);
11719 fi = fnad->files + fnad->used_files++;
11721 /* Skip all leading "./". */
11722 f = d->filename;
11723 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11724 f += 2;
11726 /* Create a new array entry. */
11727 fi->path = f;
11728 fi->length = strlen (f);
11729 fi->file_idx = d;
11731 /* Search for the file name part. */
11732 f = strrchr (f, DIR_SEPARATOR);
11733 #if defined (DIR_SEPARATOR_2)
11735 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11737 if (g != NULL)
11739 if (f == NULL || f < g)
11740 f = g;
11743 #endif
11745 fi->fname = f == NULL ? fi->path : f + 1;
11746 return 1;
11749 /* Output the directory table and the file name table. We try to minimize
11750 the total amount of memory needed. A heuristic is used to avoid large
11751 slowdowns with many input files. */
11753 static void
11754 output_file_names (void)
11756 struct file_name_acquire_data fnad;
11757 int numfiles;
11758 struct file_info *files;
11759 struct dir_info *dirs;
11760 int *saved;
11761 int *savehere;
11762 int *backmap;
11763 int ndirs;
11764 int idx_offset;
11765 int i;
11767 if (!last_emitted_file)
11769 dw2_asm_output_data (1, 0, "End directory table");
11770 dw2_asm_output_data (1, 0, "End file name table");
11771 return;
11774 numfiles = last_emitted_file->emitted_number;
11776 /* Allocate the various arrays we need. */
11777 files = XALLOCAVEC (struct file_info, numfiles);
11778 dirs = XALLOCAVEC (struct dir_info, numfiles);
11780 fnad.files = files;
11781 fnad.used_files = 0;
11782 fnad.max_files = numfiles;
11783 htab_traverse (file_table, file_name_acquire, &fnad);
11784 gcc_assert (fnad.used_files == fnad.max_files);
11786 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11788 /* Find all the different directories used. */
11789 dirs[0].path = files[0].path;
11790 dirs[0].length = files[0].fname - files[0].path;
11791 dirs[0].prefix = -1;
11792 dirs[0].count = 1;
11793 dirs[0].dir_idx = 0;
11794 files[0].dir_idx = 0;
11795 ndirs = 1;
11797 for (i = 1; i < numfiles; i++)
11798 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11799 && memcmp (dirs[ndirs - 1].path, files[i].path,
11800 dirs[ndirs - 1].length) == 0)
11802 /* Same directory as last entry. */
11803 files[i].dir_idx = ndirs - 1;
11804 ++dirs[ndirs - 1].count;
11806 else
11808 int j;
11810 /* This is a new directory. */
11811 dirs[ndirs].path = files[i].path;
11812 dirs[ndirs].length = files[i].fname - files[i].path;
11813 dirs[ndirs].count = 1;
11814 dirs[ndirs].dir_idx = ndirs;
11815 files[i].dir_idx = ndirs;
11817 /* Search for a prefix. */
11818 dirs[ndirs].prefix = -1;
11819 for (j = 0; j < ndirs; j++)
11820 if (dirs[j].length < dirs[ndirs].length
11821 && dirs[j].length > 1
11822 && (dirs[ndirs].prefix == -1
11823 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11824 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11825 dirs[ndirs].prefix = j;
11827 ++ndirs;
11830 /* Now to the actual work. We have to find a subset of the directories which
11831 allow expressing the file name using references to the directory table
11832 with the least amount of characters. We do not do an exhaustive search
11833 where we would have to check out every combination of every single
11834 possible prefix. Instead we use a heuristic which provides nearly optimal
11835 results in most cases and never is much off. */
11836 saved = XALLOCAVEC (int, ndirs);
11837 savehere = XALLOCAVEC (int, ndirs);
11839 memset (saved, '\0', ndirs * sizeof (saved[0]));
11840 for (i = 0; i < ndirs; i++)
11842 int j;
11843 int total;
11845 /* We can always save some space for the current directory. But this
11846 does not mean it will be enough to justify adding the directory. */
11847 savehere[i] = dirs[i].length;
11848 total = (savehere[i] - saved[i]) * dirs[i].count;
11850 for (j = i + 1; j < ndirs; j++)
11852 savehere[j] = 0;
11853 if (saved[j] < dirs[i].length)
11855 /* Determine whether the dirs[i] path is a prefix of the
11856 dirs[j] path. */
11857 int k;
11859 k = dirs[j].prefix;
11860 while (k != -1 && k != (int) i)
11861 k = dirs[k].prefix;
11863 if (k == (int) i)
11865 /* Yes it is. We can possibly save some memory by
11866 writing the filenames in dirs[j] relative to
11867 dirs[i]. */
11868 savehere[j] = dirs[i].length;
11869 total += (savehere[j] - saved[j]) * dirs[j].count;
11874 /* Check whether we can save enough to justify adding the dirs[i]
11875 directory. */
11876 if (total > dirs[i].length + 1)
11878 /* It's worthwhile adding. */
11879 for (j = i; j < ndirs; j++)
11880 if (savehere[j] > 0)
11882 /* Remember how much we saved for this directory so far. */
11883 saved[j] = savehere[j];
11885 /* Remember the prefix directory. */
11886 dirs[j].dir_idx = i;
11891 /* Emit the directory name table. */
11892 idx_offset = dirs[0].length > 0 ? 1 : 0;
11893 for (i = 1 - idx_offset; i < ndirs; i++)
11894 dw2_asm_output_nstring (dirs[i].path,
11895 dirs[i].length
11896 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11897 "Directory Entry: %#x", i + idx_offset);
11899 dw2_asm_output_data (1, 0, "End directory table");
11901 /* We have to emit them in the order of emitted_number since that's
11902 used in the debug info generation. To do this efficiently we
11903 generate a back-mapping of the indices first. */
11904 backmap = XALLOCAVEC (int, numfiles);
11905 for (i = 0; i < numfiles; i++)
11906 backmap[files[i].file_idx->emitted_number - 1] = i;
11908 /* Now write all the file names. */
11909 for (i = 0; i < numfiles; i++)
11911 int file_idx = backmap[i];
11912 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11914 #ifdef VMS_DEBUGGING_INFO
11915 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11917 /* Setting these fields can lead to debugger miscomparisons,
11918 but VMS Debug requires them to be set correctly. */
11920 int ver;
11921 long long cdt;
11922 long siz;
11923 int maxfilelen = strlen (files[file_idx].path)
11924 + dirs[dir_idx].length
11925 + MAX_VMS_VERSION_LEN + 1;
11926 char *filebuf = XALLOCAVEC (char, maxfilelen);
11928 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11929 snprintf (filebuf, maxfilelen, "%s;%d",
11930 files[file_idx].path + dirs[dir_idx].length, ver);
11932 dw2_asm_output_nstring
11933 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
11935 /* Include directory index. */
11936 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11938 /* Modification time. */
11939 dw2_asm_output_data_uleb128
11940 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
11941 ? cdt : 0,
11942 NULL);
11944 /* File length in bytes. */
11945 dw2_asm_output_data_uleb128
11946 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
11947 ? siz : 0,
11948 NULL);
11949 #else
11950 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
11951 "File Entry: %#x", (unsigned) i + 1);
11953 /* Include directory index. */
11954 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11956 /* Modification time. */
11957 dw2_asm_output_data_uleb128 (0, NULL);
11959 /* File length in bytes. */
11960 dw2_asm_output_data_uleb128 (0, NULL);
11961 #endif
11964 dw2_asm_output_data (1, 0, "End file name table");
11968 /* Output the source line number correspondence information. This
11969 information goes into the .debug_line section. */
11971 static void
11972 output_line_info (void)
11974 char l1[20], l2[20], p1[20], p2[20];
11975 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11976 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11977 unsigned opc;
11978 unsigned n_op_args;
11979 unsigned long lt_index;
11980 unsigned long current_line;
11981 long line_offset;
11982 long line_delta;
11983 unsigned long current_file;
11984 unsigned long function;
11985 int ver = dwarf_version;
11987 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
11988 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
11989 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
11990 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
11992 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11993 dw2_asm_output_data (4, 0xffffffff,
11994 "Initial length escape value indicating 64-bit DWARF extension");
11995 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11996 "Length of Source Line Info");
11997 ASM_OUTPUT_LABEL (asm_out_file, l1);
11999 dw2_asm_output_data (2, ver, "DWARF Version");
12000 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12001 ASM_OUTPUT_LABEL (asm_out_file, p1);
12003 /* Define the architecture-dependent minimum instruction length (in
12004 bytes). In this implementation of DWARF, this field is used for
12005 information purposes only. Since GCC generates assembly language,
12006 we have no a priori knowledge of how many instruction bytes are
12007 generated for each source line, and therefore can use only the
12008 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12009 commands. Accordingly, we fix this as `1', which is "correct
12010 enough" for all architectures, and don't let the target override. */
12011 dw2_asm_output_data (1, 1,
12012 "Minimum Instruction Length");
12014 if (ver >= 4)
12015 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12016 "Maximum Operations Per Instruction");
12017 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12018 "Default is_stmt_start flag");
12019 dw2_asm_output_data (1, DWARF_LINE_BASE,
12020 "Line Base Value (Special Opcodes)");
12021 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12022 "Line Range Value (Special Opcodes)");
12023 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12024 "Special Opcode Base");
12026 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12028 switch (opc)
12030 case DW_LNS_advance_pc:
12031 case DW_LNS_advance_line:
12032 case DW_LNS_set_file:
12033 case DW_LNS_set_column:
12034 case DW_LNS_fixed_advance_pc:
12035 n_op_args = 1;
12036 break;
12037 default:
12038 n_op_args = 0;
12039 break;
12042 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12043 opc, n_op_args);
12046 /* Write out the information about the files we use. */
12047 output_file_names ();
12048 ASM_OUTPUT_LABEL (asm_out_file, p2);
12050 /* We used to set the address register to the first location in the text
12051 section here, but that didn't accomplish anything since we already
12052 have a line note for the opening brace of the first function. */
12054 /* Generate the line number to PC correspondence table, encoded as
12055 a series of state machine operations. */
12056 current_file = 1;
12057 current_line = 1;
12059 if (cfun && in_cold_section_p)
12060 strcpy (prev_line_label, crtl->subsections.cold_section_label);
12061 else
12062 strcpy (prev_line_label, text_section_label);
12063 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12065 dw_line_info_ref line_info = &line_info_table[lt_index];
12067 #if 0
12068 /* Disable this optimization for now; GDB wants to see two line notes
12069 at the beginning of a function so it can find the end of the
12070 prologue. */
12072 /* Don't emit anything for redundant notes. Just updating the
12073 address doesn't accomplish anything, because we already assume
12074 that anything after the last address is this line. */
12075 if (line_info->dw_line_num == current_line
12076 && line_info->dw_file_num == current_file)
12077 continue;
12078 #endif
12080 /* Emit debug info for the address of the current line.
12082 Unfortunately, we have little choice here currently, and must always
12083 use the most general form. GCC does not know the address delta
12084 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
12085 attributes which will give an upper bound on the address range. We
12086 could perhaps use length attributes to determine when it is safe to
12087 use DW_LNS_fixed_advance_pc. */
12089 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12090 if (0)
12092 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
12093 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12094 "DW_LNS_fixed_advance_pc");
12095 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12097 else
12099 /* This can handle any delta. This takes
12100 4+DWARF2_ADDR_SIZE bytes. */
12101 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12102 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12103 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12104 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12107 strcpy (prev_line_label, line_label);
12109 /* Emit debug info for the source file of the current line, if
12110 different from the previous line. */
12111 if (line_info->dw_file_num != current_file)
12113 current_file = line_info->dw_file_num;
12114 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12115 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12118 /* Emit debug info for the current line number, choosing the encoding
12119 that uses the least amount of space. */
12120 if (line_info->dw_line_num != current_line)
12122 line_offset = line_info->dw_line_num - current_line;
12123 line_delta = line_offset - DWARF_LINE_BASE;
12124 current_line = line_info->dw_line_num;
12125 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12126 /* This can handle deltas from -10 to 234, using the current
12127 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
12128 takes 1 byte. */
12129 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12130 "line %lu", current_line);
12131 else
12133 /* This can handle any delta. This takes at least 4 bytes,
12134 depending on the value being encoded. */
12135 dw2_asm_output_data (1, DW_LNS_advance_line,
12136 "advance to line %lu", current_line);
12137 dw2_asm_output_data_sleb128 (line_offset, NULL);
12138 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12141 else
12142 /* We still need to start a new row, so output a copy insn. */
12143 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12146 /* Emit debug info for the address of the end of the function. */
12147 if (0)
12149 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12150 "DW_LNS_fixed_advance_pc");
12151 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12153 else
12155 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12156 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12157 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12158 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12161 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12162 dw2_asm_output_data_uleb128 (1, NULL);
12163 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12165 function = 0;
12166 current_file = 1;
12167 current_line = 1;
12168 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12170 dw_separate_line_info_ref line_info
12171 = &separate_line_info_table[lt_index];
12173 #if 0
12174 /* Don't emit anything for redundant notes. */
12175 if (line_info->dw_line_num == current_line
12176 && line_info->dw_file_num == current_file
12177 && line_info->function == function)
12178 goto cont;
12179 #endif
12181 /* Emit debug info for the address of the current line. If this is
12182 a new function, or the first line of a function, then we need
12183 to handle it differently. */
12184 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12185 lt_index);
12186 if (function != line_info->function)
12188 function = line_info->function;
12190 /* Set the address register to the first line in the function. */
12191 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12192 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12193 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12194 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12196 else
12198 /* ??? See the DW_LNS_advance_pc comment above. */
12199 if (0)
12201 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12202 "DW_LNS_fixed_advance_pc");
12203 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12205 else
12207 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12208 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12209 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12210 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12214 strcpy (prev_line_label, line_label);
12216 /* Emit debug info for the source file of the current line, if
12217 different from the previous line. */
12218 if (line_info->dw_file_num != current_file)
12220 current_file = line_info->dw_file_num;
12221 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12222 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12225 /* Emit debug info for the current line number, choosing the encoding
12226 that uses the least amount of space. */
12227 if (line_info->dw_line_num != current_line)
12229 line_offset = line_info->dw_line_num - current_line;
12230 line_delta = line_offset - DWARF_LINE_BASE;
12231 current_line = line_info->dw_line_num;
12232 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12233 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12234 "line %lu", current_line);
12235 else
12237 dw2_asm_output_data (1, DW_LNS_advance_line,
12238 "advance to line %lu", current_line);
12239 dw2_asm_output_data_sleb128 (line_offset, NULL);
12240 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12243 else
12244 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12246 #if 0
12247 cont:
12248 #endif
12250 lt_index++;
12252 /* If we're done with a function, end its sequence. */
12253 if (lt_index == separate_line_info_table_in_use
12254 || separate_line_info_table[lt_index].function != function)
12256 current_file = 1;
12257 current_line = 1;
12259 /* Emit debug info for the address of the end of the function. */
12260 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12261 if (0)
12263 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12264 "DW_LNS_fixed_advance_pc");
12265 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12267 else
12269 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12270 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12271 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12272 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12275 /* Output the marker for the end of this sequence. */
12276 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12277 dw2_asm_output_data_uleb128 (1, NULL);
12278 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12282 /* Output the marker for the end of the line number info. */
12283 ASM_OUTPUT_LABEL (asm_out_file, l2);
12286 /* Return the size of the .debug_dcall table for the compilation unit. */
12288 static unsigned long
12289 size_of_dcall_table (void)
12291 unsigned long size;
12292 unsigned int i;
12293 dcall_entry *p;
12294 tree last_poc_decl = NULL;
12296 /* Header: version + debug info section pointer + pointer size. */
12297 size = 2 + DWARF_OFFSET_SIZE + 1;
12299 /* Each entry: code label + DIE offset. */
12300 for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
12302 gcc_assert (p->targ_die != NULL);
12303 /* Insert a "from" entry when the point-of-call DIE offset changes. */
12304 if (p->poc_decl != last_poc_decl)
12306 dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12307 gcc_assert (poc_die);
12308 last_poc_decl = p->poc_decl;
12309 if (poc_die)
12310 size += (DWARF_OFFSET_SIZE
12311 + size_of_uleb128 (poc_die->die_offset));
12313 size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12316 return size;
12319 /* Output the direct call table used to disambiguate PC values when
12320 identical function have been merged. */
12322 static void
12323 output_dcall_table (void)
12325 unsigned i;
12326 unsigned long dcall_length = size_of_dcall_table ();
12327 dcall_entry *p;
12328 char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12329 tree last_poc_decl = NULL;
12331 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12332 dw2_asm_output_data (4, 0xffffffff,
12333 "Initial length escape value indicating 64-bit DWARF extension");
12334 dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12335 "Length of Direct Call Table");
12336 dw2_asm_output_data (2, 4, "Version number");
12337 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12338 debug_info_section,
12339 "Offset of Compilation Unit Info");
12340 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12342 for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
12344 /* Insert a "from" entry when the point-of-call DIE offset changes. */
12345 if (p->poc_decl != last_poc_decl)
12347 dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12348 last_poc_decl = p->poc_decl;
12349 if (poc_die)
12351 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12352 dw2_asm_output_data_uleb128 (poc_die->die_offset,
12353 "Caller DIE offset");
12356 ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12357 dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12358 dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12359 "Callee DIE offset");
12363 /* Return the size of the .debug_vcall table for the compilation unit. */
12365 static unsigned long
12366 size_of_vcall_table (void)
12368 unsigned long size;
12369 unsigned int i;
12370 vcall_entry *p;
12372 /* Header: version + pointer size. */
12373 size = 2 + 1;
12375 /* Each entry: code label + vtable slot index. */
12376 for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
12377 size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12379 return size;
12382 /* Output the virtual call table used to disambiguate PC values when
12383 identical function have been merged. */
12385 static void
12386 output_vcall_table (void)
12388 unsigned i;
12389 unsigned long vcall_length = size_of_vcall_table ();
12390 vcall_entry *p;
12391 char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12393 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12394 dw2_asm_output_data (4, 0xffffffff,
12395 "Initial length escape value indicating 64-bit DWARF extension");
12396 dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12397 "Length of Virtual Call Table");
12398 dw2_asm_output_data (2, 4, "Version number");
12399 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12401 for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
12403 ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12404 dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12405 dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12409 /* Given a pointer to a tree node for some base type, return a pointer to
12410 a DIE that describes the given type.
12412 This routine must only be called for GCC type nodes that correspond to
12413 Dwarf base (fundamental) types. */
12415 static dw_die_ref
12416 base_type_die (tree type)
12418 dw_die_ref base_type_result;
12419 enum dwarf_type encoding;
12421 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12422 return 0;
12424 /* If this is a subtype that should not be emitted as a subrange type,
12425 use the base type. See subrange_type_for_debug_p. */
12426 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12427 type = TREE_TYPE (type);
12429 switch (TREE_CODE (type))
12431 case INTEGER_TYPE:
12432 if ((dwarf_version >= 4 || !dwarf_strict)
12433 && TYPE_NAME (type)
12434 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12435 && DECL_IS_BUILTIN (TYPE_NAME (type))
12436 && DECL_NAME (TYPE_NAME (type)))
12438 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12439 if (strcmp (name, "char16_t") == 0
12440 || strcmp (name, "char32_t") == 0)
12442 encoding = DW_ATE_UTF;
12443 break;
12446 if (TYPE_STRING_FLAG (type))
12448 if (TYPE_UNSIGNED (type))
12449 encoding = DW_ATE_unsigned_char;
12450 else
12451 encoding = DW_ATE_signed_char;
12453 else if (TYPE_UNSIGNED (type))
12454 encoding = DW_ATE_unsigned;
12455 else
12456 encoding = DW_ATE_signed;
12457 break;
12459 case REAL_TYPE:
12460 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12462 if (dwarf_version >= 3 || !dwarf_strict)
12463 encoding = DW_ATE_decimal_float;
12464 else
12465 encoding = DW_ATE_lo_user;
12467 else
12468 encoding = DW_ATE_float;
12469 break;
12471 case FIXED_POINT_TYPE:
12472 if (!(dwarf_version >= 3 || !dwarf_strict))
12473 encoding = DW_ATE_lo_user;
12474 else if (TYPE_UNSIGNED (type))
12475 encoding = DW_ATE_unsigned_fixed;
12476 else
12477 encoding = DW_ATE_signed_fixed;
12478 break;
12480 /* Dwarf2 doesn't know anything about complex ints, so use
12481 a user defined type for it. */
12482 case COMPLEX_TYPE:
12483 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12484 encoding = DW_ATE_complex_float;
12485 else
12486 encoding = DW_ATE_lo_user;
12487 break;
12489 case BOOLEAN_TYPE:
12490 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12491 encoding = DW_ATE_boolean;
12492 break;
12494 default:
12495 /* No other TREE_CODEs are Dwarf fundamental types. */
12496 gcc_unreachable ();
12499 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
12501 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12502 int_size_in_bytes (type));
12503 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12505 return base_type_result;
12508 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12509 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12511 static inline int
12512 is_base_type (tree type)
12514 switch (TREE_CODE (type))
12516 case ERROR_MARK:
12517 case VOID_TYPE:
12518 case INTEGER_TYPE:
12519 case REAL_TYPE:
12520 case FIXED_POINT_TYPE:
12521 case COMPLEX_TYPE:
12522 case BOOLEAN_TYPE:
12523 return 1;
12525 case ARRAY_TYPE:
12526 case RECORD_TYPE:
12527 case UNION_TYPE:
12528 case QUAL_UNION_TYPE:
12529 case ENUMERAL_TYPE:
12530 case FUNCTION_TYPE:
12531 case METHOD_TYPE:
12532 case POINTER_TYPE:
12533 case REFERENCE_TYPE:
12534 case OFFSET_TYPE:
12535 case LANG_TYPE:
12536 case VECTOR_TYPE:
12537 return 0;
12539 default:
12540 gcc_unreachable ();
12543 return 0;
12546 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12547 node, return the size in bits for the type if it is a constant, or else
12548 return the alignment for the type if the type's size is not constant, or
12549 else return BITS_PER_WORD if the type actually turns out to be an
12550 ERROR_MARK node. */
12552 static inline unsigned HOST_WIDE_INT
12553 simple_type_size_in_bits (const_tree type)
12555 if (TREE_CODE (type) == ERROR_MARK)
12556 return BITS_PER_WORD;
12557 else if (TYPE_SIZE (type) == NULL_TREE)
12558 return 0;
12559 else if (host_integerp (TYPE_SIZE (type), 1))
12560 return tree_low_cst (TYPE_SIZE (type), 1);
12561 else
12562 return TYPE_ALIGN (type);
12565 /* Similarly, but return a double_int instead of UHWI. */
12567 static inline double_int
12568 double_int_type_size_in_bits (const_tree type)
12570 if (TREE_CODE (type) == ERROR_MARK)
12571 return uhwi_to_double_int (BITS_PER_WORD);
12572 else if (TYPE_SIZE (type) == NULL_TREE)
12573 return double_int_zero;
12574 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12575 return tree_to_double_int (TYPE_SIZE (type));
12576 else
12577 return uhwi_to_double_int (TYPE_ALIGN (type));
12580 /* Given a pointer to a tree node for a subrange type, return a pointer
12581 to a DIE that describes the given type. */
12583 static dw_die_ref
12584 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12586 dw_die_ref subrange_die;
12587 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12589 if (context_die == NULL)
12590 context_die = comp_unit_die;
12592 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12594 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12596 /* The size of the subrange type and its base type do not match,
12597 so we need to generate a size attribute for the subrange type. */
12598 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12601 if (low)
12602 add_bound_info (subrange_die, DW_AT_lower_bound, low);
12603 if (high)
12604 add_bound_info (subrange_die, DW_AT_upper_bound, high);
12606 return subrange_die;
12609 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12610 entry that chains various modifiers in front of the given type. */
12612 static dw_die_ref
12613 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12614 dw_die_ref context_die)
12616 enum tree_code code = TREE_CODE (type);
12617 dw_die_ref mod_type_die;
12618 dw_die_ref sub_die = NULL;
12619 tree item_type = NULL;
12620 tree qualified_type;
12621 tree name, low, high;
12623 if (code == ERROR_MARK)
12624 return NULL;
12626 /* See if we already have the appropriately qualified variant of
12627 this type. */
12628 qualified_type
12629 = get_qualified_type (type,
12630 ((is_const_type ? TYPE_QUAL_CONST : 0)
12631 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12633 if (qualified_type == sizetype
12634 && TYPE_NAME (qualified_type)
12635 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12637 #ifdef ENABLE_CHECKING
12638 gcc_assert (TREE_CODE (TREE_TYPE (TYPE_NAME (qualified_type)))
12639 == INTEGER_TYPE
12640 && TYPE_PRECISION (TREE_TYPE (TYPE_NAME (qualified_type)))
12641 == TYPE_PRECISION (qualified_type)
12642 && TYPE_UNSIGNED (TREE_TYPE (TYPE_NAME (qualified_type)))
12643 == TYPE_UNSIGNED (qualified_type));
12644 #endif
12645 qualified_type = TREE_TYPE (TYPE_NAME (qualified_type));
12648 /* If we do, then we can just use its DIE, if it exists. */
12649 if (qualified_type)
12651 mod_type_die = lookup_type_die (qualified_type);
12652 if (mod_type_die)
12653 return mod_type_die;
12656 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12658 /* Handle C typedef types. */
12659 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12660 && !DECL_ARTIFICIAL (name))
12662 tree dtype = TREE_TYPE (name);
12664 if (qualified_type == dtype)
12666 /* For a named type, use the typedef. */
12667 gen_type_die (qualified_type, context_die);
12668 return lookup_type_die (qualified_type);
12670 else if (is_const_type < TYPE_READONLY (dtype)
12671 || is_volatile_type < TYPE_VOLATILE (dtype)
12672 || (is_const_type <= TYPE_READONLY (dtype)
12673 && is_volatile_type <= TYPE_VOLATILE (dtype)
12674 && DECL_ORIGINAL_TYPE (name) != type))
12675 /* cv-unqualified version of named type. Just use the unnamed
12676 type to which it refers. */
12677 return modified_type_die (DECL_ORIGINAL_TYPE (name),
12678 is_const_type, is_volatile_type,
12679 context_die);
12680 /* Else cv-qualified version of named type; fall through. */
12683 if (is_const_type)
12685 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
12686 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12688 else if (is_volatile_type)
12690 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
12691 sub_die = modified_type_die (type, 0, 0, context_die);
12693 else if (code == POINTER_TYPE)
12695 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
12696 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12697 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12698 item_type = TREE_TYPE (type);
12699 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12700 add_AT_unsigned (mod_type_die, DW_AT_address_class,
12701 TYPE_ADDR_SPACE (item_type));
12703 else if (code == REFERENCE_TYPE)
12705 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12706 mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die,
12707 type);
12708 else
12709 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
12710 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12711 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12712 item_type = TREE_TYPE (type);
12713 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12714 add_AT_unsigned (mod_type_die, DW_AT_address_class,
12715 TYPE_ADDR_SPACE (item_type));
12717 else if (code == INTEGER_TYPE
12718 && TREE_TYPE (type) != NULL_TREE
12719 && subrange_type_for_debug_p (type, &low, &high))
12721 mod_type_die = subrange_type_die (type, low, high, context_die);
12722 item_type = TREE_TYPE (type);
12724 else if (is_base_type (type))
12725 mod_type_die = base_type_die (type);
12726 else
12728 gen_type_die (type, context_die);
12730 /* We have to get the type_main_variant here (and pass that to the
12731 `lookup_type_die' routine) because the ..._TYPE node we have
12732 might simply be a *copy* of some original type node (where the
12733 copy was created to help us keep track of typedef names) and
12734 that copy might have a different TYPE_UID from the original
12735 ..._TYPE node. */
12736 if (TREE_CODE (type) != VECTOR_TYPE)
12737 return lookup_type_die (type_main_variant (type));
12738 else
12739 /* Vectors have the debugging information in the type,
12740 not the main variant. */
12741 return lookup_type_die (type);
12744 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12745 don't output a DW_TAG_typedef, since there isn't one in the
12746 user's program; just attach a DW_AT_name to the type.
12747 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12748 if the base type already has the same name. */
12749 if (name
12750 && ((TREE_CODE (name) != TYPE_DECL
12751 && (qualified_type == TYPE_MAIN_VARIANT (type)
12752 || (!is_const_type && !is_volatile_type)))
12753 || (TREE_CODE (name) == TYPE_DECL
12754 && TREE_TYPE (name) == qualified_type
12755 && DECL_NAME (name))))
12757 if (TREE_CODE (name) == TYPE_DECL)
12758 /* Could just call add_name_and_src_coords_attributes here,
12759 but since this is a builtin type it doesn't have any
12760 useful source coordinates anyway. */
12761 name = DECL_NAME (name);
12762 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12764 /* This probably indicates a bug. */
12765 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12766 add_name_attribute (mod_type_die, "__unknown__");
12768 if (qualified_type)
12769 equate_type_number_to_die (qualified_type, mod_type_die);
12771 if (item_type)
12772 /* We must do this after the equate_type_number_to_die call, in case
12773 this is a recursive type. This ensures that the modified_type_die
12774 recursion will terminate even if the type is recursive. Recursive
12775 types are possible in Ada. */
12776 sub_die = modified_type_die (item_type,
12777 TYPE_READONLY (item_type),
12778 TYPE_VOLATILE (item_type),
12779 context_die);
12781 if (sub_die != NULL)
12782 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12784 return mod_type_die;
12787 /* Generate DIEs for the generic parameters of T.
12788 T must be either a generic type or a generic function.
12789 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12791 static void
12792 gen_generic_params_dies (tree t)
12794 tree parms, args;
12795 int parms_num, i;
12796 dw_die_ref die = NULL;
12798 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12799 return;
12801 if (TYPE_P (t))
12802 die = lookup_type_die (t);
12803 else if (DECL_P (t))
12804 die = lookup_decl_die (t);
12806 gcc_assert (die);
12808 parms = lang_hooks.get_innermost_generic_parms (t);
12809 if (!parms)
12810 /* T has no generic parameter. It means T is neither a generic type
12811 or function. End of story. */
12812 return;
12814 parms_num = TREE_VEC_LENGTH (parms);
12815 args = lang_hooks.get_innermost_generic_args (t);
12816 for (i = 0; i < parms_num; i++)
12818 tree parm, arg, arg_pack_elems;
12820 parm = TREE_VEC_ELT (parms, i);
12821 arg = TREE_VEC_ELT (args, i);
12822 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12823 gcc_assert (parm && TREE_VALUE (parm) && arg);
12825 if (parm && TREE_VALUE (parm) && arg)
12827 /* If PARM represents a template parameter pack,
12828 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12829 by DW_TAG_template_*_parameter DIEs for the argument
12830 pack elements of ARG. Note that ARG would then be
12831 an argument pack. */
12832 if (arg_pack_elems)
12833 template_parameter_pack_die (TREE_VALUE (parm),
12834 arg_pack_elems,
12835 die);
12836 else
12837 generic_parameter_die (TREE_VALUE (parm), arg,
12838 true /* Emit DW_AT_name */, die);
12843 /* Create and return a DIE for PARM which should be
12844 the representation of a generic type parameter.
12845 For instance, in the C++ front end, PARM would be a template parameter.
12846 ARG is the argument to PARM.
12847 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12848 name of the PARM.
12849 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12850 as a child node. */
12852 static dw_die_ref
12853 generic_parameter_die (tree parm, tree arg,
12854 bool emit_name_p,
12855 dw_die_ref parent_die)
12857 dw_die_ref tmpl_die = NULL;
12858 const char *name = NULL;
12860 if (!parm || !DECL_NAME (parm) || !arg)
12861 return NULL;
12863 /* We support non-type generic parameters and arguments,
12864 type generic parameters and arguments, as well as
12865 generic generic parameters (a.k.a. template template parameters in C++)
12866 and arguments. */
12867 if (TREE_CODE (parm) == PARM_DECL)
12868 /* PARM is a nontype generic parameter */
12869 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12870 else if (TREE_CODE (parm) == TYPE_DECL)
12871 /* PARM is a type generic parameter. */
12872 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12873 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12874 /* PARM is a generic generic parameter.
12875 Its DIE is a GNU extension. It shall have a
12876 DW_AT_name attribute to represent the name of the template template
12877 parameter, and a DW_AT_GNU_template_name attribute to represent the
12878 name of the template template argument. */
12879 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12880 parent_die, parm);
12881 else
12882 gcc_unreachable ();
12884 if (tmpl_die)
12886 tree tmpl_type;
12888 /* If PARM is a generic parameter pack, it means we are
12889 emitting debug info for a template argument pack element.
12890 In other terms, ARG is a template argument pack element.
12891 In that case, we don't emit any DW_AT_name attribute for
12892 the die. */
12893 if (emit_name_p)
12895 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12896 gcc_assert (name);
12897 add_AT_string (tmpl_die, DW_AT_name, name);
12900 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12902 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12903 TMPL_DIE should have a child DW_AT_type attribute that is set
12904 to the type of the argument to PARM, which is ARG.
12905 If PARM is a type generic parameter, TMPL_DIE should have a
12906 child DW_AT_type that is set to ARG. */
12907 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12908 add_type_attribute (tmpl_die, tmpl_type, 0,
12909 TREE_THIS_VOLATILE (tmpl_type),
12910 parent_die);
12912 else
12914 /* So TMPL_DIE is a DIE representing a
12915 a generic generic template parameter, a.k.a template template
12916 parameter in C++ and arg is a template. */
12918 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12919 to the name of the argument. */
12920 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12921 if (name)
12922 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12925 if (TREE_CODE (parm) == PARM_DECL)
12926 /* So PARM is a non-type generic parameter.
12927 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12928 attribute of TMPL_DIE which value represents the value
12929 of ARG.
12930 We must be careful here:
12931 The value of ARG might reference some function decls.
12932 We might currently be emitting debug info for a generic
12933 type and types are emitted before function decls, we don't
12934 know if the function decls referenced by ARG will actually be
12935 emitted after cgraph computations.
12936 So must defer the generation of the DW_AT_const_value to
12937 after cgraph is ready. */
12938 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12941 return tmpl_die;
12944 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12945 PARM_PACK must be a template parameter pack. The returned DIE
12946 will be child DIE of PARENT_DIE. */
12948 static dw_die_ref
12949 template_parameter_pack_die (tree parm_pack,
12950 tree parm_pack_args,
12951 dw_die_ref parent_die)
12953 dw_die_ref die;
12954 int j;
12956 gcc_assert (parent_die && parm_pack);
12958 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12959 add_name_and_src_coords_attributes (die, parm_pack);
12960 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12961 generic_parameter_die (parm_pack,
12962 TREE_VEC_ELT (parm_pack_args, j),
12963 false /* Don't emit DW_AT_name */,
12964 die);
12965 return die;
12968 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12969 an enumerated type. */
12971 static inline int
12972 type_is_enum (const_tree type)
12974 return TREE_CODE (type) == ENUMERAL_TYPE;
12977 /* Return the DBX register number described by a given RTL node. */
12979 static unsigned int
12980 dbx_reg_number (const_rtx rtl)
12982 unsigned regno = REGNO (rtl);
12984 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12986 #ifdef LEAF_REG_REMAP
12987 if (current_function_uses_only_leaf_regs)
12989 int leaf_reg = LEAF_REG_REMAP (regno);
12990 if (leaf_reg != -1)
12991 regno = (unsigned) leaf_reg;
12993 #endif
12995 return DBX_REGISTER_NUMBER (regno);
12998 /* Optionally add a DW_OP_piece term to a location description expression.
12999 DW_OP_piece is only added if the location description expression already
13000 doesn't end with DW_OP_piece. */
13002 static void
13003 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13005 dw_loc_descr_ref loc;
13007 if (*list_head != NULL)
13009 /* Find the end of the chain. */
13010 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13013 if (loc->dw_loc_opc != DW_OP_piece)
13014 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13018 /* Return a location descriptor that designates a machine register or
13019 zero if there is none. */
13021 static dw_loc_descr_ref
13022 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13024 rtx regs;
13026 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13027 return 0;
13029 /* We only use "frame base" when we're sure we're talking about the
13030 post-prologue local stack frame. We do this by *not* running
13031 register elimination until this point, and recognizing the special
13032 argument pointer and soft frame pointer rtx's.
13033 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13034 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13035 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13037 dw_loc_descr_ref result = NULL;
13039 if (dwarf_version >= 4 || !dwarf_strict)
13041 result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13042 if (result)
13043 add_loc_descr (&result,
13044 new_loc_descr (DW_OP_stack_value, 0, 0));
13046 return result;
13049 regs = targetm.dwarf_register_span (rtl);
13051 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13052 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13053 else
13054 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13057 /* Return a location descriptor that designates a machine register for
13058 a given hard register number. */
13060 static dw_loc_descr_ref
13061 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13063 dw_loc_descr_ref reg_loc_descr;
13065 if (regno <= 31)
13066 reg_loc_descr
13067 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13068 else
13069 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13071 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13072 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13074 return reg_loc_descr;
13077 /* Given an RTL of a register, return a location descriptor that
13078 designates a value that spans more than one register. */
13080 static dw_loc_descr_ref
13081 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13082 enum var_init_status initialized)
13084 int nregs, size, i;
13085 unsigned reg;
13086 dw_loc_descr_ref loc_result = NULL;
13088 reg = REGNO (rtl);
13089 #ifdef LEAF_REG_REMAP
13090 if (current_function_uses_only_leaf_regs)
13092 int leaf_reg = LEAF_REG_REMAP (reg);
13093 if (leaf_reg != -1)
13094 reg = (unsigned) leaf_reg;
13096 #endif
13097 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13098 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13100 /* Simple, contiguous registers. */
13101 if (regs == NULL_RTX)
13103 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13105 loc_result = NULL;
13106 while (nregs--)
13108 dw_loc_descr_ref t;
13110 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13111 VAR_INIT_STATUS_INITIALIZED);
13112 add_loc_descr (&loc_result, t);
13113 add_loc_descr_op_piece (&loc_result, size);
13114 ++reg;
13116 return loc_result;
13119 /* Now onto stupid register sets in non contiguous locations. */
13121 gcc_assert (GET_CODE (regs) == PARALLEL);
13123 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13124 loc_result = NULL;
13126 for (i = 0; i < XVECLEN (regs, 0); ++i)
13128 dw_loc_descr_ref t;
13130 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13131 VAR_INIT_STATUS_INITIALIZED);
13132 add_loc_descr (&loc_result, t);
13133 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13134 add_loc_descr_op_piece (&loc_result, size);
13137 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13138 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13139 return loc_result;
13142 #endif /* DWARF2_DEBUGGING_INFO */
13144 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
13146 /* Return a location descriptor that designates a constant. */
13148 static dw_loc_descr_ref
13149 int_loc_descriptor (HOST_WIDE_INT i)
13151 enum dwarf_location_atom op;
13153 /* Pick the smallest representation of a constant, rather than just
13154 defaulting to the LEB encoding. */
13155 if (i >= 0)
13157 if (i <= 31)
13158 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13159 else if (i <= 0xff)
13160 op = DW_OP_const1u;
13161 else if (i <= 0xffff)
13162 op = DW_OP_const2u;
13163 else if (HOST_BITS_PER_WIDE_INT == 32
13164 || i <= 0xffffffff)
13165 op = DW_OP_const4u;
13166 else
13167 op = DW_OP_constu;
13169 else
13171 if (i >= -0x80)
13172 op = DW_OP_const1s;
13173 else if (i >= -0x8000)
13174 op = DW_OP_const2s;
13175 else if (HOST_BITS_PER_WIDE_INT == 32
13176 || i >= -0x80000000)
13177 op = DW_OP_const4s;
13178 else
13179 op = DW_OP_consts;
13182 return new_loc_descr (op, i, 0);
13184 #endif
13186 #ifdef DWARF2_DEBUGGING_INFO
13187 /* Return loc description representing "address" of integer value.
13188 This can appear only as toplevel expression. */
13190 static dw_loc_descr_ref
13191 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13193 int litsize;
13194 dw_loc_descr_ref loc_result = NULL;
13196 if (!(dwarf_version >= 4 || !dwarf_strict))
13197 return NULL;
13199 if (i >= 0)
13201 if (i <= 31)
13202 litsize = 1;
13203 else if (i <= 0xff)
13204 litsize = 2;
13205 else if (i <= 0xffff)
13206 litsize = 3;
13207 else if (HOST_BITS_PER_WIDE_INT == 32
13208 || i <= 0xffffffff)
13209 litsize = 5;
13210 else
13211 litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13213 else
13215 if (i >= -0x80)
13216 litsize = 2;
13217 else if (i >= -0x8000)
13218 litsize = 3;
13219 else if (HOST_BITS_PER_WIDE_INT == 32
13220 || i >= -0x80000000)
13221 litsize = 5;
13222 else
13223 litsize = 1 + size_of_sleb128 (i);
13225 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13226 is more compact. For DW_OP_stack_value we need:
13227 litsize + 1 (DW_OP_stack_value)
13228 and for DW_OP_implicit_value:
13229 1 (DW_OP_implicit_value) + 1 (length) + size. */
13230 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13232 loc_result = int_loc_descriptor (i);
13233 add_loc_descr (&loc_result,
13234 new_loc_descr (DW_OP_stack_value, 0, 0));
13235 return loc_result;
13238 loc_result = new_loc_descr (DW_OP_implicit_value,
13239 size, 0);
13240 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13241 loc_result->dw_loc_oprnd2.v.val_int = i;
13242 return loc_result;
13245 /* Return a location descriptor that designates a base+offset location. */
13247 static dw_loc_descr_ref
13248 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13249 enum var_init_status initialized)
13251 unsigned int regno;
13252 dw_loc_descr_ref result;
13253 dw_fde_ref fde = current_fde ();
13255 /* We only use "frame base" when we're sure we're talking about the
13256 post-prologue local stack frame. We do this by *not* running
13257 register elimination until this point, and recognizing the special
13258 argument pointer and soft frame pointer rtx's. */
13259 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13261 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13263 if (elim != reg)
13265 if (GET_CODE (elim) == PLUS)
13267 offset += INTVAL (XEXP (elim, 1));
13268 elim = XEXP (elim, 0);
13270 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13271 && (elim == hard_frame_pointer_rtx
13272 || elim == stack_pointer_rtx))
13273 || elim == (frame_pointer_needed
13274 ? hard_frame_pointer_rtx
13275 : stack_pointer_rtx));
13277 /* If drap register is used to align stack, use frame
13278 pointer + offset to access stack variables. If stack
13279 is aligned without drap, use stack pointer + offset to
13280 access stack variables. */
13281 if (crtl->stack_realign_tried
13282 && reg == frame_pointer_rtx)
13284 int base_reg
13285 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13286 ? HARD_FRAME_POINTER_REGNUM
13287 : STACK_POINTER_REGNUM);
13288 return new_reg_loc_descr (base_reg, offset);
13291 offset += frame_pointer_fb_offset;
13292 return new_loc_descr (DW_OP_fbreg, offset, 0);
13295 else if (!optimize
13296 && fde
13297 && (fde->drap_reg == REGNO (reg)
13298 || fde->vdrap_reg == REGNO (reg)))
13300 /* Use cfa+offset to represent the location of arguments passed
13301 on the stack when drap is used to align stack.
13302 Only do this when not optimizing, for optimized code var-tracking
13303 is supposed to track where the arguments live and the register
13304 used as vdrap or drap in some spot might be used for something
13305 else in other part of the routine. */
13306 return new_loc_descr (DW_OP_fbreg, offset, 0);
13309 regno = dbx_reg_number (reg);
13310 if (regno <= 31)
13311 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13312 offset, 0);
13313 else
13314 result = new_loc_descr (DW_OP_bregx, regno, offset);
13316 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13317 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13319 return result;
13322 /* Return true if this RTL expression describes a base+offset calculation. */
13324 static inline int
13325 is_based_loc (const_rtx rtl)
13327 return (GET_CODE (rtl) == PLUS
13328 && ((REG_P (XEXP (rtl, 0))
13329 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13330 && CONST_INT_P (XEXP (rtl, 1)))));
13333 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13334 failed. */
13336 static dw_loc_descr_ref
13337 tls_mem_loc_descriptor (rtx mem)
13339 tree base;
13340 dw_loc_descr_ref loc_result;
13342 if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13343 return NULL;
13345 base = get_base_address (MEM_EXPR (mem));
13346 if (base == NULL
13347 || TREE_CODE (base) != VAR_DECL
13348 || !DECL_THREAD_LOCAL_P (base))
13349 return NULL;
13351 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13352 if (loc_result == NULL)
13353 return NULL;
13355 if (INTVAL (MEM_OFFSET (mem)))
13356 loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13358 return loc_result;
13361 /* Output debug info about reason why we failed to expand expression as dwarf
13362 expression. */
13364 static void
13365 expansion_failed (tree expr, rtx rtl, char const *reason)
13367 if (dump_file && (dump_flags & TDF_DETAILS))
13369 fprintf (dump_file, "Failed to expand as dwarf: ");
13370 if (expr)
13371 print_generic_expr (dump_file, expr, dump_flags);
13372 if (rtl)
13374 fprintf (dump_file, "\n");
13375 print_rtl (dump_file, rtl);
13377 fprintf (dump_file, "\nReason: %s\n", reason);
13381 /* Helper function for const_ok_for_output, called either directly
13382 or via for_each_rtx. */
13384 static int
13385 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13387 rtx rtl = *rtlp;
13389 if (GET_CODE (rtl) == UNSPEC)
13391 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13392 we can't express it in the debug info. */
13393 #ifdef ENABLE_CHECKING
13394 inform (current_function_decl
13395 ? DECL_SOURCE_LOCATION (current_function_decl)
13396 : UNKNOWN_LOCATION,
13397 "non-delegitimized UNSPEC %d found in variable location",
13398 XINT (rtl, 1));
13399 #endif
13400 expansion_failed (NULL_TREE, rtl,
13401 "UNSPEC hasn't been delegitimized.\n");
13402 return 1;
13405 if (GET_CODE (rtl) != SYMBOL_REF)
13406 return 0;
13408 if (CONSTANT_POOL_ADDRESS_P (rtl))
13410 bool marked;
13411 get_pool_constant_mark (rtl, &marked);
13412 /* If all references to this pool constant were optimized away,
13413 it was not output and thus we can't represent it. */
13414 if (!marked)
13416 expansion_failed (NULL_TREE, rtl,
13417 "Constant was removed from constant pool.\n");
13418 return 1;
13422 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13423 return 1;
13425 /* Avoid references to external symbols in debug info, on several targets
13426 the linker might even refuse to link when linking a shared library,
13427 and in many other cases the relocations for .debug_info/.debug_loc are
13428 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13429 to be defined within the same shared library or executable are fine. */
13430 if (SYMBOL_REF_EXTERNAL_P (rtl))
13432 tree decl = SYMBOL_REF_DECL (rtl);
13434 if (decl == NULL || !targetm.binds_local_p (decl))
13436 expansion_failed (NULL_TREE, rtl,
13437 "Symbol not defined in current TU.\n");
13438 return 1;
13442 return 0;
13445 /* Return true if constant RTL can be emitted in DW_OP_addr or
13446 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13447 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13449 static bool
13450 const_ok_for_output (rtx rtl)
13452 if (GET_CODE (rtl) == SYMBOL_REF)
13453 return const_ok_for_output_1 (&rtl, NULL) == 0;
13455 if (GET_CODE (rtl) == CONST)
13456 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13458 return true;
13461 /* The following routine converts the RTL for a variable or parameter
13462 (resident in memory) into an equivalent Dwarf representation of a
13463 mechanism for getting the address of that same variable onto the top of a
13464 hypothetical "address evaluation" stack.
13466 When creating memory location descriptors, we are effectively transforming
13467 the RTL for a memory-resident object into its Dwarf postfix expression
13468 equivalent. This routine recursively descends an RTL tree, turning
13469 it into Dwarf postfix code as it goes.
13471 MODE is the mode of the memory reference, needed to handle some
13472 autoincrement addressing modes.
13474 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13475 location list for RTL.
13477 Return 0 if we can't represent the location. */
13479 static dw_loc_descr_ref
13480 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13481 enum var_init_status initialized)
13483 dw_loc_descr_ref mem_loc_result = NULL;
13484 enum dwarf_location_atom op;
13485 dw_loc_descr_ref op0, op1;
13487 /* Note that for a dynamically sized array, the location we will generate a
13488 description of here will be the lowest numbered location which is
13489 actually within the array. That's *not* necessarily the same as the
13490 zeroth element of the array. */
13492 rtl = targetm.delegitimize_address (rtl);
13494 switch (GET_CODE (rtl))
13496 case POST_INC:
13497 case POST_DEC:
13498 case POST_MODIFY:
13499 return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13501 case SUBREG:
13502 /* The case of a subreg may arise when we have a local (register)
13503 variable or a formal (register) parameter which doesn't quite fill
13504 up an entire register. For now, just assume that it is
13505 legitimate to make the Dwarf info refer to the whole register which
13506 contains the given subreg. */
13507 if (!subreg_lowpart_p (rtl))
13508 break;
13509 rtl = SUBREG_REG (rtl);
13510 if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13511 break;
13512 if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13513 break;
13514 mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13515 break;
13517 case REG:
13518 /* Whenever a register number forms a part of the description of the
13519 method for calculating the (dynamic) address of a memory resident
13520 object, DWARF rules require the register number be referred to as
13521 a "base register". This distinction is not based in any way upon
13522 what category of register the hardware believes the given register
13523 belongs to. This is strictly DWARF terminology we're dealing with
13524 here. Note that in cases where the location of a memory-resident
13525 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13526 OP_CONST (0)) the actual DWARF location descriptor that we generate
13527 may just be OP_BASEREG (basereg). This may look deceptively like
13528 the object in question was allocated to a register (rather than in
13529 memory) so DWARF consumers need to be aware of the subtle
13530 distinction between OP_REG and OP_BASEREG. */
13531 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13532 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13533 else if (stack_realign_drap
13534 && crtl->drap_reg
13535 && crtl->args.internal_arg_pointer == rtl
13536 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13538 /* If RTL is internal_arg_pointer, which has been optimized
13539 out, use DRAP instead. */
13540 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13541 VAR_INIT_STATUS_INITIALIZED);
13543 break;
13545 case SIGN_EXTEND:
13546 case ZERO_EXTEND:
13547 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13548 VAR_INIT_STATUS_INITIALIZED);
13549 if (op0 == 0)
13550 break;
13551 else
13553 int shift = DWARF2_ADDR_SIZE
13554 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13555 shift *= BITS_PER_UNIT;
13556 if (GET_CODE (rtl) == SIGN_EXTEND)
13557 op = DW_OP_shra;
13558 else
13559 op = DW_OP_shr;
13560 mem_loc_result = op0;
13561 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13562 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13563 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13564 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13566 break;
13568 case MEM:
13569 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13570 VAR_INIT_STATUS_INITIALIZED);
13571 if (mem_loc_result == NULL)
13572 mem_loc_result = tls_mem_loc_descriptor (rtl);
13573 if (mem_loc_result != 0)
13575 if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13577 expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13578 return 0;
13580 else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13581 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13582 else
13583 add_loc_descr (&mem_loc_result,
13584 new_loc_descr (DW_OP_deref_size,
13585 GET_MODE_SIZE (GET_MODE (rtl)), 0));
13587 else
13589 rtx new_rtl = avoid_constant_pool_reference (rtl);
13590 if (new_rtl != rtl)
13591 return mem_loc_descriptor (new_rtl, mode, initialized);
13593 break;
13595 case LO_SUM:
13596 rtl = XEXP (rtl, 1);
13598 /* ... fall through ... */
13600 case LABEL_REF:
13601 /* Some ports can transform a symbol ref into a label ref, because
13602 the symbol ref is too far away and has to be dumped into a constant
13603 pool. */
13604 case CONST:
13605 case SYMBOL_REF:
13606 if (GET_CODE (rtl) == SYMBOL_REF
13607 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13609 dw_loc_descr_ref temp;
13611 /* If this is not defined, we have no way to emit the data. */
13612 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13613 break;
13615 /* We used to emit DW_OP_addr here, but that's wrong, since
13616 DW_OP_addr should be relocated by the debug info consumer,
13617 while DW_OP_GNU_push_tls_address operand should not. */
13618 temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13619 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13620 temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13621 temp->dw_loc_oprnd1.v.val_addr = rtl;
13622 temp->dtprel = true;
13624 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13625 add_loc_descr (&mem_loc_result, temp);
13627 break;
13630 if (!const_ok_for_output (rtl))
13631 break;
13633 symref:
13634 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13635 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13636 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13637 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13638 break;
13640 case CONCAT:
13641 case CONCATN:
13642 case VAR_LOCATION:
13643 expansion_failed (NULL_TREE, rtl,
13644 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13645 return 0;
13647 case PRE_MODIFY:
13648 /* Extract the PLUS expression nested inside and fall into
13649 PLUS code below. */
13650 rtl = XEXP (rtl, 1);
13651 goto plus;
13653 case PRE_INC:
13654 case PRE_DEC:
13655 /* Turn these into a PLUS expression and fall into the PLUS code
13656 below. */
13657 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13658 GEN_INT (GET_CODE (rtl) == PRE_INC
13659 ? GET_MODE_UNIT_SIZE (mode)
13660 : -GET_MODE_UNIT_SIZE (mode)));
13662 /* ... fall through ... */
13664 case PLUS:
13665 plus:
13666 if (is_based_loc (rtl))
13667 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13668 INTVAL (XEXP (rtl, 1)),
13669 VAR_INIT_STATUS_INITIALIZED);
13670 else
13672 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13673 VAR_INIT_STATUS_INITIALIZED);
13674 if (mem_loc_result == 0)
13675 break;
13677 if (CONST_INT_P (XEXP (rtl, 1)))
13678 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13679 else
13681 dw_loc_descr_ref mem_loc_result2
13682 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13683 VAR_INIT_STATUS_INITIALIZED);
13684 if (mem_loc_result2 == 0)
13685 break;
13686 add_loc_descr (&mem_loc_result, mem_loc_result2);
13687 add_loc_descr (&mem_loc_result,
13688 new_loc_descr (DW_OP_plus, 0, 0));
13691 break;
13693 /* If a pseudo-reg is optimized away, it is possible for it to
13694 be replaced with a MEM containing a multiply or shift. */
13695 case MINUS:
13696 op = DW_OP_minus;
13697 goto do_binop;
13699 case MULT:
13700 op = DW_OP_mul;
13701 goto do_binop;
13703 case DIV:
13704 op = DW_OP_div;
13705 goto do_binop;
13707 case UMOD:
13708 op = DW_OP_mod;
13709 goto do_binop;
13711 case ASHIFT:
13712 op = DW_OP_shl;
13713 goto do_binop;
13715 case ASHIFTRT:
13716 op = DW_OP_shra;
13717 goto do_binop;
13719 case LSHIFTRT:
13720 op = DW_OP_shr;
13721 goto do_binop;
13723 case AND:
13724 op = DW_OP_and;
13725 goto do_binop;
13727 case IOR:
13728 op = DW_OP_or;
13729 goto do_binop;
13731 case XOR:
13732 op = DW_OP_xor;
13733 goto do_binop;
13735 do_binop:
13736 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13737 VAR_INIT_STATUS_INITIALIZED);
13738 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13739 VAR_INIT_STATUS_INITIALIZED);
13741 if (op0 == 0 || op1 == 0)
13742 break;
13744 mem_loc_result = op0;
13745 add_loc_descr (&mem_loc_result, op1);
13746 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13747 break;
13749 case MOD:
13750 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13751 VAR_INIT_STATUS_INITIALIZED);
13752 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13753 VAR_INIT_STATUS_INITIALIZED);
13755 if (op0 == 0 || op1 == 0)
13756 break;
13758 mem_loc_result = op0;
13759 add_loc_descr (&mem_loc_result, op1);
13760 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13761 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13762 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13763 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13764 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13765 break;
13767 case NOT:
13768 op = DW_OP_not;
13769 goto do_unop;
13771 case ABS:
13772 op = DW_OP_abs;
13773 goto do_unop;
13775 case NEG:
13776 op = DW_OP_neg;
13777 goto do_unop;
13779 do_unop:
13780 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13781 VAR_INIT_STATUS_INITIALIZED);
13783 if (op0 == 0)
13784 break;
13786 mem_loc_result = op0;
13787 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13788 break;
13790 case CONST_INT:
13791 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13792 break;
13794 case EQ:
13795 op = DW_OP_eq;
13796 goto do_scompare;
13798 case GE:
13799 op = DW_OP_ge;
13800 goto do_scompare;
13802 case GT:
13803 op = DW_OP_gt;
13804 goto do_scompare;
13806 case LE:
13807 op = DW_OP_le;
13808 goto do_scompare;
13810 case LT:
13811 op = DW_OP_lt;
13812 goto do_scompare;
13814 case NE:
13815 op = DW_OP_ne;
13816 goto do_scompare;
13818 do_scompare:
13819 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13820 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13821 break;
13822 else
13824 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13826 if (op_mode == VOIDmode)
13827 op_mode = GET_MODE (XEXP (rtl, 1));
13828 if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13829 break;
13831 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13832 VAR_INIT_STATUS_INITIALIZED);
13833 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13834 VAR_INIT_STATUS_INITIALIZED);
13836 if (op0 == 0 || op1 == 0)
13837 break;
13839 if (op_mode != VOIDmode
13840 && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13842 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
13843 shift *= BITS_PER_UNIT;
13844 /* For eq/ne, if the operands are known to be zero-extended,
13845 there is no need to do the fancy shifting up. */
13846 if (op == DW_OP_eq || op == DW_OP_ne)
13848 dw_loc_descr_ref last0, last1;
13849 for (last0 = op0;
13850 last0->dw_loc_next != NULL;
13851 last0 = last0->dw_loc_next)
13853 for (last1 = op1;
13854 last1->dw_loc_next != NULL;
13855 last1 = last1->dw_loc_next)
13857 /* deref_size zero extends, and for constants we can check
13858 whether they are zero extended or not. */
13859 if (((last0->dw_loc_opc == DW_OP_deref_size
13860 && last0->dw_loc_oprnd1.v.val_int
13861 <= GET_MODE_SIZE (op_mode))
13862 || (CONST_INT_P (XEXP (rtl, 0))
13863 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13864 == (INTVAL (XEXP (rtl, 0))
13865 & GET_MODE_MASK (op_mode))))
13866 && ((last1->dw_loc_opc == DW_OP_deref_size
13867 && last1->dw_loc_oprnd1.v.val_int
13868 <= GET_MODE_SIZE (op_mode))
13869 || (CONST_INT_P (XEXP (rtl, 1))
13870 && (unsigned HOST_WIDE_INT)
13871 INTVAL (XEXP (rtl, 1))
13872 == (INTVAL (XEXP (rtl, 1))
13873 & GET_MODE_MASK (op_mode)))))
13874 goto do_compare;
13876 add_loc_descr (&op0, int_loc_descriptor (shift));
13877 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13878 if (CONST_INT_P (XEXP (rtl, 1)))
13879 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13880 else
13882 add_loc_descr (&op1, int_loc_descriptor (shift));
13883 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13888 do_compare:
13889 mem_loc_result = op0;
13890 add_loc_descr (&mem_loc_result, op1);
13891 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13892 if (STORE_FLAG_VALUE != 1)
13894 add_loc_descr (&mem_loc_result,
13895 int_loc_descriptor (STORE_FLAG_VALUE));
13896 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13898 break;
13900 case GEU:
13901 op = DW_OP_ge;
13902 goto do_ucompare;
13904 case GTU:
13905 op = DW_OP_gt;
13906 goto do_ucompare;
13908 case LEU:
13909 op = DW_OP_le;
13910 goto do_ucompare;
13912 case LTU:
13913 op = DW_OP_lt;
13914 goto do_ucompare;
13916 do_ucompare:
13917 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13918 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13919 break;
13920 else
13922 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13924 if (op_mode == VOIDmode)
13925 op_mode = GET_MODE (XEXP (rtl, 1));
13926 if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13927 break;
13929 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13930 VAR_INIT_STATUS_INITIALIZED);
13931 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13932 VAR_INIT_STATUS_INITIALIZED);
13934 if (op0 == 0 || op1 == 0)
13935 break;
13937 if (op_mode != VOIDmode
13938 && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13940 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13941 dw_loc_descr_ref last0, last1;
13942 for (last0 = op0;
13943 last0->dw_loc_next != NULL;
13944 last0 = last0->dw_loc_next)
13946 for (last1 = op1;
13947 last1->dw_loc_next != NULL;
13948 last1 = last1->dw_loc_next)
13950 if (CONST_INT_P (XEXP (rtl, 0)))
13951 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13952 /* deref_size zero extends, so no need to mask it again. */
13953 else if (last0->dw_loc_opc != DW_OP_deref_size
13954 || last0->dw_loc_oprnd1.v.val_int
13955 > GET_MODE_SIZE (op_mode))
13957 add_loc_descr (&op0, int_loc_descriptor (mask));
13958 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13960 if (CONST_INT_P (XEXP (rtl, 1)))
13961 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13962 /* deref_size zero extends, so no need to mask it again. */
13963 else if (last1->dw_loc_opc != DW_OP_deref_size
13964 || last1->dw_loc_oprnd1.v.val_int
13965 > GET_MODE_SIZE (op_mode))
13967 add_loc_descr (&op1, int_loc_descriptor (mask));
13968 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13971 else
13973 HOST_WIDE_INT bias = 1;
13974 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13975 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13976 if (CONST_INT_P (XEXP (rtl, 1)))
13977 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13978 + INTVAL (XEXP (rtl, 1)));
13979 else
13980 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
13981 bias, 0));
13984 goto do_compare;
13986 case SMIN:
13987 case SMAX:
13988 case UMIN:
13989 case UMAX:
13990 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
13991 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13992 || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
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 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14004 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14005 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14006 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14008 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14010 HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14011 add_loc_descr (&op0, int_loc_descriptor (mask));
14012 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14013 add_loc_descr (&op1, int_loc_descriptor (mask));
14014 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14016 else
14018 HOST_WIDE_INT bias = 1;
14019 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14020 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14021 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14024 else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14026 int shift = DWARF2_ADDR_SIZE
14027 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14028 shift *= BITS_PER_UNIT;
14029 add_loc_descr (&op0, int_loc_descriptor (shift));
14030 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14031 add_loc_descr (&op1, int_loc_descriptor (shift));
14032 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14035 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14036 op = DW_OP_lt;
14037 else
14038 op = DW_OP_gt;
14039 mem_loc_result = op0;
14040 add_loc_descr (&mem_loc_result, op1);
14041 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14043 dw_loc_descr_ref bra_node, drop_node;
14045 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14046 add_loc_descr (&mem_loc_result, bra_node);
14047 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14048 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14049 add_loc_descr (&mem_loc_result, drop_node);
14050 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14051 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14053 break;
14055 case ZERO_EXTRACT:
14056 case SIGN_EXTRACT:
14057 if (CONST_INT_P (XEXP (rtl, 1))
14058 && CONST_INT_P (XEXP (rtl, 2))
14059 && ((unsigned) INTVAL (XEXP (rtl, 1))
14060 + (unsigned) INTVAL (XEXP (rtl, 2))
14061 <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14062 && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14063 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14065 int shift, size;
14066 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14067 VAR_INIT_STATUS_INITIALIZED);
14068 if (op0 == 0)
14069 break;
14070 if (GET_CODE (rtl) == SIGN_EXTRACT)
14071 op = DW_OP_shra;
14072 else
14073 op = DW_OP_shr;
14074 mem_loc_result = op0;
14075 size = INTVAL (XEXP (rtl, 1));
14076 shift = INTVAL (XEXP (rtl, 2));
14077 if (BITS_BIG_ENDIAN)
14078 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14079 - shift - size;
14080 if (shift + size != (int) DWARF2_ADDR_SIZE)
14082 add_loc_descr (&mem_loc_result,
14083 int_loc_descriptor (DWARF2_ADDR_SIZE
14084 - shift - size));
14085 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14087 if (size != (int) DWARF2_ADDR_SIZE)
14089 add_loc_descr (&mem_loc_result,
14090 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14091 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14094 break;
14096 case COMPARE:
14097 case IF_THEN_ELSE:
14098 case ROTATE:
14099 case ROTATERT:
14100 case TRUNCATE:
14101 /* In theory, we could implement the above. */
14102 /* DWARF cannot represent the unsigned compare operations
14103 natively. */
14104 case SS_MULT:
14105 case US_MULT:
14106 case SS_DIV:
14107 case US_DIV:
14108 case SS_PLUS:
14109 case US_PLUS:
14110 case SS_MINUS:
14111 case US_MINUS:
14112 case SS_NEG:
14113 case US_NEG:
14114 case SS_ABS:
14115 case SS_ASHIFT:
14116 case US_ASHIFT:
14117 case SS_TRUNCATE:
14118 case US_TRUNCATE:
14119 case UDIV:
14120 case UNORDERED:
14121 case ORDERED:
14122 case UNEQ:
14123 case UNGE:
14124 case UNGT:
14125 case UNLE:
14126 case UNLT:
14127 case LTGT:
14128 case FLOAT_EXTEND:
14129 case FLOAT_TRUNCATE:
14130 case FLOAT:
14131 case UNSIGNED_FLOAT:
14132 case FIX:
14133 case UNSIGNED_FIX:
14134 case FRACT_CONVERT:
14135 case UNSIGNED_FRACT_CONVERT:
14136 case SAT_FRACT:
14137 case UNSIGNED_SAT_FRACT:
14138 case SQRT:
14139 case BSWAP:
14140 case FFS:
14141 case CLZ:
14142 case CTZ:
14143 case POPCOUNT:
14144 case PARITY:
14145 case ASM_OPERANDS:
14146 case VEC_MERGE:
14147 case VEC_SELECT:
14148 case VEC_CONCAT:
14149 case VEC_DUPLICATE:
14150 case UNSPEC:
14151 case HIGH:
14152 /* If delegitimize_address couldn't do anything with the UNSPEC, we
14153 can't express it in the debug info. This can happen e.g. with some
14154 TLS UNSPECs. */
14155 break;
14157 case CONST_STRING:
14158 resolve_one_addr (&rtl, NULL);
14159 goto symref;
14161 default:
14162 #ifdef ENABLE_CHECKING
14163 print_rtl (stderr, rtl);
14164 gcc_unreachable ();
14165 #else
14166 break;
14167 #endif
14170 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14171 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14173 return mem_loc_result;
14176 /* Return a descriptor that describes the concatenation of two locations.
14177 This is typically a complex variable. */
14179 static dw_loc_descr_ref
14180 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14182 dw_loc_descr_ref cc_loc_result = NULL;
14183 dw_loc_descr_ref x0_ref
14184 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14185 dw_loc_descr_ref x1_ref
14186 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14188 if (x0_ref == 0 || x1_ref == 0)
14189 return 0;
14191 cc_loc_result = x0_ref;
14192 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14194 add_loc_descr (&cc_loc_result, x1_ref);
14195 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14197 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14198 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14200 return cc_loc_result;
14203 /* Return a descriptor that describes the concatenation of N
14204 locations. */
14206 static dw_loc_descr_ref
14207 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14209 unsigned int i;
14210 dw_loc_descr_ref cc_loc_result = NULL;
14211 unsigned int n = XVECLEN (concatn, 0);
14213 for (i = 0; i < n; ++i)
14215 dw_loc_descr_ref ref;
14216 rtx x = XVECEXP (concatn, 0, i);
14218 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14219 if (ref == NULL)
14220 return NULL;
14222 add_loc_descr (&cc_loc_result, ref);
14223 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14226 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14227 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14229 return cc_loc_result;
14232 /* Output a proper Dwarf location descriptor for a variable or parameter
14233 which is either allocated in a register or in a memory location. For a
14234 register, we just generate an OP_REG and the register number. For a
14235 memory location we provide a Dwarf postfix expression describing how to
14236 generate the (dynamic) address of the object onto the address stack.
14238 MODE is mode of the decl if this loc_descriptor is going to be used in
14239 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14240 allowed, VOIDmode otherwise.
14242 If we don't know how to describe it, return 0. */
14244 static dw_loc_descr_ref
14245 loc_descriptor (rtx rtl, enum machine_mode mode,
14246 enum var_init_status initialized)
14248 dw_loc_descr_ref loc_result = NULL;
14250 switch (GET_CODE (rtl))
14252 case SUBREG:
14253 /* The case of a subreg may arise when we have a local (register)
14254 variable or a formal (register) parameter which doesn't quite fill
14255 up an entire register. For now, just assume that it is
14256 legitimate to make the Dwarf info refer to the whole register which
14257 contains the given subreg. */
14258 loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14259 break;
14261 case REG:
14262 loc_result = reg_loc_descriptor (rtl, initialized);
14263 break;
14265 case MEM:
14266 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14267 initialized);
14268 if (loc_result == NULL)
14269 loc_result = tls_mem_loc_descriptor (rtl);
14270 if (loc_result == NULL)
14272 rtx new_rtl = avoid_constant_pool_reference (rtl);
14273 if (new_rtl != rtl)
14274 loc_result = loc_descriptor (new_rtl, mode, initialized);
14276 break;
14278 case CONCAT:
14279 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14280 initialized);
14281 break;
14283 case CONCATN:
14284 loc_result = concatn_loc_descriptor (rtl, initialized);
14285 break;
14287 case VAR_LOCATION:
14288 /* Single part. */
14289 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14291 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14292 if (GET_CODE (loc) == EXPR_LIST)
14293 loc = XEXP (loc, 0);
14294 loc_result = loc_descriptor (loc, mode, initialized);
14295 break;
14298 rtl = XEXP (rtl, 1);
14299 /* FALLTHRU */
14301 case PARALLEL:
14303 rtvec par_elems = XVEC (rtl, 0);
14304 int num_elem = GET_NUM_ELEM (par_elems);
14305 enum machine_mode mode;
14306 int i;
14308 /* Create the first one, so we have something to add to. */
14309 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14310 VOIDmode, initialized);
14311 if (loc_result == NULL)
14312 return NULL;
14313 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14314 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14315 for (i = 1; i < num_elem; i++)
14317 dw_loc_descr_ref temp;
14319 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14320 VOIDmode, initialized);
14321 if (temp == NULL)
14322 return NULL;
14323 add_loc_descr (&loc_result, temp);
14324 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14325 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14328 break;
14330 case CONST_INT:
14331 if (mode != VOIDmode && mode != BLKmode)
14332 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14333 INTVAL (rtl));
14334 break;
14336 case CONST_DOUBLE:
14337 if (mode == VOIDmode)
14338 mode = GET_MODE (rtl);
14340 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14342 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14344 /* Note that a CONST_DOUBLE rtx could represent either an integer
14345 or a floating-point constant. A CONST_DOUBLE is used whenever
14346 the constant requires more than one word in order to be
14347 adequately represented. We output CONST_DOUBLEs as blocks. */
14348 loc_result = new_loc_descr (DW_OP_implicit_value,
14349 GET_MODE_SIZE (mode), 0);
14350 if (SCALAR_FLOAT_MODE_P (mode))
14352 unsigned int length = GET_MODE_SIZE (mode);
14353 unsigned char *array
14354 = (unsigned char*) ggc_alloc_atomic (length);
14356 insert_float (rtl, array);
14357 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14358 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14359 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14360 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14362 else
14364 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14365 loc_result->dw_loc_oprnd2.v.val_double
14366 = rtx_to_double_int (rtl);
14369 break;
14371 case CONST_VECTOR:
14372 if (mode == VOIDmode)
14373 mode = GET_MODE (rtl);
14375 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14377 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14378 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14379 unsigned char *array = (unsigned char *)
14380 ggc_alloc_atomic (length * elt_size);
14381 unsigned int i;
14382 unsigned char *p;
14384 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14385 switch (GET_MODE_CLASS (mode))
14387 case MODE_VECTOR_INT:
14388 for (i = 0, p = array; i < length; i++, p += elt_size)
14390 rtx elt = CONST_VECTOR_ELT (rtl, i);
14391 double_int val = rtx_to_double_int (elt);
14393 if (elt_size <= sizeof (HOST_WIDE_INT))
14394 insert_int (double_int_to_shwi (val), elt_size, p);
14395 else
14397 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14398 insert_double (val, p);
14401 break;
14403 case MODE_VECTOR_FLOAT:
14404 for (i = 0, p = array; i < length; i++, p += elt_size)
14406 rtx elt = CONST_VECTOR_ELT (rtl, i);
14407 insert_float (elt, p);
14409 break;
14411 default:
14412 gcc_unreachable ();
14415 loc_result = new_loc_descr (DW_OP_implicit_value,
14416 length * elt_size, 0);
14417 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14418 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14419 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14420 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14422 break;
14424 case CONST:
14425 if (mode == VOIDmode
14426 || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14427 || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14428 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14430 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14431 break;
14433 /* FALLTHROUGH */
14434 case SYMBOL_REF:
14435 if (!const_ok_for_output (rtl))
14436 break;
14437 case LABEL_REF:
14438 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14439 && (dwarf_version >= 4 || !dwarf_strict))
14441 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14442 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14443 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14444 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14445 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14447 break;
14449 default:
14450 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14451 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14452 && (dwarf_version >= 4 || !dwarf_strict))
14454 /* Value expression. */
14455 loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14456 if (loc_result)
14457 add_loc_descr (&loc_result,
14458 new_loc_descr (DW_OP_stack_value, 0, 0));
14460 break;
14463 return loc_result;
14466 /* We need to figure out what section we should use as the base for the
14467 address ranges where a given location is valid.
14468 1. If this particular DECL has a section associated with it, use that.
14469 2. If this function has a section associated with it, use that.
14470 3. Otherwise, use the text section.
14471 XXX: If you split a variable across multiple sections, we won't notice. */
14473 static const char *
14474 secname_for_decl (const_tree decl)
14476 const char *secname;
14478 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14480 tree sectree = DECL_SECTION_NAME (decl);
14481 secname = TREE_STRING_POINTER (sectree);
14483 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14485 tree sectree = DECL_SECTION_NAME (current_function_decl);
14486 secname = TREE_STRING_POINTER (sectree);
14488 else if (cfun && in_cold_section_p)
14489 secname = crtl->subsections.cold_section_label;
14490 else
14491 secname = text_section_label;
14493 return secname;
14496 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
14498 static bool
14499 decl_by_reference_p (tree decl)
14501 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14502 || TREE_CODE (decl) == VAR_DECL)
14503 && DECL_BY_REFERENCE (decl));
14506 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14507 for VARLOC. */
14509 static dw_loc_descr_ref
14510 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14511 enum var_init_status initialized)
14513 int have_address = 0;
14514 dw_loc_descr_ref descr;
14515 enum machine_mode mode;
14517 if (want_address != 2)
14519 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14520 /* Single part. */
14521 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14523 varloc = PAT_VAR_LOCATION_LOC (varloc);
14524 if (GET_CODE (varloc) == EXPR_LIST)
14525 varloc = XEXP (varloc, 0);
14526 mode = GET_MODE (varloc);
14527 if (MEM_P (varloc))
14529 rtx addr = XEXP (varloc, 0);
14530 descr = mem_loc_descriptor (addr, mode, initialized);
14531 if (descr)
14532 have_address = 1;
14533 else
14535 rtx x = avoid_constant_pool_reference (varloc);
14536 if (x != varloc)
14537 descr = mem_loc_descriptor (x, mode, initialized);
14540 else
14541 descr = mem_loc_descriptor (varloc, mode, initialized);
14543 else
14544 return 0;
14546 else
14548 if (GET_CODE (varloc) == VAR_LOCATION)
14549 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14550 else
14551 mode = DECL_MODE (loc);
14552 descr = loc_descriptor (varloc, mode, initialized);
14553 have_address = 1;
14556 if (!descr)
14557 return 0;
14559 if (want_address == 2 && !have_address
14560 && (dwarf_version >= 4 || !dwarf_strict))
14562 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14564 expansion_failed (loc, NULL_RTX,
14565 "DWARF address size mismatch");
14566 return 0;
14568 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14569 have_address = 1;
14571 /* Show if we can't fill the request for an address. */
14572 if (want_address && !have_address)
14574 expansion_failed (loc, NULL_RTX,
14575 "Want address and only have value");
14576 return 0;
14579 /* If we've got an address and don't want one, dereference. */
14580 if (!want_address && have_address)
14582 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14583 enum dwarf_location_atom op;
14585 if (size > DWARF2_ADDR_SIZE || size == -1)
14587 expansion_failed (loc, NULL_RTX,
14588 "DWARF address size mismatch");
14589 return 0;
14591 else if (size == DWARF2_ADDR_SIZE)
14592 op = DW_OP_deref;
14593 else
14594 op = DW_OP_deref_size;
14596 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14599 return descr;
14602 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14603 if it is not possible. */
14605 static dw_loc_descr_ref
14606 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14608 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14609 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14610 else if (dwarf_version >= 3 || !dwarf_strict)
14611 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14612 else
14613 return NULL;
14616 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14617 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14619 static dw_loc_descr_ref
14620 dw_sra_loc_expr (tree decl, rtx loc)
14622 rtx p;
14623 unsigned int padsize = 0;
14624 dw_loc_descr_ref descr, *descr_tail;
14625 unsigned HOST_WIDE_INT decl_size;
14626 rtx varloc;
14627 enum var_init_status initialized;
14629 if (DECL_SIZE (decl) == NULL
14630 || !host_integerp (DECL_SIZE (decl), 1))
14631 return NULL;
14633 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14634 descr = NULL;
14635 descr_tail = &descr;
14637 for (p = loc; p; p = XEXP (p, 1))
14639 unsigned int bitsize = decl_piece_bitsize (p);
14640 rtx loc_note = *decl_piece_varloc_ptr (p);
14641 dw_loc_descr_ref cur_descr;
14642 dw_loc_descr_ref *tail, last = NULL;
14643 unsigned int opsize = 0;
14645 if (loc_note == NULL_RTX
14646 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14648 padsize += bitsize;
14649 continue;
14651 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14652 varloc = NOTE_VAR_LOCATION (loc_note);
14653 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14654 if (cur_descr == NULL)
14656 padsize += bitsize;
14657 continue;
14660 /* Check that cur_descr either doesn't use
14661 DW_OP_*piece operations, or their sum is equal
14662 to bitsize. Otherwise we can't embed it. */
14663 for (tail = &cur_descr; *tail != NULL;
14664 tail = &(*tail)->dw_loc_next)
14665 if ((*tail)->dw_loc_opc == DW_OP_piece)
14667 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14668 * BITS_PER_UNIT;
14669 last = *tail;
14671 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14673 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14674 last = *tail;
14677 if (last != NULL && opsize != bitsize)
14679 padsize += bitsize;
14680 continue;
14683 /* If there is a hole, add DW_OP_*piece after empty DWARF
14684 expression, which means that those bits are optimized out. */
14685 if (padsize)
14687 if (padsize > decl_size)
14688 return NULL;
14689 decl_size -= padsize;
14690 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14691 if (*descr_tail == NULL)
14692 return NULL;
14693 descr_tail = &(*descr_tail)->dw_loc_next;
14694 padsize = 0;
14696 *descr_tail = cur_descr;
14697 descr_tail = tail;
14698 if (bitsize > decl_size)
14699 return NULL;
14700 decl_size -= bitsize;
14701 if (last == NULL)
14703 HOST_WIDE_INT offset = 0;
14704 if (GET_CODE (varloc) == VAR_LOCATION
14705 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14707 varloc = PAT_VAR_LOCATION_LOC (varloc);
14708 if (GET_CODE (varloc) == EXPR_LIST)
14709 varloc = XEXP (varloc, 0);
14713 if (GET_CODE (varloc) == CONST
14714 || GET_CODE (varloc) == SIGN_EXTEND
14715 || GET_CODE (varloc) == ZERO_EXTEND)
14716 varloc = XEXP (varloc, 0);
14717 else if (GET_CODE (varloc) == SUBREG)
14718 varloc = SUBREG_REG (varloc);
14719 else
14720 break;
14722 while (1);
14723 /* DW_OP_bit_size offset should be zero for register
14724 or implicit location descriptions and empty location
14725 descriptions, but for memory addresses needs big endian
14726 adjustment. */
14727 if (MEM_P (varloc))
14729 unsigned HOST_WIDE_INT memsize
14730 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
14731 if (memsize != bitsize)
14733 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14734 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14735 return NULL;
14736 if (memsize < bitsize)
14737 return NULL;
14738 if (BITS_BIG_ENDIAN)
14739 offset = memsize - bitsize;
14743 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14744 if (*descr_tail == NULL)
14745 return NULL;
14746 descr_tail = &(*descr_tail)->dw_loc_next;
14750 /* If there were any non-empty expressions, add padding till the end of
14751 the decl. */
14752 if (descr != NULL && decl_size != 0)
14754 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14755 if (*descr_tail == NULL)
14756 return NULL;
14758 return descr;
14761 /* Return the dwarf representation of the location list LOC_LIST of
14762 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14763 function. */
14765 static dw_loc_list_ref
14766 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14768 const char *endname, *secname;
14769 rtx varloc;
14770 enum var_init_status initialized;
14771 struct var_loc_node *node;
14772 dw_loc_descr_ref descr;
14773 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14774 dw_loc_list_ref list = NULL;
14775 dw_loc_list_ref *listp = &list;
14777 /* Now that we know what section we are using for a base,
14778 actually construct the list of locations.
14779 The first location information is what is passed to the
14780 function that creates the location list, and the remaining
14781 locations just get added on to that list.
14782 Note that we only know the start address for a location
14783 (IE location changes), so to build the range, we use
14784 the range [current location start, next location start].
14785 This means we have to special case the last node, and generate
14786 a range of [last location start, end of function label]. */
14788 secname = secname_for_decl (decl);
14790 for (node = loc_list->first; node; node = node->next)
14791 if (GET_CODE (node->loc) == EXPR_LIST
14792 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14794 if (GET_CODE (node->loc) == EXPR_LIST)
14796 /* This requires DW_OP_{,bit_}piece, which is not usable
14797 inside DWARF expressions. */
14798 if (want_address != 2)
14799 continue;
14800 descr = dw_sra_loc_expr (decl, node->loc);
14801 if (descr == NULL)
14802 continue;
14804 else
14806 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14807 varloc = NOTE_VAR_LOCATION (node->loc);
14808 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14810 if (descr)
14812 /* The variable has a location between NODE->LABEL and
14813 NODE->NEXT->LABEL. */
14814 if (node->next)
14815 endname = node->next->label;
14816 /* If the variable has a location at the last label
14817 it keeps its location until the end of function. */
14818 else if (!current_function_decl)
14819 endname = text_end_label;
14820 else
14822 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14823 current_function_funcdef_no);
14824 endname = ggc_strdup (label_id);
14827 *listp = new_loc_list (descr, node->label, endname, secname);
14828 listp = &(*listp)->dw_loc_next;
14832 /* Try to avoid the overhead of a location list emitting a location
14833 expression instead, but only if we didn't have more than one
14834 location entry in the first place. If some entries were not
14835 representable, we don't want to pretend a single entry that was
14836 applies to the entire scope in which the variable is
14837 available. */
14838 if (list && loc_list->first->next)
14839 gen_llsym (list);
14841 return list;
14844 /* Return if the loc_list has only single element and thus can be represented
14845 as location description. */
14847 static bool
14848 single_element_loc_list_p (dw_loc_list_ref list)
14850 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14851 return !list->ll_symbol;
14854 /* To each location in list LIST add loc descr REF. */
14856 static void
14857 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14859 dw_loc_descr_ref copy;
14860 add_loc_descr (&list->expr, ref);
14861 list = list->dw_loc_next;
14862 while (list)
14864 copy = ggc_alloc_dw_loc_descr_node ();
14865 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14866 add_loc_descr (&list->expr, copy);
14867 while (copy->dw_loc_next)
14869 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
14870 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14871 copy->dw_loc_next = new_copy;
14872 copy = new_copy;
14874 list = list->dw_loc_next;
14878 /* Given two lists RET and LIST
14879 produce location list that is result of adding expression in LIST
14880 to expression in RET on each possition in program.
14881 Might be destructive on both RET and LIST.
14883 TODO: We handle only simple cases of RET or LIST having at most one
14884 element. General case would inolve sorting the lists in program order
14885 and merging them that will need some additional work.
14886 Adding that will improve quality of debug info especially for SRA-ed
14887 structures. */
14889 static void
14890 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14892 if (!list)
14893 return;
14894 if (!*ret)
14896 *ret = list;
14897 return;
14899 if (!list->dw_loc_next)
14901 add_loc_descr_to_each (*ret, list->expr);
14902 return;
14904 if (!(*ret)->dw_loc_next)
14906 add_loc_descr_to_each (list, (*ret)->expr);
14907 *ret = list;
14908 return;
14910 expansion_failed (NULL_TREE, NULL_RTX,
14911 "Don't know how to merge two non-trivial"
14912 " location lists.\n");
14913 *ret = NULL;
14914 return;
14917 /* LOC is constant expression. Try a luck, look it up in constant
14918 pool and return its loc_descr of its address. */
14920 static dw_loc_descr_ref
14921 cst_pool_loc_descr (tree loc)
14923 /* Get an RTL for this, if something has been emitted. */
14924 rtx rtl = lookup_constant_def (loc);
14925 enum machine_mode mode;
14927 if (!rtl || !MEM_P (rtl))
14929 gcc_assert (!rtl);
14930 return 0;
14932 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14934 /* TODO: We might get more coverage if we was actually delaying expansion
14935 of all expressions till end of compilation when constant pools are fully
14936 populated. */
14937 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14939 expansion_failed (loc, NULL_RTX,
14940 "CST value in contant pool but not marked.");
14941 return 0;
14943 mode = GET_MODE (rtl);
14944 rtl = XEXP (rtl, 0);
14945 return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
14948 /* Return dw_loc_list representing address of addr_expr LOC
14949 by looking for innder INDIRECT_REF expression and turing it
14950 into simple arithmetics. */
14952 static dw_loc_list_ref
14953 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14955 tree obj, offset;
14956 HOST_WIDE_INT bitsize, bitpos, bytepos;
14957 enum machine_mode mode;
14958 int volatilep;
14959 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
14960 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14962 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14963 &bitsize, &bitpos, &offset, &mode,
14964 &unsignedp, &volatilep, false);
14965 STRIP_NOPS (obj);
14966 if (bitpos % BITS_PER_UNIT)
14968 expansion_failed (loc, NULL_RTX, "bitfield access");
14969 return 0;
14971 if (!INDIRECT_REF_P (obj))
14973 expansion_failed (obj,
14974 NULL_RTX, "no indirect ref in inner refrence");
14975 return 0;
14977 if (!offset && !bitpos)
14978 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14979 else if (toplev
14980 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14981 && (dwarf_version >= 4 || !dwarf_strict))
14983 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14984 if (!list_ret)
14985 return 0;
14986 if (offset)
14988 /* Variable offset. */
14989 list_ret1 = loc_list_from_tree (offset, 0);
14990 if (list_ret1 == 0)
14991 return 0;
14992 add_loc_list (&list_ret, list_ret1);
14993 if (!list_ret)
14994 return 0;
14995 add_loc_descr_to_each (list_ret,
14996 new_loc_descr (DW_OP_plus, 0, 0));
14998 bytepos = bitpos / BITS_PER_UNIT;
14999 if (bytepos > 0)
15000 add_loc_descr_to_each (list_ret,
15001 new_loc_descr (DW_OP_plus_uconst,
15002 bytepos, 0));
15003 else if (bytepos < 0)
15004 loc_list_plus_const (list_ret, bytepos);
15005 add_loc_descr_to_each (list_ret,
15006 new_loc_descr (DW_OP_stack_value, 0, 0));
15008 return list_ret;
15012 /* Generate Dwarf location list representing LOC.
15013 If WANT_ADDRESS is false, expression computing LOC will be computed
15014 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15015 if WANT_ADDRESS is 2, expression computing address useable in location
15016 will be returned (i.e. DW_OP_reg can be used
15017 to refer to register values). */
15019 static dw_loc_list_ref
15020 loc_list_from_tree (tree loc, int want_address)
15022 dw_loc_descr_ref ret = NULL, ret1 = NULL;
15023 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15024 int have_address = 0;
15025 enum dwarf_location_atom op;
15027 /* ??? Most of the time we do not take proper care for sign/zero
15028 extending the values properly. Hopefully this won't be a real
15029 problem... */
15031 switch (TREE_CODE (loc))
15033 case ERROR_MARK:
15034 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15035 return 0;
15037 case PLACEHOLDER_EXPR:
15038 /* This case involves extracting fields from an object to determine the
15039 position of other fields. We don't try to encode this here. The
15040 only user of this is Ada, which encodes the needed information using
15041 the names of types. */
15042 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15043 return 0;
15045 case CALL_EXPR:
15046 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15047 /* There are no opcodes for these operations. */
15048 return 0;
15050 case PREINCREMENT_EXPR:
15051 case PREDECREMENT_EXPR:
15052 case POSTINCREMENT_EXPR:
15053 case POSTDECREMENT_EXPR:
15054 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15055 /* There are no opcodes for these operations. */
15056 return 0;
15058 case ADDR_EXPR:
15059 /* If we already want an address, see if there is INDIRECT_REF inside
15060 e.g. for &this->field. */
15061 if (want_address)
15063 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15064 (loc, want_address == 2);
15065 if (list_ret)
15066 have_address = 1;
15067 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15068 && (ret = cst_pool_loc_descr (loc)))
15069 have_address = 1;
15071 /* Otherwise, process the argument and look for the address. */
15072 if (!list_ret && !ret)
15073 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15074 else
15076 if (want_address)
15077 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15078 return NULL;
15080 break;
15082 case VAR_DECL:
15083 if (DECL_THREAD_LOCAL_P (loc))
15085 rtx rtl;
15086 enum dwarf_location_atom first_op;
15087 enum dwarf_location_atom second_op;
15088 bool dtprel = false;
15090 if (targetm.have_tls)
15092 /* If this is not defined, we have no way to emit the
15093 data. */
15094 if (!targetm.asm_out.output_dwarf_dtprel)
15095 return 0;
15097 /* The way DW_OP_GNU_push_tls_address is specified, we
15098 can only look up addresses of objects in the current
15099 module. We used DW_OP_addr as first op, but that's
15100 wrong, because DW_OP_addr is relocated by the debug
15101 info consumer, while DW_OP_GNU_push_tls_address
15102 operand shouldn't be. */
15103 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15104 return 0;
15105 first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15106 dtprel = true;
15107 second_op = DW_OP_GNU_push_tls_address;
15109 else
15111 if (!targetm.emutls.debug_form_tls_address
15112 || !(dwarf_version >= 3 || !dwarf_strict))
15113 return 0;
15114 loc = emutls_decl (loc);
15115 first_op = DW_OP_addr;
15116 second_op = DW_OP_form_tls_address;
15119 rtl = rtl_for_decl_location (loc);
15120 if (rtl == NULL_RTX)
15121 return 0;
15123 if (!MEM_P (rtl))
15124 return 0;
15125 rtl = XEXP (rtl, 0);
15126 if (! CONSTANT_P (rtl))
15127 return 0;
15129 ret = new_loc_descr (first_op, 0, 0);
15130 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15131 ret->dw_loc_oprnd1.v.val_addr = rtl;
15132 ret->dtprel = dtprel;
15134 ret1 = new_loc_descr (second_op, 0, 0);
15135 add_loc_descr (&ret, ret1);
15137 have_address = 1;
15138 break;
15140 /* FALLTHRU */
15142 case PARM_DECL:
15143 if (DECL_HAS_VALUE_EXPR_P (loc))
15144 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15145 want_address);
15146 /* FALLTHRU */
15148 case RESULT_DECL:
15149 case FUNCTION_DECL:
15151 rtx rtl;
15152 var_loc_list *loc_list = lookup_decl_loc (loc);
15154 if (loc_list && loc_list->first)
15156 list_ret = dw_loc_list (loc_list, loc, want_address);
15157 have_address = want_address != 0;
15158 break;
15160 rtl = rtl_for_decl_location (loc);
15161 if (rtl == NULL_RTX)
15163 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15164 return 0;
15166 else if (CONST_INT_P (rtl))
15168 HOST_WIDE_INT val = INTVAL (rtl);
15169 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15170 val &= GET_MODE_MASK (DECL_MODE (loc));
15171 ret = int_loc_descriptor (val);
15173 else if (GET_CODE (rtl) == CONST_STRING)
15175 expansion_failed (loc, NULL_RTX, "CONST_STRING");
15176 return 0;
15178 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15180 ret = new_loc_descr (DW_OP_addr, 0, 0);
15181 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15182 ret->dw_loc_oprnd1.v.val_addr = rtl;
15184 else
15186 enum machine_mode mode;
15188 /* Certain constructs can only be represented at top-level. */
15189 if (want_address == 2)
15191 ret = loc_descriptor (rtl, VOIDmode,
15192 VAR_INIT_STATUS_INITIALIZED);
15193 have_address = 1;
15195 else
15197 mode = GET_MODE (rtl);
15198 if (MEM_P (rtl))
15200 rtl = XEXP (rtl, 0);
15201 have_address = 1;
15203 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15205 if (!ret)
15206 expansion_failed (loc, rtl,
15207 "failed to produce loc descriptor for rtl");
15210 break;
15212 case MEM_REF:
15213 /* ??? FIXME. */
15214 if (!integer_zerop (TREE_OPERAND (loc, 1)))
15215 return 0;
15216 /* Fallthru. */
15217 case INDIRECT_REF:
15218 case MISALIGNED_INDIRECT_REF:
15219 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15220 have_address = 1;
15221 break;
15223 case COMPOUND_EXPR:
15224 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15226 CASE_CONVERT:
15227 case VIEW_CONVERT_EXPR:
15228 case SAVE_EXPR:
15229 case MODIFY_EXPR:
15230 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15232 case COMPONENT_REF:
15233 case BIT_FIELD_REF:
15234 case ARRAY_REF:
15235 case ARRAY_RANGE_REF:
15236 case REALPART_EXPR:
15237 case IMAGPART_EXPR:
15239 tree obj, offset;
15240 HOST_WIDE_INT bitsize, bitpos, bytepos;
15241 enum machine_mode mode;
15242 int volatilep;
15243 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15245 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15246 &unsignedp, &volatilep, false);
15248 gcc_assert (obj != loc);
15250 list_ret = loc_list_from_tree (obj,
15251 want_address == 2
15252 && !bitpos && !offset ? 2 : 1);
15253 /* TODO: We can extract value of the small expression via shifting even
15254 for nonzero bitpos. */
15255 if (list_ret == 0)
15256 return 0;
15257 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15259 expansion_failed (loc, NULL_RTX,
15260 "bitfield access");
15261 return 0;
15264 if (offset != NULL_TREE)
15266 /* Variable offset. */
15267 list_ret1 = loc_list_from_tree (offset, 0);
15268 if (list_ret1 == 0)
15269 return 0;
15270 add_loc_list (&list_ret, list_ret1);
15271 if (!list_ret)
15272 return 0;
15273 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15276 bytepos = bitpos / BITS_PER_UNIT;
15277 if (bytepos > 0)
15278 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15279 else if (bytepos < 0)
15280 loc_list_plus_const (list_ret, bytepos);
15282 have_address = 1;
15283 break;
15286 case INTEGER_CST:
15287 if ((want_address || !host_integerp (loc, 0))
15288 && (ret = cst_pool_loc_descr (loc)))
15289 have_address = 1;
15290 else if (want_address == 2
15291 && host_integerp (loc, 0)
15292 && (ret = address_of_int_loc_descriptor
15293 (int_size_in_bytes (TREE_TYPE (loc)),
15294 tree_low_cst (loc, 0))))
15295 have_address = 1;
15296 else if (host_integerp (loc, 0))
15297 ret = int_loc_descriptor (tree_low_cst (loc, 0));
15298 else
15300 expansion_failed (loc, NULL_RTX,
15301 "Integer operand is not host integer");
15302 return 0;
15304 break;
15306 case CONSTRUCTOR:
15307 case REAL_CST:
15308 case STRING_CST:
15309 case COMPLEX_CST:
15310 if ((ret = cst_pool_loc_descr (loc)))
15311 have_address = 1;
15312 else
15313 /* We can construct small constants here using int_loc_descriptor. */
15314 expansion_failed (loc, NULL_RTX,
15315 "constructor or constant not in constant pool");
15316 break;
15318 case TRUTH_AND_EXPR:
15319 case TRUTH_ANDIF_EXPR:
15320 case BIT_AND_EXPR:
15321 op = DW_OP_and;
15322 goto do_binop;
15324 case TRUTH_XOR_EXPR:
15325 case BIT_XOR_EXPR:
15326 op = DW_OP_xor;
15327 goto do_binop;
15329 case TRUTH_OR_EXPR:
15330 case TRUTH_ORIF_EXPR:
15331 case BIT_IOR_EXPR:
15332 op = DW_OP_or;
15333 goto do_binop;
15335 case FLOOR_DIV_EXPR:
15336 case CEIL_DIV_EXPR:
15337 case ROUND_DIV_EXPR:
15338 case TRUNC_DIV_EXPR:
15339 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15340 return 0;
15341 op = DW_OP_div;
15342 goto do_binop;
15344 case MINUS_EXPR:
15345 op = DW_OP_minus;
15346 goto do_binop;
15348 case FLOOR_MOD_EXPR:
15349 case CEIL_MOD_EXPR:
15350 case ROUND_MOD_EXPR:
15351 case TRUNC_MOD_EXPR:
15352 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15354 op = DW_OP_mod;
15355 goto do_binop;
15357 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15358 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15359 if (list_ret == 0 || list_ret1 == 0)
15360 return 0;
15362 add_loc_list (&list_ret, list_ret1);
15363 if (list_ret == 0)
15364 return 0;
15365 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15366 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15367 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15368 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15369 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15370 break;
15372 case MULT_EXPR:
15373 op = DW_OP_mul;
15374 goto do_binop;
15376 case LSHIFT_EXPR:
15377 op = DW_OP_shl;
15378 goto do_binop;
15380 case RSHIFT_EXPR:
15381 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15382 goto do_binop;
15384 case POINTER_PLUS_EXPR:
15385 case PLUS_EXPR:
15386 if (host_integerp (TREE_OPERAND (loc, 1), 0))
15388 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15389 if (list_ret == 0)
15390 return 0;
15392 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15393 break;
15396 op = DW_OP_plus;
15397 goto do_binop;
15399 case LE_EXPR:
15400 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15401 return 0;
15403 op = DW_OP_le;
15404 goto do_binop;
15406 case GE_EXPR:
15407 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15408 return 0;
15410 op = DW_OP_ge;
15411 goto do_binop;
15413 case LT_EXPR:
15414 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15415 return 0;
15417 op = DW_OP_lt;
15418 goto do_binop;
15420 case GT_EXPR:
15421 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15422 return 0;
15424 op = DW_OP_gt;
15425 goto do_binop;
15427 case EQ_EXPR:
15428 op = DW_OP_eq;
15429 goto do_binop;
15431 case NE_EXPR:
15432 op = DW_OP_ne;
15433 goto do_binop;
15435 do_binop:
15436 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15437 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15438 if (list_ret == 0 || list_ret1 == 0)
15439 return 0;
15441 add_loc_list (&list_ret, list_ret1);
15442 if (list_ret == 0)
15443 return 0;
15444 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15445 break;
15447 case TRUTH_NOT_EXPR:
15448 case BIT_NOT_EXPR:
15449 op = DW_OP_not;
15450 goto do_unop;
15452 case ABS_EXPR:
15453 op = DW_OP_abs;
15454 goto do_unop;
15456 case NEGATE_EXPR:
15457 op = DW_OP_neg;
15458 goto do_unop;
15460 do_unop:
15461 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15462 if (list_ret == 0)
15463 return 0;
15465 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15466 break;
15468 case MIN_EXPR:
15469 case MAX_EXPR:
15471 const enum tree_code code =
15472 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15474 loc = build3 (COND_EXPR, TREE_TYPE (loc),
15475 build2 (code, integer_type_node,
15476 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15477 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15480 /* ... fall through ... */
15482 case COND_EXPR:
15484 dw_loc_descr_ref lhs
15485 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15486 dw_loc_list_ref rhs
15487 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15488 dw_loc_descr_ref bra_node, jump_node, tmp;
15490 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15491 if (list_ret == 0 || lhs == 0 || rhs == 0)
15492 return 0;
15494 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15495 add_loc_descr_to_each (list_ret, bra_node);
15497 add_loc_list (&list_ret, rhs);
15498 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15499 add_loc_descr_to_each (list_ret, jump_node);
15501 add_loc_descr_to_each (list_ret, lhs);
15502 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15503 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15505 /* ??? Need a node to point the skip at. Use a nop. */
15506 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15507 add_loc_descr_to_each (list_ret, tmp);
15508 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15509 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15511 break;
15513 case FIX_TRUNC_EXPR:
15514 return 0;
15516 default:
15517 /* Leave front-end specific codes as simply unknown. This comes
15518 up, for instance, with the C STMT_EXPR. */
15519 if ((unsigned int) TREE_CODE (loc)
15520 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15522 expansion_failed (loc, NULL_RTX,
15523 "language specific tree node");
15524 return 0;
15527 #ifdef ENABLE_CHECKING
15528 /* Otherwise this is a generic code; we should just lists all of
15529 these explicitly. We forgot one. */
15530 gcc_unreachable ();
15531 #else
15532 /* In a release build, we want to degrade gracefully: better to
15533 generate incomplete debugging information than to crash. */
15534 return NULL;
15535 #endif
15538 if (!ret && !list_ret)
15539 return 0;
15541 if (want_address == 2 && !have_address
15542 && (dwarf_version >= 4 || !dwarf_strict))
15544 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15546 expansion_failed (loc, NULL_RTX,
15547 "DWARF address size mismatch");
15548 return 0;
15550 if (ret)
15551 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15552 else
15553 add_loc_descr_to_each (list_ret,
15554 new_loc_descr (DW_OP_stack_value, 0, 0));
15555 have_address = 1;
15557 /* Show if we can't fill the request for an address. */
15558 if (want_address && !have_address)
15560 expansion_failed (loc, NULL_RTX,
15561 "Want address and only have value");
15562 return 0;
15565 gcc_assert (!ret || !list_ret);
15567 /* If we've got an address and don't want one, dereference. */
15568 if (!want_address && have_address)
15570 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15572 if (size > DWARF2_ADDR_SIZE || size == -1)
15574 expansion_failed (loc, NULL_RTX,
15575 "DWARF address size mismatch");
15576 return 0;
15578 else if (size == DWARF2_ADDR_SIZE)
15579 op = DW_OP_deref;
15580 else
15581 op = DW_OP_deref_size;
15583 if (ret)
15584 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15585 else
15586 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15588 if (ret)
15589 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15591 return list_ret;
15594 /* Same as above but return only single location expression. */
15595 static dw_loc_descr_ref
15596 loc_descriptor_from_tree (tree loc, int want_address)
15598 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15599 if (!ret)
15600 return NULL;
15601 if (ret->dw_loc_next)
15603 expansion_failed (loc, NULL_RTX,
15604 "Location list where only loc descriptor needed");
15605 return NULL;
15607 return ret->expr;
15610 /* Given a value, round it up to the lowest multiple of `boundary'
15611 which is not less than the value itself. */
15613 static inline HOST_WIDE_INT
15614 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15616 return (((value + boundary - 1) / boundary) * boundary);
15619 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15620 pointer to the declared type for the relevant field variable, or return
15621 `integer_type_node' if the given node turns out to be an
15622 ERROR_MARK node. */
15624 static inline tree
15625 field_type (const_tree decl)
15627 tree type;
15629 if (TREE_CODE (decl) == ERROR_MARK)
15630 return integer_type_node;
15632 type = DECL_BIT_FIELD_TYPE (decl);
15633 if (type == NULL_TREE)
15634 type = TREE_TYPE (decl);
15636 return type;
15639 /* Given a pointer to a tree node, return the alignment in bits for
15640 it, or else return BITS_PER_WORD if the node actually turns out to
15641 be an ERROR_MARK node. */
15643 static inline unsigned
15644 simple_type_align_in_bits (const_tree type)
15646 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15649 static inline unsigned
15650 simple_decl_align_in_bits (const_tree decl)
15652 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15655 /* Return the result of rounding T up to ALIGN. */
15657 static inline double_int
15658 round_up_to_align (double_int t, unsigned int align)
15660 double_int alignd = uhwi_to_double_int (align);
15661 t = double_int_add (t, alignd);
15662 t = double_int_add (t, double_int_minus_one);
15663 t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15664 t = double_int_mul (t, alignd);
15665 return t;
15668 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15669 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15670 or return 0 if we are unable to determine what that offset is, either
15671 because the argument turns out to be a pointer to an ERROR_MARK node, or
15672 because the offset is actually variable. (We can't handle the latter case
15673 just yet). */
15675 static HOST_WIDE_INT
15676 field_byte_offset (const_tree decl)
15678 double_int object_offset_in_bits;
15679 double_int object_offset_in_bytes;
15680 double_int bitpos_int;
15682 if (TREE_CODE (decl) == ERROR_MARK)
15683 return 0;
15685 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15687 /* We cannot yet cope with fields whose positions are variable, so
15688 for now, when we see such things, we simply return 0. Someday, we may
15689 be able to handle such cases, but it will be damn difficult. */
15690 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15691 return 0;
15693 bitpos_int = tree_to_double_int (bit_position (decl));
15695 #ifdef PCC_BITFIELD_TYPE_MATTERS
15696 if (PCC_BITFIELD_TYPE_MATTERS)
15698 tree type;
15699 tree field_size_tree;
15700 double_int deepest_bitpos;
15701 double_int field_size_in_bits;
15702 unsigned int type_align_in_bits;
15703 unsigned int decl_align_in_bits;
15704 double_int type_size_in_bits;
15706 type = field_type (decl);
15707 type_size_in_bits = double_int_type_size_in_bits (type);
15708 type_align_in_bits = simple_type_align_in_bits (type);
15710 field_size_tree = DECL_SIZE (decl);
15712 /* The size could be unspecified if there was an error, or for
15713 a flexible array member. */
15714 if (!field_size_tree)
15715 field_size_tree = bitsize_zero_node;
15717 /* If the size of the field is not constant, use the type size. */
15718 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15719 field_size_in_bits = tree_to_double_int (field_size_tree);
15720 else
15721 field_size_in_bits = type_size_in_bits;
15723 decl_align_in_bits = simple_decl_align_in_bits (decl);
15725 /* The GCC front-end doesn't make any attempt to keep track of the
15726 starting bit offset (relative to the start of the containing
15727 structure type) of the hypothetical "containing object" for a
15728 bit-field. Thus, when computing the byte offset value for the
15729 start of the "containing object" of a bit-field, we must deduce
15730 this information on our own. This can be rather tricky to do in
15731 some cases. For example, handling the following structure type
15732 definition when compiling for an i386/i486 target (which only
15733 aligns long long's to 32-bit boundaries) can be very tricky:
15735 struct S { int field1; long long field2:31; };
15737 Fortunately, there is a simple rule-of-thumb which can be used
15738 in such cases. When compiling for an i386/i486, GCC will
15739 allocate 8 bytes for the structure shown above. It decides to
15740 do this based upon one simple rule for bit-field allocation.
15741 GCC allocates each "containing object" for each bit-field at
15742 the first (i.e. lowest addressed) legitimate alignment boundary
15743 (based upon the required minimum alignment for the declared
15744 type of the field) which it can possibly use, subject to the
15745 condition that there is still enough available space remaining
15746 in the containing object (when allocated at the selected point)
15747 to fully accommodate all of the bits of the bit-field itself.
15749 This simple rule makes it obvious why GCC allocates 8 bytes for
15750 each object of the structure type shown above. When looking
15751 for a place to allocate the "containing object" for `field2',
15752 the compiler simply tries to allocate a 64-bit "containing
15753 object" at each successive 32-bit boundary (starting at zero)
15754 until it finds a place to allocate that 64- bit field such that
15755 at least 31 contiguous (and previously unallocated) bits remain
15756 within that selected 64 bit field. (As it turns out, for the
15757 example above, the compiler finds it is OK to allocate the
15758 "containing object" 64-bit field at bit-offset zero within the
15759 structure type.)
15761 Here we attempt to work backwards from the limited set of facts
15762 we're given, and we try to deduce from those facts, where GCC
15763 must have believed that the containing object started (within
15764 the structure type). The value we deduce is then used (by the
15765 callers of this routine) to generate DW_AT_location and
15766 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15767 the case of DW_AT_location, regular fields as well). */
15769 /* Figure out the bit-distance from the start of the structure to
15770 the "deepest" bit of the bit-field. */
15771 deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
15773 /* This is the tricky part. Use some fancy footwork to deduce
15774 where the lowest addressed bit of the containing object must
15775 be. */
15776 object_offset_in_bits
15777 = double_int_sub (deepest_bitpos, type_size_in_bits);
15779 /* Round up to type_align by default. This works best for
15780 bitfields. */
15781 object_offset_in_bits
15782 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15784 if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
15786 object_offset_in_bits
15787 = double_int_sub (deepest_bitpos, type_size_in_bits);
15789 /* Round up to decl_align instead. */
15790 object_offset_in_bits
15791 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15794 else
15795 #endif
15796 object_offset_in_bits = bitpos_int;
15798 object_offset_in_bytes
15799 = double_int_div (object_offset_in_bits,
15800 uhwi_to_double_int (BITS_PER_UNIT), true,
15801 TRUNC_DIV_EXPR);
15802 return double_int_to_shwi (object_offset_in_bytes);
15805 /* The following routines define various Dwarf attributes and any data
15806 associated with them. */
15808 /* Add a location description attribute value to a DIE.
15810 This emits location attributes suitable for whole variables and
15811 whole parameters. Note that the location attributes for struct fields are
15812 generated by the routine `data_member_location_attribute' below. */
15814 static inline void
15815 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15816 dw_loc_list_ref descr)
15818 if (descr == 0)
15819 return;
15820 if (single_element_loc_list_p (descr))
15821 add_AT_loc (die, attr_kind, descr->expr);
15822 else
15823 add_AT_loc_list (die, attr_kind, descr);
15826 /* Add DW_AT_accessibility attribute to DIE if needed. */
15828 static void
15829 add_accessibility_attribute (dw_die_ref die, tree decl)
15831 if (TREE_PROTECTED (decl))
15832 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15833 else if (TREE_PRIVATE (decl))
15834 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15837 /* Attach the specialized form of location attribute used for data members of
15838 struct and union types. In the special case of a FIELD_DECL node which
15839 represents a bit-field, the "offset" part of this special location
15840 descriptor must indicate the distance in bytes from the lowest-addressed
15841 byte of the containing struct or union type to the lowest-addressed byte of
15842 the "containing object" for the bit-field. (See the `field_byte_offset'
15843 function above).
15845 For any given bit-field, the "containing object" is a hypothetical object
15846 (of some integral or enum type) within which the given bit-field lives. The
15847 type of this hypothetical "containing object" is always the same as the
15848 declared type of the individual bit-field itself (for GCC anyway... the
15849 DWARF spec doesn't actually mandate this). Note that it is the size (in
15850 bytes) of the hypothetical "containing object" which will be given in the
15851 DW_AT_byte_size attribute for this bit-field. (See the
15852 `byte_size_attribute' function below.) It is also used when calculating the
15853 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15854 function below.) */
15856 static void
15857 add_data_member_location_attribute (dw_die_ref die, tree decl)
15859 HOST_WIDE_INT offset;
15860 dw_loc_descr_ref loc_descr = 0;
15862 if (TREE_CODE (decl) == TREE_BINFO)
15864 /* We're working on the TAG_inheritance for a base class. */
15865 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15867 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15868 aren't at a fixed offset from all (sub)objects of the same
15869 type. We need to extract the appropriate offset from our
15870 vtable. The following dwarf expression means
15872 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15874 This is specific to the V3 ABI, of course. */
15876 dw_loc_descr_ref tmp;
15878 /* Make a copy of the object address. */
15879 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15880 add_loc_descr (&loc_descr, tmp);
15882 /* Extract the vtable address. */
15883 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15884 add_loc_descr (&loc_descr, tmp);
15886 /* Calculate the address of the offset. */
15887 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
15888 gcc_assert (offset < 0);
15890 tmp = int_loc_descriptor (-offset);
15891 add_loc_descr (&loc_descr, tmp);
15892 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15893 add_loc_descr (&loc_descr, tmp);
15895 /* Extract the offset. */
15896 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15897 add_loc_descr (&loc_descr, tmp);
15899 /* Add it to the object address. */
15900 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15901 add_loc_descr (&loc_descr, tmp);
15903 else
15904 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
15906 else
15907 offset = field_byte_offset (decl);
15909 if (! loc_descr)
15911 if (dwarf_version > 2)
15913 /* Don't need to output a location expression, just the constant. */
15914 add_AT_int (die, DW_AT_data_member_location, offset);
15915 return;
15917 else
15919 enum dwarf_location_atom op;
15921 /* The DWARF2 standard says that we should assume that the structure
15922 address is already on the stack, so we can specify a structure
15923 field address by using DW_OP_plus_uconst. */
15925 #ifdef MIPS_DEBUGGING_INFO
15926 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
15927 operator correctly. It works only if we leave the offset on the
15928 stack. */
15929 op = DW_OP_constu;
15930 #else
15931 op = DW_OP_plus_uconst;
15932 #endif
15934 loc_descr = new_loc_descr (op, offset, 0);
15938 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15941 /* Writes integer values to dw_vec_const array. */
15943 static void
15944 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15946 while (size != 0)
15948 *dest++ = val & 0xff;
15949 val >>= 8;
15950 --size;
15954 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15956 static HOST_WIDE_INT
15957 extract_int (const unsigned char *src, unsigned int size)
15959 HOST_WIDE_INT val = 0;
15961 src += size;
15962 while (size != 0)
15964 val <<= 8;
15965 val |= *--src & 0xff;
15966 --size;
15968 return val;
15971 /* Writes double_int values to dw_vec_const array. */
15973 static void
15974 insert_double (double_int val, unsigned char *dest)
15976 unsigned char *p0 = dest;
15977 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
15979 if (WORDS_BIG_ENDIAN)
15981 p0 = p1;
15982 p1 = dest;
15985 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
15986 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
15989 /* Writes floating point values to dw_vec_const array. */
15991 static void
15992 insert_float (const_rtx rtl, unsigned char *array)
15994 REAL_VALUE_TYPE rv;
15995 long val[4];
15996 int i;
15998 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15999 real_to_target (val, &rv, GET_MODE (rtl));
16001 /* real_to_target puts 32-bit pieces in each long. Pack them. */
16002 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16004 insert_int (val[i], 4, array);
16005 array += 4;
16009 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16010 does not have a "location" either in memory or in a register. These
16011 things can arise in GNU C when a constant is passed as an actual parameter
16012 to an inlined function. They can also arise in C++ where declared
16013 constants do not necessarily get memory "homes". */
16015 static bool
16016 add_const_value_attribute (dw_die_ref die, rtx rtl)
16018 switch (GET_CODE (rtl))
16020 case CONST_INT:
16022 HOST_WIDE_INT val = INTVAL (rtl);
16024 if (val < 0)
16025 add_AT_int (die, DW_AT_const_value, val);
16026 else
16027 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16029 return true;
16031 case CONST_DOUBLE:
16032 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16033 floating-point constant. A CONST_DOUBLE is used whenever the
16034 constant requires more than one word in order to be adequately
16035 represented. */
16037 enum machine_mode mode = GET_MODE (rtl);
16039 if (SCALAR_FLOAT_MODE_P (mode))
16041 unsigned int length = GET_MODE_SIZE (mode);
16042 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16044 insert_float (rtl, array);
16045 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16047 else
16048 add_AT_double (die, DW_AT_const_value,
16049 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16051 return true;
16053 case CONST_VECTOR:
16055 enum machine_mode mode = GET_MODE (rtl);
16056 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16057 unsigned int length = CONST_VECTOR_NUNITS (rtl);
16058 unsigned char *array = (unsigned char *) ggc_alloc_atomic
16059 (length * elt_size);
16060 unsigned int i;
16061 unsigned char *p;
16063 switch (GET_MODE_CLASS (mode))
16065 case MODE_VECTOR_INT:
16066 for (i = 0, p = array; i < length; i++, p += elt_size)
16068 rtx elt = CONST_VECTOR_ELT (rtl, i);
16069 double_int val = rtx_to_double_int (elt);
16071 if (elt_size <= sizeof (HOST_WIDE_INT))
16072 insert_int (double_int_to_shwi (val), elt_size, p);
16073 else
16075 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16076 insert_double (val, p);
16079 break;
16081 case MODE_VECTOR_FLOAT:
16082 for (i = 0, p = array; i < length; i++, p += elt_size)
16084 rtx elt = CONST_VECTOR_ELT (rtl, i);
16085 insert_float (elt, p);
16087 break;
16089 default:
16090 gcc_unreachable ();
16093 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16095 return true;
16097 case CONST_STRING:
16098 if (dwarf_version >= 4 || !dwarf_strict)
16100 dw_loc_descr_ref loc_result;
16101 resolve_one_addr (&rtl, NULL);
16102 rtl_addr:
16103 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16104 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16105 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16106 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16107 add_AT_loc (die, DW_AT_location, loc_result);
16108 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16109 return true;
16111 return false;
16113 case CONST:
16114 if (CONSTANT_P (XEXP (rtl, 0)))
16115 return add_const_value_attribute (die, XEXP (rtl, 0));
16116 /* FALLTHROUGH */
16117 case SYMBOL_REF:
16118 if (!const_ok_for_output (rtl))
16119 return false;
16120 case LABEL_REF:
16121 if (dwarf_version >= 4 || !dwarf_strict)
16122 goto rtl_addr;
16123 return false;
16125 case PLUS:
16126 /* In cases where an inlined instance of an inline function is passed
16127 the address of an `auto' variable (which is local to the caller) we
16128 can get a situation where the DECL_RTL of the artificial local
16129 variable (for the inlining) which acts as a stand-in for the
16130 corresponding formal parameter (of the inline function) will look
16131 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
16132 exactly a compile-time constant expression, but it isn't the address
16133 of the (artificial) local variable either. Rather, it represents the
16134 *value* which the artificial local variable always has during its
16135 lifetime. We currently have no way to represent such quasi-constant
16136 values in Dwarf, so for now we just punt and generate nothing. */
16137 return false;
16139 case HIGH:
16140 case CONST_FIXED:
16141 return false;
16143 case MEM:
16144 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16145 && MEM_READONLY_P (rtl)
16146 && GET_MODE (rtl) == BLKmode)
16148 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16149 return true;
16151 return false;
16153 default:
16154 /* No other kinds of rtx should be possible here. */
16155 gcc_unreachable ();
16157 return false;
16160 /* Determine whether the evaluation of EXPR references any variables
16161 or functions which aren't otherwise used (and therefore may not be
16162 output). */
16163 static tree
16164 reference_to_unused (tree * tp, int * walk_subtrees,
16165 void * data ATTRIBUTE_UNUSED)
16167 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16168 *walk_subtrees = 0;
16170 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16171 && ! TREE_ASM_WRITTEN (*tp))
16172 return *tp;
16173 /* ??? The C++ FE emits debug information for using decls, so
16174 putting gcc_unreachable here falls over. See PR31899. For now
16175 be conservative. */
16176 else if (!cgraph_global_info_ready
16177 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16178 return *tp;
16179 else if (TREE_CODE (*tp) == VAR_DECL)
16181 struct varpool_node *node = varpool_get_node (*tp);
16182 if (!node || !node->needed)
16183 return *tp;
16185 else if (TREE_CODE (*tp) == FUNCTION_DECL
16186 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16188 /* The call graph machinery must have finished analyzing,
16189 optimizing and gimplifying the CU by now.
16190 So if *TP has no call graph node associated
16191 to it, it means *TP will not be emitted. */
16192 if (!cgraph_get_node (*tp))
16193 return *tp;
16195 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16196 return *tp;
16198 return NULL_TREE;
16201 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16202 for use in a later add_const_value_attribute call. */
16204 static rtx
16205 rtl_for_decl_init (tree init, tree type)
16207 rtx rtl = NULL_RTX;
16209 /* If a variable is initialized with a string constant without embedded
16210 zeros, build CONST_STRING. */
16211 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16213 tree enttype = TREE_TYPE (type);
16214 tree domain = TYPE_DOMAIN (type);
16215 enum machine_mode mode = TYPE_MODE (enttype);
16217 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16218 && domain
16219 && integer_zerop (TYPE_MIN_VALUE (domain))
16220 && compare_tree_int (TYPE_MAX_VALUE (domain),
16221 TREE_STRING_LENGTH (init) - 1) == 0
16222 && ((size_t) TREE_STRING_LENGTH (init)
16223 == strlen (TREE_STRING_POINTER (init)) + 1))
16225 rtl = gen_rtx_CONST_STRING (VOIDmode,
16226 ggc_strdup (TREE_STRING_POINTER (init)));
16227 rtl = gen_rtx_MEM (BLKmode, rtl);
16228 MEM_READONLY_P (rtl) = 1;
16231 /* Other aggregates, and complex values, could be represented using
16232 CONCAT: FIXME! */
16233 else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
16235 /* Vectors only work if their mode is supported by the target.
16236 FIXME: generic vectors ought to work too. */
16237 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
16239 /* If the initializer is something that we know will expand into an
16240 immediate RTL constant, expand it now. We must be careful not to
16241 reference variables which won't be output. */
16242 else if (initializer_constant_valid_p (init, type)
16243 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16245 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16246 possible. */
16247 if (TREE_CODE (type) == VECTOR_TYPE)
16248 switch (TREE_CODE (init))
16250 case VECTOR_CST:
16251 break;
16252 case CONSTRUCTOR:
16253 if (TREE_CONSTANT (init))
16255 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16256 bool constant_p = true;
16257 tree value;
16258 unsigned HOST_WIDE_INT ix;
16260 /* Even when ctor is constant, it might contain non-*_CST
16261 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16262 belong into VECTOR_CST nodes. */
16263 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16264 if (!CONSTANT_CLASS_P (value))
16266 constant_p = false;
16267 break;
16270 if (constant_p)
16272 init = build_vector_from_ctor (type, elts);
16273 break;
16276 /* FALLTHRU */
16278 default:
16279 return NULL;
16282 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16284 /* If expand_expr returns a MEM, it wasn't immediate. */
16285 gcc_assert (!rtl || !MEM_P (rtl));
16288 return rtl;
16291 /* Generate RTL for the variable DECL to represent its location. */
16293 static rtx
16294 rtl_for_decl_location (tree decl)
16296 rtx rtl;
16298 /* Here we have to decide where we are going to say the parameter "lives"
16299 (as far as the debugger is concerned). We only have a couple of
16300 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16302 DECL_RTL normally indicates where the parameter lives during most of the
16303 activation of the function. If optimization is enabled however, this
16304 could be either NULL or else a pseudo-reg. Both of those cases indicate
16305 that the parameter doesn't really live anywhere (as far as the code
16306 generation parts of GCC are concerned) during most of the function's
16307 activation. That will happen (for example) if the parameter is never
16308 referenced within the function.
16310 We could just generate a location descriptor here for all non-NULL
16311 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16312 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16313 where DECL_RTL is NULL or is a pseudo-reg.
16315 Note however that we can only get away with using DECL_INCOMING_RTL as
16316 a backup substitute for DECL_RTL in certain limited cases. In cases
16317 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16318 we can be sure that the parameter was passed using the same type as it is
16319 declared to have within the function, and that its DECL_INCOMING_RTL
16320 points us to a place where a value of that type is passed.
16322 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16323 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16324 because in these cases DECL_INCOMING_RTL points us to a value of some
16325 type which is *different* from the type of the parameter itself. Thus,
16326 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16327 such cases, the debugger would end up (for example) trying to fetch a
16328 `float' from a place which actually contains the first part of a
16329 `double'. That would lead to really incorrect and confusing
16330 output at debug-time.
16332 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16333 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
16334 are a couple of exceptions however. On little-endian machines we can
16335 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16336 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16337 an integral type that is smaller than TREE_TYPE (decl). These cases arise
16338 when (on a little-endian machine) a non-prototyped function has a
16339 parameter declared to be of type `short' or `char'. In such cases,
16340 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16341 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16342 passed `int' value. If the debugger then uses that address to fetch
16343 a `short' or a `char' (on a little-endian machine) the result will be
16344 the correct data, so we allow for such exceptional cases below.
16346 Note that our goal here is to describe the place where the given formal
16347 parameter lives during most of the function's activation (i.e. between the
16348 end of the prologue and the start of the epilogue). We'll do that as best
16349 as we can. Note however that if the given formal parameter is modified
16350 sometime during the execution of the function, then a stack backtrace (at
16351 debug-time) will show the function as having been called with the *new*
16352 value rather than the value which was originally passed in. This happens
16353 rarely enough that it is not a major problem, but it *is* a problem, and
16354 I'd like to fix it.
16356 A future version of dwarf2out.c may generate two additional attributes for
16357 any given DW_TAG_formal_parameter DIE which will describe the "passed
16358 type" and the "passed location" for the given formal parameter in addition
16359 to the attributes we now generate to indicate the "declared type" and the
16360 "active location" for each parameter. This additional set of attributes
16361 could be used by debuggers for stack backtraces. Separately, note that
16362 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16363 This happens (for example) for inlined-instances of inline function formal
16364 parameters which are never referenced. This really shouldn't be
16365 happening. All PARM_DECL nodes should get valid non-NULL
16366 DECL_INCOMING_RTL values. FIXME. */
16368 /* Use DECL_RTL as the "location" unless we find something better. */
16369 rtl = DECL_RTL_IF_SET (decl);
16371 /* When generating abstract instances, ignore everything except
16372 constants, symbols living in memory, and symbols living in
16373 fixed registers. */
16374 if (! reload_completed)
16376 if (rtl
16377 && (CONSTANT_P (rtl)
16378 || (MEM_P (rtl)
16379 && CONSTANT_P (XEXP (rtl, 0)))
16380 || (REG_P (rtl)
16381 && TREE_CODE (decl) == VAR_DECL
16382 && TREE_STATIC (decl))))
16384 rtl = targetm.delegitimize_address (rtl);
16385 return rtl;
16387 rtl = NULL_RTX;
16389 else if (TREE_CODE (decl) == PARM_DECL)
16391 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16393 tree declared_type = TREE_TYPE (decl);
16394 tree passed_type = DECL_ARG_TYPE (decl);
16395 enum machine_mode dmode = TYPE_MODE (declared_type);
16396 enum machine_mode pmode = TYPE_MODE (passed_type);
16398 /* This decl represents a formal parameter which was optimized out.
16399 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16400 all cases where (rtl == NULL_RTX) just below. */
16401 if (dmode == pmode)
16402 rtl = DECL_INCOMING_RTL (decl);
16403 else if (SCALAR_INT_MODE_P (dmode)
16404 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16405 && DECL_INCOMING_RTL (decl))
16407 rtx inc = DECL_INCOMING_RTL (decl);
16408 if (REG_P (inc))
16409 rtl = inc;
16410 else if (MEM_P (inc))
16412 if (BYTES_BIG_ENDIAN)
16413 rtl = adjust_address_nv (inc, dmode,
16414 GET_MODE_SIZE (pmode)
16415 - GET_MODE_SIZE (dmode));
16416 else
16417 rtl = inc;
16422 /* If the parm was passed in registers, but lives on the stack, then
16423 make a big endian correction if the mode of the type of the
16424 parameter is not the same as the mode of the rtl. */
16425 /* ??? This is the same series of checks that are made in dbxout.c before
16426 we reach the big endian correction code there. It isn't clear if all
16427 of these checks are necessary here, but keeping them all is the safe
16428 thing to do. */
16429 else if (MEM_P (rtl)
16430 && XEXP (rtl, 0) != const0_rtx
16431 && ! CONSTANT_P (XEXP (rtl, 0))
16432 /* Not passed in memory. */
16433 && !MEM_P (DECL_INCOMING_RTL (decl))
16434 /* Not passed by invisible reference. */
16435 && (!REG_P (XEXP (rtl, 0))
16436 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16437 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16438 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
16439 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16440 #endif
16442 /* Big endian correction check. */
16443 && BYTES_BIG_ENDIAN
16444 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16445 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16446 < UNITS_PER_WORD))
16448 int offset = (UNITS_PER_WORD
16449 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16451 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16452 plus_constant (XEXP (rtl, 0), offset));
16455 else if (TREE_CODE (decl) == VAR_DECL
16456 && rtl
16457 && MEM_P (rtl)
16458 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16459 && BYTES_BIG_ENDIAN)
16461 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16462 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16464 /* If a variable is declared "register" yet is smaller than
16465 a register, then if we store the variable to memory, it
16466 looks like we're storing a register-sized value, when in
16467 fact we are not. We need to adjust the offset of the
16468 storage location to reflect the actual value's bytes,
16469 else gdb will not be able to display it. */
16470 if (rsize > dsize)
16471 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16472 plus_constant (XEXP (rtl, 0), rsize-dsize));
16475 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16476 and will have been substituted directly into all expressions that use it.
16477 C does not have such a concept, but C++ and other languages do. */
16478 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16479 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16481 if (rtl)
16482 rtl = targetm.delegitimize_address (rtl);
16484 /* If we don't look past the constant pool, we risk emitting a
16485 reference to a constant pool entry that isn't referenced from
16486 code, and thus is not emitted. */
16487 if (rtl)
16488 rtl = avoid_constant_pool_reference (rtl);
16490 /* Try harder to get a rtl. If this symbol ends up not being emitted
16491 in the current CU, resolve_addr will remove the expression referencing
16492 it. */
16493 if (rtl == NULL_RTX
16494 && TREE_CODE (decl) == VAR_DECL
16495 && !DECL_EXTERNAL (decl)
16496 && TREE_STATIC (decl)
16497 && DECL_NAME (decl)
16498 && !DECL_HARD_REGISTER (decl)
16499 && DECL_MODE (decl) != VOIDmode)
16501 rtl = make_decl_rtl_for_debug (decl);
16502 if (!MEM_P (rtl)
16503 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16504 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16505 rtl = NULL_RTX;
16508 return rtl;
16511 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16512 returned. If so, the decl for the COMMON block is returned, and the
16513 value is the offset into the common block for the symbol. */
16515 static tree
16516 fortran_common (tree decl, HOST_WIDE_INT *value)
16518 tree val_expr, cvar;
16519 enum machine_mode mode;
16520 HOST_WIDE_INT bitsize, bitpos;
16521 tree offset;
16522 int volatilep = 0, unsignedp = 0;
16524 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16525 it does not have a value (the offset into the common area), or if it
16526 is thread local (as opposed to global) then it isn't common, and shouldn't
16527 be handled as such. */
16528 if (TREE_CODE (decl) != VAR_DECL
16529 || !TREE_STATIC (decl)
16530 || !DECL_HAS_VALUE_EXPR_P (decl)
16531 || !is_fortran ())
16532 return NULL_TREE;
16534 val_expr = DECL_VALUE_EXPR (decl);
16535 if (TREE_CODE (val_expr) != COMPONENT_REF)
16536 return NULL_TREE;
16538 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16539 &mode, &unsignedp, &volatilep, true);
16541 if (cvar == NULL_TREE
16542 || TREE_CODE (cvar) != VAR_DECL
16543 || DECL_ARTIFICIAL (cvar)
16544 || !TREE_PUBLIC (cvar))
16545 return NULL_TREE;
16547 *value = 0;
16548 if (offset != NULL)
16550 if (!host_integerp (offset, 0))
16551 return NULL_TREE;
16552 *value = tree_low_cst (offset, 0);
16554 if (bitpos != 0)
16555 *value += bitpos / BITS_PER_UNIT;
16557 return cvar;
16560 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16561 data attribute for a variable or a parameter. We generate the
16562 DW_AT_const_value attribute only in those cases where the given variable
16563 or parameter does not have a true "location" either in memory or in a
16564 register. This can happen (for example) when a constant is passed as an
16565 actual argument in a call to an inline function. (It's possible that
16566 these things can crop up in other ways also.) Note that one type of
16567 constant value which can be passed into an inlined function is a constant
16568 pointer. This can happen for example if an actual argument in an inlined
16569 function call evaluates to a compile-time constant address. */
16571 static bool
16572 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16573 enum dwarf_attribute attr)
16575 rtx rtl;
16576 dw_loc_list_ref list;
16577 var_loc_list *loc_list;
16579 if (TREE_CODE (decl) == ERROR_MARK)
16580 return false;
16582 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16583 || TREE_CODE (decl) == RESULT_DECL);
16585 /* Try to get some constant RTL for this decl, and use that as the value of
16586 the location. */
16588 rtl = rtl_for_decl_location (decl);
16589 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16590 && add_const_value_attribute (die, rtl))
16591 return true;
16593 /* See if we have single element location list that is equivalent to
16594 a constant value. That way we are better to use add_const_value_attribute
16595 rather than expanding constant value equivalent. */
16596 loc_list = lookup_decl_loc (decl);
16597 if (loc_list
16598 && loc_list->first
16599 && loc_list->first->next == NULL
16600 && NOTE_P (loc_list->first->loc)
16601 && NOTE_VAR_LOCATION (loc_list->first->loc)
16602 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16604 struct var_loc_node *node;
16606 node = loc_list->first;
16607 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16608 if (GET_CODE (rtl) == EXPR_LIST)
16609 rtl = XEXP (rtl, 0);
16610 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16611 && add_const_value_attribute (die, rtl))
16612 return true;
16614 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16615 if (list)
16617 add_AT_location_description (die, attr, list);
16618 return true;
16620 /* None of that worked, so it must not really have a location;
16621 try adding a constant value attribute from the DECL_INITIAL. */
16622 return tree_add_const_value_attribute_for_decl (die, decl);
16625 /* Add VARIABLE and DIE into deferred locations list. */
16627 static void
16628 defer_location (tree variable, dw_die_ref die)
16630 deferred_locations entry;
16631 entry.variable = variable;
16632 entry.die = die;
16633 VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16636 /* Helper function for tree_add_const_value_attribute. Natively encode
16637 initializer INIT into an array. Return true if successful. */
16639 static bool
16640 native_encode_initializer (tree init, unsigned char *array, int size)
16642 tree type;
16644 if (init == NULL_TREE)
16645 return false;
16647 STRIP_NOPS (init);
16648 switch (TREE_CODE (init))
16650 case STRING_CST:
16651 type = TREE_TYPE (init);
16652 if (TREE_CODE (type) == ARRAY_TYPE)
16654 tree enttype = TREE_TYPE (type);
16655 enum machine_mode mode = TYPE_MODE (enttype);
16657 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16658 return false;
16659 if (int_size_in_bytes (type) != size)
16660 return false;
16661 if (size > TREE_STRING_LENGTH (init))
16663 memcpy (array, TREE_STRING_POINTER (init),
16664 TREE_STRING_LENGTH (init));
16665 memset (array + TREE_STRING_LENGTH (init),
16666 '\0', size - TREE_STRING_LENGTH (init));
16668 else
16669 memcpy (array, TREE_STRING_POINTER (init), size);
16670 return true;
16672 return false;
16673 case CONSTRUCTOR:
16674 type = TREE_TYPE (init);
16675 if (int_size_in_bytes (type) != size)
16676 return false;
16677 if (TREE_CODE (type) == ARRAY_TYPE)
16679 HOST_WIDE_INT min_index;
16680 unsigned HOST_WIDE_INT cnt;
16681 int curpos = 0, fieldsize;
16682 constructor_elt *ce;
16684 if (TYPE_DOMAIN (type) == NULL_TREE
16685 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
16686 return false;
16688 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16689 if (fieldsize <= 0)
16690 return false;
16692 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
16693 memset (array, '\0', size);
16694 for (cnt = 0;
16695 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
16696 cnt++)
16698 tree val = ce->value;
16699 tree index = ce->index;
16700 int pos = curpos;
16701 if (index && TREE_CODE (index) == RANGE_EXPR)
16702 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
16703 * fieldsize;
16704 else if (index)
16705 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
16707 if (val)
16709 STRIP_NOPS (val);
16710 if (!native_encode_initializer (val, array + pos, fieldsize))
16711 return false;
16713 curpos = pos + fieldsize;
16714 if (index && TREE_CODE (index) == RANGE_EXPR)
16716 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
16717 - tree_low_cst (TREE_OPERAND (index, 0), 0);
16718 while (count > 0)
16720 if (val)
16721 memcpy (array + curpos, array + pos, fieldsize);
16722 curpos += fieldsize;
16725 gcc_assert (curpos <= size);
16727 return true;
16729 else if (TREE_CODE (type) == RECORD_TYPE
16730 || TREE_CODE (type) == UNION_TYPE)
16732 tree field = NULL_TREE;
16733 unsigned HOST_WIDE_INT cnt;
16734 constructor_elt *ce;
16736 if (int_size_in_bytes (type) != size)
16737 return false;
16739 if (TREE_CODE (type) == RECORD_TYPE)
16740 field = TYPE_FIELDS (type);
16742 for (cnt = 0;
16743 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
16744 cnt++, field = field ? DECL_CHAIN (field) : 0)
16746 tree val = ce->value;
16747 int pos, fieldsize;
16749 if (ce->index != 0)
16750 field = ce->index;
16752 if (val)
16753 STRIP_NOPS (val);
16755 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16756 return false;
16758 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16759 && TYPE_DOMAIN (TREE_TYPE (field))
16760 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16761 return false;
16762 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16763 || !host_integerp (DECL_SIZE_UNIT (field), 0))
16764 return false;
16765 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
16766 pos = int_byte_position (field);
16767 gcc_assert (pos + fieldsize <= size);
16768 if (val
16769 && !native_encode_initializer (val, array + pos, fieldsize))
16770 return false;
16772 return true;
16774 return false;
16775 case VIEW_CONVERT_EXPR:
16776 case NON_LVALUE_EXPR:
16777 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16778 default:
16779 return native_encode_expr (init, array, size) == size;
16783 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16784 attribute is the const value T. */
16786 static bool
16787 tree_add_const_value_attribute (dw_die_ref die, tree t)
16789 tree init;
16790 tree type = TREE_TYPE (t);
16791 rtx rtl;
16793 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16794 return false;
16796 init = t;
16797 gcc_assert (!DECL_P (init));
16799 rtl = rtl_for_decl_init (init, type);
16800 if (rtl)
16801 return add_const_value_attribute (die, rtl);
16802 /* If the host and target are sane, try harder. */
16803 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16804 && initializer_constant_valid_p (init, type))
16806 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16807 if (size > 0 && (int) size == size)
16809 unsigned char *array = (unsigned char *)
16810 ggc_alloc_cleared_atomic (size);
16812 if (native_encode_initializer (init, array, size))
16814 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16815 return true;
16819 return false;
16822 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16823 attribute is the const value of T, where T is an integral constant
16824 variable with static storage duration
16825 (so it can't be a PARM_DECL or a RESULT_DECL). */
16827 static bool
16828 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16831 if (!decl
16832 || (TREE_CODE (decl) != VAR_DECL
16833 && TREE_CODE (decl) != CONST_DECL))
16834 return false;
16836 if (TREE_READONLY (decl)
16837 && ! TREE_THIS_VOLATILE (decl)
16838 && DECL_INITIAL (decl))
16839 /* OK */;
16840 else
16841 return false;
16843 /* Don't add DW_AT_const_value if abstract origin already has one. */
16844 if (get_AT (var_die, DW_AT_const_value))
16845 return false;
16847 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16850 /* Convert the CFI instructions for the current function into a
16851 location list. This is used for DW_AT_frame_base when we targeting
16852 a dwarf2 consumer that does not support the dwarf3
16853 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16854 expressions. */
16856 static dw_loc_list_ref
16857 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16859 dw_fde_ref fde;
16860 dw_loc_list_ref list, *list_tail;
16861 dw_cfi_ref cfi;
16862 dw_cfa_location last_cfa, next_cfa;
16863 const char *start_label, *last_label, *section;
16864 dw_cfa_location remember;
16866 fde = current_fde ();
16867 gcc_assert (fde != NULL);
16869 section = secname_for_decl (current_function_decl);
16870 list_tail = &list;
16871 list = NULL;
16873 memset (&next_cfa, 0, sizeof (next_cfa));
16874 next_cfa.reg = INVALID_REGNUM;
16875 remember = next_cfa;
16877 start_label = fde->dw_fde_begin;
16879 /* ??? Bald assumption that the CIE opcode list does not contain
16880 advance opcodes. */
16881 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
16882 lookup_cfa_1 (cfi, &next_cfa, &remember);
16884 last_cfa = next_cfa;
16885 last_label = start_label;
16887 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
16888 switch (cfi->dw_cfi_opc)
16890 case DW_CFA_set_loc:
16891 case DW_CFA_advance_loc1:
16892 case DW_CFA_advance_loc2:
16893 case DW_CFA_advance_loc4:
16894 if (!cfa_equal_p (&last_cfa, &next_cfa))
16896 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16897 start_label, last_label, section);
16899 list_tail = &(*list_tail)->dw_loc_next;
16900 last_cfa = next_cfa;
16901 start_label = last_label;
16903 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16904 break;
16906 case DW_CFA_advance_loc:
16907 /* The encoding is complex enough that we should never emit this. */
16908 gcc_unreachable ();
16910 default:
16911 lookup_cfa_1 (cfi, &next_cfa, &remember);
16912 break;
16915 if (!cfa_equal_p (&last_cfa, &next_cfa))
16917 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16918 start_label, last_label, section);
16919 list_tail = &(*list_tail)->dw_loc_next;
16920 start_label = last_label;
16923 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16924 start_label, fde->dw_fde_end, section);
16926 if (list && list->dw_loc_next)
16927 gen_llsym (list);
16929 return list;
16932 /* Compute a displacement from the "steady-state frame pointer" to the
16933 frame base (often the same as the CFA), and store it in
16934 frame_pointer_fb_offset. OFFSET is added to the displacement
16935 before the latter is negated. */
16937 static void
16938 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16940 rtx reg, elim;
16942 #ifdef FRAME_POINTER_CFA_OFFSET
16943 reg = frame_pointer_rtx;
16944 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16945 #else
16946 reg = arg_pointer_rtx;
16947 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16948 #endif
16950 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
16951 if (GET_CODE (elim) == PLUS)
16953 offset += INTVAL (XEXP (elim, 1));
16954 elim = XEXP (elim, 0);
16957 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
16958 && (elim == hard_frame_pointer_rtx
16959 || elim == stack_pointer_rtx))
16960 || elim == (frame_pointer_needed
16961 ? hard_frame_pointer_rtx
16962 : stack_pointer_rtx));
16964 frame_pointer_fb_offset = -offset;
16967 /* Generate a DW_AT_name attribute given some string value to be included as
16968 the value of the attribute. */
16970 static void
16971 add_name_attribute (dw_die_ref die, const char *name_string)
16973 if (name_string != NULL && *name_string != 0)
16975 if (demangle_name_func)
16976 name_string = (*demangle_name_func) (name_string);
16978 add_AT_string (die, DW_AT_name, name_string);
16982 /* Generate a DW_AT_comp_dir attribute for DIE. */
16984 static void
16985 add_comp_dir_attribute (dw_die_ref die)
16987 const char *wd = get_src_pwd ();
16988 char *wd1;
16990 if (wd == NULL)
16991 return;
16993 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16995 int wdlen;
16997 wdlen = strlen (wd);
16998 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16999 strcpy (wd1, wd);
17000 wd1 [wdlen] = DIR_SEPARATOR;
17001 wd1 [wdlen + 1] = 0;
17002 wd = wd1;
17005 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17008 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17009 default. */
17011 static int
17012 lower_bound_default (void)
17014 switch (get_AT_unsigned (comp_unit_die, DW_AT_language))
17016 case DW_LANG_C:
17017 case DW_LANG_C89:
17018 case DW_LANG_C99:
17019 case DW_LANG_C_plus_plus:
17020 case DW_LANG_ObjC:
17021 case DW_LANG_ObjC_plus_plus:
17022 case DW_LANG_Java:
17023 return 0;
17024 case DW_LANG_Fortran77:
17025 case DW_LANG_Fortran90:
17026 case DW_LANG_Fortran95:
17027 return 1;
17028 case DW_LANG_UPC:
17029 case DW_LANG_D:
17030 case DW_LANG_Python:
17031 return dwarf_version >= 4 ? 0 : -1;
17032 case DW_LANG_Ada95:
17033 case DW_LANG_Ada83:
17034 case DW_LANG_Cobol74:
17035 case DW_LANG_Cobol85:
17036 case DW_LANG_Pascal83:
17037 case DW_LANG_Modula2:
17038 case DW_LANG_PLI:
17039 return dwarf_version >= 4 ? 1 : -1;
17040 default:
17041 return -1;
17045 /* Given a tree node describing an array bound (either lower or upper) output
17046 a representation for that bound. */
17048 static void
17049 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17051 switch (TREE_CODE (bound))
17053 case ERROR_MARK:
17054 return;
17056 /* All fixed-bounds are represented by INTEGER_CST nodes. */
17057 case INTEGER_CST:
17059 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17060 int dflt;
17062 /* Use the default if possible. */
17063 if (bound_attr == DW_AT_lower_bound
17064 && host_integerp (bound, 0)
17065 && (dflt = lower_bound_default ()) != -1
17066 && tree_low_cst (bound, 0) == dflt)
17069 /* Otherwise represent the bound as an unsigned value with the
17070 precision of its type. The precision and signedness of the
17071 type will be necessary to re-interpret it unambiguously. */
17072 else if (prec < HOST_BITS_PER_WIDE_INT)
17074 unsigned HOST_WIDE_INT mask
17075 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17076 add_AT_unsigned (subrange_die, bound_attr,
17077 TREE_INT_CST_LOW (bound) & mask);
17079 else if (prec == HOST_BITS_PER_WIDE_INT
17080 || TREE_INT_CST_HIGH (bound) == 0)
17081 add_AT_unsigned (subrange_die, bound_attr,
17082 TREE_INT_CST_LOW (bound));
17083 else
17084 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17085 TREE_INT_CST_LOW (bound));
17087 break;
17089 CASE_CONVERT:
17090 case VIEW_CONVERT_EXPR:
17091 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17092 break;
17094 case SAVE_EXPR:
17095 break;
17097 case VAR_DECL:
17098 case PARM_DECL:
17099 case RESULT_DECL:
17101 dw_die_ref decl_die = lookup_decl_die (bound);
17103 /* ??? Can this happen, or should the variable have been bound
17104 first? Probably it can, since I imagine that we try to create
17105 the types of parameters in the order in which they exist in
17106 the list, and won't have created a forward reference to a
17107 later parameter. */
17108 if (decl_die != NULL)
17110 add_AT_die_ref (subrange_die, bound_attr, decl_die);
17111 break;
17114 /* FALLTHRU */
17116 default:
17118 /* Otherwise try to create a stack operation procedure to
17119 evaluate the value of the array bound. */
17121 dw_die_ref ctx, decl_die;
17122 dw_loc_list_ref list;
17124 list = loc_list_from_tree (bound, 2);
17125 if (list == NULL || single_element_loc_list_p (list))
17127 /* If DW_AT_*bound is not a reference nor constant, it is
17128 a DWARF expression rather than location description.
17129 For that loc_list_from_tree (bound, 0) is needed.
17130 If that fails to give a single element list,
17131 fall back to outputting this as a reference anyway. */
17132 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17133 if (list2 && single_element_loc_list_p (list2))
17135 add_AT_loc (subrange_die, bound_attr, list2->expr);
17136 break;
17139 if (list == NULL)
17140 break;
17142 if (current_function_decl == 0)
17143 ctx = comp_unit_die;
17144 else
17145 ctx = lookup_decl_die (current_function_decl);
17147 decl_die = new_die (DW_TAG_variable, ctx, bound);
17148 add_AT_flag (decl_die, DW_AT_artificial, 1);
17149 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17150 add_AT_location_description (decl_die, DW_AT_location, list);
17151 add_AT_die_ref (subrange_die, bound_attr, decl_die);
17152 break;
17157 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17158 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17159 Note that the block of subscript information for an array type also
17160 includes information about the element type of the given array type. */
17162 static void
17163 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17165 unsigned dimension_number;
17166 tree lower, upper;
17167 dw_die_ref subrange_die;
17169 for (dimension_number = 0;
17170 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17171 type = TREE_TYPE (type), dimension_number++)
17173 tree domain = TYPE_DOMAIN (type);
17175 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17176 break;
17178 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17179 and (in GNU C only) variable bounds. Handle all three forms
17180 here. */
17181 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17182 if (domain)
17184 /* We have an array type with specified bounds. */
17185 lower = TYPE_MIN_VALUE (domain);
17186 upper = TYPE_MAX_VALUE (domain);
17188 /* Define the index type. */
17189 if (TREE_TYPE (domain))
17191 /* ??? This is probably an Ada unnamed subrange type. Ignore the
17192 TREE_TYPE field. We can't emit debug info for this
17193 because it is an unnamed integral type. */
17194 if (TREE_CODE (domain) == INTEGER_TYPE
17195 && TYPE_NAME (domain) == NULL_TREE
17196 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17197 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17199 else
17200 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17201 type_die);
17204 /* ??? If upper is NULL, the array has unspecified length,
17205 but it does have a lower bound. This happens with Fortran
17206 dimension arr(N:*)
17207 Since the debugger is definitely going to need to know N
17208 to produce useful results, go ahead and output the lower
17209 bound solo, and hope the debugger can cope. */
17211 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17212 if (upper)
17213 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17216 /* Otherwise we have an array type with an unspecified length. The
17217 DWARF-2 spec does not say how to handle this; let's just leave out the
17218 bounds. */
17222 static void
17223 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17225 unsigned size;
17227 switch (TREE_CODE (tree_node))
17229 case ERROR_MARK:
17230 size = 0;
17231 break;
17232 case ENUMERAL_TYPE:
17233 case RECORD_TYPE:
17234 case UNION_TYPE:
17235 case QUAL_UNION_TYPE:
17236 size = int_size_in_bytes (tree_node);
17237 break;
17238 case FIELD_DECL:
17239 /* For a data member of a struct or union, the DW_AT_byte_size is
17240 generally given as the number of bytes normally allocated for an
17241 object of the *declared* type of the member itself. This is true
17242 even for bit-fields. */
17243 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17244 break;
17245 default:
17246 gcc_unreachable ();
17249 /* Note that `size' might be -1 when we get to this point. If it is, that
17250 indicates that the byte size of the entity in question is variable. We
17251 have no good way of expressing this fact in Dwarf at the present time,
17252 so just let the -1 pass on through. */
17253 add_AT_unsigned (die, DW_AT_byte_size, size);
17256 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17257 which specifies the distance in bits from the highest order bit of the
17258 "containing object" for the bit-field to the highest order bit of the
17259 bit-field itself.
17261 For any given bit-field, the "containing object" is a hypothetical object
17262 (of some integral or enum type) within which the given bit-field lives. The
17263 type of this hypothetical "containing object" is always the same as the
17264 declared type of the individual bit-field itself. The determination of the
17265 exact location of the "containing object" for a bit-field is rather
17266 complicated. It's handled by the `field_byte_offset' function (above).
17268 Note that it is the size (in bytes) of the hypothetical "containing object"
17269 which will be given in the DW_AT_byte_size attribute for this bit-field.
17270 (See `byte_size_attribute' above). */
17272 static inline void
17273 add_bit_offset_attribute (dw_die_ref die, tree decl)
17275 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17276 tree type = DECL_BIT_FIELD_TYPE (decl);
17277 HOST_WIDE_INT bitpos_int;
17278 HOST_WIDE_INT highest_order_object_bit_offset;
17279 HOST_WIDE_INT highest_order_field_bit_offset;
17280 HOST_WIDE_INT unsigned bit_offset;
17282 /* Must be a field and a bit field. */
17283 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17285 /* We can't yet handle bit-fields whose offsets are variable, so if we
17286 encounter such things, just return without generating any attribute
17287 whatsoever. Likewise for variable or too large size. */
17288 if (! host_integerp (bit_position (decl), 0)
17289 || ! host_integerp (DECL_SIZE (decl), 1))
17290 return;
17292 bitpos_int = int_bit_position (decl);
17294 /* Note that the bit offset is always the distance (in bits) from the
17295 highest-order bit of the "containing object" to the highest-order bit of
17296 the bit-field itself. Since the "high-order end" of any object or field
17297 is different on big-endian and little-endian machines, the computation
17298 below must take account of these differences. */
17299 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17300 highest_order_field_bit_offset = bitpos_int;
17302 if (! BYTES_BIG_ENDIAN)
17304 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17305 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17308 bit_offset
17309 = (! BYTES_BIG_ENDIAN
17310 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17311 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17313 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17316 /* For a FIELD_DECL node which represents a bit field, output an attribute
17317 which specifies the length in bits of the given field. */
17319 static inline void
17320 add_bit_size_attribute (dw_die_ref die, tree decl)
17322 /* Must be a field and a bit field. */
17323 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17324 && DECL_BIT_FIELD_TYPE (decl));
17326 if (host_integerp (DECL_SIZE (decl), 1))
17327 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17330 /* If the compiled language is ANSI C, then add a 'prototyped'
17331 attribute, if arg types are given for the parameters of a function. */
17333 static inline void
17334 add_prototyped_attribute (dw_die_ref die, tree func_type)
17336 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
17337 && TYPE_ARG_TYPES (func_type) != NULL)
17338 add_AT_flag (die, DW_AT_prototyped, 1);
17341 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17342 by looking in either the type declaration or object declaration
17343 equate table. */
17345 static inline dw_die_ref
17346 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17348 dw_die_ref origin_die = NULL;
17350 if (TREE_CODE (origin) != FUNCTION_DECL)
17352 /* We may have gotten separated from the block for the inlined
17353 function, if we're in an exception handler or some such; make
17354 sure that the abstract function has been written out.
17356 Doing this for nested functions is wrong, however; functions are
17357 distinct units, and our context might not even be inline. */
17358 tree fn = origin;
17360 if (TYPE_P (fn))
17361 fn = TYPE_STUB_DECL (fn);
17363 fn = decl_function_context (fn);
17364 if (fn)
17365 dwarf2out_abstract_function (fn);
17368 if (DECL_P (origin))
17369 origin_die = lookup_decl_die (origin);
17370 else if (TYPE_P (origin))
17371 origin_die = lookup_type_die (origin);
17373 /* XXX: Functions that are never lowered don't always have correct block
17374 trees (in the case of java, they simply have no block tree, in some other
17375 languages). For these functions, there is nothing we can really do to
17376 output correct debug info for inlined functions in all cases. Rather
17377 than die, we'll just produce deficient debug info now, in that we will
17378 have variables without a proper abstract origin. In the future, when all
17379 functions are lowered, we should re-add a gcc_assert (origin_die)
17380 here. */
17382 if (origin_die)
17383 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17384 return origin_die;
17387 /* We do not currently support the pure_virtual attribute. */
17389 static inline void
17390 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17392 if (DECL_VINDEX (func_decl))
17394 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17396 if (host_integerp (DECL_VINDEX (func_decl), 0))
17397 add_AT_loc (die, DW_AT_vtable_elem_location,
17398 new_loc_descr (DW_OP_constu,
17399 tree_low_cst (DECL_VINDEX (func_decl), 0),
17400 0));
17402 /* GNU extension: Record what type this method came from originally. */
17403 if (debug_info_level > DINFO_LEVEL_TERSE
17404 && DECL_CONTEXT (func_decl))
17405 add_AT_die_ref (die, DW_AT_containing_type,
17406 lookup_type_die (DECL_CONTEXT (func_decl)));
17410 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17411 given decl. This used to be a vendor extension until after DWARF 4
17412 standardized it. */
17414 static void
17415 add_linkage_attr (dw_die_ref die, tree decl)
17417 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17419 /* Mimic what assemble_name_raw does with a leading '*'. */
17420 if (name[0] == '*')
17421 name = &name[1];
17423 if (dwarf_version >= 4)
17424 add_AT_string (die, DW_AT_linkage_name, name);
17425 else
17426 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17429 /* Add source coordinate attributes for the given decl. */
17431 static void
17432 add_src_coords_attributes (dw_die_ref die, tree decl)
17434 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17436 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17437 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17440 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17442 static void
17443 add_linkage_name (dw_die_ref die, tree decl)
17445 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17446 && TREE_PUBLIC (decl)
17447 && !DECL_ABSTRACT (decl)
17448 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17449 && die->die_tag != DW_TAG_member)
17451 /* Defer until we have an assembler name set. */
17452 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17454 limbo_die_node *asm_name;
17456 asm_name = ggc_alloc_cleared_limbo_die_node ();
17457 asm_name->die = die;
17458 asm_name->created_for = decl;
17459 asm_name->next = deferred_asm_name;
17460 deferred_asm_name = asm_name;
17462 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17463 add_linkage_attr (die, decl);
17467 /* Add a DW_AT_name attribute and source coordinate attribute for the
17468 given decl, but only if it actually has a name. */
17470 static void
17471 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17473 tree decl_name;
17475 decl_name = DECL_NAME (decl);
17476 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17478 const char *name = dwarf2_name (decl, 0);
17479 if (name)
17480 add_name_attribute (die, name);
17481 if (! DECL_ARTIFICIAL (decl))
17482 add_src_coords_attributes (die, decl);
17484 add_linkage_name (die, decl);
17487 #ifdef VMS_DEBUGGING_INFO
17488 /* Get the function's name, as described by its RTL. This may be different
17489 from the DECL_NAME name used in the source file. */
17490 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17492 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17493 XEXP (DECL_RTL (decl), 0));
17494 VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17496 #endif
17499 #ifdef VMS_DEBUGGING_INFO
17501 /* Output the debug main pointer die for VMS */
17503 void
17504 dwarf2out_vms_debug_main_pointer (void)
17506 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17507 dw_die_ref die;
17509 /* Allocate the VMS debug main subprogram die. */
17510 die = ggc_alloc_cleared_die_node ();
17511 die->die_tag = DW_TAG_subprogram;
17512 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17513 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17514 current_function_funcdef_no);
17515 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17517 /* Make it the first child of comp_unit_die. */
17518 die->die_parent = comp_unit_die;
17519 if (comp_unit_die->die_child)
17521 die->die_sib = comp_unit_die->die_child->die_sib;
17522 comp_unit_die->die_child->die_sib = die;
17524 else
17526 die->die_sib = die;
17527 comp_unit_die->die_child = die;
17530 #endif
17532 /* Push a new declaration scope. */
17534 static void
17535 push_decl_scope (tree scope)
17537 VEC_safe_push (tree, gc, decl_scope_table, scope);
17540 /* Pop a declaration scope. */
17542 static inline void
17543 pop_decl_scope (void)
17545 VEC_pop (tree, decl_scope_table);
17548 /* Return the DIE for the scope that immediately contains this type.
17549 Non-named types get global scope. Named types nested in other
17550 types get their containing scope if it's open, or global scope
17551 otherwise. All other types (i.e. function-local named types) get
17552 the current active scope. */
17554 static dw_die_ref
17555 scope_die_for (tree t, dw_die_ref context_die)
17557 dw_die_ref scope_die = NULL;
17558 tree containing_scope;
17559 int i;
17561 /* Non-types always go in the current scope. */
17562 gcc_assert (TYPE_P (t));
17564 containing_scope = TYPE_CONTEXT (t);
17566 /* Use the containing namespace if it was passed in (for a declaration). */
17567 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17569 if (context_die == lookup_decl_die (containing_scope))
17570 /* OK */;
17571 else
17572 containing_scope = NULL_TREE;
17575 /* Ignore function type "scopes" from the C frontend. They mean that
17576 a tagged type is local to a parmlist of a function declarator, but
17577 that isn't useful to DWARF. */
17578 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17579 containing_scope = NULL_TREE;
17581 if (containing_scope == NULL_TREE)
17582 scope_die = comp_unit_die;
17583 else if (TYPE_P (containing_scope))
17585 /* For types, we can just look up the appropriate DIE. But
17586 first we check to see if we're in the middle of emitting it
17587 so we know where the new DIE should go. */
17588 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17589 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17590 break;
17592 if (i < 0)
17594 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17595 || TREE_ASM_WRITTEN (containing_scope));
17596 /*We are not in the middle of emitting the type
17597 CONTAINING_SCOPE. Let's see if it's emitted already. */
17598 scope_die = lookup_type_die (containing_scope);
17600 /* If none of the current dies are suitable, we get file scope. */
17601 if (scope_die == NULL)
17602 scope_die = comp_unit_die;
17604 else
17605 scope_die = lookup_type_die (containing_scope);
17607 else
17608 scope_die = context_die;
17610 return scope_die;
17613 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17615 static inline int
17616 local_scope_p (dw_die_ref context_die)
17618 for (; context_die; context_die = context_die->die_parent)
17619 if (context_die->die_tag == DW_TAG_inlined_subroutine
17620 || context_die->die_tag == DW_TAG_subprogram)
17621 return 1;
17623 return 0;
17626 /* Returns nonzero if CONTEXT_DIE is a class. */
17628 static inline int
17629 class_scope_p (dw_die_ref context_die)
17631 return (context_die
17632 && (context_die->die_tag == DW_TAG_structure_type
17633 || context_die->die_tag == DW_TAG_class_type
17634 || context_die->die_tag == DW_TAG_interface_type
17635 || context_die->die_tag == DW_TAG_union_type));
17638 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17639 whether or not to treat a DIE in this context as a declaration. */
17641 static inline int
17642 class_or_namespace_scope_p (dw_die_ref context_die)
17644 return (class_scope_p (context_die)
17645 || (context_die && context_die->die_tag == DW_TAG_namespace));
17648 /* Many forms of DIEs require a "type description" attribute. This
17649 routine locates the proper "type descriptor" die for the type given
17650 by 'type', and adds a DW_AT_type attribute below the given die. */
17652 static void
17653 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17654 int decl_volatile, dw_die_ref context_die)
17656 enum tree_code code = TREE_CODE (type);
17657 dw_die_ref type_die = NULL;
17659 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17660 or fixed-point type, use the inner type. This is because we have no
17661 support for unnamed types in base_type_die. This can happen if this is
17662 an Ada subrange type. Correct solution is emit a subrange type die. */
17663 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17664 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17665 type = TREE_TYPE (type), code = TREE_CODE (type);
17667 if (code == ERROR_MARK
17668 /* Handle a special case. For functions whose return type is void, we
17669 generate *no* type attribute. (Note that no object may have type
17670 `void', so this only applies to function return types). */
17671 || code == VOID_TYPE)
17672 return;
17674 type_die = modified_type_die (type,
17675 decl_const || TYPE_READONLY (type),
17676 decl_volatile || TYPE_VOLATILE (type),
17677 context_die);
17679 if (type_die != NULL)
17680 add_AT_die_ref (object_die, DW_AT_type, type_die);
17683 /* Given an object die, add the calling convention attribute for the
17684 function call type. */
17685 static void
17686 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17688 enum dwarf_calling_convention value = DW_CC_normal;
17690 value = ((enum dwarf_calling_convention)
17691 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17693 /* DWARF doesn't provide a way to identify a program's source-level
17694 entry point. DW_AT_calling_convention attributes are only meant
17695 to describe functions' calling conventions. However, lacking a
17696 better way to signal the Fortran main program, we use this for the
17697 time being, following existing custom. */
17698 if (is_fortran ()
17699 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17700 value = DW_CC_program;
17702 /* Only add the attribute if the backend requests it, and
17703 is not DW_CC_normal. */
17704 if (value && (value != DW_CC_normal))
17705 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17708 /* Given a tree pointer to a struct, class, union, or enum type node, return
17709 a pointer to the (string) tag name for the given type, or zero if the type
17710 was declared without a tag. */
17712 static const char *
17713 type_tag (const_tree type)
17715 const char *name = 0;
17717 if (TYPE_NAME (type) != 0)
17719 tree t = 0;
17721 /* Find the IDENTIFIER_NODE for the type name. */
17722 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17723 && !TYPE_NAMELESS (type))
17724 t = TYPE_NAME (type);
17726 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17727 a TYPE_DECL node, regardless of whether or not a `typedef' was
17728 involved. */
17729 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17730 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17732 /* We want to be extra verbose. Don't call dwarf_name if
17733 DECL_NAME isn't set. The default hook for decl_printable_name
17734 doesn't like that, and in this context it's correct to return
17735 0, instead of "<anonymous>" or the like. */
17736 if (DECL_NAME (TYPE_NAME (type))
17737 && !DECL_NAMELESS (TYPE_NAME (type)))
17738 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17741 /* Now get the name as a string, or invent one. */
17742 if (!name && t != 0)
17743 name = IDENTIFIER_POINTER (t);
17746 return (name == 0 || *name == '\0') ? 0 : name;
17749 /* Return the type associated with a data member, make a special check
17750 for bit field types. */
17752 static inline tree
17753 member_declared_type (const_tree member)
17755 return (DECL_BIT_FIELD_TYPE (member)
17756 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17759 /* Get the decl's label, as described by its RTL. This may be different
17760 from the DECL_NAME name used in the source file. */
17762 #if 0
17763 static const char *
17764 decl_start_label (tree decl)
17766 rtx x;
17767 const char *fnname;
17769 x = DECL_RTL (decl);
17770 gcc_assert (MEM_P (x));
17772 x = XEXP (x, 0);
17773 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17775 fnname = XSTR (x, 0);
17776 return fnname;
17778 #endif
17780 /* These routines generate the internal representation of the DIE's for
17781 the compilation unit. Debugging information is collected by walking
17782 the declaration trees passed in from dwarf2out_decl(). */
17784 static void
17785 gen_array_type_die (tree type, dw_die_ref context_die)
17787 dw_die_ref scope_die = scope_die_for (type, context_die);
17788 dw_die_ref array_die;
17790 /* GNU compilers represent multidimensional array types as sequences of one
17791 dimensional array types whose element types are themselves array types.
17792 We sometimes squish that down to a single array_type DIE with multiple
17793 subscripts in the Dwarf debugging info. The draft Dwarf specification
17794 say that we are allowed to do this kind of compression in C, because
17795 there is no difference between an array of arrays and a multidimensional
17796 array. We don't do this for Ada to remain as close as possible to the
17797 actual representation, which is especially important against the language
17798 flexibilty wrt arrays of variable size. */
17800 bool collapse_nested_arrays = !is_ada ();
17801 tree element_type;
17803 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17804 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17805 if (TYPE_STRING_FLAG (type)
17806 && TREE_CODE (type) == ARRAY_TYPE
17807 && is_fortran ()
17808 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17810 HOST_WIDE_INT size;
17812 array_die = new_die (DW_TAG_string_type, scope_die, type);
17813 add_name_attribute (array_die, type_tag (type));
17814 equate_type_number_to_die (type, array_die);
17815 size = int_size_in_bytes (type);
17816 if (size >= 0)
17817 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17818 else if (TYPE_DOMAIN (type) != NULL_TREE
17819 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17820 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17822 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17823 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17825 size = int_size_in_bytes (TREE_TYPE (szdecl));
17826 if (loc && size > 0)
17828 add_AT_location_description (array_die, DW_AT_string_length, loc);
17829 if (size != DWARF2_ADDR_SIZE)
17830 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17833 return;
17836 /* ??? The SGI dwarf reader fails for array of array of enum types
17837 (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
17838 array type comes before the outer array type. We thus call gen_type_die
17839 before we new_die and must prevent nested array types collapsing for this
17840 target. */
17842 #ifdef MIPS_DEBUGGING_INFO
17843 gen_type_die (TREE_TYPE (type), context_die);
17844 collapse_nested_arrays = false;
17845 #endif
17847 array_die = new_die (DW_TAG_array_type, scope_die, type);
17848 add_name_attribute (array_die, type_tag (type));
17849 equate_type_number_to_die (type, array_die);
17851 if (TREE_CODE (type) == VECTOR_TYPE)
17853 /* The frontend feeds us a representation for the vector as a struct
17854 containing an array. Pull out the array type. */
17855 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
17856 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17859 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17860 if (is_fortran ()
17861 && TREE_CODE (type) == ARRAY_TYPE
17862 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17863 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17864 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17866 #if 0
17867 /* We default the array ordering. SDB will probably do
17868 the right things even if DW_AT_ordering is not present. It's not even
17869 an issue until we start to get into multidimensional arrays anyway. If
17870 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17871 then we'll have to put the DW_AT_ordering attribute back in. (But if
17872 and when we find out that we need to put these in, we will only do so
17873 for multidimensional arrays. */
17874 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17875 #endif
17877 #ifdef MIPS_DEBUGGING_INFO
17878 /* The SGI compilers handle arrays of unknown bound by setting
17879 AT_declaration and not emitting any subrange DIEs. */
17880 if (! TYPE_DOMAIN (type))
17881 add_AT_flag (array_die, DW_AT_declaration, 1);
17882 else
17883 #endif
17884 add_subscript_info (array_die, type, collapse_nested_arrays);
17886 /* Add representation of the type of the elements of this array type and
17887 emit the corresponding DIE if we haven't done it already. */
17888 element_type = TREE_TYPE (type);
17889 if (collapse_nested_arrays)
17890 while (TREE_CODE (element_type) == ARRAY_TYPE)
17892 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17893 break;
17894 element_type = TREE_TYPE (element_type);
17897 #ifndef MIPS_DEBUGGING_INFO
17898 gen_type_die (element_type, context_die);
17899 #endif
17901 add_type_attribute (array_die, element_type, 0, 0, context_die);
17903 if (get_AT (array_die, DW_AT_name))
17904 add_pubtype (type, array_die);
17907 static dw_loc_descr_ref
17908 descr_info_loc (tree val, tree base_decl)
17910 HOST_WIDE_INT size;
17911 dw_loc_descr_ref loc, loc2;
17912 enum dwarf_location_atom op;
17914 if (val == base_decl)
17915 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17917 switch (TREE_CODE (val))
17919 CASE_CONVERT:
17920 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17921 case VAR_DECL:
17922 return loc_descriptor_from_tree (val, 0);
17923 case INTEGER_CST:
17924 if (host_integerp (val, 0))
17925 return int_loc_descriptor (tree_low_cst (val, 0));
17926 break;
17927 case INDIRECT_REF:
17928 size = int_size_in_bytes (TREE_TYPE (val));
17929 if (size < 0)
17930 break;
17931 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17932 if (!loc)
17933 break;
17934 if (size == DWARF2_ADDR_SIZE)
17935 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17936 else
17937 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17938 return loc;
17939 case POINTER_PLUS_EXPR:
17940 case PLUS_EXPR:
17941 if (host_integerp (TREE_OPERAND (val, 1), 1)
17942 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
17943 < 16384)
17945 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17946 if (!loc)
17947 break;
17948 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
17950 else
17952 op = DW_OP_plus;
17953 do_binop:
17954 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17955 if (!loc)
17956 break;
17957 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17958 if (!loc2)
17959 break;
17960 add_loc_descr (&loc, loc2);
17961 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17963 return loc;
17964 case MINUS_EXPR:
17965 op = DW_OP_minus;
17966 goto do_binop;
17967 case MULT_EXPR:
17968 op = DW_OP_mul;
17969 goto do_binop;
17970 case EQ_EXPR:
17971 op = DW_OP_eq;
17972 goto do_binop;
17973 case NE_EXPR:
17974 op = DW_OP_ne;
17975 goto do_binop;
17976 default:
17977 break;
17979 return NULL;
17982 static void
17983 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17984 tree val, tree base_decl)
17986 dw_loc_descr_ref loc;
17988 if (host_integerp (val, 0))
17990 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
17991 return;
17994 loc = descr_info_loc (val, base_decl);
17995 if (!loc)
17996 return;
17998 add_AT_loc (die, attr, loc);
18001 /* This routine generates DIE for array with hidden descriptor, details
18002 are filled into *info by a langhook. */
18004 static void
18005 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18006 dw_die_ref context_die)
18008 dw_die_ref scope_die = scope_die_for (type, context_die);
18009 dw_die_ref array_die;
18010 int dim;
18012 array_die = new_die (DW_TAG_array_type, scope_die, type);
18013 add_name_attribute (array_die, type_tag (type));
18014 equate_type_number_to_die (type, array_die);
18016 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
18017 if (is_fortran ()
18018 && info->ndimensions >= 2)
18019 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18021 if (info->data_location)
18022 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18023 info->base_decl);
18024 if (info->associated)
18025 add_descr_info_field (array_die, DW_AT_associated, info->associated,
18026 info->base_decl);
18027 if (info->allocated)
18028 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18029 info->base_decl);
18031 for (dim = 0; dim < info->ndimensions; dim++)
18033 dw_die_ref subrange_die
18034 = new_die (DW_TAG_subrange_type, array_die, NULL);
18036 if (info->dimen[dim].lower_bound)
18038 /* If it is the default value, omit it. */
18039 int dflt;
18041 if (host_integerp (info->dimen[dim].lower_bound, 0)
18042 && (dflt = lower_bound_default ()) != -1
18043 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18045 else
18046 add_descr_info_field (subrange_die, DW_AT_lower_bound,
18047 info->dimen[dim].lower_bound,
18048 info->base_decl);
18050 if (info->dimen[dim].upper_bound)
18051 add_descr_info_field (subrange_die, DW_AT_upper_bound,
18052 info->dimen[dim].upper_bound,
18053 info->base_decl);
18054 if (info->dimen[dim].stride)
18055 add_descr_info_field (subrange_die, DW_AT_byte_stride,
18056 info->dimen[dim].stride,
18057 info->base_decl);
18060 gen_type_die (info->element_type, context_die);
18061 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18063 if (get_AT (array_die, DW_AT_name))
18064 add_pubtype (type, array_die);
18067 #if 0
18068 static void
18069 gen_entry_point_die (tree decl, dw_die_ref context_die)
18071 tree origin = decl_ultimate_origin (decl);
18072 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18074 if (origin != NULL)
18075 add_abstract_origin_attribute (decl_die, origin);
18076 else
18078 add_name_and_src_coords_attributes (decl_die, decl);
18079 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18080 0, 0, context_die);
18083 if (DECL_ABSTRACT (decl))
18084 equate_decl_number_to_die (decl, decl_die);
18085 else
18086 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18088 #endif
18090 /* Walk through the list of incomplete types again, trying once more to
18091 emit full debugging info for them. */
18093 static void
18094 retry_incomplete_types (void)
18096 int i;
18098 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18099 if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18100 DINFO_USAGE_DIR_USE))
18101 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
18104 /* Determine what tag to use for a record type. */
18106 static enum dwarf_tag
18107 record_type_tag (tree type)
18109 if (! lang_hooks.types.classify_record)
18110 return DW_TAG_structure_type;
18112 switch (lang_hooks.types.classify_record (type))
18114 case RECORD_IS_STRUCT:
18115 return DW_TAG_structure_type;
18117 case RECORD_IS_CLASS:
18118 return DW_TAG_class_type;
18120 case RECORD_IS_INTERFACE:
18121 if (dwarf_version >= 3 || !dwarf_strict)
18122 return DW_TAG_interface_type;
18123 return DW_TAG_structure_type;
18125 default:
18126 gcc_unreachable ();
18130 /* Generate a DIE to represent an enumeration type. Note that these DIEs
18131 include all of the information about the enumeration values also. Each
18132 enumerated type name/value is listed as a child of the enumerated type
18133 DIE. */
18135 static dw_die_ref
18136 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18138 dw_die_ref type_die = lookup_type_die (type);
18140 if (type_die == NULL)
18142 type_die = new_die (DW_TAG_enumeration_type,
18143 scope_die_for (type, context_die), type);
18144 equate_type_number_to_die (type, type_die);
18145 add_name_attribute (type_die, type_tag (type));
18146 if ((dwarf_version >= 4 || !dwarf_strict)
18147 && ENUM_IS_SCOPED (type))
18148 add_AT_flag (type_die, DW_AT_enum_class, 1);
18150 else if (! TYPE_SIZE (type))
18151 return type_die;
18152 else
18153 remove_AT (type_die, DW_AT_declaration);
18155 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
18156 given enum type is incomplete, do not generate the DW_AT_byte_size
18157 attribute or the DW_AT_element_list attribute. */
18158 if (TYPE_SIZE (type))
18160 tree link;
18162 TREE_ASM_WRITTEN (type) = 1;
18163 add_byte_size_attribute (type_die, type);
18164 if (TYPE_STUB_DECL (type) != NULL_TREE)
18166 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18167 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18170 /* If the first reference to this type was as the return type of an
18171 inline function, then it may not have a parent. Fix this now. */
18172 if (type_die->die_parent == NULL)
18173 add_child_die (scope_die_for (type, context_die), type_die);
18175 for (link = TYPE_VALUES (type);
18176 link != NULL; link = TREE_CHAIN (link))
18178 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18179 tree value = TREE_VALUE (link);
18181 add_name_attribute (enum_die,
18182 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18184 if (TREE_CODE (value) == CONST_DECL)
18185 value = DECL_INITIAL (value);
18187 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18188 /* DWARF2 does not provide a way of indicating whether or
18189 not enumeration constants are signed or unsigned. GDB
18190 always assumes the values are signed, so we output all
18191 values as if they were signed. That means that
18192 enumeration constants with very large unsigned values
18193 will appear to have negative values in the debugger. */
18194 add_AT_int (enum_die, DW_AT_const_value,
18195 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18198 else
18199 add_AT_flag (type_die, DW_AT_declaration, 1);
18201 if (get_AT (type_die, DW_AT_name))
18202 add_pubtype (type, type_die);
18204 return type_die;
18207 /* Generate a DIE to represent either a real live formal parameter decl or to
18208 represent just the type of some formal parameter position in some function
18209 type.
18211 Note that this routine is a bit unusual because its argument may be a
18212 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18213 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18214 node. If it's the former then this function is being called to output a
18215 DIE to represent a formal parameter object (or some inlining thereof). If
18216 it's the latter, then this function is only being called to output a
18217 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18218 argument type of some subprogram type.
18219 If EMIT_NAME_P is true, name and source coordinate attributes
18220 are emitted. */
18222 static dw_die_ref
18223 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18224 dw_die_ref context_die)
18226 tree node_or_origin = node ? node : origin;
18227 tree ultimate_origin;
18228 dw_die_ref parm_die
18229 = new_die (DW_TAG_formal_parameter, context_die, node);
18231 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18233 case tcc_declaration:
18234 ultimate_origin = decl_ultimate_origin (node_or_origin);
18235 if (node || ultimate_origin)
18236 origin = ultimate_origin;
18237 if (origin != NULL)
18238 add_abstract_origin_attribute (parm_die, origin);
18239 else if (emit_name_p)
18240 add_name_and_src_coords_attributes (parm_die, node);
18241 if (origin == NULL
18242 || (! DECL_ABSTRACT (node_or_origin)
18243 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18244 decl_function_context
18245 (node_or_origin))))
18247 tree type = TREE_TYPE (node_or_origin);
18248 if (decl_by_reference_p (node_or_origin))
18249 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18250 context_die);
18251 else
18252 add_type_attribute (parm_die, type,
18253 TREE_READONLY (node_or_origin),
18254 TREE_THIS_VOLATILE (node_or_origin),
18255 context_die);
18257 if (origin == NULL && DECL_ARTIFICIAL (node))
18258 add_AT_flag (parm_die, DW_AT_artificial, 1);
18260 if (node && node != origin)
18261 equate_decl_number_to_die (node, parm_die);
18262 if (! DECL_ABSTRACT (node_or_origin))
18263 add_location_or_const_value_attribute (parm_die, node_or_origin,
18264 DW_AT_location);
18266 break;
18268 case tcc_type:
18269 /* We were called with some kind of a ..._TYPE node. */
18270 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18271 break;
18273 default:
18274 gcc_unreachable ();
18277 return parm_die;
18280 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18281 children DW_TAG_formal_parameter DIEs representing the arguments of the
18282 parameter pack.
18284 PARM_PACK must be a function parameter pack.
18285 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18286 must point to the subsequent arguments of the function PACK_ARG belongs to.
18287 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18288 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18289 following the last one for which a DIE was generated. */
18291 static dw_die_ref
18292 gen_formal_parameter_pack_die (tree parm_pack,
18293 tree pack_arg,
18294 dw_die_ref subr_die,
18295 tree *next_arg)
18297 tree arg;
18298 dw_die_ref parm_pack_die;
18300 gcc_assert (parm_pack
18301 && lang_hooks.function_parameter_pack_p (parm_pack)
18302 && subr_die);
18304 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18305 add_src_coords_attributes (parm_pack_die, parm_pack);
18307 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18309 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18310 parm_pack))
18311 break;
18312 gen_formal_parameter_die (arg, NULL,
18313 false /* Don't emit name attribute. */,
18314 parm_pack_die);
18316 if (next_arg)
18317 *next_arg = arg;
18318 return parm_pack_die;
18321 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18322 at the end of an (ANSI prototyped) formal parameters list. */
18324 static void
18325 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18327 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18330 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18331 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18332 parameters as specified in some function type specification (except for
18333 those which appear as part of a function *definition*). */
18335 static void
18336 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18338 tree link;
18339 tree formal_type = NULL;
18340 tree first_parm_type;
18341 tree arg;
18343 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18345 arg = DECL_ARGUMENTS (function_or_method_type);
18346 function_or_method_type = TREE_TYPE (function_or_method_type);
18348 else
18349 arg = NULL_TREE;
18351 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18353 /* Make our first pass over the list of formal parameter types and output a
18354 DW_TAG_formal_parameter DIE for each one. */
18355 for (link = first_parm_type; link; )
18357 dw_die_ref parm_die;
18359 formal_type = TREE_VALUE (link);
18360 if (formal_type == void_type_node)
18361 break;
18363 /* Output a (nameless) DIE to represent the formal parameter itself. */
18364 parm_die = gen_formal_parameter_die (formal_type, NULL,
18365 true /* Emit name attribute. */,
18366 context_die);
18367 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
18368 && link == first_parm_type)
18369 || (arg && DECL_ARTIFICIAL (arg)))
18370 add_AT_flag (parm_die, DW_AT_artificial, 1);
18372 link = TREE_CHAIN (link);
18373 if (arg)
18374 arg = DECL_CHAIN (arg);
18377 /* If this function type has an ellipsis, add a
18378 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18379 if (formal_type != void_type_node)
18380 gen_unspecified_parameters_die (function_or_method_type, context_die);
18382 /* Make our second (and final) pass over the list of formal parameter types
18383 and output DIEs to represent those types (as necessary). */
18384 for (link = TYPE_ARG_TYPES (function_or_method_type);
18385 link && TREE_VALUE (link);
18386 link = TREE_CHAIN (link))
18387 gen_type_die (TREE_VALUE (link), context_die);
18390 /* We want to generate the DIE for TYPE so that we can generate the
18391 die for MEMBER, which has been defined; we will need to refer back
18392 to the member declaration nested within TYPE. If we're trying to
18393 generate minimal debug info for TYPE, processing TYPE won't do the
18394 trick; we need to attach the member declaration by hand. */
18396 static void
18397 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18399 gen_type_die (type, context_die);
18401 /* If we're trying to avoid duplicate debug info, we may not have
18402 emitted the member decl for this function. Emit it now. */
18403 if (TYPE_STUB_DECL (type)
18404 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18405 && ! lookup_decl_die (member))
18407 dw_die_ref type_die;
18408 gcc_assert (!decl_ultimate_origin (member));
18410 push_decl_scope (type);
18411 type_die = lookup_type_die (type);
18412 if (TREE_CODE (member) == FUNCTION_DECL)
18413 gen_subprogram_die (member, type_die);
18414 else if (TREE_CODE (member) == FIELD_DECL)
18416 /* Ignore the nameless fields that are used to skip bits but handle
18417 C++ anonymous unions and structs. */
18418 if (DECL_NAME (member) != NULL_TREE
18419 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18420 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18422 gen_type_die (member_declared_type (member), type_die);
18423 gen_field_die (member, type_die);
18426 else
18427 gen_variable_die (member, NULL_TREE, type_die);
18429 pop_decl_scope ();
18433 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18434 may later generate inlined and/or out-of-line instances of. */
18436 static void
18437 dwarf2out_abstract_function (tree decl)
18439 dw_die_ref old_die;
18440 tree save_fn;
18441 tree context;
18442 int was_abstract;
18443 htab_t old_decl_loc_table;
18445 /* Make sure we have the actual abstract inline, not a clone. */
18446 decl = DECL_ORIGIN (decl);
18448 old_die = lookup_decl_die (decl);
18449 if (old_die && get_AT (old_die, DW_AT_inline))
18450 /* We've already generated the abstract instance. */
18451 return;
18453 /* We can be called while recursively when seeing block defining inlined subroutine
18454 DIE. Be sure to not clobber the outer location table nor use it or we would
18455 get locations in abstract instantces. */
18456 old_decl_loc_table = decl_loc_table;
18457 decl_loc_table = NULL;
18459 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18460 we don't get confused by DECL_ABSTRACT. */
18461 if (debug_info_level > DINFO_LEVEL_TERSE)
18463 context = decl_class_context (decl);
18464 if (context)
18465 gen_type_die_for_member
18466 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
18469 /* Pretend we've just finished compiling this function. */
18470 save_fn = current_function_decl;
18471 current_function_decl = decl;
18472 push_cfun (DECL_STRUCT_FUNCTION (decl));
18474 was_abstract = DECL_ABSTRACT (decl);
18475 set_decl_abstract_flags (decl, 1);
18476 dwarf2out_decl (decl);
18477 if (! was_abstract)
18478 set_decl_abstract_flags (decl, 0);
18480 current_function_decl = save_fn;
18481 decl_loc_table = old_decl_loc_table;
18482 pop_cfun ();
18485 /* Helper function of premark_used_types() which gets called through
18486 htab_traverse.
18488 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18489 marked as unused by prune_unused_types. */
18491 static int
18492 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18494 tree type;
18495 dw_die_ref die;
18497 type = (tree) *slot;
18498 die = lookup_type_die (type);
18499 if (die != NULL)
18500 die->die_perennial_p = 1;
18501 return 1;
18504 /* Helper function of premark_types_used_by_global_vars which gets called
18505 through htab_traverse.
18507 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18508 marked as unused by prune_unused_types. The DIE of the type is marked
18509 only if the global variable using the type will actually be emitted. */
18511 static int
18512 premark_types_used_by_global_vars_helper (void **slot,
18513 void *data ATTRIBUTE_UNUSED)
18515 struct types_used_by_vars_entry *entry;
18516 dw_die_ref die;
18518 entry = (struct types_used_by_vars_entry *) *slot;
18519 gcc_assert (entry->type != NULL
18520 && entry->var_decl != NULL);
18521 die = lookup_type_die (entry->type);
18522 if (die)
18524 /* Ask cgraph if the global variable really is to be emitted.
18525 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18526 struct varpool_node *node = varpool_get_node (entry->var_decl);
18527 if (node && node->needed)
18529 die->die_perennial_p = 1;
18530 /* Keep the parent DIEs as well. */
18531 while ((die = die->die_parent) && die->die_perennial_p == 0)
18532 die->die_perennial_p = 1;
18535 return 1;
18538 /* Mark all members of used_types_hash as perennial. */
18540 static void
18541 premark_used_types (void)
18543 if (cfun && cfun->used_types_hash)
18544 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18547 /* Mark all members of types_used_by_vars_entry as perennial. */
18549 static void
18550 premark_types_used_by_global_vars (void)
18552 if (types_used_by_vars_hash)
18553 htab_traverse (types_used_by_vars_hash,
18554 premark_types_used_by_global_vars_helper, NULL);
18557 /* Generate a DIE to represent a declared function (either file-scope or
18558 block-local). */
18560 static void
18561 gen_subprogram_die (tree decl, dw_die_ref context_die)
18563 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
18564 tree origin = decl_ultimate_origin (decl);
18565 dw_die_ref subr_die;
18566 tree fn_arg_types;
18567 tree outer_scope;
18568 dw_die_ref old_die = lookup_decl_die (decl);
18569 int declaration = (current_function_decl != decl
18570 || class_or_namespace_scope_p (context_die));
18572 premark_used_types ();
18574 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18575 started to generate the abstract instance of an inline, decided to output
18576 its containing class, and proceeded to emit the declaration of the inline
18577 from the member list for the class. If so, DECLARATION takes priority;
18578 we'll get back to the abstract instance when done with the class. */
18580 /* The class-scope declaration DIE must be the primary DIE. */
18581 if (origin && declaration && class_or_namespace_scope_p (context_die))
18583 origin = NULL;
18584 gcc_assert (!old_die);
18587 /* Now that the C++ front end lazily declares artificial member fns, we
18588 might need to retrofit the declaration into its class. */
18589 if (!declaration && !origin && !old_die
18590 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18591 && !class_or_namespace_scope_p (context_die)
18592 && debug_info_level > DINFO_LEVEL_TERSE)
18593 old_die = force_decl_die (decl);
18595 if (origin != NULL)
18597 gcc_assert (!declaration || local_scope_p (context_die));
18599 /* Fixup die_parent for the abstract instance of a nested
18600 inline function. */
18601 if (old_die && old_die->die_parent == NULL)
18602 add_child_die (context_die, old_die);
18604 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18605 add_abstract_origin_attribute (subr_die, origin);
18607 else if (old_die)
18609 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18610 struct dwarf_file_data * file_index = lookup_filename (s.file);
18612 if (!get_AT_flag (old_die, DW_AT_declaration)
18613 /* We can have a normal definition following an inline one in the
18614 case of redefinition of GNU C extern inlines.
18615 It seems reasonable to use AT_specification in this case. */
18616 && !get_AT (old_die, DW_AT_inline))
18618 /* Detect and ignore this case, where we are trying to output
18619 something we have already output. */
18620 return;
18623 /* If the definition comes from the same place as the declaration,
18624 maybe use the old DIE. We always want the DIE for this function
18625 that has the *_pc attributes to be under comp_unit_die so the
18626 debugger can find it. We also need to do this for abstract
18627 instances of inlines, since the spec requires the out-of-line copy
18628 to have the same parent. For local class methods, this doesn't
18629 apply; we just use the old DIE. */
18630 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
18631 && (DECL_ARTIFICIAL (decl)
18632 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18633 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18634 == (unsigned) s.line))))
18636 subr_die = old_die;
18638 /* Clear out the declaration attribute and the formal parameters.
18639 Do not remove all children, because it is possible that this
18640 declaration die was forced using force_decl_die(). In such
18641 cases die that forced declaration die (e.g. TAG_imported_module)
18642 is one of the children that we do not want to remove. */
18643 remove_AT (subr_die, DW_AT_declaration);
18644 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18646 else
18648 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18649 add_AT_specification (subr_die, old_die);
18650 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18651 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18652 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18653 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18656 else
18658 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18660 if (TREE_PUBLIC (decl))
18661 add_AT_flag (subr_die, DW_AT_external, 1);
18663 add_name_and_src_coords_attributes (subr_die, decl);
18664 if (debug_info_level > DINFO_LEVEL_TERSE)
18666 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18667 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18668 0, 0, context_die);
18671 add_pure_or_virtual_attribute (subr_die, decl);
18672 if (DECL_ARTIFICIAL (decl))
18673 add_AT_flag (subr_die, DW_AT_artificial, 1);
18675 add_accessibility_attribute (subr_die, decl);
18678 if (declaration)
18680 if (!old_die || !get_AT (old_die, DW_AT_inline))
18682 add_AT_flag (subr_die, DW_AT_declaration, 1);
18684 /* If this is an explicit function declaration then generate
18685 a DW_AT_explicit attribute. */
18686 if (lang_hooks.decls.function_decl_explicit_p (decl)
18687 && (dwarf_version >= 3 || !dwarf_strict))
18688 add_AT_flag (subr_die, DW_AT_explicit, 1);
18690 /* The first time we see a member function, it is in the context of
18691 the class to which it belongs. We make sure of this by emitting
18692 the class first. The next time is the definition, which is
18693 handled above. The two may come from the same source text.
18695 Note that force_decl_die() forces function declaration die. It is
18696 later reused to represent definition. */
18697 equate_decl_number_to_die (decl, subr_die);
18700 else if (DECL_ABSTRACT (decl))
18702 if (DECL_DECLARED_INLINE_P (decl))
18704 if (cgraph_function_possibly_inlined_p (decl))
18705 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18706 else
18707 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18709 else
18711 if (cgraph_function_possibly_inlined_p (decl))
18712 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18713 else
18714 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18717 if (DECL_DECLARED_INLINE_P (decl)
18718 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18719 add_AT_flag (subr_die, DW_AT_artificial, 1);
18721 equate_decl_number_to_die (decl, subr_die);
18723 else if (!DECL_EXTERNAL (decl))
18725 HOST_WIDE_INT cfa_fb_offset;
18727 if (!old_die || !get_AT (old_die, DW_AT_inline))
18728 equate_decl_number_to_die (decl, subr_die);
18730 if (!flag_reorder_blocks_and_partition)
18732 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
18733 current_function_funcdef_no);
18734 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
18735 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
18736 current_function_funcdef_no);
18737 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
18739 #if VMS_DEBUGGING_INFO
18740 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18741 Section 2.3 Prologue and Epilogue Attributes:
18742 When a breakpoint is set on entry to a function, it is generally
18743 desirable for execution to be suspended, not on the very first
18744 instruction of the function, but rather at a point after the
18745 function's frame has been set up, after any language defined local
18746 declaration processing has been completed, and before execution of
18747 the first statement of the function begins. Debuggers generally
18748 cannot properly determine where this point is. Similarly for a
18749 breakpoint set on exit from a function. The prologue and epilogue
18750 attributes allow a compiler to communicate the location(s) to use. */
18753 dw_fde_ref fde = &fde_table[current_funcdef_fde];
18755 if (fde->dw_fde_vms_end_prologue)
18756 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18757 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18759 if (fde->dw_fde_vms_begin_epilogue)
18760 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18761 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18763 #endif
18765 add_pubname (decl, subr_die);
18766 add_arange (decl, subr_die);
18768 else
18769 { /* Do nothing for now; maybe need to duplicate die, one for
18770 hot section and one for cold section, then use the hot/cold
18771 section begin/end labels to generate the aranges... */
18773 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
18774 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
18775 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
18776 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
18778 add_pubname (decl, subr_die);
18779 add_arange (decl, subr_die);
18780 add_arange (decl, subr_die);
18784 #ifdef MIPS_DEBUGGING_INFO
18785 /* Add a reference to the FDE for this routine. */
18786 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
18787 #endif
18789 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18791 /* We define the "frame base" as the function's CFA. This is more
18792 convenient for several reasons: (1) It's stable across the prologue
18793 and epilogue, which makes it better than just a frame pointer,
18794 (2) With dwarf3, there exists a one-byte encoding that allows us
18795 to reference the .debug_frame data by proxy, but failing that,
18796 (3) We can at least reuse the code inspection and interpretation
18797 code that determines the CFA position at various points in the
18798 function. */
18799 if (dwarf_version >= 3)
18801 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18802 add_AT_loc (subr_die, DW_AT_frame_base, op);
18804 else
18806 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18807 if (list->dw_loc_next)
18808 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18809 else
18810 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18813 /* Compute a displacement from the "steady-state frame pointer" to
18814 the CFA. The former is what all stack slots and argument slots
18815 will reference in the rtl; the later is what we've told the
18816 debugger about. We'll need to adjust all frame_base references
18817 by this displacement. */
18818 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18820 if (cfun->static_chain_decl)
18821 add_AT_location_description (subr_die, DW_AT_static_link,
18822 loc_list_from_tree (cfun->static_chain_decl, 2));
18825 /* Generate child dies for template paramaters. */
18826 if (debug_info_level > DINFO_LEVEL_TERSE)
18827 gen_generic_params_dies (decl);
18829 /* Now output descriptions of the arguments for this function. This gets
18830 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18831 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18832 `...' at the end of the formal parameter list. In order to find out if
18833 there was a trailing ellipsis or not, we must instead look at the type
18834 associated with the FUNCTION_DECL. This will be a node of type
18835 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18836 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18837 an ellipsis at the end. */
18839 /* In the case where we are describing a mere function declaration, all we
18840 need to do here (and all we *can* do here) is to describe the *types* of
18841 its formal parameters. */
18842 if (debug_info_level <= DINFO_LEVEL_TERSE)
18844 else if (declaration)
18845 gen_formal_types_die (decl, subr_die);
18846 else
18848 /* Generate DIEs to represent all known formal parameters. */
18849 tree parm = DECL_ARGUMENTS (decl);
18850 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18851 tree generic_decl_parm = generic_decl
18852 ? DECL_ARGUMENTS (generic_decl)
18853 : NULL;
18855 /* Now we want to walk the list of parameters of the function and
18856 emit their relevant DIEs.
18858 We consider the case of DECL being an instance of a generic function
18859 as well as it being a normal function.
18861 If DECL is an instance of a generic function we walk the
18862 parameters of the generic function declaration _and_ the parameters of
18863 DECL itself. This is useful because we want to emit specific DIEs for
18864 function parameter packs and those are declared as part of the
18865 generic function declaration. In that particular case,
18866 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18867 That DIE has children DIEs representing the set of arguments
18868 of the pack. Note that the set of pack arguments can be empty.
18869 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18870 children DIE.
18872 Otherwise, we just consider the parameters of DECL. */
18873 while (generic_decl_parm || parm)
18875 if (generic_decl_parm
18876 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18877 gen_formal_parameter_pack_die (generic_decl_parm,
18878 parm, subr_die,
18879 &parm);
18880 else if (parm)
18882 gen_decl_die (parm, NULL, subr_die);
18883 parm = DECL_CHAIN (parm);
18886 if (generic_decl_parm)
18887 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18890 /* Decide whether we need an unspecified_parameters DIE at the end.
18891 There are 2 more cases to do this for: 1) the ansi ... declaration -
18892 this is detectable when the end of the arg list is not a
18893 void_type_node 2) an unprototyped function declaration (not a
18894 definition). This just means that we have no info about the
18895 parameters at all. */
18896 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
18897 if (fn_arg_types != NULL)
18899 /* This is the prototyped case, check for.... */
18900 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
18901 gen_unspecified_parameters_die (decl, subr_die);
18903 else if (DECL_INITIAL (decl) == NULL_TREE)
18904 gen_unspecified_parameters_die (decl, subr_die);
18907 /* Output Dwarf info for all of the stuff within the body of the function
18908 (if it has one - it may be just a declaration). */
18909 outer_scope = DECL_INITIAL (decl);
18911 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18912 a function. This BLOCK actually represents the outermost binding contour
18913 for the function, i.e. the contour in which the function's formal
18914 parameters and labels get declared. Curiously, it appears that the front
18915 end doesn't actually put the PARM_DECL nodes for the current function onto
18916 the BLOCK_VARS list for this outer scope, but are strung off of the
18917 DECL_ARGUMENTS list for the function instead.
18919 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18920 the LABEL_DECL nodes for the function however, and we output DWARF info
18921 for those in decls_for_scope. Just within the `outer_scope' there will be
18922 a BLOCK node representing the function's outermost pair of curly braces,
18923 and any blocks used for the base and member initializers of a C++
18924 constructor function. */
18925 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
18927 /* Emit a DW_TAG_variable DIE for a named return value. */
18928 if (DECL_NAME (DECL_RESULT (decl)))
18929 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18931 current_function_has_inlines = 0;
18932 decls_for_scope (outer_scope, subr_die, 0);
18934 #if 0 && defined (MIPS_DEBUGGING_INFO)
18935 if (current_function_has_inlines)
18937 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
18938 if (! comp_unit_has_inlines)
18940 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
18941 comp_unit_has_inlines = 1;
18944 #endif
18946 /* Add the calling convention attribute if requested. */
18947 add_calling_convention_attribute (subr_die, decl);
18951 /* Returns a hash value for X (which really is a die_struct). */
18953 static hashval_t
18954 common_block_die_table_hash (const void *x)
18956 const_dw_die_ref d = (const_dw_die_ref) x;
18957 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18960 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18961 as decl_id and die_parent of die_struct Y. */
18963 static int
18964 common_block_die_table_eq (const void *x, const void *y)
18966 const_dw_die_ref d = (const_dw_die_ref) x;
18967 const_dw_die_ref e = (const_dw_die_ref) y;
18968 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18971 /* Generate a DIE to represent a declared data object.
18972 Either DECL or ORIGIN must be non-null. */
18974 static void
18975 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18977 HOST_WIDE_INT off;
18978 tree com_decl;
18979 tree decl_or_origin = decl ? decl : origin;
18980 tree ultimate_origin;
18981 dw_die_ref var_die;
18982 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18983 dw_die_ref origin_die;
18984 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18985 || class_or_namespace_scope_p (context_die));
18986 bool specialization_p = false;
18988 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18989 if (decl || ultimate_origin)
18990 origin = ultimate_origin;
18991 com_decl = fortran_common (decl_or_origin, &off);
18993 /* Symbol in common gets emitted as a child of the common block, in the form
18994 of a data member. */
18995 if (com_decl)
18997 dw_die_ref com_die;
18998 dw_loc_list_ref loc;
18999 die_node com_die_arg;
19001 var_die = lookup_decl_die (decl_or_origin);
19002 if (var_die)
19004 if (get_AT (var_die, DW_AT_location) == NULL)
19006 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19007 if (loc)
19009 if (off)
19011 /* Optimize the common case. */
19012 if (single_element_loc_list_p (loc)
19013 && loc->expr->dw_loc_opc == DW_OP_addr
19014 && loc->expr->dw_loc_next == NULL
19015 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19016 == SYMBOL_REF)
19017 loc->expr->dw_loc_oprnd1.v.val_addr
19018 = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19019 else
19020 loc_list_plus_const (loc, off);
19022 add_AT_location_description (var_die, DW_AT_location, loc);
19023 remove_AT (var_die, DW_AT_declaration);
19026 return;
19029 if (common_block_die_table == NULL)
19030 common_block_die_table
19031 = htab_create_ggc (10, common_block_die_table_hash,
19032 common_block_die_table_eq, NULL);
19034 com_die_arg.decl_id = DECL_UID (com_decl);
19035 com_die_arg.die_parent = context_die;
19036 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19037 loc = loc_list_from_tree (com_decl, 2);
19038 if (com_die == NULL)
19040 const char *cnam
19041 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19042 void **slot;
19044 com_die = new_die (DW_TAG_common_block, context_die, decl);
19045 add_name_and_src_coords_attributes (com_die, com_decl);
19046 if (loc)
19048 add_AT_location_description (com_die, DW_AT_location, loc);
19049 /* Avoid sharing the same loc descriptor between
19050 DW_TAG_common_block and DW_TAG_variable. */
19051 loc = loc_list_from_tree (com_decl, 2);
19053 else if (DECL_EXTERNAL (decl))
19054 add_AT_flag (com_die, DW_AT_declaration, 1);
19055 add_pubname_string (cnam, com_die); /* ??? needed? */
19056 com_die->decl_id = DECL_UID (com_decl);
19057 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19058 *slot = (void *) com_die;
19060 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19062 add_AT_location_description (com_die, DW_AT_location, loc);
19063 loc = loc_list_from_tree (com_decl, 2);
19064 remove_AT (com_die, DW_AT_declaration);
19066 var_die = new_die (DW_TAG_variable, com_die, decl);
19067 add_name_and_src_coords_attributes (var_die, decl);
19068 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19069 TREE_THIS_VOLATILE (decl), context_die);
19070 add_AT_flag (var_die, DW_AT_external, 1);
19071 if (loc)
19073 if (off)
19075 /* Optimize the common case. */
19076 if (single_element_loc_list_p (loc)
19077 && loc->expr->dw_loc_opc == DW_OP_addr
19078 && loc->expr->dw_loc_next == NULL
19079 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19080 loc->expr->dw_loc_oprnd1.v.val_addr
19081 = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19082 else
19083 loc_list_plus_const (loc, off);
19085 add_AT_location_description (var_die, DW_AT_location, loc);
19087 else if (DECL_EXTERNAL (decl))
19088 add_AT_flag (var_die, DW_AT_declaration, 1);
19089 equate_decl_number_to_die (decl, var_die);
19090 return;
19093 /* If the compiler emitted a definition for the DECL declaration
19094 and if we already emitted a DIE for it, don't emit a second
19095 DIE for it again. Allow re-declarations of DECLs that are
19096 inside functions, though. */
19097 if (old_die && declaration && !local_scope_p (context_die))
19098 return;
19100 /* For static data members, the declaration in the class is supposed
19101 to have DW_TAG_member tag; the specification should still be
19102 DW_TAG_variable referencing the DW_TAG_member DIE. */
19103 if (declaration && class_scope_p (context_die))
19104 var_die = new_die (DW_TAG_member, context_die, decl);
19105 else
19106 var_die = new_die (DW_TAG_variable, context_die, decl);
19108 origin_die = NULL;
19109 if (origin != NULL)
19110 origin_die = add_abstract_origin_attribute (var_die, origin);
19112 /* Loop unrolling can create multiple blocks that refer to the same
19113 static variable, so we must test for the DW_AT_declaration flag.
19115 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19116 copy decls and set the DECL_ABSTRACT flag on them instead of
19117 sharing them.
19119 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19121 ??? The declare_in_namespace support causes us to get two DIEs for one
19122 variable, both of which are declarations. We want to avoid considering
19123 one to be a specification, so we must test that this DIE is not a
19124 declaration. */
19125 else if (old_die && TREE_STATIC (decl) && ! declaration
19126 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19128 /* This is a definition of a C++ class level static. */
19129 add_AT_specification (var_die, old_die);
19130 specialization_p = true;
19131 if (DECL_NAME (decl))
19133 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19134 struct dwarf_file_data * file_index = lookup_filename (s.file);
19136 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19137 add_AT_file (var_die, DW_AT_decl_file, file_index);
19139 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19140 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19142 if (old_die->die_tag == DW_TAG_member)
19143 add_linkage_name (var_die, decl);
19146 else
19147 add_name_and_src_coords_attributes (var_die, decl);
19149 if ((origin == NULL && !specialization_p)
19150 || (origin != NULL
19151 && !DECL_ABSTRACT (decl_or_origin)
19152 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19153 decl_function_context
19154 (decl_or_origin))))
19156 tree type = TREE_TYPE (decl_or_origin);
19158 if (decl_by_reference_p (decl_or_origin))
19159 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19160 else
19161 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19162 TREE_THIS_VOLATILE (decl_or_origin), context_die);
19165 if (origin == NULL && !specialization_p)
19167 if (TREE_PUBLIC (decl))
19168 add_AT_flag (var_die, DW_AT_external, 1);
19170 if (DECL_ARTIFICIAL (decl))
19171 add_AT_flag (var_die, DW_AT_artificial, 1);
19173 add_accessibility_attribute (var_die, decl);
19176 if (declaration)
19177 add_AT_flag (var_die, DW_AT_declaration, 1);
19179 if (decl && (DECL_ABSTRACT (decl) || declaration))
19180 equate_decl_number_to_die (decl, var_die);
19182 if (! declaration
19183 && (! DECL_ABSTRACT (decl_or_origin)
19184 /* Local static vars are shared between all clones/inlines,
19185 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19186 already set. */
19187 || (TREE_CODE (decl_or_origin) == VAR_DECL
19188 && TREE_STATIC (decl_or_origin)
19189 && DECL_RTL_SET_P (decl_or_origin)))
19190 /* When abstract origin already has DW_AT_location attribute, no need
19191 to add it again. */
19192 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19194 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19195 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19196 defer_location (decl_or_origin, var_die);
19197 else
19198 add_location_or_const_value_attribute (var_die,
19199 decl_or_origin,
19200 DW_AT_location);
19201 add_pubname (decl_or_origin, var_die);
19203 else
19204 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19207 /* Generate a DIE to represent a named constant. */
19209 static void
19210 gen_const_die (tree decl, dw_die_ref context_die)
19212 dw_die_ref const_die;
19213 tree type = TREE_TYPE (decl);
19215 const_die = new_die (DW_TAG_constant, context_die, decl);
19216 add_name_and_src_coords_attributes (const_die, decl);
19217 add_type_attribute (const_die, type, 1, 0, context_die);
19218 if (TREE_PUBLIC (decl))
19219 add_AT_flag (const_die, DW_AT_external, 1);
19220 if (DECL_ARTIFICIAL (decl))
19221 add_AT_flag (const_die, DW_AT_artificial, 1);
19222 tree_add_const_value_attribute_for_decl (const_die, decl);
19225 /* Generate a DIE to represent a label identifier. */
19227 static void
19228 gen_label_die (tree decl, dw_die_ref context_die)
19230 tree origin = decl_ultimate_origin (decl);
19231 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19232 rtx insn;
19233 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19235 if (origin != NULL)
19236 add_abstract_origin_attribute (lbl_die, origin);
19237 else
19238 add_name_and_src_coords_attributes (lbl_die, decl);
19240 if (DECL_ABSTRACT (decl))
19241 equate_decl_number_to_die (decl, lbl_die);
19242 else
19244 insn = DECL_RTL_IF_SET (decl);
19246 /* Deleted labels are programmer specified labels which have been
19247 eliminated because of various optimizations. We still emit them
19248 here so that it is possible to put breakpoints on them. */
19249 if (insn
19250 && (LABEL_P (insn)
19251 || ((NOTE_P (insn)
19252 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19254 /* When optimization is enabled (via -O) some parts of the compiler
19255 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19256 represent source-level labels which were explicitly declared by
19257 the user. This really shouldn't be happening though, so catch
19258 it if it ever does happen. */
19259 gcc_assert (!INSN_DELETED_P (insn));
19261 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19262 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19267 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19268 attributes to the DIE for a block STMT, to describe where the inlined
19269 function was called from. This is similar to add_src_coords_attributes. */
19271 static inline void
19272 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19274 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19276 if (dwarf_version >= 3 || !dwarf_strict)
19278 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19279 add_AT_unsigned (die, DW_AT_call_line, s.line);
19284 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19285 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19287 static inline void
19288 add_high_low_attributes (tree stmt, dw_die_ref die)
19290 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19292 if (BLOCK_FRAGMENT_CHAIN (stmt)
19293 && (dwarf_version >= 3 || !dwarf_strict))
19295 tree chain;
19297 if (inlined_function_outer_scope_p (stmt))
19299 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19300 BLOCK_NUMBER (stmt));
19301 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19304 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19306 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19309 add_ranges (chain);
19310 chain = BLOCK_FRAGMENT_CHAIN (chain);
19312 while (chain);
19313 add_ranges (NULL);
19315 else
19317 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19318 BLOCK_NUMBER (stmt));
19319 add_AT_lbl_id (die, DW_AT_low_pc, label);
19320 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19321 BLOCK_NUMBER (stmt));
19322 add_AT_lbl_id (die, DW_AT_high_pc, label);
19326 /* Generate a DIE for a lexical block. */
19328 static void
19329 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19331 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19333 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19334 add_high_low_attributes (stmt, stmt_die);
19336 decls_for_scope (stmt, stmt_die, depth);
19339 /* Generate a DIE for an inlined subprogram. */
19341 static void
19342 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19344 tree decl;
19346 /* The instance of function that is effectively being inlined shall not
19347 be abstract. */
19348 gcc_assert (! BLOCK_ABSTRACT (stmt));
19350 decl = block_ultimate_origin (stmt);
19352 /* Emit info for the abstract instance first, if we haven't yet. We
19353 must emit this even if the block is abstract, otherwise when we
19354 emit the block below (or elsewhere), we may end up trying to emit
19355 a die whose origin die hasn't been emitted, and crashing. */
19356 dwarf2out_abstract_function (decl);
19358 if (! BLOCK_ABSTRACT (stmt))
19360 dw_die_ref subr_die
19361 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19363 add_abstract_origin_attribute (subr_die, decl);
19364 if (TREE_ASM_WRITTEN (stmt))
19365 add_high_low_attributes (stmt, subr_die);
19366 add_call_src_coords_attributes (stmt, subr_die);
19368 decls_for_scope (stmt, subr_die, depth);
19369 current_function_has_inlines = 1;
19373 /* Generate a DIE for a field in a record, or structure. */
19375 static void
19376 gen_field_die (tree decl, dw_die_ref context_die)
19378 dw_die_ref decl_die;
19380 if (TREE_TYPE (decl) == error_mark_node)
19381 return;
19383 decl_die = new_die (DW_TAG_member, context_die, decl);
19384 add_name_and_src_coords_attributes (decl_die, decl);
19385 add_type_attribute (decl_die, member_declared_type (decl),
19386 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19387 context_die);
19389 if (DECL_BIT_FIELD_TYPE (decl))
19391 add_byte_size_attribute (decl_die, decl);
19392 add_bit_size_attribute (decl_die, decl);
19393 add_bit_offset_attribute (decl_die, decl);
19396 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19397 add_data_member_location_attribute (decl_die, decl);
19399 if (DECL_ARTIFICIAL (decl))
19400 add_AT_flag (decl_die, DW_AT_artificial, 1);
19402 add_accessibility_attribute (decl_die, decl);
19404 /* Equate decl number to die, so that we can look up this decl later on. */
19405 equate_decl_number_to_die (decl, decl_die);
19408 #if 0
19409 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19410 Use modified_type_die instead.
19411 We keep this code here just in case these types of DIEs may be needed to
19412 represent certain things in other languages (e.g. Pascal) someday. */
19414 static void
19415 gen_pointer_type_die (tree type, dw_die_ref context_die)
19417 dw_die_ref ptr_die
19418 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19420 equate_type_number_to_die (type, ptr_die);
19421 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19422 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19425 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19426 Use modified_type_die instead.
19427 We keep this code here just in case these types of DIEs may be needed to
19428 represent certain things in other languages (e.g. Pascal) someday. */
19430 static void
19431 gen_reference_type_die (tree type, dw_die_ref context_die)
19433 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19435 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19436 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19437 else
19438 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19440 equate_type_number_to_die (type, ref_die);
19441 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19442 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19444 #endif
19446 /* Generate a DIE for a pointer to a member type. */
19448 static void
19449 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19451 dw_die_ref ptr_die
19452 = new_die (DW_TAG_ptr_to_member_type,
19453 scope_die_for (type, context_die), type);
19455 equate_type_number_to_die (type, ptr_die);
19456 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19457 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19458 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19461 /* Generate the DIE for the compilation unit. */
19463 static dw_die_ref
19464 gen_compile_unit_die (const char *filename)
19466 dw_die_ref die;
19467 char producer[250];
19468 const char *language_string = lang_hooks.name;
19469 int language;
19471 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19473 if (filename)
19475 add_name_attribute (die, filename);
19476 /* Don't add cwd for <built-in>. */
19477 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19478 add_comp_dir_attribute (die);
19481 sprintf (producer, "%s %s", language_string, version_string);
19483 #ifdef MIPS_DEBUGGING_INFO
19484 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19485 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19486 not appear in the producer string, the debugger reaches the conclusion
19487 that the object file is stripped and has no debugging information.
19488 To get the MIPS/SGI debugger to believe that there is debugging
19489 information in the object file, we add a -g to the producer string. */
19490 if (debug_info_level > DINFO_LEVEL_TERSE)
19491 strcat (producer, " -g");
19492 #endif
19494 add_AT_string (die, DW_AT_producer, producer);
19496 language = DW_LANG_C89;
19497 if (strcmp (language_string, "GNU C++") == 0)
19498 language = DW_LANG_C_plus_plus;
19499 else if (strcmp (language_string, "GNU F77") == 0)
19500 language = DW_LANG_Fortran77;
19501 else if (strcmp (language_string, "GNU Pascal") == 0)
19502 language = DW_LANG_Pascal83;
19503 else if (dwarf_version >= 3 || !dwarf_strict)
19505 if (strcmp (language_string, "GNU Ada") == 0)
19506 language = DW_LANG_Ada95;
19507 else if (strcmp (language_string, "GNU Fortran") == 0)
19508 language = DW_LANG_Fortran95;
19509 else if (strcmp (language_string, "GNU Java") == 0)
19510 language = DW_LANG_Java;
19511 else if (strcmp (language_string, "GNU Objective-C") == 0)
19512 language = DW_LANG_ObjC;
19513 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19514 language = DW_LANG_ObjC_plus_plus;
19517 add_AT_unsigned (die, DW_AT_language, language);
19519 switch (language)
19521 case DW_LANG_Fortran77:
19522 case DW_LANG_Fortran90:
19523 case DW_LANG_Fortran95:
19524 /* Fortran has case insensitive identifiers and the front-end
19525 lowercases everything. */
19526 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19527 break;
19528 default:
19529 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19530 break;
19532 return die;
19535 /* Generate the DIE for a base class. */
19537 static void
19538 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19540 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19542 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19543 add_data_member_location_attribute (die, binfo);
19545 if (BINFO_VIRTUAL_P (binfo))
19546 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19548 if (access == access_public_node)
19549 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19550 else if (access == access_protected_node)
19551 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19554 /* Generate a DIE for a class member. */
19556 static void
19557 gen_member_die (tree type, dw_die_ref context_die)
19559 tree member;
19560 tree binfo = TYPE_BINFO (type);
19561 dw_die_ref child;
19563 /* If this is not an incomplete type, output descriptions of each of its
19564 members. Note that as we output the DIEs necessary to represent the
19565 members of this record or union type, we will also be trying to output
19566 DIEs to represent the *types* of those members. However the `type'
19567 function (above) will specifically avoid generating type DIEs for member
19568 types *within* the list of member DIEs for this (containing) type except
19569 for those types (of members) which are explicitly marked as also being
19570 members of this (containing) type themselves. The g++ front- end can
19571 force any given type to be treated as a member of some other (containing)
19572 type by setting the TYPE_CONTEXT of the given (member) type to point to
19573 the TREE node representing the appropriate (containing) type. */
19575 /* First output info about the base classes. */
19576 if (binfo)
19578 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19579 int i;
19580 tree base;
19582 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19583 gen_inheritance_die (base,
19584 (accesses ? VEC_index (tree, accesses, i)
19585 : access_public_node), context_die);
19588 /* Now output info about the data members and type members. */
19589 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19591 /* If we thought we were generating minimal debug info for TYPE
19592 and then changed our minds, some of the member declarations
19593 may have already been defined. Don't define them again, but
19594 do put them in the right order. */
19596 child = lookup_decl_die (member);
19597 if (child)
19598 splice_child_die (context_die, child);
19599 else
19600 gen_decl_die (member, NULL, context_die);
19603 /* Now output info about the function members (if any). */
19604 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19606 /* Don't include clones in the member list. */
19607 if (DECL_ABSTRACT_ORIGIN (member))
19608 continue;
19610 child = lookup_decl_die (member);
19611 if (child)
19612 splice_child_die (context_die, child);
19613 else
19614 gen_decl_die (member, NULL, context_die);
19618 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19619 is set, we pretend that the type was never defined, so we only get the
19620 member DIEs needed by later specification DIEs. */
19622 static void
19623 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19624 enum debug_info_usage usage)
19626 dw_die_ref type_die = lookup_type_die (type);
19627 dw_die_ref scope_die = 0;
19628 int nested = 0;
19629 int complete = (TYPE_SIZE (type)
19630 && (! TYPE_STUB_DECL (type)
19631 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19632 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19633 complete = complete && should_emit_struct_debug (type, usage);
19635 if (type_die && ! complete)
19636 return;
19638 if (TYPE_CONTEXT (type) != NULL_TREE
19639 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19640 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19641 nested = 1;
19643 scope_die = scope_die_for (type, context_die);
19645 if (! type_die || (nested && scope_die == comp_unit_die))
19646 /* First occurrence of type or toplevel definition of nested class. */
19648 dw_die_ref old_die = type_die;
19650 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19651 ? record_type_tag (type) : DW_TAG_union_type,
19652 scope_die, type);
19653 equate_type_number_to_die (type, type_die);
19654 if (old_die)
19655 add_AT_specification (type_die, old_die);
19656 else
19657 add_name_attribute (type_die, type_tag (type));
19659 else
19660 remove_AT (type_die, DW_AT_declaration);
19662 /* Generate child dies for template paramaters. */
19663 if (debug_info_level > DINFO_LEVEL_TERSE
19664 && COMPLETE_TYPE_P (type))
19665 gen_generic_params_dies (type);
19667 /* If this type has been completed, then give it a byte_size attribute and
19668 then give a list of members. */
19669 if (complete && !ns_decl)
19671 /* Prevent infinite recursion in cases where the type of some member of
19672 this type is expressed in terms of this type itself. */
19673 TREE_ASM_WRITTEN (type) = 1;
19674 add_byte_size_attribute (type_die, type);
19675 if (TYPE_STUB_DECL (type) != NULL_TREE)
19677 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19678 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19681 /* If the first reference to this type was as the return type of an
19682 inline function, then it may not have a parent. Fix this now. */
19683 if (type_die->die_parent == NULL)
19684 add_child_die (scope_die, type_die);
19686 push_decl_scope (type);
19687 gen_member_die (type, type_die);
19688 pop_decl_scope ();
19690 /* GNU extension: Record what type our vtable lives in. */
19691 if (TYPE_VFIELD (type))
19693 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19695 gen_type_die (vtype, context_die);
19696 add_AT_die_ref (type_die, DW_AT_containing_type,
19697 lookup_type_die (vtype));
19700 else
19702 add_AT_flag (type_die, DW_AT_declaration, 1);
19704 /* We don't need to do this for function-local types. */
19705 if (TYPE_STUB_DECL (type)
19706 && ! decl_function_context (TYPE_STUB_DECL (type)))
19707 VEC_safe_push (tree, gc, incomplete_types, type);
19710 if (get_AT (type_die, DW_AT_name))
19711 add_pubtype (type, type_die);
19714 /* Generate a DIE for a subroutine _type_. */
19716 static void
19717 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19719 tree return_type = TREE_TYPE (type);
19720 dw_die_ref subr_die
19721 = new_die (DW_TAG_subroutine_type,
19722 scope_die_for (type, context_die), type);
19724 equate_type_number_to_die (type, subr_die);
19725 add_prototyped_attribute (subr_die, type);
19726 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19727 gen_formal_types_die (type, subr_die);
19729 if (get_AT (subr_die, DW_AT_name))
19730 add_pubtype (type, subr_die);
19733 /* Generate a DIE for a type definition. */
19735 static void
19736 gen_typedef_die (tree decl, dw_die_ref context_die)
19738 dw_die_ref type_die;
19739 tree origin;
19741 if (TREE_ASM_WRITTEN (decl))
19742 return;
19744 TREE_ASM_WRITTEN (decl) = 1;
19745 type_die = new_die (DW_TAG_typedef, context_die, decl);
19746 origin = decl_ultimate_origin (decl);
19747 if (origin != NULL)
19748 add_abstract_origin_attribute (type_die, origin);
19749 else
19751 tree type;
19753 add_name_and_src_coords_attributes (type_die, decl);
19754 if (DECL_ORIGINAL_TYPE (decl))
19756 type = DECL_ORIGINAL_TYPE (decl);
19758 gcc_assert (type != TREE_TYPE (decl));
19759 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19761 else
19763 type = TREE_TYPE (decl);
19765 if (is_naming_typedef_decl (TYPE_NAME (type)))
19767 Here, we are in the case of decl being a typedef naming
19768 an anonymous type, e.g:
19769 typedef struct {...} foo;
19770 In that case TREE_TYPE (decl) is not a typedef variant
19771 type and TYPE_NAME of the anonymous type is set to the
19772 TYPE_DECL of the typedef. This construct is emitted by
19773 the C++ FE.
19775 TYPE is the anonymous struct named by the typedef
19776 DECL. As we need the DW_AT_type attribute of the
19777 DW_TAG_typedef to point to the DIE of TYPE, let's
19778 generate that DIE right away. add_type_attribute
19779 called below will then pick (via lookup_type_die) that
19780 anonymous struct DIE. */
19781 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19784 add_type_attribute (type_die, type, TREE_READONLY (decl),
19785 TREE_THIS_VOLATILE (decl), context_die);
19787 if (is_naming_typedef_decl (decl))
19788 /* We want that all subsequent calls to lookup_type_die with
19789 TYPE in argument yield the DW_TAG_typedef we have just
19790 created. */
19791 equate_type_number_to_die (type, type_die);
19793 add_accessibility_attribute (type_die, decl);
19796 if (DECL_ABSTRACT (decl))
19797 equate_decl_number_to_die (decl, type_die);
19799 if (get_AT (type_die, DW_AT_name))
19800 add_pubtype (decl, type_die);
19803 /* Generate a DIE for a struct, class, enum or union type. */
19805 static void
19806 gen_tagged_type_die (tree type,
19807 dw_die_ref context_die,
19808 enum debug_info_usage usage)
19810 int need_pop;
19812 if (type == NULL_TREE
19813 || !is_tagged_type (type))
19814 return;
19816 /* If this is a nested type whose containing class hasn't been written
19817 out yet, writing it out will cover this one, too. This does not apply
19818 to instantiations of member class templates; they need to be added to
19819 the containing class as they are generated. FIXME: This hurts the
19820 idea of combining type decls from multiple TUs, since we can't predict
19821 what set of template instantiations we'll get. */
19822 if (TYPE_CONTEXT (type)
19823 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19824 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19826 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19828 if (TREE_ASM_WRITTEN (type))
19829 return;
19831 /* If that failed, attach ourselves to the stub. */
19832 push_decl_scope (TYPE_CONTEXT (type));
19833 context_die = lookup_type_die (TYPE_CONTEXT (type));
19834 need_pop = 1;
19836 else if (TYPE_CONTEXT (type) != NULL_TREE
19837 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19839 /* If this type is local to a function that hasn't been written
19840 out yet, use a NULL context for now; it will be fixed up in
19841 decls_for_scope. */
19842 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19843 need_pop = 0;
19845 else
19847 context_die = declare_in_namespace (type, context_die);
19848 need_pop = 0;
19851 if (TREE_CODE (type) == ENUMERAL_TYPE)
19853 /* This might have been written out by the call to
19854 declare_in_namespace. */
19855 if (!TREE_ASM_WRITTEN (type))
19856 gen_enumeration_type_die (type, context_die);
19858 else
19859 gen_struct_or_union_type_die (type, context_die, usage);
19861 if (need_pop)
19862 pop_decl_scope ();
19864 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19865 it up if it is ever completed. gen_*_type_die will set it for us
19866 when appropriate. */
19869 /* Generate a type description DIE. */
19871 static void
19872 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19873 enum debug_info_usage usage)
19875 struct array_descr_info info;
19877 if (type == NULL_TREE || type == error_mark_node)
19878 return;
19880 /* If TYPE is a typedef type variant, let's generate debug info
19881 for the parent typedef which TYPE is a type of. */
19882 if (typedef_variant_p (type))
19884 if (TREE_ASM_WRITTEN (type))
19885 return;
19887 /* Prevent broken recursion; we can't hand off to the same type. */
19888 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19890 /* Use the DIE of the containing namespace as the parent DIE of
19891 the type description DIE we want to generate. */
19892 if (DECL_CONTEXT (TYPE_NAME (type))
19893 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19894 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19896 TREE_ASM_WRITTEN (type) = 1;
19898 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19899 return;
19902 /* If type is an anonymous tagged type named by a typedef, let's
19903 generate debug info for the typedef. */
19904 if (is_naming_typedef_decl (TYPE_NAME (type)))
19906 /* Use the DIE of the containing namespace as the parent DIE of
19907 the type description DIE we want to generate. */
19908 if (DECL_CONTEXT (TYPE_NAME (type))
19909 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19910 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19912 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19913 return;
19916 /* If this is an array type with hidden descriptor, handle it first. */
19917 if (!TREE_ASM_WRITTEN (type)
19918 && lang_hooks.types.get_array_descr_info
19919 && lang_hooks.types.get_array_descr_info (type, &info)
19920 && (dwarf_version >= 3 || !dwarf_strict))
19922 gen_descr_array_type_die (type, &info, context_die);
19923 TREE_ASM_WRITTEN (type) = 1;
19924 return;
19927 /* We are going to output a DIE to represent the unqualified version
19928 of this type (i.e. without any const or volatile qualifiers) so
19929 get the main variant (i.e. the unqualified version) of this type
19930 now. (Vectors are special because the debugging info is in the
19931 cloned type itself). */
19932 if (TREE_CODE (type) != VECTOR_TYPE)
19933 type = type_main_variant (type);
19935 if (TREE_ASM_WRITTEN (type))
19936 return;
19938 switch (TREE_CODE (type))
19940 case ERROR_MARK:
19941 break;
19943 case POINTER_TYPE:
19944 case REFERENCE_TYPE:
19945 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19946 ensures that the gen_type_die recursion will terminate even if the
19947 type is recursive. Recursive types are possible in Ada. */
19948 /* ??? We could perhaps do this for all types before the switch
19949 statement. */
19950 TREE_ASM_WRITTEN (type) = 1;
19952 /* For these types, all that is required is that we output a DIE (or a
19953 set of DIEs) to represent the "basis" type. */
19954 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19955 DINFO_USAGE_IND_USE);
19956 break;
19958 case OFFSET_TYPE:
19959 /* This code is used for C++ pointer-to-data-member types.
19960 Output a description of the relevant class type. */
19961 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19962 DINFO_USAGE_IND_USE);
19964 /* Output a description of the type of the object pointed to. */
19965 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19966 DINFO_USAGE_IND_USE);
19968 /* Now output a DIE to represent this pointer-to-data-member type
19969 itself. */
19970 gen_ptr_to_mbr_type_die (type, context_die);
19971 break;
19973 case FUNCTION_TYPE:
19974 /* Force out return type (in case it wasn't forced out already). */
19975 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19976 DINFO_USAGE_DIR_USE);
19977 gen_subroutine_type_die (type, context_die);
19978 break;
19980 case METHOD_TYPE:
19981 /* Force out return type (in case it wasn't forced out already). */
19982 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19983 DINFO_USAGE_DIR_USE);
19984 gen_subroutine_type_die (type, context_die);
19985 break;
19987 case ARRAY_TYPE:
19988 gen_array_type_die (type, context_die);
19989 break;
19991 case VECTOR_TYPE:
19992 gen_array_type_die (type, context_die);
19993 break;
19995 case ENUMERAL_TYPE:
19996 case RECORD_TYPE:
19997 case UNION_TYPE:
19998 case QUAL_UNION_TYPE:
19999 gen_tagged_type_die (type, context_die, usage);
20000 return;
20002 case VOID_TYPE:
20003 case INTEGER_TYPE:
20004 case REAL_TYPE:
20005 case FIXED_POINT_TYPE:
20006 case COMPLEX_TYPE:
20007 case BOOLEAN_TYPE:
20008 /* No DIEs needed for fundamental types. */
20009 break;
20011 case LANG_TYPE:
20012 /* Just use DW_TAG_unspecified_type. */
20014 dw_die_ref type_die = lookup_type_die (type);
20015 if (type_die == NULL)
20017 tree name = TYPE_NAME (type);
20018 if (TREE_CODE (name) == TYPE_DECL)
20019 name = DECL_NAME (name);
20020 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die, type);
20021 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20022 equate_type_number_to_die (type, type_die);
20025 break;
20027 default:
20028 gcc_unreachable ();
20031 TREE_ASM_WRITTEN (type) = 1;
20034 static void
20035 gen_type_die (tree type, dw_die_ref context_die)
20037 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20040 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20041 things which are local to the given block. */
20043 static void
20044 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20046 int must_output_die = 0;
20047 bool inlined_func;
20049 /* Ignore blocks that are NULL. */
20050 if (stmt == NULL_TREE)
20051 return;
20053 inlined_func = inlined_function_outer_scope_p (stmt);
20055 /* If the block is one fragment of a non-contiguous block, do not
20056 process the variables, since they will have been done by the
20057 origin block. Do process subblocks. */
20058 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20060 tree sub;
20062 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20063 gen_block_die (sub, context_die, depth + 1);
20065 return;
20068 /* Determine if we need to output any Dwarf DIEs at all to represent this
20069 block. */
20070 if (inlined_func)
20071 /* The outer scopes for inlinings *must* always be represented. We
20072 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20073 must_output_die = 1;
20074 else
20076 /* Determine if this block directly contains any "significant"
20077 local declarations which we will need to output DIEs for. */
20078 if (debug_info_level > DINFO_LEVEL_TERSE)
20079 /* We are not in terse mode so *any* local declaration counts
20080 as being a "significant" one. */
20081 must_output_die = ((BLOCK_VARS (stmt) != NULL
20082 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20083 && (TREE_USED (stmt)
20084 || TREE_ASM_WRITTEN (stmt)
20085 || BLOCK_ABSTRACT (stmt)));
20086 else if ((TREE_USED (stmt)
20087 || TREE_ASM_WRITTEN (stmt)
20088 || BLOCK_ABSTRACT (stmt))
20089 && !dwarf2out_ignore_block (stmt))
20090 must_output_die = 1;
20093 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20094 DIE for any block which contains no significant local declarations at
20095 all. Rather, in such cases we just call `decls_for_scope' so that any
20096 needed Dwarf info for any sub-blocks will get properly generated. Note
20097 that in terse mode, our definition of what constitutes a "significant"
20098 local declaration gets restricted to include only inlined function
20099 instances and local (nested) function definitions. */
20100 if (must_output_die)
20102 if (inlined_func)
20104 /* If STMT block is abstract, that means we have been called
20105 indirectly from dwarf2out_abstract_function.
20106 That function rightfully marks the descendent blocks (of
20107 the abstract function it is dealing with) as being abstract,
20108 precisely to prevent us from emitting any
20109 DW_TAG_inlined_subroutine DIE as a descendent
20110 of an abstract function instance. So in that case, we should
20111 not call gen_inlined_subroutine_die.
20113 Later though, when cgraph asks dwarf2out to emit info
20114 for the concrete instance of the function decl into which
20115 the concrete instance of STMT got inlined, the later will lead
20116 to the generation of a DW_TAG_inlined_subroutine DIE. */
20117 if (! BLOCK_ABSTRACT (stmt))
20118 gen_inlined_subroutine_die (stmt, context_die, depth);
20120 else
20121 gen_lexical_block_die (stmt, context_die, depth);
20123 else
20124 decls_for_scope (stmt, context_die, depth);
20127 /* Process variable DECL (or variable with origin ORIGIN) within
20128 block STMT and add it to CONTEXT_DIE. */
20129 static void
20130 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20132 dw_die_ref die;
20133 tree decl_or_origin = decl ? decl : origin;
20135 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20136 die = lookup_decl_die (decl_or_origin);
20137 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20138 && TYPE_DECL_IS_STUB (decl_or_origin))
20139 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20140 else
20141 die = NULL;
20143 if (die != NULL && die->die_parent == NULL)
20144 add_child_die (context_die, die);
20145 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20146 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20147 stmt, context_die);
20148 else
20149 gen_decl_die (decl, origin, context_die);
20152 /* Generate all of the decls declared within a given scope and (recursively)
20153 all of its sub-blocks. */
20155 static void
20156 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20158 tree decl;
20159 unsigned int i;
20160 tree subblocks;
20162 /* Ignore NULL blocks. */
20163 if (stmt == NULL_TREE)
20164 return;
20166 /* Output the DIEs to represent all of the data objects and typedefs
20167 declared directly within this block but not within any nested
20168 sub-blocks. Also, nested function and tag DIEs have been
20169 generated with a parent of NULL; fix that up now. */
20170 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20171 process_scope_var (stmt, decl, NULL_TREE, context_die);
20172 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20173 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20174 context_die);
20176 /* If we're at -g1, we're not interested in subblocks. */
20177 if (debug_info_level <= DINFO_LEVEL_TERSE)
20178 return;
20180 /* Output the DIEs to represent all sub-blocks (and the items declared
20181 therein) of this block. */
20182 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20183 subblocks != NULL;
20184 subblocks = BLOCK_CHAIN (subblocks))
20185 gen_block_die (subblocks, context_die, depth + 1);
20188 /* Is this a typedef we can avoid emitting? */
20190 static inline int
20191 is_redundant_typedef (const_tree decl)
20193 if (TYPE_DECL_IS_STUB (decl))
20194 return 1;
20196 if (DECL_ARTIFICIAL (decl)
20197 && DECL_CONTEXT (decl)
20198 && is_tagged_type (DECL_CONTEXT (decl))
20199 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20200 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20201 /* Also ignore the artificial member typedef for the class name. */
20202 return 1;
20204 return 0;
20207 /* Return TRUE if TYPE is a typedef that names a type for linkage
20208 purposes. This kind of typedefs is produced by the C++ FE for
20209 constructs like:
20211 typedef struct {...} foo;
20213 In that case, there is no typedef variant type produced for foo.
20214 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20215 struct type. */
20217 static bool
20218 is_naming_typedef_decl (const_tree decl)
20220 if (decl == NULL_TREE
20221 || TREE_CODE (decl) != TYPE_DECL
20222 || !is_tagged_type (TREE_TYPE (decl))
20223 || DECL_IS_BUILTIN (decl)
20224 || is_redundant_typedef (decl)
20225 /* It looks like Ada produces TYPE_DECLs that are very similar
20226 to C++ naming typedefs but that have different
20227 semantics. Let's be specific to c++ for now. */
20228 || !is_cxx ())
20229 return FALSE;
20231 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20232 && TYPE_NAME (TREE_TYPE (decl)) == decl
20233 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20234 != TYPE_NAME (TREE_TYPE (decl))));
20237 /* Returns the DIE for a context. */
20239 static inline dw_die_ref
20240 get_context_die (tree context)
20242 if (context)
20244 /* Find die that represents this context. */
20245 if (TYPE_P (context))
20246 return force_type_die (TYPE_MAIN_VARIANT (context));
20247 else
20248 return force_decl_die (context);
20250 return comp_unit_die;
20253 /* Returns the DIE for decl. A DIE will always be returned. */
20255 static dw_die_ref
20256 force_decl_die (tree decl)
20258 dw_die_ref decl_die;
20259 unsigned saved_external_flag;
20260 tree save_fn = NULL_TREE;
20261 decl_die = lookup_decl_die (decl);
20262 if (!decl_die)
20264 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20266 decl_die = lookup_decl_die (decl);
20267 if (decl_die)
20268 return decl_die;
20270 switch (TREE_CODE (decl))
20272 case FUNCTION_DECL:
20273 /* Clear current_function_decl, so that gen_subprogram_die thinks
20274 that this is a declaration. At this point, we just want to force
20275 declaration die. */
20276 save_fn = current_function_decl;
20277 current_function_decl = NULL_TREE;
20278 gen_subprogram_die (decl, context_die);
20279 current_function_decl = save_fn;
20280 break;
20282 case VAR_DECL:
20283 /* Set external flag to force declaration die. Restore it after
20284 gen_decl_die() call. */
20285 saved_external_flag = DECL_EXTERNAL (decl);
20286 DECL_EXTERNAL (decl) = 1;
20287 gen_decl_die (decl, NULL, context_die);
20288 DECL_EXTERNAL (decl) = saved_external_flag;
20289 break;
20291 case NAMESPACE_DECL:
20292 if (dwarf_version >= 3 || !dwarf_strict)
20293 dwarf2out_decl (decl);
20294 else
20295 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20296 decl_die = comp_unit_die;
20297 break;
20299 default:
20300 gcc_unreachable ();
20303 /* We should be able to find the DIE now. */
20304 if (!decl_die)
20305 decl_die = lookup_decl_die (decl);
20306 gcc_assert (decl_die);
20309 return decl_die;
20312 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20313 always returned. */
20315 static dw_die_ref
20316 force_type_die (tree type)
20318 dw_die_ref type_die;
20320 type_die = lookup_type_die (type);
20321 if (!type_die)
20323 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20325 type_die = modified_type_die (type, TYPE_READONLY (type),
20326 TYPE_VOLATILE (type), context_die);
20327 gcc_assert (type_die);
20329 return type_die;
20332 /* Force out any required namespaces to be able to output DECL,
20333 and return the new context_die for it, if it's changed. */
20335 static dw_die_ref
20336 setup_namespace_context (tree thing, dw_die_ref context_die)
20338 tree context = (DECL_P (thing)
20339 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20340 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20341 /* Force out the namespace. */
20342 context_die = force_decl_die (context);
20344 return context_die;
20347 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20348 type) within its namespace, if appropriate.
20350 For compatibility with older debuggers, namespace DIEs only contain
20351 declarations; all definitions are emitted at CU scope. */
20353 static dw_die_ref
20354 declare_in_namespace (tree thing, dw_die_ref context_die)
20356 dw_die_ref ns_context;
20358 if (debug_info_level <= DINFO_LEVEL_TERSE)
20359 return context_die;
20361 /* If this decl is from an inlined function, then don't try to emit it in its
20362 namespace, as we will get confused. It would have already been emitted
20363 when the abstract instance of the inline function was emitted anyways. */
20364 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20365 return context_die;
20367 ns_context = setup_namespace_context (thing, context_die);
20369 if (ns_context != context_die)
20371 if (is_fortran ())
20372 return ns_context;
20373 if (DECL_P (thing))
20374 gen_decl_die (thing, NULL, ns_context);
20375 else
20376 gen_type_die (thing, ns_context);
20378 return context_die;
20381 /* Generate a DIE for a namespace or namespace alias. */
20383 static void
20384 gen_namespace_die (tree decl, dw_die_ref context_die)
20386 dw_die_ref namespace_die;
20388 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20389 they are an alias of. */
20390 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20392 /* Output a real namespace or module. */
20393 context_die = setup_namespace_context (decl, comp_unit_die);
20394 namespace_die = new_die (is_fortran ()
20395 ? DW_TAG_module : DW_TAG_namespace,
20396 context_die, decl);
20397 /* For Fortran modules defined in different CU don't add src coords. */
20398 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20400 const char *name = dwarf2_name (decl, 0);
20401 if (name)
20402 add_name_attribute (namespace_die, name);
20404 else
20405 add_name_and_src_coords_attributes (namespace_die, decl);
20406 if (DECL_EXTERNAL (decl))
20407 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20408 equate_decl_number_to_die (decl, namespace_die);
20410 else
20412 /* Output a namespace alias. */
20414 /* Force out the namespace we are an alias of, if necessary. */
20415 dw_die_ref origin_die
20416 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20418 if (DECL_CONTEXT (decl) == NULL_TREE
20419 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20420 context_die = setup_namespace_context (decl, comp_unit_die);
20421 /* Now create the namespace alias DIE. */
20422 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20423 add_name_and_src_coords_attributes (namespace_die, decl);
20424 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20425 equate_decl_number_to_die (decl, namespace_die);
20429 /* Generate Dwarf debug information for a decl described by DECL. */
20431 static void
20432 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20434 tree decl_or_origin = decl ? decl : origin;
20435 tree class_origin = NULL, ultimate_origin;
20437 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20438 return;
20440 switch (TREE_CODE (decl_or_origin))
20442 case ERROR_MARK:
20443 break;
20445 case CONST_DECL:
20446 if (!is_fortran ())
20448 /* The individual enumerators of an enum type get output when we output
20449 the Dwarf representation of the relevant enum type itself. */
20450 break;
20453 /* Emit its type. */
20454 gen_type_die (TREE_TYPE (decl), context_die);
20456 /* And its containing namespace. */
20457 context_die = declare_in_namespace (decl, context_die);
20459 gen_const_die (decl, context_die);
20460 break;
20462 case FUNCTION_DECL:
20463 /* Don't output any DIEs to represent mere function declarations,
20464 unless they are class members or explicit block externs. */
20465 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20466 && DECL_CONTEXT (decl_or_origin) == NULL_TREE
20467 && (current_function_decl == NULL_TREE
20468 || DECL_ARTIFICIAL (decl_or_origin)))
20469 break;
20471 #if 0
20472 /* FIXME */
20473 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20474 on local redeclarations of global functions. That seems broken. */
20475 if (current_function_decl != decl)
20476 /* This is only a declaration. */;
20477 #endif
20479 /* If we're emitting a clone, emit info for the abstract instance. */
20480 if (origin || DECL_ORIGIN (decl) != decl)
20481 dwarf2out_abstract_function (origin
20482 ? DECL_ORIGIN (origin)
20483 : DECL_ABSTRACT_ORIGIN (decl));
20485 /* If we're emitting an out-of-line copy of an inline function,
20486 emit info for the abstract instance and set up to refer to it. */
20487 else if (cgraph_function_possibly_inlined_p (decl)
20488 && ! DECL_ABSTRACT (decl)
20489 && ! class_or_namespace_scope_p (context_die)
20490 /* dwarf2out_abstract_function won't emit a die if this is just
20491 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20492 that case, because that works only if we have a die. */
20493 && DECL_INITIAL (decl) != NULL_TREE)
20495 dwarf2out_abstract_function (decl);
20496 set_decl_origin_self (decl);
20499 /* Otherwise we're emitting the primary DIE for this decl. */
20500 else if (debug_info_level > DINFO_LEVEL_TERSE)
20502 /* Before we describe the FUNCTION_DECL itself, make sure that we
20503 have described its return type. */
20504 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20506 /* And its virtual context. */
20507 if (DECL_VINDEX (decl) != NULL_TREE)
20508 gen_type_die (DECL_CONTEXT (decl), context_die);
20510 /* And its containing type. */
20511 if (!origin)
20512 origin = decl_class_context (decl);
20513 if (origin != NULL_TREE)
20514 gen_type_die_for_member (origin, decl, context_die);
20516 /* And its containing namespace. */
20517 context_die = declare_in_namespace (decl, context_die);
20520 /* Now output a DIE to represent the function itself. */
20521 if (decl)
20522 gen_subprogram_die (decl, context_die);
20523 break;
20525 case TYPE_DECL:
20526 /* If we are in terse mode, don't generate any DIEs to represent any
20527 actual typedefs. */
20528 if (debug_info_level <= DINFO_LEVEL_TERSE)
20529 break;
20531 /* In the special case of a TYPE_DECL node representing the declaration
20532 of some type tag, if the given TYPE_DECL is marked as having been
20533 instantiated from some other (original) TYPE_DECL node (e.g. one which
20534 was generated within the original definition of an inline function) we
20535 used to generate a special (abbreviated) DW_TAG_structure_type,
20536 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20537 should be actually referencing those DIEs, as variable DIEs with that
20538 type would be emitted already in the abstract origin, so it was always
20539 removed during unused type prunning. Don't add anything in this
20540 case. */
20541 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20542 break;
20544 if (is_redundant_typedef (decl))
20545 gen_type_die (TREE_TYPE (decl), context_die);
20546 else
20547 /* Output a DIE to represent the typedef itself. */
20548 gen_typedef_die (decl, context_die);
20549 break;
20551 case LABEL_DECL:
20552 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20553 gen_label_die (decl, context_die);
20554 break;
20556 case VAR_DECL:
20557 case RESULT_DECL:
20558 /* If we are in terse mode, don't generate any DIEs to represent any
20559 variable declarations or definitions. */
20560 if (debug_info_level <= DINFO_LEVEL_TERSE)
20561 break;
20563 /* Output any DIEs that are needed to specify the type of this data
20564 object. */
20565 if (decl_by_reference_p (decl_or_origin))
20566 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20567 else
20568 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20570 /* And its containing type. */
20571 class_origin = decl_class_context (decl_or_origin);
20572 if (class_origin != NULL_TREE)
20573 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20575 /* And its containing namespace. */
20576 context_die = declare_in_namespace (decl_or_origin, context_die);
20578 /* Now output the DIE to represent the data object itself. This gets
20579 complicated because of the possibility that the VAR_DECL really
20580 represents an inlined instance of a formal parameter for an inline
20581 function. */
20582 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20583 if (ultimate_origin != NULL_TREE
20584 && TREE_CODE (ultimate_origin) == PARM_DECL)
20585 gen_formal_parameter_die (decl, origin,
20586 true /* Emit name attribute. */,
20587 context_die);
20588 else
20589 gen_variable_die (decl, origin, context_die);
20590 break;
20592 case FIELD_DECL:
20593 /* Ignore the nameless fields that are used to skip bits but handle C++
20594 anonymous unions and structs. */
20595 if (DECL_NAME (decl) != NULL_TREE
20596 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20597 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20599 gen_type_die (member_declared_type (decl), context_die);
20600 gen_field_die (decl, context_die);
20602 break;
20604 case PARM_DECL:
20605 if (DECL_BY_REFERENCE (decl_or_origin))
20606 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20607 else
20608 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20609 gen_formal_parameter_die (decl, origin,
20610 true /* Emit name attribute. */,
20611 context_die);
20612 break;
20614 case NAMESPACE_DECL:
20615 case IMPORTED_DECL:
20616 if (dwarf_version >= 3 || !dwarf_strict)
20617 gen_namespace_die (decl, context_die);
20618 break;
20620 default:
20621 /* Probably some frontend-internal decl. Assume we don't care. */
20622 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20623 break;
20627 /* Output debug information for global decl DECL. Called from toplev.c after
20628 compilation proper has finished. */
20630 static void
20631 dwarf2out_global_decl (tree decl)
20633 /* Output DWARF2 information for file-scope tentative data object
20634 declarations, file-scope (extern) function declarations (which
20635 had no corresponding body) and file-scope tagged type declarations
20636 and definitions which have not yet been forced out. */
20637 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20638 dwarf2out_decl (decl);
20641 /* Output debug information for type decl DECL. Called from toplev.c
20642 and from language front ends (to record built-in types). */
20643 static void
20644 dwarf2out_type_decl (tree decl, int local)
20646 if (!local)
20647 dwarf2out_decl (decl);
20650 /* Output debug information for imported module or decl DECL.
20651 NAME is non-NULL name in the lexical block if the decl has been renamed.
20652 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20653 that DECL belongs to.
20654 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20655 static void
20656 dwarf2out_imported_module_or_decl_1 (tree decl,
20657 tree name,
20658 tree lexical_block,
20659 dw_die_ref lexical_block_die)
20661 expanded_location xloc;
20662 dw_die_ref imported_die = NULL;
20663 dw_die_ref at_import_die;
20665 if (TREE_CODE (decl) == IMPORTED_DECL)
20667 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20668 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20669 gcc_assert (decl);
20671 else
20672 xloc = expand_location (input_location);
20674 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20676 at_import_die = force_type_die (TREE_TYPE (decl));
20677 /* For namespace N { typedef void T; } using N::T; base_type_die
20678 returns NULL, but DW_TAG_imported_declaration requires
20679 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20680 if (!at_import_die)
20682 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20683 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20684 at_import_die = lookup_type_die (TREE_TYPE (decl));
20685 gcc_assert (at_import_die);
20688 else
20690 at_import_die = lookup_decl_die (decl);
20691 if (!at_import_die)
20693 /* If we're trying to avoid duplicate debug info, we may not have
20694 emitted the member decl for this field. Emit it now. */
20695 if (TREE_CODE (decl) == FIELD_DECL)
20697 tree type = DECL_CONTEXT (decl);
20699 if (TYPE_CONTEXT (type)
20700 && TYPE_P (TYPE_CONTEXT (type))
20701 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20702 DINFO_USAGE_DIR_USE))
20703 return;
20704 gen_type_die_for_member (type, decl,
20705 get_context_die (TYPE_CONTEXT (type)));
20707 at_import_die = force_decl_die (decl);
20711 if (TREE_CODE (decl) == NAMESPACE_DECL)
20713 if (dwarf_version >= 3 || !dwarf_strict)
20714 imported_die = new_die (DW_TAG_imported_module,
20715 lexical_block_die,
20716 lexical_block);
20717 else
20718 return;
20720 else
20721 imported_die = new_die (DW_TAG_imported_declaration,
20722 lexical_block_die,
20723 lexical_block);
20725 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20726 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20727 if (name)
20728 add_AT_string (imported_die, DW_AT_name,
20729 IDENTIFIER_POINTER (name));
20730 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20733 /* Output debug information for imported module or decl DECL.
20734 NAME is non-NULL name in context if the decl has been renamed.
20735 CHILD is true if decl is one of the renamed decls as part of
20736 importing whole module. */
20738 static void
20739 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20740 bool child)
20742 /* dw_die_ref at_import_die; */
20743 dw_die_ref scope_die;
20745 if (debug_info_level <= DINFO_LEVEL_TERSE)
20746 return;
20748 gcc_assert (decl);
20750 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20751 We need decl DIE for reference and scope die. First, get DIE for the decl
20752 itself. */
20754 /* Get the scope die for decl context. Use comp_unit_die for global module
20755 or decl. If die is not found for non globals, force new die. */
20756 if (context
20757 && TYPE_P (context)
20758 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20759 return;
20761 if (!(dwarf_version >= 3 || !dwarf_strict))
20762 return;
20764 scope_die = get_context_die (context);
20766 if (child)
20768 gcc_assert (scope_die->die_child);
20769 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20770 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20771 scope_die = scope_die->die_child;
20774 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20775 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20779 /* Write the debugging output for DECL. */
20781 void
20782 dwarf2out_decl (tree decl)
20784 dw_die_ref context_die = comp_unit_die;
20786 switch (TREE_CODE (decl))
20788 case ERROR_MARK:
20789 return;
20791 case FUNCTION_DECL:
20792 /* What we would really like to do here is to filter out all mere
20793 file-scope declarations of file-scope functions which are never
20794 referenced later within this translation unit (and keep all of ones
20795 that *are* referenced later on) but we aren't clairvoyant, so we have
20796 no idea which functions will be referenced in the future (i.e. later
20797 on within the current translation unit). So here we just ignore all
20798 file-scope function declarations which are not also definitions. If
20799 and when the debugger needs to know something about these functions,
20800 it will have to hunt around and find the DWARF information associated
20801 with the definition of the function.
20803 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20804 nodes represent definitions and which ones represent mere
20805 declarations. We have to check DECL_INITIAL instead. That's because
20806 the C front-end supports some weird semantics for "extern inline"
20807 function definitions. These can get inlined within the current
20808 translation unit (and thus, we need to generate Dwarf info for their
20809 abstract instances so that the Dwarf info for the concrete inlined
20810 instances can have something to refer to) but the compiler never
20811 generates any out-of-lines instances of such things (despite the fact
20812 that they *are* definitions).
20814 The important point is that the C front-end marks these "extern
20815 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20816 them anyway. Note that the C++ front-end also plays some similar games
20817 for inline function definitions appearing within include files which
20818 also contain `#pragma interface' pragmas. */
20819 if (DECL_INITIAL (decl) == NULL_TREE)
20820 return;
20822 /* If we're a nested function, initially use a parent of NULL; if we're
20823 a plain function, this will be fixed up in decls_for_scope. If
20824 we're a method, it will be ignored, since we already have a DIE. */
20825 if (decl_function_context (decl)
20826 /* But if we're in terse mode, we don't care about scope. */
20827 && debug_info_level > DINFO_LEVEL_TERSE)
20828 context_die = NULL;
20829 break;
20831 case VAR_DECL:
20832 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20833 declaration and if the declaration was never even referenced from
20834 within this entire compilation unit. We suppress these DIEs in
20835 order to save space in the .debug section (by eliminating entries
20836 which are probably useless). Note that we must not suppress
20837 block-local extern declarations (whether used or not) because that
20838 would screw-up the debugger's name lookup mechanism and cause it to
20839 miss things which really ought to be in scope at a given point. */
20840 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20841 return;
20843 /* For local statics lookup proper context die. */
20844 if (TREE_STATIC (decl) && decl_function_context (decl))
20845 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20847 /* If we are in terse mode, don't generate any DIEs to represent any
20848 variable declarations or definitions. */
20849 if (debug_info_level <= DINFO_LEVEL_TERSE)
20850 return;
20851 break;
20853 case CONST_DECL:
20854 if (debug_info_level <= DINFO_LEVEL_TERSE)
20855 return;
20856 if (!is_fortran ())
20857 return;
20858 if (TREE_STATIC (decl) && decl_function_context (decl))
20859 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20860 break;
20862 case NAMESPACE_DECL:
20863 case IMPORTED_DECL:
20864 if (debug_info_level <= DINFO_LEVEL_TERSE)
20865 return;
20866 if (lookup_decl_die (decl) != NULL)
20867 return;
20868 break;
20870 case TYPE_DECL:
20871 /* Don't emit stubs for types unless they are needed by other DIEs. */
20872 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20873 return;
20875 /* Don't bother trying to generate any DIEs to represent any of the
20876 normal built-in types for the language we are compiling. */
20877 if (DECL_IS_BUILTIN (decl))
20879 /* OK, we need to generate one for `bool' so GDB knows what type
20880 comparisons have. */
20881 if (is_cxx ()
20882 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
20883 && ! DECL_IGNORED_P (decl))
20884 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
20886 return;
20889 /* If we are in terse mode, don't generate any DIEs for types. */
20890 if (debug_info_level <= DINFO_LEVEL_TERSE)
20891 return;
20893 /* If we're a function-scope tag, initially use a parent of NULL;
20894 this will be fixed up in decls_for_scope. */
20895 if (decl_function_context (decl))
20896 context_die = NULL;
20898 break;
20900 default:
20901 return;
20904 gen_decl_die (decl, NULL, context_die);
20907 /* Write the debugging output for DECL. */
20909 static void
20910 dwarf2out_function_decl (tree decl)
20912 dwarf2out_decl (decl);
20914 htab_empty (decl_loc_table);
20917 /* Output a marker (i.e. a label) for the beginning of the generated code for
20918 a lexical block. */
20920 static void
20921 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20922 unsigned int blocknum)
20924 switch_to_section (current_function_section ());
20925 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20928 /* Output a marker (i.e. a label) for the end of the generated code for a
20929 lexical block. */
20931 static void
20932 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20934 switch_to_section (current_function_section ());
20935 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20938 /* Returns nonzero if it is appropriate not to emit any debugging
20939 information for BLOCK, because it doesn't contain any instructions.
20941 Don't allow this for blocks with nested functions or local classes
20942 as we would end up with orphans, and in the presence of scheduling
20943 we may end up calling them anyway. */
20945 static bool
20946 dwarf2out_ignore_block (const_tree block)
20948 tree decl;
20949 unsigned int i;
20951 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20952 if (TREE_CODE (decl) == FUNCTION_DECL
20953 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20954 return 0;
20955 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20957 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20958 if (TREE_CODE (decl) == FUNCTION_DECL
20959 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20960 return 0;
20963 return 1;
20966 /* Hash table routines for file_hash. */
20968 static int
20969 file_table_eq (const void *p1_p, const void *p2_p)
20971 const struct dwarf_file_data *const p1 =
20972 (const struct dwarf_file_data *) p1_p;
20973 const char *const p2 = (const char *) p2_p;
20974 return strcmp (p1->filename, p2) == 0;
20977 static hashval_t
20978 file_table_hash (const void *p_p)
20980 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20981 return htab_hash_string (p->filename);
20984 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20985 dwarf2out.c) and return its "index". The index of each (known) filename is
20986 just a unique number which is associated with only that one filename. We
20987 need such numbers for the sake of generating labels (in the .debug_sfnames
20988 section) and references to those files numbers (in the .debug_srcinfo
20989 and.debug_macinfo sections). If the filename given as an argument is not
20990 found in our current list, add it to the list and assign it the next
20991 available unique index number. In order to speed up searches, we remember
20992 the index of the filename was looked up last. This handles the majority of
20993 all searches. */
20995 static struct dwarf_file_data *
20996 lookup_filename (const char *file_name)
20998 void ** slot;
20999 struct dwarf_file_data * created;
21001 /* Check to see if the file name that was searched on the previous
21002 call matches this file name. If so, return the index. */
21003 if (file_table_last_lookup
21004 && (file_name == file_table_last_lookup->filename
21005 || strcmp (file_table_last_lookup->filename, file_name) == 0))
21006 return file_table_last_lookup;
21008 /* Didn't match the previous lookup, search the table. */
21009 slot = htab_find_slot_with_hash (file_table, file_name,
21010 htab_hash_string (file_name), INSERT);
21011 if (*slot)
21012 return (struct dwarf_file_data *) *slot;
21014 created = ggc_alloc_dwarf_file_data ();
21015 created->filename = file_name;
21016 created->emitted_number = 0;
21017 *slot = created;
21018 return created;
21021 /* If the assembler will construct the file table, then translate the compiler
21022 internal file table number into the assembler file table number, and emit
21023 a .file directive if we haven't already emitted one yet. The file table
21024 numbers are different because we prune debug info for unused variables and
21025 types, which may include filenames. */
21027 static int
21028 maybe_emit_file (struct dwarf_file_data * fd)
21030 if (! fd->emitted_number)
21032 if (last_emitted_file)
21033 fd->emitted_number = last_emitted_file->emitted_number + 1;
21034 else
21035 fd->emitted_number = 1;
21036 last_emitted_file = fd;
21038 if (DWARF2_ASM_LINE_DEBUG_INFO)
21040 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21041 output_quoted_string (asm_out_file,
21042 remap_debug_filename (fd->filename));
21043 fputc ('\n', asm_out_file);
21047 return fd->emitted_number;
21050 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21051 That generation should happen after function debug info has been
21052 generated. The value of the attribute is the constant value of ARG. */
21054 static void
21055 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21057 die_arg_entry entry;
21059 if (!die || !arg)
21060 return;
21062 if (!tmpl_value_parm_die_table)
21063 tmpl_value_parm_die_table
21064 = VEC_alloc (die_arg_entry, gc, 32);
21066 entry.die = die;
21067 entry.arg = arg;
21068 VEC_safe_push (die_arg_entry, gc,
21069 tmpl_value_parm_die_table,
21070 &entry);
21073 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21074 by append_entry_to_tmpl_value_parm_die_table. This function must
21075 be called after function DIEs have been generated. */
21077 static void
21078 gen_remaining_tmpl_value_param_die_attribute (void)
21080 if (tmpl_value_parm_die_table)
21082 unsigned i;
21083 die_arg_entry *e;
21085 for (i = 0;
21086 VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
21087 i++)
21088 tree_add_const_value_attribute (e->die, e->arg);
21093 /* Replace DW_AT_name for the decl with name. */
21095 static void
21096 dwarf2out_set_name (tree decl, tree name)
21098 dw_die_ref die;
21099 dw_attr_ref attr;
21100 const char *dname;
21102 die = TYPE_SYMTAB_DIE (decl);
21103 if (!die)
21104 return;
21106 dname = dwarf2_name (name, 0);
21107 if (!dname)
21108 return;
21110 attr = get_AT (die, DW_AT_name);
21111 if (attr)
21113 struct indirect_string_node *node;
21115 node = find_AT_string (dname);
21116 /* replace the string. */
21117 attr->dw_attr_val.v.val_str = node;
21120 else
21121 add_name_attribute (die, dname);
21124 /* Called by the final INSN scan whenever we see a direct function call.
21125 Make an entry into the direct call table, recording the point of call
21126 and a reference to the target function's debug entry. */
21128 static void
21129 dwarf2out_direct_call (tree targ)
21131 dcall_entry e;
21132 tree origin = decl_ultimate_origin (targ);
21134 /* If this is a clone, use the abstract origin as the target. */
21135 if (origin)
21136 targ = origin;
21138 e.poc_label_num = poc_label_num++;
21139 e.poc_decl = current_function_decl;
21140 e.targ_die = force_decl_die (targ);
21141 VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21143 /* Drop a label at the return point to mark the point of call. */
21144 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21147 /* Returns a hash value for X (which really is a struct vcall_insn). */
21149 static hashval_t
21150 vcall_insn_table_hash (const void *x)
21152 return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21155 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21156 insnd_uid of *Y. */
21158 static int
21159 vcall_insn_table_eq (const void *x, const void *y)
21161 return (((const struct vcall_insn *) x)->insn_uid
21162 == ((const struct vcall_insn *) y)->insn_uid);
21165 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE. */
21167 static void
21168 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21170 struct vcall_insn *item = ggc_alloc_vcall_insn ();
21171 struct vcall_insn **slot;
21173 gcc_assert (item);
21174 item->insn_uid = insn_uid;
21175 item->vtable_slot = vtable_slot;
21176 slot = (struct vcall_insn **)
21177 htab_find_slot_with_hash (vcall_insn_table, &item,
21178 (hashval_t) insn_uid, INSERT);
21179 *slot = item;
21182 /* Return the VTABLE_SLOT associated with INSN_UID. */
21184 static unsigned int
21185 lookup_vcall_insn (unsigned int insn_uid)
21187 struct vcall_insn item;
21188 struct vcall_insn *p;
21190 item.insn_uid = insn_uid;
21191 item.vtable_slot = 0;
21192 p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21193 (void *) &item,
21194 (hashval_t) insn_uid);
21195 if (p == NULL)
21196 return (unsigned int) -1;
21197 return p->vtable_slot;
21201 /* Called when lowering indirect calls to RTL. We make a note of INSN_UID
21202 and the OBJ_TYPE_REF_TOKEN from ADDR. For C++ virtual calls, the token
21203 is the vtable slot index that we will need to put in the virtual call
21204 table later. */
21206 static void
21207 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21209 if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21211 tree token = OBJ_TYPE_REF_TOKEN (addr);
21212 if (TREE_CODE (token) == INTEGER_CST)
21213 store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21217 /* Called when scheduling RTL, when a CALL_INSN is split. Copies the
21218 OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21219 with NEW_INSN. */
21221 static void
21222 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21224 unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21226 if (vtable_slot != (unsigned int) -1)
21227 store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21230 /* Called by the final INSN scan whenever we see a virtual function call.
21231 Make an entry into the virtual call table, recording the point of call
21232 and the slot index of the vtable entry used to call the virtual member
21233 function. The slot index was associated with the INSN_UID during the
21234 lowering to RTL. */
21236 static void
21237 dwarf2out_virtual_call (int insn_uid)
21239 unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21240 vcall_entry e;
21242 if (vtable_slot == (unsigned int) -1)
21243 return;
21245 e.poc_label_num = poc_label_num++;
21246 e.vtable_slot = vtable_slot;
21247 VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21249 /* Drop a label at the return point to mark the point of call. */
21250 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21253 /* Called by the final INSN scan whenever we see a var location. We
21254 use it to drop labels in the right places, and throw the location in
21255 our lookup table. */
21257 static void
21258 dwarf2out_var_location (rtx loc_note)
21260 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21261 struct var_loc_node *newloc;
21262 rtx next_real;
21263 static const char *last_label;
21264 static const char *last_postcall_label;
21265 static bool last_in_cold_section_p;
21266 tree decl;
21268 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21269 return;
21271 next_real = next_real_insn (loc_note);
21272 /* If there are no instructions which would be affected by this note,
21273 don't do anything. */
21274 if (next_real == NULL_RTX)
21275 return;
21277 /* If there were any real insns between note we processed last time
21278 and this note (or if it is the first note), clear
21279 last_{,postcall_}label so that they are not reused this time. */
21280 if (last_var_location_insn == NULL_RTX
21281 || last_var_location_insn != next_real
21282 || last_in_cold_section_p != in_cold_section_p)
21284 last_label = NULL;
21285 last_postcall_label = NULL;
21288 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21289 newloc = add_var_loc_to_decl (decl, loc_note,
21290 NOTE_DURING_CALL_P (loc_note)
21291 ? last_postcall_label : last_label);
21292 if (newloc == NULL)
21293 return;
21295 /* If there were no real insns between note we processed last time
21296 and this note, use the label we emitted last time. Otherwise
21297 create a new label and emit it. */
21298 if (last_label == NULL)
21300 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21301 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21302 loclabel_num++;
21303 last_label = ggc_strdup (loclabel);
21306 if (!NOTE_DURING_CALL_P (loc_note))
21307 newloc->label = last_label;
21308 else
21310 if (!last_postcall_label)
21312 sprintf (loclabel, "%s-1", last_label);
21313 last_postcall_label = ggc_strdup (loclabel);
21315 newloc->label = last_postcall_label;
21318 last_var_location_insn = next_real;
21319 last_in_cold_section_p = in_cold_section_p;
21322 /* We need to reset the locations at the beginning of each
21323 function. We can't do this in the end_function hook, because the
21324 declarations that use the locations won't have been output when
21325 that hook is called. Also compute have_multiple_function_sections here. */
21327 static void
21328 dwarf2out_begin_function (tree fun)
21330 if (function_section (fun) != text_section)
21331 have_multiple_function_sections = true;
21333 dwarf2out_note_section_used ();
21336 /* Output a label to mark the beginning of a source code line entry
21337 and record information relating to this source line, in
21338 'line_info_table' for later output of the .debug_line section. */
21340 static void
21341 dwarf2out_source_line (unsigned int line, const char *filename,
21342 int discriminator, bool is_stmt)
21344 static bool last_is_stmt = true;
21346 if (debug_info_level >= DINFO_LEVEL_NORMAL
21347 && line != 0)
21349 int file_num = maybe_emit_file (lookup_filename (filename));
21351 switch_to_section (current_function_section ());
21353 /* If requested, emit something human-readable. */
21354 if (flag_debug_asm)
21355 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21356 filename, line);
21358 if (DWARF2_ASM_LINE_DEBUG_INFO)
21360 /* Emit the .loc directive understood by GNU as. */
21361 fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21362 if (is_stmt != last_is_stmt)
21364 fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21365 last_is_stmt = is_stmt;
21367 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21368 fprintf (asm_out_file, " discriminator %d", discriminator);
21369 fputc ('\n', asm_out_file);
21371 /* Indicate that line number info exists. */
21372 line_info_table_in_use++;
21374 else if (function_section (current_function_decl) != text_section)
21376 dw_separate_line_info_ref line_info;
21377 targetm.asm_out.internal_label (asm_out_file,
21378 SEPARATE_LINE_CODE_LABEL,
21379 separate_line_info_table_in_use);
21381 /* Expand the line info table if necessary. */
21382 if (separate_line_info_table_in_use
21383 == separate_line_info_table_allocated)
21385 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21386 separate_line_info_table
21387 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21388 separate_line_info_table,
21389 separate_line_info_table_allocated);
21390 memset (separate_line_info_table
21391 + separate_line_info_table_in_use,
21393 (LINE_INFO_TABLE_INCREMENT
21394 * sizeof (dw_separate_line_info_entry)));
21397 /* Add the new entry at the end of the line_info_table. */
21398 line_info
21399 = &separate_line_info_table[separate_line_info_table_in_use++];
21400 line_info->dw_file_num = file_num;
21401 line_info->dw_line_num = line;
21402 line_info->function = current_function_funcdef_no;
21404 else
21406 dw_line_info_ref line_info;
21408 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21409 line_info_table_in_use);
21411 /* Expand the line info table if necessary. */
21412 if (line_info_table_in_use == line_info_table_allocated)
21414 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21415 line_info_table
21416 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21417 line_info_table_allocated);
21418 memset (line_info_table + line_info_table_in_use, 0,
21419 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21422 /* Add the new entry at the end of the line_info_table. */
21423 line_info = &line_info_table[line_info_table_in_use++];
21424 line_info->dw_file_num = file_num;
21425 line_info->dw_line_num = line;
21430 /* Record the beginning of a new source file. */
21432 static void
21433 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21435 if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21437 /* Record the beginning of the file for break_out_includes. */
21438 dw_die_ref bincl_die;
21440 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
21441 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21444 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21446 int file_num = maybe_emit_file (lookup_filename (filename));
21448 switch_to_section (debug_macinfo_section);
21449 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21450 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
21451 lineno);
21453 dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
21457 /* Record the end of a source file. */
21459 static void
21460 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21462 if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21463 /* Record the end of the file for break_out_includes. */
21464 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
21466 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21468 switch_to_section (debug_macinfo_section);
21469 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21473 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21474 the tail part of the directive line, i.e. the part which is past the
21475 initial whitespace, #, whitespace, directive-name, whitespace part. */
21477 static void
21478 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21479 const char *buffer ATTRIBUTE_UNUSED)
21481 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21483 switch_to_section (debug_macinfo_section);
21484 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
21485 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21486 dw2_asm_output_nstring (buffer, -1, "The macro");
21490 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21491 the tail part of the directive line, i.e. the part which is past the
21492 initial whitespace, #, whitespace, directive-name, whitespace part. */
21494 static void
21495 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21496 const char *buffer ATTRIBUTE_UNUSED)
21498 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21500 switch_to_section (debug_macinfo_section);
21501 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
21502 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21503 dw2_asm_output_nstring (buffer, -1, "The macro");
21507 /* Set up for Dwarf output at the start of compilation. */
21509 static void
21510 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21512 /* Allocate the file_table. */
21513 file_table = htab_create_ggc (50, file_table_hash,
21514 file_table_eq, NULL);
21516 /* Allocate the decl_die_table. */
21517 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21518 decl_die_table_eq, NULL);
21520 /* Allocate the decl_loc_table. */
21521 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21522 decl_loc_table_eq, NULL);
21524 /* Allocate the initial hunk of the decl_scope_table. */
21525 decl_scope_table = VEC_alloc (tree, gc, 256);
21527 /* Allocate the initial hunk of the abbrev_die_table. */
21528 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21529 (ABBREV_DIE_TABLE_INCREMENT);
21530 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21531 /* Zero-th entry is allocated, but unused. */
21532 abbrev_die_table_in_use = 1;
21534 /* Allocate the initial hunk of the line_info_table. */
21535 line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
21536 (LINE_INFO_TABLE_INCREMENT);
21537 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
21539 /* Zero-th entry is allocated, but unused. */
21540 line_info_table_in_use = 1;
21542 /* Allocate the pubtypes and pubnames vectors. */
21543 pubname_table = VEC_alloc (pubname_entry, gc, 32);
21544 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21546 /* Allocate the table that maps insn UIDs to vtable slot indexes. */
21547 vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
21548 vcall_insn_table_eq, NULL);
21550 /* Generate the initial DIE for the .debug section. Note that the (string)
21551 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
21552 will (typically) be a relative pathname and that this pathname should be
21553 taken as being relative to the directory from which the compiler was
21554 invoked when the given (base) source file was compiled. We will fill
21555 in this value in dwarf2out_finish. */
21556 comp_unit_die = gen_compile_unit_die (NULL);
21558 incomplete_types = VEC_alloc (tree, gc, 64);
21560 used_rtx_array = VEC_alloc (rtx, gc, 32);
21562 debug_info_section = get_section (DEBUG_INFO_SECTION,
21563 SECTION_DEBUG, NULL);
21564 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21565 SECTION_DEBUG, NULL);
21566 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21567 SECTION_DEBUG, NULL);
21568 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
21569 SECTION_DEBUG, NULL);
21570 debug_line_section = get_section (DEBUG_LINE_SECTION,
21571 SECTION_DEBUG, NULL);
21572 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21573 SECTION_DEBUG, NULL);
21574 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21575 SECTION_DEBUG, NULL);
21576 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21577 SECTION_DEBUG, NULL);
21578 debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
21579 SECTION_DEBUG, NULL);
21580 debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
21581 SECTION_DEBUG, NULL);
21582 debug_str_section = get_section (DEBUG_STR_SECTION,
21583 DEBUG_STR_SECTION_FLAGS, NULL);
21584 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21585 SECTION_DEBUG, NULL);
21586 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21587 SECTION_DEBUG, NULL);
21589 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21590 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21591 DEBUG_ABBREV_SECTION_LABEL, 0);
21592 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21593 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21594 COLD_TEXT_SECTION_LABEL, 0);
21595 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21597 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21598 DEBUG_INFO_SECTION_LABEL, 0);
21599 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21600 DEBUG_LINE_SECTION_LABEL, 0);
21601 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21602 DEBUG_RANGES_SECTION_LABEL, 0);
21603 switch_to_section (debug_abbrev_section);
21604 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
21605 switch_to_section (debug_info_section);
21606 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
21607 switch_to_section (debug_line_section);
21608 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
21610 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21612 switch_to_section (debug_macinfo_section);
21613 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21614 DEBUG_MACINFO_SECTION_LABEL, 0);
21615 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
21618 switch_to_section (text_section);
21619 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21620 if (flag_reorder_blocks_and_partition)
21622 cold_text_section = unlikely_text_section ();
21623 switch_to_section (cold_text_section);
21624 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21629 /* Called before cgraph_optimize starts outputtting functions, variables
21630 and toplevel asms into assembly. */
21632 static void
21633 dwarf2out_assembly_start (void)
21635 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE && dwarf2out_do_cfi_asm ())
21637 #ifndef TARGET_UNWIND_INFO
21638 if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
21639 #endif
21640 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21644 /* A helper function for dwarf2out_finish called through
21645 htab_traverse. Emit one queued .debug_str string. */
21647 static int
21648 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21650 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21652 if (node->label && node->refcount)
21654 switch_to_section (debug_str_section);
21655 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21656 assemble_string (node->str, strlen (node->str) + 1);
21659 return 1;
21662 #if ENABLE_ASSERT_CHECKING
21663 /* Verify that all marks are clear. */
21665 static void
21666 verify_marks_clear (dw_die_ref die)
21668 dw_die_ref c;
21670 gcc_assert (! die->die_mark);
21671 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21673 #endif /* ENABLE_ASSERT_CHECKING */
21675 /* Clear the marks for a die and its children.
21676 Be cool if the mark isn't set. */
21678 static void
21679 prune_unmark_dies (dw_die_ref die)
21681 dw_die_ref c;
21683 if (die->die_mark)
21684 die->die_mark = 0;
21685 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21688 /* Given DIE that we're marking as used, find any other dies
21689 it references as attributes and mark them as used. */
21691 static void
21692 prune_unused_types_walk_attribs (dw_die_ref die)
21694 dw_attr_ref a;
21695 unsigned ix;
21697 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
21699 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21701 /* A reference to another DIE.
21702 Make sure that it will get emitted.
21703 If it was broken out into a comdat group, don't follow it. */
21704 if (dwarf_version < 4
21705 || a->dw_attr == DW_AT_specification
21706 || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21707 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21709 /* Set the string's refcount to 0 so that prune_unused_types_mark
21710 accounts properly for it. */
21711 if (AT_class (a) == dw_val_class_str)
21712 a->dw_attr_val.v.val_str->refcount = 0;
21717 /* Mark DIE as being used. If DOKIDS is true, then walk down
21718 to DIE's children. */
21720 static void
21721 prune_unused_types_mark (dw_die_ref die, int dokids)
21723 dw_die_ref c;
21725 if (die->die_mark == 0)
21727 /* We haven't done this node yet. Mark it as used. */
21728 die->die_mark = 1;
21730 /* We also have to mark its parents as used.
21731 (But we don't want to mark our parents' kids due to this.) */
21732 if (die->die_parent)
21733 prune_unused_types_mark (die->die_parent, 0);
21735 /* Mark any referenced nodes. */
21736 prune_unused_types_walk_attribs (die);
21738 /* If this node is a specification,
21739 also mark the definition, if it exists. */
21740 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21741 prune_unused_types_mark (die->die_definition, 1);
21744 if (dokids && die->die_mark != 2)
21746 /* We need to walk the children, but haven't done so yet.
21747 Remember that we've walked the kids. */
21748 die->die_mark = 2;
21750 /* If this is an array type, we need to make sure our
21751 kids get marked, even if they're types. If we're
21752 breaking out types into comdat sections, do this
21753 for all type definitions. */
21754 if (die->die_tag == DW_TAG_array_type
21755 || (dwarf_version >= 4
21756 && is_type_die (die) && ! is_declaration_die (die)))
21757 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21758 else
21759 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21763 /* For local classes, look if any static member functions were emitted
21764 and if so, mark them. */
21766 static void
21767 prune_unused_types_walk_local_classes (dw_die_ref die)
21769 dw_die_ref c;
21771 if (die->die_mark == 2)
21772 return;
21774 switch (die->die_tag)
21776 case DW_TAG_structure_type:
21777 case DW_TAG_union_type:
21778 case DW_TAG_class_type:
21779 break;
21781 case DW_TAG_subprogram:
21782 if (!get_AT_flag (die, DW_AT_declaration)
21783 || die->die_definition != NULL)
21784 prune_unused_types_mark (die, 1);
21785 return;
21787 default:
21788 return;
21791 /* Mark children. */
21792 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21795 /* Walk the tree DIE and mark types that we actually use. */
21797 static void
21798 prune_unused_types_walk (dw_die_ref die)
21800 dw_die_ref c;
21802 /* Don't do anything if this node is already marked and
21803 children have been marked as well. */
21804 if (die->die_mark == 2)
21805 return;
21807 switch (die->die_tag)
21809 case DW_TAG_structure_type:
21810 case DW_TAG_union_type:
21811 case DW_TAG_class_type:
21812 if (die->die_perennial_p)
21813 break;
21815 for (c = die->die_parent; c; c = c->die_parent)
21816 if (c->die_tag == DW_TAG_subprogram)
21817 break;
21819 /* Finding used static member functions inside of classes
21820 is needed just for local classes, because for other classes
21821 static member function DIEs with DW_AT_specification
21822 are emitted outside of the DW_TAG_*_type. If we ever change
21823 it, we'd need to call this even for non-local classes. */
21824 if (c)
21825 prune_unused_types_walk_local_classes (die);
21827 /* It's a type node --- don't mark it. */
21828 return;
21830 case DW_TAG_const_type:
21831 case DW_TAG_packed_type:
21832 case DW_TAG_pointer_type:
21833 case DW_TAG_reference_type:
21834 case DW_TAG_rvalue_reference_type:
21835 case DW_TAG_volatile_type:
21836 case DW_TAG_typedef:
21837 case DW_TAG_array_type:
21838 case DW_TAG_interface_type:
21839 case DW_TAG_friend:
21840 case DW_TAG_variant_part:
21841 case DW_TAG_enumeration_type:
21842 case DW_TAG_subroutine_type:
21843 case DW_TAG_string_type:
21844 case DW_TAG_set_type:
21845 case DW_TAG_subrange_type:
21846 case DW_TAG_ptr_to_member_type:
21847 case DW_TAG_file_type:
21848 if (die->die_perennial_p)
21849 break;
21851 /* It's a type node --- don't mark it. */
21852 return;
21854 default:
21855 /* Mark everything else. */
21856 break;
21859 if (die->die_mark == 0)
21861 die->die_mark = 1;
21863 /* Now, mark any dies referenced from here. */
21864 prune_unused_types_walk_attribs (die);
21867 die->die_mark = 2;
21869 /* Mark children. */
21870 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21873 /* Increment the string counts on strings referred to from DIE's
21874 attributes. */
21876 static void
21877 prune_unused_types_update_strings (dw_die_ref die)
21879 dw_attr_ref a;
21880 unsigned ix;
21882 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
21883 if (AT_class (a) == dw_val_class_str)
21885 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21886 s->refcount++;
21887 /* Avoid unnecessarily putting strings that are used less than
21888 twice in the hash table. */
21889 if (s->refcount
21890 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21892 void ** slot;
21893 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21894 htab_hash_string (s->str),
21895 INSERT);
21896 gcc_assert (*slot == NULL);
21897 *slot = s;
21902 /* Remove from the tree DIE any dies that aren't marked. */
21904 static void
21905 prune_unused_types_prune (dw_die_ref die)
21907 dw_die_ref c;
21909 gcc_assert (die->die_mark);
21910 prune_unused_types_update_strings (die);
21912 if (! die->die_child)
21913 return;
21915 c = die->die_child;
21916 do {
21917 dw_die_ref prev = c;
21918 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21919 if (c == die->die_child)
21921 /* No marked children between 'prev' and the end of the list. */
21922 if (prev == c)
21923 /* No marked children at all. */
21924 die->die_child = NULL;
21925 else
21927 prev->die_sib = c->die_sib;
21928 die->die_child = prev;
21930 return;
21933 if (c != prev->die_sib)
21934 prev->die_sib = c;
21935 prune_unused_types_prune (c);
21936 } while (c != die->die_child);
21939 /* A helper function for dwarf2out_finish called through
21940 htab_traverse. Clear .debug_str strings that we haven't already
21941 decided to emit. */
21943 static int
21944 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21946 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21948 if (!node->label || !node->refcount)
21949 htab_clear_slot (debug_str_hash, h);
21951 return 1;
21954 /* Remove dies representing declarations that we never use. */
21956 static void
21957 prune_unused_types (void)
21959 unsigned int i;
21960 limbo_die_node *node;
21961 comdat_type_node *ctnode;
21962 pubname_ref pub;
21963 dcall_entry *dcall;
21965 #if ENABLE_ASSERT_CHECKING
21966 /* All the marks should already be clear. */
21967 verify_marks_clear (comp_unit_die);
21968 for (node = limbo_die_list; node; node = node->next)
21969 verify_marks_clear (node->die);
21970 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21971 verify_marks_clear (ctnode->root_die);
21972 #endif /* ENABLE_ASSERT_CHECKING */
21974 /* Mark types that are used in global variables. */
21975 premark_types_used_by_global_vars ();
21977 /* Set the mark on nodes that are actually used. */
21978 prune_unused_types_walk (comp_unit_die);
21979 for (node = limbo_die_list; node; node = node->next)
21980 prune_unused_types_walk (node->die);
21981 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21983 prune_unused_types_walk (ctnode->root_die);
21984 prune_unused_types_mark (ctnode->type_die, 1);
21987 /* Also set the mark on nodes referenced from the
21988 pubname_table or arange_table. */
21989 for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
21990 prune_unused_types_mark (pub->die, 1);
21991 for (i = 0; i < arange_table_in_use; i++)
21992 prune_unused_types_mark (arange_table[i], 1);
21994 /* Mark nodes referenced from the direct call table. */
21995 for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, dcall); i++)
21996 prune_unused_types_mark (dcall->targ_die, 1);
21998 /* Get rid of nodes that aren't marked; and update the string counts. */
21999 if (debug_str_hash && debug_str_hash_forced)
22000 htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22001 else if (debug_str_hash)
22002 htab_empty (debug_str_hash);
22003 prune_unused_types_prune (comp_unit_die);
22004 for (node = limbo_die_list; node; node = node->next)
22005 prune_unused_types_prune (node->die);
22006 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22007 prune_unused_types_prune (ctnode->root_die);
22009 /* Leave the marks clear. */
22010 prune_unmark_dies (comp_unit_die);
22011 for (node = limbo_die_list; node; node = node->next)
22012 prune_unmark_dies (node->die);
22013 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22014 prune_unmark_dies (ctnode->root_die);
22017 /* Set the parameter to true if there are any relative pathnames in
22018 the file table. */
22019 static int
22020 file_table_relative_p (void ** slot, void *param)
22022 bool *p = (bool *) param;
22023 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22024 if (!IS_ABSOLUTE_PATH (d->filename))
22026 *p = true;
22027 return 0;
22029 return 1;
22032 /* Routines to manipulate hash table of comdat type units. */
22034 static hashval_t
22035 htab_ct_hash (const void *of)
22037 hashval_t h;
22038 const comdat_type_node *const type_node = (const comdat_type_node *) of;
22040 memcpy (&h, type_node->signature, sizeof (h));
22041 return h;
22044 static int
22045 htab_ct_eq (const void *of1, const void *of2)
22047 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22048 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22050 return (! memcmp (type_node_1->signature, type_node_2->signature,
22051 DWARF_TYPE_SIGNATURE_SIZE));
22054 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22055 to the location it would have been added, should we know its
22056 DECL_ASSEMBLER_NAME when we added other attributes. This will
22057 probably improve compactness of debug info, removing equivalent
22058 abbrevs, and hide any differences caused by deferring the
22059 computation of the assembler name, triggered by e.g. PCH. */
22061 static inline void
22062 move_linkage_attr (dw_die_ref die)
22064 unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22065 dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22067 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22068 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22070 while (--ix > 0)
22072 dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22074 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22075 break;
22078 if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22080 VEC_pop (dw_attr_node, die->die_attr);
22081 VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22085 /* Helper function for resolve_addr, attempt to resolve
22086 one CONST_STRING, return non-zero if not successful. Similarly verify that
22087 SYMBOL_REFs refer to variables emitted in the current CU. */
22089 static int
22090 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22092 rtx rtl = *addr;
22094 if (GET_CODE (rtl) == CONST_STRING)
22096 size_t len = strlen (XSTR (rtl, 0)) + 1;
22097 tree t = build_string (len, XSTR (rtl, 0));
22098 tree tlen = build_int_cst (NULL_TREE, len - 1);
22099 TREE_TYPE (t)
22100 = build_array_type (char_type_node, build_index_type (tlen));
22101 rtl = lookup_constant_def (t);
22102 if (!rtl || !MEM_P (rtl))
22103 return 1;
22104 rtl = XEXP (rtl, 0);
22105 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22106 *addr = rtl;
22107 return 0;
22110 if (GET_CODE (rtl) == SYMBOL_REF
22111 && SYMBOL_REF_DECL (rtl)
22112 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22113 return 1;
22115 if (GET_CODE (rtl) == CONST
22116 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22117 return 1;
22119 return 0;
22122 /* Helper function for resolve_addr, handle one location
22123 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22124 the location list couldn't be resolved. */
22126 static bool
22127 resolve_addr_in_expr (dw_loc_descr_ref loc)
22129 for (; loc; loc = loc->dw_loc_next)
22130 if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22131 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22132 || (loc->dw_loc_opc == DW_OP_implicit_value
22133 && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22134 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22135 return false;
22136 return true;
22139 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22140 an address in .rodata section if the string literal is emitted there,
22141 or remove the containing location list or replace DW_AT_const_value
22142 with DW_AT_location and empty location expression, if it isn't found
22143 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22144 to something that has been emitted in the current CU. */
22146 static void
22147 resolve_addr (dw_die_ref die)
22149 dw_die_ref c;
22150 dw_attr_ref a;
22151 dw_loc_list_ref *curr;
22152 unsigned ix;
22154 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
22155 switch (AT_class (a))
22157 case dw_val_class_loc_list:
22158 curr = AT_loc_list_ptr (a);
22159 while (*curr)
22161 if (!resolve_addr_in_expr ((*curr)->expr))
22163 dw_loc_list_ref next = (*curr)->dw_loc_next;
22164 if (next && (*curr)->ll_symbol)
22166 gcc_assert (!next->ll_symbol);
22167 next->ll_symbol = (*curr)->ll_symbol;
22169 *curr = next;
22171 else
22172 curr = &(*curr)->dw_loc_next;
22174 if (!AT_loc_list (a))
22176 remove_AT (die, a->dw_attr);
22177 ix--;
22179 break;
22180 case dw_val_class_loc:
22181 if (!resolve_addr_in_expr (AT_loc (a)))
22183 remove_AT (die, a->dw_attr);
22184 ix--;
22186 break;
22187 case dw_val_class_addr:
22188 if (a->dw_attr == DW_AT_const_value
22189 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22191 remove_AT (die, a->dw_attr);
22192 ix--;
22194 break;
22195 default:
22196 break;
22199 FOR_EACH_CHILD (die, c, resolve_addr (c));
22202 /* Output stuff that dwarf requires at the end of every file,
22203 and generate the DWARF-2 debugging info. */
22205 static void
22206 dwarf2out_finish (const char *filename)
22208 limbo_die_node *node, *next_node;
22209 comdat_type_node *ctnode;
22210 htab_t comdat_type_table;
22211 dw_die_ref die = 0;
22212 unsigned int i;
22214 gen_remaining_tmpl_value_param_die_attribute ();
22216 /* Add the name for the main input file now. We delayed this from
22217 dwarf2out_init to avoid complications with PCH. */
22218 add_name_attribute (comp_unit_die, remap_debug_filename (filename));
22219 if (!IS_ABSOLUTE_PATH (filename))
22220 add_comp_dir_attribute (comp_unit_die);
22221 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
22223 bool p = false;
22224 htab_traverse (file_table, file_table_relative_p, &p);
22225 if (p)
22226 add_comp_dir_attribute (comp_unit_die);
22229 for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22231 add_location_or_const_value_attribute (
22232 VEC_index (deferred_locations, deferred_locations_list, i)->die,
22233 VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22234 DW_AT_location);
22237 /* Traverse the limbo die list, and add parent/child links. The only
22238 dies without parents that should be here are concrete instances of
22239 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
22240 For concrete instances, we can get the parent die from the abstract
22241 instance. */
22242 for (node = limbo_die_list; node; node = next_node)
22244 next_node = node->next;
22245 die = node->die;
22247 if (die->die_parent == NULL)
22249 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22251 if (origin)
22252 add_child_die (origin->die_parent, die);
22253 else if (die == comp_unit_die)
22255 else if (seen_error ())
22256 /* It's OK to be confused by errors in the input. */
22257 add_child_die (comp_unit_die, die);
22258 else
22260 /* In certain situations, the lexical block containing a
22261 nested function can be optimized away, which results
22262 in the nested function die being orphaned. Likewise
22263 with the return type of that nested function. Force
22264 this to be a child of the containing function.
22266 It may happen that even the containing function got fully
22267 inlined and optimized out. In that case we are lost and
22268 assign the empty child. This should not be big issue as
22269 the function is likely unreachable too. */
22270 tree context = NULL_TREE;
22272 gcc_assert (node->created_for);
22274 if (DECL_P (node->created_for))
22275 context = DECL_CONTEXT (node->created_for);
22276 else if (TYPE_P (node->created_for))
22277 context = TYPE_CONTEXT (node->created_for);
22279 gcc_assert (context
22280 && (TREE_CODE (context) == FUNCTION_DECL
22281 || TREE_CODE (context) == NAMESPACE_DECL));
22283 origin = lookup_decl_die (context);
22284 if (origin)
22285 add_child_die (origin, die);
22286 else
22287 add_child_die (comp_unit_die, die);
22292 limbo_die_list = NULL;
22294 resolve_addr (comp_unit_die);
22296 for (node = deferred_asm_name; node; node = node->next)
22298 tree decl = node->created_for;
22299 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22301 add_linkage_attr (node->die, decl);
22302 move_linkage_attr (node->die);
22306 deferred_asm_name = NULL;
22308 /* Walk through the list of incomplete types again, trying once more to
22309 emit full debugging info for them. */
22310 retry_incomplete_types ();
22312 if (flag_eliminate_unused_debug_types)
22313 prune_unused_types ();
22315 /* Generate separate CUs for each of the include files we've seen.
22316 They will go into limbo_die_list. */
22317 if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22318 break_out_includes (comp_unit_die);
22320 /* Generate separate COMDAT sections for type DIEs. */
22321 if (dwarf_version >= 4)
22323 break_out_comdat_types (comp_unit_die);
22325 /* Each new type_unit DIE was added to the limbo die list when created.
22326 Since these have all been added to comdat_type_list, clear the
22327 limbo die list. */
22328 limbo_die_list = NULL;
22330 /* For each new comdat type unit, copy declarations for incomplete
22331 types to make the new unit self-contained (i.e., no direct
22332 references to the main compile unit). */
22333 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22334 copy_decls_for_unworthy_types (ctnode->root_die);
22335 copy_decls_for_unworthy_types (comp_unit_die);
22337 /* In the process of copying declarations from one unit to another,
22338 we may have left some declarations behind that are no longer
22339 referenced. Prune them. */
22340 prune_unused_types ();
22343 /* Traverse the DIE's and add add sibling attributes to those DIE's
22344 that have children. */
22345 add_sibling_attributes (comp_unit_die);
22346 for (node = limbo_die_list; node; node = node->next)
22347 add_sibling_attributes (node->die);
22348 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22349 add_sibling_attributes (ctnode->root_die);
22351 /* Output a terminator label for the .text section. */
22352 switch_to_section (text_section);
22353 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22354 if (flag_reorder_blocks_and_partition)
22356 switch_to_section (unlikely_text_section ());
22357 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22360 /* We can only use the low/high_pc attributes if all of the code was
22361 in .text. */
22362 if (!have_multiple_function_sections
22363 || !(dwarf_version >= 3 || !dwarf_strict))
22365 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
22366 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
22369 else
22371 unsigned fde_idx = 0;
22372 bool range_list_added = false;
22374 /* We need to give .debug_loc and .debug_ranges an appropriate
22375 "base address". Use zero so that these addresses become
22376 absolute. Historically, we've emitted the unexpected
22377 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22378 Emit both to give time for other tools to adapt. */
22379 add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
22380 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
22382 if (text_section_used)
22383 add_ranges_by_labels (comp_unit_die, text_section_label,
22384 text_end_label, &range_list_added);
22385 if (flag_reorder_blocks_and_partition && cold_text_section_used)
22386 add_ranges_by_labels (comp_unit_die, cold_text_section_label,
22387 cold_end_label, &range_list_added);
22389 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
22391 dw_fde_ref fde = &fde_table[fde_idx];
22393 if (fde->dw_fde_switched_sections)
22395 if (!fde->in_std_section)
22396 add_ranges_by_labels (comp_unit_die,
22397 fde->dw_fde_hot_section_label,
22398 fde->dw_fde_hot_section_end_label,
22399 &range_list_added);
22400 if (!fde->cold_in_std_section)
22401 add_ranges_by_labels (comp_unit_die,
22402 fde->dw_fde_unlikely_section_label,
22403 fde->dw_fde_unlikely_section_end_label,
22404 &range_list_added);
22406 else if (!fde->in_std_section)
22407 add_ranges_by_labels (comp_unit_die, fde->dw_fde_begin,
22408 fde->dw_fde_end, &range_list_added);
22411 if (range_list_added)
22412 add_ranges (NULL);
22415 /* Output location list section if necessary. */
22416 if (have_location_lists)
22418 /* Output the location lists info. */
22419 switch_to_section (debug_loc_section);
22420 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22421 DEBUG_LOC_SECTION_LABEL, 0);
22422 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22423 output_location_lists (die);
22426 if (debug_info_level >= DINFO_LEVEL_NORMAL)
22427 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
22428 debug_line_section_label);
22430 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22431 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
22433 /* Output all of the compilation units. We put the main one last so that
22434 the offsets are available to output_pubnames. */
22435 for (node = limbo_die_list; node; node = node->next)
22436 output_comp_unit (node->die, 0);
22438 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22439 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22441 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22443 /* Don't output duplicate types. */
22444 if (*slot != HTAB_EMPTY_ENTRY)
22445 continue;
22447 /* Add a pointer to the line table for the main compilation unit
22448 so that the debugger can make sense of DW_AT_decl_file
22449 attributes. */
22450 if (debug_info_level >= DINFO_LEVEL_NORMAL)
22451 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22452 debug_line_section_label);
22454 output_comdat_type_unit (ctnode);
22455 *slot = ctnode;
22457 htab_delete (comdat_type_table);
22459 /* Output the main compilation unit if non-empty or if .debug_macinfo
22460 has been emitted. */
22461 output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
22463 /* Output the abbreviation table. */
22464 switch_to_section (debug_abbrev_section);
22465 output_abbrev_section ();
22467 /* Output public names table if necessary. */
22468 if (!VEC_empty (pubname_entry, pubname_table))
22470 switch_to_section (debug_pubnames_section);
22471 output_pubnames (pubname_table);
22474 /* Output public types table if necessary. */
22475 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22476 It shouldn't hurt to emit it always, since pure DWARF2 consumers
22477 simply won't look for the section. */
22478 if (!VEC_empty (pubname_entry, pubtype_table))
22480 switch_to_section (debug_pubtypes_section);
22481 output_pubnames (pubtype_table);
22484 /* Output direct and virtual call tables if necessary. */
22485 if (!VEC_empty (dcall_entry, dcall_table))
22487 switch_to_section (debug_dcall_section);
22488 output_dcall_table ();
22490 if (!VEC_empty (vcall_entry, vcall_table))
22492 switch_to_section (debug_vcall_section);
22493 output_vcall_table ();
22496 /* Output the address range information. We only put functions in the arange
22497 table, so don't write it out if we don't have any. */
22498 if (fde_table_in_use)
22500 switch_to_section (debug_aranges_section);
22501 output_aranges ();
22504 /* Output ranges section if necessary. */
22505 if (ranges_table_in_use)
22507 switch_to_section (debug_ranges_section);
22508 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22509 output_ranges ();
22512 /* Output the source line correspondence table. We must do this
22513 even if there is no line information. Otherwise, on an empty
22514 translation unit, we will generate a present, but empty,
22515 .debug_info section. IRIX 6.5 `nm' will then complain when
22516 examining the file. This is done late so that any filenames
22517 used by the debug_info section are marked as 'used'. */
22518 if (! DWARF2_ASM_LINE_DEBUG_INFO)
22520 switch_to_section (debug_line_section);
22521 output_line_info ();
22524 /* Have to end the macro section. */
22525 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22527 switch_to_section (debug_macinfo_section);
22528 dw2_asm_output_data (1, 0, "End compilation unit");
22531 /* If we emitted any DW_FORM_strp form attribute, output the string
22532 table too. */
22533 if (debug_str_hash)
22534 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22536 #else
22538 /* This should never be used, but its address is needed for comparisons. */
22539 const struct gcc_debug_hooks dwarf2_debug_hooks =
22541 0, /* init */
22542 0, /* finish */
22543 0, /* assembly_start */
22544 0, /* define */
22545 0, /* undef */
22546 0, /* start_source_file */
22547 0, /* end_source_file */
22548 0, /* begin_block */
22549 0, /* end_block */
22550 0, /* ignore_block */
22551 0, /* source_line */
22552 0, /* begin_prologue */
22553 0, /* end_prologue */
22554 0, /* begin_epilogue */
22555 0, /* end_epilogue */
22556 0, /* begin_function */
22557 0, /* end_function */
22558 0, /* function_decl */
22559 0, /* global_decl */
22560 0, /* type_decl */
22561 0, /* imported_module_or_decl */
22562 0, /* deferred_inline_function */
22563 0, /* outlining_inline_function */
22564 0, /* label */
22565 0, /* handle_pch */
22566 0, /* var_location */
22567 0, /* switch_text_section */
22568 0, /* direct_call */
22569 0, /* virtual_call_token */
22570 0, /* copy_call_info */
22571 0, /* virtual_call */
22572 0, /* set_name */
22573 0 /* start_end_main_source_file */
22576 #endif /* DWARF2_DEBUGGING_INFO */
22578 #include "gt-dwarf2out.h"