mips.h (set_volatile): Delete.
[official-gcc.git] / gcc / dwarf2out.c
blob8a398e4d465d3324596dc88e8eb80dfff6627c21
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 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 /* The size of the target's pointer type. */
134 #ifndef PTR_SIZE
135 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
136 #endif
138 /* Array of RTXes referenced by the debugging information, which therefore
139 must be kept around forever. */
140 static GTY(()) VEC(rtx,gc) *used_rtx_array;
142 /* A pointer to the base of a list of incomplete types which might be
143 completed at some later time. incomplete_types_list needs to be a
144 VEC(tree,gc) because we want to tell the garbage collector about
145 it. */
146 static GTY(()) VEC(tree,gc) *incomplete_types;
148 /* A pointer to the base of a table of references to declaration
149 scopes. This table is a display which tracks the nesting
150 of declaration scopes at the current scope and containing
151 scopes. This table is used to find the proper place to
152 define type declaration DIE's. */
153 static GTY(()) VEC(tree,gc) *decl_scope_table;
155 /* Pointers to various DWARF2 sections. */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_aranges_section;
159 static GTY(()) section *debug_macinfo_section;
160 static GTY(()) section *debug_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_ranges_section;
166 static GTY(()) section *debug_frame_section;
168 /* How to start an assembler comment. */
169 #ifndef ASM_COMMENT_START
170 #define ASM_COMMENT_START ";#"
171 #endif
173 typedef struct dw_cfi_struct *dw_cfi_ref;
174 typedef struct dw_fde_struct *dw_fde_ref;
175 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
177 /* Call frames are described using a sequence of Call Frame
178 Information instructions. The register number, offset
179 and address fields are provided as possible operands;
180 their use is selected by the opcode field. */
182 enum dw_cfi_oprnd_type {
183 dw_cfi_oprnd_unused,
184 dw_cfi_oprnd_reg_num,
185 dw_cfi_oprnd_offset,
186 dw_cfi_oprnd_addr,
187 dw_cfi_oprnd_loc
190 typedef union dw_cfi_oprnd_struct GTY(())
192 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
193 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
194 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
195 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
197 dw_cfi_oprnd;
199 typedef struct dw_cfi_struct GTY(())
201 dw_cfi_ref dw_cfi_next;
202 enum dwarf_call_frame_info dw_cfi_opc;
203 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
204 dw_cfi_oprnd1;
205 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
206 dw_cfi_oprnd2;
208 dw_cfi_node;
210 /* This is how we define the location of the CFA. We use to handle it
211 as REG + OFFSET all the time, but now it can be more complex.
212 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
213 Instead of passing around REG and OFFSET, we pass a copy
214 of this structure. */
215 typedef struct cfa_loc GTY(())
217 HOST_WIDE_INT offset;
218 HOST_WIDE_INT base_offset;
219 unsigned int reg;
220 int indirect; /* 1 if CFA is accessed via a dereference. */
221 } dw_cfa_location;
223 /* All call frame descriptions (FDE's) in the GCC generated DWARF
224 refer to a single Common Information Entry (CIE), defined at
225 the beginning of the .debug_frame section. This use of a single
226 CIE obviates the need to keep track of multiple CIE's
227 in the DWARF generation routines below. */
229 typedef struct dw_fde_struct GTY(())
231 tree decl;
232 const char *dw_fde_begin;
233 const char *dw_fde_current_label;
234 const char *dw_fde_end;
235 const char *dw_fde_hot_section_label;
236 const char *dw_fde_hot_section_end_label;
237 const char *dw_fde_unlikely_section_label;
238 const char *dw_fde_unlikely_section_end_label;
239 bool dw_fde_switched_sections;
240 dw_cfi_ref dw_fde_cfi;
241 unsigned funcdef_number;
242 unsigned all_throwers_are_sibcalls : 1;
243 unsigned nothrow : 1;
244 unsigned uses_eh_lsda : 1;
246 dw_fde_node;
248 /* Maximum size (in bytes) of an artificially generated label. */
249 #define MAX_ARTIFICIAL_LABEL_BYTES 30
251 /* The size of addresses as they appear in the Dwarf 2 data.
252 Some architectures use word addresses to refer to code locations,
253 but Dwarf 2 info always uses byte addresses. On such machines,
254 Dwarf 2 addresses need to be larger than the architecture's
255 pointers. */
256 #ifndef DWARF2_ADDR_SIZE
257 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
258 #endif
260 /* The size in bytes of a DWARF field indicating an offset or length
261 relative to a debug info section, specified to be 4 bytes in the
262 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
263 as PTR_SIZE. */
265 #ifndef DWARF_OFFSET_SIZE
266 #define DWARF_OFFSET_SIZE 4
267 #endif
269 /* According to the (draft) DWARF 3 specification, the initial length
270 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
271 bytes are 0xffffffff, followed by the length stored in the next 8
272 bytes.
274 However, the SGI/MIPS ABI uses an initial length which is equal to
275 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
277 #ifndef DWARF_INITIAL_LENGTH_SIZE
278 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
279 #endif
281 #define DWARF_VERSION 2
283 /* Round SIZE up to the nearest BOUNDARY. */
284 #define DWARF_ROUND(SIZE,BOUNDARY) \
285 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
287 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
288 #ifndef DWARF_CIE_DATA_ALIGNMENT
289 #ifdef STACK_GROWS_DOWNWARD
290 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
291 #else
292 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
293 #endif
294 #endif
296 /* CIE identifier. */
297 #if HOST_BITS_PER_WIDE_INT >= 64
298 #define DWARF_CIE_ID \
299 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
300 #else
301 #define DWARF_CIE_ID DW_CIE_ID
302 #endif
304 /* A pointer to the base of a table that contains frame description
305 information for each routine. */
306 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
308 /* Number of elements currently allocated for fde_table. */
309 static GTY(()) unsigned fde_table_allocated;
311 /* Number of elements in fde_table currently in use. */
312 static GTY(()) unsigned fde_table_in_use;
314 /* Size (in elements) of increments by which we may expand the
315 fde_table. */
316 #define FDE_TABLE_INCREMENT 256
318 /* A list of call frame insns for the CIE. */
319 static GTY(()) dw_cfi_ref cie_cfi_head;
321 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
322 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
323 attribute that accelerates the lookup of the FDE associated
324 with the subprogram. This variable holds the table index of the FDE
325 associated with the current function (body) definition. */
326 static unsigned current_funcdef_fde;
327 #endif
329 struct indirect_string_node GTY(())
331 const char *str;
332 unsigned int refcount;
333 unsigned int form;
334 char *label;
337 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
339 static GTY(()) int dw2_string_counter;
340 static GTY(()) unsigned long dwarf2out_cfi_label_num;
342 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
344 /* Forward declarations for functions defined in this file. */
346 static char *stripattributes (const char *);
347 static const char *dwarf_cfi_name (unsigned);
348 static dw_cfi_ref new_cfi (void);
349 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
350 static void add_fde_cfi (const char *, dw_cfi_ref);
351 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
352 static void lookup_cfa (dw_cfa_location *);
353 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
354 #ifdef DWARF2_UNWIND_INFO
355 static void initial_return_save (rtx);
356 #endif
357 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
358 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
359 static void output_call_frame_info (int);
360 static void dwarf2out_stack_adjust (rtx, bool);
361 static void flush_queued_reg_saves (void);
362 static bool clobbers_queued_reg_save (const_rtx);
363 static void dwarf2out_frame_debug_expr (rtx, const char *);
365 /* Support for complex CFA locations. */
366 static void output_cfa_loc (dw_cfi_ref);
367 static void get_cfa_from_loc_descr (dw_cfa_location *,
368 struct dw_loc_descr_struct *);
369 static struct dw_loc_descr_struct *build_cfa_loc
370 (dw_cfa_location *, HOST_WIDE_INT);
371 static void def_cfa_1 (const char *, dw_cfa_location *);
373 /* How to start an assembler comment. */
374 #ifndef ASM_COMMENT_START
375 #define ASM_COMMENT_START ";#"
376 #endif
378 /* Data and reference forms for relocatable data. */
379 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
380 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
382 #ifndef DEBUG_FRAME_SECTION
383 #define DEBUG_FRAME_SECTION ".debug_frame"
384 #endif
386 #ifndef FUNC_BEGIN_LABEL
387 #define FUNC_BEGIN_LABEL "LFB"
388 #endif
390 #ifndef FUNC_END_LABEL
391 #define FUNC_END_LABEL "LFE"
392 #endif
394 #ifndef FRAME_BEGIN_LABEL
395 #define FRAME_BEGIN_LABEL "Lframe"
396 #endif
397 #define CIE_AFTER_SIZE_LABEL "LSCIE"
398 #define CIE_END_LABEL "LECIE"
399 #define FDE_LABEL "LSFDE"
400 #define FDE_AFTER_SIZE_LABEL "LASFDE"
401 #define FDE_END_LABEL "LEFDE"
402 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
403 #define LINE_NUMBER_END_LABEL "LELT"
404 #define LN_PROLOG_AS_LABEL "LASLTP"
405 #define LN_PROLOG_END_LABEL "LELTP"
406 #define DIE_LABEL_PREFIX "DW"
408 /* The DWARF 2 CFA column which tracks the return address. Normally this
409 is the column for PC, or the first column after all of the hard
410 registers. */
411 #ifndef DWARF_FRAME_RETURN_COLUMN
412 #ifdef PC_REGNUM
413 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
414 #else
415 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
416 #endif
417 #endif
419 /* The mapping from gcc register number to DWARF 2 CFA column number. By
420 default, we just provide columns for all registers. */
421 #ifndef DWARF_FRAME_REGNUM
422 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
423 #endif
425 /* Hook used by __throw. */
428 expand_builtin_dwarf_sp_column (void)
430 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
431 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
434 /* Return a pointer to a copy of the section string name S with all
435 attributes stripped off, and an asterisk prepended (for assemble_name). */
437 static inline char *
438 stripattributes (const char *s)
440 char *stripped = XNEWVEC (char, strlen (s) + 2);
441 char *p = stripped;
443 *p++ = '*';
445 while (*s && *s != ',')
446 *p++ = *s++;
448 *p = '\0';
449 return stripped;
452 /* MEM is a memory reference for the register size table, each element of
453 which has mode MODE. Initialize column C as a return address column. */
455 static void
456 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
458 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
459 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
460 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
463 /* Generate code to initialize the register size table. */
465 void
466 expand_builtin_init_dwarf_reg_sizes (tree address)
468 unsigned int i;
469 enum machine_mode mode = TYPE_MODE (char_type_node);
470 rtx addr = expand_normal (address);
471 rtx mem = gen_rtx_MEM (BLKmode, addr);
472 bool wrote_return_column = false;
474 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
476 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
478 if (rnum < DWARF_FRAME_REGISTERS)
480 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
481 enum machine_mode save_mode = reg_raw_mode[i];
482 HOST_WIDE_INT size;
484 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
485 save_mode = choose_hard_reg_mode (i, 1, true);
486 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
488 if (save_mode == VOIDmode)
489 continue;
490 wrote_return_column = true;
492 size = GET_MODE_SIZE (save_mode);
493 if (offset < 0)
494 continue;
496 emit_move_insn (adjust_address (mem, mode, offset),
497 gen_int_mode (size, mode));
501 if (!wrote_return_column)
502 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
504 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
505 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
506 #endif
508 targetm.init_dwarf_reg_sizes_extra (address);
511 /* Convert a DWARF call frame info. operation to its string name */
513 static const char *
514 dwarf_cfi_name (unsigned int cfi_opc)
516 switch (cfi_opc)
518 case DW_CFA_advance_loc:
519 return "DW_CFA_advance_loc";
520 case DW_CFA_offset:
521 return "DW_CFA_offset";
522 case DW_CFA_restore:
523 return "DW_CFA_restore";
524 case DW_CFA_nop:
525 return "DW_CFA_nop";
526 case DW_CFA_set_loc:
527 return "DW_CFA_set_loc";
528 case DW_CFA_advance_loc1:
529 return "DW_CFA_advance_loc1";
530 case DW_CFA_advance_loc2:
531 return "DW_CFA_advance_loc2";
532 case DW_CFA_advance_loc4:
533 return "DW_CFA_advance_loc4";
534 case DW_CFA_offset_extended:
535 return "DW_CFA_offset_extended";
536 case DW_CFA_restore_extended:
537 return "DW_CFA_restore_extended";
538 case DW_CFA_undefined:
539 return "DW_CFA_undefined";
540 case DW_CFA_same_value:
541 return "DW_CFA_same_value";
542 case DW_CFA_register:
543 return "DW_CFA_register";
544 case DW_CFA_remember_state:
545 return "DW_CFA_remember_state";
546 case DW_CFA_restore_state:
547 return "DW_CFA_restore_state";
548 case DW_CFA_def_cfa:
549 return "DW_CFA_def_cfa";
550 case DW_CFA_def_cfa_register:
551 return "DW_CFA_def_cfa_register";
552 case DW_CFA_def_cfa_offset:
553 return "DW_CFA_def_cfa_offset";
555 /* DWARF 3 */
556 case DW_CFA_def_cfa_expression:
557 return "DW_CFA_def_cfa_expression";
558 case DW_CFA_expression:
559 return "DW_CFA_expression";
560 case DW_CFA_offset_extended_sf:
561 return "DW_CFA_offset_extended_sf";
562 case DW_CFA_def_cfa_sf:
563 return "DW_CFA_def_cfa_sf";
564 case DW_CFA_def_cfa_offset_sf:
565 return "DW_CFA_def_cfa_offset_sf";
567 /* SGI/MIPS specific */
568 case DW_CFA_MIPS_advance_loc8:
569 return "DW_CFA_MIPS_advance_loc8";
571 /* GNU extensions */
572 case DW_CFA_GNU_window_save:
573 return "DW_CFA_GNU_window_save";
574 case DW_CFA_GNU_args_size:
575 return "DW_CFA_GNU_args_size";
576 case DW_CFA_GNU_negative_offset_extended:
577 return "DW_CFA_GNU_negative_offset_extended";
579 default:
580 return "DW_CFA_<unknown>";
584 /* Return a pointer to a newly allocated Call Frame Instruction. */
586 static inline dw_cfi_ref
587 new_cfi (void)
589 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
591 cfi->dw_cfi_next = NULL;
592 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
593 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
595 return cfi;
598 /* Add a Call Frame Instruction to list of instructions. */
600 static inline void
601 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
603 dw_cfi_ref *p;
605 /* Find the end of the chain. */
606 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
609 *p = cfi;
612 /* Generate a new label for the CFI info to refer to. */
614 char *
615 dwarf2out_cfi_label (void)
617 static char label[20];
619 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
620 ASM_OUTPUT_LABEL (asm_out_file, label);
621 return label;
624 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
625 or to the CIE if LABEL is NULL. */
627 static void
628 add_fde_cfi (const char *label, dw_cfi_ref cfi)
630 if (label)
632 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
634 if (*label == 0)
635 label = dwarf2out_cfi_label ();
637 if (fde->dw_fde_current_label == NULL
638 || strcmp (label, fde->dw_fde_current_label) != 0)
640 dw_cfi_ref xcfi;
642 label = xstrdup (label);
644 /* Set the location counter to the new label. */
645 xcfi = new_cfi ();
646 /* If we have a current label, advance from there, otherwise
647 set the location directly using set_loc. */
648 xcfi->dw_cfi_opc = fde->dw_fde_current_label
649 ? DW_CFA_advance_loc4
650 : DW_CFA_set_loc;
651 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
652 add_cfi (&fde->dw_fde_cfi, xcfi);
654 fde->dw_fde_current_label = label;
657 add_cfi (&fde->dw_fde_cfi, cfi);
660 else
661 add_cfi (&cie_cfi_head, cfi);
664 /* Subroutine of lookup_cfa. */
666 static void
667 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
669 switch (cfi->dw_cfi_opc)
671 case DW_CFA_def_cfa_offset:
672 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
673 break;
674 case DW_CFA_def_cfa_offset_sf:
675 loc->offset
676 = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
677 break;
678 case DW_CFA_def_cfa_register:
679 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
680 break;
681 case DW_CFA_def_cfa:
682 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
683 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
684 break;
685 case DW_CFA_def_cfa_sf:
686 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
687 loc->offset
688 = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
689 break;
690 case DW_CFA_def_cfa_expression:
691 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
692 break;
693 default:
694 break;
698 /* Find the previous value for the CFA. */
700 static void
701 lookup_cfa (dw_cfa_location *loc)
703 dw_cfi_ref cfi;
705 loc->reg = INVALID_REGNUM;
706 loc->offset = 0;
707 loc->indirect = 0;
708 loc->base_offset = 0;
710 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
711 lookup_cfa_1 (cfi, loc);
713 if (fde_table_in_use)
715 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
716 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
717 lookup_cfa_1 (cfi, loc);
721 /* The current rule for calculating the DWARF2 canonical frame address. */
722 static dw_cfa_location cfa;
724 /* The register used for saving registers to the stack, and its offset
725 from the CFA. */
726 static dw_cfa_location cfa_store;
728 /* The running total of the size of arguments pushed onto the stack. */
729 static HOST_WIDE_INT args_size;
731 /* The last args_size we actually output. */
732 static HOST_WIDE_INT old_args_size;
734 /* Entry point to update the canonical frame address (CFA).
735 LABEL is passed to add_fde_cfi. The value of CFA is now to be
736 calculated from REG+OFFSET. */
738 void
739 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
741 dw_cfa_location loc;
742 loc.indirect = 0;
743 loc.base_offset = 0;
744 loc.reg = reg;
745 loc.offset = offset;
746 def_cfa_1 (label, &loc);
749 /* Determine if two dw_cfa_location structures define the same data. */
751 static bool
752 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
754 return (loc1->reg == loc2->reg
755 && loc1->offset == loc2->offset
756 && loc1->indirect == loc2->indirect
757 && (loc1->indirect == 0
758 || loc1->base_offset == loc2->base_offset));
761 /* This routine does the actual work. The CFA is now calculated from
762 the dw_cfa_location structure. */
764 static void
765 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
767 dw_cfi_ref cfi;
768 dw_cfa_location old_cfa, loc;
770 cfa = *loc_p;
771 loc = *loc_p;
773 if (cfa_store.reg == loc.reg && loc.indirect == 0)
774 cfa_store.offset = loc.offset;
776 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
777 lookup_cfa (&old_cfa);
779 /* If nothing changed, no need to issue any call frame instructions. */
780 if (cfa_equal_p (&loc, &old_cfa))
781 return;
783 cfi = new_cfi ();
785 if (loc.reg == old_cfa.reg && !loc.indirect)
787 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
788 the CFA register did not change but the offset did. */
789 if (loc.offset < 0)
791 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
792 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
794 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
795 cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
797 else
799 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
800 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
804 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
805 else if (loc.offset == old_cfa.offset
806 && old_cfa.reg != INVALID_REGNUM
807 && !loc.indirect)
809 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
810 indicating the CFA register has changed to <register> but the
811 offset has not changed. */
812 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
813 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
815 #endif
817 else if (loc.indirect == 0)
819 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
820 indicating the CFA register has changed to <register> with
821 the specified offset. */
822 if (loc.offset < 0)
824 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
825 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
827 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
828 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
829 cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
831 else
833 cfi->dw_cfi_opc = DW_CFA_def_cfa;
834 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
835 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
838 else
840 /* Construct a DW_CFA_def_cfa_expression instruction to
841 calculate the CFA using a full location expression since no
842 register-offset pair is available. */
843 struct dw_loc_descr_struct *loc_list;
845 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
846 loc_list = build_cfa_loc (&loc, 0);
847 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
850 add_fde_cfi (label, cfi);
853 /* Add the CFI for saving a register. REG is the CFA column number.
854 LABEL is passed to add_fde_cfi.
855 If SREG is -1, the register is saved at OFFSET from the CFA;
856 otherwise it is saved in SREG. */
858 static void
859 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
861 dw_cfi_ref cfi = new_cfi ();
863 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
865 if (sreg == INVALID_REGNUM)
867 if (reg & ~0x3f)
868 /* The register number won't fit in 6 bits, so we have to use
869 the long form. */
870 cfi->dw_cfi_opc = DW_CFA_offset_extended;
871 else
872 cfi->dw_cfi_opc = DW_CFA_offset;
874 #ifdef ENABLE_CHECKING
876 /* If we get an offset that is not a multiple of
877 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
878 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
879 description. */
880 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
882 gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
884 #endif
885 offset /= DWARF_CIE_DATA_ALIGNMENT;
886 if (offset < 0)
887 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
889 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
891 else if (sreg == reg)
892 cfi->dw_cfi_opc = DW_CFA_same_value;
893 else
895 cfi->dw_cfi_opc = DW_CFA_register;
896 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
899 add_fde_cfi (label, cfi);
902 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
903 This CFI tells the unwinder that it needs to restore the window registers
904 from the previous frame's window save area.
906 ??? Perhaps we should note in the CIE where windows are saved (instead of
907 assuming 0(cfa)) and what registers are in the window. */
909 void
910 dwarf2out_window_save (const char *label)
912 dw_cfi_ref cfi = new_cfi ();
914 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
915 add_fde_cfi (label, cfi);
918 /* Add a CFI to update the running total of the size of arguments
919 pushed onto the stack. */
921 void
922 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
924 dw_cfi_ref cfi;
926 if (size == old_args_size)
927 return;
929 old_args_size = size;
931 cfi = new_cfi ();
932 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
933 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
934 add_fde_cfi (label, cfi);
937 /* Entry point for saving a register to the stack. REG is the GCC register
938 number. LABEL and OFFSET are passed to reg_save. */
940 void
941 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
943 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
946 /* Entry point for saving the return address in the stack.
947 LABEL and OFFSET are passed to reg_save. */
949 void
950 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
952 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
955 /* Entry point for saving the return address in a register.
956 LABEL and SREG are passed to reg_save. */
958 void
959 dwarf2out_return_reg (const char *label, unsigned int sreg)
961 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
964 #ifdef DWARF2_UNWIND_INFO
965 /* Record the initial position of the return address. RTL is
966 INCOMING_RETURN_ADDR_RTX. */
968 static void
969 initial_return_save (rtx rtl)
971 unsigned int reg = INVALID_REGNUM;
972 HOST_WIDE_INT offset = 0;
974 switch (GET_CODE (rtl))
976 case REG:
977 /* RA is in a register. */
978 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
979 break;
981 case MEM:
982 /* RA is on the stack. */
983 rtl = XEXP (rtl, 0);
984 switch (GET_CODE (rtl))
986 case REG:
987 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
988 offset = 0;
989 break;
991 case PLUS:
992 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
993 offset = INTVAL (XEXP (rtl, 1));
994 break;
996 case MINUS:
997 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
998 offset = -INTVAL (XEXP (rtl, 1));
999 break;
1001 default:
1002 gcc_unreachable ();
1005 break;
1007 case PLUS:
1008 /* The return address is at some offset from any value we can
1009 actually load. For instance, on the SPARC it is in %i7+8. Just
1010 ignore the offset for now; it doesn't matter for unwinding frames. */
1011 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1012 initial_return_save (XEXP (rtl, 0));
1013 return;
1015 default:
1016 gcc_unreachable ();
1019 if (reg != DWARF_FRAME_RETURN_COLUMN)
1020 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1022 #endif
1024 /* Given a SET, calculate the amount of stack adjustment it
1025 contains. */
1027 static HOST_WIDE_INT
1028 stack_adjust_offset (const_rtx pattern)
1030 const_rtx src = SET_SRC (pattern);
1031 const_rtx dest = SET_DEST (pattern);
1032 HOST_WIDE_INT offset = 0;
1033 enum rtx_code code;
1035 if (dest == stack_pointer_rtx)
1037 /* (set (reg sp) (plus (reg sp) (const_int))) */
1038 code = GET_CODE (src);
1039 if (! (code == PLUS || code == MINUS)
1040 || XEXP (src, 0) != stack_pointer_rtx
1041 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1042 return 0;
1044 offset = INTVAL (XEXP (src, 1));
1045 if (code == PLUS)
1046 offset = -offset;
1048 else if (MEM_P (dest))
1050 /* (set (mem (pre_dec (reg sp))) (foo)) */
1051 src = XEXP (dest, 0);
1052 code = GET_CODE (src);
1054 switch (code)
1056 case PRE_MODIFY:
1057 case POST_MODIFY:
1058 if (XEXP (src, 0) == stack_pointer_rtx)
1060 rtx val = XEXP (XEXP (src, 1), 1);
1061 /* We handle only adjustments by constant amount. */
1062 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1063 && GET_CODE (val) == CONST_INT);
1064 offset = -INTVAL (val);
1065 break;
1067 return 0;
1069 case PRE_DEC:
1070 case POST_DEC:
1071 if (XEXP (src, 0) == stack_pointer_rtx)
1073 offset = GET_MODE_SIZE (GET_MODE (dest));
1074 break;
1076 return 0;
1078 case PRE_INC:
1079 case POST_INC:
1080 if (XEXP (src, 0) == stack_pointer_rtx)
1082 offset = -GET_MODE_SIZE (GET_MODE (dest));
1083 break;
1085 return 0;
1087 default:
1088 return 0;
1091 else
1092 return 0;
1094 return offset;
1097 /* Check INSN to see if it looks like a push or a stack adjustment, and
1098 make a note of it if it does. EH uses this information to find out how
1099 much extra space it needs to pop off the stack. */
1101 static void
1102 dwarf2out_stack_adjust (rtx insn, bool after_p)
1104 HOST_WIDE_INT offset;
1105 const char *label;
1106 int i;
1108 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1109 with this function. Proper support would require all frame-related
1110 insns to be marked, and to be able to handle saving state around
1111 epilogues textually in the middle of the function. */
1112 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1113 return;
1115 /* If only calls can throw, and we have a frame pointer,
1116 save up adjustments until we see the CALL_INSN. */
1117 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1119 if (CALL_P (insn) && !after_p)
1121 /* Extract the size of the args from the CALL rtx itself. */
1122 insn = PATTERN (insn);
1123 if (GET_CODE (insn) == PARALLEL)
1124 insn = XVECEXP (insn, 0, 0);
1125 if (GET_CODE (insn) == SET)
1126 insn = SET_SRC (insn);
1127 gcc_assert (GET_CODE (insn) == CALL);
1128 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1130 return;
1133 if (CALL_P (insn) && !after_p)
1135 if (!flag_asynchronous_unwind_tables)
1136 dwarf2out_args_size ("", args_size);
1137 return;
1139 else if (BARRIER_P (insn))
1141 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1142 the compiler will have already emitted a stack adjustment, but
1143 doesn't bother for calls to noreturn functions. */
1144 #ifdef STACK_GROWS_DOWNWARD
1145 offset = -args_size;
1146 #else
1147 offset = args_size;
1148 #endif
1150 else if (GET_CODE (PATTERN (insn)) == SET)
1151 offset = stack_adjust_offset (PATTERN (insn));
1152 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1153 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1155 /* There may be stack adjustments inside compound insns. Search
1156 for them. */
1157 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1158 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1159 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1161 else
1162 return;
1164 if (offset == 0)
1165 return;
1167 if (cfa.reg == STACK_POINTER_REGNUM)
1168 cfa.offset += offset;
1170 #ifndef STACK_GROWS_DOWNWARD
1171 offset = -offset;
1172 #endif
1174 args_size += offset;
1175 if (args_size < 0)
1176 args_size = 0;
1178 label = dwarf2out_cfi_label ();
1179 def_cfa_1 (label, &cfa);
1180 if (flag_asynchronous_unwind_tables)
1181 dwarf2out_args_size (label, args_size);
1184 #endif
1186 /* We delay emitting a register save until either (a) we reach the end
1187 of the prologue or (b) the register is clobbered. This clusters
1188 register saves so that there are fewer pc advances. */
1190 struct queued_reg_save GTY(())
1192 struct queued_reg_save *next;
1193 rtx reg;
1194 HOST_WIDE_INT cfa_offset;
1195 rtx saved_reg;
1198 static GTY(()) struct queued_reg_save *queued_reg_saves;
1200 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1201 struct reg_saved_in_data GTY(()) {
1202 rtx orig_reg;
1203 rtx saved_in_reg;
1206 /* A list of registers saved in other registers.
1207 The list intentionally has a small maximum capacity of 4; if your
1208 port needs more than that, you might consider implementing a
1209 more efficient data structure. */
1210 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1211 static GTY(()) size_t num_regs_saved_in_regs;
1213 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1214 static const char *last_reg_save_label;
1216 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1217 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1219 static void
1220 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1222 struct queued_reg_save *q;
1224 /* Duplicates waste space, but it's also necessary to remove them
1225 for correctness, since the queue gets output in reverse
1226 order. */
1227 for (q = queued_reg_saves; q != NULL; q = q->next)
1228 if (REGNO (q->reg) == REGNO (reg))
1229 break;
1231 if (q == NULL)
1233 q = ggc_alloc (sizeof (*q));
1234 q->next = queued_reg_saves;
1235 queued_reg_saves = q;
1238 q->reg = reg;
1239 q->cfa_offset = offset;
1240 q->saved_reg = sreg;
1242 last_reg_save_label = label;
1245 /* Output all the entries in QUEUED_REG_SAVES. */
1247 static void
1248 flush_queued_reg_saves (void)
1250 struct queued_reg_save *q;
1252 for (q = queued_reg_saves; q; q = q->next)
1254 size_t i;
1255 unsigned int reg, sreg;
1257 for (i = 0; i < num_regs_saved_in_regs; i++)
1258 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1259 break;
1260 if (q->saved_reg && i == num_regs_saved_in_regs)
1262 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1263 num_regs_saved_in_regs++;
1265 if (i != num_regs_saved_in_regs)
1267 regs_saved_in_regs[i].orig_reg = q->reg;
1268 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1271 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1272 if (q->saved_reg)
1273 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1274 else
1275 sreg = INVALID_REGNUM;
1276 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1279 queued_reg_saves = NULL;
1280 last_reg_save_label = NULL;
1283 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1284 location for? Or, does it clobber a register which we've previously
1285 said that some other register is saved in, and for which we now
1286 have a new location for? */
1288 static bool
1289 clobbers_queued_reg_save (const_rtx insn)
1291 struct queued_reg_save *q;
1293 for (q = queued_reg_saves; q; q = q->next)
1295 size_t i;
1296 if (modified_in_p (q->reg, insn))
1297 return true;
1298 for (i = 0; i < num_regs_saved_in_regs; i++)
1299 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1300 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1301 return true;
1304 return false;
1307 /* Entry point for saving the first register into the second. */
1309 void
1310 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1312 size_t i;
1313 unsigned int regno, sregno;
1315 for (i = 0; i < num_regs_saved_in_regs; i++)
1316 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1317 break;
1318 if (i == num_regs_saved_in_regs)
1320 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1321 num_regs_saved_in_regs++;
1323 regs_saved_in_regs[i].orig_reg = reg;
1324 regs_saved_in_regs[i].saved_in_reg = sreg;
1326 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1327 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1328 reg_save (label, regno, sregno, 0);
1331 /* What register, if any, is currently saved in REG? */
1333 static rtx
1334 reg_saved_in (rtx reg)
1336 unsigned int regn = REGNO (reg);
1337 size_t i;
1338 struct queued_reg_save *q;
1340 for (q = queued_reg_saves; q; q = q->next)
1341 if (q->saved_reg && regn == REGNO (q->saved_reg))
1342 return q->reg;
1344 for (i = 0; i < num_regs_saved_in_regs; i++)
1345 if (regs_saved_in_regs[i].saved_in_reg
1346 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1347 return regs_saved_in_regs[i].orig_reg;
1349 return NULL_RTX;
1353 /* A temporary register holding an integral value used in adjusting SP
1354 or setting up the store_reg. The "offset" field holds the integer
1355 value, not an offset. */
1356 static dw_cfa_location cfa_temp;
1358 /* Record call frame debugging information for an expression EXPR,
1359 which either sets SP or FP (adjusting how we calculate the frame
1360 address) or saves a register to the stack or another register.
1361 LABEL indicates the address of EXPR.
1363 This function encodes a state machine mapping rtxes to actions on
1364 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1365 users need not read the source code.
1367 The High-Level Picture
1369 Changes in the register we use to calculate the CFA: Currently we
1370 assume that if you copy the CFA register into another register, we
1371 should take the other one as the new CFA register; this seems to
1372 work pretty well. If it's wrong for some target, it's simple
1373 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1375 Changes in the register we use for saving registers to the stack:
1376 This is usually SP, but not always. Again, we deduce that if you
1377 copy SP into another register (and SP is not the CFA register),
1378 then the new register is the one we will be using for register
1379 saves. This also seems to work.
1381 Register saves: There's not much guesswork about this one; if
1382 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1383 register save, and the register used to calculate the destination
1384 had better be the one we think we're using for this purpose.
1385 It's also assumed that a copy from a call-saved register to another
1386 register is saving that register if RTX_FRAME_RELATED_P is set on
1387 that instruction. If the copy is from a call-saved register to
1388 the *same* register, that means that the register is now the same
1389 value as in the caller.
1391 Except: If the register being saved is the CFA register, and the
1392 offset is nonzero, we are saving the CFA, so we assume we have to
1393 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1394 the intent is to save the value of SP from the previous frame.
1396 In addition, if a register has previously been saved to a different
1397 register,
1399 Invariants / Summaries of Rules
1401 cfa current rule for calculating the CFA. It usually
1402 consists of a register and an offset.
1403 cfa_store register used by prologue code to save things to the stack
1404 cfa_store.offset is the offset from the value of
1405 cfa_store.reg to the actual CFA
1406 cfa_temp register holding an integral value. cfa_temp.offset
1407 stores the value, which will be used to adjust the
1408 stack pointer. cfa_temp is also used like cfa_store,
1409 to track stores to the stack via fp or a temp reg.
1411 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1412 with cfa.reg as the first operand changes the cfa.reg and its
1413 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1414 cfa_temp.offset.
1416 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1417 expression yielding a constant. This sets cfa_temp.reg
1418 and cfa_temp.offset.
1420 Rule 5: Create a new register cfa_store used to save items to the
1421 stack.
1423 Rules 10-14: Save a register to the stack. Define offset as the
1424 difference of the original location and cfa_store's
1425 location (or cfa_temp's location if cfa_temp is used).
1427 The Rules
1429 "{a,b}" indicates a choice of a xor b.
1430 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1432 Rule 1:
1433 (set <reg1> <reg2>:cfa.reg)
1434 effects: cfa.reg = <reg1>
1435 cfa.offset unchanged
1436 cfa_temp.reg = <reg1>
1437 cfa_temp.offset = cfa.offset
1439 Rule 2:
1440 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1441 {<const_int>,<reg>:cfa_temp.reg}))
1442 effects: cfa.reg = sp if fp used
1443 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1444 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1445 if cfa_store.reg==sp
1447 Rule 3:
1448 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1449 effects: cfa.reg = fp
1450 cfa_offset += +/- <const_int>
1452 Rule 4:
1453 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1454 constraints: <reg1> != fp
1455 <reg1> != sp
1456 effects: cfa.reg = <reg1>
1457 cfa_temp.reg = <reg1>
1458 cfa_temp.offset = cfa.offset
1460 Rule 5:
1461 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1462 constraints: <reg1> != fp
1463 <reg1> != sp
1464 effects: cfa_store.reg = <reg1>
1465 cfa_store.offset = cfa.offset - cfa_temp.offset
1467 Rule 6:
1468 (set <reg> <const_int>)
1469 effects: cfa_temp.reg = <reg>
1470 cfa_temp.offset = <const_int>
1472 Rule 7:
1473 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1474 effects: cfa_temp.reg = <reg1>
1475 cfa_temp.offset |= <const_int>
1477 Rule 8:
1478 (set <reg> (high <exp>))
1479 effects: none
1481 Rule 9:
1482 (set <reg> (lo_sum <exp> <const_int>))
1483 effects: cfa_temp.reg = <reg>
1484 cfa_temp.offset = <const_int>
1486 Rule 10:
1487 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1488 effects: cfa_store.offset -= <const_int>
1489 cfa.offset = cfa_store.offset if cfa.reg == sp
1490 cfa.reg = sp
1491 cfa.base_offset = -cfa_store.offset
1493 Rule 11:
1494 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1495 effects: cfa_store.offset += -/+ mode_size(mem)
1496 cfa.offset = cfa_store.offset if cfa.reg == sp
1497 cfa.reg = sp
1498 cfa.base_offset = -cfa_store.offset
1500 Rule 12:
1501 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1503 <reg2>)
1504 effects: cfa.reg = <reg1>
1505 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1507 Rule 13:
1508 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1509 effects: cfa.reg = <reg1>
1510 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1512 Rule 14:
1513 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1514 effects: cfa.reg = <reg1>
1515 cfa.base_offset = -cfa_temp.offset
1516 cfa_temp.offset -= mode_size(mem)
1518 Rule 15:
1519 (set <reg> {unspec, unspec_volatile})
1520 effects: target-dependent */
1522 static void
1523 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1525 rtx src, dest;
1526 HOST_WIDE_INT offset;
1528 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1529 the PARALLEL independently. The first element is always processed if
1530 it is a SET. This is for backward compatibility. Other elements
1531 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1532 flag is set in them. */
1533 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1535 int par_index;
1536 int limit = XVECLEN (expr, 0);
1537 rtx elem;
1539 /* PARALLELs have strict read-modify-write semantics, so we
1540 ought to evaluate every rvalue before changing any lvalue.
1541 It's cumbersome to do that in general, but there's an
1542 easy approximation that is enough for all current users:
1543 handle register saves before register assignments. */
1544 if (GET_CODE (expr) == PARALLEL)
1545 for (par_index = 0; par_index < limit; par_index++)
1547 elem = XVECEXP (expr, 0, par_index);
1548 if (GET_CODE (elem) == SET
1549 && MEM_P (SET_DEST (elem))
1550 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1551 dwarf2out_frame_debug_expr (elem, label);
1554 for (par_index = 0; par_index < limit; par_index++)
1556 elem = XVECEXP (expr, 0, par_index);
1557 if (GET_CODE (elem) == SET
1558 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1559 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1560 dwarf2out_frame_debug_expr (elem, label);
1562 return;
1565 gcc_assert (GET_CODE (expr) == SET);
1567 src = SET_SRC (expr);
1568 dest = SET_DEST (expr);
1570 if (REG_P (src))
1572 rtx rsi = reg_saved_in (src);
1573 if (rsi)
1574 src = rsi;
1577 switch (GET_CODE (dest))
1579 case REG:
1580 switch (GET_CODE (src))
1582 /* Setting FP from SP. */
1583 case REG:
1584 if (cfa.reg == (unsigned) REGNO (src))
1586 /* Rule 1 */
1587 /* Update the CFA rule wrt SP or FP. Make sure src is
1588 relative to the current CFA register.
1590 We used to require that dest be either SP or FP, but the
1591 ARM copies SP to a temporary register, and from there to
1592 FP. So we just rely on the backends to only set
1593 RTX_FRAME_RELATED_P on appropriate insns. */
1594 cfa.reg = REGNO (dest);
1595 cfa_temp.reg = cfa.reg;
1596 cfa_temp.offset = cfa.offset;
1598 else
1600 /* Saving a register in a register. */
1601 gcc_assert (!fixed_regs [REGNO (dest)]
1602 /* For the SPARC and its register window. */
1603 || (DWARF_FRAME_REGNUM (REGNO (src))
1604 == DWARF_FRAME_RETURN_COLUMN));
1605 queue_reg_save (label, src, dest, 0);
1607 break;
1609 case PLUS:
1610 case MINUS:
1611 case LO_SUM:
1612 if (dest == stack_pointer_rtx)
1614 /* Rule 2 */
1615 /* Adjusting SP. */
1616 switch (GET_CODE (XEXP (src, 1)))
1618 case CONST_INT:
1619 offset = INTVAL (XEXP (src, 1));
1620 break;
1621 case REG:
1622 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1623 == cfa_temp.reg);
1624 offset = cfa_temp.offset;
1625 break;
1626 default:
1627 gcc_unreachable ();
1630 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1632 /* Restoring SP from FP in the epilogue. */
1633 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1634 cfa.reg = STACK_POINTER_REGNUM;
1636 else if (GET_CODE (src) == LO_SUM)
1637 /* Assume we've set the source reg of the LO_SUM from sp. */
1639 else
1640 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1642 if (GET_CODE (src) != MINUS)
1643 offset = -offset;
1644 if (cfa.reg == STACK_POINTER_REGNUM)
1645 cfa.offset += offset;
1646 if (cfa_store.reg == STACK_POINTER_REGNUM)
1647 cfa_store.offset += offset;
1649 else if (dest == hard_frame_pointer_rtx)
1651 /* Rule 3 */
1652 /* Either setting the FP from an offset of the SP,
1653 or adjusting the FP */
1654 gcc_assert (frame_pointer_needed);
1656 gcc_assert (REG_P (XEXP (src, 0))
1657 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1658 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1659 offset = INTVAL (XEXP (src, 1));
1660 if (GET_CODE (src) != MINUS)
1661 offset = -offset;
1662 cfa.offset += offset;
1663 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1665 else
1667 gcc_assert (GET_CODE (src) != MINUS);
1669 /* Rule 4 */
1670 if (REG_P (XEXP (src, 0))
1671 && REGNO (XEXP (src, 0)) == cfa.reg
1672 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1674 /* Setting a temporary CFA register that will be copied
1675 into the FP later on. */
1676 offset = - INTVAL (XEXP (src, 1));
1677 cfa.offset += offset;
1678 cfa.reg = REGNO (dest);
1679 /* Or used to save regs to the stack. */
1680 cfa_temp.reg = cfa.reg;
1681 cfa_temp.offset = cfa.offset;
1684 /* Rule 5 */
1685 else if (REG_P (XEXP (src, 0))
1686 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1687 && XEXP (src, 1) == stack_pointer_rtx)
1689 /* Setting a scratch register that we will use instead
1690 of SP for saving registers to the stack. */
1691 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1692 cfa_store.reg = REGNO (dest);
1693 cfa_store.offset = cfa.offset - cfa_temp.offset;
1696 /* Rule 9 */
1697 else if (GET_CODE (src) == LO_SUM
1698 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1700 cfa_temp.reg = REGNO (dest);
1701 cfa_temp.offset = INTVAL (XEXP (src, 1));
1703 else
1704 gcc_unreachable ();
1706 break;
1708 /* Rule 6 */
1709 case CONST_INT:
1710 cfa_temp.reg = REGNO (dest);
1711 cfa_temp.offset = INTVAL (src);
1712 break;
1714 /* Rule 7 */
1715 case IOR:
1716 gcc_assert (REG_P (XEXP (src, 0))
1717 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1718 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1720 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1721 cfa_temp.reg = REGNO (dest);
1722 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1723 break;
1725 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1726 which will fill in all of the bits. */
1727 /* Rule 8 */
1728 case HIGH:
1729 break;
1731 /* Rule 15 */
1732 case UNSPEC:
1733 case UNSPEC_VOLATILE:
1734 gcc_assert (targetm.dwarf_handle_frame_unspec);
1735 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1736 return;
1738 default:
1739 gcc_unreachable ();
1742 def_cfa_1 (label, &cfa);
1743 break;
1745 case MEM:
1746 gcc_assert (REG_P (src));
1748 /* Saving a register to the stack. Make sure dest is relative to the
1749 CFA register. */
1750 switch (GET_CODE (XEXP (dest, 0)))
1752 /* Rule 10 */
1753 /* With a push. */
1754 case PRE_MODIFY:
1755 /* We can't handle variable size modifications. */
1756 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1757 == CONST_INT);
1758 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1760 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1761 && cfa_store.reg == STACK_POINTER_REGNUM);
1763 cfa_store.offset += offset;
1764 if (cfa.reg == STACK_POINTER_REGNUM)
1765 cfa.offset = cfa_store.offset;
1767 offset = -cfa_store.offset;
1768 break;
1770 /* Rule 11 */
1771 case PRE_INC:
1772 case PRE_DEC:
1773 offset = GET_MODE_SIZE (GET_MODE (dest));
1774 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1775 offset = -offset;
1777 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1778 && cfa_store.reg == STACK_POINTER_REGNUM);
1780 cfa_store.offset += offset;
1781 if (cfa.reg == STACK_POINTER_REGNUM)
1782 cfa.offset = cfa_store.offset;
1784 offset = -cfa_store.offset;
1785 break;
1787 /* Rule 12 */
1788 /* With an offset. */
1789 case PLUS:
1790 case MINUS:
1791 case LO_SUM:
1793 int regno;
1795 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1796 && REG_P (XEXP (XEXP (dest, 0), 0)));
1797 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1798 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1799 offset = -offset;
1801 regno = REGNO (XEXP (XEXP (dest, 0), 0));
1803 if (cfa_store.reg == (unsigned) regno)
1804 offset -= cfa_store.offset;
1805 else
1807 gcc_assert (cfa_temp.reg == (unsigned) regno);
1808 offset -= cfa_temp.offset;
1811 break;
1813 /* Rule 13 */
1814 /* Without an offset. */
1815 case REG:
1817 int regno = REGNO (XEXP (dest, 0));
1819 if (cfa_store.reg == (unsigned) regno)
1820 offset = -cfa_store.offset;
1821 else
1823 gcc_assert (cfa_temp.reg == (unsigned) regno);
1824 offset = -cfa_temp.offset;
1827 break;
1829 /* Rule 14 */
1830 case POST_INC:
1831 gcc_assert (cfa_temp.reg
1832 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1833 offset = -cfa_temp.offset;
1834 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1835 break;
1837 default:
1838 gcc_unreachable ();
1841 if (REGNO (src) != STACK_POINTER_REGNUM
1842 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1843 && (unsigned) REGNO (src) == cfa.reg)
1845 /* We're storing the current CFA reg into the stack. */
1847 if (cfa.offset == 0)
1849 /* If the source register is exactly the CFA, assume
1850 we're saving SP like any other register; this happens
1851 on the ARM. */
1852 def_cfa_1 (label, &cfa);
1853 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1854 break;
1856 else
1858 /* Otherwise, we'll need to look in the stack to
1859 calculate the CFA. */
1860 rtx x = XEXP (dest, 0);
1862 if (!REG_P (x))
1863 x = XEXP (x, 0);
1864 gcc_assert (REG_P (x));
1866 cfa.reg = REGNO (x);
1867 cfa.base_offset = offset;
1868 cfa.indirect = 1;
1869 def_cfa_1 (label, &cfa);
1870 break;
1874 def_cfa_1 (label, &cfa);
1875 queue_reg_save (label, src, NULL_RTX, offset);
1876 break;
1878 default:
1879 gcc_unreachable ();
1883 /* Record call frame debugging information for INSN, which either
1884 sets SP or FP (adjusting how we calculate the frame address) or saves a
1885 register to the stack. If INSN is NULL_RTX, initialize our state.
1887 If AFTER_P is false, we're being called before the insn is emitted,
1888 otherwise after. Call instructions get invoked twice. */
1890 void
1891 dwarf2out_frame_debug (rtx insn, bool after_p)
1893 const char *label;
1894 rtx src;
1896 if (insn == NULL_RTX)
1898 size_t i;
1900 /* Flush any queued register saves. */
1901 flush_queued_reg_saves ();
1903 /* Set up state for generating call frame debug info. */
1904 lookup_cfa (&cfa);
1905 gcc_assert (cfa.reg
1906 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1908 cfa.reg = STACK_POINTER_REGNUM;
1909 cfa_store = cfa;
1910 cfa_temp.reg = -1;
1911 cfa_temp.offset = 0;
1913 for (i = 0; i < num_regs_saved_in_regs; i++)
1915 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1916 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1918 num_regs_saved_in_regs = 0;
1919 return;
1922 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1923 flush_queued_reg_saves ();
1925 if (! RTX_FRAME_RELATED_P (insn))
1927 if (!ACCUMULATE_OUTGOING_ARGS)
1928 dwarf2out_stack_adjust (insn, after_p);
1929 return;
1932 label = dwarf2out_cfi_label ();
1933 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1934 if (src)
1935 insn = XEXP (src, 0);
1936 else
1937 insn = PATTERN (insn);
1939 dwarf2out_frame_debug_expr (insn, label);
1942 #endif
1944 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
1945 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1946 (enum dwarf_call_frame_info cfi);
1948 static enum dw_cfi_oprnd_type
1949 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1951 switch (cfi)
1953 case DW_CFA_nop:
1954 case DW_CFA_GNU_window_save:
1955 return dw_cfi_oprnd_unused;
1957 case DW_CFA_set_loc:
1958 case DW_CFA_advance_loc1:
1959 case DW_CFA_advance_loc2:
1960 case DW_CFA_advance_loc4:
1961 case DW_CFA_MIPS_advance_loc8:
1962 return dw_cfi_oprnd_addr;
1964 case DW_CFA_offset:
1965 case DW_CFA_offset_extended:
1966 case DW_CFA_def_cfa:
1967 case DW_CFA_offset_extended_sf:
1968 case DW_CFA_def_cfa_sf:
1969 case DW_CFA_restore_extended:
1970 case DW_CFA_undefined:
1971 case DW_CFA_same_value:
1972 case DW_CFA_def_cfa_register:
1973 case DW_CFA_register:
1974 return dw_cfi_oprnd_reg_num;
1976 case DW_CFA_def_cfa_offset:
1977 case DW_CFA_GNU_args_size:
1978 case DW_CFA_def_cfa_offset_sf:
1979 return dw_cfi_oprnd_offset;
1981 case DW_CFA_def_cfa_expression:
1982 case DW_CFA_expression:
1983 return dw_cfi_oprnd_loc;
1985 default:
1986 gcc_unreachable ();
1990 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
1991 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1992 (enum dwarf_call_frame_info cfi);
1994 static enum dw_cfi_oprnd_type
1995 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1997 switch (cfi)
1999 case DW_CFA_def_cfa:
2000 case DW_CFA_def_cfa_sf:
2001 case DW_CFA_offset:
2002 case DW_CFA_offset_extended_sf:
2003 case DW_CFA_offset_extended:
2004 return dw_cfi_oprnd_offset;
2006 case DW_CFA_register:
2007 return dw_cfi_oprnd_reg_num;
2009 default:
2010 return dw_cfi_oprnd_unused;
2014 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2016 /* Switch to eh_frame_section. If we don't have an eh_frame_section,
2017 switch to the data section instead, and write out a synthetic label
2018 for collect2. */
2020 static void
2021 switch_to_eh_frame_section (void)
2023 tree label;
2025 #ifdef EH_FRAME_SECTION_NAME
2026 if (eh_frame_section == 0)
2028 int flags;
2030 if (EH_TABLES_CAN_BE_READ_ONLY)
2032 int fde_encoding;
2033 int per_encoding;
2034 int lsda_encoding;
2036 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2037 /*global=*/0);
2038 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2039 /*global=*/1);
2040 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2041 /*global=*/0);
2042 flags = ((! flag_pic
2043 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2044 && (fde_encoding & 0x70) != DW_EH_PE_aligned
2045 && (per_encoding & 0x70) != DW_EH_PE_absptr
2046 && (per_encoding & 0x70) != DW_EH_PE_aligned
2047 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2048 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2049 ? 0 : SECTION_WRITE);
2051 else
2052 flags = SECTION_WRITE;
2053 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2055 #endif
2057 if (eh_frame_section)
2058 switch_to_section (eh_frame_section);
2059 else
2061 /* We have no special eh_frame section. Put the information in
2062 the data section and emit special labels to guide collect2. */
2063 switch_to_section (data_section);
2064 label = get_file_function_name ("F");
2065 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2066 targetm.asm_out.globalize_label (asm_out_file,
2067 IDENTIFIER_POINTER (label));
2068 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2072 /* Output a Call Frame Information opcode and its operand(s). */
2074 static void
2075 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2077 unsigned long r;
2078 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2079 dw2_asm_output_data (1, (cfi->dw_cfi_opc
2080 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2081 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2082 cfi->dw_cfi_oprnd1.dw_cfi_offset);
2083 else if (cfi->dw_cfi_opc == DW_CFA_offset)
2085 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2086 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2087 "DW_CFA_offset, column 0x%lx", r);
2088 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2090 else if (cfi->dw_cfi_opc == DW_CFA_restore)
2092 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2093 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2094 "DW_CFA_restore, column 0x%lx", r);
2096 else
2098 dw2_asm_output_data (1, cfi->dw_cfi_opc,
2099 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2101 switch (cfi->dw_cfi_opc)
2103 case DW_CFA_set_loc:
2104 if (for_eh)
2105 dw2_asm_output_encoded_addr_rtx (
2106 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2107 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2108 false, NULL);
2109 else
2110 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2111 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2112 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2113 break;
2115 case DW_CFA_advance_loc1:
2116 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2117 fde->dw_fde_current_label, NULL);
2118 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2119 break;
2121 case DW_CFA_advance_loc2:
2122 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2123 fde->dw_fde_current_label, NULL);
2124 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2125 break;
2127 case DW_CFA_advance_loc4:
2128 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2129 fde->dw_fde_current_label, NULL);
2130 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2131 break;
2133 case DW_CFA_MIPS_advance_loc8:
2134 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2135 fde->dw_fde_current_label, NULL);
2136 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2137 break;
2139 case DW_CFA_offset_extended:
2140 case DW_CFA_def_cfa:
2141 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2142 dw2_asm_output_data_uleb128 (r, NULL);
2143 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2144 break;
2146 case DW_CFA_offset_extended_sf:
2147 case DW_CFA_def_cfa_sf:
2148 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2149 dw2_asm_output_data_uleb128 (r, NULL);
2150 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2151 break;
2153 case DW_CFA_restore_extended:
2154 case DW_CFA_undefined:
2155 case DW_CFA_same_value:
2156 case DW_CFA_def_cfa_register:
2157 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2158 dw2_asm_output_data_uleb128 (r, NULL);
2159 break;
2161 case DW_CFA_register:
2162 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2163 dw2_asm_output_data_uleb128 (r, NULL);
2164 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2165 dw2_asm_output_data_uleb128 (r, NULL);
2166 break;
2168 case DW_CFA_def_cfa_offset:
2169 case DW_CFA_GNU_args_size:
2170 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2171 break;
2173 case DW_CFA_def_cfa_offset_sf:
2174 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2175 break;
2177 case DW_CFA_GNU_window_save:
2178 break;
2180 case DW_CFA_def_cfa_expression:
2181 case DW_CFA_expression:
2182 output_cfa_loc (cfi);
2183 break;
2185 case DW_CFA_GNU_negative_offset_extended:
2186 /* Obsoleted by DW_CFA_offset_extended_sf. */
2187 gcc_unreachable ();
2189 default:
2190 break;
2195 /* Output the call frame information used to record information
2196 that relates to calculating the frame pointer, and records the
2197 location of saved registers. */
2199 static void
2200 output_call_frame_info (int for_eh)
2202 unsigned int i;
2203 dw_fde_ref fde;
2204 dw_cfi_ref cfi;
2205 char l1[20], l2[20], section_start_label[20];
2206 bool any_lsda_needed = false;
2207 char augmentation[6];
2208 int augmentation_size;
2209 int fde_encoding = DW_EH_PE_absptr;
2210 int per_encoding = DW_EH_PE_absptr;
2211 int lsda_encoding = DW_EH_PE_absptr;
2212 int return_reg;
2214 /* Don't emit a CIE if there won't be any FDEs. */
2215 if (fde_table_in_use == 0)
2216 return;
2218 /* If we make FDEs linkonce, we may have to emit an empty label for
2219 an FDE that wouldn't otherwise be emitted. We want to avoid
2220 having an FDE kept around when the function it refers to is
2221 discarded. Example where this matters: a primary function
2222 template in C++ requires EH information, but an explicit
2223 specialization doesn't. */
2224 if (TARGET_USES_WEAK_UNWIND_INFO
2225 && ! flag_asynchronous_unwind_tables
2226 && flag_exceptions
2227 && for_eh)
2228 for (i = 0; i < fde_table_in_use; i++)
2229 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2230 && !fde_table[i].uses_eh_lsda
2231 && ! DECL_WEAK (fde_table[i].decl))
2232 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2233 for_eh, /* empty */ 1);
2235 /* If we don't have any functions we'll want to unwind out of, don't
2236 emit any EH unwind information. Note that if exceptions aren't
2237 enabled, we won't have collected nothrow information, and if we
2238 asked for asynchronous tables, we always want this info. */
2239 if (for_eh)
2241 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2243 for (i = 0; i < fde_table_in_use; i++)
2244 if (fde_table[i].uses_eh_lsda)
2245 any_eh_needed = any_lsda_needed = true;
2246 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2247 any_eh_needed = true;
2248 else if (! fde_table[i].nothrow
2249 && ! fde_table[i].all_throwers_are_sibcalls)
2250 any_eh_needed = true;
2252 if (! any_eh_needed)
2253 return;
2256 /* We're going to be generating comments, so turn on app. */
2257 if (flag_debug_asm)
2258 app_enable ();
2260 if (for_eh)
2261 switch_to_eh_frame_section ();
2262 else
2264 if (!debug_frame_section)
2265 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2266 SECTION_DEBUG, NULL);
2267 switch_to_section (debug_frame_section);
2270 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2271 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2273 /* Output the CIE. */
2274 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2275 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2276 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2277 dw2_asm_output_data (4, 0xffffffff,
2278 "Initial length escape value indicating 64-bit DWARF extension");
2279 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2280 "Length of Common Information Entry");
2281 ASM_OUTPUT_LABEL (asm_out_file, l1);
2283 /* Now that the CIE pointer is PC-relative for EH,
2284 use 0 to identify the CIE. */
2285 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2286 (for_eh ? 0 : DWARF_CIE_ID),
2287 "CIE Identifier Tag");
2289 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2291 augmentation[0] = 0;
2292 augmentation_size = 0;
2293 if (for_eh)
2295 char *p;
2297 /* Augmentation:
2298 z Indicates that a uleb128 is present to size the
2299 augmentation section.
2300 L Indicates the encoding (and thus presence) of
2301 an LSDA pointer in the FDE augmentation.
2302 R Indicates a non-default pointer encoding for
2303 FDE code pointers.
2304 P Indicates the presence of an encoding + language
2305 personality routine in the CIE augmentation. */
2307 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2308 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2309 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2311 p = augmentation + 1;
2312 if (eh_personality_libfunc)
2314 *p++ = 'P';
2315 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2316 assemble_external_libcall (eh_personality_libfunc);
2318 if (any_lsda_needed)
2320 *p++ = 'L';
2321 augmentation_size += 1;
2323 if (fde_encoding != DW_EH_PE_absptr)
2325 *p++ = 'R';
2326 augmentation_size += 1;
2328 if (p > augmentation + 1)
2330 augmentation[0] = 'z';
2331 *p = '\0';
2334 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2335 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2337 int offset = ( 4 /* Length */
2338 + 4 /* CIE Id */
2339 + 1 /* CIE version */
2340 + strlen (augmentation) + 1 /* Augmentation */
2341 + size_of_uleb128 (1) /* Code alignment */
2342 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2343 + 1 /* RA column */
2344 + 1 /* Augmentation size */
2345 + 1 /* Personality encoding */ );
2346 int pad = -offset & (PTR_SIZE - 1);
2348 augmentation_size += pad;
2350 /* Augmentations should be small, so there's scarce need to
2351 iterate for a solution. Die if we exceed one uleb128 byte. */
2352 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2356 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2357 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2358 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2359 "CIE Data Alignment Factor");
2361 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2362 if (DW_CIE_VERSION == 1)
2363 dw2_asm_output_data (1, return_reg, "CIE RA Column");
2364 else
2365 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2367 if (augmentation[0])
2369 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2370 if (eh_personality_libfunc)
2372 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2373 eh_data_format_name (per_encoding));
2374 dw2_asm_output_encoded_addr_rtx (per_encoding,
2375 eh_personality_libfunc,
2376 true, NULL);
2379 if (any_lsda_needed)
2380 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2381 eh_data_format_name (lsda_encoding));
2383 if (fde_encoding != DW_EH_PE_absptr)
2384 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2385 eh_data_format_name (fde_encoding));
2388 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2389 output_cfi (cfi, NULL, for_eh);
2391 /* Pad the CIE out to an address sized boundary. */
2392 ASM_OUTPUT_ALIGN (asm_out_file,
2393 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2394 ASM_OUTPUT_LABEL (asm_out_file, l2);
2396 /* Loop through all of the FDE's. */
2397 for (i = 0; i < fde_table_in_use; i++)
2399 fde = &fde_table[i];
2401 /* Don't emit EH unwind info for leaf functions that don't need it. */
2402 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2403 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2404 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2405 && !fde->uses_eh_lsda)
2406 continue;
2408 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2409 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2410 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2411 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2412 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2413 dw2_asm_output_data (4, 0xffffffff,
2414 "Initial length escape value indicating 64-bit DWARF extension");
2415 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2416 "FDE Length");
2417 ASM_OUTPUT_LABEL (asm_out_file, l1);
2419 if (for_eh)
2420 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2421 else
2422 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2423 debug_frame_section, "FDE CIE offset");
2425 if (for_eh)
2427 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2428 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2429 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2430 sym_ref,
2431 false,
2432 "FDE initial location");
2433 if (fde->dw_fde_switched_sections)
2435 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2436 fde->dw_fde_unlikely_section_label);
2437 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2438 fde->dw_fde_hot_section_label);
2439 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2440 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2441 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2442 "FDE initial location");
2443 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2444 fde->dw_fde_hot_section_end_label,
2445 fde->dw_fde_hot_section_label,
2446 "FDE address range");
2447 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2448 "FDE initial location");
2449 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2450 fde->dw_fde_unlikely_section_end_label,
2451 fde->dw_fde_unlikely_section_label,
2452 "FDE address range");
2454 else
2455 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2456 fde->dw_fde_end, fde->dw_fde_begin,
2457 "FDE address range");
2459 else
2461 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2462 "FDE initial location");
2463 if (fde->dw_fde_switched_sections)
2465 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2466 fde->dw_fde_hot_section_label,
2467 "FDE initial location");
2468 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2469 fde->dw_fde_hot_section_end_label,
2470 fde->dw_fde_hot_section_label,
2471 "FDE address range");
2472 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2473 fde->dw_fde_unlikely_section_label,
2474 "FDE initial location");
2475 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2476 fde->dw_fde_unlikely_section_end_label,
2477 fde->dw_fde_unlikely_section_label,
2478 "FDE address range");
2480 else
2481 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2482 fde->dw_fde_end, fde->dw_fde_begin,
2483 "FDE address range");
2486 if (augmentation[0])
2488 if (any_lsda_needed)
2490 int size = size_of_encoded_value (lsda_encoding);
2492 if (lsda_encoding == DW_EH_PE_aligned)
2494 int offset = ( 4 /* Length */
2495 + 4 /* CIE offset */
2496 + 2 * size_of_encoded_value (fde_encoding)
2497 + 1 /* Augmentation size */ );
2498 int pad = -offset & (PTR_SIZE - 1);
2500 size += pad;
2501 gcc_assert (size_of_uleb128 (size) == 1);
2504 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2506 if (fde->uses_eh_lsda)
2508 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2509 fde->funcdef_number);
2510 dw2_asm_output_encoded_addr_rtx (
2511 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2512 false, "Language Specific Data Area");
2514 else
2516 if (lsda_encoding == DW_EH_PE_aligned)
2517 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2518 dw2_asm_output_data
2519 (size_of_encoded_value (lsda_encoding), 0,
2520 "Language Specific Data Area (none)");
2523 else
2524 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2527 /* Loop through the Call Frame Instructions associated with
2528 this FDE. */
2529 fde->dw_fde_current_label = fde->dw_fde_begin;
2530 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2531 output_cfi (cfi, fde, for_eh);
2533 /* Pad the FDE out to an address sized boundary. */
2534 ASM_OUTPUT_ALIGN (asm_out_file,
2535 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2536 ASM_OUTPUT_LABEL (asm_out_file, l2);
2539 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2540 dw2_asm_output_data (4, 0, "End of Table");
2541 #ifdef MIPS_DEBUGGING_INFO
2542 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2543 get a value of 0. Putting .align 0 after the label fixes it. */
2544 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2545 #endif
2547 /* Turn off app to make assembly quicker. */
2548 if (flag_debug_asm)
2549 app_disable ();
2552 /* Output a marker (i.e. a label) for the beginning of a function, before
2553 the prologue. */
2555 void
2556 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2557 const char *file ATTRIBUTE_UNUSED)
2559 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2560 char * dup_label;
2561 dw_fde_ref fde;
2563 current_function_func_begin_label = NULL;
2565 #ifdef TARGET_UNWIND_INFO
2566 /* ??? current_function_func_begin_label is also used by except.c
2567 for call-site information. We must emit this label if it might
2568 be used. */
2569 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2570 && ! dwarf2out_do_frame ())
2571 return;
2572 #else
2573 if (! dwarf2out_do_frame ())
2574 return;
2575 #endif
2577 switch_to_section (function_section (current_function_decl));
2578 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2579 current_function_funcdef_no);
2580 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2581 current_function_funcdef_no);
2582 dup_label = xstrdup (label);
2583 current_function_func_begin_label = dup_label;
2585 #ifdef TARGET_UNWIND_INFO
2586 /* We can elide the fde allocation if we're not emitting debug info. */
2587 if (! dwarf2out_do_frame ())
2588 return;
2589 #endif
2591 /* Expand the fde table if necessary. */
2592 if (fde_table_in_use == fde_table_allocated)
2594 fde_table_allocated += FDE_TABLE_INCREMENT;
2595 fde_table = ggc_realloc (fde_table,
2596 fde_table_allocated * sizeof (dw_fde_node));
2597 memset (fde_table + fde_table_in_use, 0,
2598 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2601 /* Record the FDE associated with this function. */
2602 current_funcdef_fde = fde_table_in_use;
2604 /* Add the new FDE at the end of the fde_table. */
2605 fde = &fde_table[fde_table_in_use++];
2606 fde->decl = current_function_decl;
2607 fde->dw_fde_begin = dup_label;
2608 fde->dw_fde_current_label = dup_label;
2609 fde->dw_fde_hot_section_label = NULL;
2610 fde->dw_fde_hot_section_end_label = NULL;
2611 fde->dw_fde_unlikely_section_label = NULL;
2612 fde->dw_fde_unlikely_section_end_label = NULL;
2613 fde->dw_fde_switched_sections = false;
2614 fde->dw_fde_end = NULL;
2615 fde->dw_fde_cfi = NULL;
2616 fde->funcdef_number = current_function_funcdef_no;
2617 fde->nothrow = TREE_NOTHROW (current_function_decl);
2618 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2619 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2621 args_size = old_args_size = 0;
2623 /* We only want to output line number information for the genuine dwarf2
2624 prologue case, not the eh frame case. */
2625 #ifdef DWARF2_DEBUGGING_INFO
2626 if (file)
2627 dwarf2out_source_line (line, file);
2628 #endif
2631 /* Output a marker (i.e. a label) for the absolute end of the generated code
2632 for a function definition. This gets called *after* the epilogue code has
2633 been generated. */
2635 void
2636 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2637 const char *file ATTRIBUTE_UNUSED)
2639 dw_fde_ref fde;
2640 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2642 /* Output a label to mark the endpoint of the code generated for this
2643 function. */
2644 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2645 current_function_funcdef_no);
2646 ASM_OUTPUT_LABEL (asm_out_file, label);
2647 fde = &fde_table[fde_table_in_use - 1];
2648 fde->dw_fde_end = xstrdup (label);
2651 void
2652 dwarf2out_frame_init (void)
2654 /* Allocate the initial hunk of the fde_table. */
2655 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2656 fde_table_allocated = FDE_TABLE_INCREMENT;
2657 fde_table_in_use = 0;
2659 /* Generate the CFA instructions common to all FDE's. Do it now for the
2660 sake of lookup_cfa. */
2662 /* On entry, the Canonical Frame Address is at SP. */
2663 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2665 #ifdef DWARF2_UNWIND_INFO
2666 if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
2667 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2668 #endif
2671 void
2672 dwarf2out_frame_finish (void)
2674 /* Output call frame information. */
2675 if (DWARF2_FRAME_INFO)
2676 output_call_frame_info (0);
2678 #ifndef TARGET_UNWIND_INFO
2679 /* Output another copy for the unwinder. */
2680 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2681 output_call_frame_info (1);
2682 #endif
2684 #endif
2686 /* And now, the subset of the debugging information support code necessary
2687 for emitting location expressions. */
2689 /* Data about a single source file. */
2690 struct dwarf_file_data GTY(())
2692 const char * filename;
2693 int emitted_number;
2696 /* We need some way to distinguish DW_OP_addr with a direct symbol
2697 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2698 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2701 typedef struct dw_val_struct *dw_val_ref;
2702 typedef struct die_struct *dw_die_ref;
2703 typedef const struct die_struct *const_dw_die_ref;
2704 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2705 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2707 /* Each DIE may have a series of attribute/value pairs. Values
2708 can take on several forms. The forms that are used in this
2709 implementation are listed below. */
2711 enum dw_val_class
2713 dw_val_class_addr,
2714 dw_val_class_offset,
2715 dw_val_class_loc,
2716 dw_val_class_loc_list,
2717 dw_val_class_range_list,
2718 dw_val_class_const,
2719 dw_val_class_unsigned_const,
2720 dw_val_class_long_long,
2721 dw_val_class_vec,
2722 dw_val_class_flag,
2723 dw_val_class_die_ref,
2724 dw_val_class_fde_ref,
2725 dw_val_class_lbl_id,
2726 dw_val_class_lineptr,
2727 dw_val_class_str,
2728 dw_val_class_macptr,
2729 dw_val_class_file
2732 /* Describe a double word constant value. */
2733 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2735 typedef struct dw_long_long_struct GTY(())
2737 unsigned long hi;
2738 unsigned long low;
2740 dw_long_long_const;
2742 /* Describe a floating point constant value, or a vector constant value. */
2744 typedef struct dw_vec_struct GTY(())
2746 unsigned char * GTY((length ("%h.length"))) array;
2747 unsigned length;
2748 unsigned elt_size;
2750 dw_vec_const;
2752 /* The dw_val_node describes an attribute's value, as it is
2753 represented internally. */
2755 typedef struct dw_val_struct GTY(())
2757 enum dw_val_class val_class;
2758 union dw_val_struct_union
2760 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2761 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2762 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2763 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2764 HOST_WIDE_INT GTY ((default)) val_int;
2765 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2766 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2767 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2768 struct dw_val_die_union
2770 dw_die_ref die;
2771 int external;
2772 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2773 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2774 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2775 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2776 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2777 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2779 GTY ((desc ("%1.val_class"))) v;
2781 dw_val_node;
2783 /* Locations in memory are described using a sequence of stack machine
2784 operations. */
2786 typedef struct dw_loc_descr_struct GTY(())
2788 dw_loc_descr_ref dw_loc_next;
2789 enum dwarf_location_atom dw_loc_opc;
2790 dw_val_node dw_loc_oprnd1;
2791 dw_val_node dw_loc_oprnd2;
2792 int dw_loc_addr;
2794 dw_loc_descr_node;
2796 /* Location lists are ranges + location descriptions for that range,
2797 so you can track variables that are in different places over
2798 their entire life. */
2799 typedef struct dw_loc_list_struct GTY(())
2801 dw_loc_list_ref dw_loc_next;
2802 const char *begin; /* Label for begin address of range */
2803 const char *end; /* Label for end address of range */
2804 char *ll_symbol; /* Label for beginning of location list.
2805 Only on head of list */
2806 const char *section; /* Section this loclist is relative to */
2807 dw_loc_descr_ref expr;
2808 } dw_loc_list_node;
2810 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2812 static const char *dwarf_stack_op_name (unsigned);
2813 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2814 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2815 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2816 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2817 static unsigned long size_of_locs (dw_loc_descr_ref);
2818 static void output_loc_operands (dw_loc_descr_ref);
2819 static void output_loc_sequence (dw_loc_descr_ref);
2821 /* Convert a DWARF stack opcode into its string name. */
2823 static const char *
2824 dwarf_stack_op_name (unsigned int op)
2826 switch (op)
2828 case DW_OP_addr:
2829 case INTERNAL_DW_OP_tls_addr:
2830 return "DW_OP_addr";
2831 case DW_OP_deref:
2832 return "DW_OP_deref";
2833 case DW_OP_const1u:
2834 return "DW_OP_const1u";
2835 case DW_OP_const1s:
2836 return "DW_OP_const1s";
2837 case DW_OP_const2u:
2838 return "DW_OP_const2u";
2839 case DW_OP_const2s:
2840 return "DW_OP_const2s";
2841 case DW_OP_const4u:
2842 return "DW_OP_const4u";
2843 case DW_OP_const4s:
2844 return "DW_OP_const4s";
2845 case DW_OP_const8u:
2846 return "DW_OP_const8u";
2847 case DW_OP_const8s:
2848 return "DW_OP_const8s";
2849 case DW_OP_constu:
2850 return "DW_OP_constu";
2851 case DW_OP_consts:
2852 return "DW_OP_consts";
2853 case DW_OP_dup:
2854 return "DW_OP_dup";
2855 case DW_OP_drop:
2856 return "DW_OP_drop";
2857 case DW_OP_over:
2858 return "DW_OP_over";
2859 case DW_OP_pick:
2860 return "DW_OP_pick";
2861 case DW_OP_swap:
2862 return "DW_OP_swap";
2863 case DW_OP_rot:
2864 return "DW_OP_rot";
2865 case DW_OP_xderef:
2866 return "DW_OP_xderef";
2867 case DW_OP_abs:
2868 return "DW_OP_abs";
2869 case DW_OP_and:
2870 return "DW_OP_and";
2871 case DW_OP_div:
2872 return "DW_OP_div";
2873 case DW_OP_minus:
2874 return "DW_OP_minus";
2875 case DW_OP_mod:
2876 return "DW_OP_mod";
2877 case DW_OP_mul:
2878 return "DW_OP_mul";
2879 case DW_OP_neg:
2880 return "DW_OP_neg";
2881 case DW_OP_not:
2882 return "DW_OP_not";
2883 case DW_OP_or:
2884 return "DW_OP_or";
2885 case DW_OP_plus:
2886 return "DW_OP_plus";
2887 case DW_OP_plus_uconst:
2888 return "DW_OP_plus_uconst";
2889 case DW_OP_shl:
2890 return "DW_OP_shl";
2891 case DW_OP_shr:
2892 return "DW_OP_shr";
2893 case DW_OP_shra:
2894 return "DW_OP_shra";
2895 case DW_OP_xor:
2896 return "DW_OP_xor";
2897 case DW_OP_bra:
2898 return "DW_OP_bra";
2899 case DW_OP_eq:
2900 return "DW_OP_eq";
2901 case DW_OP_ge:
2902 return "DW_OP_ge";
2903 case DW_OP_gt:
2904 return "DW_OP_gt";
2905 case DW_OP_le:
2906 return "DW_OP_le";
2907 case DW_OP_lt:
2908 return "DW_OP_lt";
2909 case DW_OP_ne:
2910 return "DW_OP_ne";
2911 case DW_OP_skip:
2912 return "DW_OP_skip";
2913 case DW_OP_lit0:
2914 return "DW_OP_lit0";
2915 case DW_OP_lit1:
2916 return "DW_OP_lit1";
2917 case DW_OP_lit2:
2918 return "DW_OP_lit2";
2919 case DW_OP_lit3:
2920 return "DW_OP_lit3";
2921 case DW_OP_lit4:
2922 return "DW_OP_lit4";
2923 case DW_OP_lit5:
2924 return "DW_OP_lit5";
2925 case DW_OP_lit6:
2926 return "DW_OP_lit6";
2927 case DW_OP_lit7:
2928 return "DW_OP_lit7";
2929 case DW_OP_lit8:
2930 return "DW_OP_lit8";
2931 case DW_OP_lit9:
2932 return "DW_OP_lit9";
2933 case DW_OP_lit10:
2934 return "DW_OP_lit10";
2935 case DW_OP_lit11:
2936 return "DW_OP_lit11";
2937 case DW_OP_lit12:
2938 return "DW_OP_lit12";
2939 case DW_OP_lit13:
2940 return "DW_OP_lit13";
2941 case DW_OP_lit14:
2942 return "DW_OP_lit14";
2943 case DW_OP_lit15:
2944 return "DW_OP_lit15";
2945 case DW_OP_lit16:
2946 return "DW_OP_lit16";
2947 case DW_OP_lit17:
2948 return "DW_OP_lit17";
2949 case DW_OP_lit18:
2950 return "DW_OP_lit18";
2951 case DW_OP_lit19:
2952 return "DW_OP_lit19";
2953 case DW_OP_lit20:
2954 return "DW_OP_lit20";
2955 case DW_OP_lit21:
2956 return "DW_OP_lit21";
2957 case DW_OP_lit22:
2958 return "DW_OP_lit22";
2959 case DW_OP_lit23:
2960 return "DW_OP_lit23";
2961 case DW_OP_lit24:
2962 return "DW_OP_lit24";
2963 case DW_OP_lit25:
2964 return "DW_OP_lit25";
2965 case DW_OP_lit26:
2966 return "DW_OP_lit26";
2967 case DW_OP_lit27:
2968 return "DW_OP_lit27";
2969 case DW_OP_lit28:
2970 return "DW_OP_lit28";
2971 case DW_OP_lit29:
2972 return "DW_OP_lit29";
2973 case DW_OP_lit30:
2974 return "DW_OP_lit30";
2975 case DW_OP_lit31:
2976 return "DW_OP_lit31";
2977 case DW_OP_reg0:
2978 return "DW_OP_reg0";
2979 case DW_OP_reg1:
2980 return "DW_OP_reg1";
2981 case DW_OP_reg2:
2982 return "DW_OP_reg2";
2983 case DW_OP_reg3:
2984 return "DW_OP_reg3";
2985 case DW_OP_reg4:
2986 return "DW_OP_reg4";
2987 case DW_OP_reg5:
2988 return "DW_OP_reg5";
2989 case DW_OP_reg6:
2990 return "DW_OP_reg6";
2991 case DW_OP_reg7:
2992 return "DW_OP_reg7";
2993 case DW_OP_reg8:
2994 return "DW_OP_reg8";
2995 case DW_OP_reg9:
2996 return "DW_OP_reg9";
2997 case DW_OP_reg10:
2998 return "DW_OP_reg10";
2999 case DW_OP_reg11:
3000 return "DW_OP_reg11";
3001 case DW_OP_reg12:
3002 return "DW_OP_reg12";
3003 case DW_OP_reg13:
3004 return "DW_OP_reg13";
3005 case DW_OP_reg14:
3006 return "DW_OP_reg14";
3007 case DW_OP_reg15:
3008 return "DW_OP_reg15";
3009 case DW_OP_reg16:
3010 return "DW_OP_reg16";
3011 case DW_OP_reg17:
3012 return "DW_OP_reg17";
3013 case DW_OP_reg18:
3014 return "DW_OP_reg18";
3015 case DW_OP_reg19:
3016 return "DW_OP_reg19";
3017 case DW_OP_reg20:
3018 return "DW_OP_reg20";
3019 case DW_OP_reg21:
3020 return "DW_OP_reg21";
3021 case DW_OP_reg22:
3022 return "DW_OP_reg22";
3023 case DW_OP_reg23:
3024 return "DW_OP_reg23";
3025 case DW_OP_reg24:
3026 return "DW_OP_reg24";
3027 case DW_OP_reg25:
3028 return "DW_OP_reg25";
3029 case DW_OP_reg26:
3030 return "DW_OP_reg26";
3031 case DW_OP_reg27:
3032 return "DW_OP_reg27";
3033 case DW_OP_reg28:
3034 return "DW_OP_reg28";
3035 case DW_OP_reg29:
3036 return "DW_OP_reg29";
3037 case DW_OP_reg30:
3038 return "DW_OP_reg30";
3039 case DW_OP_reg31:
3040 return "DW_OP_reg31";
3041 case DW_OP_breg0:
3042 return "DW_OP_breg0";
3043 case DW_OP_breg1:
3044 return "DW_OP_breg1";
3045 case DW_OP_breg2:
3046 return "DW_OP_breg2";
3047 case DW_OP_breg3:
3048 return "DW_OP_breg3";
3049 case DW_OP_breg4:
3050 return "DW_OP_breg4";
3051 case DW_OP_breg5:
3052 return "DW_OP_breg5";
3053 case DW_OP_breg6:
3054 return "DW_OP_breg6";
3055 case DW_OP_breg7:
3056 return "DW_OP_breg7";
3057 case DW_OP_breg8:
3058 return "DW_OP_breg8";
3059 case DW_OP_breg9:
3060 return "DW_OP_breg9";
3061 case DW_OP_breg10:
3062 return "DW_OP_breg10";
3063 case DW_OP_breg11:
3064 return "DW_OP_breg11";
3065 case DW_OP_breg12:
3066 return "DW_OP_breg12";
3067 case DW_OP_breg13:
3068 return "DW_OP_breg13";
3069 case DW_OP_breg14:
3070 return "DW_OP_breg14";
3071 case DW_OP_breg15:
3072 return "DW_OP_breg15";
3073 case DW_OP_breg16:
3074 return "DW_OP_breg16";
3075 case DW_OP_breg17:
3076 return "DW_OP_breg17";
3077 case DW_OP_breg18:
3078 return "DW_OP_breg18";
3079 case DW_OP_breg19:
3080 return "DW_OP_breg19";
3081 case DW_OP_breg20:
3082 return "DW_OP_breg20";
3083 case DW_OP_breg21:
3084 return "DW_OP_breg21";
3085 case DW_OP_breg22:
3086 return "DW_OP_breg22";
3087 case DW_OP_breg23:
3088 return "DW_OP_breg23";
3089 case DW_OP_breg24:
3090 return "DW_OP_breg24";
3091 case DW_OP_breg25:
3092 return "DW_OP_breg25";
3093 case DW_OP_breg26:
3094 return "DW_OP_breg26";
3095 case DW_OP_breg27:
3096 return "DW_OP_breg27";
3097 case DW_OP_breg28:
3098 return "DW_OP_breg28";
3099 case DW_OP_breg29:
3100 return "DW_OP_breg29";
3101 case DW_OP_breg30:
3102 return "DW_OP_breg30";
3103 case DW_OP_breg31:
3104 return "DW_OP_breg31";
3105 case DW_OP_regx:
3106 return "DW_OP_regx";
3107 case DW_OP_fbreg:
3108 return "DW_OP_fbreg";
3109 case DW_OP_bregx:
3110 return "DW_OP_bregx";
3111 case DW_OP_piece:
3112 return "DW_OP_piece";
3113 case DW_OP_deref_size:
3114 return "DW_OP_deref_size";
3115 case DW_OP_xderef_size:
3116 return "DW_OP_xderef_size";
3117 case DW_OP_nop:
3118 return "DW_OP_nop";
3119 case DW_OP_push_object_address:
3120 return "DW_OP_push_object_address";
3121 case DW_OP_call2:
3122 return "DW_OP_call2";
3123 case DW_OP_call4:
3124 return "DW_OP_call4";
3125 case DW_OP_call_ref:
3126 return "DW_OP_call_ref";
3127 case DW_OP_GNU_push_tls_address:
3128 return "DW_OP_GNU_push_tls_address";
3129 case DW_OP_GNU_uninit:
3130 return "DW_OP_GNU_uninit";
3131 default:
3132 return "OP_<unknown>";
3136 /* Return a pointer to a newly allocated location description. Location
3137 descriptions are simple expression terms that can be strung
3138 together to form more complicated location (address) descriptions. */
3140 static inline dw_loc_descr_ref
3141 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3142 unsigned HOST_WIDE_INT oprnd2)
3144 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3146 descr->dw_loc_opc = op;
3147 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3148 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3149 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3150 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3152 return descr;
3155 /* Add a location description term to a location description expression. */
3157 static inline void
3158 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3160 dw_loc_descr_ref *d;
3162 /* Find the end of the chain. */
3163 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3166 *d = descr;
3169 /* Return the size of a location descriptor. */
3171 static unsigned long
3172 size_of_loc_descr (dw_loc_descr_ref loc)
3174 unsigned long size = 1;
3176 switch (loc->dw_loc_opc)
3178 case DW_OP_addr:
3179 case INTERNAL_DW_OP_tls_addr:
3180 size += DWARF2_ADDR_SIZE;
3181 break;
3182 case DW_OP_const1u:
3183 case DW_OP_const1s:
3184 size += 1;
3185 break;
3186 case DW_OP_const2u:
3187 case DW_OP_const2s:
3188 size += 2;
3189 break;
3190 case DW_OP_const4u:
3191 case DW_OP_const4s:
3192 size += 4;
3193 break;
3194 case DW_OP_const8u:
3195 case DW_OP_const8s:
3196 size += 8;
3197 break;
3198 case DW_OP_constu:
3199 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3200 break;
3201 case DW_OP_consts:
3202 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3203 break;
3204 case DW_OP_pick:
3205 size += 1;
3206 break;
3207 case DW_OP_plus_uconst:
3208 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3209 break;
3210 case DW_OP_skip:
3211 case DW_OP_bra:
3212 size += 2;
3213 break;
3214 case DW_OP_breg0:
3215 case DW_OP_breg1:
3216 case DW_OP_breg2:
3217 case DW_OP_breg3:
3218 case DW_OP_breg4:
3219 case DW_OP_breg5:
3220 case DW_OP_breg6:
3221 case DW_OP_breg7:
3222 case DW_OP_breg8:
3223 case DW_OP_breg9:
3224 case DW_OP_breg10:
3225 case DW_OP_breg11:
3226 case DW_OP_breg12:
3227 case DW_OP_breg13:
3228 case DW_OP_breg14:
3229 case DW_OP_breg15:
3230 case DW_OP_breg16:
3231 case DW_OP_breg17:
3232 case DW_OP_breg18:
3233 case DW_OP_breg19:
3234 case DW_OP_breg20:
3235 case DW_OP_breg21:
3236 case DW_OP_breg22:
3237 case DW_OP_breg23:
3238 case DW_OP_breg24:
3239 case DW_OP_breg25:
3240 case DW_OP_breg26:
3241 case DW_OP_breg27:
3242 case DW_OP_breg28:
3243 case DW_OP_breg29:
3244 case DW_OP_breg30:
3245 case DW_OP_breg31:
3246 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3247 break;
3248 case DW_OP_regx:
3249 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3250 break;
3251 case DW_OP_fbreg:
3252 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3253 break;
3254 case DW_OP_bregx:
3255 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3256 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3257 break;
3258 case DW_OP_piece:
3259 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3260 break;
3261 case DW_OP_deref_size:
3262 case DW_OP_xderef_size:
3263 size += 1;
3264 break;
3265 case DW_OP_call2:
3266 size += 2;
3267 break;
3268 case DW_OP_call4:
3269 size += 4;
3270 break;
3271 case DW_OP_call_ref:
3272 size += DWARF2_ADDR_SIZE;
3273 break;
3274 default:
3275 break;
3278 return size;
3281 /* Return the size of a series of location descriptors. */
3283 static unsigned long
3284 size_of_locs (dw_loc_descr_ref loc)
3286 dw_loc_descr_ref l;
3287 unsigned long size;
3289 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3290 field, to avoid writing to a PCH file. */
3291 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3293 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3294 break;
3295 size += size_of_loc_descr (l);
3297 if (! l)
3298 return size;
3300 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3302 l->dw_loc_addr = size;
3303 size += size_of_loc_descr (l);
3306 return size;
3309 /* Output location description stack opcode's operands (if any). */
3311 static void
3312 output_loc_operands (dw_loc_descr_ref loc)
3314 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3315 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3317 switch (loc->dw_loc_opc)
3319 #ifdef DWARF2_DEBUGGING_INFO
3320 case DW_OP_addr:
3321 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3322 break;
3323 case DW_OP_const2u:
3324 case DW_OP_const2s:
3325 dw2_asm_output_data (2, val1->v.val_int, NULL);
3326 break;
3327 case DW_OP_const4u:
3328 case DW_OP_const4s:
3329 dw2_asm_output_data (4, val1->v.val_int, NULL);
3330 break;
3331 case DW_OP_const8u:
3332 case DW_OP_const8s:
3333 gcc_assert (HOST_BITS_PER_LONG >= 64);
3334 dw2_asm_output_data (8, val1->v.val_int, NULL);
3335 break;
3336 case DW_OP_skip:
3337 case DW_OP_bra:
3339 int offset;
3341 gcc_assert (val1->val_class == dw_val_class_loc);
3342 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3344 dw2_asm_output_data (2, offset, NULL);
3346 break;
3347 #else
3348 case DW_OP_addr:
3349 case DW_OP_const2u:
3350 case DW_OP_const2s:
3351 case DW_OP_const4u:
3352 case DW_OP_const4s:
3353 case DW_OP_const8u:
3354 case DW_OP_const8s:
3355 case DW_OP_skip:
3356 case DW_OP_bra:
3357 /* We currently don't make any attempt to make sure these are
3358 aligned properly like we do for the main unwind info, so
3359 don't support emitting things larger than a byte if we're
3360 only doing unwinding. */
3361 gcc_unreachable ();
3362 #endif
3363 case DW_OP_const1u:
3364 case DW_OP_const1s:
3365 dw2_asm_output_data (1, val1->v.val_int, NULL);
3366 break;
3367 case DW_OP_constu:
3368 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3369 break;
3370 case DW_OP_consts:
3371 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3372 break;
3373 case DW_OP_pick:
3374 dw2_asm_output_data (1, val1->v.val_int, NULL);
3375 break;
3376 case DW_OP_plus_uconst:
3377 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3378 break;
3379 case DW_OP_breg0:
3380 case DW_OP_breg1:
3381 case DW_OP_breg2:
3382 case DW_OP_breg3:
3383 case DW_OP_breg4:
3384 case DW_OP_breg5:
3385 case DW_OP_breg6:
3386 case DW_OP_breg7:
3387 case DW_OP_breg8:
3388 case DW_OP_breg9:
3389 case DW_OP_breg10:
3390 case DW_OP_breg11:
3391 case DW_OP_breg12:
3392 case DW_OP_breg13:
3393 case DW_OP_breg14:
3394 case DW_OP_breg15:
3395 case DW_OP_breg16:
3396 case DW_OP_breg17:
3397 case DW_OP_breg18:
3398 case DW_OP_breg19:
3399 case DW_OP_breg20:
3400 case DW_OP_breg21:
3401 case DW_OP_breg22:
3402 case DW_OP_breg23:
3403 case DW_OP_breg24:
3404 case DW_OP_breg25:
3405 case DW_OP_breg26:
3406 case DW_OP_breg27:
3407 case DW_OP_breg28:
3408 case DW_OP_breg29:
3409 case DW_OP_breg30:
3410 case DW_OP_breg31:
3411 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3412 break;
3413 case DW_OP_regx:
3414 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3415 break;
3416 case DW_OP_fbreg:
3417 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3418 break;
3419 case DW_OP_bregx:
3420 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3421 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3422 break;
3423 case DW_OP_piece:
3424 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3425 break;
3426 case DW_OP_deref_size:
3427 case DW_OP_xderef_size:
3428 dw2_asm_output_data (1, val1->v.val_int, NULL);
3429 break;
3431 case INTERNAL_DW_OP_tls_addr:
3432 if (targetm.asm_out.output_dwarf_dtprel)
3434 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3435 DWARF2_ADDR_SIZE,
3436 val1->v.val_addr);
3437 fputc ('\n', asm_out_file);
3439 else
3440 gcc_unreachable ();
3441 break;
3443 default:
3444 /* Other codes have no operands. */
3445 break;
3449 /* Output a sequence of location operations. */
3451 static void
3452 output_loc_sequence (dw_loc_descr_ref loc)
3454 for (; loc != NULL; loc = loc->dw_loc_next)
3456 /* Output the opcode. */
3457 dw2_asm_output_data (1, loc->dw_loc_opc,
3458 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3460 /* Output the operand(s) (if any). */
3461 output_loc_operands (loc);
3465 /* This routine will generate the correct assembly data for a location
3466 description based on a cfi entry with a complex address. */
3468 static void
3469 output_cfa_loc (dw_cfi_ref cfi)
3471 dw_loc_descr_ref loc;
3472 unsigned long size;
3474 /* Output the size of the block. */
3475 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3476 size = size_of_locs (loc);
3477 dw2_asm_output_data_uleb128 (size, NULL);
3479 /* Now output the operations themselves. */
3480 output_loc_sequence (loc);
3483 /* This function builds a dwarf location descriptor sequence from a
3484 dw_cfa_location, adding the given OFFSET to the result of the
3485 expression. */
3487 static struct dw_loc_descr_struct *
3488 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3490 struct dw_loc_descr_struct *head, *tmp;
3492 offset += cfa->offset;
3494 if (cfa->indirect)
3496 if (cfa->base_offset)
3498 if (cfa->reg <= 31)
3499 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3500 else
3501 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3503 else if (cfa->reg <= 31)
3504 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3505 else
3506 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3508 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3509 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3510 add_loc_descr (&head, tmp);
3511 if (offset != 0)
3513 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3514 add_loc_descr (&head, tmp);
3517 else
3519 if (offset == 0)
3520 if (cfa->reg <= 31)
3521 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3522 else
3523 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3524 else if (cfa->reg <= 31)
3525 head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3526 else
3527 head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3530 return head;
3533 /* This function fills in aa dw_cfa_location structure from a dwarf location
3534 descriptor sequence. */
3536 static void
3537 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3539 struct dw_loc_descr_struct *ptr;
3540 cfa->offset = 0;
3541 cfa->base_offset = 0;
3542 cfa->indirect = 0;
3543 cfa->reg = -1;
3545 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3547 enum dwarf_location_atom op = ptr->dw_loc_opc;
3549 switch (op)
3551 case DW_OP_reg0:
3552 case DW_OP_reg1:
3553 case DW_OP_reg2:
3554 case DW_OP_reg3:
3555 case DW_OP_reg4:
3556 case DW_OP_reg5:
3557 case DW_OP_reg6:
3558 case DW_OP_reg7:
3559 case DW_OP_reg8:
3560 case DW_OP_reg9:
3561 case DW_OP_reg10:
3562 case DW_OP_reg11:
3563 case DW_OP_reg12:
3564 case DW_OP_reg13:
3565 case DW_OP_reg14:
3566 case DW_OP_reg15:
3567 case DW_OP_reg16:
3568 case DW_OP_reg17:
3569 case DW_OP_reg18:
3570 case DW_OP_reg19:
3571 case DW_OP_reg20:
3572 case DW_OP_reg21:
3573 case DW_OP_reg22:
3574 case DW_OP_reg23:
3575 case DW_OP_reg24:
3576 case DW_OP_reg25:
3577 case DW_OP_reg26:
3578 case DW_OP_reg27:
3579 case DW_OP_reg28:
3580 case DW_OP_reg29:
3581 case DW_OP_reg30:
3582 case DW_OP_reg31:
3583 cfa->reg = op - DW_OP_reg0;
3584 break;
3585 case DW_OP_regx:
3586 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3587 break;
3588 case DW_OP_breg0:
3589 case DW_OP_breg1:
3590 case DW_OP_breg2:
3591 case DW_OP_breg3:
3592 case DW_OP_breg4:
3593 case DW_OP_breg5:
3594 case DW_OP_breg6:
3595 case DW_OP_breg7:
3596 case DW_OP_breg8:
3597 case DW_OP_breg9:
3598 case DW_OP_breg10:
3599 case DW_OP_breg11:
3600 case DW_OP_breg12:
3601 case DW_OP_breg13:
3602 case DW_OP_breg14:
3603 case DW_OP_breg15:
3604 case DW_OP_breg16:
3605 case DW_OP_breg17:
3606 case DW_OP_breg18:
3607 case DW_OP_breg19:
3608 case DW_OP_breg20:
3609 case DW_OP_breg21:
3610 case DW_OP_breg22:
3611 case DW_OP_breg23:
3612 case DW_OP_breg24:
3613 case DW_OP_breg25:
3614 case DW_OP_breg26:
3615 case DW_OP_breg27:
3616 case DW_OP_breg28:
3617 case DW_OP_breg29:
3618 case DW_OP_breg30:
3619 case DW_OP_breg31:
3620 cfa->reg = op - DW_OP_breg0;
3621 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3622 break;
3623 case DW_OP_bregx:
3624 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3625 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3626 break;
3627 case DW_OP_deref:
3628 cfa->indirect = 1;
3629 break;
3630 case DW_OP_plus_uconst:
3631 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3632 break;
3633 default:
3634 internal_error ("DW_LOC_OP %s not implemented",
3635 dwarf_stack_op_name (ptr->dw_loc_opc));
3639 #endif /* .debug_frame support */
3641 /* And now, the support for symbolic debugging information. */
3642 #ifdef DWARF2_DEBUGGING_INFO
3644 /* .debug_str support. */
3645 static int output_indirect_string (void **, void *);
3647 static void dwarf2out_init (const char *);
3648 static void dwarf2out_finish (const char *);
3649 static void dwarf2out_define (unsigned int, const char *);
3650 static void dwarf2out_undef (unsigned int, const char *);
3651 static void dwarf2out_start_source_file (unsigned, const char *);
3652 static void dwarf2out_end_source_file (unsigned);
3653 static void dwarf2out_begin_block (unsigned, unsigned);
3654 static void dwarf2out_end_block (unsigned, unsigned);
3655 static bool dwarf2out_ignore_block (const_tree);
3656 static void dwarf2out_global_decl (tree);
3657 static void dwarf2out_type_decl (tree, int);
3658 static void dwarf2out_imported_module_or_decl (tree, tree);
3659 static void dwarf2out_abstract_function (tree);
3660 static void dwarf2out_var_location (rtx);
3661 static void dwarf2out_begin_function (tree);
3662 static void dwarf2out_switch_text_section (void);
3664 /* The debug hooks structure. */
3666 const struct gcc_debug_hooks dwarf2_debug_hooks =
3668 dwarf2out_init,
3669 dwarf2out_finish,
3670 dwarf2out_define,
3671 dwarf2out_undef,
3672 dwarf2out_start_source_file,
3673 dwarf2out_end_source_file,
3674 dwarf2out_begin_block,
3675 dwarf2out_end_block,
3676 dwarf2out_ignore_block,
3677 dwarf2out_source_line,
3678 dwarf2out_begin_prologue,
3679 debug_nothing_int_charstar, /* end_prologue */
3680 dwarf2out_end_epilogue,
3681 dwarf2out_begin_function,
3682 debug_nothing_int, /* end_function */
3683 dwarf2out_decl, /* function_decl */
3684 dwarf2out_global_decl,
3685 dwarf2out_type_decl, /* type_decl */
3686 dwarf2out_imported_module_or_decl,
3687 debug_nothing_tree, /* deferred_inline_function */
3688 /* The DWARF 2 backend tries to reduce debugging bloat by not
3689 emitting the abstract description of inline functions until
3690 something tries to reference them. */
3691 dwarf2out_abstract_function, /* outlining_inline_function */
3692 debug_nothing_rtx, /* label */
3693 debug_nothing_int, /* handle_pch */
3694 dwarf2out_var_location,
3695 dwarf2out_switch_text_section,
3696 1 /* start_end_main_source_file */
3698 #endif
3700 /* NOTE: In the comments in this file, many references are made to
3701 "Debugging Information Entries". This term is abbreviated as `DIE'
3702 throughout the remainder of this file. */
3704 /* An internal representation of the DWARF output is built, and then
3705 walked to generate the DWARF debugging info. The walk of the internal
3706 representation is done after the entire program has been compiled.
3707 The types below are used to describe the internal representation. */
3709 /* Various DIE's use offsets relative to the beginning of the
3710 .debug_info section to refer to each other. */
3712 typedef long int dw_offset;
3714 /* Define typedefs here to avoid circular dependencies. */
3716 typedef struct dw_attr_struct *dw_attr_ref;
3717 typedef struct dw_line_info_struct *dw_line_info_ref;
3718 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3719 typedef struct pubname_struct *pubname_ref;
3720 typedef struct dw_ranges_struct *dw_ranges_ref;
3721 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
3723 /* Each entry in the line_info_table maintains the file and
3724 line number associated with the label generated for that
3725 entry. The label gives the PC value associated with
3726 the line number entry. */
3728 typedef struct dw_line_info_struct GTY(())
3730 unsigned long dw_file_num;
3731 unsigned long dw_line_num;
3733 dw_line_info_entry;
3735 /* Line information for functions in separate sections; each one gets its
3736 own sequence. */
3737 typedef struct dw_separate_line_info_struct GTY(())
3739 unsigned long dw_file_num;
3740 unsigned long dw_line_num;
3741 unsigned long function;
3743 dw_separate_line_info_entry;
3745 /* Each DIE attribute has a field specifying the attribute kind,
3746 a link to the next attribute in the chain, and an attribute value.
3747 Attributes are typically linked below the DIE they modify. */
3749 typedef struct dw_attr_struct GTY(())
3751 enum dwarf_attribute dw_attr;
3752 dw_val_node dw_attr_val;
3754 dw_attr_node;
3756 DEF_VEC_O(dw_attr_node);
3757 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3759 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3760 The children of each node form a circular list linked by
3761 die_sib. die_child points to the node *before* the "first" child node. */
3763 typedef struct die_struct GTY(())
3765 enum dwarf_tag die_tag;
3766 char *die_symbol;
3767 VEC(dw_attr_node,gc) * die_attr;
3768 dw_die_ref die_parent;
3769 dw_die_ref die_child;
3770 dw_die_ref die_sib;
3771 dw_die_ref die_definition; /* ref from a specification to its definition */
3772 dw_offset die_offset;
3773 unsigned long die_abbrev;
3774 int die_mark;
3775 /* Die is used and must not be pruned as unused. */
3776 int die_perennial_p;
3777 unsigned int decl_id;
3779 die_node;
3781 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3782 #define FOR_EACH_CHILD(die, c, expr) do { \
3783 c = die->die_child; \
3784 if (c) do { \
3785 c = c->die_sib; \
3786 expr; \
3787 } while (c != die->die_child); \
3788 } while (0)
3790 /* The pubname structure */
3792 typedef struct pubname_struct GTY(())
3794 dw_die_ref die;
3795 const char *name;
3797 pubname_entry;
3799 DEF_VEC_O(pubname_entry);
3800 DEF_VEC_ALLOC_O(pubname_entry, gc);
3802 struct dw_ranges_struct GTY(())
3804 /* If this is positive, it's a block number, otherwise it's a
3805 bitwise-negated index into dw_ranges_by_label. */
3806 int num;
3809 struct dw_ranges_by_label_struct GTY(())
3811 const char *begin;
3812 const char *end;
3815 /* The limbo die list structure. */
3816 typedef struct limbo_die_struct GTY(())
3818 dw_die_ref die;
3819 tree created_for;
3820 struct limbo_die_struct *next;
3822 limbo_die_node;
3824 /* How to start an assembler comment. */
3825 #ifndef ASM_COMMENT_START
3826 #define ASM_COMMENT_START ";#"
3827 #endif
3829 /* Define a macro which returns nonzero for a TYPE_DECL which was
3830 implicitly generated for a tagged type.
3832 Note that unlike the gcc front end (which generates a NULL named
3833 TYPE_DECL node for each complete tagged type, each array type, and
3834 each function type node created) the g++ front end generates a
3835 _named_ TYPE_DECL node for each tagged type node created.
3836 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3837 generate a DW_TAG_typedef DIE for them. */
3839 #define TYPE_DECL_IS_STUB(decl) \
3840 (DECL_NAME (decl) == NULL_TREE \
3841 || (DECL_ARTIFICIAL (decl) \
3842 && is_tagged_type (TREE_TYPE (decl)) \
3843 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3844 /* This is necessary for stub decls that \
3845 appear in nested inline functions. */ \
3846 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3847 && (decl_ultimate_origin (decl) \
3848 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3850 /* Information concerning the compilation unit's programming
3851 language, and compiler version. */
3853 /* Fixed size portion of the DWARF compilation unit header. */
3854 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3855 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3857 /* Fixed size portion of public names info. */
3858 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3860 /* Fixed size portion of the address range info. */
3861 #define DWARF_ARANGES_HEADER_SIZE \
3862 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3863 DWARF2_ADDR_SIZE * 2) \
3864 - DWARF_INITIAL_LENGTH_SIZE)
3866 /* Size of padding portion in the address range info. It must be
3867 aligned to twice the pointer size. */
3868 #define DWARF_ARANGES_PAD_SIZE \
3869 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3870 DWARF2_ADDR_SIZE * 2) \
3871 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3873 /* Use assembler line directives if available. */
3874 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3875 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3876 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3877 #else
3878 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3879 #endif
3880 #endif
3882 /* Minimum line offset in a special line info. opcode.
3883 This value was chosen to give a reasonable range of values. */
3884 #define DWARF_LINE_BASE -10
3886 /* First special line opcode - leave room for the standard opcodes. */
3887 #define DWARF_LINE_OPCODE_BASE 10
3889 /* Range of line offsets in a special line info. opcode. */
3890 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3892 /* Flag that indicates the initial value of the is_stmt_start flag.
3893 In the present implementation, we do not mark any lines as
3894 the beginning of a source statement, because that information
3895 is not made available by the GCC front-end. */
3896 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3898 #ifdef DWARF2_DEBUGGING_INFO
3899 /* This location is used by calc_die_sizes() to keep track
3900 the offset of each DIE within the .debug_info section. */
3901 static unsigned long next_die_offset;
3902 #endif
3904 /* Record the root of the DIE's built for the current compilation unit. */
3905 static GTY(()) dw_die_ref comp_unit_die;
3907 /* A list of DIEs with a NULL parent waiting to be relocated. */
3908 static GTY(()) limbo_die_node *limbo_die_list;
3910 /* Filenames referenced by this compilation unit. */
3911 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3913 /* A hash table of references to DIE's that describe declarations.
3914 The key is a DECL_UID() which is a unique number identifying each decl. */
3915 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3917 /* Node of the variable location list. */
3918 struct var_loc_node GTY ((chain_next ("%h.next")))
3920 rtx GTY (()) var_loc_note;
3921 const char * GTY (()) label;
3922 const char * GTY (()) section_label;
3923 struct var_loc_node * GTY (()) next;
3926 /* Variable location list. */
3927 struct var_loc_list_def GTY (())
3929 struct var_loc_node * GTY (()) first;
3931 /* Do not mark the last element of the chained list because
3932 it is marked through the chain. */
3933 struct var_loc_node * GTY ((skip ("%h"))) last;
3935 /* DECL_UID of the variable decl. */
3936 unsigned int decl_id;
3938 typedef struct var_loc_list_def var_loc_list;
3941 /* Table of decl location linked lists. */
3942 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3944 /* A pointer to the base of a list of references to DIE's that
3945 are uniquely identified by their tag, presence/absence of
3946 children DIE's, and list of attribute/value pairs. */
3947 static GTY((length ("abbrev_die_table_allocated")))
3948 dw_die_ref *abbrev_die_table;
3950 /* Number of elements currently allocated for abbrev_die_table. */
3951 static GTY(()) unsigned abbrev_die_table_allocated;
3953 /* Number of elements in type_die_table currently in use. */
3954 static GTY(()) unsigned abbrev_die_table_in_use;
3956 /* Size (in elements) of increments by which we may expand the
3957 abbrev_die_table. */
3958 #define ABBREV_DIE_TABLE_INCREMENT 256
3960 /* A pointer to the base of a table that contains line information
3961 for each source code line in .text in the compilation unit. */
3962 static GTY((length ("line_info_table_allocated")))
3963 dw_line_info_ref line_info_table;
3965 /* Number of elements currently allocated for line_info_table. */
3966 static GTY(()) unsigned line_info_table_allocated;
3968 /* Number of elements in line_info_table currently in use. */
3969 static GTY(()) unsigned line_info_table_in_use;
3971 /* True if the compilation unit places functions in more than one section. */
3972 static GTY(()) bool have_multiple_function_sections = false;
3974 /* A pointer to the base of a table that contains line information
3975 for each source code line outside of .text in the compilation unit. */
3976 static GTY ((length ("separate_line_info_table_allocated")))
3977 dw_separate_line_info_ref separate_line_info_table;
3979 /* Number of elements currently allocated for separate_line_info_table. */
3980 static GTY(()) unsigned separate_line_info_table_allocated;
3982 /* Number of elements in separate_line_info_table currently in use. */
3983 static GTY(()) unsigned separate_line_info_table_in_use;
3985 /* Size (in elements) of increments by which we may expand the
3986 line_info_table. */
3987 #define LINE_INFO_TABLE_INCREMENT 1024
3989 /* A pointer to the base of a table that contains a list of publicly
3990 accessible names. */
3991 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
3993 /* A pointer to the base of a table that contains a list of publicly
3994 accessible types. */
3995 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3997 /* Array of dies for which we should generate .debug_arange info. */
3998 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4000 /* Number of elements currently allocated for arange_table. */
4001 static GTY(()) unsigned arange_table_allocated;
4003 /* Number of elements in arange_table currently in use. */
4004 static GTY(()) unsigned arange_table_in_use;
4006 /* Size (in elements) of increments by which we may expand the
4007 arange_table. */
4008 #define ARANGE_TABLE_INCREMENT 64
4010 /* Array of dies for which we should generate .debug_ranges info. */
4011 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4013 /* Number of elements currently allocated for ranges_table. */
4014 static GTY(()) unsigned ranges_table_allocated;
4016 /* Number of elements in ranges_table currently in use. */
4017 static GTY(()) unsigned ranges_table_in_use;
4019 /* Array of pairs of labels referenced in ranges_table. */
4020 static GTY ((length ("ranges_by_label_allocated")))
4021 dw_ranges_by_label_ref ranges_by_label;
4023 /* Number of elements currently allocated for ranges_by_label. */
4024 static GTY(()) unsigned ranges_by_label_allocated;
4026 /* Number of elements in ranges_by_label currently in use. */
4027 static GTY(()) unsigned ranges_by_label_in_use;
4029 /* Size (in elements) of increments by which we may expand the
4030 ranges_table. */
4031 #define RANGES_TABLE_INCREMENT 64
4033 /* Whether we have location lists that need outputting */
4034 static GTY(()) bool have_location_lists;
4036 /* Unique label counter. */
4037 static GTY(()) unsigned int loclabel_num;
4039 #ifdef DWARF2_DEBUGGING_INFO
4040 /* Record whether the function being analyzed contains inlined functions. */
4041 static int current_function_has_inlines;
4042 #endif
4043 #if 0 && defined (MIPS_DEBUGGING_INFO)
4044 static int comp_unit_has_inlines;
4045 #endif
4047 /* The last file entry emitted by maybe_emit_file(). */
4048 static GTY(()) struct dwarf_file_data * last_emitted_file;
4050 /* Number of internal labels generated by gen_internal_sym(). */
4051 static GTY(()) int label_num;
4053 /* Cached result of previous call to lookup_filename. */
4054 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4056 /* Whether the default text and cold text sections have been used at
4057 all. */
4059 static GTY(()) bool text_section_used = false;
4060 static GTY(()) bool cold_text_section_used = false;
4062 /* The default cold text section. */
4063 static GTY(()) section *cold_text_section;
4065 #ifdef DWARF2_DEBUGGING_INFO
4067 /* Offset from the "steady-state frame pointer" to the frame base,
4068 within the current function. */
4069 static HOST_WIDE_INT frame_pointer_fb_offset;
4071 /* Forward declarations for functions defined in this file. */
4073 static int is_pseudo_reg (const_rtx);
4074 static tree type_main_variant (tree);
4075 static int is_tagged_type (const_tree);
4076 static const char *dwarf_tag_name (unsigned);
4077 static const char *dwarf_attr_name (unsigned);
4078 static const char *dwarf_form_name (unsigned);
4079 static tree decl_ultimate_origin (const_tree);
4080 static tree block_ultimate_origin (const_tree);
4081 static tree decl_class_context (tree);
4082 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4083 static inline enum dw_val_class AT_class (dw_attr_ref);
4084 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4085 static inline unsigned AT_flag (dw_attr_ref);
4086 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4087 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4088 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4089 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4090 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4091 unsigned long);
4092 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4093 unsigned int, unsigned char *);
4094 static hashval_t debug_str_do_hash (const void *);
4095 static int debug_str_eq (const void *, const void *);
4096 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4097 static inline const char *AT_string (dw_attr_ref);
4098 static int AT_string_form (dw_attr_ref);
4099 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4100 static void add_AT_specification (dw_die_ref, dw_die_ref);
4101 static inline dw_die_ref AT_ref (dw_attr_ref);
4102 static inline int AT_ref_external (dw_attr_ref);
4103 static inline void set_AT_ref_external (dw_attr_ref, int);
4104 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4105 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4106 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4107 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4108 dw_loc_list_ref);
4109 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4110 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4111 static inline rtx AT_addr (dw_attr_ref);
4112 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4113 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4114 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4115 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4116 unsigned HOST_WIDE_INT);
4117 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4118 unsigned long);
4119 static inline const char *AT_lbl (dw_attr_ref);
4120 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4121 static const char *get_AT_low_pc (dw_die_ref);
4122 static const char *get_AT_hi_pc (dw_die_ref);
4123 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4124 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4125 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4126 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4127 static bool is_c_family (void);
4128 static bool is_cxx (void);
4129 static bool is_java (void);
4130 static bool is_fortran (void);
4131 static bool is_ada (void);
4132 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4133 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4134 static void add_child_die (dw_die_ref, dw_die_ref);
4135 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4136 static dw_die_ref lookup_type_die (tree);
4137 static void equate_type_number_to_die (tree, dw_die_ref);
4138 static hashval_t decl_die_table_hash (const void *);
4139 static int decl_die_table_eq (const void *, const void *);
4140 static dw_die_ref lookup_decl_die (tree);
4141 static hashval_t decl_loc_table_hash (const void *);
4142 static int decl_loc_table_eq (const void *, const void *);
4143 static var_loc_list *lookup_decl_loc (const_tree);
4144 static void equate_decl_number_to_die (tree, dw_die_ref);
4145 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4146 static void print_spaces (FILE *);
4147 static void print_die (dw_die_ref, FILE *);
4148 static void print_dwarf_line_table (FILE *);
4149 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4150 static dw_die_ref pop_compile_unit (dw_die_ref);
4151 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4152 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4153 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4154 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4155 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4156 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4157 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4158 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4159 static void compute_section_prefix (dw_die_ref);
4160 static int is_type_die (dw_die_ref);
4161 static int is_comdat_die (dw_die_ref);
4162 static int is_symbol_die (dw_die_ref);
4163 static void assign_symbol_names (dw_die_ref);
4164 static void break_out_includes (dw_die_ref);
4165 static hashval_t htab_cu_hash (const void *);
4166 static int htab_cu_eq (const void *, const void *);
4167 static void htab_cu_del (void *);
4168 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4169 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4170 static void add_sibling_attributes (dw_die_ref);
4171 static void build_abbrev_table (dw_die_ref);
4172 static void output_location_lists (dw_die_ref);
4173 static int constant_size (long unsigned);
4174 static unsigned long size_of_die (dw_die_ref);
4175 static void calc_die_sizes (dw_die_ref);
4176 static void mark_dies (dw_die_ref);
4177 static void unmark_dies (dw_die_ref);
4178 static void unmark_all_dies (dw_die_ref);
4179 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4180 static unsigned long size_of_aranges (void);
4181 static enum dwarf_form value_format (dw_attr_ref);
4182 static void output_value_format (dw_attr_ref);
4183 static void output_abbrev_section (void);
4184 static void output_die_symbol (dw_die_ref);
4185 static void output_die (dw_die_ref);
4186 static void output_compilation_unit_header (void);
4187 static void output_comp_unit (dw_die_ref, int);
4188 static const char *dwarf2_name (tree, int);
4189 static void add_pubname (tree, dw_die_ref);
4190 static void add_pubtype (tree, dw_die_ref);
4191 static void output_pubnames (VEC (pubname_entry,gc) *);
4192 static void add_arange (tree, dw_die_ref);
4193 static void output_aranges (void);
4194 static unsigned int add_ranges_num (int);
4195 static unsigned int add_ranges (const_tree);
4196 static unsigned int add_ranges_by_labels (const char *, const char *);
4197 static void output_ranges (void);
4198 static void output_line_info (void);
4199 static void output_file_names (void);
4200 static dw_die_ref base_type_die (tree);
4201 static int is_base_type (tree);
4202 static bool is_subrange_type (const_tree);
4203 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4204 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4205 static int type_is_enum (const_tree);
4206 static unsigned int dbx_reg_number (const_rtx);
4207 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4208 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
4209 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
4210 enum var_init_status);
4211 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
4212 enum var_init_status);
4213 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4214 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
4215 enum var_init_status);
4216 static int is_based_loc (const_rtx);
4217 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
4218 enum var_init_status);
4219 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
4220 enum var_init_status);
4221 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
4222 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4223 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4224 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4225 static tree field_type (const_tree);
4226 static unsigned int simple_type_align_in_bits (const_tree);
4227 static unsigned int simple_decl_align_in_bits (const_tree);
4228 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
4229 static HOST_WIDE_INT field_byte_offset (const_tree);
4230 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4231 dw_loc_descr_ref);
4232 static void add_data_member_location_attribute (dw_die_ref, tree);
4233 static void add_const_value_attribute (dw_die_ref, rtx);
4234 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4235 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4236 static void insert_float (const_rtx, unsigned char *);
4237 static rtx rtl_for_decl_location (tree);
4238 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4239 enum dwarf_attribute);
4240 static void tree_add_const_value_attribute (dw_die_ref, tree);
4241 static void add_name_attribute (dw_die_ref, const char *);
4242 static void add_comp_dir_attribute (dw_die_ref);
4243 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4244 static void add_subscript_info (dw_die_ref, tree);
4245 static void add_byte_size_attribute (dw_die_ref, tree);
4246 static void add_bit_offset_attribute (dw_die_ref, tree);
4247 static void add_bit_size_attribute (dw_die_ref, tree);
4248 static void add_prototyped_attribute (dw_die_ref, tree);
4249 static void add_abstract_origin_attribute (dw_die_ref, tree);
4250 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4251 static void add_src_coords_attributes (dw_die_ref, tree);
4252 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4253 static void push_decl_scope (tree);
4254 static void pop_decl_scope (void);
4255 static dw_die_ref scope_die_for (tree, dw_die_ref);
4256 static inline int local_scope_p (dw_die_ref);
4257 static inline int class_or_namespace_scope_p (dw_die_ref);
4258 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4259 static void add_calling_convention_attribute (dw_die_ref, tree);
4260 static const char *type_tag (const_tree);
4261 static tree member_declared_type (const_tree);
4262 #if 0
4263 static const char *decl_start_label (tree);
4264 #endif
4265 static void gen_array_type_die (tree, dw_die_ref);
4266 #if 0
4267 static void gen_entry_point_die (tree, dw_die_ref);
4268 #endif
4269 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4270 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4271 static void gen_inlined_union_type_die (tree, dw_die_ref);
4272 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4273 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4274 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4275 static void gen_formal_types_die (tree, dw_die_ref);
4276 static void gen_subprogram_die (tree, dw_die_ref);
4277 static void gen_variable_die (tree, dw_die_ref);
4278 static void gen_label_die (tree, dw_die_ref);
4279 static void gen_lexical_block_die (tree, dw_die_ref, int);
4280 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4281 static void gen_field_die (tree, dw_die_ref);
4282 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4283 static dw_die_ref gen_compile_unit_die (const char *);
4284 static void gen_inheritance_die (tree, tree, dw_die_ref);
4285 static void gen_member_die (tree, dw_die_ref);
4286 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4287 enum debug_info_usage);
4288 static void gen_subroutine_type_die (tree, dw_die_ref);
4289 static void gen_typedef_die (tree, dw_die_ref);
4290 static void gen_type_die (tree, dw_die_ref);
4291 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4292 static void gen_block_die (tree, dw_die_ref, int);
4293 static void decls_for_scope (tree, dw_die_ref, int);
4294 static int is_redundant_typedef (const_tree);
4295 static void gen_namespace_die (tree);
4296 static void gen_decl_die (tree, dw_die_ref);
4297 static dw_die_ref force_decl_die (tree);
4298 static dw_die_ref force_type_die (tree);
4299 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4300 static void declare_in_namespace (tree, dw_die_ref);
4301 static struct dwarf_file_data * lookup_filename (const char *);
4302 static void retry_incomplete_types (void);
4303 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4304 static void splice_child_die (dw_die_ref, dw_die_ref);
4305 static int file_info_cmp (const void *, const void *);
4306 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4307 const char *, const char *, unsigned);
4308 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4309 const char *, const char *,
4310 const char *);
4311 static void output_loc_list (dw_loc_list_ref);
4312 static char *gen_internal_sym (const char *);
4314 static void prune_unmark_dies (dw_die_ref);
4315 static void prune_unused_types_mark (dw_die_ref, int);
4316 static void prune_unused_types_walk (dw_die_ref);
4317 static void prune_unused_types_walk_attribs (dw_die_ref);
4318 static void prune_unused_types_prune (dw_die_ref);
4319 static void prune_unused_types (void);
4320 static int maybe_emit_file (struct dwarf_file_data *fd);
4322 /* Section names used to hold DWARF debugging information. */
4323 #ifndef DEBUG_INFO_SECTION
4324 #define DEBUG_INFO_SECTION ".debug_info"
4325 #endif
4326 #ifndef DEBUG_ABBREV_SECTION
4327 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4328 #endif
4329 #ifndef DEBUG_ARANGES_SECTION
4330 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4331 #endif
4332 #ifndef DEBUG_MACINFO_SECTION
4333 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4334 #endif
4335 #ifndef DEBUG_LINE_SECTION
4336 #define DEBUG_LINE_SECTION ".debug_line"
4337 #endif
4338 #ifndef DEBUG_LOC_SECTION
4339 #define DEBUG_LOC_SECTION ".debug_loc"
4340 #endif
4341 #ifndef DEBUG_PUBNAMES_SECTION
4342 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4343 #endif
4344 #ifndef DEBUG_STR_SECTION
4345 #define DEBUG_STR_SECTION ".debug_str"
4346 #endif
4347 #ifndef DEBUG_RANGES_SECTION
4348 #define DEBUG_RANGES_SECTION ".debug_ranges"
4349 #endif
4351 /* Standard ELF section names for compiled code and data. */
4352 #ifndef TEXT_SECTION_NAME
4353 #define TEXT_SECTION_NAME ".text"
4354 #endif
4356 /* Section flags for .debug_str section. */
4357 #define DEBUG_STR_SECTION_FLAGS \
4358 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
4359 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4360 : SECTION_DEBUG)
4362 /* Labels we insert at beginning sections we can reference instead of
4363 the section names themselves. */
4365 #ifndef TEXT_SECTION_LABEL
4366 #define TEXT_SECTION_LABEL "Ltext"
4367 #endif
4368 #ifndef COLD_TEXT_SECTION_LABEL
4369 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4370 #endif
4371 #ifndef DEBUG_LINE_SECTION_LABEL
4372 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4373 #endif
4374 #ifndef DEBUG_INFO_SECTION_LABEL
4375 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4376 #endif
4377 #ifndef DEBUG_ABBREV_SECTION_LABEL
4378 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4379 #endif
4380 #ifndef DEBUG_LOC_SECTION_LABEL
4381 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4382 #endif
4383 #ifndef DEBUG_RANGES_SECTION_LABEL
4384 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4385 #endif
4386 #ifndef DEBUG_MACINFO_SECTION_LABEL
4387 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4388 #endif
4390 /* Definitions of defaults for formats and names of various special
4391 (artificial) labels which may be generated within this file (when the -g
4392 options is used and DWARF2_DEBUGGING_INFO is in effect.
4393 If necessary, these may be overridden from within the tm.h file, but
4394 typically, overriding these defaults is unnecessary. */
4396 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4397 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4398 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4399 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4400 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4401 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4402 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4403 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4404 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4405 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4407 #ifndef TEXT_END_LABEL
4408 #define TEXT_END_LABEL "Letext"
4409 #endif
4410 #ifndef COLD_END_LABEL
4411 #define COLD_END_LABEL "Letext_cold"
4412 #endif
4413 #ifndef BLOCK_BEGIN_LABEL
4414 #define BLOCK_BEGIN_LABEL "LBB"
4415 #endif
4416 #ifndef BLOCK_END_LABEL
4417 #define BLOCK_END_LABEL "LBE"
4418 #endif
4419 #ifndef LINE_CODE_LABEL
4420 #define LINE_CODE_LABEL "LM"
4421 #endif
4422 #ifndef SEPARATE_LINE_CODE_LABEL
4423 #define SEPARATE_LINE_CODE_LABEL "LSM"
4424 #endif
4427 /* We allow a language front-end to designate a function that is to be
4428 called to "demangle" any name before it is put into a DIE. */
4430 static const char *(*demangle_name_func) (const char *);
4432 void
4433 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4435 demangle_name_func = func;
4438 /* Test if rtl node points to a pseudo register. */
4440 static inline int
4441 is_pseudo_reg (const_rtx rtl)
4443 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4444 || (GET_CODE (rtl) == SUBREG
4445 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4448 /* Return a reference to a type, with its const and volatile qualifiers
4449 removed. */
4451 static inline tree
4452 type_main_variant (tree type)
4454 type = TYPE_MAIN_VARIANT (type);
4456 /* ??? There really should be only one main variant among any group of
4457 variants of a given type (and all of the MAIN_VARIANT values for all
4458 members of the group should point to that one type) but sometimes the C
4459 front-end messes this up for array types, so we work around that bug
4460 here. */
4461 if (TREE_CODE (type) == ARRAY_TYPE)
4462 while (type != TYPE_MAIN_VARIANT (type))
4463 type = TYPE_MAIN_VARIANT (type);
4465 return type;
4468 /* Return nonzero if the given type node represents a tagged type. */
4470 static inline int
4471 is_tagged_type (const_tree type)
4473 enum tree_code code = TREE_CODE (type);
4475 return (code == RECORD_TYPE || code == UNION_TYPE
4476 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4479 /* Convert a DIE tag into its string name. */
4481 static const char *
4482 dwarf_tag_name (unsigned int tag)
4484 switch (tag)
4486 case DW_TAG_padding:
4487 return "DW_TAG_padding";
4488 case DW_TAG_array_type:
4489 return "DW_TAG_array_type";
4490 case DW_TAG_class_type:
4491 return "DW_TAG_class_type";
4492 case DW_TAG_entry_point:
4493 return "DW_TAG_entry_point";
4494 case DW_TAG_enumeration_type:
4495 return "DW_TAG_enumeration_type";
4496 case DW_TAG_formal_parameter:
4497 return "DW_TAG_formal_parameter";
4498 case DW_TAG_imported_declaration:
4499 return "DW_TAG_imported_declaration";
4500 case DW_TAG_label:
4501 return "DW_TAG_label";
4502 case DW_TAG_lexical_block:
4503 return "DW_TAG_lexical_block";
4504 case DW_TAG_member:
4505 return "DW_TAG_member";
4506 case DW_TAG_pointer_type:
4507 return "DW_TAG_pointer_type";
4508 case DW_TAG_reference_type:
4509 return "DW_TAG_reference_type";
4510 case DW_TAG_compile_unit:
4511 return "DW_TAG_compile_unit";
4512 case DW_TAG_string_type:
4513 return "DW_TAG_string_type";
4514 case DW_TAG_structure_type:
4515 return "DW_TAG_structure_type";
4516 case DW_TAG_subroutine_type:
4517 return "DW_TAG_subroutine_type";
4518 case DW_TAG_typedef:
4519 return "DW_TAG_typedef";
4520 case DW_TAG_union_type:
4521 return "DW_TAG_union_type";
4522 case DW_TAG_unspecified_parameters:
4523 return "DW_TAG_unspecified_parameters";
4524 case DW_TAG_variant:
4525 return "DW_TAG_variant";
4526 case DW_TAG_common_block:
4527 return "DW_TAG_common_block";
4528 case DW_TAG_common_inclusion:
4529 return "DW_TAG_common_inclusion";
4530 case DW_TAG_inheritance:
4531 return "DW_TAG_inheritance";
4532 case DW_TAG_inlined_subroutine:
4533 return "DW_TAG_inlined_subroutine";
4534 case DW_TAG_module:
4535 return "DW_TAG_module";
4536 case DW_TAG_ptr_to_member_type:
4537 return "DW_TAG_ptr_to_member_type";
4538 case DW_TAG_set_type:
4539 return "DW_TAG_set_type";
4540 case DW_TAG_subrange_type:
4541 return "DW_TAG_subrange_type";
4542 case DW_TAG_with_stmt:
4543 return "DW_TAG_with_stmt";
4544 case DW_TAG_access_declaration:
4545 return "DW_TAG_access_declaration";
4546 case DW_TAG_base_type:
4547 return "DW_TAG_base_type";
4548 case DW_TAG_catch_block:
4549 return "DW_TAG_catch_block";
4550 case DW_TAG_const_type:
4551 return "DW_TAG_const_type";
4552 case DW_TAG_constant:
4553 return "DW_TAG_constant";
4554 case DW_TAG_enumerator:
4555 return "DW_TAG_enumerator";
4556 case DW_TAG_file_type:
4557 return "DW_TAG_file_type";
4558 case DW_TAG_friend:
4559 return "DW_TAG_friend";
4560 case DW_TAG_namelist:
4561 return "DW_TAG_namelist";
4562 case DW_TAG_namelist_item:
4563 return "DW_TAG_namelist_item";
4564 case DW_TAG_namespace:
4565 return "DW_TAG_namespace";
4566 case DW_TAG_packed_type:
4567 return "DW_TAG_packed_type";
4568 case DW_TAG_subprogram:
4569 return "DW_TAG_subprogram";
4570 case DW_TAG_template_type_param:
4571 return "DW_TAG_template_type_param";
4572 case DW_TAG_template_value_param:
4573 return "DW_TAG_template_value_param";
4574 case DW_TAG_thrown_type:
4575 return "DW_TAG_thrown_type";
4576 case DW_TAG_try_block:
4577 return "DW_TAG_try_block";
4578 case DW_TAG_variant_part:
4579 return "DW_TAG_variant_part";
4580 case DW_TAG_variable:
4581 return "DW_TAG_variable";
4582 case DW_TAG_volatile_type:
4583 return "DW_TAG_volatile_type";
4584 case DW_TAG_imported_module:
4585 return "DW_TAG_imported_module";
4586 case DW_TAG_MIPS_loop:
4587 return "DW_TAG_MIPS_loop";
4588 case DW_TAG_format_label:
4589 return "DW_TAG_format_label";
4590 case DW_TAG_function_template:
4591 return "DW_TAG_function_template";
4592 case DW_TAG_class_template:
4593 return "DW_TAG_class_template";
4594 case DW_TAG_GNU_BINCL:
4595 return "DW_TAG_GNU_BINCL";
4596 case DW_TAG_GNU_EINCL:
4597 return "DW_TAG_GNU_EINCL";
4598 default:
4599 return "DW_TAG_<unknown>";
4603 /* Convert a DWARF attribute code into its string name. */
4605 static const char *
4606 dwarf_attr_name (unsigned int attr)
4608 switch (attr)
4610 case DW_AT_sibling:
4611 return "DW_AT_sibling";
4612 case DW_AT_location:
4613 return "DW_AT_location";
4614 case DW_AT_name:
4615 return "DW_AT_name";
4616 case DW_AT_ordering:
4617 return "DW_AT_ordering";
4618 case DW_AT_subscr_data:
4619 return "DW_AT_subscr_data";
4620 case DW_AT_byte_size:
4621 return "DW_AT_byte_size";
4622 case DW_AT_bit_offset:
4623 return "DW_AT_bit_offset";
4624 case DW_AT_bit_size:
4625 return "DW_AT_bit_size";
4626 case DW_AT_element_list:
4627 return "DW_AT_element_list";
4628 case DW_AT_stmt_list:
4629 return "DW_AT_stmt_list";
4630 case DW_AT_low_pc:
4631 return "DW_AT_low_pc";
4632 case DW_AT_high_pc:
4633 return "DW_AT_high_pc";
4634 case DW_AT_language:
4635 return "DW_AT_language";
4636 case DW_AT_member:
4637 return "DW_AT_member";
4638 case DW_AT_discr:
4639 return "DW_AT_discr";
4640 case DW_AT_discr_value:
4641 return "DW_AT_discr_value";
4642 case DW_AT_visibility:
4643 return "DW_AT_visibility";
4644 case DW_AT_import:
4645 return "DW_AT_import";
4646 case DW_AT_string_length:
4647 return "DW_AT_string_length";
4648 case DW_AT_common_reference:
4649 return "DW_AT_common_reference";
4650 case DW_AT_comp_dir:
4651 return "DW_AT_comp_dir";
4652 case DW_AT_const_value:
4653 return "DW_AT_const_value";
4654 case DW_AT_containing_type:
4655 return "DW_AT_containing_type";
4656 case DW_AT_default_value:
4657 return "DW_AT_default_value";
4658 case DW_AT_inline:
4659 return "DW_AT_inline";
4660 case DW_AT_is_optional:
4661 return "DW_AT_is_optional";
4662 case DW_AT_lower_bound:
4663 return "DW_AT_lower_bound";
4664 case DW_AT_producer:
4665 return "DW_AT_producer";
4666 case DW_AT_prototyped:
4667 return "DW_AT_prototyped";
4668 case DW_AT_return_addr:
4669 return "DW_AT_return_addr";
4670 case DW_AT_start_scope:
4671 return "DW_AT_start_scope";
4672 case DW_AT_stride_size:
4673 return "DW_AT_stride_size";
4674 case DW_AT_upper_bound:
4675 return "DW_AT_upper_bound";
4676 case DW_AT_abstract_origin:
4677 return "DW_AT_abstract_origin";
4678 case DW_AT_accessibility:
4679 return "DW_AT_accessibility";
4680 case DW_AT_address_class:
4681 return "DW_AT_address_class";
4682 case DW_AT_artificial:
4683 return "DW_AT_artificial";
4684 case DW_AT_base_types:
4685 return "DW_AT_base_types";
4686 case DW_AT_calling_convention:
4687 return "DW_AT_calling_convention";
4688 case DW_AT_count:
4689 return "DW_AT_count";
4690 case DW_AT_data_member_location:
4691 return "DW_AT_data_member_location";
4692 case DW_AT_decl_column:
4693 return "DW_AT_decl_column";
4694 case DW_AT_decl_file:
4695 return "DW_AT_decl_file";
4696 case DW_AT_decl_line:
4697 return "DW_AT_decl_line";
4698 case DW_AT_declaration:
4699 return "DW_AT_declaration";
4700 case DW_AT_discr_list:
4701 return "DW_AT_discr_list";
4702 case DW_AT_encoding:
4703 return "DW_AT_encoding";
4704 case DW_AT_external:
4705 return "DW_AT_external";
4706 case DW_AT_frame_base:
4707 return "DW_AT_frame_base";
4708 case DW_AT_friend:
4709 return "DW_AT_friend";
4710 case DW_AT_identifier_case:
4711 return "DW_AT_identifier_case";
4712 case DW_AT_macro_info:
4713 return "DW_AT_macro_info";
4714 case DW_AT_namelist_items:
4715 return "DW_AT_namelist_items";
4716 case DW_AT_priority:
4717 return "DW_AT_priority";
4718 case DW_AT_segment:
4719 return "DW_AT_segment";
4720 case DW_AT_specification:
4721 return "DW_AT_specification";
4722 case DW_AT_static_link:
4723 return "DW_AT_static_link";
4724 case DW_AT_type:
4725 return "DW_AT_type";
4726 case DW_AT_use_location:
4727 return "DW_AT_use_location";
4728 case DW_AT_variable_parameter:
4729 return "DW_AT_variable_parameter";
4730 case DW_AT_virtuality:
4731 return "DW_AT_virtuality";
4732 case DW_AT_vtable_elem_location:
4733 return "DW_AT_vtable_elem_location";
4735 case DW_AT_allocated:
4736 return "DW_AT_allocated";
4737 case DW_AT_associated:
4738 return "DW_AT_associated";
4739 case DW_AT_data_location:
4740 return "DW_AT_data_location";
4741 case DW_AT_stride:
4742 return "DW_AT_stride";
4743 case DW_AT_entry_pc:
4744 return "DW_AT_entry_pc";
4745 case DW_AT_use_UTF8:
4746 return "DW_AT_use_UTF8";
4747 case DW_AT_extension:
4748 return "DW_AT_extension";
4749 case DW_AT_ranges:
4750 return "DW_AT_ranges";
4751 case DW_AT_trampoline:
4752 return "DW_AT_trampoline";
4753 case DW_AT_call_column:
4754 return "DW_AT_call_column";
4755 case DW_AT_call_file:
4756 return "DW_AT_call_file";
4757 case DW_AT_call_line:
4758 return "DW_AT_call_line";
4760 case DW_AT_MIPS_fde:
4761 return "DW_AT_MIPS_fde";
4762 case DW_AT_MIPS_loop_begin:
4763 return "DW_AT_MIPS_loop_begin";
4764 case DW_AT_MIPS_tail_loop_begin:
4765 return "DW_AT_MIPS_tail_loop_begin";
4766 case DW_AT_MIPS_epilog_begin:
4767 return "DW_AT_MIPS_epilog_begin";
4768 case DW_AT_MIPS_loop_unroll_factor:
4769 return "DW_AT_MIPS_loop_unroll_factor";
4770 case DW_AT_MIPS_software_pipeline_depth:
4771 return "DW_AT_MIPS_software_pipeline_depth";
4772 case DW_AT_MIPS_linkage_name:
4773 return "DW_AT_MIPS_linkage_name";
4774 case DW_AT_MIPS_stride:
4775 return "DW_AT_MIPS_stride";
4776 case DW_AT_MIPS_abstract_name:
4777 return "DW_AT_MIPS_abstract_name";
4778 case DW_AT_MIPS_clone_origin:
4779 return "DW_AT_MIPS_clone_origin";
4780 case DW_AT_MIPS_has_inlines:
4781 return "DW_AT_MIPS_has_inlines";
4783 case DW_AT_sf_names:
4784 return "DW_AT_sf_names";
4785 case DW_AT_src_info:
4786 return "DW_AT_src_info";
4787 case DW_AT_mac_info:
4788 return "DW_AT_mac_info";
4789 case DW_AT_src_coords:
4790 return "DW_AT_src_coords";
4791 case DW_AT_body_begin:
4792 return "DW_AT_body_begin";
4793 case DW_AT_body_end:
4794 return "DW_AT_body_end";
4795 case DW_AT_GNU_vector:
4796 return "DW_AT_GNU_vector";
4798 case DW_AT_VMS_rtnbeg_pd_address:
4799 return "DW_AT_VMS_rtnbeg_pd_address";
4801 default:
4802 return "DW_AT_<unknown>";
4806 /* Convert a DWARF value form code into its string name. */
4808 static const char *
4809 dwarf_form_name (unsigned int form)
4811 switch (form)
4813 case DW_FORM_addr:
4814 return "DW_FORM_addr";
4815 case DW_FORM_block2:
4816 return "DW_FORM_block2";
4817 case DW_FORM_block4:
4818 return "DW_FORM_block4";
4819 case DW_FORM_data2:
4820 return "DW_FORM_data2";
4821 case DW_FORM_data4:
4822 return "DW_FORM_data4";
4823 case DW_FORM_data8:
4824 return "DW_FORM_data8";
4825 case DW_FORM_string:
4826 return "DW_FORM_string";
4827 case DW_FORM_block:
4828 return "DW_FORM_block";
4829 case DW_FORM_block1:
4830 return "DW_FORM_block1";
4831 case DW_FORM_data1:
4832 return "DW_FORM_data1";
4833 case DW_FORM_flag:
4834 return "DW_FORM_flag";
4835 case DW_FORM_sdata:
4836 return "DW_FORM_sdata";
4837 case DW_FORM_strp:
4838 return "DW_FORM_strp";
4839 case DW_FORM_udata:
4840 return "DW_FORM_udata";
4841 case DW_FORM_ref_addr:
4842 return "DW_FORM_ref_addr";
4843 case DW_FORM_ref1:
4844 return "DW_FORM_ref1";
4845 case DW_FORM_ref2:
4846 return "DW_FORM_ref2";
4847 case DW_FORM_ref4:
4848 return "DW_FORM_ref4";
4849 case DW_FORM_ref8:
4850 return "DW_FORM_ref8";
4851 case DW_FORM_ref_udata:
4852 return "DW_FORM_ref_udata";
4853 case DW_FORM_indirect:
4854 return "DW_FORM_indirect";
4855 default:
4856 return "DW_FORM_<unknown>";
4860 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4861 instance of an inlined instance of a decl which is local to an inline
4862 function, so we have to trace all of the way back through the origin chain
4863 to find out what sort of node actually served as the original seed for the
4864 given block. */
4866 static tree
4867 decl_ultimate_origin (const_tree decl)
4869 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4870 return NULL_TREE;
4872 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4873 nodes in the function to point to themselves; ignore that if
4874 we're trying to output the abstract instance of this function. */
4875 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4876 return NULL_TREE;
4878 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4879 most distant ancestor, this should never happen. */
4880 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4882 return DECL_ABSTRACT_ORIGIN (decl);
4885 /* Determine the "ultimate origin" of a block. The block may be an inlined
4886 instance of an inlined instance of a block which is local to an inline
4887 function, so we have to trace all of the way back through the origin chain
4888 to find out what sort of node actually served as the original seed for the
4889 given block. */
4891 static tree
4892 block_ultimate_origin (const_tree block)
4894 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4896 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4897 nodes in the function to point to themselves; ignore that if
4898 we're trying to output the abstract instance of this function. */
4899 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4900 return NULL_TREE;
4902 if (immediate_origin == NULL_TREE)
4903 return NULL_TREE;
4904 else
4906 tree ret_val;
4907 tree lookahead = immediate_origin;
4911 ret_val = lookahead;
4912 lookahead = (TREE_CODE (ret_val) == BLOCK
4913 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4915 while (lookahead != NULL && lookahead != ret_val);
4917 /* The block's abstract origin chain may not be the *ultimate* origin of
4918 the block. It could lead to a DECL that has an abstract origin set.
4919 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4920 will give us if it has one). Note that DECL's abstract origins are
4921 supposed to be the most distant ancestor (or so decl_ultimate_origin
4922 claims), so we don't need to loop following the DECL origins. */
4923 if (DECL_P (ret_val))
4924 return DECL_ORIGIN (ret_val);
4926 return ret_val;
4930 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4931 of a virtual function may refer to a base class, so we check the 'this'
4932 parameter. */
4934 static tree
4935 decl_class_context (tree decl)
4937 tree context = NULL_TREE;
4939 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4940 context = DECL_CONTEXT (decl);
4941 else
4942 context = TYPE_MAIN_VARIANT
4943 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4945 if (context && !TYPE_P (context))
4946 context = NULL_TREE;
4948 return context;
4951 /* Add an attribute/value pair to a DIE. */
4953 static inline void
4954 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4956 /* Maybe this should be an assert? */
4957 if (die == NULL)
4958 return;
4960 if (die->die_attr == NULL)
4961 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4962 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4965 static inline enum dw_val_class
4966 AT_class (dw_attr_ref a)
4968 return a->dw_attr_val.val_class;
4971 /* Add a flag value attribute to a DIE. */
4973 static inline void
4974 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4976 dw_attr_node attr;
4978 attr.dw_attr = attr_kind;
4979 attr.dw_attr_val.val_class = dw_val_class_flag;
4980 attr.dw_attr_val.v.val_flag = flag;
4981 add_dwarf_attr (die, &attr);
4984 static inline unsigned
4985 AT_flag (dw_attr_ref a)
4987 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4988 return a->dw_attr_val.v.val_flag;
4991 /* Add a signed integer attribute value to a DIE. */
4993 static inline void
4994 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4996 dw_attr_node attr;
4998 attr.dw_attr = attr_kind;
4999 attr.dw_attr_val.val_class = dw_val_class_const;
5000 attr.dw_attr_val.v.val_int = int_val;
5001 add_dwarf_attr (die, &attr);
5004 static inline HOST_WIDE_INT
5005 AT_int (dw_attr_ref a)
5007 gcc_assert (a && AT_class (a) == dw_val_class_const);
5008 return a->dw_attr_val.v.val_int;
5011 /* Add an unsigned integer attribute value to a DIE. */
5013 static inline void
5014 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5015 unsigned HOST_WIDE_INT unsigned_val)
5017 dw_attr_node attr;
5019 attr.dw_attr = attr_kind;
5020 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5021 attr.dw_attr_val.v.val_unsigned = unsigned_val;
5022 add_dwarf_attr (die, &attr);
5025 static inline unsigned HOST_WIDE_INT
5026 AT_unsigned (dw_attr_ref a)
5028 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5029 return a->dw_attr_val.v.val_unsigned;
5032 /* Add an unsigned double integer attribute value to a DIE. */
5034 static inline void
5035 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5036 long unsigned int val_hi, long unsigned int val_low)
5038 dw_attr_node attr;
5040 attr.dw_attr = attr_kind;
5041 attr.dw_attr_val.val_class = dw_val_class_long_long;
5042 attr.dw_attr_val.v.val_long_long.hi = val_hi;
5043 attr.dw_attr_val.v.val_long_long.low = val_low;
5044 add_dwarf_attr (die, &attr);
5047 /* Add a floating point attribute value to a DIE and return it. */
5049 static inline void
5050 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5051 unsigned int length, unsigned int elt_size, unsigned char *array)
5053 dw_attr_node attr;
5055 attr.dw_attr = attr_kind;
5056 attr.dw_attr_val.val_class = dw_val_class_vec;
5057 attr.dw_attr_val.v.val_vec.length = length;
5058 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5059 attr.dw_attr_val.v.val_vec.array = array;
5060 add_dwarf_attr (die, &attr);
5063 /* Hash and equality functions for debug_str_hash. */
5065 static hashval_t
5066 debug_str_do_hash (const void *x)
5068 return htab_hash_string (((const struct indirect_string_node *)x)->str);
5071 static int
5072 debug_str_eq (const void *x1, const void *x2)
5074 return strcmp ((((const struct indirect_string_node *)x1)->str),
5075 (const char *)x2) == 0;
5078 /* Add a string attribute value to a DIE. */
5080 static inline void
5081 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5083 dw_attr_node attr;
5084 struct indirect_string_node *node;
5085 void **slot;
5087 if (! debug_str_hash)
5088 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5089 debug_str_eq, NULL);
5091 slot = htab_find_slot_with_hash (debug_str_hash, str,
5092 htab_hash_string (str), INSERT);
5093 if (*slot == NULL)
5095 node = (struct indirect_string_node *)
5096 ggc_alloc_cleared (sizeof (struct indirect_string_node));
5097 node->str = ggc_strdup (str);
5098 *slot = node;
5100 else
5101 node = (struct indirect_string_node *) *slot;
5103 node->refcount++;
5105 attr.dw_attr = attr_kind;
5106 attr.dw_attr_val.val_class = dw_val_class_str;
5107 attr.dw_attr_val.v.val_str = node;
5108 add_dwarf_attr (die, &attr);
5111 static inline const char *
5112 AT_string (dw_attr_ref a)
5114 gcc_assert (a && AT_class (a) == dw_val_class_str);
5115 return a->dw_attr_val.v.val_str->str;
5118 /* Find out whether a string should be output inline in DIE
5119 or out-of-line in .debug_str section. */
5121 static int
5122 AT_string_form (dw_attr_ref a)
5124 struct indirect_string_node *node;
5125 unsigned int len;
5126 char label[32];
5128 gcc_assert (a && AT_class (a) == dw_val_class_str);
5130 node = a->dw_attr_val.v.val_str;
5131 if (node->form)
5132 return node->form;
5134 len = strlen (node->str) + 1;
5136 /* If the string is shorter or equal to the size of the reference, it is
5137 always better to put it inline. */
5138 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5139 return node->form = DW_FORM_string;
5141 /* If we cannot expect the linker to merge strings in .debug_str
5142 section, only put it into .debug_str if it is worth even in this
5143 single module. */
5144 if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5145 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5146 return node->form = DW_FORM_string;
5148 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5149 ++dw2_string_counter;
5150 node->label = xstrdup (label);
5152 return node->form = DW_FORM_strp;
5155 /* Add a DIE reference attribute value to a DIE. */
5157 static inline void
5158 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5160 dw_attr_node attr;
5162 attr.dw_attr = attr_kind;
5163 attr.dw_attr_val.val_class = dw_val_class_die_ref;
5164 attr.dw_attr_val.v.val_die_ref.die = targ_die;
5165 attr.dw_attr_val.v.val_die_ref.external = 0;
5166 add_dwarf_attr (die, &attr);
5169 /* Add an AT_specification attribute to a DIE, and also make the back
5170 pointer from the specification to the definition. */
5172 static inline void
5173 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5175 add_AT_die_ref (die, DW_AT_specification, targ_die);
5176 gcc_assert (!targ_die->die_definition);
5177 targ_die->die_definition = die;
5180 static inline dw_die_ref
5181 AT_ref (dw_attr_ref a)
5183 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5184 return a->dw_attr_val.v.val_die_ref.die;
5187 static inline int
5188 AT_ref_external (dw_attr_ref a)
5190 if (a && AT_class (a) == dw_val_class_die_ref)
5191 return a->dw_attr_val.v.val_die_ref.external;
5193 return 0;
5196 static inline void
5197 set_AT_ref_external (dw_attr_ref a, int i)
5199 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5200 a->dw_attr_val.v.val_die_ref.external = i;
5203 /* Add an FDE reference attribute value to a DIE. */
5205 static inline void
5206 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5208 dw_attr_node attr;
5210 attr.dw_attr = attr_kind;
5211 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5212 attr.dw_attr_val.v.val_fde_index = targ_fde;
5213 add_dwarf_attr (die, &attr);
5216 /* Add a location description attribute value to a DIE. */
5218 static inline void
5219 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5221 dw_attr_node attr;
5223 attr.dw_attr = attr_kind;
5224 attr.dw_attr_val.val_class = dw_val_class_loc;
5225 attr.dw_attr_val.v.val_loc = loc;
5226 add_dwarf_attr (die, &attr);
5229 static inline dw_loc_descr_ref
5230 AT_loc (dw_attr_ref a)
5232 gcc_assert (a && AT_class (a) == dw_val_class_loc);
5233 return a->dw_attr_val.v.val_loc;
5236 static inline void
5237 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5239 dw_attr_node attr;
5241 attr.dw_attr = attr_kind;
5242 attr.dw_attr_val.val_class = dw_val_class_loc_list;
5243 attr.dw_attr_val.v.val_loc_list = loc_list;
5244 add_dwarf_attr (die, &attr);
5245 have_location_lists = true;
5248 static inline dw_loc_list_ref
5249 AT_loc_list (dw_attr_ref a)
5251 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5252 return a->dw_attr_val.v.val_loc_list;
5255 /* Add an address constant attribute value to a DIE. */
5257 static inline void
5258 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5260 dw_attr_node attr;
5262 attr.dw_attr = attr_kind;
5263 attr.dw_attr_val.val_class = dw_val_class_addr;
5264 attr.dw_attr_val.v.val_addr = addr;
5265 add_dwarf_attr (die, &attr);
5268 /* Get the RTX from to an address DIE attribute. */
5270 static inline rtx
5271 AT_addr (dw_attr_ref a)
5273 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5274 return a->dw_attr_val.v.val_addr;
5277 /* Add a file attribute value to a DIE. */
5279 static inline void
5280 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5281 struct dwarf_file_data *fd)
5283 dw_attr_node attr;
5285 attr.dw_attr = attr_kind;
5286 attr.dw_attr_val.val_class = dw_val_class_file;
5287 attr.dw_attr_val.v.val_file = fd;
5288 add_dwarf_attr (die, &attr);
5291 /* Get the dwarf_file_data from a file DIE attribute. */
5293 static inline struct dwarf_file_data *
5294 AT_file (dw_attr_ref a)
5296 gcc_assert (a && AT_class (a) == dw_val_class_file);
5297 return a->dw_attr_val.v.val_file;
5300 /* Add a label identifier attribute value to a DIE. */
5302 static inline void
5303 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5305 dw_attr_node attr;
5307 attr.dw_attr = attr_kind;
5308 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5309 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5310 add_dwarf_attr (die, &attr);
5313 /* Add a section offset attribute value to a DIE, an offset into the
5314 debug_line section. */
5316 static inline void
5317 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5318 const char *label)
5320 dw_attr_node attr;
5322 attr.dw_attr = attr_kind;
5323 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5324 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5325 add_dwarf_attr (die, &attr);
5328 /* Add a section offset attribute value to a DIE, an offset into the
5329 debug_macinfo section. */
5331 static inline void
5332 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5333 const char *label)
5335 dw_attr_node attr;
5337 attr.dw_attr = attr_kind;
5338 attr.dw_attr_val.val_class = dw_val_class_macptr;
5339 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5340 add_dwarf_attr (die, &attr);
5343 /* Add an offset attribute value to a DIE. */
5345 static inline void
5346 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5347 unsigned HOST_WIDE_INT offset)
5349 dw_attr_node attr;
5351 attr.dw_attr = attr_kind;
5352 attr.dw_attr_val.val_class = dw_val_class_offset;
5353 attr.dw_attr_val.v.val_offset = offset;
5354 add_dwarf_attr (die, &attr);
5357 /* Add an range_list attribute value to a DIE. */
5359 static void
5360 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5361 long unsigned int offset)
5363 dw_attr_node attr;
5365 attr.dw_attr = attr_kind;
5366 attr.dw_attr_val.val_class = dw_val_class_range_list;
5367 attr.dw_attr_val.v.val_offset = offset;
5368 add_dwarf_attr (die, &attr);
5371 static inline const char *
5372 AT_lbl (dw_attr_ref a)
5374 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5375 || AT_class (a) == dw_val_class_lineptr
5376 || AT_class (a) == dw_val_class_macptr));
5377 return a->dw_attr_val.v.val_lbl_id;
5380 /* Get the attribute of type attr_kind. */
5382 static dw_attr_ref
5383 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5385 dw_attr_ref a;
5386 unsigned ix;
5387 dw_die_ref spec = NULL;
5389 if (! die)
5390 return NULL;
5392 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5393 if (a->dw_attr == attr_kind)
5394 return a;
5395 else if (a->dw_attr == DW_AT_specification
5396 || a->dw_attr == DW_AT_abstract_origin)
5397 spec = AT_ref (a);
5399 if (spec)
5400 return get_AT (spec, attr_kind);
5402 return NULL;
5405 /* Return the "low pc" attribute value, typically associated with a subprogram
5406 DIE. Return null if the "low pc" attribute is either not present, or if it
5407 cannot be represented as an assembler label identifier. */
5409 static inline const char *
5410 get_AT_low_pc (dw_die_ref die)
5412 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5414 return a ? AT_lbl (a) : NULL;
5417 /* Return the "high pc" attribute value, typically associated with a subprogram
5418 DIE. Return null if the "high pc" attribute is either not present, or if it
5419 cannot be represented as an assembler label identifier. */
5421 static inline const char *
5422 get_AT_hi_pc (dw_die_ref die)
5424 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5426 return a ? AT_lbl (a) : NULL;
5429 /* Return the value of the string attribute designated by ATTR_KIND, or
5430 NULL if it is not present. */
5432 static inline const char *
5433 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5435 dw_attr_ref a = get_AT (die, attr_kind);
5437 return a ? AT_string (a) : NULL;
5440 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5441 if it is not present. */
5443 static inline int
5444 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5446 dw_attr_ref a = get_AT (die, attr_kind);
5448 return a ? AT_flag (a) : 0;
5451 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5452 if it is not present. */
5454 static inline unsigned
5455 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5457 dw_attr_ref a = get_AT (die, attr_kind);
5459 return a ? AT_unsigned (a) : 0;
5462 static inline dw_die_ref
5463 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5465 dw_attr_ref a = get_AT (die, attr_kind);
5467 return a ? AT_ref (a) : NULL;
5470 static inline struct dwarf_file_data *
5471 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5473 dw_attr_ref a = get_AT (die, attr_kind);
5475 return a ? AT_file (a) : NULL;
5478 /* Return TRUE if the language is C or C++. */
5480 static inline bool
5481 is_c_family (void)
5483 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5485 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5486 || lang == DW_LANG_C99
5487 || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5490 /* Return TRUE if the language is C++. */
5492 static inline bool
5493 is_cxx (void)
5495 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5497 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5500 /* Return TRUE if the language is Fortran. */
5502 static inline bool
5503 is_fortran (void)
5505 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5507 return (lang == DW_LANG_Fortran77
5508 || lang == DW_LANG_Fortran90
5509 || lang == DW_LANG_Fortran95);
5512 /* Return TRUE if the language is Java. */
5514 static inline bool
5515 is_java (void)
5517 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5519 return lang == DW_LANG_Java;
5522 /* Return TRUE if the language is Ada. */
5524 static inline bool
5525 is_ada (void)
5527 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5529 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5532 /* Remove the specified attribute if present. */
5534 static void
5535 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5537 dw_attr_ref a;
5538 unsigned ix;
5540 if (! die)
5541 return;
5543 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5544 if (a->dw_attr == attr_kind)
5546 if (AT_class (a) == dw_val_class_str)
5547 if (a->dw_attr_val.v.val_str->refcount)
5548 a->dw_attr_val.v.val_str->refcount--;
5550 /* VEC_ordered_remove should help reduce the number of abbrevs
5551 that are needed. */
5552 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5553 return;
5557 /* Remove CHILD from its parent. PREV must have the property that
5558 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5560 static void
5561 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5563 gcc_assert (child->die_parent == prev->die_parent);
5564 gcc_assert (prev->die_sib == child);
5565 if (prev == child)
5567 gcc_assert (child->die_parent->die_child == child);
5568 prev = NULL;
5570 else
5571 prev->die_sib = child->die_sib;
5572 if (child->die_parent->die_child == child)
5573 child->die_parent->die_child = prev;
5576 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5577 matches TAG. */
5579 static void
5580 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5582 dw_die_ref c;
5584 c = die->die_child;
5585 if (c) do {
5586 dw_die_ref prev = c;
5587 c = c->die_sib;
5588 while (c->die_tag == tag)
5590 remove_child_with_prev (c, prev);
5591 /* Might have removed every child. */
5592 if (c == c->die_sib)
5593 return;
5594 c = c->die_sib;
5596 } while (c != die->die_child);
5599 /* Add a CHILD_DIE as the last child of DIE. */
5601 static void
5602 add_child_die (dw_die_ref die, dw_die_ref child_die)
5604 /* FIXME this should probably be an assert. */
5605 if (! die || ! child_die)
5606 return;
5607 gcc_assert (die != child_die);
5609 child_die->die_parent = die;
5610 if (die->die_child)
5612 child_die->die_sib = die->die_child->die_sib;
5613 die->die_child->die_sib = child_die;
5615 else
5616 child_die->die_sib = child_die;
5617 die->die_child = child_die;
5620 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5621 is the specification, to the end of PARENT's list of children.
5622 This is done by removing and re-adding it. */
5624 static void
5625 splice_child_die (dw_die_ref parent, dw_die_ref child)
5627 dw_die_ref p;
5629 /* We want the declaration DIE from inside the class, not the
5630 specification DIE at toplevel. */
5631 if (child->die_parent != parent)
5633 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5635 if (tmp)
5636 child = tmp;
5639 gcc_assert (child->die_parent == parent
5640 || (child->die_parent
5641 == get_AT_ref (parent, DW_AT_specification)));
5643 for (p = child->die_parent->die_child; ; p = p->die_sib)
5644 if (p->die_sib == child)
5646 remove_child_with_prev (child, p);
5647 break;
5650 add_child_die (parent, child);
5653 /* Return a pointer to a newly created DIE node. */
5655 static inline dw_die_ref
5656 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5658 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5660 die->die_tag = tag_value;
5662 if (parent_die != NULL)
5663 add_child_die (parent_die, die);
5664 else
5666 limbo_die_node *limbo_node;
5668 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5669 limbo_node->die = die;
5670 limbo_node->created_for = t;
5671 limbo_node->next = limbo_die_list;
5672 limbo_die_list = limbo_node;
5675 return die;
5678 /* Return the DIE associated with the given type specifier. */
5680 static inline dw_die_ref
5681 lookup_type_die (tree type)
5683 return TYPE_SYMTAB_DIE (type);
5686 /* Equate a DIE to a given type specifier. */
5688 static inline void
5689 equate_type_number_to_die (tree type, dw_die_ref type_die)
5691 TYPE_SYMTAB_DIE (type) = type_die;
5694 /* Returns a hash value for X (which really is a die_struct). */
5696 static hashval_t
5697 decl_die_table_hash (const void *x)
5699 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5702 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5704 static int
5705 decl_die_table_eq (const void *x, const void *y)
5707 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5710 /* Return the DIE associated with a given declaration. */
5712 static inline dw_die_ref
5713 lookup_decl_die (tree decl)
5715 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5718 /* Returns a hash value for X (which really is a var_loc_list). */
5720 static hashval_t
5721 decl_loc_table_hash (const void *x)
5723 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5726 /* Return nonzero if decl_id of var_loc_list X is the same as
5727 UID of decl *Y. */
5729 static int
5730 decl_loc_table_eq (const void *x, const void *y)
5732 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5735 /* Return the var_loc list associated with a given declaration. */
5737 static inline var_loc_list *
5738 lookup_decl_loc (const_tree decl)
5740 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5743 /* Equate a DIE to a particular declaration. */
5745 static void
5746 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5748 unsigned int decl_id = DECL_UID (decl);
5749 void **slot;
5751 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5752 *slot = decl_die;
5753 decl_die->decl_id = decl_id;
5756 /* Add a variable location node to the linked list for DECL. */
5758 static void
5759 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5761 unsigned int decl_id = DECL_UID (decl);
5762 var_loc_list *temp;
5763 void **slot;
5765 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5766 if (*slot == NULL)
5768 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5769 temp->decl_id = decl_id;
5770 *slot = temp;
5772 else
5773 temp = *slot;
5775 if (temp->last)
5777 /* If the current location is the same as the end of the list,
5778 and either both or neither of the locations is uninitialized,
5779 we have nothing to do. */
5780 if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5781 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5782 || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5783 != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
5784 && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5785 == VAR_INIT_STATUS_UNINITIALIZED)
5786 || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
5787 == VAR_INIT_STATUS_UNINITIALIZED))))
5789 /* Add LOC to the end of list and update LAST. */
5790 temp->last->next = loc;
5791 temp->last = loc;
5794 /* Do not add empty location to the beginning of the list. */
5795 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5797 temp->first = loc;
5798 temp->last = loc;
5802 /* Keep track of the number of spaces used to indent the
5803 output of the debugging routines that print the structure of
5804 the DIE internal representation. */
5805 static int print_indent;
5807 /* Indent the line the number of spaces given by print_indent. */
5809 static inline void
5810 print_spaces (FILE *outfile)
5812 fprintf (outfile, "%*s", print_indent, "");
5815 /* Print the information associated with a given DIE, and its children.
5816 This routine is a debugging aid only. */
5818 static void
5819 print_die (dw_die_ref die, FILE *outfile)
5821 dw_attr_ref a;
5822 dw_die_ref c;
5823 unsigned ix;
5825 print_spaces (outfile);
5826 fprintf (outfile, "DIE %4ld: %s\n",
5827 die->die_offset, dwarf_tag_name (die->die_tag));
5828 print_spaces (outfile);
5829 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5830 fprintf (outfile, " offset: %ld\n", die->die_offset);
5832 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5834 print_spaces (outfile);
5835 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5837 switch (AT_class (a))
5839 case dw_val_class_addr:
5840 fprintf (outfile, "address");
5841 break;
5842 case dw_val_class_offset:
5843 fprintf (outfile, "offset");
5844 break;
5845 case dw_val_class_loc:
5846 fprintf (outfile, "location descriptor");
5847 break;
5848 case dw_val_class_loc_list:
5849 fprintf (outfile, "location list -> label:%s",
5850 AT_loc_list (a)->ll_symbol);
5851 break;
5852 case dw_val_class_range_list:
5853 fprintf (outfile, "range list");
5854 break;
5855 case dw_val_class_const:
5856 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5857 break;
5858 case dw_val_class_unsigned_const:
5859 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5860 break;
5861 case dw_val_class_long_long:
5862 fprintf (outfile, "constant (%lu,%lu)",
5863 a->dw_attr_val.v.val_long_long.hi,
5864 a->dw_attr_val.v.val_long_long.low);
5865 break;
5866 case dw_val_class_vec:
5867 fprintf (outfile, "floating-point or vector constant");
5868 break;
5869 case dw_val_class_flag:
5870 fprintf (outfile, "%u", AT_flag (a));
5871 break;
5872 case dw_val_class_die_ref:
5873 if (AT_ref (a) != NULL)
5875 if (AT_ref (a)->die_symbol)
5876 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5877 else
5878 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5880 else
5881 fprintf (outfile, "die -> <null>");
5882 break;
5883 case dw_val_class_lbl_id:
5884 case dw_val_class_lineptr:
5885 case dw_val_class_macptr:
5886 fprintf (outfile, "label: %s", AT_lbl (a));
5887 break;
5888 case dw_val_class_str:
5889 if (AT_string (a) != NULL)
5890 fprintf (outfile, "\"%s\"", AT_string (a));
5891 else
5892 fprintf (outfile, "<null>");
5893 break;
5894 case dw_val_class_file:
5895 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5896 AT_file (a)->emitted_number);
5897 break;
5898 default:
5899 break;
5902 fprintf (outfile, "\n");
5905 if (die->die_child != NULL)
5907 print_indent += 4;
5908 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5909 print_indent -= 4;
5911 if (print_indent == 0)
5912 fprintf (outfile, "\n");
5915 /* Print the contents of the source code line number correspondence table.
5916 This routine is a debugging aid only. */
5918 static void
5919 print_dwarf_line_table (FILE *outfile)
5921 unsigned i;
5922 dw_line_info_ref line_info;
5924 fprintf (outfile, "\n\nDWARF source line information\n");
5925 for (i = 1; i < line_info_table_in_use; i++)
5927 line_info = &line_info_table[i];
5928 fprintf (outfile, "%5d: %4ld %6ld\n", i,
5929 line_info->dw_file_num,
5930 line_info->dw_line_num);
5933 fprintf (outfile, "\n\n");
5936 /* Print the information collected for a given DIE. */
5938 void
5939 debug_dwarf_die (dw_die_ref die)
5941 print_die (die, stderr);
5944 /* Print all DWARF information collected for the compilation unit.
5945 This routine is a debugging aid only. */
5947 void
5948 debug_dwarf (void)
5950 print_indent = 0;
5951 print_die (comp_unit_die, stderr);
5952 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5953 print_dwarf_line_table (stderr);
5956 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5957 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5958 DIE that marks the start of the DIEs for this include file. */
5960 static dw_die_ref
5961 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5963 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5964 dw_die_ref new_unit = gen_compile_unit_die (filename);
5966 new_unit->die_sib = old_unit;
5967 return new_unit;
5970 /* Close an include-file CU and reopen the enclosing one. */
5972 static dw_die_ref
5973 pop_compile_unit (dw_die_ref old_unit)
5975 dw_die_ref new_unit = old_unit->die_sib;
5977 old_unit->die_sib = NULL;
5978 return new_unit;
5981 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5982 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5984 /* Calculate the checksum of a location expression. */
5986 static inline void
5987 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5989 CHECKSUM (loc->dw_loc_opc);
5990 CHECKSUM (loc->dw_loc_oprnd1);
5991 CHECKSUM (loc->dw_loc_oprnd2);
5994 /* Calculate the checksum of an attribute. */
5996 static void
5997 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5999 dw_loc_descr_ref loc;
6000 rtx r;
6002 CHECKSUM (at->dw_attr);
6004 /* We don't care that this was compiled with a different compiler
6005 snapshot; if the output is the same, that's what matters. */
6006 if (at->dw_attr == DW_AT_producer)
6007 return;
6009 switch (AT_class (at))
6011 case dw_val_class_const:
6012 CHECKSUM (at->dw_attr_val.v.val_int);
6013 break;
6014 case dw_val_class_unsigned_const:
6015 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6016 break;
6017 case dw_val_class_long_long:
6018 CHECKSUM (at->dw_attr_val.v.val_long_long);
6019 break;
6020 case dw_val_class_vec:
6021 CHECKSUM (at->dw_attr_val.v.val_vec);
6022 break;
6023 case dw_val_class_flag:
6024 CHECKSUM (at->dw_attr_val.v.val_flag);
6025 break;
6026 case dw_val_class_str:
6027 CHECKSUM_STRING (AT_string (at));
6028 break;
6030 case dw_val_class_addr:
6031 r = AT_addr (at);
6032 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6033 CHECKSUM_STRING (XSTR (r, 0));
6034 break;
6036 case dw_val_class_offset:
6037 CHECKSUM (at->dw_attr_val.v.val_offset);
6038 break;
6040 case dw_val_class_loc:
6041 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6042 loc_checksum (loc, ctx);
6043 break;
6045 case dw_val_class_die_ref:
6046 die_checksum (AT_ref (at), ctx, mark);
6047 break;
6049 case dw_val_class_fde_ref:
6050 case dw_val_class_lbl_id:
6051 case dw_val_class_lineptr:
6052 case dw_val_class_macptr:
6053 break;
6055 case dw_val_class_file:
6056 CHECKSUM_STRING (AT_file (at)->filename);
6057 break;
6059 default:
6060 break;
6064 /* Calculate the checksum of a DIE. */
6066 static void
6067 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6069 dw_die_ref c;
6070 dw_attr_ref a;
6071 unsigned ix;
6073 /* To avoid infinite recursion. */
6074 if (die->die_mark)
6076 CHECKSUM (die->die_mark);
6077 return;
6079 die->die_mark = ++(*mark);
6081 CHECKSUM (die->die_tag);
6083 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6084 attr_checksum (a, ctx, mark);
6086 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6089 #undef CHECKSUM
6090 #undef CHECKSUM_STRING
6092 /* Do the location expressions look same? */
6093 static inline int
6094 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6096 return loc1->dw_loc_opc == loc2->dw_loc_opc
6097 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6098 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6101 /* Do the values look the same? */
6102 static int
6103 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6105 dw_loc_descr_ref loc1, loc2;
6106 rtx r1, r2;
6108 if (v1->val_class != v2->val_class)
6109 return 0;
6111 switch (v1->val_class)
6113 case dw_val_class_const:
6114 return v1->v.val_int == v2->v.val_int;
6115 case dw_val_class_unsigned_const:
6116 return v1->v.val_unsigned == v2->v.val_unsigned;
6117 case dw_val_class_long_long:
6118 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6119 && v1->v.val_long_long.low == v2->v.val_long_long.low;
6120 case dw_val_class_vec:
6121 if (v1->v.val_vec.length != v2->v.val_vec.length
6122 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6123 return 0;
6124 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6125 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6126 return 0;
6127 return 1;
6128 case dw_val_class_flag:
6129 return v1->v.val_flag == v2->v.val_flag;
6130 case dw_val_class_str:
6131 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6133 case dw_val_class_addr:
6134 r1 = v1->v.val_addr;
6135 r2 = v2->v.val_addr;
6136 if (GET_CODE (r1) != GET_CODE (r2))
6137 return 0;
6138 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6139 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6141 case dw_val_class_offset:
6142 return v1->v.val_offset == v2->v.val_offset;
6144 case dw_val_class_loc:
6145 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6146 loc1 && loc2;
6147 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6148 if (!same_loc_p (loc1, loc2, mark))
6149 return 0;
6150 return !loc1 && !loc2;
6152 case dw_val_class_die_ref:
6153 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6155 case dw_val_class_fde_ref:
6156 case dw_val_class_lbl_id:
6157 case dw_val_class_lineptr:
6158 case dw_val_class_macptr:
6159 return 1;
6161 case dw_val_class_file:
6162 return v1->v.val_file == v2->v.val_file;
6164 default:
6165 return 1;
6169 /* Do the attributes look the same? */
6171 static int
6172 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6174 if (at1->dw_attr != at2->dw_attr)
6175 return 0;
6177 /* We don't care that this was compiled with a different compiler
6178 snapshot; if the output is the same, that's what matters. */
6179 if (at1->dw_attr == DW_AT_producer)
6180 return 1;
6182 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6185 /* Do the dies look the same? */
6187 static int
6188 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6190 dw_die_ref c1, c2;
6191 dw_attr_ref a1;
6192 unsigned ix;
6194 /* To avoid infinite recursion. */
6195 if (die1->die_mark)
6196 return die1->die_mark == die2->die_mark;
6197 die1->die_mark = die2->die_mark = ++(*mark);
6199 if (die1->die_tag != die2->die_tag)
6200 return 0;
6202 if (VEC_length (dw_attr_node, die1->die_attr)
6203 != VEC_length (dw_attr_node, die2->die_attr))
6204 return 0;
6206 for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6207 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6208 return 0;
6210 c1 = die1->die_child;
6211 c2 = die2->die_child;
6212 if (! c1)
6214 if (c2)
6215 return 0;
6217 else
6218 for (;;)
6220 if (!same_die_p (c1, c2, mark))
6221 return 0;
6222 c1 = c1->die_sib;
6223 c2 = c2->die_sib;
6224 if (c1 == die1->die_child)
6226 if (c2 == die2->die_child)
6227 break;
6228 else
6229 return 0;
6233 return 1;
6236 /* Do the dies look the same? Wrapper around same_die_p. */
6238 static int
6239 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6241 int mark = 0;
6242 int ret = same_die_p (die1, die2, &mark);
6244 unmark_all_dies (die1);
6245 unmark_all_dies (die2);
6247 return ret;
6250 /* The prefix to attach to symbols on DIEs in the current comdat debug
6251 info section. */
6252 static char *comdat_symbol_id;
6254 /* The index of the current symbol within the current comdat CU. */
6255 static unsigned int comdat_symbol_number;
6257 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6258 children, and set comdat_symbol_id accordingly. */
6260 static void
6261 compute_section_prefix (dw_die_ref unit_die)
6263 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6264 const char *base = die_name ? lbasename (die_name) : "anonymous";
6265 char *name = alloca (strlen (base) + 64);
6266 char *p;
6267 int i, mark;
6268 unsigned char checksum[16];
6269 struct md5_ctx ctx;
6271 /* Compute the checksum of the DIE, then append part of it as hex digits to
6272 the name filename of the unit. */
6274 md5_init_ctx (&ctx);
6275 mark = 0;
6276 die_checksum (unit_die, &ctx, &mark);
6277 unmark_all_dies (unit_die);
6278 md5_finish_ctx (&ctx, checksum);
6280 sprintf (name, "%s.", base);
6281 clean_symbol_name (name);
6283 p = name + strlen (name);
6284 for (i = 0; i < 4; i++)
6286 sprintf (p, "%.2x", checksum[i]);
6287 p += 2;
6290 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6291 comdat_symbol_number = 0;
6294 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6296 static int
6297 is_type_die (dw_die_ref die)
6299 switch (die->die_tag)
6301 case DW_TAG_array_type:
6302 case DW_TAG_class_type:
6303 case DW_TAG_enumeration_type:
6304 case DW_TAG_pointer_type:
6305 case DW_TAG_reference_type:
6306 case DW_TAG_string_type:
6307 case DW_TAG_structure_type:
6308 case DW_TAG_subroutine_type:
6309 case DW_TAG_union_type:
6310 case DW_TAG_ptr_to_member_type:
6311 case DW_TAG_set_type:
6312 case DW_TAG_subrange_type:
6313 case DW_TAG_base_type:
6314 case DW_TAG_const_type:
6315 case DW_TAG_file_type:
6316 case DW_TAG_packed_type:
6317 case DW_TAG_volatile_type:
6318 case DW_TAG_typedef:
6319 return 1;
6320 default:
6321 return 0;
6325 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6326 Basically, we want to choose the bits that are likely to be shared between
6327 compilations (types) and leave out the bits that are specific to individual
6328 compilations (functions). */
6330 static int
6331 is_comdat_die (dw_die_ref c)
6333 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6334 we do for stabs. The advantage is a greater likelihood of sharing between
6335 objects that don't include headers in the same order (and therefore would
6336 put the base types in a different comdat). jason 8/28/00 */
6338 if (c->die_tag == DW_TAG_base_type)
6339 return 0;
6341 if (c->die_tag == DW_TAG_pointer_type
6342 || c->die_tag == DW_TAG_reference_type
6343 || c->die_tag == DW_TAG_const_type
6344 || c->die_tag == DW_TAG_volatile_type)
6346 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6348 return t ? is_comdat_die (t) : 0;
6351 return is_type_die (c);
6354 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6355 compilation unit. */
6357 static int
6358 is_symbol_die (dw_die_ref c)
6360 return (is_type_die (c)
6361 || (get_AT (c, DW_AT_declaration)
6362 && !get_AT (c, DW_AT_specification))
6363 || c->die_tag == DW_TAG_namespace);
6366 static char *
6367 gen_internal_sym (const char *prefix)
6369 char buf[256];
6371 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6372 return xstrdup (buf);
6375 /* Assign symbols to all worthy DIEs under DIE. */
6377 static void
6378 assign_symbol_names (dw_die_ref die)
6380 dw_die_ref c;
6382 if (is_symbol_die (die))
6384 if (comdat_symbol_id)
6386 char *p = alloca (strlen (comdat_symbol_id) + 64);
6388 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6389 comdat_symbol_id, comdat_symbol_number++);
6390 die->die_symbol = xstrdup (p);
6392 else
6393 die->die_symbol = gen_internal_sym ("LDIE");
6396 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6399 struct cu_hash_table_entry
6401 dw_die_ref cu;
6402 unsigned min_comdat_num, max_comdat_num;
6403 struct cu_hash_table_entry *next;
6406 /* Routines to manipulate hash table of CUs. */
6407 static hashval_t
6408 htab_cu_hash (const void *of)
6410 const struct cu_hash_table_entry *entry = of;
6412 return htab_hash_string (entry->cu->die_symbol);
6415 static int
6416 htab_cu_eq (const void *of1, const void *of2)
6418 const struct cu_hash_table_entry *entry1 = of1;
6419 const struct die_struct *entry2 = of2;
6421 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6424 static void
6425 htab_cu_del (void *what)
6427 struct cu_hash_table_entry *next, *entry = what;
6429 while (entry)
6431 next = entry->next;
6432 free (entry);
6433 entry = next;
6437 /* Check whether we have already seen this CU and set up SYM_NUM
6438 accordingly. */
6439 static int
6440 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6442 struct cu_hash_table_entry dummy;
6443 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6445 dummy.max_comdat_num = 0;
6447 slot = (struct cu_hash_table_entry **)
6448 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6449 INSERT);
6450 entry = *slot;
6452 for (; entry; last = entry, entry = entry->next)
6454 if (same_die_p_wrap (cu, entry->cu))
6455 break;
6458 if (entry)
6460 *sym_num = entry->min_comdat_num;
6461 return 1;
6464 entry = XCNEW (struct cu_hash_table_entry);
6465 entry->cu = cu;
6466 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6467 entry->next = *slot;
6468 *slot = entry;
6470 return 0;
6473 /* Record SYM_NUM to record of CU in HTABLE. */
6474 static void
6475 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6477 struct cu_hash_table_entry **slot, *entry;
6479 slot = (struct cu_hash_table_entry **)
6480 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6481 NO_INSERT);
6482 entry = *slot;
6484 entry->max_comdat_num = sym_num;
6487 /* Traverse the DIE (which is always comp_unit_die), and set up
6488 additional compilation units for each of the include files we see
6489 bracketed by BINCL/EINCL. */
6491 static void
6492 break_out_includes (dw_die_ref die)
6494 dw_die_ref c;
6495 dw_die_ref unit = NULL;
6496 limbo_die_node *node, **pnode;
6497 htab_t cu_hash_table;
6499 c = die->die_child;
6500 if (c) do {
6501 dw_die_ref prev = c;
6502 c = c->die_sib;
6503 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6504 || (unit && is_comdat_die (c)))
6506 dw_die_ref next = c->die_sib;
6508 /* This DIE is for a secondary CU; remove it from the main one. */
6509 remove_child_with_prev (c, prev);
6511 if (c->die_tag == DW_TAG_GNU_BINCL)
6512 unit = push_new_compile_unit (unit, c);
6513 else if (c->die_tag == DW_TAG_GNU_EINCL)
6514 unit = pop_compile_unit (unit);
6515 else
6516 add_child_die (unit, c);
6517 c = next;
6518 if (c == die->die_child)
6519 break;
6521 } while (c != die->die_child);
6523 #if 0
6524 /* We can only use this in debugging, since the frontend doesn't check
6525 to make sure that we leave every include file we enter. */
6526 gcc_assert (!unit);
6527 #endif
6529 assign_symbol_names (die);
6530 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6531 for (node = limbo_die_list, pnode = &limbo_die_list;
6532 node;
6533 node = node->next)
6535 int is_dupl;
6537 compute_section_prefix (node->die);
6538 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6539 &comdat_symbol_number);
6540 assign_symbol_names (node->die);
6541 if (is_dupl)
6542 *pnode = node->next;
6543 else
6545 pnode = &node->next;
6546 record_comdat_symbol_number (node->die, cu_hash_table,
6547 comdat_symbol_number);
6550 htab_delete (cu_hash_table);
6553 /* Traverse the DIE and add a sibling attribute if it may have the
6554 effect of speeding up access to siblings. To save some space,
6555 avoid generating sibling attributes for DIE's without children. */
6557 static void
6558 add_sibling_attributes (dw_die_ref die)
6560 dw_die_ref c;
6562 if (! die->die_child)
6563 return;
6565 if (die->die_parent && die != die->die_parent->die_child)
6566 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6568 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6571 /* Output all location lists for the DIE and its children. */
6573 static void
6574 output_location_lists (dw_die_ref die)
6576 dw_die_ref c;
6577 dw_attr_ref a;
6578 unsigned ix;
6580 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6581 if (AT_class (a) == dw_val_class_loc_list)
6582 output_loc_list (AT_loc_list (a));
6584 FOR_EACH_CHILD (die, c, output_location_lists (c));
6587 /* The format of each DIE (and its attribute value pairs) is encoded in an
6588 abbreviation table. This routine builds the abbreviation table and assigns
6589 a unique abbreviation id for each abbreviation entry. The children of each
6590 die are visited recursively. */
6592 static void
6593 build_abbrev_table (dw_die_ref die)
6595 unsigned long abbrev_id;
6596 unsigned int n_alloc;
6597 dw_die_ref c;
6598 dw_attr_ref a;
6599 unsigned ix;
6601 /* Scan the DIE references, and mark as external any that refer to
6602 DIEs from other CUs (i.e. those which are not marked). */
6603 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6604 if (AT_class (a) == dw_val_class_die_ref
6605 && AT_ref (a)->die_mark == 0)
6607 gcc_assert (AT_ref (a)->die_symbol);
6609 set_AT_ref_external (a, 1);
6612 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6614 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6615 dw_attr_ref die_a, abbrev_a;
6616 unsigned ix;
6617 bool ok = true;
6619 if (abbrev->die_tag != die->die_tag)
6620 continue;
6621 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6622 continue;
6624 if (VEC_length (dw_attr_node, abbrev->die_attr)
6625 != VEC_length (dw_attr_node, die->die_attr))
6626 continue;
6628 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6630 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6631 if ((abbrev_a->dw_attr != die_a->dw_attr)
6632 || (value_format (abbrev_a) != value_format (die_a)))
6634 ok = false;
6635 break;
6638 if (ok)
6639 break;
6642 if (abbrev_id >= abbrev_die_table_in_use)
6644 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6646 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6647 abbrev_die_table = ggc_realloc (abbrev_die_table,
6648 sizeof (dw_die_ref) * n_alloc);
6650 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6651 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6652 abbrev_die_table_allocated = n_alloc;
6655 ++abbrev_die_table_in_use;
6656 abbrev_die_table[abbrev_id] = die;
6659 die->die_abbrev = abbrev_id;
6660 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6663 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6665 static int
6666 constant_size (long unsigned int value)
6668 int log;
6670 if (value == 0)
6671 log = 0;
6672 else
6673 log = floor_log2 (value);
6675 log = log / 8;
6676 log = 1 << (floor_log2 (log) + 1);
6678 return log;
6681 /* Return the size of a DIE as it is represented in the
6682 .debug_info section. */
6684 static unsigned long
6685 size_of_die (dw_die_ref die)
6687 unsigned long size = 0;
6688 dw_attr_ref a;
6689 unsigned ix;
6691 size += size_of_uleb128 (die->die_abbrev);
6692 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6694 switch (AT_class (a))
6696 case dw_val_class_addr:
6697 size += DWARF2_ADDR_SIZE;
6698 break;
6699 case dw_val_class_offset:
6700 size += DWARF_OFFSET_SIZE;
6701 break;
6702 case dw_val_class_loc:
6704 unsigned long lsize = size_of_locs (AT_loc (a));
6706 /* Block length. */
6707 size += constant_size (lsize);
6708 size += lsize;
6710 break;
6711 case dw_val_class_loc_list:
6712 size += DWARF_OFFSET_SIZE;
6713 break;
6714 case dw_val_class_range_list:
6715 size += DWARF_OFFSET_SIZE;
6716 break;
6717 case dw_val_class_const:
6718 size += size_of_sleb128 (AT_int (a));
6719 break;
6720 case dw_val_class_unsigned_const:
6721 size += constant_size (AT_unsigned (a));
6722 break;
6723 case dw_val_class_long_long:
6724 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6725 break;
6726 case dw_val_class_vec:
6727 size += 1 + (a->dw_attr_val.v.val_vec.length
6728 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6729 break;
6730 case dw_val_class_flag:
6731 size += 1;
6732 break;
6733 case dw_val_class_die_ref:
6734 if (AT_ref_external (a))
6735 size += DWARF2_ADDR_SIZE;
6736 else
6737 size += DWARF_OFFSET_SIZE;
6738 break;
6739 case dw_val_class_fde_ref:
6740 size += DWARF_OFFSET_SIZE;
6741 break;
6742 case dw_val_class_lbl_id:
6743 size += DWARF2_ADDR_SIZE;
6744 break;
6745 case dw_val_class_lineptr:
6746 case dw_val_class_macptr:
6747 size += DWARF_OFFSET_SIZE;
6748 break;
6749 case dw_val_class_str:
6750 if (AT_string_form (a) == DW_FORM_strp)
6751 size += DWARF_OFFSET_SIZE;
6752 else
6753 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6754 break;
6755 case dw_val_class_file:
6756 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6757 break;
6758 default:
6759 gcc_unreachable ();
6763 return size;
6766 /* Size the debugging information associated with a given DIE. Visits the
6767 DIE's children recursively. Updates the global variable next_die_offset, on
6768 each time through. Uses the current value of next_die_offset to update the
6769 die_offset field in each DIE. */
6771 static void
6772 calc_die_sizes (dw_die_ref die)
6774 dw_die_ref c;
6776 die->die_offset = next_die_offset;
6777 next_die_offset += size_of_die (die);
6779 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6781 if (die->die_child != NULL)
6782 /* Count the null byte used to terminate sibling lists. */
6783 next_die_offset += 1;
6786 /* Set the marks for a die and its children. We do this so
6787 that we know whether or not a reference needs to use FORM_ref_addr; only
6788 DIEs in the same CU will be marked. We used to clear out the offset
6789 and use that as the flag, but ran into ordering problems. */
6791 static void
6792 mark_dies (dw_die_ref die)
6794 dw_die_ref c;
6796 gcc_assert (!die->die_mark);
6798 die->die_mark = 1;
6799 FOR_EACH_CHILD (die, c, mark_dies (c));
6802 /* Clear the marks for a die and its children. */
6804 static void
6805 unmark_dies (dw_die_ref die)
6807 dw_die_ref c;
6809 gcc_assert (die->die_mark);
6811 die->die_mark = 0;
6812 FOR_EACH_CHILD (die, c, unmark_dies (c));
6815 /* Clear the marks for a die, its children and referred dies. */
6817 static void
6818 unmark_all_dies (dw_die_ref die)
6820 dw_die_ref c;
6821 dw_attr_ref a;
6822 unsigned ix;
6824 if (!die->die_mark)
6825 return;
6826 die->die_mark = 0;
6828 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6830 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6831 if (AT_class (a) == dw_val_class_die_ref)
6832 unmark_all_dies (AT_ref (a));
6835 /* Return the size of the .debug_pubnames or .debug_pubtypes table
6836 generated for the compilation unit. */
6838 static unsigned long
6839 size_of_pubnames (VEC (pubname_entry, gc) * names)
6841 unsigned long size;
6842 unsigned i;
6843 pubname_ref p;
6845 size = DWARF_PUBNAMES_HEADER_SIZE;
6846 for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6847 if (names != pubtype_table
6848 || p->die->die_offset != 0
6849 || !flag_eliminate_unused_debug_types)
6850 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6852 size += DWARF_OFFSET_SIZE;
6853 return size;
6856 /* Return the size of the information in the .debug_aranges section. */
6858 static unsigned long
6859 size_of_aranges (void)
6861 unsigned long size;
6863 size = DWARF_ARANGES_HEADER_SIZE;
6865 /* Count the address/length pair for this compilation unit. */
6866 if (text_section_used)
6867 size += 2 * DWARF2_ADDR_SIZE;
6868 if (cold_text_section_used)
6869 size += 2 * DWARF2_ADDR_SIZE;
6870 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6872 /* Count the two zero words used to terminated the address range table. */
6873 size += 2 * DWARF2_ADDR_SIZE;
6874 return size;
6877 /* Select the encoding of an attribute value. */
6879 static enum dwarf_form
6880 value_format (dw_attr_ref a)
6882 switch (a->dw_attr_val.val_class)
6884 case dw_val_class_addr:
6885 return DW_FORM_addr;
6886 case dw_val_class_range_list:
6887 case dw_val_class_offset:
6888 case dw_val_class_loc_list:
6889 switch (DWARF_OFFSET_SIZE)
6891 case 4:
6892 return DW_FORM_data4;
6893 case 8:
6894 return DW_FORM_data8;
6895 default:
6896 gcc_unreachable ();
6898 case dw_val_class_loc:
6899 switch (constant_size (size_of_locs (AT_loc (a))))
6901 case 1:
6902 return DW_FORM_block1;
6903 case 2:
6904 return DW_FORM_block2;
6905 default:
6906 gcc_unreachable ();
6908 case dw_val_class_const:
6909 return DW_FORM_sdata;
6910 case dw_val_class_unsigned_const:
6911 switch (constant_size (AT_unsigned (a)))
6913 case 1:
6914 return DW_FORM_data1;
6915 case 2:
6916 return DW_FORM_data2;
6917 case 4:
6918 return DW_FORM_data4;
6919 case 8:
6920 return DW_FORM_data8;
6921 default:
6922 gcc_unreachable ();
6924 case dw_val_class_long_long:
6925 return DW_FORM_block1;
6926 case dw_val_class_vec:
6927 return DW_FORM_block1;
6928 case dw_val_class_flag:
6929 return DW_FORM_flag;
6930 case dw_val_class_die_ref:
6931 if (AT_ref_external (a))
6932 return DW_FORM_ref_addr;
6933 else
6934 return DW_FORM_ref;
6935 case dw_val_class_fde_ref:
6936 return DW_FORM_data;
6937 case dw_val_class_lbl_id:
6938 return DW_FORM_addr;
6939 case dw_val_class_lineptr:
6940 case dw_val_class_macptr:
6941 return DW_FORM_data;
6942 case dw_val_class_str:
6943 return AT_string_form (a);
6944 case dw_val_class_file:
6945 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
6947 case 1:
6948 return DW_FORM_data1;
6949 case 2:
6950 return DW_FORM_data2;
6951 case 4:
6952 return DW_FORM_data4;
6953 default:
6954 gcc_unreachable ();
6957 default:
6958 gcc_unreachable ();
6962 /* Output the encoding of an attribute value. */
6964 static void
6965 output_value_format (dw_attr_ref a)
6967 enum dwarf_form form = value_format (a);
6969 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6972 /* Output the .debug_abbrev section which defines the DIE abbreviation
6973 table. */
6975 static void
6976 output_abbrev_section (void)
6978 unsigned long abbrev_id;
6980 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6982 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6983 unsigned ix;
6984 dw_attr_ref a_attr;
6986 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6987 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6988 dwarf_tag_name (abbrev->die_tag));
6990 if (abbrev->die_child != NULL)
6991 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6992 else
6993 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6995 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
6996 ix++)
6998 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6999 dwarf_attr_name (a_attr->dw_attr));
7000 output_value_format (a_attr);
7003 dw2_asm_output_data (1, 0, NULL);
7004 dw2_asm_output_data (1, 0, NULL);
7007 /* Terminate the table. */
7008 dw2_asm_output_data (1, 0, NULL);
7011 /* Output a symbol we can use to refer to this DIE from another CU. */
7013 static inline void
7014 output_die_symbol (dw_die_ref die)
7016 char *sym = die->die_symbol;
7018 if (sym == 0)
7019 return;
7021 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7022 /* We make these global, not weak; if the target doesn't support
7023 .linkonce, it doesn't support combining the sections, so debugging
7024 will break. */
7025 targetm.asm_out.globalize_label (asm_out_file, sym);
7027 ASM_OUTPUT_LABEL (asm_out_file, sym);
7030 /* Return a new location list, given the begin and end range, and the
7031 expression. gensym tells us whether to generate a new internal symbol for
7032 this location list node, which is done for the head of the list only. */
7034 static inline dw_loc_list_ref
7035 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7036 const char *section, unsigned int gensym)
7038 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
7040 retlist->begin = begin;
7041 retlist->end = end;
7042 retlist->expr = expr;
7043 retlist->section = section;
7044 if (gensym)
7045 retlist->ll_symbol = gen_internal_sym ("LLST");
7047 return retlist;
7050 /* Add a location description expression to a location list. */
7052 static inline void
7053 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7054 const char *begin, const char *end,
7055 const char *section)
7057 dw_loc_list_ref *d;
7059 /* Find the end of the chain. */
7060 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7063 /* Add a new location list node to the list. */
7064 *d = new_loc_list (descr, begin, end, section, 0);
7067 /* Note that the current function section is being used for code. */
7069 static void
7070 dwarf2out_note_section_used (void)
7072 section *sec = current_function_section ();
7073 if (sec == text_section)
7074 text_section_used = true;
7075 else if (sec == cold_text_section)
7076 cold_text_section_used = true;
7079 static void
7080 dwarf2out_switch_text_section (void)
7082 dw_fde_ref fde;
7084 gcc_assert (cfun);
7086 fde = &fde_table[fde_table_in_use - 1];
7087 fde->dw_fde_switched_sections = true;
7088 fde->dw_fde_hot_section_label = cfun->hot_section_label;
7089 fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
7090 fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
7091 fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
7092 have_multiple_function_sections = true;
7094 /* Reset the current label on switching text sections, so that we
7095 don't attempt to advance_loc4 between labels in different sections. */
7096 fde->dw_fde_current_label = NULL;
7098 dwarf2out_note_section_used ();
7101 /* Output the location list given to us. */
7103 static void
7104 output_loc_list (dw_loc_list_ref list_head)
7106 dw_loc_list_ref curr = list_head;
7108 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7110 /* Walk the location list, and output each range + expression. */
7111 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7113 unsigned long size;
7114 /* Don't output an entry that starts and ends at the same address. */
7115 if (strcmp (curr->begin, curr->end) == 0)
7116 continue;
7117 if (!have_multiple_function_sections)
7119 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7120 "Location list begin address (%s)",
7121 list_head->ll_symbol);
7122 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7123 "Location list end address (%s)",
7124 list_head->ll_symbol);
7126 else
7128 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7129 "Location list begin address (%s)",
7130 list_head->ll_symbol);
7131 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7132 "Location list end address (%s)",
7133 list_head->ll_symbol);
7135 size = size_of_locs (curr->expr);
7137 /* Output the block length for this list of location operations. */
7138 gcc_assert (size <= 0xffff);
7139 dw2_asm_output_data (2, size, "%s", "Location expression size");
7141 output_loc_sequence (curr->expr);
7144 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7145 "Location list terminator begin (%s)",
7146 list_head->ll_symbol);
7147 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7148 "Location list terminator end (%s)",
7149 list_head->ll_symbol);
7152 /* Output the DIE and its attributes. Called recursively to generate
7153 the definitions of each child DIE. */
7155 static void
7156 output_die (dw_die_ref die)
7158 dw_attr_ref a;
7159 dw_die_ref c;
7160 unsigned long size;
7161 unsigned ix;
7163 /* If someone in another CU might refer to us, set up a symbol for
7164 them to point to. */
7165 if (die->die_symbol)
7166 output_die_symbol (die);
7168 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7169 (unsigned long)die->die_offset,
7170 dwarf_tag_name (die->die_tag));
7172 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7174 const char *name = dwarf_attr_name (a->dw_attr);
7176 switch (AT_class (a))
7178 case dw_val_class_addr:
7179 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7180 break;
7182 case dw_val_class_offset:
7183 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7184 "%s", name);
7185 break;
7187 case dw_val_class_range_list:
7189 char *p = strchr (ranges_section_label, '\0');
7191 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7192 a->dw_attr_val.v.val_offset);
7193 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7194 debug_ranges_section, "%s", name);
7195 *p = '\0';
7197 break;
7199 case dw_val_class_loc:
7200 size = size_of_locs (AT_loc (a));
7202 /* Output the block length for this list of location operations. */
7203 dw2_asm_output_data (constant_size (size), size, "%s", name);
7205 output_loc_sequence (AT_loc (a));
7206 break;
7208 case dw_val_class_const:
7209 /* ??? It would be slightly more efficient to use a scheme like is
7210 used for unsigned constants below, but gdb 4.x does not sign
7211 extend. Gdb 5.x does sign extend. */
7212 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7213 break;
7215 case dw_val_class_unsigned_const:
7216 dw2_asm_output_data (constant_size (AT_unsigned (a)),
7217 AT_unsigned (a), "%s", name);
7218 break;
7220 case dw_val_class_long_long:
7222 unsigned HOST_WIDE_INT first, second;
7224 dw2_asm_output_data (1,
7225 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7226 "%s", name);
7228 if (WORDS_BIG_ENDIAN)
7230 first = a->dw_attr_val.v.val_long_long.hi;
7231 second = a->dw_attr_val.v.val_long_long.low;
7233 else
7235 first = a->dw_attr_val.v.val_long_long.low;
7236 second = a->dw_attr_val.v.val_long_long.hi;
7239 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7240 first, "long long constant");
7241 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7242 second, NULL);
7244 break;
7246 case dw_val_class_vec:
7248 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7249 unsigned int len = a->dw_attr_val.v.val_vec.length;
7250 unsigned int i;
7251 unsigned char *p;
7253 dw2_asm_output_data (1, len * elt_size, "%s", name);
7254 if (elt_size > sizeof (HOST_WIDE_INT))
7256 elt_size /= 2;
7257 len *= 2;
7259 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7260 i < len;
7261 i++, p += elt_size)
7262 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7263 "fp or vector constant word %u", i);
7264 break;
7267 case dw_val_class_flag:
7268 dw2_asm_output_data (1, AT_flag (a), "%s", name);
7269 break;
7271 case dw_val_class_loc_list:
7273 char *sym = AT_loc_list (a)->ll_symbol;
7275 gcc_assert (sym);
7276 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7277 "%s", name);
7279 break;
7281 case dw_val_class_die_ref:
7282 if (AT_ref_external (a))
7284 char *sym = AT_ref (a)->die_symbol;
7286 gcc_assert (sym);
7287 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7288 "%s", name);
7290 else
7292 gcc_assert (AT_ref (a)->die_offset);
7293 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7294 "%s", name);
7296 break;
7298 case dw_val_class_fde_ref:
7300 char l1[20];
7302 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7303 a->dw_attr_val.v.val_fde_index * 2);
7304 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7305 "%s", name);
7307 break;
7309 case dw_val_class_lbl_id:
7310 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7311 break;
7313 case dw_val_class_lineptr:
7314 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7315 debug_line_section, "%s", name);
7316 break;
7318 case dw_val_class_macptr:
7319 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7320 debug_macinfo_section, "%s", name);
7321 break;
7323 case dw_val_class_str:
7324 if (AT_string_form (a) == DW_FORM_strp)
7325 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7326 a->dw_attr_val.v.val_str->label,
7327 debug_str_section,
7328 "%s: \"%s\"", name, AT_string (a));
7329 else
7330 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7331 break;
7333 case dw_val_class_file:
7335 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7337 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7338 a->dw_attr_val.v.val_file->filename);
7339 break;
7342 default:
7343 gcc_unreachable ();
7347 FOR_EACH_CHILD (die, c, output_die (c));
7349 /* Add null byte to terminate sibling list. */
7350 if (die->die_child != NULL)
7351 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7352 (unsigned long) die->die_offset);
7355 /* Output the compilation unit that appears at the beginning of the
7356 .debug_info section, and precedes the DIE descriptions. */
7358 static void
7359 output_compilation_unit_header (void)
7361 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7362 dw2_asm_output_data (4, 0xffffffff,
7363 "Initial length escape value indicating 64-bit DWARF extension");
7364 dw2_asm_output_data (DWARF_OFFSET_SIZE,
7365 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7366 "Length of Compilation Unit Info");
7367 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7368 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7369 debug_abbrev_section,
7370 "Offset Into Abbrev. Section");
7371 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7374 /* Output the compilation unit DIE and its children. */
7376 static void
7377 output_comp_unit (dw_die_ref die, int output_if_empty)
7379 const char *secname;
7380 char *oldsym, *tmp;
7382 /* Unless we are outputting main CU, we may throw away empty ones. */
7383 if (!output_if_empty && die->die_child == NULL)
7384 return;
7386 /* Even if there are no children of this DIE, we must output the information
7387 about the compilation unit. Otherwise, on an empty translation unit, we
7388 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7389 will then complain when examining the file. First mark all the DIEs in
7390 this CU so we know which get local refs. */
7391 mark_dies (die);
7393 build_abbrev_table (die);
7395 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7396 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7397 calc_die_sizes (die);
7399 oldsym = die->die_symbol;
7400 if (oldsym)
7402 tmp = alloca (strlen (oldsym) + 24);
7404 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7405 secname = tmp;
7406 die->die_symbol = NULL;
7407 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7409 else
7410 switch_to_section (debug_info_section);
7412 /* Output debugging information. */
7413 output_compilation_unit_header ();
7414 output_die (die);
7416 /* Leave the marks on the main CU, so we can check them in
7417 output_pubnames. */
7418 if (oldsym)
7420 unmark_dies (die);
7421 die->die_symbol = oldsym;
7425 /* Return the DWARF2/3 pubname associated with a decl. */
7427 static const char *
7428 dwarf2_name (tree decl, int scope)
7430 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7433 /* Add a new entry to .debug_pubnames if appropriate. */
7435 static void
7436 add_pubname (tree decl, dw_die_ref die)
7438 pubname_entry e;
7440 if (! TREE_PUBLIC (decl))
7441 return;
7443 e.die = die;
7444 e.name = xstrdup (dwarf2_name (decl, 1));
7445 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7448 /* Add a new entry to .debug_pubtypes if appropriate. */
7450 static void
7451 add_pubtype (tree decl, dw_die_ref die)
7453 pubname_entry e;
7455 e.name = NULL;
7456 if ((TREE_PUBLIC (decl)
7457 || die->die_parent == comp_unit_die)
7458 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7460 e.die = die;
7461 if (TYPE_P (decl))
7463 if (TYPE_NAME (decl))
7465 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7466 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7467 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7468 && DECL_NAME (TYPE_NAME (decl)))
7469 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7470 else
7471 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7474 else
7475 e.name = xstrdup (dwarf2_name (decl, 1));
7477 /* If we don't have a name for the type, there's no point in adding
7478 it to the table. */
7479 if (e.name && e.name[0] != '\0')
7480 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7484 /* Output the public names table used to speed up access to externally
7485 visible names; or the public types table used to find type definitions. */
7487 static void
7488 output_pubnames (VEC (pubname_entry, gc) * names)
7490 unsigned i;
7491 unsigned long pubnames_length = size_of_pubnames (names);
7492 pubname_ref pub;
7494 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7495 dw2_asm_output_data (4, 0xffffffff,
7496 "Initial length escape value indicating 64-bit DWARF extension");
7497 if (names == pubname_table)
7498 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7499 "Length of Public Names Info");
7500 else
7501 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7502 "Length of Public Type Names Info");
7503 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7504 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7505 debug_info_section,
7506 "Offset of Compilation Unit Info");
7507 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7508 "Compilation Unit Length");
7510 for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7512 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7513 if (names == pubname_table)
7514 gcc_assert (pub->die->die_mark);
7516 if (names != pubtype_table
7517 || pub->die->die_offset != 0
7518 || !flag_eliminate_unused_debug_types)
7520 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7521 "DIE offset");
7523 dw2_asm_output_nstring (pub->name, -1, "external name");
7527 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7530 /* Add a new entry to .debug_aranges if appropriate. */
7532 static void
7533 add_arange (tree decl, dw_die_ref die)
7535 if (! DECL_SECTION_NAME (decl))
7536 return;
7538 if (arange_table_in_use == arange_table_allocated)
7540 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7541 arange_table = ggc_realloc (arange_table,
7542 (arange_table_allocated
7543 * sizeof (dw_die_ref)));
7544 memset (arange_table + arange_table_in_use, 0,
7545 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7548 arange_table[arange_table_in_use++] = die;
7551 /* Output the information that goes into the .debug_aranges table.
7552 Namely, define the beginning and ending address range of the
7553 text section generated for this compilation unit. */
7555 static void
7556 output_aranges (void)
7558 unsigned i;
7559 unsigned long aranges_length = size_of_aranges ();
7561 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7562 dw2_asm_output_data (4, 0xffffffff,
7563 "Initial length escape value indicating 64-bit DWARF extension");
7564 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7565 "Length of Address Ranges Info");
7566 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7567 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7568 debug_info_section,
7569 "Offset of Compilation Unit Info");
7570 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7571 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7573 /* We need to align to twice the pointer size here. */
7574 if (DWARF_ARANGES_PAD_SIZE)
7576 /* Pad using a 2 byte words so that padding is correct for any
7577 pointer size. */
7578 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7579 2 * DWARF2_ADDR_SIZE);
7580 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7581 dw2_asm_output_data (2, 0, NULL);
7584 /* It is necessary not to output these entries if the sections were
7585 not used; if the sections were not used, the length will be 0 and
7586 the address may end up as 0 if the section is discarded by ld
7587 --gc-sections, leaving an invalid (0, 0) entry that can be
7588 confused with the terminator. */
7589 if (text_section_used)
7591 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7592 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7593 text_section_label, "Length");
7595 if (cold_text_section_used)
7597 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7598 "Address");
7599 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7600 cold_text_section_label, "Length");
7603 for (i = 0; i < arange_table_in_use; i++)
7605 dw_die_ref die = arange_table[i];
7607 /* We shouldn't see aranges for DIEs outside of the main CU. */
7608 gcc_assert (die->die_mark);
7610 if (die->die_tag == DW_TAG_subprogram)
7612 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7613 "Address");
7614 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7615 get_AT_low_pc (die), "Length");
7617 else
7619 /* A static variable; extract the symbol from DW_AT_location.
7620 Note that this code isn't currently hit, as we only emit
7621 aranges for functions (jason 9/23/99). */
7622 dw_attr_ref a = get_AT (die, DW_AT_location);
7623 dw_loc_descr_ref loc;
7625 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7627 loc = AT_loc (a);
7628 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7630 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7631 loc->dw_loc_oprnd1.v.val_addr, "Address");
7632 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7633 get_AT_unsigned (die, DW_AT_byte_size),
7634 "Length");
7638 /* Output the terminator words. */
7639 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7640 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7643 /* Add a new entry to .debug_ranges. Return the offset at which it
7644 was placed. */
7646 static unsigned int
7647 add_ranges_num (int num)
7649 unsigned int in_use = ranges_table_in_use;
7651 if (in_use == ranges_table_allocated)
7653 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7654 ranges_table
7655 = ggc_realloc (ranges_table, (ranges_table_allocated
7656 * sizeof (struct dw_ranges_struct)));
7657 memset (ranges_table + ranges_table_in_use, 0,
7658 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7661 ranges_table[in_use].num = num;
7662 ranges_table_in_use = in_use + 1;
7664 return in_use * 2 * DWARF2_ADDR_SIZE;
7667 /* Add a new entry to .debug_ranges corresponding to a block, or a
7668 range terminator if BLOCK is NULL. */
7670 static unsigned int
7671 add_ranges (const_tree block)
7673 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
7676 /* Add a new entry to .debug_ranges corresponding to a pair of
7677 labels. */
7679 static unsigned int
7680 add_ranges_by_labels (const char *begin, const char *end)
7682 unsigned int in_use = ranges_by_label_in_use;
7684 if (in_use == ranges_by_label_allocated)
7686 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
7687 ranges_by_label
7688 = ggc_realloc (ranges_by_label,
7689 (ranges_by_label_allocated
7690 * sizeof (struct dw_ranges_by_label_struct)));
7691 memset (ranges_by_label + ranges_by_label_in_use, 0,
7692 RANGES_TABLE_INCREMENT
7693 * sizeof (struct dw_ranges_by_label_struct));
7696 ranges_by_label[in_use].begin = begin;
7697 ranges_by_label[in_use].end = end;
7698 ranges_by_label_in_use = in_use + 1;
7700 return add_ranges_num (-(int)in_use - 1);
7703 static void
7704 output_ranges (void)
7706 unsigned i;
7707 static const char *const start_fmt = "Offset 0x%x";
7708 const char *fmt = start_fmt;
7710 for (i = 0; i < ranges_table_in_use; i++)
7712 int block_num = ranges_table[i].num;
7714 if (block_num > 0)
7716 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7717 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7719 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7720 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7722 /* If all code is in the text section, then the compilation
7723 unit base address defaults to DW_AT_low_pc, which is the
7724 base of the text section. */
7725 if (!have_multiple_function_sections)
7727 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7728 text_section_label,
7729 fmt, i * 2 * DWARF2_ADDR_SIZE);
7730 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7731 text_section_label, NULL);
7734 /* Otherwise, the compilation unit base address is zero,
7735 which allows us to use absolute addresses, and not worry
7736 about whether the target supports cross-section
7737 arithmetic. */
7738 else
7740 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7741 fmt, i * 2 * DWARF2_ADDR_SIZE);
7742 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7745 fmt = NULL;
7748 /* Negative block_num stands for an index into ranges_by_label. */
7749 else if (block_num < 0)
7751 int lab_idx = - block_num - 1;
7753 if (!have_multiple_function_sections)
7755 gcc_unreachable ();
7756 #if 0
7757 /* If we ever use add_ranges_by_labels () for a single
7758 function section, all we have to do is to take out
7759 the #if 0 above. */
7760 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7761 ranges_by_label[lab_idx].begin,
7762 text_section_label,
7763 fmt, i * 2 * DWARF2_ADDR_SIZE);
7764 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7765 ranges_by_label[lab_idx].end,
7766 text_section_label, NULL);
7767 #endif
7769 else
7771 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7772 ranges_by_label[lab_idx].begin,
7773 fmt, i * 2 * DWARF2_ADDR_SIZE);
7774 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7775 ranges_by_label[lab_idx].end,
7776 NULL);
7779 else
7781 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7782 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7783 fmt = start_fmt;
7788 /* Data structure containing information about input files. */
7789 struct file_info
7791 const char *path; /* Complete file name. */
7792 const char *fname; /* File name part. */
7793 int length; /* Length of entire string. */
7794 struct dwarf_file_data * file_idx; /* Index in input file table. */
7795 int dir_idx; /* Index in directory table. */
7798 /* Data structure containing information about directories with source
7799 files. */
7800 struct dir_info
7802 const char *path; /* Path including directory name. */
7803 int length; /* Path length. */
7804 int prefix; /* Index of directory entry which is a prefix. */
7805 int count; /* Number of files in this directory. */
7806 int dir_idx; /* Index of directory used as base. */
7809 /* Callback function for file_info comparison. We sort by looking at
7810 the directories in the path. */
7812 static int
7813 file_info_cmp (const void *p1, const void *p2)
7815 const struct file_info *s1 = p1;
7816 const struct file_info *s2 = p2;
7817 const unsigned char *cp1;
7818 const unsigned char *cp2;
7820 /* Take care of file names without directories. We need to make sure that
7821 we return consistent values to qsort since some will get confused if
7822 we return the same value when identical operands are passed in opposite
7823 orders. So if neither has a directory, return 0 and otherwise return
7824 1 or -1 depending on which one has the directory. */
7825 if ((s1->path == s1->fname || s2->path == s2->fname))
7826 return (s2->path == s2->fname) - (s1->path == s1->fname);
7828 cp1 = (const unsigned char *) s1->path;
7829 cp2 = (const unsigned char *) s2->path;
7831 while (1)
7833 ++cp1;
7834 ++cp2;
7835 /* Reached the end of the first path? If so, handle like above. */
7836 if ((cp1 == (const unsigned char *) s1->fname)
7837 || (cp2 == (const unsigned char *) s2->fname))
7838 return ((cp2 == (const unsigned char *) s2->fname)
7839 - (cp1 == (const unsigned char *) s1->fname));
7841 /* Character of current path component the same? */
7842 else if (*cp1 != *cp2)
7843 return *cp1 - *cp2;
7847 struct file_name_acquire_data
7849 struct file_info *files;
7850 int used_files;
7851 int max_files;
7854 /* Traversal function for the hash table. */
7856 static int
7857 file_name_acquire (void ** slot, void *data)
7859 struct file_name_acquire_data *fnad = data;
7860 struct dwarf_file_data *d = *slot;
7861 struct file_info *fi;
7862 const char *f;
7864 gcc_assert (fnad->max_files >= d->emitted_number);
7866 if (! d->emitted_number)
7867 return 1;
7869 gcc_assert (fnad->max_files != fnad->used_files);
7871 fi = fnad->files + fnad->used_files++;
7873 /* Skip all leading "./". */
7874 f = d->filename;
7875 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7876 f += 2;
7878 /* Create a new array entry. */
7879 fi->path = f;
7880 fi->length = strlen (f);
7881 fi->file_idx = d;
7883 /* Search for the file name part. */
7884 f = strrchr (f, DIR_SEPARATOR);
7885 #if defined (DIR_SEPARATOR_2)
7887 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7889 if (g != NULL)
7891 if (f == NULL || f < g)
7892 f = g;
7895 #endif
7897 fi->fname = f == NULL ? fi->path : f + 1;
7898 return 1;
7901 /* Output the directory table and the file name table. We try to minimize
7902 the total amount of memory needed. A heuristic is used to avoid large
7903 slowdowns with many input files. */
7905 static void
7906 output_file_names (void)
7908 struct file_name_acquire_data fnad;
7909 int numfiles;
7910 struct file_info *files;
7911 struct dir_info *dirs;
7912 int *saved;
7913 int *savehere;
7914 int *backmap;
7915 int ndirs;
7916 int idx_offset;
7917 int i;
7918 int idx;
7920 if (!last_emitted_file)
7922 dw2_asm_output_data (1, 0, "End directory table");
7923 dw2_asm_output_data (1, 0, "End file name table");
7924 return;
7927 numfiles = last_emitted_file->emitted_number;
7929 /* Allocate the various arrays we need. */
7930 files = alloca (numfiles * sizeof (struct file_info));
7931 dirs = alloca (numfiles * sizeof (struct dir_info));
7933 fnad.files = files;
7934 fnad.used_files = 0;
7935 fnad.max_files = numfiles;
7936 htab_traverse (file_table, file_name_acquire, &fnad);
7937 gcc_assert (fnad.used_files == fnad.max_files);
7939 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
7941 /* Find all the different directories used. */
7942 dirs[0].path = files[0].path;
7943 dirs[0].length = files[0].fname - files[0].path;
7944 dirs[0].prefix = -1;
7945 dirs[0].count = 1;
7946 dirs[0].dir_idx = 0;
7947 files[0].dir_idx = 0;
7948 ndirs = 1;
7950 for (i = 1; i < numfiles; i++)
7951 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7952 && memcmp (dirs[ndirs - 1].path, files[i].path,
7953 dirs[ndirs - 1].length) == 0)
7955 /* Same directory as last entry. */
7956 files[i].dir_idx = ndirs - 1;
7957 ++dirs[ndirs - 1].count;
7959 else
7961 int j;
7963 /* This is a new directory. */
7964 dirs[ndirs].path = files[i].path;
7965 dirs[ndirs].length = files[i].fname - files[i].path;
7966 dirs[ndirs].count = 1;
7967 dirs[ndirs].dir_idx = ndirs;
7968 files[i].dir_idx = ndirs;
7970 /* Search for a prefix. */
7971 dirs[ndirs].prefix = -1;
7972 for (j = 0; j < ndirs; j++)
7973 if (dirs[j].length < dirs[ndirs].length
7974 && dirs[j].length > 1
7975 && (dirs[ndirs].prefix == -1
7976 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7977 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7978 dirs[ndirs].prefix = j;
7980 ++ndirs;
7983 /* Now to the actual work. We have to find a subset of the directories which
7984 allow expressing the file name using references to the directory table
7985 with the least amount of characters. We do not do an exhaustive search
7986 where we would have to check out every combination of every single
7987 possible prefix. Instead we use a heuristic which provides nearly optimal
7988 results in most cases and never is much off. */
7989 saved = alloca (ndirs * sizeof (int));
7990 savehere = alloca (ndirs * sizeof (int));
7992 memset (saved, '\0', ndirs * sizeof (saved[0]));
7993 for (i = 0; i < ndirs; i++)
7995 int j;
7996 int total;
7998 /* We can always save some space for the current directory. But this
7999 does not mean it will be enough to justify adding the directory. */
8000 savehere[i] = dirs[i].length;
8001 total = (savehere[i] - saved[i]) * dirs[i].count;
8003 for (j = i + 1; j < ndirs; j++)
8005 savehere[j] = 0;
8006 if (saved[j] < dirs[i].length)
8008 /* Determine whether the dirs[i] path is a prefix of the
8009 dirs[j] path. */
8010 int k;
8012 k = dirs[j].prefix;
8013 while (k != -1 && k != (int) i)
8014 k = dirs[k].prefix;
8016 if (k == (int) i)
8018 /* Yes it is. We can possibly save some memory by
8019 writing the filenames in dirs[j] relative to
8020 dirs[i]. */
8021 savehere[j] = dirs[i].length;
8022 total += (savehere[j] - saved[j]) * dirs[j].count;
8027 /* Check whether we can save enough to justify adding the dirs[i]
8028 directory. */
8029 if (total > dirs[i].length + 1)
8031 /* It's worthwhile adding. */
8032 for (j = i; j < ndirs; j++)
8033 if (savehere[j] > 0)
8035 /* Remember how much we saved for this directory so far. */
8036 saved[j] = savehere[j];
8038 /* Remember the prefix directory. */
8039 dirs[j].dir_idx = i;
8044 /* Emit the directory name table. */
8045 idx = 1;
8046 idx_offset = dirs[0].length > 0 ? 1 : 0;
8047 for (i = 1 - idx_offset; i < ndirs; i++)
8048 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8049 "Directory Entry: 0x%x", i + idx_offset);
8051 dw2_asm_output_data (1, 0, "End directory table");
8053 /* We have to emit them in the order of emitted_number since that's
8054 used in the debug info generation. To do this efficiently we
8055 generate a back-mapping of the indices first. */
8056 backmap = alloca (numfiles * sizeof (int));
8057 for (i = 0; i < numfiles; i++)
8058 backmap[files[i].file_idx->emitted_number - 1] = i;
8060 /* Now write all the file names. */
8061 for (i = 0; i < numfiles; i++)
8063 int file_idx = backmap[i];
8064 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8066 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8067 "File Entry: 0x%x", (unsigned) i + 1);
8069 /* Include directory index. */
8070 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8072 /* Modification time. */
8073 dw2_asm_output_data_uleb128 (0, NULL);
8075 /* File length in bytes. */
8076 dw2_asm_output_data_uleb128 (0, NULL);
8079 dw2_asm_output_data (1, 0, "End file name table");
8083 /* Output the source line number correspondence information. This
8084 information goes into the .debug_line section. */
8086 static void
8087 output_line_info (void)
8089 char l1[20], l2[20], p1[20], p2[20];
8090 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8091 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8092 unsigned opc;
8093 unsigned n_op_args;
8094 unsigned long lt_index;
8095 unsigned long current_line;
8096 long line_offset;
8097 long line_delta;
8098 unsigned long current_file;
8099 unsigned long function;
8101 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8102 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8103 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8104 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8106 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8107 dw2_asm_output_data (4, 0xffffffff,
8108 "Initial length escape value indicating 64-bit DWARF extension");
8109 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8110 "Length of Source Line Info");
8111 ASM_OUTPUT_LABEL (asm_out_file, l1);
8113 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8114 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8115 ASM_OUTPUT_LABEL (asm_out_file, p1);
8117 /* Define the architecture-dependent minimum instruction length (in
8118 bytes). In this implementation of DWARF, this field is used for
8119 information purposes only. Since GCC generates assembly language,
8120 we have no a priori knowledge of how many instruction bytes are
8121 generated for each source line, and therefore can use only the
8122 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8123 commands. Accordingly, we fix this as `1', which is "correct
8124 enough" for all architectures, and don't let the target override. */
8125 dw2_asm_output_data (1, 1,
8126 "Minimum Instruction Length");
8128 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8129 "Default is_stmt_start flag");
8130 dw2_asm_output_data (1, DWARF_LINE_BASE,
8131 "Line Base Value (Special Opcodes)");
8132 dw2_asm_output_data (1, DWARF_LINE_RANGE,
8133 "Line Range Value (Special Opcodes)");
8134 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8135 "Special Opcode Base");
8137 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8139 switch (opc)
8141 case DW_LNS_advance_pc:
8142 case DW_LNS_advance_line:
8143 case DW_LNS_set_file:
8144 case DW_LNS_set_column:
8145 case DW_LNS_fixed_advance_pc:
8146 n_op_args = 1;
8147 break;
8148 default:
8149 n_op_args = 0;
8150 break;
8153 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8154 opc, n_op_args);
8157 /* Write out the information about the files we use. */
8158 output_file_names ();
8159 ASM_OUTPUT_LABEL (asm_out_file, p2);
8161 /* We used to set the address register to the first location in the text
8162 section here, but that didn't accomplish anything since we already
8163 have a line note for the opening brace of the first function. */
8165 /* Generate the line number to PC correspondence table, encoded as
8166 a series of state machine operations. */
8167 current_file = 1;
8168 current_line = 1;
8170 if (cfun && in_cold_section_p)
8171 strcpy (prev_line_label, cfun->cold_section_label);
8172 else
8173 strcpy (prev_line_label, text_section_label);
8174 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8176 dw_line_info_ref line_info = &line_info_table[lt_index];
8178 #if 0
8179 /* Disable this optimization for now; GDB wants to see two line notes
8180 at the beginning of a function so it can find the end of the
8181 prologue. */
8183 /* Don't emit anything for redundant notes. Just updating the
8184 address doesn't accomplish anything, because we already assume
8185 that anything after the last address is this line. */
8186 if (line_info->dw_line_num == current_line
8187 && line_info->dw_file_num == current_file)
8188 continue;
8189 #endif
8191 /* Emit debug info for the address of the current line.
8193 Unfortunately, we have little choice here currently, and must always
8194 use the most general form. GCC does not know the address delta
8195 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
8196 attributes which will give an upper bound on the address range. We
8197 could perhaps use length attributes to determine when it is safe to
8198 use DW_LNS_fixed_advance_pc. */
8200 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8201 if (0)
8203 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
8204 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8205 "DW_LNS_fixed_advance_pc");
8206 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8208 else
8210 /* This can handle any delta. This takes
8211 4+DWARF2_ADDR_SIZE bytes. */
8212 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8213 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8214 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8215 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8218 strcpy (prev_line_label, line_label);
8220 /* Emit debug info for the source file of the current line, if
8221 different from the previous line. */
8222 if (line_info->dw_file_num != current_file)
8224 current_file = line_info->dw_file_num;
8225 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8226 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8229 /* Emit debug info for the current line number, choosing the encoding
8230 that uses the least amount of space. */
8231 if (line_info->dw_line_num != current_line)
8233 line_offset = line_info->dw_line_num - current_line;
8234 line_delta = line_offset - DWARF_LINE_BASE;
8235 current_line = line_info->dw_line_num;
8236 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8237 /* This can handle deltas from -10 to 234, using the current
8238 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
8239 takes 1 byte. */
8240 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8241 "line %lu", current_line);
8242 else
8244 /* This can handle any delta. This takes at least 4 bytes,
8245 depending on the value being encoded. */
8246 dw2_asm_output_data (1, DW_LNS_advance_line,
8247 "advance to line %lu", current_line);
8248 dw2_asm_output_data_sleb128 (line_offset, NULL);
8249 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8252 else
8253 /* We still need to start a new row, so output a copy insn. */
8254 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8257 /* Emit debug info for the address of the end of the function. */
8258 if (0)
8260 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8261 "DW_LNS_fixed_advance_pc");
8262 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8264 else
8266 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8267 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8268 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8269 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8272 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8273 dw2_asm_output_data_uleb128 (1, NULL);
8274 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8276 function = 0;
8277 current_file = 1;
8278 current_line = 1;
8279 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8281 dw_separate_line_info_ref line_info
8282 = &separate_line_info_table[lt_index];
8284 #if 0
8285 /* Don't emit anything for redundant notes. */
8286 if (line_info->dw_line_num == current_line
8287 && line_info->dw_file_num == current_file
8288 && line_info->function == function)
8289 goto cont;
8290 #endif
8292 /* Emit debug info for the address of the current line. If this is
8293 a new function, or the first line of a function, then we need
8294 to handle it differently. */
8295 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8296 lt_index);
8297 if (function != line_info->function)
8299 function = line_info->function;
8301 /* Set the address register to the first line in the function. */
8302 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8303 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8304 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8305 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8307 else
8309 /* ??? See the DW_LNS_advance_pc comment above. */
8310 if (0)
8312 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8313 "DW_LNS_fixed_advance_pc");
8314 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8316 else
8318 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8319 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8320 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8321 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8325 strcpy (prev_line_label, line_label);
8327 /* Emit debug info for the source file of the current line, if
8328 different from the previous line. */
8329 if (line_info->dw_file_num != current_file)
8331 current_file = line_info->dw_file_num;
8332 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8333 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8336 /* Emit debug info for the current line number, choosing the encoding
8337 that uses the least amount of space. */
8338 if (line_info->dw_line_num != current_line)
8340 line_offset = line_info->dw_line_num - current_line;
8341 line_delta = line_offset - DWARF_LINE_BASE;
8342 current_line = line_info->dw_line_num;
8343 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8344 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8345 "line %lu", current_line);
8346 else
8348 dw2_asm_output_data (1, DW_LNS_advance_line,
8349 "advance to line %lu", current_line);
8350 dw2_asm_output_data_sleb128 (line_offset, NULL);
8351 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8354 else
8355 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8357 #if 0
8358 cont:
8359 #endif
8361 lt_index++;
8363 /* If we're done with a function, end its sequence. */
8364 if (lt_index == separate_line_info_table_in_use
8365 || separate_line_info_table[lt_index].function != function)
8367 current_file = 1;
8368 current_line = 1;
8370 /* Emit debug info for the address of the end of the function. */
8371 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8372 if (0)
8374 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8375 "DW_LNS_fixed_advance_pc");
8376 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8378 else
8380 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8381 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8382 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8383 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8386 /* Output the marker for the end of this sequence. */
8387 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8388 dw2_asm_output_data_uleb128 (1, NULL);
8389 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8393 /* Output the marker for the end of the line number info. */
8394 ASM_OUTPUT_LABEL (asm_out_file, l2);
8397 /* Given a pointer to a tree node for some base type, return a pointer to
8398 a DIE that describes the given type.
8400 This routine must only be called for GCC type nodes that correspond to
8401 Dwarf base (fundamental) types. */
8403 static dw_die_ref
8404 base_type_die (tree type)
8406 dw_die_ref base_type_result;
8407 enum dwarf_type encoding;
8409 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8410 return 0;
8412 switch (TREE_CODE (type))
8414 case INTEGER_TYPE:
8415 if (TYPE_STRING_FLAG (type))
8417 if (TYPE_UNSIGNED (type))
8418 encoding = DW_ATE_unsigned_char;
8419 else
8420 encoding = DW_ATE_signed_char;
8422 else if (TYPE_UNSIGNED (type))
8423 encoding = DW_ATE_unsigned;
8424 else
8425 encoding = DW_ATE_signed;
8426 break;
8428 case REAL_TYPE:
8429 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8430 encoding = DW_ATE_decimal_float;
8431 else
8432 encoding = DW_ATE_float;
8433 break;
8435 case FIXED_POINT_TYPE:
8436 if (TYPE_UNSIGNED (type))
8437 encoding = DW_ATE_signed_fixed;
8438 else
8439 encoding = DW_ATE_unsigned_fixed;
8440 break;
8442 /* Dwarf2 doesn't know anything about complex ints, so use
8443 a user defined type for it. */
8444 case COMPLEX_TYPE:
8445 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8446 encoding = DW_ATE_complex_float;
8447 else
8448 encoding = DW_ATE_lo_user;
8449 break;
8451 case BOOLEAN_TYPE:
8452 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
8453 encoding = DW_ATE_boolean;
8454 break;
8456 default:
8457 /* No other TREE_CODEs are Dwarf fundamental types. */
8458 gcc_unreachable ();
8461 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8463 /* This probably indicates a bug. */
8464 if (! TYPE_NAME (type))
8465 add_name_attribute (base_type_result, "__unknown__");
8467 add_AT_unsigned (base_type_result, DW_AT_byte_size,
8468 int_size_in_bytes (type));
8469 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8471 return base_type_result;
8474 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8475 given input type is a Dwarf "fundamental" type. Otherwise return null. */
8477 static inline int
8478 is_base_type (tree type)
8480 switch (TREE_CODE (type))
8482 case ERROR_MARK:
8483 case VOID_TYPE:
8484 case INTEGER_TYPE:
8485 case REAL_TYPE:
8486 case FIXED_POINT_TYPE:
8487 case COMPLEX_TYPE:
8488 case BOOLEAN_TYPE:
8489 return 1;
8491 case ARRAY_TYPE:
8492 case RECORD_TYPE:
8493 case UNION_TYPE:
8494 case QUAL_UNION_TYPE:
8495 case ENUMERAL_TYPE:
8496 case FUNCTION_TYPE:
8497 case METHOD_TYPE:
8498 case POINTER_TYPE:
8499 case REFERENCE_TYPE:
8500 case OFFSET_TYPE:
8501 case LANG_TYPE:
8502 case VECTOR_TYPE:
8503 return 0;
8505 default:
8506 gcc_unreachable ();
8509 return 0;
8512 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8513 node, return the size in bits for the type if it is a constant, or else
8514 return the alignment for the type if the type's size is not constant, or
8515 else return BITS_PER_WORD if the type actually turns out to be an
8516 ERROR_MARK node. */
8518 static inline unsigned HOST_WIDE_INT
8519 simple_type_size_in_bits (const_tree type)
8521 if (TREE_CODE (type) == ERROR_MARK)
8522 return BITS_PER_WORD;
8523 else if (TYPE_SIZE (type) == NULL_TREE)
8524 return 0;
8525 else if (host_integerp (TYPE_SIZE (type), 1))
8526 return tree_low_cst (TYPE_SIZE (type), 1);
8527 else
8528 return TYPE_ALIGN (type);
8531 /* Return true if the debug information for the given type should be
8532 emitted as a subrange type. */
8534 static inline bool
8535 is_subrange_type (const_tree type)
8537 tree subtype = TREE_TYPE (type);
8539 /* Subrange types are identified by the fact that they are integer
8540 types, and that they have a subtype which is either an integer type
8541 or an enumeral type. */
8543 if (TREE_CODE (type) != INTEGER_TYPE
8544 || subtype == NULL_TREE)
8545 return false;
8547 if (TREE_CODE (subtype) != INTEGER_TYPE
8548 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8549 return false;
8551 if (TREE_CODE (type) == TREE_CODE (subtype)
8552 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8553 && TYPE_MIN_VALUE (type) != NULL
8554 && TYPE_MIN_VALUE (subtype) != NULL
8555 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8556 && TYPE_MAX_VALUE (type) != NULL
8557 && TYPE_MAX_VALUE (subtype) != NULL
8558 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8560 /* The type and its subtype have the same representation. If in
8561 addition the two types also have the same name, then the given
8562 type is not a subrange type, but rather a plain base type. */
8563 /* FIXME: brobecker/2004-03-22:
8564 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8565 therefore be sufficient to check the TYPE_SIZE node pointers
8566 rather than checking the actual size. Unfortunately, we have
8567 found some cases, such as in the Ada "integer" type, where
8568 this is not the case. Until this problem is solved, we need to
8569 keep checking the actual size. */
8570 tree type_name = TYPE_NAME (type);
8571 tree subtype_name = TYPE_NAME (subtype);
8573 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8574 type_name = DECL_NAME (type_name);
8576 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8577 subtype_name = DECL_NAME (subtype_name);
8579 if (type_name == subtype_name)
8580 return false;
8583 return true;
8586 /* Given a pointer to a tree node for a subrange type, return a pointer
8587 to a DIE that describes the given type. */
8589 static dw_die_ref
8590 subrange_type_die (tree type, dw_die_ref context_die)
8592 dw_die_ref subrange_die;
8593 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8595 if (context_die == NULL)
8596 context_die = comp_unit_die;
8598 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8600 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8602 /* The size of the subrange type and its base type do not match,
8603 so we need to generate a size attribute for the subrange type. */
8604 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8607 if (TYPE_MIN_VALUE (type) != NULL)
8608 add_bound_info (subrange_die, DW_AT_lower_bound,
8609 TYPE_MIN_VALUE (type));
8610 if (TYPE_MAX_VALUE (type) != NULL)
8611 add_bound_info (subrange_die, DW_AT_upper_bound,
8612 TYPE_MAX_VALUE (type));
8614 return subrange_die;
8617 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8618 entry that chains various modifiers in front of the given type. */
8620 static dw_die_ref
8621 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8622 dw_die_ref context_die)
8624 enum tree_code code = TREE_CODE (type);
8625 dw_die_ref mod_type_die;
8626 dw_die_ref sub_die = NULL;
8627 tree item_type = NULL;
8628 tree qualified_type;
8629 tree name;
8631 if (code == ERROR_MARK)
8632 return NULL;
8634 /* See if we already have the appropriately qualified variant of
8635 this type. */
8636 qualified_type
8637 = get_qualified_type (type,
8638 ((is_const_type ? TYPE_QUAL_CONST : 0)
8639 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8641 /* If we do, then we can just use its DIE, if it exists. */
8642 if (qualified_type)
8644 mod_type_die = lookup_type_die (qualified_type);
8645 if (mod_type_die)
8646 return mod_type_die;
8649 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8651 /* Handle C typedef types. */
8652 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8654 tree dtype = TREE_TYPE (name);
8656 if (qualified_type == dtype)
8658 /* For a named type, use the typedef. */
8659 gen_type_die (qualified_type, context_die);
8660 return lookup_type_die (qualified_type);
8662 else if (is_const_type < TYPE_READONLY (dtype)
8663 || is_volatile_type < TYPE_VOLATILE (dtype)
8664 || (is_const_type <= TYPE_READONLY (dtype)
8665 && is_volatile_type <= TYPE_VOLATILE (dtype)
8666 && DECL_ORIGINAL_TYPE (name) != type))
8667 /* cv-unqualified version of named type. Just use the unnamed
8668 type to which it refers. */
8669 return modified_type_die (DECL_ORIGINAL_TYPE (name),
8670 is_const_type, is_volatile_type,
8671 context_die);
8672 /* Else cv-qualified version of named type; fall through. */
8675 if (is_const_type)
8677 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8678 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8680 else if (is_volatile_type)
8682 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8683 sub_die = modified_type_die (type, 0, 0, context_die);
8685 else if (code == POINTER_TYPE)
8687 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8688 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8689 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8690 item_type = TREE_TYPE (type);
8692 else if (code == REFERENCE_TYPE)
8694 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8695 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8696 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8697 item_type = TREE_TYPE (type);
8699 else if (is_subrange_type (type))
8701 mod_type_die = subrange_type_die (type, context_die);
8702 item_type = TREE_TYPE (type);
8704 else if (is_base_type (type))
8705 mod_type_die = base_type_die (type);
8706 else
8708 gen_type_die (type, context_die);
8710 /* We have to get the type_main_variant here (and pass that to the
8711 `lookup_type_die' routine) because the ..._TYPE node we have
8712 might simply be a *copy* of some original type node (where the
8713 copy was created to help us keep track of typedef names) and
8714 that copy might have a different TYPE_UID from the original
8715 ..._TYPE node. */
8716 if (TREE_CODE (type) != VECTOR_TYPE)
8717 return lookup_type_die (type_main_variant (type));
8718 else
8719 /* Vectors have the debugging information in the type,
8720 not the main variant. */
8721 return lookup_type_die (type);
8724 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
8725 don't output a DW_TAG_typedef, since there isn't one in the
8726 user's program; just attach a DW_AT_name to the type. */
8727 if (name
8728 && (TREE_CODE (name) != TYPE_DECL
8729 || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
8731 if (TREE_CODE (name) == TYPE_DECL)
8732 /* Could just call add_name_and_src_coords_attributes here,
8733 but since this is a builtin type it doesn't have any
8734 useful source coordinates anyway. */
8735 name = DECL_NAME (name);
8736 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8739 if (qualified_type)
8740 equate_type_number_to_die (qualified_type, mod_type_die);
8742 if (item_type)
8743 /* We must do this after the equate_type_number_to_die call, in case
8744 this is a recursive type. This ensures that the modified_type_die
8745 recursion will terminate even if the type is recursive. Recursive
8746 types are possible in Ada. */
8747 sub_die = modified_type_die (item_type,
8748 TYPE_READONLY (item_type),
8749 TYPE_VOLATILE (item_type),
8750 context_die);
8752 if (sub_die != NULL)
8753 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8755 return mod_type_die;
8758 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8759 an enumerated type. */
8761 static inline int
8762 type_is_enum (const_tree type)
8764 return TREE_CODE (type) == ENUMERAL_TYPE;
8767 /* Return the DBX register number described by a given RTL node. */
8769 static unsigned int
8770 dbx_reg_number (const_rtx rtl)
8772 unsigned regno = REGNO (rtl);
8774 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8776 #ifdef LEAF_REG_REMAP
8777 if (current_function_uses_only_leaf_regs)
8779 int leaf_reg = LEAF_REG_REMAP (regno);
8780 if (leaf_reg != -1)
8781 regno = (unsigned) leaf_reg;
8783 #endif
8785 return DBX_REGISTER_NUMBER (regno);
8788 /* Optionally add a DW_OP_piece term to a location description expression.
8789 DW_OP_piece is only added if the location description expression already
8790 doesn't end with DW_OP_piece. */
8792 static void
8793 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8795 dw_loc_descr_ref loc;
8797 if (*list_head != NULL)
8799 /* Find the end of the chain. */
8800 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8803 if (loc->dw_loc_opc != DW_OP_piece)
8804 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8808 /* Return a location descriptor that designates a machine register or
8809 zero if there is none. */
8811 static dw_loc_descr_ref
8812 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
8814 rtx regs;
8816 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8817 return 0;
8819 regs = targetm.dwarf_register_span (rtl);
8821 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8822 return multiple_reg_loc_descriptor (rtl, regs, initialized);
8823 else
8824 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
8827 /* Return a location descriptor that designates a machine register for
8828 a given hard register number. */
8830 static dw_loc_descr_ref
8831 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
8833 dw_loc_descr_ref reg_loc_descr;
8834 if (regno <= 31)
8835 reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8836 else
8837 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
8839 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8840 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8842 return reg_loc_descr;
8845 /* Given an RTL of a register, return a location descriptor that
8846 designates a value that spans more than one register. */
8848 static dw_loc_descr_ref
8849 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
8850 enum var_init_status initialized)
8852 int nregs, size, i;
8853 unsigned reg;
8854 dw_loc_descr_ref loc_result = NULL;
8856 reg = REGNO (rtl);
8857 #ifdef LEAF_REG_REMAP
8858 if (current_function_uses_only_leaf_regs)
8860 int leaf_reg = LEAF_REG_REMAP (reg);
8861 if (leaf_reg != -1)
8862 reg = (unsigned) leaf_reg;
8864 #endif
8865 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8866 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8868 /* Simple, contiguous registers. */
8869 if (regs == NULL_RTX)
8871 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8873 loc_result = NULL;
8874 while (nregs--)
8876 dw_loc_descr_ref t;
8878 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
8879 VAR_INIT_STATUS_INITIALIZED);
8880 add_loc_descr (&loc_result, t);
8881 add_loc_descr_op_piece (&loc_result, size);
8882 ++reg;
8884 return loc_result;
8887 /* Now onto stupid register sets in non contiguous locations. */
8889 gcc_assert (GET_CODE (regs) == PARALLEL);
8891 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8892 loc_result = NULL;
8894 for (i = 0; i < XVECLEN (regs, 0); ++i)
8896 dw_loc_descr_ref t;
8898 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
8899 VAR_INIT_STATUS_INITIALIZED);
8900 add_loc_descr (&loc_result, t);
8901 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8902 add_loc_descr_op_piece (&loc_result, size);
8905 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8906 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8907 return loc_result;
8910 /* Return a location descriptor that designates a constant. */
8912 static dw_loc_descr_ref
8913 int_loc_descriptor (HOST_WIDE_INT i)
8915 enum dwarf_location_atom op;
8917 /* Pick the smallest representation of a constant, rather than just
8918 defaulting to the LEB encoding. */
8919 if (i >= 0)
8921 if (i <= 31)
8922 op = DW_OP_lit0 + i;
8923 else if (i <= 0xff)
8924 op = DW_OP_const1u;
8925 else if (i <= 0xffff)
8926 op = DW_OP_const2u;
8927 else if (HOST_BITS_PER_WIDE_INT == 32
8928 || i <= 0xffffffff)
8929 op = DW_OP_const4u;
8930 else
8931 op = DW_OP_constu;
8933 else
8935 if (i >= -0x80)
8936 op = DW_OP_const1s;
8937 else if (i >= -0x8000)
8938 op = DW_OP_const2s;
8939 else if (HOST_BITS_PER_WIDE_INT == 32
8940 || i >= -0x80000000)
8941 op = DW_OP_const4s;
8942 else
8943 op = DW_OP_consts;
8946 return new_loc_descr (op, i, 0);
8949 /* Return a location descriptor that designates a base+offset location. */
8951 static dw_loc_descr_ref
8952 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
8953 enum var_init_status initialized)
8955 unsigned int regno;
8956 dw_loc_descr_ref result;
8958 /* We only use "frame base" when we're sure we're talking about the
8959 post-prologue local stack frame. We do this by *not* running
8960 register elimination until this point, and recognizing the special
8961 argument pointer and soft frame pointer rtx's. */
8962 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8964 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8966 if (elim != reg)
8968 if (GET_CODE (elim) == PLUS)
8970 offset += INTVAL (XEXP (elim, 1));
8971 elim = XEXP (elim, 0);
8973 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8974 : stack_pointer_rtx));
8975 offset += frame_pointer_fb_offset;
8977 return new_loc_descr (DW_OP_fbreg, offset, 0);
8981 regno = dbx_reg_number (reg);
8982 if (regno <= 31)
8983 result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
8984 else
8985 result = new_loc_descr (DW_OP_bregx, regno, offset);
8987 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8988 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8990 return result;
8993 /* Return true if this RTL expression describes a base+offset calculation. */
8995 static inline int
8996 is_based_loc (const_rtx rtl)
8998 return (GET_CODE (rtl) == PLUS
8999 && ((REG_P (XEXP (rtl, 0))
9000 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9001 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9004 /* Return a descriptor that describes the concatenation of N locations
9005 used to form the address of a memory location. */
9007 static dw_loc_descr_ref
9008 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9009 enum var_init_status initialized)
9011 unsigned int i;
9012 dw_loc_descr_ref cc_loc_result = NULL;
9013 unsigned int n = XVECLEN (concatn, 0);
9015 for (i = 0; i < n; ++i)
9017 dw_loc_descr_ref ref;
9018 rtx x = XVECEXP (concatn, 0, i);
9020 ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9021 if (ref == NULL)
9022 return NULL;
9024 add_loc_descr (&cc_loc_result, ref);
9025 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9028 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9029 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9031 return cc_loc_result;
9034 /* The following routine converts the RTL for a variable or parameter
9035 (resident in memory) into an equivalent Dwarf representation of a
9036 mechanism for getting the address of that same variable onto the top of a
9037 hypothetical "address evaluation" stack.
9039 When creating memory location descriptors, we are effectively transforming
9040 the RTL for a memory-resident object into its Dwarf postfix expression
9041 equivalent. This routine recursively descends an RTL tree, turning
9042 it into Dwarf postfix code as it goes.
9044 MODE is the mode of the memory reference, needed to handle some
9045 autoincrement addressing modes.
9047 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9048 location list for RTL.
9050 Return 0 if we can't represent the location. */
9052 static dw_loc_descr_ref
9053 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9054 enum var_init_status initialized)
9056 dw_loc_descr_ref mem_loc_result = NULL;
9057 enum dwarf_location_atom op;
9059 /* Note that for a dynamically sized array, the location we will generate a
9060 description of here will be the lowest numbered location which is
9061 actually within the array. That's *not* necessarily the same as the
9062 zeroth element of the array. */
9064 rtl = targetm.delegitimize_address (rtl);
9066 switch (GET_CODE (rtl))
9068 case POST_INC:
9069 case POST_DEC:
9070 case POST_MODIFY:
9071 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
9072 just fall into the SUBREG code. */
9074 /* ... fall through ... */
9076 case SUBREG:
9077 /* The case of a subreg may arise when we have a local (register)
9078 variable or a formal (register) parameter which doesn't quite fill
9079 up an entire register. For now, just assume that it is
9080 legitimate to make the Dwarf info refer to the whole register which
9081 contains the given subreg. */
9082 rtl = XEXP (rtl, 0);
9084 /* ... fall through ... */
9086 case REG:
9087 /* Whenever a register number forms a part of the description of the
9088 method for calculating the (dynamic) address of a memory resident
9089 object, DWARF rules require the register number be referred to as
9090 a "base register". This distinction is not based in any way upon
9091 what category of register the hardware believes the given register
9092 belongs to. This is strictly DWARF terminology we're dealing with
9093 here. Note that in cases where the location of a memory-resident
9094 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9095 OP_CONST (0)) the actual DWARF location descriptor that we generate
9096 may just be OP_BASEREG (basereg). This may look deceptively like
9097 the object in question was allocated to a register (rather than in
9098 memory) so DWARF consumers need to be aware of the subtle
9099 distinction between OP_REG and OP_BASEREG. */
9100 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9101 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9102 break;
9104 case MEM:
9105 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9106 VAR_INIT_STATUS_INITIALIZED);
9107 if (mem_loc_result != 0)
9108 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9109 break;
9111 case LO_SUM:
9112 rtl = XEXP (rtl, 1);
9114 /* ... fall through ... */
9116 case LABEL_REF:
9117 /* Some ports can transform a symbol ref into a label ref, because
9118 the symbol ref is too far away and has to be dumped into a constant
9119 pool. */
9120 case CONST:
9121 case SYMBOL_REF:
9122 /* Alternatively, the symbol in the constant pool might be referenced
9123 by a different symbol. */
9124 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9126 bool marked;
9127 rtx tmp = get_pool_constant_mark (rtl, &marked);
9129 if (GET_CODE (tmp) == SYMBOL_REF)
9131 rtl = tmp;
9132 if (CONSTANT_POOL_ADDRESS_P (tmp))
9133 get_pool_constant_mark (tmp, &marked);
9134 else
9135 marked = true;
9138 /* If all references to this pool constant were optimized away,
9139 it was not output and thus we can't represent it.
9140 FIXME: might try to use DW_OP_const_value here, though
9141 DW_OP_piece complicates it. */
9142 if (!marked)
9143 return 0;
9146 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9147 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9148 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9149 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9150 break;
9152 case PRE_MODIFY:
9153 /* Extract the PLUS expression nested inside and fall into
9154 PLUS code below. */
9155 rtl = XEXP (rtl, 1);
9156 goto plus;
9158 case PRE_INC:
9159 case PRE_DEC:
9160 /* Turn these into a PLUS expression and fall into the PLUS code
9161 below. */
9162 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
9163 GEN_INT (GET_CODE (rtl) == PRE_INC
9164 ? GET_MODE_UNIT_SIZE (mode)
9165 : -GET_MODE_UNIT_SIZE (mode)));
9167 /* ... fall through ... */
9169 case PLUS:
9170 plus:
9171 if (is_based_loc (rtl))
9172 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
9173 INTVAL (XEXP (rtl, 1)),
9174 VAR_INIT_STATUS_INITIALIZED);
9175 else
9177 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
9178 VAR_INIT_STATUS_INITIALIZED);
9179 if (mem_loc_result == 0)
9180 break;
9182 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
9183 && INTVAL (XEXP (rtl, 1)) >= 0)
9184 add_loc_descr (&mem_loc_result,
9185 new_loc_descr (DW_OP_plus_uconst,
9186 INTVAL (XEXP (rtl, 1)), 0));
9187 else
9189 add_loc_descr (&mem_loc_result,
9190 mem_loc_descriptor (XEXP (rtl, 1), mode,
9191 VAR_INIT_STATUS_INITIALIZED));
9192 add_loc_descr (&mem_loc_result,
9193 new_loc_descr (DW_OP_plus, 0, 0));
9196 break;
9198 /* If a pseudo-reg is optimized away, it is possible for it to
9199 be replaced with a MEM containing a multiply or shift. */
9200 case MULT:
9201 op = DW_OP_mul;
9202 goto do_binop;
9204 case ASHIFT:
9205 op = DW_OP_shl;
9206 goto do_binop;
9208 case ASHIFTRT:
9209 op = DW_OP_shra;
9210 goto do_binop;
9212 case LSHIFTRT:
9213 op = DW_OP_shr;
9214 goto do_binop;
9216 do_binop:
9218 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
9219 VAR_INIT_STATUS_INITIALIZED);
9220 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
9221 VAR_INIT_STATUS_INITIALIZED);
9223 if (op0 == 0 || op1 == 0)
9224 break;
9226 mem_loc_result = op0;
9227 add_loc_descr (&mem_loc_result, op1);
9228 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9229 break;
9232 case CONST_INT:
9233 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9234 break;
9236 case CONCATN:
9237 mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
9238 VAR_INIT_STATUS_INITIALIZED);
9239 break;
9241 default:
9242 gcc_unreachable ();
9245 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9246 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9248 return mem_loc_result;
9251 /* Return a descriptor that describes the concatenation of two locations.
9252 This is typically a complex variable. */
9254 static dw_loc_descr_ref
9255 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
9257 dw_loc_descr_ref cc_loc_result = NULL;
9258 dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
9259 dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
9261 if (x0_ref == 0 || x1_ref == 0)
9262 return 0;
9264 cc_loc_result = x0_ref;
9265 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9267 add_loc_descr (&cc_loc_result, x1_ref);
9268 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9270 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9271 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9273 return cc_loc_result;
9276 /* Return a descriptor that describes the concatenation of N
9277 locations. */
9279 static dw_loc_descr_ref
9280 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
9282 unsigned int i;
9283 dw_loc_descr_ref cc_loc_result = NULL;
9284 unsigned int n = XVECLEN (concatn, 0);
9286 for (i = 0; i < n; ++i)
9288 dw_loc_descr_ref ref;
9289 rtx x = XVECEXP (concatn, 0, i);
9291 ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
9292 if (ref == NULL)
9293 return NULL;
9295 add_loc_descr (&cc_loc_result, ref);
9296 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9299 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9300 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9302 return cc_loc_result;
9305 /* Output a proper Dwarf location descriptor for a variable or parameter
9306 which is either allocated in a register or in a memory location. For a
9307 register, we just generate an OP_REG and the register number. For a
9308 memory location we provide a Dwarf postfix expression describing how to
9309 generate the (dynamic) address of the object onto the address stack.
9311 If we don't know how to describe it, return 0. */
9313 static dw_loc_descr_ref
9314 loc_descriptor (rtx rtl, enum var_init_status initialized)
9316 dw_loc_descr_ref loc_result = NULL;
9318 switch (GET_CODE (rtl))
9320 case SUBREG:
9321 /* The case of a subreg may arise when we have a local (register)
9322 variable or a formal (register) parameter which doesn't quite fill
9323 up an entire register. For now, just assume that it is
9324 legitimate to make the Dwarf info refer to the whole register which
9325 contains the given subreg. */
9326 rtl = SUBREG_REG (rtl);
9328 /* ... fall through ... */
9330 case REG:
9331 loc_result = reg_loc_descriptor (rtl, initialized);
9332 break;
9334 case MEM:
9335 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9336 initialized);
9337 break;
9339 case CONCAT:
9340 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
9341 initialized);
9342 break;
9344 case CONCATN:
9345 loc_result = concatn_loc_descriptor (rtl, initialized);
9346 break;
9348 case VAR_LOCATION:
9349 /* Single part. */
9350 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9352 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
9353 break;
9356 rtl = XEXP (rtl, 1);
9357 /* FALLTHRU */
9359 case PARALLEL:
9361 rtvec par_elems = XVEC (rtl, 0);
9362 int num_elem = GET_NUM_ELEM (par_elems);
9363 enum machine_mode mode;
9364 int i;
9366 /* Create the first one, so we have something to add to. */
9367 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
9368 initialized);
9369 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9370 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9371 for (i = 1; i < num_elem; i++)
9373 dw_loc_descr_ref temp;
9375 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
9376 initialized);
9377 add_loc_descr (&loc_result, temp);
9378 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9379 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9382 break;
9384 default:
9385 gcc_unreachable ();
9388 return loc_result;
9391 /* Similar, but generate the descriptor from trees instead of rtl. This comes
9392 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
9393 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9394 top-level invocation, and we require the address of LOC; is 0 if we require
9395 the value of LOC. */
9397 static dw_loc_descr_ref
9398 loc_descriptor_from_tree_1 (tree loc, int want_address)
9400 dw_loc_descr_ref ret, ret1;
9401 int have_address = 0;
9402 enum dwarf_location_atom op;
9404 /* ??? Most of the time we do not take proper care for sign/zero
9405 extending the values properly. Hopefully this won't be a real
9406 problem... */
9408 switch (TREE_CODE (loc))
9410 case ERROR_MARK:
9411 return 0;
9413 case PLACEHOLDER_EXPR:
9414 /* This case involves extracting fields from an object to determine the
9415 position of other fields. We don't try to encode this here. The
9416 only user of this is Ada, which encodes the needed information using
9417 the names of types. */
9418 return 0;
9420 case CALL_EXPR:
9421 return 0;
9423 case PREINCREMENT_EXPR:
9424 case PREDECREMENT_EXPR:
9425 case POSTINCREMENT_EXPR:
9426 case POSTDECREMENT_EXPR:
9427 /* There are no opcodes for these operations. */
9428 return 0;
9430 case ADDR_EXPR:
9431 /* If we already want an address, there's nothing we can do. */
9432 if (want_address)
9433 return 0;
9435 /* Otherwise, process the argument and look for the address. */
9436 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9438 case VAR_DECL:
9439 if (DECL_THREAD_LOCAL_P (loc))
9441 rtx rtl;
9443 /* If this is not defined, we have no way to emit the data. */
9444 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
9445 return 0;
9447 /* The way DW_OP_GNU_push_tls_address is specified, we can only
9448 look up addresses of objects in the current module. */
9449 if (DECL_EXTERNAL (loc))
9450 return 0;
9452 rtl = rtl_for_decl_location (loc);
9453 if (rtl == NULL_RTX)
9454 return 0;
9456 if (!MEM_P (rtl))
9457 return 0;
9458 rtl = XEXP (rtl, 0);
9459 if (! CONSTANT_P (rtl))
9460 return 0;
9462 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9463 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9464 ret->dw_loc_oprnd1.v.val_addr = rtl;
9466 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9467 add_loc_descr (&ret, ret1);
9469 have_address = 1;
9470 break;
9472 /* FALLTHRU */
9474 case PARM_DECL:
9475 if (DECL_HAS_VALUE_EXPR_P (loc))
9476 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9477 want_address);
9478 /* FALLTHRU */
9480 case RESULT_DECL:
9481 case FUNCTION_DECL:
9483 rtx rtl = rtl_for_decl_location (loc);
9485 if (rtl == NULL_RTX)
9486 return 0;
9487 else if (GET_CODE (rtl) == CONST_INT)
9489 HOST_WIDE_INT val = INTVAL (rtl);
9490 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9491 val &= GET_MODE_MASK (DECL_MODE (loc));
9492 ret = int_loc_descriptor (val);
9494 else if (GET_CODE (rtl) == CONST_STRING)
9495 return 0;
9496 else if (CONSTANT_P (rtl))
9498 ret = new_loc_descr (DW_OP_addr, 0, 0);
9499 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9500 ret->dw_loc_oprnd1.v.val_addr = rtl;
9502 else
9504 enum machine_mode mode;
9506 /* Certain constructs can only be represented at top-level. */
9507 if (want_address == 2)
9508 return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
9510 mode = GET_MODE (rtl);
9511 if (MEM_P (rtl))
9513 rtl = XEXP (rtl, 0);
9514 have_address = 1;
9516 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9519 break;
9521 case INDIRECT_REF:
9522 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9523 have_address = 1;
9524 break;
9526 case COMPOUND_EXPR:
9527 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9529 case NOP_EXPR:
9530 case CONVERT_EXPR:
9531 case NON_LVALUE_EXPR:
9532 case VIEW_CONVERT_EXPR:
9533 case SAVE_EXPR:
9534 case GIMPLE_MODIFY_STMT:
9535 return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9536 want_address);
9538 case COMPONENT_REF:
9539 case BIT_FIELD_REF:
9540 case ARRAY_REF:
9541 case ARRAY_RANGE_REF:
9543 tree obj, offset;
9544 HOST_WIDE_INT bitsize, bitpos, bytepos;
9545 enum machine_mode mode;
9546 int volatilep;
9547 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9549 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9550 &unsignedp, &volatilep, false);
9552 if (obj == loc)
9553 return 0;
9555 ret = loc_descriptor_from_tree_1 (obj, 1);
9556 if (ret == 0
9557 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9558 return 0;
9560 if (offset != NULL_TREE)
9562 /* Variable offset. */
9563 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9564 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9567 bytepos = bitpos / BITS_PER_UNIT;
9568 if (bytepos > 0)
9569 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9570 else if (bytepos < 0)
9572 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9573 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9576 have_address = 1;
9577 break;
9580 case INTEGER_CST:
9581 if (host_integerp (loc, 0))
9582 ret = int_loc_descriptor (tree_low_cst (loc, 0));
9583 else
9584 return 0;
9585 break;
9587 case CONSTRUCTOR:
9589 /* Get an RTL for this, if something has been emitted. */
9590 rtx rtl = lookup_constant_def (loc);
9591 enum machine_mode mode;
9593 if (!rtl || !MEM_P (rtl))
9594 return 0;
9595 mode = GET_MODE (rtl);
9596 rtl = XEXP (rtl, 0);
9597 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9598 have_address = 1;
9599 break;
9602 case TRUTH_AND_EXPR:
9603 case TRUTH_ANDIF_EXPR:
9604 case BIT_AND_EXPR:
9605 op = DW_OP_and;
9606 goto do_binop;
9608 case TRUTH_XOR_EXPR:
9609 case BIT_XOR_EXPR:
9610 op = DW_OP_xor;
9611 goto do_binop;
9613 case TRUTH_OR_EXPR:
9614 case TRUTH_ORIF_EXPR:
9615 case BIT_IOR_EXPR:
9616 op = DW_OP_or;
9617 goto do_binop;
9619 case FLOOR_DIV_EXPR:
9620 case CEIL_DIV_EXPR:
9621 case ROUND_DIV_EXPR:
9622 case TRUNC_DIV_EXPR:
9623 op = DW_OP_div;
9624 goto do_binop;
9626 case MINUS_EXPR:
9627 op = DW_OP_minus;
9628 goto do_binop;
9630 case FLOOR_MOD_EXPR:
9631 case CEIL_MOD_EXPR:
9632 case ROUND_MOD_EXPR:
9633 case TRUNC_MOD_EXPR:
9634 op = DW_OP_mod;
9635 goto do_binop;
9637 case MULT_EXPR:
9638 op = DW_OP_mul;
9639 goto do_binop;
9641 case LSHIFT_EXPR:
9642 op = DW_OP_shl;
9643 goto do_binop;
9645 case RSHIFT_EXPR:
9646 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9647 goto do_binop;
9649 case POINTER_PLUS_EXPR:
9650 case PLUS_EXPR:
9651 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9652 && host_integerp (TREE_OPERAND (loc, 1), 0))
9654 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9655 if (ret == 0)
9656 return 0;
9658 add_loc_descr (&ret,
9659 new_loc_descr (DW_OP_plus_uconst,
9660 tree_low_cst (TREE_OPERAND (loc, 1),
9662 0));
9663 break;
9666 op = DW_OP_plus;
9667 goto do_binop;
9669 case LE_EXPR:
9670 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9671 return 0;
9673 op = DW_OP_le;
9674 goto do_binop;
9676 case GE_EXPR:
9677 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9678 return 0;
9680 op = DW_OP_ge;
9681 goto do_binop;
9683 case LT_EXPR:
9684 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9685 return 0;
9687 op = DW_OP_lt;
9688 goto do_binop;
9690 case GT_EXPR:
9691 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9692 return 0;
9694 op = DW_OP_gt;
9695 goto do_binop;
9697 case EQ_EXPR:
9698 op = DW_OP_eq;
9699 goto do_binop;
9701 case NE_EXPR:
9702 op = DW_OP_ne;
9703 goto do_binop;
9705 do_binop:
9706 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9707 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9708 if (ret == 0 || ret1 == 0)
9709 return 0;
9711 add_loc_descr (&ret, ret1);
9712 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9713 break;
9715 case TRUTH_NOT_EXPR:
9716 case BIT_NOT_EXPR:
9717 op = DW_OP_not;
9718 goto do_unop;
9720 case ABS_EXPR:
9721 op = DW_OP_abs;
9722 goto do_unop;
9724 case NEGATE_EXPR:
9725 op = DW_OP_neg;
9726 goto do_unop;
9728 do_unop:
9729 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9730 if (ret == 0)
9731 return 0;
9733 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9734 break;
9736 case MIN_EXPR:
9737 case MAX_EXPR:
9739 const enum tree_code code =
9740 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9742 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9743 build2 (code, integer_type_node,
9744 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9745 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9748 /* ... fall through ... */
9750 case COND_EXPR:
9752 dw_loc_descr_ref lhs
9753 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9754 dw_loc_descr_ref rhs
9755 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9756 dw_loc_descr_ref bra_node, jump_node, tmp;
9758 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9759 if (ret == 0 || lhs == 0 || rhs == 0)
9760 return 0;
9762 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9763 add_loc_descr (&ret, bra_node);
9765 add_loc_descr (&ret, rhs);
9766 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9767 add_loc_descr (&ret, jump_node);
9769 add_loc_descr (&ret, lhs);
9770 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9771 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9773 /* ??? Need a node to point the skip at. Use a nop. */
9774 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9775 add_loc_descr (&ret, tmp);
9776 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9777 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9779 break;
9781 case FIX_TRUNC_EXPR:
9782 return 0;
9784 default:
9785 /* Leave front-end specific codes as simply unknown. This comes
9786 up, for instance, with the C STMT_EXPR. */
9787 if ((unsigned int) TREE_CODE (loc)
9788 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9789 return 0;
9791 #ifdef ENABLE_CHECKING
9792 /* Otherwise this is a generic code; we should just lists all of
9793 these explicitly. We forgot one. */
9794 gcc_unreachable ();
9795 #else
9796 /* In a release build, we want to degrade gracefully: better to
9797 generate incomplete debugging information than to crash. */
9798 return NULL;
9799 #endif
9802 /* Show if we can't fill the request for an address. */
9803 if (want_address && !have_address)
9804 return 0;
9806 /* If we've got an address and don't want one, dereference. */
9807 if (!want_address && have_address && ret)
9809 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9811 if (size > DWARF2_ADDR_SIZE || size == -1)
9812 return 0;
9813 else if (size == DWARF2_ADDR_SIZE)
9814 op = DW_OP_deref;
9815 else
9816 op = DW_OP_deref_size;
9818 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9821 return ret;
9824 static inline dw_loc_descr_ref
9825 loc_descriptor_from_tree (tree loc)
9827 return loc_descriptor_from_tree_1 (loc, 2);
9830 /* Given a value, round it up to the lowest multiple of `boundary'
9831 which is not less than the value itself. */
9833 static inline HOST_WIDE_INT
9834 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9836 return (((value + boundary - 1) / boundary) * boundary);
9839 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9840 pointer to the declared type for the relevant field variable, or return
9841 `integer_type_node' if the given node turns out to be an
9842 ERROR_MARK node. */
9844 static inline tree
9845 field_type (const_tree decl)
9847 tree type;
9849 if (TREE_CODE (decl) == ERROR_MARK)
9850 return integer_type_node;
9852 type = DECL_BIT_FIELD_TYPE (decl);
9853 if (type == NULL_TREE)
9854 type = TREE_TYPE (decl);
9856 return type;
9859 /* Given a pointer to a tree node, return the alignment in bits for
9860 it, or else return BITS_PER_WORD if the node actually turns out to
9861 be an ERROR_MARK node. */
9863 static inline unsigned
9864 simple_type_align_in_bits (const_tree type)
9866 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9869 static inline unsigned
9870 simple_decl_align_in_bits (const_tree decl)
9872 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9875 /* Return the result of rounding T up to ALIGN. */
9877 static inline HOST_WIDE_INT
9878 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9880 /* We must be careful if T is negative because HOST_WIDE_INT can be
9881 either "above" or "below" unsigned int as per the C promotion
9882 rules, depending on the host, thus making the signedness of the
9883 direct multiplication and division unpredictable. */
9884 unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9886 u += align - 1;
9887 u /= align;
9888 u *= align;
9890 return (HOST_WIDE_INT) u;
9893 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9894 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9895 or return 0 if we are unable to determine what that offset is, either
9896 because the argument turns out to be a pointer to an ERROR_MARK node, or
9897 because the offset is actually variable. (We can't handle the latter case
9898 just yet). */
9900 static HOST_WIDE_INT
9901 field_byte_offset (const_tree decl)
9903 HOST_WIDE_INT object_offset_in_bits;
9904 HOST_WIDE_INT bitpos_int;
9906 if (TREE_CODE (decl) == ERROR_MARK)
9907 return 0;
9909 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9911 /* We cannot yet cope with fields whose positions are variable, so
9912 for now, when we see such things, we simply return 0. Someday, we may
9913 be able to handle such cases, but it will be damn difficult. */
9914 if (! host_integerp (bit_position (decl), 0))
9915 return 0;
9917 bitpos_int = int_bit_position (decl);
9919 #ifdef PCC_BITFIELD_TYPE_MATTERS
9920 if (PCC_BITFIELD_TYPE_MATTERS)
9922 tree type;
9923 tree field_size_tree;
9924 HOST_WIDE_INT deepest_bitpos;
9925 unsigned HOST_WIDE_INT field_size_in_bits;
9926 unsigned int type_align_in_bits;
9927 unsigned int decl_align_in_bits;
9928 unsigned HOST_WIDE_INT type_size_in_bits;
9930 type = field_type (decl);
9931 field_size_tree = DECL_SIZE (decl);
9933 /* The size could be unspecified if there was an error, or for
9934 a flexible array member. */
9935 if (! field_size_tree)
9936 field_size_tree = bitsize_zero_node;
9938 /* If we don't know the size of the field, pretend it's a full word. */
9939 if (host_integerp (field_size_tree, 1))
9940 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9941 else
9942 field_size_in_bits = BITS_PER_WORD;
9944 type_size_in_bits = simple_type_size_in_bits (type);
9945 type_align_in_bits = simple_type_align_in_bits (type);
9946 decl_align_in_bits = simple_decl_align_in_bits (decl);
9948 /* The GCC front-end doesn't make any attempt to keep track of the
9949 starting bit offset (relative to the start of the containing
9950 structure type) of the hypothetical "containing object" for a
9951 bit-field. Thus, when computing the byte offset value for the
9952 start of the "containing object" of a bit-field, we must deduce
9953 this information on our own. This can be rather tricky to do in
9954 some cases. For example, handling the following structure type
9955 definition when compiling for an i386/i486 target (which only
9956 aligns long long's to 32-bit boundaries) can be very tricky:
9958 struct S { int field1; long long field2:31; };
9960 Fortunately, there is a simple rule-of-thumb which can be used
9961 in such cases. When compiling for an i386/i486, GCC will
9962 allocate 8 bytes for the structure shown above. It decides to
9963 do this based upon one simple rule for bit-field allocation.
9964 GCC allocates each "containing object" for each bit-field at
9965 the first (i.e. lowest addressed) legitimate alignment boundary
9966 (based upon the required minimum alignment for the declared
9967 type of the field) which it can possibly use, subject to the
9968 condition that there is still enough available space remaining
9969 in the containing object (when allocated at the selected point)
9970 to fully accommodate all of the bits of the bit-field itself.
9972 This simple rule makes it obvious why GCC allocates 8 bytes for
9973 each object of the structure type shown above. When looking
9974 for a place to allocate the "containing object" for `field2',
9975 the compiler simply tries to allocate a 64-bit "containing
9976 object" at each successive 32-bit boundary (starting at zero)
9977 until it finds a place to allocate that 64- bit field such that
9978 at least 31 contiguous (and previously unallocated) bits remain
9979 within that selected 64 bit field. (As it turns out, for the
9980 example above, the compiler finds it is OK to allocate the
9981 "containing object" 64-bit field at bit-offset zero within the
9982 structure type.)
9984 Here we attempt to work backwards from the limited set of facts
9985 we're given, and we try to deduce from those facts, where GCC
9986 must have believed that the containing object started (within
9987 the structure type). The value we deduce is then used (by the
9988 callers of this routine) to generate DW_AT_location and
9989 DW_AT_bit_offset attributes for fields (both bit-fields and, in
9990 the case of DW_AT_location, regular fields as well). */
9992 /* Figure out the bit-distance from the start of the structure to
9993 the "deepest" bit of the bit-field. */
9994 deepest_bitpos = bitpos_int + field_size_in_bits;
9996 /* This is the tricky part. Use some fancy footwork to deduce
9997 where the lowest addressed bit of the containing object must
9998 be. */
9999 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10001 /* Round up to type_align by default. This works best for
10002 bitfields. */
10003 object_offset_in_bits
10004 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10006 if (object_offset_in_bits > bitpos_int)
10008 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10010 /* Round up to decl_align instead. */
10011 object_offset_in_bits
10012 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10015 else
10016 #endif
10017 object_offset_in_bits = bitpos_int;
10019 return object_offset_in_bits / BITS_PER_UNIT;
10022 /* The following routines define various Dwarf attributes and any data
10023 associated with them. */
10025 /* Add a location description attribute value to a DIE.
10027 This emits location attributes suitable for whole variables and
10028 whole parameters. Note that the location attributes for struct fields are
10029 generated by the routine `data_member_location_attribute' below. */
10031 static inline void
10032 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10033 dw_loc_descr_ref descr)
10035 if (descr != 0)
10036 add_AT_loc (die, attr_kind, descr);
10039 /* Attach the specialized form of location attribute used for data members of
10040 struct and union types. In the special case of a FIELD_DECL node which
10041 represents a bit-field, the "offset" part of this special location
10042 descriptor must indicate the distance in bytes from the lowest-addressed
10043 byte of the containing struct or union type to the lowest-addressed byte of
10044 the "containing object" for the bit-field. (See the `field_byte_offset'
10045 function above).
10047 For any given bit-field, the "containing object" is a hypothetical object
10048 (of some integral or enum type) within which the given bit-field lives. The
10049 type of this hypothetical "containing object" is always the same as the
10050 declared type of the individual bit-field itself (for GCC anyway... the
10051 DWARF spec doesn't actually mandate this). Note that it is the size (in
10052 bytes) of the hypothetical "containing object" which will be given in the
10053 DW_AT_byte_size attribute for this bit-field. (See the
10054 `byte_size_attribute' function below.) It is also used when calculating the
10055 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
10056 function below.) */
10058 static void
10059 add_data_member_location_attribute (dw_die_ref die, tree decl)
10061 HOST_WIDE_INT offset;
10062 dw_loc_descr_ref loc_descr = 0;
10064 if (TREE_CODE (decl) == TREE_BINFO)
10066 /* We're working on the TAG_inheritance for a base class. */
10067 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10069 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10070 aren't at a fixed offset from all (sub)objects of the same
10071 type. We need to extract the appropriate offset from our
10072 vtable. The following dwarf expression means
10074 BaseAddr = ObAddr + *((*ObAddr) - Offset)
10076 This is specific to the V3 ABI, of course. */
10078 dw_loc_descr_ref tmp;
10080 /* Make a copy of the object address. */
10081 tmp = new_loc_descr (DW_OP_dup, 0, 0);
10082 add_loc_descr (&loc_descr, tmp);
10084 /* Extract the vtable address. */
10085 tmp = new_loc_descr (DW_OP_deref, 0, 0);
10086 add_loc_descr (&loc_descr, tmp);
10088 /* Calculate the address of the offset. */
10089 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10090 gcc_assert (offset < 0);
10092 tmp = int_loc_descriptor (-offset);
10093 add_loc_descr (&loc_descr, tmp);
10094 tmp = new_loc_descr (DW_OP_minus, 0, 0);
10095 add_loc_descr (&loc_descr, tmp);
10097 /* Extract the offset. */
10098 tmp = new_loc_descr (DW_OP_deref, 0, 0);
10099 add_loc_descr (&loc_descr, tmp);
10101 /* Add it to the object address. */
10102 tmp = new_loc_descr (DW_OP_plus, 0, 0);
10103 add_loc_descr (&loc_descr, tmp);
10105 else
10106 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10108 else
10109 offset = field_byte_offset (decl);
10111 if (! loc_descr)
10113 enum dwarf_location_atom op;
10115 /* The DWARF2 standard says that we should assume that the structure
10116 address is already on the stack, so we can specify a structure field
10117 address by using DW_OP_plus_uconst. */
10119 #ifdef MIPS_DEBUGGING_INFO
10120 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10121 operator correctly. It works only if we leave the offset on the
10122 stack. */
10123 op = DW_OP_constu;
10124 #else
10125 op = DW_OP_plus_uconst;
10126 #endif
10128 loc_descr = new_loc_descr (op, offset, 0);
10131 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10134 /* Writes integer values to dw_vec_const array. */
10136 static void
10137 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10139 while (size != 0)
10141 *dest++ = val & 0xff;
10142 val >>= 8;
10143 --size;
10147 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
10149 static HOST_WIDE_INT
10150 extract_int (const unsigned char *src, unsigned int size)
10152 HOST_WIDE_INT val = 0;
10154 src += size;
10155 while (size != 0)
10157 val <<= 8;
10158 val |= *--src & 0xff;
10159 --size;
10161 return val;
10164 /* Writes floating point values to dw_vec_const array. */
10166 static void
10167 insert_float (const_rtx rtl, unsigned char *array)
10169 REAL_VALUE_TYPE rv;
10170 long val[4];
10171 int i;
10173 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
10174 real_to_target (val, &rv, GET_MODE (rtl));
10176 /* real_to_target puts 32-bit pieces in each long. Pack them. */
10177 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
10179 insert_int (val[i], 4, array);
10180 array += 4;
10184 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
10185 does not have a "location" either in memory or in a register. These
10186 things can arise in GNU C when a constant is passed as an actual parameter
10187 to an inlined function. They can also arise in C++ where declared
10188 constants do not necessarily get memory "homes". */
10190 static void
10191 add_const_value_attribute (dw_die_ref die, rtx rtl)
10193 switch (GET_CODE (rtl))
10195 case CONST_INT:
10197 HOST_WIDE_INT val = INTVAL (rtl);
10199 if (val < 0)
10200 add_AT_int (die, DW_AT_const_value, val);
10201 else
10202 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
10204 break;
10206 case CONST_DOUBLE:
10207 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
10208 floating-point constant. A CONST_DOUBLE is used whenever the
10209 constant requires more than one word in order to be adequately
10210 represented. We output CONST_DOUBLEs as blocks. */
10212 enum machine_mode mode = GET_MODE (rtl);
10214 if (SCALAR_FLOAT_MODE_P (mode))
10216 unsigned int length = GET_MODE_SIZE (mode);
10217 unsigned char *array = ggc_alloc (length);
10219 insert_float (rtl, array);
10220 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10222 else
10224 /* ??? We really should be using HOST_WIDE_INT throughout. */
10225 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10227 add_AT_long_long (die, DW_AT_const_value,
10228 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10231 break;
10233 case CONST_VECTOR:
10235 enum machine_mode mode = GET_MODE (rtl);
10236 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10237 unsigned int length = CONST_VECTOR_NUNITS (rtl);
10238 unsigned char *array = ggc_alloc (length * elt_size);
10239 unsigned int i;
10240 unsigned char *p;
10242 switch (GET_MODE_CLASS (mode))
10244 case MODE_VECTOR_INT:
10245 for (i = 0, p = array; i < length; i++, p += elt_size)
10247 rtx elt = CONST_VECTOR_ELT (rtl, i);
10248 HOST_WIDE_INT lo, hi;
10250 switch (GET_CODE (elt))
10252 case CONST_INT:
10253 lo = INTVAL (elt);
10254 hi = -(lo < 0);
10255 break;
10257 case CONST_DOUBLE:
10258 lo = CONST_DOUBLE_LOW (elt);
10259 hi = CONST_DOUBLE_HIGH (elt);
10260 break;
10262 default:
10263 gcc_unreachable ();
10266 if (elt_size <= sizeof (HOST_WIDE_INT))
10267 insert_int (lo, elt_size, p);
10268 else
10270 unsigned char *p0 = p;
10271 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10273 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10274 if (WORDS_BIG_ENDIAN)
10276 p0 = p1;
10277 p1 = p;
10279 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10280 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10283 break;
10285 case MODE_VECTOR_FLOAT:
10286 for (i = 0, p = array; i < length; i++, p += elt_size)
10288 rtx elt = CONST_VECTOR_ELT (rtl, i);
10289 insert_float (elt, p);
10291 break;
10293 default:
10294 gcc_unreachable ();
10297 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10299 break;
10301 case CONST_STRING:
10302 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10303 break;
10305 case SYMBOL_REF:
10306 case LABEL_REF:
10307 case CONST:
10308 add_AT_addr (die, DW_AT_const_value, rtl);
10309 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10310 break;
10312 case PLUS:
10313 /* In cases where an inlined instance of an inline function is passed
10314 the address of an `auto' variable (which is local to the caller) we
10315 can get a situation where the DECL_RTL of the artificial local
10316 variable (for the inlining) which acts as a stand-in for the
10317 corresponding formal parameter (of the inline function) will look
10318 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
10319 exactly a compile-time constant expression, but it isn't the address
10320 of the (artificial) local variable either. Rather, it represents the
10321 *value* which the artificial local variable always has during its
10322 lifetime. We currently have no way to represent such quasi-constant
10323 values in Dwarf, so for now we just punt and generate nothing. */
10324 break;
10326 default:
10327 /* No other kinds of rtx should be possible here. */
10328 gcc_unreachable ();
10333 /* Determine whether the evaluation of EXPR references any variables
10334 or functions which aren't otherwise used (and therefore may not be
10335 output). */
10336 static tree
10337 reference_to_unused (tree * tp, int * walk_subtrees,
10338 void * data ATTRIBUTE_UNUSED)
10340 if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10341 *walk_subtrees = 0;
10343 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10344 && ! TREE_ASM_WRITTEN (*tp))
10345 return *tp;
10346 else if (!flag_unit_at_a_time)
10347 return NULL_TREE;
10348 /* ??? The C++ FE emits debug information for using decls, so
10349 putting gcc_unreachable here falls over. See PR31899. For now
10350 be conservative. */
10351 else if (!cgraph_global_info_ready
10352 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10353 return *tp;
10354 else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10356 struct varpool_node *node = varpool_node (*tp);
10357 if (!node->needed)
10358 return *tp;
10360 else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10361 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10363 struct cgraph_node *node = cgraph_node (*tp);
10364 if (!node->output)
10365 return *tp;
10368 return NULL_TREE;
10371 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10372 for use in a later add_const_value_attribute call. */
10374 static rtx
10375 rtl_for_decl_init (tree init, tree type)
10377 rtx rtl = NULL_RTX;
10379 /* If a variable is initialized with a string constant without embedded
10380 zeros, build CONST_STRING. */
10381 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10383 tree enttype = TREE_TYPE (type);
10384 tree domain = TYPE_DOMAIN (type);
10385 enum machine_mode mode = TYPE_MODE (enttype);
10387 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10388 && domain
10389 && integer_zerop (TYPE_MIN_VALUE (domain))
10390 && compare_tree_int (TYPE_MAX_VALUE (domain),
10391 TREE_STRING_LENGTH (init) - 1) == 0
10392 && ((size_t) TREE_STRING_LENGTH (init)
10393 == strlen (TREE_STRING_POINTER (init)) + 1))
10394 rtl = gen_rtx_CONST_STRING (VOIDmode,
10395 ggc_strdup (TREE_STRING_POINTER (init)));
10397 /* Other aggregates, and complex values, could be represented using
10398 CONCAT: FIXME! */
10399 else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10401 /* Vectors only work if their mode is supported by the target.
10402 FIXME: generic vectors ought to work too. */
10403 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10405 /* If the initializer is something that we know will expand into an
10406 immediate RTL constant, expand it now. We must be careful not to
10407 reference variables which won't be output. */
10408 else if (initializer_constant_valid_p (init, type)
10409 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10411 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10412 possible. */
10413 if (TREE_CODE (type) == VECTOR_TYPE)
10414 switch (TREE_CODE (init))
10416 case VECTOR_CST:
10417 break;
10418 case CONSTRUCTOR:
10419 if (TREE_CONSTANT (init))
10421 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10422 bool constant_p = true;
10423 tree value;
10424 unsigned HOST_WIDE_INT ix;
10426 /* Even when ctor is constant, it might contain non-*_CST
10427 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10428 belong into VECTOR_CST nodes. */
10429 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10430 if (!CONSTANT_CLASS_P (value))
10432 constant_p = false;
10433 break;
10436 if (constant_p)
10438 init = build_vector_from_ctor (type, elts);
10439 break;
10442 /* FALLTHRU */
10444 default:
10445 return NULL;
10448 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10450 /* If expand_expr returns a MEM, it wasn't immediate. */
10451 gcc_assert (!rtl || !MEM_P (rtl));
10454 return rtl;
10457 /* Generate RTL for the variable DECL to represent its location. */
10459 static rtx
10460 rtl_for_decl_location (tree decl)
10462 rtx rtl;
10464 /* Here we have to decide where we are going to say the parameter "lives"
10465 (as far as the debugger is concerned). We only have a couple of
10466 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10468 DECL_RTL normally indicates where the parameter lives during most of the
10469 activation of the function. If optimization is enabled however, this
10470 could be either NULL or else a pseudo-reg. Both of those cases indicate
10471 that the parameter doesn't really live anywhere (as far as the code
10472 generation parts of GCC are concerned) during most of the function's
10473 activation. That will happen (for example) if the parameter is never
10474 referenced within the function.
10476 We could just generate a location descriptor here for all non-NULL
10477 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10478 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10479 where DECL_RTL is NULL or is a pseudo-reg.
10481 Note however that we can only get away with using DECL_INCOMING_RTL as
10482 a backup substitute for DECL_RTL in certain limited cases. In cases
10483 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10484 we can be sure that the parameter was passed using the same type as it is
10485 declared to have within the function, and that its DECL_INCOMING_RTL
10486 points us to a place where a value of that type is passed.
10488 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10489 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10490 because in these cases DECL_INCOMING_RTL points us to a value of some
10491 type which is *different* from the type of the parameter itself. Thus,
10492 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10493 such cases, the debugger would end up (for example) trying to fetch a
10494 `float' from a place which actually contains the first part of a
10495 `double'. That would lead to really incorrect and confusing
10496 output at debug-time.
10498 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10499 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
10500 are a couple of exceptions however. On little-endian machines we can
10501 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10502 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10503 an integral type that is smaller than TREE_TYPE (decl). These cases arise
10504 when (on a little-endian machine) a non-prototyped function has a
10505 parameter declared to be of type `short' or `char'. In such cases,
10506 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10507 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10508 passed `int' value. If the debugger then uses that address to fetch
10509 a `short' or a `char' (on a little-endian machine) the result will be
10510 the correct data, so we allow for such exceptional cases below.
10512 Note that our goal here is to describe the place where the given formal
10513 parameter lives during most of the function's activation (i.e. between the
10514 end of the prologue and the start of the epilogue). We'll do that as best
10515 as we can. Note however that if the given formal parameter is modified
10516 sometime during the execution of the function, then a stack backtrace (at
10517 debug-time) will show the function as having been called with the *new*
10518 value rather than the value which was originally passed in. This happens
10519 rarely enough that it is not a major problem, but it *is* a problem, and
10520 I'd like to fix it.
10522 A future version of dwarf2out.c may generate two additional attributes for
10523 any given DW_TAG_formal_parameter DIE which will describe the "passed
10524 type" and the "passed location" for the given formal parameter in addition
10525 to the attributes we now generate to indicate the "declared type" and the
10526 "active location" for each parameter. This additional set of attributes
10527 could be used by debuggers for stack backtraces. Separately, note that
10528 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10529 This happens (for example) for inlined-instances of inline function formal
10530 parameters which are never referenced. This really shouldn't be
10531 happening. All PARM_DECL nodes should get valid non-NULL
10532 DECL_INCOMING_RTL values. FIXME. */
10534 /* Use DECL_RTL as the "location" unless we find something better. */
10535 rtl = DECL_RTL_IF_SET (decl);
10537 /* When generating abstract instances, ignore everything except
10538 constants, symbols living in memory, and symbols living in
10539 fixed registers. */
10540 if (! reload_completed)
10542 if (rtl
10543 && (CONSTANT_P (rtl)
10544 || (MEM_P (rtl)
10545 && CONSTANT_P (XEXP (rtl, 0)))
10546 || (REG_P (rtl)
10547 && TREE_CODE (decl) == VAR_DECL
10548 && TREE_STATIC (decl))))
10550 rtl = targetm.delegitimize_address (rtl);
10551 return rtl;
10553 rtl = NULL_RTX;
10555 else if (TREE_CODE (decl) == PARM_DECL)
10557 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10559 tree declared_type = TREE_TYPE (decl);
10560 tree passed_type = DECL_ARG_TYPE (decl);
10561 enum machine_mode dmode = TYPE_MODE (declared_type);
10562 enum machine_mode pmode = TYPE_MODE (passed_type);
10564 /* This decl represents a formal parameter which was optimized out.
10565 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10566 all cases where (rtl == NULL_RTX) just below. */
10567 if (dmode == pmode)
10568 rtl = DECL_INCOMING_RTL (decl);
10569 else if (SCALAR_INT_MODE_P (dmode)
10570 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10571 && DECL_INCOMING_RTL (decl))
10573 rtx inc = DECL_INCOMING_RTL (decl);
10574 if (REG_P (inc))
10575 rtl = inc;
10576 else if (MEM_P (inc))
10578 if (BYTES_BIG_ENDIAN)
10579 rtl = adjust_address_nv (inc, dmode,
10580 GET_MODE_SIZE (pmode)
10581 - GET_MODE_SIZE (dmode));
10582 else
10583 rtl = inc;
10588 /* If the parm was passed in registers, but lives on the stack, then
10589 make a big endian correction if the mode of the type of the
10590 parameter is not the same as the mode of the rtl. */
10591 /* ??? This is the same series of checks that are made in dbxout.c before
10592 we reach the big endian correction code there. It isn't clear if all
10593 of these checks are necessary here, but keeping them all is the safe
10594 thing to do. */
10595 else if (MEM_P (rtl)
10596 && XEXP (rtl, 0) != const0_rtx
10597 && ! CONSTANT_P (XEXP (rtl, 0))
10598 /* Not passed in memory. */
10599 && !MEM_P (DECL_INCOMING_RTL (decl))
10600 /* Not passed by invisible reference. */
10601 && (!REG_P (XEXP (rtl, 0))
10602 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10603 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10604 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10605 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10606 #endif
10608 /* Big endian correction check. */
10609 && BYTES_BIG_ENDIAN
10610 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10611 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10612 < UNITS_PER_WORD))
10614 int offset = (UNITS_PER_WORD
10615 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10617 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10618 plus_constant (XEXP (rtl, 0), offset));
10621 else if (TREE_CODE (decl) == VAR_DECL
10622 && rtl
10623 && MEM_P (rtl)
10624 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10625 && BYTES_BIG_ENDIAN)
10627 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10628 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10630 /* If a variable is declared "register" yet is smaller than
10631 a register, then if we store the variable to memory, it
10632 looks like we're storing a register-sized value, when in
10633 fact we are not. We need to adjust the offset of the
10634 storage location to reflect the actual value's bytes,
10635 else gdb will not be able to display it. */
10636 if (rsize > dsize)
10637 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10638 plus_constant (XEXP (rtl, 0), rsize-dsize));
10641 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10642 and will have been substituted directly into all expressions that use it.
10643 C does not have such a concept, but C++ and other languages do. */
10644 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10645 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10647 if (rtl)
10648 rtl = targetm.delegitimize_address (rtl);
10650 /* If we don't look past the constant pool, we risk emitting a
10651 reference to a constant pool entry that isn't referenced from
10652 code, and thus is not emitted. */
10653 if (rtl)
10654 rtl = avoid_constant_pool_reference (rtl);
10656 return rtl;
10659 /* We need to figure out what section we should use as the base for the
10660 address ranges where a given location is valid.
10661 1. If this particular DECL has a section associated with it, use that.
10662 2. If this function has a section associated with it, use that.
10663 3. Otherwise, use the text section.
10664 XXX: If you split a variable across multiple sections, we won't notice. */
10666 static const char *
10667 secname_for_decl (const_tree decl)
10669 const char *secname;
10671 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10673 tree sectree = DECL_SECTION_NAME (decl);
10674 secname = TREE_STRING_POINTER (sectree);
10676 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10678 tree sectree = DECL_SECTION_NAME (current_function_decl);
10679 secname = TREE_STRING_POINTER (sectree);
10681 else if (cfun && in_cold_section_p)
10682 secname = cfun->cold_section_label;
10683 else
10684 secname = text_section_label;
10686 return secname;
10689 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10690 data attribute for a variable or a parameter. We generate the
10691 DW_AT_const_value attribute only in those cases where the given variable
10692 or parameter does not have a true "location" either in memory or in a
10693 register. This can happen (for example) when a constant is passed as an
10694 actual argument in a call to an inline function. (It's possible that
10695 these things can crop up in other ways also.) Note that one type of
10696 constant value which can be passed into an inlined function is a constant
10697 pointer. This can happen for example if an actual argument in an inlined
10698 function call evaluates to a compile-time constant address. */
10700 static void
10701 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10702 enum dwarf_attribute attr)
10704 rtx rtl;
10705 dw_loc_descr_ref descr;
10706 var_loc_list *loc_list;
10707 struct var_loc_node *node;
10708 if (TREE_CODE (decl) == ERROR_MARK)
10709 return;
10711 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10712 || TREE_CODE (decl) == RESULT_DECL);
10714 /* See if we possibly have multiple locations for this variable. */
10715 loc_list = lookup_decl_loc (decl);
10717 /* If it truly has multiple locations, the first and last node will
10718 differ. */
10719 if (loc_list && loc_list->first != loc_list->last)
10721 const char *endname, *secname;
10722 dw_loc_list_ref list;
10723 rtx varloc;
10724 enum var_init_status initialized;
10726 /* Now that we know what section we are using for a base,
10727 actually construct the list of locations.
10728 The first location information is what is passed to the
10729 function that creates the location list, and the remaining
10730 locations just get added on to that list.
10731 Note that we only know the start address for a location
10732 (IE location changes), so to build the range, we use
10733 the range [current location start, next location start].
10734 This means we have to special case the last node, and generate
10735 a range of [last location start, end of function label]. */
10737 node = loc_list->first;
10738 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10739 secname = secname_for_decl (decl);
10741 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
10742 initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10743 else
10744 initialized = VAR_INIT_STATUS_INITIALIZED;
10746 list = new_loc_list (loc_descriptor (varloc, initialized),
10747 node->label, node->next->label, secname, 1);
10748 node = node->next;
10750 for (; node->next; node = node->next)
10751 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10753 /* The variable has a location between NODE->LABEL and
10754 NODE->NEXT->LABEL. */
10755 enum var_init_status initialized =
10756 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10757 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10758 add_loc_descr_to_loc_list (&list,
10759 loc_descriptor (varloc, initialized),
10760 node->label, node->next->label, secname);
10763 /* If the variable has a location at the last label
10764 it keeps its location until the end of function. */
10765 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10767 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10768 enum var_init_status initialized =
10769 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10771 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10772 if (!current_function_decl)
10773 endname = text_end_label;
10774 else
10776 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10777 current_function_funcdef_no);
10778 endname = ggc_strdup (label_id);
10780 add_loc_descr_to_loc_list (&list,
10781 loc_descriptor (varloc, initialized),
10782 node->label, endname, secname);
10785 /* Finally, add the location list to the DIE, and we are done. */
10786 add_AT_loc_list (die, attr, list);
10787 return;
10790 /* Try to get some constant RTL for this decl, and use that as the value of
10791 the location. */
10793 rtl = rtl_for_decl_location (decl);
10794 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10796 add_const_value_attribute (die, rtl);
10797 return;
10800 /* If we have tried to generate the location otherwise, and it
10801 didn't work out (we wouldn't be here if we did), and we have a one entry
10802 location list, try generating a location from that. */
10803 if (loc_list && loc_list->first)
10805 enum var_init_status status;
10806 node = loc_list->first;
10807 status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10808 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
10809 if (descr)
10811 add_AT_location_description (die, attr, descr);
10812 return;
10816 /* We couldn't get any rtl, so try directly generating the location
10817 description from the tree. */
10818 descr = loc_descriptor_from_tree (decl);
10819 if (descr)
10821 add_AT_location_description (die, attr, descr);
10822 return;
10824 /* None of that worked, so it must not really have a location;
10825 try adding a constant value attribute from the DECL_INITIAL. */
10826 tree_add_const_value_attribute (die, decl);
10829 /* If we don't have a copy of this variable in memory for some reason (such
10830 as a C++ member constant that doesn't have an out-of-line definition),
10831 we should tell the debugger about the constant value. */
10833 static void
10834 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10836 tree init = DECL_INITIAL (decl);
10837 tree type = TREE_TYPE (decl);
10838 rtx rtl;
10840 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10841 /* OK */;
10842 else
10843 return;
10845 rtl = rtl_for_decl_init (init, type);
10846 if (rtl)
10847 add_const_value_attribute (var_die, rtl);
10850 /* Convert the CFI instructions for the current function into a
10851 location list. This is used for DW_AT_frame_base when we targeting
10852 a dwarf2 consumer that does not support the dwarf3
10853 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
10854 expressions. */
10856 static dw_loc_list_ref
10857 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10859 dw_fde_ref fde;
10860 dw_loc_list_ref list, *list_tail;
10861 dw_cfi_ref cfi;
10862 dw_cfa_location last_cfa, next_cfa;
10863 const char *start_label, *last_label, *section;
10865 fde = &fde_table[fde_table_in_use - 1];
10867 section = secname_for_decl (current_function_decl);
10868 list_tail = &list;
10869 list = NULL;
10871 next_cfa.reg = INVALID_REGNUM;
10872 next_cfa.offset = 0;
10873 next_cfa.indirect = 0;
10874 next_cfa.base_offset = 0;
10876 start_label = fde->dw_fde_begin;
10878 /* ??? Bald assumption that the CIE opcode list does not contain
10879 advance opcodes. */
10880 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10881 lookup_cfa_1 (cfi, &next_cfa);
10883 last_cfa = next_cfa;
10884 last_label = start_label;
10886 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10887 switch (cfi->dw_cfi_opc)
10889 case DW_CFA_set_loc:
10890 case DW_CFA_advance_loc1:
10891 case DW_CFA_advance_loc2:
10892 case DW_CFA_advance_loc4:
10893 if (!cfa_equal_p (&last_cfa, &next_cfa))
10895 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10896 start_label, last_label, section,
10897 list == NULL);
10899 list_tail = &(*list_tail)->dw_loc_next;
10900 last_cfa = next_cfa;
10901 start_label = last_label;
10903 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10904 break;
10906 case DW_CFA_advance_loc:
10907 /* The encoding is complex enough that we should never emit this. */
10908 case DW_CFA_remember_state:
10909 case DW_CFA_restore_state:
10910 /* We don't handle these two in this function. It would be possible
10911 if it were to be required. */
10912 gcc_unreachable ();
10914 default:
10915 lookup_cfa_1 (cfi, &next_cfa);
10916 break;
10919 if (!cfa_equal_p (&last_cfa, &next_cfa))
10921 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10922 start_label, last_label, section,
10923 list == NULL);
10924 list_tail = &(*list_tail)->dw_loc_next;
10925 start_label = last_label;
10927 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10928 start_label, fde->dw_fde_end, section,
10929 list == NULL);
10931 return list;
10934 /* Compute a displacement from the "steady-state frame pointer" to the
10935 frame base (often the same as the CFA), and store it in
10936 frame_pointer_fb_offset. OFFSET is added to the displacement
10937 before the latter is negated. */
10939 static void
10940 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10942 rtx reg, elim;
10944 #ifdef FRAME_POINTER_CFA_OFFSET
10945 reg = frame_pointer_rtx;
10946 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10947 #else
10948 reg = arg_pointer_rtx;
10949 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10950 #endif
10952 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10953 if (GET_CODE (elim) == PLUS)
10955 offset += INTVAL (XEXP (elim, 1));
10956 elim = XEXP (elim, 0);
10958 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10959 : stack_pointer_rtx));
10961 frame_pointer_fb_offset = -offset;
10964 /* Generate a DW_AT_name attribute given some string value to be included as
10965 the value of the attribute. */
10967 static void
10968 add_name_attribute (dw_die_ref die, const char *name_string)
10970 if (name_string != NULL && *name_string != 0)
10972 if (demangle_name_func)
10973 name_string = (*demangle_name_func) (name_string);
10975 add_AT_string (die, DW_AT_name, name_string);
10979 /* Generate a DW_AT_comp_dir attribute for DIE. */
10981 static void
10982 add_comp_dir_attribute (dw_die_ref die)
10984 const char *wd = get_src_pwd ();
10985 if (wd != NULL)
10986 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
10989 /* Given a tree node describing an array bound (either lower or upper) output
10990 a representation for that bound. */
10992 static void
10993 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10995 switch (TREE_CODE (bound))
10997 case ERROR_MARK:
10998 return;
11000 /* All fixed-bounds are represented by INTEGER_CST nodes. */
11001 case INTEGER_CST:
11002 if (! host_integerp (bound, 0)
11003 || (bound_attr == DW_AT_lower_bound
11004 && (((is_c_family () || is_java ()) && integer_zerop (bound))
11005 || (is_fortran () && integer_onep (bound)))))
11006 /* Use the default. */
11008 else
11009 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11010 break;
11012 case CONVERT_EXPR:
11013 case NOP_EXPR:
11014 case NON_LVALUE_EXPR:
11015 case VIEW_CONVERT_EXPR:
11016 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11017 break;
11019 case SAVE_EXPR:
11020 break;
11022 case VAR_DECL:
11023 case PARM_DECL:
11024 case RESULT_DECL:
11026 dw_die_ref decl_die = lookup_decl_die (bound);
11028 /* ??? Can this happen, or should the variable have been bound
11029 first? Probably it can, since I imagine that we try to create
11030 the types of parameters in the order in which they exist in
11031 the list, and won't have created a forward reference to a
11032 later parameter. */
11033 if (decl_die != NULL)
11034 add_AT_die_ref (subrange_die, bound_attr, decl_die);
11035 break;
11038 default:
11040 /* Otherwise try to create a stack operation procedure to
11041 evaluate the value of the array bound. */
11043 dw_die_ref ctx, decl_die;
11044 dw_loc_descr_ref loc;
11046 loc = loc_descriptor_from_tree (bound);
11047 if (loc == NULL)
11048 break;
11050 if (current_function_decl == 0)
11051 ctx = comp_unit_die;
11052 else
11053 ctx = lookup_decl_die (current_function_decl);
11055 decl_die = new_die (DW_TAG_variable, ctx, bound);
11056 add_AT_flag (decl_die, DW_AT_artificial, 1);
11057 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11058 add_AT_loc (decl_die, DW_AT_location, loc);
11060 add_AT_die_ref (subrange_die, bound_attr, decl_die);
11061 break;
11066 /* Note that the block of subscript information for an array type also
11067 includes information about the element type of type given array type. */
11069 static void
11070 add_subscript_info (dw_die_ref type_die, tree type)
11072 #ifndef MIPS_DEBUGGING_INFO
11073 unsigned dimension_number;
11074 #endif
11075 tree lower, upper;
11076 dw_die_ref subrange_die;
11078 /* The GNU compilers represent multidimensional array types as sequences of
11079 one dimensional array types whose element types are themselves array
11080 types. Here we squish that down, so that each multidimensional array
11081 type gets only one array_type DIE in the Dwarf debugging info. The draft
11082 Dwarf specification say that we are allowed to do this kind of
11083 compression in C (because there is no difference between an array or
11084 arrays and a multidimensional array in C) but for other source languages
11085 (e.g. Ada) we probably shouldn't do this. */
11087 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11088 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11089 We work around this by disabling this feature. See also
11090 gen_array_type_die. */
11091 #ifndef MIPS_DEBUGGING_INFO
11092 for (dimension_number = 0;
11093 TREE_CODE (type) == ARRAY_TYPE;
11094 type = TREE_TYPE (type), dimension_number++)
11095 #endif
11097 tree domain = TYPE_DOMAIN (type);
11099 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
11100 and (in GNU C only) variable bounds. Handle all three forms
11101 here. */
11102 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
11103 if (domain)
11105 /* We have an array type with specified bounds. */
11106 lower = TYPE_MIN_VALUE (domain);
11107 upper = TYPE_MAX_VALUE (domain);
11109 /* Define the index type. */
11110 if (TREE_TYPE (domain))
11112 /* ??? This is probably an Ada unnamed subrange type. Ignore the
11113 TREE_TYPE field. We can't emit debug info for this
11114 because it is an unnamed integral type. */
11115 if (TREE_CODE (domain) == INTEGER_TYPE
11116 && TYPE_NAME (domain) == NULL_TREE
11117 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
11118 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
11120 else
11121 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
11122 type_die);
11125 /* ??? If upper is NULL, the array has unspecified length,
11126 but it does have a lower bound. This happens with Fortran
11127 dimension arr(N:*)
11128 Since the debugger is definitely going to need to know N
11129 to produce useful results, go ahead and output the lower
11130 bound solo, and hope the debugger can cope. */
11132 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
11133 if (upper)
11134 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
11137 /* Otherwise we have an array type with an unspecified length. The
11138 DWARF-2 spec does not say how to handle this; let's just leave out the
11139 bounds. */
11143 static void
11144 add_byte_size_attribute (dw_die_ref die, tree tree_node)
11146 unsigned size;
11148 switch (TREE_CODE (tree_node))
11150 case ERROR_MARK:
11151 size = 0;
11152 break;
11153 case ENUMERAL_TYPE:
11154 case RECORD_TYPE:
11155 case UNION_TYPE:
11156 case QUAL_UNION_TYPE:
11157 size = int_size_in_bytes (tree_node);
11158 break;
11159 case FIELD_DECL:
11160 /* For a data member of a struct or union, the DW_AT_byte_size is
11161 generally given as the number of bytes normally allocated for an
11162 object of the *declared* type of the member itself. This is true
11163 even for bit-fields. */
11164 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
11165 break;
11166 default:
11167 gcc_unreachable ();
11170 /* Note that `size' might be -1 when we get to this point. If it is, that
11171 indicates that the byte size of the entity in question is variable. We
11172 have no good way of expressing this fact in Dwarf at the present time,
11173 so just let the -1 pass on through. */
11174 add_AT_unsigned (die, DW_AT_byte_size, size);
11177 /* For a FIELD_DECL node which represents a bit-field, output an attribute
11178 which specifies the distance in bits from the highest order bit of the
11179 "containing object" for the bit-field to the highest order bit of the
11180 bit-field itself.
11182 For any given bit-field, the "containing object" is a hypothetical object
11183 (of some integral or enum type) within which the given bit-field lives. The
11184 type of this hypothetical "containing object" is always the same as the
11185 declared type of the individual bit-field itself. The determination of the
11186 exact location of the "containing object" for a bit-field is rather
11187 complicated. It's handled by the `field_byte_offset' function (above).
11189 Note that it is the size (in bytes) of the hypothetical "containing object"
11190 which will be given in the DW_AT_byte_size attribute for this bit-field.
11191 (See `byte_size_attribute' above). */
11193 static inline void
11194 add_bit_offset_attribute (dw_die_ref die, tree decl)
11196 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
11197 tree type = DECL_BIT_FIELD_TYPE (decl);
11198 HOST_WIDE_INT bitpos_int;
11199 HOST_WIDE_INT highest_order_object_bit_offset;
11200 HOST_WIDE_INT highest_order_field_bit_offset;
11201 HOST_WIDE_INT unsigned bit_offset;
11203 /* Must be a field and a bit field. */
11204 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
11206 /* We can't yet handle bit-fields whose offsets are variable, so if we
11207 encounter such things, just return without generating any attribute
11208 whatsoever. Likewise for variable or too large size. */
11209 if (! host_integerp (bit_position (decl), 0)
11210 || ! host_integerp (DECL_SIZE (decl), 1))
11211 return;
11213 bitpos_int = int_bit_position (decl);
11215 /* Note that the bit offset is always the distance (in bits) from the
11216 highest-order bit of the "containing object" to the highest-order bit of
11217 the bit-field itself. Since the "high-order end" of any object or field
11218 is different on big-endian and little-endian machines, the computation
11219 below must take account of these differences. */
11220 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
11221 highest_order_field_bit_offset = bitpos_int;
11223 if (! BYTES_BIG_ENDIAN)
11225 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
11226 highest_order_object_bit_offset += simple_type_size_in_bits (type);
11229 bit_offset
11230 = (! BYTES_BIG_ENDIAN
11231 ? highest_order_object_bit_offset - highest_order_field_bit_offset
11232 : highest_order_field_bit_offset - highest_order_object_bit_offset);
11234 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
11237 /* For a FIELD_DECL node which represents a bit field, output an attribute
11238 which specifies the length in bits of the given field. */
11240 static inline void
11241 add_bit_size_attribute (dw_die_ref die, tree decl)
11243 /* Must be a field and a bit field. */
11244 gcc_assert (TREE_CODE (decl) == FIELD_DECL
11245 && DECL_BIT_FIELD_TYPE (decl));
11247 if (host_integerp (DECL_SIZE (decl), 1))
11248 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
11251 /* If the compiled language is ANSI C, then add a 'prototyped'
11252 attribute, if arg types are given for the parameters of a function. */
11254 static inline void
11255 add_prototyped_attribute (dw_die_ref die, tree func_type)
11257 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
11258 && TYPE_ARG_TYPES (func_type) != NULL)
11259 add_AT_flag (die, DW_AT_prototyped, 1);
11262 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
11263 by looking in either the type declaration or object declaration
11264 equate table. */
11266 static inline void
11267 add_abstract_origin_attribute (dw_die_ref die, tree origin)
11269 dw_die_ref origin_die = NULL;
11271 if (TREE_CODE (origin) != FUNCTION_DECL)
11273 /* We may have gotten separated from the block for the inlined
11274 function, if we're in an exception handler or some such; make
11275 sure that the abstract function has been written out.
11277 Doing this for nested functions is wrong, however; functions are
11278 distinct units, and our context might not even be inline. */
11279 tree fn = origin;
11281 if (TYPE_P (fn))
11282 fn = TYPE_STUB_DECL (fn);
11284 fn = decl_function_context (fn);
11285 if (fn)
11286 dwarf2out_abstract_function (fn);
11289 if (DECL_P (origin))
11290 origin_die = lookup_decl_die (origin);
11291 else if (TYPE_P (origin))
11292 origin_die = lookup_type_die (origin);
11294 /* XXX: Functions that are never lowered don't always have correct block
11295 trees (in the case of java, they simply have no block tree, in some other
11296 languages). For these functions, there is nothing we can really do to
11297 output correct debug info for inlined functions in all cases. Rather
11298 than die, we'll just produce deficient debug info now, in that we will
11299 have variables without a proper abstract origin. In the future, when all
11300 functions are lowered, we should re-add a gcc_assert (origin_die)
11301 here. */
11303 if (origin_die)
11304 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11307 /* We do not currently support the pure_virtual attribute. */
11309 static inline void
11310 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11312 if (DECL_VINDEX (func_decl))
11314 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11316 if (host_integerp (DECL_VINDEX (func_decl), 0))
11317 add_AT_loc (die, DW_AT_vtable_elem_location,
11318 new_loc_descr (DW_OP_constu,
11319 tree_low_cst (DECL_VINDEX (func_decl), 0),
11320 0));
11322 /* GNU extension: Record what type this method came from originally. */
11323 if (debug_info_level > DINFO_LEVEL_TERSE)
11324 add_AT_die_ref (die, DW_AT_containing_type,
11325 lookup_type_die (DECL_CONTEXT (func_decl)));
11329 /* Add source coordinate attributes for the given decl. */
11331 static void
11332 add_src_coords_attributes (dw_die_ref die, tree decl)
11334 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11336 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11337 add_AT_unsigned (die, DW_AT_decl_line, s.line);
11340 /* Add a DW_AT_name attribute and source coordinate attribute for the
11341 given decl, but only if it actually has a name. */
11343 static void
11344 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11346 tree decl_name;
11348 decl_name = DECL_NAME (decl);
11349 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11351 add_name_attribute (die, dwarf2_name (decl, 0));
11352 if (! DECL_ARTIFICIAL (decl))
11353 add_src_coords_attributes (die, decl);
11355 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11356 && TREE_PUBLIC (decl)
11357 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11358 && !DECL_ABSTRACT (decl)
11359 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
11360 add_AT_string (die, DW_AT_MIPS_linkage_name,
11361 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11364 #ifdef VMS_DEBUGGING_INFO
11365 /* Get the function's name, as described by its RTL. This may be different
11366 from the DECL_NAME name used in the source file. */
11367 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11369 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11370 XEXP (DECL_RTL (decl), 0));
11371 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11373 #endif
11376 /* Push a new declaration scope. */
11378 static void
11379 push_decl_scope (tree scope)
11381 VEC_safe_push (tree, gc, decl_scope_table, scope);
11384 /* Pop a declaration scope. */
11386 static inline void
11387 pop_decl_scope (void)
11389 VEC_pop (tree, decl_scope_table);
11392 /* Return the DIE for the scope that immediately contains this type.
11393 Non-named types get global scope. Named types nested in other
11394 types get their containing scope if it's open, or global scope
11395 otherwise. All other types (i.e. function-local named types) get
11396 the current active scope. */
11398 static dw_die_ref
11399 scope_die_for (tree t, dw_die_ref context_die)
11401 dw_die_ref scope_die = NULL;
11402 tree containing_scope;
11403 int i;
11405 /* Non-types always go in the current scope. */
11406 gcc_assert (TYPE_P (t));
11408 containing_scope = TYPE_CONTEXT (t);
11410 /* Use the containing namespace if it was passed in (for a declaration). */
11411 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11413 if (context_die == lookup_decl_die (containing_scope))
11414 /* OK */;
11415 else
11416 containing_scope = NULL_TREE;
11419 /* Ignore function type "scopes" from the C frontend. They mean that
11420 a tagged type is local to a parmlist of a function declarator, but
11421 that isn't useful to DWARF. */
11422 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11423 containing_scope = NULL_TREE;
11425 if (containing_scope == NULL_TREE)
11426 scope_die = comp_unit_die;
11427 else if (TYPE_P (containing_scope))
11429 /* For types, we can just look up the appropriate DIE. But
11430 first we check to see if we're in the middle of emitting it
11431 so we know where the new DIE should go. */
11432 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11433 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11434 break;
11436 if (i < 0)
11438 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11439 || TREE_ASM_WRITTEN (containing_scope));
11441 /* If none of the current dies are suitable, we get file scope. */
11442 scope_die = comp_unit_die;
11444 else
11445 scope_die = lookup_type_die (containing_scope);
11447 else
11448 scope_die = context_die;
11450 return scope_die;
11453 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
11455 static inline int
11456 local_scope_p (dw_die_ref context_die)
11458 for (; context_die; context_die = context_die->die_parent)
11459 if (context_die->die_tag == DW_TAG_inlined_subroutine
11460 || context_die->die_tag == DW_TAG_subprogram)
11461 return 1;
11463 return 0;
11466 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11467 whether or not to treat a DIE in this context as a declaration. */
11469 static inline int
11470 class_or_namespace_scope_p (dw_die_ref context_die)
11472 return (context_die
11473 && (context_die->die_tag == DW_TAG_structure_type
11474 || context_die->die_tag == DW_TAG_union_type
11475 || context_die->die_tag == DW_TAG_namespace));
11478 /* Many forms of DIEs require a "type description" attribute. This
11479 routine locates the proper "type descriptor" die for the type given
11480 by 'type', and adds a DW_AT_type attribute below the given die. */
11482 static void
11483 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11484 int decl_volatile, dw_die_ref context_die)
11486 enum tree_code code = TREE_CODE (type);
11487 dw_die_ref type_die = NULL;
11489 /* ??? If this type is an unnamed subrange type of an integral, floating-point
11490 or fixed-point type, use the inner type. This is because we have no
11491 support for unnamed types in base_type_die. This can happen if this is
11492 an Ada subrange type. Correct solution is emit a subrange type die. */
11493 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
11494 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11495 type = TREE_TYPE (type), code = TREE_CODE (type);
11497 if (code == ERROR_MARK
11498 /* Handle a special case. For functions whose return type is void, we
11499 generate *no* type attribute. (Note that no object may have type
11500 `void', so this only applies to function return types). */
11501 || code == VOID_TYPE)
11502 return;
11504 type_die = modified_type_die (type,
11505 decl_const || TYPE_READONLY (type),
11506 decl_volatile || TYPE_VOLATILE (type),
11507 context_die);
11509 if (type_die != NULL)
11510 add_AT_die_ref (object_die, DW_AT_type, type_die);
11513 /* Given an object die, add the calling convention attribute for the
11514 function call type. */
11515 static void
11516 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
11518 enum dwarf_calling_convention value = DW_CC_normal;
11520 value = targetm.dwarf_calling_convention (type);
11522 /* Only add the attribute if the backend requests it, and
11523 is not DW_CC_normal. */
11524 if (value && (value != DW_CC_normal))
11525 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11528 /* Given a tree pointer to a struct, class, union, or enum type node, return
11529 a pointer to the (string) tag name for the given type, or zero if the type
11530 was declared without a tag. */
11532 static const char *
11533 type_tag (const_tree type)
11535 const char *name = 0;
11537 if (TYPE_NAME (type) != 0)
11539 tree t = 0;
11541 /* Find the IDENTIFIER_NODE for the type name. */
11542 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11543 t = TYPE_NAME (type);
11545 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11546 a TYPE_DECL node, regardless of whether or not a `typedef' was
11547 involved. */
11548 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11549 && ! DECL_IGNORED_P (TYPE_NAME (type)))
11551 /* We want to be extra verbose. Don't call dwarf_name if
11552 DECL_NAME isn't set. The default hook for decl_printable_name
11553 doesn't like that, and in this context it's correct to return
11554 0, instead of "<anonymous>" or the like. */
11555 if (DECL_NAME (TYPE_NAME (type)))
11556 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11559 /* Now get the name as a string, or invent one. */
11560 if (!name && t != 0)
11561 name = IDENTIFIER_POINTER (t);
11564 return (name == 0 || *name == '\0') ? 0 : name;
11567 /* Return the type associated with a data member, make a special check
11568 for bit field types. */
11570 static inline tree
11571 member_declared_type (const_tree member)
11573 return (DECL_BIT_FIELD_TYPE (member)
11574 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11577 /* Get the decl's label, as described by its RTL. This may be different
11578 from the DECL_NAME name used in the source file. */
11580 #if 0
11581 static const char *
11582 decl_start_label (tree decl)
11584 rtx x;
11585 const char *fnname;
11587 x = DECL_RTL (decl);
11588 gcc_assert (MEM_P (x));
11590 x = XEXP (x, 0);
11591 gcc_assert (GET_CODE (x) == SYMBOL_REF);
11593 fnname = XSTR (x, 0);
11594 return fnname;
11596 #endif
11598 /* These routines generate the internal representation of the DIE's for
11599 the compilation unit. Debugging information is collected by walking
11600 the declaration trees passed in from dwarf2out_decl(). */
11602 static void
11603 gen_array_type_die (tree type, dw_die_ref context_die)
11605 dw_die_ref scope_die = scope_die_for (type, context_die);
11606 dw_die_ref array_die;
11607 tree element_type;
11609 /* ??? The SGI dwarf reader fails for array of array of enum types unless
11610 the inner array type comes before the outer array type. Thus we must
11611 call gen_type_die before we call new_die. See below also. */
11612 #ifdef MIPS_DEBUGGING_INFO
11613 gen_type_die (TREE_TYPE (type), context_die);
11614 #endif
11616 array_die = new_die (DW_TAG_array_type, scope_die, type);
11617 add_name_attribute (array_die, type_tag (type));
11618 equate_type_number_to_die (type, array_die);
11620 if (TREE_CODE (type) == VECTOR_TYPE)
11622 /* The frontend feeds us a representation for the vector as a struct
11623 containing an array. Pull out the array type. */
11624 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11625 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11628 #if 0
11629 /* We default the array ordering. SDB will probably do
11630 the right things even if DW_AT_ordering is not present. It's not even
11631 an issue until we start to get into multidimensional arrays anyway. If
11632 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11633 then we'll have to put the DW_AT_ordering attribute back in. (But if
11634 and when we find out that we need to put these in, we will only do so
11635 for multidimensional arrays. */
11636 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11637 #endif
11639 #ifdef MIPS_DEBUGGING_INFO
11640 /* The SGI compilers handle arrays of unknown bound by setting
11641 AT_declaration and not emitting any subrange DIEs. */
11642 if (! TYPE_DOMAIN (type))
11643 add_AT_flag (array_die, DW_AT_declaration, 1);
11644 else
11645 #endif
11646 add_subscript_info (array_die, type);
11648 /* Add representation of the type of the elements of this array type. */
11649 element_type = TREE_TYPE (type);
11651 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11652 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11653 We work around this by disabling this feature. See also
11654 add_subscript_info. */
11655 #ifndef MIPS_DEBUGGING_INFO
11656 while (TREE_CODE (element_type) == ARRAY_TYPE)
11657 element_type = TREE_TYPE (element_type);
11659 gen_type_die (element_type, context_die);
11660 #endif
11662 add_type_attribute (array_die, element_type, 0, 0, context_die);
11664 if (get_AT (array_die, DW_AT_name))
11665 add_pubtype (type, array_die);
11668 #if 0
11669 static void
11670 gen_entry_point_die (tree decl, dw_die_ref context_die)
11672 tree origin = decl_ultimate_origin (decl);
11673 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11675 if (origin != NULL)
11676 add_abstract_origin_attribute (decl_die, origin);
11677 else
11679 add_name_and_src_coords_attributes (decl_die, decl);
11680 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11681 0, 0, context_die);
11684 if (DECL_ABSTRACT (decl))
11685 equate_decl_number_to_die (decl, decl_die);
11686 else
11687 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11689 #endif
11691 /* Walk through the list of incomplete types again, trying once more to
11692 emit full debugging info for them. */
11694 static void
11695 retry_incomplete_types (void)
11697 int i;
11699 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11700 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11703 /* Generate a DIE to represent an inlined instance of an enumeration type. */
11705 static void
11706 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11708 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11710 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11711 be incomplete and such types are not marked. */
11712 add_abstract_origin_attribute (type_die, type);
11715 /* Generate a DIE to represent an inlined instance of a structure type. */
11717 static void
11718 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11720 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11722 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11723 be incomplete and such types are not marked. */
11724 add_abstract_origin_attribute (type_die, type);
11727 /* Generate a DIE to represent an inlined instance of a union type. */
11729 static void
11730 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11732 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11734 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11735 be incomplete and such types are not marked. */
11736 add_abstract_origin_attribute (type_die, type);
11739 /* Generate a DIE to represent an enumeration type. Note that these DIEs
11740 include all of the information about the enumeration values also. Each
11741 enumerated type name/value is listed as a child of the enumerated type
11742 DIE. */
11744 static dw_die_ref
11745 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11747 dw_die_ref type_die = lookup_type_die (type);
11749 if (type_die == NULL)
11751 type_die = new_die (DW_TAG_enumeration_type,
11752 scope_die_for (type, context_die), type);
11753 equate_type_number_to_die (type, type_die);
11754 add_name_attribute (type_die, type_tag (type));
11756 else if (! TYPE_SIZE (type))
11757 return type_die;
11758 else
11759 remove_AT (type_die, DW_AT_declaration);
11761 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
11762 given enum type is incomplete, do not generate the DW_AT_byte_size
11763 attribute or the DW_AT_element_list attribute. */
11764 if (TYPE_SIZE (type))
11766 tree link;
11768 TREE_ASM_WRITTEN (type) = 1;
11769 add_byte_size_attribute (type_die, type);
11770 if (TYPE_STUB_DECL (type) != NULL_TREE)
11771 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11773 /* If the first reference to this type was as the return type of an
11774 inline function, then it may not have a parent. Fix this now. */
11775 if (type_die->die_parent == NULL)
11776 add_child_die (scope_die_for (type, context_die), type_die);
11778 for (link = TYPE_VALUES (type);
11779 link != NULL; link = TREE_CHAIN (link))
11781 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11782 tree value = TREE_VALUE (link);
11784 add_name_attribute (enum_die,
11785 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11787 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11788 /* DWARF2 does not provide a way of indicating whether or
11789 not enumeration constants are signed or unsigned. GDB
11790 always assumes the values are signed, so we output all
11791 values as if they were signed. That means that
11792 enumeration constants with very large unsigned values
11793 will appear to have negative values in the debugger. */
11794 add_AT_int (enum_die, DW_AT_const_value,
11795 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11798 else
11799 add_AT_flag (type_die, DW_AT_declaration, 1);
11801 if (get_AT (type_die, DW_AT_name))
11802 add_pubtype (type, type_die);
11804 return type_die;
11807 /* Generate a DIE to represent either a real live formal parameter decl or to
11808 represent just the type of some formal parameter position in some function
11809 type.
11811 Note that this routine is a bit unusual because its argument may be a
11812 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11813 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11814 node. If it's the former then this function is being called to output a
11815 DIE to represent a formal parameter object (or some inlining thereof). If
11816 it's the latter, then this function is only being called to output a
11817 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11818 argument type of some subprogram type. */
11820 static dw_die_ref
11821 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11823 dw_die_ref parm_die
11824 = new_die (DW_TAG_formal_parameter, context_die, node);
11825 tree origin;
11827 switch (TREE_CODE_CLASS (TREE_CODE (node)))
11829 case tcc_declaration:
11830 origin = decl_ultimate_origin (node);
11831 if (origin != NULL)
11832 add_abstract_origin_attribute (parm_die, origin);
11833 else
11835 add_name_and_src_coords_attributes (parm_die, node);
11836 add_type_attribute (parm_die, TREE_TYPE (node),
11837 TREE_READONLY (node),
11838 TREE_THIS_VOLATILE (node),
11839 context_die);
11840 if (DECL_ARTIFICIAL (node))
11841 add_AT_flag (parm_die, DW_AT_artificial, 1);
11844 equate_decl_number_to_die (node, parm_die);
11845 if (! DECL_ABSTRACT (node))
11846 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11848 break;
11850 case tcc_type:
11851 /* We were called with some kind of a ..._TYPE node. */
11852 add_type_attribute (parm_die, node, 0, 0, context_die);
11853 break;
11855 default:
11856 gcc_unreachable ();
11859 return parm_die;
11862 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11863 at the end of an (ANSI prototyped) formal parameters list. */
11865 static void
11866 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11868 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11871 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11872 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11873 parameters as specified in some function type specification (except for
11874 those which appear as part of a function *definition*). */
11876 static void
11877 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11879 tree link;
11880 tree formal_type = NULL;
11881 tree first_parm_type;
11882 tree arg;
11884 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11886 arg = DECL_ARGUMENTS (function_or_method_type);
11887 function_or_method_type = TREE_TYPE (function_or_method_type);
11889 else
11890 arg = NULL_TREE;
11892 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11894 /* Make our first pass over the list of formal parameter types and output a
11895 DW_TAG_formal_parameter DIE for each one. */
11896 for (link = first_parm_type; link; )
11898 dw_die_ref parm_die;
11900 formal_type = TREE_VALUE (link);
11901 if (formal_type == void_type_node)
11902 break;
11904 /* Output a (nameless) DIE to represent the formal parameter itself. */
11905 parm_die = gen_formal_parameter_die (formal_type, context_die);
11906 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11907 && link == first_parm_type)
11908 || (arg && DECL_ARTIFICIAL (arg)))
11909 add_AT_flag (parm_die, DW_AT_artificial, 1);
11911 link = TREE_CHAIN (link);
11912 if (arg)
11913 arg = TREE_CHAIN (arg);
11916 /* If this function type has an ellipsis, add a
11917 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11918 if (formal_type != void_type_node)
11919 gen_unspecified_parameters_die (function_or_method_type, context_die);
11921 /* Make our second (and final) pass over the list of formal parameter types
11922 and output DIEs to represent those types (as necessary). */
11923 for (link = TYPE_ARG_TYPES (function_or_method_type);
11924 link && TREE_VALUE (link);
11925 link = TREE_CHAIN (link))
11926 gen_type_die (TREE_VALUE (link), context_die);
11929 /* We want to generate the DIE for TYPE so that we can generate the
11930 die for MEMBER, which has been defined; we will need to refer back
11931 to the member declaration nested within TYPE. If we're trying to
11932 generate minimal debug info for TYPE, processing TYPE won't do the
11933 trick; we need to attach the member declaration by hand. */
11935 static void
11936 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11938 gen_type_die (type, context_die);
11940 /* If we're trying to avoid duplicate debug info, we may not have
11941 emitted the member decl for this function. Emit it now. */
11942 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11943 && ! lookup_decl_die (member))
11945 dw_die_ref type_die;
11946 gcc_assert (!decl_ultimate_origin (member));
11948 push_decl_scope (type);
11949 type_die = lookup_type_die (type);
11950 if (TREE_CODE (member) == FUNCTION_DECL)
11951 gen_subprogram_die (member, type_die);
11952 else if (TREE_CODE (member) == FIELD_DECL)
11954 /* Ignore the nameless fields that are used to skip bits but handle
11955 C++ anonymous unions and structs. */
11956 if (DECL_NAME (member) != NULL_TREE
11957 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11958 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11960 gen_type_die (member_declared_type (member), type_die);
11961 gen_field_die (member, type_die);
11964 else
11965 gen_variable_die (member, type_die);
11967 pop_decl_scope ();
11971 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11972 may later generate inlined and/or out-of-line instances of. */
11974 static void
11975 dwarf2out_abstract_function (tree decl)
11977 dw_die_ref old_die;
11978 tree save_fn;
11979 tree context;
11980 int was_abstract = DECL_ABSTRACT (decl);
11982 /* Make sure we have the actual abstract inline, not a clone. */
11983 decl = DECL_ORIGIN (decl);
11985 old_die = lookup_decl_die (decl);
11986 if (old_die && get_AT (old_die, DW_AT_inline))
11987 /* We've already generated the abstract instance. */
11988 return;
11990 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11991 we don't get confused by DECL_ABSTRACT. */
11992 if (debug_info_level > DINFO_LEVEL_TERSE)
11994 context = decl_class_context (decl);
11995 if (context)
11996 gen_type_die_for_member
11997 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
12000 /* Pretend we've just finished compiling this function. */
12001 save_fn = current_function_decl;
12002 current_function_decl = decl;
12003 push_cfun (DECL_STRUCT_FUNCTION (decl));
12005 set_decl_abstract_flags (decl, 1);
12006 dwarf2out_decl (decl);
12007 if (! was_abstract)
12008 set_decl_abstract_flags (decl, 0);
12010 current_function_decl = save_fn;
12011 pop_cfun ();
12014 /* Helper function of premark_used_types() which gets called through
12015 htab_traverse_resize().
12017 Marks the DIE of a given type in *SLOT as perennial, so it never gets
12018 marked as unused by prune_unused_types. */
12019 static int
12020 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
12022 tree type;
12023 dw_die_ref die;
12025 type = *slot;
12026 die = lookup_type_die (type);
12027 if (die != NULL)
12028 die->die_perennial_p = 1;
12029 return 1;
12032 /* Mark all members of used_types_hash as perennial. */
12033 static void
12034 premark_used_types (void)
12036 if (cfun && cfun->used_types_hash)
12037 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
12040 /* Generate a DIE to represent a declared function (either file-scope or
12041 block-local). */
12043 static void
12044 gen_subprogram_die (tree decl, dw_die_ref context_die)
12046 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12047 tree origin = decl_ultimate_origin (decl);
12048 dw_die_ref subr_die;
12049 tree fn_arg_types;
12050 tree outer_scope;
12051 dw_die_ref old_die = lookup_decl_die (decl);
12052 int declaration = (current_function_decl != decl
12053 || class_or_namespace_scope_p (context_die));
12055 premark_used_types ();
12057 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
12058 started to generate the abstract instance of an inline, decided to output
12059 its containing class, and proceeded to emit the declaration of the inline
12060 from the member list for the class. If so, DECLARATION takes priority;
12061 we'll get back to the abstract instance when done with the class. */
12063 /* The class-scope declaration DIE must be the primary DIE. */
12064 if (origin && declaration && class_or_namespace_scope_p (context_die))
12066 origin = NULL;
12067 gcc_assert (!old_die);
12070 /* Now that the C++ front end lazily declares artificial member fns, we
12071 might need to retrofit the declaration into its class. */
12072 if (!declaration && !origin && !old_die
12073 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
12074 && !class_or_namespace_scope_p (context_die)
12075 && debug_info_level > DINFO_LEVEL_TERSE)
12076 old_die = force_decl_die (decl);
12078 if (origin != NULL)
12080 gcc_assert (!declaration || local_scope_p (context_die));
12082 /* Fixup die_parent for the abstract instance of a nested
12083 inline function. */
12084 if (old_die && old_die->die_parent == NULL)
12085 add_child_die (context_die, old_die);
12087 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12088 add_abstract_origin_attribute (subr_die, origin);
12090 else if (old_die)
12092 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12093 struct dwarf_file_data * file_index = lookup_filename (s.file);
12095 if (!get_AT_flag (old_die, DW_AT_declaration)
12096 /* We can have a normal definition following an inline one in the
12097 case of redefinition of GNU C extern inlines.
12098 It seems reasonable to use AT_specification in this case. */
12099 && !get_AT (old_die, DW_AT_inline))
12101 /* Detect and ignore this case, where we are trying to output
12102 something we have already output. */
12103 return;
12106 /* If the definition comes from the same place as the declaration,
12107 maybe use the old DIE. We always want the DIE for this function
12108 that has the *_pc attributes to be under comp_unit_die so the
12109 debugger can find it. We also need to do this for abstract
12110 instances of inlines, since the spec requires the out-of-line copy
12111 to have the same parent. For local class methods, this doesn't
12112 apply; we just use the old DIE. */
12113 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
12114 && (DECL_ARTIFICIAL (decl)
12115 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
12116 && (get_AT_unsigned (old_die, DW_AT_decl_line)
12117 == (unsigned) s.line))))
12119 subr_die = old_die;
12121 /* Clear out the declaration attribute and the formal parameters.
12122 Do not remove all children, because it is possible that this
12123 declaration die was forced using force_decl_die(). In such
12124 cases die that forced declaration die (e.g. TAG_imported_module)
12125 is one of the children that we do not want to remove. */
12126 remove_AT (subr_die, DW_AT_declaration);
12127 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
12129 else
12131 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12132 add_AT_specification (subr_die, old_die);
12133 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12134 add_AT_file (subr_die, DW_AT_decl_file, file_index);
12135 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12136 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
12139 else
12141 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12143 if (TREE_PUBLIC (decl))
12144 add_AT_flag (subr_die, DW_AT_external, 1);
12146 add_name_and_src_coords_attributes (subr_die, decl);
12147 if (debug_info_level > DINFO_LEVEL_TERSE)
12149 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
12150 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
12151 0, 0, context_die);
12154 add_pure_or_virtual_attribute (subr_die, decl);
12155 if (DECL_ARTIFICIAL (decl))
12156 add_AT_flag (subr_die, DW_AT_artificial, 1);
12158 if (TREE_PROTECTED (decl))
12159 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
12160 else if (TREE_PRIVATE (decl))
12161 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
12164 if (declaration)
12166 if (!old_die || !get_AT (old_die, DW_AT_inline))
12168 add_AT_flag (subr_die, DW_AT_declaration, 1);
12170 /* The first time we see a member function, it is in the context of
12171 the class to which it belongs. We make sure of this by emitting
12172 the class first. The next time is the definition, which is
12173 handled above. The two may come from the same source text.
12175 Note that force_decl_die() forces function declaration die. It is
12176 later reused to represent definition. */
12177 equate_decl_number_to_die (decl, subr_die);
12180 else if (DECL_ABSTRACT (decl))
12182 if (DECL_DECLARED_INLINE_P (decl))
12184 if (cgraph_function_possibly_inlined_p (decl))
12185 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
12186 else
12187 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
12189 else
12191 if (cgraph_function_possibly_inlined_p (decl))
12192 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
12193 else
12194 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
12197 if (DECL_DECLARED_INLINE_P (decl)
12198 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
12199 add_AT_flag (subr_die, DW_AT_artificial, 1);
12201 equate_decl_number_to_die (decl, subr_die);
12203 else if (!DECL_EXTERNAL (decl))
12205 HOST_WIDE_INT cfa_fb_offset;
12207 if (!old_die || !get_AT (old_die, DW_AT_inline))
12208 equate_decl_number_to_die (decl, subr_die);
12210 if (!flag_reorder_blocks_and_partition)
12212 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
12213 current_function_funcdef_no);
12214 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
12215 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12216 current_function_funcdef_no);
12217 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
12219 add_pubname (decl, subr_die);
12220 add_arange (decl, subr_die);
12222 else
12223 { /* Do nothing for now; maybe need to duplicate die, one for
12224 hot section and ond for cold section, then use the hot/cold
12225 section begin/end labels to generate the aranges... */
12227 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
12228 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
12229 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
12230 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
12232 add_pubname (decl, subr_die);
12233 add_arange (decl, subr_die);
12234 add_arange (decl, subr_die);
12238 #ifdef MIPS_DEBUGGING_INFO
12239 /* Add a reference to the FDE for this routine. */
12240 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
12241 #endif
12243 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
12245 /* We define the "frame base" as the function's CFA. This is more
12246 convenient for several reasons: (1) It's stable across the prologue
12247 and epilogue, which makes it better than just a frame pointer,
12248 (2) With dwarf3, there exists a one-byte encoding that allows us
12249 to reference the .debug_frame data by proxy, but failing that,
12250 (3) We can at least reuse the code inspection and interpretation
12251 code that determines the CFA position at various points in the
12252 function. */
12253 /* ??? Use some command-line or configury switch to enable the use
12254 of dwarf3 DW_OP_call_frame_cfa. At present there are no dwarf
12255 consumers that understand it; fall back to "pure" dwarf2 and
12256 convert the CFA data into a location list. */
12258 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
12259 if (list->dw_loc_next)
12260 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
12261 else
12262 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12265 /* Compute a displacement from the "steady-state frame pointer" to
12266 the CFA. The former is what all stack slots and argument slots
12267 will reference in the rtl; the later is what we've told the
12268 debugger about. We'll need to adjust all frame_base references
12269 by this displacement. */
12270 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
12272 if (cfun->static_chain_decl)
12273 add_AT_location_description (subr_die, DW_AT_static_link,
12274 loc_descriptor_from_tree (cfun->static_chain_decl));
12277 /* Now output descriptions of the arguments for this function. This gets
12278 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
12279 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12280 `...' at the end of the formal parameter list. In order to find out if
12281 there was a trailing ellipsis or not, we must instead look at the type
12282 associated with the FUNCTION_DECL. This will be a node of type
12283 FUNCTION_TYPE. If the chain of type nodes hanging off of this
12284 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
12285 an ellipsis at the end. */
12287 /* In the case where we are describing a mere function declaration, all we
12288 need to do here (and all we *can* do here) is to describe the *types* of
12289 its formal parameters. */
12290 if (debug_info_level <= DINFO_LEVEL_TERSE)
12292 else if (declaration)
12293 gen_formal_types_die (decl, subr_die);
12294 else
12296 /* Generate DIEs to represent all known formal parameters. */
12297 tree arg_decls = DECL_ARGUMENTS (decl);
12298 tree parm;
12300 /* When generating DIEs, generate the unspecified_parameters DIE
12301 instead if we come across the arg "__builtin_va_alist" */
12302 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12303 if (TREE_CODE (parm) == PARM_DECL)
12305 if (DECL_NAME (parm)
12306 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12307 "__builtin_va_alist"))
12308 gen_unspecified_parameters_die (parm, subr_die);
12309 else
12310 gen_decl_die (parm, subr_die);
12313 /* Decide whether we need an unspecified_parameters DIE at the end.
12314 There are 2 more cases to do this for: 1) the ansi ... declaration -
12315 this is detectable when the end of the arg list is not a
12316 void_type_node 2) an unprototyped function declaration (not a
12317 definition). This just means that we have no info about the
12318 parameters at all. */
12319 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12320 if (fn_arg_types != NULL)
12322 /* This is the prototyped case, check for.... */
12323 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12324 gen_unspecified_parameters_die (decl, subr_die);
12326 else if (DECL_INITIAL (decl) == NULL_TREE)
12327 gen_unspecified_parameters_die (decl, subr_die);
12330 /* Output Dwarf info for all of the stuff within the body of the function
12331 (if it has one - it may be just a declaration). */
12332 outer_scope = DECL_INITIAL (decl);
12334 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12335 a function. This BLOCK actually represents the outermost binding contour
12336 for the function, i.e. the contour in which the function's formal
12337 parameters and labels get declared. Curiously, it appears that the front
12338 end doesn't actually put the PARM_DECL nodes for the current function onto
12339 the BLOCK_VARS list for this outer scope, but are strung off of the
12340 DECL_ARGUMENTS list for the function instead.
12342 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12343 the LABEL_DECL nodes for the function however, and we output DWARF info
12344 for those in decls_for_scope. Just within the `outer_scope' there will be
12345 a BLOCK node representing the function's outermost pair of curly braces,
12346 and any blocks used for the base and member initializers of a C++
12347 constructor function. */
12348 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12350 /* Emit a DW_TAG_variable DIE for a named return value. */
12351 if (DECL_NAME (DECL_RESULT (decl)))
12352 gen_decl_die (DECL_RESULT (decl), subr_die);
12354 current_function_has_inlines = 0;
12355 decls_for_scope (outer_scope, subr_die, 0);
12357 #if 0 && defined (MIPS_DEBUGGING_INFO)
12358 if (current_function_has_inlines)
12360 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12361 if (! comp_unit_has_inlines)
12363 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12364 comp_unit_has_inlines = 1;
12367 #endif
12369 /* Add the calling convention attribute if requested. */
12370 add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
12374 /* Generate a DIE to represent a declared data object. */
12376 static void
12377 gen_variable_die (tree decl, dw_die_ref context_die)
12379 tree origin = decl_ultimate_origin (decl);
12380 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
12382 dw_die_ref old_die = lookup_decl_die (decl);
12383 int declaration = (DECL_EXTERNAL (decl)
12384 /* If DECL is COMDAT and has not actually been
12385 emitted, we cannot take its address; there
12386 might end up being no definition anywhere in
12387 the program. For example, consider the C++
12388 test case:
12390 template <class T>
12391 struct S { static const int i = 7; };
12393 template <class T>
12394 const int S<T>::i;
12396 int f() { return S<int>::i; }
12398 Here, S<int>::i is not DECL_EXTERNAL, but no
12399 definition is required, so the compiler will
12400 not emit a definition. */
12401 || (TREE_CODE (decl) == VAR_DECL
12402 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12403 || class_or_namespace_scope_p (context_die));
12405 if (origin != NULL)
12406 add_abstract_origin_attribute (var_die, origin);
12408 /* Loop unrolling can create multiple blocks that refer to the same
12409 static variable, so we must test for the DW_AT_declaration flag.
12411 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12412 copy decls and set the DECL_ABSTRACT flag on them instead of
12413 sharing them.
12415 ??? Duplicated blocks have been rewritten to use .debug_ranges.
12417 ??? The declare_in_namespace support causes us to get two DIEs for one
12418 variable, both of which are declarations. We want to avoid considering
12419 one to be a specification, so we must test that this DIE is not a
12420 declaration. */
12421 else if (old_die && TREE_STATIC (decl) && ! declaration
12422 && get_AT_flag (old_die, DW_AT_declaration) == 1)
12424 /* This is a definition of a C++ class level static. */
12425 add_AT_specification (var_die, old_die);
12426 if (DECL_NAME (decl))
12428 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12429 struct dwarf_file_data * file_index = lookup_filename (s.file);
12431 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12432 add_AT_file (var_die, DW_AT_decl_file, file_index);
12434 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12435 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12438 else
12440 add_name_and_src_coords_attributes (var_die, decl);
12441 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
12442 TREE_THIS_VOLATILE (decl), context_die);
12444 if (TREE_PUBLIC (decl))
12445 add_AT_flag (var_die, DW_AT_external, 1);
12447 if (DECL_ARTIFICIAL (decl))
12448 add_AT_flag (var_die, DW_AT_artificial, 1);
12450 if (TREE_PROTECTED (decl))
12451 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12452 else if (TREE_PRIVATE (decl))
12453 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12456 if (declaration)
12457 add_AT_flag (var_die, DW_AT_declaration, 1);
12459 if (DECL_ABSTRACT (decl) || declaration)
12460 equate_decl_number_to_die (decl, var_die);
12462 if (! declaration && ! DECL_ABSTRACT (decl))
12464 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12465 add_pubname (decl, var_die);
12467 else
12468 tree_add_const_value_attribute (var_die, decl);
12471 /* Generate a DIE to represent a label identifier. */
12473 static void
12474 gen_label_die (tree decl, dw_die_ref context_die)
12476 tree origin = decl_ultimate_origin (decl);
12477 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12478 rtx insn;
12479 char label[MAX_ARTIFICIAL_LABEL_BYTES];
12481 if (origin != NULL)
12482 add_abstract_origin_attribute (lbl_die, origin);
12483 else
12484 add_name_and_src_coords_attributes (lbl_die, decl);
12486 if (DECL_ABSTRACT (decl))
12487 equate_decl_number_to_die (decl, lbl_die);
12488 else
12490 insn = DECL_RTL_IF_SET (decl);
12492 /* Deleted labels are programmer specified labels which have been
12493 eliminated because of various optimizations. We still emit them
12494 here so that it is possible to put breakpoints on them. */
12495 if (insn
12496 && (LABEL_P (insn)
12497 || ((NOTE_P (insn)
12498 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
12500 /* When optimization is enabled (via -O) some parts of the compiler
12501 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12502 represent source-level labels which were explicitly declared by
12503 the user. This really shouldn't be happening though, so catch
12504 it if it ever does happen. */
12505 gcc_assert (!INSN_DELETED_P (insn));
12507 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12508 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12513 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
12514 attributes to the DIE for a block STMT, to describe where the inlined
12515 function was called from. This is similar to add_src_coords_attributes. */
12517 static inline void
12518 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12520 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12522 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12523 add_AT_unsigned (die, DW_AT_call_line, s.line);
12527 /* If STMT's abstract origin is a function declaration and STMT's
12528 first subblock's abstract origin is the function's outermost block,
12529 then we're looking at the main entry point. */
12530 static bool
12531 is_inlined_entry_point (const_tree stmt)
12533 tree decl, block;
12535 if (!stmt || TREE_CODE (stmt) != BLOCK)
12536 return false;
12538 decl = block_ultimate_origin (stmt);
12540 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12541 return false;
12543 block = BLOCK_SUBBLOCKS (stmt);
12545 if (block)
12547 if (TREE_CODE (block) != BLOCK)
12548 return false;
12550 block = block_ultimate_origin (block);
12553 return block == DECL_INITIAL (decl);
12556 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12557 Add low_pc and high_pc attributes to the DIE for a block STMT. */
12559 static inline void
12560 add_high_low_attributes (tree stmt, dw_die_ref die)
12562 char label[MAX_ARTIFICIAL_LABEL_BYTES];
12564 if (BLOCK_FRAGMENT_CHAIN (stmt))
12566 tree chain;
12568 if (is_inlined_entry_point (stmt))
12570 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12571 BLOCK_NUMBER (stmt));
12572 add_AT_lbl_id (die, DW_AT_entry_pc, label);
12575 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12577 chain = BLOCK_FRAGMENT_CHAIN (stmt);
12580 add_ranges (chain);
12581 chain = BLOCK_FRAGMENT_CHAIN (chain);
12583 while (chain);
12584 add_ranges (NULL);
12586 else
12588 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12589 BLOCK_NUMBER (stmt));
12590 add_AT_lbl_id (die, DW_AT_low_pc, label);
12591 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12592 BLOCK_NUMBER (stmt));
12593 add_AT_lbl_id (die, DW_AT_high_pc, label);
12597 /* Generate a DIE for a lexical block. */
12599 static void
12600 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12602 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12604 if (! BLOCK_ABSTRACT (stmt))
12605 add_high_low_attributes (stmt, stmt_die);
12607 decls_for_scope (stmt, stmt_die, depth);
12610 /* Generate a DIE for an inlined subprogram. */
12612 static void
12613 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12615 tree decl = block_ultimate_origin (stmt);
12617 /* Emit info for the abstract instance first, if we haven't yet. We
12618 must emit this even if the block is abstract, otherwise when we
12619 emit the block below (or elsewhere), we may end up trying to emit
12620 a die whose origin die hasn't been emitted, and crashing. */
12621 dwarf2out_abstract_function (decl);
12623 if (! BLOCK_ABSTRACT (stmt))
12625 dw_die_ref subr_die
12626 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12628 add_abstract_origin_attribute (subr_die, decl);
12629 add_high_low_attributes (stmt, subr_die);
12630 add_call_src_coords_attributes (stmt, subr_die);
12632 decls_for_scope (stmt, subr_die, depth);
12633 current_function_has_inlines = 1;
12635 else
12636 /* We may get here if we're the outer block of function A that was
12637 inlined into function B that was inlined into function C. When
12638 generating debugging info for C, dwarf2out_abstract_function(B)
12639 would mark all inlined blocks as abstract, including this one.
12640 So, we wouldn't (and shouldn't) expect labels to be generated
12641 for this one. Instead, just emit debugging info for
12642 declarations within the block. This is particularly important
12643 in the case of initializers of arguments passed from B to us:
12644 if they're statement expressions containing declarations, we
12645 wouldn't generate dies for their abstract variables, and then,
12646 when generating dies for the real variables, we'd die (pun
12647 intended :-) */
12648 gen_lexical_block_die (stmt, context_die, depth);
12651 /* Generate a DIE for a field in a record, or structure. */
12653 static void
12654 gen_field_die (tree decl, dw_die_ref context_die)
12656 dw_die_ref decl_die;
12658 if (TREE_TYPE (decl) == error_mark_node)
12659 return;
12661 decl_die = new_die (DW_TAG_member, context_die, decl);
12662 add_name_and_src_coords_attributes (decl_die, decl);
12663 add_type_attribute (decl_die, member_declared_type (decl),
12664 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12665 context_die);
12667 if (DECL_BIT_FIELD_TYPE (decl))
12669 add_byte_size_attribute (decl_die, decl);
12670 add_bit_size_attribute (decl_die, decl);
12671 add_bit_offset_attribute (decl_die, decl);
12674 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12675 add_data_member_location_attribute (decl_die, decl);
12677 if (DECL_ARTIFICIAL (decl))
12678 add_AT_flag (decl_die, DW_AT_artificial, 1);
12680 if (TREE_PROTECTED (decl))
12681 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12682 else if (TREE_PRIVATE (decl))
12683 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12685 /* Equate decl number to die, so that we can look up this decl later on. */
12686 equate_decl_number_to_die (decl, decl_die);
12689 #if 0
12690 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12691 Use modified_type_die instead.
12692 We keep this code here just in case these types of DIEs may be needed to
12693 represent certain things in other languages (e.g. Pascal) someday. */
12695 static void
12696 gen_pointer_type_die (tree type, dw_die_ref context_die)
12698 dw_die_ref ptr_die
12699 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12701 equate_type_number_to_die (type, ptr_die);
12702 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12703 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12706 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12707 Use modified_type_die instead.
12708 We keep this code here just in case these types of DIEs may be needed to
12709 represent certain things in other languages (e.g. Pascal) someday. */
12711 static void
12712 gen_reference_type_die (tree type, dw_die_ref context_die)
12714 dw_die_ref ref_die
12715 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12717 equate_type_number_to_die (type, ref_die);
12718 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12719 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12721 #endif
12723 /* Generate a DIE for a pointer to a member type. */
12725 static void
12726 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12728 dw_die_ref ptr_die
12729 = new_die (DW_TAG_ptr_to_member_type,
12730 scope_die_for (type, context_die), type);
12732 equate_type_number_to_die (type, ptr_die);
12733 add_AT_die_ref (ptr_die, DW_AT_containing_type,
12734 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12735 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12738 /* Generate the DIE for the compilation unit. */
12740 static dw_die_ref
12741 gen_compile_unit_die (const char *filename)
12743 dw_die_ref die;
12744 char producer[250];
12745 const char *language_string = lang_hooks.name;
12746 int language;
12748 die = new_die (DW_TAG_compile_unit, NULL, NULL);
12750 if (filename)
12752 add_name_attribute (die, filename);
12753 /* Don't add cwd for <built-in>. */
12754 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
12755 add_comp_dir_attribute (die);
12758 sprintf (producer, "%s %s", language_string, version_string);
12760 #ifdef MIPS_DEBUGGING_INFO
12761 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12762 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12763 not appear in the producer string, the debugger reaches the conclusion
12764 that the object file is stripped and has no debugging information.
12765 To get the MIPS/SGI debugger to believe that there is debugging
12766 information in the object file, we add a -g to the producer string. */
12767 if (debug_info_level > DINFO_LEVEL_TERSE)
12768 strcat (producer, " -g");
12769 #endif
12771 add_AT_string (die, DW_AT_producer, producer);
12773 if (strcmp (language_string, "GNU C++") == 0)
12774 language = DW_LANG_C_plus_plus;
12775 else if (strcmp (language_string, "GNU Ada") == 0)
12776 language = DW_LANG_Ada95;
12777 else if (strcmp (language_string, "GNU F77") == 0)
12778 language = DW_LANG_Fortran77;
12779 else if (strcmp (language_string, "GNU F95") == 0)
12780 language = DW_LANG_Fortran95;
12781 else if (strcmp (language_string, "GNU Pascal") == 0)
12782 language = DW_LANG_Pascal83;
12783 else if (strcmp (language_string, "GNU Java") == 0)
12784 language = DW_LANG_Java;
12785 else if (strcmp (language_string, "GNU Objective-C") == 0)
12786 language = DW_LANG_ObjC;
12787 else if (strcmp (language_string, "GNU Objective-C++") == 0)
12788 language = DW_LANG_ObjC_plus_plus;
12789 else
12790 language = DW_LANG_C89;
12792 add_AT_unsigned (die, DW_AT_language, language);
12793 return die;
12796 /* Generate the DIE for a base class. */
12798 static void
12799 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
12801 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
12803 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12804 add_data_member_location_attribute (die, binfo);
12806 if (BINFO_VIRTUAL_P (binfo))
12807 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12809 if (access == access_public_node)
12810 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12811 else if (access == access_protected_node)
12812 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12815 /* Generate a DIE for a class member. */
12817 static void
12818 gen_member_die (tree type, dw_die_ref context_die)
12820 tree member;
12821 tree binfo = TYPE_BINFO (type);
12822 dw_die_ref child;
12824 /* If this is not an incomplete type, output descriptions of each of its
12825 members. Note that as we output the DIEs necessary to represent the
12826 members of this record or union type, we will also be trying to output
12827 DIEs to represent the *types* of those members. However the `type'
12828 function (above) will specifically avoid generating type DIEs for member
12829 types *within* the list of member DIEs for this (containing) type except
12830 for those types (of members) which are explicitly marked as also being
12831 members of this (containing) type themselves. The g++ front- end can
12832 force any given type to be treated as a member of some other (containing)
12833 type by setting the TYPE_CONTEXT of the given (member) type to point to
12834 the TREE node representing the appropriate (containing) type. */
12836 /* First output info about the base classes. */
12837 if (binfo)
12839 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12840 int i;
12841 tree base;
12843 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12844 gen_inheritance_die (base,
12845 (accesses ? VEC_index (tree, accesses, i)
12846 : access_public_node), context_die);
12849 /* Now output info about the data members and type members. */
12850 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12852 /* If we thought we were generating minimal debug info for TYPE
12853 and then changed our minds, some of the member declarations
12854 may have already been defined. Don't define them again, but
12855 do put them in the right order. */
12857 child = lookup_decl_die (member);
12858 if (child)
12859 splice_child_die (context_die, child);
12860 else
12861 gen_decl_die (member, context_die);
12864 /* Now output info about the function members (if any). */
12865 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12867 /* Don't include clones in the member list. */
12868 if (DECL_ABSTRACT_ORIGIN (member))
12869 continue;
12871 child = lookup_decl_die (member);
12872 if (child)
12873 splice_child_die (context_die, child);
12874 else
12875 gen_decl_die (member, context_die);
12879 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
12880 is set, we pretend that the type was never defined, so we only get the
12881 member DIEs needed by later specification DIEs. */
12883 static void
12884 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
12885 enum debug_info_usage usage)
12887 dw_die_ref type_die = lookup_type_die (type);
12888 dw_die_ref scope_die = 0;
12889 int nested = 0;
12890 int complete = (TYPE_SIZE (type)
12891 && (! TYPE_STUB_DECL (type)
12892 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12893 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12894 complete = complete && should_emit_struct_debug (type, usage);
12896 if (type_die && ! complete)
12897 return;
12899 if (TYPE_CONTEXT (type) != NULL_TREE
12900 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12901 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12902 nested = 1;
12904 scope_die = scope_die_for (type, context_die);
12906 if (! type_die || (nested && scope_die == comp_unit_die))
12907 /* First occurrence of type or toplevel definition of nested class. */
12909 dw_die_ref old_die = type_die;
12911 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12912 ? DW_TAG_structure_type : DW_TAG_union_type,
12913 scope_die, type);
12914 equate_type_number_to_die (type, type_die);
12915 if (old_die)
12916 add_AT_specification (type_die, old_die);
12917 else
12918 add_name_attribute (type_die, type_tag (type));
12920 else
12921 remove_AT (type_die, DW_AT_declaration);
12923 /* If this type has been completed, then give it a byte_size attribute and
12924 then give a list of members. */
12925 if (complete && !ns_decl)
12927 /* Prevent infinite recursion in cases where the type of some member of
12928 this type is expressed in terms of this type itself. */
12929 TREE_ASM_WRITTEN (type) = 1;
12930 add_byte_size_attribute (type_die, type);
12931 if (TYPE_STUB_DECL (type) != NULL_TREE)
12932 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12934 /* If the first reference to this type was as the return type of an
12935 inline function, then it may not have a parent. Fix this now. */
12936 if (type_die->die_parent == NULL)
12937 add_child_die (scope_die, type_die);
12939 push_decl_scope (type);
12940 gen_member_die (type, type_die);
12941 pop_decl_scope ();
12943 /* GNU extension: Record what type our vtable lives in. */
12944 if (TYPE_VFIELD (type))
12946 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12948 gen_type_die (vtype, context_die);
12949 add_AT_die_ref (type_die, DW_AT_containing_type,
12950 lookup_type_die (vtype));
12953 else
12955 add_AT_flag (type_die, DW_AT_declaration, 1);
12957 /* We don't need to do this for function-local types. */
12958 if (TYPE_STUB_DECL (type)
12959 && ! decl_function_context (TYPE_STUB_DECL (type)))
12960 VEC_safe_push (tree, gc, incomplete_types, type);
12963 if (get_AT (type_die, DW_AT_name))
12964 add_pubtype (type, type_die);
12967 /* Generate a DIE for a subroutine _type_. */
12969 static void
12970 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12972 tree return_type = TREE_TYPE (type);
12973 dw_die_ref subr_die
12974 = new_die (DW_TAG_subroutine_type,
12975 scope_die_for (type, context_die), type);
12977 equate_type_number_to_die (type, subr_die);
12978 add_prototyped_attribute (subr_die, type);
12979 add_type_attribute (subr_die, return_type, 0, 0, context_die);
12980 gen_formal_types_die (type, subr_die);
12982 if (get_AT (subr_die, DW_AT_name))
12983 add_pubtype (type, subr_die);
12986 /* Generate a DIE for a type definition. */
12988 static void
12989 gen_typedef_die (tree decl, dw_die_ref context_die)
12991 dw_die_ref type_die;
12992 tree origin;
12994 if (TREE_ASM_WRITTEN (decl))
12995 return;
12997 TREE_ASM_WRITTEN (decl) = 1;
12998 type_die = new_die (DW_TAG_typedef, context_die, decl);
12999 origin = decl_ultimate_origin (decl);
13000 if (origin != NULL)
13001 add_abstract_origin_attribute (type_die, origin);
13002 else
13004 tree type;
13006 add_name_and_src_coords_attributes (type_die, decl);
13007 if (DECL_ORIGINAL_TYPE (decl))
13009 type = DECL_ORIGINAL_TYPE (decl);
13011 gcc_assert (type != TREE_TYPE (decl));
13012 equate_type_number_to_die (TREE_TYPE (decl), type_die);
13014 else
13015 type = TREE_TYPE (decl);
13017 add_type_attribute (type_die, type, TREE_READONLY (decl),
13018 TREE_THIS_VOLATILE (decl), context_die);
13021 if (DECL_ABSTRACT (decl))
13022 equate_decl_number_to_die (decl, type_die);
13024 if (get_AT (type_die, DW_AT_name))
13025 add_pubtype (decl, type_die);
13028 /* Generate a type description DIE. */
13030 static void
13031 gen_type_die_with_usage (tree type, dw_die_ref context_die,
13032 enum debug_info_usage usage)
13034 int need_pop;
13036 if (type == NULL_TREE || type == error_mark_node)
13037 return;
13039 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13040 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
13042 if (TREE_ASM_WRITTEN (type))
13043 return;
13045 /* Prevent broken recursion; we can't hand off to the same type. */
13046 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
13048 TREE_ASM_WRITTEN (type) = 1;
13049 gen_decl_die (TYPE_NAME (type), context_die);
13050 return;
13053 /* We are going to output a DIE to represent the unqualified version
13054 of this type (i.e. without any const or volatile qualifiers) so
13055 get the main variant (i.e. the unqualified version) of this type
13056 now. (Vectors are special because the debugging info is in the
13057 cloned type itself). */
13058 if (TREE_CODE (type) != VECTOR_TYPE)
13059 type = type_main_variant (type);
13061 if (TREE_ASM_WRITTEN (type))
13062 return;
13064 switch (TREE_CODE (type))
13066 case ERROR_MARK:
13067 break;
13069 case POINTER_TYPE:
13070 case REFERENCE_TYPE:
13071 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
13072 ensures that the gen_type_die recursion will terminate even if the
13073 type is recursive. Recursive types are possible in Ada. */
13074 /* ??? We could perhaps do this for all types before the switch
13075 statement. */
13076 TREE_ASM_WRITTEN (type) = 1;
13078 /* For these types, all that is required is that we output a DIE (or a
13079 set of DIEs) to represent the "basis" type. */
13080 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13081 DINFO_USAGE_IND_USE);
13082 break;
13084 case OFFSET_TYPE:
13085 /* This code is used for C++ pointer-to-data-member types.
13086 Output a description of the relevant class type. */
13087 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
13088 DINFO_USAGE_IND_USE);
13090 /* Output a description of the type of the object pointed to. */
13091 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13092 DINFO_USAGE_IND_USE);
13094 /* Now output a DIE to represent this pointer-to-data-member type
13095 itself. */
13096 gen_ptr_to_mbr_type_die (type, context_die);
13097 break;
13099 case FUNCTION_TYPE:
13100 /* Force out return type (in case it wasn't forced out already). */
13101 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13102 DINFO_USAGE_DIR_USE);
13103 gen_subroutine_type_die (type, context_die);
13104 break;
13106 case METHOD_TYPE:
13107 /* Force out return type (in case it wasn't forced out already). */
13108 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13109 DINFO_USAGE_DIR_USE);
13110 gen_subroutine_type_die (type, context_die);
13111 break;
13113 case ARRAY_TYPE:
13114 gen_array_type_die (type, context_die);
13115 break;
13117 case VECTOR_TYPE:
13118 gen_array_type_die (type, context_die);
13119 break;
13121 case ENUMERAL_TYPE:
13122 case RECORD_TYPE:
13123 case UNION_TYPE:
13124 case QUAL_UNION_TYPE:
13125 /* If this is a nested type whose containing class hasn't been written
13126 out yet, writing it out will cover this one, too. This does not apply
13127 to instantiations of member class templates; they need to be added to
13128 the containing class as they are generated. FIXME: This hurts the
13129 idea of combining type decls from multiple TUs, since we can't predict
13130 what set of template instantiations we'll get. */
13131 if (TYPE_CONTEXT (type)
13132 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13133 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
13135 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
13137 if (TREE_ASM_WRITTEN (type))
13138 return;
13140 /* If that failed, attach ourselves to the stub. */
13141 push_decl_scope (TYPE_CONTEXT (type));
13142 context_die = lookup_type_die (TYPE_CONTEXT (type));
13143 need_pop = 1;
13145 else
13147 declare_in_namespace (type, context_die);
13148 need_pop = 0;
13151 if (TREE_CODE (type) == ENUMERAL_TYPE)
13153 /* This might have been written out by the call to
13154 declare_in_namespace. */
13155 if (!TREE_ASM_WRITTEN (type))
13156 gen_enumeration_type_die (type, context_die);
13158 else
13159 gen_struct_or_union_type_die (type, context_die, usage);
13161 if (need_pop)
13162 pop_decl_scope ();
13164 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
13165 it up if it is ever completed. gen_*_type_die will set it for us
13166 when appropriate. */
13167 return;
13169 case VOID_TYPE:
13170 case INTEGER_TYPE:
13171 case REAL_TYPE:
13172 case FIXED_POINT_TYPE:
13173 case COMPLEX_TYPE:
13174 case BOOLEAN_TYPE:
13175 /* No DIEs needed for fundamental types. */
13176 break;
13178 case LANG_TYPE:
13179 /* No Dwarf representation currently defined. */
13180 break;
13182 default:
13183 gcc_unreachable ();
13186 TREE_ASM_WRITTEN (type) = 1;
13189 static void
13190 gen_type_die (tree type, dw_die_ref context_die)
13192 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
13195 /* Generate a DIE for a tagged type instantiation. */
13197 static void
13198 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
13200 if (type == NULL_TREE || type == error_mark_node)
13201 return;
13203 /* We are going to output a DIE to represent the unqualified version of
13204 this type (i.e. without any const or volatile qualifiers) so make sure
13205 that we have the main variant (i.e. the unqualified version) of this
13206 type now. */
13207 gcc_assert (type == type_main_variant (type));
13209 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
13210 an instance of an unresolved type. */
13212 switch (TREE_CODE (type))
13214 case ERROR_MARK:
13215 break;
13217 case ENUMERAL_TYPE:
13218 gen_inlined_enumeration_type_die (type, context_die);
13219 break;
13221 case RECORD_TYPE:
13222 gen_inlined_structure_type_die (type, context_die);
13223 break;
13225 case UNION_TYPE:
13226 case QUAL_UNION_TYPE:
13227 gen_inlined_union_type_die (type, context_die);
13228 break;
13230 default:
13231 gcc_unreachable ();
13235 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
13236 things which are local to the given block. */
13238 static void
13239 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
13241 int must_output_die = 0;
13242 tree origin;
13243 tree decl;
13244 enum tree_code origin_code;
13246 /* Ignore blocks that are NULL. */
13247 if (stmt == NULL_TREE)
13248 return;
13250 /* If the block is one fragment of a non-contiguous block, do not
13251 process the variables, since they will have been done by the
13252 origin block. Do process subblocks. */
13253 if (BLOCK_FRAGMENT_ORIGIN (stmt))
13255 tree sub;
13257 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
13258 gen_block_die (sub, context_die, depth + 1);
13260 return;
13263 /* Determine the "ultimate origin" of this block. This block may be an
13264 inlined instance of an inlined instance of inline function, so we have
13265 to trace all of the way back through the origin chain to find out what
13266 sort of node actually served as the original seed for the creation of
13267 the current block. */
13268 origin = block_ultimate_origin (stmt);
13269 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13271 /* Determine if we need to output any Dwarf DIEs at all to represent this
13272 block. */
13273 if (origin_code == FUNCTION_DECL)
13274 /* The outer scopes for inlinings *must* always be represented. We
13275 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
13276 must_output_die = 1;
13277 else
13279 /* In the case where the current block represents an inlining of the
13280 "body block" of an inline function, we must *NOT* output any DIE for
13281 this block because we have already output a DIE to represent the whole
13282 inlined function scope and the "body block" of any function doesn't
13283 really represent a different scope according to ANSI C rules. So we
13284 check here to make sure that this block does not represent a "body
13285 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
13286 if (! is_body_block (origin ? origin : stmt))
13288 /* Determine if this block directly contains any "significant"
13289 local declarations which we will need to output DIEs for. */
13290 if (debug_info_level > DINFO_LEVEL_TERSE)
13291 /* We are not in terse mode so *any* local declaration counts
13292 as being a "significant" one. */
13293 must_output_die = (BLOCK_VARS (stmt) != NULL
13294 && (TREE_USED (stmt)
13295 || TREE_ASM_WRITTEN (stmt)
13296 || BLOCK_ABSTRACT (stmt)));
13297 else
13298 /* We are in terse mode, so only local (nested) function
13299 definitions count as "significant" local declarations. */
13300 for (decl = BLOCK_VARS (stmt);
13301 decl != NULL; decl = TREE_CHAIN (decl))
13302 if (TREE_CODE (decl) == FUNCTION_DECL
13303 && DECL_INITIAL (decl))
13305 must_output_die = 1;
13306 break;
13311 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13312 DIE for any block which contains no significant local declarations at
13313 all. Rather, in such cases we just call `decls_for_scope' so that any
13314 needed Dwarf info for any sub-blocks will get properly generated. Note
13315 that in terse mode, our definition of what constitutes a "significant"
13316 local declaration gets restricted to include only inlined function
13317 instances and local (nested) function definitions. */
13318 if (must_output_die)
13320 if (origin_code == FUNCTION_DECL)
13321 gen_inlined_subroutine_die (stmt, context_die, depth);
13322 else
13323 gen_lexical_block_die (stmt, context_die, depth);
13325 else
13326 decls_for_scope (stmt, context_die, depth);
13329 /* Generate all of the decls declared within a given scope and (recursively)
13330 all of its sub-blocks. */
13332 static void
13333 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13335 tree decl;
13336 tree subblocks;
13338 /* Ignore NULL blocks. */
13339 if (stmt == NULL_TREE)
13340 return;
13342 if (TREE_USED (stmt))
13344 /* Output the DIEs to represent all of the data objects and typedefs
13345 declared directly within this block but not within any nested
13346 sub-blocks. Also, nested function and tag DIEs have been
13347 generated with a parent of NULL; fix that up now. */
13348 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13350 dw_die_ref die;
13352 if (TREE_CODE (decl) == FUNCTION_DECL)
13353 die = lookup_decl_die (decl);
13354 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13355 die = lookup_type_die (TREE_TYPE (decl));
13356 else
13357 die = NULL;
13359 if (die != NULL && die->die_parent == NULL)
13360 add_child_die (context_die, die);
13361 /* Do not produce debug information for static variables since
13362 these might be optimized out. We are called for these later
13363 in varpool_analyze_pending_decls. */
13364 if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13366 else
13367 gen_decl_die (decl, context_die);
13371 /* If we're at -g1, we're not interested in subblocks. */
13372 if (debug_info_level <= DINFO_LEVEL_TERSE)
13373 return;
13375 /* Output the DIEs to represent all sub-blocks (and the items declared
13376 therein) of this block. */
13377 for (subblocks = BLOCK_SUBBLOCKS (stmt);
13378 subblocks != NULL;
13379 subblocks = BLOCK_CHAIN (subblocks))
13380 gen_block_die (subblocks, context_die, depth + 1);
13383 /* Is this a typedef we can avoid emitting? */
13385 static inline int
13386 is_redundant_typedef (const_tree decl)
13388 if (TYPE_DECL_IS_STUB (decl))
13389 return 1;
13391 if (DECL_ARTIFICIAL (decl)
13392 && DECL_CONTEXT (decl)
13393 && is_tagged_type (DECL_CONTEXT (decl))
13394 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13395 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13396 /* Also ignore the artificial member typedef for the class name. */
13397 return 1;
13399 return 0;
13402 /* Returns the DIE for decl. A DIE will always be returned. */
13404 static dw_die_ref
13405 force_decl_die (tree decl)
13407 dw_die_ref decl_die;
13408 unsigned saved_external_flag;
13409 tree save_fn = NULL_TREE;
13410 decl_die = lookup_decl_die (decl);
13411 if (!decl_die)
13413 dw_die_ref context_die;
13414 tree decl_context = DECL_CONTEXT (decl);
13415 if (decl_context)
13417 /* Find die that represents this context. */
13418 if (TYPE_P (decl_context))
13419 context_die = force_type_die (decl_context);
13420 else
13421 context_die = force_decl_die (decl_context);
13423 else
13424 context_die = comp_unit_die;
13426 decl_die = lookup_decl_die (decl);
13427 if (decl_die)
13428 return decl_die;
13430 switch (TREE_CODE (decl))
13432 case FUNCTION_DECL:
13433 /* Clear current_function_decl, so that gen_subprogram_die thinks
13434 that this is a declaration. At this point, we just want to force
13435 declaration die. */
13436 save_fn = current_function_decl;
13437 current_function_decl = NULL_TREE;
13438 gen_subprogram_die (decl, context_die);
13439 current_function_decl = save_fn;
13440 break;
13442 case VAR_DECL:
13443 /* Set external flag to force declaration die. Restore it after
13444 gen_decl_die() call. */
13445 saved_external_flag = DECL_EXTERNAL (decl);
13446 DECL_EXTERNAL (decl) = 1;
13447 gen_decl_die (decl, context_die);
13448 DECL_EXTERNAL (decl) = saved_external_flag;
13449 break;
13451 case NAMESPACE_DECL:
13452 dwarf2out_decl (decl);
13453 break;
13455 default:
13456 gcc_unreachable ();
13459 /* We should be able to find the DIE now. */
13460 if (!decl_die)
13461 decl_die = lookup_decl_die (decl);
13462 gcc_assert (decl_die);
13465 return decl_die;
13468 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
13469 always returned. */
13471 static dw_die_ref
13472 force_type_die (tree type)
13474 dw_die_ref type_die;
13476 type_die = lookup_type_die (type);
13477 if (!type_die)
13479 dw_die_ref context_die;
13480 if (TYPE_CONTEXT (type))
13482 if (TYPE_P (TYPE_CONTEXT (type)))
13483 context_die = force_type_die (TYPE_CONTEXT (type));
13484 else
13485 context_die = force_decl_die (TYPE_CONTEXT (type));
13487 else
13488 context_die = comp_unit_die;
13490 type_die = lookup_type_die (type);
13491 if (type_die)
13492 return type_die;
13493 gen_type_die (type, context_die);
13494 type_die = lookup_type_die (type);
13495 gcc_assert (type_die);
13497 return type_die;
13500 /* Force out any required namespaces to be able to output DECL,
13501 and return the new context_die for it, if it's changed. */
13503 static dw_die_ref
13504 setup_namespace_context (tree thing, dw_die_ref context_die)
13506 tree context = (DECL_P (thing)
13507 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13508 if (context && TREE_CODE (context) == NAMESPACE_DECL)
13509 /* Force out the namespace. */
13510 context_die = force_decl_die (context);
13512 return context_die;
13515 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13516 type) within its namespace, if appropriate.
13518 For compatibility with older debuggers, namespace DIEs only contain
13519 declarations; all definitions are emitted at CU scope. */
13521 static void
13522 declare_in_namespace (tree thing, dw_die_ref context_die)
13524 dw_die_ref ns_context;
13526 if (debug_info_level <= DINFO_LEVEL_TERSE)
13527 return;
13529 /* If this decl is from an inlined function, then don't try to emit it in its
13530 namespace, as we will get confused. It would have already been emitted
13531 when the abstract instance of the inline function was emitted anyways. */
13532 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13533 return;
13535 ns_context = setup_namespace_context (thing, context_die);
13537 if (ns_context != context_die)
13539 if (DECL_P (thing))
13540 gen_decl_die (thing, ns_context);
13541 else
13542 gen_type_die (thing, ns_context);
13546 /* Generate a DIE for a namespace or namespace alias. */
13548 static void
13549 gen_namespace_die (tree decl)
13551 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13553 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13554 they are an alias of. */
13555 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13557 /* Output a real namespace. */
13558 dw_die_ref namespace_die
13559 = new_die (DW_TAG_namespace, context_die, decl);
13560 add_name_and_src_coords_attributes (namespace_die, decl);
13561 equate_decl_number_to_die (decl, namespace_die);
13563 else
13565 /* Output a namespace alias. */
13567 /* Force out the namespace we are an alias of, if necessary. */
13568 dw_die_ref origin_die
13569 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13571 /* Now create the namespace alias DIE. */
13572 dw_die_ref namespace_die
13573 = new_die (DW_TAG_imported_declaration, context_die, decl);
13574 add_name_and_src_coords_attributes (namespace_die, decl);
13575 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13576 equate_decl_number_to_die (decl, namespace_die);
13580 /* Generate Dwarf debug information for a decl described by DECL. */
13582 static void
13583 gen_decl_die (tree decl, dw_die_ref context_die)
13585 tree origin;
13587 if (DECL_P (decl) && DECL_IGNORED_P (decl))
13588 return;
13590 switch (TREE_CODE (decl))
13592 case ERROR_MARK:
13593 break;
13595 case CONST_DECL:
13596 /* The individual enumerators of an enum type get output when we output
13597 the Dwarf representation of the relevant enum type itself. */
13598 break;
13600 case FUNCTION_DECL:
13601 /* Don't output any DIEs to represent mere function declarations,
13602 unless they are class members or explicit block externs. */
13603 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13604 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13605 break;
13607 #if 0
13608 /* FIXME */
13609 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13610 on local redeclarations of global functions. That seems broken. */
13611 if (current_function_decl != decl)
13612 /* This is only a declaration. */;
13613 #endif
13615 /* If we're emitting a clone, emit info for the abstract instance. */
13616 if (DECL_ORIGIN (decl) != decl)
13617 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13619 /* If we're emitting an out-of-line copy of an inline function,
13620 emit info for the abstract instance and set up to refer to it. */
13621 else if (cgraph_function_possibly_inlined_p (decl)
13622 && ! DECL_ABSTRACT (decl)
13623 && ! class_or_namespace_scope_p (context_die)
13624 /* dwarf2out_abstract_function won't emit a die if this is just
13625 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
13626 that case, because that works only if we have a die. */
13627 && DECL_INITIAL (decl) != NULL_TREE)
13629 dwarf2out_abstract_function (decl);
13630 set_decl_origin_self (decl);
13633 /* Otherwise we're emitting the primary DIE for this decl. */
13634 else if (debug_info_level > DINFO_LEVEL_TERSE)
13636 /* Before we describe the FUNCTION_DECL itself, make sure that we
13637 have described its return type. */
13638 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13640 /* And its virtual context. */
13641 if (DECL_VINDEX (decl) != NULL_TREE)
13642 gen_type_die (DECL_CONTEXT (decl), context_die);
13644 /* And its containing type. */
13645 origin = decl_class_context (decl);
13646 if (origin != NULL_TREE)
13647 gen_type_die_for_member (origin, decl, context_die);
13649 /* And its containing namespace. */
13650 declare_in_namespace (decl, context_die);
13653 /* Now output a DIE to represent the function itself. */
13654 gen_subprogram_die (decl, context_die);
13655 break;
13657 case TYPE_DECL:
13658 /* If we are in terse mode, don't generate any DIEs to represent any
13659 actual typedefs. */
13660 if (debug_info_level <= DINFO_LEVEL_TERSE)
13661 break;
13663 /* In the special case of a TYPE_DECL node representing the declaration
13664 of some type tag, if the given TYPE_DECL is marked as having been
13665 instantiated from some other (original) TYPE_DECL node (e.g. one which
13666 was generated within the original definition of an inline function) we
13667 have to generate a special (abbreviated) DW_TAG_structure_type,
13668 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
13669 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
13670 && is_tagged_type (TREE_TYPE (decl)))
13672 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13673 break;
13676 if (is_redundant_typedef (decl))
13677 gen_type_die (TREE_TYPE (decl), context_die);
13678 else
13679 /* Output a DIE to represent the typedef itself. */
13680 gen_typedef_die (decl, context_die);
13681 break;
13683 case LABEL_DECL:
13684 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13685 gen_label_die (decl, context_die);
13686 break;
13688 case VAR_DECL:
13689 case RESULT_DECL:
13690 /* If we are in terse mode, don't generate any DIEs to represent any
13691 variable declarations or definitions. */
13692 if (debug_info_level <= DINFO_LEVEL_TERSE)
13693 break;
13695 /* Output any DIEs that are needed to specify the type of this data
13696 object. */
13697 gen_type_die (TREE_TYPE (decl), context_die);
13699 /* And its containing type. */
13700 origin = decl_class_context (decl);
13701 if (origin != NULL_TREE)
13702 gen_type_die_for_member (origin, decl, context_die);
13704 /* And its containing namespace. */
13705 declare_in_namespace (decl, context_die);
13707 /* Now output the DIE to represent the data object itself. This gets
13708 complicated because of the possibility that the VAR_DECL really
13709 represents an inlined instance of a formal parameter for an inline
13710 function. */
13711 origin = decl_ultimate_origin (decl);
13712 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13713 gen_formal_parameter_die (decl, context_die);
13714 else
13715 gen_variable_die (decl, context_die);
13716 break;
13718 case FIELD_DECL:
13719 /* Ignore the nameless fields that are used to skip bits but handle C++
13720 anonymous unions and structs. */
13721 if (DECL_NAME (decl) != NULL_TREE
13722 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13723 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13725 gen_type_die (member_declared_type (decl), context_die);
13726 gen_field_die (decl, context_die);
13728 break;
13730 case PARM_DECL:
13731 gen_type_die (TREE_TYPE (decl), context_die);
13732 gen_formal_parameter_die (decl, context_die);
13733 break;
13735 case NAMESPACE_DECL:
13736 gen_namespace_die (decl);
13737 break;
13739 default:
13740 /* Probably some frontend-internal decl. Assume we don't care. */
13741 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13742 break;
13746 /* Output debug information for global decl DECL. Called from toplev.c after
13747 compilation proper has finished. */
13749 static void
13750 dwarf2out_global_decl (tree decl)
13752 /* Output DWARF2 information for file-scope tentative data object
13753 declarations, file-scope (extern) function declarations (which had no
13754 corresponding body) and file-scope tagged type declarations and
13755 definitions which have not yet been forced out. */
13756 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13757 dwarf2out_decl (decl);
13760 /* Output debug information for type decl DECL. Called from toplev.c
13761 and from language front ends (to record built-in types). */
13762 static void
13763 dwarf2out_type_decl (tree decl, int local)
13765 if (!local)
13766 dwarf2out_decl (decl);
13769 /* Output debug information for imported module or decl. */
13771 static void
13772 dwarf2out_imported_module_or_decl (tree decl, tree context)
13774 dw_die_ref imported_die, at_import_die;
13775 dw_die_ref scope_die;
13776 expanded_location xloc;
13778 if (debug_info_level <= DINFO_LEVEL_TERSE)
13779 return;
13781 gcc_assert (decl);
13783 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13784 We need decl DIE for reference and scope die. First, get DIE for the decl
13785 itself. */
13787 /* Get the scope die for decl context. Use comp_unit_die for global module
13788 or decl. If die is not found for non globals, force new die. */
13789 if (!context)
13790 scope_die = comp_unit_die;
13791 else if (TYPE_P (context))
13793 if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
13794 return;
13795 scope_die = force_type_die (context);
13797 else
13798 scope_die = force_decl_die (context);
13800 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
13801 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13803 if (is_base_type (TREE_TYPE (decl)))
13804 at_import_die = base_type_die (TREE_TYPE (decl));
13805 else
13806 at_import_die = force_type_die (TREE_TYPE (decl));
13808 else
13810 at_import_die = lookup_decl_die (decl);
13811 if (!at_import_die)
13813 /* If we're trying to avoid duplicate debug info, we may not have
13814 emitted the member decl for this field. Emit it now. */
13815 if (TREE_CODE (decl) == FIELD_DECL)
13817 tree type = DECL_CONTEXT (decl);
13818 dw_die_ref type_context_die;
13820 if (TYPE_CONTEXT (type))
13821 if (TYPE_P (TYPE_CONTEXT (type)))
13823 if (!should_emit_struct_debug (TYPE_CONTEXT (type),
13824 DINFO_USAGE_DIR_USE))
13825 return;
13826 type_context_die = force_type_die (TYPE_CONTEXT (type));
13828 else
13829 type_context_die = force_decl_die (TYPE_CONTEXT (type));
13830 else
13831 type_context_die = comp_unit_die;
13832 gen_type_die_for_member (type, decl, type_context_die);
13834 at_import_die = force_decl_die (decl);
13838 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
13839 if (TREE_CODE (decl) == NAMESPACE_DECL)
13840 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13841 else
13842 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13844 xloc = expand_location (input_location);
13845 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
13846 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13847 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13850 /* Write the debugging output for DECL. */
13852 void
13853 dwarf2out_decl (tree decl)
13855 dw_die_ref context_die = comp_unit_die;
13857 switch (TREE_CODE (decl))
13859 case ERROR_MARK:
13860 return;
13862 case FUNCTION_DECL:
13863 /* What we would really like to do here is to filter out all mere
13864 file-scope declarations of file-scope functions which are never
13865 referenced later within this translation unit (and keep all of ones
13866 that *are* referenced later on) but we aren't clairvoyant, so we have
13867 no idea which functions will be referenced in the future (i.e. later
13868 on within the current translation unit). So here we just ignore all
13869 file-scope function declarations which are not also definitions. If
13870 and when the debugger needs to know something about these functions,
13871 it will have to hunt around and find the DWARF information associated
13872 with the definition of the function.
13874 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13875 nodes represent definitions and which ones represent mere
13876 declarations. We have to check DECL_INITIAL instead. That's because
13877 the C front-end supports some weird semantics for "extern inline"
13878 function definitions. These can get inlined within the current
13879 translation unit (and thus, we need to generate Dwarf info for their
13880 abstract instances so that the Dwarf info for the concrete inlined
13881 instances can have something to refer to) but the compiler never
13882 generates any out-of-lines instances of such things (despite the fact
13883 that they *are* definitions).
13885 The important point is that the C front-end marks these "extern
13886 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13887 them anyway. Note that the C++ front-end also plays some similar games
13888 for inline function definitions appearing within include files which
13889 also contain `#pragma interface' pragmas. */
13890 if (DECL_INITIAL (decl) == NULL_TREE)
13891 return;
13893 /* If we're a nested function, initially use a parent of NULL; if we're
13894 a plain function, this will be fixed up in decls_for_scope. If
13895 we're a method, it will be ignored, since we already have a DIE. */
13896 if (decl_function_context (decl)
13897 /* But if we're in terse mode, we don't care about scope. */
13898 && debug_info_level > DINFO_LEVEL_TERSE)
13899 context_die = NULL;
13900 break;
13902 case VAR_DECL:
13903 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13904 declaration and if the declaration was never even referenced from
13905 within this entire compilation unit. We suppress these DIEs in
13906 order to save space in the .debug section (by eliminating entries
13907 which are probably useless). Note that we must not suppress
13908 block-local extern declarations (whether used or not) because that
13909 would screw-up the debugger's name lookup mechanism and cause it to
13910 miss things which really ought to be in scope at a given point. */
13911 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13912 return;
13914 /* For local statics lookup proper context die. */
13915 if (TREE_STATIC (decl) && decl_function_context (decl))
13916 context_die = lookup_decl_die (DECL_CONTEXT (decl));
13918 /* If we are in terse mode, don't generate any DIEs to represent any
13919 variable declarations or definitions. */
13920 if (debug_info_level <= DINFO_LEVEL_TERSE)
13921 return;
13922 break;
13924 case NAMESPACE_DECL:
13925 if (debug_info_level <= DINFO_LEVEL_TERSE)
13926 return;
13927 if (lookup_decl_die (decl) != NULL)
13928 return;
13929 break;
13931 case TYPE_DECL:
13932 /* Don't emit stubs for types unless they are needed by other DIEs. */
13933 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13934 return;
13936 /* Don't bother trying to generate any DIEs to represent any of the
13937 normal built-in types for the language we are compiling. */
13938 if (DECL_IS_BUILTIN (decl))
13940 /* OK, we need to generate one for `bool' so GDB knows what type
13941 comparisons have. */
13942 if (is_cxx ()
13943 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13944 && ! DECL_IGNORED_P (decl))
13945 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13947 return;
13950 /* If we are in terse mode, don't generate any DIEs for types. */
13951 if (debug_info_level <= DINFO_LEVEL_TERSE)
13952 return;
13954 /* If we're a function-scope tag, initially use a parent of NULL;
13955 this will be fixed up in decls_for_scope. */
13956 if (decl_function_context (decl))
13957 context_die = NULL;
13959 break;
13961 default:
13962 return;
13965 gen_decl_die (decl, context_die);
13968 /* Output a marker (i.e. a label) for the beginning of the generated code for
13969 a lexical block. */
13971 static void
13972 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13973 unsigned int blocknum)
13975 switch_to_section (current_function_section ());
13976 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13979 /* Output a marker (i.e. a label) for the end of the generated code for a
13980 lexical block. */
13982 static void
13983 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13985 switch_to_section (current_function_section ());
13986 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13989 /* Returns nonzero if it is appropriate not to emit any debugging
13990 information for BLOCK, because it doesn't contain any instructions.
13992 Don't allow this for blocks with nested functions or local classes
13993 as we would end up with orphans, and in the presence of scheduling
13994 we may end up calling them anyway. */
13996 static bool
13997 dwarf2out_ignore_block (const_tree block)
13999 tree decl;
14001 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
14002 if (TREE_CODE (decl) == FUNCTION_DECL
14003 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
14004 return 0;
14006 return 1;
14009 /* Hash table routines for file_hash. */
14011 static int
14012 file_table_eq (const void *p1_p, const void *p2_p)
14014 const struct dwarf_file_data * p1 = p1_p;
14015 const char * p2 = p2_p;
14016 return strcmp (p1->filename, p2) == 0;
14019 static hashval_t
14020 file_table_hash (const void *p_p)
14022 const struct dwarf_file_data * p = p_p;
14023 return htab_hash_string (p->filename);
14026 /* Lookup FILE_NAME (in the list of filenames that we know about here in
14027 dwarf2out.c) and return its "index". The index of each (known) filename is
14028 just a unique number which is associated with only that one filename. We
14029 need such numbers for the sake of generating labels (in the .debug_sfnames
14030 section) and references to those files numbers (in the .debug_srcinfo
14031 and.debug_macinfo sections). If the filename given as an argument is not
14032 found in our current list, add it to the list and assign it the next
14033 available unique index number. In order to speed up searches, we remember
14034 the index of the filename was looked up last. This handles the majority of
14035 all searches. */
14037 static struct dwarf_file_data *
14038 lookup_filename (const char *file_name)
14040 void ** slot;
14041 struct dwarf_file_data * created;
14043 /* Check to see if the file name that was searched on the previous
14044 call matches this file name. If so, return the index. */
14045 if (file_table_last_lookup
14046 && (file_name == file_table_last_lookup->filename
14047 || strcmp (file_table_last_lookup->filename, file_name) == 0))
14048 return file_table_last_lookup;
14050 /* Didn't match the previous lookup, search the table. */
14051 slot = htab_find_slot_with_hash (file_table, file_name,
14052 htab_hash_string (file_name), INSERT);
14053 if (*slot)
14054 return *slot;
14056 created = ggc_alloc (sizeof (struct dwarf_file_data));
14057 created->filename = file_name;
14058 created->emitted_number = 0;
14059 *slot = created;
14060 return created;
14063 /* If the assembler will construct the file table, then translate the compiler
14064 internal file table number into the assembler file table number, and emit
14065 a .file directive if we haven't already emitted one yet. The file table
14066 numbers are different because we prune debug info for unused variables and
14067 types, which may include filenames. */
14069 static int
14070 maybe_emit_file (struct dwarf_file_data * fd)
14072 if (! fd->emitted_number)
14074 if (last_emitted_file)
14075 fd->emitted_number = last_emitted_file->emitted_number + 1;
14076 else
14077 fd->emitted_number = 1;
14078 last_emitted_file = fd;
14080 if (DWARF2_ASM_LINE_DEBUG_INFO)
14082 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
14083 output_quoted_string (asm_out_file,
14084 remap_debug_filename (fd->filename));
14085 fputc ('\n', asm_out_file);
14089 return fd->emitted_number;
14092 /* Called by the final INSN scan whenever we see a var location. We
14093 use it to drop labels in the right places, and throw the location in
14094 our lookup table. */
14096 static void
14097 dwarf2out_var_location (rtx loc_note)
14099 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
14100 struct var_loc_node *newloc;
14101 rtx prev_insn;
14102 static rtx last_insn;
14103 static const char *last_label;
14104 tree decl;
14106 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
14107 return;
14108 prev_insn = PREV_INSN (loc_note);
14110 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
14111 /* If the insn we processed last time is the previous insn
14112 and it is also a var location note, use the label we emitted
14113 last time. */
14114 if (last_insn != NULL_RTX
14115 && last_insn == prev_insn
14116 && NOTE_P (prev_insn)
14117 && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
14119 newloc->label = last_label;
14121 else
14123 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
14124 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
14125 loclabel_num++;
14126 newloc->label = ggc_strdup (loclabel);
14128 newloc->var_loc_note = loc_note;
14129 newloc->next = NULL;
14131 if (cfun && in_cold_section_p)
14132 newloc->section_label = cfun->cold_section_label;
14133 else
14134 newloc->section_label = text_section_label;
14136 last_insn = loc_note;
14137 last_label = newloc->label;
14138 decl = NOTE_VAR_LOCATION_DECL (loc_note);
14139 add_var_loc_to_decl (decl, newloc);
14142 /* We need to reset the locations at the beginning of each
14143 function. We can't do this in the end_function hook, because the
14144 declarations that use the locations won't have been output when
14145 that hook is called. Also compute have_multiple_function_sections here. */
14147 static void
14148 dwarf2out_begin_function (tree fun)
14150 htab_empty (decl_loc_table);
14152 if (function_section (fun) != text_section)
14153 have_multiple_function_sections = true;
14155 dwarf2out_note_section_used ();
14158 /* Output a label to mark the beginning of a source code line entry
14159 and record information relating to this source line, in
14160 'line_info_table' for later output of the .debug_line section. */
14162 static void
14163 dwarf2out_source_line (unsigned int line, const char *filename)
14165 if (debug_info_level >= DINFO_LEVEL_NORMAL
14166 && line != 0)
14168 int file_num = maybe_emit_file (lookup_filename (filename));
14170 switch_to_section (current_function_section ());
14172 /* If requested, emit something human-readable. */
14173 if (flag_debug_asm)
14174 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
14175 filename, line);
14177 if (DWARF2_ASM_LINE_DEBUG_INFO)
14179 /* Emit the .loc directive understood by GNU as. */
14180 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
14182 /* Indicate that line number info exists. */
14183 line_info_table_in_use++;
14185 else if (function_section (current_function_decl) != text_section)
14187 dw_separate_line_info_ref line_info;
14188 targetm.asm_out.internal_label (asm_out_file,
14189 SEPARATE_LINE_CODE_LABEL,
14190 separate_line_info_table_in_use);
14192 /* Expand the line info table if necessary. */
14193 if (separate_line_info_table_in_use
14194 == separate_line_info_table_allocated)
14196 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14197 separate_line_info_table
14198 = ggc_realloc (separate_line_info_table,
14199 separate_line_info_table_allocated
14200 * sizeof (dw_separate_line_info_entry));
14201 memset (separate_line_info_table
14202 + separate_line_info_table_in_use,
14204 (LINE_INFO_TABLE_INCREMENT
14205 * sizeof (dw_separate_line_info_entry)));
14208 /* Add the new entry at the end of the line_info_table. */
14209 line_info
14210 = &separate_line_info_table[separate_line_info_table_in_use++];
14211 line_info->dw_file_num = file_num;
14212 line_info->dw_line_num = line;
14213 line_info->function = current_function_funcdef_no;
14215 else
14217 dw_line_info_ref line_info;
14219 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
14220 line_info_table_in_use);
14222 /* Expand the line info table if necessary. */
14223 if (line_info_table_in_use == line_info_table_allocated)
14225 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14226 line_info_table
14227 = ggc_realloc (line_info_table,
14228 (line_info_table_allocated
14229 * sizeof (dw_line_info_entry)));
14230 memset (line_info_table + line_info_table_in_use, 0,
14231 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
14234 /* Add the new entry at the end of the line_info_table. */
14235 line_info = &line_info_table[line_info_table_in_use++];
14236 line_info->dw_file_num = file_num;
14237 line_info->dw_line_num = line;
14242 /* Record the beginning of a new source file. */
14244 static void
14245 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
14247 if (flag_eliminate_dwarf2_dups)
14249 /* Record the beginning of the file for break_out_includes. */
14250 dw_die_ref bincl_die;
14252 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
14253 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
14256 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14258 int file_num = maybe_emit_file (lookup_filename (filename));
14260 switch_to_section (debug_macinfo_section);
14261 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
14262 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
14263 lineno);
14265 dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
14269 /* Record the end of a source file. */
14271 static void
14272 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
14274 if (flag_eliminate_dwarf2_dups)
14275 /* Record the end of the file for break_out_includes. */
14276 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
14278 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14280 switch_to_section (debug_macinfo_section);
14281 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14285 /* Called from debug_define in toplev.c. The `buffer' parameter contains
14286 the tail part of the directive line, i.e. the part which is past the
14287 initial whitespace, #, whitespace, directive-name, whitespace part. */
14289 static void
14290 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14291 const char *buffer ATTRIBUTE_UNUSED)
14293 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14295 switch_to_section (debug_macinfo_section);
14296 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14297 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14298 dw2_asm_output_nstring (buffer, -1, "The macro");
14302 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
14303 the tail part of the directive line, i.e. the part which is past the
14304 initial whitespace, #, whitespace, directive-name, whitespace part. */
14306 static void
14307 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14308 const char *buffer ATTRIBUTE_UNUSED)
14310 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14312 switch_to_section (debug_macinfo_section);
14313 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14314 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14315 dw2_asm_output_nstring (buffer, -1, "The macro");
14319 /* Set up for Dwarf output at the start of compilation. */
14321 static void
14322 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14324 /* Allocate the file_table. */
14325 file_table = htab_create_ggc (50, file_table_hash,
14326 file_table_eq, NULL);
14328 /* Allocate the decl_die_table. */
14329 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14330 decl_die_table_eq, NULL);
14332 /* Allocate the decl_loc_table. */
14333 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14334 decl_loc_table_eq, NULL);
14336 /* Allocate the initial hunk of the decl_scope_table. */
14337 decl_scope_table = VEC_alloc (tree, gc, 256);
14339 /* Allocate the initial hunk of the abbrev_die_table. */
14340 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14341 * sizeof (dw_die_ref));
14342 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14343 /* Zero-th entry is allocated, but unused. */
14344 abbrev_die_table_in_use = 1;
14346 /* Allocate the initial hunk of the line_info_table. */
14347 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14348 * sizeof (dw_line_info_entry));
14349 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14351 /* Zero-th entry is allocated, but unused. */
14352 line_info_table_in_use = 1;
14354 /* Allocate the pubtypes and pubnames vectors. */
14355 pubname_table = VEC_alloc (pubname_entry, gc, 32);
14356 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14358 /* Generate the initial DIE for the .debug section. Note that the (string)
14359 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14360 will (typically) be a relative pathname and that this pathname should be
14361 taken as being relative to the directory from which the compiler was
14362 invoked when the given (base) source file was compiled. We will fill
14363 in this value in dwarf2out_finish. */
14364 comp_unit_die = gen_compile_unit_die (NULL);
14366 incomplete_types = VEC_alloc (tree, gc, 64);
14368 used_rtx_array = VEC_alloc (rtx, gc, 32);
14370 debug_info_section = get_section (DEBUG_INFO_SECTION,
14371 SECTION_DEBUG, NULL);
14372 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14373 SECTION_DEBUG, NULL);
14374 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14375 SECTION_DEBUG, NULL);
14376 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14377 SECTION_DEBUG, NULL);
14378 debug_line_section = get_section (DEBUG_LINE_SECTION,
14379 SECTION_DEBUG, NULL);
14380 debug_loc_section = get_section (DEBUG_LOC_SECTION,
14381 SECTION_DEBUG, NULL);
14382 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14383 SECTION_DEBUG, NULL);
14384 #ifdef DEBUG_PUBTYPES_SECTION
14385 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14386 SECTION_DEBUG, NULL);
14387 #endif
14388 debug_str_section = get_section (DEBUG_STR_SECTION,
14389 DEBUG_STR_SECTION_FLAGS, NULL);
14390 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14391 SECTION_DEBUG, NULL);
14392 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14393 SECTION_DEBUG, NULL);
14395 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14396 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14397 DEBUG_ABBREV_SECTION_LABEL, 0);
14398 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14399 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14400 COLD_TEXT_SECTION_LABEL, 0);
14401 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14403 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14404 DEBUG_INFO_SECTION_LABEL, 0);
14405 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14406 DEBUG_LINE_SECTION_LABEL, 0);
14407 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14408 DEBUG_RANGES_SECTION_LABEL, 0);
14409 switch_to_section (debug_abbrev_section);
14410 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14411 switch_to_section (debug_info_section);
14412 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14413 switch_to_section (debug_line_section);
14414 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14416 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14418 switch_to_section (debug_macinfo_section);
14419 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14420 DEBUG_MACINFO_SECTION_LABEL, 0);
14421 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14424 switch_to_section (text_section);
14425 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14426 if (flag_reorder_blocks_and_partition)
14428 cold_text_section = unlikely_text_section ();
14429 switch_to_section (cold_text_section);
14430 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14434 /* A helper function for dwarf2out_finish called through
14435 ht_forall. Emit one queued .debug_str string. */
14437 static int
14438 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14440 struct indirect_string_node *node = (struct indirect_string_node *) *h;
14442 if (node->form == DW_FORM_strp)
14444 switch_to_section (debug_str_section);
14445 ASM_OUTPUT_LABEL (asm_out_file, node->label);
14446 assemble_string (node->str, strlen (node->str) + 1);
14449 return 1;
14452 #if ENABLE_ASSERT_CHECKING
14453 /* Verify that all marks are clear. */
14455 static void
14456 verify_marks_clear (dw_die_ref die)
14458 dw_die_ref c;
14460 gcc_assert (! die->die_mark);
14461 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14463 #endif /* ENABLE_ASSERT_CHECKING */
14465 /* Clear the marks for a die and its children.
14466 Be cool if the mark isn't set. */
14468 static void
14469 prune_unmark_dies (dw_die_ref die)
14471 dw_die_ref c;
14473 if (die->die_mark)
14474 die->die_mark = 0;
14475 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14478 /* Given DIE that we're marking as used, find any other dies
14479 it references as attributes and mark them as used. */
14481 static void
14482 prune_unused_types_walk_attribs (dw_die_ref die)
14484 dw_attr_ref a;
14485 unsigned ix;
14487 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14489 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14491 /* A reference to another DIE.
14492 Make sure that it will get emitted. */
14493 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14495 /* Set the string's refcount to 0 so that prune_unused_types_mark
14496 accounts properly for it. */
14497 if (AT_class (a) == dw_val_class_str)
14498 a->dw_attr_val.v.val_str->refcount = 0;
14503 /* Mark DIE as being used. If DOKIDS is true, then walk down
14504 to DIE's children. */
14506 static void
14507 prune_unused_types_mark (dw_die_ref die, int dokids)
14509 dw_die_ref c;
14511 if (die->die_mark == 0)
14513 /* We haven't done this node yet. Mark it as used. */
14514 die->die_mark = 1;
14516 /* We also have to mark its parents as used.
14517 (But we don't want to mark our parents' kids due to this.) */
14518 if (die->die_parent)
14519 prune_unused_types_mark (die->die_parent, 0);
14521 /* Mark any referenced nodes. */
14522 prune_unused_types_walk_attribs (die);
14524 /* If this node is a specification,
14525 also mark the definition, if it exists. */
14526 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14527 prune_unused_types_mark (die->die_definition, 1);
14530 if (dokids && die->die_mark != 2)
14532 /* We need to walk the children, but haven't done so yet.
14533 Remember that we've walked the kids. */
14534 die->die_mark = 2;
14536 /* If this is an array type, we need to make sure our
14537 kids get marked, even if they're types. */
14538 if (die->die_tag == DW_TAG_array_type)
14539 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14540 else
14541 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14546 /* Walk the tree DIE and mark types that we actually use. */
14548 static void
14549 prune_unused_types_walk (dw_die_ref die)
14551 dw_die_ref c;
14553 /* Don't do anything if this node is already marked. */
14554 if (die->die_mark)
14555 return;
14557 switch (die->die_tag)
14559 case DW_TAG_const_type:
14560 case DW_TAG_packed_type:
14561 case DW_TAG_pointer_type:
14562 case DW_TAG_reference_type:
14563 case DW_TAG_volatile_type:
14564 case DW_TAG_typedef:
14565 case DW_TAG_array_type:
14566 case DW_TAG_structure_type:
14567 case DW_TAG_union_type:
14568 case DW_TAG_class_type:
14569 case DW_TAG_friend:
14570 case DW_TAG_variant_part:
14571 case DW_TAG_enumeration_type:
14572 case DW_TAG_subroutine_type:
14573 case DW_TAG_string_type:
14574 case DW_TAG_set_type:
14575 case DW_TAG_subrange_type:
14576 case DW_TAG_ptr_to_member_type:
14577 case DW_TAG_file_type:
14578 if (die->die_perennial_p)
14579 break;
14581 /* It's a type node --- don't mark it. */
14582 return;
14584 default:
14585 /* Mark everything else. */
14586 break;
14589 die->die_mark = 1;
14591 /* Now, mark any dies referenced from here. */
14592 prune_unused_types_walk_attribs (die);
14594 /* Mark children. */
14595 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14598 /* Increment the string counts on strings referred to from DIE's
14599 attributes. */
14601 static void
14602 prune_unused_types_update_strings (dw_die_ref die)
14604 dw_attr_ref a;
14605 unsigned ix;
14607 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14608 if (AT_class (a) == dw_val_class_str)
14610 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14611 s->refcount++;
14612 /* Avoid unnecessarily putting strings that are used less than
14613 twice in the hash table. */
14614 if (s->refcount
14615 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
14617 void ** slot;
14618 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14619 htab_hash_string (s->str),
14620 INSERT);
14621 gcc_assert (*slot == NULL);
14622 *slot = s;
14627 /* Remove from the tree DIE any dies that aren't marked. */
14629 static void
14630 prune_unused_types_prune (dw_die_ref die)
14632 dw_die_ref c;
14634 gcc_assert (die->die_mark);
14635 prune_unused_types_update_strings (die);
14637 if (! die->die_child)
14638 return;
14640 c = die->die_child;
14641 do {
14642 dw_die_ref prev = c;
14643 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14644 if (c == die->die_child)
14646 /* No marked children between 'prev' and the end of the list. */
14647 if (prev == c)
14648 /* No marked children at all. */
14649 die->die_child = NULL;
14650 else
14652 prev->die_sib = c->die_sib;
14653 die->die_child = prev;
14655 return;
14658 if (c != prev->die_sib)
14659 prev->die_sib = c;
14660 prune_unused_types_prune (c);
14661 } while (c != die->die_child);
14665 /* Remove dies representing declarations that we never use. */
14667 static void
14668 prune_unused_types (void)
14670 unsigned int i;
14671 limbo_die_node *node;
14672 pubname_ref pub;
14674 #if ENABLE_ASSERT_CHECKING
14675 /* All the marks should already be clear. */
14676 verify_marks_clear (comp_unit_die);
14677 for (node = limbo_die_list; node; node = node->next)
14678 verify_marks_clear (node->die);
14679 #endif /* ENABLE_ASSERT_CHECKING */
14681 /* Set the mark on nodes that are actually used. */
14682 prune_unused_types_walk (comp_unit_die);
14683 for (node = limbo_die_list; node; node = node->next)
14684 prune_unused_types_walk (node->die);
14686 /* Also set the mark on nodes referenced from the
14687 pubname_table or arange_table. */
14688 for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14689 prune_unused_types_mark (pub->die, 1);
14690 for (i = 0; i < arange_table_in_use; i++)
14691 prune_unused_types_mark (arange_table[i], 1);
14693 /* Get rid of nodes that aren't marked; and update the string counts. */
14694 if (debug_str_hash)
14695 htab_empty (debug_str_hash);
14696 prune_unused_types_prune (comp_unit_die);
14697 for (node = limbo_die_list; node; node = node->next)
14698 prune_unused_types_prune (node->die);
14700 /* Leave the marks clear. */
14701 prune_unmark_dies (comp_unit_die);
14702 for (node = limbo_die_list; node; node = node->next)
14703 prune_unmark_dies (node->die);
14706 /* Set the parameter to true if there are any relative pathnames in
14707 the file table. */
14708 static int
14709 file_table_relative_p (void ** slot, void *param)
14711 bool *p = param;
14712 struct dwarf_file_data *d = *slot;
14713 if (!IS_ABSOLUTE_PATH (d->filename))
14715 *p = true;
14716 return 0;
14718 return 1;
14721 /* Output stuff that dwarf requires at the end of every file,
14722 and generate the DWARF-2 debugging info. */
14724 static void
14725 dwarf2out_finish (const char *filename)
14727 limbo_die_node *node, *next_node;
14728 dw_die_ref die = 0;
14730 /* Add the name for the main input file now. We delayed this from
14731 dwarf2out_init to avoid complications with PCH. */
14732 add_name_attribute (comp_unit_die, remap_debug_filename (filename));
14733 if (!IS_ABSOLUTE_PATH (filename))
14734 add_comp_dir_attribute (comp_unit_die);
14735 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14737 bool p = false;
14738 htab_traverse (file_table, file_table_relative_p, &p);
14739 if (p)
14740 add_comp_dir_attribute (comp_unit_die);
14743 /* Traverse the limbo die list, and add parent/child links. The only
14744 dies without parents that should be here are concrete instances of
14745 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
14746 For concrete instances, we can get the parent die from the abstract
14747 instance. */
14748 for (node = limbo_die_list; node; node = next_node)
14750 next_node = node->next;
14751 die = node->die;
14753 if (die->die_parent == NULL)
14755 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
14757 if (origin)
14758 add_child_die (origin->die_parent, die);
14759 else if (die == comp_unit_die)
14761 else if (errorcount > 0 || sorrycount > 0)
14762 /* It's OK to be confused by errors in the input. */
14763 add_child_die (comp_unit_die, die);
14764 else
14766 /* In certain situations, the lexical block containing a
14767 nested function can be optimized away, which results
14768 in the nested function die being orphaned. Likewise
14769 with the return type of that nested function. Force
14770 this to be a child of the containing function.
14772 It may happen that even the containing function got fully
14773 inlined and optimized out. In that case we are lost and
14774 assign the empty child. This should not be big issue as
14775 the function is likely unreachable too. */
14776 tree context = NULL_TREE;
14778 gcc_assert (node->created_for);
14780 if (DECL_P (node->created_for))
14781 context = DECL_CONTEXT (node->created_for);
14782 else if (TYPE_P (node->created_for))
14783 context = TYPE_CONTEXT (node->created_for);
14785 gcc_assert (context
14786 && (TREE_CODE (context) == FUNCTION_DECL
14787 || TREE_CODE (context) == NAMESPACE_DECL));
14789 origin = lookup_decl_die (context);
14790 if (origin)
14791 add_child_die (origin, die);
14792 else
14793 add_child_die (comp_unit_die, die);
14798 limbo_die_list = NULL;
14800 /* Walk through the list of incomplete types again, trying once more to
14801 emit full debugging info for them. */
14802 retry_incomplete_types ();
14804 if (flag_eliminate_unused_debug_types)
14805 prune_unused_types ();
14807 /* Generate separate CUs for each of the include files we've seen.
14808 They will go into limbo_die_list. */
14809 if (flag_eliminate_dwarf2_dups)
14810 break_out_includes (comp_unit_die);
14812 /* Traverse the DIE's and add add sibling attributes to those DIE's
14813 that have children. */
14814 add_sibling_attributes (comp_unit_die);
14815 for (node = limbo_die_list; node; node = node->next)
14816 add_sibling_attributes (node->die);
14818 /* Output a terminator label for the .text section. */
14819 switch_to_section (text_section);
14820 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14821 if (flag_reorder_blocks_and_partition)
14823 switch_to_section (unlikely_text_section ());
14824 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14827 /* We can only use the low/high_pc attributes if all of the code was
14828 in .text. */
14829 if (!have_multiple_function_sections)
14831 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14832 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
14835 else
14837 unsigned fde_idx = 0;
14839 /* We need to give .debug_loc and .debug_ranges an appropriate
14840 "base address". Use zero so that these addresses become
14841 absolute. Historically, we've emitted the unexpected
14842 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
14843 Emit both to give time for other tools to adapt. */
14844 add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
14845 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14847 add_AT_range_list (comp_unit_die, DW_AT_ranges,
14848 add_ranges_by_labels (text_section_label,
14849 text_end_label));
14850 if (flag_reorder_blocks_and_partition)
14851 add_ranges_by_labels (cold_text_section_label,
14852 cold_end_label);
14854 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
14856 dw_fde_ref fde = &fde_table[fde_idx];
14858 if (fde->dw_fde_switched_sections)
14860 add_ranges_by_labels (fde->dw_fde_hot_section_label,
14861 fde->dw_fde_hot_section_end_label);
14862 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
14863 fde->dw_fde_unlikely_section_end_label);
14865 else
14866 add_ranges_by_labels (fde->dw_fde_begin,
14867 fde->dw_fde_end);
14870 add_ranges (NULL);
14873 /* Output location list section if necessary. */
14874 if (have_location_lists)
14876 /* Output the location lists info. */
14877 switch_to_section (debug_loc_section);
14878 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14879 DEBUG_LOC_SECTION_LABEL, 0);
14880 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14881 output_location_lists (die);
14884 if (debug_info_level >= DINFO_LEVEL_NORMAL)
14885 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
14886 debug_line_section_label);
14888 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14889 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
14891 /* Output all of the compilation units. We put the main one last so that
14892 the offsets are available to output_pubnames. */
14893 for (node = limbo_die_list; node; node = node->next)
14894 output_comp_unit (node->die, 0);
14896 output_comp_unit (comp_unit_die, 0);
14898 /* Output the abbreviation table. */
14899 switch_to_section (debug_abbrev_section);
14900 output_abbrev_section ();
14902 /* Output public names table if necessary. */
14903 if (!VEC_empty (pubname_entry, pubname_table))
14905 switch_to_section (debug_pubnames_section);
14906 output_pubnames (pubname_table);
14909 #ifdef DEBUG_PUBTYPES_SECTION
14910 /* Output public types table if necessary. */
14911 if (!VEC_empty (pubname_entry, pubtype_table))
14913 switch_to_section (debug_pubtypes_section);
14914 output_pubnames (pubtype_table);
14916 #endif
14918 /* Output the address range information. We only put functions in the arange
14919 table, so don't write it out if we don't have any. */
14920 if (fde_table_in_use)
14922 switch_to_section (debug_aranges_section);
14923 output_aranges ();
14926 /* Output ranges section if necessary. */
14927 if (ranges_table_in_use)
14929 switch_to_section (debug_ranges_section);
14930 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
14931 output_ranges ();
14934 /* Output the source line correspondence table. We must do this
14935 even if there is no line information. Otherwise, on an empty
14936 translation unit, we will generate a present, but empty,
14937 .debug_info section. IRIX 6.5 `nm' will then complain when
14938 examining the file. This is done late so that any filenames
14939 used by the debug_info section are marked as 'used'. */
14940 if (! DWARF2_ASM_LINE_DEBUG_INFO)
14942 switch_to_section (debug_line_section);
14943 output_line_info ();
14946 /* Have to end the macro section. */
14947 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14949 switch_to_section (debug_macinfo_section);
14950 dw2_asm_output_data (1, 0, "End compilation unit");
14953 /* If we emitted any DW_FORM_strp form attribute, output the string
14954 table too. */
14955 if (debug_str_hash)
14956 htab_traverse (debug_str_hash, output_indirect_string, NULL);
14958 #else
14960 /* This should never be used, but its address is needed for comparisons. */
14961 const struct gcc_debug_hooks dwarf2_debug_hooks;
14963 #endif /* DWARF2_DEBUGGING_INFO */
14965 #include "gt-dwarf2out.h"