Fix PR#.
[official-gcc.git] / gcc / dwarf2out.c
blobfff0859e0be26f462ef573610593b6840bea3425
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 Free Software Foundation, Inc.
4 Contributed by Gary Funck (gary@intrepid.com).
5 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6 Extensively modified by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* TODO: Emit .debug_line header even when there are no functions, since
25 the file numbers are used by .debug_info. Alternately, leave
26 out locations for types and decls.
27 Avoid talking about ctors and op= for PODs.
28 Factor out common prologue sequences into multiple CIEs. */
30 /* The first part of this file deals with the DWARF 2 frame unwind
31 information, which is also used by the GCC efficient exception handling
32 mechanism. The second part, controlled only by an #ifdef
33 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34 information. */
36 /* DWARF2 Abbreviation Glossary:
38 CFA = Canonical Frame Address
39 a fixed address on the stack which identifies a call frame.
40 We define it to be the value of SP just before the call insn.
41 The CFA register and offset, which may change during the course
42 of the function, are used to calculate its value at runtime.
44 CFI = Call Frame Instruction
45 an instruction for the DWARF2 abstract machine
47 CIE = Common Information Entry
48 information describing information common to one or more FDEs
50 DIE = Debugging Information Entry
52 FDE = Frame Description Entry
53 information describing the stack call frame, in particular,
54 how to restore registers
56 DW_CFA_... = DWARF2 CFA call frame instruction
57 DW_TAG_... = DWARF2 DIE tag */
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.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 "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.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"
93 #ifdef DWARF2_DEBUGGING_INFO
94 static void dwarf2out_source_line (unsigned int, const char *);
95 #endif
97 #ifndef DWARF2_FRAME_INFO
98 # ifdef DWARF2_DEBUGGING_INFO
99 # define DWARF2_FRAME_INFO \
100 (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
101 # else
102 # define DWARF2_FRAME_INFO 0
103 # endif
104 #endif
106 /* Map register numbers held in the call frame info that gcc has
107 collected using DWARF_FRAME_REGNUM to those that should be output in
108 .debug_frame and .eh_frame. */
109 #ifndef DWARF2_FRAME_REG_OUT
110 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
111 #endif
113 /* Decide whether we want to emit frame unwind information for the current
114 translation unit. */
117 dwarf2out_do_frame (void)
119 /* We want to emit correct CFA location expressions or lists, so we
120 have to return true if we're going to output debug info, even if
121 we're not going to output frame or unwind info. */
122 return (write_symbols == DWARF2_DEBUG
123 || write_symbols == VMS_AND_DWARF2_DEBUG
124 || DWARF2_FRAME_INFO
125 #ifdef DWARF2_UNWIND_INFO
126 || (DWARF2_UNWIND_INFO
127 && (flag_unwind_tables
128 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
129 #endif
133 /* Decide whether to emit frame unwind via assembler directives. */
136 dwarf2out_do_cfi_asm (void)
138 int enc;
140 if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
141 return false;
142 if (!eh_personality_libfunc)
143 return true;
144 if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
145 return false;
147 /* Make sure the personality encoding is one the assembler can support.
148 In particular, aligned addresses can't be handled. */
149 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
150 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
151 return false;
152 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
153 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
154 return false;
156 return true;
159 /* The size of the target's pointer type. */
160 #ifndef PTR_SIZE
161 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
162 #endif
164 /* Array of RTXes referenced by the debugging information, which therefore
165 must be kept around forever. */
166 static GTY(()) VEC(rtx,gc) *used_rtx_array;
168 /* A pointer to the base of a list of incomplete types which might be
169 completed at some later time. incomplete_types_list needs to be a
170 VEC(tree,gc) because we want to tell the garbage collector about
171 it. */
172 static GTY(()) VEC(tree,gc) *incomplete_types;
174 /* A pointer to the base of a table of references to declaration
175 scopes. This table is a display which tracks the nesting
176 of declaration scopes at the current scope and containing
177 scopes. This table is used to find the proper place to
178 define type declaration DIE's. */
179 static GTY(()) VEC(tree,gc) *decl_scope_table;
181 /* Pointers to various DWARF2 sections. */
182 static GTY(()) section *debug_info_section;
183 static GTY(()) section *debug_abbrev_section;
184 static GTY(()) section *debug_aranges_section;
185 static GTY(()) section *debug_macinfo_section;
186 static GTY(()) section *debug_line_section;
187 static GTY(()) section *debug_loc_section;
188 static GTY(()) section *debug_pubnames_section;
189 static GTY(()) section *debug_pubtypes_section;
190 static GTY(()) section *debug_str_section;
191 static GTY(()) section *debug_ranges_section;
192 static GTY(()) section *debug_frame_section;
194 /* How to start an assembler comment. */
195 #ifndef ASM_COMMENT_START
196 #define ASM_COMMENT_START ";#"
197 #endif
199 typedef struct dw_cfi_struct *dw_cfi_ref;
200 typedef struct dw_fde_struct *dw_fde_ref;
201 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
203 /* Call frames are described using a sequence of Call Frame
204 Information instructions. The register number, offset
205 and address fields are provided as possible operands;
206 their use is selected by the opcode field. */
208 enum dw_cfi_oprnd_type {
209 dw_cfi_oprnd_unused,
210 dw_cfi_oprnd_reg_num,
211 dw_cfi_oprnd_offset,
212 dw_cfi_oprnd_addr,
213 dw_cfi_oprnd_loc
216 typedef union dw_cfi_oprnd_struct GTY(())
218 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
219 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
220 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
221 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
223 dw_cfi_oprnd;
225 typedef struct dw_cfi_struct GTY(())
227 dw_cfi_ref dw_cfi_next;
228 enum dwarf_call_frame_info dw_cfi_opc;
229 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
230 dw_cfi_oprnd1;
231 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
232 dw_cfi_oprnd2;
234 dw_cfi_node;
236 /* This is how we define the location of the CFA. We use to handle it
237 as REG + OFFSET all the time, but now it can be more complex.
238 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
239 Instead of passing around REG and OFFSET, we pass a copy
240 of this structure. */
241 typedef struct cfa_loc GTY(())
243 HOST_WIDE_INT offset;
244 HOST_WIDE_INT base_offset;
245 unsigned int reg;
246 int indirect; /* 1 if CFA is accessed via a dereference. */
247 } dw_cfa_location;
249 /* All call frame descriptions (FDE's) in the GCC generated DWARF
250 refer to a single Common Information Entry (CIE), defined at
251 the beginning of the .debug_frame section. This use of a single
252 CIE obviates the need to keep track of multiple CIE's
253 in the DWARF generation routines below. */
255 typedef struct dw_fde_struct GTY(())
257 tree decl;
258 const char *dw_fde_begin;
259 const char *dw_fde_current_label;
260 const char *dw_fde_end;
261 const char *dw_fde_hot_section_label;
262 const char *dw_fde_hot_section_end_label;
263 const char *dw_fde_unlikely_section_label;
264 const char *dw_fde_unlikely_section_end_label;
265 bool dw_fde_switched_sections;
266 dw_cfi_ref dw_fde_cfi;
267 unsigned funcdef_number;
268 HOST_WIDE_INT stack_realignment;
269 /* Dynamic realign argument pointer register. */
270 unsigned int drap_reg;
271 /* Virtual dynamic realign argument pointer register. */
272 unsigned int vdrap_reg;
273 unsigned all_throwers_are_sibcalls : 1;
274 unsigned nothrow : 1;
275 unsigned uses_eh_lsda : 1;
276 /* Whether we did stack realign in this call frame. */
277 unsigned stack_realign : 1;
278 /* Whether dynamic realign argument pointer register has been saved. */
279 unsigned drap_reg_saved: 1;
281 dw_fde_node;
283 /* Maximum size (in bytes) of an artificially generated label. */
284 #define MAX_ARTIFICIAL_LABEL_BYTES 30
286 /* The size of addresses as they appear in the Dwarf 2 data.
287 Some architectures use word addresses to refer to code locations,
288 but Dwarf 2 info always uses byte addresses. On such machines,
289 Dwarf 2 addresses need to be larger than the architecture's
290 pointers. */
291 #ifndef DWARF2_ADDR_SIZE
292 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
293 #endif
295 /* The size in bytes of a DWARF field indicating an offset or length
296 relative to a debug info section, specified to be 4 bytes in the
297 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
298 as PTR_SIZE. */
300 #ifndef DWARF_OFFSET_SIZE
301 #define DWARF_OFFSET_SIZE 4
302 #endif
304 /* According to the (draft) DWARF 3 specification, the initial length
305 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
306 bytes are 0xffffffff, followed by the length stored in the next 8
307 bytes.
309 However, the SGI/MIPS ABI uses an initial length which is equal to
310 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
312 #ifndef DWARF_INITIAL_LENGTH_SIZE
313 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
314 #endif
316 #define DWARF_VERSION 2
318 /* Round SIZE up to the nearest BOUNDARY. */
319 #define DWARF_ROUND(SIZE,BOUNDARY) \
320 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
322 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
323 #ifndef DWARF_CIE_DATA_ALIGNMENT
324 #ifdef STACK_GROWS_DOWNWARD
325 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
326 #else
327 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
328 #endif
329 #endif
331 /* CIE identifier. */
332 #if HOST_BITS_PER_WIDE_INT >= 64
333 #define DWARF_CIE_ID \
334 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
335 #else
336 #define DWARF_CIE_ID DW_CIE_ID
337 #endif
339 /* A pointer to the base of a table that contains frame description
340 information for each routine. */
341 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
343 /* Number of elements currently allocated for fde_table. */
344 static GTY(()) unsigned fde_table_allocated;
346 /* Number of elements in fde_table currently in use. */
347 static GTY(()) unsigned fde_table_in_use;
349 /* Size (in elements) of increments by which we may expand the
350 fde_table. */
351 #define FDE_TABLE_INCREMENT 256
353 /* Get the current fde_table entry we should use. */
355 static inline dw_fde_ref
356 current_fde (void)
358 return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
361 /* A list of call frame insns for the CIE. */
362 static GTY(()) dw_cfi_ref cie_cfi_head;
364 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
365 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
366 attribute that accelerates the lookup of the FDE associated
367 with the subprogram. This variable holds the table index of the FDE
368 associated with the current function (body) definition. */
369 static unsigned current_funcdef_fde;
370 #endif
372 struct indirect_string_node GTY(())
374 const char *str;
375 unsigned int refcount;
376 unsigned int form;
377 char *label;
380 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
382 static GTY(()) int dw2_string_counter;
383 static GTY(()) unsigned long dwarf2out_cfi_label_num;
385 /* True if the compilation unit places functions in more than one section. */
386 static GTY(()) bool have_multiple_function_sections = false;
388 /* Whether the default text and cold text sections have been used at all. */
390 static GTY(()) bool text_section_used = false;
391 static GTY(()) bool cold_text_section_used = false;
393 /* The default cold text section. */
394 static GTY(()) section *cold_text_section;
396 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
398 /* Forward declarations for functions defined in this file. */
400 static char *stripattributes (const char *);
401 static const char *dwarf_cfi_name (unsigned);
402 static dw_cfi_ref new_cfi (void);
403 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
404 static void add_fde_cfi (const char *, dw_cfi_ref);
405 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
406 static void lookup_cfa (dw_cfa_location *);
407 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
408 #ifdef DWARF2_UNWIND_INFO
409 static void initial_return_save (rtx);
410 #endif
411 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
412 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
413 static void output_cfi_directive (dw_cfi_ref);
414 static void output_call_frame_info (int);
415 static void dwarf2out_note_section_used (void);
416 static void dwarf2out_stack_adjust (rtx, bool);
417 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
418 static void flush_queued_reg_saves (void);
419 static bool clobbers_queued_reg_save (const_rtx);
420 static void dwarf2out_frame_debug_expr (rtx, const char *);
422 /* Support for complex CFA locations. */
423 static void output_cfa_loc (dw_cfi_ref);
424 static void output_cfa_loc_raw (dw_cfi_ref);
425 static void get_cfa_from_loc_descr (dw_cfa_location *,
426 struct dw_loc_descr_struct *);
427 static struct dw_loc_descr_struct *build_cfa_loc
428 (dw_cfa_location *, HOST_WIDE_INT);
429 static struct dw_loc_descr_struct *build_cfa_aligned_loc
430 (HOST_WIDE_INT, HOST_WIDE_INT);
431 static void def_cfa_1 (const char *, dw_cfa_location *);
433 /* How to start an assembler comment. */
434 #ifndef ASM_COMMENT_START
435 #define ASM_COMMENT_START ";#"
436 #endif
438 /* Data and reference forms for relocatable data. */
439 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
440 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
442 #ifndef DEBUG_FRAME_SECTION
443 #define DEBUG_FRAME_SECTION ".debug_frame"
444 #endif
446 #ifndef FUNC_BEGIN_LABEL
447 #define FUNC_BEGIN_LABEL "LFB"
448 #endif
450 #ifndef FUNC_END_LABEL
451 #define FUNC_END_LABEL "LFE"
452 #endif
454 #ifndef FRAME_BEGIN_LABEL
455 #define FRAME_BEGIN_LABEL "Lframe"
456 #endif
457 #define CIE_AFTER_SIZE_LABEL "LSCIE"
458 #define CIE_END_LABEL "LECIE"
459 #define FDE_LABEL "LSFDE"
460 #define FDE_AFTER_SIZE_LABEL "LASFDE"
461 #define FDE_END_LABEL "LEFDE"
462 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
463 #define LINE_NUMBER_END_LABEL "LELT"
464 #define LN_PROLOG_AS_LABEL "LASLTP"
465 #define LN_PROLOG_END_LABEL "LELTP"
466 #define DIE_LABEL_PREFIX "DW"
468 /* The DWARF 2 CFA column which tracks the return address. Normally this
469 is the column for PC, or the first column after all of the hard
470 registers. */
471 #ifndef DWARF_FRAME_RETURN_COLUMN
472 #ifdef PC_REGNUM
473 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
474 #else
475 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
476 #endif
477 #endif
479 /* The mapping from gcc register number to DWARF 2 CFA column number. By
480 default, we just provide columns for all registers. */
481 #ifndef DWARF_FRAME_REGNUM
482 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
483 #endif
485 /* Hook used by __throw. */
488 expand_builtin_dwarf_sp_column (void)
490 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
491 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
494 /* Return a pointer to a copy of the section string name S with all
495 attributes stripped off, and an asterisk prepended (for assemble_name). */
497 static inline char *
498 stripattributes (const char *s)
500 char *stripped = XNEWVEC (char, strlen (s) + 2);
501 char *p = stripped;
503 *p++ = '*';
505 while (*s && *s != ',')
506 *p++ = *s++;
508 *p = '\0';
509 return stripped;
512 /* MEM is a memory reference for the register size table, each element of
513 which has mode MODE. Initialize column C as a return address column. */
515 static void
516 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
518 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
519 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
520 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
523 /* Generate code to initialize the register size table. */
525 void
526 expand_builtin_init_dwarf_reg_sizes (tree address)
528 unsigned int i;
529 enum machine_mode mode = TYPE_MODE (char_type_node);
530 rtx addr = expand_normal (address);
531 rtx mem = gen_rtx_MEM (BLKmode, addr);
532 bool wrote_return_column = false;
534 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
536 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
538 if (rnum < DWARF_FRAME_REGISTERS)
540 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
541 enum machine_mode save_mode = reg_raw_mode[i];
542 HOST_WIDE_INT size;
544 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
545 save_mode = choose_hard_reg_mode (i, 1, true);
546 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
548 if (save_mode == VOIDmode)
549 continue;
550 wrote_return_column = true;
552 size = GET_MODE_SIZE (save_mode);
553 if (offset < 0)
554 continue;
556 emit_move_insn (adjust_address (mem, mode, offset),
557 gen_int_mode (size, mode));
561 if (!wrote_return_column)
562 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
564 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
565 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
566 #endif
568 targetm.init_dwarf_reg_sizes_extra (address);
571 /* Convert a DWARF call frame info. operation to its string name */
573 static const char *
574 dwarf_cfi_name (unsigned int cfi_opc)
576 switch (cfi_opc)
578 case DW_CFA_advance_loc:
579 return "DW_CFA_advance_loc";
580 case DW_CFA_offset:
581 return "DW_CFA_offset";
582 case DW_CFA_restore:
583 return "DW_CFA_restore";
584 case DW_CFA_nop:
585 return "DW_CFA_nop";
586 case DW_CFA_set_loc:
587 return "DW_CFA_set_loc";
588 case DW_CFA_advance_loc1:
589 return "DW_CFA_advance_loc1";
590 case DW_CFA_advance_loc2:
591 return "DW_CFA_advance_loc2";
592 case DW_CFA_advance_loc4:
593 return "DW_CFA_advance_loc4";
594 case DW_CFA_offset_extended:
595 return "DW_CFA_offset_extended";
596 case DW_CFA_restore_extended:
597 return "DW_CFA_restore_extended";
598 case DW_CFA_undefined:
599 return "DW_CFA_undefined";
600 case DW_CFA_same_value:
601 return "DW_CFA_same_value";
602 case DW_CFA_register:
603 return "DW_CFA_register";
604 case DW_CFA_remember_state:
605 return "DW_CFA_remember_state";
606 case DW_CFA_restore_state:
607 return "DW_CFA_restore_state";
608 case DW_CFA_def_cfa:
609 return "DW_CFA_def_cfa";
610 case DW_CFA_def_cfa_register:
611 return "DW_CFA_def_cfa_register";
612 case DW_CFA_def_cfa_offset:
613 return "DW_CFA_def_cfa_offset";
615 /* DWARF 3 */
616 case DW_CFA_def_cfa_expression:
617 return "DW_CFA_def_cfa_expression";
618 case DW_CFA_expression:
619 return "DW_CFA_expression";
620 case DW_CFA_offset_extended_sf:
621 return "DW_CFA_offset_extended_sf";
622 case DW_CFA_def_cfa_sf:
623 return "DW_CFA_def_cfa_sf";
624 case DW_CFA_def_cfa_offset_sf:
625 return "DW_CFA_def_cfa_offset_sf";
627 /* SGI/MIPS specific */
628 case DW_CFA_MIPS_advance_loc8:
629 return "DW_CFA_MIPS_advance_loc8";
631 /* GNU extensions */
632 case DW_CFA_GNU_window_save:
633 return "DW_CFA_GNU_window_save";
634 case DW_CFA_GNU_args_size:
635 return "DW_CFA_GNU_args_size";
636 case DW_CFA_GNU_negative_offset_extended:
637 return "DW_CFA_GNU_negative_offset_extended";
639 default:
640 return "DW_CFA_<unknown>";
644 /* Return a pointer to a newly allocated Call Frame Instruction. */
646 static inline dw_cfi_ref
647 new_cfi (void)
649 dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
651 cfi->dw_cfi_next = NULL;
652 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
653 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
655 return cfi;
658 /* Add a Call Frame Instruction to list of instructions. */
660 static inline void
661 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
663 dw_cfi_ref *p;
664 dw_fde_ref fde = current_fde ();
666 /* When DRAP is used, CFA is defined with an expression. Redefine
667 CFA may lead to a different CFA value. */
668 if (fde && fde->drap_reg != INVALID_REGNUM)
669 switch (cfi->dw_cfi_opc)
671 case DW_CFA_def_cfa_register:
672 case DW_CFA_def_cfa_offset:
673 case DW_CFA_def_cfa_offset_sf:
674 case DW_CFA_def_cfa:
675 case DW_CFA_def_cfa_sf:
676 gcc_unreachable ();
678 default:
679 break;
682 /* Find the end of the chain. */
683 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
686 *p = cfi;
689 /* Generate a new label for the CFI info to refer to. */
691 char *
692 dwarf2out_cfi_label (void)
694 static char label[20];
696 if (dwarf2out_do_cfi_asm ())
698 /* In this case, we will be emitting the asm directive instead of
699 the label, so just return a placeholder to keep the rest of the
700 interfaces happy. */
701 strcpy (label, "<do not output>");
703 else
705 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
706 ASM_OUTPUT_LABEL (asm_out_file, label);
709 return label;
712 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
713 or to the CIE if LABEL is NULL. */
715 static void
716 add_fde_cfi (const char *label, dw_cfi_ref cfi)
718 dw_cfi_ref *list_head = &cie_cfi_head;
720 if (dwarf2out_do_cfi_asm ())
722 if (label)
724 output_cfi_directive (cfi);
726 /* We still have to add the cfi to the list so that
727 lookup_cfa works later on. */
728 list_head = &current_fde ()->dw_fde_cfi;
730 /* ??? If this is a CFI for the CIE, we don't emit. This
731 assumes that the standard CIE contents that the assembler
732 uses matches the standard CIE contents that the compiler
733 uses. This is probably a bad assumption. I'm not quite
734 sure how to address this for now. */
736 else if (label)
738 dw_fde_ref fde = current_fde ();
740 gcc_assert (fde != NULL);
742 if (*label == 0)
743 label = dwarf2out_cfi_label ();
745 if (fde->dw_fde_current_label == NULL
746 || strcmp (label, fde->dw_fde_current_label) != 0)
748 dw_cfi_ref xcfi;
750 label = xstrdup (label);
752 /* Set the location counter to the new label. */
753 xcfi = new_cfi ();
754 /* If we have a current label, advance from there, otherwise
755 set the location directly using set_loc. */
756 xcfi->dw_cfi_opc = fde->dw_fde_current_label
757 ? DW_CFA_advance_loc4
758 : DW_CFA_set_loc;
759 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
760 add_cfi (&fde->dw_fde_cfi, xcfi);
762 fde->dw_fde_current_label = label;
765 list_head = &fde->dw_fde_cfi;
768 add_cfi (list_head, cfi);
771 /* Subroutine of lookup_cfa. */
773 static void
774 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
776 switch (cfi->dw_cfi_opc)
778 case DW_CFA_def_cfa_offset:
779 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
780 break;
781 case DW_CFA_def_cfa_offset_sf:
782 loc->offset
783 = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
784 break;
785 case DW_CFA_def_cfa_register:
786 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
787 break;
788 case DW_CFA_def_cfa:
789 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
790 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
791 break;
792 case DW_CFA_def_cfa_sf:
793 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
794 loc->offset
795 = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
796 break;
797 case DW_CFA_def_cfa_expression:
798 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
799 break;
800 default:
801 break;
805 /* Find the previous value for the CFA. */
807 static void
808 lookup_cfa (dw_cfa_location *loc)
810 dw_cfi_ref cfi;
811 dw_fde_ref fde;
813 loc->reg = INVALID_REGNUM;
814 loc->offset = 0;
815 loc->indirect = 0;
816 loc->base_offset = 0;
818 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
819 lookup_cfa_1 (cfi, loc);
821 fde = current_fde ();
822 if (fde)
823 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
824 lookup_cfa_1 (cfi, loc);
827 /* The current rule for calculating the DWARF2 canonical frame address. */
828 static dw_cfa_location cfa;
830 /* The register used for saving registers to the stack, and its offset
831 from the CFA. */
832 static dw_cfa_location cfa_store;
834 /* The running total of the size of arguments pushed onto the stack. */
835 static HOST_WIDE_INT args_size;
837 /* The last args_size we actually output. */
838 static HOST_WIDE_INT old_args_size;
840 /* Entry point to update the canonical frame address (CFA).
841 LABEL is passed to add_fde_cfi. The value of CFA is now to be
842 calculated from REG+OFFSET. */
844 void
845 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
847 dw_cfa_location loc;
848 loc.indirect = 0;
849 loc.base_offset = 0;
850 loc.reg = reg;
851 loc.offset = offset;
852 def_cfa_1 (label, &loc);
855 /* Determine if two dw_cfa_location structures define the same data. */
857 static bool
858 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
860 return (loc1->reg == loc2->reg
861 && loc1->offset == loc2->offset
862 && loc1->indirect == loc2->indirect
863 && (loc1->indirect == 0
864 || loc1->base_offset == loc2->base_offset));
867 /* This routine does the actual work. The CFA is now calculated from
868 the dw_cfa_location structure. */
870 static void
871 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
873 dw_cfi_ref cfi;
874 dw_cfa_location old_cfa, loc;
876 cfa = *loc_p;
877 loc = *loc_p;
879 if (cfa_store.reg == loc.reg && loc.indirect == 0)
880 cfa_store.offset = loc.offset;
882 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
883 lookup_cfa (&old_cfa);
885 /* If nothing changed, no need to issue any call frame instructions. */
886 if (cfa_equal_p (&loc, &old_cfa))
887 return;
889 cfi = new_cfi ();
891 if (loc.reg == old_cfa.reg && !loc.indirect)
893 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
894 the CFA register did not change but the offset did. */
895 if (loc.offset < 0)
897 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
898 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
900 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
901 cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
903 else
905 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
906 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
910 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
911 else if (loc.offset == old_cfa.offset
912 && old_cfa.reg != INVALID_REGNUM
913 && !loc.indirect)
915 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
916 indicating the CFA register has changed to <register> but the
917 offset has not changed. */
918 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
919 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
921 #endif
923 else if (loc.indirect == 0)
925 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
926 indicating the CFA register has changed to <register> with
927 the specified offset. */
928 if (loc.offset < 0)
930 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
931 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
933 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
934 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
935 cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
937 else
939 cfi->dw_cfi_opc = DW_CFA_def_cfa;
940 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
941 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
944 else
946 /* Construct a DW_CFA_def_cfa_expression instruction to
947 calculate the CFA using a full location expression since no
948 register-offset pair is available. */
949 struct dw_loc_descr_struct *loc_list;
951 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
952 loc_list = build_cfa_loc (&loc, 0);
953 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
956 add_fde_cfi (label, cfi);
959 /* Add the CFI for saving a register. REG is the CFA column number.
960 LABEL is passed to add_fde_cfi.
961 If SREG is -1, the register is saved at OFFSET from the CFA;
962 otherwise it is saved in SREG. */
964 static void
965 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
967 dw_cfi_ref cfi = new_cfi ();
968 dw_fde_ref fde = current_fde ();
970 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
972 /* When stack is aligned, store REG using DW_CFA_expression with
973 FP. */
974 if (fde
975 && fde->stack_realign
976 && sreg == INVALID_REGNUM)
978 cfi->dw_cfi_opc = DW_CFA_expression;
979 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
980 cfi->dw_cfi_oprnd1.dw_cfi_loc
981 = build_cfa_aligned_loc (offset, fde->stack_realignment);
983 else if (sreg == INVALID_REGNUM)
985 if (reg & ~0x3f)
986 /* The register number won't fit in 6 bits, so we have to use
987 the long form. */
988 cfi->dw_cfi_opc = DW_CFA_offset_extended;
989 else
990 cfi->dw_cfi_opc = DW_CFA_offset;
992 #ifdef ENABLE_CHECKING
994 /* If we get an offset that is not a multiple of
995 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
996 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
997 description. */
998 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
1000 gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
1002 #endif
1003 offset /= DWARF_CIE_DATA_ALIGNMENT;
1004 if (offset < 0)
1005 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1007 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1009 else if (sreg == reg)
1010 cfi->dw_cfi_opc = DW_CFA_same_value;
1011 else
1013 cfi->dw_cfi_opc = DW_CFA_register;
1014 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1017 add_fde_cfi (label, cfi);
1020 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
1021 This CFI tells the unwinder that it needs to restore the window registers
1022 from the previous frame's window save area.
1024 ??? Perhaps we should note in the CIE where windows are saved (instead of
1025 assuming 0(cfa)) and what registers are in the window. */
1027 void
1028 dwarf2out_window_save (const char *label)
1030 dw_cfi_ref cfi = new_cfi ();
1032 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1033 add_fde_cfi (label, cfi);
1036 /* Add a CFI to update the running total of the size of arguments
1037 pushed onto the stack. */
1039 void
1040 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1042 dw_cfi_ref cfi;
1044 if (size == old_args_size)
1045 return;
1047 old_args_size = size;
1049 cfi = new_cfi ();
1050 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1051 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1052 add_fde_cfi (label, cfi);
1055 /* Entry point for saving a register to the stack. REG is the GCC register
1056 number. LABEL and OFFSET are passed to reg_save. */
1058 void
1059 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1061 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1064 /* Entry point for saving the return address in the stack.
1065 LABEL and OFFSET are passed to reg_save. */
1067 void
1068 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1070 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1073 /* Entry point for saving the return address in a register.
1074 LABEL and SREG are passed to reg_save. */
1076 void
1077 dwarf2out_return_reg (const char *label, unsigned int sreg)
1079 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1082 #ifdef DWARF2_UNWIND_INFO
1083 /* Record the initial position of the return address. RTL is
1084 INCOMING_RETURN_ADDR_RTX. */
1086 static void
1087 initial_return_save (rtx rtl)
1089 unsigned int reg = INVALID_REGNUM;
1090 HOST_WIDE_INT offset = 0;
1092 switch (GET_CODE (rtl))
1094 case REG:
1095 /* RA is in a register. */
1096 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1097 break;
1099 case MEM:
1100 /* RA is on the stack. */
1101 rtl = XEXP (rtl, 0);
1102 switch (GET_CODE (rtl))
1104 case REG:
1105 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1106 offset = 0;
1107 break;
1109 case PLUS:
1110 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1111 offset = INTVAL (XEXP (rtl, 1));
1112 break;
1114 case MINUS:
1115 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1116 offset = -INTVAL (XEXP (rtl, 1));
1117 break;
1119 default:
1120 gcc_unreachable ();
1123 break;
1125 case PLUS:
1126 /* The return address is at some offset from any value we can
1127 actually load. For instance, on the SPARC it is in %i7+8. Just
1128 ignore the offset for now; it doesn't matter for unwinding frames. */
1129 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1130 initial_return_save (XEXP (rtl, 0));
1131 return;
1133 default:
1134 gcc_unreachable ();
1137 if (reg != DWARF_FRAME_RETURN_COLUMN)
1138 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1140 #endif
1142 /* Given a SET, calculate the amount of stack adjustment it
1143 contains. */
1145 static HOST_WIDE_INT
1146 stack_adjust_offset (const_rtx pattern)
1148 const_rtx src = SET_SRC (pattern);
1149 const_rtx dest = SET_DEST (pattern);
1150 HOST_WIDE_INT offset = 0;
1151 enum rtx_code code;
1153 if (dest == stack_pointer_rtx)
1155 /* (set (reg sp) (plus (reg sp) (const_int))) */
1156 code = GET_CODE (src);
1157 if (! (code == PLUS || code == MINUS)
1158 || XEXP (src, 0) != stack_pointer_rtx
1159 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1160 return 0;
1162 offset = INTVAL (XEXP (src, 1));
1163 if (code == PLUS)
1164 offset = -offset;
1166 else if (MEM_P (dest))
1168 /* (set (mem (pre_dec (reg sp))) (foo)) */
1169 src = XEXP (dest, 0);
1170 code = GET_CODE (src);
1172 switch (code)
1174 case PRE_MODIFY:
1175 case POST_MODIFY:
1176 if (XEXP (src, 0) == stack_pointer_rtx)
1178 rtx val = XEXP (XEXP (src, 1), 1);
1179 /* We handle only adjustments by constant amount. */
1180 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1181 && GET_CODE (val) == CONST_INT);
1182 offset = -INTVAL (val);
1183 break;
1185 return 0;
1187 case PRE_DEC:
1188 case POST_DEC:
1189 if (XEXP (src, 0) == stack_pointer_rtx)
1191 offset = GET_MODE_SIZE (GET_MODE (dest));
1192 break;
1194 return 0;
1196 case PRE_INC:
1197 case POST_INC:
1198 if (XEXP (src, 0) == stack_pointer_rtx)
1200 offset = -GET_MODE_SIZE (GET_MODE (dest));
1201 break;
1203 return 0;
1205 default:
1206 return 0;
1209 else
1210 return 0;
1212 return offset;
1215 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1216 indexed by INSN_UID. */
1218 static HOST_WIDE_INT *barrier_args_size;
1220 /* Helper function for compute_barrier_args_size. Handle one insn. */
1222 static HOST_WIDE_INT
1223 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1224 VEC (rtx, heap) **next)
1226 HOST_WIDE_INT offset = 0;
1227 int i;
1229 if (! RTX_FRAME_RELATED_P (insn))
1231 if (prologue_epilogue_contains (insn)
1232 || sibcall_epilogue_contains (insn))
1233 /* Nothing */;
1234 else if (GET_CODE (PATTERN (insn)) == SET)
1235 offset = stack_adjust_offset (PATTERN (insn));
1236 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1237 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1239 /* There may be stack adjustments inside compound insns. Search
1240 for them. */
1241 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1242 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1243 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1246 else
1248 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1250 if (expr)
1252 expr = XEXP (expr, 0);
1253 if (GET_CODE (expr) == PARALLEL
1254 || GET_CODE (expr) == SEQUENCE)
1255 for (i = 1; i < XVECLEN (expr, 0); i++)
1257 rtx elem = XVECEXP (expr, 0, i);
1259 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1260 offset += stack_adjust_offset (elem);
1265 #ifndef STACK_GROWS_DOWNWARD
1266 offset = -offset;
1267 #endif
1269 cur_args_size += offset;
1270 if (cur_args_size < 0)
1271 cur_args_size = 0;
1273 if (JUMP_P (insn))
1275 rtx dest = JUMP_LABEL (insn);
1277 if (dest)
1279 if (barrier_args_size [INSN_UID (dest)] < 0)
1281 barrier_args_size [INSN_UID (dest)] = cur_args_size;
1282 VEC_safe_push (rtx, heap, *next, dest);
1287 return cur_args_size;
1290 /* Walk the whole function and compute args_size on BARRIERs. */
1292 static void
1293 compute_barrier_args_size (void)
1295 int max_uid = get_max_uid (), i;
1296 rtx insn;
1297 VEC (rtx, heap) *worklist, *next, *tmp;
1299 barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1300 for (i = 0; i < max_uid; i++)
1301 barrier_args_size[i] = -1;
1303 worklist = VEC_alloc (rtx, heap, 20);
1304 next = VEC_alloc (rtx, heap, 20);
1305 insn = get_insns ();
1306 barrier_args_size[INSN_UID (insn)] = 0;
1307 VEC_quick_push (rtx, worklist, insn);
1308 for (;;)
1310 while (!VEC_empty (rtx, worklist))
1312 rtx prev, body, first_insn;
1313 HOST_WIDE_INT cur_args_size;
1315 first_insn = insn = VEC_pop (rtx, worklist);
1316 cur_args_size = barrier_args_size[INSN_UID (insn)];
1317 prev = prev_nonnote_insn (insn);
1318 if (prev && BARRIER_P (prev))
1319 barrier_args_size[INSN_UID (prev)] = cur_args_size;
1321 for (; insn; insn = NEXT_INSN (insn))
1323 if (INSN_DELETED_P (insn) || NOTE_P (insn))
1324 continue;
1325 if (BARRIER_P (insn))
1326 break;
1328 if (LABEL_P (insn))
1330 if (insn == first_insn)
1331 continue;
1332 else if (barrier_args_size[INSN_UID (insn)] < 0)
1334 barrier_args_size[INSN_UID (insn)] = cur_args_size;
1335 continue;
1337 else
1339 /* The insns starting with this label have been
1340 already scanned or are in the worklist. */
1341 break;
1345 body = PATTERN (insn);
1346 if (GET_CODE (body) == SEQUENCE)
1348 for (i = 1; i < XVECLEN (body, 0); i++)
1349 cur_args_size
1350 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1351 cur_args_size, &next);
1352 cur_args_size
1353 = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1354 cur_args_size, &next);
1356 else
1357 cur_args_size
1358 = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1362 if (VEC_empty (rtx, next))
1363 break;
1365 /* Swap WORKLIST with NEXT and truncate NEXT for next iteration. */
1366 tmp = next;
1367 next = worklist;
1368 worklist = tmp;
1369 VEC_truncate (rtx, next, 0);
1372 VEC_free (rtx, heap, worklist);
1373 VEC_free (rtx, heap, next);
1377 /* Check INSN to see if it looks like a push or a stack adjustment, and
1378 make a note of it if it does. EH uses this information to find out how
1379 much extra space it needs to pop off the stack. */
1381 static void
1382 dwarf2out_stack_adjust (rtx insn, bool after_p)
1384 HOST_WIDE_INT offset;
1385 const char *label;
1386 int i;
1388 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1389 with this function. Proper support would require all frame-related
1390 insns to be marked, and to be able to handle saving state around
1391 epilogues textually in the middle of the function. */
1392 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1393 return;
1395 /* If only calls can throw, and we have a frame pointer,
1396 save up adjustments until we see the CALL_INSN. */
1397 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1399 if (CALL_P (insn) && !after_p)
1401 /* Extract the size of the args from the CALL rtx itself. */
1402 insn = PATTERN (insn);
1403 if (GET_CODE (insn) == PARALLEL)
1404 insn = XVECEXP (insn, 0, 0);
1405 if (GET_CODE (insn) == SET)
1406 insn = SET_SRC (insn);
1407 gcc_assert (GET_CODE (insn) == CALL);
1408 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1410 return;
1413 if (CALL_P (insn) && !after_p)
1415 if (!flag_asynchronous_unwind_tables)
1416 dwarf2out_args_size ("", args_size);
1417 return;
1419 else if (BARRIER_P (insn))
1421 /* Don't call compute_barrier_args_size () if the only
1422 BARRIER is at the end of function. */
1423 if (barrier_args_size == NULL && next_nonnote_insn (insn))
1424 compute_barrier_args_size ();
1425 if (barrier_args_size == NULL)
1426 offset = 0;
1427 else
1429 offset = barrier_args_size[INSN_UID (insn)];
1430 if (offset < 0)
1431 offset = 0;
1434 offset -= args_size;
1435 #ifndef STACK_GROWS_DOWNWARD
1436 offset = -offset;
1437 #endif
1439 else if (GET_CODE (PATTERN (insn)) == SET)
1440 offset = stack_adjust_offset (PATTERN (insn));
1441 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1442 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1444 /* There may be stack adjustments inside compound insns. Search
1445 for them. */
1446 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1447 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1448 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1450 else
1451 return;
1453 if (offset == 0)
1454 return;
1456 label = dwarf2out_cfi_label ();
1457 dwarf2out_args_size_adjust (offset, label);
1460 /* Adjust args_size based on stack adjustment OFFSET. */
1462 static void
1463 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1465 if (cfa.reg == STACK_POINTER_REGNUM)
1466 cfa.offset += offset;
1468 if (cfa_store.reg == STACK_POINTER_REGNUM)
1469 cfa_store.offset += offset;
1471 #ifndef STACK_GROWS_DOWNWARD
1472 offset = -offset;
1473 #endif
1475 args_size += offset;
1476 if (args_size < 0)
1477 args_size = 0;
1479 def_cfa_1 (label, &cfa);
1480 if (flag_asynchronous_unwind_tables)
1481 dwarf2out_args_size (label, args_size);
1484 #endif
1486 /* We delay emitting a register save until either (a) we reach the end
1487 of the prologue or (b) the register is clobbered. This clusters
1488 register saves so that there are fewer pc advances. */
1490 struct queued_reg_save GTY(())
1492 struct queued_reg_save *next;
1493 rtx reg;
1494 HOST_WIDE_INT cfa_offset;
1495 rtx saved_reg;
1498 static GTY(()) struct queued_reg_save *queued_reg_saves;
1500 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1501 struct reg_saved_in_data GTY(()) {
1502 rtx orig_reg;
1503 rtx saved_in_reg;
1506 /* A list of registers saved in other registers.
1507 The list intentionally has a small maximum capacity of 4; if your
1508 port needs more than that, you might consider implementing a
1509 more efficient data structure. */
1510 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1511 static GTY(()) size_t num_regs_saved_in_regs;
1513 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1514 static const char *last_reg_save_label;
1516 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1517 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1519 static void
1520 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1522 struct queued_reg_save *q;
1524 /* Duplicates waste space, but it's also necessary to remove them
1525 for correctness, since the queue gets output in reverse
1526 order. */
1527 for (q = queued_reg_saves; q != NULL; q = q->next)
1528 if (REGNO (q->reg) == REGNO (reg))
1529 break;
1531 if (q == NULL)
1533 q = GGC_NEW (struct queued_reg_save);
1534 q->next = queued_reg_saves;
1535 queued_reg_saves = q;
1538 q->reg = reg;
1539 q->cfa_offset = offset;
1540 q->saved_reg = sreg;
1542 last_reg_save_label = label;
1545 /* Output all the entries in QUEUED_REG_SAVES. */
1547 static void
1548 flush_queued_reg_saves (void)
1550 struct queued_reg_save *q;
1552 for (q = queued_reg_saves; q; q = q->next)
1554 size_t i;
1555 unsigned int reg, sreg;
1557 for (i = 0; i < num_regs_saved_in_regs; i++)
1558 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1559 break;
1560 if (q->saved_reg && i == num_regs_saved_in_regs)
1562 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1563 num_regs_saved_in_regs++;
1565 if (i != num_regs_saved_in_regs)
1567 regs_saved_in_regs[i].orig_reg = q->reg;
1568 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1571 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1572 if (q->saved_reg)
1573 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1574 else
1575 sreg = INVALID_REGNUM;
1576 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1579 queued_reg_saves = NULL;
1580 last_reg_save_label = NULL;
1583 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1584 location for? Or, does it clobber a register which we've previously
1585 said that some other register is saved in, and for which we now
1586 have a new location for? */
1588 static bool
1589 clobbers_queued_reg_save (const_rtx insn)
1591 struct queued_reg_save *q;
1593 for (q = queued_reg_saves; q; q = q->next)
1595 size_t i;
1596 if (modified_in_p (q->reg, insn))
1597 return true;
1598 for (i = 0; i < num_regs_saved_in_regs; i++)
1599 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1600 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1601 return true;
1604 return false;
1607 /* Entry point for saving the first register into the second. */
1609 void
1610 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1612 size_t i;
1613 unsigned int regno, sregno;
1615 for (i = 0; i < num_regs_saved_in_regs; i++)
1616 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1617 break;
1618 if (i == num_regs_saved_in_regs)
1620 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1621 num_regs_saved_in_regs++;
1623 regs_saved_in_regs[i].orig_reg = reg;
1624 regs_saved_in_regs[i].saved_in_reg = sreg;
1626 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1627 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1628 reg_save (label, regno, sregno, 0);
1631 /* What register, if any, is currently saved in REG? */
1633 static rtx
1634 reg_saved_in (rtx reg)
1636 unsigned int regn = REGNO (reg);
1637 size_t i;
1638 struct queued_reg_save *q;
1640 for (q = queued_reg_saves; q; q = q->next)
1641 if (q->saved_reg && regn == REGNO (q->saved_reg))
1642 return q->reg;
1644 for (i = 0; i < num_regs_saved_in_regs; i++)
1645 if (regs_saved_in_regs[i].saved_in_reg
1646 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1647 return regs_saved_in_regs[i].orig_reg;
1649 return NULL_RTX;
1653 /* A temporary register holding an integral value used in adjusting SP
1654 or setting up the store_reg. The "offset" field holds the integer
1655 value, not an offset. */
1656 static dw_cfa_location cfa_temp;
1658 /* Record call frame debugging information for an expression EXPR,
1659 which either sets SP or FP (adjusting how we calculate the frame
1660 address) or saves a register to the stack or another register.
1661 LABEL indicates the address of EXPR.
1663 This function encodes a state machine mapping rtxes to actions on
1664 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1665 users need not read the source code.
1667 The High-Level Picture
1669 Changes in the register we use to calculate the CFA: Currently we
1670 assume that if you copy the CFA register into another register, we
1671 should take the other one as the new CFA register; this seems to
1672 work pretty well. If it's wrong for some target, it's simple
1673 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1675 Changes in the register we use for saving registers to the stack:
1676 This is usually SP, but not always. Again, we deduce that if you
1677 copy SP into another register (and SP is not the CFA register),
1678 then the new register is the one we will be using for register
1679 saves. This also seems to work.
1681 Register saves: There's not much guesswork about this one; if
1682 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1683 register save, and the register used to calculate the destination
1684 had better be the one we think we're using for this purpose.
1685 It's also assumed that a copy from a call-saved register to another
1686 register is saving that register if RTX_FRAME_RELATED_P is set on
1687 that instruction. If the copy is from a call-saved register to
1688 the *same* register, that means that the register is now the same
1689 value as in the caller.
1691 Except: If the register being saved is the CFA register, and the
1692 offset is nonzero, we are saving the CFA, so we assume we have to
1693 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1694 the intent is to save the value of SP from the previous frame.
1696 In addition, if a register has previously been saved to a different
1697 register,
1699 Invariants / Summaries of Rules
1701 cfa current rule for calculating the CFA. It usually
1702 consists of a register and an offset.
1703 cfa_store register used by prologue code to save things to the stack
1704 cfa_store.offset is the offset from the value of
1705 cfa_store.reg to the actual CFA
1706 cfa_temp register holding an integral value. cfa_temp.offset
1707 stores the value, which will be used to adjust the
1708 stack pointer. cfa_temp is also used like cfa_store,
1709 to track stores to the stack via fp or a temp reg.
1711 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1712 with cfa.reg as the first operand changes the cfa.reg and its
1713 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1714 cfa_temp.offset.
1716 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1717 expression yielding a constant. This sets cfa_temp.reg
1718 and cfa_temp.offset.
1720 Rule 5: Create a new register cfa_store used to save items to the
1721 stack.
1723 Rules 10-14: Save a register to the stack. Define offset as the
1724 difference of the original location and cfa_store's
1725 location (or cfa_temp's location if cfa_temp is used).
1727 Rules 16-20: If AND operation happens on sp in prologue, we assume
1728 stack is realigned. We will use a group of DW_OP_XXX
1729 expressions to represent the location of the stored
1730 register instead of CFA+offset.
1732 The Rules
1734 "{a,b}" indicates a choice of a xor b.
1735 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1737 Rule 1:
1738 (set <reg1> <reg2>:cfa.reg)
1739 effects: cfa.reg = <reg1>
1740 cfa.offset unchanged
1741 cfa_temp.reg = <reg1>
1742 cfa_temp.offset = cfa.offset
1744 Rule 2:
1745 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1746 {<const_int>,<reg>:cfa_temp.reg}))
1747 effects: cfa.reg = sp if fp used
1748 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1749 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1750 if cfa_store.reg==sp
1752 Rule 3:
1753 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1754 effects: cfa.reg = fp
1755 cfa_offset += +/- <const_int>
1757 Rule 4:
1758 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1759 constraints: <reg1> != fp
1760 <reg1> != sp
1761 effects: cfa.reg = <reg1>
1762 cfa_temp.reg = <reg1>
1763 cfa_temp.offset = cfa.offset
1765 Rule 5:
1766 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1767 constraints: <reg1> != fp
1768 <reg1> != sp
1769 effects: cfa_store.reg = <reg1>
1770 cfa_store.offset = cfa.offset - cfa_temp.offset
1772 Rule 6:
1773 (set <reg> <const_int>)
1774 effects: cfa_temp.reg = <reg>
1775 cfa_temp.offset = <const_int>
1777 Rule 7:
1778 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1779 effects: cfa_temp.reg = <reg1>
1780 cfa_temp.offset |= <const_int>
1782 Rule 8:
1783 (set <reg> (high <exp>))
1784 effects: none
1786 Rule 9:
1787 (set <reg> (lo_sum <exp> <const_int>))
1788 effects: cfa_temp.reg = <reg>
1789 cfa_temp.offset = <const_int>
1791 Rule 10:
1792 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1793 effects: cfa_store.offset -= <const_int>
1794 cfa.offset = cfa_store.offset if cfa.reg == sp
1795 cfa.reg = sp
1796 cfa.base_offset = -cfa_store.offset
1798 Rule 11:
1799 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1800 effects: cfa_store.offset += -/+ mode_size(mem)
1801 cfa.offset = cfa_store.offset if cfa.reg == sp
1802 cfa.reg = sp
1803 cfa.base_offset = -cfa_store.offset
1805 Rule 12:
1806 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1808 <reg2>)
1809 effects: cfa.reg = <reg1>
1810 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1812 Rule 13:
1813 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1814 effects: cfa.reg = <reg1>
1815 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1817 Rule 14:
1818 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1819 effects: cfa.reg = <reg1>
1820 cfa.base_offset = -cfa_temp.offset
1821 cfa_temp.offset -= mode_size(mem)
1823 Rule 15:
1824 (set <reg> {unspec, unspec_volatile})
1825 effects: target-dependent
1827 Rule 16:
1828 (set sp (and: sp <const_int>))
1829 constraints: cfa_store.reg == sp
1830 effects: current_fde.stack_realign = 1
1831 cfa_store.offset = 0
1832 fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
1834 Rule 17:
1835 (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
1836 effects: cfa_store.offset += -/+ mode_size(mem)
1838 Rule 18:
1839 (set (mem ({pre_inc, pre_dec} sp)) fp)
1840 constraints: fde->stack_realign == 1
1841 effects: cfa_store.offset = 0
1842 cfa.reg != HARD_FRAME_POINTER_REGNUM
1844 Rule 19:
1845 (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
1846 constraints: fde->stack_realign == 1
1847 && cfa.offset == 0
1848 && cfa.indirect == 0
1849 && cfa.reg != HARD_FRAME_POINTER_REGNUM
1850 effects: Use DW_CFA_def_cfa_expression to define cfa
1851 cfa.reg == fde->drap_reg
1853 Rule 20:
1854 (set reg fde->drap_reg)
1855 constraints: fde->vdrap_reg == INVALID_REGNUM
1856 effects: fde->vdrap_reg = reg.
1857 (set mem fde->drap_reg)
1858 constraints: fde->drap_reg_saved == 1
1859 effects: none. */
1861 static void
1862 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1864 rtx src, dest, span;
1865 HOST_WIDE_INT offset;
1866 dw_fde_ref fde;
1868 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1869 the PARALLEL independently. The first element is always processed if
1870 it is a SET. This is for backward compatibility. Other elements
1871 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1872 flag is set in them. */
1873 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1875 int par_index;
1876 int limit = XVECLEN (expr, 0);
1877 rtx elem;
1879 /* PARALLELs have strict read-modify-write semantics, so we
1880 ought to evaluate every rvalue before changing any lvalue.
1881 It's cumbersome to do that in general, but there's an
1882 easy approximation that is enough for all current users:
1883 handle register saves before register assignments. */
1884 if (GET_CODE (expr) == PARALLEL)
1885 for (par_index = 0; par_index < limit; par_index++)
1887 elem = XVECEXP (expr, 0, par_index);
1888 if (GET_CODE (elem) == SET
1889 && MEM_P (SET_DEST (elem))
1890 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1891 dwarf2out_frame_debug_expr (elem, label);
1894 for (par_index = 0; par_index < limit; par_index++)
1896 elem = XVECEXP (expr, 0, par_index);
1897 if (GET_CODE (elem) == SET
1898 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1899 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1900 dwarf2out_frame_debug_expr (elem, label);
1901 else if (GET_CODE (elem) == SET
1902 && par_index != 0
1903 && !RTX_FRAME_RELATED_P (elem))
1905 /* Stack adjustment combining might combine some post-prologue
1906 stack adjustment into a prologue stack adjustment. */
1907 HOST_WIDE_INT offset = stack_adjust_offset (elem);
1909 if (offset != 0)
1910 dwarf2out_args_size_adjust (offset, label);
1913 return;
1916 gcc_assert (GET_CODE (expr) == SET);
1918 src = SET_SRC (expr);
1919 dest = SET_DEST (expr);
1921 if (REG_P (src))
1923 rtx rsi = reg_saved_in (src);
1924 if (rsi)
1925 src = rsi;
1928 fde = current_fde ();
1930 if (GET_CODE (src) == REG
1931 && fde
1932 && fde->drap_reg == REGNO (src)
1933 && (fde->drap_reg_saved
1934 || GET_CODE (dest) == REG))
1936 /* Rule 20 */
1937 /* If we are saving dynamic realign argument pointer to a
1938 register, the destination is virtual dynamic realign
1939 argument pointer. It may be used to access argument. */
1940 if (GET_CODE (dest) == REG)
1942 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
1943 fde->vdrap_reg = REGNO (dest);
1945 return;
1948 switch (GET_CODE (dest))
1950 case REG:
1951 switch (GET_CODE (src))
1953 /* Setting FP from SP. */
1954 case REG:
1955 if (cfa.reg == (unsigned) REGNO (src))
1957 /* Rule 1 */
1958 /* Update the CFA rule wrt SP or FP. Make sure src is
1959 relative to the current CFA register.
1961 We used to require that dest be either SP or FP, but the
1962 ARM copies SP to a temporary register, and from there to
1963 FP. So we just rely on the backends to only set
1964 RTX_FRAME_RELATED_P on appropriate insns. */
1965 cfa.reg = REGNO (dest);
1966 cfa_temp.reg = cfa.reg;
1967 cfa_temp.offset = cfa.offset;
1969 else
1971 /* Saving a register in a register. */
1972 gcc_assert (!fixed_regs [REGNO (dest)]
1973 /* For the SPARC and its register window. */
1974 || (DWARF_FRAME_REGNUM (REGNO (src))
1975 == DWARF_FRAME_RETURN_COLUMN));
1977 /* After stack is aligned, we can only save SP in FP
1978 if drap register is used. In this case, we have
1979 to restore stack pointer with the CFA value and we
1980 don't generate this DWARF information. */
1981 if (fde
1982 && fde->stack_realign
1983 && REGNO (src) == STACK_POINTER_REGNUM)
1984 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
1985 && fde->drap_reg != INVALID_REGNUM
1986 && cfa.reg != REGNO (src));
1987 else
1988 queue_reg_save (label, src, dest, 0);
1990 break;
1992 case PLUS:
1993 case MINUS:
1994 case LO_SUM:
1995 if (dest == stack_pointer_rtx)
1997 /* Rule 2 */
1998 /* Adjusting SP. */
1999 switch (GET_CODE (XEXP (src, 1)))
2001 case CONST_INT:
2002 offset = INTVAL (XEXP (src, 1));
2003 break;
2004 case REG:
2005 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2006 == cfa_temp.reg);
2007 offset = cfa_temp.offset;
2008 break;
2009 default:
2010 gcc_unreachable ();
2013 if (XEXP (src, 0) == hard_frame_pointer_rtx)
2015 /* Restoring SP from FP in the epilogue. */
2016 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2017 cfa.reg = STACK_POINTER_REGNUM;
2019 else if (GET_CODE (src) == LO_SUM)
2020 /* Assume we've set the source reg of the LO_SUM from sp. */
2022 else
2023 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2025 if (GET_CODE (src) != MINUS)
2026 offset = -offset;
2027 if (cfa.reg == STACK_POINTER_REGNUM)
2028 cfa.offset += offset;
2029 if (cfa_store.reg == STACK_POINTER_REGNUM)
2030 cfa_store.offset += offset;
2032 else if (dest == hard_frame_pointer_rtx)
2034 /* Rule 3 */
2035 /* Either setting the FP from an offset of the SP,
2036 or adjusting the FP */
2037 gcc_assert (frame_pointer_needed);
2039 gcc_assert (REG_P (XEXP (src, 0))
2040 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2041 && GET_CODE (XEXP (src, 1)) == CONST_INT);
2042 offset = INTVAL (XEXP (src, 1));
2043 if (GET_CODE (src) != MINUS)
2044 offset = -offset;
2045 cfa.offset += offset;
2046 cfa.reg = HARD_FRAME_POINTER_REGNUM;
2048 else
2050 gcc_assert (GET_CODE (src) != MINUS);
2052 /* Rule 4 */
2053 if (REG_P (XEXP (src, 0))
2054 && REGNO (XEXP (src, 0)) == cfa.reg
2055 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2057 /* Setting a temporary CFA register that will be copied
2058 into the FP later on. */
2059 offset = - INTVAL (XEXP (src, 1));
2060 cfa.offset += offset;
2061 cfa.reg = REGNO (dest);
2062 /* Or used to save regs to the stack. */
2063 cfa_temp.reg = cfa.reg;
2064 cfa_temp.offset = cfa.offset;
2067 /* Rule 5 */
2068 else if (REG_P (XEXP (src, 0))
2069 && REGNO (XEXP (src, 0)) == cfa_temp.reg
2070 && XEXP (src, 1) == stack_pointer_rtx)
2072 /* Setting a scratch register that we will use instead
2073 of SP for saving registers to the stack. */
2074 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2075 cfa_store.reg = REGNO (dest);
2076 cfa_store.offset = cfa.offset - cfa_temp.offset;
2079 /* Rule 9 */
2080 else if (GET_CODE (src) == LO_SUM
2081 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2083 cfa_temp.reg = REGNO (dest);
2084 cfa_temp.offset = INTVAL (XEXP (src, 1));
2086 else
2087 gcc_unreachable ();
2089 break;
2091 /* Rule 6 */
2092 case CONST_INT:
2093 cfa_temp.reg = REGNO (dest);
2094 cfa_temp.offset = INTVAL (src);
2095 break;
2097 /* Rule 7 */
2098 case IOR:
2099 gcc_assert (REG_P (XEXP (src, 0))
2100 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2101 && GET_CODE (XEXP (src, 1)) == CONST_INT);
2103 if ((unsigned) REGNO (dest) != cfa_temp.reg)
2104 cfa_temp.reg = REGNO (dest);
2105 cfa_temp.offset |= INTVAL (XEXP (src, 1));
2106 break;
2108 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2109 which will fill in all of the bits. */
2110 /* Rule 8 */
2111 case HIGH:
2112 break;
2114 /* Rule 15 */
2115 case UNSPEC:
2116 case UNSPEC_VOLATILE:
2117 gcc_assert (targetm.dwarf_handle_frame_unspec);
2118 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2119 return;
2121 /* Rule 16 */
2122 case AND:
2123 /* If this AND operation happens on stack pointer in prologue,
2124 we assume the stack is realigned and we extract the
2125 alignment. */
2126 if (fde && XEXP (src, 0) == stack_pointer_rtx)
2128 gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2129 fde->stack_realign = 1;
2130 fde->stack_realignment = INTVAL (XEXP (src, 1));
2131 cfa_store.offset = 0;
2133 if (cfa.reg != STACK_POINTER_REGNUM
2134 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2135 fde->drap_reg = cfa.reg;
2137 return;
2139 default:
2140 gcc_unreachable ();
2143 def_cfa_1 (label, &cfa);
2144 break;
2146 case MEM:
2148 /* Saving a register to the stack. Make sure dest is relative to the
2149 CFA register. */
2150 switch (GET_CODE (XEXP (dest, 0)))
2152 /* Rule 10 */
2153 /* With a push. */
2154 case PRE_MODIFY:
2155 /* We can't handle variable size modifications. */
2156 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2157 == CONST_INT);
2158 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2160 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2161 && cfa_store.reg == STACK_POINTER_REGNUM);
2163 cfa_store.offset += offset;
2164 if (cfa.reg == STACK_POINTER_REGNUM)
2165 cfa.offset = cfa_store.offset;
2167 offset = -cfa_store.offset;
2168 break;
2170 /* Rule 11 */
2171 case PRE_INC:
2172 case PRE_DEC:
2173 offset = GET_MODE_SIZE (GET_MODE (dest));
2174 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2175 offset = -offset;
2177 gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2178 == STACK_POINTER_REGNUM)
2179 && cfa_store.reg == STACK_POINTER_REGNUM);
2181 cfa_store.offset += offset;
2183 /* Rule 18: If stack is aligned, we will use FP as a
2184 reference to represent the address of the stored
2185 regiser. */
2186 if (fde
2187 && fde->stack_realign
2188 && src == hard_frame_pointer_rtx)
2190 gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2191 cfa_store.offset = 0;
2194 if (cfa.reg == STACK_POINTER_REGNUM)
2195 cfa.offset = cfa_store.offset;
2197 offset = -cfa_store.offset;
2198 break;
2200 /* Rule 12 */
2201 /* With an offset. */
2202 case PLUS:
2203 case MINUS:
2204 case LO_SUM:
2206 int regno;
2208 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
2209 && REG_P (XEXP (XEXP (dest, 0), 0)));
2210 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2211 if (GET_CODE (XEXP (dest, 0)) == MINUS)
2212 offset = -offset;
2214 regno = REGNO (XEXP (XEXP (dest, 0), 0));
2216 if (cfa_store.reg == (unsigned) regno)
2217 offset -= cfa_store.offset;
2218 else
2220 gcc_assert (cfa_temp.reg == (unsigned) regno);
2221 offset -= cfa_temp.offset;
2224 break;
2226 /* Rule 13 */
2227 /* Without an offset. */
2228 case REG:
2230 int regno = REGNO (XEXP (dest, 0));
2232 if (cfa_store.reg == (unsigned) regno)
2233 offset = -cfa_store.offset;
2234 else
2236 gcc_assert (cfa_temp.reg == (unsigned) regno);
2237 offset = -cfa_temp.offset;
2240 break;
2242 /* Rule 14 */
2243 case POST_INC:
2244 gcc_assert (cfa_temp.reg
2245 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2246 offset = -cfa_temp.offset;
2247 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2248 break;
2250 default:
2251 gcc_unreachable ();
2254 /* Rule 17 */
2255 /* If the source operand of this MEM operation is not a
2256 register, basically the source is return address. Here
2257 we only care how much stack grew and we don't save it. */
2258 if (!REG_P (src))
2259 break;
2261 if (REGNO (src) != STACK_POINTER_REGNUM
2262 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2263 && (unsigned) REGNO (src) == cfa.reg)
2265 /* We're storing the current CFA reg into the stack. */
2267 if (cfa.offset == 0)
2269 /* Rule 19 */
2270 /* If stack is aligned, putting CFA reg into stack means
2271 we can no longer use reg + offset to represent CFA.
2272 Here we use DW_CFA_def_cfa_expression instead. The
2273 result of this expression equals to the original CFA
2274 value. */
2275 if (fde
2276 && fde->stack_realign
2277 && cfa.indirect == 0
2278 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2280 dw_cfa_location cfa_exp;
2282 gcc_assert (fde->drap_reg == cfa.reg);
2284 cfa_exp.indirect = 1;
2285 cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2286 cfa_exp.base_offset = offset;
2287 cfa_exp.offset = 0;
2289 fde->drap_reg_saved = 1;
2291 def_cfa_1 (label, &cfa_exp);
2292 break;
2295 /* If the source register is exactly the CFA, assume
2296 we're saving SP like any other register; this happens
2297 on the ARM. */
2298 def_cfa_1 (label, &cfa);
2299 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2300 break;
2302 else
2304 /* Otherwise, we'll need to look in the stack to
2305 calculate the CFA. */
2306 rtx x = XEXP (dest, 0);
2308 if (!REG_P (x))
2309 x = XEXP (x, 0);
2310 gcc_assert (REG_P (x));
2312 cfa.reg = REGNO (x);
2313 cfa.base_offset = offset;
2314 cfa.indirect = 1;
2315 def_cfa_1 (label, &cfa);
2316 break;
2320 def_cfa_1 (label, &cfa);
2322 span = targetm.dwarf_register_span (src);
2324 if (!span)
2325 queue_reg_save (label, src, NULL_RTX, offset);
2326 else
2328 /* We have a PARALLEL describing where the contents of SRC
2329 live. Queue register saves for each piece of the
2330 PARALLEL. */
2331 int par_index;
2332 int limit;
2333 HOST_WIDE_INT span_offset = offset;
2335 gcc_assert (GET_CODE (span) == PARALLEL);
2337 limit = XVECLEN (span, 0);
2338 for (par_index = 0; par_index < limit; par_index++)
2340 rtx elem = XVECEXP (span, 0, par_index);
2342 queue_reg_save (label, elem, NULL_RTX, span_offset);
2343 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2347 break;
2349 default:
2350 gcc_unreachable ();
2354 /* Record call frame debugging information for INSN, which either
2355 sets SP or FP (adjusting how we calculate the frame address) or saves a
2356 register to the stack. If INSN is NULL_RTX, initialize our state.
2358 If AFTER_P is false, we're being called before the insn is emitted,
2359 otherwise after. Call instructions get invoked twice. */
2361 void
2362 dwarf2out_frame_debug (rtx insn, bool after_p)
2364 const char *label;
2365 rtx src;
2367 if (insn == NULL_RTX)
2369 size_t i;
2371 /* Flush any queued register saves. */
2372 flush_queued_reg_saves ();
2374 /* Set up state for generating call frame debug info. */
2375 lookup_cfa (&cfa);
2376 gcc_assert (cfa.reg
2377 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2379 cfa.reg = STACK_POINTER_REGNUM;
2380 cfa_store = cfa;
2381 cfa_temp.reg = -1;
2382 cfa_temp.offset = 0;
2384 for (i = 0; i < num_regs_saved_in_regs; i++)
2386 regs_saved_in_regs[i].orig_reg = NULL_RTX;
2387 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2389 num_regs_saved_in_regs = 0;
2391 if (barrier_args_size)
2393 XDELETEVEC (barrier_args_size);
2394 barrier_args_size = NULL;
2396 return;
2399 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2400 flush_queued_reg_saves ();
2402 if (! RTX_FRAME_RELATED_P (insn))
2404 if (!ACCUMULATE_OUTGOING_ARGS)
2405 dwarf2out_stack_adjust (insn, after_p);
2406 return;
2409 label = dwarf2out_cfi_label ();
2410 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2411 if (src)
2412 insn = XEXP (src, 0);
2413 else
2414 insn = PATTERN (insn);
2416 dwarf2out_frame_debug_expr (insn, label);
2419 #endif
2421 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
2422 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2423 (enum dwarf_call_frame_info cfi);
2425 static enum dw_cfi_oprnd_type
2426 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2428 switch (cfi)
2430 case DW_CFA_nop:
2431 case DW_CFA_GNU_window_save:
2432 return dw_cfi_oprnd_unused;
2434 case DW_CFA_set_loc:
2435 case DW_CFA_advance_loc1:
2436 case DW_CFA_advance_loc2:
2437 case DW_CFA_advance_loc4:
2438 case DW_CFA_MIPS_advance_loc8:
2439 return dw_cfi_oprnd_addr;
2441 case DW_CFA_offset:
2442 case DW_CFA_offset_extended:
2443 case DW_CFA_def_cfa:
2444 case DW_CFA_offset_extended_sf:
2445 case DW_CFA_def_cfa_sf:
2446 case DW_CFA_restore_extended:
2447 case DW_CFA_undefined:
2448 case DW_CFA_same_value:
2449 case DW_CFA_def_cfa_register:
2450 case DW_CFA_register:
2451 return dw_cfi_oprnd_reg_num;
2453 case DW_CFA_def_cfa_offset:
2454 case DW_CFA_GNU_args_size:
2455 case DW_CFA_def_cfa_offset_sf:
2456 return dw_cfi_oprnd_offset;
2458 case DW_CFA_def_cfa_expression:
2459 case DW_CFA_expression:
2460 return dw_cfi_oprnd_loc;
2462 default:
2463 gcc_unreachable ();
2467 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
2468 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2469 (enum dwarf_call_frame_info cfi);
2471 static enum dw_cfi_oprnd_type
2472 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2474 switch (cfi)
2476 case DW_CFA_def_cfa:
2477 case DW_CFA_def_cfa_sf:
2478 case DW_CFA_offset:
2479 case DW_CFA_offset_extended_sf:
2480 case DW_CFA_offset_extended:
2481 return dw_cfi_oprnd_offset;
2483 case DW_CFA_register:
2484 return dw_cfi_oprnd_reg_num;
2486 default:
2487 return dw_cfi_oprnd_unused;
2491 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2493 /* Switch to eh_frame_section. If we don't have an eh_frame_section,
2494 switch to the data section instead, and write out a synthetic label
2495 for collect2. */
2497 static void
2498 switch_to_eh_frame_section (void)
2500 tree label;
2502 #ifdef EH_FRAME_SECTION_NAME
2503 if (eh_frame_section == 0)
2505 int flags;
2507 if (EH_TABLES_CAN_BE_READ_ONLY)
2509 int fde_encoding;
2510 int per_encoding;
2511 int lsda_encoding;
2513 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2514 /*global=*/0);
2515 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2516 /*global=*/1);
2517 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2518 /*global=*/0);
2519 flags = ((! flag_pic
2520 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2521 && (fde_encoding & 0x70) != DW_EH_PE_aligned
2522 && (per_encoding & 0x70) != DW_EH_PE_absptr
2523 && (per_encoding & 0x70) != DW_EH_PE_aligned
2524 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2525 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2526 ? 0 : SECTION_WRITE);
2528 else
2529 flags = SECTION_WRITE;
2530 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2532 #endif
2534 if (eh_frame_section)
2535 switch_to_section (eh_frame_section);
2536 else
2538 /* We have no special eh_frame section. Put the information in
2539 the data section and emit special labels to guide collect2. */
2540 switch_to_section (data_section);
2541 label = get_file_function_name ("F");
2542 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2543 targetm.asm_out.globalize_label (asm_out_file,
2544 IDENTIFIER_POINTER (label));
2545 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2549 /* Output a Call Frame Information opcode and its operand(s). */
2551 static void
2552 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2554 unsigned long r;
2555 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2556 dw2_asm_output_data (1, (cfi->dw_cfi_opc
2557 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2558 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2559 ((unsigned HOST_WIDE_INT)
2560 cfi->dw_cfi_oprnd1.dw_cfi_offset));
2561 else if (cfi->dw_cfi_opc == DW_CFA_offset)
2563 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2564 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2565 "DW_CFA_offset, column 0x%lx", r);
2566 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2568 else if (cfi->dw_cfi_opc == DW_CFA_restore)
2570 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2571 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2572 "DW_CFA_restore, column 0x%lx", r);
2574 else
2576 dw2_asm_output_data (1, cfi->dw_cfi_opc,
2577 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2579 switch (cfi->dw_cfi_opc)
2581 case DW_CFA_set_loc:
2582 if (for_eh)
2583 dw2_asm_output_encoded_addr_rtx (
2584 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2585 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2586 false, NULL);
2587 else
2588 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2589 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2590 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2591 break;
2593 case DW_CFA_advance_loc1:
2594 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2595 fde->dw_fde_current_label, NULL);
2596 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2597 break;
2599 case DW_CFA_advance_loc2:
2600 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2601 fde->dw_fde_current_label, NULL);
2602 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2603 break;
2605 case DW_CFA_advance_loc4:
2606 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2607 fde->dw_fde_current_label, NULL);
2608 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2609 break;
2611 case DW_CFA_MIPS_advance_loc8:
2612 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2613 fde->dw_fde_current_label, NULL);
2614 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2615 break;
2617 case DW_CFA_offset_extended:
2618 case DW_CFA_def_cfa:
2619 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2620 dw2_asm_output_data_uleb128 (r, NULL);
2621 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2622 break;
2624 case DW_CFA_offset_extended_sf:
2625 case DW_CFA_def_cfa_sf:
2626 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2627 dw2_asm_output_data_uleb128 (r, NULL);
2628 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2629 break;
2631 case DW_CFA_restore_extended:
2632 case DW_CFA_undefined:
2633 case DW_CFA_same_value:
2634 case DW_CFA_def_cfa_register:
2635 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2636 dw2_asm_output_data_uleb128 (r, NULL);
2637 break;
2639 case DW_CFA_register:
2640 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2641 dw2_asm_output_data_uleb128 (r, NULL);
2642 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2643 dw2_asm_output_data_uleb128 (r, NULL);
2644 break;
2646 case DW_CFA_def_cfa_offset:
2647 case DW_CFA_GNU_args_size:
2648 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2649 break;
2651 case DW_CFA_def_cfa_offset_sf:
2652 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2653 break;
2655 case DW_CFA_GNU_window_save:
2656 break;
2658 case DW_CFA_def_cfa_expression:
2659 case DW_CFA_expression:
2660 output_cfa_loc (cfi);
2661 break;
2663 case DW_CFA_GNU_negative_offset_extended:
2664 /* Obsoleted by DW_CFA_offset_extended_sf. */
2665 gcc_unreachable ();
2667 default:
2668 break;
2673 /* Similar, but do it via assembler directives instead. */
2675 static void
2676 output_cfi_directive (dw_cfi_ref cfi)
2678 unsigned long r, r2;
2680 switch (cfi->dw_cfi_opc)
2682 case DW_CFA_advance_loc:
2683 case DW_CFA_advance_loc1:
2684 case DW_CFA_advance_loc2:
2685 case DW_CFA_advance_loc4:
2686 case DW_CFA_MIPS_advance_loc8:
2687 case DW_CFA_set_loc:
2688 /* Should only be created by add_fde_cfi in a code path not
2689 followed when emitting via directives. The assembler is
2690 going to take care of this for us. */
2691 gcc_unreachable ();
2693 case DW_CFA_offset:
2694 case DW_CFA_offset_extended:
2695 case DW_CFA_offset_extended_sf:
2696 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2697 fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2698 r, cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT);
2699 break;
2701 case DW_CFA_restore:
2702 case DW_CFA_restore_extended:
2703 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2704 fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
2705 break;
2707 case DW_CFA_undefined:
2708 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2709 fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
2710 break;
2712 case DW_CFA_same_value:
2713 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2714 fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
2715 break;
2717 case DW_CFA_def_cfa:
2718 case DW_CFA_def_cfa_sf:
2719 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2720 fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2721 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2722 break;
2724 case DW_CFA_def_cfa_register:
2725 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2726 fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
2727 break;
2729 case DW_CFA_register:
2730 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2731 r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 0);
2732 fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
2733 break;
2735 case DW_CFA_def_cfa_offset:
2736 case DW_CFA_def_cfa_offset_sf:
2737 fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
2738 HOST_WIDE_INT_PRINT_DEC"\n",
2739 cfi->dw_cfi_oprnd1.dw_cfi_offset);
2740 break;
2742 case DW_CFA_GNU_args_size:
2743 fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
2744 dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2745 if (flag_debug_asm)
2746 fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
2747 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
2748 fputc ('\n', asm_out_file);
2749 break;
2751 case DW_CFA_GNU_window_save:
2752 fprintf (asm_out_file, "\t.cfi_window_save\n");
2753 break;
2755 case DW_CFA_def_cfa_expression:
2756 case DW_CFA_expression:
2757 fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
2758 output_cfa_loc_raw (cfi);
2759 fputc ('\n', asm_out_file);
2760 break;
2762 default:
2763 gcc_unreachable ();
2767 /* Output the call frame information used to record information
2768 that relates to calculating the frame pointer, and records the
2769 location of saved registers. */
2771 static void
2772 output_call_frame_info (int for_eh)
2774 unsigned int i;
2775 dw_fde_ref fde;
2776 dw_cfi_ref cfi;
2777 char l1[20], l2[20], section_start_label[20];
2778 bool any_lsda_needed = false;
2779 char augmentation[6];
2780 int augmentation_size;
2781 int fde_encoding = DW_EH_PE_absptr;
2782 int per_encoding = DW_EH_PE_absptr;
2783 int lsda_encoding = DW_EH_PE_absptr;
2784 int return_reg;
2786 /* Don't emit a CIE if there won't be any FDEs. */
2787 if (fde_table_in_use == 0)
2788 return;
2790 /* Nothing to do if the assembler's doing it all. */
2791 if (dwarf2out_do_cfi_asm ())
2792 return;
2794 /* If we make FDEs linkonce, we may have to emit an empty label for
2795 an FDE that wouldn't otherwise be emitted. We want to avoid
2796 having an FDE kept around when the function it refers to is
2797 discarded. Example where this matters: a primary function
2798 template in C++ requires EH information, but an explicit
2799 specialization doesn't. */
2800 if (TARGET_USES_WEAK_UNWIND_INFO
2801 && ! flag_asynchronous_unwind_tables
2802 && flag_exceptions
2803 && for_eh)
2804 for (i = 0; i < fde_table_in_use; i++)
2805 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2806 && !fde_table[i].uses_eh_lsda
2807 && ! DECL_WEAK (fde_table[i].decl))
2808 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2809 for_eh, /* empty */ 1);
2811 /* If we don't have any functions we'll want to unwind out of, don't
2812 emit any EH unwind information. Note that if exceptions aren't
2813 enabled, we won't have collected nothrow information, and if we
2814 asked for asynchronous tables, we always want this info. */
2815 if (for_eh)
2817 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2819 for (i = 0; i < fde_table_in_use; i++)
2820 if (fde_table[i].uses_eh_lsda)
2821 any_eh_needed = any_lsda_needed = true;
2822 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2823 any_eh_needed = true;
2824 else if (! fde_table[i].nothrow
2825 && ! fde_table[i].all_throwers_are_sibcalls)
2826 any_eh_needed = true;
2828 if (! any_eh_needed)
2829 return;
2832 /* We're going to be generating comments, so turn on app. */
2833 if (flag_debug_asm)
2834 app_enable ();
2836 if (for_eh)
2837 switch_to_eh_frame_section ();
2838 else
2840 if (!debug_frame_section)
2841 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2842 SECTION_DEBUG, NULL);
2843 switch_to_section (debug_frame_section);
2846 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2847 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2849 /* Output the CIE. */
2850 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2851 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2852 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2853 dw2_asm_output_data (4, 0xffffffff,
2854 "Initial length escape value indicating 64-bit DWARF extension");
2855 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2856 "Length of Common Information Entry");
2857 ASM_OUTPUT_LABEL (asm_out_file, l1);
2859 /* Now that the CIE pointer is PC-relative for EH,
2860 use 0 to identify the CIE. */
2861 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2862 (for_eh ? 0 : DWARF_CIE_ID),
2863 "CIE Identifier Tag");
2865 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2867 augmentation[0] = 0;
2868 augmentation_size = 0;
2869 if (for_eh)
2871 char *p;
2873 /* Augmentation:
2874 z Indicates that a uleb128 is present to size the
2875 augmentation section.
2876 L Indicates the encoding (and thus presence) of
2877 an LSDA pointer in the FDE augmentation.
2878 R Indicates a non-default pointer encoding for
2879 FDE code pointers.
2880 P Indicates the presence of an encoding + language
2881 personality routine in the CIE augmentation. */
2883 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2884 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2885 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2887 p = augmentation + 1;
2888 if (eh_personality_libfunc)
2890 *p++ = 'P';
2891 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2892 assemble_external_libcall (eh_personality_libfunc);
2894 if (any_lsda_needed)
2896 *p++ = 'L';
2897 augmentation_size += 1;
2899 if (fde_encoding != DW_EH_PE_absptr)
2901 *p++ = 'R';
2902 augmentation_size += 1;
2904 if (p > augmentation + 1)
2906 augmentation[0] = 'z';
2907 *p = '\0';
2910 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2911 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2913 int offset = ( 4 /* Length */
2914 + 4 /* CIE Id */
2915 + 1 /* CIE version */
2916 + strlen (augmentation) + 1 /* Augmentation */
2917 + size_of_uleb128 (1) /* Code alignment */
2918 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2919 + 1 /* RA column */
2920 + 1 /* Augmentation size */
2921 + 1 /* Personality encoding */ );
2922 int pad = -offset & (PTR_SIZE - 1);
2924 augmentation_size += pad;
2926 /* Augmentations should be small, so there's scarce need to
2927 iterate for a solution. Die if we exceed one uleb128 byte. */
2928 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2932 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2933 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2934 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2935 "CIE Data Alignment Factor");
2937 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2938 if (DW_CIE_VERSION == 1)
2939 dw2_asm_output_data (1, return_reg, "CIE RA Column");
2940 else
2941 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2943 if (augmentation[0])
2945 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2946 if (eh_personality_libfunc)
2948 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2949 eh_data_format_name (per_encoding));
2950 dw2_asm_output_encoded_addr_rtx (per_encoding,
2951 eh_personality_libfunc,
2952 true, NULL);
2955 if (any_lsda_needed)
2956 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2957 eh_data_format_name (lsda_encoding));
2959 if (fde_encoding != DW_EH_PE_absptr)
2960 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2961 eh_data_format_name (fde_encoding));
2964 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2965 output_cfi (cfi, NULL, for_eh);
2967 /* Pad the CIE out to an address sized boundary. */
2968 ASM_OUTPUT_ALIGN (asm_out_file,
2969 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2970 ASM_OUTPUT_LABEL (asm_out_file, l2);
2972 /* Loop through all of the FDE's. */
2973 for (i = 0; i < fde_table_in_use; i++)
2975 fde = &fde_table[i];
2977 /* Don't emit EH unwind info for leaf functions that don't need it. */
2978 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2979 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2980 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2981 && !fde->uses_eh_lsda)
2982 continue;
2984 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2985 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2986 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2987 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2988 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2989 dw2_asm_output_data (4, 0xffffffff,
2990 "Initial length escape value indicating 64-bit DWARF extension");
2991 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2992 "FDE Length");
2993 ASM_OUTPUT_LABEL (asm_out_file, l1);
2995 if (for_eh)
2996 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2997 else
2998 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2999 debug_frame_section, "FDE CIE offset");
3001 if (for_eh)
3003 if (fde->dw_fde_switched_sections)
3005 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
3006 fde->dw_fde_unlikely_section_label);
3007 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
3008 fde->dw_fde_hot_section_label);
3009 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3010 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
3011 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
3012 "FDE initial location");
3013 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3014 fde->dw_fde_hot_section_end_label,
3015 fde->dw_fde_hot_section_label,
3016 "FDE address range");
3017 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
3018 "FDE initial location");
3019 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3020 fde->dw_fde_unlikely_section_end_label,
3021 fde->dw_fde_unlikely_section_label,
3022 "FDE address range");
3024 else
3026 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3027 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3028 dw2_asm_output_encoded_addr_rtx (fde_encoding,
3029 sym_ref,
3030 false,
3031 "FDE initial location");
3032 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3033 fde->dw_fde_end, fde->dw_fde_begin,
3034 "FDE address range");
3037 else
3039 if (fde->dw_fde_switched_sections)
3041 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3042 fde->dw_fde_hot_section_label,
3043 "FDE initial location");
3044 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3045 fde->dw_fde_hot_section_end_label,
3046 fde->dw_fde_hot_section_label,
3047 "FDE address range");
3048 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3049 fde->dw_fde_unlikely_section_label,
3050 "FDE initial location");
3051 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3052 fde->dw_fde_unlikely_section_end_label,
3053 fde->dw_fde_unlikely_section_label,
3054 "FDE address range");
3056 else
3058 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3059 "FDE initial location");
3060 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3061 fde->dw_fde_end, fde->dw_fde_begin,
3062 "FDE address range");
3066 if (augmentation[0])
3068 if (any_lsda_needed)
3070 int size = size_of_encoded_value (lsda_encoding);
3072 if (lsda_encoding == DW_EH_PE_aligned)
3074 int offset = ( 4 /* Length */
3075 + 4 /* CIE offset */
3076 + 2 * size_of_encoded_value (fde_encoding)
3077 + 1 /* Augmentation size */ );
3078 int pad = -offset & (PTR_SIZE - 1);
3080 size += pad;
3081 gcc_assert (size_of_uleb128 (size) == 1);
3084 dw2_asm_output_data_uleb128 (size, "Augmentation size");
3086 if (fde->uses_eh_lsda)
3088 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3089 fde->funcdef_number);
3090 dw2_asm_output_encoded_addr_rtx (
3091 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3092 false, "Language Specific Data Area");
3094 else
3096 if (lsda_encoding == DW_EH_PE_aligned)
3097 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3098 dw2_asm_output_data
3099 (size_of_encoded_value (lsda_encoding), 0,
3100 "Language Specific Data Area (none)");
3103 else
3104 dw2_asm_output_data_uleb128 (0, "Augmentation size");
3107 /* Loop through the Call Frame Instructions associated with
3108 this FDE. */
3109 fde->dw_fde_current_label = fde->dw_fde_begin;
3110 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3111 output_cfi (cfi, fde, for_eh);
3113 /* Pad the FDE out to an address sized boundary. */
3114 ASM_OUTPUT_ALIGN (asm_out_file,
3115 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3116 ASM_OUTPUT_LABEL (asm_out_file, l2);
3119 if (for_eh && targetm.terminate_dw2_eh_frame_info)
3120 dw2_asm_output_data (4, 0, "End of Table");
3121 #ifdef MIPS_DEBUGGING_INFO
3122 /* Work around Irix 6 assembler bug whereby labels at the end of a section
3123 get a value of 0. Putting .align 0 after the label fixes it. */
3124 ASM_OUTPUT_ALIGN (asm_out_file, 0);
3125 #endif
3127 /* Turn off app to make assembly quicker. */
3128 if (flag_debug_asm)
3129 app_disable ();
3132 /* Output a marker (i.e. a label) for the beginning of a function, before
3133 the prologue. */
3135 void
3136 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3137 const char *file ATTRIBUTE_UNUSED)
3139 char label[MAX_ARTIFICIAL_LABEL_BYTES];
3140 char * dup_label;
3141 dw_fde_ref fde;
3143 current_function_func_begin_label = NULL;
3145 #ifdef TARGET_UNWIND_INFO
3146 /* ??? current_function_func_begin_label is also used by except.c
3147 for call-site information. We must emit this label if it might
3148 be used. */
3149 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3150 && ! dwarf2out_do_frame ())
3151 return;
3152 #else
3153 if (! dwarf2out_do_frame ())
3154 return;
3155 #endif
3157 switch_to_section (function_section (current_function_decl));
3158 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3159 current_function_funcdef_no);
3160 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3161 current_function_funcdef_no);
3162 dup_label = xstrdup (label);
3163 current_function_func_begin_label = dup_label;
3165 #ifdef TARGET_UNWIND_INFO
3166 /* We can elide the fde allocation if we're not emitting debug info. */
3167 if (! dwarf2out_do_frame ())
3168 return;
3169 #endif
3171 /* Expand the fde table if necessary. */
3172 if (fde_table_in_use == fde_table_allocated)
3174 fde_table_allocated += FDE_TABLE_INCREMENT;
3175 fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3176 memset (fde_table + fde_table_in_use, 0,
3177 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3180 /* Record the FDE associated with this function. */
3181 current_funcdef_fde = fde_table_in_use;
3183 /* Add the new FDE at the end of the fde_table. */
3184 fde = &fde_table[fde_table_in_use++];
3185 fde->decl = current_function_decl;
3186 fde->dw_fde_begin = dup_label;
3187 fde->dw_fde_current_label = dup_label;
3188 fde->dw_fde_hot_section_label = NULL;
3189 fde->dw_fde_hot_section_end_label = NULL;
3190 fde->dw_fde_unlikely_section_label = NULL;
3191 fde->dw_fde_unlikely_section_end_label = NULL;
3192 fde->dw_fde_switched_sections = false;
3193 fde->dw_fde_end = NULL;
3194 fde->dw_fde_cfi = NULL;
3195 fde->funcdef_number = current_function_funcdef_no;
3196 fde->nothrow = TREE_NOTHROW (current_function_decl);
3197 fde->uses_eh_lsda = crtl->uses_eh_lsda;
3198 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3199 fde->drap_reg = INVALID_REGNUM;
3200 fde->vdrap_reg = INVALID_REGNUM;
3202 args_size = old_args_size = 0;
3204 /* We only want to output line number information for the genuine dwarf2
3205 prologue case, not the eh frame case. */
3206 #ifdef DWARF2_DEBUGGING_INFO
3207 if (file)
3208 dwarf2out_source_line (line, file);
3209 #endif
3211 if (dwarf2out_do_cfi_asm ())
3213 int enc;
3214 rtx ref;
3216 fprintf (asm_out_file, "\t.cfi_startproc\n");
3218 if (eh_personality_libfunc)
3220 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3221 ref = eh_personality_libfunc;
3223 /* ??? The GAS support isn't entirely consistent. We have to
3224 handle indirect support ourselves, but PC-relative is done
3225 in the assembler. Further, the assembler can't handle any
3226 of the weirder relocation types. */
3227 if (enc & DW_EH_PE_indirect)
3228 ref = dw2_force_const_mem (ref, true);
3230 fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3231 output_addr_const (asm_out_file, ref);
3232 fputc ('\n', asm_out_file);
3235 if (crtl->uses_eh_lsda)
3237 char lab[20];
3239 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3240 ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3241 current_function_funcdef_no);
3242 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3243 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3245 if (enc & DW_EH_PE_indirect)
3246 ref = dw2_force_const_mem (ref, true);
3248 fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3249 output_addr_const (asm_out_file, ref);
3250 fputc ('\n', asm_out_file);
3255 /* Output a marker (i.e. a label) for the absolute end of the generated code
3256 for a function definition. This gets called *after* the epilogue code has
3257 been generated. */
3259 void
3260 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3261 const char *file ATTRIBUTE_UNUSED)
3263 dw_fde_ref fde;
3264 char label[MAX_ARTIFICIAL_LABEL_BYTES];
3266 if (dwarf2out_do_cfi_asm ())
3267 fprintf (asm_out_file, "\t.cfi_endproc\n");
3269 /* Output a label to mark the endpoint of the code generated for this
3270 function. */
3271 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3272 current_function_funcdef_no);
3273 ASM_OUTPUT_LABEL (asm_out_file, label);
3274 fde = current_fde ();
3275 gcc_assert (fde != NULL);
3276 fde->dw_fde_end = xstrdup (label);
3279 void
3280 dwarf2out_frame_init (void)
3282 /* Allocate the initial hunk of the fde_table. */
3283 fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3284 fde_table_allocated = FDE_TABLE_INCREMENT;
3285 fde_table_in_use = 0;
3287 /* Generate the CFA instructions common to all FDE's. Do it now for the
3288 sake of lookup_cfa. */
3290 /* On entry, the Canonical Frame Address is at SP. */
3291 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3293 #ifdef DWARF2_UNWIND_INFO
3294 if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3295 initial_return_save (INCOMING_RETURN_ADDR_RTX);
3296 #endif
3299 void
3300 dwarf2out_frame_finish (void)
3302 /* Output call frame information. */
3303 if (DWARF2_FRAME_INFO)
3304 output_call_frame_info (0);
3306 #ifndef TARGET_UNWIND_INFO
3307 /* Output another copy for the unwinder. */
3308 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3309 output_call_frame_info (1);
3310 #endif
3313 /* Note that the current function section is being used for code. */
3315 static void
3316 dwarf2out_note_section_used (void)
3318 section *sec = current_function_section ();
3319 if (sec == text_section)
3320 text_section_used = true;
3321 else if (sec == cold_text_section)
3322 cold_text_section_used = true;
3325 void
3326 dwarf2out_switch_text_section (void)
3328 dw_fde_ref fde = current_fde ();
3330 gcc_assert (cfun && fde);
3332 fde->dw_fde_switched_sections = true;
3333 fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3334 fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3335 fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3336 fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3337 have_multiple_function_sections = true;
3339 /* Reset the current label on switching text sections, so that we
3340 don't attempt to advance_loc4 between labels in different sections. */
3341 fde->dw_fde_current_label = NULL;
3343 /* There is no need to mark used sections when not debugging. */
3344 if (cold_text_section != NULL)
3345 dwarf2out_note_section_used ();
3347 #endif
3349 /* And now, the subset of the debugging information support code necessary
3350 for emitting location expressions. */
3352 /* Data about a single source file. */
3353 struct dwarf_file_data GTY(())
3355 const char * filename;
3356 int emitted_number;
3359 /* We need some way to distinguish DW_OP_addr with a direct symbol
3360 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
3361 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
3364 typedef struct dw_val_struct *dw_val_ref;
3365 typedef struct die_struct *dw_die_ref;
3366 typedef const struct die_struct *const_dw_die_ref;
3367 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3368 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3370 /* Each DIE may have a series of attribute/value pairs. Values
3371 can take on several forms. The forms that are used in this
3372 implementation are listed below. */
3374 enum dw_val_class
3376 dw_val_class_addr,
3377 dw_val_class_offset,
3378 dw_val_class_loc,
3379 dw_val_class_loc_list,
3380 dw_val_class_range_list,
3381 dw_val_class_const,
3382 dw_val_class_unsigned_const,
3383 dw_val_class_long_long,
3384 dw_val_class_vec,
3385 dw_val_class_flag,
3386 dw_val_class_die_ref,
3387 dw_val_class_fde_ref,
3388 dw_val_class_lbl_id,
3389 dw_val_class_lineptr,
3390 dw_val_class_str,
3391 dw_val_class_macptr,
3392 dw_val_class_file
3395 /* Describe a double word constant value. */
3396 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
3398 typedef struct dw_long_long_struct GTY(())
3400 unsigned long hi;
3401 unsigned long low;
3403 dw_long_long_const;
3405 /* Describe a floating point constant value, or a vector constant value. */
3407 typedef struct dw_vec_struct GTY(())
3409 unsigned char * GTY((length ("%h.length"))) array;
3410 unsigned length;
3411 unsigned elt_size;
3413 dw_vec_const;
3415 /* The dw_val_node describes an attribute's value, as it is
3416 represented internally. */
3418 typedef struct dw_val_struct GTY(())
3420 enum dw_val_class val_class;
3421 union dw_val_struct_union
3423 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3424 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3425 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3426 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3427 HOST_WIDE_INT GTY ((default)) val_int;
3428 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3429 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3430 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3431 struct dw_val_die_union
3433 dw_die_ref die;
3434 int external;
3435 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3436 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3437 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3438 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3439 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3440 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3442 GTY ((desc ("%1.val_class"))) v;
3444 dw_val_node;
3446 /* Locations in memory are described using a sequence of stack machine
3447 operations. */
3449 typedef struct dw_loc_descr_struct GTY(())
3451 dw_loc_descr_ref dw_loc_next;
3452 enum dwarf_location_atom dw_loc_opc;
3453 dw_val_node dw_loc_oprnd1;
3454 dw_val_node dw_loc_oprnd2;
3455 int dw_loc_addr;
3457 dw_loc_descr_node;
3459 /* Location lists are ranges + location descriptions for that range,
3460 so you can track variables that are in different places over
3461 their entire life. */
3462 typedef struct dw_loc_list_struct GTY(())
3464 dw_loc_list_ref dw_loc_next;
3465 const char *begin; /* Label for begin address of range */
3466 const char *end; /* Label for end address of range */
3467 char *ll_symbol; /* Label for beginning of location list.
3468 Only on head of list */
3469 const char *section; /* Section this loclist is relative to */
3470 dw_loc_descr_ref expr;
3471 } dw_loc_list_node;
3473 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3475 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3477 /* Convert a DWARF stack opcode into its string name. */
3479 static const char *
3480 dwarf_stack_op_name (unsigned int op)
3482 switch (op)
3484 case DW_OP_addr:
3485 case INTERNAL_DW_OP_tls_addr:
3486 return "DW_OP_addr";
3487 case DW_OP_deref:
3488 return "DW_OP_deref";
3489 case DW_OP_const1u:
3490 return "DW_OP_const1u";
3491 case DW_OP_const1s:
3492 return "DW_OP_const1s";
3493 case DW_OP_const2u:
3494 return "DW_OP_const2u";
3495 case DW_OP_const2s:
3496 return "DW_OP_const2s";
3497 case DW_OP_const4u:
3498 return "DW_OP_const4u";
3499 case DW_OP_const4s:
3500 return "DW_OP_const4s";
3501 case DW_OP_const8u:
3502 return "DW_OP_const8u";
3503 case DW_OP_const8s:
3504 return "DW_OP_const8s";
3505 case DW_OP_constu:
3506 return "DW_OP_constu";
3507 case DW_OP_consts:
3508 return "DW_OP_consts";
3509 case DW_OP_dup:
3510 return "DW_OP_dup";
3511 case DW_OP_drop:
3512 return "DW_OP_drop";
3513 case DW_OP_over:
3514 return "DW_OP_over";
3515 case DW_OP_pick:
3516 return "DW_OP_pick";
3517 case DW_OP_swap:
3518 return "DW_OP_swap";
3519 case DW_OP_rot:
3520 return "DW_OP_rot";
3521 case DW_OP_xderef:
3522 return "DW_OP_xderef";
3523 case DW_OP_abs:
3524 return "DW_OP_abs";
3525 case DW_OP_and:
3526 return "DW_OP_and";
3527 case DW_OP_div:
3528 return "DW_OP_div";
3529 case DW_OP_minus:
3530 return "DW_OP_minus";
3531 case DW_OP_mod:
3532 return "DW_OP_mod";
3533 case DW_OP_mul:
3534 return "DW_OP_mul";
3535 case DW_OP_neg:
3536 return "DW_OP_neg";
3537 case DW_OP_not:
3538 return "DW_OP_not";
3539 case DW_OP_or:
3540 return "DW_OP_or";
3541 case DW_OP_plus:
3542 return "DW_OP_plus";
3543 case DW_OP_plus_uconst:
3544 return "DW_OP_plus_uconst";
3545 case DW_OP_shl:
3546 return "DW_OP_shl";
3547 case DW_OP_shr:
3548 return "DW_OP_shr";
3549 case DW_OP_shra:
3550 return "DW_OP_shra";
3551 case DW_OP_xor:
3552 return "DW_OP_xor";
3553 case DW_OP_bra:
3554 return "DW_OP_bra";
3555 case DW_OP_eq:
3556 return "DW_OP_eq";
3557 case DW_OP_ge:
3558 return "DW_OP_ge";
3559 case DW_OP_gt:
3560 return "DW_OP_gt";
3561 case DW_OP_le:
3562 return "DW_OP_le";
3563 case DW_OP_lt:
3564 return "DW_OP_lt";
3565 case DW_OP_ne:
3566 return "DW_OP_ne";
3567 case DW_OP_skip:
3568 return "DW_OP_skip";
3569 case DW_OP_lit0:
3570 return "DW_OP_lit0";
3571 case DW_OP_lit1:
3572 return "DW_OP_lit1";
3573 case DW_OP_lit2:
3574 return "DW_OP_lit2";
3575 case DW_OP_lit3:
3576 return "DW_OP_lit3";
3577 case DW_OP_lit4:
3578 return "DW_OP_lit4";
3579 case DW_OP_lit5:
3580 return "DW_OP_lit5";
3581 case DW_OP_lit6:
3582 return "DW_OP_lit6";
3583 case DW_OP_lit7:
3584 return "DW_OP_lit7";
3585 case DW_OP_lit8:
3586 return "DW_OP_lit8";
3587 case DW_OP_lit9:
3588 return "DW_OP_lit9";
3589 case DW_OP_lit10:
3590 return "DW_OP_lit10";
3591 case DW_OP_lit11:
3592 return "DW_OP_lit11";
3593 case DW_OP_lit12:
3594 return "DW_OP_lit12";
3595 case DW_OP_lit13:
3596 return "DW_OP_lit13";
3597 case DW_OP_lit14:
3598 return "DW_OP_lit14";
3599 case DW_OP_lit15:
3600 return "DW_OP_lit15";
3601 case DW_OP_lit16:
3602 return "DW_OP_lit16";
3603 case DW_OP_lit17:
3604 return "DW_OP_lit17";
3605 case DW_OP_lit18:
3606 return "DW_OP_lit18";
3607 case DW_OP_lit19:
3608 return "DW_OP_lit19";
3609 case DW_OP_lit20:
3610 return "DW_OP_lit20";
3611 case DW_OP_lit21:
3612 return "DW_OP_lit21";
3613 case DW_OP_lit22:
3614 return "DW_OP_lit22";
3615 case DW_OP_lit23:
3616 return "DW_OP_lit23";
3617 case DW_OP_lit24:
3618 return "DW_OP_lit24";
3619 case DW_OP_lit25:
3620 return "DW_OP_lit25";
3621 case DW_OP_lit26:
3622 return "DW_OP_lit26";
3623 case DW_OP_lit27:
3624 return "DW_OP_lit27";
3625 case DW_OP_lit28:
3626 return "DW_OP_lit28";
3627 case DW_OP_lit29:
3628 return "DW_OP_lit29";
3629 case DW_OP_lit30:
3630 return "DW_OP_lit30";
3631 case DW_OP_lit31:
3632 return "DW_OP_lit31";
3633 case DW_OP_reg0:
3634 return "DW_OP_reg0";
3635 case DW_OP_reg1:
3636 return "DW_OP_reg1";
3637 case DW_OP_reg2:
3638 return "DW_OP_reg2";
3639 case DW_OP_reg3:
3640 return "DW_OP_reg3";
3641 case DW_OP_reg4:
3642 return "DW_OP_reg4";
3643 case DW_OP_reg5:
3644 return "DW_OP_reg5";
3645 case DW_OP_reg6:
3646 return "DW_OP_reg6";
3647 case DW_OP_reg7:
3648 return "DW_OP_reg7";
3649 case DW_OP_reg8:
3650 return "DW_OP_reg8";
3651 case DW_OP_reg9:
3652 return "DW_OP_reg9";
3653 case DW_OP_reg10:
3654 return "DW_OP_reg10";
3655 case DW_OP_reg11:
3656 return "DW_OP_reg11";
3657 case DW_OP_reg12:
3658 return "DW_OP_reg12";
3659 case DW_OP_reg13:
3660 return "DW_OP_reg13";
3661 case DW_OP_reg14:
3662 return "DW_OP_reg14";
3663 case DW_OP_reg15:
3664 return "DW_OP_reg15";
3665 case DW_OP_reg16:
3666 return "DW_OP_reg16";
3667 case DW_OP_reg17:
3668 return "DW_OP_reg17";
3669 case DW_OP_reg18:
3670 return "DW_OP_reg18";
3671 case DW_OP_reg19:
3672 return "DW_OP_reg19";
3673 case DW_OP_reg20:
3674 return "DW_OP_reg20";
3675 case DW_OP_reg21:
3676 return "DW_OP_reg21";
3677 case DW_OP_reg22:
3678 return "DW_OP_reg22";
3679 case DW_OP_reg23:
3680 return "DW_OP_reg23";
3681 case DW_OP_reg24:
3682 return "DW_OP_reg24";
3683 case DW_OP_reg25:
3684 return "DW_OP_reg25";
3685 case DW_OP_reg26:
3686 return "DW_OP_reg26";
3687 case DW_OP_reg27:
3688 return "DW_OP_reg27";
3689 case DW_OP_reg28:
3690 return "DW_OP_reg28";
3691 case DW_OP_reg29:
3692 return "DW_OP_reg29";
3693 case DW_OP_reg30:
3694 return "DW_OP_reg30";
3695 case DW_OP_reg31:
3696 return "DW_OP_reg31";
3697 case DW_OP_breg0:
3698 return "DW_OP_breg0";
3699 case DW_OP_breg1:
3700 return "DW_OP_breg1";
3701 case DW_OP_breg2:
3702 return "DW_OP_breg2";
3703 case DW_OP_breg3:
3704 return "DW_OP_breg3";
3705 case DW_OP_breg4:
3706 return "DW_OP_breg4";
3707 case DW_OP_breg5:
3708 return "DW_OP_breg5";
3709 case DW_OP_breg6:
3710 return "DW_OP_breg6";
3711 case DW_OP_breg7:
3712 return "DW_OP_breg7";
3713 case DW_OP_breg8:
3714 return "DW_OP_breg8";
3715 case DW_OP_breg9:
3716 return "DW_OP_breg9";
3717 case DW_OP_breg10:
3718 return "DW_OP_breg10";
3719 case DW_OP_breg11:
3720 return "DW_OP_breg11";
3721 case DW_OP_breg12:
3722 return "DW_OP_breg12";
3723 case DW_OP_breg13:
3724 return "DW_OP_breg13";
3725 case DW_OP_breg14:
3726 return "DW_OP_breg14";
3727 case DW_OP_breg15:
3728 return "DW_OP_breg15";
3729 case DW_OP_breg16:
3730 return "DW_OP_breg16";
3731 case DW_OP_breg17:
3732 return "DW_OP_breg17";
3733 case DW_OP_breg18:
3734 return "DW_OP_breg18";
3735 case DW_OP_breg19:
3736 return "DW_OP_breg19";
3737 case DW_OP_breg20:
3738 return "DW_OP_breg20";
3739 case DW_OP_breg21:
3740 return "DW_OP_breg21";
3741 case DW_OP_breg22:
3742 return "DW_OP_breg22";
3743 case DW_OP_breg23:
3744 return "DW_OP_breg23";
3745 case DW_OP_breg24:
3746 return "DW_OP_breg24";
3747 case DW_OP_breg25:
3748 return "DW_OP_breg25";
3749 case DW_OP_breg26:
3750 return "DW_OP_breg26";
3751 case DW_OP_breg27:
3752 return "DW_OP_breg27";
3753 case DW_OP_breg28:
3754 return "DW_OP_breg28";
3755 case DW_OP_breg29:
3756 return "DW_OP_breg29";
3757 case DW_OP_breg30:
3758 return "DW_OP_breg30";
3759 case DW_OP_breg31:
3760 return "DW_OP_breg31";
3761 case DW_OP_regx:
3762 return "DW_OP_regx";
3763 case DW_OP_fbreg:
3764 return "DW_OP_fbreg";
3765 case DW_OP_bregx:
3766 return "DW_OP_bregx";
3767 case DW_OP_piece:
3768 return "DW_OP_piece";
3769 case DW_OP_deref_size:
3770 return "DW_OP_deref_size";
3771 case DW_OP_xderef_size:
3772 return "DW_OP_xderef_size";
3773 case DW_OP_nop:
3774 return "DW_OP_nop";
3775 case DW_OP_push_object_address:
3776 return "DW_OP_push_object_address";
3777 case DW_OP_call2:
3778 return "DW_OP_call2";
3779 case DW_OP_call4:
3780 return "DW_OP_call4";
3781 case DW_OP_call_ref:
3782 return "DW_OP_call_ref";
3783 case DW_OP_GNU_push_tls_address:
3784 return "DW_OP_GNU_push_tls_address";
3785 case DW_OP_GNU_uninit:
3786 return "DW_OP_GNU_uninit";
3787 default:
3788 return "OP_<unknown>";
3792 /* Return a pointer to a newly allocated location description. Location
3793 descriptions are simple expression terms that can be strung
3794 together to form more complicated location (address) descriptions. */
3796 static inline dw_loc_descr_ref
3797 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3798 unsigned HOST_WIDE_INT oprnd2)
3800 dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
3802 descr->dw_loc_opc = op;
3803 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3804 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3805 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3806 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3808 return descr;
3811 /* Return a pointer to a newly allocated location description for
3812 REG and OFFSET. */
3814 static inline dw_loc_descr_ref
3815 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
3817 if (offset)
3819 if (reg <= 31)
3820 return new_loc_descr (DW_OP_breg0 + reg, offset, 0);
3821 else
3822 return new_loc_descr (DW_OP_bregx, reg, offset);
3824 else if (reg <= 31)
3825 return new_loc_descr (DW_OP_reg0 + reg, 0, 0);
3826 else
3827 return new_loc_descr (DW_OP_regx, reg, 0);
3830 /* Add a location description term to a location description expression. */
3832 static inline void
3833 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3835 dw_loc_descr_ref *d;
3837 /* Find the end of the chain. */
3838 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3841 *d = descr;
3844 /* Return the size of a location descriptor. */
3846 static unsigned long
3847 size_of_loc_descr (dw_loc_descr_ref loc)
3849 unsigned long size = 1;
3851 switch (loc->dw_loc_opc)
3853 case DW_OP_addr:
3854 case INTERNAL_DW_OP_tls_addr:
3855 size += DWARF2_ADDR_SIZE;
3856 break;
3857 case DW_OP_const1u:
3858 case DW_OP_const1s:
3859 size += 1;
3860 break;
3861 case DW_OP_const2u:
3862 case DW_OP_const2s:
3863 size += 2;
3864 break;
3865 case DW_OP_const4u:
3866 case DW_OP_const4s:
3867 size += 4;
3868 break;
3869 case DW_OP_const8u:
3870 case DW_OP_const8s:
3871 size += 8;
3872 break;
3873 case DW_OP_constu:
3874 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3875 break;
3876 case DW_OP_consts:
3877 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3878 break;
3879 case DW_OP_pick:
3880 size += 1;
3881 break;
3882 case DW_OP_plus_uconst:
3883 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3884 break;
3885 case DW_OP_skip:
3886 case DW_OP_bra:
3887 size += 2;
3888 break;
3889 case DW_OP_breg0:
3890 case DW_OP_breg1:
3891 case DW_OP_breg2:
3892 case DW_OP_breg3:
3893 case DW_OP_breg4:
3894 case DW_OP_breg5:
3895 case DW_OP_breg6:
3896 case DW_OP_breg7:
3897 case DW_OP_breg8:
3898 case DW_OP_breg9:
3899 case DW_OP_breg10:
3900 case DW_OP_breg11:
3901 case DW_OP_breg12:
3902 case DW_OP_breg13:
3903 case DW_OP_breg14:
3904 case DW_OP_breg15:
3905 case DW_OP_breg16:
3906 case DW_OP_breg17:
3907 case DW_OP_breg18:
3908 case DW_OP_breg19:
3909 case DW_OP_breg20:
3910 case DW_OP_breg21:
3911 case DW_OP_breg22:
3912 case DW_OP_breg23:
3913 case DW_OP_breg24:
3914 case DW_OP_breg25:
3915 case DW_OP_breg26:
3916 case DW_OP_breg27:
3917 case DW_OP_breg28:
3918 case DW_OP_breg29:
3919 case DW_OP_breg30:
3920 case DW_OP_breg31:
3921 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3922 break;
3923 case DW_OP_regx:
3924 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3925 break;
3926 case DW_OP_fbreg:
3927 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3928 break;
3929 case DW_OP_bregx:
3930 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3931 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3932 break;
3933 case DW_OP_piece:
3934 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3935 break;
3936 case DW_OP_deref_size:
3937 case DW_OP_xderef_size:
3938 size += 1;
3939 break;
3940 case DW_OP_call2:
3941 size += 2;
3942 break;
3943 case DW_OP_call4:
3944 size += 4;
3945 break;
3946 case DW_OP_call_ref:
3947 size += DWARF2_ADDR_SIZE;
3948 break;
3949 default:
3950 break;
3953 return size;
3956 /* Return the size of a series of location descriptors. */
3958 static unsigned long
3959 size_of_locs (dw_loc_descr_ref loc)
3961 dw_loc_descr_ref l;
3962 unsigned long size;
3964 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3965 field, to avoid writing to a PCH file. */
3966 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3968 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3969 break;
3970 size += size_of_loc_descr (l);
3972 if (! l)
3973 return size;
3975 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3977 l->dw_loc_addr = size;
3978 size += size_of_loc_descr (l);
3981 return size;
3984 /* Output location description stack opcode's operands (if any). */
3986 static void
3987 output_loc_operands (dw_loc_descr_ref loc)
3989 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3990 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3992 switch (loc->dw_loc_opc)
3994 #ifdef DWARF2_DEBUGGING_INFO
3995 case DW_OP_addr:
3996 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3997 break;
3998 case DW_OP_const2u:
3999 case DW_OP_const2s:
4000 dw2_asm_output_data (2, val1->v.val_int, NULL);
4001 break;
4002 case DW_OP_const4u:
4003 case DW_OP_const4s:
4004 dw2_asm_output_data (4, val1->v.val_int, NULL);
4005 break;
4006 case DW_OP_const8u:
4007 case DW_OP_const8s:
4008 gcc_assert (HOST_BITS_PER_LONG >= 64);
4009 dw2_asm_output_data (8, val1->v.val_int, NULL);
4010 break;
4011 case DW_OP_skip:
4012 case DW_OP_bra:
4014 int offset;
4016 gcc_assert (val1->val_class == dw_val_class_loc);
4017 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4019 dw2_asm_output_data (2, offset, NULL);
4021 break;
4022 #else
4023 case DW_OP_addr:
4024 case DW_OP_const2u:
4025 case DW_OP_const2s:
4026 case DW_OP_const4u:
4027 case DW_OP_const4s:
4028 case DW_OP_const8u:
4029 case DW_OP_const8s:
4030 case DW_OP_skip:
4031 case DW_OP_bra:
4032 /* We currently don't make any attempt to make sure these are
4033 aligned properly like we do for the main unwind info, so
4034 don't support emitting things larger than a byte if we're
4035 only doing unwinding. */
4036 gcc_unreachable ();
4037 #endif
4038 case DW_OP_const1u:
4039 case DW_OP_const1s:
4040 dw2_asm_output_data (1, val1->v.val_int, NULL);
4041 break;
4042 case DW_OP_constu:
4043 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4044 break;
4045 case DW_OP_consts:
4046 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4047 break;
4048 case DW_OP_pick:
4049 dw2_asm_output_data (1, val1->v.val_int, NULL);
4050 break;
4051 case DW_OP_plus_uconst:
4052 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4053 break;
4054 case DW_OP_breg0:
4055 case DW_OP_breg1:
4056 case DW_OP_breg2:
4057 case DW_OP_breg3:
4058 case DW_OP_breg4:
4059 case DW_OP_breg5:
4060 case DW_OP_breg6:
4061 case DW_OP_breg7:
4062 case DW_OP_breg8:
4063 case DW_OP_breg9:
4064 case DW_OP_breg10:
4065 case DW_OP_breg11:
4066 case DW_OP_breg12:
4067 case DW_OP_breg13:
4068 case DW_OP_breg14:
4069 case DW_OP_breg15:
4070 case DW_OP_breg16:
4071 case DW_OP_breg17:
4072 case DW_OP_breg18:
4073 case DW_OP_breg19:
4074 case DW_OP_breg20:
4075 case DW_OP_breg21:
4076 case DW_OP_breg22:
4077 case DW_OP_breg23:
4078 case DW_OP_breg24:
4079 case DW_OP_breg25:
4080 case DW_OP_breg26:
4081 case DW_OP_breg27:
4082 case DW_OP_breg28:
4083 case DW_OP_breg29:
4084 case DW_OP_breg30:
4085 case DW_OP_breg31:
4086 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4087 break;
4088 case DW_OP_regx:
4089 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4090 break;
4091 case DW_OP_fbreg:
4092 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4093 break;
4094 case DW_OP_bregx:
4095 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4096 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4097 break;
4098 case DW_OP_piece:
4099 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4100 break;
4101 case DW_OP_deref_size:
4102 case DW_OP_xderef_size:
4103 dw2_asm_output_data (1, val1->v.val_int, NULL);
4104 break;
4106 case INTERNAL_DW_OP_tls_addr:
4107 if (targetm.asm_out.output_dwarf_dtprel)
4109 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4110 DWARF2_ADDR_SIZE,
4111 val1->v.val_addr);
4112 fputc ('\n', asm_out_file);
4114 else
4115 gcc_unreachable ();
4116 break;
4118 default:
4119 /* Other codes have no operands. */
4120 break;
4124 /* Output a sequence of location operations. */
4126 static void
4127 output_loc_sequence (dw_loc_descr_ref loc)
4129 for (; loc != NULL; loc = loc->dw_loc_next)
4131 /* Output the opcode. */
4132 dw2_asm_output_data (1, loc->dw_loc_opc,
4133 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4135 /* Output the operand(s) (if any). */
4136 output_loc_operands (loc);
4140 /* Output location description stack opcode's operands (if any).
4141 The output is single bytes on a line, suitable for .cfi_escape. */
4143 static void
4144 output_loc_operands_raw (dw_loc_descr_ref loc)
4146 dw_val_ref val1 = &loc->dw_loc_oprnd1;
4147 dw_val_ref val2 = &loc->dw_loc_oprnd2;
4149 switch (loc->dw_loc_opc)
4151 case DW_OP_addr:
4152 /* We cannot output addresses in .cfi_escape, only bytes. */
4153 gcc_unreachable ();
4155 case DW_OP_const1u:
4156 case DW_OP_const1s:
4157 case DW_OP_pick:
4158 case DW_OP_deref_size:
4159 case DW_OP_xderef_size:
4160 fputc (',', asm_out_file);
4161 dw2_asm_output_data_raw (1, val1->v.val_int);
4162 break;
4164 case DW_OP_const2u:
4165 case DW_OP_const2s:
4166 fputc (',', asm_out_file);
4167 dw2_asm_output_data_raw (2, val1->v.val_int);
4168 break;
4170 case DW_OP_const4u:
4171 case DW_OP_const4s:
4172 fputc (',', asm_out_file);
4173 dw2_asm_output_data_raw (4, val1->v.val_int);
4174 break;
4176 case DW_OP_const8u:
4177 case DW_OP_const8s:
4178 gcc_assert (HOST_BITS_PER_LONG >= 64);
4179 fputc (',', asm_out_file);
4180 dw2_asm_output_data_raw (8, val1->v.val_int);
4181 break;
4183 case DW_OP_skip:
4184 case DW_OP_bra:
4186 int offset;
4188 gcc_assert (val1->val_class == dw_val_class_loc);
4189 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4191 fputc (',', asm_out_file);
4192 dw2_asm_output_data_raw (2, offset);
4194 break;
4196 case DW_OP_constu:
4197 case DW_OP_plus_uconst:
4198 case DW_OP_regx:
4199 case DW_OP_piece:
4200 fputc (',', asm_out_file);
4201 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4202 break;
4204 case DW_OP_consts:
4205 case DW_OP_breg0:
4206 case DW_OP_breg1:
4207 case DW_OP_breg2:
4208 case DW_OP_breg3:
4209 case DW_OP_breg4:
4210 case DW_OP_breg5:
4211 case DW_OP_breg6:
4212 case DW_OP_breg7:
4213 case DW_OP_breg8:
4214 case DW_OP_breg9:
4215 case DW_OP_breg10:
4216 case DW_OP_breg11:
4217 case DW_OP_breg12:
4218 case DW_OP_breg13:
4219 case DW_OP_breg14:
4220 case DW_OP_breg15:
4221 case DW_OP_breg16:
4222 case DW_OP_breg17:
4223 case DW_OP_breg18:
4224 case DW_OP_breg19:
4225 case DW_OP_breg20:
4226 case DW_OP_breg21:
4227 case DW_OP_breg22:
4228 case DW_OP_breg23:
4229 case DW_OP_breg24:
4230 case DW_OP_breg25:
4231 case DW_OP_breg26:
4232 case DW_OP_breg27:
4233 case DW_OP_breg28:
4234 case DW_OP_breg29:
4235 case DW_OP_breg30:
4236 case DW_OP_breg31:
4237 case DW_OP_fbreg:
4238 fputc (',', asm_out_file);
4239 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4240 break;
4242 case DW_OP_bregx:
4243 fputc (',', asm_out_file);
4244 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4245 fputc (',', asm_out_file);
4246 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4247 break;
4249 case INTERNAL_DW_OP_tls_addr:
4250 gcc_unreachable ();
4252 default:
4253 /* Other codes have no operands. */
4254 break;
4258 static void
4259 output_loc_sequence_raw (dw_loc_descr_ref loc)
4261 while (1)
4263 /* Output the opcode. */
4264 fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4265 output_loc_operands_raw (loc);
4267 if (!loc->dw_loc_next)
4268 break;
4269 loc = loc->dw_loc_next;
4271 fputc (',', asm_out_file);
4275 /* This routine will generate the correct assembly data for a location
4276 description based on a cfi entry with a complex address. */
4278 static void
4279 output_cfa_loc (dw_cfi_ref cfi)
4281 dw_loc_descr_ref loc;
4282 unsigned long size;
4284 if (cfi->dw_cfi_opc == DW_CFA_expression)
4285 dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4287 /* Output the size of the block. */
4288 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4289 size = size_of_locs (loc);
4290 dw2_asm_output_data_uleb128 (size, NULL);
4292 /* Now output the operations themselves. */
4293 output_loc_sequence (loc);
4296 /* Similar, but used for .cfi_escape. */
4298 static void
4299 output_cfa_loc_raw (dw_cfi_ref cfi)
4301 dw_loc_descr_ref loc;
4302 unsigned long size;
4304 if (cfi->dw_cfi_opc == DW_CFA_expression)
4305 fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4307 /* Output the size of the block. */
4308 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4309 size = size_of_locs (loc);
4310 dw2_asm_output_data_uleb128_raw (size);
4311 fputc (',', asm_out_file);
4313 /* Now output the operations themselves. */
4314 output_loc_sequence_raw (loc);
4317 /* This function builds a dwarf location descriptor sequence from a
4318 dw_cfa_location, adding the given OFFSET to the result of the
4319 expression. */
4321 static struct dw_loc_descr_struct *
4322 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4324 struct dw_loc_descr_struct *head, *tmp;
4326 offset += cfa->offset;
4328 if (cfa->indirect)
4330 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4331 head->dw_loc_oprnd1.val_class = dw_val_class_const;
4332 tmp = new_loc_descr (DW_OP_deref, 0, 0);
4333 add_loc_descr (&head, tmp);
4334 if (offset != 0)
4336 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4337 add_loc_descr (&head, tmp);
4340 else
4341 head = new_reg_loc_descr (cfa->reg, offset);
4343 return head;
4346 /* This function builds a dwarf location descriptor sequence for
4347 the address at OFFSET from the CFA when stack is aligned to
4348 ALIGNMENT byte. */
4350 static struct dw_loc_descr_struct *
4351 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4353 struct dw_loc_descr_struct *head;
4354 unsigned int dwarf_fp
4355 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4357 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
4358 if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4360 head = new_reg_loc_descr (dwarf_fp, 0);
4361 add_loc_descr (&head, int_loc_descriptor (alignment));
4362 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4364 add_loc_descr (&head, int_loc_descriptor (offset));
4365 add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
4367 else
4368 head = new_reg_loc_descr (dwarf_fp, offset);
4369 return head;
4372 /* This function fills in aa dw_cfa_location structure from a dwarf location
4373 descriptor sequence. */
4375 static void
4376 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4378 struct dw_loc_descr_struct *ptr;
4379 cfa->offset = 0;
4380 cfa->base_offset = 0;
4381 cfa->indirect = 0;
4382 cfa->reg = -1;
4384 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4386 enum dwarf_location_atom op = ptr->dw_loc_opc;
4388 switch (op)
4390 case DW_OP_reg0:
4391 case DW_OP_reg1:
4392 case DW_OP_reg2:
4393 case DW_OP_reg3:
4394 case DW_OP_reg4:
4395 case DW_OP_reg5:
4396 case DW_OP_reg6:
4397 case DW_OP_reg7:
4398 case DW_OP_reg8:
4399 case DW_OP_reg9:
4400 case DW_OP_reg10:
4401 case DW_OP_reg11:
4402 case DW_OP_reg12:
4403 case DW_OP_reg13:
4404 case DW_OP_reg14:
4405 case DW_OP_reg15:
4406 case DW_OP_reg16:
4407 case DW_OP_reg17:
4408 case DW_OP_reg18:
4409 case DW_OP_reg19:
4410 case DW_OP_reg20:
4411 case DW_OP_reg21:
4412 case DW_OP_reg22:
4413 case DW_OP_reg23:
4414 case DW_OP_reg24:
4415 case DW_OP_reg25:
4416 case DW_OP_reg26:
4417 case DW_OP_reg27:
4418 case DW_OP_reg28:
4419 case DW_OP_reg29:
4420 case DW_OP_reg30:
4421 case DW_OP_reg31:
4422 cfa->reg = op - DW_OP_reg0;
4423 break;
4424 case DW_OP_regx:
4425 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4426 break;
4427 case DW_OP_breg0:
4428 case DW_OP_breg1:
4429 case DW_OP_breg2:
4430 case DW_OP_breg3:
4431 case DW_OP_breg4:
4432 case DW_OP_breg5:
4433 case DW_OP_breg6:
4434 case DW_OP_breg7:
4435 case DW_OP_breg8:
4436 case DW_OP_breg9:
4437 case DW_OP_breg10:
4438 case DW_OP_breg11:
4439 case DW_OP_breg12:
4440 case DW_OP_breg13:
4441 case DW_OP_breg14:
4442 case DW_OP_breg15:
4443 case DW_OP_breg16:
4444 case DW_OP_breg17:
4445 case DW_OP_breg18:
4446 case DW_OP_breg19:
4447 case DW_OP_breg20:
4448 case DW_OP_breg21:
4449 case DW_OP_breg22:
4450 case DW_OP_breg23:
4451 case DW_OP_breg24:
4452 case DW_OP_breg25:
4453 case DW_OP_breg26:
4454 case DW_OP_breg27:
4455 case DW_OP_breg28:
4456 case DW_OP_breg29:
4457 case DW_OP_breg30:
4458 case DW_OP_breg31:
4459 cfa->reg = op - DW_OP_breg0;
4460 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4461 break;
4462 case DW_OP_bregx:
4463 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4464 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4465 break;
4466 case DW_OP_deref:
4467 cfa->indirect = 1;
4468 break;
4469 case DW_OP_plus_uconst:
4470 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4471 break;
4472 default:
4473 internal_error ("DW_LOC_OP %s not implemented",
4474 dwarf_stack_op_name (ptr->dw_loc_opc));
4478 #endif /* .debug_frame support */
4480 /* And now, the support for symbolic debugging information. */
4481 #ifdef DWARF2_DEBUGGING_INFO
4483 /* .debug_str support. */
4484 static int output_indirect_string (void **, void *);
4486 static void dwarf2out_init (const char *);
4487 static void dwarf2out_finish (const char *);
4488 static void dwarf2out_define (unsigned int, const char *);
4489 static void dwarf2out_undef (unsigned int, const char *);
4490 static void dwarf2out_start_source_file (unsigned, const char *);
4491 static void dwarf2out_end_source_file (unsigned);
4492 static void dwarf2out_begin_block (unsigned, unsigned);
4493 static void dwarf2out_end_block (unsigned, unsigned);
4494 static bool dwarf2out_ignore_block (const_tree);
4495 static void dwarf2out_global_decl (tree);
4496 static void dwarf2out_type_decl (tree, int);
4497 static void dwarf2out_imported_module_or_decl (tree, tree);
4498 static void dwarf2out_abstract_function (tree);
4499 static void dwarf2out_var_location (rtx);
4500 static void dwarf2out_begin_function (tree);
4502 /* The debug hooks structure. */
4504 const struct gcc_debug_hooks dwarf2_debug_hooks =
4506 dwarf2out_init,
4507 dwarf2out_finish,
4508 dwarf2out_define,
4509 dwarf2out_undef,
4510 dwarf2out_start_source_file,
4511 dwarf2out_end_source_file,
4512 dwarf2out_begin_block,
4513 dwarf2out_end_block,
4514 dwarf2out_ignore_block,
4515 dwarf2out_source_line,
4516 dwarf2out_begin_prologue,
4517 debug_nothing_int_charstar, /* end_prologue */
4518 dwarf2out_end_epilogue,
4519 dwarf2out_begin_function,
4520 debug_nothing_int, /* end_function */
4521 dwarf2out_decl, /* function_decl */
4522 dwarf2out_global_decl,
4523 dwarf2out_type_decl, /* type_decl */
4524 dwarf2out_imported_module_or_decl,
4525 debug_nothing_tree, /* deferred_inline_function */
4526 /* The DWARF 2 backend tries to reduce debugging bloat by not
4527 emitting the abstract description of inline functions until
4528 something tries to reference them. */
4529 dwarf2out_abstract_function, /* outlining_inline_function */
4530 debug_nothing_rtx, /* label */
4531 debug_nothing_int, /* handle_pch */
4532 dwarf2out_var_location,
4533 dwarf2out_switch_text_section,
4534 1 /* start_end_main_source_file */
4536 #endif
4538 /* NOTE: In the comments in this file, many references are made to
4539 "Debugging Information Entries". This term is abbreviated as `DIE'
4540 throughout the remainder of this file. */
4542 /* An internal representation of the DWARF output is built, and then
4543 walked to generate the DWARF debugging info. The walk of the internal
4544 representation is done after the entire program has been compiled.
4545 The types below are used to describe the internal representation. */
4547 /* Various DIE's use offsets relative to the beginning of the
4548 .debug_info section to refer to each other. */
4550 typedef long int dw_offset;
4552 /* Define typedefs here to avoid circular dependencies. */
4554 typedef struct dw_attr_struct *dw_attr_ref;
4555 typedef struct dw_line_info_struct *dw_line_info_ref;
4556 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4557 typedef struct pubname_struct *pubname_ref;
4558 typedef struct dw_ranges_struct *dw_ranges_ref;
4559 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4561 /* Each entry in the line_info_table maintains the file and
4562 line number associated with the label generated for that
4563 entry. The label gives the PC value associated with
4564 the line number entry. */
4566 typedef struct dw_line_info_struct GTY(())
4568 unsigned long dw_file_num;
4569 unsigned long dw_line_num;
4571 dw_line_info_entry;
4573 /* Line information for functions in separate sections; each one gets its
4574 own sequence. */
4575 typedef struct dw_separate_line_info_struct GTY(())
4577 unsigned long dw_file_num;
4578 unsigned long dw_line_num;
4579 unsigned long function;
4581 dw_separate_line_info_entry;
4583 /* Each DIE attribute has a field specifying the attribute kind,
4584 a link to the next attribute in the chain, and an attribute value.
4585 Attributes are typically linked below the DIE they modify. */
4587 typedef struct dw_attr_struct GTY(())
4589 enum dwarf_attribute dw_attr;
4590 dw_val_node dw_attr_val;
4592 dw_attr_node;
4594 DEF_VEC_O(dw_attr_node);
4595 DEF_VEC_ALLOC_O(dw_attr_node,gc);
4597 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
4598 The children of each node form a circular list linked by
4599 die_sib. die_child points to the node *before* the "first" child node. */
4601 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
4603 enum dwarf_tag die_tag;
4604 char *die_symbol;
4605 VEC(dw_attr_node,gc) * die_attr;
4606 dw_die_ref die_parent;
4607 dw_die_ref die_child;
4608 dw_die_ref die_sib;
4609 dw_die_ref die_definition; /* ref from a specification to its definition */
4610 dw_offset die_offset;
4611 unsigned long die_abbrev;
4612 int die_mark;
4613 /* Die is used and must not be pruned as unused. */
4614 int die_perennial_p;
4615 unsigned int decl_id;
4617 die_node;
4619 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
4620 #define FOR_EACH_CHILD(die, c, expr) do { \
4621 c = die->die_child; \
4622 if (c) do { \
4623 c = c->die_sib; \
4624 expr; \
4625 } while (c != die->die_child); \
4626 } while (0)
4628 /* The pubname structure */
4630 typedef struct pubname_struct GTY(())
4632 dw_die_ref die;
4633 const char *name;
4635 pubname_entry;
4637 DEF_VEC_O(pubname_entry);
4638 DEF_VEC_ALLOC_O(pubname_entry, gc);
4640 struct dw_ranges_struct GTY(())
4642 /* If this is positive, it's a block number, otherwise it's a
4643 bitwise-negated index into dw_ranges_by_label. */
4644 int num;
4647 struct dw_ranges_by_label_struct GTY(())
4649 const char *begin;
4650 const char *end;
4653 /* The limbo die list structure. */
4654 typedef struct limbo_die_struct GTY(())
4656 dw_die_ref die;
4657 tree created_for;
4658 struct limbo_die_struct *next;
4660 limbo_die_node;
4662 /* How to start an assembler comment. */
4663 #ifndef ASM_COMMENT_START
4664 #define ASM_COMMENT_START ";#"
4665 #endif
4667 /* Define a macro which returns nonzero for a TYPE_DECL which was
4668 implicitly generated for a tagged type.
4670 Note that unlike the gcc front end (which generates a NULL named
4671 TYPE_DECL node for each complete tagged type, each array type, and
4672 each function type node created) the g++ front end generates a
4673 _named_ TYPE_DECL node for each tagged type node created.
4674 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
4675 generate a DW_TAG_typedef DIE for them. */
4677 #define TYPE_DECL_IS_STUB(decl) \
4678 (DECL_NAME (decl) == NULL_TREE \
4679 || (DECL_ARTIFICIAL (decl) \
4680 && is_tagged_type (TREE_TYPE (decl)) \
4681 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
4682 /* This is necessary for stub decls that \
4683 appear in nested inline functions. */ \
4684 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
4685 && (decl_ultimate_origin (decl) \
4686 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
4688 /* Information concerning the compilation unit's programming
4689 language, and compiler version. */
4691 /* Fixed size portion of the DWARF compilation unit header. */
4692 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
4693 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
4695 /* Fixed size portion of public names info. */
4696 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
4698 /* Fixed size portion of the address range info. */
4699 #define DWARF_ARANGES_HEADER_SIZE \
4700 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4701 DWARF2_ADDR_SIZE * 2) \
4702 - DWARF_INITIAL_LENGTH_SIZE)
4704 /* Size of padding portion in the address range info. It must be
4705 aligned to twice the pointer size. */
4706 #define DWARF_ARANGES_PAD_SIZE \
4707 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4708 DWARF2_ADDR_SIZE * 2) \
4709 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
4711 /* Use assembler line directives if available. */
4712 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
4713 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
4714 #define DWARF2_ASM_LINE_DEBUG_INFO 1
4715 #else
4716 #define DWARF2_ASM_LINE_DEBUG_INFO 0
4717 #endif
4718 #endif
4720 /* Minimum line offset in a special line info. opcode.
4721 This value was chosen to give a reasonable range of values. */
4722 #define DWARF_LINE_BASE -10
4724 /* First special line opcode - leave room for the standard opcodes. */
4725 #define DWARF_LINE_OPCODE_BASE 10
4727 /* Range of line offsets in a special line info. opcode. */
4728 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
4730 /* Flag that indicates the initial value of the is_stmt_start flag.
4731 In the present implementation, we do not mark any lines as
4732 the beginning of a source statement, because that information
4733 is not made available by the GCC front-end. */
4734 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
4736 #ifdef DWARF2_DEBUGGING_INFO
4737 /* This location is used by calc_die_sizes() to keep track
4738 the offset of each DIE within the .debug_info section. */
4739 static unsigned long next_die_offset;
4740 #endif
4742 /* Record the root of the DIE's built for the current compilation unit. */
4743 static GTY(()) dw_die_ref comp_unit_die;
4745 /* A list of DIEs with a NULL parent waiting to be relocated. */
4746 static GTY(()) limbo_die_node *limbo_die_list;
4748 /* Filenames referenced by this compilation unit. */
4749 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
4751 /* A hash table of references to DIE's that describe declarations.
4752 The key is a DECL_UID() which is a unique number identifying each decl. */
4753 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4755 /* Node of the variable location list. */
4756 struct var_loc_node GTY ((chain_next ("%h.next")))
4758 rtx GTY (()) var_loc_note;
4759 const char * GTY (()) label;
4760 const char * GTY (()) section_label;
4761 struct var_loc_node * GTY (()) next;
4764 /* Variable location list. */
4765 struct var_loc_list_def GTY (())
4767 struct var_loc_node * GTY (()) first;
4769 /* Do not mark the last element of the chained list because
4770 it is marked through the chain. */
4771 struct var_loc_node * GTY ((skip ("%h"))) last;
4773 /* DECL_UID of the variable decl. */
4774 unsigned int decl_id;
4776 typedef struct var_loc_list_def var_loc_list;
4779 /* Table of decl location linked lists. */
4780 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4782 /* A pointer to the base of a list of references to DIE's that
4783 are uniquely identified by their tag, presence/absence of
4784 children DIE's, and list of attribute/value pairs. */
4785 static GTY((length ("abbrev_die_table_allocated")))
4786 dw_die_ref *abbrev_die_table;
4788 /* Number of elements currently allocated for abbrev_die_table. */
4789 static GTY(()) unsigned abbrev_die_table_allocated;
4791 /* Number of elements in type_die_table currently in use. */
4792 static GTY(()) unsigned abbrev_die_table_in_use;
4794 /* Size (in elements) of increments by which we may expand the
4795 abbrev_die_table. */
4796 #define ABBREV_DIE_TABLE_INCREMENT 256
4798 /* A pointer to the base of a table that contains line information
4799 for each source code line in .text in the compilation unit. */
4800 static GTY((length ("line_info_table_allocated")))
4801 dw_line_info_ref line_info_table;
4803 /* Number of elements currently allocated for line_info_table. */
4804 static GTY(()) unsigned line_info_table_allocated;
4806 /* Number of elements in line_info_table currently in use. */
4807 static GTY(()) unsigned line_info_table_in_use;
4809 /* A pointer to the base of a table that contains line information
4810 for each source code line outside of .text in the compilation unit. */
4811 static GTY ((length ("separate_line_info_table_allocated")))
4812 dw_separate_line_info_ref separate_line_info_table;
4814 /* Number of elements currently allocated for separate_line_info_table. */
4815 static GTY(()) unsigned separate_line_info_table_allocated;
4817 /* Number of elements in separate_line_info_table currently in use. */
4818 static GTY(()) unsigned separate_line_info_table_in_use;
4820 /* Size (in elements) of increments by which we may expand the
4821 line_info_table. */
4822 #define LINE_INFO_TABLE_INCREMENT 1024
4824 /* A pointer to the base of a table that contains a list of publicly
4825 accessible names. */
4826 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
4828 /* A pointer to the base of a table that contains a list of publicly
4829 accessible types. */
4830 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4832 /* Array of dies for which we should generate .debug_arange info. */
4833 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4835 /* Number of elements currently allocated for arange_table. */
4836 static GTY(()) unsigned arange_table_allocated;
4838 /* Number of elements in arange_table currently in use. */
4839 static GTY(()) unsigned arange_table_in_use;
4841 /* Size (in elements) of increments by which we may expand the
4842 arange_table. */
4843 #define ARANGE_TABLE_INCREMENT 64
4845 /* Array of dies for which we should generate .debug_ranges info. */
4846 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4848 /* Number of elements currently allocated for ranges_table. */
4849 static GTY(()) unsigned ranges_table_allocated;
4851 /* Number of elements in ranges_table currently in use. */
4852 static GTY(()) unsigned ranges_table_in_use;
4854 /* Array of pairs of labels referenced in ranges_table. */
4855 static GTY ((length ("ranges_by_label_allocated")))
4856 dw_ranges_by_label_ref ranges_by_label;
4858 /* Number of elements currently allocated for ranges_by_label. */
4859 static GTY(()) unsigned ranges_by_label_allocated;
4861 /* Number of elements in ranges_by_label currently in use. */
4862 static GTY(()) unsigned ranges_by_label_in_use;
4864 /* Size (in elements) of increments by which we may expand the
4865 ranges_table. */
4866 #define RANGES_TABLE_INCREMENT 64
4868 /* Whether we have location lists that need outputting */
4869 static GTY(()) bool have_location_lists;
4871 /* Unique label counter. */
4872 static GTY(()) unsigned int loclabel_num;
4874 #ifdef DWARF2_DEBUGGING_INFO
4875 /* Record whether the function being analyzed contains inlined functions. */
4876 static int current_function_has_inlines;
4877 #endif
4878 #if 0 && defined (MIPS_DEBUGGING_INFO)
4879 static int comp_unit_has_inlines;
4880 #endif
4882 /* The last file entry emitted by maybe_emit_file(). */
4883 static GTY(()) struct dwarf_file_data * last_emitted_file;
4885 /* Number of internal labels generated by gen_internal_sym(). */
4886 static GTY(()) int label_num;
4888 /* Cached result of previous call to lookup_filename. */
4889 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4891 #ifdef DWARF2_DEBUGGING_INFO
4893 /* Offset from the "steady-state frame pointer" to the frame base,
4894 within the current function. */
4895 static HOST_WIDE_INT frame_pointer_fb_offset;
4897 /* Forward declarations for functions defined in this file. */
4899 static int is_pseudo_reg (const_rtx);
4900 static tree type_main_variant (tree);
4901 static int is_tagged_type (const_tree);
4902 static const char *dwarf_tag_name (unsigned);
4903 static const char *dwarf_attr_name (unsigned);
4904 static const char *dwarf_form_name (unsigned);
4905 static tree decl_ultimate_origin (const_tree);
4906 static tree block_ultimate_origin (const_tree);
4907 static tree decl_class_context (tree);
4908 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4909 static inline enum dw_val_class AT_class (dw_attr_ref);
4910 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4911 static inline unsigned AT_flag (dw_attr_ref);
4912 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4913 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4914 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4915 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4916 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4917 unsigned long);
4918 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4919 unsigned int, unsigned char *);
4920 static hashval_t debug_str_do_hash (const void *);
4921 static int debug_str_eq (const void *, const void *);
4922 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4923 static inline const char *AT_string (dw_attr_ref);
4924 static int AT_string_form (dw_attr_ref);
4925 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4926 static void add_AT_specification (dw_die_ref, dw_die_ref);
4927 static inline dw_die_ref AT_ref (dw_attr_ref);
4928 static inline int AT_ref_external (dw_attr_ref);
4929 static inline void set_AT_ref_external (dw_attr_ref, int);
4930 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4931 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4932 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4933 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4934 dw_loc_list_ref);
4935 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4936 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4937 static inline rtx AT_addr (dw_attr_ref);
4938 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4939 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4940 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4941 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4942 unsigned HOST_WIDE_INT);
4943 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4944 unsigned long);
4945 static inline const char *AT_lbl (dw_attr_ref);
4946 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4947 static const char *get_AT_low_pc (dw_die_ref);
4948 static const char *get_AT_hi_pc (dw_die_ref);
4949 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4950 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4951 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4952 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4953 static bool is_c_family (void);
4954 static bool is_cxx (void);
4955 static bool is_java (void);
4956 static bool is_fortran (void);
4957 static bool is_ada (void);
4958 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4959 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4960 static void add_child_die (dw_die_ref, dw_die_ref);
4961 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4962 static dw_die_ref lookup_type_die (tree);
4963 static void equate_type_number_to_die (tree, dw_die_ref);
4964 static hashval_t decl_die_table_hash (const void *);
4965 static int decl_die_table_eq (const void *, const void *);
4966 static dw_die_ref lookup_decl_die (tree);
4967 static hashval_t decl_loc_table_hash (const void *);
4968 static int decl_loc_table_eq (const void *, const void *);
4969 static var_loc_list *lookup_decl_loc (const_tree);
4970 static void equate_decl_number_to_die (tree, dw_die_ref);
4971 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4972 static void print_spaces (FILE *);
4973 static void print_die (dw_die_ref, FILE *);
4974 static void print_dwarf_line_table (FILE *);
4975 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4976 static dw_die_ref pop_compile_unit (dw_die_ref);
4977 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4978 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4979 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4980 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4981 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4982 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4983 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4984 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4985 static void compute_section_prefix (dw_die_ref);
4986 static int is_type_die (dw_die_ref);
4987 static int is_comdat_die (dw_die_ref);
4988 static int is_symbol_die (dw_die_ref);
4989 static void assign_symbol_names (dw_die_ref);
4990 static void break_out_includes (dw_die_ref);
4991 static hashval_t htab_cu_hash (const void *);
4992 static int htab_cu_eq (const void *, const void *);
4993 static void htab_cu_del (void *);
4994 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4995 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4996 static void add_sibling_attributes (dw_die_ref);
4997 static void build_abbrev_table (dw_die_ref);
4998 static void output_location_lists (dw_die_ref);
4999 static int constant_size (long unsigned);
5000 static unsigned long size_of_die (dw_die_ref);
5001 static void calc_die_sizes (dw_die_ref);
5002 static void mark_dies (dw_die_ref);
5003 static void unmark_dies (dw_die_ref);
5004 static void unmark_all_dies (dw_die_ref);
5005 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5006 static unsigned long size_of_aranges (void);
5007 static enum dwarf_form value_format (dw_attr_ref);
5008 static void output_value_format (dw_attr_ref);
5009 static void output_abbrev_section (void);
5010 static void output_die_symbol (dw_die_ref);
5011 static void output_die (dw_die_ref);
5012 static void output_compilation_unit_header (void);
5013 static void output_comp_unit (dw_die_ref, int);
5014 static const char *dwarf2_name (tree, int);
5015 static void add_pubname (tree, dw_die_ref);
5016 static void add_pubname_string (const char *, dw_die_ref);
5017 static void add_pubtype (tree, dw_die_ref);
5018 static void output_pubnames (VEC (pubname_entry,gc) *);
5019 static void add_arange (tree, dw_die_ref);
5020 static void output_aranges (void);
5021 static unsigned int add_ranges_num (int);
5022 static unsigned int add_ranges (const_tree);
5023 static unsigned int add_ranges_by_labels (const char *, const char *);
5024 static void output_ranges (void);
5025 static void output_line_info (void);
5026 static void output_file_names (void);
5027 static dw_die_ref base_type_die (tree);
5028 static int is_base_type (tree);
5029 static bool is_subrange_type (const_tree);
5030 static dw_die_ref subrange_type_die (tree, dw_die_ref);
5031 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5032 static int type_is_enum (const_tree);
5033 static unsigned int dbx_reg_number (const_rtx);
5034 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5035 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5036 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5037 enum var_init_status);
5038 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5039 enum var_init_status);
5040 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5041 enum var_init_status);
5042 static int is_based_loc (const_rtx);
5043 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5044 enum var_init_status);
5045 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5046 enum var_init_status);
5047 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5048 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5049 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5050 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5051 static tree field_type (const_tree);
5052 static unsigned int simple_type_align_in_bits (const_tree);
5053 static unsigned int simple_decl_align_in_bits (const_tree);
5054 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5055 static HOST_WIDE_INT field_byte_offset (const_tree);
5056 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5057 dw_loc_descr_ref);
5058 static void add_data_member_location_attribute (dw_die_ref, tree);
5059 static void add_const_value_attribute (dw_die_ref, rtx);
5060 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5061 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5062 static void insert_float (const_rtx, unsigned char *);
5063 static rtx rtl_for_decl_location (tree);
5064 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5065 enum dwarf_attribute);
5066 static void tree_add_const_value_attribute (dw_die_ref, tree);
5067 static void add_name_attribute (dw_die_ref, const char *);
5068 static void add_comp_dir_attribute (dw_die_ref);
5069 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5070 static void add_subscript_info (dw_die_ref, tree, bool);
5071 static void add_byte_size_attribute (dw_die_ref, tree);
5072 static void add_bit_offset_attribute (dw_die_ref, tree);
5073 static void add_bit_size_attribute (dw_die_ref, tree);
5074 static void add_prototyped_attribute (dw_die_ref, tree);
5075 static void add_abstract_origin_attribute (dw_die_ref, tree);
5076 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5077 static void add_src_coords_attributes (dw_die_ref, tree);
5078 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5079 static void push_decl_scope (tree);
5080 static void pop_decl_scope (void);
5081 static dw_die_ref scope_die_for (tree, dw_die_ref);
5082 static inline int local_scope_p (dw_die_ref);
5083 static inline int class_or_namespace_scope_p (dw_die_ref);
5084 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5085 static void add_calling_convention_attribute (dw_die_ref, tree);
5086 static const char *type_tag (const_tree);
5087 static tree member_declared_type (const_tree);
5088 #if 0
5089 static const char *decl_start_label (tree);
5090 #endif
5091 static void gen_array_type_die (tree, dw_die_ref);
5092 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5093 #if 0
5094 static void gen_entry_point_die (tree, dw_die_ref);
5095 #endif
5096 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
5097 static void gen_inlined_structure_type_die (tree, dw_die_ref);
5098 static void gen_inlined_union_type_die (tree, dw_die_ref);
5099 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5100 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
5101 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5102 static void gen_formal_types_die (tree, dw_die_ref);
5103 static void gen_subprogram_die (tree, dw_die_ref);
5104 static void gen_variable_die (tree, dw_die_ref);
5105 static void gen_label_die (tree, dw_die_ref);
5106 static void gen_lexical_block_die (tree, dw_die_ref, int);
5107 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5108 static void gen_field_die (tree, dw_die_ref);
5109 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5110 static dw_die_ref gen_compile_unit_die (const char *);
5111 static void gen_inheritance_die (tree, tree, dw_die_ref);
5112 static void gen_member_die (tree, dw_die_ref);
5113 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5114 enum debug_info_usage);
5115 static void gen_subroutine_type_die (tree, dw_die_ref);
5116 static void gen_typedef_die (tree, dw_die_ref);
5117 static void gen_type_die (tree, dw_die_ref);
5118 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
5119 static void gen_block_die (tree, dw_die_ref, int);
5120 static void decls_for_scope (tree, dw_die_ref, int);
5121 static int is_redundant_typedef (const_tree);
5122 static void gen_namespace_die (tree);
5123 static void gen_decl_die (tree, dw_die_ref);
5124 static dw_die_ref force_decl_die (tree);
5125 static dw_die_ref force_type_die (tree);
5126 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5127 static void declare_in_namespace (tree, dw_die_ref);
5128 static struct dwarf_file_data * lookup_filename (const char *);
5129 static void retry_incomplete_types (void);
5130 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5131 static void splice_child_die (dw_die_ref, dw_die_ref);
5132 static int file_info_cmp (const void *, const void *);
5133 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5134 const char *, const char *, unsigned);
5135 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5136 const char *, const char *,
5137 const char *);
5138 static void output_loc_list (dw_loc_list_ref);
5139 static char *gen_internal_sym (const char *);
5141 static void prune_unmark_dies (dw_die_ref);
5142 static void prune_unused_types_mark (dw_die_ref, int);
5143 static void prune_unused_types_walk (dw_die_ref);
5144 static void prune_unused_types_walk_attribs (dw_die_ref);
5145 static void prune_unused_types_prune (dw_die_ref);
5146 static void prune_unused_types (void);
5147 static int maybe_emit_file (struct dwarf_file_data *fd);
5149 /* Section names used to hold DWARF debugging information. */
5150 #ifndef DEBUG_INFO_SECTION
5151 #define DEBUG_INFO_SECTION ".debug_info"
5152 #endif
5153 #ifndef DEBUG_ABBREV_SECTION
5154 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
5155 #endif
5156 #ifndef DEBUG_ARANGES_SECTION
5157 #define DEBUG_ARANGES_SECTION ".debug_aranges"
5158 #endif
5159 #ifndef DEBUG_MACINFO_SECTION
5160 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
5161 #endif
5162 #ifndef DEBUG_LINE_SECTION
5163 #define DEBUG_LINE_SECTION ".debug_line"
5164 #endif
5165 #ifndef DEBUG_LOC_SECTION
5166 #define DEBUG_LOC_SECTION ".debug_loc"
5167 #endif
5168 #ifndef DEBUG_PUBNAMES_SECTION
5169 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
5170 #endif
5171 #ifndef DEBUG_STR_SECTION
5172 #define DEBUG_STR_SECTION ".debug_str"
5173 #endif
5174 #ifndef DEBUG_RANGES_SECTION
5175 #define DEBUG_RANGES_SECTION ".debug_ranges"
5176 #endif
5178 /* Standard ELF section names for compiled code and data. */
5179 #ifndef TEXT_SECTION_NAME
5180 #define TEXT_SECTION_NAME ".text"
5181 #endif
5183 /* Section flags for .debug_str section. */
5184 #define DEBUG_STR_SECTION_FLAGS \
5185 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
5186 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
5187 : SECTION_DEBUG)
5189 /* Labels we insert at beginning sections we can reference instead of
5190 the section names themselves. */
5192 #ifndef TEXT_SECTION_LABEL
5193 #define TEXT_SECTION_LABEL "Ltext"
5194 #endif
5195 #ifndef COLD_TEXT_SECTION_LABEL
5196 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
5197 #endif
5198 #ifndef DEBUG_LINE_SECTION_LABEL
5199 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
5200 #endif
5201 #ifndef DEBUG_INFO_SECTION_LABEL
5202 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
5203 #endif
5204 #ifndef DEBUG_ABBREV_SECTION_LABEL
5205 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
5206 #endif
5207 #ifndef DEBUG_LOC_SECTION_LABEL
5208 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
5209 #endif
5210 #ifndef DEBUG_RANGES_SECTION_LABEL
5211 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
5212 #endif
5213 #ifndef DEBUG_MACINFO_SECTION_LABEL
5214 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
5215 #endif
5217 /* Definitions of defaults for formats and names of various special
5218 (artificial) labels which may be generated within this file (when the -g
5219 options is used and DWARF2_DEBUGGING_INFO is in effect.
5220 If necessary, these may be overridden from within the tm.h file, but
5221 typically, overriding these defaults is unnecessary. */
5223 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5224 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5225 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5226 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5227 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5228 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5229 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5230 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5231 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5232 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5234 #ifndef TEXT_END_LABEL
5235 #define TEXT_END_LABEL "Letext"
5236 #endif
5237 #ifndef COLD_END_LABEL
5238 #define COLD_END_LABEL "Letext_cold"
5239 #endif
5240 #ifndef BLOCK_BEGIN_LABEL
5241 #define BLOCK_BEGIN_LABEL "LBB"
5242 #endif
5243 #ifndef BLOCK_END_LABEL
5244 #define BLOCK_END_LABEL "LBE"
5245 #endif
5246 #ifndef LINE_CODE_LABEL
5247 #define LINE_CODE_LABEL "LM"
5248 #endif
5249 #ifndef SEPARATE_LINE_CODE_LABEL
5250 #define SEPARATE_LINE_CODE_LABEL "LSM"
5251 #endif
5254 /* We allow a language front-end to designate a function that is to be
5255 called to "demangle" any name before it is put into a DIE. */
5257 static const char *(*demangle_name_func) (const char *);
5259 void
5260 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5262 demangle_name_func = func;
5265 /* Test if rtl node points to a pseudo register. */
5267 static inline int
5268 is_pseudo_reg (const_rtx rtl)
5270 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5271 || (GET_CODE (rtl) == SUBREG
5272 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5275 /* Return a reference to a type, with its const and volatile qualifiers
5276 removed. */
5278 static inline tree
5279 type_main_variant (tree type)
5281 type = TYPE_MAIN_VARIANT (type);
5283 /* ??? There really should be only one main variant among any group of
5284 variants of a given type (and all of the MAIN_VARIANT values for all
5285 members of the group should point to that one type) but sometimes the C
5286 front-end messes this up for array types, so we work around that bug
5287 here. */
5288 if (TREE_CODE (type) == ARRAY_TYPE)
5289 while (type != TYPE_MAIN_VARIANT (type))
5290 type = TYPE_MAIN_VARIANT (type);
5292 return type;
5295 /* Return nonzero if the given type node represents a tagged type. */
5297 static inline int
5298 is_tagged_type (const_tree type)
5300 enum tree_code code = TREE_CODE (type);
5302 return (code == RECORD_TYPE || code == UNION_TYPE
5303 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5306 /* Convert a DIE tag into its string name. */
5308 static const char *
5309 dwarf_tag_name (unsigned int tag)
5311 switch (tag)
5313 case DW_TAG_padding:
5314 return "DW_TAG_padding";
5315 case DW_TAG_array_type:
5316 return "DW_TAG_array_type";
5317 case DW_TAG_class_type:
5318 return "DW_TAG_class_type";
5319 case DW_TAG_entry_point:
5320 return "DW_TAG_entry_point";
5321 case DW_TAG_enumeration_type:
5322 return "DW_TAG_enumeration_type";
5323 case DW_TAG_formal_parameter:
5324 return "DW_TAG_formal_parameter";
5325 case DW_TAG_imported_declaration:
5326 return "DW_TAG_imported_declaration";
5327 case DW_TAG_label:
5328 return "DW_TAG_label";
5329 case DW_TAG_lexical_block:
5330 return "DW_TAG_lexical_block";
5331 case DW_TAG_member:
5332 return "DW_TAG_member";
5333 case DW_TAG_pointer_type:
5334 return "DW_TAG_pointer_type";
5335 case DW_TAG_reference_type:
5336 return "DW_TAG_reference_type";
5337 case DW_TAG_compile_unit:
5338 return "DW_TAG_compile_unit";
5339 case DW_TAG_string_type:
5340 return "DW_TAG_string_type";
5341 case DW_TAG_structure_type:
5342 return "DW_TAG_structure_type";
5343 case DW_TAG_subroutine_type:
5344 return "DW_TAG_subroutine_type";
5345 case DW_TAG_typedef:
5346 return "DW_TAG_typedef";
5347 case DW_TAG_union_type:
5348 return "DW_TAG_union_type";
5349 case DW_TAG_unspecified_parameters:
5350 return "DW_TAG_unspecified_parameters";
5351 case DW_TAG_variant:
5352 return "DW_TAG_variant";
5353 case DW_TAG_common_block:
5354 return "DW_TAG_common_block";
5355 case DW_TAG_common_inclusion:
5356 return "DW_TAG_common_inclusion";
5357 case DW_TAG_inheritance:
5358 return "DW_TAG_inheritance";
5359 case DW_TAG_inlined_subroutine:
5360 return "DW_TAG_inlined_subroutine";
5361 case DW_TAG_module:
5362 return "DW_TAG_module";
5363 case DW_TAG_ptr_to_member_type:
5364 return "DW_TAG_ptr_to_member_type";
5365 case DW_TAG_set_type:
5366 return "DW_TAG_set_type";
5367 case DW_TAG_subrange_type:
5368 return "DW_TAG_subrange_type";
5369 case DW_TAG_with_stmt:
5370 return "DW_TAG_with_stmt";
5371 case DW_TAG_access_declaration:
5372 return "DW_TAG_access_declaration";
5373 case DW_TAG_base_type:
5374 return "DW_TAG_base_type";
5375 case DW_TAG_catch_block:
5376 return "DW_TAG_catch_block";
5377 case DW_TAG_const_type:
5378 return "DW_TAG_const_type";
5379 case DW_TAG_constant:
5380 return "DW_TAG_constant";
5381 case DW_TAG_enumerator:
5382 return "DW_TAG_enumerator";
5383 case DW_TAG_file_type:
5384 return "DW_TAG_file_type";
5385 case DW_TAG_friend:
5386 return "DW_TAG_friend";
5387 case DW_TAG_namelist:
5388 return "DW_TAG_namelist";
5389 case DW_TAG_namelist_item:
5390 return "DW_TAG_namelist_item";
5391 case DW_TAG_packed_type:
5392 return "DW_TAG_packed_type";
5393 case DW_TAG_subprogram:
5394 return "DW_TAG_subprogram";
5395 case DW_TAG_template_type_param:
5396 return "DW_TAG_template_type_param";
5397 case DW_TAG_template_value_param:
5398 return "DW_TAG_template_value_param";
5399 case DW_TAG_thrown_type:
5400 return "DW_TAG_thrown_type";
5401 case DW_TAG_try_block:
5402 return "DW_TAG_try_block";
5403 case DW_TAG_variant_part:
5404 return "DW_TAG_variant_part";
5405 case DW_TAG_variable:
5406 return "DW_TAG_variable";
5407 case DW_TAG_volatile_type:
5408 return "DW_TAG_volatile_type";
5409 case DW_TAG_dwarf_procedure:
5410 return "DW_TAG_dwarf_procedure";
5411 case DW_TAG_restrict_type:
5412 return "DW_TAG_restrict_type";
5413 case DW_TAG_interface_type:
5414 return "DW_TAG_interface_type";
5415 case DW_TAG_namespace:
5416 return "DW_TAG_namespace";
5417 case DW_TAG_imported_module:
5418 return "DW_TAG_imported_module";
5419 case DW_TAG_unspecified_type:
5420 return "DW_TAG_unspecified_type";
5421 case DW_TAG_partial_unit:
5422 return "DW_TAG_partial_unit";
5423 case DW_TAG_imported_unit:
5424 return "DW_TAG_imported_unit";
5425 case DW_TAG_condition:
5426 return "DW_TAG_condition";
5427 case DW_TAG_shared_type:
5428 return "DW_TAG_shared_type";
5429 case DW_TAG_MIPS_loop:
5430 return "DW_TAG_MIPS_loop";
5431 case DW_TAG_format_label:
5432 return "DW_TAG_format_label";
5433 case DW_TAG_function_template:
5434 return "DW_TAG_function_template";
5435 case DW_TAG_class_template:
5436 return "DW_TAG_class_template";
5437 case DW_TAG_GNU_BINCL:
5438 return "DW_TAG_GNU_BINCL";
5439 case DW_TAG_GNU_EINCL:
5440 return "DW_TAG_GNU_EINCL";
5441 default:
5442 return "DW_TAG_<unknown>";
5446 /* Convert a DWARF attribute code into its string name. */
5448 static const char *
5449 dwarf_attr_name (unsigned int attr)
5451 switch (attr)
5453 case DW_AT_sibling:
5454 return "DW_AT_sibling";
5455 case DW_AT_location:
5456 return "DW_AT_location";
5457 case DW_AT_name:
5458 return "DW_AT_name";
5459 case DW_AT_ordering:
5460 return "DW_AT_ordering";
5461 case DW_AT_subscr_data:
5462 return "DW_AT_subscr_data";
5463 case DW_AT_byte_size:
5464 return "DW_AT_byte_size";
5465 case DW_AT_bit_offset:
5466 return "DW_AT_bit_offset";
5467 case DW_AT_bit_size:
5468 return "DW_AT_bit_size";
5469 case DW_AT_element_list:
5470 return "DW_AT_element_list";
5471 case DW_AT_stmt_list:
5472 return "DW_AT_stmt_list";
5473 case DW_AT_low_pc:
5474 return "DW_AT_low_pc";
5475 case DW_AT_high_pc:
5476 return "DW_AT_high_pc";
5477 case DW_AT_language:
5478 return "DW_AT_language";
5479 case DW_AT_member:
5480 return "DW_AT_member";
5481 case DW_AT_discr:
5482 return "DW_AT_discr";
5483 case DW_AT_discr_value:
5484 return "DW_AT_discr_value";
5485 case DW_AT_visibility:
5486 return "DW_AT_visibility";
5487 case DW_AT_import:
5488 return "DW_AT_import";
5489 case DW_AT_string_length:
5490 return "DW_AT_string_length";
5491 case DW_AT_common_reference:
5492 return "DW_AT_common_reference";
5493 case DW_AT_comp_dir:
5494 return "DW_AT_comp_dir";
5495 case DW_AT_const_value:
5496 return "DW_AT_const_value";
5497 case DW_AT_containing_type:
5498 return "DW_AT_containing_type";
5499 case DW_AT_default_value:
5500 return "DW_AT_default_value";
5501 case DW_AT_inline:
5502 return "DW_AT_inline";
5503 case DW_AT_is_optional:
5504 return "DW_AT_is_optional";
5505 case DW_AT_lower_bound:
5506 return "DW_AT_lower_bound";
5507 case DW_AT_producer:
5508 return "DW_AT_producer";
5509 case DW_AT_prototyped:
5510 return "DW_AT_prototyped";
5511 case DW_AT_return_addr:
5512 return "DW_AT_return_addr";
5513 case DW_AT_start_scope:
5514 return "DW_AT_start_scope";
5515 case DW_AT_bit_stride:
5516 return "DW_AT_bit_stride";
5517 case DW_AT_upper_bound:
5518 return "DW_AT_upper_bound";
5519 case DW_AT_abstract_origin:
5520 return "DW_AT_abstract_origin";
5521 case DW_AT_accessibility:
5522 return "DW_AT_accessibility";
5523 case DW_AT_address_class:
5524 return "DW_AT_address_class";
5525 case DW_AT_artificial:
5526 return "DW_AT_artificial";
5527 case DW_AT_base_types:
5528 return "DW_AT_base_types";
5529 case DW_AT_calling_convention:
5530 return "DW_AT_calling_convention";
5531 case DW_AT_count:
5532 return "DW_AT_count";
5533 case DW_AT_data_member_location:
5534 return "DW_AT_data_member_location";
5535 case DW_AT_decl_column:
5536 return "DW_AT_decl_column";
5537 case DW_AT_decl_file:
5538 return "DW_AT_decl_file";
5539 case DW_AT_decl_line:
5540 return "DW_AT_decl_line";
5541 case DW_AT_declaration:
5542 return "DW_AT_declaration";
5543 case DW_AT_discr_list:
5544 return "DW_AT_discr_list";
5545 case DW_AT_encoding:
5546 return "DW_AT_encoding";
5547 case DW_AT_external:
5548 return "DW_AT_external";
5549 case DW_AT_frame_base:
5550 return "DW_AT_frame_base";
5551 case DW_AT_friend:
5552 return "DW_AT_friend";
5553 case DW_AT_identifier_case:
5554 return "DW_AT_identifier_case";
5555 case DW_AT_macro_info:
5556 return "DW_AT_macro_info";
5557 case DW_AT_namelist_items:
5558 return "DW_AT_namelist_items";
5559 case DW_AT_priority:
5560 return "DW_AT_priority";
5561 case DW_AT_segment:
5562 return "DW_AT_segment";
5563 case DW_AT_specification:
5564 return "DW_AT_specification";
5565 case DW_AT_static_link:
5566 return "DW_AT_static_link";
5567 case DW_AT_type:
5568 return "DW_AT_type";
5569 case DW_AT_use_location:
5570 return "DW_AT_use_location";
5571 case DW_AT_variable_parameter:
5572 return "DW_AT_variable_parameter";
5573 case DW_AT_virtuality:
5574 return "DW_AT_virtuality";
5575 case DW_AT_vtable_elem_location:
5576 return "DW_AT_vtable_elem_location";
5578 case DW_AT_allocated:
5579 return "DW_AT_allocated";
5580 case DW_AT_associated:
5581 return "DW_AT_associated";
5582 case DW_AT_data_location:
5583 return "DW_AT_data_location";
5584 case DW_AT_byte_stride:
5585 return "DW_AT_byte_stride";
5586 case DW_AT_entry_pc:
5587 return "DW_AT_entry_pc";
5588 case DW_AT_use_UTF8:
5589 return "DW_AT_use_UTF8";
5590 case DW_AT_extension:
5591 return "DW_AT_extension";
5592 case DW_AT_ranges:
5593 return "DW_AT_ranges";
5594 case DW_AT_trampoline:
5595 return "DW_AT_trampoline";
5596 case DW_AT_call_column:
5597 return "DW_AT_call_column";
5598 case DW_AT_call_file:
5599 return "DW_AT_call_file";
5600 case DW_AT_call_line:
5601 return "DW_AT_call_line";
5603 case DW_AT_MIPS_fde:
5604 return "DW_AT_MIPS_fde";
5605 case DW_AT_MIPS_loop_begin:
5606 return "DW_AT_MIPS_loop_begin";
5607 case DW_AT_MIPS_tail_loop_begin:
5608 return "DW_AT_MIPS_tail_loop_begin";
5609 case DW_AT_MIPS_epilog_begin:
5610 return "DW_AT_MIPS_epilog_begin";
5611 case DW_AT_MIPS_loop_unroll_factor:
5612 return "DW_AT_MIPS_loop_unroll_factor";
5613 case DW_AT_MIPS_software_pipeline_depth:
5614 return "DW_AT_MIPS_software_pipeline_depth";
5615 case DW_AT_MIPS_linkage_name:
5616 return "DW_AT_MIPS_linkage_name";
5617 case DW_AT_MIPS_stride:
5618 return "DW_AT_MIPS_stride";
5619 case DW_AT_MIPS_abstract_name:
5620 return "DW_AT_MIPS_abstract_name";
5621 case DW_AT_MIPS_clone_origin:
5622 return "DW_AT_MIPS_clone_origin";
5623 case DW_AT_MIPS_has_inlines:
5624 return "DW_AT_MIPS_has_inlines";
5626 case DW_AT_sf_names:
5627 return "DW_AT_sf_names";
5628 case DW_AT_src_info:
5629 return "DW_AT_src_info";
5630 case DW_AT_mac_info:
5631 return "DW_AT_mac_info";
5632 case DW_AT_src_coords:
5633 return "DW_AT_src_coords";
5634 case DW_AT_body_begin:
5635 return "DW_AT_body_begin";
5636 case DW_AT_body_end:
5637 return "DW_AT_body_end";
5638 case DW_AT_GNU_vector:
5639 return "DW_AT_GNU_vector";
5641 case DW_AT_VMS_rtnbeg_pd_address:
5642 return "DW_AT_VMS_rtnbeg_pd_address";
5644 default:
5645 return "DW_AT_<unknown>";
5649 /* Convert a DWARF value form code into its string name. */
5651 static const char *
5652 dwarf_form_name (unsigned int form)
5654 switch (form)
5656 case DW_FORM_addr:
5657 return "DW_FORM_addr";
5658 case DW_FORM_block2:
5659 return "DW_FORM_block2";
5660 case DW_FORM_block4:
5661 return "DW_FORM_block4";
5662 case DW_FORM_data2:
5663 return "DW_FORM_data2";
5664 case DW_FORM_data4:
5665 return "DW_FORM_data4";
5666 case DW_FORM_data8:
5667 return "DW_FORM_data8";
5668 case DW_FORM_string:
5669 return "DW_FORM_string";
5670 case DW_FORM_block:
5671 return "DW_FORM_block";
5672 case DW_FORM_block1:
5673 return "DW_FORM_block1";
5674 case DW_FORM_data1:
5675 return "DW_FORM_data1";
5676 case DW_FORM_flag:
5677 return "DW_FORM_flag";
5678 case DW_FORM_sdata:
5679 return "DW_FORM_sdata";
5680 case DW_FORM_strp:
5681 return "DW_FORM_strp";
5682 case DW_FORM_udata:
5683 return "DW_FORM_udata";
5684 case DW_FORM_ref_addr:
5685 return "DW_FORM_ref_addr";
5686 case DW_FORM_ref1:
5687 return "DW_FORM_ref1";
5688 case DW_FORM_ref2:
5689 return "DW_FORM_ref2";
5690 case DW_FORM_ref4:
5691 return "DW_FORM_ref4";
5692 case DW_FORM_ref8:
5693 return "DW_FORM_ref8";
5694 case DW_FORM_ref_udata:
5695 return "DW_FORM_ref_udata";
5696 case DW_FORM_indirect:
5697 return "DW_FORM_indirect";
5698 default:
5699 return "DW_FORM_<unknown>";
5703 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
5704 instance of an inlined instance of a decl which is local to an inline
5705 function, so we have to trace all of the way back through the origin chain
5706 to find out what sort of node actually served as the original seed for the
5707 given block. */
5709 static tree
5710 decl_ultimate_origin (const_tree decl)
5712 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
5713 return NULL_TREE;
5715 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
5716 nodes in the function to point to themselves; ignore that if
5717 we're trying to output the abstract instance of this function. */
5718 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
5719 return NULL_TREE;
5721 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
5722 most distant ancestor, this should never happen. */
5723 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
5725 return DECL_ABSTRACT_ORIGIN (decl);
5728 /* Determine the "ultimate origin" of a block. The block may be an inlined
5729 instance of an inlined instance of a block which is local to an inline
5730 function, so we have to trace all of the way back through the origin chain
5731 to find out what sort of node actually served as the original seed for the
5732 given block. */
5734 static tree
5735 block_ultimate_origin (const_tree block)
5737 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
5739 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
5740 nodes in the function to point to themselves; ignore that if
5741 we're trying to output the abstract instance of this function. */
5742 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
5743 return NULL_TREE;
5745 if (immediate_origin == NULL_TREE)
5746 return NULL_TREE;
5747 else
5749 tree ret_val;
5750 tree lookahead = immediate_origin;
5754 ret_val = lookahead;
5755 lookahead = (TREE_CODE (ret_val) == BLOCK
5756 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
5758 while (lookahead != NULL && lookahead != ret_val);
5760 /* The block's abstract origin chain may not be the *ultimate* origin of
5761 the block. It could lead to a DECL that has an abstract origin set.
5762 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
5763 will give us if it has one). Note that DECL's abstract origins are
5764 supposed to be the most distant ancestor (or so decl_ultimate_origin
5765 claims), so we don't need to loop following the DECL origins. */
5766 if (DECL_P (ret_val))
5767 return DECL_ORIGIN (ret_val);
5769 return ret_val;
5773 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
5774 of a virtual function may refer to a base class, so we check the 'this'
5775 parameter. */
5777 static tree
5778 decl_class_context (tree decl)
5780 tree context = NULL_TREE;
5782 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5783 context = DECL_CONTEXT (decl);
5784 else
5785 context = TYPE_MAIN_VARIANT
5786 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5788 if (context && !TYPE_P (context))
5789 context = NULL_TREE;
5791 return context;
5794 /* Add an attribute/value pair to a DIE. */
5796 static inline void
5797 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5799 /* Maybe this should be an assert? */
5800 if (die == NULL)
5801 return;
5803 if (die->die_attr == NULL)
5804 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5805 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5808 static inline enum dw_val_class
5809 AT_class (dw_attr_ref a)
5811 return a->dw_attr_val.val_class;
5814 /* Add a flag value attribute to a DIE. */
5816 static inline void
5817 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5819 dw_attr_node attr;
5821 attr.dw_attr = attr_kind;
5822 attr.dw_attr_val.val_class = dw_val_class_flag;
5823 attr.dw_attr_val.v.val_flag = flag;
5824 add_dwarf_attr (die, &attr);
5827 static inline unsigned
5828 AT_flag (dw_attr_ref a)
5830 gcc_assert (a && AT_class (a) == dw_val_class_flag);
5831 return a->dw_attr_val.v.val_flag;
5834 /* Add a signed integer attribute value to a DIE. */
5836 static inline void
5837 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5839 dw_attr_node attr;
5841 attr.dw_attr = attr_kind;
5842 attr.dw_attr_val.val_class = dw_val_class_const;
5843 attr.dw_attr_val.v.val_int = int_val;
5844 add_dwarf_attr (die, &attr);
5847 static inline HOST_WIDE_INT
5848 AT_int (dw_attr_ref a)
5850 gcc_assert (a && AT_class (a) == dw_val_class_const);
5851 return a->dw_attr_val.v.val_int;
5854 /* Add an unsigned integer attribute value to a DIE. */
5856 static inline void
5857 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5858 unsigned HOST_WIDE_INT unsigned_val)
5860 dw_attr_node attr;
5862 attr.dw_attr = attr_kind;
5863 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5864 attr.dw_attr_val.v.val_unsigned = unsigned_val;
5865 add_dwarf_attr (die, &attr);
5868 static inline unsigned HOST_WIDE_INT
5869 AT_unsigned (dw_attr_ref a)
5871 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5872 return a->dw_attr_val.v.val_unsigned;
5875 /* Add an unsigned double integer attribute value to a DIE. */
5877 static inline void
5878 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5879 long unsigned int val_hi, long unsigned int val_low)
5881 dw_attr_node attr;
5883 attr.dw_attr = attr_kind;
5884 attr.dw_attr_val.val_class = dw_val_class_long_long;
5885 attr.dw_attr_val.v.val_long_long.hi = val_hi;
5886 attr.dw_attr_val.v.val_long_long.low = val_low;
5887 add_dwarf_attr (die, &attr);
5890 /* Add a floating point attribute value to a DIE and return it. */
5892 static inline void
5893 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5894 unsigned int length, unsigned int elt_size, unsigned char *array)
5896 dw_attr_node attr;
5898 attr.dw_attr = attr_kind;
5899 attr.dw_attr_val.val_class = dw_val_class_vec;
5900 attr.dw_attr_val.v.val_vec.length = length;
5901 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5902 attr.dw_attr_val.v.val_vec.array = array;
5903 add_dwarf_attr (die, &attr);
5906 /* Hash and equality functions for debug_str_hash. */
5908 static hashval_t
5909 debug_str_do_hash (const void *x)
5911 return htab_hash_string (((const struct indirect_string_node *)x)->str);
5914 static int
5915 debug_str_eq (const void *x1, const void *x2)
5917 return strcmp ((((const struct indirect_string_node *)x1)->str),
5918 (const char *)x2) == 0;
5921 /* Add a string attribute value to a DIE. */
5923 static inline void
5924 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5926 dw_attr_node attr;
5927 struct indirect_string_node *node;
5928 void **slot;
5930 if (! debug_str_hash)
5931 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5932 debug_str_eq, NULL);
5934 slot = htab_find_slot_with_hash (debug_str_hash, str,
5935 htab_hash_string (str), INSERT);
5936 if (*slot == NULL)
5938 node = (struct indirect_string_node *)
5939 ggc_alloc_cleared (sizeof (struct indirect_string_node));
5940 node->str = ggc_strdup (str);
5941 *slot = node;
5943 else
5944 node = (struct indirect_string_node *) *slot;
5946 node->refcount++;
5948 attr.dw_attr = attr_kind;
5949 attr.dw_attr_val.val_class = dw_val_class_str;
5950 attr.dw_attr_val.v.val_str = node;
5951 add_dwarf_attr (die, &attr);
5954 static inline const char *
5955 AT_string (dw_attr_ref a)
5957 gcc_assert (a && AT_class (a) == dw_val_class_str);
5958 return a->dw_attr_val.v.val_str->str;
5961 /* Find out whether a string should be output inline in DIE
5962 or out-of-line in .debug_str section. */
5964 static int
5965 AT_string_form (dw_attr_ref a)
5967 struct indirect_string_node *node;
5968 unsigned int len;
5969 char label[32];
5971 gcc_assert (a && AT_class (a) == dw_val_class_str);
5973 node = a->dw_attr_val.v.val_str;
5974 if (node->form)
5975 return node->form;
5977 len = strlen (node->str) + 1;
5979 /* If the string is shorter or equal to the size of the reference, it is
5980 always better to put it inline. */
5981 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5982 return node->form = DW_FORM_string;
5984 /* If we cannot expect the linker to merge strings in .debug_str
5985 section, only put it into .debug_str if it is worth even in this
5986 single module. */
5987 if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5988 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5989 return node->form = DW_FORM_string;
5991 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5992 ++dw2_string_counter;
5993 node->label = xstrdup (label);
5995 return node->form = DW_FORM_strp;
5998 /* Add a DIE reference attribute value to a DIE. */
6000 static inline void
6001 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6003 dw_attr_node attr;
6005 attr.dw_attr = attr_kind;
6006 attr.dw_attr_val.val_class = dw_val_class_die_ref;
6007 attr.dw_attr_val.v.val_die_ref.die = targ_die;
6008 attr.dw_attr_val.v.val_die_ref.external = 0;
6009 add_dwarf_attr (die, &attr);
6012 /* Add an AT_specification attribute to a DIE, and also make the back
6013 pointer from the specification to the definition. */
6015 static inline void
6016 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6018 add_AT_die_ref (die, DW_AT_specification, targ_die);
6019 gcc_assert (!targ_die->die_definition);
6020 targ_die->die_definition = die;
6023 static inline dw_die_ref
6024 AT_ref (dw_attr_ref a)
6026 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6027 return a->dw_attr_val.v.val_die_ref.die;
6030 static inline int
6031 AT_ref_external (dw_attr_ref a)
6033 if (a && AT_class (a) == dw_val_class_die_ref)
6034 return a->dw_attr_val.v.val_die_ref.external;
6036 return 0;
6039 static inline void
6040 set_AT_ref_external (dw_attr_ref a, int i)
6042 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6043 a->dw_attr_val.v.val_die_ref.external = i;
6046 /* Add an FDE reference attribute value to a DIE. */
6048 static inline void
6049 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6051 dw_attr_node attr;
6053 attr.dw_attr = attr_kind;
6054 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6055 attr.dw_attr_val.v.val_fde_index = targ_fde;
6056 add_dwarf_attr (die, &attr);
6059 /* Add a location description attribute value to a DIE. */
6061 static inline void
6062 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6064 dw_attr_node attr;
6066 attr.dw_attr = attr_kind;
6067 attr.dw_attr_val.val_class = dw_val_class_loc;
6068 attr.dw_attr_val.v.val_loc = loc;
6069 add_dwarf_attr (die, &attr);
6072 static inline dw_loc_descr_ref
6073 AT_loc (dw_attr_ref a)
6075 gcc_assert (a && AT_class (a) == dw_val_class_loc);
6076 return a->dw_attr_val.v.val_loc;
6079 static inline void
6080 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6082 dw_attr_node attr;
6084 attr.dw_attr = attr_kind;
6085 attr.dw_attr_val.val_class = dw_val_class_loc_list;
6086 attr.dw_attr_val.v.val_loc_list = loc_list;
6087 add_dwarf_attr (die, &attr);
6088 have_location_lists = true;
6091 static inline dw_loc_list_ref
6092 AT_loc_list (dw_attr_ref a)
6094 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6095 return a->dw_attr_val.v.val_loc_list;
6098 /* Add an address constant attribute value to a DIE. */
6100 static inline void
6101 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6103 dw_attr_node attr;
6105 attr.dw_attr = attr_kind;
6106 attr.dw_attr_val.val_class = dw_val_class_addr;
6107 attr.dw_attr_val.v.val_addr = addr;
6108 add_dwarf_attr (die, &attr);
6111 /* Get the RTX from to an address DIE attribute. */
6113 static inline rtx
6114 AT_addr (dw_attr_ref a)
6116 gcc_assert (a && AT_class (a) == dw_val_class_addr);
6117 return a->dw_attr_val.v.val_addr;
6120 /* Add a file attribute value to a DIE. */
6122 static inline void
6123 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6124 struct dwarf_file_data *fd)
6126 dw_attr_node attr;
6128 attr.dw_attr = attr_kind;
6129 attr.dw_attr_val.val_class = dw_val_class_file;
6130 attr.dw_attr_val.v.val_file = fd;
6131 add_dwarf_attr (die, &attr);
6134 /* Get the dwarf_file_data from a file DIE attribute. */
6136 static inline struct dwarf_file_data *
6137 AT_file (dw_attr_ref a)
6139 gcc_assert (a && AT_class (a) == dw_val_class_file);
6140 return a->dw_attr_val.v.val_file;
6143 /* Add a label identifier attribute value to a DIE. */
6145 static inline void
6146 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6148 dw_attr_node attr;
6150 attr.dw_attr = attr_kind;
6151 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6152 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6153 add_dwarf_attr (die, &attr);
6156 /* Add a section offset attribute value to a DIE, an offset into the
6157 debug_line section. */
6159 static inline void
6160 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6161 const char *label)
6163 dw_attr_node attr;
6165 attr.dw_attr = attr_kind;
6166 attr.dw_attr_val.val_class = dw_val_class_lineptr;
6167 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6168 add_dwarf_attr (die, &attr);
6171 /* Add a section offset attribute value to a DIE, an offset into the
6172 debug_macinfo section. */
6174 static inline void
6175 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6176 const char *label)
6178 dw_attr_node attr;
6180 attr.dw_attr = attr_kind;
6181 attr.dw_attr_val.val_class = dw_val_class_macptr;
6182 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6183 add_dwarf_attr (die, &attr);
6186 /* Add an offset attribute value to a DIE. */
6188 static inline void
6189 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6190 unsigned HOST_WIDE_INT offset)
6192 dw_attr_node attr;
6194 attr.dw_attr = attr_kind;
6195 attr.dw_attr_val.val_class = dw_val_class_offset;
6196 attr.dw_attr_val.v.val_offset = offset;
6197 add_dwarf_attr (die, &attr);
6200 /* Add an range_list attribute value to a DIE. */
6202 static void
6203 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6204 long unsigned int offset)
6206 dw_attr_node attr;
6208 attr.dw_attr = attr_kind;
6209 attr.dw_attr_val.val_class = dw_val_class_range_list;
6210 attr.dw_attr_val.v.val_offset = offset;
6211 add_dwarf_attr (die, &attr);
6214 static inline const char *
6215 AT_lbl (dw_attr_ref a)
6217 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6218 || AT_class (a) == dw_val_class_lineptr
6219 || AT_class (a) == dw_val_class_macptr));
6220 return a->dw_attr_val.v.val_lbl_id;
6223 /* Get the attribute of type attr_kind. */
6225 static dw_attr_ref
6226 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6228 dw_attr_ref a;
6229 unsigned ix;
6230 dw_die_ref spec = NULL;
6232 if (! die)
6233 return NULL;
6235 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6236 if (a->dw_attr == attr_kind)
6237 return a;
6238 else if (a->dw_attr == DW_AT_specification
6239 || a->dw_attr == DW_AT_abstract_origin)
6240 spec = AT_ref (a);
6242 if (spec)
6243 return get_AT (spec, attr_kind);
6245 return NULL;
6248 /* Return the "low pc" attribute value, typically associated with a subprogram
6249 DIE. Return null if the "low pc" attribute is either not present, or if it
6250 cannot be represented as an assembler label identifier. */
6252 static inline const char *
6253 get_AT_low_pc (dw_die_ref die)
6255 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6257 return a ? AT_lbl (a) : NULL;
6260 /* Return the "high pc" attribute value, typically associated with a subprogram
6261 DIE. Return null if the "high pc" attribute is either not present, or if it
6262 cannot be represented as an assembler label identifier. */
6264 static inline const char *
6265 get_AT_hi_pc (dw_die_ref die)
6267 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6269 return a ? AT_lbl (a) : NULL;
6272 /* Return the value of the string attribute designated by ATTR_KIND, or
6273 NULL if it is not present. */
6275 static inline const char *
6276 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6278 dw_attr_ref a = get_AT (die, attr_kind);
6280 return a ? AT_string (a) : NULL;
6283 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6284 if it is not present. */
6286 static inline int
6287 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6289 dw_attr_ref a = get_AT (die, attr_kind);
6291 return a ? AT_flag (a) : 0;
6294 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6295 if it is not present. */
6297 static inline unsigned
6298 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6300 dw_attr_ref a = get_AT (die, attr_kind);
6302 return a ? AT_unsigned (a) : 0;
6305 static inline dw_die_ref
6306 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6308 dw_attr_ref a = get_AT (die, attr_kind);
6310 return a ? AT_ref (a) : NULL;
6313 static inline struct dwarf_file_data *
6314 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6316 dw_attr_ref a = get_AT (die, attr_kind);
6318 return a ? AT_file (a) : NULL;
6321 /* Return TRUE if the language is C or C++. */
6323 static inline bool
6324 is_c_family (void)
6326 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6328 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6329 || lang == DW_LANG_C99
6330 || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6333 /* Return TRUE if the language is C++. */
6335 static inline bool
6336 is_cxx (void)
6338 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6340 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6343 /* Return TRUE if the language is Fortran. */
6345 static inline bool
6346 is_fortran (void)
6348 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6350 return (lang == DW_LANG_Fortran77
6351 || lang == DW_LANG_Fortran90
6352 || lang == DW_LANG_Fortran95);
6355 /* Return TRUE if the language is Java. */
6357 static inline bool
6358 is_java (void)
6360 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6362 return lang == DW_LANG_Java;
6365 /* Return TRUE if the language is Ada. */
6367 static inline bool
6368 is_ada (void)
6370 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6372 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6375 /* Remove the specified attribute if present. */
6377 static void
6378 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6380 dw_attr_ref a;
6381 unsigned ix;
6383 if (! die)
6384 return;
6386 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6387 if (a->dw_attr == attr_kind)
6389 if (AT_class (a) == dw_val_class_str)
6390 if (a->dw_attr_val.v.val_str->refcount)
6391 a->dw_attr_val.v.val_str->refcount--;
6393 /* VEC_ordered_remove should help reduce the number of abbrevs
6394 that are needed. */
6395 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6396 return;
6400 /* Remove CHILD from its parent. PREV must have the property that
6401 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
6403 static void
6404 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6406 gcc_assert (child->die_parent == prev->die_parent);
6407 gcc_assert (prev->die_sib == child);
6408 if (prev == child)
6410 gcc_assert (child->die_parent->die_child == child);
6411 prev = NULL;
6413 else
6414 prev->die_sib = child->die_sib;
6415 if (child->die_parent->die_child == child)
6416 child->die_parent->die_child = prev;
6419 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
6420 matches TAG. */
6422 static void
6423 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6425 dw_die_ref c;
6427 c = die->die_child;
6428 if (c) do {
6429 dw_die_ref prev = c;
6430 c = c->die_sib;
6431 while (c->die_tag == tag)
6433 remove_child_with_prev (c, prev);
6434 /* Might have removed every child. */
6435 if (c == c->die_sib)
6436 return;
6437 c = c->die_sib;
6439 } while (c != die->die_child);
6442 /* Add a CHILD_DIE as the last child of DIE. */
6444 static void
6445 add_child_die (dw_die_ref die, dw_die_ref child_die)
6447 /* FIXME this should probably be an assert. */
6448 if (! die || ! child_die)
6449 return;
6450 gcc_assert (die != child_die);
6452 child_die->die_parent = die;
6453 if (die->die_child)
6455 child_die->die_sib = die->die_child->die_sib;
6456 die->die_child->die_sib = child_die;
6458 else
6459 child_die->die_sib = child_die;
6460 die->die_child = child_die;
6463 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6464 is the specification, to the end of PARENT's list of children.
6465 This is done by removing and re-adding it. */
6467 static void
6468 splice_child_die (dw_die_ref parent, dw_die_ref child)
6470 dw_die_ref p;
6472 /* We want the declaration DIE from inside the class, not the
6473 specification DIE at toplevel. */
6474 if (child->die_parent != parent)
6476 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6478 if (tmp)
6479 child = tmp;
6482 gcc_assert (child->die_parent == parent
6483 || (child->die_parent
6484 == get_AT_ref (parent, DW_AT_specification)));
6486 for (p = child->die_parent->die_child; ; p = p->die_sib)
6487 if (p->die_sib == child)
6489 remove_child_with_prev (child, p);
6490 break;
6493 add_child_die (parent, child);
6496 /* Return a pointer to a newly created DIE node. */
6498 static inline dw_die_ref
6499 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6501 dw_die_ref die = GGC_CNEW (die_node);
6503 die->die_tag = tag_value;
6505 if (parent_die != NULL)
6506 add_child_die (parent_die, die);
6507 else
6509 limbo_die_node *limbo_node;
6511 limbo_node = GGC_CNEW (limbo_die_node);
6512 limbo_node->die = die;
6513 limbo_node->created_for = t;
6514 limbo_node->next = limbo_die_list;
6515 limbo_die_list = limbo_node;
6518 return die;
6521 /* Return the DIE associated with the given type specifier. */
6523 static inline dw_die_ref
6524 lookup_type_die (tree type)
6526 return TYPE_SYMTAB_DIE (type);
6529 /* Equate a DIE to a given type specifier. */
6531 static inline void
6532 equate_type_number_to_die (tree type, dw_die_ref type_die)
6534 TYPE_SYMTAB_DIE (type) = type_die;
6537 /* Returns a hash value for X (which really is a die_struct). */
6539 static hashval_t
6540 decl_die_table_hash (const void *x)
6542 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6545 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
6547 static int
6548 decl_die_table_eq (const void *x, const void *y)
6550 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6553 /* Return the DIE associated with a given declaration. */
6555 static inline dw_die_ref
6556 lookup_decl_die (tree decl)
6558 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6561 /* Returns a hash value for X (which really is a var_loc_list). */
6563 static hashval_t
6564 decl_loc_table_hash (const void *x)
6566 return (hashval_t) ((const var_loc_list *) x)->decl_id;
6569 /* Return nonzero if decl_id of var_loc_list X is the same as
6570 UID of decl *Y. */
6572 static int
6573 decl_loc_table_eq (const void *x, const void *y)
6575 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6578 /* Return the var_loc list associated with a given declaration. */
6580 static inline var_loc_list *
6581 lookup_decl_loc (const_tree decl)
6583 return (var_loc_list *)
6584 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6587 /* Equate a DIE to a particular declaration. */
6589 static void
6590 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6592 unsigned int decl_id = DECL_UID (decl);
6593 void **slot;
6595 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6596 *slot = decl_die;
6597 decl_die->decl_id = decl_id;
6600 /* Add a variable location node to the linked list for DECL. */
6602 static void
6603 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
6605 unsigned int decl_id = DECL_UID (decl);
6606 var_loc_list *temp;
6607 void **slot;
6609 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
6610 if (*slot == NULL)
6612 temp = GGC_CNEW (var_loc_list);
6613 temp->decl_id = decl_id;
6614 *slot = temp;
6616 else
6617 temp = (var_loc_list *) *slot;
6619 if (temp->last)
6621 /* If the current location is the same as the end of the list,
6622 and either both or neither of the locations is uninitialized,
6623 we have nothing to do. */
6624 if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
6625 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
6626 || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6627 != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
6628 && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6629 == VAR_INIT_STATUS_UNINITIALIZED)
6630 || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
6631 == VAR_INIT_STATUS_UNINITIALIZED))))
6633 /* Add LOC to the end of list and update LAST. */
6634 temp->last->next = loc;
6635 temp->last = loc;
6638 /* Do not add empty location to the beginning of the list. */
6639 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
6641 temp->first = loc;
6642 temp->last = loc;
6646 /* Keep track of the number of spaces used to indent the
6647 output of the debugging routines that print the structure of
6648 the DIE internal representation. */
6649 static int print_indent;
6651 /* Indent the line the number of spaces given by print_indent. */
6653 static inline void
6654 print_spaces (FILE *outfile)
6656 fprintf (outfile, "%*s", print_indent, "");
6659 /* Print the information associated with a given DIE, and its children.
6660 This routine is a debugging aid only. */
6662 static void
6663 print_die (dw_die_ref die, FILE *outfile)
6665 dw_attr_ref a;
6666 dw_die_ref c;
6667 unsigned ix;
6669 print_spaces (outfile);
6670 fprintf (outfile, "DIE %4ld: %s\n",
6671 die->die_offset, dwarf_tag_name (die->die_tag));
6672 print_spaces (outfile);
6673 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6674 fprintf (outfile, " offset: %ld\n", die->die_offset);
6676 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6678 print_spaces (outfile);
6679 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6681 switch (AT_class (a))
6683 case dw_val_class_addr:
6684 fprintf (outfile, "address");
6685 break;
6686 case dw_val_class_offset:
6687 fprintf (outfile, "offset");
6688 break;
6689 case dw_val_class_loc:
6690 fprintf (outfile, "location descriptor");
6691 break;
6692 case dw_val_class_loc_list:
6693 fprintf (outfile, "location list -> label:%s",
6694 AT_loc_list (a)->ll_symbol);
6695 break;
6696 case dw_val_class_range_list:
6697 fprintf (outfile, "range list");
6698 break;
6699 case dw_val_class_const:
6700 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
6701 break;
6702 case dw_val_class_unsigned_const:
6703 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
6704 break;
6705 case dw_val_class_long_long:
6706 fprintf (outfile, "constant (%lu,%lu)",
6707 a->dw_attr_val.v.val_long_long.hi,
6708 a->dw_attr_val.v.val_long_long.low);
6709 break;
6710 case dw_val_class_vec:
6711 fprintf (outfile, "floating-point or vector constant");
6712 break;
6713 case dw_val_class_flag:
6714 fprintf (outfile, "%u", AT_flag (a));
6715 break;
6716 case dw_val_class_die_ref:
6717 if (AT_ref (a) != NULL)
6719 if (AT_ref (a)->die_symbol)
6720 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
6721 else
6722 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
6724 else
6725 fprintf (outfile, "die -> <null>");
6726 break;
6727 case dw_val_class_lbl_id:
6728 case dw_val_class_lineptr:
6729 case dw_val_class_macptr:
6730 fprintf (outfile, "label: %s", AT_lbl (a));
6731 break;
6732 case dw_val_class_str:
6733 if (AT_string (a) != NULL)
6734 fprintf (outfile, "\"%s\"", AT_string (a));
6735 else
6736 fprintf (outfile, "<null>");
6737 break;
6738 case dw_val_class_file:
6739 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6740 AT_file (a)->emitted_number);
6741 break;
6742 default:
6743 break;
6746 fprintf (outfile, "\n");
6749 if (die->die_child != NULL)
6751 print_indent += 4;
6752 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6753 print_indent -= 4;
6755 if (print_indent == 0)
6756 fprintf (outfile, "\n");
6759 /* Print the contents of the source code line number correspondence table.
6760 This routine is a debugging aid only. */
6762 static void
6763 print_dwarf_line_table (FILE *outfile)
6765 unsigned i;
6766 dw_line_info_ref line_info;
6768 fprintf (outfile, "\n\nDWARF source line information\n");
6769 for (i = 1; i < line_info_table_in_use; i++)
6771 line_info = &line_info_table[i];
6772 fprintf (outfile, "%5d: %4ld %6ld\n", i,
6773 line_info->dw_file_num,
6774 line_info->dw_line_num);
6777 fprintf (outfile, "\n\n");
6780 /* Print the information collected for a given DIE. */
6782 void
6783 debug_dwarf_die (dw_die_ref die)
6785 print_die (die, stderr);
6788 /* Print all DWARF information collected for the compilation unit.
6789 This routine is a debugging aid only. */
6791 void
6792 debug_dwarf (void)
6794 print_indent = 0;
6795 print_die (comp_unit_die, stderr);
6796 if (! DWARF2_ASM_LINE_DEBUG_INFO)
6797 print_dwarf_line_table (stderr);
6800 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6801 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6802 DIE that marks the start of the DIEs for this include file. */
6804 static dw_die_ref
6805 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6807 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6808 dw_die_ref new_unit = gen_compile_unit_die (filename);
6810 new_unit->die_sib = old_unit;
6811 return new_unit;
6814 /* Close an include-file CU and reopen the enclosing one. */
6816 static dw_die_ref
6817 pop_compile_unit (dw_die_ref old_unit)
6819 dw_die_ref new_unit = old_unit->die_sib;
6821 old_unit->die_sib = NULL;
6822 return new_unit;
6825 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6826 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6828 /* Calculate the checksum of a location expression. */
6830 static inline void
6831 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6833 CHECKSUM (loc->dw_loc_opc);
6834 CHECKSUM (loc->dw_loc_oprnd1);
6835 CHECKSUM (loc->dw_loc_oprnd2);
6838 /* Calculate the checksum of an attribute. */
6840 static void
6841 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6843 dw_loc_descr_ref loc;
6844 rtx r;
6846 CHECKSUM (at->dw_attr);
6848 /* We don't care that this was compiled with a different compiler
6849 snapshot; if the output is the same, that's what matters. */
6850 if (at->dw_attr == DW_AT_producer)
6851 return;
6853 switch (AT_class (at))
6855 case dw_val_class_const:
6856 CHECKSUM (at->dw_attr_val.v.val_int);
6857 break;
6858 case dw_val_class_unsigned_const:
6859 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6860 break;
6861 case dw_val_class_long_long:
6862 CHECKSUM (at->dw_attr_val.v.val_long_long);
6863 break;
6864 case dw_val_class_vec:
6865 CHECKSUM (at->dw_attr_val.v.val_vec);
6866 break;
6867 case dw_val_class_flag:
6868 CHECKSUM (at->dw_attr_val.v.val_flag);
6869 break;
6870 case dw_val_class_str:
6871 CHECKSUM_STRING (AT_string (at));
6872 break;
6874 case dw_val_class_addr:
6875 r = AT_addr (at);
6876 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6877 CHECKSUM_STRING (XSTR (r, 0));
6878 break;
6880 case dw_val_class_offset:
6881 CHECKSUM (at->dw_attr_val.v.val_offset);
6882 break;
6884 case dw_val_class_loc:
6885 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6886 loc_checksum (loc, ctx);
6887 break;
6889 case dw_val_class_die_ref:
6890 die_checksum (AT_ref (at), ctx, mark);
6891 break;
6893 case dw_val_class_fde_ref:
6894 case dw_val_class_lbl_id:
6895 case dw_val_class_lineptr:
6896 case dw_val_class_macptr:
6897 break;
6899 case dw_val_class_file:
6900 CHECKSUM_STRING (AT_file (at)->filename);
6901 break;
6903 default:
6904 break;
6908 /* Calculate the checksum of a DIE. */
6910 static void
6911 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6913 dw_die_ref c;
6914 dw_attr_ref a;
6915 unsigned ix;
6917 /* To avoid infinite recursion. */
6918 if (die->die_mark)
6920 CHECKSUM (die->die_mark);
6921 return;
6923 die->die_mark = ++(*mark);
6925 CHECKSUM (die->die_tag);
6927 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6928 attr_checksum (a, ctx, mark);
6930 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6933 #undef CHECKSUM
6934 #undef CHECKSUM_STRING
6936 /* Do the location expressions look same? */
6937 static inline int
6938 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6940 return loc1->dw_loc_opc == loc2->dw_loc_opc
6941 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6942 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6945 /* Do the values look the same? */
6946 static int
6947 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6949 dw_loc_descr_ref loc1, loc2;
6950 rtx r1, r2;
6952 if (v1->val_class != v2->val_class)
6953 return 0;
6955 switch (v1->val_class)
6957 case dw_val_class_const:
6958 return v1->v.val_int == v2->v.val_int;
6959 case dw_val_class_unsigned_const:
6960 return v1->v.val_unsigned == v2->v.val_unsigned;
6961 case dw_val_class_long_long:
6962 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6963 && v1->v.val_long_long.low == v2->v.val_long_long.low;
6964 case dw_val_class_vec:
6965 if (v1->v.val_vec.length != v2->v.val_vec.length
6966 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6967 return 0;
6968 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6969 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6970 return 0;
6971 return 1;
6972 case dw_val_class_flag:
6973 return v1->v.val_flag == v2->v.val_flag;
6974 case dw_val_class_str:
6975 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6977 case dw_val_class_addr:
6978 r1 = v1->v.val_addr;
6979 r2 = v2->v.val_addr;
6980 if (GET_CODE (r1) != GET_CODE (r2))
6981 return 0;
6982 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6983 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6985 case dw_val_class_offset:
6986 return v1->v.val_offset == v2->v.val_offset;
6988 case dw_val_class_loc:
6989 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6990 loc1 && loc2;
6991 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6992 if (!same_loc_p (loc1, loc2, mark))
6993 return 0;
6994 return !loc1 && !loc2;
6996 case dw_val_class_die_ref:
6997 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6999 case dw_val_class_fde_ref:
7000 case dw_val_class_lbl_id:
7001 case dw_val_class_lineptr:
7002 case dw_val_class_macptr:
7003 return 1;
7005 case dw_val_class_file:
7006 return v1->v.val_file == v2->v.val_file;
7008 default:
7009 return 1;
7013 /* Do the attributes look the same? */
7015 static int
7016 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7018 if (at1->dw_attr != at2->dw_attr)
7019 return 0;
7021 /* We don't care that this was compiled with a different compiler
7022 snapshot; if the output is the same, that's what matters. */
7023 if (at1->dw_attr == DW_AT_producer)
7024 return 1;
7026 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7029 /* Do the dies look the same? */
7031 static int
7032 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7034 dw_die_ref c1, c2;
7035 dw_attr_ref a1;
7036 unsigned ix;
7038 /* To avoid infinite recursion. */
7039 if (die1->die_mark)
7040 return die1->die_mark == die2->die_mark;
7041 die1->die_mark = die2->die_mark = ++(*mark);
7043 if (die1->die_tag != die2->die_tag)
7044 return 0;
7046 if (VEC_length (dw_attr_node, die1->die_attr)
7047 != VEC_length (dw_attr_node, die2->die_attr))
7048 return 0;
7050 for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7051 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7052 return 0;
7054 c1 = die1->die_child;
7055 c2 = die2->die_child;
7056 if (! c1)
7058 if (c2)
7059 return 0;
7061 else
7062 for (;;)
7064 if (!same_die_p (c1, c2, mark))
7065 return 0;
7066 c1 = c1->die_sib;
7067 c2 = c2->die_sib;
7068 if (c1 == die1->die_child)
7070 if (c2 == die2->die_child)
7071 break;
7072 else
7073 return 0;
7077 return 1;
7080 /* Do the dies look the same? Wrapper around same_die_p. */
7082 static int
7083 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7085 int mark = 0;
7086 int ret = same_die_p (die1, die2, &mark);
7088 unmark_all_dies (die1);
7089 unmark_all_dies (die2);
7091 return ret;
7094 /* The prefix to attach to symbols on DIEs in the current comdat debug
7095 info section. */
7096 static char *comdat_symbol_id;
7098 /* The index of the current symbol within the current comdat CU. */
7099 static unsigned int comdat_symbol_number;
7101 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7102 children, and set comdat_symbol_id accordingly. */
7104 static void
7105 compute_section_prefix (dw_die_ref unit_die)
7107 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7108 const char *base = die_name ? lbasename (die_name) : "anonymous";
7109 char *name = XALLOCAVEC (char, strlen (base) + 64);
7110 char *p;
7111 int i, mark;
7112 unsigned char checksum[16];
7113 struct md5_ctx ctx;
7115 /* Compute the checksum of the DIE, then append part of it as hex digits to
7116 the name filename of the unit. */
7118 md5_init_ctx (&ctx);
7119 mark = 0;
7120 die_checksum (unit_die, &ctx, &mark);
7121 unmark_all_dies (unit_die);
7122 md5_finish_ctx (&ctx, checksum);
7124 sprintf (name, "%s.", base);
7125 clean_symbol_name (name);
7127 p = name + strlen (name);
7128 for (i = 0; i < 4; i++)
7130 sprintf (p, "%.2x", checksum[i]);
7131 p += 2;
7134 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7135 comdat_symbol_number = 0;
7138 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7140 static int
7141 is_type_die (dw_die_ref die)
7143 switch (die->die_tag)
7145 case DW_TAG_array_type:
7146 case DW_TAG_class_type:
7147 case DW_TAG_interface_type:
7148 case DW_TAG_enumeration_type:
7149 case DW_TAG_pointer_type:
7150 case DW_TAG_reference_type:
7151 case DW_TAG_string_type:
7152 case DW_TAG_structure_type:
7153 case DW_TAG_subroutine_type:
7154 case DW_TAG_union_type:
7155 case DW_TAG_ptr_to_member_type:
7156 case DW_TAG_set_type:
7157 case DW_TAG_subrange_type:
7158 case DW_TAG_base_type:
7159 case DW_TAG_const_type:
7160 case DW_TAG_file_type:
7161 case DW_TAG_packed_type:
7162 case DW_TAG_volatile_type:
7163 case DW_TAG_typedef:
7164 return 1;
7165 default:
7166 return 0;
7170 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7171 Basically, we want to choose the bits that are likely to be shared between
7172 compilations (types) and leave out the bits that are specific to individual
7173 compilations (functions). */
7175 static int
7176 is_comdat_die (dw_die_ref c)
7178 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7179 we do for stabs. The advantage is a greater likelihood of sharing between
7180 objects that don't include headers in the same order (and therefore would
7181 put the base types in a different comdat). jason 8/28/00 */
7183 if (c->die_tag == DW_TAG_base_type)
7184 return 0;
7186 if (c->die_tag == DW_TAG_pointer_type
7187 || c->die_tag == DW_TAG_reference_type
7188 || c->die_tag == DW_TAG_const_type
7189 || c->die_tag == DW_TAG_volatile_type)
7191 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7193 return t ? is_comdat_die (t) : 0;
7196 return is_type_die (c);
7199 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7200 compilation unit. */
7202 static int
7203 is_symbol_die (dw_die_ref c)
7205 return (is_type_die (c)
7206 || (get_AT (c, DW_AT_declaration)
7207 && !get_AT (c, DW_AT_specification))
7208 || c->die_tag == DW_TAG_namespace);
7211 static char *
7212 gen_internal_sym (const char *prefix)
7214 char buf[256];
7216 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7217 return xstrdup (buf);
7220 /* Assign symbols to all worthy DIEs under DIE. */
7222 static void
7223 assign_symbol_names (dw_die_ref die)
7225 dw_die_ref c;
7227 if (is_symbol_die (die))
7229 if (comdat_symbol_id)
7231 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7233 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7234 comdat_symbol_id, comdat_symbol_number++);
7235 die->die_symbol = xstrdup (p);
7237 else
7238 die->die_symbol = gen_internal_sym ("LDIE");
7241 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7244 struct cu_hash_table_entry
7246 dw_die_ref cu;
7247 unsigned min_comdat_num, max_comdat_num;
7248 struct cu_hash_table_entry *next;
7251 /* Routines to manipulate hash table of CUs. */
7252 static hashval_t
7253 htab_cu_hash (const void *of)
7255 const struct cu_hash_table_entry *const entry =
7256 (const struct cu_hash_table_entry *) of;
7258 return htab_hash_string (entry->cu->die_symbol);
7261 static int
7262 htab_cu_eq (const void *of1, const void *of2)
7264 const struct cu_hash_table_entry *const entry1 =
7265 (const struct cu_hash_table_entry *) of1;
7266 const struct die_struct *const entry2 = (const struct die_struct *) of2;
7268 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7271 static void
7272 htab_cu_del (void *what)
7274 struct cu_hash_table_entry *next,
7275 *entry = (struct cu_hash_table_entry *) what;
7277 while (entry)
7279 next = entry->next;
7280 free (entry);
7281 entry = next;
7285 /* Check whether we have already seen this CU and set up SYM_NUM
7286 accordingly. */
7287 static int
7288 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7290 struct cu_hash_table_entry dummy;
7291 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7293 dummy.max_comdat_num = 0;
7295 slot = (struct cu_hash_table_entry **)
7296 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7297 INSERT);
7298 entry = *slot;
7300 for (; entry; last = entry, entry = entry->next)
7302 if (same_die_p_wrap (cu, entry->cu))
7303 break;
7306 if (entry)
7308 *sym_num = entry->min_comdat_num;
7309 return 1;
7312 entry = XCNEW (struct cu_hash_table_entry);
7313 entry->cu = cu;
7314 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7315 entry->next = *slot;
7316 *slot = entry;
7318 return 0;
7321 /* Record SYM_NUM to record of CU in HTABLE. */
7322 static void
7323 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7325 struct cu_hash_table_entry **slot, *entry;
7327 slot = (struct cu_hash_table_entry **)
7328 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7329 NO_INSERT);
7330 entry = *slot;
7332 entry->max_comdat_num = sym_num;
7335 /* Traverse the DIE (which is always comp_unit_die), and set up
7336 additional compilation units for each of the include files we see
7337 bracketed by BINCL/EINCL. */
7339 static void
7340 break_out_includes (dw_die_ref die)
7342 dw_die_ref c;
7343 dw_die_ref unit = NULL;
7344 limbo_die_node *node, **pnode;
7345 htab_t cu_hash_table;
7347 c = die->die_child;
7348 if (c) do {
7349 dw_die_ref prev = c;
7350 c = c->die_sib;
7351 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7352 || (unit && is_comdat_die (c)))
7354 dw_die_ref next = c->die_sib;
7356 /* This DIE is for a secondary CU; remove it from the main one. */
7357 remove_child_with_prev (c, prev);
7359 if (c->die_tag == DW_TAG_GNU_BINCL)
7360 unit = push_new_compile_unit (unit, c);
7361 else if (c->die_tag == DW_TAG_GNU_EINCL)
7362 unit = pop_compile_unit (unit);
7363 else
7364 add_child_die (unit, c);
7365 c = next;
7366 if (c == die->die_child)
7367 break;
7369 } while (c != die->die_child);
7371 #if 0
7372 /* We can only use this in debugging, since the frontend doesn't check
7373 to make sure that we leave every include file we enter. */
7374 gcc_assert (!unit);
7375 #endif
7377 assign_symbol_names (die);
7378 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7379 for (node = limbo_die_list, pnode = &limbo_die_list;
7380 node;
7381 node = node->next)
7383 int is_dupl;
7385 compute_section_prefix (node->die);
7386 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7387 &comdat_symbol_number);
7388 assign_symbol_names (node->die);
7389 if (is_dupl)
7390 *pnode = node->next;
7391 else
7393 pnode = &node->next;
7394 record_comdat_symbol_number (node->die, cu_hash_table,
7395 comdat_symbol_number);
7398 htab_delete (cu_hash_table);
7401 /* Traverse the DIE and add a sibling attribute if it may have the
7402 effect of speeding up access to siblings. To save some space,
7403 avoid generating sibling attributes for DIE's without children. */
7405 static void
7406 add_sibling_attributes (dw_die_ref die)
7408 dw_die_ref c;
7410 if (! die->die_child)
7411 return;
7413 if (die->die_parent && die != die->die_parent->die_child)
7414 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7416 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7419 /* Output all location lists for the DIE and its children. */
7421 static void
7422 output_location_lists (dw_die_ref die)
7424 dw_die_ref c;
7425 dw_attr_ref a;
7426 unsigned ix;
7428 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7429 if (AT_class (a) == dw_val_class_loc_list)
7430 output_loc_list (AT_loc_list (a));
7432 FOR_EACH_CHILD (die, c, output_location_lists (c));
7435 /* The format of each DIE (and its attribute value pairs) is encoded in an
7436 abbreviation table. This routine builds the abbreviation table and assigns
7437 a unique abbreviation id for each abbreviation entry. The children of each
7438 die are visited recursively. */
7440 static void
7441 build_abbrev_table (dw_die_ref die)
7443 unsigned long abbrev_id;
7444 unsigned int n_alloc;
7445 dw_die_ref c;
7446 dw_attr_ref a;
7447 unsigned ix;
7449 /* Scan the DIE references, and mark as external any that refer to
7450 DIEs from other CUs (i.e. those which are not marked). */
7451 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7452 if (AT_class (a) == dw_val_class_die_ref
7453 && AT_ref (a)->die_mark == 0)
7455 gcc_assert (AT_ref (a)->die_symbol);
7457 set_AT_ref_external (a, 1);
7460 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7462 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7463 dw_attr_ref die_a, abbrev_a;
7464 unsigned ix;
7465 bool ok = true;
7467 if (abbrev->die_tag != die->die_tag)
7468 continue;
7469 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7470 continue;
7472 if (VEC_length (dw_attr_node, abbrev->die_attr)
7473 != VEC_length (dw_attr_node, die->die_attr))
7474 continue;
7476 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7478 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7479 if ((abbrev_a->dw_attr != die_a->dw_attr)
7480 || (value_format (abbrev_a) != value_format (die_a)))
7482 ok = false;
7483 break;
7486 if (ok)
7487 break;
7490 if (abbrev_id >= abbrev_die_table_in_use)
7492 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7494 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7495 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7496 n_alloc);
7498 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7499 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7500 abbrev_die_table_allocated = n_alloc;
7503 ++abbrev_die_table_in_use;
7504 abbrev_die_table[abbrev_id] = die;
7507 die->die_abbrev = abbrev_id;
7508 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7511 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7513 static int
7514 constant_size (long unsigned int value)
7516 int log;
7518 if (value == 0)
7519 log = 0;
7520 else
7521 log = floor_log2 (value);
7523 log = log / 8;
7524 log = 1 << (floor_log2 (log) + 1);
7526 return log;
7529 /* Return the size of a DIE as it is represented in the
7530 .debug_info section. */
7532 static unsigned long
7533 size_of_die (dw_die_ref die)
7535 unsigned long size = 0;
7536 dw_attr_ref a;
7537 unsigned ix;
7539 size += size_of_uleb128 (die->die_abbrev);
7540 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7542 switch (AT_class (a))
7544 case dw_val_class_addr:
7545 size += DWARF2_ADDR_SIZE;
7546 break;
7547 case dw_val_class_offset:
7548 size += DWARF_OFFSET_SIZE;
7549 break;
7550 case dw_val_class_loc:
7552 unsigned long lsize = size_of_locs (AT_loc (a));
7554 /* Block length. */
7555 size += constant_size (lsize);
7556 size += lsize;
7558 break;
7559 case dw_val_class_loc_list:
7560 size += DWARF_OFFSET_SIZE;
7561 break;
7562 case dw_val_class_range_list:
7563 size += DWARF_OFFSET_SIZE;
7564 break;
7565 case dw_val_class_const:
7566 size += size_of_sleb128 (AT_int (a));
7567 break;
7568 case dw_val_class_unsigned_const:
7569 size += constant_size (AT_unsigned (a));
7570 break;
7571 case dw_val_class_long_long:
7572 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7573 break;
7574 case dw_val_class_vec:
7575 size += 1 + (a->dw_attr_val.v.val_vec.length
7576 * a->dw_attr_val.v.val_vec.elt_size); /* block */
7577 break;
7578 case dw_val_class_flag:
7579 size += 1;
7580 break;
7581 case dw_val_class_die_ref:
7582 if (AT_ref_external (a))
7583 size += DWARF2_ADDR_SIZE;
7584 else
7585 size += DWARF_OFFSET_SIZE;
7586 break;
7587 case dw_val_class_fde_ref:
7588 size += DWARF_OFFSET_SIZE;
7589 break;
7590 case dw_val_class_lbl_id:
7591 size += DWARF2_ADDR_SIZE;
7592 break;
7593 case dw_val_class_lineptr:
7594 case dw_val_class_macptr:
7595 size += DWARF_OFFSET_SIZE;
7596 break;
7597 case dw_val_class_str:
7598 if (AT_string_form (a) == DW_FORM_strp)
7599 size += DWARF_OFFSET_SIZE;
7600 else
7601 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7602 break;
7603 case dw_val_class_file:
7604 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7605 break;
7606 default:
7607 gcc_unreachable ();
7611 return size;
7614 /* Size the debugging information associated with a given DIE. Visits the
7615 DIE's children recursively. Updates the global variable next_die_offset, on
7616 each time through. Uses the current value of next_die_offset to update the
7617 die_offset field in each DIE. */
7619 static void
7620 calc_die_sizes (dw_die_ref die)
7622 dw_die_ref c;
7624 die->die_offset = next_die_offset;
7625 next_die_offset += size_of_die (die);
7627 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7629 if (die->die_child != NULL)
7630 /* Count the null byte used to terminate sibling lists. */
7631 next_die_offset += 1;
7634 /* Set the marks for a die and its children. We do this so
7635 that we know whether or not a reference needs to use FORM_ref_addr; only
7636 DIEs in the same CU will be marked. We used to clear out the offset
7637 and use that as the flag, but ran into ordering problems. */
7639 static void
7640 mark_dies (dw_die_ref die)
7642 dw_die_ref c;
7644 gcc_assert (!die->die_mark);
7646 die->die_mark = 1;
7647 FOR_EACH_CHILD (die, c, mark_dies (c));
7650 /* Clear the marks for a die and its children. */
7652 static void
7653 unmark_dies (dw_die_ref die)
7655 dw_die_ref c;
7657 gcc_assert (die->die_mark);
7659 die->die_mark = 0;
7660 FOR_EACH_CHILD (die, c, unmark_dies (c));
7663 /* Clear the marks for a die, its children and referred dies. */
7665 static void
7666 unmark_all_dies (dw_die_ref die)
7668 dw_die_ref c;
7669 dw_attr_ref a;
7670 unsigned ix;
7672 if (!die->die_mark)
7673 return;
7674 die->die_mark = 0;
7676 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7678 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7679 if (AT_class (a) == dw_val_class_die_ref)
7680 unmark_all_dies (AT_ref (a));
7683 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7684 generated for the compilation unit. */
7686 static unsigned long
7687 size_of_pubnames (VEC (pubname_entry, gc) * names)
7689 unsigned long size;
7690 unsigned i;
7691 pubname_ref p;
7693 size = DWARF_PUBNAMES_HEADER_SIZE;
7694 for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
7695 if (names != pubtype_table
7696 || p->die->die_offset != 0
7697 || !flag_eliminate_unused_debug_types)
7698 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7700 size += DWARF_OFFSET_SIZE;
7701 return size;
7704 /* Return the size of the information in the .debug_aranges section. */
7706 static unsigned long
7707 size_of_aranges (void)
7709 unsigned long size;
7711 size = DWARF_ARANGES_HEADER_SIZE;
7713 /* Count the address/length pair for this compilation unit. */
7714 if (text_section_used)
7715 size += 2 * DWARF2_ADDR_SIZE;
7716 if (cold_text_section_used)
7717 size += 2 * DWARF2_ADDR_SIZE;
7718 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
7720 /* Count the two zero words used to terminated the address range table. */
7721 size += 2 * DWARF2_ADDR_SIZE;
7722 return size;
7725 /* Select the encoding of an attribute value. */
7727 static enum dwarf_form
7728 value_format (dw_attr_ref a)
7730 switch (a->dw_attr_val.val_class)
7732 case dw_val_class_addr:
7733 return DW_FORM_addr;
7734 case dw_val_class_range_list:
7735 case dw_val_class_offset:
7736 case dw_val_class_loc_list:
7737 switch (DWARF_OFFSET_SIZE)
7739 case 4:
7740 return DW_FORM_data4;
7741 case 8:
7742 return DW_FORM_data8;
7743 default:
7744 gcc_unreachable ();
7746 case dw_val_class_loc:
7747 switch (constant_size (size_of_locs (AT_loc (a))))
7749 case 1:
7750 return DW_FORM_block1;
7751 case 2:
7752 return DW_FORM_block2;
7753 default:
7754 gcc_unreachable ();
7756 case dw_val_class_const:
7757 return DW_FORM_sdata;
7758 case dw_val_class_unsigned_const:
7759 switch (constant_size (AT_unsigned (a)))
7761 case 1:
7762 return DW_FORM_data1;
7763 case 2:
7764 return DW_FORM_data2;
7765 case 4:
7766 return DW_FORM_data4;
7767 case 8:
7768 return DW_FORM_data8;
7769 default:
7770 gcc_unreachable ();
7772 case dw_val_class_long_long:
7773 return DW_FORM_block1;
7774 case dw_val_class_vec:
7775 return DW_FORM_block1;
7776 case dw_val_class_flag:
7777 return DW_FORM_flag;
7778 case dw_val_class_die_ref:
7779 if (AT_ref_external (a))
7780 return DW_FORM_ref_addr;
7781 else
7782 return DW_FORM_ref;
7783 case dw_val_class_fde_ref:
7784 return DW_FORM_data;
7785 case dw_val_class_lbl_id:
7786 return DW_FORM_addr;
7787 case dw_val_class_lineptr:
7788 case dw_val_class_macptr:
7789 return DW_FORM_data;
7790 case dw_val_class_str:
7791 return AT_string_form (a);
7792 case dw_val_class_file:
7793 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7795 case 1:
7796 return DW_FORM_data1;
7797 case 2:
7798 return DW_FORM_data2;
7799 case 4:
7800 return DW_FORM_data4;
7801 default:
7802 gcc_unreachable ();
7805 default:
7806 gcc_unreachable ();
7810 /* Output the encoding of an attribute value. */
7812 static void
7813 output_value_format (dw_attr_ref a)
7815 enum dwarf_form form = value_format (a);
7817 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7820 /* Output the .debug_abbrev section which defines the DIE abbreviation
7821 table. */
7823 static void
7824 output_abbrev_section (void)
7826 unsigned long abbrev_id;
7828 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7830 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7831 unsigned ix;
7832 dw_attr_ref a_attr;
7834 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7835 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7836 dwarf_tag_name (abbrev->die_tag));
7838 if (abbrev->die_child != NULL)
7839 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7840 else
7841 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7843 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7844 ix++)
7846 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7847 dwarf_attr_name (a_attr->dw_attr));
7848 output_value_format (a_attr);
7851 dw2_asm_output_data (1, 0, NULL);
7852 dw2_asm_output_data (1, 0, NULL);
7855 /* Terminate the table. */
7856 dw2_asm_output_data (1, 0, NULL);
7859 /* Output a symbol we can use to refer to this DIE from another CU. */
7861 static inline void
7862 output_die_symbol (dw_die_ref die)
7864 char *sym = die->die_symbol;
7866 if (sym == 0)
7867 return;
7869 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7870 /* We make these global, not weak; if the target doesn't support
7871 .linkonce, it doesn't support combining the sections, so debugging
7872 will break. */
7873 targetm.asm_out.globalize_label (asm_out_file, sym);
7875 ASM_OUTPUT_LABEL (asm_out_file, sym);
7878 /* Return a new location list, given the begin and end range, and the
7879 expression. gensym tells us whether to generate a new internal symbol for
7880 this location list node, which is done for the head of the list only. */
7882 static inline dw_loc_list_ref
7883 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7884 const char *section, unsigned int gensym)
7886 dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7888 retlist->begin = begin;
7889 retlist->end = end;
7890 retlist->expr = expr;
7891 retlist->section = section;
7892 if (gensym)
7893 retlist->ll_symbol = gen_internal_sym ("LLST");
7895 return retlist;
7898 /* Add a location description expression to a location list. */
7900 static inline void
7901 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7902 const char *begin, const char *end,
7903 const char *section)
7905 dw_loc_list_ref *d;
7907 /* Find the end of the chain. */
7908 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7911 /* Add a new location list node to the list. */
7912 *d = new_loc_list (descr, begin, end, section, 0);
7915 /* Output the location list given to us. */
7917 static void
7918 output_loc_list (dw_loc_list_ref list_head)
7920 dw_loc_list_ref curr = list_head;
7922 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7924 /* Walk the location list, and output each range + expression. */
7925 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7927 unsigned long size;
7928 /* Don't output an entry that starts and ends at the same address. */
7929 if (strcmp (curr->begin, curr->end) == 0)
7930 continue;
7931 if (!have_multiple_function_sections)
7933 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7934 "Location list begin address (%s)",
7935 list_head->ll_symbol);
7936 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7937 "Location list end address (%s)",
7938 list_head->ll_symbol);
7940 else
7942 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7943 "Location list begin address (%s)",
7944 list_head->ll_symbol);
7945 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7946 "Location list end address (%s)",
7947 list_head->ll_symbol);
7949 size = size_of_locs (curr->expr);
7951 /* Output the block length for this list of location operations. */
7952 gcc_assert (size <= 0xffff);
7953 dw2_asm_output_data (2, size, "%s", "Location expression size");
7955 output_loc_sequence (curr->expr);
7958 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7959 "Location list terminator begin (%s)",
7960 list_head->ll_symbol);
7961 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7962 "Location list terminator end (%s)",
7963 list_head->ll_symbol);
7966 /* Output the DIE and its attributes. Called recursively to generate
7967 the definitions of each child DIE. */
7969 static void
7970 output_die (dw_die_ref die)
7972 dw_attr_ref a;
7973 dw_die_ref c;
7974 unsigned long size;
7975 unsigned ix;
7977 /* If someone in another CU might refer to us, set up a symbol for
7978 them to point to. */
7979 if (die->die_symbol)
7980 output_die_symbol (die);
7982 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7983 (unsigned long)die->die_offset,
7984 dwarf_tag_name (die->die_tag));
7986 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7988 const char *name = dwarf_attr_name (a->dw_attr);
7990 switch (AT_class (a))
7992 case dw_val_class_addr:
7993 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7994 break;
7996 case dw_val_class_offset:
7997 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7998 "%s", name);
7999 break;
8001 case dw_val_class_range_list:
8003 char *p = strchr (ranges_section_label, '\0');
8005 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8006 a->dw_attr_val.v.val_offset);
8007 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8008 debug_ranges_section, "%s", name);
8009 *p = '\0';
8011 break;
8013 case dw_val_class_loc:
8014 size = size_of_locs (AT_loc (a));
8016 /* Output the block length for this list of location operations. */
8017 dw2_asm_output_data (constant_size (size), size, "%s", name);
8019 output_loc_sequence (AT_loc (a));
8020 break;
8022 case dw_val_class_const:
8023 /* ??? It would be slightly more efficient to use a scheme like is
8024 used for unsigned constants below, but gdb 4.x does not sign
8025 extend. Gdb 5.x does sign extend. */
8026 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8027 break;
8029 case dw_val_class_unsigned_const:
8030 dw2_asm_output_data (constant_size (AT_unsigned (a)),
8031 AT_unsigned (a), "%s", name);
8032 break;
8034 case dw_val_class_long_long:
8036 unsigned HOST_WIDE_INT first, second;
8038 dw2_asm_output_data (1,
8039 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8040 "%s", name);
8042 if (WORDS_BIG_ENDIAN)
8044 first = a->dw_attr_val.v.val_long_long.hi;
8045 second = a->dw_attr_val.v.val_long_long.low;
8047 else
8049 first = a->dw_attr_val.v.val_long_long.low;
8050 second = a->dw_attr_val.v.val_long_long.hi;
8053 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8054 first, "long long constant");
8055 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8056 second, NULL);
8058 break;
8060 case dw_val_class_vec:
8062 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8063 unsigned int len = a->dw_attr_val.v.val_vec.length;
8064 unsigned int i;
8065 unsigned char *p;
8067 dw2_asm_output_data (1, len * elt_size, "%s", name);
8068 if (elt_size > sizeof (HOST_WIDE_INT))
8070 elt_size /= 2;
8071 len *= 2;
8073 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8074 i < len;
8075 i++, p += elt_size)
8076 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8077 "fp or vector constant word %u", i);
8078 break;
8081 case dw_val_class_flag:
8082 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8083 break;
8085 case dw_val_class_loc_list:
8087 char *sym = AT_loc_list (a)->ll_symbol;
8089 gcc_assert (sym);
8090 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8091 "%s", name);
8093 break;
8095 case dw_val_class_die_ref:
8096 if (AT_ref_external (a))
8098 char *sym = AT_ref (a)->die_symbol;
8100 gcc_assert (sym);
8101 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8102 "%s", name);
8104 else
8106 gcc_assert (AT_ref (a)->die_offset);
8107 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8108 "%s", name);
8110 break;
8112 case dw_val_class_fde_ref:
8114 char l1[20];
8116 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8117 a->dw_attr_val.v.val_fde_index * 2);
8118 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8119 "%s", name);
8121 break;
8123 case dw_val_class_lbl_id:
8124 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8125 break;
8127 case dw_val_class_lineptr:
8128 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8129 debug_line_section, "%s", name);
8130 break;
8132 case dw_val_class_macptr:
8133 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8134 debug_macinfo_section, "%s", name);
8135 break;
8137 case dw_val_class_str:
8138 if (AT_string_form (a) == DW_FORM_strp)
8139 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8140 a->dw_attr_val.v.val_str->label,
8141 debug_str_section,
8142 "%s: \"%s\"", name, AT_string (a));
8143 else
8144 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8145 break;
8147 case dw_val_class_file:
8149 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8151 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8152 a->dw_attr_val.v.val_file->filename);
8153 break;
8156 default:
8157 gcc_unreachable ();
8161 FOR_EACH_CHILD (die, c, output_die (c));
8163 /* Add null byte to terminate sibling list. */
8164 if (die->die_child != NULL)
8165 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8166 (unsigned long) die->die_offset);
8169 /* Output the compilation unit that appears at the beginning of the
8170 .debug_info section, and precedes the DIE descriptions. */
8172 static void
8173 output_compilation_unit_header (void)
8175 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8176 dw2_asm_output_data (4, 0xffffffff,
8177 "Initial length escape value indicating 64-bit DWARF extension");
8178 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8179 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8180 "Length of Compilation Unit Info");
8181 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8182 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8183 debug_abbrev_section,
8184 "Offset Into Abbrev. Section");
8185 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8188 /* Output the compilation unit DIE and its children. */
8190 static void
8191 output_comp_unit (dw_die_ref die, int output_if_empty)
8193 const char *secname;
8194 char *oldsym, *tmp;
8196 /* Unless we are outputting main CU, we may throw away empty ones. */
8197 if (!output_if_empty && die->die_child == NULL)
8198 return;
8200 /* Even if there are no children of this DIE, we must output the information
8201 about the compilation unit. Otherwise, on an empty translation unit, we
8202 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8203 will then complain when examining the file. First mark all the DIEs in
8204 this CU so we know which get local refs. */
8205 mark_dies (die);
8207 build_abbrev_table (die);
8209 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8210 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8211 calc_die_sizes (die);
8213 oldsym = die->die_symbol;
8214 if (oldsym)
8216 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8218 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8219 secname = tmp;
8220 die->die_symbol = NULL;
8221 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8223 else
8224 switch_to_section (debug_info_section);
8226 /* Output debugging information. */
8227 output_compilation_unit_header ();
8228 output_die (die);
8230 /* Leave the marks on the main CU, so we can check them in
8231 output_pubnames. */
8232 if (oldsym)
8234 unmark_dies (die);
8235 die->die_symbol = oldsym;
8239 /* Return the DWARF2/3 pubname associated with a decl. */
8241 static const char *
8242 dwarf2_name (tree decl, int scope)
8244 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8247 /* Add a new entry to .debug_pubnames if appropriate. */
8249 static void
8250 add_pubname_string (const char *str, dw_die_ref die)
8252 pubname_entry e;
8254 e.die = die;
8255 e.name = xstrdup (str);
8256 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8259 static void
8260 add_pubname (tree decl, dw_die_ref die)
8263 if (TREE_PUBLIC (decl))
8264 add_pubname_string (dwarf2_name (decl, 1), die);
8267 /* Add a new entry to .debug_pubtypes if appropriate. */
8269 static void
8270 add_pubtype (tree decl, dw_die_ref die)
8272 pubname_entry e;
8274 e.name = NULL;
8275 if ((TREE_PUBLIC (decl)
8276 || die->die_parent == comp_unit_die)
8277 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8279 e.die = die;
8280 if (TYPE_P (decl))
8282 if (TYPE_NAME (decl))
8284 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8285 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8286 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8287 && DECL_NAME (TYPE_NAME (decl)))
8288 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8289 else
8290 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8293 else
8294 e.name = xstrdup (dwarf2_name (decl, 1));
8296 /* If we don't have a name for the type, there's no point in adding
8297 it to the table. */
8298 if (e.name && e.name[0] != '\0')
8299 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8303 /* Output the public names table used to speed up access to externally
8304 visible names; or the public types table used to find type definitions. */
8306 static void
8307 output_pubnames (VEC (pubname_entry, gc) * names)
8309 unsigned i;
8310 unsigned long pubnames_length = size_of_pubnames (names);
8311 pubname_ref pub;
8313 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8314 dw2_asm_output_data (4, 0xffffffff,
8315 "Initial length escape value indicating 64-bit DWARF extension");
8316 if (names == pubname_table)
8317 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8318 "Length of Public Names Info");
8319 else
8320 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8321 "Length of Public Type Names Info");
8322 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8323 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8324 debug_info_section,
8325 "Offset of Compilation Unit Info");
8326 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8327 "Compilation Unit Length");
8329 for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8331 /* We shouldn't see pubnames for DIEs outside of the main CU. */
8332 if (names == pubname_table)
8333 gcc_assert (pub->die->die_mark);
8335 if (names != pubtype_table
8336 || pub->die->die_offset != 0
8337 || !flag_eliminate_unused_debug_types)
8339 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8340 "DIE offset");
8342 dw2_asm_output_nstring (pub->name, -1, "external name");
8346 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8349 /* Add a new entry to .debug_aranges if appropriate. */
8351 static void
8352 add_arange (tree decl, dw_die_ref die)
8354 if (! DECL_SECTION_NAME (decl))
8355 return;
8357 if (arange_table_in_use == arange_table_allocated)
8359 arange_table_allocated += ARANGE_TABLE_INCREMENT;
8360 arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8361 arange_table_allocated);
8362 memset (arange_table + arange_table_in_use, 0,
8363 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8366 arange_table[arange_table_in_use++] = die;
8369 /* Output the information that goes into the .debug_aranges table.
8370 Namely, define the beginning and ending address range of the
8371 text section generated for this compilation unit. */
8373 static void
8374 output_aranges (void)
8376 unsigned i;
8377 unsigned long aranges_length = size_of_aranges ();
8379 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8380 dw2_asm_output_data (4, 0xffffffff,
8381 "Initial length escape value indicating 64-bit DWARF extension");
8382 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8383 "Length of Address Ranges Info");
8384 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8385 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8386 debug_info_section,
8387 "Offset of Compilation Unit Info");
8388 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8389 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8391 /* We need to align to twice the pointer size here. */
8392 if (DWARF_ARANGES_PAD_SIZE)
8394 /* Pad using a 2 byte words so that padding is correct for any
8395 pointer size. */
8396 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8397 2 * DWARF2_ADDR_SIZE);
8398 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8399 dw2_asm_output_data (2, 0, NULL);
8402 /* It is necessary not to output these entries if the sections were
8403 not used; if the sections were not used, the length will be 0 and
8404 the address may end up as 0 if the section is discarded by ld
8405 --gc-sections, leaving an invalid (0, 0) entry that can be
8406 confused with the terminator. */
8407 if (text_section_used)
8409 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8410 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8411 text_section_label, "Length");
8413 if (cold_text_section_used)
8415 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8416 "Address");
8417 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8418 cold_text_section_label, "Length");
8421 for (i = 0; i < arange_table_in_use; i++)
8423 dw_die_ref die = arange_table[i];
8425 /* We shouldn't see aranges for DIEs outside of the main CU. */
8426 gcc_assert (die->die_mark);
8428 if (die->die_tag == DW_TAG_subprogram)
8430 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8431 "Address");
8432 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8433 get_AT_low_pc (die), "Length");
8435 else
8437 /* A static variable; extract the symbol from DW_AT_location.
8438 Note that this code isn't currently hit, as we only emit
8439 aranges for functions (jason 9/23/99). */
8440 dw_attr_ref a = get_AT (die, DW_AT_location);
8441 dw_loc_descr_ref loc;
8443 gcc_assert (a && AT_class (a) == dw_val_class_loc);
8445 loc = AT_loc (a);
8446 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8448 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8449 loc->dw_loc_oprnd1.v.val_addr, "Address");
8450 dw2_asm_output_data (DWARF2_ADDR_SIZE,
8451 get_AT_unsigned (die, DW_AT_byte_size),
8452 "Length");
8456 /* Output the terminator words. */
8457 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8458 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8461 /* Add a new entry to .debug_ranges. Return the offset at which it
8462 was placed. */
8464 static unsigned int
8465 add_ranges_num (int num)
8467 unsigned int in_use = ranges_table_in_use;
8469 if (in_use == ranges_table_allocated)
8471 ranges_table_allocated += RANGES_TABLE_INCREMENT;
8472 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8473 ranges_table_allocated);
8474 memset (ranges_table + ranges_table_in_use, 0,
8475 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8478 ranges_table[in_use].num = num;
8479 ranges_table_in_use = in_use + 1;
8481 return in_use * 2 * DWARF2_ADDR_SIZE;
8484 /* Add a new entry to .debug_ranges corresponding to a block, or a
8485 range terminator if BLOCK is NULL. */
8487 static unsigned int
8488 add_ranges (const_tree block)
8490 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8493 /* Add a new entry to .debug_ranges corresponding to a pair of
8494 labels. */
8496 static unsigned int
8497 add_ranges_by_labels (const char *begin, const char *end)
8499 unsigned int in_use = ranges_by_label_in_use;
8501 if (in_use == ranges_by_label_allocated)
8503 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8504 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8505 ranges_by_label,
8506 ranges_by_label_allocated);
8507 memset (ranges_by_label + ranges_by_label_in_use, 0,
8508 RANGES_TABLE_INCREMENT
8509 * sizeof (struct dw_ranges_by_label_struct));
8512 ranges_by_label[in_use].begin = begin;
8513 ranges_by_label[in_use].end = end;
8514 ranges_by_label_in_use = in_use + 1;
8516 return add_ranges_num (-(int)in_use - 1);
8519 static void
8520 output_ranges (void)
8522 unsigned i;
8523 static const char *const start_fmt = "Offset 0x%x";
8524 const char *fmt = start_fmt;
8526 for (i = 0; i < ranges_table_in_use; i++)
8528 int block_num = ranges_table[i].num;
8530 if (block_num > 0)
8532 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8533 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8535 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8536 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8538 /* If all code is in the text section, then the compilation
8539 unit base address defaults to DW_AT_low_pc, which is the
8540 base of the text section. */
8541 if (!have_multiple_function_sections)
8543 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8544 text_section_label,
8545 fmt, i * 2 * DWARF2_ADDR_SIZE);
8546 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8547 text_section_label, NULL);
8550 /* Otherwise, the compilation unit base address is zero,
8551 which allows us to use absolute addresses, and not worry
8552 about whether the target supports cross-section
8553 arithmetic. */
8554 else
8556 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8557 fmt, i * 2 * DWARF2_ADDR_SIZE);
8558 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8561 fmt = NULL;
8564 /* Negative block_num stands for an index into ranges_by_label. */
8565 else if (block_num < 0)
8567 int lab_idx = - block_num - 1;
8569 if (!have_multiple_function_sections)
8571 gcc_unreachable ();
8572 #if 0
8573 /* If we ever use add_ranges_by_labels () for a single
8574 function section, all we have to do is to take out
8575 the #if 0 above. */
8576 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8577 ranges_by_label[lab_idx].begin,
8578 text_section_label,
8579 fmt, i * 2 * DWARF2_ADDR_SIZE);
8580 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8581 ranges_by_label[lab_idx].end,
8582 text_section_label, NULL);
8583 #endif
8585 else
8587 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8588 ranges_by_label[lab_idx].begin,
8589 fmt, i * 2 * DWARF2_ADDR_SIZE);
8590 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8591 ranges_by_label[lab_idx].end,
8592 NULL);
8595 else
8597 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8598 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8599 fmt = start_fmt;
8604 /* Data structure containing information about input files. */
8605 struct file_info
8607 const char *path; /* Complete file name. */
8608 const char *fname; /* File name part. */
8609 int length; /* Length of entire string. */
8610 struct dwarf_file_data * file_idx; /* Index in input file table. */
8611 int dir_idx; /* Index in directory table. */
8614 /* Data structure containing information about directories with source
8615 files. */
8616 struct dir_info
8618 const char *path; /* Path including directory name. */
8619 int length; /* Path length. */
8620 int prefix; /* Index of directory entry which is a prefix. */
8621 int count; /* Number of files in this directory. */
8622 int dir_idx; /* Index of directory used as base. */
8625 /* Callback function for file_info comparison. We sort by looking at
8626 the directories in the path. */
8628 static int
8629 file_info_cmp (const void *p1, const void *p2)
8631 const struct file_info *const s1 = (const struct file_info *) p1;
8632 const struct file_info *const s2 = (const struct file_info *) p2;
8633 const unsigned char *cp1;
8634 const unsigned char *cp2;
8636 /* Take care of file names without directories. We need to make sure that
8637 we return consistent values to qsort since some will get confused if
8638 we return the same value when identical operands are passed in opposite
8639 orders. So if neither has a directory, return 0 and otherwise return
8640 1 or -1 depending on which one has the directory. */
8641 if ((s1->path == s1->fname || s2->path == s2->fname))
8642 return (s2->path == s2->fname) - (s1->path == s1->fname);
8644 cp1 = (const unsigned char *) s1->path;
8645 cp2 = (const unsigned char *) s2->path;
8647 while (1)
8649 ++cp1;
8650 ++cp2;
8651 /* Reached the end of the first path? If so, handle like above. */
8652 if ((cp1 == (const unsigned char *) s1->fname)
8653 || (cp2 == (const unsigned char *) s2->fname))
8654 return ((cp2 == (const unsigned char *) s2->fname)
8655 - (cp1 == (const unsigned char *) s1->fname));
8657 /* Character of current path component the same? */
8658 else if (*cp1 != *cp2)
8659 return *cp1 - *cp2;
8663 struct file_name_acquire_data
8665 struct file_info *files;
8666 int used_files;
8667 int max_files;
8670 /* Traversal function for the hash table. */
8672 static int
8673 file_name_acquire (void ** slot, void *data)
8675 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8676 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8677 struct file_info *fi;
8678 const char *f;
8680 gcc_assert (fnad->max_files >= d->emitted_number);
8682 if (! d->emitted_number)
8683 return 1;
8685 gcc_assert (fnad->max_files != fnad->used_files);
8687 fi = fnad->files + fnad->used_files++;
8689 /* Skip all leading "./". */
8690 f = d->filename;
8691 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8692 f += 2;
8694 /* Create a new array entry. */
8695 fi->path = f;
8696 fi->length = strlen (f);
8697 fi->file_idx = d;
8699 /* Search for the file name part. */
8700 f = strrchr (f, DIR_SEPARATOR);
8701 #if defined (DIR_SEPARATOR_2)
8703 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8705 if (g != NULL)
8707 if (f == NULL || f < g)
8708 f = g;
8711 #endif
8713 fi->fname = f == NULL ? fi->path : f + 1;
8714 return 1;
8717 /* Output the directory table and the file name table. We try to minimize
8718 the total amount of memory needed. A heuristic is used to avoid large
8719 slowdowns with many input files. */
8721 static void
8722 output_file_names (void)
8724 struct file_name_acquire_data fnad;
8725 int numfiles;
8726 struct file_info *files;
8727 struct dir_info *dirs;
8728 int *saved;
8729 int *savehere;
8730 int *backmap;
8731 int ndirs;
8732 int idx_offset;
8733 int i;
8734 int idx;
8736 if (!last_emitted_file)
8738 dw2_asm_output_data (1, 0, "End directory table");
8739 dw2_asm_output_data (1, 0, "End file name table");
8740 return;
8743 numfiles = last_emitted_file->emitted_number;
8745 /* Allocate the various arrays we need. */
8746 files = XALLOCAVEC (struct file_info, numfiles);
8747 dirs = XALLOCAVEC (struct dir_info, numfiles);
8749 fnad.files = files;
8750 fnad.used_files = 0;
8751 fnad.max_files = numfiles;
8752 htab_traverse (file_table, file_name_acquire, &fnad);
8753 gcc_assert (fnad.used_files == fnad.max_files);
8755 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8757 /* Find all the different directories used. */
8758 dirs[0].path = files[0].path;
8759 dirs[0].length = files[0].fname - files[0].path;
8760 dirs[0].prefix = -1;
8761 dirs[0].count = 1;
8762 dirs[0].dir_idx = 0;
8763 files[0].dir_idx = 0;
8764 ndirs = 1;
8766 for (i = 1; i < numfiles; i++)
8767 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8768 && memcmp (dirs[ndirs - 1].path, files[i].path,
8769 dirs[ndirs - 1].length) == 0)
8771 /* Same directory as last entry. */
8772 files[i].dir_idx = ndirs - 1;
8773 ++dirs[ndirs - 1].count;
8775 else
8777 int j;
8779 /* This is a new directory. */
8780 dirs[ndirs].path = files[i].path;
8781 dirs[ndirs].length = files[i].fname - files[i].path;
8782 dirs[ndirs].count = 1;
8783 dirs[ndirs].dir_idx = ndirs;
8784 files[i].dir_idx = ndirs;
8786 /* Search for a prefix. */
8787 dirs[ndirs].prefix = -1;
8788 for (j = 0; j < ndirs; j++)
8789 if (dirs[j].length < dirs[ndirs].length
8790 && dirs[j].length > 1
8791 && (dirs[ndirs].prefix == -1
8792 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8793 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8794 dirs[ndirs].prefix = j;
8796 ++ndirs;
8799 /* Now to the actual work. We have to find a subset of the directories which
8800 allow expressing the file name using references to the directory table
8801 with the least amount of characters. We do not do an exhaustive search
8802 where we would have to check out every combination of every single
8803 possible prefix. Instead we use a heuristic which provides nearly optimal
8804 results in most cases and never is much off. */
8805 saved = XALLOCAVEC (int, ndirs);
8806 savehere = XALLOCAVEC (int, ndirs);
8808 memset (saved, '\0', ndirs * sizeof (saved[0]));
8809 for (i = 0; i < ndirs; i++)
8811 int j;
8812 int total;
8814 /* We can always save some space for the current directory. But this
8815 does not mean it will be enough to justify adding the directory. */
8816 savehere[i] = dirs[i].length;
8817 total = (savehere[i] - saved[i]) * dirs[i].count;
8819 for (j = i + 1; j < ndirs; j++)
8821 savehere[j] = 0;
8822 if (saved[j] < dirs[i].length)
8824 /* Determine whether the dirs[i] path is a prefix of the
8825 dirs[j] path. */
8826 int k;
8828 k = dirs[j].prefix;
8829 while (k != -1 && k != (int) i)
8830 k = dirs[k].prefix;
8832 if (k == (int) i)
8834 /* Yes it is. We can possibly save some memory by
8835 writing the filenames in dirs[j] relative to
8836 dirs[i]. */
8837 savehere[j] = dirs[i].length;
8838 total += (savehere[j] - saved[j]) * dirs[j].count;
8843 /* Check whether we can save enough to justify adding the dirs[i]
8844 directory. */
8845 if (total > dirs[i].length + 1)
8847 /* It's worthwhile adding. */
8848 for (j = i; j < ndirs; j++)
8849 if (savehere[j] > 0)
8851 /* Remember how much we saved for this directory so far. */
8852 saved[j] = savehere[j];
8854 /* Remember the prefix directory. */
8855 dirs[j].dir_idx = i;
8860 /* Emit the directory name table. */
8861 idx = 1;
8862 idx_offset = dirs[0].length > 0 ? 1 : 0;
8863 for (i = 1 - idx_offset; i < ndirs; i++)
8864 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8865 "Directory Entry: 0x%x", i + idx_offset);
8867 dw2_asm_output_data (1, 0, "End directory table");
8869 /* We have to emit them in the order of emitted_number since that's
8870 used in the debug info generation. To do this efficiently we
8871 generate a back-mapping of the indices first. */
8872 backmap = XALLOCAVEC (int, numfiles);
8873 for (i = 0; i < numfiles; i++)
8874 backmap[files[i].file_idx->emitted_number - 1] = i;
8876 /* Now write all the file names. */
8877 for (i = 0; i < numfiles; i++)
8879 int file_idx = backmap[i];
8880 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8882 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8883 "File Entry: 0x%x", (unsigned) i + 1);
8885 /* Include directory index. */
8886 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8888 /* Modification time. */
8889 dw2_asm_output_data_uleb128 (0, NULL);
8891 /* File length in bytes. */
8892 dw2_asm_output_data_uleb128 (0, NULL);
8895 dw2_asm_output_data (1, 0, "End file name table");
8899 /* Output the source line number correspondence information. This
8900 information goes into the .debug_line section. */
8902 static void
8903 output_line_info (void)
8905 char l1[20], l2[20], p1[20], p2[20];
8906 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8907 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8908 unsigned opc;
8909 unsigned n_op_args;
8910 unsigned long lt_index;
8911 unsigned long current_line;
8912 long line_offset;
8913 long line_delta;
8914 unsigned long current_file;
8915 unsigned long function;
8917 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8918 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8919 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8920 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8922 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8923 dw2_asm_output_data (4, 0xffffffff,
8924 "Initial length escape value indicating 64-bit DWARF extension");
8925 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8926 "Length of Source Line Info");
8927 ASM_OUTPUT_LABEL (asm_out_file, l1);
8929 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8930 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8931 ASM_OUTPUT_LABEL (asm_out_file, p1);
8933 /* Define the architecture-dependent minimum instruction length (in
8934 bytes). In this implementation of DWARF, this field is used for
8935 information purposes only. Since GCC generates assembly language,
8936 we have no a priori knowledge of how many instruction bytes are
8937 generated for each source line, and therefore can use only the
8938 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8939 commands. Accordingly, we fix this as `1', which is "correct
8940 enough" for all architectures, and don't let the target override. */
8941 dw2_asm_output_data (1, 1,
8942 "Minimum Instruction Length");
8944 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8945 "Default is_stmt_start flag");
8946 dw2_asm_output_data (1, DWARF_LINE_BASE,
8947 "Line Base Value (Special Opcodes)");
8948 dw2_asm_output_data (1, DWARF_LINE_RANGE,
8949 "Line Range Value (Special Opcodes)");
8950 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8951 "Special Opcode Base");
8953 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8955 switch (opc)
8957 case DW_LNS_advance_pc:
8958 case DW_LNS_advance_line:
8959 case DW_LNS_set_file:
8960 case DW_LNS_set_column:
8961 case DW_LNS_fixed_advance_pc:
8962 n_op_args = 1;
8963 break;
8964 default:
8965 n_op_args = 0;
8966 break;
8969 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8970 opc, n_op_args);
8973 /* Write out the information about the files we use. */
8974 output_file_names ();
8975 ASM_OUTPUT_LABEL (asm_out_file, p2);
8977 /* We used to set the address register to the first location in the text
8978 section here, but that didn't accomplish anything since we already
8979 have a line note for the opening brace of the first function. */
8981 /* Generate the line number to PC correspondence table, encoded as
8982 a series of state machine operations. */
8983 current_file = 1;
8984 current_line = 1;
8986 if (cfun && in_cold_section_p)
8987 strcpy (prev_line_label, crtl->subsections.cold_section_label);
8988 else
8989 strcpy (prev_line_label, text_section_label);
8990 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8992 dw_line_info_ref line_info = &line_info_table[lt_index];
8994 #if 0
8995 /* Disable this optimization for now; GDB wants to see two line notes
8996 at the beginning of a function so it can find the end of the
8997 prologue. */
8999 /* Don't emit anything for redundant notes. Just updating the
9000 address doesn't accomplish anything, because we already assume
9001 that anything after the last address is this line. */
9002 if (line_info->dw_line_num == current_line
9003 && line_info->dw_file_num == current_file)
9004 continue;
9005 #endif
9007 /* Emit debug info for the address of the current line.
9009 Unfortunately, we have little choice here currently, and must always
9010 use the most general form. GCC does not know the address delta
9011 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
9012 attributes which will give an upper bound on the address range. We
9013 could perhaps use length attributes to determine when it is safe to
9014 use DW_LNS_fixed_advance_pc. */
9016 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9017 if (0)
9019 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
9020 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9021 "DW_LNS_fixed_advance_pc");
9022 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9024 else
9026 /* This can handle any delta. This takes
9027 4+DWARF2_ADDR_SIZE bytes. */
9028 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9029 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9030 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9031 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9034 strcpy (prev_line_label, line_label);
9036 /* Emit debug info for the source file of the current line, if
9037 different from the previous line. */
9038 if (line_info->dw_file_num != current_file)
9040 current_file = line_info->dw_file_num;
9041 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9042 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9045 /* Emit debug info for the current line number, choosing the encoding
9046 that uses the least amount of space. */
9047 if (line_info->dw_line_num != current_line)
9049 line_offset = line_info->dw_line_num - current_line;
9050 line_delta = line_offset - DWARF_LINE_BASE;
9051 current_line = line_info->dw_line_num;
9052 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9053 /* This can handle deltas from -10 to 234, using the current
9054 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
9055 takes 1 byte. */
9056 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9057 "line %lu", current_line);
9058 else
9060 /* This can handle any delta. This takes at least 4 bytes,
9061 depending on the value being encoded. */
9062 dw2_asm_output_data (1, DW_LNS_advance_line,
9063 "advance to line %lu", current_line);
9064 dw2_asm_output_data_sleb128 (line_offset, NULL);
9065 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9068 else
9069 /* We still need to start a new row, so output a copy insn. */
9070 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9073 /* Emit debug info for the address of the end of the function. */
9074 if (0)
9076 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9077 "DW_LNS_fixed_advance_pc");
9078 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9080 else
9082 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9083 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9084 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9085 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9088 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9089 dw2_asm_output_data_uleb128 (1, NULL);
9090 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9092 function = 0;
9093 current_file = 1;
9094 current_line = 1;
9095 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9097 dw_separate_line_info_ref line_info
9098 = &separate_line_info_table[lt_index];
9100 #if 0
9101 /* Don't emit anything for redundant notes. */
9102 if (line_info->dw_line_num == current_line
9103 && line_info->dw_file_num == current_file
9104 && line_info->function == function)
9105 goto cont;
9106 #endif
9108 /* Emit debug info for the address of the current line. If this is
9109 a new function, or the first line of a function, then we need
9110 to handle it differently. */
9111 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9112 lt_index);
9113 if (function != line_info->function)
9115 function = line_info->function;
9117 /* Set the address register to the first line in the function. */
9118 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9119 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9120 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9121 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9123 else
9125 /* ??? See the DW_LNS_advance_pc comment above. */
9126 if (0)
9128 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9129 "DW_LNS_fixed_advance_pc");
9130 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9132 else
9134 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9135 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9136 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9137 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9141 strcpy (prev_line_label, line_label);
9143 /* Emit debug info for the source file of the current line, if
9144 different from the previous line. */
9145 if (line_info->dw_file_num != current_file)
9147 current_file = line_info->dw_file_num;
9148 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9149 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9152 /* Emit debug info for the current line number, choosing the encoding
9153 that uses the least amount of space. */
9154 if (line_info->dw_line_num != current_line)
9156 line_offset = line_info->dw_line_num - current_line;
9157 line_delta = line_offset - DWARF_LINE_BASE;
9158 current_line = line_info->dw_line_num;
9159 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9160 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9161 "line %lu", current_line);
9162 else
9164 dw2_asm_output_data (1, DW_LNS_advance_line,
9165 "advance to line %lu", current_line);
9166 dw2_asm_output_data_sleb128 (line_offset, NULL);
9167 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9170 else
9171 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9173 #if 0
9174 cont:
9175 #endif
9177 lt_index++;
9179 /* If we're done with a function, end its sequence. */
9180 if (lt_index == separate_line_info_table_in_use
9181 || separate_line_info_table[lt_index].function != function)
9183 current_file = 1;
9184 current_line = 1;
9186 /* Emit debug info for the address of the end of the function. */
9187 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9188 if (0)
9190 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9191 "DW_LNS_fixed_advance_pc");
9192 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9194 else
9196 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9197 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9198 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9199 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9202 /* Output the marker for the end of this sequence. */
9203 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9204 dw2_asm_output_data_uleb128 (1, NULL);
9205 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9209 /* Output the marker for the end of the line number info. */
9210 ASM_OUTPUT_LABEL (asm_out_file, l2);
9213 /* Given a pointer to a tree node for some base type, return a pointer to
9214 a DIE that describes the given type.
9216 This routine must only be called for GCC type nodes that correspond to
9217 Dwarf base (fundamental) types. */
9219 static dw_die_ref
9220 base_type_die (tree type)
9222 dw_die_ref base_type_result;
9223 enum dwarf_type encoding;
9225 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9226 return 0;
9228 switch (TREE_CODE (type))
9230 case INTEGER_TYPE:
9231 if (TYPE_STRING_FLAG (type))
9233 if (TYPE_UNSIGNED (type))
9234 encoding = DW_ATE_unsigned_char;
9235 else
9236 encoding = DW_ATE_signed_char;
9238 else if (TYPE_UNSIGNED (type))
9239 encoding = DW_ATE_unsigned;
9240 else
9241 encoding = DW_ATE_signed;
9242 break;
9244 case REAL_TYPE:
9245 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9246 encoding = DW_ATE_decimal_float;
9247 else
9248 encoding = DW_ATE_float;
9249 break;
9251 case FIXED_POINT_TYPE:
9252 if (TYPE_UNSIGNED (type))
9253 encoding = DW_ATE_unsigned_fixed;
9254 else
9255 encoding = DW_ATE_signed_fixed;
9256 break;
9258 /* Dwarf2 doesn't know anything about complex ints, so use
9259 a user defined type for it. */
9260 case COMPLEX_TYPE:
9261 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9262 encoding = DW_ATE_complex_float;
9263 else
9264 encoding = DW_ATE_lo_user;
9265 break;
9267 case BOOLEAN_TYPE:
9268 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
9269 encoding = DW_ATE_boolean;
9270 break;
9272 default:
9273 /* No other TREE_CODEs are Dwarf fundamental types. */
9274 gcc_unreachable ();
9277 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9279 /* This probably indicates a bug. */
9280 if (! TYPE_NAME (type))
9281 add_name_attribute (base_type_result, "__unknown__");
9283 add_AT_unsigned (base_type_result, DW_AT_byte_size,
9284 int_size_in_bytes (type));
9285 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9287 return base_type_result;
9290 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9291 given input type is a Dwarf "fundamental" type. Otherwise return null. */
9293 static inline int
9294 is_base_type (tree type)
9296 switch (TREE_CODE (type))
9298 case ERROR_MARK:
9299 case VOID_TYPE:
9300 case INTEGER_TYPE:
9301 case REAL_TYPE:
9302 case FIXED_POINT_TYPE:
9303 case COMPLEX_TYPE:
9304 case BOOLEAN_TYPE:
9305 return 1;
9307 case ARRAY_TYPE:
9308 case RECORD_TYPE:
9309 case UNION_TYPE:
9310 case QUAL_UNION_TYPE:
9311 case ENUMERAL_TYPE:
9312 case FUNCTION_TYPE:
9313 case METHOD_TYPE:
9314 case POINTER_TYPE:
9315 case REFERENCE_TYPE:
9316 case OFFSET_TYPE:
9317 case LANG_TYPE:
9318 case VECTOR_TYPE:
9319 return 0;
9321 default:
9322 gcc_unreachable ();
9325 return 0;
9328 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9329 node, return the size in bits for the type if it is a constant, or else
9330 return the alignment for the type if the type's size is not constant, or
9331 else return BITS_PER_WORD if the type actually turns out to be an
9332 ERROR_MARK node. */
9334 static inline unsigned HOST_WIDE_INT
9335 simple_type_size_in_bits (const_tree type)
9337 if (TREE_CODE (type) == ERROR_MARK)
9338 return BITS_PER_WORD;
9339 else if (TYPE_SIZE (type) == NULL_TREE)
9340 return 0;
9341 else if (host_integerp (TYPE_SIZE (type), 1))
9342 return tree_low_cst (TYPE_SIZE (type), 1);
9343 else
9344 return TYPE_ALIGN (type);
9347 /* Return true if the debug information for the given type should be
9348 emitted as a subrange type. */
9350 static inline bool
9351 is_subrange_type (const_tree type)
9353 tree subtype = TREE_TYPE (type);
9355 /* Subrange types are identified by the fact that they are integer
9356 types, and that they have a subtype which is either an integer type
9357 or an enumeral type. */
9359 if (TREE_CODE (type) != INTEGER_TYPE
9360 || subtype == NULL_TREE)
9361 return false;
9363 if (TREE_CODE (subtype) != INTEGER_TYPE
9364 && TREE_CODE (subtype) != ENUMERAL_TYPE
9365 && TREE_CODE (subtype) != BOOLEAN_TYPE)
9366 return false;
9368 if (TREE_CODE (type) == TREE_CODE (subtype)
9369 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
9370 && TYPE_MIN_VALUE (type) != NULL
9371 && TYPE_MIN_VALUE (subtype) != NULL
9372 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
9373 && TYPE_MAX_VALUE (type) != NULL
9374 && TYPE_MAX_VALUE (subtype) != NULL
9375 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
9377 /* The type and its subtype have the same representation. If in
9378 addition the two types also have the same name, then the given
9379 type is not a subrange type, but rather a plain base type. */
9380 /* FIXME: brobecker/2004-03-22:
9381 Sizetype INTEGER_CSTs nodes are canonicalized. It should
9382 therefore be sufficient to check the TYPE_SIZE node pointers
9383 rather than checking the actual size. Unfortunately, we have
9384 found some cases, such as in the Ada "integer" type, where
9385 this is not the case. Until this problem is solved, we need to
9386 keep checking the actual size. */
9387 tree type_name = TYPE_NAME (type);
9388 tree subtype_name = TYPE_NAME (subtype);
9390 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
9391 type_name = DECL_NAME (type_name);
9393 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
9394 subtype_name = DECL_NAME (subtype_name);
9396 if (type_name == subtype_name)
9397 return false;
9400 return true;
9403 /* Given a pointer to a tree node for a subrange type, return a pointer
9404 to a DIE that describes the given type. */
9406 static dw_die_ref
9407 subrange_type_die (tree type, dw_die_ref context_die)
9409 dw_die_ref subrange_die;
9410 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9412 if (context_die == NULL)
9413 context_die = comp_unit_die;
9415 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9417 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9419 /* The size of the subrange type and its base type do not match,
9420 so we need to generate a size attribute for the subrange type. */
9421 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9424 if (TYPE_MIN_VALUE (type) != NULL)
9425 add_bound_info (subrange_die, DW_AT_lower_bound,
9426 TYPE_MIN_VALUE (type));
9427 if (TYPE_MAX_VALUE (type) != NULL)
9428 add_bound_info (subrange_die, DW_AT_upper_bound,
9429 TYPE_MAX_VALUE (type));
9431 return subrange_die;
9434 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9435 entry that chains various modifiers in front of the given type. */
9437 static dw_die_ref
9438 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9439 dw_die_ref context_die)
9441 enum tree_code code = TREE_CODE (type);
9442 dw_die_ref mod_type_die;
9443 dw_die_ref sub_die = NULL;
9444 tree item_type = NULL;
9445 tree qualified_type;
9446 tree name;
9448 if (code == ERROR_MARK)
9449 return NULL;
9451 /* See if we already have the appropriately qualified variant of
9452 this type. */
9453 qualified_type
9454 = get_qualified_type (type,
9455 ((is_const_type ? TYPE_QUAL_CONST : 0)
9456 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9458 /* If we do, then we can just use its DIE, if it exists. */
9459 if (qualified_type)
9461 mod_type_die = lookup_type_die (qualified_type);
9462 if (mod_type_die)
9463 return mod_type_die;
9466 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9468 /* Handle C typedef types. */
9469 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9471 tree dtype = TREE_TYPE (name);
9473 if (qualified_type == dtype)
9475 /* For a named type, use the typedef. */
9476 gen_type_die (qualified_type, context_die);
9477 return lookup_type_die (qualified_type);
9479 else if (is_const_type < TYPE_READONLY (dtype)
9480 || is_volatile_type < TYPE_VOLATILE (dtype)
9481 || (is_const_type <= TYPE_READONLY (dtype)
9482 && is_volatile_type <= TYPE_VOLATILE (dtype)
9483 && DECL_ORIGINAL_TYPE (name) != type))
9484 /* cv-unqualified version of named type. Just use the unnamed
9485 type to which it refers. */
9486 return modified_type_die (DECL_ORIGINAL_TYPE (name),
9487 is_const_type, is_volatile_type,
9488 context_die);
9489 /* Else cv-qualified version of named type; fall through. */
9492 if (is_const_type)
9494 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9495 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9497 else if (is_volatile_type)
9499 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9500 sub_die = modified_type_die (type, 0, 0, context_die);
9502 else if (code == POINTER_TYPE)
9504 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9505 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9506 simple_type_size_in_bits (type) / BITS_PER_UNIT);
9507 item_type = TREE_TYPE (type);
9509 else if (code == REFERENCE_TYPE)
9511 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9512 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9513 simple_type_size_in_bits (type) / BITS_PER_UNIT);
9514 item_type = TREE_TYPE (type);
9516 else if (is_subrange_type (type))
9518 mod_type_die = subrange_type_die (type, context_die);
9519 item_type = TREE_TYPE (type);
9521 else if (is_base_type (type))
9522 mod_type_die = base_type_die (type);
9523 else
9525 gen_type_die (type, context_die);
9527 /* We have to get the type_main_variant here (and pass that to the
9528 `lookup_type_die' routine) because the ..._TYPE node we have
9529 might simply be a *copy* of some original type node (where the
9530 copy was created to help us keep track of typedef names) and
9531 that copy might have a different TYPE_UID from the original
9532 ..._TYPE node. */
9533 if (TREE_CODE (type) != VECTOR_TYPE)
9534 return lookup_type_die (type_main_variant (type));
9535 else
9536 /* Vectors have the debugging information in the type,
9537 not the main variant. */
9538 return lookup_type_die (type);
9541 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
9542 don't output a DW_TAG_typedef, since there isn't one in the
9543 user's program; just attach a DW_AT_name to the type. */
9544 if (name
9545 && (TREE_CODE (name) != TYPE_DECL
9546 || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9548 if (TREE_CODE (name) == TYPE_DECL)
9549 /* Could just call add_name_and_src_coords_attributes here,
9550 but since this is a builtin type it doesn't have any
9551 useful source coordinates anyway. */
9552 name = DECL_NAME (name);
9553 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9556 if (qualified_type)
9557 equate_type_number_to_die (qualified_type, mod_type_die);
9559 if (item_type)
9560 /* We must do this after the equate_type_number_to_die call, in case
9561 this is a recursive type. This ensures that the modified_type_die
9562 recursion will terminate even if the type is recursive. Recursive
9563 types are possible in Ada. */
9564 sub_die = modified_type_die (item_type,
9565 TYPE_READONLY (item_type),
9566 TYPE_VOLATILE (item_type),
9567 context_die);
9569 if (sub_die != NULL)
9570 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9572 return mod_type_die;
9575 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9576 an enumerated type. */
9578 static inline int
9579 type_is_enum (const_tree type)
9581 return TREE_CODE (type) == ENUMERAL_TYPE;
9584 /* Return the DBX register number described by a given RTL node. */
9586 static unsigned int
9587 dbx_reg_number (const_rtx rtl)
9589 unsigned regno = REGNO (rtl);
9591 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9593 #ifdef LEAF_REG_REMAP
9594 if (current_function_uses_only_leaf_regs)
9596 int leaf_reg = LEAF_REG_REMAP (regno);
9597 if (leaf_reg != -1)
9598 regno = (unsigned) leaf_reg;
9600 #endif
9602 return DBX_REGISTER_NUMBER (regno);
9605 /* Optionally add a DW_OP_piece term to a location description expression.
9606 DW_OP_piece is only added if the location description expression already
9607 doesn't end with DW_OP_piece. */
9609 static void
9610 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9612 dw_loc_descr_ref loc;
9614 if (*list_head != NULL)
9616 /* Find the end of the chain. */
9617 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9620 if (loc->dw_loc_opc != DW_OP_piece)
9621 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9625 /* Return a location descriptor that designates a machine register or
9626 zero if there is none. */
9628 static dw_loc_descr_ref
9629 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9631 rtx regs;
9633 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9634 return 0;
9636 regs = targetm.dwarf_register_span (rtl);
9638 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9639 return multiple_reg_loc_descriptor (rtl, regs, initialized);
9640 else
9641 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9644 /* Return a location descriptor that designates a machine register for
9645 a given hard register number. */
9647 static dw_loc_descr_ref
9648 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9650 dw_loc_descr_ref reg_loc_descr = new_reg_loc_descr (regno, 0);
9652 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9653 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9655 return reg_loc_descr;
9658 /* Given an RTL of a register, return a location descriptor that
9659 designates a value that spans more than one register. */
9661 static dw_loc_descr_ref
9662 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9663 enum var_init_status initialized)
9665 int nregs, size, i;
9666 unsigned reg;
9667 dw_loc_descr_ref loc_result = NULL;
9669 reg = REGNO (rtl);
9670 #ifdef LEAF_REG_REMAP
9671 if (current_function_uses_only_leaf_regs)
9673 int leaf_reg = LEAF_REG_REMAP (reg);
9674 if (leaf_reg != -1)
9675 reg = (unsigned) leaf_reg;
9677 #endif
9678 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9679 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9681 /* Simple, contiguous registers. */
9682 if (regs == NULL_RTX)
9684 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9686 loc_result = NULL;
9687 while (nregs--)
9689 dw_loc_descr_ref t;
9691 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9692 VAR_INIT_STATUS_INITIALIZED);
9693 add_loc_descr (&loc_result, t);
9694 add_loc_descr_op_piece (&loc_result, size);
9695 ++reg;
9697 return loc_result;
9700 /* Now onto stupid register sets in non contiguous locations. */
9702 gcc_assert (GET_CODE (regs) == PARALLEL);
9704 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9705 loc_result = NULL;
9707 for (i = 0; i < XVECLEN (regs, 0); ++i)
9709 dw_loc_descr_ref t;
9711 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9712 VAR_INIT_STATUS_INITIALIZED);
9713 add_loc_descr (&loc_result, t);
9714 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9715 add_loc_descr_op_piece (&loc_result, size);
9718 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9719 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9720 return loc_result;
9723 #endif /* DWARF2_DEBUGGING_INFO */
9725 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
9727 /* Return a location descriptor that designates a constant. */
9729 static dw_loc_descr_ref
9730 int_loc_descriptor (HOST_WIDE_INT i)
9732 enum dwarf_location_atom op;
9734 /* Pick the smallest representation of a constant, rather than just
9735 defaulting to the LEB encoding. */
9736 if (i >= 0)
9738 if (i <= 31)
9739 op = DW_OP_lit0 + i;
9740 else if (i <= 0xff)
9741 op = DW_OP_const1u;
9742 else if (i <= 0xffff)
9743 op = DW_OP_const2u;
9744 else if (HOST_BITS_PER_WIDE_INT == 32
9745 || i <= 0xffffffff)
9746 op = DW_OP_const4u;
9747 else
9748 op = DW_OP_constu;
9750 else
9752 if (i >= -0x80)
9753 op = DW_OP_const1s;
9754 else if (i >= -0x8000)
9755 op = DW_OP_const2s;
9756 else if (HOST_BITS_PER_WIDE_INT == 32
9757 || i >= -0x80000000)
9758 op = DW_OP_const4s;
9759 else
9760 op = DW_OP_consts;
9763 return new_loc_descr (op, i, 0);
9765 #endif
9767 #ifdef DWARF2_DEBUGGING_INFO
9769 /* Return a location descriptor that designates a base+offset location. */
9771 static dw_loc_descr_ref
9772 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9773 enum var_init_status initialized)
9775 unsigned int regno;
9776 dw_loc_descr_ref result;
9777 dw_fde_ref fde = current_fde ();
9779 /* We only use "frame base" when we're sure we're talking about the
9780 post-prologue local stack frame. We do this by *not* running
9781 register elimination until this point, and recognizing the special
9782 argument pointer and soft frame pointer rtx's. */
9783 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9785 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9787 if (elim != reg)
9789 if (GET_CODE (elim) == PLUS)
9791 offset += INTVAL (XEXP (elim, 1));
9792 elim = XEXP (elim, 0);
9794 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
9795 && (elim == hard_frame_pointer_rtx
9796 || elim == stack_pointer_rtx))
9797 || elim == (frame_pointer_needed
9798 ? hard_frame_pointer_rtx
9799 : stack_pointer_rtx));
9801 /* If drap register is used to align stack, use frame
9802 pointer + offset to access stack variables. If stack
9803 is aligned without drap, use stack pointer + offset to
9804 access stack variables. */
9805 if (crtl->stack_realign_tried
9806 && cfa.reg == HARD_FRAME_POINTER_REGNUM
9807 && reg == frame_pointer_rtx)
9809 int base_reg
9810 = DWARF_FRAME_REGNUM (cfa.indirect
9811 ? HARD_FRAME_POINTER_REGNUM
9812 : STACK_POINTER_REGNUM);
9813 return new_reg_loc_descr (base_reg, offset);
9816 offset += frame_pointer_fb_offset;
9817 return new_loc_descr (DW_OP_fbreg, offset, 0);
9820 else if (fde
9821 && fde->drap_reg != INVALID_REGNUM
9822 && (fde->drap_reg == REGNO (reg)
9823 || fde->vdrap_reg == REGNO (reg)))
9825 /* Use cfa+offset to represent the location of arguments passed
9826 on stack when drap is used to align stack. */
9827 return new_loc_descr (DW_OP_fbreg, offset, 0);
9830 regno = dbx_reg_number (reg);
9831 if (regno <= 31)
9832 result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9833 else
9834 result = new_loc_descr (DW_OP_bregx, regno, offset);
9836 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9837 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9839 return result;
9842 /* Return true if this RTL expression describes a base+offset calculation. */
9844 static inline int
9845 is_based_loc (const_rtx rtl)
9847 return (GET_CODE (rtl) == PLUS
9848 && ((REG_P (XEXP (rtl, 0))
9849 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9850 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9853 /* Return a descriptor that describes the concatenation of N locations
9854 used to form the address of a memory location. */
9856 static dw_loc_descr_ref
9857 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9858 enum var_init_status initialized)
9860 unsigned int i;
9861 dw_loc_descr_ref cc_loc_result = NULL;
9862 unsigned int n = XVECLEN (concatn, 0);
9864 for (i = 0; i < n; ++i)
9866 dw_loc_descr_ref ref;
9867 rtx x = XVECEXP (concatn, 0, i);
9869 ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9870 if (ref == NULL)
9871 return NULL;
9873 add_loc_descr (&cc_loc_result, ref);
9874 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9877 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9878 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9880 return cc_loc_result;
9883 /* The following routine converts the RTL for a variable or parameter
9884 (resident in memory) into an equivalent Dwarf representation of a
9885 mechanism for getting the address of that same variable onto the top of a
9886 hypothetical "address evaluation" stack.
9888 When creating memory location descriptors, we are effectively transforming
9889 the RTL for a memory-resident object into its Dwarf postfix expression
9890 equivalent. This routine recursively descends an RTL tree, turning
9891 it into Dwarf postfix code as it goes.
9893 MODE is the mode of the memory reference, needed to handle some
9894 autoincrement addressing modes.
9896 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9897 location list for RTL.
9899 Return 0 if we can't represent the location. */
9901 static dw_loc_descr_ref
9902 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9903 enum var_init_status initialized)
9905 dw_loc_descr_ref mem_loc_result = NULL;
9906 enum dwarf_location_atom op;
9908 /* Note that for a dynamically sized array, the location we will generate a
9909 description of here will be the lowest numbered location which is
9910 actually within the array. That's *not* necessarily the same as the
9911 zeroth element of the array. */
9913 rtl = targetm.delegitimize_address (rtl);
9915 switch (GET_CODE (rtl))
9917 case POST_INC:
9918 case POST_DEC:
9919 case POST_MODIFY:
9920 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
9921 just fall into the SUBREG code. */
9923 /* ... fall through ... */
9925 case SUBREG:
9926 /* The case of a subreg may arise when we have a local (register)
9927 variable or a formal (register) parameter which doesn't quite fill
9928 up an entire register. For now, just assume that it is
9929 legitimate to make the Dwarf info refer to the whole register which
9930 contains the given subreg. */
9931 rtl = XEXP (rtl, 0);
9933 /* ... fall through ... */
9935 case REG:
9936 /* Whenever a register number forms a part of the description of the
9937 method for calculating the (dynamic) address of a memory resident
9938 object, DWARF rules require the register number be referred to as
9939 a "base register". This distinction is not based in any way upon
9940 what category of register the hardware believes the given register
9941 belongs to. This is strictly DWARF terminology we're dealing with
9942 here. Note that in cases where the location of a memory-resident
9943 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9944 OP_CONST (0)) the actual DWARF location descriptor that we generate
9945 may just be OP_BASEREG (basereg). This may look deceptively like
9946 the object in question was allocated to a register (rather than in
9947 memory) so DWARF consumers need to be aware of the subtle
9948 distinction between OP_REG and OP_BASEREG. */
9949 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9950 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9951 break;
9953 case MEM:
9954 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9955 VAR_INIT_STATUS_INITIALIZED);
9956 if (mem_loc_result != 0)
9957 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9958 break;
9960 case LO_SUM:
9961 rtl = XEXP (rtl, 1);
9963 /* ... fall through ... */
9965 case LABEL_REF:
9966 /* Some ports can transform a symbol ref into a label ref, because
9967 the symbol ref is too far away and has to be dumped into a constant
9968 pool. */
9969 case CONST:
9970 case SYMBOL_REF:
9971 /* Alternatively, the symbol in the constant pool might be referenced
9972 by a different symbol. */
9973 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9975 bool marked;
9976 rtx tmp = get_pool_constant_mark (rtl, &marked);
9978 if (GET_CODE (tmp) == SYMBOL_REF)
9980 rtl = tmp;
9981 if (CONSTANT_POOL_ADDRESS_P (tmp))
9982 get_pool_constant_mark (tmp, &marked);
9983 else
9984 marked = true;
9987 /* If all references to this pool constant were optimized away,
9988 it was not output and thus we can't represent it.
9989 FIXME: might try to use DW_OP_const_value here, though
9990 DW_OP_piece complicates it. */
9991 if (!marked)
9992 return 0;
9995 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9996 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9997 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9998 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9999 break;
10001 case PRE_MODIFY:
10002 /* Extract the PLUS expression nested inside and fall into
10003 PLUS code below. */
10004 rtl = XEXP (rtl, 1);
10005 goto plus;
10007 case PRE_INC:
10008 case PRE_DEC:
10009 /* Turn these into a PLUS expression and fall into the PLUS code
10010 below. */
10011 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10012 GEN_INT (GET_CODE (rtl) == PRE_INC
10013 ? GET_MODE_UNIT_SIZE (mode)
10014 : -GET_MODE_UNIT_SIZE (mode)));
10016 /* ... fall through ... */
10018 case PLUS:
10019 plus:
10020 if (is_based_loc (rtl))
10021 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10022 INTVAL (XEXP (rtl, 1)),
10023 VAR_INIT_STATUS_INITIALIZED);
10024 else
10026 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10027 VAR_INIT_STATUS_INITIALIZED);
10028 if (mem_loc_result == 0)
10029 break;
10031 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
10032 && INTVAL (XEXP (rtl, 1)) >= 0)
10033 add_loc_descr (&mem_loc_result,
10034 new_loc_descr (DW_OP_plus_uconst,
10035 INTVAL (XEXP (rtl, 1)), 0));
10036 else
10038 add_loc_descr (&mem_loc_result,
10039 mem_loc_descriptor (XEXP (rtl, 1), mode,
10040 VAR_INIT_STATUS_INITIALIZED));
10041 add_loc_descr (&mem_loc_result,
10042 new_loc_descr (DW_OP_plus, 0, 0));
10045 break;
10047 /* If a pseudo-reg is optimized away, it is possible for it to
10048 be replaced with a MEM containing a multiply or shift. */
10049 case MULT:
10050 op = DW_OP_mul;
10051 goto do_binop;
10053 case ASHIFT:
10054 op = DW_OP_shl;
10055 goto do_binop;
10057 case ASHIFTRT:
10058 op = DW_OP_shra;
10059 goto do_binop;
10061 case LSHIFTRT:
10062 op = DW_OP_shr;
10063 goto do_binop;
10065 do_binop:
10067 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10068 VAR_INIT_STATUS_INITIALIZED);
10069 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10070 VAR_INIT_STATUS_INITIALIZED);
10072 if (op0 == 0 || op1 == 0)
10073 break;
10075 mem_loc_result = op0;
10076 add_loc_descr (&mem_loc_result, op1);
10077 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10078 break;
10081 case CONST_INT:
10082 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10083 break;
10085 case CONCATN:
10086 mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10087 VAR_INIT_STATUS_INITIALIZED);
10088 break;
10090 default:
10091 gcc_unreachable ();
10094 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10095 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10097 return mem_loc_result;
10100 /* Return a descriptor that describes the concatenation of two locations.
10101 This is typically a complex variable. */
10103 static dw_loc_descr_ref
10104 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10106 dw_loc_descr_ref cc_loc_result = NULL;
10107 dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10108 dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10110 if (x0_ref == 0 || x1_ref == 0)
10111 return 0;
10113 cc_loc_result = x0_ref;
10114 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10116 add_loc_descr (&cc_loc_result, x1_ref);
10117 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10119 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10120 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10122 return cc_loc_result;
10125 /* Return a descriptor that describes the concatenation of N
10126 locations. */
10128 static dw_loc_descr_ref
10129 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10131 unsigned int i;
10132 dw_loc_descr_ref cc_loc_result = NULL;
10133 unsigned int n = XVECLEN (concatn, 0);
10135 for (i = 0; i < n; ++i)
10137 dw_loc_descr_ref ref;
10138 rtx x = XVECEXP (concatn, 0, i);
10140 ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10141 if (ref == NULL)
10142 return NULL;
10144 add_loc_descr (&cc_loc_result, ref);
10145 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10148 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10149 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10151 return cc_loc_result;
10154 /* Output a proper Dwarf location descriptor for a variable or parameter
10155 which is either allocated in a register or in a memory location. For a
10156 register, we just generate an OP_REG and the register number. For a
10157 memory location we provide a Dwarf postfix expression describing how to
10158 generate the (dynamic) address of the object onto the address stack.
10160 If we don't know how to describe it, return 0. */
10162 static dw_loc_descr_ref
10163 loc_descriptor (rtx rtl, enum var_init_status initialized)
10165 dw_loc_descr_ref loc_result = NULL;
10167 switch (GET_CODE (rtl))
10169 case SUBREG:
10170 /* The case of a subreg may arise when we have a local (register)
10171 variable or a formal (register) parameter which doesn't quite fill
10172 up an entire register. For now, just assume that it is
10173 legitimate to make the Dwarf info refer to the whole register which
10174 contains the given subreg. */
10175 rtl = SUBREG_REG (rtl);
10177 /* ... fall through ... */
10179 case REG:
10180 loc_result = reg_loc_descriptor (rtl, initialized);
10181 break;
10183 case MEM:
10184 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10185 initialized);
10186 break;
10188 case CONCAT:
10189 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10190 initialized);
10191 break;
10193 case CONCATN:
10194 loc_result = concatn_loc_descriptor (rtl, initialized);
10195 break;
10197 case VAR_LOCATION:
10198 /* Single part. */
10199 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10201 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10202 break;
10205 rtl = XEXP (rtl, 1);
10206 /* FALLTHRU */
10208 case PARALLEL:
10210 rtvec par_elems = XVEC (rtl, 0);
10211 int num_elem = GET_NUM_ELEM (par_elems);
10212 enum machine_mode mode;
10213 int i;
10215 /* Create the first one, so we have something to add to. */
10216 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10217 initialized);
10218 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10219 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10220 for (i = 1; i < num_elem; i++)
10222 dw_loc_descr_ref temp;
10224 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10225 initialized);
10226 add_loc_descr (&loc_result, temp);
10227 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10228 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10231 break;
10233 default:
10234 gcc_unreachable ();
10237 return loc_result;
10240 /* Similar, but generate the descriptor from trees instead of rtl. This comes
10241 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
10242 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10243 top-level invocation, and we require the address of LOC; is 0 if we require
10244 the value of LOC. */
10246 static dw_loc_descr_ref
10247 loc_descriptor_from_tree_1 (tree loc, int want_address)
10249 dw_loc_descr_ref ret, ret1;
10250 int have_address = 0;
10251 enum dwarf_location_atom op;
10253 /* ??? Most of the time we do not take proper care for sign/zero
10254 extending the values properly. Hopefully this won't be a real
10255 problem... */
10257 switch (TREE_CODE (loc))
10259 case ERROR_MARK:
10260 return 0;
10262 case PLACEHOLDER_EXPR:
10263 /* This case involves extracting fields from an object to determine the
10264 position of other fields. We don't try to encode this here. The
10265 only user of this is Ada, which encodes the needed information using
10266 the names of types. */
10267 return 0;
10269 case CALL_EXPR:
10270 return 0;
10272 case PREINCREMENT_EXPR:
10273 case PREDECREMENT_EXPR:
10274 case POSTINCREMENT_EXPR:
10275 case POSTDECREMENT_EXPR:
10276 /* There are no opcodes for these operations. */
10277 return 0;
10279 case ADDR_EXPR:
10280 /* If we already want an address, there's nothing we can do. */
10281 if (want_address)
10282 return 0;
10284 /* Otherwise, process the argument and look for the address. */
10285 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10287 case VAR_DECL:
10288 if (DECL_THREAD_LOCAL_P (loc))
10290 rtx rtl;
10291 unsigned first_op;
10292 unsigned second_op;
10294 if (targetm.have_tls)
10296 /* If this is not defined, we have no way to emit the
10297 data. */
10298 if (!targetm.asm_out.output_dwarf_dtprel)
10299 return 0;
10301 /* The way DW_OP_GNU_push_tls_address is specified, we
10302 can only look up addresses of objects in the current
10303 module. */
10304 if (DECL_EXTERNAL (loc))
10305 return 0;
10306 first_op = INTERNAL_DW_OP_tls_addr;
10307 second_op = DW_OP_GNU_push_tls_address;
10309 else
10311 if (!targetm.emutls.debug_form_tls_address)
10312 return 0;
10313 loc = emutls_decl (loc);
10314 first_op = DW_OP_addr;
10315 second_op = DW_OP_form_tls_address;
10318 rtl = rtl_for_decl_location (loc);
10319 if (rtl == NULL_RTX)
10320 return 0;
10322 if (!MEM_P (rtl))
10323 return 0;
10324 rtl = XEXP (rtl, 0);
10325 if (! CONSTANT_P (rtl))
10326 return 0;
10328 ret = new_loc_descr (first_op, 0, 0);
10329 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10330 ret->dw_loc_oprnd1.v.val_addr = rtl;
10332 ret1 = new_loc_descr (second_op, 0, 0);
10333 add_loc_descr (&ret, ret1);
10335 have_address = 1;
10336 break;
10338 /* FALLTHRU */
10340 case PARM_DECL:
10341 if (DECL_HAS_VALUE_EXPR_P (loc))
10342 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10343 want_address);
10344 /* FALLTHRU */
10346 case RESULT_DECL:
10347 case FUNCTION_DECL:
10349 rtx rtl = rtl_for_decl_location (loc);
10351 if (rtl == NULL_RTX)
10352 return 0;
10353 else if (GET_CODE (rtl) == CONST_INT)
10355 HOST_WIDE_INT val = INTVAL (rtl);
10356 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10357 val &= GET_MODE_MASK (DECL_MODE (loc));
10358 ret = int_loc_descriptor (val);
10360 else if (GET_CODE (rtl) == CONST_STRING)
10361 return 0;
10362 else if (CONSTANT_P (rtl))
10364 ret = new_loc_descr (DW_OP_addr, 0, 0);
10365 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10366 ret->dw_loc_oprnd1.v.val_addr = rtl;
10368 else
10370 enum machine_mode mode;
10372 /* Certain constructs can only be represented at top-level. */
10373 if (want_address == 2)
10374 return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10376 mode = GET_MODE (rtl);
10377 if (MEM_P (rtl))
10379 rtl = XEXP (rtl, 0);
10380 have_address = 1;
10382 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10385 break;
10387 case INDIRECT_REF:
10388 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10389 have_address = 1;
10390 break;
10392 case COMPOUND_EXPR:
10393 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10395 CASE_CONVERT:
10396 case VIEW_CONVERT_EXPR:
10397 case SAVE_EXPR:
10398 case MODIFY_EXPR:
10399 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10401 case COMPONENT_REF:
10402 case BIT_FIELD_REF:
10403 case ARRAY_REF:
10404 case ARRAY_RANGE_REF:
10406 tree obj, offset;
10407 HOST_WIDE_INT bitsize, bitpos, bytepos;
10408 enum machine_mode mode;
10409 int volatilep;
10410 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10412 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10413 &unsignedp, &volatilep, false);
10415 if (obj == loc)
10416 return 0;
10418 ret = loc_descriptor_from_tree_1 (obj, 1);
10419 if (ret == 0
10420 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10421 return 0;
10423 if (offset != NULL_TREE)
10425 /* Variable offset. */
10426 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
10427 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10430 bytepos = bitpos / BITS_PER_UNIT;
10431 if (bytepos > 0)
10432 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
10433 else if (bytepos < 0)
10435 add_loc_descr (&ret, int_loc_descriptor (bytepos));
10436 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10439 have_address = 1;
10440 break;
10443 case INTEGER_CST:
10444 if (host_integerp (loc, 0))
10445 ret = int_loc_descriptor (tree_low_cst (loc, 0));
10446 else
10447 return 0;
10448 break;
10450 case CONSTRUCTOR:
10452 /* Get an RTL for this, if something has been emitted. */
10453 rtx rtl = lookup_constant_def (loc);
10454 enum machine_mode mode;
10456 if (!rtl || !MEM_P (rtl))
10457 return 0;
10458 mode = GET_MODE (rtl);
10459 rtl = XEXP (rtl, 0);
10460 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10461 have_address = 1;
10462 break;
10465 case TRUTH_AND_EXPR:
10466 case TRUTH_ANDIF_EXPR:
10467 case BIT_AND_EXPR:
10468 op = DW_OP_and;
10469 goto do_binop;
10471 case TRUTH_XOR_EXPR:
10472 case BIT_XOR_EXPR:
10473 op = DW_OP_xor;
10474 goto do_binop;
10476 case TRUTH_OR_EXPR:
10477 case TRUTH_ORIF_EXPR:
10478 case BIT_IOR_EXPR:
10479 op = DW_OP_or;
10480 goto do_binop;
10482 case FLOOR_DIV_EXPR:
10483 case CEIL_DIV_EXPR:
10484 case ROUND_DIV_EXPR:
10485 case TRUNC_DIV_EXPR:
10486 op = DW_OP_div;
10487 goto do_binop;
10489 case MINUS_EXPR:
10490 op = DW_OP_minus;
10491 goto do_binop;
10493 case FLOOR_MOD_EXPR:
10494 case CEIL_MOD_EXPR:
10495 case ROUND_MOD_EXPR:
10496 case TRUNC_MOD_EXPR:
10497 op = DW_OP_mod;
10498 goto do_binop;
10500 case MULT_EXPR:
10501 op = DW_OP_mul;
10502 goto do_binop;
10504 case LSHIFT_EXPR:
10505 op = DW_OP_shl;
10506 goto do_binop;
10508 case RSHIFT_EXPR:
10509 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10510 goto do_binop;
10512 case POINTER_PLUS_EXPR:
10513 case PLUS_EXPR:
10514 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10515 && host_integerp (TREE_OPERAND (loc, 1), 0))
10517 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10518 if (ret == 0)
10519 return 0;
10521 add_loc_descr (&ret,
10522 new_loc_descr (DW_OP_plus_uconst,
10523 tree_low_cst (TREE_OPERAND (loc, 1),
10525 0));
10526 break;
10529 op = DW_OP_plus;
10530 goto do_binop;
10532 case LE_EXPR:
10533 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10534 return 0;
10536 op = DW_OP_le;
10537 goto do_binop;
10539 case GE_EXPR:
10540 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10541 return 0;
10543 op = DW_OP_ge;
10544 goto do_binop;
10546 case LT_EXPR:
10547 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10548 return 0;
10550 op = DW_OP_lt;
10551 goto do_binop;
10553 case GT_EXPR:
10554 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10555 return 0;
10557 op = DW_OP_gt;
10558 goto do_binop;
10560 case EQ_EXPR:
10561 op = DW_OP_eq;
10562 goto do_binop;
10564 case NE_EXPR:
10565 op = DW_OP_ne;
10566 goto do_binop;
10568 do_binop:
10569 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10570 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10571 if (ret == 0 || ret1 == 0)
10572 return 0;
10574 add_loc_descr (&ret, ret1);
10575 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10576 break;
10578 case TRUTH_NOT_EXPR:
10579 case BIT_NOT_EXPR:
10580 op = DW_OP_not;
10581 goto do_unop;
10583 case ABS_EXPR:
10584 op = DW_OP_abs;
10585 goto do_unop;
10587 case NEGATE_EXPR:
10588 op = DW_OP_neg;
10589 goto do_unop;
10591 do_unop:
10592 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10593 if (ret == 0)
10594 return 0;
10596 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10597 break;
10599 case MIN_EXPR:
10600 case MAX_EXPR:
10602 const enum tree_code code =
10603 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
10605 loc = build3 (COND_EXPR, TREE_TYPE (loc),
10606 build2 (code, integer_type_node,
10607 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
10608 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
10611 /* ... fall through ... */
10613 case COND_EXPR:
10615 dw_loc_descr_ref lhs
10616 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10617 dw_loc_descr_ref rhs
10618 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
10619 dw_loc_descr_ref bra_node, jump_node, tmp;
10621 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10622 if (ret == 0 || lhs == 0 || rhs == 0)
10623 return 0;
10625 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10626 add_loc_descr (&ret, bra_node);
10628 add_loc_descr (&ret, rhs);
10629 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
10630 add_loc_descr (&ret, jump_node);
10632 add_loc_descr (&ret, lhs);
10633 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10634 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
10636 /* ??? Need a node to point the skip at. Use a nop. */
10637 tmp = new_loc_descr (DW_OP_nop, 0, 0);
10638 add_loc_descr (&ret, tmp);
10639 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10640 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
10642 break;
10644 case FIX_TRUNC_EXPR:
10645 return 0;
10647 default:
10648 /* Leave front-end specific codes as simply unknown. This comes
10649 up, for instance, with the C STMT_EXPR. */
10650 if ((unsigned int) TREE_CODE (loc)
10651 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
10652 return 0;
10654 #ifdef ENABLE_CHECKING
10655 /* Otherwise this is a generic code; we should just lists all of
10656 these explicitly. We forgot one. */
10657 gcc_unreachable ();
10658 #else
10659 /* In a release build, we want to degrade gracefully: better to
10660 generate incomplete debugging information than to crash. */
10661 return NULL;
10662 #endif
10665 /* Show if we can't fill the request for an address. */
10666 if (want_address && !have_address)
10667 return 0;
10669 /* If we've got an address and don't want one, dereference. */
10670 if (!want_address && have_address && ret)
10672 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
10674 if (size > DWARF2_ADDR_SIZE || size == -1)
10675 return 0;
10676 else if (size == DWARF2_ADDR_SIZE)
10677 op = DW_OP_deref;
10678 else
10679 op = DW_OP_deref_size;
10681 add_loc_descr (&ret, new_loc_descr (op, size, 0));
10684 return ret;
10687 static inline dw_loc_descr_ref
10688 loc_descriptor_from_tree (tree loc)
10690 return loc_descriptor_from_tree_1 (loc, 2);
10693 /* Given a value, round it up to the lowest multiple of `boundary'
10694 which is not less than the value itself. */
10696 static inline HOST_WIDE_INT
10697 ceiling (HOST_WIDE_INT value, unsigned int boundary)
10699 return (((value + boundary - 1) / boundary) * boundary);
10702 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
10703 pointer to the declared type for the relevant field variable, or return
10704 `integer_type_node' if the given node turns out to be an
10705 ERROR_MARK node. */
10707 static inline tree
10708 field_type (const_tree decl)
10710 tree type;
10712 if (TREE_CODE (decl) == ERROR_MARK)
10713 return integer_type_node;
10715 type = DECL_BIT_FIELD_TYPE (decl);
10716 if (type == NULL_TREE)
10717 type = TREE_TYPE (decl);
10719 return type;
10722 /* Given a pointer to a tree node, return the alignment in bits for
10723 it, or else return BITS_PER_WORD if the node actually turns out to
10724 be an ERROR_MARK node. */
10726 static inline unsigned
10727 simple_type_align_in_bits (const_tree type)
10729 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
10732 static inline unsigned
10733 simple_decl_align_in_bits (const_tree decl)
10735 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
10738 /* Return the result of rounding T up to ALIGN. */
10740 static inline HOST_WIDE_INT
10741 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
10743 /* We must be careful if T is negative because HOST_WIDE_INT can be
10744 either "above" or "below" unsigned int as per the C promotion
10745 rules, depending on the host, thus making the signedness of the
10746 direct multiplication and division unpredictable. */
10747 unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
10749 u += align - 1;
10750 u /= align;
10751 u *= align;
10753 return (HOST_WIDE_INT) u;
10756 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
10757 lowest addressed byte of the "containing object" for the given FIELD_DECL,
10758 or return 0 if we are unable to determine what that offset is, either
10759 because the argument turns out to be a pointer to an ERROR_MARK node, or
10760 because the offset is actually variable. (We can't handle the latter case
10761 just yet). */
10763 static HOST_WIDE_INT
10764 field_byte_offset (const_tree decl)
10766 HOST_WIDE_INT object_offset_in_bits;
10767 HOST_WIDE_INT bitpos_int;
10769 if (TREE_CODE (decl) == ERROR_MARK)
10770 return 0;
10772 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10774 /* We cannot yet cope with fields whose positions are variable, so
10775 for now, when we see such things, we simply return 0. Someday, we may
10776 be able to handle such cases, but it will be damn difficult. */
10777 if (! host_integerp (bit_position (decl), 0))
10778 return 0;
10780 bitpos_int = int_bit_position (decl);
10782 #ifdef PCC_BITFIELD_TYPE_MATTERS
10783 if (PCC_BITFIELD_TYPE_MATTERS)
10785 tree type;
10786 tree field_size_tree;
10787 HOST_WIDE_INT deepest_bitpos;
10788 unsigned HOST_WIDE_INT field_size_in_bits;
10789 unsigned int type_align_in_bits;
10790 unsigned int decl_align_in_bits;
10791 unsigned HOST_WIDE_INT type_size_in_bits;
10793 type = field_type (decl);
10794 field_size_tree = DECL_SIZE (decl);
10796 /* The size could be unspecified if there was an error, or for
10797 a flexible array member. */
10798 if (! field_size_tree)
10799 field_size_tree = bitsize_zero_node;
10801 /* If we don't know the size of the field, pretend it's a full word. */
10802 if (host_integerp (field_size_tree, 1))
10803 field_size_in_bits = tree_low_cst (field_size_tree, 1);
10804 else
10805 field_size_in_bits = BITS_PER_WORD;
10807 type_size_in_bits = simple_type_size_in_bits (type);
10808 type_align_in_bits = simple_type_align_in_bits (type);
10809 decl_align_in_bits = simple_decl_align_in_bits (decl);
10811 /* The GCC front-end doesn't make any attempt to keep track of the
10812 starting bit offset (relative to the start of the containing
10813 structure type) of the hypothetical "containing object" for a
10814 bit-field. Thus, when computing the byte offset value for the
10815 start of the "containing object" of a bit-field, we must deduce
10816 this information on our own. This can be rather tricky to do in
10817 some cases. For example, handling the following structure type
10818 definition when compiling for an i386/i486 target (which only
10819 aligns long long's to 32-bit boundaries) can be very tricky:
10821 struct S { int field1; long long field2:31; };
10823 Fortunately, there is a simple rule-of-thumb which can be used
10824 in such cases. When compiling for an i386/i486, GCC will
10825 allocate 8 bytes for the structure shown above. It decides to
10826 do this based upon one simple rule for bit-field allocation.
10827 GCC allocates each "containing object" for each bit-field at
10828 the first (i.e. lowest addressed) legitimate alignment boundary
10829 (based upon the required minimum alignment for the declared
10830 type of the field) which it can possibly use, subject to the
10831 condition that there is still enough available space remaining
10832 in the containing object (when allocated at the selected point)
10833 to fully accommodate all of the bits of the bit-field itself.
10835 This simple rule makes it obvious why GCC allocates 8 bytes for
10836 each object of the structure type shown above. When looking
10837 for a place to allocate the "containing object" for `field2',
10838 the compiler simply tries to allocate a 64-bit "containing
10839 object" at each successive 32-bit boundary (starting at zero)
10840 until it finds a place to allocate that 64- bit field such that
10841 at least 31 contiguous (and previously unallocated) bits remain
10842 within that selected 64 bit field. (As it turns out, for the
10843 example above, the compiler finds it is OK to allocate the
10844 "containing object" 64-bit field at bit-offset zero within the
10845 structure type.)
10847 Here we attempt to work backwards from the limited set of facts
10848 we're given, and we try to deduce from those facts, where GCC
10849 must have believed that the containing object started (within
10850 the structure type). The value we deduce is then used (by the
10851 callers of this routine) to generate DW_AT_location and
10852 DW_AT_bit_offset attributes for fields (both bit-fields and, in
10853 the case of DW_AT_location, regular fields as well). */
10855 /* Figure out the bit-distance from the start of the structure to
10856 the "deepest" bit of the bit-field. */
10857 deepest_bitpos = bitpos_int + field_size_in_bits;
10859 /* This is the tricky part. Use some fancy footwork to deduce
10860 where the lowest addressed bit of the containing object must
10861 be. */
10862 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10864 /* Round up to type_align by default. This works best for
10865 bitfields. */
10866 object_offset_in_bits
10867 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10869 if (object_offset_in_bits > bitpos_int)
10871 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10873 /* Round up to decl_align instead. */
10874 object_offset_in_bits
10875 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10878 else
10879 #endif
10880 object_offset_in_bits = bitpos_int;
10882 return object_offset_in_bits / BITS_PER_UNIT;
10885 /* The following routines define various Dwarf attributes and any data
10886 associated with them. */
10888 /* Add a location description attribute value to a DIE.
10890 This emits location attributes suitable for whole variables and
10891 whole parameters. Note that the location attributes for struct fields are
10892 generated by the routine `data_member_location_attribute' below. */
10894 static inline void
10895 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10896 dw_loc_descr_ref descr)
10898 if (descr != 0)
10899 add_AT_loc (die, attr_kind, descr);
10902 /* Attach the specialized form of location attribute used for data members of
10903 struct and union types. In the special case of a FIELD_DECL node which
10904 represents a bit-field, the "offset" part of this special location
10905 descriptor must indicate the distance in bytes from the lowest-addressed
10906 byte of the containing struct or union type to the lowest-addressed byte of
10907 the "containing object" for the bit-field. (See the `field_byte_offset'
10908 function above).
10910 For any given bit-field, the "containing object" is a hypothetical object
10911 (of some integral or enum type) within which the given bit-field lives. The
10912 type of this hypothetical "containing object" is always the same as the
10913 declared type of the individual bit-field itself (for GCC anyway... the
10914 DWARF spec doesn't actually mandate this). Note that it is the size (in
10915 bytes) of the hypothetical "containing object" which will be given in the
10916 DW_AT_byte_size attribute for this bit-field. (See the
10917 `byte_size_attribute' function below.) It is also used when calculating the
10918 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
10919 function below.) */
10921 static void
10922 add_data_member_location_attribute (dw_die_ref die, tree decl)
10924 HOST_WIDE_INT offset;
10925 dw_loc_descr_ref loc_descr = 0;
10927 if (TREE_CODE (decl) == TREE_BINFO)
10929 /* We're working on the TAG_inheritance for a base class. */
10930 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10932 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10933 aren't at a fixed offset from all (sub)objects of the same
10934 type. We need to extract the appropriate offset from our
10935 vtable. The following dwarf expression means
10937 BaseAddr = ObAddr + *((*ObAddr) - Offset)
10939 This is specific to the V3 ABI, of course. */
10941 dw_loc_descr_ref tmp;
10943 /* Make a copy of the object address. */
10944 tmp = new_loc_descr (DW_OP_dup, 0, 0);
10945 add_loc_descr (&loc_descr, tmp);
10947 /* Extract the vtable address. */
10948 tmp = new_loc_descr (DW_OP_deref, 0, 0);
10949 add_loc_descr (&loc_descr, tmp);
10951 /* Calculate the address of the offset. */
10952 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10953 gcc_assert (offset < 0);
10955 tmp = int_loc_descriptor (-offset);
10956 add_loc_descr (&loc_descr, tmp);
10957 tmp = new_loc_descr (DW_OP_minus, 0, 0);
10958 add_loc_descr (&loc_descr, tmp);
10960 /* Extract the offset. */
10961 tmp = new_loc_descr (DW_OP_deref, 0, 0);
10962 add_loc_descr (&loc_descr, tmp);
10964 /* Add it to the object address. */
10965 tmp = new_loc_descr (DW_OP_plus, 0, 0);
10966 add_loc_descr (&loc_descr, tmp);
10968 else
10969 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10971 else
10972 offset = field_byte_offset (decl);
10974 if (! loc_descr)
10976 enum dwarf_location_atom op;
10978 /* The DWARF2 standard says that we should assume that the structure
10979 address is already on the stack, so we can specify a structure field
10980 address by using DW_OP_plus_uconst. */
10982 #ifdef MIPS_DEBUGGING_INFO
10983 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10984 operator correctly. It works only if we leave the offset on the
10985 stack. */
10986 op = DW_OP_constu;
10987 #else
10988 op = DW_OP_plus_uconst;
10989 #endif
10991 loc_descr = new_loc_descr (op, offset, 0);
10994 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10997 /* Writes integer values to dw_vec_const array. */
10999 static void
11000 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11002 while (size != 0)
11004 *dest++ = val & 0xff;
11005 val >>= 8;
11006 --size;
11010 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
11012 static HOST_WIDE_INT
11013 extract_int (const unsigned char *src, unsigned int size)
11015 HOST_WIDE_INT val = 0;
11017 src += size;
11018 while (size != 0)
11020 val <<= 8;
11021 val |= *--src & 0xff;
11022 --size;
11024 return val;
11027 /* Writes floating point values to dw_vec_const array. */
11029 static void
11030 insert_float (const_rtx rtl, unsigned char *array)
11032 REAL_VALUE_TYPE rv;
11033 long val[4];
11034 int i;
11036 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11037 real_to_target (val, &rv, GET_MODE (rtl));
11039 /* real_to_target puts 32-bit pieces in each long. Pack them. */
11040 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11042 insert_int (val[i], 4, array);
11043 array += 4;
11047 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11048 does not have a "location" either in memory or in a register. These
11049 things can arise in GNU C when a constant is passed as an actual parameter
11050 to an inlined function. They can also arise in C++ where declared
11051 constants do not necessarily get memory "homes". */
11053 static void
11054 add_const_value_attribute (dw_die_ref die, rtx rtl)
11056 switch (GET_CODE (rtl))
11058 case CONST_INT:
11060 HOST_WIDE_INT val = INTVAL (rtl);
11062 if (val < 0)
11063 add_AT_int (die, DW_AT_const_value, val);
11064 else
11065 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11067 break;
11069 case CONST_DOUBLE:
11070 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11071 floating-point constant. A CONST_DOUBLE is used whenever the
11072 constant requires more than one word in order to be adequately
11073 represented. We output CONST_DOUBLEs as blocks. */
11075 enum machine_mode mode = GET_MODE (rtl);
11077 if (SCALAR_FLOAT_MODE_P (mode))
11079 unsigned int length = GET_MODE_SIZE (mode);
11080 unsigned char *array = GGC_NEWVEC (unsigned char, length);
11082 insert_float (rtl, array);
11083 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11085 else
11087 /* ??? We really should be using HOST_WIDE_INT throughout. */
11088 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11090 add_AT_long_long (die, DW_AT_const_value,
11091 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11094 break;
11096 case CONST_VECTOR:
11098 enum machine_mode mode = GET_MODE (rtl);
11099 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11100 unsigned int length = CONST_VECTOR_NUNITS (rtl);
11101 unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11102 unsigned int i;
11103 unsigned char *p;
11105 switch (GET_MODE_CLASS (mode))
11107 case MODE_VECTOR_INT:
11108 for (i = 0, p = array; i < length; i++, p += elt_size)
11110 rtx elt = CONST_VECTOR_ELT (rtl, i);
11111 HOST_WIDE_INT lo, hi;
11113 switch (GET_CODE (elt))
11115 case CONST_INT:
11116 lo = INTVAL (elt);
11117 hi = -(lo < 0);
11118 break;
11120 case CONST_DOUBLE:
11121 lo = CONST_DOUBLE_LOW (elt);
11122 hi = CONST_DOUBLE_HIGH (elt);
11123 break;
11125 default:
11126 gcc_unreachable ();
11129 if (elt_size <= sizeof (HOST_WIDE_INT))
11130 insert_int (lo, elt_size, p);
11131 else
11133 unsigned char *p0 = p;
11134 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11136 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11137 if (WORDS_BIG_ENDIAN)
11139 p0 = p1;
11140 p1 = p;
11142 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11143 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11146 break;
11148 case MODE_VECTOR_FLOAT:
11149 for (i = 0, p = array; i < length; i++, p += elt_size)
11151 rtx elt = CONST_VECTOR_ELT (rtl, i);
11152 insert_float (elt, p);
11154 break;
11156 default:
11157 gcc_unreachable ();
11160 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11162 break;
11164 case CONST_STRING:
11165 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11166 break;
11168 case SYMBOL_REF:
11169 case LABEL_REF:
11170 case CONST:
11171 add_AT_addr (die, DW_AT_const_value, rtl);
11172 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11173 break;
11175 case PLUS:
11176 /* In cases where an inlined instance of an inline function is passed
11177 the address of an `auto' variable (which is local to the caller) we
11178 can get a situation where the DECL_RTL of the artificial local
11179 variable (for the inlining) which acts as a stand-in for the
11180 corresponding formal parameter (of the inline function) will look
11181 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
11182 exactly a compile-time constant expression, but it isn't the address
11183 of the (artificial) local variable either. Rather, it represents the
11184 *value* which the artificial local variable always has during its
11185 lifetime. We currently have no way to represent such quasi-constant
11186 values in Dwarf, so for now we just punt and generate nothing. */
11187 break;
11189 default:
11190 /* No other kinds of rtx should be possible here. */
11191 gcc_unreachable ();
11196 /* Determine whether the evaluation of EXPR references any variables
11197 or functions which aren't otherwise used (and therefore may not be
11198 output). */
11199 static tree
11200 reference_to_unused (tree * tp, int * walk_subtrees,
11201 void * data ATTRIBUTE_UNUSED)
11203 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11204 *walk_subtrees = 0;
11206 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11207 && ! TREE_ASM_WRITTEN (*tp))
11208 return *tp;
11209 /* ??? The C++ FE emits debug information for using decls, so
11210 putting gcc_unreachable here falls over. See PR31899. For now
11211 be conservative. */
11212 else if (!cgraph_global_info_ready
11213 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11214 return *tp;
11215 else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11217 struct varpool_node *node = varpool_node (*tp);
11218 if (!node->needed)
11219 return *tp;
11221 else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11222 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11224 struct cgraph_node *node = cgraph_node (*tp);
11225 if (!node->output)
11226 return *tp;
11228 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11229 return *tp;
11231 return NULL_TREE;
11234 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11235 for use in a later add_const_value_attribute call. */
11237 static rtx
11238 rtl_for_decl_init (tree init, tree type)
11240 rtx rtl = NULL_RTX;
11242 /* If a variable is initialized with a string constant without embedded
11243 zeros, build CONST_STRING. */
11244 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11246 tree enttype = TREE_TYPE (type);
11247 tree domain = TYPE_DOMAIN (type);
11248 enum machine_mode mode = TYPE_MODE (enttype);
11250 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11251 && domain
11252 && integer_zerop (TYPE_MIN_VALUE (domain))
11253 && compare_tree_int (TYPE_MAX_VALUE (domain),
11254 TREE_STRING_LENGTH (init) - 1) == 0
11255 && ((size_t) TREE_STRING_LENGTH (init)
11256 == strlen (TREE_STRING_POINTER (init)) + 1))
11257 rtl = gen_rtx_CONST_STRING (VOIDmode,
11258 ggc_strdup (TREE_STRING_POINTER (init)));
11260 /* Other aggregates, and complex values, could be represented using
11261 CONCAT: FIXME! */
11262 else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11264 /* Vectors only work if their mode is supported by the target.
11265 FIXME: generic vectors ought to work too. */
11266 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11268 /* If the initializer is something that we know will expand into an
11269 immediate RTL constant, expand it now. We must be careful not to
11270 reference variables which won't be output. */
11271 else if (initializer_constant_valid_p (init, type)
11272 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11274 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11275 possible. */
11276 if (TREE_CODE (type) == VECTOR_TYPE)
11277 switch (TREE_CODE (init))
11279 case VECTOR_CST:
11280 break;
11281 case CONSTRUCTOR:
11282 if (TREE_CONSTANT (init))
11284 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11285 bool constant_p = true;
11286 tree value;
11287 unsigned HOST_WIDE_INT ix;
11289 /* Even when ctor is constant, it might contain non-*_CST
11290 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11291 belong into VECTOR_CST nodes. */
11292 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11293 if (!CONSTANT_CLASS_P (value))
11295 constant_p = false;
11296 break;
11299 if (constant_p)
11301 init = build_vector_from_ctor (type, elts);
11302 break;
11305 /* FALLTHRU */
11307 default:
11308 return NULL;
11311 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11313 /* If expand_expr returns a MEM, it wasn't immediate. */
11314 gcc_assert (!rtl || !MEM_P (rtl));
11317 return rtl;
11320 /* Generate RTL for the variable DECL to represent its location. */
11322 static rtx
11323 rtl_for_decl_location (tree decl)
11325 rtx rtl;
11327 /* Here we have to decide where we are going to say the parameter "lives"
11328 (as far as the debugger is concerned). We only have a couple of
11329 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11331 DECL_RTL normally indicates where the parameter lives during most of the
11332 activation of the function. If optimization is enabled however, this
11333 could be either NULL or else a pseudo-reg. Both of those cases indicate
11334 that the parameter doesn't really live anywhere (as far as the code
11335 generation parts of GCC are concerned) during most of the function's
11336 activation. That will happen (for example) if the parameter is never
11337 referenced within the function.
11339 We could just generate a location descriptor here for all non-NULL
11340 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11341 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11342 where DECL_RTL is NULL or is a pseudo-reg.
11344 Note however that we can only get away with using DECL_INCOMING_RTL as
11345 a backup substitute for DECL_RTL in certain limited cases. In cases
11346 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11347 we can be sure that the parameter was passed using the same type as it is
11348 declared to have within the function, and that its DECL_INCOMING_RTL
11349 points us to a place where a value of that type is passed.
11351 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11352 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11353 because in these cases DECL_INCOMING_RTL points us to a value of some
11354 type which is *different* from the type of the parameter itself. Thus,
11355 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11356 such cases, the debugger would end up (for example) trying to fetch a
11357 `float' from a place which actually contains the first part of a
11358 `double'. That would lead to really incorrect and confusing
11359 output at debug-time.
11361 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11362 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
11363 are a couple of exceptions however. On little-endian machines we can
11364 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11365 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11366 an integral type that is smaller than TREE_TYPE (decl). These cases arise
11367 when (on a little-endian machine) a non-prototyped function has a
11368 parameter declared to be of type `short' or `char'. In such cases,
11369 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11370 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11371 passed `int' value. If the debugger then uses that address to fetch
11372 a `short' or a `char' (on a little-endian machine) the result will be
11373 the correct data, so we allow for such exceptional cases below.
11375 Note that our goal here is to describe the place where the given formal
11376 parameter lives during most of the function's activation (i.e. between the
11377 end of the prologue and the start of the epilogue). We'll do that as best
11378 as we can. Note however that if the given formal parameter is modified
11379 sometime during the execution of the function, then a stack backtrace (at
11380 debug-time) will show the function as having been called with the *new*
11381 value rather than the value which was originally passed in. This happens
11382 rarely enough that it is not a major problem, but it *is* a problem, and
11383 I'd like to fix it.
11385 A future version of dwarf2out.c may generate two additional attributes for
11386 any given DW_TAG_formal_parameter DIE which will describe the "passed
11387 type" and the "passed location" for the given formal parameter in addition
11388 to the attributes we now generate to indicate the "declared type" and the
11389 "active location" for each parameter. This additional set of attributes
11390 could be used by debuggers for stack backtraces. Separately, note that
11391 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11392 This happens (for example) for inlined-instances of inline function formal
11393 parameters which are never referenced. This really shouldn't be
11394 happening. All PARM_DECL nodes should get valid non-NULL
11395 DECL_INCOMING_RTL values. FIXME. */
11397 /* Use DECL_RTL as the "location" unless we find something better. */
11398 rtl = DECL_RTL_IF_SET (decl);
11400 /* When generating abstract instances, ignore everything except
11401 constants, symbols living in memory, and symbols living in
11402 fixed registers. */
11403 if (! reload_completed)
11405 if (rtl
11406 && (CONSTANT_P (rtl)
11407 || (MEM_P (rtl)
11408 && CONSTANT_P (XEXP (rtl, 0)))
11409 || (REG_P (rtl)
11410 && TREE_CODE (decl) == VAR_DECL
11411 && TREE_STATIC (decl))))
11413 rtl = targetm.delegitimize_address (rtl);
11414 return rtl;
11416 rtl = NULL_RTX;
11418 else if (TREE_CODE (decl) == PARM_DECL)
11420 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11422 tree declared_type = TREE_TYPE (decl);
11423 tree passed_type = DECL_ARG_TYPE (decl);
11424 enum machine_mode dmode = TYPE_MODE (declared_type);
11425 enum machine_mode pmode = TYPE_MODE (passed_type);
11427 /* This decl represents a formal parameter which was optimized out.
11428 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11429 all cases where (rtl == NULL_RTX) just below. */
11430 if (dmode == pmode)
11431 rtl = DECL_INCOMING_RTL (decl);
11432 else if (SCALAR_INT_MODE_P (dmode)
11433 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11434 && DECL_INCOMING_RTL (decl))
11436 rtx inc = DECL_INCOMING_RTL (decl);
11437 if (REG_P (inc))
11438 rtl = inc;
11439 else if (MEM_P (inc))
11441 if (BYTES_BIG_ENDIAN)
11442 rtl = adjust_address_nv (inc, dmode,
11443 GET_MODE_SIZE (pmode)
11444 - GET_MODE_SIZE (dmode));
11445 else
11446 rtl = inc;
11451 /* If the parm was passed in registers, but lives on the stack, then
11452 make a big endian correction if the mode of the type of the
11453 parameter is not the same as the mode of the rtl. */
11454 /* ??? This is the same series of checks that are made in dbxout.c before
11455 we reach the big endian correction code there. It isn't clear if all
11456 of these checks are necessary here, but keeping them all is the safe
11457 thing to do. */
11458 else if (MEM_P (rtl)
11459 && XEXP (rtl, 0) != const0_rtx
11460 && ! CONSTANT_P (XEXP (rtl, 0))
11461 /* Not passed in memory. */
11462 && !MEM_P (DECL_INCOMING_RTL (decl))
11463 /* Not passed by invisible reference. */
11464 && (!REG_P (XEXP (rtl, 0))
11465 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11466 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11467 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11468 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11469 #endif
11471 /* Big endian correction check. */
11472 && BYTES_BIG_ENDIAN
11473 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11474 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11475 < UNITS_PER_WORD))
11477 int offset = (UNITS_PER_WORD
11478 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11480 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11481 plus_constant (XEXP (rtl, 0), offset));
11484 else if (TREE_CODE (decl) == VAR_DECL
11485 && rtl
11486 && MEM_P (rtl)
11487 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11488 && BYTES_BIG_ENDIAN)
11490 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11491 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11493 /* If a variable is declared "register" yet is smaller than
11494 a register, then if we store the variable to memory, it
11495 looks like we're storing a register-sized value, when in
11496 fact we are not. We need to adjust the offset of the
11497 storage location to reflect the actual value's bytes,
11498 else gdb will not be able to display it. */
11499 if (rsize > dsize)
11500 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11501 plus_constant (XEXP (rtl, 0), rsize-dsize));
11504 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11505 and will have been substituted directly into all expressions that use it.
11506 C does not have such a concept, but C++ and other languages do. */
11507 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11508 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11510 if (rtl)
11511 rtl = targetm.delegitimize_address (rtl);
11513 /* If we don't look past the constant pool, we risk emitting a
11514 reference to a constant pool entry that isn't referenced from
11515 code, and thus is not emitted. */
11516 if (rtl)
11517 rtl = avoid_constant_pool_reference (rtl);
11519 return rtl;
11522 /* We need to figure out what section we should use as the base for the
11523 address ranges where a given location is valid.
11524 1. If this particular DECL has a section associated with it, use that.
11525 2. If this function has a section associated with it, use that.
11526 3. Otherwise, use the text section.
11527 XXX: If you split a variable across multiple sections, we won't notice. */
11529 static const char *
11530 secname_for_decl (const_tree decl)
11532 const char *secname;
11534 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11536 tree sectree = DECL_SECTION_NAME (decl);
11537 secname = TREE_STRING_POINTER (sectree);
11539 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11541 tree sectree = DECL_SECTION_NAME (current_function_decl);
11542 secname = TREE_STRING_POINTER (sectree);
11544 else if (cfun && in_cold_section_p)
11545 secname = crtl->subsections.cold_section_label;
11546 else
11547 secname = text_section_label;
11549 return secname;
11552 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_RTX is returned.
11553 If so, the rtx for the SYMBOL_REF for the COMMON block is returned, and the
11554 value is the offset into the common block for the symbol. */
11556 static tree
11557 fortran_common (tree decl, HOST_WIDE_INT *value)
11559 tree val_expr, cvar;
11560 enum machine_mode mode;
11561 HOST_WIDE_INT bitsize, bitpos;
11562 tree offset;
11563 int volatilep = 0, unsignedp = 0;
11565 /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11566 it does not have a value (the offset into the common area), or if it
11567 is thread local (as opposed to global) then it isn't common, and shouldn't
11568 be handled as such. */
11569 if (TREE_CODE (decl) != VAR_DECL
11570 || !TREE_PUBLIC (decl)
11571 || !TREE_STATIC (decl)
11572 || !DECL_HAS_VALUE_EXPR_P (decl)
11573 || !is_fortran ())
11574 return NULL_TREE;
11576 val_expr = DECL_VALUE_EXPR (decl);
11577 if (TREE_CODE (val_expr) != COMPONENT_REF)
11578 return NULL_TREE;
11580 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11581 &mode, &unsignedp, &volatilep, true);
11583 if (cvar == NULL_TREE
11584 || TREE_CODE (cvar) != VAR_DECL
11585 || DECL_ARTIFICIAL (cvar)
11586 || !TREE_PUBLIC (cvar))
11587 return NULL_TREE;
11589 *value = 0;
11590 if (offset != NULL)
11592 if (!host_integerp (offset, 0))
11593 return NULL_TREE;
11594 *value = tree_low_cst (offset, 0);
11596 if (bitpos != 0)
11597 *value += bitpos / BITS_PER_UNIT;
11599 return cvar;
11603 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
11604 data attribute for a variable or a parameter. We generate the
11605 DW_AT_const_value attribute only in those cases where the given variable
11606 or parameter does not have a true "location" either in memory or in a
11607 register. This can happen (for example) when a constant is passed as an
11608 actual argument in a call to an inline function. (It's possible that
11609 these things can crop up in other ways also.) Note that one type of
11610 constant value which can be passed into an inlined function is a constant
11611 pointer. This can happen for example if an actual argument in an inlined
11612 function call evaluates to a compile-time constant address. */
11614 static void
11615 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
11616 enum dwarf_attribute attr)
11618 rtx rtl;
11619 dw_loc_descr_ref descr;
11620 var_loc_list *loc_list;
11621 struct var_loc_node *node;
11622 if (TREE_CODE (decl) == ERROR_MARK)
11623 return;
11625 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
11626 || TREE_CODE (decl) == RESULT_DECL);
11628 /* See if we possibly have multiple locations for this variable. */
11629 loc_list = lookup_decl_loc (decl);
11631 /* If it truly has multiple locations, the first and last node will
11632 differ. */
11633 if (loc_list && loc_list->first != loc_list->last)
11635 const char *endname, *secname;
11636 dw_loc_list_ref list;
11637 rtx varloc;
11638 enum var_init_status initialized;
11640 /* Now that we know what section we are using for a base,
11641 actually construct the list of locations.
11642 The first location information is what is passed to the
11643 function that creates the location list, and the remaining
11644 locations just get added on to that list.
11645 Note that we only know the start address for a location
11646 (IE location changes), so to build the range, we use
11647 the range [current location start, next location start].
11648 This means we have to special case the last node, and generate
11649 a range of [last location start, end of function label]. */
11651 node = loc_list->first;
11652 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11653 secname = secname_for_decl (decl);
11655 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
11656 initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11657 else
11658 initialized = VAR_INIT_STATUS_INITIALIZED;
11660 list = new_loc_list (loc_descriptor (varloc, initialized),
11661 node->label, node->next->label, secname, 1);
11662 node = node->next;
11664 for (; node->next; node = node->next)
11665 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11667 /* The variable has a location between NODE->LABEL and
11668 NODE->NEXT->LABEL. */
11669 enum var_init_status initialized =
11670 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11671 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11672 add_loc_descr_to_loc_list (&list,
11673 loc_descriptor (varloc, initialized),
11674 node->label, node->next->label, secname);
11677 /* If the variable has a location at the last label
11678 it keeps its location until the end of function. */
11679 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11681 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11682 enum var_init_status initialized =
11683 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11685 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11686 if (!current_function_decl)
11687 endname = text_end_label;
11688 else
11690 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11691 current_function_funcdef_no);
11692 endname = ggc_strdup (label_id);
11694 add_loc_descr_to_loc_list (&list,
11695 loc_descriptor (varloc, initialized),
11696 node->label, endname, secname);
11699 /* Finally, add the location list to the DIE, and we are done. */
11700 add_AT_loc_list (die, attr, list);
11701 return;
11704 /* Try to get some constant RTL for this decl, and use that as the value of
11705 the location. */
11707 rtl = rtl_for_decl_location (decl);
11708 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
11710 add_const_value_attribute (die, rtl);
11711 return;
11714 /* If we have tried to generate the location otherwise, and it
11715 didn't work out (we wouldn't be here if we did), and we have a one entry
11716 location list, try generating a location from that. */
11717 if (loc_list && loc_list->first)
11719 enum var_init_status status;
11720 node = loc_list->first;
11721 status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11722 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
11723 if (descr)
11725 add_AT_location_description (die, attr, descr);
11726 return;
11730 /* We couldn't get any rtl, so try directly generating the location
11731 description from the tree. */
11732 descr = loc_descriptor_from_tree (decl);
11733 if (descr)
11735 add_AT_location_description (die, attr, descr);
11736 return;
11738 /* None of that worked, so it must not really have a location;
11739 try adding a constant value attribute from the DECL_INITIAL. */
11740 tree_add_const_value_attribute (die, decl);
11743 /* If we don't have a copy of this variable in memory for some reason (such
11744 as a C++ member constant that doesn't have an out-of-line definition),
11745 we should tell the debugger about the constant value. */
11747 static void
11748 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
11750 tree init = DECL_INITIAL (decl);
11751 tree type = TREE_TYPE (decl);
11752 rtx rtl;
11754 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
11755 /* OK */;
11756 else
11757 return;
11759 rtl = rtl_for_decl_init (init, type);
11760 if (rtl)
11761 add_const_value_attribute (var_die, rtl);
11764 /* Convert the CFI instructions for the current function into a
11765 location list. This is used for DW_AT_frame_base when we targeting
11766 a dwarf2 consumer that does not support the dwarf3
11767 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
11768 expressions. */
11770 static dw_loc_list_ref
11771 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
11773 dw_fde_ref fde;
11774 dw_loc_list_ref list, *list_tail;
11775 dw_cfi_ref cfi;
11776 dw_cfa_location last_cfa, next_cfa;
11777 const char *start_label, *last_label, *section;
11779 fde = current_fde ();
11780 gcc_assert (fde != NULL);
11782 section = secname_for_decl (current_function_decl);
11783 list_tail = &list;
11784 list = NULL;
11786 next_cfa.reg = INVALID_REGNUM;
11787 next_cfa.offset = 0;
11788 next_cfa.indirect = 0;
11789 next_cfa.base_offset = 0;
11791 start_label = fde->dw_fde_begin;
11793 /* ??? Bald assumption that the CIE opcode list does not contain
11794 advance opcodes. */
11795 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
11796 lookup_cfa_1 (cfi, &next_cfa);
11798 last_cfa = next_cfa;
11799 last_label = start_label;
11801 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
11802 switch (cfi->dw_cfi_opc)
11804 case DW_CFA_set_loc:
11805 case DW_CFA_advance_loc1:
11806 case DW_CFA_advance_loc2:
11807 case DW_CFA_advance_loc4:
11808 if (!cfa_equal_p (&last_cfa, &next_cfa))
11810 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11811 start_label, last_label, section,
11812 list == NULL);
11814 list_tail = &(*list_tail)->dw_loc_next;
11815 last_cfa = next_cfa;
11816 start_label = last_label;
11818 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
11819 break;
11821 case DW_CFA_advance_loc:
11822 /* The encoding is complex enough that we should never emit this. */
11823 case DW_CFA_remember_state:
11824 case DW_CFA_restore_state:
11825 /* We don't handle these two in this function. It would be possible
11826 if it were to be required. */
11827 gcc_unreachable ();
11829 default:
11830 lookup_cfa_1 (cfi, &next_cfa);
11831 break;
11834 if (!cfa_equal_p (&last_cfa, &next_cfa))
11836 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11837 start_label, last_label, section,
11838 list == NULL);
11839 list_tail = &(*list_tail)->dw_loc_next;
11840 start_label = last_label;
11842 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
11843 start_label, fde->dw_fde_end, section,
11844 list == NULL);
11846 return list;
11849 /* Compute a displacement from the "steady-state frame pointer" to the
11850 frame base (often the same as the CFA), and store it in
11851 frame_pointer_fb_offset. OFFSET is added to the displacement
11852 before the latter is negated. */
11854 static void
11855 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
11857 rtx reg, elim;
11859 #ifdef FRAME_POINTER_CFA_OFFSET
11860 reg = frame_pointer_rtx;
11861 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
11862 #else
11863 reg = arg_pointer_rtx;
11864 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
11865 #endif
11867 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
11868 if (GET_CODE (elim) == PLUS)
11870 offset += INTVAL (XEXP (elim, 1));
11871 elim = XEXP (elim, 0);
11874 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11875 && (elim == hard_frame_pointer_rtx
11876 || elim == stack_pointer_rtx))
11877 || elim == (frame_pointer_needed
11878 ? hard_frame_pointer_rtx
11879 : stack_pointer_rtx));
11881 frame_pointer_fb_offset = -offset;
11884 /* Generate a DW_AT_name attribute given some string value to be included as
11885 the value of the attribute. */
11887 static void
11888 add_name_attribute (dw_die_ref die, const char *name_string)
11890 if (name_string != NULL && *name_string != 0)
11892 if (demangle_name_func)
11893 name_string = (*demangle_name_func) (name_string);
11895 add_AT_string (die, DW_AT_name, name_string);
11899 /* Generate a DW_AT_comp_dir attribute for DIE. */
11901 static void
11902 add_comp_dir_attribute (dw_die_ref die)
11904 const char *wd = get_src_pwd ();
11905 if (wd != NULL)
11906 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
11909 /* Given a tree node describing an array bound (either lower or upper) output
11910 a representation for that bound. */
11912 static void
11913 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
11915 switch (TREE_CODE (bound))
11917 case ERROR_MARK:
11918 return;
11920 /* All fixed-bounds are represented by INTEGER_CST nodes. */
11921 case INTEGER_CST:
11922 if (! host_integerp (bound, 0)
11923 || (bound_attr == DW_AT_lower_bound
11924 && (((is_c_family () || is_java ()) && integer_zerop (bound))
11925 || (is_fortran () && integer_onep (bound)))))
11926 /* Use the default. */
11928 else
11929 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11930 break;
11932 CASE_CONVERT:
11933 case VIEW_CONVERT_EXPR:
11934 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11935 break;
11937 case SAVE_EXPR:
11938 break;
11940 case VAR_DECL:
11941 case PARM_DECL:
11942 case RESULT_DECL:
11944 dw_die_ref decl_die = lookup_decl_die (bound);
11946 /* ??? Can this happen, or should the variable have been bound
11947 first? Probably it can, since I imagine that we try to create
11948 the types of parameters in the order in which they exist in
11949 the list, and won't have created a forward reference to a
11950 later parameter. */
11951 if (decl_die != NULL)
11952 add_AT_die_ref (subrange_die, bound_attr, decl_die);
11953 break;
11956 default:
11958 /* Otherwise try to create a stack operation procedure to
11959 evaluate the value of the array bound. */
11961 dw_die_ref ctx, decl_die;
11962 dw_loc_descr_ref loc;
11964 loc = loc_descriptor_from_tree (bound);
11965 if (loc == NULL)
11966 break;
11968 if (current_function_decl == 0)
11969 ctx = comp_unit_die;
11970 else
11971 ctx = lookup_decl_die (current_function_decl);
11973 decl_die = new_die (DW_TAG_variable, ctx, bound);
11974 add_AT_flag (decl_die, DW_AT_artificial, 1);
11975 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11976 add_AT_loc (decl_die, DW_AT_location, loc);
11978 add_AT_die_ref (subrange_die, bound_attr, decl_die);
11979 break;
11984 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
11985 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
11986 Note that the block of subscript information for an array type also
11987 includes information about the element type of the given array type. */
11989 static void
11990 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
11992 unsigned dimension_number;
11993 tree lower, upper;
11994 dw_die_ref subrange_die;
11996 for (dimension_number = 0;
11997 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
11998 type = TREE_TYPE (type), dimension_number++)
12000 tree domain = TYPE_DOMAIN (type);
12002 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12003 and (in GNU C only) variable bounds. Handle all three forms
12004 here. */
12005 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12006 if (domain)
12008 /* We have an array type with specified bounds. */
12009 lower = TYPE_MIN_VALUE (domain);
12010 upper = TYPE_MAX_VALUE (domain);
12012 /* Define the index type. */
12013 if (TREE_TYPE (domain))
12015 /* ??? This is probably an Ada unnamed subrange type. Ignore the
12016 TREE_TYPE field. We can't emit debug info for this
12017 because it is an unnamed integral type. */
12018 if (TREE_CODE (domain) == INTEGER_TYPE
12019 && TYPE_NAME (domain) == NULL_TREE
12020 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12021 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12023 else
12024 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12025 type_die);
12028 /* ??? If upper is NULL, the array has unspecified length,
12029 but it does have a lower bound. This happens with Fortran
12030 dimension arr(N:*)
12031 Since the debugger is definitely going to need to know N
12032 to produce useful results, go ahead and output the lower
12033 bound solo, and hope the debugger can cope. */
12035 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12036 if (upper)
12037 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12040 /* Otherwise we have an array type with an unspecified length. The
12041 DWARF-2 spec does not say how to handle this; let's just leave out the
12042 bounds. */
12046 static void
12047 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12049 unsigned size;
12051 switch (TREE_CODE (tree_node))
12053 case ERROR_MARK:
12054 size = 0;
12055 break;
12056 case ENUMERAL_TYPE:
12057 case RECORD_TYPE:
12058 case UNION_TYPE:
12059 case QUAL_UNION_TYPE:
12060 size = int_size_in_bytes (tree_node);
12061 break;
12062 case FIELD_DECL:
12063 /* For a data member of a struct or union, the DW_AT_byte_size is
12064 generally given as the number of bytes normally allocated for an
12065 object of the *declared* type of the member itself. This is true
12066 even for bit-fields. */
12067 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12068 break;
12069 default:
12070 gcc_unreachable ();
12073 /* Note that `size' might be -1 when we get to this point. If it is, that
12074 indicates that the byte size of the entity in question is variable. We
12075 have no good way of expressing this fact in Dwarf at the present time,
12076 so just let the -1 pass on through. */
12077 add_AT_unsigned (die, DW_AT_byte_size, size);
12080 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12081 which specifies the distance in bits from the highest order bit of the
12082 "containing object" for the bit-field to the highest order bit of the
12083 bit-field itself.
12085 For any given bit-field, the "containing object" is a hypothetical object
12086 (of some integral or enum type) within which the given bit-field lives. The
12087 type of this hypothetical "containing object" is always the same as the
12088 declared type of the individual bit-field itself. The determination of the
12089 exact location of the "containing object" for a bit-field is rather
12090 complicated. It's handled by the `field_byte_offset' function (above).
12092 Note that it is the size (in bytes) of the hypothetical "containing object"
12093 which will be given in the DW_AT_byte_size attribute for this bit-field.
12094 (See `byte_size_attribute' above). */
12096 static inline void
12097 add_bit_offset_attribute (dw_die_ref die, tree decl)
12099 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12100 tree type = DECL_BIT_FIELD_TYPE (decl);
12101 HOST_WIDE_INT bitpos_int;
12102 HOST_WIDE_INT highest_order_object_bit_offset;
12103 HOST_WIDE_INT highest_order_field_bit_offset;
12104 HOST_WIDE_INT unsigned bit_offset;
12106 /* Must be a field and a bit field. */
12107 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12109 /* We can't yet handle bit-fields whose offsets are variable, so if we
12110 encounter such things, just return without generating any attribute
12111 whatsoever. Likewise for variable or too large size. */
12112 if (! host_integerp (bit_position (decl), 0)
12113 || ! host_integerp (DECL_SIZE (decl), 1))
12114 return;
12116 bitpos_int = int_bit_position (decl);
12118 /* Note that the bit offset is always the distance (in bits) from the
12119 highest-order bit of the "containing object" to the highest-order bit of
12120 the bit-field itself. Since the "high-order end" of any object or field
12121 is different on big-endian and little-endian machines, the computation
12122 below must take account of these differences. */
12123 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12124 highest_order_field_bit_offset = bitpos_int;
12126 if (! BYTES_BIG_ENDIAN)
12128 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12129 highest_order_object_bit_offset += simple_type_size_in_bits (type);
12132 bit_offset
12133 = (! BYTES_BIG_ENDIAN
12134 ? highest_order_object_bit_offset - highest_order_field_bit_offset
12135 : highest_order_field_bit_offset - highest_order_object_bit_offset);
12137 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12140 /* For a FIELD_DECL node which represents a bit field, output an attribute
12141 which specifies the length in bits of the given field. */
12143 static inline void
12144 add_bit_size_attribute (dw_die_ref die, tree decl)
12146 /* Must be a field and a bit field. */
12147 gcc_assert (TREE_CODE (decl) == FIELD_DECL
12148 && DECL_BIT_FIELD_TYPE (decl));
12150 if (host_integerp (DECL_SIZE (decl), 1))
12151 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12154 /* If the compiled language is ANSI C, then add a 'prototyped'
12155 attribute, if arg types are given for the parameters of a function. */
12157 static inline void
12158 add_prototyped_attribute (dw_die_ref die, tree func_type)
12160 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12161 && TYPE_ARG_TYPES (func_type) != NULL)
12162 add_AT_flag (die, DW_AT_prototyped, 1);
12165 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
12166 by looking in either the type declaration or object declaration
12167 equate table. */
12169 static inline void
12170 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12172 dw_die_ref origin_die = NULL;
12174 if (TREE_CODE (origin) != FUNCTION_DECL)
12176 /* We may have gotten separated from the block for the inlined
12177 function, if we're in an exception handler or some such; make
12178 sure that the abstract function has been written out.
12180 Doing this for nested functions is wrong, however; functions are
12181 distinct units, and our context might not even be inline. */
12182 tree fn = origin;
12184 if (TYPE_P (fn))
12185 fn = TYPE_STUB_DECL (fn);
12187 fn = decl_function_context (fn);
12188 if (fn)
12189 dwarf2out_abstract_function (fn);
12192 if (DECL_P (origin))
12193 origin_die = lookup_decl_die (origin);
12194 else if (TYPE_P (origin))
12195 origin_die = lookup_type_die (origin);
12197 /* XXX: Functions that are never lowered don't always have correct block
12198 trees (in the case of java, they simply have no block tree, in some other
12199 languages). For these functions, there is nothing we can really do to
12200 output correct debug info for inlined functions in all cases. Rather
12201 than die, we'll just produce deficient debug info now, in that we will
12202 have variables without a proper abstract origin. In the future, when all
12203 functions are lowered, we should re-add a gcc_assert (origin_die)
12204 here. */
12206 if (origin_die)
12207 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12210 /* We do not currently support the pure_virtual attribute. */
12212 static inline void
12213 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12215 if (DECL_VINDEX (func_decl))
12217 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12219 if (host_integerp (DECL_VINDEX (func_decl), 0))
12220 add_AT_loc (die, DW_AT_vtable_elem_location,
12221 new_loc_descr (DW_OP_constu,
12222 tree_low_cst (DECL_VINDEX (func_decl), 0),
12223 0));
12225 /* GNU extension: Record what type this method came from originally. */
12226 if (debug_info_level > DINFO_LEVEL_TERSE)
12227 add_AT_die_ref (die, DW_AT_containing_type,
12228 lookup_type_die (DECL_CONTEXT (func_decl)));
12232 /* Add source coordinate attributes for the given decl. */
12234 static void
12235 add_src_coords_attributes (dw_die_ref die, tree decl)
12237 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12239 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12240 add_AT_unsigned (die, DW_AT_decl_line, s.line);
12243 /* Add a DW_AT_name attribute and source coordinate attribute for the
12244 given decl, but only if it actually has a name. */
12246 static void
12247 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12249 tree decl_name;
12251 decl_name = DECL_NAME (decl);
12252 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12254 add_name_attribute (die, dwarf2_name (decl, 0));
12255 if (! DECL_ARTIFICIAL (decl))
12256 add_src_coords_attributes (die, decl);
12258 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12259 && TREE_PUBLIC (decl)
12260 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
12261 && !DECL_ABSTRACT (decl)
12262 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12263 && !is_fortran ())
12264 add_AT_string (die, DW_AT_MIPS_linkage_name,
12265 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12268 #ifdef VMS_DEBUGGING_INFO
12269 /* Get the function's name, as described by its RTL. This may be different
12270 from the DECL_NAME name used in the source file. */
12271 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12273 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12274 XEXP (DECL_RTL (decl), 0));
12275 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12277 #endif
12280 /* Push a new declaration scope. */
12282 static void
12283 push_decl_scope (tree scope)
12285 VEC_safe_push (tree, gc, decl_scope_table, scope);
12288 /* Pop a declaration scope. */
12290 static inline void
12291 pop_decl_scope (void)
12293 VEC_pop (tree, decl_scope_table);
12296 /* Return the DIE for the scope that immediately contains this type.
12297 Non-named types get global scope. Named types nested in other
12298 types get their containing scope if it's open, or global scope
12299 otherwise. All other types (i.e. function-local named types) get
12300 the current active scope. */
12302 static dw_die_ref
12303 scope_die_for (tree t, dw_die_ref context_die)
12305 dw_die_ref scope_die = NULL;
12306 tree containing_scope;
12307 int i;
12309 /* Non-types always go in the current scope. */
12310 gcc_assert (TYPE_P (t));
12312 containing_scope = TYPE_CONTEXT (t);
12314 /* Use the containing namespace if it was passed in (for a declaration). */
12315 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12317 if (context_die == lookup_decl_die (containing_scope))
12318 /* OK */;
12319 else
12320 containing_scope = NULL_TREE;
12323 /* Ignore function type "scopes" from the C frontend. They mean that
12324 a tagged type is local to a parmlist of a function declarator, but
12325 that isn't useful to DWARF. */
12326 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12327 containing_scope = NULL_TREE;
12329 if (containing_scope == NULL_TREE)
12330 scope_die = comp_unit_die;
12331 else if (TYPE_P (containing_scope))
12333 /* For types, we can just look up the appropriate DIE. But
12334 first we check to see if we're in the middle of emitting it
12335 so we know where the new DIE should go. */
12336 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12337 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
12338 break;
12340 if (i < 0)
12342 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12343 || TREE_ASM_WRITTEN (containing_scope));
12345 /* If none of the current dies are suitable, we get file scope. */
12346 scope_die = comp_unit_die;
12348 else
12349 scope_die = lookup_type_die (containing_scope);
12351 else
12352 scope_die = context_die;
12354 return scope_die;
12357 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
12359 static inline int
12360 local_scope_p (dw_die_ref context_die)
12362 for (; context_die; context_die = context_die->die_parent)
12363 if (context_die->die_tag == DW_TAG_inlined_subroutine
12364 || context_die->die_tag == DW_TAG_subprogram)
12365 return 1;
12367 return 0;
12370 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12371 whether or not to treat a DIE in this context as a declaration. */
12373 static inline int
12374 class_or_namespace_scope_p (dw_die_ref context_die)
12376 return (context_die
12377 && (context_die->die_tag == DW_TAG_structure_type
12378 || context_die->die_tag == DW_TAG_class_type
12379 || context_die->die_tag == DW_TAG_interface_type
12380 || context_die->die_tag == DW_TAG_union_type
12381 || context_die->die_tag == DW_TAG_namespace));
12384 /* Many forms of DIEs require a "type description" attribute. This
12385 routine locates the proper "type descriptor" die for the type given
12386 by 'type', and adds a DW_AT_type attribute below the given die. */
12388 static void
12389 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
12390 int decl_volatile, dw_die_ref context_die)
12392 enum tree_code code = TREE_CODE (type);
12393 dw_die_ref type_die = NULL;
12395 /* ??? If this type is an unnamed subrange type of an integral, floating-point
12396 or fixed-point type, use the inner type. This is because we have no
12397 support for unnamed types in base_type_die. This can happen if this is
12398 an Ada subrange type. Correct solution is emit a subrange type die. */
12399 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
12400 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
12401 type = TREE_TYPE (type), code = TREE_CODE (type);
12403 if (code == ERROR_MARK
12404 /* Handle a special case. For functions whose return type is void, we
12405 generate *no* type attribute. (Note that no object may have type
12406 `void', so this only applies to function return types). */
12407 || code == VOID_TYPE)
12408 return;
12410 type_die = modified_type_die (type,
12411 decl_const || TYPE_READONLY (type),
12412 decl_volatile || TYPE_VOLATILE (type),
12413 context_die);
12415 if (type_die != NULL)
12416 add_AT_die_ref (object_die, DW_AT_type, type_die);
12419 /* Given an object die, add the calling convention attribute for the
12420 function call type. */
12421 static void
12422 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
12424 enum dwarf_calling_convention value = DW_CC_normal;
12426 value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
12428 /* DWARF doesn't provide a way to identify a program's source-level
12429 entry point. DW_AT_calling_convention attributes are only meant
12430 to describe functions' calling conventions. However, lacking a
12431 better way to signal the Fortran main program, we use this for the
12432 time being, following existing custom. */
12433 if (is_fortran ()
12434 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
12435 value = DW_CC_program;
12437 /* Only add the attribute if the backend requests it, and
12438 is not DW_CC_normal. */
12439 if (value && (value != DW_CC_normal))
12440 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
12443 /* Given a tree pointer to a struct, class, union, or enum type node, return
12444 a pointer to the (string) tag name for the given type, or zero if the type
12445 was declared without a tag. */
12447 static const char *
12448 type_tag (const_tree type)
12450 const char *name = 0;
12452 if (TYPE_NAME (type) != 0)
12454 tree t = 0;
12456 /* Find the IDENTIFIER_NODE for the type name. */
12457 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
12458 t = TYPE_NAME (type);
12460 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
12461 a TYPE_DECL node, regardless of whether or not a `typedef' was
12462 involved. */
12463 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12464 && ! DECL_IGNORED_P (TYPE_NAME (type)))
12466 /* We want to be extra verbose. Don't call dwarf_name if
12467 DECL_NAME isn't set. The default hook for decl_printable_name
12468 doesn't like that, and in this context it's correct to return
12469 0, instead of "<anonymous>" or the like. */
12470 if (DECL_NAME (TYPE_NAME (type)))
12471 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
12474 /* Now get the name as a string, or invent one. */
12475 if (!name && t != 0)
12476 name = IDENTIFIER_POINTER (t);
12479 return (name == 0 || *name == '\0') ? 0 : name;
12482 /* Return the type associated with a data member, make a special check
12483 for bit field types. */
12485 static inline tree
12486 member_declared_type (const_tree member)
12488 return (DECL_BIT_FIELD_TYPE (member)
12489 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
12492 /* Get the decl's label, as described by its RTL. This may be different
12493 from the DECL_NAME name used in the source file. */
12495 #if 0
12496 static const char *
12497 decl_start_label (tree decl)
12499 rtx x;
12500 const char *fnname;
12502 x = DECL_RTL (decl);
12503 gcc_assert (MEM_P (x));
12505 x = XEXP (x, 0);
12506 gcc_assert (GET_CODE (x) == SYMBOL_REF);
12508 fnname = XSTR (x, 0);
12509 return fnname;
12511 #endif
12513 /* These routines generate the internal representation of the DIE's for
12514 the compilation unit. Debugging information is collected by walking
12515 the declaration trees passed in from dwarf2out_decl(). */
12517 static void
12518 gen_array_type_die (tree type, dw_die_ref context_die)
12520 dw_die_ref scope_die = scope_die_for (type, context_die);
12521 dw_die_ref array_die;
12523 /* GNU compilers represent multidimensional array types as sequences of one
12524 dimensional array types whose element types are themselves array types.
12525 We sometimes squish that down to a single array_type DIE with multiple
12526 subscripts in the Dwarf debugging info. The draft Dwarf specification
12527 say that we are allowed to do this kind of compression in C, because
12528 there is no difference between an array of arrays and a multidimensional
12529 array. We don't do this for Ada to remain as close as possible to the
12530 actual representation, which is especially important against the language
12531 flexibilty wrt arrays of variable size. */
12533 bool collapse_nested_arrays = !is_ada ();
12534 tree element_type;
12536 /* ??? The SGI dwarf reader fails for array of array of enum types
12537 (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
12538 array type comes before the outer array type. We thus call gen_type_die
12539 before we new_die and must prevent nested array types collapsing for this
12540 target. */
12542 #ifdef MIPS_DEBUGGING_INFO
12543 gen_type_die (TREE_TYPE (type), context_die);
12544 collapse_nested_arrays = false;
12545 #endif
12547 array_die = new_die (DW_TAG_array_type, scope_die, type);
12548 add_name_attribute (array_die, type_tag (type));
12549 equate_type_number_to_die (type, array_die);
12551 if (TREE_CODE (type) == VECTOR_TYPE)
12553 /* The frontend feeds us a representation for the vector as a struct
12554 containing an array. Pull out the array type. */
12555 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
12556 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
12559 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
12560 if (is_fortran ()
12561 && TREE_CODE (type) == ARRAY_TYPE
12562 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
12563 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12565 #if 0
12566 /* We default the array ordering. SDB will probably do
12567 the right things even if DW_AT_ordering is not present. It's not even
12568 an issue until we start to get into multidimensional arrays anyway. If
12569 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
12570 then we'll have to put the DW_AT_ordering attribute back in. (But if
12571 and when we find out that we need to put these in, we will only do so
12572 for multidimensional arrays. */
12573 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
12574 #endif
12576 #ifdef MIPS_DEBUGGING_INFO
12577 /* The SGI compilers handle arrays of unknown bound by setting
12578 AT_declaration and not emitting any subrange DIEs. */
12579 if (! TYPE_DOMAIN (type))
12580 add_AT_flag (array_die, DW_AT_declaration, 1);
12581 else
12582 #endif
12583 add_subscript_info (array_die, type, collapse_nested_arrays);
12585 /* Add representation of the type of the elements of this array type and
12586 emit the corresponding DIE if we haven't done it already. */
12587 element_type = TREE_TYPE (type);
12588 if (collapse_nested_arrays)
12589 while (TREE_CODE (element_type) == ARRAY_TYPE)
12590 element_type = TREE_TYPE (element_type);
12592 #ifndef MIPS_DEBUGGING_INFO
12593 gen_type_die (element_type, context_die);
12594 #endif
12596 add_type_attribute (array_die, element_type, 0, 0, context_die);
12598 if (get_AT (array_die, DW_AT_name))
12599 add_pubtype (type, array_die);
12602 static dw_loc_descr_ref
12603 descr_info_loc (tree val, tree base_decl)
12605 HOST_WIDE_INT size;
12606 dw_loc_descr_ref loc, loc2;
12607 enum dwarf_location_atom op;
12609 if (val == base_decl)
12610 return new_loc_descr (DW_OP_push_object_address, 0, 0);
12612 switch (TREE_CODE (val))
12614 CASE_CONVERT:
12615 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12616 case INTEGER_CST:
12617 if (host_integerp (val, 0))
12618 return int_loc_descriptor (tree_low_cst (val, 0));
12619 break;
12620 case INDIRECT_REF:
12621 size = int_size_in_bytes (TREE_TYPE (val));
12622 if (size < 0)
12623 break;
12624 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12625 if (!loc)
12626 break;
12627 if (size == DWARF2_ADDR_SIZE)
12628 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
12629 else
12630 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
12631 return loc;
12632 case POINTER_PLUS_EXPR:
12633 case PLUS_EXPR:
12634 if (host_integerp (TREE_OPERAND (val, 1), 1)
12635 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
12636 < 16384)
12638 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12639 if (!loc)
12640 break;
12641 add_loc_descr (&loc,
12642 new_loc_descr (DW_OP_plus_uconst,
12643 tree_low_cst (TREE_OPERAND (val, 1),
12644 1), 0));
12646 else
12648 op = DW_OP_plus;
12649 do_binop:
12650 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12651 if (!loc)
12652 break;
12653 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
12654 if (!loc2)
12655 break;
12656 add_loc_descr (&loc, loc2);
12657 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
12659 return loc;
12660 case MINUS_EXPR:
12661 op = DW_OP_minus;
12662 goto do_binop;
12663 case MULT_EXPR:
12664 op = DW_OP_mul;
12665 goto do_binop;
12666 case EQ_EXPR:
12667 op = DW_OP_eq;
12668 goto do_binop;
12669 case NE_EXPR:
12670 op = DW_OP_ne;
12671 goto do_binop;
12672 default:
12673 break;
12675 return NULL;
12678 static void
12679 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
12680 tree val, tree base_decl)
12682 dw_loc_descr_ref loc;
12684 if (host_integerp (val, 0))
12686 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
12687 return;
12690 loc = descr_info_loc (val, base_decl);
12691 if (!loc)
12692 return;
12694 add_AT_loc (die, attr, loc);
12697 /* This routine generates DIE for array with hidden descriptor, details
12698 are filled into *info by a langhook. */
12700 static void
12701 gen_descr_array_type_die (tree type, struct array_descr_info *info,
12702 dw_die_ref context_die)
12704 dw_die_ref scope_die = scope_die_for (type, context_die);
12705 dw_die_ref array_die;
12706 int dim;
12708 array_die = new_die (DW_TAG_array_type, scope_die, type);
12709 add_name_attribute (array_die, type_tag (type));
12710 equate_type_number_to_die (type, array_die);
12712 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
12713 if (is_fortran ()
12714 && info->ndimensions >= 2)
12715 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12717 if (info->data_location)
12718 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
12719 info->base_decl);
12720 if (info->associated)
12721 add_descr_info_field (array_die, DW_AT_associated, info->associated,
12722 info->base_decl);
12723 if (info->allocated)
12724 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
12725 info->base_decl);
12727 for (dim = 0; dim < info->ndimensions; dim++)
12729 dw_die_ref subrange_die
12730 = new_die (DW_TAG_subrange_type, array_die, NULL);
12732 if (info->dimen[dim].lower_bound)
12734 /* If it is the default value, omit it. */
12735 if ((is_c_family () || is_java ())
12736 && integer_zerop (info->dimen[dim].lower_bound))
12738 else if (is_fortran ()
12739 && integer_onep (info->dimen[dim].lower_bound))
12741 else
12742 add_descr_info_field (subrange_die, DW_AT_lower_bound,
12743 info->dimen[dim].lower_bound,
12744 info->base_decl);
12746 if (info->dimen[dim].upper_bound)
12747 add_descr_info_field (subrange_die, DW_AT_upper_bound,
12748 info->dimen[dim].upper_bound,
12749 info->base_decl);
12750 if (info->dimen[dim].stride)
12751 add_descr_info_field (subrange_die, DW_AT_byte_stride,
12752 info->dimen[dim].stride,
12753 info->base_decl);
12756 gen_type_die (info->element_type, context_die);
12757 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
12759 if (get_AT (array_die, DW_AT_name))
12760 add_pubtype (type, array_die);
12763 #if 0
12764 static void
12765 gen_entry_point_die (tree decl, dw_die_ref context_die)
12767 tree origin = decl_ultimate_origin (decl);
12768 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
12770 if (origin != NULL)
12771 add_abstract_origin_attribute (decl_die, origin);
12772 else
12774 add_name_and_src_coords_attributes (decl_die, decl);
12775 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
12776 0, 0, context_die);
12779 if (DECL_ABSTRACT (decl))
12780 equate_decl_number_to_die (decl, decl_die);
12781 else
12782 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
12784 #endif
12786 /* Walk through the list of incomplete types again, trying once more to
12787 emit full debugging info for them. */
12789 static void
12790 retry_incomplete_types (void)
12792 int i;
12794 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
12795 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
12798 /* Generate a DIE to represent an inlined instance of an enumeration type. */
12800 static void
12801 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
12803 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
12805 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12806 be incomplete and such types are not marked. */
12807 add_abstract_origin_attribute (type_die, type);
12810 /* Determine what tag to use for a record type. */
12812 static enum dwarf_tag
12813 record_type_tag (tree type)
12815 if (! lang_hooks.types.classify_record)
12816 return DW_TAG_structure_type;
12818 switch (lang_hooks.types.classify_record (type))
12820 case RECORD_IS_STRUCT:
12821 return DW_TAG_structure_type;
12823 case RECORD_IS_CLASS:
12824 return DW_TAG_class_type;
12826 case RECORD_IS_INTERFACE:
12827 return DW_TAG_interface_type;
12829 default:
12830 gcc_unreachable ();
12834 /* Generate a DIE to represent an inlined instance of a structure type. */
12836 static void
12837 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
12839 dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
12841 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12842 be incomplete and such types are not marked. */
12843 add_abstract_origin_attribute (type_die, type);
12846 /* Generate a DIE to represent an inlined instance of a union type. */
12848 static void
12849 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
12851 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
12853 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12854 be incomplete and such types are not marked. */
12855 add_abstract_origin_attribute (type_die, type);
12858 /* Generate a DIE to represent an enumeration type. Note that these DIEs
12859 include all of the information about the enumeration values also. Each
12860 enumerated type name/value is listed as a child of the enumerated type
12861 DIE. */
12863 static dw_die_ref
12864 gen_enumeration_type_die (tree type, dw_die_ref context_die)
12866 dw_die_ref type_die = lookup_type_die (type);
12868 if (type_die == NULL)
12870 type_die = new_die (DW_TAG_enumeration_type,
12871 scope_die_for (type, context_die), type);
12872 equate_type_number_to_die (type, type_die);
12873 add_name_attribute (type_die, type_tag (type));
12875 else if (! TYPE_SIZE (type))
12876 return type_die;
12877 else
12878 remove_AT (type_die, DW_AT_declaration);
12880 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
12881 given enum type is incomplete, do not generate the DW_AT_byte_size
12882 attribute or the DW_AT_element_list attribute. */
12883 if (TYPE_SIZE (type))
12885 tree link;
12887 TREE_ASM_WRITTEN (type) = 1;
12888 add_byte_size_attribute (type_die, type);
12889 if (TYPE_STUB_DECL (type) != NULL_TREE)
12890 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12892 /* If the first reference to this type was as the return type of an
12893 inline function, then it may not have a parent. Fix this now. */
12894 if (type_die->die_parent == NULL)
12895 add_child_die (scope_die_for (type, context_die), type_die);
12897 for (link = TYPE_VALUES (type);
12898 link != NULL; link = TREE_CHAIN (link))
12900 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
12901 tree value = TREE_VALUE (link);
12903 add_name_attribute (enum_die,
12904 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
12906 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
12907 /* DWARF2 does not provide a way of indicating whether or
12908 not enumeration constants are signed or unsigned. GDB
12909 always assumes the values are signed, so we output all
12910 values as if they were signed. That means that
12911 enumeration constants with very large unsigned values
12912 will appear to have negative values in the debugger. */
12913 add_AT_int (enum_die, DW_AT_const_value,
12914 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
12917 else
12918 add_AT_flag (type_die, DW_AT_declaration, 1);
12920 if (get_AT (type_die, DW_AT_name))
12921 add_pubtype (type, type_die);
12923 return type_die;
12926 /* Generate a DIE to represent either a real live formal parameter decl or to
12927 represent just the type of some formal parameter position in some function
12928 type.
12930 Note that this routine is a bit unusual because its argument may be a
12931 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
12932 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
12933 node. If it's the former then this function is being called to output a
12934 DIE to represent a formal parameter object (or some inlining thereof). If
12935 it's the latter, then this function is only being called to output a
12936 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
12937 argument type of some subprogram type. */
12939 static dw_die_ref
12940 gen_formal_parameter_die (tree node, dw_die_ref context_die)
12942 dw_die_ref parm_die
12943 = new_die (DW_TAG_formal_parameter, context_die, node);
12944 tree origin;
12946 switch (TREE_CODE_CLASS (TREE_CODE (node)))
12948 case tcc_declaration:
12949 origin = decl_ultimate_origin (node);
12950 if (origin != NULL)
12951 add_abstract_origin_attribute (parm_die, origin);
12952 else
12954 tree type = TREE_TYPE (node);
12955 add_name_and_src_coords_attributes (parm_die, node);
12956 if (DECL_BY_REFERENCE (node))
12957 type = TREE_TYPE (type);
12958 add_type_attribute (parm_die, type,
12959 TREE_READONLY (node),
12960 TREE_THIS_VOLATILE (node),
12961 context_die);
12962 if (DECL_ARTIFICIAL (node))
12963 add_AT_flag (parm_die, DW_AT_artificial, 1);
12966 equate_decl_number_to_die (node, parm_die);
12967 if (! DECL_ABSTRACT (node))
12968 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
12970 break;
12972 case tcc_type:
12973 /* We were called with some kind of a ..._TYPE node. */
12974 add_type_attribute (parm_die, node, 0, 0, context_die);
12975 break;
12977 default:
12978 gcc_unreachable ();
12981 return parm_die;
12984 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
12985 at the end of an (ANSI prototyped) formal parameters list. */
12987 static void
12988 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
12990 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
12993 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
12994 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
12995 parameters as specified in some function type specification (except for
12996 those which appear as part of a function *definition*). */
12998 static void
12999 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13001 tree link;
13002 tree formal_type = NULL;
13003 tree first_parm_type;
13004 tree arg;
13006 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13008 arg = DECL_ARGUMENTS (function_or_method_type);
13009 function_or_method_type = TREE_TYPE (function_or_method_type);
13011 else
13012 arg = NULL_TREE;
13014 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13016 /* Make our first pass over the list of formal parameter types and output a
13017 DW_TAG_formal_parameter DIE for each one. */
13018 for (link = first_parm_type; link; )
13020 dw_die_ref parm_die;
13022 formal_type = TREE_VALUE (link);
13023 if (formal_type == void_type_node)
13024 break;
13026 /* Output a (nameless) DIE to represent the formal parameter itself. */
13027 parm_die = gen_formal_parameter_die (formal_type, context_die);
13028 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13029 && link == first_parm_type)
13030 || (arg && DECL_ARTIFICIAL (arg)))
13031 add_AT_flag (parm_die, DW_AT_artificial, 1);
13033 link = TREE_CHAIN (link);
13034 if (arg)
13035 arg = TREE_CHAIN (arg);
13038 /* If this function type has an ellipsis, add a
13039 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
13040 if (formal_type != void_type_node)
13041 gen_unspecified_parameters_die (function_or_method_type, context_die);
13043 /* Make our second (and final) pass over the list of formal parameter types
13044 and output DIEs to represent those types (as necessary). */
13045 for (link = TYPE_ARG_TYPES (function_or_method_type);
13046 link && TREE_VALUE (link);
13047 link = TREE_CHAIN (link))
13048 gen_type_die (TREE_VALUE (link), context_die);
13051 /* We want to generate the DIE for TYPE so that we can generate the
13052 die for MEMBER, which has been defined; we will need to refer back
13053 to the member declaration nested within TYPE. If we're trying to
13054 generate minimal debug info for TYPE, processing TYPE won't do the
13055 trick; we need to attach the member declaration by hand. */
13057 static void
13058 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13060 gen_type_die (type, context_die);
13062 /* If we're trying to avoid duplicate debug info, we may not have
13063 emitted the member decl for this function. Emit it now. */
13064 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13065 && ! lookup_decl_die (member))
13067 dw_die_ref type_die;
13068 gcc_assert (!decl_ultimate_origin (member));
13070 push_decl_scope (type);
13071 type_die = lookup_type_die (type);
13072 if (TREE_CODE (member) == FUNCTION_DECL)
13073 gen_subprogram_die (member, type_die);
13074 else if (TREE_CODE (member) == FIELD_DECL)
13076 /* Ignore the nameless fields that are used to skip bits but handle
13077 C++ anonymous unions and structs. */
13078 if (DECL_NAME (member) != NULL_TREE
13079 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13080 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13082 gen_type_die (member_declared_type (member), type_die);
13083 gen_field_die (member, type_die);
13086 else
13087 gen_variable_die (member, type_die);
13089 pop_decl_scope ();
13093 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13094 may later generate inlined and/or out-of-line instances of. */
13096 static void
13097 dwarf2out_abstract_function (tree decl)
13099 dw_die_ref old_die;
13100 tree save_fn;
13101 tree context;
13102 int was_abstract = DECL_ABSTRACT (decl);
13104 /* Make sure we have the actual abstract inline, not a clone. */
13105 decl = DECL_ORIGIN (decl);
13107 old_die = lookup_decl_die (decl);
13108 if (old_die && get_AT (old_die, DW_AT_inline))
13109 /* We've already generated the abstract instance. */
13110 return;
13112 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13113 we don't get confused by DECL_ABSTRACT. */
13114 if (debug_info_level > DINFO_LEVEL_TERSE)
13116 context = decl_class_context (decl);
13117 if (context)
13118 gen_type_die_for_member
13119 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13122 /* Pretend we've just finished compiling this function. */
13123 save_fn = current_function_decl;
13124 current_function_decl = decl;
13125 push_cfun (DECL_STRUCT_FUNCTION (decl));
13127 set_decl_abstract_flags (decl, 1);
13128 dwarf2out_decl (decl);
13129 if (! was_abstract)
13130 set_decl_abstract_flags (decl, 0);
13132 current_function_decl = save_fn;
13133 pop_cfun ();
13136 /* Helper function of premark_used_types() which gets called through
13137 htab_traverse_resize().
13139 Marks the DIE of a given type in *SLOT as perennial, so it never gets
13140 marked as unused by prune_unused_types. */
13141 static int
13142 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13144 tree type;
13145 dw_die_ref die;
13147 type = (tree) *slot;
13148 die = lookup_type_die (type);
13149 if (die != NULL)
13150 die->die_perennial_p = 1;
13151 return 1;
13154 /* Mark all members of used_types_hash as perennial. */
13155 static void
13156 premark_used_types (void)
13158 if (cfun && cfun->used_types_hash)
13159 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13162 /* Generate a DIE to represent a declared function (either file-scope or
13163 block-local). */
13165 static void
13166 gen_subprogram_die (tree decl, dw_die_ref context_die)
13168 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13169 tree origin = decl_ultimate_origin (decl);
13170 dw_die_ref subr_die;
13171 tree fn_arg_types;
13172 tree outer_scope;
13173 dw_die_ref old_die = lookup_decl_die (decl);
13174 int declaration = (current_function_decl != decl
13175 || class_or_namespace_scope_p (context_die));
13177 premark_used_types ();
13179 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13180 started to generate the abstract instance of an inline, decided to output
13181 its containing class, and proceeded to emit the declaration of the inline
13182 from the member list for the class. If so, DECLARATION takes priority;
13183 we'll get back to the abstract instance when done with the class. */
13185 /* The class-scope declaration DIE must be the primary DIE. */
13186 if (origin && declaration && class_or_namespace_scope_p (context_die))
13188 origin = NULL;
13189 gcc_assert (!old_die);
13192 /* Now that the C++ front end lazily declares artificial member fns, we
13193 might need to retrofit the declaration into its class. */
13194 if (!declaration && !origin && !old_die
13195 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13196 && !class_or_namespace_scope_p (context_die)
13197 && debug_info_level > DINFO_LEVEL_TERSE)
13198 old_die = force_decl_die (decl);
13200 if (origin != NULL)
13202 gcc_assert (!declaration || local_scope_p (context_die));
13204 /* Fixup die_parent for the abstract instance of a nested
13205 inline function. */
13206 if (old_die && old_die->die_parent == NULL)
13207 add_child_die (context_die, old_die);
13209 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13210 add_abstract_origin_attribute (subr_die, origin);
13212 else if (old_die)
13214 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13215 struct dwarf_file_data * file_index = lookup_filename (s.file);
13217 if (!get_AT_flag (old_die, DW_AT_declaration)
13218 /* We can have a normal definition following an inline one in the
13219 case of redefinition of GNU C extern inlines.
13220 It seems reasonable to use AT_specification in this case. */
13221 && !get_AT (old_die, DW_AT_inline))
13223 /* Detect and ignore this case, where we are trying to output
13224 something we have already output. */
13225 return;
13228 /* If the definition comes from the same place as the declaration,
13229 maybe use the old DIE. We always want the DIE for this function
13230 that has the *_pc attributes to be under comp_unit_die so the
13231 debugger can find it. We also need to do this for abstract
13232 instances of inlines, since the spec requires the out-of-line copy
13233 to have the same parent. For local class methods, this doesn't
13234 apply; we just use the old DIE. */
13235 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13236 && (DECL_ARTIFICIAL (decl)
13237 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13238 && (get_AT_unsigned (old_die, DW_AT_decl_line)
13239 == (unsigned) s.line))))
13241 subr_die = old_die;
13243 /* Clear out the declaration attribute and the formal parameters.
13244 Do not remove all children, because it is possible that this
13245 declaration die was forced using force_decl_die(). In such
13246 cases die that forced declaration die (e.g. TAG_imported_module)
13247 is one of the children that we do not want to remove. */
13248 remove_AT (subr_die, DW_AT_declaration);
13249 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13251 else
13253 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13254 add_AT_specification (subr_die, old_die);
13255 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13256 add_AT_file (subr_die, DW_AT_decl_file, file_index);
13257 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13258 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13261 else
13263 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13265 if (TREE_PUBLIC (decl))
13266 add_AT_flag (subr_die, DW_AT_external, 1);
13268 add_name_and_src_coords_attributes (subr_die, decl);
13269 if (debug_info_level > DINFO_LEVEL_TERSE)
13271 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13272 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13273 0, 0, context_die);
13276 add_pure_or_virtual_attribute (subr_die, decl);
13277 if (DECL_ARTIFICIAL (decl))
13278 add_AT_flag (subr_die, DW_AT_artificial, 1);
13280 if (TREE_PROTECTED (decl))
13281 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13282 else if (TREE_PRIVATE (decl))
13283 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13286 if (declaration)
13288 if (!old_die || !get_AT (old_die, DW_AT_inline))
13290 add_AT_flag (subr_die, DW_AT_declaration, 1);
13292 /* The first time we see a member function, it is in the context of
13293 the class to which it belongs. We make sure of this by emitting
13294 the class first. The next time is the definition, which is
13295 handled above. The two may come from the same source text.
13297 Note that force_decl_die() forces function declaration die. It is
13298 later reused to represent definition. */
13299 equate_decl_number_to_die (decl, subr_die);
13302 else if (DECL_ABSTRACT (decl))
13304 if (DECL_DECLARED_INLINE_P (decl))
13306 if (cgraph_function_possibly_inlined_p (decl))
13307 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
13308 else
13309 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
13311 else
13313 if (cgraph_function_possibly_inlined_p (decl))
13314 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
13315 else
13316 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
13319 if (DECL_DECLARED_INLINE_P (decl)
13320 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
13321 add_AT_flag (subr_die, DW_AT_artificial, 1);
13323 equate_decl_number_to_die (decl, subr_die);
13325 else if (!DECL_EXTERNAL (decl))
13327 HOST_WIDE_INT cfa_fb_offset;
13329 if (!old_die || !get_AT (old_die, DW_AT_inline))
13330 equate_decl_number_to_die (decl, subr_die);
13332 if (!flag_reorder_blocks_and_partition)
13334 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13335 current_function_funcdef_no);
13336 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13337 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13338 current_function_funcdef_no);
13339 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
13341 add_pubname (decl, subr_die);
13342 add_arange (decl, subr_die);
13344 else
13345 { /* Do nothing for now; maybe need to duplicate die, one for
13346 hot section and one for cold section, then use the hot/cold
13347 section begin/end labels to generate the aranges... */
13349 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13350 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13351 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13352 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13354 add_pubname (decl, subr_die);
13355 add_arange (decl, subr_die);
13356 add_arange (decl, subr_die);
13360 #ifdef MIPS_DEBUGGING_INFO
13361 /* Add a reference to the FDE for this routine. */
13362 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13363 #endif
13365 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13367 /* We define the "frame base" as the function's CFA. This is more
13368 convenient for several reasons: (1) It's stable across the prologue
13369 and epilogue, which makes it better than just a frame pointer,
13370 (2) With dwarf3, there exists a one-byte encoding that allows us
13371 to reference the .debug_frame data by proxy, but failing that,
13372 (3) We can at least reuse the code inspection and interpretation
13373 code that determines the CFA position at various points in the
13374 function. */
13375 /* ??? Use some command-line or configury switch to enable the use
13376 of dwarf3 DW_OP_call_frame_cfa. At present there are no dwarf
13377 consumers that understand it; fall back to "pure" dwarf2 and
13378 convert the CFA data into a location list. */
13380 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
13381 if (list->dw_loc_next)
13382 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
13383 else
13384 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
13387 /* Compute a displacement from the "steady-state frame pointer" to
13388 the CFA. The former is what all stack slots and argument slots
13389 will reference in the rtl; the later is what we've told the
13390 debugger about. We'll need to adjust all frame_base references
13391 by this displacement. */
13392 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
13394 if (cfun->static_chain_decl)
13395 add_AT_location_description (subr_die, DW_AT_static_link,
13396 loc_descriptor_from_tree (cfun->static_chain_decl));
13399 /* Now output descriptions of the arguments for this function. This gets
13400 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
13401 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
13402 `...' at the end of the formal parameter list. In order to find out if
13403 there was a trailing ellipsis or not, we must instead look at the type
13404 associated with the FUNCTION_DECL. This will be a node of type
13405 FUNCTION_TYPE. If the chain of type nodes hanging off of this
13406 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
13407 an ellipsis at the end. */
13409 /* In the case where we are describing a mere function declaration, all we
13410 need to do here (and all we *can* do here) is to describe the *types* of
13411 its formal parameters. */
13412 if (debug_info_level <= DINFO_LEVEL_TERSE)
13414 else if (declaration)
13415 gen_formal_types_die (decl, subr_die);
13416 else
13418 /* Generate DIEs to represent all known formal parameters. */
13419 tree arg_decls = DECL_ARGUMENTS (decl);
13420 tree parm;
13422 /* When generating DIEs, generate the unspecified_parameters DIE
13423 instead if we come across the arg "__builtin_va_alist" */
13424 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
13425 if (TREE_CODE (parm) == PARM_DECL)
13427 if (DECL_NAME (parm)
13428 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
13429 "__builtin_va_alist"))
13430 gen_unspecified_parameters_die (parm, subr_die);
13431 else
13432 gen_decl_die (parm, subr_die);
13435 /* Decide whether we need an unspecified_parameters DIE at the end.
13436 There are 2 more cases to do this for: 1) the ansi ... declaration -
13437 this is detectable when the end of the arg list is not a
13438 void_type_node 2) an unprototyped function declaration (not a
13439 definition). This just means that we have no info about the
13440 parameters at all. */
13441 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
13442 if (fn_arg_types != NULL)
13444 /* This is the prototyped case, check for.... */
13445 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
13446 gen_unspecified_parameters_die (decl, subr_die);
13448 else if (DECL_INITIAL (decl) == NULL_TREE)
13449 gen_unspecified_parameters_die (decl, subr_die);
13452 /* Output Dwarf info for all of the stuff within the body of the function
13453 (if it has one - it may be just a declaration). */
13454 outer_scope = DECL_INITIAL (decl);
13456 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
13457 a function. This BLOCK actually represents the outermost binding contour
13458 for the function, i.e. the contour in which the function's formal
13459 parameters and labels get declared. Curiously, it appears that the front
13460 end doesn't actually put the PARM_DECL nodes for the current function onto
13461 the BLOCK_VARS list for this outer scope, but are strung off of the
13462 DECL_ARGUMENTS list for the function instead.
13464 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
13465 the LABEL_DECL nodes for the function however, and we output DWARF info
13466 for those in decls_for_scope. Just within the `outer_scope' there will be
13467 a BLOCK node representing the function's outermost pair of curly braces,
13468 and any blocks used for the base and member initializers of a C++
13469 constructor function. */
13470 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
13472 /* Emit a DW_TAG_variable DIE for a named return value. */
13473 if (DECL_NAME (DECL_RESULT (decl)))
13474 gen_decl_die (DECL_RESULT (decl), subr_die);
13476 current_function_has_inlines = 0;
13477 decls_for_scope (outer_scope, subr_die, 0);
13479 #if 0 && defined (MIPS_DEBUGGING_INFO)
13480 if (current_function_has_inlines)
13482 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
13483 if (! comp_unit_has_inlines)
13485 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
13486 comp_unit_has_inlines = 1;
13489 #endif
13491 /* Add the calling convention attribute if requested. */
13492 add_calling_convention_attribute (subr_die, decl);
13496 /* Generate a DIE to represent a declared data object. */
13498 static void
13499 gen_variable_die (tree decl, dw_die_ref context_die)
13501 HOST_WIDE_INT off;
13502 tree com_decl;
13503 dw_die_ref var_die;
13504 tree origin = decl_ultimate_origin (decl);
13505 dw_die_ref old_die = lookup_decl_die (decl);
13506 int declaration = (DECL_EXTERNAL (decl)
13507 /* If DECL is COMDAT and has not actually been
13508 emitted, we cannot take its address; there
13509 might end up being no definition anywhere in
13510 the program. For example, consider the C++
13511 test case:
13513 template <class T>
13514 struct S { static const int i = 7; };
13516 template <class T>
13517 const int S<T>::i;
13519 int f() { return S<int>::i; }
13521 Here, S<int>::i is not DECL_EXTERNAL, but no
13522 definition is required, so the compiler will
13523 not emit a definition. */
13524 || (TREE_CODE (decl) == VAR_DECL
13525 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
13526 || class_or_namespace_scope_p (context_die));
13528 com_decl = fortran_common (decl, &off);
13530 /* Symbol in common gets emitted as a child of the common block, in the form
13531 of a data member.
13533 ??? This creates a new common block die for every common block symbol.
13534 Better to share same common block die for all symbols in that block. */
13535 if (com_decl)
13537 tree field;
13538 dw_die_ref com_die;
13539 const char *cnam = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
13540 dw_loc_descr_ref loc = loc_descriptor_from_tree (com_decl);
13542 field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
13543 var_die = new_die (DW_TAG_common_block, context_die, decl);
13544 add_name_and_src_coords_attributes (var_die, field);
13545 add_AT_flag (var_die, DW_AT_external, 1);
13546 add_AT_loc (var_die, DW_AT_location, loc);
13547 com_die = new_die (DW_TAG_member, var_die, decl);
13548 add_name_and_src_coords_attributes (com_die, decl);
13549 add_type_attribute (com_die, TREE_TYPE (decl), TREE_READONLY (decl),
13550 TREE_THIS_VOLATILE (decl), context_die);
13551 add_AT_loc (com_die, DW_AT_data_member_location,
13552 int_loc_descriptor (off));
13553 add_pubname_string (cnam, var_die); /* ??? needed? */
13554 return;
13557 var_die = new_die (DW_TAG_variable, context_die, decl);
13559 if (origin != NULL)
13560 add_abstract_origin_attribute (var_die, origin);
13562 /* Loop unrolling can create multiple blocks that refer to the same
13563 static variable, so we must test for the DW_AT_declaration flag.
13565 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
13566 copy decls and set the DECL_ABSTRACT flag on them instead of
13567 sharing them.
13569 ??? Duplicated blocks have been rewritten to use .debug_ranges.
13571 ??? The declare_in_namespace support causes us to get two DIEs for one
13572 variable, both of which are declarations. We want to avoid considering
13573 one to be a specification, so we must test that this DIE is not a
13574 declaration. */
13575 else if (old_die && TREE_STATIC (decl) && ! declaration
13576 && get_AT_flag (old_die, DW_AT_declaration) == 1)
13578 /* This is a definition of a C++ class level static. */
13579 add_AT_specification (var_die, old_die);
13580 if (DECL_NAME (decl))
13582 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13583 struct dwarf_file_data * file_index = lookup_filename (s.file);
13585 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13586 add_AT_file (var_die, DW_AT_decl_file, file_index);
13588 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13589 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
13592 else
13594 tree type = TREE_TYPE (decl);
13595 if ((TREE_CODE (decl) == PARM_DECL
13596 || TREE_CODE (decl) == RESULT_DECL)
13597 && DECL_BY_REFERENCE (decl))
13598 type = TREE_TYPE (type);
13600 add_name_and_src_coords_attributes (var_die, decl);
13601 add_type_attribute (var_die, type, TREE_READONLY (decl),
13602 TREE_THIS_VOLATILE (decl), context_die);
13604 if (TREE_PUBLIC (decl))
13605 add_AT_flag (var_die, DW_AT_external, 1);
13607 if (DECL_ARTIFICIAL (decl))
13608 add_AT_flag (var_die, DW_AT_artificial, 1);
13610 if (TREE_PROTECTED (decl))
13611 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
13612 else if (TREE_PRIVATE (decl))
13613 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
13616 if (declaration)
13617 add_AT_flag (var_die, DW_AT_declaration, 1);
13619 if (DECL_ABSTRACT (decl) || declaration)
13620 equate_decl_number_to_die (decl, var_die);
13622 if (! declaration && ! DECL_ABSTRACT (decl))
13624 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
13625 add_pubname (decl, var_die);
13627 else
13628 tree_add_const_value_attribute (var_die, decl);
13631 /* Generate a DIE to represent a label identifier. */
13633 static void
13634 gen_label_die (tree decl, dw_die_ref context_die)
13636 tree origin = decl_ultimate_origin (decl);
13637 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
13638 rtx insn;
13639 char label[MAX_ARTIFICIAL_LABEL_BYTES];
13641 if (origin != NULL)
13642 add_abstract_origin_attribute (lbl_die, origin);
13643 else
13644 add_name_and_src_coords_attributes (lbl_die, decl);
13646 if (DECL_ABSTRACT (decl))
13647 equate_decl_number_to_die (decl, lbl_die);
13648 else
13650 insn = DECL_RTL_IF_SET (decl);
13652 /* Deleted labels are programmer specified labels which have been
13653 eliminated because of various optimizations. We still emit them
13654 here so that it is possible to put breakpoints on them. */
13655 if (insn
13656 && (LABEL_P (insn)
13657 || ((NOTE_P (insn)
13658 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
13660 /* When optimization is enabled (via -O) some parts of the compiler
13661 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
13662 represent source-level labels which were explicitly declared by
13663 the user. This really shouldn't be happening though, so catch
13664 it if it ever does happen. */
13665 gcc_assert (!INSN_DELETED_P (insn));
13667 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
13668 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
13673 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
13674 attributes to the DIE for a block STMT, to describe where the inlined
13675 function was called from. This is similar to add_src_coords_attributes. */
13677 static inline void
13678 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
13680 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
13682 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
13683 add_AT_unsigned (die, DW_AT_call_line, s.line);
13687 /* If STMT's abstract origin is a function declaration and STMT's
13688 first subblock's abstract origin is the function's outermost block,
13689 then we're looking at the main entry point. */
13690 static bool
13691 is_inlined_entry_point (const_tree stmt)
13693 tree decl, block;
13695 if (!stmt || TREE_CODE (stmt) != BLOCK)
13696 return false;
13698 decl = block_ultimate_origin (stmt);
13700 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
13701 return false;
13703 block = BLOCK_SUBBLOCKS (stmt);
13705 if (block)
13707 if (TREE_CODE (block) != BLOCK)
13708 return false;
13710 block = block_ultimate_origin (block);
13713 return block == DECL_INITIAL (decl);
13716 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
13717 Add low_pc and high_pc attributes to the DIE for a block STMT. */
13719 static inline void
13720 add_high_low_attributes (tree stmt, dw_die_ref die)
13722 char label[MAX_ARTIFICIAL_LABEL_BYTES];
13724 if (BLOCK_FRAGMENT_CHAIN (stmt))
13726 tree chain;
13728 if (is_inlined_entry_point (stmt))
13730 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
13731 BLOCK_NUMBER (stmt));
13732 add_AT_lbl_id (die, DW_AT_entry_pc, label);
13735 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
13737 chain = BLOCK_FRAGMENT_CHAIN (stmt);
13740 add_ranges (chain);
13741 chain = BLOCK_FRAGMENT_CHAIN (chain);
13743 while (chain);
13744 add_ranges (NULL);
13746 else
13748 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
13749 BLOCK_NUMBER (stmt));
13750 add_AT_lbl_id (die, DW_AT_low_pc, label);
13751 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
13752 BLOCK_NUMBER (stmt));
13753 add_AT_lbl_id (die, DW_AT_high_pc, label);
13757 /* Generate a DIE for a lexical block. */
13759 static void
13760 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
13762 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
13764 if (! BLOCK_ABSTRACT (stmt))
13765 add_high_low_attributes (stmt, stmt_die);
13767 decls_for_scope (stmt, stmt_die, depth);
13770 /* Generate a DIE for an inlined subprogram. */
13772 static void
13773 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
13775 tree decl = block_ultimate_origin (stmt);
13777 /* Emit info for the abstract instance first, if we haven't yet. We
13778 must emit this even if the block is abstract, otherwise when we
13779 emit the block below (or elsewhere), we may end up trying to emit
13780 a die whose origin die hasn't been emitted, and crashing. */
13781 dwarf2out_abstract_function (decl);
13783 if (! BLOCK_ABSTRACT (stmt))
13785 dw_die_ref subr_die
13786 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
13788 add_abstract_origin_attribute (subr_die, decl);
13789 add_high_low_attributes (stmt, subr_die);
13790 add_call_src_coords_attributes (stmt, subr_die);
13792 decls_for_scope (stmt, subr_die, depth);
13793 current_function_has_inlines = 1;
13795 else
13796 /* We may get here if we're the outer block of function A that was
13797 inlined into function B that was inlined into function C. When
13798 generating debugging info for C, dwarf2out_abstract_function(B)
13799 would mark all inlined blocks as abstract, including this one.
13800 So, we wouldn't (and shouldn't) expect labels to be generated
13801 for this one. Instead, just emit debugging info for
13802 declarations within the block. This is particularly important
13803 in the case of initializers of arguments passed from B to us:
13804 if they're statement expressions containing declarations, we
13805 wouldn't generate dies for their abstract variables, and then,
13806 when generating dies for the real variables, we'd die (pun
13807 intended :-) */
13808 gen_lexical_block_die (stmt, context_die, depth);
13811 /* Generate a DIE for a field in a record, or structure. */
13813 static void
13814 gen_field_die (tree decl, dw_die_ref context_die)
13816 dw_die_ref decl_die;
13818 if (TREE_TYPE (decl) == error_mark_node)
13819 return;
13821 decl_die = new_die (DW_TAG_member, context_die, decl);
13822 add_name_and_src_coords_attributes (decl_die, decl);
13823 add_type_attribute (decl_die, member_declared_type (decl),
13824 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
13825 context_die);
13827 if (DECL_BIT_FIELD_TYPE (decl))
13829 add_byte_size_attribute (decl_die, decl);
13830 add_bit_size_attribute (decl_die, decl);
13831 add_bit_offset_attribute (decl_die, decl);
13834 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
13835 add_data_member_location_attribute (decl_die, decl);
13837 if (DECL_ARTIFICIAL (decl))
13838 add_AT_flag (decl_die, DW_AT_artificial, 1);
13840 if (TREE_PROTECTED (decl))
13841 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
13842 else if (TREE_PRIVATE (decl))
13843 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
13845 /* Equate decl number to die, so that we can look up this decl later on. */
13846 equate_decl_number_to_die (decl, decl_die);
13849 #if 0
13850 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13851 Use modified_type_die instead.
13852 We keep this code here just in case these types of DIEs may be needed to
13853 represent certain things in other languages (e.g. Pascal) someday. */
13855 static void
13856 gen_pointer_type_die (tree type, dw_die_ref context_die)
13858 dw_die_ref ptr_die
13859 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
13861 equate_type_number_to_die (type, ptr_die);
13862 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13863 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13866 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13867 Use modified_type_die instead.
13868 We keep this code here just in case these types of DIEs may be needed to
13869 represent certain things in other languages (e.g. Pascal) someday. */
13871 static void
13872 gen_reference_type_die (tree type, dw_die_ref context_die)
13874 dw_die_ref ref_die
13875 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
13877 equate_type_number_to_die (type, ref_die);
13878 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
13879 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13881 #endif
13883 /* Generate a DIE for a pointer to a member type. */
13885 static void
13886 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
13888 dw_die_ref ptr_die
13889 = new_die (DW_TAG_ptr_to_member_type,
13890 scope_die_for (type, context_die), type);
13892 equate_type_number_to_die (type, ptr_die);
13893 add_AT_die_ref (ptr_die, DW_AT_containing_type,
13894 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
13895 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13898 /* Generate the DIE for the compilation unit. */
13900 static dw_die_ref
13901 gen_compile_unit_die (const char *filename)
13903 dw_die_ref die;
13904 char producer[250];
13905 const char *language_string = lang_hooks.name;
13906 int language;
13908 die = new_die (DW_TAG_compile_unit, NULL, NULL);
13910 if (filename)
13912 add_name_attribute (die, filename);
13913 /* Don't add cwd for <built-in>. */
13914 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
13915 add_comp_dir_attribute (die);
13918 sprintf (producer, "%s %s", language_string, version_string);
13920 #ifdef MIPS_DEBUGGING_INFO
13921 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
13922 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
13923 not appear in the producer string, the debugger reaches the conclusion
13924 that the object file is stripped and has no debugging information.
13925 To get the MIPS/SGI debugger to believe that there is debugging
13926 information in the object file, we add a -g to the producer string. */
13927 if (debug_info_level > DINFO_LEVEL_TERSE)
13928 strcat (producer, " -g");
13929 #endif
13931 add_AT_string (die, DW_AT_producer, producer);
13933 if (strcmp (language_string, "GNU C++") == 0)
13934 language = DW_LANG_C_plus_plus;
13935 else if (strcmp (language_string, "GNU Ada") == 0)
13936 language = DW_LANG_Ada95;
13937 else if (strcmp (language_string, "GNU F77") == 0)
13938 language = DW_LANG_Fortran77;
13939 else if (strcmp (language_string, "GNU Fortran") == 0)
13940 language = DW_LANG_Fortran95;
13941 else if (strcmp (language_string, "GNU Pascal") == 0)
13942 language = DW_LANG_Pascal83;
13943 else if (strcmp (language_string, "GNU Java") == 0)
13944 language = DW_LANG_Java;
13945 else if (strcmp (language_string, "GNU Objective-C") == 0)
13946 language = DW_LANG_ObjC;
13947 else if (strcmp (language_string, "GNU Objective-C++") == 0)
13948 language = DW_LANG_ObjC_plus_plus;
13949 else
13950 language = DW_LANG_C89;
13952 add_AT_unsigned (die, DW_AT_language, language);
13953 return die;
13956 /* Generate the DIE for a base class. */
13958 static void
13959 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
13961 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
13963 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
13964 add_data_member_location_attribute (die, binfo);
13966 if (BINFO_VIRTUAL_P (binfo))
13967 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13969 if (access == access_public_node)
13970 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13971 else if (access == access_protected_node)
13972 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13975 /* Generate a DIE for a class member. */
13977 static void
13978 gen_member_die (tree type, dw_die_ref context_die)
13980 tree member;
13981 tree binfo = TYPE_BINFO (type);
13982 dw_die_ref child;
13984 /* If this is not an incomplete type, output descriptions of each of its
13985 members. Note that as we output the DIEs necessary to represent the
13986 members of this record or union type, we will also be trying to output
13987 DIEs to represent the *types* of those members. However the `type'
13988 function (above) will specifically avoid generating type DIEs for member
13989 types *within* the list of member DIEs for this (containing) type except
13990 for those types (of members) which are explicitly marked as also being
13991 members of this (containing) type themselves. The g++ front- end can
13992 force any given type to be treated as a member of some other (containing)
13993 type by setting the TYPE_CONTEXT of the given (member) type to point to
13994 the TREE node representing the appropriate (containing) type. */
13996 /* First output info about the base classes. */
13997 if (binfo)
13999 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14000 int i;
14001 tree base;
14003 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14004 gen_inheritance_die (base,
14005 (accesses ? VEC_index (tree, accesses, i)
14006 : access_public_node), context_die);
14009 /* Now output info about the data members and type members. */
14010 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14012 /* If we thought we were generating minimal debug info for TYPE
14013 and then changed our minds, some of the member declarations
14014 may have already been defined. Don't define them again, but
14015 do put them in the right order. */
14017 child = lookup_decl_die (member);
14018 if (child)
14019 splice_child_die (context_die, child);
14020 else
14021 gen_decl_die (member, context_die);
14024 /* Now output info about the function members (if any). */
14025 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14027 /* Don't include clones in the member list. */
14028 if (DECL_ABSTRACT_ORIGIN (member))
14029 continue;
14031 child = lookup_decl_die (member);
14032 if (child)
14033 splice_child_die (context_die, child);
14034 else
14035 gen_decl_die (member, context_die);
14039 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
14040 is set, we pretend that the type was never defined, so we only get the
14041 member DIEs needed by later specification DIEs. */
14043 static void
14044 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14045 enum debug_info_usage usage)
14047 dw_die_ref type_die = lookup_type_die (type);
14048 dw_die_ref scope_die = 0;
14049 int nested = 0;
14050 int complete = (TYPE_SIZE (type)
14051 && (! TYPE_STUB_DECL (type)
14052 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14053 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14054 complete = complete && should_emit_struct_debug (type, usage);
14056 if (type_die && ! complete)
14057 return;
14059 if (TYPE_CONTEXT (type) != NULL_TREE
14060 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14061 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14062 nested = 1;
14064 scope_die = scope_die_for (type, context_die);
14066 if (! type_die || (nested && scope_die == comp_unit_die))
14067 /* First occurrence of type or toplevel definition of nested class. */
14069 dw_die_ref old_die = type_die;
14071 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14072 ? record_type_tag (type) : DW_TAG_union_type,
14073 scope_die, type);
14074 equate_type_number_to_die (type, type_die);
14075 if (old_die)
14076 add_AT_specification (type_die, old_die);
14077 else
14078 add_name_attribute (type_die, type_tag (type));
14080 else
14081 remove_AT (type_die, DW_AT_declaration);
14083 /* If this type has been completed, then give it a byte_size attribute and
14084 then give a list of members. */
14085 if (complete && !ns_decl)
14087 /* Prevent infinite recursion in cases where the type of some member of
14088 this type is expressed in terms of this type itself. */
14089 TREE_ASM_WRITTEN (type) = 1;
14090 add_byte_size_attribute (type_die, type);
14091 if (TYPE_STUB_DECL (type) != NULL_TREE)
14092 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14094 /* If the first reference to this type was as the return type of an
14095 inline function, then it may not have a parent. Fix this now. */
14096 if (type_die->die_parent == NULL)
14097 add_child_die (scope_die, type_die);
14099 push_decl_scope (type);
14100 gen_member_die (type, type_die);
14101 pop_decl_scope ();
14103 /* GNU extension: Record what type our vtable lives in. */
14104 if (TYPE_VFIELD (type))
14106 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14108 gen_type_die (vtype, context_die);
14109 add_AT_die_ref (type_die, DW_AT_containing_type,
14110 lookup_type_die (vtype));
14113 else
14115 add_AT_flag (type_die, DW_AT_declaration, 1);
14117 /* We don't need to do this for function-local types. */
14118 if (TYPE_STUB_DECL (type)
14119 && ! decl_function_context (TYPE_STUB_DECL (type)))
14120 VEC_safe_push (tree, gc, incomplete_types, type);
14123 if (get_AT (type_die, DW_AT_name))
14124 add_pubtype (type, type_die);
14127 /* Generate a DIE for a subroutine _type_. */
14129 static void
14130 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14132 tree return_type = TREE_TYPE (type);
14133 dw_die_ref subr_die
14134 = new_die (DW_TAG_subroutine_type,
14135 scope_die_for (type, context_die), type);
14137 equate_type_number_to_die (type, subr_die);
14138 add_prototyped_attribute (subr_die, type);
14139 add_type_attribute (subr_die, return_type, 0, 0, context_die);
14140 gen_formal_types_die (type, subr_die);
14142 if (get_AT (subr_die, DW_AT_name))
14143 add_pubtype (type, subr_die);
14146 /* Generate a DIE for a type definition. */
14148 static void
14149 gen_typedef_die (tree decl, dw_die_ref context_die)
14151 dw_die_ref type_die;
14152 tree origin;
14154 if (TREE_ASM_WRITTEN (decl))
14155 return;
14157 TREE_ASM_WRITTEN (decl) = 1;
14158 type_die = new_die (DW_TAG_typedef, context_die, decl);
14159 origin = decl_ultimate_origin (decl);
14160 if (origin != NULL)
14161 add_abstract_origin_attribute (type_die, origin);
14162 else
14164 tree type;
14166 add_name_and_src_coords_attributes (type_die, decl);
14167 if (DECL_ORIGINAL_TYPE (decl))
14169 type = DECL_ORIGINAL_TYPE (decl);
14171 gcc_assert (type != TREE_TYPE (decl));
14172 equate_type_number_to_die (TREE_TYPE (decl), type_die);
14174 else
14175 type = TREE_TYPE (decl);
14177 add_type_attribute (type_die, type, TREE_READONLY (decl),
14178 TREE_THIS_VOLATILE (decl), context_die);
14181 if (DECL_ABSTRACT (decl))
14182 equate_decl_number_to_die (decl, type_die);
14184 if (get_AT (type_die, DW_AT_name))
14185 add_pubtype (decl, type_die);
14188 /* Generate a type description DIE. */
14190 static void
14191 gen_type_die_with_usage (tree type, dw_die_ref context_die,
14192 enum debug_info_usage usage)
14194 int need_pop;
14195 struct array_descr_info info;
14197 if (type == NULL_TREE || type == error_mark_node)
14198 return;
14200 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14201 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
14203 if (TREE_ASM_WRITTEN (type))
14204 return;
14206 /* Prevent broken recursion; we can't hand off to the same type. */
14207 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
14209 TREE_ASM_WRITTEN (type) = 1;
14210 gen_decl_die (TYPE_NAME (type), context_die);
14211 return;
14214 /* If this is an array type with hidden descriptor, handle it first. */
14215 if (!TREE_ASM_WRITTEN (type)
14216 && lang_hooks.types.get_array_descr_info
14217 && lang_hooks.types.get_array_descr_info (type, &info))
14219 gen_descr_array_type_die (type, &info, context_die);
14220 TREE_ASM_WRITTEN (type) = 1;
14221 return;
14224 /* We are going to output a DIE to represent the unqualified version
14225 of this type (i.e. without any const or volatile qualifiers) so
14226 get the main variant (i.e. the unqualified version) of this type
14227 now. (Vectors are special because the debugging info is in the
14228 cloned type itself). */
14229 if (TREE_CODE (type) != VECTOR_TYPE)
14230 type = type_main_variant (type);
14232 if (TREE_ASM_WRITTEN (type))
14233 return;
14235 switch (TREE_CODE (type))
14237 case ERROR_MARK:
14238 break;
14240 case POINTER_TYPE:
14241 case REFERENCE_TYPE:
14242 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
14243 ensures that the gen_type_die recursion will terminate even if the
14244 type is recursive. Recursive types are possible in Ada. */
14245 /* ??? We could perhaps do this for all types before the switch
14246 statement. */
14247 TREE_ASM_WRITTEN (type) = 1;
14249 /* For these types, all that is required is that we output a DIE (or a
14250 set of DIEs) to represent the "basis" type. */
14251 gen_type_die_with_usage (TREE_TYPE (type), context_die,
14252 DINFO_USAGE_IND_USE);
14253 break;
14255 case OFFSET_TYPE:
14256 /* This code is used for C++ pointer-to-data-member types.
14257 Output a description of the relevant class type. */
14258 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
14259 DINFO_USAGE_IND_USE);
14261 /* Output a description of the type of the object pointed to. */
14262 gen_type_die_with_usage (TREE_TYPE (type), context_die,
14263 DINFO_USAGE_IND_USE);
14265 /* Now output a DIE to represent this pointer-to-data-member type
14266 itself. */
14267 gen_ptr_to_mbr_type_die (type, context_die);
14268 break;
14270 case FUNCTION_TYPE:
14271 /* Force out return type (in case it wasn't forced out already). */
14272 gen_type_die_with_usage (TREE_TYPE (type), context_die,
14273 DINFO_USAGE_DIR_USE);
14274 gen_subroutine_type_die (type, context_die);
14275 break;
14277 case METHOD_TYPE:
14278 /* Force out return type (in case it wasn't forced out already). */
14279 gen_type_die_with_usage (TREE_TYPE (type), context_die,
14280 DINFO_USAGE_DIR_USE);
14281 gen_subroutine_type_die (type, context_die);
14282 break;
14284 case ARRAY_TYPE:
14285 gen_array_type_die (type, context_die);
14286 break;
14288 case VECTOR_TYPE:
14289 gen_array_type_die (type, context_die);
14290 break;
14292 case ENUMERAL_TYPE:
14293 case RECORD_TYPE:
14294 case UNION_TYPE:
14295 case QUAL_UNION_TYPE:
14296 /* If this is a nested type whose containing class hasn't been written
14297 out yet, writing it out will cover this one, too. This does not apply
14298 to instantiations of member class templates; they need to be added to
14299 the containing class as they are generated. FIXME: This hurts the
14300 idea of combining type decls from multiple TUs, since we can't predict
14301 what set of template instantiations we'll get. */
14302 if (TYPE_CONTEXT (type)
14303 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14304 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
14306 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
14308 if (TREE_ASM_WRITTEN (type))
14309 return;
14311 /* If that failed, attach ourselves to the stub. */
14312 push_decl_scope (TYPE_CONTEXT (type));
14313 context_die = lookup_type_die (TYPE_CONTEXT (type));
14314 need_pop = 1;
14316 else
14318 declare_in_namespace (type, context_die);
14319 need_pop = 0;
14322 if (TREE_CODE (type) == ENUMERAL_TYPE)
14324 /* This might have been written out by the call to
14325 declare_in_namespace. */
14326 if (!TREE_ASM_WRITTEN (type))
14327 gen_enumeration_type_die (type, context_die);
14329 else
14330 gen_struct_or_union_type_die (type, context_die, usage);
14332 if (need_pop)
14333 pop_decl_scope ();
14335 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
14336 it up if it is ever completed. gen_*_type_die will set it for us
14337 when appropriate. */
14338 return;
14340 case VOID_TYPE:
14341 case INTEGER_TYPE:
14342 case REAL_TYPE:
14343 case FIXED_POINT_TYPE:
14344 case COMPLEX_TYPE:
14345 case BOOLEAN_TYPE:
14346 /* No DIEs needed for fundamental types. */
14347 break;
14349 case LANG_TYPE:
14350 /* No Dwarf representation currently defined. */
14351 break;
14353 default:
14354 gcc_unreachable ();
14357 TREE_ASM_WRITTEN (type) = 1;
14360 static void
14361 gen_type_die (tree type, dw_die_ref context_die)
14363 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
14366 /* Generate a DIE for a tagged type instantiation. */
14368 static void
14369 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
14371 if (type == NULL_TREE || type == error_mark_node)
14372 return;
14374 /* We are going to output a DIE to represent the unqualified version of
14375 this type (i.e. without any const or volatile qualifiers) so make sure
14376 that we have the main variant (i.e. the unqualified version) of this
14377 type now. */
14378 gcc_assert (type == type_main_variant (type));
14380 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
14381 an instance of an unresolved type. */
14383 switch (TREE_CODE (type))
14385 case ERROR_MARK:
14386 break;
14388 case ENUMERAL_TYPE:
14389 gen_inlined_enumeration_type_die (type, context_die);
14390 break;
14392 case RECORD_TYPE:
14393 gen_inlined_structure_type_die (type, context_die);
14394 break;
14396 case UNION_TYPE:
14397 case QUAL_UNION_TYPE:
14398 gen_inlined_union_type_die (type, context_die);
14399 break;
14401 default:
14402 gcc_unreachable ();
14406 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
14407 things which are local to the given block. */
14409 static void
14410 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
14412 int must_output_die = 0;
14413 tree origin;
14414 tree decl;
14415 enum tree_code origin_code;
14417 /* Ignore blocks that are NULL. */
14418 if (stmt == NULL_TREE)
14419 return;
14421 /* If the block is one fragment of a non-contiguous block, do not
14422 process the variables, since they will have been done by the
14423 origin block. Do process subblocks. */
14424 if (BLOCK_FRAGMENT_ORIGIN (stmt))
14426 tree sub;
14428 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
14429 gen_block_die (sub, context_die, depth + 1);
14431 return;
14434 /* Determine the "ultimate origin" of this block. This block may be an
14435 inlined instance of an inlined instance of inline function, so we have
14436 to trace all of the way back through the origin chain to find out what
14437 sort of node actually served as the original seed for the creation of
14438 the current block. */
14439 origin = block_ultimate_origin (stmt);
14440 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
14442 /* Determine if we need to output any Dwarf DIEs at all to represent this
14443 block. */
14444 if (origin_code == FUNCTION_DECL)
14445 /* The outer scopes for inlinings *must* always be represented. We
14446 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
14447 must_output_die = 1;
14448 else
14450 /* In the case where the current block represents an inlining of the
14451 "body block" of an inline function, we must *NOT* output any DIE for
14452 this block because we have already output a DIE to represent the whole
14453 inlined function scope and the "body block" of any function doesn't
14454 really represent a different scope according to ANSI C rules. So we
14455 check here to make sure that this block does not represent a "body
14456 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
14457 if (! is_body_block (origin ? origin : stmt))
14459 /* Determine if this block directly contains any "significant"
14460 local declarations which we will need to output DIEs for. */
14461 if (debug_info_level > DINFO_LEVEL_TERSE)
14462 /* We are not in terse mode so *any* local declaration counts
14463 as being a "significant" one. */
14464 must_output_die = (BLOCK_VARS (stmt) != NULL
14465 && (TREE_USED (stmt)
14466 || TREE_ASM_WRITTEN (stmt)
14467 || BLOCK_ABSTRACT (stmt)));
14468 else
14469 /* We are in terse mode, so only local (nested) function
14470 definitions count as "significant" local declarations. */
14471 for (decl = BLOCK_VARS (stmt);
14472 decl != NULL; decl = TREE_CHAIN (decl))
14473 if (TREE_CODE (decl) == FUNCTION_DECL
14474 && DECL_INITIAL (decl))
14476 must_output_die = 1;
14477 break;
14482 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
14483 DIE for any block which contains no significant local declarations at
14484 all. Rather, in such cases we just call `decls_for_scope' so that any
14485 needed Dwarf info for any sub-blocks will get properly generated. Note
14486 that in terse mode, our definition of what constitutes a "significant"
14487 local declaration gets restricted to include only inlined function
14488 instances and local (nested) function definitions. */
14489 if (must_output_die)
14491 if (origin_code == FUNCTION_DECL)
14492 gen_inlined_subroutine_die (stmt, context_die, depth);
14493 else
14494 gen_lexical_block_die (stmt, context_die, depth);
14496 else
14497 decls_for_scope (stmt, context_die, depth);
14500 /* Generate all of the decls declared within a given scope and (recursively)
14501 all of its sub-blocks. */
14503 static void
14504 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
14506 tree decl;
14507 tree subblocks;
14509 /* Ignore NULL blocks. */
14510 if (stmt == NULL_TREE)
14511 return;
14513 if (TREE_USED (stmt))
14515 /* Output the DIEs to represent all of the data objects and typedefs
14516 declared directly within this block but not within any nested
14517 sub-blocks. Also, nested function and tag DIEs have been
14518 generated with a parent of NULL; fix that up now. */
14519 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
14521 dw_die_ref die;
14523 if (TREE_CODE (decl) == FUNCTION_DECL)
14524 die = lookup_decl_die (decl);
14525 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
14526 die = lookup_type_die (TREE_TYPE (decl));
14527 else
14528 die = NULL;
14530 if (die != NULL && die->die_parent == NULL)
14531 add_child_die (context_die, die);
14532 /* Do not produce debug information for static variables since
14533 these might be optimized out. We are called for these later
14534 in varpool_analyze_pending_decls.
14536 But *do* produce it for Fortran COMMON variables because,
14537 even though they are static, their names can differ depending
14538 on the scope, which we need to preserve. */
14539 if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)
14540 && !(is_fortran () && TREE_PUBLIC (decl)))
14542 else
14543 gen_decl_die (decl, context_die);
14547 /* If we're at -g1, we're not interested in subblocks. */
14548 if (debug_info_level <= DINFO_LEVEL_TERSE)
14549 return;
14551 /* Output the DIEs to represent all sub-blocks (and the items declared
14552 therein) of this block. */
14553 for (subblocks = BLOCK_SUBBLOCKS (stmt);
14554 subblocks != NULL;
14555 subblocks = BLOCK_CHAIN (subblocks))
14556 gen_block_die (subblocks, context_die, depth + 1);
14559 /* Is this a typedef we can avoid emitting? */
14561 static inline int
14562 is_redundant_typedef (const_tree decl)
14564 if (TYPE_DECL_IS_STUB (decl))
14565 return 1;
14567 if (DECL_ARTIFICIAL (decl)
14568 && DECL_CONTEXT (decl)
14569 && is_tagged_type (DECL_CONTEXT (decl))
14570 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
14571 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
14572 /* Also ignore the artificial member typedef for the class name. */
14573 return 1;
14575 return 0;
14578 /* Returns the DIE for a context. */
14580 static inline dw_die_ref
14581 get_context_die (tree context)
14583 if (context)
14585 /* Find die that represents this context. */
14586 if (TYPE_P (context))
14587 return force_type_die (context);
14588 else
14589 return force_decl_die (context);
14591 return comp_unit_die;
14594 /* Returns the DIE for decl. A DIE will always be returned. */
14596 static dw_die_ref
14597 force_decl_die (tree decl)
14599 dw_die_ref decl_die;
14600 unsigned saved_external_flag;
14601 tree save_fn = NULL_TREE;
14602 decl_die = lookup_decl_die (decl);
14603 if (!decl_die)
14605 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
14607 decl_die = lookup_decl_die (decl);
14608 if (decl_die)
14609 return decl_die;
14611 switch (TREE_CODE (decl))
14613 case FUNCTION_DECL:
14614 /* Clear current_function_decl, so that gen_subprogram_die thinks
14615 that this is a declaration. At this point, we just want to force
14616 declaration die. */
14617 save_fn = current_function_decl;
14618 current_function_decl = NULL_TREE;
14619 gen_subprogram_die (decl, context_die);
14620 current_function_decl = save_fn;
14621 break;
14623 case VAR_DECL:
14624 /* Set external flag to force declaration die. Restore it after
14625 gen_decl_die() call. */
14626 saved_external_flag = DECL_EXTERNAL (decl);
14627 DECL_EXTERNAL (decl) = 1;
14628 gen_decl_die (decl, context_die);
14629 DECL_EXTERNAL (decl) = saved_external_flag;
14630 break;
14632 case NAMESPACE_DECL:
14633 dwarf2out_decl (decl);
14634 break;
14636 default:
14637 gcc_unreachable ();
14640 /* We should be able to find the DIE now. */
14641 if (!decl_die)
14642 decl_die = lookup_decl_die (decl);
14643 gcc_assert (decl_die);
14646 return decl_die;
14649 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
14650 always returned. */
14652 static dw_die_ref
14653 force_type_die (tree type)
14655 dw_die_ref type_die;
14657 type_die = lookup_type_die (type);
14658 if (!type_die)
14660 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
14662 type_die = modified_type_die (type, TYPE_READONLY (type),
14663 TYPE_VOLATILE (type), context_die);
14664 gcc_assert (type_die);
14666 return type_die;
14669 /* Force out any required namespaces to be able to output DECL,
14670 and return the new context_die for it, if it's changed. */
14672 static dw_die_ref
14673 setup_namespace_context (tree thing, dw_die_ref context_die)
14675 tree context = (DECL_P (thing)
14676 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
14677 if (context && TREE_CODE (context) == NAMESPACE_DECL)
14678 /* Force out the namespace. */
14679 context_die = force_decl_die (context);
14681 return context_die;
14684 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
14685 type) within its namespace, if appropriate.
14687 For compatibility with older debuggers, namespace DIEs only contain
14688 declarations; all definitions are emitted at CU scope. */
14690 static void
14691 declare_in_namespace (tree thing, dw_die_ref context_die)
14693 dw_die_ref ns_context;
14695 if (debug_info_level <= DINFO_LEVEL_TERSE)
14696 return;
14698 /* If this decl is from an inlined function, then don't try to emit it in its
14699 namespace, as we will get confused. It would have already been emitted
14700 when the abstract instance of the inline function was emitted anyways. */
14701 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
14702 return;
14704 ns_context = setup_namespace_context (thing, context_die);
14706 if (ns_context != context_die)
14708 if (DECL_P (thing))
14709 gen_decl_die (thing, ns_context);
14710 else
14711 gen_type_die (thing, ns_context);
14715 /* Generate a DIE for a namespace or namespace alias. */
14717 static void
14718 gen_namespace_die (tree decl)
14720 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
14722 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
14723 they are an alias of. */
14724 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
14726 /* Output a real namespace. */
14727 dw_die_ref namespace_die
14728 = new_die (DW_TAG_namespace, context_die, decl);
14729 add_name_and_src_coords_attributes (namespace_die, decl);
14730 equate_decl_number_to_die (decl, namespace_die);
14732 else
14734 /* Output a namespace alias. */
14736 /* Force out the namespace we are an alias of, if necessary. */
14737 dw_die_ref origin_die
14738 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
14740 /* Now create the namespace alias DIE. */
14741 dw_die_ref namespace_die
14742 = new_die (DW_TAG_imported_declaration, context_die, decl);
14743 add_name_and_src_coords_attributes (namespace_die, decl);
14744 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
14745 equate_decl_number_to_die (decl, namespace_die);
14749 /* Generate Dwarf debug information for a decl described by DECL. */
14751 static void
14752 gen_decl_die (tree decl, dw_die_ref context_die)
14754 tree origin;
14756 if (DECL_P (decl) && DECL_IGNORED_P (decl))
14757 return;
14759 switch (TREE_CODE (decl))
14761 case ERROR_MARK:
14762 break;
14764 case CONST_DECL:
14765 /* The individual enumerators of an enum type get output when we output
14766 the Dwarf representation of the relevant enum type itself. */
14767 break;
14769 case FUNCTION_DECL:
14770 /* Don't output any DIEs to represent mere function declarations,
14771 unless they are class members or explicit block externs. */
14772 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
14773 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
14774 break;
14776 #if 0
14777 /* FIXME */
14778 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
14779 on local redeclarations of global functions. That seems broken. */
14780 if (current_function_decl != decl)
14781 /* This is only a declaration. */;
14782 #endif
14784 /* If we're emitting a clone, emit info for the abstract instance. */
14785 if (DECL_ORIGIN (decl) != decl)
14786 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
14788 /* If we're emitting an out-of-line copy of an inline function,
14789 emit info for the abstract instance and set up to refer to it. */
14790 else if (cgraph_function_possibly_inlined_p (decl)
14791 && ! DECL_ABSTRACT (decl)
14792 && ! class_or_namespace_scope_p (context_die)
14793 /* dwarf2out_abstract_function won't emit a die if this is just
14794 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
14795 that case, because that works only if we have a die. */
14796 && DECL_INITIAL (decl) != NULL_TREE)
14798 dwarf2out_abstract_function (decl);
14799 set_decl_origin_self (decl);
14802 /* Otherwise we're emitting the primary DIE for this decl. */
14803 else if (debug_info_level > DINFO_LEVEL_TERSE)
14805 /* Before we describe the FUNCTION_DECL itself, make sure that we
14806 have described its return type. */
14807 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14809 /* And its virtual context. */
14810 if (DECL_VINDEX (decl) != NULL_TREE)
14811 gen_type_die (DECL_CONTEXT (decl), context_die);
14813 /* And its containing type. */
14814 origin = decl_class_context (decl);
14815 if (origin != NULL_TREE)
14816 gen_type_die_for_member (origin, decl, context_die);
14818 /* And its containing namespace. */
14819 declare_in_namespace (decl, context_die);
14822 /* Now output a DIE to represent the function itself. */
14823 gen_subprogram_die (decl, context_die);
14824 break;
14826 case TYPE_DECL:
14827 /* If we are in terse mode, don't generate any DIEs to represent any
14828 actual typedefs. */
14829 if (debug_info_level <= DINFO_LEVEL_TERSE)
14830 break;
14832 /* In the special case of a TYPE_DECL node representing the declaration
14833 of some type tag, if the given TYPE_DECL is marked as having been
14834 instantiated from some other (original) TYPE_DECL node (e.g. one which
14835 was generated within the original definition of an inline function) we
14836 have to generate a special (abbreviated) DW_TAG_structure_type,
14837 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
14838 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
14839 && is_tagged_type (TREE_TYPE (decl)))
14841 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
14842 break;
14845 if (is_redundant_typedef (decl))
14846 gen_type_die (TREE_TYPE (decl), context_die);
14847 else
14848 /* Output a DIE to represent the typedef itself. */
14849 gen_typedef_die (decl, context_die);
14850 break;
14852 case LABEL_DECL:
14853 if (debug_info_level >= DINFO_LEVEL_NORMAL)
14854 gen_label_die (decl, context_die);
14855 break;
14857 case VAR_DECL:
14858 case RESULT_DECL:
14859 /* If we are in terse mode, don't generate any DIEs to represent any
14860 variable declarations or definitions. */
14861 if (debug_info_level <= DINFO_LEVEL_TERSE)
14862 break;
14864 /* If this is the global definition of the Fortran COMMON block, we don't
14865 need to do anything. Syntactically, the block itself has no identity,
14866 just its constituent identifiers. */
14867 if (TREE_CODE (decl) == VAR_DECL
14868 && TREE_PUBLIC (decl)
14869 && TREE_STATIC (decl)
14870 && is_fortran ()
14871 && !DECL_HAS_VALUE_EXPR_P (decl))
14872 break;
14874 /* Output any DIEs that are needed to specify the type of this data
14875 object. */
14876 if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
14877 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14878 else
14879 gen_type_die (TREE_TYPE (decl), context_die);
14881 /* And its containing type. */
14882 origin = decl_class_context (decl);
14883 if (origin != NULL_TREE)
14884 gen_type_die_for_member (origin, decl, context_die);
14886 /* And its containing namespace. */
14887 declare_in_namespace (decl, context_die);
14889 /* Now output the DIE to represent the data object itself. This gets
14890 complicated because of the possibility that the VAR_DECL really
14891 represents an inlined instance of a formal parameter for an inline
14892 function. */
14893 origin = decl_ultimate_origin (decl);
14894 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
14895 gen_formal_parameter_die (decl, context_die);
14896 else
14897 gen_variable_die (decl, context_die);
14898 break;
14900 case FIELD_DECL:
14901 /* Ignore the nameless fields that are used to skip bits but handle C++
14902 anonymous unions and structs. */
14903 if (DECL_NAME (decl) != NULL_TREE
14904 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
14905 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
14907 gen_type_die (member_declared_type (decl), context_die);
14908 gen_field_die (decl, context_die);
14910 break;
14912 case PARM_DECL:
14913 if (DECL_BY_REFERENCE (decl))
14914 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14915 else
14916 gen_type_die (TREE_TYPE (decl), context_die);
14917 gen_formal_parameter_die (decl, context_die);
14918 break;
14920 case NAMESPACE_DECL:
14921 gen_namespace_die (decl);
14922 break;
14924 default:
14925 /* Probably some frontend-internal decl. Assume we don't care. */
14926 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
14927 break;
14931 /* Output debug information for global decl DECL. Called from toplev.c after
14932 compilation proper has finished. */
14934 static void
14935 dwarf2out_global_decl (tree decl)
14937 /* Output DWARF2 information for file-scope tentative data object
14938 declarations, file-scope (extern) function declarations (which
14939 had no corresponding body) and file-scope tagged type declarations
14940 and definitions which have not yet been forced out.
14942 Ignore the global decl of any Fortran COMMON blocks which also
14943 wind up here though they have already been described in the local
14944 scope for the procedures using them. */
14945 if (TREE_CODE (decl) == VAR_DECL
14946 && TREE_PUBLIC (decl) && TREE_STATIC (decl) && is_fortran ())
14947 return;
14949 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
14950 dwarf2out_decl (decl);
14953 /* Output debug information for type decl DECL. Called from toplev.c
14954 and from language front ends (to record built-in types). */
14955 static void
14956 dwarf2out_type_decl (tree decl, int local)
14958 if (!local)
14959 dwarf2out_decl (decl);
14962 /* Output debug information for imported module or decl. */
14964 static void
14965 dwarf2out_imported_module_or_decl (tree decl, tree context)
14967 dw_die_ref imported_die, at_import_die;
14968 dw_die_ref scope_die;
14969 expanded_location xloc;
14971 if (debug_info_level <= DINFO_LEVEL_TERSE)
14972 return;
14974 gcc_assert (decl);
14976 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
14977 We need decl DIE for reference and scope die. First, get DIE for the decl
14978 itself. */
14980 /* Get the scope die for decl context. Use comp_unit_die for global module
14981 or decl. If die is not found for non globals, force new die. */
14982 if (context
14983 && TYPE_P (context)
14984 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
14985 return;
14986 scope_die = get_context_die (context);
14988 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
14989 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
14991 if (is_base_type (TREE_TYPE (decl)))
14992 at_import_die = base_type_die (TREE_TYPE (decl));
14993 else
14994 at_import_die = force_type_die (TREE_TYPE (decl));
14995 /* For namespace N { typedef void T; } using N::T; base_type_die
14996 returns NULL, but DW_TAG_imported_declaration requires
14997 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
14998 if (!at_import_die)
15000 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15001 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15002 at_import_die = lookup_type_die (TREE_TYPE (decl));
15003 gcc_assert (at_import_die);
15006 else
15008 at_import_die = lookup_decl_die (decl);
15009 if (!at_import_die)
15011 /* If we're trying to avoid duplicate debug info, we may not have
15012 emitted the member decl for this field. Emit it now. */
15013 if (TREE_CODE (decl) == FIELD_DECL)
15015 tree type = DECL_CONTEXT (decl);
15017 if (TYPE_CONTEXT (type)
15018 && TYPE_P (TYPE_CONTEXT (type))
15019 && !should_emit_struct_debug (TYPE_CONTEXT (type),
15020 DINFO_USAGE_DIR_USE))
15021 return;
15022 gen_type_die_for_member (type, decl,
15023 get_context_die (TYPE_CONTEXT (type)));
15025 at_import_die = force_decl_die (decl);
15029 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
15030 if (TREE_CODE (decl) == NAMESPACE_DECL)
15031 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
15032 else
15033 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
15035 xloc = expand_location (input_location);
15036 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15037 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15038 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15041 /* Write the debugging output for DECL. */
15043 void
15044 dwarf2out_decl (tree decl)
15046 dw_die_ref context_die = comp_unit_die;
15048 switch (TREE_CODE (decl))
15050 case ERROR_MARK:
15051 return;
15053 case FUNCTION_DECL:
15054 /* What we would really like to do here is to filter out all mere
15055 file-scope declarations of file-scope functions which are never
15056 referenced later within this translation unit (and keep all of ones
15057 that *are* referenced later on) but we aren't clairvoyant, so we have
15058 no idea which functions will be referenced in the future (i.e. later
15059 on within the current translation unit). So here we just ignore all
15060 file-scope function declarations which are not also definitions. If
15061 and when the debugger needs to know something about these functions,
15062 it will have to hunt around and find the DWARF information associated
15063 with the definition of the function.
15065 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15066 nodes represent definitions and which ones represent mere
15067 declarations. We have to check DECL_INITIAL instead. That's because
15068 the C front-end supports some weird semantics for "extern inline"
15069 function definitions. These can get inlined within the current
15070 translation unit (and thus, we need to generate Dwarf info for their
15071 abstract instances so that the Dwarf info for the concrete inlined
15072 instances can have something to refer to) but the compiler never
15073 generates any out-of-lines instances of such things (despite the fact
15074 that they *are* definitions).
15076 The important point is that the C front-end marks these "extern
15077 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15078 them anyway. Note that the C++ front-end also plays some similar games
15079 for inline function definitions appearing within include files which
15080 also contain `#pragma interface' pragmas. */
15081 if (DECL_INITIAL (decl) == NULL_TREE)
15082 return;
15084 /* If we're a nested function, initially use a parent of NULL; if we're
15085 a plain function, this will be fixed up in decls_for_scope. If
15086 we're a method, it will be ignored, since we already have a DIE. */
15087 if (decl_function_context (decl)
15088 /* But if we're in terse mode, we don't care about scope. */
15089 && debug_info_level > DINFO_LEVEL_TERSE)
15090 context_die = NULL;
15091 break;
15093 case VAR_DECL:
15094 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15095 declaration and if the declaration was never even referenced from
15096 within this entire compilation unit. We suppress these DIEs in
15097 order to save space in the .debug section (by eliminating entries
15098 which are probably useless). Note that we must not suppress
15099 block-local extern declarations (whether used or not) because that
15100 would screw-up the debugger's name lookup mechanism and cause it to
15101 miss things which really ought to be in scope at a given point. */
15102 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15103 return;
15105 /* For local statics lookup proper context die. */
15106 if (TREE_STATIC (decl) && decl_function_context (decl))
15107 context_die = lookup_decl_die (DECL_CONTEXT (decl));
15109 /* If we are in terse mode, don't generate any DIEs to represent any
15110 variable declarations or definitions. */
15111 if (debug_info_level <= DINFO_LEVEL_TERSE)
15112 return;
15113 break;
15115 case NAMESPACE_DECL:
15116 if (debug_info_level <= DINFO_LEVEL_TERSE)
15117 return;
15118 if (lookup_decl_die (decl) != NULL)
15119 return;
15120 break;
15122 case TYPE_DECL:
15123 /* Don't emit stubs for types unless they are needed by other DIEs. */
15124 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15125 return;
15127 /* Don't bother trying to generate any DIEs to represent any of the
15128 normal built-in types for the language we are compiling. */
15129 if (DECL_IS_BUILTIN (decl))
15131 /* OK, we need to generate one for `bool' so GDB knows what type
15132 comparisons have. */
15133 if (is_cxx ()
15134 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15135 && ! DECL_IGNORED_P (decl))
15136 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15138 return;
15141 /* If we are in terse mode, don't generate any DIEs for types. */
15142 if (debug_info_level <= DINFO_LEVEL_TERSE)
15143 return;
15145 /* If we're a function-scope tag, initially use a parent of NULL;
15146 this will be fixed up in decls_for_scope. */
15147 if (decl_function_context (decl))
15148 context_die = NULL;
15150 break;
15152 default:
15153 return;
15156 gen_decl_die (decl, context_die);
15159 /* Output a marker (i.e. a label) for the beginning of the generated code for
15160 a lexical block. */
15162 static void
15163 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15164 unsigned int blocknum)
15166 switch_to_section (current_function_section ());
15167 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
15170 /* Output a marker (i.e. a label) for the end of the generated code for a
15171 lexical block. */
15173 static void
15174 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
15176 switch_to_section (current_function_section ());
15177 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
15180 /* Returns nonzero if it is appropriate not to emit any debugging
15181 information for BLOCK, because it doesn't contain any instructions.
15183 Don't allow this for blocks with nested functions or local classes
15184 as we would end up with orphans, and in the presence of scheduling
15185 we may end up calling them anyway. */
15187 static bool
15188 dwarf2out_ignore_block (const_tree block)
15190 tree decl;
15192 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
15193 if (TREE_CODE (decl) == FUNCTION_DECL
15194 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15195 return 0;
15197 return 1;
15200 /* Hash table routines for file_hash. */
15202 static int
15203 file_table_eq (const void *p1_p, const void *p2_p)
15205 const struct dwarf_file_data *const p1 =
15206 (const struct dwarf_file_data *) p1_p;
15207 const char *const p2 = (const char *) p2_p;
15208 return strcmp (p1->filename, p2) == 0;
15211 static hashval_t
15212 file_table_hash (const void *p_p)
15214 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
15215 return htab_hash_string (p->filename);
15218 /* Lookup FILE_NAME (in the list of filenames that we know about here in
15219 dwarf2out.c) and return its "index". The index of each (known) filename is
15220 just a unique number which is associated with only that one filename. We
15221 need such numbers for the sake of generating labels (in the .debug_sfnames
15222 section) and references to those files numbers (in the .debug_srcinfo
15223 and.debug_macinfo sections). If the filename given as an argument is not
15224 found in our current list, add it to the list and assign it the next
15225 available unique index number. In order to speed up searches, we remember
15226 the index of the filename was looked up last. This handles the majority of
15227 all searches. */
15229 static struct dwarf_file_data *
15230 lookup_filename (const char *file_name)
15232 void ** slot;
15233 struct dwarf_file_data * created;
15235 /* Check to see if the file name that was searched on the previous
15236 call matches this file name. If so, return the index. */
15237 if (file_table_last_lookup
15238 && (file_name == file_table_last_lookup->filename
15239 || strcmp (file_table_last_lookup->filename, file_name) == 0))
15240 return file_table_last_lookup;
15242 /* Didn't match the previous lookup, search the table. */
15243 slot = htab_find_slot_with_hash (file_table, file_name,
15244 htab_hash_string (file_name), INSERT);
15245 if (*slot)
15246 return (struct dwarf_file_data *) *slot;
15248 created = GGC_NEW (struct dwarf_file_data);
15249 created->filename = file_name;
15250 created->emitted_number = 0;
15251 *slot = created;
15252 return created;
15255 /* If the assembler will construct the file table, then translate the compiler
15256 internal file table number into the assembler file table number, and emit
15257 a .file directive if we haven't already emitted one yet. The file table
15258 numbers are different because we prune debug info for unused variables and
15259 types, which may include filenames. */
15261 static int
15262 maybe_emit_file (struct dwarf_file_data * fd)
15264 if (! fd->emitted_number)
15266 if (last_emitted_file)
15267 fd->emitted_number = last_emitted_file->emitted_number + 1;
15268 else
15269 fd->emitted_number = 1;
15270 last_emitted_file = fd;
15272 if (DWARF2_ASM_LINE_DEBUG_INFO)
15274 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
15275 output_quoted_string (asm_out_file,
15276 remap_debug_filename (fd->filename));
15277 fputc ('\n', asm_out_file);
15281 return fd->emitted_number;
15284 /* Called by the final INSN scan whenever we see a var location. We
15285 use it to drop labels in the right places, and throw the location in
15286 our lookup table. */
15288 static void
15289 dwarf2out_var_location (rtx loc_note)
15291 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
15292 struct var_loc_node *newloc;
15293 rtx prev_insn;
15294 static rtx last_insn;
15295 static const char *last_label;
15296 tree decl;
15298 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
15299 return;
15300 prev_insn = PREV_INSN (loc_note);
15302 newloc = GGC_CNEW (struct var_loc_node);
15303 /* If the insn we processed last time is the previous insn
15304 and it is also a var location note, use the label we emitted
15305 last time. */
15306 if (last_insn != NULL_RTX
15307 && last_insn == prev_insn
15308 && NOTE_P (prev_insn)
15309 && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
15311 newloc->label = last_label;
15313 else
15315 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
15316 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
15317 loclabel_num++;
15318 newloc->label = ggc_strdup (loclabel);
15320 newloc->var_loc_note = loc_note;
15321 newloc->next = NULL;
15323 if (cfun && in_cold_section_p)
15324 newloc->section_label = crtl->subsections.cold_section_label;
15325 else
15326 newloc->section_label = text_section_label;
15328 last_insn = loc_note;
15329 last_label = newloc->label;
15330 decl = NOTE_VAR_LOCATION_DECL (loc_note);
15331 add_var_loc_to_decl (decl, newloc);
15334 /* We need to reset the locations at the beginning of each
15335 function. We can't do this in the end_function hook, because the
15336 declarations that use the locations won't have been output when
15337 that hook is called. Also compute have_multiple_function_sections here. */
15339 static void
15340 dwarf2out_begin_function (tree fun)
15342 htab_empty (decl_loc_table);
15344 if (function_section (fun) != text_section)
15345 have_multiple_function_sections = true;
15347 dwarf2out_note_section_used ();
15350 /* Output a label to mark the beginning of a source code line entry
15351 and record information relating to this source line, in
15352 'line_info_table' for later output of the .debug_line section. */
15354 static void
15355 dwarf2out_source_line (unsigned int line, const char *filename)
15357 if (debug_info_level >= DINFO_LEVEL_NORMAL
15358 && line != 0)
15360 int file_num = maybe_emit_file (lookup_filename (filename));
15362 switch_to_section (current_function_section ());
15364 /* If requested, emit something human-readable. */
15365 if (flag_debug_asm)
15366 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
15367 filename, line);
15369 if (DWARF2_ASM_LINE_DEBUG_INFO)
15371 /* Emit the .loc directive understood by GNU as. */
15372 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
15374 /* Indicate that line number info exists. */
15375 line_info_table_in_use++;
15377 else if (function_section (current_function_decl) != text_section)
15379 dw_separate_line_info_ref line_info;
15380 targetm.asm_out.internal_label (asm_out_file,
15381 SEPARATE_LINE_CODE_LABEL,
15382 separate_line_info_table_in_use);
15384 /* Expand the line info table if necessary. */
15385 if (separate_line_info_table_in_use
15386 == separate_line_info_table_allocated)
15388 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15389 separate_line_info_table
15390 = GGC_RESIZEVEC (dw_separate_line_info_entry,
15391 separate_line_info_table,
15392 separate_line_info_table_allocated);
15393 memset (separate_line_info_table
15394 + separate_line_info_table_in_use,
15396 (LINE_INFO_TABLE_INCREMENT
15397 * sizeof (dw_separate_line_info_entry)));
15400 /* Add the new entry at the end of the line_info_table. */
15401 line_info
15402 = &separate_line_info_table[separate_line_info_table_in_use++];
15403 line_info->dw_file_num = file_num;
15404 line_info->dw_line_num = line;
15405 line_info->function = current_function_funcdef_no;
15407 else
15409 dw_line_info_ref line_info;
15411 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
15412 line_info_table_in_use);
15414 /* Expand the line info table if necessary. */
15415 if (line_info_table_in_use == line_info_table_allocated)
15417 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15418 line_info_table
15419 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
15420 line_info_table_allocated);
15421 memset (line_info_table + line_info_table_in_use, 0,
15422 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
15425 /* Add the new entry at the end of the line_info_table. */
15426 line_info = &line_info_table[line_info_table_in_use++];
15427 line_info->dw_file_num = file_num;
15428 line_info->dw_line_num = line;
15433 /* Record the beginning of a new source file. */
15435 static void
15436 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
15438 if (flag_eliminate_dwarf2_dups)
15440 /* Record the beginning of the file for break_out_includes. */
15441 dw_die_ref bincl_die;
15443 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
15444 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
15447 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15449 int file_num = maybe_emit_file (lookup_filename (filename));
15451 switch_to_section (debug_macinfo_section);
15452 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
15453 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
15454 lineno);
15456 dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
15460 /* Record the end of a source file. */
15462 static void
15463 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
15465 if (flag_eliminate_dwarf2_dups)
15466 /* Record the end of the file for break_out_includes. */
15467 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
15469 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15471 switch_to_section (debug_macinfo_section);
15472 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
15476 /* Called from debug_define in toplev.c. The `buffer' parameter contains
15477 the tail part of the directive line, i.e. the part which is past the
15478 initial whitespace, #, whitespace, directive-name, whitespace part. */
15480 static void
15481 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
15482 const char *buffer ATTRIBUTE_UNUSED)
15484 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15486 switch_to_section (debug_macinfo_section);
15487 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
15488 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
15489 dw2_asm_output_nstring (buffer, -1, "The macro");
15493 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
15494 the tail part of the directive line, i.e. the part which is past the
15495 initial whitespace, #, whitespace, directive-name, whitespace part. */
15497 static void
15498 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
15499 const char *buffer ATTRIBUTE_UNUSED)
15501 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15503 switch_to_section (debug_macinfo_section);
15504 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
15505 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
15506 dw2_asm_output_nstring (buffer, -1, "The macro");
15510 /* Set up for Dwarf output at the start of compilation. */
15512 static void
15513 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
15515 /* Allocate the file_table. */
15516 file_table = htab_create_ggc (50, file_table_hash,
15517 file_table_eq, NULL);
15519 /* Allocate the decl_die_table. */
15520 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
15521 decl_die_table_eq, NULL);
15523 /* Allocate the decl_loc_table. */
15524 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
15525 decl_loc_table_eq, NULL);
15527 /* Allocate the initial hunk of the decl_scope_table. */
15528 decl_scope_table = VEC_alloc (tree, gc, 256);
15530 /* Allocate the initial hunk of the abbrev_die_table. */
15531 abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
15532 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
15533 /* Zero-th entry is allocated, but unused. */
15534 abbrev_die_table_in_use = 1;
15536 /* Allocate the initial hunk of the line_info_table. */
15537 line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
15538 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
15540 /* Zero-th entry is allocated, but unused. */
15541 line_info_table_in_use = 1;
15543 /* Allocate the pubtypes and pubnames vectors. */
15544 pubname_table = VEC_alloc (pubname_entry, gc, 32);
15545 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
15547 /* Generate the initial DIE for the .debug section. Note that the (string)
15548 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
15549 will (typically) be a relative pathname and that this pathname should be
15550 taken as being relative to the directory from which the compiler was
15551 invoked when the given (base) source file was compiled. We will fill
15552 in this value in dwarf2out_finish. */
15553 comp_unit_die = gen_compile_unit_die (NULL);
15555 incomplete_types = VEC_alloc (tree, gc, 64);
15557 used_rtx_array = VEC_alloc (rtx, gc, 32);
15559 debug_info_section = get_section (DEBUG_INFO_SECTION,
15560 SECTION_DEBUG, NULL);
15561 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
15562 SECTION_DEBUG, NULL);
15563 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
15564 SECTION_DEBUG, NULL);
15565 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
15566 SECTION_DEBUG, NULL);
15567 debug_line_section = get_section (DEBUG_LINE_SECTION,
15568 SECTION_DEBUG, NULL);
15569 debug_loc_section = get_section (DEBUG_LOC_SECTION,
15570 SECTION_DEBUG, NULL);
15571 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
15572 SECTION_DEBUG, NULL);
15573 #ifdef DEBUG_PUBTYPES_SECTION
15574 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
15575 SECTION_DEBUG, NULL);
15576 #endif
15577 debug_str_section = get_section (DEBUG_STR_SECTION,
15578 DEBUG_STR_SECTION_FLAGS, NULL);
15579 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
15580 SECTION_DEBUG, NULL);
15581 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
15582 SECTION_DEBUG, NULL);
15584 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
15585 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
15586 DEBUG_ABBREV_SECTION_LABEL, 0);
15587 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
15588 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
15589 COLD_TEXT_SECTION_LABEL, 0);
15590 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
15592 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
15593 DEBUG_INFO_SECTION_LABEL, 0);
15594 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
15595 DEBUG_LINE_SECTION_LABEL, 0);
15596 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
15597 DEBUG_RANGES_SECTION_LABEL, 0);
15598 switch_to_section (debug_abbrev_section);
15599 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
15600 switch_to_section (debug_info_section);
15601 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
15602 switch_to_section (debug_line_section);
15603 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
15605 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15607 switch_to_section (debug_macinfo_section);
15608 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
15609 DEBUG_MACINFO_SECTION_LABEL, 0);
15610 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
15613 switch_to_section (text_section);
15614 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
15615 if (flag_reorder_blocks_and_partition)
15617 cold_text_section = unlikely_text_section ();
15618 switch_to_section (cold_text_section);
15619 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
15623 /* A helper function for dwarf2out_finish called through
15624 ht_forall. Emit one queued .debug_str string. */
15626 static int
15627 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
15629 struct indirect_string_node *node = (struct indirect_string_node *) *h;
15631 if (node->form == DW_FORM_strp)
15633 switch_to_section (debug_str_section);
15634 ASM_OUTPUT_LABEL (asm_out_file, node->label);
15635 assemble_string (node->str, strlen (node->str) + 1);
15638 return 1;
15641 #if ENABLE_ASSERT_CHECKING
15642 /* Verify that all marks are clear. */
15644 static void
15645 verify_marks_clear (dw_die_ref die)
15647 dw_die_ref c;
15649 gcc_assert (! die->die_mark);
15650 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
15652 #endif /* ENABLE_ASSERT_CHECKING */
15654 /* Clear the marks for a die and its children.
15655 Be cool if the mark isn't set. */
15657 static void
15658 prune_unmark_dies (dw_die_ref die)
15660 dw_die_ref c;
15662 if (die->die_mark)
15663 die->die_mark = 0;
15664 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
15667 /* Given DIE that we're marking as used, find any other dies
15668 it references as attributes and mark them as used. */
15670 static void
15671 prune_unused_types_walk_attribs (dw_die_ref die)
15673 dw_attr_ref a;
15674 unsigned ix;
15676 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
15678 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
15680 /* A reference to another DIE.
15681 Make sure that it will get emitted. */
15682 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
15684 /* Set the string's refcount to 0 so that prune_unused_types_mark
15685 accounts properly for it. */
15686 if (AT_class (a) == dw_val_class_str)
15687 a->dw_attr_val.v.val_str->refcount = 0;
15692 /* Mark DIE as being used. If DOKIDS is true, then walk down
15693 to DIE's children. */
15695 static void
15696 prune_unused_types_mark (dw_die_ref die, int dokids)
15698 dw_die_ref c;
15700 if (die->die_mark == 0)
15702 /* We haven't done this node yet. Mark it as used. */
15703 die->die_mark = 1;
15705 /* We also have to mark its parents as used.
15706 (But we don't want to mark our parents' kids due to this.) */
15707 if (die->die_parent)
15708 prune_unused_types_mark (die->die_parent, 0);
15710 /* Mark any referenced nodes. */
15711 prune_unused_types_walk_attribs (die);
15713 /* If this node is a specification,
15714 also mark the definition, if it exists. */
15715 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
15716 prune_unused_types_mark (die->die_definition, 1);
15719 if (dokids && die->die_mark != 2)
15721 /* We need to walk the children, but haven't done so yet.
15722 Remember that we've walked the kids. */
15723 die->die_mark = 2;
15725 /* If this is an array type, we need to make sure our
15726 kids get marked, even if they're types. */
15727 if (die->die_tag == DW_TAG_array_type)
15728 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
15729 else
15730 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15735 /* Walk the tree DIE and mark types that we actually use. */
15737 static void
15738 prune_unused_types_walk (dw_die_ref die)
15740 dw_die_ref c;
15742 /* Don't do anything if this node is already marked. */
15743 if (die->die_mark)
15744 return;
15746 switch (die->die_tag)
15748 case DW_TAG_const_type:
15749 case DW_TAG_packed_type:
15750 case DW_TAG_pointer_type:
15751 case DW_TAG_reference_type:
15752 case DW_TAG_volatile_type:
15753 case DW_TAG_typedef:
15754 case DW_TAG_array_type:
15755 case DW_TAG_structure_type:
15756 case DW_TAG_union_type:
15757 case DW_TAG_class_type:
15758 case DW_TAG_interface_type:
15759 case DW_TAG_friend:
15760 case DW_TAG_variant_part:
15761 case DW_TAG_enumeration_type:
15762 case DW_TAG_subroutine_type:
15763 case DW_TAG_string_type:
15764 case DW_TAG_set_type:
15765 case DW_TAG_subrange_type:
15766 case DW_TAG_ptr_to_member_type:
15767 case DW_TAG_file_type:
15768 if (die->die_perennial_p)
15769 break;
15771 /* It's a type node --- don't mark it. */
15772 return;
15774 default:
15775 /* Mark everything else. */
15776 break;
15779 die->die_mark = 1;
15781 /* Now, mark any dies referenced from here. */
15782 prune_unused_types_walk_attribs (die);
15784 /* Mark children. */
15785 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15788 /* Increment the string counts on strings referred to from DIE's
15789 attributes. */
15791 static void
15792 prune_unused_types_update_strings (dw_die_ref die)
15794 dw_attr_ref a;
15795 unsigned ix;
15797 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
15798 if (AT_class (a) == dw_val_class_str)
15800 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
15801 s->refcount++;
15802 /* Avoid unnecessarily putting strings that are used less than
15803 twice in the hash table. */
15804 if (s->refcount
15805 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
15807 void ** slot;
15808 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
15809 htab_hash_string (s->str),
15810 INSERT);
15811 gcc_assert (*slot == NULL);
15812 *slot = s;
15817 /* Remove from the tree DIE any dies that aren't marked. */
15819 static void
15820 prune_unused_types_prune (dw_die_ref die)
15822 dw_die_ref c;
15824 gcc_assert (die->die_mark);
15825 prune_unused_types_update_strings (die);
15827 if (! die->die_child)
15828 return;
15830 c = die->die_child;
15831 do {
15832 dw_die_ref prev = c;
15833 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
15834 if (c == die->die_child)
15836 /* No marked children between 'prev' and the end of the list. */
15837 if (prev == c)
15838 /* No marked children at all. */
15839 die->die_child = NULL;
15840 else
15842 prev->die_sib = c->die_sib;
15843 die->die_child = prev;
15845 return;
15848 if (c != prev->die_sib)
15849 prev->die_sib = c;
15850 prune_unused_types_prune (c);
15851 } while (c != die->die_child);
15855 /* Remove dies representing declarations that we never use. */
15857 static void
15858 prune_unused_types (void)
15860 unsigned int i;
15861 limbo_die_node *node;
15862 pubname_ref pub;
15864 #if ENABLE_ASSERT_CHECKING
15865 /* All the marks should already be clear. */
15866 verify_marks_clear (comp_unit_die);
15867 for (node = limbo_die_list; node; node = node->next)
15868 verify_marks_clear (node->die);
15869 #endif /* ENABLE_ASSERT_CHECKING */
15871 /* Set the mark on nodes that are actually used. */
15872 prune_unused_types_walk (comp_unit_die);
15873 for (node = limbo_die_list; node; node = node->next)
15874 prune_unused_types_walk (node->die);
15876 /* Also set the mark on nodes referenced from the
15877 pubname_table or arange_table. */
15878 for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
15879 prune_unused_types_mark (pub->die, 1);
15880 for (i = 0; i < arange_table_in_use; i++)
15881 prune_unused_types_mark (arange_table[i], 1);
15883 /* Get rid of nodes that aren't marked; and update the string counts. */
15884 if (debug_str_hash)
15885 htab_empty (debug_str_hash);
15886 prune_unused_types_prune (comp_unit_die);
15887 for (node = limbo_die_list; node; node = node->next)
15888 prune_unused_types_prune (node->die);
15890 /* Leave the marks clear. */
15891 prune_unmark_dies (comp_unit_die);
15892 for (node = limbo_die_list; node; node = node->next)
15893 prune_unmark_dies (node->die);
15896 /* Set the parameter to true if there are any relative pathnames in
15897 the file table. */
15898 static int
15899 file_table_relative_p (void ** slot, void *param)
15901 bool *p = (bool *) param;
15902 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
15903 if (!IS_ABSOLUTE_PATH (d->filename))
15905 *p = true;
15906 return 0;
15908 return 1;
15911 /* Output stuff that dwarf requires at the end of every file,
15912 and generate the DWARF-2 debugging info. */
15914 static void
15915 dwarf2out_finish (const char *filename)
15917 limbo_die_node *node, *next_node;
15918 dw_die_ref die = 0;
15920 /* Add the name for the main input file now. We delayed this from
15921 dwarf2out_init to avoid complications with PCH. */
15922 add_name_attribute (comp_unit_die, remap_debug_filename (filename));
15923 if (!IS_ABSOLUTE_PATH (filename))
15924 add_comp_dir_attribute (comp_unit_die);
15925 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
15927 bool p = false;
15928 htab_traverse (file_table, file_table_relative_p, &p);
15929 if (p)
15930 add_comp_dir_attribute (comp_unit_die);
15933 /* Traverse the limbo die list, and add parent/child links. The only
15934 dies without parents that should be here are concrete instances of
15935 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
15936 For concrete instances, we can get the parent die from the abstract
15937 instance. */
15938 for (node = limbo_die_list; node; node = next_node)
15940 next_node = node->next;
15941 die = node->die;
15943 if (die->die_parent == NULL)
15945 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
15947 if (origin)
15948 add_child_die (origin->die_parent, die);
15949 else if (die == comp_unit_die)
15951 else if (errorcount > 0 || sorrycount > 0)
15952 /* It's OK to be confused by errors in the input. */
15953 add_child_die (comp_unit_die, die);
15954 else
15956 /* In certain situations, the lexical block containing a
15957 nested function can be optimized away, which results
15958 in the nested function die being orphaned. Likewise
15959 with the return type of that nested function. Force
15960 this to be a child of the containing function.
15962 It may happen that even the containing function got fully
15963 inlined and optimized out. In that case we are lost and
15964 assign the empty child. This should not be big issue as
15965 the function is likely unreachable too. */
15966 tree context = NULL_TREE;
15968 gcc_assert (node->created_for);
15970 if (DECL_P (node->created_for))
15971 context = DECL_CONTEXT (node->created_for);
15972 else if (TYPE_P (node->created_for))
15973 context = TYPE_CONTEXT (node->created_for);
15975 gcc_assert (context
15976 && (TREE_CODE (context) == FUNCTION_DECL
15977 || TREE_CODE (context) == NAMESPACE_DECL));
15979 origin = lookup_decl_die (context);
15980 if (origin)
15981 add_child_die (origin, die);
15982 else
15983 add_child_die (comp_unit_die, die);
15988 limbo_die_list = NULL;
15990 /* Walk through the list of incomplete types again, trying once more to
15991 emit full debugging info for them. */
15992 retry_incomplete_types ();
15994 if (flag_eliminate_unused_debug_types)
15995 prune_unused_types ();
15997 /* Generate separate CUs for each of the include files we've seen.
15998 They will go into limbo_die_list. */
15999 if (flag_eliminate_dwarf2_dups)
16000 break_out_includes (comp_unit_die);
16002 /* Traverse the DIE's and add add sibling attributes to those DIE's
16003 that have children. */
16004 add_sibling_attributes (comp_unit_die);
16005 for (node = limbo_die_list; node; node = node->next)
16006 add_sibling_attributes (node->die);
16008 /* Output a terminator label for the .text section. */
16009 switch_to_section (text_section);
16010 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16011 if (flag_reorder_blocks_and_partition)
16013 switch_to_section (unlikely_text_section ());
16014 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16017 /* We can only use the low/high_pc attributes if all of the code was
16018 in .text. */
16019 if (!have_multiple_function_sections)
16021 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
16022 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
16025 else
16027 unsigned fde_idx = 0;
16029 /* We need to give .debug_loc and .debug_ranges an appropriate
16030 "base address". Use zero so that these addresses become
16031 absolute. Historically, we've emitted the unexpected
16032 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
16033 Emit both to give time for other tools to adapt. */
16034 add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
16035 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
16037 add_AT_range_list (comp_unit_die, DW_AT_ranges,
16038 add_ranges_by_labels (text_section_label,
16039 text_end_label));
16040 if (flag_reorder_blocks_and_partition)
16041 add_ranges_by_labels (cold_text_section_label,
16042 cold_end_label);
16044 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
16046 dw_fde_ref fde = &fde_table[fde_idx];
16048 if (fde->dw_fde_switched_sections)
16050 add_ranges_by_labels (fde->dw_fde_hot_section_label,
16051 fde->dw_fde_hot_section_end_label);
16052 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
16053 fde->dw_fde_unlikely_section_end_label);
16055 else
16056 add_ranges_by_labels (fde->dw_fde_begin,
16057 fde->dw_fde_end);
16060 add_ranges (NULL);
16063 /* Output location list section if necessary. */
16064 if (have_location_lists)
16066 /* Output the location lists info. */
16067 switch_to_section (debug_loc_section);
16068 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
16069 DEBUG_LOC_SECTION_LABEL, 0);
16070 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
16071 output_location_lists (die);
16074 if (debug_info_level >= DINFO_LEVEL_NORMAL)
16075 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
16076 debug_line_section_label);
16078 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16079 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
16081 /* Output all of the compilation units. We put the main one last so that
16082 the offsets are available to output_pubnames. */
16083 for (node = limbo_die_list; node; node = node->next)
16084 output_comp_unit (node->die, 0);
16086 output_comp_unit (comp_unit_die, 0);
16088 /* Output the abbreviation table. */
16089 switch_to_section (debug_abbrev_section);
16090 output_abbrev_section ();
16092 /* Output public names table if necessary. */
16093 if (!VEC_empty (pubname_entry, pubname_table))
16095 switch_to_section (debug_pubnames_section);
16096 output_pubnames (pubname_table);
16099 #ifdef DEBUG_PUBTYPES_SECTION
16100 /* Output public types table if necessary. */
16101 if (!VEC_empty (pubname_entry, pubtype_table))
16103 switch_to_section (debug_pubtypes_section);
16104 output_pubnames (pubtype_table);
16106 #endif
16108 /* Output the address range information. We only put functions in the arange
16109 table, so don't write it out if we don't have any. */
16110 if (fde_table_in_use)
16112 switch_to_section (debug_aranges_section);
16113 output_aranges ();
16116 /* Output ranges section if necessary. */
16117 if (ranges_table_in_use)
16119 switch_to_section (debug_ranges_section);
16120 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
16121 output_ranges ();
16124 /* Output the source line correspondence table. We must do this
16125 even if there is no line information. Otherwise, on an empty
16126 translation unit, we will generate a present, but empty,
16127 .debug_info section. IRIX 6.5 `nm' will then complain when
16128 examining the file. This is done late so that any filenames
16129 used by the debug_info section are marked as 'used'. */
16130 if (! DWARF2_ASM_LINE_DEBUG_INFO)
16132 switch_to_section (debug_line_section);
16133 output_line_info ();
16136 /* Have to end the macro section. */
16137 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16139 switch_to_section (debug_macinfo_section);
16140 dw2_asm_output_data (1, 0, "End compilation unit");
16143 /* If we emitted any DW_FORM_strp form attribute, output the string
16144 table too. */
16145 if (debug_str_hash)
16146 htab_traverse (debug_str_hash, output_indirect_string, NULL);
16148 #else
16150 /* This should never be used, but its address is needed for comparisons. */
16151 const struct gcc_debug_hooks dwarf2_debug_hooks;
16153 #endif /* DWARF2_DEBUGGING_INFO */
16155 #include "gt-dwarf2out.h"